Skip to content

Commit

Permalink
[Rewards] Store creator hash prefixes in a separate lookup file
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Feb 28, 2025
1 parent 6e99529 commit f730894
Show file tree
Hide file tree
Showing 39 changed files with 860 additions and 493 deletions.
27 changes: 23 additions & 4 deletions components/brave_rewards/content/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "brave/components/brave_rewards/content/service_sandbox_type.h"
#include "brave/components/brave_rewards/core/buildflags/buildflags.h"
#include "brave/components/brave_rewards/core/engine/global_constants.h"
#include "brave/components/brave_rewards/core/engine/hash_prefix_store.h"
#include "brave/components/brave_rewards/core/engine/parameters/rewards_parameters_provider.h"
#include "brave/components/brave_rewards/core/engine/rewards_database.h"
#include "brave/components/brave_rewards/core/features.h"
Expand Down Expand Up @@ -236,12 +237,14 @@ void DeferCallback(base::Location location, Callback callback, Args&&... args) {
// read comment about file pathes at src\base\files\file_path.h
#if BUILDFLAG(IS_WIN)
const base::FilePath::StringType kDiagnosticLogPath(L"Rewards.log");
const base::FilePath::StringType kCreatorPrefixStore(L"RewardsCreators.db");
const base::FilePath::StringType kLegacy_state(L"ledger_state");
const base::FilePath::StringType kPublisher_state(L"publisher_state");
const base::FilePath::StringType kPublisher_info_db(L"publisher_info_db");
const base::FilePath::StringType kPublishers_list(L"publishers_list");
#else
const base::FilePath::StringType kDiagnosticLogPath("Rewards.log");
const base::FilePath::StringType kCreatorPrefixStore("RewardsCreators.db");
const base::FilePath::StringType kLegacy_state("ledger_state");
const base::FilePath::StringType kPublisher_state("publisher_state");
const base::FilePath::StringType kPublisher_info_db("publisher_info_db");
Expand Down Expand Up @@ -272,9 +275,11 @@ RewardsServiceImpl::RewardsServiceImpl(
publisher_state_path_(profile_path.Append(kPublisher_state)),
publisher_info_db_path_(profile_path.Append(kPublisher_info_db)),
publisher_list_path_(profile_path.Append(kPublishers_list)),
creator_prefix_store_path_(profile_path.Append(kCreatorPrefixStore)),
diagnostic_log_(new DiagnosticLog(profile_path.Append(kDiagnosticLogPath),
kDiagnosticLogMaxFileSize,
kDiagnosticLogKeepNumLines)),
creator_prefix_store_(file_task_runner_),
notification_service_(new RewardsNotificationServiceImpl(prefs)),
conversion_monitor_(prefs) {
ready_ = std::make_unique<base::OneShotEvent>();
Expand Down Expand Up @@ -386,6 +391,8 @@ void RewardsServiceImpl::StartEngineProcessIfNecessary() {
rewards_database_ = base::SequenceBound<internal::RewardsDatabase>(
file_task_runner_, publisher_info_db_path_);

creator_prefix_store_.BindRemote<HashPrefixStore>(creator_prefix_store_path_);

BLOG(1, "Starting engine process");

if (!engine_factory_.is_bound()) {
Expand Down Expand Up @@ -1066,10 +1073,8 @@ void RewardsServiceImpl::OnDiagnosticLogDeletedForCompleteReset(
bool success) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const std::vector<base::FilePath> paths = {
legacy_state_path_,
publisher_state_path_,
publisher_info_db_path_,
publisher_list_path_,
legacy_state_path_, publisher_state_path_, publisher_info_db_path_,
publisher_list_path_, creator_prefix_store_path_,
};
file_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&DeleteFilesOnFileTaskRunner, paths),
Expand All @@ -1092,6 +1097,7 @@ void RewardsServiceImpl::Reset() {
engine_factory_.reset();
ready_ = std::make_unique<base::OneShotEvent>();
rewards_database_.Reset();
creator_prefix_store_.reset();
BLOG(1, "Successfully reset rewards service");
}

Expand Down Expand Up @@ -2103,6 +2109,19 @@ void RewardsServiceImpl::OnRunDBTransaction(
std::move(callback).Run(std::move(response));
}

void RewardsServiceImpl::UpdateCreatorPrefixStore(
mojom::HashPrefixDataPtr prefix_data,
UpdateCreatorPrefixStoreCallback callback) {
creator_prefix_store_->UpdatePrefixes(std::move(prefix_data),
std::move(callback));
}

void RewardsServiceImpl::CreatorPrefixStoreContains(
const std::string& value,
CreatorPrefixStoreContainsCallback callback) {
creator_prefix_store_->ContainsPrefix(value, std::move(callback));
}

void RewardsServiceImpl::ForTestingSetTestResponseCallback(
const GetTestResponseCallback& callback) {
test_response_callback_ = callback;
Expand Down
12 changes: 12 additions & 0 deletions components/brave_rewards/content/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include "brave/components/brave_rewards/content/diagnostic_log.h"
#include "brave/components/brave_rewards/content/rewards_p3a.h"
#include "brave/components/brave_rewards/content/rewards_service.h"
#include "brave/components/brave_rewards/core/mojom/rewards_database.mojom.h"
#include "brave/components/brave_rewards/core/mojom/rewards_engine.mojom.h"
#include "brave/components/brave_rewards/core/remote_worker.h"
#include "brave/components/brave_rewards/core/rewards_flags.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
Expand Down Expand Up @@ -429,6 +431,14 @@ class RewardsServiceImpl final : public RewardsService,
void RunDBTransaction(mojom::DBTransactionPtr transaction,
RunDBTransactionCallback callback) override;

void UpdateCreatorPrefixStore(
mojom::HashPrefixDataPtr prefix_data,
UpdateCreatorPrefixStoreCallback callback) override;

void CreatorPrefixStoreContains(
const std::string& value,
CreatorPrefixStoreContainsCallback callback) override;

void ClearAllNotifications() override;

void ExternalWalletConnected() override;
Expand Down Expand Up @@ -502,9 +512,11 @@ class RewardsServiceImpl final : public RewardsService,
const base::FilePath publisher_state_path_;
const base::FilePath publisher_info_db_path_;
const base::FilePath publisher_list_path_;
const base::FilePath creator_prefix_store_path_;

std::unique_ptr<DiagnosticLog> diagnostic_log_;
base::SequenceBound<internal::RewardsDatabase> rewards_database_;
RemoteWorker<mojom::HashPrefixStore> creator_prefix_store_;
std::unique_ptr<RewardsNotificationServiceImpl> notification_service_;
std::unique_ptr<RewardsServiceObserver> extension_observer_;

Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static_library("core") {
"pref_registry.h",
"publisher_utils.cc",
"publisher_utils.h",
"remote_worker.h",
"rewards_flags.cc",
"rewards_flags.h",
"rewards_util.cc",
Expand All @@ -33,6 +34,7 @@ static_library("core") {
"//base",
"//brave/components/l10n/common",
"//components/prefs",
"//mojo/public/mojom/base",
"//net",
"//url",
]
Expand Down
9 changes: 6 additions & 3 deletions components/brave_rewards/core/engine/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ static_library("engine") {
"database/database_migration.h",
"database/database_publisher_info.cc",
"database/database_publisher_info.h",
"database/database_publisher_prefix_list.cc",
"database/database_publisher_prefix_list.h",
"database/database_recurring_tip.cc",
"database/database_recurring_tip.h",
"database/database_server_publisher_banner.cc",
Expand Down Expand Up @@ -203,6 +201,7 @@ static_library("engine") {
"database/migration/migration_v39.h",
"database/migration/migration_v4.h",
"database/migration/migration_v40.h",
"database/migration/migration_v41.h",
"database/migration/migration_v5.h",
"database/migration/migration_v6.h",
"database/migration/migration_v7.h",
Expand Down Expand Up @@ -313,6 +312,9 @@ static_library("engine") {
"endpoints/zebpay/post_oauth_zebpay.h",
"gemini/gemini.cc",
"gemini/gemini.h",
"hash_prefix_iterator.h",
"hash_prefix_store.cc",
"hash_prefix_store.h",
"initialization_manager.cc",
"initialization_manager.h",
"legacy/bat_state.cc",
Expand Down Expand Up @@ -343,7 +345,6 @@ static_library("engine") {
"publisher/media/media.h",
"publisher/media/youtube.cc",
"publisher/media/youtube.h",
"publisher/prefix_iterator.h",
"publisher/prefix_list_reader.cc",
"publisher/prefix_list_reader.h",
"publisher/prefix_util.cc",
Expand Down Expand Up @@ -379,6 +380,8 @@ static_library("engine") {
"state/state_migration_v13.h",
"state/state_migration_v14.cc",
"state/state_migration_v14.h",
"state/state_migration_v15.cc",
"state/state_migration_v15.h",
"state/state_migration_v2.cc",
"state/state_migration_v2.h",
"state/state_migration_v3.cc",
Expand Down
12 changes: 0 additions & 12 deletions components/brave_rewards/core/engine/database/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Database::Database(RewardsEngine& engine)
external_transactions_(engine),
media_publisher_info_(engine),
publisher_info_(engine),
publisher_prefix_list_(engine),
recurring_tip_(engine),
server_publisher_info_(engine),
sku_order_(engine),
Expand Down Expand Up @@ -358,17 +357,6 @@ void Database::RemoveRecurringTip(const std::string& publisher_key,
/**
* SERVER PUBLISHER INFO
*/
void Database::SearchPublisherPrefixList(
const std::string& publisher_prefix,
SearchPublisherPrefixListCallback callback) {
publisher_prefix_list_.Search(publisher_prefix, std::move(callback));
}

void Database::ResetPublisherPrefixList(publisher::PrefixListReader reader,
ResultCallback callback) {
publisher_prefix_list_.Reset(std::move(reader), std::move(callback));
}

void Database::InsertServerPublisherInfo(
const mojom::ServerPublisherInfo& server_info,
ResultCallback callback) {
Expand Down
9 changes: 0 additions & 9 deletions components/brave_rewards/core/engine/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
#include "brave/components/brave_rewards/core/engine/database/database_initialize.h"
#include "brave/components/brave_rewards/core/engine/database/database_media_publisher_info.h"
#include "brave/components/brave_rewards/core/engine/database/database_publisher_info.h"
#include "brave/components/brave_rewards/core/engine/database/database_publisher_prefix_list.h"
#include "brave/components/brave_rewards/core/engine/database/database_recurring_tip.h"
#include "brave/components/brave_rewards/core/engine/database/database_server_publisher_info.h"
#include "brave/components/brave_rewards/core/engine/database/database_sku_order.h"
#include "brave/components/brave_rewards/core/engine/database/database_sku_transaction.h"
#include "brave/components/brave_rewards/core/engine/database/database_unblinded_token.h"
#include "brave/components/brave_rewards/core/engine/publisher/prefix_list_reader.h"
#include "brave/components/brave_rewards/core/engine/rewards_callbacks.h"

namespace brave_rewards::internal {
Expand Down Expand Up @@ -232,12 +230,6 @@ class Database {
/**
* SERVER PUBLISHER INFO
*/
void SearchPublisherPrefixList(const std::string& publisher_key,
SearchPublisherPrefixListCallback callback);

void ResetPublisherPrefixList(publisher::PrefixListReader reader,
ResultCallback callback);

void InsertServerPublisherInfo(const mojom::ServerPublisherInfo& server_info,
ResultCallback callback);

Expand Down Expand Up @@ -317,7 +309,6 @@ class Database {
DatabaseExternalTransactions external_transactions_;
DatabaseMediaPublisherInfo media_publisher_info_;
DatabasePublisherInfo publisher_info_;
DatabasePublisherPrefixList publisher_prefix_list_;
DatabaseRecurringTip recurring_tip_;
DatabaseServerPublisherInfo server_publisher_info_;
DatabaseSKUOrder sku_order_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v39.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v4.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v40.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v41.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v5.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v6.h"
#include "brave/components/brave_rewards/core/engine/database/migration/migration_v7.h"
Expand Down Expand Up @@ -140,7 +141,8 @@ void DatabaseMigration::Start(uint32_t table_version, ResultCallback callback) {
migration::v37,
migration::v38,
migration::v39,
migration::v40};
migration::v40,
migration::v41};

DCHECK_LE(target_version, mappings.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,11 @@ TEST_F(RewardsDatabaseMigrationTest, Migration_40) {
EXPECT_FALSE(GetDB()->DoesTableExist("processed_publisher"));
}

TEST_F(RewardsDatabaseMigrationTest, Migration_41) {
DatabaseMigration::SetTargetVersionForTesting(41);
InitializeDatabaseAtVersion(39);
InitializeEngine();
EXPECT_FALSE(GetDB()->DoesTableExist("publisher_prefix_list"));
}

} // namespace brave_rewards::internal
Loading

0 comments on commit f730894

Please sign in to comment.