From 80991a5089ff3bbfd2b9bd2f58a1ba942c0f23a2 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 7 Aug 2020 13:18:21 +0200 Subject: [PATCH] Adds event logging system Resolves https://github.com/brave/brave-browser/issues/10781 --- .../ui/webui/brave_rewards_internals_ui.cc | 41 ++++- browser/ui/webui/brave_webui_source.cc | 4 + .../browser/ads_service_impl_unittest.cc | 4 + components/brave_rewards/browser/BUILD.gn | 2 + components/brave_rewards/browser/event_log.cc | 16 ++ components/brave_rewards/browser/event_log.h | 26 +++ .../brave_rewards/browser/rewards_database.cc | 13 +- .../brave_rewards/browser/rewards_service.h | 6 + .../browser/rewards_service_impl.cc | 27 +++ .../browser/rewards_service_impl.h | 11 +- .../test/common/rewards_browsertest_util.cc | 11 ++ .../test/common/rewards_browsertest_util.h | 2 + .../test/rewards_database_browsertest.cc | 23 ++- .../actions/rewards_internals_actions.ts | 8 + .../internals/brave_rewards_internals.tsx | 7 +- .../resources/internals/components/app.tsx | 14 +- .../internals/components/event_logs.tsx | 41 +++++ .../constants/rewards_internals_types.ts | 4 +- .../reducers/rewards_internals_reducer.ts | 11 ++ .../resources/internals/storage.ts | 3 +- .../resources/internals/style.ts | 12 +- components/brave_rewards/test/BUILD.gn | 2 +- components/definitions/rewardsInternals.d.ts | 10 +- .../resources/brave_components_strings.grd | 4 + .../services/bat_ledger/bat_ledger_impl.cc | 23 +++ .../services/bat_ledger/bat_ledger_impl.h | 6 + .../public/interfaces/bat_ledger.mojom | 2 + .../publisher_info_schema_current.txt | 2 + vendor/bat-native-ledger/BUILD.gn | 2 + .../include/bat/ledger/ledger.h | 3 + .../include/bat/ledger/mojom_structs.h | 4 + .../bat/ledger/public/interfaces/ledger.mojom | 6 + .../public/interfaces/ledger_database.mojom | 3 +- .../internal/contribution/contribution_ac.cc | 5 + .../bat/ledger/internal/database/database.cc | 44 ++++- .../bat/ledger/internal/database/database.h | 16 ++ .../database_activity_info_unittest.cc | 12 ++ .../internal/database/database_event_log.cc | 162 ++++++++++++++++++ .../internal/database/database_event_log.h | 38 ++++ .../internal/database/database_migration.cc | 26 ++- .../ledger/internal/database/database_util.cc | 2 +- .../database/migration/migration_v29.h | 30 ++++ .../src/bat/ledger/internal/ledger_impl.cc | 16 +- .../src/bat/ledger/internal/ledger_impl.h | 4 + .../bat/ledger/internal/legacy/bat_helper.cc | 4 +- .../ledger/internal/logging/event_log_keys.h | 26 +++ .../ledger/internal/logging/logging_util.cc | 2 +- .../internal/promotion/promotion_transfer.cc | 15 +- .../internal/publisher/publisher_unittest.cc | 7 + .../internal/response/response_publisher.cc | 2 +- .../src/bat/ledger/internal/state/state.cc | 57 +++++- .../ledger/internal/state/state_migration.cc | 9 +- .../ledger/internal/state/state_migration.h | 2 + .../internal/state/state_migration_v5.cc | 73 ++++++++ .../internal/state/state_migration_v5.h | 30 ++++ .../src/bat/ledger/internal/uphold/uphold.cc | 8 + .../internal/uphold/uphold_authorization.cc | 8 + .../ledger/internal/uphold/uphold_wallet.cc | 4 + .../src/bat/ledger/internal/wallet/wallet.cc | 8 + 59 files changed, 933 insertions(+), 30 deletions(-) create mode 100644 components/brave_rewards/browser/event_log.cc create mode 100644 components/brave_rewards/browser/event_log.h create mode 100644 components/brave_rewards/resources/internals/components/event_logs.tsx create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.cc create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.h create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/database/migration/migration_v29.h create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/logging/event_log_keys.h create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.cc create mode 100644 vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.h diff --git a/browser/ui/webui/brave_rewards_internals_ui.cc b/browser/ui/webui/brave_rewards_internals_ui.cc index 8e31467c9a86..5136c713f5b8 100644 --- a/browser/ui/webui/brave_rewards_internals_ui.cc +++ b/browser/ui/webui/brave_rewards_internals_ui.cc @@ -67,6 +67,8 @@ class RewardsInternalsDOMHandler : public content::WebUIMessageHandler { void OnGetExternalWallet( int32_t result, std::unique_ptr wallet); + void GetEventLogs(const base::ListValue* args); + void OnGetEventLogs(const std::vector&); brave_rewards::RewardsService* rewards_service_; // NOT OWNED Profile* profile_; @@ -121,12 +123,16 @@ void RewardsInternalsDOMHandler::RegisterMessages() { base::BindRepeating( &RewardsInternalsDOMHandler::ClearLog, base::Unretained(this))); - web_ui()->RegisterMessageCallback( "brave_rewards_internals.getExternalWallet", base::BindRepeating( &RewardsInternalsDOMHandler::GetExternalWallet, base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_rewards_internals.getEventLogs", + base::BindRepeating( + &RewardsInternalsDOMHandler::GetEventLogs, + base::Unretained(this))); } void RewardsInternalsDOMHandler::Init() { @@ -410,6 +416,39 @@ void RewardsInternalsDOMHandler::OnGetExternalWallet( data); } +void RewardsInternalsDOMHandler::GetEventLogs(const base::ListValue* args) { + if (!rewards_service_) { + return; + } + + rewards_service_->GetEventLogs( + base::BindOnce( + &RewardsInternalsDOMHandler::OnGetEventLogs, + weak_ptr_factory_.GetWeakPtr())); +} + +void RewardsInternalsDOMHandler::OnGetEventLogs( + const std::vector& logs) { + if (!web_ui()->CanCallJavascript()) { + return; + } + + base::Value data(base::Value::Type::LIST); + + for (const auto& log : logs) { + base::Value item(base::Value::Type::DICTIONARY); + item.SetStringKey("id", log.event_log_id); + item.SetStringKey("key", log.key); + item.SetStringKey("value", log.value); + item.SetIntKey("createdAt", log.created_at); + data.Append(std::move(item)); + } + + web_ui()->CallJavascriptFunctionUnsafe( + "brave_rewards_internals.eventLogs", + std::move(data)); +} + } // namespace BraveRewardsInternalsUI::BraveRewardsInternalsUI(content::WebUI* web_ui, diff --git a/browser/ui/webui/brave_webui_source.cc b/browser/ui/webui/brave_webui_source.cc index e52086888ea7..f68a906af43b 100644 --- a/browser/ui/webui/brave_webui_source.cc +++ b/browser/ui/webui/brave_webui_source.cc @@ -852,6 +852,9 @@ void CustomizeWebUIHTMLSource(const std::string &name, { "contributionStepRewardsOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_REWARDS_OFF }, // NOLINT { "contributionStepAutoContributeOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_AUTO_CONTRIBUTE_OFF }, // NOLINT { "contributionStepRetryCount", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_RETRY_COUNT }, // NOLINT + { "eventLogKey", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_KEY }, + { "eventLogValue", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_VALUE }, + { "eventLogTime", IDS_BRAVE_REWARDS_INTERNALS_EVENT_LOG_TIME }, { "mainDisclaimer", IDS_BRAVE_REWARDS_INTERNALS_MAIN_DISCLAIMER }, { "rewardsNotEnabled", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_NOT_ENABLED }, // NOLINT { "rewardsTypeAuto", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_TYPE_AUTO }, // NOLINT @@ -893,6 +896,7 @@ void CustomizeWebUIHTMLSource(const std::string &name, { "tabLogs", IDS_BRAVE_REWARDS_INTERNALS_TAB_LOGS }, { "tabPromotions", IDS_BRAVE_REWARDS_INTERNALS_TAB_PROMOTIONS }, { "tabContributions", IDS_BRAVE_REWARDS_INTERNALS_TAB_CONTRIBUTIONS }, + { "tabEventLogs", IDS_BRAVE_REWARDS_INTERNALS_TAB_EVENT_LOGS }, { "totalAmount", IDS_BRAVE_REWARDS_INTERNALS_TOTAL_AMOUNT }, { "totalBalance", IDS_BRAVE_REWARDS_INTERNALS_TOTAL_BALANCE }, { "userId", IDS_BRAVE_REWARDS_INTERNALS_USER_ID }, diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index 5abbac390900..3a37d8c818aa 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -219,6 +219,10 @@ class MockRewardsService : public RewardsService { brave_rewards::ClearDiagnosticLogCallback callback)); MOCK_METHOD1(CompleteReset, void(brave_rewards::SuccessCallback callback)); + + MOCK_METHOD1( + GetEventLogs, + void(brave_rewards::GetEventLogsCallback callback)); }; class AdsServiceTest : public testing::Test { diff --git a/components/brave_rewards/browser/BUILD.gn b/components/brave_rewards/browser/BUILD.gn index 0100cd019d1f..45783484a211 100644 --- a/components/brave_rewards/browser/BUILD.gn +++ b/components/brave_rewards/browser/BUILD.gn @@ -61,6 +61,8 @@ source_set("browser") { "contribution_report_info.h", "rewards_database.h", "rewards_database.cc", + "event_log.h", + "event_log.cc", ] deps = [ diff --git a/components/brave_rewards/browser/event_log.cc b/components/brave_rewards/browser/event_log.cc new file mode 100644 index 000000000000..71fd756ec0a6 --- /dev/null +++ b/components/brave_rewards/browser/event_log.cc @@ -0,0 +1,16 @@ +/* Copyright (c) 2020 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/. */ + +#include "brave/components/brave_rewards/browser/event_log.h" + +namespace brave_rewards { + +EventLog::EventLog() = default; + +EventLog::~EventLog() = default; + +EventLog::EventLog(const EventLog &properties) = default; + +} // namespace brave_rewards diff --git a/components/brave_rewards/browser/event_log.h b/components/brave_rewards/browser/event_log.h new file mode 100644 index 000000000000..ca7754d9cc8b --- /dev/null +++ b/components/brave_rewards/browser/event_log.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2020 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_REWARDS_BROWSER_EVENT_LOG_H_ +#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EVENT_LOG_H_ + +#include + +namespace brave_rewards { + +struct EventLog { + EventLog(); + ~EventLog(); + EventLog(const EventLog& properties); + + std::string event_log_id; + std::string key; + std::string value; + uint64_t created_at; +}; + +} // namespace brave_rewards + +#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EVENT_LOG_H_ diff --git a/components/brave_rewards/browser/rewards_database.cc b/components/brave_rewards/browser/rewards_database.cc index df778ed8d143..86fba498f0e6 100644 --- a/components/brave_rewards/browser/rewards_database.cc +++ b/components/brave_rewards/browser/rewards_database.cc @@ -125,6 +125,15 @@ void RewardsDatabase::RunTransaction( return; } + // Close command must always be sent as single command in transaction + if (transaction->commands.size() == 1 && + transaction->commands[0]->type == ledger::DBCommand::Type::CLOSE) { + db_.Close(); + initialized_ = false; + command_response->status = ledger::DBCommandResponse::Status::RESPONSE_OK; + return; + } + sql::Transaction committer(&db_); if (!committer.Begin()) { command_response->status = @@ -170,8 +179,10 @@ void RewardsDatabase::RunTransaction( status = ledger::DBCommandResponse::Status::RESPONSE_OK; break; } - default: { + case ledger::DBCommand::Type::CLOSE: { NOTREACHED(); + status = ledger::DBCommandResponse::Status::COMMAND_ERROR; + break; } } diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index ca32d88a1ad5..233cf0b301b9 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -19,6 +19,7 @@ #include "brave/components/brave_rewards/browser/balance_report.h" #include "brave/components/brave_rewards/browser/content_site.h" #include "brave/components/brave_rewards/browser/contribution_info.h" +#include "brave/components/brave_rewards/browser/event_log.h" #include "brave/components/brave_rewards/browser/external_wallet.h" #include "brave/components/brave_rewards/browser/publisher_banner.h" #include "brave/components/brave_rewards/browser/pending_contribution.h" @@ -129,6 +130,9 @@ using ClearDiagnosticLogCallback = base::OnceCallback; using SuccessCallback = base::OnceCallback; +using GetEventLogsCallback = + base::OnceCallback&)>; + class RewardsService : public KeyedService { public: RewardsService(); @@ -333,6 +337,8 @@ class RewardsService : public KeyedService { virtual void CompleteReset(SuccessCallback callback) = 0; + virtual void GetEventLogs(GetEventLogsCallback callback) = 0; + protected: base::ObserverList observers_; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 15d7a729d656..020f2928e8b6 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -47,6 +47,7 @@ #include "brave/components/brave_rewards/browser/balance_report.h" #include "brave/components/brave_rewards/browser/content_site.h" #include "brave/components/brave_rewards/browser/contribution_info.h" +#include "brave/components/brave_rewards/browser/event_log.h" #include "brave/components/brave_rewards/browser/file_util.h" #include "brave/components/brave_rewards/browser/logging.h" #include "brave/components/brave_rewards/browser/logging_util.h" @@ -3807,4 +3808,30 @@ void RewardsServiceImpl::OnDeleteLog( callback(success ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR); } +void RewardsServiceImpl::GetEventLogs(GetEventLogsCallback callback) { + if (!Connected()) { + return; + } + + bat_ledger_->GetEventLogs( + base::BindOnce(&RewardsServiceImpl::OnGetEventLogs, + AsWeakPtr(), + std::move(callback))); +} + +void RewardsServiceImpl::OnGetEventLogs( + GetEventLogsCallback callback, + ledger::EventLogs logs) { + std::vector event_logs; + for (const auto& log : logs) { + brave_rewards::EventLog properties; + properties.event_log_id = log->event_log_id; + properties.key = log->key; + properties.value = log->value; + properties.created_at = log->created_at; + event_logs.push_back(properties); + } + std::move(callback).Run(std::move(event_logs)); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 35c28102f9bc..5ee9d2a1bc6e 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -318,6 +318,10 @@ class RewardsServiceImpl : public RewardsService, void GetAllPromotions(GetAllPromotionsCallback callback) override; + void GetEventLogs(GetEventLogsCallback callback) override; + + void StopLedger(StopLedgerCallback callback); + // Testing methods void SetLedgerEnvForTesting(); void PrepareLedgerEnvForTesting(); @@ -335,11 +339,10 @@ class RewardsServiceImpl : public RewardsService, void EnableGreaseLion(const bool enabled); - void StopLedger(StopLedgerCallback callback); - void OnStopLedger( StopLedgerCallback callback, const ledger::Result result); + void OnStopLedgerForCompleteReset( SuccessCallback callback, const ledger::Result result); @@ -708,6 +711,10 @@ class RewardsServiceImpl : public RewardsService, void OnDeleteLog(ledger::ResultCallback callback, const bool success); + void OnGetEventLogs( + GetEventLogsCallback callback, + ledger::EventLogs logs); + #if defined(OS_ANDROID) ledger::Environment GetServerEnvironmentForAndroid(); void CreateWalletAttestationResult( diff --git a/components/brave_rewards/browser/test/common/rewards_browsertest_util.cc b/components/brave_rewards/browser/test/common/rewards_browsertest_util.cc index 1a6a4a607518..353611659795 100644 --- a/components/brave_rewards/browser/test/common/rewards_browsertest_util.cc +++ b/components/brave_rewards/browser/test/common/rewards_browsertest_util.cc @@ -3,6 +3,8 @@ * 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/. */ +#include + #include "base/files/file_util.h" #include "base/path_service.h" #include "base/run_loop.h" @@ -111,4 +113,13 @@ void NavigateToPublisherPage( ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); } +void WaitForLedgerStop(brave_rewards::RewardsServiceImpl* rewards_service) { + base::RunLoop run_loop; + rewards_service->StopLedger( + base::BindLambdaForTesting([&](const ledger::Result) { + run_loop.Quit(); + })); + run_loop.Run(); +} + } // namespace rewards_browsertest_util diff --git a/components/brave_rewards/browser/test/common/rewards_browsertest_util.h b/components/brave_rewards/browser/test/common/rewards_browsertest_util.h index 442cbee5307b..848957a18514 100644 --- a/components/brave_rewards/browser/test/common/rewards_browsertest_util.h +++ b/components/brave_rewards/browser/test/common/rewards_browsertest_util.h @@ -50,6 +50,8 @@ void NavigateToPublisherPage( const std::string& publisher_key, const std::string& path = ""); +void WaitForLedgerStop(brave_rewards::RewardsServiceImpl* rewards_service); + } // namespace rewards_browsertest_util #endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_TEST_COMMON_REWARDS_BROWSERTEST_UTIL_H_ diff --git a/components/brave_rewards/browser/test/rewards_database_browsertest.cc b/components/brave_rewards/browser/test/rewards_database_browsertest.cc index 727ddbfcf70a..7b155ff7b239 100644 --- a/components/brave_rewards/browser/test/rewards_database_browsertest.cc +++ b/components/brave_rewards/browser/test/rewards_database_browsertest.cc @@ -6,6 +6,7 @@ #include "base/path_service.h" #include "base/strings/string_split.h" #include "base/strings/stringprintf.h" +#include "base/test/bind_test_util.h" #include "bat/ledger/internal/database/database_util.h" #include "bat/ledger/mojom_structs.h" #include "brave/browser/brave_rewards/rewards_service_factory.h" @@ -244,6 +245,7 @@ IN_PROC_BROWSER_TEST_F(RewardsDatabaseBrowserTest, SchemaCheck_2) { base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); const std::string schema = GetSchema(); EXPECT_EQ(schema, GetExpectedSchemaString()); ASSERT_EQ( @@ -257,9 +259,11 @@ IN_PROC_BROWSER_TEST_F(RewardsDatabaseBrowserTest, Migration_4_ActivityInfo) { base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); ledger::PublisherInfoList list; - const std::string query = "SELECT publisher_id, visits FROM activity_info"; + const std::string query = + "SELECT publisher_id, visits FROM activity_info"; sql::Statement info_sql(db_.GetUniqueStatement(query.c_str())); while (info_sql.Step()) { auto info = ledger::PublisherInfo::New(); @@ -281,6 +285,7 @@ IN_PROC_BROWSER_TEST_F(RewardsDatabaseBrowserTest, Migration_5_ActivityInfo) { base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); ledger::PublisherInfoList list; const std::string query = "SELECT publisher_id, visits FROM activity_info"; @@ -306,6 +311,7 @@ IN_PROC_BROWSER_TEST_F(RewardsDatabaseBrowserTest, Migration_6_ActivityInfo) { base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); ledger::PublisherInfoList list; const std::string query = @@ -359,6 +365,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); auto pending_contribution = ledger::PendingContribution::New(); pending_contribution->publisher_key = "reddit.com"; @@ -393,6 +400,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("contribution_info"), 5); EXPECT_EQ(CountTableRows("contribution_info_publishers"), 4); @@ -442,6 +450,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("pending_contribution"), 4); @@ -475,6 +484,7 @@ IN_PROC_BROWSER_TEST_F(RewardsDatabaseBrowserTest, Migration_13_Promotion) { base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("promotion"), 1); } @@ -487,6 +497,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("unblinded_tokens"), 5); @@ -524,6 +535,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("contribution_info"), 5); @@ -549,6 +561,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("promotion"), 2); @@ -580,6 +593,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("creds_batch"), 2); @@ -634,6 +648,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("unblinded_tokens"), 80); @@ -663,6 +678,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("contribution_info_publishers"), 4); @@ -709,6 +725,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("contribution_queue"), 1); @@ -760,6 +777,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("contribution_queue"), 1); @@ -794,6 +812,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("unblinded_tokens"), 1); @@ -831,6 +850,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("unblinded_tokens"), 10); @@ -892,6 +912,7 @@ IN_PROC_BROWSER_TEST_F( base::ScopedAllowBlockingForTesting allow_blocking; InitDB(); rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_); + rewards_browsertest_util::WaitForLedgerStop(rewards_service_); EXPECT_EQ(CountTableRows("server_publisher_info"), 1); EXPECT_EQ(CountTableRows("server_publisher_banner"), 1); EXPECT_EQ(CountTableRows("server_publisher_amounts"), 3); diff --git a/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts b/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts index 1848d2b1225f..286fae400ffa 100644 --- a/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts +++ b/components/brave_rewards/resources/internals/actions/rewards_internals_actions.ts @@ -68,3 +68,11 @@ export const onExternalWallet = (result: number, wallet: RewardsInternals.Extern result, wallet }) + +export const getEventLogs = (type: RewardsInternals.WalletType) => action(types.GET_EVENT_LOGS, { + type +}) + +export const onEventLogs = (logs: RewardsInternals.EventLog[]) => action(types.ON_EVENT_LOGS, { + logs +}) diff --git a/components/brave_rewards/resources/internals/brave_rewards_internals.tsx b/components/brave_rewards/resources/internals/brave_rewards_internals.tsx index 2b7d31085983..ced31103e0cb 100644 --- a/components/brave_rewards/resources/internals/brave_rewards_internals.tsx +++ b/components/brave_rewards/resources/internals/brave_rewards_internals.tsx @@ -63,6 +63,10 @@ window.cr.define('brave_rewards_internals', function () { getActions().onExternalWallet(properties.result, properties.wallet) } + function eventLogs (logs: RewardsInternals.EventLog[]) { + getActions().onEventLogs(logs) + } + function initialize () { window.i18nTemplate.process(window.document, window.loadTimeData) @@ -84,7 +88,8 @@ window.cr.define('brave_rewards_internals', function () { promotions, partialLog, fullLog, - externalWallet + externalWallet, + eventLogs } }) diff --git a/components/brave_rewards/resources/internals/components/app.tsx b/components/brave_rewards/resources/internals/components/app.tsx index 8107a72e6ab0..81a41bbc3296 100644 --- a/components/brave_rewards/resources/internals/components/app.tsx +++ b/components/brave_rewards/resources/internals/components/app.tsx @@ -10,6 +10,7 @@ import { connect } from 'react-redux' import { Contributions } from './contributions' import { Promotions } from './promotions' import { General } from './general' +import { EventLogs } from './event_logs' import { Log } from './log' import { Tabs } from 'brave-ui/components' import { Wrapper, MainTitle, DisabledContent, Disclaimer } from '../style' @@ -76,6 +77,10 @@ export class RewardsInternalsPage extends React.Component { this.getContributions() break } + case 'eventLogs': { + this.getEventLogs() + break + } } } @@ -103,8 +108,12 @@ export class RewardsInternalsPage extends React.Component { this.actions.getContributions() } + getEventLogs = () => { + this.actions.getEventLogs() + } + render () { - const { isRewardsEnabled, contributions, promotions, log, fullLog } = this.props.rewardsInternalsData + const { isRewardsEnabled, contributions, promotions, log, fullLog, eventLogs } = this.props.rewardsInternalsData if (!isRewardsEnabled) { return ( @@ -144,6 +153,9 @@ export class RewardsInternalsPage extends React.Component {
+
+ +
) } diff --git a/components/brave_rewards/resources/internals/components/event_logs.tsx b/components/brave_rewards/resources/internals/components/event_logs.tsx new file mode 100644 index 000000000000..49f08aac0a16 --- /dev/null +++ b/components/brave_rewards/resources/internals/components/event_logs.tsx @@ -0,0 +1,41 @@ +/* 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/. */ + +import * as React from 'react' + +// Components +import { EventTable, EventCell } from '../style' + +// Utils +import { getLocale } from '../../../../common/locale' +import { formatDate } from '../utils' + +interface Props { + items: RewardsInternals.EventLog[] +} + +export class EventLogs extends React.Component { + render () { + return ( + + + + {getLocale('eventLogTime')} + {getLocale('eventLogKey')} + {getLocale('eventLogValue')} + + + + {this.props.items.map((item) => + + {formatDate(item.createdAt * 1000)} + {item.key} + {item.value} + + )} + + + ) + } +} diff --git a/components/brave_rewards/resources/internals/constants/rewards_internals_types.ts b/components/brave_rewards/resources/internals/constants/rewards_internals_types.ts index d3fc7ab7b901..daf027fa63a4 100644 --- a/components/brave_rewards/resources/internals/constants/rewards_internals_types.ts +++ b/components/brave_rewards/resources/internals/constants/rewards_internals_types.ts @@ -20,5 +20,7 @@ export const enum types { CLEAR_LOG = '@@rewards_internals/CLEAR_LOG', DOWNLOAD_COMPLETED = '@@rewards_internals/DOWNLOAD_COMPLETED', GET_EXTERNAL_WALLET = '@@rewards_internals/GET_EXTERNAL_WALLET', - ON_EXTERNAL_WALLET = '@@rewards_internals/ON_EXTERNAL_WALLET' + ON_EXTERNAL_WALLET = '@@rewards_internals/ON_EXTERNAL_WALLET', + GET_EVENT_LOGS = '@@rewards_internals/GET_EVENT_LOGS', + ON_EVENT_LOGS = '@@rewards_internals/ON_EVENT_LOGS' } diff --git a/components/brave_rewards/resources/internals/reducers/rewards_internals_reducer.ts b/components/brave_rewards/resources/internals/reducers/rewards_internals_reducer.ts index 7f3241a11bb1..74848814880a 100644 --- a/components/brave_rewards/resources/internals/reducers/rewards_internals_reducer.ts +++ b/components/brave_rewards/resources/internals/reducers/rewards_internals_reducer.ts @@ -78,6 +78,17 @@ const rewardsInternalsReducer: Reducer = (st state = { ...state } state.externalWallet = action.payload.wallet break + case types.GET_EVENT_LOGS: + chrome.send('brave_rewards_internals.getEventLogs') + break + case types.ON_EVENT_LOGS: + state = { ...state } + if (!action.payload.logs || !Array.isArray(action.payload.logs)) { + break + } + state.eventLogs = action.payload.logs + .sort((a: RewardsInternals.EventLog, b: RewardsInternals.EventLog) => b.createdAt - a.createdAt) + break default: break } diff --git a/components/brave_rewards/resources/internals/storage.ts b/components/brave_rewards/resources/internals/storage.ts index 7992edd50ea5..fea7256882a7 100644 --- a/components/brave_rewards/resources/internals/storage.ts +++ b/components/brave_rewards/resources/internals/storage.ts @@ -20,7 +20,8 @@ const defaultState: RewardsInternals.State = { externalWallet: { address: '', status: 0 - } + }, + eventLogs: [] } export const load = (): RewardsInternals.State => { diff --git a/components/brave_rewards/resources/internals/style.ts b/components/brave_rewards/resources/internals/style.ts index f6d4399693ce..629134b8605f 100644 --- a/components/brave_rewards/resources/internals/style.ts +++ b/components/brave_rewards/resources/internals/style.ts @@ -8,7 +8,7 @@ import { ComponentType } from 'react' export const Wrapper = styled<{}, 'div'>('div')` width: 100%; - max-width: 1200px; + max-width: 1350px; margin: 15px auto 0; font-family: ${p => p.theme.fontFamily.body}; font-size: 14px; @@ -75,3 +75,13 @@ export const Disclaimer = styled<{}, 'p'>('p')` padding: 0px; text-align: center; ` + +export const EventTable = styled<{}, 'table'>('table')` + border-spacing: 0; + margin: 0 auto; +` + +export const EventCell = styled<{}, 'td'>('td')` + border-bottom: 1px solid ${p => p.theme.palette.blackFade85}; + padding: 10px; +` diff --git a/components/brave_rewards/test/BUILD.gn b/components/brave_rewards/test/BUILD.gn index ab64a6fd89af..38a5e2058b7c 100644 --- a/components/brave_rewards/test/BUILD.gn +++ b/components/brave_rewards/test/BUILD.gn @@ -36,7 +36,7 @@ source_set("brave_rewards_unit_tests") { "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/publisher_settings_state_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/report_balance_state_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/wallet_info_state_unittest.cc", - "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/logging_util_unittest.cc", + "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/logging/logging_util_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/github_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper_unittest.cc", "//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/reddit_unittest.cc", diff --git a/components/definitions/rewardsInternals.d.ts b/components/definitions/rewardsInternals.d.ts index 0b69221206c7..4b89dea48c06 100644 --- a/components/definitions/rewardsInternals.d.ts +++ b/components/definitions/rewardsInternals.d.ts @@ -15,7 +15,8 @@ declare namespace RewardsInternals { promotions: Promotion[] log: string fullLog: string - externalWallet: ExternalWallet + externalWallet: ExternalWallet, + eventLogs: EventLog[] } export interface ContributionInfo { @@ -68,4 +69,11 @@ declare namespace RewardsInternals { address: string status: WalletStatus } + + export interface EventLog { + id: string + key: string + value: string + createdAt: number + } } diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index ec142b9b010e..8b1027e49d14 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -405,6 +405,9 @@ Rewards was turned off Auto-contribute was turned off Stopped retrying + Key + Value + Logged at WARNING: Data on this page may be sensitive. Treat them as you would your wallet private keys. Be careful who you share them with. Type: Contributions @@ -440,6 +443,7 @@ One-time tip Recurring tip Contributions + Event logs General info Logs Promotions diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index 939add2c37e3..44e68deb13e1 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -1046,4 +1046,27 @@ void BatLedgerImpl::Shutdown(ShutdownCallback callback) { _1)); } + +// static +void BatLedgerImpl::OnGetEventLogs( + CallbackHolder* holder, + ledger::EventLogs logs) { + DCHECK(holder); + if (holder->is_valid()) { + std::move(holder->get()).Run(std::move(logs)); + } + + delete holder; +} + +void BatLedgerImpl::GetEventLogs(GetEventLogsCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger_->GetEventLogs( + std::bind(BatLedgerImpl::OnGetEventLogs, + holder, + _1)); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index 4747e9455adc..4ab2489f2129 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -224,6 +224,8 @@ class BatLedgerImpl : void Shutdown(ShutdownCallback callback) override; + void GetEventLogs(GetEventLogsCallback callback) override; + private: // workaround to pass base::OnceCallback into std::bind template @@ -406,6 +408,10 @@ class BatLedgerImpl : CallbackHolder* holder, const ledger::Result result); + static void OnGetEventLogs( + CallbackHolder* holder, + ledger::EventLogs logs); + std::unique_ptr bat_ledger_client_mojo_bridge_; std::unique_ptr ledger_; }; diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index a7111fbe4485..55caa622e34f 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -150,6 +150,8 @@ interface BatLedger { GetAllPromotions() => (map items); Shutdown() => (ledger.mojom.Result result); + + GetEventLogs() => (array logs); }; interface BatLedgerClient { diff --git a/test/data/rewards-data/migration/publisher_info_schema_current.txt b/test/data/rewards-data/migration/publisher_info_schema_current.txt index d2e5c9539ebd..38c333f35d09 100644 --- a/test/data/rewards-data/migration/publisher_info_schema_current.txt +++ b/test/data/rewards-data/migration/publisher_info_schema_current.txt @@ -24,6 +24,7 @@ index|sqlite_autoindex_contribution_info_publishers_1|contribution_info_publishe index|sqlite_autoindex_contribution_queue_1|contribution_queue| index|sqlite_autoindex_creds_batch_1|creds_batch| index|sqlite_autoindex_creds_batch_2|creds_batch| +index|sqlite_autoindex_event_log_1|event_log| index|sqlite_autoindex_media_publisher_info_1|media_publisher_info| index|sqlite_autoindex_meta_1|meta| index|sqlite_autoindex_processed_publisher_1|processed_publisher| @@ -48,6 +49,7 @@ table|contribution_info_publishers|contribution_info_publishers|CREATE TABLE con table|contribution_queue|contribution_queue|CREATE TABLE contribution_queue ( contribution_queue_id TEXT PRIMARY KEY NOT NULL, type INTEGER NOT NULL, amount DOUBLE NOT NULL, partial INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL , completed_at TIMESTAMP NOT NULL DEFAULT 0) table|contribution_queue_publishers|contribution_queue_publishers|CREATE TABLE contribution_queue_publishers ( contribution_queue_id TEXT NOT NULL, publisher_key TEXT NOT NULL, amount_percent DOUBLE NOT NULL ) table|creds_batch|creds_batch|CREATE TABLE creds_batch (creds_id TEXT PRIMARY KEY NOT NULL, trigger_id TEXT NOT NULL, trigger_type INT NOT NULL, creds TEXT NOT NULL, blinded_creds TEXT NOT NULL, signed_creds TEXT, public_key TEXT, batch_proof TEXT, status INT NOT NULL DEFAULT 0, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT creds_batch_unique UNIQUE (trigger_id, trigger_type) ) +table|event_log|event_log|CREATE TABLE event_log ( event_log_id LONGVARCHAR PRIMARY KEY NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, created_at TIMESTAMP NOT NULL ) table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info ( media_key TEXT NOT NULL PRIMARY KEY UNIQUE, publisher_id LONGVARCHAR NOT NULL ) table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR) table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution ( pending_contribution_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, publisher_id LONGVARCHAR NOT NULL, amount DOUBLE DEFAULT 0 NOT NULL, added_date INTEGER DEFAULT 0 NOT NULL, viewing_id LONGVARCHAR NOT NULL, type INTEGER NOT NULL ) diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 079d1a6e7234..ef7fadb4af1f 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -392,6 +392,8 @@ source_set("ledger") { "src/bat/ledger/internal/state/state_migration_v3.h", "src/bat/ledger/internal/state/state_migration_v4.cc", "src/bat/ledger/internal/state/state_migration_v4.h", + "src/bat/ledger/internal/state/state_migration_v5.cc", + "src/bat/ledger/internal/state/state_migration_v5.h", "src/bat/ledger/internal/legacy/client_state.cc", "src/bat/ledger/internal/legacy/client_state.h", "src/bat/ledger/internal/legacy/publisher_settings_state.cc", diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 3816913a00e6..34f9dd63d576 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -50,6 +50,7 @@ using GetMonthlyReportCallback = std::function; using GetAllMonthlyReportIdsCallback = std::function&)>; +using GetEventLogsCallback = std::function; class LEDGER_EXPORT Ledger { public: @@ -322,6 +323,8 @@ class LEDGER_EXPORT Ledger { SKUOrderCallback callback) = 0; virtual void Shutdown(ResultCallback callback) = 0; + + virtual void GetEventLogs(GetEventLogsCallback callback) = 0; }; } // namespace ledger diff --git a/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h b/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h index 5f50d38faf07..5693ffa8517d 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h +++ b/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h @@ -76,6 +76,10 @@ using CredsBatchType = mojom::CredsBatchType; using CredsBatchStatus = mojom::CredsBatchStatus; +using EventLog = ledger::mojom::EventLog; +using EventLogPtr = ledger::mojom::EventLogPtr; +using EventLogs = std::vector; + using Environment = ledger::mojom::Environment; using ExcludeFilter = mojom::ExcludeFilter; diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom index 1ac24342dab0..784c9bac5805 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -534,3 +534,9 @@ enum InlineTipsPlatforms { GITHUB = 3 }; +struct EventLog { + string event_log_id; + string key; + string value; + uint64 created_at; +}; diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger_database.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger_database.mojom index 1dd79c71f9dd..19e9a6084537 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger_database.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger_database.mojom @@ -25,7 +25,8 @@ struct DBCommand { RUN, EXECUTE, MIGRATE, - VACUUM + VACUUM, + CLOSE }; enum RecordBindingType { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc index 56d8776800ee..09ce95f737a9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc @@ -7,6 +7,7 @@ #include "base/guid.h" #include "bat/ledger/internal/contribution/contribution_ac.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/ledger_impl.h" using std::placeholders::_1; @@ -84,6 +85,10 @@ void ContributionAC::PreparePublisherList(ledger::PublisherInfoList list) { queue->partial = true; queue->publishers = std::move(queue_list); + ledger_->database()->SaveEventLog( + ledger::log::kACAddedToQueue, + std::to_string(queue->amount)); + auto save_callback = std::bind(&ContributionAC::QueueSaved, this, _1); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc index dd630ddd4cf4..26638f50dc72 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc @@ -8,9 +8,10 @@ #include "bat/ledger/internal/database/database.h" #include "bat/ledger/internal/database/database_activity_info.h" #include "bat/ledger/internal/database/database_balance_report.h" -#include "bat/ledger/internal/database/database_creds_batch.h" #include "bat/ledger/internal/database/database_contribution_info.h" #include "bat/ledger/internal/database/database_contribution_queue.h" +#include "bat/ledger/internal/database/database_creds_batch.h" +#include "bat/ledger/internal/database/database_event_log.h" #include "bat/ledger/internal/database/database_initialize.h" #include "bat/ledger/internal/database/database_media_publisher_info.h" #include "bat/ledger/internal/database/database_multi_tables.h" @@ -24,9 +25,13 @@ #include "bat/ledger/internal/database/database_sku_order.h" #include "bat/ledger/internal/database/database_sku_transaction.h" #include "bat/ledger/internal/database/database_unblinded_token.h" +#include "bat/ledger/internal/database/database_util.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/publisher/prefix_list_reader.h" +using std::placeholders::_1; + namespace braveledger_database { Database::Database(bat_ledger::LedgerImpl* ledger) : @@ -39,6 +44,7 @@ Database::Database(bat_ledger::LedgerImpl* ledger) : contribution_queue_ = std::make_unique(ledger_); contribution_info_ = std::make_unique(ledger_); creds_batch_ = std::make_unique(ledger_); + event_log_ = std::make_unique(ledger_); media_publisher_info_ = std::make_unique(ledger_); multi_tables_ = std::make_unique(ledger_); @@ -66,6 +72,21 @@ void Database::Initialize( initialize_->Start(execute_create_script, callback); } +void Database::Close(ledger::ResultCallback callback) { + auto transaction = ledger::DBTransaction::New(); + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::CLOSE; + + transaction->commands.push_back(std::move(command)); + auto transaction_callback = std::bind(&OnResultCallback, + _1, + callback); + + ledger_->ledger_client()->RunDBTransaction( + std::move(transaction), + transaction_callback); +} + /** * ACTIVITY INFO */ @@ -284,6 +305,23 @@ void Database::GetCredsBatchesByTriggers( creds_batch_->GetRecordsByTriggers(trigger_ids, callback); } +/** + * EVENT LOG + */ +void Database::SaveEventLog(const std::string& key, const std::string& value) { + event_log_->Insert(key, value); +} + +void Database::SaveEventLogs( + const std::map& records, + ledger::ResultCallback callback) { + event_log_->InsertRecords(records, callback); +} + +void Database::GetLastEventLogs(ledger::GetEventLogsCallback callback) { + event_log_->GetLastRecords(callback); +} + /** * MEDIA PUBLISHER INFO */ @@ -454,6 +492,9 @@ void Database::GetExcludedList(ledger::PublisherInfoListCallback callback) { void Database::SaveRecurringTip( ledger::RecurringTipPtr info, ledger::ResultCallback callback) { + if (info) { + SaveEventLog(ledger::log::kRecurringTipAdded, info->publisher_key); + } recurring_tip_->InsertOrUpdate(std::move(info), callback); } @@ -464,6 +505,7 @@ void Database::GetRecurringTips(ledger::PublisherInfoListCallback callback) { void Database::RemoveRecurringTip( const std::string& publisher_key, ledger::ResultCallback callback) { + SaveEventLog(ledger::log::kRecurringTipRemoved, publisher_key); recurring_tip_->DeleteRecord(publisher_key, callback); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h index 5812fc5f8cc9..c42afad598ce 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -28,6 +29,7 @@ class DatabaseInitialize; class DatabaseActivityInfo; class DatabaseBalanceReport; class DatabaseCredsBatch; +class DatabaseEventLog; class DatabaseContributionInfo; class DatabaseContributionQueue; class DatabaseMediaPublisherInfo; @@ -52,6 +54,8 @@ class Database { const bool execute_create_script, ledger::ResultCallback callback); + void Close(ledger::ResultCallback callback); + /** * ACTIVITY INFO */ @@ -192,6 +196,17 @@ class Database { const std::vector& trigger_ids, ledger::GetCredsBatchListCallback callback); + /** + * EVENT LOG + */ + void SaveEventLog(const std::string& key, const std::string& value); + + void SaveEventLogs( + const std::map& records, + ledger::ResultCallback callback); + + void GetLastEventLogs(ledger::GetEventLogsCallback callback); + /** * MEDIA PUBLISHER INFO */ @@ -422,6 +437,7 @@ class Database { std::unique_ptr contribution_info_; std::unique_ptr contribution_queue_; std::unique_ptr creds_batch_; + std::unique_ptr event_log_; std::unique_ptr pending_contribution_; std::unique_ptr processed_publisher_; std::unique_ptr promotion_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc index 8d9037729164..89db7248fbb3 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc @@ -9,6 +9,7 @@ #include "base/test/task_environment.h" #include "bat/ledger/internal/database/database_activity_info.h" +#include "bat/ledger/internal/database/database_mock.h" #include "bat/ledger/internal/database/database_util.h" #include "bat/ledger/internal/ledger_client_mock.h" #include "bat/ledger/internal/ledger_impl_mock.h" @@ -29,15 +30,23 @@ class DatabaseActivityInfoTest : public ::testing::Test { std::unique_ptr mock_ledger_impl_; std::string execute_script_; std::unique_ptr activity_; + std::unique_ptr mock_database_; DatabaseActivityInfoTest() { mock_ledger_client_ = std::make_unique(); mock_ledger_impl_ = std::make_unique(mock_ledger_client_.get()); activity_ = std::make_unique(mock_ledger_impl_.get()); + mock_database_ = std::make_unique( + mock_ledger_impl_.get()); } ~DatabaseActivityInfoTest() override {} + + void SetUp() override { + ON_CALL(*mock_ledger_impl_, database()) + .WillByDefault(testing::Return(mock_database_.get())); + } }; TEST_F(DatabaseActivityInfoTest, InsertOrUpdateNull) { @@ -180,6 +189,9 @@ TEST_F(DatabaseActivityInfoTest, DeleteRecordEmpty) { TEST_F(DatabaseActivityInfoTest, DeleteRecordOk) { EXPECT_CALL(*mock_ledger_client_, RunDBTransaction(_, _)).Times(1); + ON_CALL(*mock_ledger_client_, GetUint64State(_)) + .WillByDefault(testing::Return(1597744617)); + const std::string query = "DELETE FROM activity_info " "WHERE publisher_id = ? AND reconcile_stamp = ?"; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.cc new file mode 100644 index 000000000000..a99992c0e93c --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.cc @@ -0,0 +1,162 @@ +/* Copyright (c) 2020 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/. */ + +#include + +#include "base/guid.h" +#include "base/strings/stringprintf.h" +#include "bat/ledger/internal/common/time_util.h" +#include "bat/ledger/internal/database/database_event_log.h" +#include "bat/ledger/internal/database/database_util.h" +#include "bat/ledger/internal/ledger_impl.h" + +using std::placeholders::_1; + +namespace braveledger_database { + +namespace { + +const char kTableName[] = "event_log"; + +} // namespace + +DatabaseEventLog::DatabaseEventLog( + bat_ledger::LedgerImpl* ledger) : + DatabaseTable(ledger) { +} + +DatabaseEventLog::~DatabaseEventLog() = default; + +void DatabaseEventLog::Insert( + const std::string& key, + const std::string& value) { + if (key.empty() || value.empty()) { + BLOG_IF(1, key.empty(), "Key is empty"); + BLOG_IF(1, value.empty(), "Value is empty"); + return; + } + + auto transaction = ledger::DBTransaction::New(); + + const std::string query = base::StringPrintf( + "INSERT INTO %s (event_log_id, key, value, created_at) " + "VALUES (?, ?, ?, ?)", + kTableName); + + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::RUN; + command->command = query; + + BindString(command.get(), 0, base::GenerateGUID()); + BindString(command.get(), 1, key); + BindString(command.get(), 2, value); + BindInt64(command.get(), 3, braveledger_time_util::GetCurrentTimeStamp()); + + transaction->commands.push_back(std::move(command)); + + ledger_->ledger_client()->RunDBTransaction( + std::move(transaction), + [](ledger::DBCommandResponsePtr response){}); +} + +void DatabaseEventLog::InsertRecords( + const std::map& records, + ledger::ResultCallback callback) { + if (records.empty()) { + BLOG(0, "No records"); + callback(ledger::Result::NOT_FOUND); + return; + } + + auto transaction = ledger::DBTransaction::New(); + auto time = braveledger_time_util::GetCurrentTimeStamp(); + const std::string base_query = base::StringPrintf( + "INSERT INTO %s (event_log_id, key, value, created_at) VALUES ", + kTableName); + + std::string query = base_query; + for (const auto& record : records) { + query += base::StringPrintf( + R"(("%s","%s","%s",%u),)", + base::GenerateGUID().c_str(), + record.first.c_str(), + record.second.c_str(), + static_cast(time)); + } + + query.pop_back(); + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::EXECUTE; + command->command = query; + + transaction->commands.push_back(std::move(command)); + + auto transaction_callback = std::bind(&OnResultCallback, + _1, + callback); + + ledger_->ledger_client()->RunDBTransaction( + std::move(transaction), + transaction_callback); +} + +void DatabaseEventLog::GetLastRecords(ledger::GetEventLogsCallback callback) { + auto transaction = ledger::DBTransaction::New(); + + const std::string query = base::StringPrintf( + "SELECT event_log_id, key, value, created_at " + "FROM %s ORDER BY created_at DESC LIMIT 2000", + kTableName); + + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::READ; + command->command = query; + + command->record_bindings = { + ledger::DBCommand::RecordBindingType::STRING_TYPE, + ledger::DBCommand::RecordBindingType::STRING_TYPE, + ledger::DBCommand::RecordBindingType::STRING_TYPE, + ledger::DBCommand::RecordBindingType::INT64_TYPE, + }; + + transaction->commands.push_back(std::move(command)); + + auto transaction_callback = std::bind(&DatabaseEventLog::OnGetAllRecords, + this, + _1, + callback); + + ledger_->ledger_client()->RunDBTransaction( + std::move(transaction), + transaction_callback); +} + +void DatabaseEventLog::OnGetAllRecords( + ledger::DBCommandResponsePtr response, + ledger::GetEventLogsCallback callback) { + if (!response || + response->status != ledger::DBCommandResponse::Status::RESPONSE_OK) { + BLOG(0, "Response is wrong"); + callback({}); + return; + } + + ledger::EventLogs list; + for (auto const& record : response->result->get_records()) { + auto info = ledger::EventLog::New(); + auto* record_pointer = record.get(); + + info->event_log_id = GetStringColumn(record_pointer, 0); + info->key = GetStringColumn(record_pointer, 1); + info->value = GetStringColumn(record_pointer, 2); + info->created_at = GetInt64Column(record_pointer, 3); + + list.push_back(std::move(info)); + } + + callback(std::move(list)); +} + +} // namespace braveledger_database diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.h new file mode 100644 index 000000000000..1b29b6ffb01a --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_event_log.h @@ -0,0 +1,38 @@ +/* Copyright (c) 2020 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 BRAVELEDGER_DATABASE_DATABASE_EVENT_LOG_H_ +#define BRAVELEDGER_DATABASE_DATABASE_EVENT_LOG_H_ + +#include +#include + +#include "bat/ledger/internal/database/database_table.h" + +namespace braveledger_database { + +class DatabaseEventLog: public DatabaseTable { + public: + explicit DatabaseEventLog(bat_ledger::LedgerImpl* ledger); + ~DatabaseEventLog() override; + + void Insert(const std::string& key, const std::string& value); + + void InsertRecords( + const std::map& records, + ledger::ResultCallback callback); + + // returns last 2000 records + void GetLastRecords(ledger::GetEventLogsCallback callback); + + private: + void OnGetAllRecords( + ledger::DBCommandResponsePtr response, + ledger::GetEventLogsCallback callback); +}; + +} // namespace braveledger_database + +#endif // BRAVELEDGER_DATABASE_DATABASE_EVENT_LOG_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_migration.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_migration.cc index 9aae057d9afa..aa10831faced 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_migration.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_migration.cc @@ -3,10 +3,10 @@ * 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/. */ -#include #include #include +#include "base/strings/stringprintf.h" #include "base/strings/string_util.h" #include "bat/ledger/internal/database/database_migration.h" #include "bat/ledger/internal/database/database_util.h" @@ -38,7 +38,9 @@ #include "bat/ledger/internal/database/migration/migration_v26.h" #include "bat/ledger/internal/database/migration/migration_v27.h" #include "bat/ledger/internal/database/migration/migration_v28.h" +#include "bat/ledger/internal/database/migration/migration_v29.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "third_party/re2/src/re2/re2.h" // NOTICE!! @@ -104,6 +106,7 @@ void DatabaseMigration::Start( migration::v26, migration::v27, migration::v28, + migration::v29, }; DCHECK_LE(target_version, mappings.size()); @@ -126,13 +129,26 @@ void DatabaseMigration::Start( command->type = ledger::DBCommand::Type::VACUUM; transaction->commands.push_back(std::move(command)); - auto transaction_callback = std::bind(&braveledger_database::OnResultCallback, - _1, - callback); + const std::string message = base::StringPrintf( + "%d->%d", + start_version, + migrated_version); ledger_->ledger_client()->RunDBTransaction( std::move(transaction), - transaction_callback); + [this, callback, message](ledger::DBCommandResponsePtr response) { + if (response && + response->status == + ledger::DBCommandResponse::Status::RESPONSE_OK) { + ledger_->database()->SaveEventLog( + ledger::log::kDatabaseMigrated, + message); + callback(ledger::Result::LEDGER_OK); + return; + } + + callback(ledger::Result::LEDGER_ERROR); + }); } void DatabaseMigration::GenerateCommand( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_util.cc index c593e6c11287..aa139afbdd1a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_util.cc @@ -11,7 +11,7 @@ namespace { -const int kCurrentVersionNumber = 28; +const int kCurrentVersionNumber = 29; const int kCompatibleVersionNumber = 1; } // namespace diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/migration/migration_v29.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/migration/migration_v29.h new file mode 100644 index 000000000000..06684e8b89d9 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/migration/migration_v29.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2020 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 BRAVELEDGER_DATABASE_MIGRATION_MIGRATION_V29_H_ +#define BRAVELEDGER_DATABASE_MIGRATION_MIGRATION_V29_H_ + +namespace ledger { +namespace database { +namespace migration { + +const char v29[] = R"( + PRAGMA foreign_keys = off; + DROP TABLE IF EXISTS event_log; + PRAGMA foreign_keys = on; + + CREATE TABLE event_log ( + event_log_id LONGVARCHAR PRIMARY KEY NOT NULL, + key TEXT NOT NULL, + value TEXT NOT NULL, + created_at TIMESTAMP NOT NULL + ); +)"; + +} // namespace migration +} // namespace database +} // namespace ledger + +#endif // BRAVELEDGER_DATABASE_MIGRATION_MIGRATION_V29_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 1a8fa46329e8..809dd38da665 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -765,8 +765,22 @@ void LedgerImpl::Shutdown(ledger::ResultCallback callback) { 1, result != ledger::Result::LEDGER_OK, "Not all wallets were disconnected"); - database()->FinishAllInProgressContributions(callback); + auto finish_callback = std::bind(&LedgerImpl::OnAllDone, + this, + _1, + callback); + database()->FinishAllInProgressContributions(finish_callback); }); } +void LedgerImpl::OnAllDone( + const ledger::Result result, + ledger::ResultCallback callback) { + database()->Close(callback); +} + +void LedgerImpl::GetEventLogs(ledger::GetEventLogsCallback callback) { + database()->GetLastEventLogs(callback); +} + } // namespace bat_ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 205eaa70c04e..19a6190a5266 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -334,8 +334,12 @@ class LedgerImpl : public ledger::Ledger { void Shutdown(ledger::ResultCallback callback) override; + void GetEventLogs(ledger::GetEventLogsCallback callback) override; + // end ledger.h + void OnAllDone(const ledger::Result result, ledger::ResultCallback callback); + ledger::LedgerClient* ledger_client_; std::unique_ptr promotion_; std::unique_ptr publisher_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc index b2c438c6ddbc..5f6a373a788b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_helper.cc @@ -13,8 +13,8 @@ #include #include "base/strings/string_split.h" -#include "bat/ledger/internal/bat_helper.h" -#include "bat/ledger/internal/legacy/logging/logging.h" +#include "bat/ledger/internal/legacy/bat_helper.h" +#include "bat/ledger/internal/logging/logging.h" #include "bat/ledger/internal/static_values.h" #include "bat/ledger/ledger.h" #include "rapidjson/document.h" diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/logging/event_log_keys.h b/vendor/bat-native-ledger/src/bat/ledger/internal/logging/event_log_keys.h new file mode 100644 index 000000000000..65c9dfea2289 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/logging/event_log_keys.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2020 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 BRAVELEDGER_LOGGING_EVENT_LOG_KEYS_H_ +#define BRAVELEDGER_LOGGING_EVENT_LOG_KEYS_H_ + +#include + +namespace ledger { +namespace log { + +const char kACAddedToQueue[] = "ac_added_to_queue"; +const char kWalletConnected[] = "wallet_connected"; +const char kWalletVerified[] = "wallet_verified"; +const char kWalletDisconnected[] = "wallet_disconnected"; +const char kRecurringTipAdded[] = "recurring_tip_added"; +const char kRecurringTipRemoved[] = "recurring_tip_removed"; +const char kDatabaseMigrated[] = "database_migrated"; +const char kPromotionsClaimed[] = "promotion_claimed"; + +} // namespace log +} // namespace ledger + +#endif // BRAVELEDGER_LOGGING_EVENT_LOG_KEYS_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/logging/logging_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/logging/logging_util.cc index 12da08da2818..a6dd05354000 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/logging/logging_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/logging/logging_util.cc @@ -9,7 +9,7 @@ #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" -#include "bat/ledger/internal/logging.h" +#include "bat/ledger/internal/logging/logging.h" namespace ledger { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_transfer.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_transfer.cc index d39485f08ab9..7f30f24533e3 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_transfer.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_transfer.cc @@ -6,8 +6,10 @@ #include #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/promotion/promotion_transfer.h" #include "bat/ledger/internal/promotion/promotion_util.h" +#include "bat/ledger/internal/static_values.h" using std::placeholders::_1; using std::placeholders::_2; @@ -78,7 +80,18 @@ void PromotionTransfer::OnGetEligibleTokens( redeem.processor = ledger::ContributionProcessor::BRAVE_TOKENS; redeem.token_list = token_list; - credentials_->RedeemTokens(redeem, callback); + const double transfer_amount = + token_list.size() * braveledger_ledger::_vote_price; + credentials_->RedeemTokens( + redeem, + [this, transfer_amount, callback](const ledger::Result result) { + if (result == ledger::Result::LEDGER_OK) { + ledger_->database()->SaveEventLog( + ledger::log::kPromotionsClaimed, + std::to_string(transfer_amount)); + } + callback(result); + }); } } // namespace braveledger_promotion diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher_unittest.cc index c058647790b6..d666699ebb09 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher_unittest.cc @@ -7,6 +7,7 @@ #include #include "base/test/task_environment.h" +#include "bat/ledger/internal/database/database_mock.h" #include "bat/ledger/internal/ledger_client_mock.h" #include "bat/ledger/internal/ledger_impl_mock.h" #include "bat/ledger/internal/publisher/publisher.h" @@ -47,15 +48,21 @@ class PublisherTest : public testing::Test { std::unique_ptr mock_ledger_client_; std::unique_ptr mock_ledger_impl_; std::unique_ptr publisher_; + std::unique_ptr mock_database_; PublisherTest() { mock_ledger_client_ = std::make_unique(); mock_ledger_impl_ = std::make_unique(mock_ledger_client_.get()); publisher_ = std::make_unique(mock_ledger_impl_.get()); + mock_database_ = std::make_unique( + mock_ledger_impl_.get()); } void SetUp() override { + ON_CALL(*mock_ledger_impl_, database()) + .WillByDefault(testing::Return(mock_database_.get())); + ON_CALL(*mock_ledger_client_, GetDoubleState(ledger::kStateScoreA)) .WillByDefault( Invoke([this](const std::string& key) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_publisher.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_publisher.cc index a7cf49df6dab..d08472b2ee3c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_publisher.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/response/response_publisher.cc @@ -10,7 +10,7 @@ #include "base/json/json_reader.h" #include "bat/ledger/internal/common/brotli_helpers.h" #include "bat/ledger/internal/common/time_util.h" -#include "bat/ledger/internal/logging.h" +#include "bat/ledger/internal/logging/logging.h" #include "bat/ledger/internal/publisher/protos/channel_response.pb.h" #include "brave/components/brave_private_cdn/private_cdn_helper.h" #include "net/http/http_status_code.h" diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc index 83a59b2163d3..aba58058ac27 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc @@ -88,6 +88,9 @@ void State::Initialize(ledger::ResultCallback callback) { } void State::SetVersion(const int version) { + ledger_->database()->SaveEventLog( + ledger::kStateVersion, + std::to_string(version)); ledger_->ledger_client()->SetIntegerState(ledger::kStateVersion, version); } @@ -96,6 +99,9 @@ int State::GetVersion() { } void State::SetPublisherMinVisitTime(const int duration) { + ledger_->database()->SaveEventLog( + ledger::kStateMinVisitTime, + std::to_string(duration)); ledger_->ledger_client()->SetIntegerState( ledger::kStateMinVisitTime, duration); @@ -108,6 +114,9 @@ int State::GetPublisherMinVisitTime() { } void State::SetPublisherMinVisits(const int visits) { + ledger_->database()->SaveEventLog( + ledger::kStateMinVisits, + std::to_string(visits)); ledger_->ledger_client()->SetIntegerState(ledger::kStateMinVisits, visits); ledger_->publisher()->SynopsisNormalizer(); } @@ -117,6 +126,9 @@ int State::GetPublisherMinVisits() { } void State::SetPublisherAllowNonVerified(const bool allow) { + ledger_->database()->SaveEventLog( + ledger::kStateAllowNonVerified, + std::to_string(allow)); ledger_->ledger_client()->SetBooleanState( ledger::kStateAllowNonVerified, allow); @@ -129,6 +141,9 @@ bool State::GetPublisherAllowNonVerified() { } void State::SetPublisherAllowVideos(const bool allow) { + ledger_->database()->SaveEventLog( + ledger::kStateAllowVideoContribution, + std::to_string(allow)); ledger_->ledger_client()->SetBooleanState( ledger::kStateAllowVideoContribution, allow); @@ -141,6 +156,8 @@ bool State::GetPublisherAllowVideos() { } void State::SetScoreValues(double a, double b) { + ledger_->database()->SaveEventLog(ledger::kStateScoreA, std::to_string(a)); + ledger_->database()->SaveEventLog(ledger::kStateScoreB, std::to_string(b)); ledger_->ledger_client()->SetDoubleState(ledger::kStateScoreA, a); ledger_->ledger_client()->SetDoubleState(ledger::kStateScoreB, b); } @@ -152,6 +169,9 @@ void State::GetScoreValues(double* a, double* b) { } void State::SetRewardsMainEnabled(bool enabled) { + ledger_->database()->SaveEventLog( + ledger::kStateEnabled, + std::to_string(enabled)); ledger_->ledger_client()->SetBooleanState(ledger::kStateEnabled, enabled); } @@ -160,6 +180,9 @@ bool State::GetRewardsMainEnabled() { } void State::SetAutoContributeEnabled(bool enabled) { + ledger_->database()->SaveEventLog( + ledger::kStateAutoContributeEnabled, + std::to_string(enabled)); ledger_->ledger_client()->SetBooleanState( ledger::kStateAutoContributeEnabled, enabled); @@ -171,6 +194,9 @@ bool State::GetAutoContributeEnabled() { } void State::SetAutoContributionAmount(const double amount) { + ledger_->database()->SaveEventLog( + ledger::kStateAutoContributeAmount, + std::to_string(amount)); ledger_->ledger_client()->SetDoubleState( ledger::kStateAutoContributeAmount, amount); @@ -207,6 +233,9 @@ void State::SetReconcileStamp(const int reconcile_interval) { reconcile_stamp += braveledger_ledger::_reconcile_default_interval; } + ledger_->database()->SaveEventLog( + ledger::kStateNextReconcileStamp, + std::to_string(reconcile_stamp)); ledger_->ledger_client()->SetUint64State( ledger::kStateNextReconcileStamp, reconcile_stamp); @@ -221,6 +250,9 @@ uint64_t State::GetCreationStamp() { } void State::SetCreationStamp(const uint64_t stamp) { + ledger_->database()->SaveEventLog( + ledger::kStateCreationStamp, + std::to_string(stamp)); ledger_->ledger_client()->SetUint64State(ledger::kStateCreationStamp, stamp); } @@ -240,9 +272,15 @@ std::vector State::GetRecoverySeed() { } void State::SetRecoverySeed(const std::vector& seed) { + const std::string seed_string = base::Base64Encode(seed); + std::string event_string; + if (seed.size() > 1) { + event_string = std::to_string(seed[0]+seed[1]); + } + ledger_->database()->SaveEventLog(ledger::kStateRecoverySeed, event_string); ledger_->ledger_client()->SetStringState( ledger::kStateRecoverySeed, - base::Base64Encode(seed)); + seed_string); } std::string State::GetPaymentId() { @@ -250,6 +288,7 @@ std::string State::GetPaymentId() { } void State::SetPaymentId(const std::string& id) { + ledger_->database()->SaveEventLog(ledger::kStatePaymentId, id); ledger_->ledger_client()->SetStringState(ledger::kStatePaymentId, id); } @@ -262,8 +301,10 @@ bool State::GetInlineTippingPlatformEnabled( void State::SetInlineTippingPlatformEnabled( const ledger::InlineTipsPlatforms platform, const bool enabled) { + const std::string platform_string = ConvertInlineTipPlatformToKey(platform); + ledger_->database()->SaveEventLog(platform_string, std::to_string(enabled)); ledger_->ledger_client()->SetBooleanState( - ConvertInlineTipPlatformToKey(platform), + platform_string, enabled); } @@ -338,6 +379,9 @@ std::vector State::GetMonthlyTipChoices() { } void State::SetFetchOldBalanceEnabled(bool enabled) { + ledger_->database()->SaveEventLog( + ledger::kStateFetchOldBalance, + std::to_string(enabled)); ledger_->ledger_client()->SetBooleanState( ledger::kStateFetchOldBalance, enabled); @@ -349,6 +393,9 @@ bool State::GetFetchOldBalanceEnabled() { } void State::SetEmptyBalanceChecked(const bool checked) { + ledger_->database()->SaveEventLog( + ledger::kStateEmptyBalanceChecked, + std::to_string(checked)); ledger_->ledger_client()->SetBooleanState( ledger::kStateEmptyBalanceChecked, checked); @@ -371,6 +418,9 @@ uint64_t State::GetServerPublisherListStamp() { } void State::SetPromotionCorruptedMigrated(const bool migrated) { + ledger_->database()->SaveEventLog( + ledger::kStatePromotionCorruptedMigrated, + std::to_string(migrated)); ledger_->ledger_client()->SetBooleanState( ledger::kStatePromotionCorruptedMigrated, migrated); @@ -393,6 +443,9 @@ uint64_t State::GetPromotionLastFetchStamp() { } void State::SetAnonTransferChecked(const bool checked) { + ledger_->database()->SaveEventLog( + ledger::kStateAnonTransferChecked, + std::to_string(checked)); ledger_->ledger_client()->SetBooleanState( ledger::kStateAnonTransferChecked, checked); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc index bb4b39123cac..5a744f6b2edb 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc @@ -12,7 +12,7 @@ using std::placeholders::_1; namespace { -const int kCurrentVersionNumber = 4; +const int kCurrentVersionNumber = 5; } // namespace @@ -23,8 +23,9 @@ StateMigration::StateMigration(bat_ledger::LedgerImpl* ledger) : v2_(std::make_unique(ledger)), v3_(std::make_unique()), v4_(std::make_unique(ledger)), + v5_(std::make_unique(ledger)), ledger_(ledger) { - DCHECK(v1_ && v2_ && v3_ && v4_); + DCHECK(v1_ && v2_ && v3_ && v4_ && v5_); } StateMigration::~StateMigration() = default; @@ -61,6 +62,10 @@ void StateMigration::Migrate(ledger::ResultCallback callback) { v4_->Migrate(migrate_callback); return; } + case 5: { + v5_->Migrate(migrate_callback); + return; + } } BLOG(0, "Migration version is not handled " << new_version); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h index 0c9bac053bdc..30d6487fcc15 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h @@ -13,6 +13,7 @@ #include "bat/ledger/internal/state/state_migration_v2.h" #include "bat/ledger/internal/state/state_migration_v3.h" #include "bat/ledger/internal/state/state_migration_v4.h" +#include "bat/ledger/internal/state/state_migration_v5.h" #include "bat/ledger/ledger.h" namespace bat_ledger { @@ -38,6 +39,7 @@ class StateMigration { std::unique_ptr v2_; std::unique_ptr v3_; std::unique_ptr v4_; + std::unique_ptr v5_; bat_ledger::LedgerImpl* ledger_; // NOT OWNED }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.cc new file mode 100644 index 000000000000..f6c1f812cdc0 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.cc @@ -0,0 +1,73 @@ +/* Copyright (c) 2020 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/. */ +#include "bat/ledger/internal/state/state_migration_v5.h" + +#include +#include +#include + +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/state/state_keys.h" +#include "bat/ledger/internal/state/state_migration_v4.h" + +namespace braveledger_state { + +StateMigrationV5::StateMigrationV5(bat_ledger::LedgerImpl* ledger) : + ledger_(ledger) { + DCHECK(ledger_); +} + +StateMigrationV5::~StateMigrationV5() = default; + +void StateMigrationV5::Migrate(ledger::ResultCallback callback) { + const auto seed = ledger_->ledger_client()->GetStringState( + ledger::kStateRecoverySeed); + if (seed.empty()) { + callback(ledger::Result::LEDGER_OK); + return; + } + + std::map events; + + // Auto contribute + auto enabled = ledger_->ledger_client()->GetBooleanState( + ledger::kStateAutoContributeEnabled); + events.insert(std::make_pair( + ledger::kStateAutoContributeEnabled, + std::to_string(enabled))); + + // Seed + if (seed.size() > 1) { + const std::string event_string = seed.substr(0, 2); + events.insert(std::make_pair(ledger::kStateRecoverySeed, event_string)); + } + + // Payment id + events.insert(std::make_pair( + ledger::kStatePaymentId, + ledger_->ledger_client()->GetStringState(ledger::kStatePaymentId))); + + // Enabled + enabled = ledger_->ledger_client()->GetBooleanState(ledger::kStateEnabled); + events.insert(std::make_pair(ledger::kStateEnabled, std::to_string(enabled))); + + // Next reconcile + const auto reconcile_stamp = ledger_->ledger_client()->GetUint64State( + ledger::kStateNextReconcileStamp); + events.insert(std::make_pair( + ledger::kStateNextReconcileStamp, + std::to_string(reconcile_stamp))); + + // Creation stamp + const auto creation_stamp = + ledger_->ledger_client()->GetUint64State(ledger::kStateCreationStamp); + events.insert(std::make_pair( + ledger::kStateCreationStamp, + std::to_string(creation_stamp))); + + ledger_->database()->SaveEventLogs(events, callback); +} + +} // namespace braveledger_state diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.h new file mode 100644 index 000000000000..858c03af10f3 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v5.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2020 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 BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V5_H_ +#define BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V5_H_ + +#include "bat/ledger/ledger.h" + +namespace bat_ledger { +class LedgerImpl; +} + +namespace braveledger_state { + +class StateMigrationV5 { + public: + explicit StateMigrationV5(bat_ledger::LedgerImpl* ledger); + ~StateMigrationV5(); + + void Migrate(ledger::ResultCallback callback); + + private: + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace braveledger_state + +#endif // BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V5_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc index b6ccfce57f05..65efec3f1b64 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc @@ -12,6 +12,7 @@ #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/common/time_util.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/uphold/uphold_authorization.h" @@ -208,6 +209,13 @@ void Uphold::DisconnectWallet() { return; } + if (!wallet->address.empty()) { + ledger_->database()->SaveEventLog( + ledger::log::kWalletDisconnected, + static_cast(ledger::kWalletUphold) + "/" + + wallet->address.substr(0, 5)); + } + wallet = ledger::wallet::ResetWallet(std::move(wallet)); ledger_->ledger_client()->ShowNotification( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc index 213c7a17a48b..33d7cd4ae793 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_authorization.cc @@ -9,6 +9,7 @@ #include "base/strings/stringprintf.h" #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/response/response_uphold.h" #include "bat/ledger/internal/uphold/uphold_authorization.h" #include "bat/ledger/internal/uphold/uphold_util.h" @@ -238,6 +239,13 @@ void UpholdAuthorization::OnCardCreate( ledger::kWalletUphold, wallet_ptr->Clone()); + if (!address.empty()) { + ledger_->database()->SaveEventLog( + ledger::log::kWalletConnected, + static_cast(ledger::kWalletUphold) + "/" + + address.substr(0, 5)); + } + std::map args; if (wallet_ptr->status != ledger::WalletStatus::VERIFIED) { args["redirect_url"] = GetSecondStepVerify(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc index d9983273b45f..750429dc7159 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_wallet.cc @@ -8,6 +8,7 @@ #include "base/json/json_reader.h" #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/uphold/uphold_util.h" #include "bat/ledger/internal/uphold/uphold_wallet.h" @@ -166,6 +167,9 @@ ledger::WalletStatus UpholdWallet::GetNewStatus( "wallet_new_verified", {"Uphold"}, [](ledger::Result _){}); + ledger_->database()->SaveEventLog( + ledger::log::kWalletVerified, + ledger::kWalletUphold); break; } case ledger::WalletStatus::VERIFIED: { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc index 9247529c362b..1706098ddf76 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc @@ -11,6 +11,7 @@ #include "base/values.h" #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/logging/event_log_keys.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/wallet/wallet_util.h" @@ -144,6 +145,13 @@ void Wallet::DisconnectWallet( return; } + if (!wallet_ptr->address.empty()) { + ledger_->database()->SaveEventLog( + ledger::log::kWalletDisconnected, + static_cast(wallet_type) + "/" + + wallet_ptr->address.substr(0, 5)); + } + wallet_ptr = ResetWallet(std::move(wallet_ptr)); ledger_->ledger_client()->SaveExternalWallet( wallet_type,