Skip to content

Commit

Permalink
Separate storage write operations from RW
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Dec 9, 2024
1 parent fd48654 commit cb95f7d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
10 changes: 5 additions & 5 deletions wallet/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ use std::collections::{BTreeMap, BTreeSet};
use std::ops::{Add, Sub};
use std::sync::Arc;
use wallet_storage::{
StoreTxRw, WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageWriteLocked,
WalletStorageWriteUnlocked,
StoreTxRw, WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageReadWriteLocked,
WalletStorageWriteLocked, WalletStorageWriteUnlocked,
};
use wallet_types::utxo_types::{get_utxo_type, UtxoState, UtxoStates, UtxoType, UtxoTypes};
use wallet_types::wallet_tx::{BlockData, TxData, TxState};
Expand Down Expand Up @@ -1955,7 +1955,7 @@ impl<K: AccountKeyChains> Account<K> {
pub fn scan_new_inmempool_transactions(
&mut self,
transactions: &[SignedTransaction],
db_tx: &mut impl WalletStorageWriteLocked,
db_tx: &mut impl WalletStorageReadWriteLocked,
wallet_events: &impl WalletEvents,
) -> WalletResult<()> {
self.scan_new_unconfirmed_transactions(
Expand All @@ -1969,7 +1969,7 @@ impl<K: AccountKeyChains> Account<K> {
pub fn scan_new_inactive_transactions(
&mut self,
transactions: &[SignedTransaction],
db_tx: &mut impl WalletStorageWriteLocked,
db_tx: &mut impl WalletStorageReadWriteLocked,
wallet_events: &impl WalletEvents,
) -> WalletResult<()> {
self.scan_new_unconfirmed_transactions(
Expand All @@ -1984,7 +1984,7 @@ impl<K: AccountKeyChains> Account<K> {
&mut self,
transactions: &[SignedTransaction],
make_tx_state: fn(u64) -> TxState,
db_tx: &mut impl WalletStorageWriteLocked,
db_tx: &mut impl WalletStorageReadWriteLocked,
wallet_events: &impl WalletEvents,
) -> WalletResult<()> {
let account_id = self.get_account_id();
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/key_chain/master_key_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crypto::vrf::ExtendedVRFPrivateKey;
use std::sync::Arc;
use wallet_storage::{
StoreTxRwUnlocked, WalletStorageReadLocked, WalletStorageReadUnlocked,
WalletStorageWriteUnlocked,
WalletStorageReadWriteUnlocked, WalletStorageWriteUnlocked,
};
use wallet_types::seed_phrase::{SerializableSeedPhrase, StoreSeedPhrase};

Expand Down Expand Up @@ -131,7 +131,7 @@ impl MasterKeyChain {

pub fn create_account_key_chain(
&self,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
account_index: U31,
lookahead_size: u32,
) -> KeyChainResult<AccountKeyChainImplSoftware> {
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crypto::key::{
SignatureError,
};
use wallet_storage::{
WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageWriteUnlocked,
WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageReadWriteUnlocked,
};
use wallet_types::{
partially_signed_transaction::PartiallySignedTransaction, signature_status::SignatureStatus,
Expand Down Expand Up @@ -145,7 +145,7 @@ pub trait SignerProvider {
chain_config: Arc<ChainConfig>,
account_index: U31,
name: Option<String>,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
) -> WalletResult<Account<Self::K>>;

fn load_account_from_database(
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/signer/software_signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use itertools::Itertools;
use randomness::make_true_rng;
use wallet_storage::{
StoreTxRwUnlocked, WalletStorageReadLocked, WalletStorageReadUnlocked,
WalletStorageWriteUnlocked,
WalletStorageReadWriteUnlocked,
};
use wallet_types::{
partially_signed_transaction::PartiallySignedTransaction, seed_phrase::StoreSeedPhrase,
Expand Down Expand Up @@ -461,7 +461,7 @@ impl SignerProvider for SoftwareSignerProvider {
chain_config: Arc<ChainConfig>,
next_account_index: U31,
name: Option<String>,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
) -> WalletResult<Account<AccountKeyChainImplSoftware>> {
let lookahead_size = db_tx.get_lookahead_size()?;
let account_key_chain = self.master_key_chain.create_account_key_chain(
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/signer/trezor_signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use trezor_client::{
};
use utils::ensure;
use wallet_storage::{
WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageWriteUnlocked,
WalletStorageReadLocked, WalletStorageReadUnlocked, WalletStorageReadWriteUnlocked,
};
use wallet_types::{
account_info::DEFAULT_ACCOUNT_INDEX,
Expand Down Expand Up @@ -1148,7 +1148,7 @@ impl SignerProvider for TrezorSignerProvider {
chain_config: Arc<ChainConfig>,
account_index: U31,
name: Option<String>,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
) -> WalletResult<Account<Self::K>> {
let account_pubkey = self.fetch_extended_pub_key(&chain_config, account_index)?;

Expand Down
11 changes: 6 additions & 5 deletions wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub use wallet_storage::Error;
use wallet_storage::{
DefaultBackend, Store, StoreTxRo, StoreTxRw, StoreTxRwUnlocked, TransactionRoLocked,
TransactionRwLocked, TransactionRwUnlocked, Transactional, WalletStorageReadLocked,
WalletStorageReadUnlocked, WalletStorageWriteLocked, WalletStorageWriteUnlocked,
WalletStorageReadUnlocked, WalletStorageReadWriteLocked, WalletStorageReadWriteUnlocked,
WalletStorageWriteLocked, WalletStorageWriteUnlocked,
};
use wallet_types::account_info::{StandaloneAddressDetails, StandaloneAddresses};
use wallet_types::chain_info::ChainInfo;
Expand Down Expand Up @@ -670,7 +671,7 @@ where

fn reset_wallet_transactions(
chain_config: Arc<ChainConfig>,
db_tx: &mut impl WalletStorageWriteLocked,
db_tx: &mut impl WalletStorageReadWriteLocked,
) -> WalletResult<()> {
db_tx.clear_transactions()?;
db_tx.clear_addresses()?;
Expand Down Expand Up @@ -701,7 +702,7 @@ where

fn reset_wallet_transactions_and_load(
chain_config: Arc<ChainConfig>,
db_tx: &mut impl WalletStorageWriteLocked,
db_tx: &mut impl WalletStorageReadWriteLocked,
signer_provider: &P,
) -> WalletResult<BTreeMap<U31, Account<P::K>>> {
Self::reset_wallet_transactions(chain_config.clone(), db_tx)?;
Expand All @@ -723,7 +724,7 @@ where

fn migrate_next_unused_account(
chain_config: Arc<ChainConfig>,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
signer_provider: &mut P,
) -> Result<(), WalletError> {
let accounts_info = db_tx.get_accounts_info()?;
Expand Down Expand Up @@ -901,7 +902,7 @@ where
fn create_next_unused_account(
next_account_index: U31,
chain_config: Arc<ChainConfig>,
db_tx: &mut impl WalletStorageWriteUnlocked,
db_tx: &mut impl WalletStorageReadWriteUnlocked,
name: Option<String>,
signer_provider: &mut P,
) -> WalletResult<(U31, Account<P::K>)> {
Expand Down
5 changes: 5 additions & 0 deletions wallet/storage/src/internal/store_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ impl<'st, B: storage::Backend> crate::TransactionRoUnlocked for StoreTxRoUnlocke
}
}

impl<'st, B: storage::Backend> crate::WalletStorageReadWriteLocked for StoreTxRw<'st, B> {}

impl<'st, B: storage::Backend> crate::TransactionRwLocked for StoreTxRw<'st, B> {
fn commit(self) -> crate::Result<()> {
self.storage.commit().map_err(Into::into)
Expand All @@ -741,6 +743,9 @@ impl<'st, B: storage::Backend> crate::TransactionRwLocked for StoreTxRw<'st, B>
}
}

impl<'st, B: storage::Backend> crate::WalletStorageReadWriteLocked for StoreTxRwUnlocked<'st, B> {}
impl<'st, B: storage::Backend> crate::WalletStorageReadWriteUnlocked for StoreTxRwUnlocked<'st, B> {}

impl<'st, B: storage::Backend> crate::TransactionRwUnlocked for StoreTxRwUnlocked<'st, B> {
fn commit(self) -> crate::Result<()> {
self.storage.commit().map_err(Into::into)
Expand Down
20 changes: 15 additions & 5 deletions wallet/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub trait WalletStorageEncryptionRead {
}

/// Modifying operations on persistent wallet data
pub trait WalletStorageWriteLocked: WalletStorageReadLocked {
pub trait WalletStorageWriteLocked {
/// Set storage version
fn set_storage_version(&mut self, version: u32) -> Result<()>;
fn set_wallet_type(&mut self, wallet_type: WalletType) -> Result<()>;
Expand Down Expand Up @@ -200,7 +200,7 @@ pub trait WalletStorageWriteLocked: WalletStorageReadLocked {
}

/// Modifying operations on persistent wallet data with access to encrypted data
pub trait WalletStorageWriteUnlocked: WalletStorageReadUnlocked + WalletStorageWriteLocked {
pub trait WalletStorageWriteUnlocked: WalletStorageWriteLocked {
fn set_root_key(&mut self, content: &RootKeys) -> Result<()>;
fn del_root_key(&mut self) -> Result<()>;
fn set_seed_phrase(&mut self, seed_phrase: SerializableSeedPhrase) -> Result<()>;
Expand All @@ -221,6 +221,13 @@ pub trait WalletStorageEncryptionWrite {
fn encrypt_seed_phrase(&mut self, new_encryption_key: &Option<SymmetricKey>) -> Result<()>;
}

pub trait WalletStorageReadWriteLocked: WalletStorageReadLocked + WalletStorageWriteLocked {}

pub trait WalletStorageReadWriteUnlocked:
WalletStorageReadUnlocked + WalletStorageWriteUnlocked + WalletStorageReadWriteLocked
{
}

/// Marker trait for types where read/write operations are run in a transaction
pub trait IsTransaction: is_transaction_seal::Seal {}

Expand All @@ -237,7 +244,7 @@ pub trait TransactionRoUnlocked: WalletStorageReadUnlocked + IsTransaction {
}

/// Operations on read-write transactions
pub trait TransactionRwLocked: WalletStorageWriteLocked + IsTransaction {
pub trait TransactionRwLocked: WalletStorageReadWriteLocked + IsTransaction {
/// Abort the transaction
fn abort(self);

Expand All @@ -246,7 +253,7 @@ pub trait TransactionRwLocked: WalletStorageWriteLocked + IsTransaction {
}

/// Operations on read-write transactions
pub trait TransactionRwUnlocked: WalletStorageWriteUnlocked + IsTransaction {
pub trait TransactionRwUnlocked: WalletStorageReadWriteUnlocked + IsTransaction {
/// Abort the transaction
fn abort(self);

Expand Down Expand Up @@ -284,7 +291,10 @@ pub trait Transactional<'t> {
) -> Result<Self::TransactionRwUnlocked>;
}

pub trait WalletStorage: WalletStorageWriteLocked + for<'tx> Transactional<'tx> + Send {}
pub trait WalletStorage:
WalletStorageWriteLocked + WalletStorageReadLocked + for<'tx> Transactional<'tx> + Send
{
}

pub type DefaultBackend = storage_sqlite::Sqlite;
pub type WalletStorageTxRwImpl<'st> = StoreTxRw<'st, storage_sqlite::Sqlite>;

0 comments on commit cb95f7d

Please sign in to comment.