Skip to content

Commit

Permalink
Extend logvmmaps (#2264)
Browse files Browse the repository at this point in the history
* Extend logvmmaps

* Log errors on write failure

* Update RPC help text

* Update logvmmaps tests

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Aug 3, 2023
1 parent 017590e commit d94f4c7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/masternodes/evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ ResVal<uint256> CVMDomainGraphView::GetVMDomainTxEdge(VMDomainEdge type, uint256
return DeFiErrors::DatabaseKeyNotFound(txHashKey.GetHex());
}

void CVMDomainGraphView::ForEachVMDomainBlockEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback) {
void CVMDomainGraphView::ForEachVMDomainBlockEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback, const std::pair<VMDomainEdge, uint256> &start) {
ForEach<VMDomainBlockEdge, std::pair<uint8_t, uint256>, uint256>(
[&callback](const std::pair<uint8_t, uint256> &key, uint256 val) {
auto k = std::make_pair(static_cast<VMDomainEdge>(key.first), key.second);
return callback(k, val);
});
}, std::make_pair(static_cast<uint8_t>(start.first), start.second));
}

void CVMDomainGraphView::ForEachVMDomainTxEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback) {
void CVMDomainGraphView::ForEachVMDomainTxEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback, const std::pair<VMDomainEdge, uint256> &start) {
ForEach<VMDomainTxEdge, std::pair<uint8_t, uint256>, uint256>(
[&callback](const std::pair<uint8_t, uint256> &key, uint256 val) {
auto k = std::make_pair(static_cast<VMDomainEdge>(key.first), key.second);
return callback(k, val);
});
}, std::make_pair(static_cast<uint8_t>(start.first), start.second));
}
4 changes: 2 additions & 2 deletions src/masternodes/evm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class CVMDomainGraphView : public virtual CStorageView {
public:
Res SetVMDomainBlockEdge(VMDomainEdge type, uint256 blockHashKey, uint256 blockHash);
ResVal<uint256> GetVMDomainBlockEdge(VMDomainEdge type, uint256 blockHashKey) const;
void ForEachVMDomainBlockEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback);
void ForEachVMDomainBlockEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback, const std::pair<VMDomainEdge, uint256> &start = {});

Res SetVMDomainTxEdge(VMDomainEdge type, uint256 txHashKey, uint256 txHash);
ResVal<uint256> GetVMDomainTxEdge(VMDomainEdge type, uint256 txHashKey) const;
void ForEachVMDomainTxEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback);
void ForEachVMDomainTxEdges(std::function<bool(const std::pair<VMDomainEdge, uint256> &, const uint256 &)> callback, const std::pair<VMDomainEdge, uint256> &start = {});

struct VMDomainBlockEdge {
static constexpr uint8_t prefix() { return 'N'; }
Expand Down
10 changes: 8 additions & 2 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3993,8 +3993,14 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
sha3(obj.evmTx, evmTxHashBytes);
auto txHash = tx.GetHash();
auto evmTxHash = uint256S(HexStr(evmTxHashBytes));
mnview.SetVMDomainTxEdge(VMDomainEdge::DVMToEVM, txHash, evmTxHash);
mnview.SetVMDomainTxEdge(VMDomainEdge::EVMToDVM, evmTxHash, txHash);
auto res = mnview.SetVMDomainTxEdge(VMDomainEdge::DVMToEVM, txHash, evmTxHash);
if (!res) {
LogPrintf("Failed to store DVMtoEVM TX hash for DFI TX %s\n", txHash.ToString());
}
res = mnview.SetVMDomainTxEdge(VMDomainEdge::EVMToDVM, evmTxHash, txHash);
if (!res) {
LogPrintf("Failed to store EVMToDVM TX hash for DFI TX %s\n", txHash.ToString());
}
return Res::Ok();
}

Expand Down
49 changes: 39 additions & 10 deletions src/masternodes/rpc_evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum class VMDomainRPCMapType {

static int VMDomainRPCMapTypeCount = 7;

enum class VMDomainIndexType { BlockHash, TxHash };
enum class VMDomainIndexType { BlockHashDVMToEVM, BlockHashEVMToDVM, TxHashDVMToEVM, TxHashEVMToDVM };

UniValue evmtx(const JSONRPCRequest &request) {
auto pwallet = GetWallet(request);
Expand Down Expand Up @@ -267,7 +267,11 @@ UniValue logvmmaps(const JSONRPCRequest &request) {
RPCHelpMan{
"logvmmaps",
"\nLogs all block or tx indexes for debugging.\n",
{{"type", RPCArg::Type::NUM, RPCArg::Optional::NO, "Type of log: 0 - Blocks, 1 - Txs"}},
{{"type", RPCArg::Type::NUM, RPCArg::Optional::NO, "Type of log:\n"
" 0 - DVMToEVM Blocks\n"
" 1 - EVMToDVM Blocks\n"
" 2 - DVMToEVM TXs\n"
" 3 - EVMToDVM TXs"}},
RPCResult{"{...} (array) Json object with account balances if rpcresult is enabled."
"This is for debugging purposes only.\n"},
RPCExamples{HelpExampleCli("logvmmaps", R"('"<hex>"' 1)")},
Expand All @@ -276,37 +280,62 @@ UniValue logvmmaps(const JSONRPCRequest &request) {

LOCK(cs_main);

size_t count{};
uint64_t count{};
UniValue result{UniValue::VOBJ};
UniValue indexesJson{UniValue::VOBJ};
const auto type = static_cast<VMDomainIndexType>(request.params[0].get_int());
// TODO: For now, we iterate through the whole list. But this is just a debugging RPC.
// But there's no need to iterate the whole list, we can start at where we need to and
// return false, once we hit the limit and stop the iter.
switch (type) {
case VMDomainIndexType::BlockHash: {
case VMDomainIndexType::BlockHashDVMToEVM: {
pcustomcsview->ForEachVMDomainBlockEdges(
[&](const std::pair<VMDomainEdge, uint256> &index, uint256 blockHash) {
[&](const std::pair<VMDomainEdge, uint256> &index, const uint256 &blockHash) {
if (index.first == VMDomainEdge::DVMToEVM) {
indexesJson.pushKV(index.second.GetHex(), blockHash.GetHex());
++count;
}
return true;
});
}, std::make_pair(VMDomainEdge::DVMToEVM, uint256{}));
break;
}
case VMDomainIndexType::BlockHashEVMToDVM: {
pcustomcsview->ForEachVMDomainBlockEdges(
[&](const std::pair<VMDomainEdge, uint256> &index, const uint256 &blockHash) {
if (index.first == VMDomainEdge::EVMToDVM) {
indexesJson.pushKV(index.second.GetHex(), blockHash.GetHex());
++count;
}
return true;
}, std::make_pair(VMDomainEdge::EVMToDVM, uint256{}));
break;
}
case VMDomainIndexType::TxHash: {
pcustomcsview->ForEachVMDomainTxEdges([&](const std::pair<VMDomainEdge, uint256> &index, uint256 txHash) {
case VMDomainIndexType::TxHashDVMToEVM: {
pcustomcsview->ForEachVMDomainTxEdges([&](const std::pair<VMDomainEdge, uint256> &index, const uint256 &txHash) {
if (index.first == VMDomainEdge::DVMToEVM) {
indexesJson.pushKV(index.second.GetHex(), txHash.GetHex());
++count;
}
return true;
});
}, std::make_pair(VMDomainEdge::DVMToEVM, uint256{}));
break;
}
case VMDomainIndexType::TxHashEVMToDVM: {
pcustomcsview->ForEachVMDomainTxEdges([&](const std::pair<VMDomainEdge, uint256> &index, const uint256 &txHash) {
if (index.first == VMDomainEdge::EVMToDVM) {
indexesJson.pushKV(index.second.GetHex(), txHash.GetHex());
++count;
}
return true;
}, std::make_pair(VMDomainEdge::EVMToDVM, uint256{}));
break;
}
default:
throw JSONRPCError(RPC_INVALID_PARAMETER, "type out of range");
}

result.pushKV("indexes", indexesJson);
result.pushKV("count", static_cast<uint64_t>(count));
result.pushKV("count", count);
return result;
}

Expand Down
10 changes: 8 additions & 2 deletions src/masternodes/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2429,8 +2429,14 @@ static void ProcessEVMQueue(const CBlock &block, const CBlockIndex *pindex, CCus
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) {
LogPrintf("Failed to store DVMtoEVM block hash for DFI TX %s\n", block.GetHash().ToString());
}
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 (!blockResult.failed_transactions.empty()) {
std::vector<std::string> failedTransactions;
Expand Down
10 changes: 5 additions & 5 deletions test/functional/feature_evm_vmmap_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ def vmmap_rollback_should_succeed(self):
new_block = self.nodes[0].eth_getBlockByNumber("latest", False)['hash']
list_blocks = self.nodes[0].logvmmaps(0)
list_blocks = list(list_blocks['indexes'].values())
list_tx = self.nodes[0].logvmmaps(1)
list_tx = self.nodes[0].logvmmaps(2)
list_tx = list(list_tx['indexes'].keys())
assert_equal(base_block[2:] in list_blocks, True)
assert_equal(new_block[2:] in list_blocks, True)
assert_equal(tx in list_tx, True)
self.nodes[0].invalidateblock(base_block_dvm)
list_blocks = self.nodes[0].logvmmaps(0)
list_blocks = list(list_blocks['indexes'].values())
list_tx = self.nodes[0].logvmmaps(1)
list_tx = self.nodes[0].logvmmaps(2)
list_tx = list(list_tx['indexes'].keys())
assert_equal(base_block[2:] in list_blocks, True)
assert_equal(new_block[2:] in list_blocks, False)
Expand All @@ -160,13 +160,13 @@ def logvmmaps_tx_exist(self):
self.nodes[0].generate(1)
tx = self.nodes[0].evmtx(self.ethAddress, 0, 21, 21000, self.toAddress, 1)
self.nodes[0].generate(1)
list_tx = self.nodes[0].logvmmaps(1)
list_tx = self.nodes[0].logvmmaps(2)
eth_tx = self.nodes[0].eth_getBlockByNumber("latest", False)['transactions'][0]
assert_equal(eth_tx[2:] in list(list_tx['indexes'].values()), True)
assert_equal(tx in list(list_tx['indexes'].keys()), True)

def logvmmaps_invalid_tx_should_fail(self):
list_tx = self.nodes[0].logvmmaps(1)
list_tx = self.nodes[0].logvmmaps(2)
assert_equal("invalid tx" not in list(list_tx['indexes'].values()), True)
assert_equal("0x0000000000000000000000000000000000000000000000000000000000000000" not in list(list_tx['indexes'].values()), True)
assert_equal("garbage" not in list(list_tx['indexes'].values()), True)
Expand All @@ -180,7 +180,7 @@ def logvmmaps_block_exist(self):
assert_equal(dfi_block in list(list_blocks['indexes'].keys()), True)

def logvmmaps_invalid_block_should_fail(self):
list_block = self.nodes[0].logvmmaps(1)
list_block = self.nodes[0].logvmmaps(2)
assert_equal("invalid tx" not in list(list_block['indexes'].values()), True)
assert_equal("0x0000000000000000000000000000000000000000000000000000000000000000" not in list(list_block['indexes'].values()), True)
assert_equal("garbage" not in list(list_block['indexes'].values()), True)
Expand Down

0 comments on commit d94f4c7

Please sign in to comment.