Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ProcessEVMQueue to fallible location #2263

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 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,13 +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);

cache.SetVMDomainBlockEdge(VMDomainEdge::DVMToEVM, block.GetHash(), evmBlockHash);
cache.SetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash, block.GetHash());
auto res = cache.SetVMDomainBlockEdge(VMDomainEdge::DVMToEVM, block.GetHash(), evmBlockHash);
if (!res) return res;

res = cache.SetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash, block.GetHash());
if (!res) return res;

if (!blockResult.failed_transactions.empty()) {
std::vector<std::string> failedTransactions;
Expand All @@ -2441,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 @@ -2499,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