Skip to content

Commit

Permalink
Move ProcessEVMQueue to fallible location (#2263)
Browse files Browse the repository at this point in the history
Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Aug 3, 2023
1 parent d94f4c7 commit 37e7c14
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
60 changes: 38 additions & 22 deletions src/masternodes/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,8 +2378,8 @@ static void RevertFailedTransferDomainTxs(const std::vector<std::string> &failed
}
}

static void ProcessEVMQueue(const CBlock &block, const CBlockIndex *pindex, CCustomCSView &cache, const CChainParams& chainparams, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary) {
if (!IsEVMEnabled(pindex->nHeight, cache, chainparams.GetConsensus())) return;
static Res ProcessEVMQueue(const CBlock &block, const CBlockIndex *pindex, CCustomCSView &cache, const CChainParams& chainparams, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary) {
if (!IsEVMEnabled(pindex->nHeight, cache, chainparams.GetConsensus())) return Res::Ok();

CKeyID minter;
assert(block.ExtractMinterKey(minter));
Expand Down Expand Up @@ -2424,19 +2424,16 @@ static void ProcessEVMQueue(const CBlock &block, const CBlockIndex *pindex, CCus
CrossBoundaryResult result;
const auto blockResult = evm_try_finalize(result, evmQueueId, false, block.nBits, beneficiary, block.GetBlockTime());
if (!result.ok) {
LogPrintf("ERROR: EVM try finalize failed: %s\n", result.reason.c_str());
return Res::Err(result.reason.c_str());
}
auto evmBlockHashData = std::vector<uint8_t>(blockResult.block_hash.rbegin(), blockResult.block_hash.rend());
auto evmBlockHash = uint256(evmBlockHashData);

auto res = cache.SetVMDomainBlockEdge(VMDomainEdge::DVMToEVM, block.GetHash(), evmBlockHash);
if (!res) {
LogPrintf("Failed to store DVMtoEVM block hash for DFI TX %s\n", block.GetHash().ToString());
}
if (!res) return res;

res = cache.SetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash, block.GetHash());
if (!res) {
LogPrintf("Failed to store EVMToDVM block hash for DFI TX %s\n", block.GetHash().ToString());
}
if (!res) return res;

if (!blockResult.failed_transactions.empty()) {
std::vector<std::string> failedTransactions;
Expand All @@ -2447,8 +2444,37 @@ static void ProcessEVMQueue(const CBlock &block, const CBlockIndex *pindex, CCus
RevertFailedTransferDomainTxs(failedTransactions, block, chainparams.GetConsensus(), pindex->nHeight, cache);
}

cache.AddBalance(Params().GetConsensus().burnAddress, {DCT_ID{}, static_cast<CAmount>(blockResult.total_burnt_fees)});
cache.AddBalance(minerAddress, {DCT_ID{}, static_cast<CAmount>(blockResult.total_priority_fees)});
res = cache.AddBalance(Params().GetConsensus().burnAddress, {DCT_ID{}, static_cast<CAmount>(blockResult.total_burnt_fees)});
if (!res) return res;
res = cache.AddBalance(minerAddress, {DCT_ID{}, static_cast<CAmount>(blockResult.total_priority_fees)});
if (!res) return res;

return Res::Ok();
}

static void FlushCacheCreateUndo(const CBlockIndex *pindex, CCustomCSView &mnview, CCustomCSView &cache, const uint256 hash) {
// construct undo
auto& flushable = cache.GetStorage();
auto undo = CUndo::Construct(mnview.GetStorage(), flushable.GetRaw());
// flush changes to underlying view
cache.Flush();
// write undo
if (!undo.before.empty()) {
mnview.SetUndo(UndoKey{static_cast<uint32_t>(pindex->nHeight), hash }, undo);
}
}

Res ProcessFallibleEvent(const CBlock &block, const CBlockIndex *pindex, CCustomCSView &mnview, const CChainParams& chainparams, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary) {
CCustomCSView cache(mnview);

// Process EVM block
auto res = ProcessEVMQueue(block, pindex, cache, chainparams, evmQueueId, beneficiary);
if (!res) return res;

// Construct undo
FlushCacheCreateUndo(pindex, mnview, cache, uint256S(std::string(64, '1')));

return Res::Ok();
}

void ProcessDeFiEvent(const CBlock &block, const CBlockIndex* pindex, CCustomCSView& mnview, const CCoinsViewCache& view, const CChainParams& chainparams, const CreationTxs &creationTxs, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary) {
Expand Down Expand Up @@ -2505,16 +2531,6 @@ void ProcessDeFiEvent(const CBlock &block, const CBlockIndex* pindex, CCustomCSV
// Migrate foundation members to attributes
ProcessGrandCentralEvents(pindex, cache, chainparams);

// Execute EVM Queue
ProcessEVMQueue(block, pindex, cache, chainparams, evmQueueId, beneficiary);

// construct undo
auto& flushable = cache.GetStorage();
auto undo = CUndo::Construct(mnview.GetStorage(), flushable.GetRaw());
// flush changes to underlying view
cache.Flush();
// write undo
if (!undo.before.empty()) {
mnview.SetUndo(UndoKey{static_cast<uint32_t>(pindex->nHeight), uint256() }, undo); // "zero hash"
}
FlushCacheCreateUndo(pindex, mnview, cache, uint256());
}
2 changes: 2 additions & 0 deletions src/masternodes/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CCustomCSView;
using CreationTxs = std::map<uint32_t, std::pair<uint256, std::vector<std::pair<DCT_ID, uint256>>>>;

void ProcessDeFiEvent(const CBlock &block, const CBlockIndex* pindex, CCustomCSView& mnview, const CCoinsViewCache& view, const CChainParams& chainparams, const CreationTxs &creationTxs, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary);
Res ProcessFallibleEvent(const CBlock &block, const CBlockIndex *pindex, CCustomCSView &mnview, const CChainParams& chainparams, const uint64_t evmQueueId, std::array<uint8_t, 20>& beneficiary);

std::vector<CAuctionBatch> CollectAuctionBatches(const CVaultAssets& vaultAssets, const TAmounts& collBalances, const TAmounts& loanBalances);


Expand Down
9 changes: 8 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,8 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
}

// special case: possible undo (first) of custom 'complex changes' for the whole block (expired orders and/or prices)
mnview.OnUndoTx(uint256(), (uint32_t) pindex->nHeight); // undo for "zero hash"
mnview.OnUndoTx(uint256(), static_cast<uint32_t>(pindex->nHeight)); // undo for "zero hash"
mnview.OnUndoTx(uint256S(std::string(64, '1')), static_cast<uint32_t>(pindex->nHeight)); // undo for "one hash"

// Undo community balance increments
ReverseGeneralCoinbaseTx(mnview, pindex->nHeight, Params().GetConsensus());
Expand Down Expand Up @@ -2838,6 +2839,12 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
// account changes are validated
accountsView.Flush();

// Execute EVM Queue
res = ProcessFallibleEvent(block, pindex, mnview, chainparams, evmQueueId, beneficiary);
if (!res.ok) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, error("%s: %s", __func__, res.msg), REJECT_INVALID, res.dbgMsg);
}

if (!WriteUndoDataForBlock(blockundo, state, pindex, chainparams))
return false;

Expand Down

0 comments on commit 37e7c14

Please sign in to comment.