Skip to content

Commit

Permalink
Update rust to support new databases/tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
swatanabe committed Oct 11, 2024
1 parent 5cf4f8d commit e1e40ae
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 16 deletions.
47 changes: 33 additions & 14 deletions rust/psibase/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ pub enum DbId {
/// The first 64 bits of the key match the service.
WriteOnly,

/// Data that is not part of consensus
///
/// Only accessible to subjective services during transactions,
/// but readable by all services during RPC. Doesn't undo
/// from aborting transactions, aborting blocks, or forking
/// blocks. Individual nodes may modify this database or wipe
// it entirely at will.
///
/// The first 64 bits of the key match the service.
Subjective,

/// Tables used by native code
///
/// This database enforces constraints during write. Only
Expand Down Expand Up @@ -125,12 +114,39 @@ pub enum DbId {
/// block signatures
BlockProof,

/// Not accessible to WASM. During joint consensus, this holds a
/// subset of native as of the last irreversible block. Outside
/// joint consensus, it is empty.
PrevAuthServices,

/// Number of defined databases
///
/// This number may grow in the future
NumDatabases,
NumChainDatabases,

/// Data that is not part of consensus
///
/// Only accessible to subjective services during transactions,
/// but readable by all services during RPC. Doesn't undo
/// from aborting transactions, aborting blocks, or forking
/// blocks. Individual nodes may modify this database or wipe
// it entirely at will.
///
/// The first 64 bits of the key match the service.
Subjective = BEGIN_INDEPENDENT,

/// Subjective tables used by native code
///
/// Not fully implemented yet and not available to services. Doesn't
/// undo from aborting transactions, aborting blocks, or forking
/// blocks.
NativeSubjective,

EndIndependent,
}

const BEGIN_INDEPENDENT: u32 = 64;

impl Pack for DbId {
const FIXED_SIZE: u32 = 4;

Expand All @@ -147,14 +163,17 @@ impl<'a> Unpack<'a> for DbId {

fn unpack(src: &'a [u8], pos: &mut u32) -> fracpack::Result<Self> {
let u32_form = u32::unpack(src, pos)?;
if u32_form >= DbId::NumDatabases as u32 {
if u32_form >= DbId::NumChainDatabases as u32
&& !(BEGIN_INDEPENDENT..(DbId::EndIndependent as u32)).contains(&u32_form)
{
return Err(fracpack::Error::BadEnumIndex);
}
Ok(unsafe { std::mem::transmute(u32_form) })
}

fn verify(src: &'a [u8], pos: &mut u32) -> fracpack::Result<()> {
u32::verify(src, pos)
DbId::unpack(src, pos)?;
Ok(())
}
}

Expand Down
99 changes: 97 additions & 2 deletions rust/psibase/src/native_tables.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(non_snake_case)]

use crate::{
AccountNumber, Action, BlockHeader, BlockInfo, Checksum256, DbId, Hex, JointConsensus, Pack,
ToSchema, Unpack,
AccountNumber, Action, BlockHeader, BlockInfo, BlockNum, Checksum256, Claim, DbId, Hex,
JointConsensus, Pack, ToSchema, Unpack,
};
use serde::{Deserialize, Serialize};

Expand All @@ -17,6 +17,10 @@ pub const TRANSACTION_WASM_CONFIG_TABLE: NativeTable = 5;
pub const PROOF_WASM_CONFIG_TABLE: NativeTable = 6; // Also for first auth
pub const CONFIG_TABLE: NativeTable = 7;
pub const NOTIFY_TABLE: NativeTable = 8;
pub const BLOCK_DATA_TABLE: NativeTable = 9;
pub const CONSENSUS_CHANGE_TABLE: NativeTable = 10;
pub const SNAPSHOT_TABLE: NativeTable = 11;
pub const SCHEDULED_SNAPSHOT_TABLE: NativeTable = 12;

pub const NATIVE_TABLE_PRIMARY_INDEX: NativeIndex = 0;

Expand Down Expand Up @@ -161,3 +165,94 @@ impl NotifyRow {
(NOTIFY_TABLE, NATIVE_TABLE_PRIMARY_INDEX)
}
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct BlockDataRow {
pub blockId: Checksum256,
pub auxConsensusData: Option<Hex<Vec<u8>>>,
}

impl BlockDataRow {
pub const DB: DbId = DbId::NativeSubjective;
pub fn key(&self) -> (NativeTable, NativeIndex, Checksum256) {
(
BLOCK_DATA_TABLE,
NATIVE_TABLE_PRIMARY_INDEX,
self.blockId.clone(),
)
}
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct ConsensusChangeRow {
pub start: BlockNum,
pub commit: BlockNum,
pub end: BlockNum,
}

impl ConsensusChangeRow {
pub const DB: DbId = DbId::NativeSubjective;
pub fn key(&self) -> (NativeTable, NativeIndex, BlockNum) {
(
CONSENSUS_CHANGE_TABLE,
NATIVE_TABLE_PRIMARY_INDEX,
self.start,
)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct StateChecksum {
pub serviceRoot: Checksum256,
pub nativeRoot: Checksum256,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct StateSignature {
pub account: AccountNumber,
pub claim: Claim,
pub rawData: Hex<Vec<u8>>,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct SnapshotStateItem {
pub state: StateChecksum,
pub signatures: Vec<StateSignature>,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct SnapshotRow {
pub id: Checksum256,
pub state: Option<SnapshotStateItem>,
pub other: Vec<SnapshotStateItem>,
}

impl SnapshotRow {
pub const DB: DbId = DbId::NativeSubjective;
pub fn key(&self) -> (NativeTable, NativeIndex, Checksum256) {
(SNAPSHOT_TABLE, NATIVE_TABLE_PRIMARY_INDEX, self.id.clone())
}
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct ScheduledSnapshotRow {
pub blockNum: BlockNum,
}

impl ScheduledSnapshotRow {
pub const DB: DbId = DbId::Native;
pub fn key(&self) -> (NativeTable, NativeIndex, BlockNum) {
(
SCHEDULED_SNAPSHOT_TABLE,
NATIVE_TABLE_PRIMARY_INDEX,
self.blockNum,
)
}
}

0 comments on commit e1e40ae

Please sign in to comment.