Skip to content

Commit 44d124a

Browse files
committed
Refactor SyncState for zcash tests
1 parent e111d2d commit 44d124a

28 files changed

+310
-582
lines changed

components/brave_wallet/browser/brave_wallet_service.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,13 @@ BraveWalletService::BraveWalletService(
211211

212212
if (IsZCashEnabled()) {
213213
zcash_wallet_service_ = std::make_unique<ZCashWalletService>(
214-
delegate_->GetWalletBaseDirectory().AppendASCII(kZCashDataFolderName),
215-
*keyring_service(), network_manager(), url_loader_factory);
214+
*keyring_service(),
215+
std::make_unique<ZCashRpc>(network_manager(), url_loader_factory));
216+
zcash_wallet_service_->SetupSyncState(
217+
OrchardSyncState::CreateSyncStateSequence(),
218+
OrchardSyncState::CreateSyncState(
219+
delegate_->GetWalletBaseDirectory().AppendASCII(
220+
kZCashDataFolderName)));
216221
}
217222

218223
if (IsCardanoEnabled()) {

components/brave_wallet/browser/internal/orchard_storage/orchard_storage.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "base/check.h"
1616
#include "base/containers/to_vector.h"
1717
#include "base/files/file_util.h"
18+
#include "base/sequence_checker.h"
1819
#include "base/threading/scoped_blocking_call.h"
1920
#include "base/types/expected_macros.h"
2021
#include "sql/meta_table.h"
@@ -157,7 +158,10 @@ OrchardStorage::AccountMeta& OrchardStorage::AccountMeta::operator=(
157158

158159
OrchardStorage::OrchardStorage(const base::FilePath& path_to_database)
159160
: db_file_path_(path_to_database),
160-
database_(sql::Database::Tag("OrchardStorageDatabase")) {}
161+
database_(sql::Database::Tag("OrchardStorageDatabase")) {
162+
// Instantiated on default sequence. Operates on dedicated sequence.
163+
DETACH_FROM_SEQUENCE(sequence_checker_);
164+
}
161165

162166
OrchardStorage::~OrchardStorage() {
163167
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

components/brave_wallet/browser/internal/orchard_storage/orchard_storage_unittest.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class OrchardStorageTest : public testing::Test {
2626

2727
void OrchardStorageTest::SetUp() {
2828
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29-
base::FilePath db_path(
30-
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("orchard.db")));
31-
orchard_storage_ = std::make_unique<OrchardStorage>(db_path);
29+
orchard_storage_ = std::make_unique<OrchardStorage>(
30+
temp_dir_.GetPath().AppendASCII("orchard.db"));
3231
orchard_storage_->EnsureDbInit();
3332
}
3433

components/brave_wallet/browser/internal/orchard_sync_state.cc

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,28 @@
1111
#include "base/check.h"
1212
#include "base/check_is_test.h"
1313
#include "base/containers/extend.h"
14+
#include "base/task/thread_pool.h"
1415
#include "base/types/expected_macros.h"
15-
#include "brave/components/brave_wallet/browser/internal/orchard_storage/orchard_shard_tree_types.h"
1616
#include "brave/components/brave_wallet/common/zcash_utils.h"
1717

1818
namespace brave_wallet {
1919

20+
namespace {
21+
inline constexpr char kOrchardDatabaseName[] = "orchard.db";
22+
}
23+
24+
// static
25+
scoped_refptr<base::SequencedTaskRunner>
26+
OrchardSyncState::CreateSyncStateSequence() {
27+
return base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()});
28+
}
29+
30+
// static
31+
std::unique_ptr<OrchardSyncState> OrchardSyncState::CreateSyncState(
32+
const base::FilePath& zcash_data_path) {
33+
return std::make_unique<OrchardSyncState>(zcash_data_path);
34+
}
35+
2036
OrchardSyncState::SpendableNotesBundle::SpendableNotesBundle() = default;
2137
OrchardSyncState::SpendableNotesBundle::~SpendableNotesBundle() = default;
2238
OrchardSyncState::SpendableNotesBundle::SpendableNotesBundle(
@@ -25,8 +41,13 @@ OrchardSyncState::SpendableNotesBundle&
2541
OrchardSyncState::SpendableNotesBundle::operator=(
2642
OrchardSyncState::SpendableNotesBundle&&) = default;
2743

28-
OrchardSyncState::OrchardSyncState(const base::FilePath& path_to_database)
29-
: storage_(OrchardStorage(path_to_database)) {}
44+
OrchardSyncState::OrchardSyncState(
45+
const base::FilePath& path_to_database_folder)
46+
: storage_(OrchardStorage(
47+
path_to_database_folder.AppendASCII(kOrchardDatabaseName))) {
48+
// Instantiated on default sequence. Operates on dedicated sequence.
49+
DETACH_FROM_SEQUENCE(sequence_checker_);
50+
}
3051

3152
OrchardSyncState::~OrchardSyncState() {
3253
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

components/brave_wallet/browser/internal/orchard_sync_state.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313

1414
#include "base/sequence_checker.h"
15+
#include "base/threading/sequence_bound.h"
1516
#include "brave/components/brave_wallet/browser/internal/orchard_block_scanner.h"
1617
#include "brave/components/brave_wallet/browser/internal/orchard_storage/orchard_shard_tree_types.h"
1718
#include "brave/components/brave_wallet/browser/internal/orchard_storage/orchard_storage.h"
@@ -29,6 +30,15 @@ inline constexpr uint32_t kZCashPublicAddressMinConfirmations = 10u;
2930
// commitment tree, which is used to sign notes for spending.
3031
class OrchardSyncState {
3132
public:
33+
using SequenceBound = base::SequenceBound<std::unique_ptr<OrchardSyncState>>;
34+
35+
// Creates a new sequence for an instance of OrchardSyncState to be bound to.
36+
static scoped_refptr<base::SequencedTaskRunner> CreateSyncStateSequence();
37+
38+
// Creates a new sync state with the given data path.
39+
static std::unique_ptr<OrchardSyncState> CreateSyncState(
40+
const base::FilePath& zcash_data_path);
41+
3242
struct SpendableNotesBundle {
3343
SpendableNotesBundle();
3444
SpendableNotesBundle(const SpendableNotesBundle&) = delete;
@@ -41,7 +51,7 @@ class OrchardSyncState {
4151
std::optional<uint32_t> anchor_block_id;
4252
};
4353

44-
explicit OrchardSyncState(const base::FilePath& path_to_database);
54+
explicit OrchardSyncState(const base::FilePath& path_to_database_folder);
4555
virtual ~OrchardSyncState();
4656

4757
base::expected<OrchardStorage::Result, OrchardStorage::Error> RegisterAccount(

components/brave_wallet/browser/internal/orchard_sync_state_unittest.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ void OrchardSyncStateTest::SetUp() {
6060
mojom::KeyringId::kZCashMainnet,
6161
mojom::AccountKind::kDerived, 0);
6262
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
63-
base::FilePath db_path(
64-
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("orchard.db")));
65-
sync_state_ = std::make_unique<OrchardSyncState>(db_path);
63+
sync_state_ = std::make_unique<OrchardSyncState>(temp_dir_.GetPath());
6664
sync_state_->OverrideShardTreeForTesting(
6765
account_id_, orchard::CreateShardTreeForTesting(
6866
sync_state_->orchard_storage(), account_id_));

components/brave_wallet/browser/zcash/zcash_action_context.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace brave_wallet {
1212
ZCashActionContext::ZCashActionContext(
1313
ZCashRpc& zcash_rpc,
1414
const std::optional<OrchardAddrRawPart>& account_internal_addr,
15-
base::SequenceBound<OrchardSyncState>& sync_state,
15+
OrchardSyncState::SequenceBound& sync_state,
1616
const mojom::AccountIdPtr& account_id)
1717
: zcash_rpc(zcash_rpc),
1818
account_internal_addr(account_internal_addr),

components/brave_wallet/browser/zcash/zcash_action_context.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@
99
#include <string>
1010

1111
#include "base/memory/raw_ref.h"
12-
#include "base/threading/sequence_bound.h"
12+
#include "brave/components/brave_wallet/browser/internal/orchard_sync_state.h"
1313
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
1414
#include "brave/components/brave_wallet/common/buildflags/buildflags.h"
1515
#include "brave/components/brave_wallet/common/zcash_utils.h"
1616

1717
namespace brave_wallet {
1818

19-
class OrchardSyncState;
2019
class ZCashRpc;
2120

2221
// Basic context required by most orchard-related operations.
2322
struct ZCashActionContext {
2423
ZCashActionContext(
2524
ZCashRpc& zcash_rpc,
2625
const std::optional<OrchardAddrRawPart>& account_internal_addr,
27-
base::SequenceBound<OrchardSyncState>& sync_state,
26+
OrchardSyncState::SequenceBound& sync_state,
2827
const mojom::AccountIdPtr& account_id);
2928
~ZCashActionContext();
3029
raw_ref<ZCashRpc> zcash_rpc;
3130
std::optional<OrchardAddrRawPart> account_internal_addr;
32-
raw_ref<base::SequenceBound<OrchardSyncState>> sync_state;
31+
raw_ref<OrchardSyncState::SequenceBound> sync_state;
3332
ZCashActionContext(ZCashActionContext&) = delete;
3433
ZCashActionContext& operator=(ZCashActionContext&) = delete;
3534
ZCashActionContext& operator=(ZCashActionContext&&);

components/brave_wallet/browser/zcash/zcash_auto_sync_manager_unittest.cc

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
#include "base/files/scoped_temp_dir.h"
1212
#include "base/test/task_environment.h"
1313
#include "brave/components/brave_wallet/browser/pref_names.h"
14-
#include "brave/components/brave_wallet/browser/zcash/zcash_action_context.h"
14+
#include "brave/components/brave_wallet/browser/zcash/zcash_test_utils.h"
1515
#include "brave/components/brave_wallet/browser/zcash/zcash_wallet_service.h"
1616
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
1717
#include "brave/components/brave_wallet/common/common_utils.h"
18-
#include "brave/components/brave_wallet/common/zcash_utils.h"
1918
#include "components/sync_preferences/testing_pref_service_syncable.h"
2019
#include "testing/gmock/include/gmock/gmock.h"
2120
#include "testing/gtest/include/gtest/gtest.h"
@@ -26,14 +25,9 @@ using testing::_;
2625

2726
namespace {
2827

29-
class MockZCashWalletService : public ZCashWalletService {
28+
class MockZCashWalletService : public TestingZCashWalletService {
3029
public:
31-
MockZCashWalletService(base::FilePath zcash_data_path,
32-
KeyringService& keyring_service,
33-
std::unique_ptr<ZCashRpc> zcash_rpc)
34-
: ZCashWalletService(zcash_data_path,
35-
keyring_service,
36-
std::move(zcash_rpc)) {}
30+
using TestingZCashWalletService::TestingZCashWalletService;
3731

3832
MOCK_METHOD2(GetChainTipStatus,
3933
void(mojom::AccountIdPtr account_id,
@@ -54,28 +48,24 @@ class ZCashAutoSyncManagerTest : public testing::Test {
5448

5549
void SetUp() override {
5650
ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
57-
base::FilePath db_path(
58-
temp_directory_.GetPath().Append(FILE_PATH_LITERAL("orchard.db")));
59-
zcash_rpc_ = std::make_unique<ZCashRpc>(nullptr, nullptr);
6051
brave_wallet::RegisterProfilePrefs(prefs_.registry());
6152
brave_wallet::RegisterLocalStatePrefs(local_state_.registry());
6253
keyring_service_ =
6354
std::make_unique<KeyringService>(nullptr, &prefs_, &local_state_);
6455
mock_zcash_wallet_service_ = std::make_unique<MockZCashWalletService>(
65-
db_path, *keyring_service_,
66-
std::make_unique<ZCashRpc>(nullptr, nullptr));
56+
*keyring_service_, std::make_unique<ZCashRpc>(nullptr, nullptr));
57+
mock_zcash_wallet_service_->SetupSyncState(
58+
OrchardSyncState::CreateSyncStateSequence(),
59+
OrchardSyncState::CreateSyncState(temp_directory_.GetPath()));
60+
6761
account_id_ = MakeIndexBasedAccountId(mojom::CoinType::ZEC,
6862
mojom::KeyringId::kZCashMainnet,
6963
mojom::AccountKind::kDerived, 0);
7064
zcash_auto_sync_manager_ = std::make_unique<ZCashAutoSyncManager>(
7165
*mock_zcash_wallet_service_,
72-
ZCashActionContext(*zcash_rpc_, OrchardAddrRawPart(),
73-
mock_zcash_wallet_service_->sync_state(),
74-
account_id_));
66+
mock_zcash_wallet_service_->CreateActionContext(account_id_));
7567
}
7668

77-
void TearDown() override { sync_state_.SynchronouslyResetForTest(); }
78-
7969
base::test::TaskEnvironment& task_environment() { return task_environment_; }
8070

8171
MockZCashWalletService& mock_zcash_wallet_service() {
@@ -87,16 +77,14 @@ class ZCashAutoSyncManagerTest : public testing::Test {
8777
}
8878

8979
private:
80+
base::test::TaskEnvironment task_environment_;
9081
base::ScopedTempDir temp_directory_;
9182
sync_preferences::TestingPrefServiceSyncable prefs_;
9283
sync_preferences::TestingPrefServiceSyncable local_state_;
9384
mojom::AccountIdPtr account_id_;
94-
base::SequenceBound<OrchardSyncState> sync_state_;
9585
std::unique_ptr<KeyringService> keyring_service_;
96-
std::unique_ptr<ZCashRpc> zcash_rpc_;
9786
std::unique_ptr<MockZCashWalletService> mock_zcash_wallet_service_;
9887
std::unique_ptr<ZCashAutoSyncManager> zcash_auto_sync_manager_;
99-
base::test::TaskEnvironment task_environment_;
10088
};
10189

10290
TEST_F(ZCashAutoSyncManagerTest, InitialSync) {

components/brave_wallet/browser/zcash/zcash_blocks_batch_scan_task.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "base/check_op.h"
1515
#include "base/containers/extend.h"
1616
#include "base/strings/string_number_conversions.h"
17-
#include "brave/components/brave_wallet/browser/zcash/zcash_wallet_service.h"
17+
#include "brave/components/brave_wallet/browser/zcash/zcash_rpc.h"
1818

1919
namespace brave_wallet {
2020

0 commit comments

Comments
 (0)