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

[ZCash ] Implement shard tree and shard storage #26477

Open
wants to merge 14 commits into
base: shard_crate_impl
Choose a base branch
from

Conversation

cypt4
Copy link
Collaborator

@cypt4 cypt4 commented Nov 11, 2024

Resolves brave/brave-browser#39314

Crate pr : #26474

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@cypt4 cypt4 requested a review from a team as a code owner November 11, 2024 17:24
@cypt4 cypt4 requested review from fmarier, a team and bridiver as code owners November 11, 2024 18:53
@cypt4 cypt4 force-pushed the shard_tree_impl_1 branch 3 times, most recently from 2eb3bc5 to 2d67e9d Compare November 11, 2024 20:18
@cypt4 cypt4 changed the title Implelent shard tree and shard storage [ZCash ] Implement shard tree and shard storage Nov 11, 2024
@@ -312,10 +312,14 @@ static_library("browser") {

if (enable_orchard) {
sources += [
"zcash/orchard_shard_tree_delegate_impl.cc",
Copy link
Collaborator

Choose a reason for hiding this comment

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

these really need to move into their own BUILD.gn file in zcash directory

Copy link
Collaborator

Choose a reason for hiding this comment

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

meaning all of the files here, not just the new ones

@@ -57,6 +70,17 @@ source_set("orchard_impl") {
]
}

source_set("shard_store") {
visibility = [ ":*" ]
sources = [ "cxx/src/shard_store.h" ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't want to use cxx directories anymore and it's not clear to me why this would go somewhere other than the existing ochrard_impl target. In a separate PR we should move all the files in orchard_impl into an internal directory

Also generally speaking you shouldn't create a target in one directory that includes files in a subdirectory, especially if those are the only files it includes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's not clear to me why this would go somewhere other than the existing ochrard_impl target.

There will be cycle dependency otherwise rust_lib depends on shard_store.h and orchard_impl depends on rust_lib

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would use qr_code_generator as an example to follow for this which would probably mean shard_store.h moves into this directory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's a bit confusing that this directory is called rust and also contains cpp files, but we should probably rename it in a separate PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved to the zcash/rust directory

// Note witness is a Merkle path in the Orchard commitment tree from the
// note to the tree root according some selected anchor(selected right border in
// the commitment tree).
struct OrchardNoteWitness {
Copy link
Collaborator

@bridiver bridiver Nov 13, 2024

Choose a reason for hiding this comment

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

This entire file is a problem, we should not just be dumping a bunch of structs in here like this. You don't necessarily need a file for each, but they should be organized in some sensible way because this file is a mess

Copy link
Collaborator

Choose a reason for hiding this comment

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

it seems like these probably belong with https://github.com/brave/brave-core/pull/26477/files#r1840956570

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved

std::vector<uint8_t> frontier;
};

class OrchardShardTreeDelegate {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This definitely should go in its own file

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved to separate file


namespace brave_wallet::orchard {

class OrchardDecodedBlocksBundle {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please provide some comments for these new classes and also the need unit tests

OrchardDecodedBlocksBundleImpl::GetDiscoveredNotes() {
std::vector<OrchardNote> result;

for (size_t i = 0; i < batch_decode_result_->size(); i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How big could this list be? Is there an upper bound?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should be calling result.reserve(batch_decode_result_->size()) before looping.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shouldn't be too big, this is sort of incoming transactions, in most cases it should be less than 10, i guess

orchard_action->ciphertext.size() != kOrchardCipherTextSize) {
return std::nullopt;
}
std::unique_ptr<OrchardDecodedBlocksBundle> OrchardBlockDecoderImpl::ScanBlocks(
Copy link
Collaborator

Choose a reason for hiding this comment

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

unit tests?

@cypt4 cypt4 force-pushed the shard_tree_impl_1 branch 2 times, most recently from 80425d6 to c754ce2 Compare November 20, 2024 20:30
@cypt4 cypt4 force-pushed the shard_tree_impl_1 branch 2 times, most recently from 443fd78 to b801c0f Compare November 22, 2024 16:17

namespace brave_wallet {

OrchardTreeState::OrchardTreeState() = default;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this does not belong in common. Only code used in both browser and renderer process belongs in common

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved to orchard_types

OrchardShardTreeManager::CalculateWitness(
const std::vector<OrchardInput>& notes,
uint32_t checkpoint_position) {
std::vector<OrchardInput> result;
Copy link
Collaborator

Choose a reason for hiding this comment

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

result.resever(notes.size());

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

return base::unexpected(witness.error());
}
result.push_back(input);
result.back().witness = witness.value();
Copy link
Collaborator

Choose a reason for hiding this comment

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

std::move

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 46 to 51
static std::unique_ptr<OrchardShardTreeManager> Create(
std::unique_ptr<OrchardShardTreeDelegate> delegate);

// Creates shard tree size of 8 for testing.
static std::unique_ptr<OrchardShardTreeManager> CreateForTesting(
std::unique_ptr<OrchardShardTreeDelegate> delegate);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need these factory functions? Do you think it would be helpful if we simplified instantiation by just having it happening in place, rather than having this hidden behind this class?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file was deleted

auto result = CreateResultForTesting(prior_tree_state, commitments);
tree_manager()->InsertCommitments(std::move(result));

{
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a particular reason to have these blocks at the end of the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

Comment on lines 348 to 358
if (i == 2) {
commitments.push_back(CreateCommitment(
CreateMockCommitmentValue(i, kDefaultCommitmentSeed), true,
std::nullopt));
} else if (i == 3) {
commitments.push_back(CreateCommitment(
CreateMockCommitmentValue(i, kDefaultCommitmentSeed), false, 1));
} else if (i == 5) {
commitments.push_back(CreateCommitment(
CreateMockCommitmentValue(i, kDefaultCommitmentSeed), false, 2));
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A switch will make this terser and easier to read, don't you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

~OrchardShardTreeDelegate();

base::expected<std::optional<OrchardShardTreeCap>, Error> GetCap() const;
base::expected<bool, Error> PutCap(const OrchardShardTreeCap& cap);
Copy link
Collaborator

@cdesouza-chromium cdesouza-chromium Nov 27, 2024

Choose a reason for hiding this comment

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

There's an idiom for when you want to signal failure, but you don't have an error code:

base::expected<T, void>

I'm bringing this up because we have an Error enum with a single value. I think using void for type would better communicate the fact that we are passing an error, but we don't have anything useful about the error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file was deleted

Comment on lines 46 to 53
base::ranges::copy(orchard_action->nullifier,
orchard_compact_action.nullifier.begin());
base::ranges::copy(orchard_action->cmx,
orchard_compact_action.cmx.begin());
base::ranges::copy(orchard_action->ephemeral_key,
orchard_compact_action.ephemeral_key.begin());
base::ranges::copy(orchard_action->ciphertext,
orchard_compact_action.enc_cipher_text.begin());
Copy link
Collaborator

Choose a reason for hiding this comment

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

These copies are supposed to be something like:

base::span(orchard_compact_action.nullifier).copy_from(orchard_action->nullifier);
base::span(orchard_compact_action.cmx).copy_from(orchard_action->cmx);
base::span(orchard_compact_action.ephemeral_key).copy_from(orchard_action->ephemeral_key);
base::span(orchard_compact_action.enc_cipher_text).copy_from(orchard_action->ciphertext);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/run-audit-deps Check for known npm/cargo vulnerabilities (audit_deps) CI/run-network-audit Run network-audit CI/run-upstream-tests Run upstream unit and browser tests on Linux and Windows (otherwise only on Linux) CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet/core feature/web3/wallet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants