Skip to content

Commit

Permalink
persistence: use new nonasync crate and its APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Aug 30, 2024
1 parent 48594c2 commit 4772823
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 435 deletions.
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" }
90 changes: 90 additions & 0 deletions src/persistence/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// RGB standard library for working with smart contracts on Bitcoin & Lightning
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2024 by
// Dr Maxim Orlovsky <[email protected]>
//
// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::PathBuf;

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

use crate::persistence::{MemIndex, MemStash, MemState};

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct FsBinStore {
pub stash: PathBuf,
pub state: PathBuf,
pub index: PathBuf,
}

impl FsBinStore {
pub fn new(path: PathBuf) -> Self {
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");

Self {
stash,
state,
index,
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L38-L51

Added lines #L38 - L51 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 57 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L54-L57

Added lines #L54 - L57 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 63 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L59-L63

Added lines #L59 - L63 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 70 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L67-L70

Added lines #L67 - L70 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 76 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L72-L76

Added lines #L72 - L76 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 83 in src/persistence/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L80-L83

Added lines #L80 - L83 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/persistence/fs.rs#L85-L89

Added lines #L85 - L89 were not covered by tests
}
7 changes: 4 additions & 3 deletions src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ use std::error::Error;
use std::fmt::Debug;

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

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

#[derive(Debug, Display, Error, From)]
Expand Down Expand Up @@ -84,7 +85,7 @@ impl<P: IndexProvider> From<IndexWriteError<<P as IndexWriteProvider>::Error>> f
}
}

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

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

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

pub trait IndexReadProvider {
type Error: Clone + Eq + Error;
Expand Down
Loading

0 comments on commit 4772823

Please sign in to comment.