Skip to content

Commit

Permalink
persistence: impl CloneNoPersistence for stock objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Aug 30, 2024
1 parent 4772823 commit db39342
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::error::Error;
use std::fmt::Debug;

use amplify::confinement;
use nonasync::persistence::Persisting;
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::{
Assign, AssignmentType, BundleId, ContractId, ExposedState, Extension, Genesis, GenesisSeal,
GraphSeal, OpId, Operation, Opout, TransitionBundle, TypedAssigns, XChain, XOutputSeal,
Expand Down Expand Up @@ -124,11 +124,19 @@ pub enum IndexInconsistency {
BundleWitnessUnknown(BundleId),
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Index<P: IndexProvider> {
provider: P,
}

impl<P: IndexProvider> CloneNoPersistence for Index<P> {
fn clone_no_persistence(&self) -> Self {
Self {
provider: self.provider.clone_no_persistence(),
}
}

Check warning on line 137 in src/persistence/index.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/index.rs#L133-L137

Added lines #L133 - L137 were not covered by tests
}

impl<P: IndexProvider> Default for Index<P>
where P: Default
{
Expand Down Expand Up @@ -350,7 +358,10 @@ impl<P: IndexProvider> StoreTransaction for Index<P> {
fn rollback_transaction(&mut self) { self.provider.rollback_transaction() }
}

pub trait IndexProvider: Debug + Persisting + IndexReadProvider + IndexWriteProvider {}
pub trait IndexProvider:
Debug + CloneNoPersistence + Persisting + IndexReadProvider + IndexWriteProvider
{
}

pub trait IndexReadProvider {
type Error: Clone + Eq + Error;
Expand Down
46 changes: 45 additions & 1 deletion src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use amplify::confinement::{
use amplify::num::u24;
use bp::dbc::tapret::TapretCommitment;
use commit_verify::{CommitId, Conceal};
use nonasync::persistence::{Persistence, PersistenceError, Persisting};
use nonasync::persistence::{CloneNoPersistence, Persistence, PersistenceError, Persisting};
use rgb::validation::ResolveWitness;
use rgb::vm::{
ContractStateAccess, ContractStateEvolve, GlobalContractState, GlobalOrd, GlobalStateIter,
Expand Down Expand Up @@ -128,6 +128,27 @@ impl MemStash {
}
}

impl CloneNoPersistence for MemStash {
fn clone_no_persistence(&self) -> Self {
Self {
persistence: None,
schemata: self.schemata.clone(),
ifaces: self.ifaces.clone(),
geneses: self.geneses.clone(),
suppl: self.suppl.clone(),
bundles: self.bundles.clone(),
extensions: self.extensions.clone(),
witnesses: self.witnesses.clone(),
attachments: self.attachments.clone(),
secret_seals: self.secret_seals.clone(),
type_system: self.type_system.clone(),
identities: self.identities.clone(),
libs: self.libs.clone(),
sigs: self.sigs.clone(),
}

Check warning on line 148 in src/persistence/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/memory.rs#L132-L148

Added lines #L132 - L148 were not covered by tests
}
}

impl Persisting for MemStash {
#[inline]
fn persistence(&self) -> Option<&Persistence<Self>> { self.persistence.as_ref() }
Expand Down Expand Up @@ -487,6 +508,16 @@ impl MemState {
}
}

impl CloneNoPersistence for MemState {
fn clone_no_persistence(&self) -> Self {
Self {
persistence: None,
witnesses: self.witnesses.clone(),
contracts: self.contracts.clone(),
}

Check warning on line 517 in src/persistence/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/memory.rs#L512-L517

Added lines #L512 - L517 were not covered by tests
}
}

impl Persisting for MemState {
#[inline]
fn persistence(&self) -> Option<&Persistence<Self>> { self.persistence.as_ref() }

Check warning on line 523 in src/persistence/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/memory.rs#L523

Added line #L523 was not covered by tests
Expand Down Expand Up @@ -1191,6 +1222,19 @@ impl MemIndex {
}
}

impl CloneNoPersistence for MemIndex {
fn clone_no_persistence(&self) -> Self {
Self {
persistence: None,
op_bundle_index: self.op_bundle_index.clone(),
bundle_contract_index: self.bundle_contract_index.clone(),
bundle_witness_index: self.bundle_witness_index.clone(),
contract_index: self.contract_index.clone(),
terminal_index: self.terminal_index.clone(),
}

Check warning on line 1234 in src/persistence/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/memory.rs#L1226-L1234

Added lines #L1226 - L1234 were not covered by tests
}
}

impl Persisting for MemIndex {
#[inline]
fn persistence(&self) -> Option<&Persistence<Self>> { self.persistence.as_ref() }

Check warning on line 1240 in src/persistence/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/memory.rs#L1240

Added line #L1240 was not covered by tests
Expand Down
17 changes: 14 additions & 3 deletions src/persistence/stash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use amplify::confinement::{Confined, MediumBlob, TinyOrdMap};
use bp::dbc::anchor::MergeError;
use bp::dbc::tapret::TapretCommitment;
use commit_verify::mpc;
use nonasync::persistence::Persisting;
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::validation::Scripts;
use rgb::{
AttachId, BundleId, ContractId, Extension, Genesis, GraphSeal, Identity, OpId, Operation,
Expand Down Expand Up @@ -186,11 +186,19 @@ impl SchemaIfaces {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Stash<P: StashProvider> {
provider: P,
}

impl<P: StashProvider> CloneNoPersistence for Stash<P> {
fn clone_no_persistence(&self) -> Self {
Self {
provider: self.provider.clone_no_persistence(),
}
}

Check warning on line 199 in src/persistence/stash.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/stash.rs#L195-L199

Added lines #L195 - L199 were not covered by tests
}

impl<P: StashProvider> Default for Stash<P>
where P: Default
{
Expand Down Expand Up @@ -619,7 +627,10 @@ impl<P: StashProvider> StoreTransaction for Stash<P> {
fn rollback_transaction(&mut self) { self.provider.rollback_transaction() }
}

pub trait StashProvider: Debug + Persisting + StashReadProvider + StashWriteProvider {}
pub trait StashProvider:
Debug + CloneNoPersistence + Persisting + StashReadProvider + StashWriteProvider
{
}

pub trait StashReadProvider {
/// Error type which must indicate problems on data retrieval.
Expand Down
17 changes: 14 additions & 3 deletions src/persistence/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::fmt::Debug;
use std::iter;

use invoice::Amount;
use nonasync::persistence::Persisting;
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::validation::{ResolveWitness, WitnessResolverError};
use rgb::vm::{ContractStateAccess, WitnessOrd};
use rgb::{
Expand Down Expand Up @@ -92,11 +92,19 @@ impl PersistedState {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct State<P: StateProvider> {
provider: P,
}

impl<P: StateProvider> CloneNoPersistence for State<P> {
fn clone_no_persistence(&self) -> Self {
Self {
provider: self.provider.clone_no_persistence(),
}
}

Check warning on line 105 in src/persistence/state.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/state.rs#L101-L105

Added lines #L101 - L105 were not covered by tests
}

impl<P: StateProvider> Default for State<P>
where P: Default
{
Expand Down Expand Up @@ -255,7 +263,10 @@ impl<P: StateProvider> StoreTransaction for State<P> {
fn rollback_transaction(&mut self) { self.provider.rollback_transaction() }
}

pub trait StateProvider: Debug + Persisting + StateReadProvider + StateWriteProvider {}
pub trait StateProvider:
Debug + CloneNoPersistence + Persisting + StateReadProvider + StateWriteProvider
{
}

pub trait StateReadProvider {
type ContractRead<'a>: ContractStateRead
Expand Down
14 changes: 12 additions & 2 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use bp::seals::txout::CloseMethod;
use bp::Vout;
use chrono::Utc;
use invoice::{Amount, Beneficiary, InvoiceState, NonFungible, RgbInvoice};
use nonasync::persistence::{PersistenceError, PersistenceProvider};
use nonasync::persistence::{CloneNoPersistence, PersistenceError, PersistenceProvider};
use rgb::validation::{DbcProof, EAnchor, ResolveWitness, WitnessResolverError};
use rgb::{
validation, AssignmentType, BlindingFactor, BundleId, ContractId, DataState, GraphSeal,
Expand Down Expand Up @@ -343,7 +343,7 @@ stock_err_conv!(ContractIfaceError, InputError);
pub type StockErrorMem<E = Infallible> = StockError<MemStash, MemState, MemIndex, E>;
pub type StockErrorAll<S = MemStash, H = MemState, P = MemIndex> = StockError<S, H, P, InputError>;

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Stock<
S: StashProvider = MemStash,
H: StateProvider = MemState,
Expand All @@ -354,6 +354,16 @@ pub struct Stock<
index: Index<P>,
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> CloneNoPersistence for Stock<S, H, P> {
fn clone_no_persistence(&self) -> Self {
Self {
stash: self.stash.clone_no_persistence(),
state: self.state.clone_no_persistence(),
index: self.index.clone_no_persistence(),
}
}

Check warning on line 364 in src/persistence/stock.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/stock.rs#L358-L364

Added lines #L358 - L364 were not covered by tests
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> Default for Stock<S, H, P>
where
S: Default,
Expand Down

0 comments on commit db39342

Please sign in to comment.