Skip to content

Commit

Permalink
persistence: use explicit in_memory method to construct mem-structs…
Browse files Browse the repository at this point in the history
… with no file storage
  • Loading branch information
dr-orlovsky committed Aug 2, 2024
1 parent bb6ff8f commit 3aaafcb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
71 changes: 59 additions & 12 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ use crate::LIB_NAME_RGB_STORAGE;
//////////

/// Hoard is an in-memory stash useful for WASM implementations.
#[derive(Getters, Clone, Debug, Default)]
#[derive(Getters, Clone, Debug)]
#[getter(prefix = "debug_")]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE, dumb = Self::in_memory())]
pub struct MemStash {
#[strict_type(skip)]
dirty: bool,
Expand All @@ -102,6 +102,28 @@ pub struct MemStash {
impl StrictSerialize for MemStash {}
impl StrictDeserialize for MemStash {}

impl MemStash {
pub fn in_memory() -> Self {
Self {
dirty: false,
filename: None,
schemata: empty!(),
ifaces: empty!(),
geneses: empty!(),
suppl: empty!(),
bundles: empty!(),
extensions: empty!(),
witnesses: empty!(),
attachments: empty!(),
secret_seals: empty!(),
type_system: none!(),
identities: empty!(),
libs: empty!(),
sigs: empty!(),
}
}
}

impl StoreTransaction for MemStash {
type TransactionErr = SerializeError;

Expand Down Expand Up @@ -434,10 +456,10 @@ impl StashWriteProvider for MemStash {
// STATE
//////////

#[derive(Getters, Clone, Debug, Default)]
#[derive(Getters, Clone, Debug)]
#[getter(prefix = "debug_")]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE, dumb = Self::in_memory())]
pub struct MemState {
#[strict_type(skip)]
dirty: bool,
Expand All @@ -452,6 +474,17 @@ pub struct MemState {
impl StrictSerialize for MemState {}
impl StrictDeserialize for MemState {}

impl MemState {
pub fn in_memory() -> Self {
Self {
dirty: false,
filename: None,
witnesses: empty!(),
contracts: empty!(),
}
}
}

impl StoreTransaction for MemState {
type TransactionErr = SerializeError;

Expand Down Expand Up @@ -1132,10 +1165,10 @@ pub struct ContractIndex {
outpoint_opouts: MediumOrdMap<XOutputSeal, MediumOrdSet<Opout>>,
}

#[derive(Getters, Clone, Debug, Default)]
#[derive(Getters, Clone, Debug)]
#[getter(prefix = "debug_")]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STORAGE, dumb = Self::in_memory())]
pub struct MemIndex {
#[strict_type(skip)]
dirty: bool,
Expand All @@ -1153,6 +1186,20 @@ pub struct MemIndex {
impl StrictSerialize for MemIndex {}
impl StrictDeserialize for MemIndex {}

impl MemIndex {
pub fn in_memory() -> Self {
Self {
dirty: false,
filename: None,
op_bundle_index: empty!(),
bundle_contract_index: empty!(),
bundle_witness_index: empty!(),
contract_index: empty!(),
terminal_index: empty!(),
}
}
}

impl StoreTransaction for MemIndex {
type TransactionErr = SerializeError;

Expand Down Expand Up @@ -1421,7 +1468,7 @@ mod fs {
Self {
dirty: true,
filename: Some(filename.to_owned()),
..default!()
..Self::in_memory()
}
}

Expand Down Expand Up @@ -1458,7 +1505,7 @@ mod fs {
Self {
dirty: true,
filename: Some(filename.to_owned()),
..default!()
..Self::in_memory()
}
}

Expand Down Expand Up @@ -1495,7 +1542,7 @@ mod fs {
Self {
dirty: true,
filename: Some(filename.to_owned()),
..default!()
..Self::in_memory()
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ where
}

impl Stock {
pub fn in_memory() -> Self { Self::default() }
#[inline]
pub fn in_memory() -> Self {
Self::with(MemStash::in_memory(), MemState::in_memory(), MemIndex::in_memory())
}
}

#[cfg(feature = "fs")]
Expand Down Expand Up @@ -1306,16 +1309,14 @@ mod test {

#[test]
fn test_consign() {
let mut stock = Stock::<MemStash, MemState, MemIndex>::default();
let mut stock = Stock::in_memory();
let seal = XChain::with(
rgbcore::Layer1::Bitcoin,
GraphSeal::new_random_vout(bp::dbc::Method::OpretFirst, Vout::from_u32(0)),
);
let secret_seal = seal.conceal();

stock
.store_secret_seal(seal)
.expect_err("we can't store to FS and fail here since we have not filename assigned");
stock.store_secret_seal(seal).unwrap();
let contract_id =
ContractId::from_baid64_str("rgb:qFuT6DN8-9AuO95M-7R8R8Mc-AZvs7zG-obum1Va-BRnweKk")
.unwrap();
Expand All @@ -1326,7 +1327,7 @@ mod test {

#[test]
fn test_export_contract() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let stock = Stock::in_memory();
let contract_id =
ContractId::from_baid64_str("rgb:qFuT6DN8-9AuO95M-7R8R8Mc-AZvs7zG-obum1Va-BRnweKk")
.unwrap();
Expand All @@ -1337,7 +1338,7 @@ mod test {

#[test]
fn test_export_schema() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let stock = Stock::in_memory();
let hasher = Sha256::default();
let schema_id = SchemaId::from(hasher);
if let Ok(schema) = stock.export_schema(schema_id) {
Expand All @@ -1347,7 +1348,7 @@ mod test {

#[test]
fn test_blank_builder_ifaceid() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let stock = Stock::in_memory();
let hasher = Sha256::default();
let iface_id = IfaceId::from(hasher.clone());
let bytes_hash = hasher.finish();
Expand All @@ -1359,7 +1360,7 @@ mod test {

#[test]
fn test_blank_builder_ifacename() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let stock = Stock::in_memory();
let hasher = Sha256::default();
let bytes_hash = hasher.finish();
let contract_id = ContractId::copy_from_slice(bytes_hash).unwrap();
Expand All @@ -1372,7 +1373,7 @@ mod test {

#[test]
fn test_transition_builder() {
let stock = Stock::<MemStash, MemState, MemIndex>::default();
let stock = Stock::in_memory();
let hasher = Sha256::default();
let iface_id = IfaceId::from(hasher.clone());

Expand Down

0 comments on commit 3aaafcb

Please sign in to comment.