Skip to content

Commit

Permalink
Add readme for DBs and risc0 adapter (Sovereign-Labs#333)
Browse files Browse the repository at this point in the history
* 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
preston-evans98 and citizen-stig authored May 25, 2023
1 parent 88b3008 commit 4330790
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions adapters/risc0/README.md
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
2 changes: 2 additions & 0 deletions examples/demo-rollup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package instead.

By swapping out or modifying the imported state transition function, you can customize
this example full-node to run arbitrary logic.
This particular example relies on the state transition exported by [`demo-stf`](../demo-stf/). If you want to
understand how to build your own state transition function, check out at the docs in that package.

## How to Customize This Example

Expand Down
60 changes: 60 additions & 0 deletions full-node/db/schemadb/README.md
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
}
```
19 changes: 19 additions & 0 deletions full-node/db/sovereign-db/README.md
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.

0 comments on commit 4330790

Please sign in to comment.