Skip to content

Commit

Permalink
Merge pull request #16 from ChainSafe/willem/serialization
Browse files Browse the repository at this point in the history
Wallet Serialization
  • Loading branch information
ec2 authored Sep 16, 2024
2 parents 868cb9b + b04e3d8 commit aaa5c07
Show file tree
Hide file tree
Showing 22 changed files with 2,231 additions and 51 deletions.
86 changes: 82 additions & 4 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,6 @@ impl AccountBirthday {
///
/// This API is intended primarily to be used in testing contexts; under normal circumstances,
/// [`AccountBirthday::from_treestate`] should be used instead.
#[cfg(any(test, feature = "test-dependencies"))]
pub fn from_parts(prior_chain_state: ChainState, recover_until: Option<BlockHeight>) -> Self {
Self {
prior_chain_state,
Expand Down Expand Up @@ -1732,6 +1731,11 @@ impl AccountBirthday {
self.recover_until
}

/// Returns the chain state prior to the birthday height of the account.
pub fn prior_chain_state(&self) -> &ChainState {
&self.prior_chain_state
}

#[cfg(any(test, feature = "test-dependencies"))]
/// Constructs a new [`AccountBirthday`] at the given network upgrade's activation,
/// with no "recover until" height.
Expand Down
15 changes: 13 additions & 2 deletions zcash_client_memory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nonempty.workspace = true
prost.workspace = true
group.workspace = true
jubjub.workspace = true
serde = { workspace = true, features = ["rc"] }

# - Secret management
secrecy.workspace = true
Expand All @@ -51,10 +52,20 @@ shardtree = { workspace = true, features = ["legacy-api"] }
thiserror = "1.0.61"

rayon.workspace = true
serde_with = "3.9.0"

# - Test dependencies
proptest = { workspace = true, optional = true }

[dev-dependencies]
ciborium = "0.2.2"
serde_json.workspace = true
postcard = { version = "1.0.10", features = ["alloc"] }


[features]
default = ["multicore"]

local-consensus = ["zcash_protocol/local-consensus"]
## Enables multithreading support for creating proofs and building subtrees.
multicore = ["zcash_primitives/multicore"]

Expand All @@ -63,7 +74,7 @@ multicore = ["zcash_primitives/multicore"]
orchard = ["dep:orchard", "zcash_client_backend/orchard", "zcash_keys/orchard"]

## Exposes APIs that are useful for testing, such as `proptest` strategies.
test-dependencies = ["incrementalmerkletree/test-dependencies", "zcash_primitives/test-dependencies", "zcash_client_backend/test-dependencies", "incrementalmerkletree/test-dependencies"]
test-dependencies = ["dep:proptest", "incrementalmerkletree/test-dependencies", "shardtree/test-dependencies", "zcash_primitives/test-dependencies", "zcash_client_backend/test-dependencies", "incrementalmerkletree/test-dependencies"]

## Enables receiving transparent funds and sending to transparent recipients
transparent-inputs = ["dep:bip32", "zcash_keys/transparent-inputs", "zcash_client_backend/transparent-inputs"]
Expand Down
41 changes: 35 additions & 6 deletions zcash_client_memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
use incrementalmerkletree::{Address, Marking, Position, Retention};
use scanning::ScanQueue;

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, FromInto};
use shardtree::{
store::{memory::MemoryShardStore, ShardStore as _},
ShardTree,
};
use std::{
collections::{hash_map::Entry, BTreeMap, BTreeSet},
collections::{btree_map::Entry, BTreeMap, BTreeSet},
num::NonZeroU32,
ops::{Range, RangeInclusive},
};
Expand All @@ -16,10 +18,12 @@ use zcash_protocol::{
consensus::{self, NetworkUpgrade},
ShieldedProtocol,
};

use zip32::fingerprint::SeedFingerprint;

use zcash_primitives::{consensus::BlockHeight, transaction::TxId};

use zcash_client_backend::data_api::SAPLING_SHARD_HEIGHT;
use zcash_client_backend::{
data_api::{
scanning::{ScanPriority, ScanRange},
Expand All @@ -29,8 +33,6 @@ use zcash_client_backend::{
wallet::{NoteId, WalletSaplingOutput},
};

use zcash_client_backend::data_api::SAPLING_SHARD_HEIGHT;

#[cfg(feature = "orchard")]
use zcash_client_backend::{data_api::ORCHARD_SHARD_HEIGHT, wallet::WalletOrchardOutput};

Expand All @@ -54,13 +56,18 @@ pub(crate) const PRUNING_DEPTH: u32 = 100;
/// The number of blocks to verify ahead when the chain tip is updated.
pub(crate) const VERIFY_LOOKAHEAD: u32 = 10;

use types::serialization::*;

/// The main in-memory wallet database. Implements all the traits needed to be used as a backend.
#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct MemoryWalletDb<P: consensus::Parameters> {
#[serde(skip)]
params: P,
accounts: Accounts,
#[serde_as(as = "BTreeMap<FromInto<u32>, _>")]
blocks: BTreeMap<BlockHeight, MemoryWalletBlock>,
tx_table: TransactionTable,

received_notes: ReceivedNoteTable,
received_note_spends: ReceievdNoteSpends,
nullifiers: NullifierMap,
Expand All @@ -69,25 +76,27 @@ pub struct MemoryWalletDb<P: consensus::Parameters> {
sent_notes: SentNoteTable,

tx_locator: TxLocatorMap,

scan_queue: ScanQueue,

#[serde_as(as = "MemoryShardTreeDef")]
sapling_tree: ShardTree<
MemoryShardStore<sapling::Node, BlockHeight>,
{ SAPLING_SHARD_HEIGHT * 2 },
SAPLING_SHARD_HEIGHT,
>,
/// Stores the block height corresponding to the last note commitment in a shard
#[serde_as(as = "BTreeMap<TreeAddressDef, FromInto<u32>>")]
sapling_tree_shard_end_heights: BTreeMap<Address, BlockHeight>,

#[cfg(feature = "orchard")]
#[serde_as(as = "MemoryShardTreeDef")]
orchard_tree: ShardTree<
MemoryShardStore<orchard::tree::MerkleHashOrchard, BlockHeight>,
{ ORCHARD_SHARD_HEIGHT * 2 },
ORCHARD_SHARD_HEIGHT,
>,
#[cfg(feature = "orchard")]
/// Stores the block height corresponding to the last note commitment in a shard
#[serde_as(as = "BTreeMap<TreeAddressDef, FromInto<u32>>")]
orchard_tree_shard_end_heights: BTreeMap<Address, BlockHeight>,
}

Expand All @@ -113,6 +122,10 @@ impl<P: consensus::Parameters> MemoryWalletDb<P> {
}
}

pub fn params(&self) -> &P {
&self.params
}

pub(crate) fn add_account(
&mut self,
kind: AccountSource,
Expand Down Expand Up @@ -871,3 +884,19 @@ impl<P: consensus::Parameters> MemoryWalletDb<P> {
}
}
}

#[cfg(test)]
mod test {

use ciborium::into_writer;

use crate::MemoryWalletDb;
#[test]
fn test_empty_wallet_serialization() {
let network = zcash_primitives::consensus::Network::TestNetwork;
let wallet = MemoryWalletDb::new(network, 100);
let mut wallet_ser = vec![];
into_writer(&wallet, &mut wallet_ser).unwrap();
println!("Empty Wallet Size: {}", wallet_ser.len());
}
}
3 changes: 1 addition & 2 deletions zcash_client_memory/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ impl TestCache for MemBlockCache {
type InsertResult = ();

fn block_source(&self) -> &Self::BlockSource {
&self
self
}

fn insert(&mut self, cb: &CompactBlock) -> Self::InsertResult {
self.0.insert(cb.height().into(), cb.clone());
()
}
}

Expand Down
Loading

0 comments on commit aaa5c07

Please sign in to comment.