diff --git a/src/Makefile.am b/src/Makefile.am index b47b23bac9..6e346a9b93 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -167,6 +167,7 @@ DEFI_CORE_H = \ masternodes/govvariables/oracle_block_interval.h \ masternodes/govvariables/oracle_deviation.h \ masternodes/gv.h \ + masternodes/historywriter.h \ masternodes/icxorder.h \ masternodes/incentivefunding.h \ masternodes/loan.h \ @@ -389,6 +390,7 @@ libdefi_server_a_SOURCES = \ masternodes/govvariables/oracle_block_interval.cpp \ masternodes/govvariables/oracle_deviation.cpp \ masternodes/gv.cpp \ + masternodes/historywriter.cpp \ masternodes/icxorder.cpp \ masternodes/incentivefunding.cpp \ masternodes/loan.cpp \ diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index 3ed6dd187e..3beafe4a54 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -175,7 +175,7 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c const auto txType = GuessCustomTxType(tx, dummy); if (NotAllowedToFail(txType, nSpendHeight) || (nSpendHeight >= chainparams.GetConsensus().GrandCentralHeight && txType == CustomTxType::UpdateMasternode)) { - CCustomCSView discardCache(mnview); + CCustomCSView discardCache(mnview, nullptr, nullptr, nullptr); auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight, 0, &canSpend); if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) { return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg); diff --git a/src/masternodes/accountshistory.cpp b/src/masternodes/accountshistory.cpp index 607dfd6a57..56a1caa874 100644 --- a/src/masternodes/accountshistory.cpp +++ b/src/masternodes/accountshistory.cpp @@ -5,38 +5,9 @@ #include #include #include -#include +#include #include -struct AccountHistoryKeyNew { - uint32_t blockHeight; - CScript owner; - uint32_t txn; // for order in block - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream &s, Operation ser_action) { - if (ser_action.ForRead()) { - READWRITE(WrapBigEndian(blockHeight)); - blockHeight = ~blockHeight; - } else { - uint32_t blockHeight_ = ~blockHeight; - READWRITE(WrapBigEndian(blockHeight_)); - } - - READWRITE(owner); - - if (ser_action.ForRead()) { - READWRITE(WrapBigEndian(txn)); - txn = ~txn; - } else { - uint32_t txn_ = ~txn; - READWRITE(WrapBigEndian(txn_)); - } - } -}; - static AccountHistoryKeyNew Convert(const AccountHistoryKey &key) { return {key.blockHeight, key.owner, key.txn}; } @@ -90,10 +61,9 @@ std::optional CAccountsHistoryView::ReadAccountHistory(cons return ReadBy(key); } -Res CAccountsHistoryView::WriteAccountHistory(const AccountHistoryKey &key, const AccountHistoryValue &value) { +void CAccountsHistoryView::WriteAccountHistory(const AccountHistoryKey &key, const AccountHistoryValue &value) { WriteBy(key, value); WriteBy(Convert(key), '\0'); - return Res::Ok(); } Res CAccountsHistoryView::EraseAccountHistory(const AccountHistoryKey &key) { @@ -126,19 +96,23 @@ CAccountsHistoryWriter::CAccountsHistoryWriter(CCustomCSView &storage, uint32_t height, uint32_t txn, const uint256 &txid, - uint8_t type, - CHistoryWriters *writers) + uint8_t type) : CStorageView(new CFlushableStorageKV(static_cast(storage.GetStorage()))), height(height), txn(txn), txid(txid), type(type), - writers(writers) {} + writers(storage.GetHistoryWriters()) { +} + +CAccountsHistoryWriter::~CAccountsHistoryWriter() { + writers.ClearState(); +} Res CAccountsHistoryWriter::AddBalance(const CScript &owner, CTokenAmount amount) { auto res = CCustomCSView::AddBalance(owner, amount); - if (writers && amount.nValue != 0 && res.ok) { - writers->AddBalance(owner, amount, vaultID); + if (amount.nValue != 0 && res.ok) { + writers.AddBalance(owner, amount, vaultID); } return res; @@ -146,99 +120,17 @@ Res CAccountsHistoryWriter::AddBalance(const CScript &owner, CTokenAmount amount Res CAccountsHistoryWriter::SubBalance(const CScript &owner, CTokenAmount amount) { auto res = CCustomCSView::SubBalance(owner, amount); - if (writers && res.ok && amount.nValue != 0) { - writers->SubBalance(owner, amount, vaultID); + if (res.ok && amount.nValue != 0) { + writers.SubBalance(owner, amount, vaultID); } return res; } bool CAccountsHistoryWriter::Flush() { - if (writers) { - writers->Flush(height, txid, txn, type, vaultID); - } + writers.Flush(height, txid, txn, type, vaultID); return CCustomCSView::Flush(); } -CAccountHistoryStorage *CAccountsHistoryWriter::GetAccountHistoryStore() { - return writers ? writers->GetAccountHistoryStore() : nullptr; -}; - -CHistoryWriters::CHistoryWriters(CAccountHistoryStorage *historyView, - CBurnHistoryStorage *burnView, - CVaultHistoryStorage *vaultView) - : historyView(historyView), - burnView(burnView), - vaultView(vaultView) {} - -extern std::string ScriptToString(const CScript &script); - -void CHistoryWriters::AddBalance(const CScript &owner, const CTokenAmount amount, const uint256 &vaultID) { - if (historyView) { - diffs[owner][amount.nTokenId] += amount.nValue; - } - if (burnView && owner == Params().GetConsensus().burnAddress) { - burnDiffs[owner][amount.nTokenId] += amount.nValue; - } - if (vaultView && !vaultID.IsNull()) { - vaultDiffs[vaultID][owner][amount.nTokenId] += amount.nValue; - } -} - -void CHistoryWriters::AddFeeBurn(const CScript &owner, const CAmount amount) { - if (burnView && amount != 0) { - burnDiffs[owner][DCT_ID{0}] += amount; - } -} - -void CHistoryWriters::SubBalance(const CScript &owner, const CTokenAmount amount, const uint256 &vaultID) { - if (historyView) { - diffs[owner][amount.nTokenId] -= amount.nValue; - } - if (burnView && owner == Params().GetConsensus().burnAddress) { - burnDiffs[owner][amount.nTokenId] -= amount.nValue; - } - if (vaultView && !vaultID.IsNull()) { - vaultDiffs[vaultID][owner][amount.nTokenId] -= amount.nValue; - } -} - -void CHistoryWriters::Flush(const uint32_t height, - const uint256 &txid, - const uint32_t txn, - const uint8_t type, - const uint256 &vaultID) { - if (historyView) { - for (const auto &diff : diffs) { - LogPrint(BCLog::ACCOUNTCHANGE, - "AccountChange: txid=%s addr=%s change=%s\n", - txid.GetHex(), - ScriptToString(diff.first), - (CBalances{diff.second}.ToString())); - historyView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second}); - } - } - if (burnView) { - for (const auto &diff : burnDiffs) { - burnView->WriteAccountHistory({diff.first, height, txn}, {txid, type, diff.second}); - } - } - if (vaultView) { - for (const auto &diff : vaultDiffs) { - for (const auto &addresses : diff.second) { - vaultView->WriteVaultHistory({height, diff.first, txn, addresses.first}, - {txid, type, addresses.second}); - } - } - if (!schemeID.empty()) { - vaultView->WriteVaultScheme({vaultID, height}, {type, txid, schemeID, txn}); - } - if (!globalLoanScheme.identifier.empty()) { - vaultView->WriteGlobalScheme({height, txn, globalLoanScheme.schemeCreationTxid}, - {globalLoanScheme, type, txid}); - } - } -} - std::unique_ptr paccountHistoryDB; std::unique_ptr pburnHistoryDB; diff --git a/src/masternodes/accountshistory.h b/src/masternodes/accountshistory.h index 4e5c53343f..c04fb1a93a 100644 --- a/src/masternodes/accountshistory.h +++ b/src/masternodes/accountshistory.h @@ -12,55 +12,18 @@ #include