Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add store providers to persistence module #251

Merged
merged 9 commits into from
Sep 5, 2024
55 changes: 38 additions & 17 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ license = "Apache-2.0"

[workspace.dependencies]
amplify = "4.7.0"
nonasync = "0.1.0"
ascii-armor = "0.7.1"
baid64 = "0.2.2"
strict_encoding = "2.7.0-rc.1"
Expand Down Expand Up @@ -54,6 +55,7 @@ crate-type = ["cdylib", "rlib"] # We need this for WASM

[dependencies]
amplify = { workspace = true }
nonasync = { workspace = true }
ascii-armor = { workspace = true }
baid64 = { workspace = true }
strict_encoding = { workspace = true }
Expand Down Expand Up @@ -97,4 +99,5 @@ wasm-bindgen-test = "0.3"
features = ["all"]

[patch.crates-io]
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "nonce" }
nonasync = { git = "https://github.com/rust-amplify/amplify-nonasync" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "nonce" }
76 changes: 67 additions & 9 deletions src/persistence/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,75 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::{fs, io};

use strict_encoding::{DeserializeError, SerializeError};
use amplify::confinement::U32 as U32MAX;
use nonasync::persistence::{PersistenceError, PersistenceProvider};
use strict_encoding::{StrictDeserialize, StrictSerialize};

pub trait FsStored: Sized {
fn new(path: impl ToOwned<Owned = PathBuf>) -> Self;
fn load(path: impl ToOwned<Owned = PathBuf>) -> Result<Self, DeserializeError>;
use crate::persistence::{MemIndex, MemStash, MemState};

fn is_dirty(&self) -> bool;
fn filename(&self) -> Option<&Path>;
fn set_filename(&mut self, filename: impl ToOwned<Owned = PathBuf>) -> Option<PathBuf>;
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct FsBinStore {
pub stash: PathBuf,
pub state: PathBuf,
pub index: PathBuf,
}

impl FsBinStore {
pub fn new(path: PathBuf) -> io::Result<Self> {
fs::create_dir_all(&path)?;

Check warning on line 40 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

let mut stash = path.clone();
stash.push("stash.dat");
let mut state = path.clone();
state.push("state.dat");
let mut index = path.clone();
index.push("index.dat");

Ok(Self {
stash,
state,
index,
})
}

Check warning on line 54 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L42-L54

Added lines #L42 - L54 were not covered by tests
}
impl PersistenceProvider<MemStash> for FsBinStore {
fn load(&self) -> Result<MemStash, PersistenceError> {
MemStash::strict_deserialize_from_file::<U32MAX>(&self.stash)
.map_err(PersistenceError::with)
}

Check warning on line 60 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L57-L60

Added lines #L57 - L60 were not covered by tests

fn store(&self, object: &MemStash) -> Result<(), PersistenceError> {
object
.strict_serialize_to_file::<U32MAX>(&self.stash)
.map_err(PersistenceError::with)
}

Check warning on line 66 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L62-L66

Added lines #L62 - L66 were not covered by tests
}

impl PersistenceProvider<MemState> for FsBinStore {
fn load(&self) -> Result<MemState, PersistenceError> {
MemState::strict_deserialize_from_file::<U32MAX>(&self.state)
.map_err(PersistenceError::with)
}

Check warning on line 73 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L70-L73

Added lines #L70 - L73 were not covered by tests

fn store(&self, object: &MemState) -> Result<(), PersistenceError> {
object
.strict_serialize_to_file::<U32MAX>(&self.state)
.map_err(PersistenceError::with)
}

Check warning on line 79 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L75-L79

Added lines #L75 - L79 were not covered by tests
}

impl PersistenceProvider<MemIndex> for FsBinStore {
fn load(&self) -> Result<MemIndex, PersistenceError> {
MemIndex::strict_deserialize_from_file::<U32MAX>(&self.index)
.map_err(PersistenceError::with)
}

Check warning on line 86 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L83-L86

Added lines #L83 - L86 were not covered by tests

fn store(&self) -> Result<(), SerializeError>;
fn store(&self, object: &MemIndex) -> Result<(), PersistenceError> {
object
.strict_serialize_to_file::<U32MAX>(&self.index)
.map_err(PersistenceError::with)
}

Check warning on line 92 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L88-L92

Added lines #L88 - L92 were not covered by tests
}
26 changes: 18 additions & 8 deletions src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
use std::fmt::Debug;

use amplify::confinement;
use nonasync::persistence::{CloneNoPersistence, Persisting};
use rgb::{
Assign, AssignmentType, BundleId, ContractId, ExposedState, Extension, Genesis, GenesisSeal,
GraphSeal, OpId, Operation, Opout, TransitionBundle, TypedAssigns, XChain, XOutputSeal,
XWitnessId,
};
use strict_encoding::SerializeError;

use crate::containers::{BundledWitness, ConsignmentExt, ToWitnessId};
use crate::persistence::StoreTransaction;
use crate::persistence::{MemError, StoreTransaction};
use crate::SecretSeal;

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[derive(Debug, Display, Error, From)]
#[display(inner)]
pub enum IndexError<P: IndexProvider> {
/// Connectivity errors which may be recoverable and temporary.
Expand Down Expand Up @@ -85,7 +85,7 @@
}
}

impl From<confinement::Error> for IndexWriteError<SerializeError> {
impl From<confinement::Error> for IndexWriteError<MemError> {
fn from(err: confinement::Error) -> Self { IndexWriteError::Connectivity(err.into()) }
}

Expand Down Expand Up @@ -124,11 +124,19 @@
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 All @@ -146,7 +154,6 @@
pub fn as_provider(&self) -> &P { &self.provider }

#[doc(hidden)]
#[cfg(feature = "fs")]
pub(super) fn as_provider_mut(&mut self) -> &mut P { &mut self.provider }

pub(super) fn index_consignment(
Expand Down Expand Up @@ -351,7 +358,10 @@
fn rollback_transaction(&mut self) { self.provider.rollback_transaction() }
}

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

pub trait IndexReadProvider {
type Error: Clone + Eq + Error;
Expand Down Expand Up @@ -386,7 +396,7 @@
}

pub trait IndexWriteProvider: StoreTransaction<TransactionErr = Self::Error> {
type Error: Clone + Eq + Error;
type Error: Error;

fn register_contract(&mut self, contract_id: ContractId) -> Result<bool, Self::Error>;

Expand Down
Loading
Loading