diff --git a/src/persistence/memory.rs b/src/persistence/memory.rs index 1b95ebed..5afa9287 100644 --- a/src/persistence/memory.rs +++ b/src/persistence/memory.rs @@ -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, @@ -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; @@ -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, @@ -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; @@ -1132,10 +1165,10 @@ pub struct ContractIndex { outpoint_opouts: MediumOrdMap>, } -#[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, @@ -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; @@ -1421,7 +1468,7 @@ mod fs { Self { dirty: true, filename: Some(filename.to_owned()), - ..default!() + ..Self::in_memory() } } @@ -1458,7 +1505,7 @@ mod fs { Self { dirty: true, filename: Some(filename.to_owned()), - ..default!() + ..Self::in_memory() } } @@ -1495,7 +1542,7 @@ mod fs { Self { dirty: true, filename: Some(filename.to_owned()), - ..default!() + ..Self::in_memory() } } diff --git a/src/persistence/stock.rs b/src/persistence/stock.rs index d4723855..008c99d5 100644 --- a/src/persistence/stock.rs +++ b/src/persistence/stock.rs @@ -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")] @@ -1306,16 +1309,14 @@ mod test { #[test] fn test_consign() { - let mut stock = Stock::::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(); @@ -1326,7 +1327,7 @@ mod test { #[test] fn test_export_contract() { - let stock = Stock::::default(); + let stock = Stock::in_memory(); let contract_id = ContractId::from_baid64_str("rgb:qFuT6DN8-9AuO95M-7R8R8Mc-AZvs7zG-obum1Va-BRnweKk") .unwrap(); @@ -1337,7 +1338,7 @@ mod test { #[test] fn test_export_schema() { - let stock = Stock::::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) { @@ -1347,7 +1348,7 @@ mod test { #[test] fn test_blank_builder_ifaceid() { - let stock = Stock::::default(); + let stock = Stock::in_memory(); let hasher = Sha256::default(); let iface_id = IfaceId::from(hasher.clone()); let bytes_hash = hasher.finish(); @@ -1359,7 +1360,7 @@ mod test { #[test] fn test_blank_builder_ifacename() { - let stock = Stock::::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(); @@ -1372,7 +1373,7 @@ mod test { #[test] fn test_transition_builder() { - let stock = Stock::::default(); + let stock = Stock::in_memory(); let hasher = Sha256::default(); let iface_id = IfaceId::from(hasher.clone());