-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathaccountshistory.cpp
More file actions
156 lines (127 loc) · 5.23 KB
/
Copy pathaccountshistory.cpp
File metadata and controls
156 lines (127 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) DeFi Blockchain Developers
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#include <dfi/accounts.h>
#include <dfi/accountshistory.h>
#include <dfi/historywriter.h>
#include <dfi/vaulthistory.h>
#include <key_io.h>
static AccountHistoryKeyNew Convert(const AccountHistoryKey &key) {
return {key.blockHeight, key.owner, key.txn};
}
static AccountHistoryKey Convert(const AccountHistoryKeyNew &key) {
return {key.owner, key.blockHeight, key.txn};
}
void CAccountsHistoryView::CreateMultiIndexIfNeeded() {
AccountHistoryKeyNew anyNewKey{~0u, {}, ~0u};
if (auto it = LowerBound<ByAccountHistoryKeyNew>(anyNewKey); it.Valid()) {
return;
}
LogPrintf("Adding multi index in progress...\n");
auto startTime = GetTimeMillis();
AccountHistoryKey startKey{{}, ~0u, ~0u};
auto it = LowerBound<ByAccountHistoryKey>(startKey);
for (; it.Valid(); it.Next()) {
WriteBy<ByAccountHistoryKeyNew>(Convert(it.Key()), '\0');
}
Flush();
LogPrint(BCLog::BENCH, " - Multi index took: %dms\n", GetTimeMillis() - startTime);
}
void CAccountsHistoryView::ForEachAccountHistory(
std::function<bool(const AccountHistoryKey &, AccountHistoryValue)> callback,
const CScript &owner,
uint32_t height,
uint32_t txn) {
if (!owner.empty()) {
ForEach<ByAccountHistoryKey, AccountHistoryKey, AccountHistoryValue>(callback, {owner, height, txn});
return;
}
ForEach<ByAccountHistoryKeyNew, AccountHistoryKeyNew, char>(
[&](const AccountHistoryKeyNew &newKey, char) {
auto key = Convert(newKey);
auto value = ReadAccountHistory(key);
assert(value);
return callback(key, *value);
},
{height, owner, txn});
}
std::optional<AccountHistoryValue> CAccountsHistoryView::ReadAccountHistory(const AccountHistoryKey &key) const {
return ReadBy<ByAccountHistoryKey, AccountHistoryValue>(key);
}
void CAccountsHistoryView::WriteAccountHistory(const AccountHistoryKey &key, const AccountHistoryValue &value) {
WriteBy<ByAccountHistoryKey>(key, value);
WriteBy<ByAccountHistoryKeyNew>(Convert(key), '\0');
}
Res CAccountsHistoryView::EraseAccountHistory(const AccountHistoryKey &key) {
EraseBy<ByAccountHistoryKey>(key);
EraseBy<ByAccountHistoryKeyNew>(Convert(key));
return Res::Ok();
}
Res CAccountsHistoryView::EraseAccountHistoryHeight(uint32_t height) {
std::vector<AccountHistoryKey> keysToDelete;
auto it = LowerBound<ByAccountHistoryKeyNew>(AccountHistoryKeyNew{height, {}, ~0u});
for (; it.Valid() && it.Key().blockHeight == height; it.Next()) {
keysToDelete.push_back(Convert(it.Key()));
}
for (const auto &key : keysToDelete) {
EraseAccountHistory(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)) {}
CBurnHistoryStorage::CBurnHistoryStorage(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)
: CStorageView(new CFlushableStorageKV(static_cast<CStorageKV &>(storage.GetStorage()))),
height(height),
txn(txn),
txid(txid),
type(type),
writers(storage.GetHistoryWriters()) {
if (storage.HasAttributes()) {
attributes = storage.GetAttributes();
}
persistentAttributes = true;
}
CAccountsHistoryWriter::~CAccountsHistoryWriter() {
writers.ClearState();
}
Res CAccountsHistoryWriter::AddBalance(const CScript &owner, CTokenAmount amount) {
auto res = CCustomCSView::AddBalance(owner, amount);
if (amount.nValue != 0 && res.ok) {
writers.AddBalance(owner, amount, vaultID);
}
return res;
}
Res CAccountsHistoryWriter::SubBalance(const CScript &owner, CTokenAmount amount) {
auto res = CCustomCSView::SubBalance(owner, amount);
if (res.ok && amount.nValue != 0) {
writers.SubBalance(owner, amount, vaultID);
}
return res;
}
Res CAccountsHistoryWriter::AddVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) {
auto res = CCustomCSView::AddVaultCollateral(vaultId, amount);
if (res && amount.nValue) {
writers.AddVaultCollateral(amount, vaultID);
}
return res;
}
Res CAccountsHistoryWriter::SubVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) {
auto res = CCustomCSView::SubVaultCollateral(vaultId, amount);
if (res && amount.nValue) {
writers.SubVaultCollateral(amount, vaultID);
}
return res;
}
bool CAccountsHistoryWriter::Flush() {
writers.Flush(height, txid, txn, type, vaultID);
return CCustomCSView::Flush();
}
std::unique_ptr<CAccountHistoryStorage> paccountHistoryDB;
std::unique_ptr<CBurnHistoryStorage> pburnHistoryDB;