-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from DeFiCh/feature/listaccounthistory
rpc listaccounthistory
- Loading branch information
Showing
15 changed files
with
383 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,4 @@ Res CAccountsView::SubBalances(CScript const & owner, CBalances const & balances | |
} | ||
return Res::Ok(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2020 DeFi Blockchain Developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <masternodes/accountshistory.h> | ||
#include <masternodes/accounts.h> | ||
#include <key_io.h> | ||
|
||
/// @attention make sure that it does not overlap with those in masternodes.cpp/tokens.cpp/undos.cpp/accounts.cpp !!! | ||
const unsigned char CAccountsHistoryView::ByAccountHistoryKey::prefix = 'h'; // don't intersects with CMintedHeadersView::MintedHeaders::prefix due to different DB | ||
|
||
void CAccountsHistoryView::ForEachAccountHistory(std::function<bool(CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diffs)> callback, AccountHistoryKey start) const | ||
{ | ||
ForEach<ByAccountHistoryKey, AccountHistoryKey, AccountHistoryValue>([&callback] (AccountHistoryKey const & key, AccountHistoryValue const & val) { | ||
return callback(key.owner,key.blockHeight, key.txn, val.txid, val.category, val.diff); | ||
}, start); | ||
} | ||
|
||
Res CAccountsHistoryView::SetAccountHistory(const CScript & owner, uint32_t height, uint32_t txn, const uint256 & txid, unsigned char category, TAmounts const & diff) | ||
{ | ||
//// left for debug: | ||
// std::string ownerStr; | ||
// CTxDestination dest; | ||
// if (!ExtractDestination(owner, dest)) { | ||
// ownerStr = owner.GetHex(); | ||
// } else | ||
// ownerStr = EncodeDestination(dest); | ||
// LogPrintf("DEBUG: SetAccountHistory: owner: %s, ownerStr: %s, block: %i, txn: %d, txid: %s, diffs: %ld\n", owner.GetHex().c_str(), ownerStr.c_str(), height, txn, txid.ToString().c_str(), diff.size()); | ||
|
||
WriteBy<ByAccountHistoryKey>(AccountHistoryKey{owner, height, txn}, AccountHistoryValue{txid, category, diff}); | ||
return Res::Ok(); | ||
} | ||
|
||
bool CAccountsHistoryView::TrackAffectedAccounts(CStorageKV const & before, MapKV const & diff, uint32_t height, uint32_t txn, const uint256 & txid, unsigned char category) { | ||
if (!gArgs.GetBoolArg("-acindex", false)) | ||
return false; | ||
|
||
std::map<CScript, TAmounts> balancesDiff; | ||
using TKey = std::pair<unsigned char, BalanceKey>; | ||
|
||
for (auto it = diff.lower_bound({CAccountsView::ByBalanceKey::prefix}); it != diff.end() && it->first.at(0) == CAccountsView::ByBalanceKey::prefix; ++it) { | ||
CAmount oldAmount = 0, newAmount = 0; | ||
|
||
if (it->second) { | ||
BytesToDbType(*it->second, newAmount); | ||
} | ||
TBytes beforeVal; | ||
if (before.Read(it->first, beforeVal)) { | ||
BytesToDbType(beforeVal, oldAmount); | ||
} | ||
TKey balanceKey; | ||
BytesToDbType(it->first, balanceKey); | ||
balancesDiff[balanceKey.second.owner][balanceKey.second.tokenID] = newAmount - oldAmount; | ||
} | ||
for (auto const & kv : balancesDiff) { | ||
SetAccountHistory(kv.first, height, txn, txid, category, kv.second); | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2020 DeFi Blockchain Developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef DEFI_MASTERNODES_ACCOUNTSHISTORY_H | ||
#define DEFI_MASTERNODES_ACCOUNTSHISTORY_H | ||
|
||
#include <flushablestorage.h> | ||
#include <masternodes/res.h> | ||
#include <amount.h> | ||
#include <script/script.h> | ||
#include <uint256.h> | ||
|
||
|
||
struct AccountHistoryKey { | ||
CScript owner; | ||
uint32_t blockHeight; | ||
uint32_t txn; // for order in block | ||
|
||
ADD_SERIALIZE_METHODS; | ||
|
||
template <typename Stream, typename Operation> | ||
inline void SerializationOp(Stream& s, Operation ser_action) { | ||
READWRITE(owner); | ||
|
||
if (ser_action.ForRead()) { | ||
READWRITE(WrapBigEndian(blockHeight)); | ||
blockHeight = ~blockHeight; | ||
READWRITE(WrapBigEndian(txn)); | ||
txn = ~txn; | ||
} | ||
else { | ||
uint32_t blockHeight_ = ~blockHeight; | ||
READWRITE(WrapBigEndian(blockHeight_)); | ||
uint32_t txn_ = ~txn; | ||
READWRITE(WrapBigEndian(txn_)); | ||
} | ||
} | ||
}; | ||
|
||
struct AccountHistoryValue { | ||
uint256 txid; | ||
unsigned char category; | ||
TAmounts diff; | ||
|
||
ADD_SERIALIZE_METHODS; | ||
|
||
template <typename Stream, typename Operation> | ||
inline void SerializationOp(Stream& s, Operation ser_action) { | ||
READWRITE(txid); | ||
READWRITE(category); | ||
READWRITE(diff); | ||
} | ||
}; | ||
|
||
class CAccountsHistoryView : public virtual CStorageView | ||
{ | ||
public: | ||
Res SetAccountHistory(CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diff); | ||
void ForEachAccountHistory(std::function<bool(CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diff)> callback, AccountHistoryKey start) const; | ||
bool TrackAffectedAccounts(CStorageKV const & before, MapKV const & diff, uint32_t height, uint32_t txn, const uint256 & txid, unsigned char category); | ||
|
||
// tags | ||
struct ByAccountHistoryKey { static const unsigned char prefix; }; | ||
}; | ||
|
||
#endif //DEFI_MASTERNODES_ACCOUNTSHISTORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,5 +220,4 @@ struct BalanceKey { | |
READWRITE(WrapBigEndian(tokenID.v)); | ||
} | ||
}; | ||
|
||
#endif //DEFI_MASTERNODES_BALANCES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.