Skip to content

Commit

Permalink
Shielded inputs support WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Sep 13, 2024
1 parent d2512db commit 72d1078
Show file tree
Hide file tree
Showing 45 changed files with 4,664 additions and 349 deletions.
2 changes: 1 addition & 1 deletion build/commands/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ const util = {
wasInterrupted

if (shouldRunGnGen) {
util.run('gn', ['gen', outputDir, ...extraGnGenOpts, ...internalOpts], options)
util.run('gn', ['gen', '--ide=qtcreator', outputDir, ...extraGnGenOpts, ...internalOpts], options)
}
})
},
Expand Down
10 changes: 10 additions & 0 deletions components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,22 @@ static_library("browser") {

if (enable_orchard) {
sources += [
"zcash/zcash_blocks_batch_scan_task.cc",
"zcash/zcash_blocks_batch_scan_task.h",
"zcash/zcash_create_shield_all_transaction_task.cc",
"zcash/zcash_create_shield_all_transaction_task.h",
"zcash/zcash_orchard_storage.cc",
"zcash/zcash_orchard_storage.h",
"zcash/zcash_orchard_sync_state.cc",
"zcash/zcash_orchard_sync_state.h",
"zcash/zcash_scan_blocks_task.cc",
"zcash/zcash_scan_blocks_task.h",
"zcash/zcash_shield_sync_service.cc",
"zcash/zcash_shield_sync_service.h",
"zcash/zcash_update_subtree_roots_task.cc",
"zcash/zcash_update_subtree_roots_task.h",
"zcash/zcash_verify_chain_state_task.cc",
"zcash/zcash_verify_chain_state_task.h",
]

deps += [
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ if (enable_orchard) {
"orchard_block_scanner.h",
"orchard_bundle_manager.cc",
"orchard_bundle_manager.h",
"orchard_shard_tree_manager.cc",
"orchard_shard_tree_manager.h",
]
deps = [ "//brave/components/brave_wallet/browser/zcash/rust" ]
}
Expand Down
64 changes: 31 additions & 33 deletions components/brave_wallet/browser/internal/orchard_block_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ namespace brave_wallet {
OrchardBlockScanner::Result::Result() = default;

OrchardBlockScanner::Result::Result(std::vector<OrchardNote> discovered_notes,
std::vector<OrchardNullifier> spent_notes)
std::vector<OrchardNullifier> spent_notes,
std::unique_ptr<orchard::OrchardDecodedBlocksBundle> scanned_blocks)
: discovered_notes(std::move(discovered_notes)),
spent_notes(std::move(spent_notes)) {}
found_nullifiers(std::move(spent_notes)),
scanned_blocks(std::move(scanned_blocks)) {}

OrchardBlockScanner::Result::Result(const Result&) = default;
OrchardBlockScanner::Result::Result(OrchardBlockScanner::Result&&) = default;
OrchardBlockScanner::Result& OrchardBlockScanner::Result::operator=(OrchardBlockScanner::Result&&) = default;

OrchardBlockScanner::Result& OrchardBlockScanner::Result::operator=(
const Result&) = default;
// OrchardBlockScanner::Result::Result(const Result&) = default;

// OrchardBlockScanner::Result& OrchardBlockScanner::Result::operator=(
// const Result&) = default;

OrchardBlockScanner::Result::~Result() = default;

Expand All @@ -29,50 +34,43 @@ OrchardBlockScanner::~OrchardBlockScanner() = default;

base::expected<OrchardBlockScanner::Result, OrchardBlockScanner::ErrorCode>
OrchardBlockScanner::ScanBlocks(
std::vector<OrchardNote> known_notes,
FrontierChainState chain_state,
std::vector<zcash::mojom::CompactBlockPtr> blocks) {
std::unique_ptr<orchard::OrchardDecodedBlocksBundle> result =
decoder_->ScanBlocks(chain_state, blocks);
if (!result) {
return base::unexpected(ErrorCode::kInputError);
}

if (!result->GetDiscoveredNotes()) {
return base::unexpected(ErrorCode::kInputError);
}

std::vector<OrchardNullifier> found_nullifiers;
std::vector<OrchardNote> found_notes;
std::vector<OrchardNote> found_notes = result->GetDiscoveredNotes().value();

for (const auto& block : blocks) {
// Scan block using the decoder initialized with the provided fvk
// to find new spendable notes.
auto scan_result = decoder_->ScanBlock(block);
if (!scan_result) {
return base::unexpected(ErrorCode::kDecoderError);
}
found_notes.insert(found_notes.end(), scan_result->begin(),
scan_result->end());
// Place found notes to the known notes list so we can also check for
// nullifiers
known_notes.insert(known_notes.end(), scan_result->begin(),
scan_result->end());
for (const auto& tx : block->vtx) {
// We only scan orchard actions here
for (const auto& orchard_action : tx->orchard_actions) {
if (orchard_action->nullifier.size() != kOrchardNullifierSize) {
return base::unexpected(ErrorCode::kInputError);
}

std::array<uint8_t, kOrchardNullifierSize> action_nullifier;
base::ranges::copy(orchard_action->nullifier, action_nullifier.begin());

OrchardNullifier nullifier;
// Nullifier is a public information about some note being spent.
// Here we are trying to find a known spendable notes which nullifier
// -- Here we are trying to find a known spendable notes which nullifier
// matches nullifier from the processed transaction.
if (std::find_if(known_notes.begin(), known_notes.end(),
[&action_nullifier](const auto& v) {
return v.nullifier == action_nullifier;
}) != known_notes.end()) {
OrchardNullifier nullifier;
nullifier.block_id = block->height;
nullifier.nullifier = action_nullifier;
found_nullifiers.push_back(std::move(nullifier));
}
base::ranges::copy(orchard_action->nullifier,
nullifier.nullifier.begin());
nullifier.block_id = block->height;
found_nullifiers.push_back(std::move(nullifier));
}
}
}
return Result({std::move(found_notes), std::move(found_nullifiers)});

return Result({std::move(found_notes), std::move(found_nullifiers),
std::move(result)});
}

} // namespace brave_wallet
16 changes: 11 additions & 5 deletions components/brave_wallet/browser/internal/orchard_block_scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include <vector>

#include "base/types/expected.h"
#include "brave/components/brave_wallet/browser/internal/orchard_block_scanner.h"
#include "brave/components/brave_wallet/browser/zcash/rust/orchard_block_decoder.h"
#include "brave/components/brave_wallet/browser/zcash/rust/orchard_decoded_blocks_bunde.h"
#include "brave/components/brave_wallet/common/zcash_utils.h"

namespace brave_wallet {
Expand All @@ -26,15 +28,19 @@ class OrchardBlockScanner {
struct Result {
Result();
Result(std::vector<OrchardNote> discovered_notes,
std::vector<OrchardNullifier> spent_notes);
Result(const Result&);
Result& operator=(const Result&);
std::vector<OrchardNullifier> spent_notes,
std::unique_ptr<orchard::OrchardDecodedBlocksBundle> scanned_blocks);
Result(const Result&) = delete;
Result& operator=(const Result&) = delete;
Result(Result&&);
Result& operator=(Result&&);
~Result();

// New notes have been discovered
std::vector<OrchardNote> discovered_notes;
// Nullifiers for the previously discovered notes
std::vector<OrchardNullifier> spent_notes;
std::vector<OrchardNullifier> found_nullifiers;
std::unique_ptr<orchard::OrchardDecodedBlocksBundle> scanned_blocks;
};

explicit OrchardBlockScanner(const OrchardFullViewKey& full_view_key);
Expand All @@ -43,7 +49,7 @@ class OrchardBlockScanner {
// Scans blocks to find incoming notes related to fvk
// Also checks whether existing notes were spent.
virtual base::expected<Result, OrchardBlockScanner::ErrorCode> ScanBlocks(
std::vector<OrchardNote> known_notes,
FrontierChainState chain_state,
std::vector<zcash::mojom::CompactBlockPtr> blocks);

private:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_wallet/browser/internal/orchard_shard_tree_manager.h"

#include "brave/components/brave_wallet/common/zcash_utils.h"

namespace brave_wallet {

// static
std::unique_ptr<OrchardShardTreeManager> OrchardShardTreeManager::Create(
std::unique_ptr<OrchardShardTreeDelegate> delegate) {
auto shard_tree = orchard::OrchardShardTree::Create(std::move(delegate));
if (!shard_tree) {
return nullptr;
}
return std::make_unique<OrchardShardTreeManager>(std::move(shard_tree));
}

OrchardShardTreeManager::OrchardShardTreeManager(
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree> shard_tree) {
orchard_shard_tree_ = std::move(shard_tree);
}

OrchardShardTreeManager::~OrchardShardTreeManager() {}

bool OrchardShardTreeManager::InsertCommitments(
OrchardBlockScanner::Result result) {
return orchard_shard_tree_->ApplyScanResults(
std::move(result.scanned_blocks));
}

} // namespace brave_wallet
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_

#include "base/types/expected.h"
#include "brave/components/brave_wallet/browser/internal/orchard_block_scanner.h"
#include "brave/components/brave_wallet/browser/zcash/rust/orchard_shard_tree.h"
#include "brave/components/brave_wallet/common/zcash_utils.h"

namespace brave_wallet {

class OrchardShardTreeManager {
public:
OrchardShardTreeManager(
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree> shard_tree);
~OrchardShardTreeManager();
bool InsertCommitments(OrchardBlockScanner::Result commitments);

static std::unique_ptr<OrchardShardTreeManager> Create(
std::unique_ptr<OrchardShardTreeDelegate> delegate);

private:
std::unique_ptr<::brave_wallet::orchard::OrchardShardTree>
orchard_shard_tree_;
};

} // namespace brave_wallet

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_INTERNAL_ORCHARD_SHARD_TREE_MANAGER_H_
20 changes: 20 additions & 0 deletions components/brave_wallet/browser/zcash/rust/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ source_set("orchard_headers") {
"authorized_orchard_bundle.h",
"extended_spending_key.h",
"orchard_block_decoder.h",
"orchard_shard_tree.h",
"unauthorized_orchard_bundle.h",
"orchard_decoded_blocks_bunde.h",
]

public_deps = [
Expand All @@ -44,6 +46,10 @@ source_set("orchard_impl") {
"extended_spending_key_impl.h",
"orchard_block_decoder_impl.cc",
"orchard_block_decoder_impl.h",
"orchard_decoded_blocks_bunde_impl.cc",
"orchard_decoded_blocks_bunde_impl.h",
"orchard_shard_tree_impl.cc",
"orchard_shard_tree_impl.h",
"unauthorized_orchard_bundle_impl.cc",
"unauthorized_orchard_bundle_impl.h",
]
Expand All @@ -57,6 +63,16 @@ source_set("orchard_impl") {
]
}

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

public_deps = [
"//base",
"//build/rust:cxx_cppdeps",
]
}

rust_static_library("rust_lib") {
visibility = [ ":orchard_impl" ]

Expand All @@ -69,13 +85,17 @@ rust_static_library("rust_lib") {
deps = [
"librustzcash:zcash_client_backend",
"librustzcash:zcash_primitives",
"librustzcash:zcash_protocol",
"//brave/components/brave_wallet/rust:rust_lib",
"//brave/third_party/rust/byteorder/v1:lib",
"//brave/third_party/rust/incrementalmerkletree/v0_5:lib",
"//brave/third_party/rust/memuse/v0_2:lib",
"//brave/third_party/rust/nonempty/v0_7:lib",
"//brave/third_party/rust/orchard/v0_8:lib",
"//brave/third_party/rust/rand/v0_8:lib",
"//brave/third_party/rust/shardtree/v0_3:lib",
"//brave/third_party/rust/zcash_note_encryption/v0_4:lib",
]

public_deps = [ ":shard_store" ]
}
74 changes: 74 additions & 0 deletions components/brave_wallet/browser/zcash/rust/cxx/src/shard_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_

#include "brave/components/brave_wallet/common/zcash_utils.h"
#include "third_party/rust/cxx/v1/cxx.h"

namespace brave_wallet::orchard {

enum class ShardStoreStatusCode : uint32_t;
struct FfiShardTree;
struct FfiShardAddress;
struct FfiCheckpoint;
struct FfiCap;

using ShardStoreContext = ::brave_wallet::OrchardShardTreeDelegate;

ShardStoreStatusCode orchard_last_shard(const ShardStoreContext& ctx,
FfiShardTree& into);
ShardStoreStatusCode orchard_put_shard(ShardStoreContext& ctx,
const FfiShardTree& tree);
ShardStoreStatusCode orchard_get_shard(const ShardStoreContext& ctx,
const FfiShardAddress& addr,
FfiShardTree& tree);
ShardStoreStatusCode orchard_get_shard_roots(
const ShardStoreContext& ctx,
::rust::Vec<FfiShardAddress>& into);
ShardStoreStatusCode orchard_truncate(ShardStoreContext& ctx,
const FfiShardAddress& address);
ShardStoreStatusCode orchard_get_cap(const ShardStoreContext& ctx,
FfiCap& into);
ShardStoreStatusCode orchard_put_cap(ShardStoreContext& ctx,
const FfiCap& tree);
ShardStoreStatusCode orchard_min_checkpoint_id(const ShardStoreContext& ctx,
uint32_t& into);
ShardStoreStatusCode orchard_max_checkpoint_id(const ShardStoreContext& ctx,
uint32_t& into);
ShardStoreStatusCode orchard_add_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint);
ShardStoreStatusCode orchard_checkpoint_count(const ShardStoreContext& ctx,
size_t& into);
ShardStoreStatusCode orchard_get_checkpoint_at_depth(
const ShardStoreContext& ctx,
size_t depth,
uint32_t& into_checkpoint_id,
FfiCheckpoint& into_checpoint);
ShardStoreStatusCode orchard_get_checkpoint(const ShardStoreContext& ctx,
uint32_t checkpoint_id,
FfiCheckpoint& into);
ShardStoreStatusCode orchard_update_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint);
ShardStoreStatusCode orchard_remove_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id);
ShardStoreStatusCode orchard_truncate_checkpoint(ShardStoreContext& ctx,
uint32_t checkpoint_id);
ShardStoreStatusCode orchard_with_checkpoints(
const ShardStoreContext& ctx,
size_t limit,
rust::cxxbridge1::Fn<ShardStoreStatusCode(uint32_t checkpoint_id,
const FfiCheckpoint& checkpoint)>
fn);
ShardStoreStatusCode orchard_get_checkpoints(const ShardStoreContext& ctx,
size_t limit,
::rust::Vec<FfiCheckpoint>& into);

} // namespace brave_wallet::orchard

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_RUST_CXX_SRC_SHARD_STORE_H_
Loading

0 comments on commit 72d1078

Please sign in to comment.