Skip to content

Instantly share code, notes, and snippets.

@0xNineteen
Created March 14, 2023 20:47
Show Gist options
  • Save 0xNineteen/e73d7714f52586f2d2f10f076542930c to your computer and use it in GitHub Desktop.
Save 0xNineteen/e73d7714f52586f2d2f10f076542930c to your computer and use it in GitHub Desktop.
use paste::paste;
macro_rules! dispatch_trait_fn {
($value_name:ident : $value:ty => $key_name:ident : $key:ty) => {
paste!(
fn [<get_ $value_name>](&self, $key_name:$key) -> Result<$value>;
fn [<put_ $value_name>](&self, $value_name:$value) -> Result<$key>;
);
}
}
trait ChainState {
// fn get_block(&self, hash: Sha256Bytes) -> Option<Block>;
// fn put_block(&self, block: Block) -> Result<Sha256Bytes>;
dispatch_trait_fn!(block: Block => hash: Sha256Bytes);
dispatch_trait_fn!(account: Account => hash: Sha256Bytes);
}
// get_deserialize(hash => Account)
// Account::try_from_slice(
// db.get(account_digest)?.unwrap().as_slice()
// )?
macro_rules! get {
($db:expr, $input:ident => $value:ty) => {
Ok(<$value>::try_from_slice(
$db.get($input)?.unwrap().as_slice()
)?)
};
}
struct RocksDB {
db: DB
}
impl ChainState for RocksDB {
fn get_account(&self, hash:Sha256Bytes) -> Result<Account> {
get!(self.db, hash => Account)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment