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

change(state): Insert only the first tree in each series of identical trees into finalized state #7266

Merged
merged 21 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7e6d483
Pass ZebraDB to batch preparation
upbqdn Jul 19, 2023
b9726b4
Dedup the insertion of Sapling trees into database
upbqdn Jul 19, 2023
1f747b8
Dedup the insertion of Orchard trees into database
upbqdn Jul 19, 2023
87cfcd5
Update snapshots
upbqdn Jul 19, 2023
ff59e14
Rename batch preparation of trees
upbqdn Jul 19, 2023
08fc14b
Simplify the naming of note commitment trees
upbqdn Jul 19, 2023
3f31bdc
Correctly retrieve Sapling trees from fin state
upbqdn Jul 19, 2023
bd56276
Correctly retrieve Orchard trees from fin state
upbqdn Jul 19, 2023
5dce24f
Simplify the naming of methods for Sprout trees
upbqdn Jul 19, 2023
a943ac3
Simplify the naming of methods for Sapling trees
upbqdn Jul 19, 2023
1b8a243
Simplify the naming of methods for Orchard trees
upbqdn Jul 19, 2023
5c022aa
Reduce disk reads by caching trees. (#7276)
arya2 Jul 26, 2023
ef6a863
Bump the state minor version
upbqdn Jul 26, 2023
678534e
Reset the state patch version
upbqdn Jul 26, 2023
dafb753
Simplify the preparation of genesis trees
upbqdn Jul 26, 2023
52ed3e1
Store the roots of the trees of the genesis block
upbqdn Jul 26, 2023
e19f365
Add the genesis roots to snapshots
upbqdn Jul 26, 2023
e607c3c
Merge branch 'main' into dedup-tree-insertion-fin-state
upbqdn Aug 7, 2023
65f1cb5
Merge branch 'main' into dedup-tree-insertion-fin-state
upbqdn Aug 8, 2023
8ec4631
fix(test): Don't include shielded data in genesis blocks (#7302)
upbqdn Aug 8, 2023
9cee028
fix(state): Fix marking format upgrades (#7304)
upbqdn Aug 8, 2023
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
2 changes: 1 addition & 1 deletion zebra-state/src/service/check/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn fetch_sprout_final_treestates(

let input_tree = parent_chain
.and_then(|chain| chain.sprout_trees_by_anchor.get(&joinsplit.anchor).cloned())
.or_else(|| finalized_state.sprout_note_commitment_tree_by_anchor(&joinsplit.anchor));
.or_else(|| finalized_state.sprout_tree_by_anchor(&joinsplit.anchor));

if let Some(input_tree) = input_tree {
sprout_final_treestates.insert(joinsplit.anchor, input_tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
upbqdn marked this conversation as resolved.
Show resolved Hide resolved
KV(
k: "000001",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
KV(
k: "000002",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
upbqdn marked this conversation as resolved.
Show resolved Hide resolved
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
KV(
k: "000002",
v: "0001ae2935f1dfd8a24aed7c70df7de3a668eb7a49b1319880dde2bbd9031ae5d82f",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
KV(
k: "000002",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ expression: cf_data
k: "000000",
v: "0000",
),
KV(
k: "000001",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
KV(
k: "000002",
v: "0001fbc2f4300c01f0b7820d00e3347c8da4ee614674376cbc45359daa54f9b5493e",
),
]
29 changes: 12 additions & 17 deletions zebra-state/src/service/finalized_state/zebra_db/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,28 @@ impl ZebraDb {
}))
}

/// Returns the Sapling
/// [`NoteCommitmentTree`](sapling::tree::NoteCommitmentTree) specified by a
/// hash or height, if it exists in the finalized `db`.
/// Returns the Sapling [`note commitment tree`](sapling::tree::NoteCommitmentTree) specified by
/// a hash or height, if it exists in the finalized state.
#[allow(clippy::unwrap_in_result)]
pub fn sapling_tree(
pub fn sapling_tree_by_hash_or_height(
&self,
hash_or_height: HashOrHeight,
) -> Option<Arc<sapling::tree::NoteCommitmentTree>> {
let height = hash_or_height.height_or_else(|hash| self.height(hash))?;

let sapling_tree_handle = self.db.cf_handle("sapling_note_commitment_tree").unwrap();

self.db.zs_get(&sapling_tree_handle, &height)
self.sapling_tree_by_height(&height)
}

/// Returns the Orchard
/// [`NoteCommitmentTree`](orchard::tree::NoteCommitmentTree) specified by a
/// hash or height, if it exists in the finalized `db`.
/// Returns the Orchard [`note commitment tree`](orchard::tree::NoteCommitmentTree) specified by
/// a hash or height, if it exists in the finalized state.
#[allow(clippy::unwrap_in_result)]
pub fn orchard_tree(
pub fn orchard_tree_by_hash_or_height(
&self,
hash_or_height: HashOrHeight,
) -> Option<Arc<orchard::tree::NoteCommitmentTree>> {
let height = hash_or_height.height_or_else(|hash| self.height(hash))?;

let orchard_tree_handle = self.db.cf_handle("orchard_note_commitment_tree").unwrap();

self.db.zs_get(&orchard_tree_handle, &height)
self.orchard_tree_by_height(&height)
}
upbqdn marked this conversation as resolved.
Show resolved Hide resolved

// Read tip block methods
Expand Down Expand Up @@ -375,7 +369,7 @@ impl ZebraDb {

// In case of errors, propagate and do not write the batch.
batch.prepare_block_batch(
&self.db,
self,
&finalized,
new_outputs_by_out_loc,
spent_utxos_by_outpoint,
Expand Down Expand Up @@ -426,14 +420,15 @@ impl DiskWriteBatch {
#[allow(clippy::too_many_arguments)]
pub fn prepare_block_batch(
&mut self,
db: &DiskDb,
zebra_db: &ZebraDb,
finalized: &SemanticallyVerifiedBlockWithTrees,
new_outputs_by_out_loc: BTreeMap<OutputLocation, transparent::Utxo>,
spent_utxos_by_outpoint: HashMap<transparent::OutPoint, transparent::Utxo>,
spent_utxos_by_out_loc: BTreeMap<OutputLocation, transparent::Utxo>,
address_balances: HashMap<transparent::Address, AddressBalanceLocation>,
value_pool: ValueBalance<NonNegative>,
) -> Result<(), BoxError> {
let db = &zebra_db.db;
// Commit block and transaction data.
// (Transaction indexes, note commitments, and UTXOs are committed later.)
self.prepare_block_header_and_transaction_data_batch(db, &finalized.verified)?;
Expand Down Expand Up @@ -462,7 +457,7 @@ impl DiskWriteBatch {
)?;
self.prepare_shielded_transaction_batch(db, &finalized.verified)?;

self.prepare_note_commitment_batch(db, finalized)?;
self.prepare_trees_batch(zebra_db, finalized)?;

// Commit UTXOs and value pools
self.prepare_chain_value_pools_batch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
//
// We only store the sprout tree for the tip by height, so we can't check sprout here.
let sapling_tree = state
.sapling_note_commitment_tree_by_height(&block::Height::MIN)
.sapling_tree_by_height(&block::Height::MIN)
.expect("the genesis block in the database has a Sapling tree");
let orchard_tree = state
.orchard_note_commitment_tree_by_height(&block::Height::MIN)
.orchard_tree_by_height(&block::Height::MIN)
.expect("the genesis block in the database has an Orchard tree");

assert_eq!(*sapling_tree, sapling::tree::NoteCommitmentTree::default());
Expand All @@ -243,13 +243,13 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {

// Shielded

let stored_sprout_trees = state.sprout_note_commitments_full_map();
let stored_sprout_trees = state.sprout_trees_full_map();
let mut stored_sapling_trees = Vec::new();
let mut stored_orchard_trees = Vec::new();

let sprout_tree_at_tip = state.sprout_note_commitment_tree();
let sapling_tree_at_tip = state.sapling_note_commitment_tree();
let orchard_tree_at_tip = state.orchard_note_commitment_tree();
let sprout_tree_at_tip = state.sprout_tree();
let sapling_tree_at_tip = state.sapling_tree();
let orchard_tree_at_tip = state.orchard_tree();

// Test the history tree.
//
Expand Down Expand Up @@ -278,10 +278,10 @@ fn snapshot_block_and_transaction_data(state: &FinalizedState) {
//
// TODO: test the rest of the shielded data (anchors, nullifiers)
let sapling_tree_by_height = state
.sapling_note_commitment_tree_by_height(&query_height)
.sapling_tree_by_height(&query_height)
.expect("heights up to tip have Sapling trees");
let orchard_tree_by_height = state
.orchard_note_commitment_tree_by_height(&query_height)
.orchard_tree_by_height(&query_height)
.expect("heights up to tip have Orchard trees");

// We don't need to snapshot the heights,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ expression: stored_orchard_trees
inner: Frontier(
frontier: None,
),
cached_root: Some(Root(Base(
bytes: (174, 41, 53, 241, 223, 216, 162, 74, 237, 124, 112, 223, 125, 227, 166, 104, 235, 122, 73, 177, 49, 152, 128, 221, 226, 187, 217, 3, 26, 229, 216, 47),
))),
cached_root: None,
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
)),
(Height(2), NoteCommitmentTree(
inner: Frontier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ expression: stored_orchard_trees
inner: Frontier(
frontier: None,
),
cached_root: Some(Root(Base(
bytes: (174, 41, 53, 241, 223, 216, 162, 74, 237, 124, 112, 223, 125, 227, 166, 104, 235, 122, 73, 177, 49, 152, 128, 221, 226, 187, 217, 3, 26, 229, 216, 47),
))),
cached_root: None,
)),
(Height(2), NoteCommitmentTree(
inner: Frontier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ expression: stored_sapling_trees
inner: Frontier(
frontier: None,
),
cached_root: Some(Root(Fq(
bytes: (251, 194, 244, 48, 12, 1, 240, 183, 130, 13, 0, 227, 52, 124, 141, 164, 238, 97, 70, 116, 55, 108, 188, 69, 53, 157, 170, 84, 249, 181, 73, 62),
))),
cached_root: None,
)),
(Height(2), NoteCommitmentTree(
inner: Frontier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ expression: stored_sapling_trees
inner: Frontier(
frontier: None,
),
cached_root: Some(Root(Fq(
bytes: (251, 194, 244, 48, 12, 1, 240, 183, 130, 13, 0, 227, 52, 124, 141, 164, 238, 97, 70, 116, 55, 108, 188, 69, 53, 157, 170, 84, 249, 181, 73, 62),
))),
cached_root: None,
)),
(Height(2), NoteCommitmentTree(
inner: Frontier(
Expand Down
Loading
Loading