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

Basic put_block function for the memory wallet #1298

Merged
merged 17 commits into from
Apr 18, 2024

Conversation

oxarbitrage
Copy link
Contributor

@oxarbitrage oxarbitrage commented Mar 21, 2024

I want to create the most basic version of a put_blocks function for the memory wallet, one where we assume that the blocks will be in order.

This PR is for @nuttycom to take a look, it has some comments and questions in the code and also review comments.

Copy link
Contributor Author

@oxarbitrage oxarbitrage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty new to the codebase so i have a lot of unknowns and questions.

Cargo.toml Outdated Show resolved Hide resolved
zcash_client_backend/src/data_api.rs Outdated Show resolved Hide resolved
zcash_client_backend/src/data_api.rs Outdated Show resolved Hide resolved
@@ -98,6 +98,7 @@ impl<AccountId, N> Recipient<AccountId, Option<N>> {
/// The shielded subset of a [`Transaction`]'s data that is relevant to a particular wallet.
///
/// [`Transaction`]: zcash_primitives::transaction::Transaction
#[derive(Clone)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do this for my version of put_blocks as i was not able to make it otherwise, it might be not needed and not wanted.

Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it's taken me a bit to get to commenting on this; it's been a busy time getting Zashi 1.0 out. :)

zcash_client_backend/src/data_api.rs Outdated Show resolved Hide resolved
@@ -236,7 +235,10 @@ impl WalletRead for MemoryWalletDb {
}

fn chain_height(&self) -> Result<Option<BlockHeight>, Self::Error> {
todo!()
match self.blocks.last_key_value() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to take into consideration here is how this is handled in zcash_client_sqlite: in that context, the chain tip may not yet have been scanned, so we get the current chain height as the maximum height known to the scan queue, rather than the blocks table.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks are added to the memory wallet database by put_blocks which currently receives a vector of ScannedBlock. Do we plan to change that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever is the case, ill open a separated PR for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks are added to the memory wallet database by put_blocks which currently receives a vector of ScannedBlock. Do we plan to change that ?

No; the main distinction here is that the chain tip height is used in computing how to prioritize the scanning queue; we scan blocks out-of-order in many cases to improve time to spendability. The important related operation is update_chain_tip; basically, chain_height should return whatever is correct as of the last call to update_chain_tip, not the last call to put_blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In update_chain_tip where do you store the height that is passed by argument so you can use it in chain_height?

fn put_blocks(
&mut self,
// TODO: Figure out what to do with this field.
_from_state: &super::chain::ChainState,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, i added that at a4323c2


self.blocks.insert(block.block_height, memory_block);

// Add the sapling commitments to the sapling tree.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed now that we added the frontier?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; the frontier just provides a starting point, but the note commitments for each note in the block, and the commitment for the block-end checkpoint, must also be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, i added the orchard version of this at 370a4aa

@oxarbitrage oxarbitrage changed the title WIP: Basic put_block function for the memory wallet Basic put_block function for the memory wallet Apr 9, 2024
@oxarbitrage oxarbitrage marked this pull request as ready for review April 9, 2024 19:34
Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The incorrect start to the note commitment tree position is blocking.

Comment on lines 505 to 508
if let Some(value) = from_state.final_sapling_tree().value() {
self.sapling_tree
.batch_insert(value.position(), block_commitments.sapling.into_iter());
}
Copy link
Contributor

@nuttycom nuttycom Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug, in that it doesn't handle constructing the tree starting from Sapling activation properly. I think something like this is necessary. Also, the final position of the previous frontier is one less than the starting position of new note commitments being added to the tree.

Suggested change
if let Some(value) = from_state.final_sapling_tree().value() {
self.sapling_tree
.batch_insert(value.position(), block_commitments.sapling.into_iter());
}
let start_position = from_state.final_sapling_tree().value().map_or(0.into(), |t| t.position() + 1);
self.sapling_tree.batch_insert(start_position, block_commitments.sapling.into_iter());

Comment on lines 511 to 515
// Add the Orchard commitments to the orchard tree.
if let Some(value) = from_state.final_orchard_tree().value() {
self.orchard_tree
.batch_insert(value.position(), block_commitments.orchard.into_iter());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Add the Orchard commitments to the orchard tree.
if let Some(value) = from_state.final_orchard_tree().value() {
self.orchard_tree
.batch_insert(value.position(), block_commitments.orchard.into_iter());
}
{
// Add the Orchard commitments to the orchard tree.
let start_position = from_state.final_orchard_tree().value().map_or(0.into(), |t| t.position() + 1);
self.orchard_tree.batch_insert(start_position, block_commitments.orchard.into_iter());

@oxarbitrage
Copy link
Contributor Author

I fixed the clippy warnings in c9a236e

The orchard tests failures are unrelated to this PR as they are also happening in #1293

Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK c9a236e

@nuttycom nuttycom merged commit 5d2cd6f into zcash:memory_wallet_db Apr 18, 2024
20 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants