Skip to content

Commit

Permalink
Make account history own database
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Apr 15, 2021
1 parent 15985c4 commit 2538a48
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 110 deletions.
11 changes: 10 additions & 1 deletion src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CStorageKV {
virtual bool Read(const TBytes& key, TBytes& value) const = 0;
virtual std::unique_ptr<CStorageKVIterator> NewIterator() = 0;
virtual size_t SizeEstimate() const = 0;
virtual void Discard() = 0;
virtual bool Flush() = 0;
};

Expand Down Expand Up @@ -153,6 +154,11 @@ class CStorageLevelDB : public CStorageKV {
begin.clear();
return result;
}
void Discard() override {
end.clear();
begin.clear();
batch.Clear();
}
size_t SizeEstimate() const override {
return batch.SizeEstimate();
}
Expand Down Expand Up @@ -300,6 +306,9 @@ class CFlushableStorageKV : public CStorageKV {
changed.clear();
return true;
}
void Discard() override {
changed.clear();
}
size_t SizeEstimate() const override {
return memusage::DynamicUsage(changed);
}
Expand Down Expand Up @@ -478,7 +487,7 @@ class CStorageView {
}

bool Flush() { return DB().Flush(); }

void Discard() { DB().Discard(); }
size_t SizeEstimate() const { return DB().SizeEstimate(); }

protected:
Expand Down
6 changes: 6 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,12 @@ bool AppInitMain(InitInterfaces& interfaces)
// Ensure we are on latest DB version
pcustomcsview->SetDbVersion(CCustomCSView::DbVersion);

// make account history db
paccountHistoryDB.reset();
if (gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) {
paccountHistoryDB = MakeUnique<CAccountHistoryStorage>(GetDataDir() / "history", nCustomCacheSize, false, fReset || fReindexChainState);
}

// If necessary, upgrade from older database format.
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
if (!::ChainstateActive().CoinsDB().Upgrade()) {
Expand Down
83 changes: 79 additions & 4 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,93 @@
#include <masternodes/masternodes.h>
#include <key_io.h>

#include <limits>

/// @attention make sure that it does not overlap with those in masternodes.cpp/tokens.cpp/undos.cpp/accounts.cpp !!!
/// @Note it's in own database
const unsigned char CAccountsHistoryView::ByAccountHistoryKey::prefix = 'h';

void CAccountsHistoryView::ForEachAccountHistory(std::function<bool(AccountHistoryKey const &, CLazySerialize<AccountHistoryValue>)> callback, AccountHistoryKey const & start)
{
ForEach<ByAccountHistoryKey, AccountHistoryKey, AccountHistoryValue>(callback, start);
}

Res CAccountsHistoryView::SetAccountHistory(const AccountHistoryKey& key, const AccountHistoryValue& value)
Res CAccountsHistoryView::WriteAccountHistory(const AccountHistoryKey& key, const AccountHistoryValue& value)
{
WriteBy<ByAccountHistoryKey>(key, value);
return Res::Ok();
}

Res CAccountsHistoryView::EraseAccountHistory(const AccountHistoryKey& key)
{
EraseBy<ByAccountHistoryKey>(key);
return Res::Ok();
}

CAccountHistoryStorage::CAccountHistoryStorage(const fs::path& dbName, std::size_t cacheSize, bool fMemory, bool fWipe)
: CStorageView(new CStorageLevelDB(dbName, cacheSize, fMemory, fWipe))
{
}

CAccountsHistoryWriter::CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView)
: CStorageView(new CFlushableStorageKV(storage.GetRaw())), height(height), txn(txn), txid(txid), type(type), historyView(historyView)
{
}

Res CAccountsHistoryWriter::AddBalance(CScript const & owner, CTokenAmount amount)
{
auto res = CCustomCSView::AddBalance(owner, amount);
if (historyView && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] += amount.nValue;
}
return res;
}

Res CAccountsHistoryWriter::SubBalance(CScript const & owner, CTokenAmount amount)
{
auto res = CCustomCSView::SubBalance(owner, amount);
if (historyView && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] -= amount.nValue;
}
return res;
}

bool CAccountsHistoryWriter::Flush()
{
if (historyView) {
for (const auto& diff : diffs) {
historyView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second});
}
}
return CCustomCSView::Flush();
}

CAccountsHistoryEraser::CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView)
: CStorageView(new CFlushableStorageKV(storage.GetRaw())), height(height), txn(txn), historyView(historyView)
{
}

Res CAccountsHistoryEraser::AddBalance(CScript const & owner, CTokenAmount)
{
if (historyView) {
accounts.insert(owner);
}
return Res::Ok();
}

Res CAccountsHistoryEraser::SubBalance(CScript const & owner, CTokenAmount)
{
if (historyView) {
accounts.insert(owner);
}
return Res::Ok();
}

bool CAccountsHistoryEraser::Flush()
{
if (historyView) {
for (const auto& account : accounts) {
historyView->EraseAccountHistory({account, height, txn});
}
}
return CCustomCSView::Flush();
}

std::unique_ptr<CAccountHistoryStorage> paccountHistoryDB;
43 changes: 41 additions & 2 deletions src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include <amount.h>
#include <flushablestorage.h>
#include <masternodes/masternodes.h>
#include <script/script.h>
#include <uint256.h>


struct AccountHistoryKey {
CScript owner;
uint32_t blockHeight;
Expand Down Expand Up @@ -55,13 +55,52 @@ struct AccountHistoryValue {
class CAccountsHistoryView : public virtual CStorageView
{
public:
Res SetAccountHistory(AccountHistoryKey const & key, AccountHistoryValue const & value);
Res WriteAccountHistory(AccountHistoryKey const & key, AccountHistoryValue const & value);
Res EraseAccountHistory(AccountHistoryKey const & key);
void ForEachAccountHistory(std::function<bool(AccountHistoryKey const &, CLazySerialize<AccountHistoryValue>)> callback, AccountHistoryKey const & start = {});

// tags
struct ByAccountHistoryKey { static const unsigned char prefix; };
};

class CAccountHistoryStorage : public CAccountsHistoryView
{
public:
CAccountHistoryStorage(const fs::path& dbName, std::size_t cacheSize, bool fMemory = false, bool fWipe = false);
};

class CAccountsHistoryWriter : public CCustomCSView
{
const uint32_t height;
const uint32_t txn;
const uint256 txid;
const uint8_t type;
std::map<CScript, TAmounts> diffs;
CAccountsHistoryView* historyView;

public:
CAccountsHistoryWriter(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type, CAccountsHistoryView* historyView);
Res AddBalance(CScript const & owner, CTokenAmount amount) override;
Res SubBalance(CScript const & owner, CTokenAmount amount) override;
bool Flush();
};

class CAccountsHistoryEraser : public CCustomCSView
{
const uint32_t height;
const uint32_t txn;
std::set<CScript> accounts;
CAccountsHistoryView* historyView;

public:
CAccountsHistoryEraser(CCustomCSView & storage, uint32_t height, uint32_t txn, CAccountsHistoryView* historyView);
Res AddBalance(CScript const & owner, CTokenAmount amount) override;
Res SubBalance(CScript const & owner, CTokenAmount amount) override;
bool Flush();
};

extern std::unique_ptr<CAccountHistoryStorage> paccountHistoryDB;

static constexpr bool DEFAULT_ACINDEX = true;

#endif //DEFI_MASTERNODES_ACCOUNTSHISTORY_H
34 changes: 0 additions & 34 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,40 +768,6 @@ bool CCustomCSView::CalculateOwnerRewards(CScript const & owner, uint32_t target
return UpdateBalancesHeight(owner, targetHeight);
}

CAccountsHistoryStorage::CAccountsHistoryStorage(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type)
: CStorageView(new CFlushableStorageKV(storage.GetRaw())), height(height), txn(txn), txid(txid), type(type)
{
acindex = gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX);
}

Res CAccountsHistoryStorage::AddBalance(CScript const & owner, CTokenAmount amount)
{
auto res = CCustomCSView::AddBalance(owner, amount);
if (acindex && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] += amount.nValue;
}
return res;
}

Res CAccountsHistoryStorage::SubBalance(CScript const & owner, CTokenAmount amount)
{
auto res = CCustomCSView::SubBalance(owner, amount);
if (acindex && res.ok && amount.nValue != 0) {
diffs[owner][amount.nTokenId] -= amount.nValue;
}
return res;
}

bool CAccountsHistoryStorage::Flush()
{
if (acindex) {
for (const auto& diff : diffs) {
SetAccountHistory({diff.first, height, txn}, {txid, type, diff.second});
}
}
return CCustomCSView::Flush();
}

std::map<CKeyID, CKey> AmISignerNow(CAnchorData::CTeam const & team)
{
AssertLockHeld(cs_main);
Expand Down
17 changes: 0 additions & 17 deletions src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <pubkey.h>
#include <serialize.h>
#include <masternodes/accounts.h>
#include <masternodes/accountshistory.h>
#include <masternodes/anchors.h>
#include <masternodes/incentivefunding.h>
#include <masternodes/tokens.h>
Expand Down Expand Up @@ -240,7 +239,6 @@ class CCustomCSView
, public CAnchorRewardsView
, public CTokensView
, public CAccountsView
, public CAccountsHistoryView
, public CCommunityBalancesView
, public CUndosView
, public CPoolPairView
Expand Down Expand Up @@ -286,21 +284,6 @@ class CCustomCSView
}
};

class CAccountsHistoryStorage : public CCustomCSView
{
bool acindex;
const uint32_t height;
const uint32_t txn;
const uint256 txid;
const uint8_t type;
std::map<CScript, TAmounts> diffs;
public:
CAccountsHistoryStorage(CCustomCSView & storage, uint32_t height, uint32_t txn, const uint256& txid, uint8_t type);
Res AddBalance(CScript const & owner, CTokenAmount amount) override;
Res SubBalance(CScript const & owner, CTokenAmount amount) override;
bool Flush();
};

std::map<CKeyID, CKey> AmISignerNow(CAnchorData::CTeam const & team);

/** Global DB and view that holds enhanced chainstate data (should be protected by cs_main) */
Expand Down
Loading

0 comments on commit 2538a48

Please sign in to comment.