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

Refactor FS store and load workflows #234

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 9 additions & 91 deletions src/persistence/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,99 +19,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

use amplify::confinement::U32;
use strict_encoding::{DeserializeError, SerializeError, StrictDeserialize, StrictSerialize};
use strict_encoding::{DeserializeError, SerializeError};

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

pub trait LoadFs: Sized {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError>;
}

pub trait StoreFs {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError>;
}

impl<S: StashProvider, H: StateProvider, I: IndexProvider> LoadFs for Stock<S, H, I>
where
S: LoadFs,
H: LoadFs,
I: LoadFs,
{
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let path = path.as_ref();
let stash = S::load(path)?;
let state = H::load(path)?;
let index = I::load(path)?;

Ok(Stock::with(stash, state, index))
}
}

impl<S: StashProvider, H: StateProvider, I: IndexProvider> StoreFs for Stock<S, H, I>
where
S: StoreFs,
H: StoreFs,
I: StoreFs,
{
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let path = path.as_ref();
self.as_stash_provider().store(path)?;
self.as_state_provider().store(path)?;
self.as_index_provider().store(path)?;

Ok(())
}
}

impl LoadFs for MemStash {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("stash.dat");
Self::strict_deserialize_from_file::<U32>(file)
}
}

impl StoreFs for MemStash {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("stash.dat");
self.strict_serialize_to_file::<U32>(file)
}
}

impl LoadFs for MemState {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("state.dat");
Self::strict_deserialize_from_file::<U32>(file)
}
}

impl StoreFs for MemState {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("state.dat");
self.strict_serialize_to_file::<U32>(file)
}
}

impl LoadFs for MemIndex {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("index.dat");
Self::strict_deserialize_from_file::<U32>(file)
}
}
fn is_dirty(&self) -> bool;
fn filename(&self) -> &Path;
fn set_filename(&mut self, filename: impl ToOwned<Owned = PathBuf>) -> PathBuf;

impl StoreFs for MemIndex {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("index.dat");
self.strict_serialize_to_file::<U32>(file)
}
fn store(&self) -> Result<(), SerializeError>;
}
30 changes: 29 additions & 1 deletion src/persistence/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ use std::collections::BTreeSet;
use std::error::Error;
use std::fmt::Debug;

use amplify::confinement;
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, Consignment, ToWitnessId};
use crate::persistence::StoreTransaction;
use crate::SecretSeal;

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
Expand Down Expand Up @@ -82,6 +85,10 @@ impl<P: IndexProvider> From<IndexWriteError<<P as IndexWriteProvider>::Error>> f
}
}

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

#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum IndexInconsistency {
Expand Down Expand Up @@ -146,6 +153,9 @@ impl<P: IndexProvider> Index<P> {
#[doc(hidden)]
pub fn as_provider(&self) -> &P { &self.provider }

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

pub(super) fn index_consignment<const TRANSFER: bool>(
&mut self,
consignment: &Consignment<TRANSFER>,
Expand Down Expand Up @@ -330,6 +340,24 @@ impl<P: IndexProvider> Index<P> {
}
}

impl<P: IndexProvider> StoreTransaction for Index<P> {
type TransactionErr = IndexError<P>;

fn begin_transaction(&mut self) -> Result<(), Self::TransactionErr> {
self.provider
.begin_transaction()
.map_err(IndexError::WriteProvider)
}

fn commit_transaction(&mut self) -> Result<(), Self::TransactionErr> {
self.provider
.commit_transaction()
.map_err(IndexError::WriteProvider)
}

fn rollback_transaction(&mut self) { self.provider.rollback_transaction() }
}

pub trait IndexProvider: Debug + IndexReadProvider + IndexWriteProvider {}

pub trait IndexReadProvider {
Expand Down Expand Up @@ -364,7 +392,7 @@ pub trait IndexReadProvider {
) -> Result<(XWitnessId, ContractId), IndexReadError<Self::Error>>;
}

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

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