forked from availproject/sovereign-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readme for DBs and risc0 adapter (Sovereign-Labs#333)
* Add readme for schemadb and risc0 adapter * Add README for Sovereign DB * Update full-node/db/sovereign-db/README.md Co-authored-by: Nikolai Golub <[email protected]> * Update full-node/db/schemadb/README.md Co-authored-by: Nikolai Golub <[email protected]> --------- Co-authored-by: Nikolai Golub <[email protected]>
- Loading branch information
1 parent
88b3008
commit 4330790
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Risc0 Adapter | ||
|
||
This package adapts Risc0 version 0.14 to work as a ZKVM for the Sovereign SDK. | ||
|
||
## Limitations | ||
|
||
Since recursion is not included in the 0.14 release, this adapter is currently limited - individual "slots" may | ||
be proven, but those proofs cannot be recursively combined to facilitate bridging or ultra-fast sync. | ||
|
||
## Warning | ||
|
||
Risc0 is currently under active development and has not been audited. This adapter has also not been audited. Do not | ||
deploy in production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Schema DB | ||
|
||
This package is a low-level wrapper transforming [RocksDB](https://rocksdb.org/) from a byte-oriented key value store into a | ||
type-oriented store. It's adapted from a simliar package in Aptos-Core. | ||
|
||
The most important concept exposed by Schema DB is a `Schema`, which maps a column-family name | ||
codec's for the key and value. | ||
|
||
```rust | ||
pub trait Schema { | ||
/// The column family name associated with this struct. | ||
/// Note: all schemas within the same SchemaDB must have distinct column family names. | ||
const COLUMN_FAMILY_NAME: &'static str; | ||
|
||
/// Type of the key. | ||
type Key: KeyCodec<Self>; | ||
|
||
/// Type of the value. | ||
type Value: ValueCodec<Self>; | ||
} | ||
``` | ||
|
||
Using this schema, we can write a generic functions for storing and fetching typed data and ensure that | ||
it's always encoded/decoded in the way we expect. | ||
|
||
```rust | ||
impl SchemaDB { | ||
pub fn put<S: Schema>(&self, key: &impl KeyCodec<S>, value: &impl ValueCodec<S>) -> Result<()> { | ||
let key_bytes = key.encode_key()?; | ||
let value_bytes = value.encode_value()?; | ||
self.rocks_db_handle.put(S::COLUMN_FAMILY_NAME, key_bytes, value_bytes) | ||
} | ||
} | ||
``` | ||
|
||
To actually store and retrieve data, all we need to do is to implement a Schema | ||
|
||
```rust | ||
pub struct AccountBalanceSchema; | ||
|
||
impl Schema for AccountBalanceSchema { | ||
const COLUMN_FAMILY_NAME = "account_balances" | ||
type Key = Account; | ||
type Value = u64; | ||
} | ||
|
||
impl KeyCodec<AccountBalanceSchema> for Account { | ||
fn encode_key(&self) -> Vec<u8> { | ||
bincode::to_vec(self) | ||
} | ||
|
||
fn decode_key(key: Vec<u8>) -> { | ||
// elided | ||
} | ||
} | ||
|
||
impl ValueCode<AccountBlanceSchema> for u64 { | ||
// elided | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Sovereign DB | ||
|
||
This package provides a high-level interface to a [Schema DB](../schemadb/README.md) designed specifically for use with the Sovereign SDK. | ||
It exposes two db types: `LedgerDB`, and `StateDB`. | ||
|
||
## LedgerDB | ||
|
||
As the name implies, the `LedgerDB` is designed to store ledger history. It has tables for slots, batches, transactions, and events. | ||
The `LedgerDB` also implements the `LedgerRpcProvider` trait, allowing it to easily serve chain history over RPC | ||
|
||
## StateDB | ||
|
||
The StateDB is intended to be used with the Jellyfish Merkle Tree provided by the sovereign Module System. If you aren't using the | ||
module system, chances are that you'll want to implement your own State Database. | ||
|
||
StateDB is designed to store Jellyfish Merkle Tree data efficiently. It maintains a flat store mapping `(Key, Version)` tuples | ||
to values, as well as a mapping from JMT `NodeKey`s to JMT `Nodes`. | ||
|
||
In the module system, StateDB is abstracted behind the Storage interface, so you won't interact with it directly. |