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

feat: Re-enable vmmap blocknumber #2361

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
64 changes: 31 additions & 33 deletions src/masternodes/rpc_evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,44 +234,42 @@ UniValue vmmap(const JSONRPCRequest &request) {

return VMDomainRPCMapType::Unknown;
};
/*

auto crossBoundaryOkOrThrow = [&throwInvalidParam](CrossBoundaryResult &result) {
if (!result.ok) {
throwInvalidParam(result.reason.c_str());
}
};

auto tryResolveBlockNumberType =
[&throwUnsupportedAuto, &crossBoundaryOkOrThrow](const std::string input) {
[&throwUnsupportedAuto](const std::string input) {
uint64_t height;
if (!ParseUInt64(input, &height)) {
return VMDomainRPCMapType::Unknown;
}
CrossBoundaryResult result;
auto evmBlockCount = evm_try_get_block_count(result);
crossBoundaryOkOrThrow(result);

// evm block count always less than dvm block count
if (height > evmBlockCount) {
CrossBoundaryResult evmResult;
evm_try_get_block_hash_by_number(evmResult, height);
if (evmResult.ok) {
return VMDomainRPCMapType::BlockNumberEVMToDVM;
}

CBlockIndex* dvmBlock = ::ChainActive()[height];
if (dvmBlock != nullptr) {
return VMDomainRPCMapType::BlockNumberDVMToEVM;
} else {
// auto evmFirstBlock = evm_try_get_first_block(result);
// crossBoundaryOkOrThrow(result);
// if (height >= evmFirstBlock && height <= evmBlockCount) {
// return VMDomainRPCMapType::BlockNumberEVMToDVM;
// }
throwUnsupportedAuto();
return VMDomainRPCMapType::Unknown;
}

throwUnsupportedAuto();
return VMDomainRPCMapType::Unknown;
};
*/

auto type = static_cast<VMDomainRPCMapType>(typeInt);
ResVal res = ResVal<std::string>(std::string{}, Res::Ok());

auto handleAutoInfer = [&]() -> std::tuple<VMDomainRPCMapType, bool> {
// auto mapType = tryResolveBlockNumberType(input);
// if (mapType != VMDomainRPCMapType::Unknown)
// return {mapType, false};
auto mapType = tryResolveBlockNumberType(input);
if (mapType != VMDomainRPCMapType::Unknown)
return {mapType, false};

auto inLength = input.length();
if (inLength == 64 || inLength == 66) {
Expand All @@ -296,7 +294,7 @@ UniValue vmmap(const JSONRPCRequest &request) {
return ret;
}
};
/*

auto finalizeBlockNumberResult = [&](uint64_t &number, const VMDomainRPCMapType type, const uint64_t input) {
UniValue ret(UniValue::VOBJ);
ret.pushKV("input", input);
Expand All @@ -307,6 +305,7 @@ UniValue vmmap(const JSONRPCRequest &request) {

auto handleMapBlockNumberEVMToDVMRequest = [&throwInvalidParam,
&finalizeBlockNumberResult,
&ensureEVMHashStripped,
&crossBoundaryOkOrThrow](const std::string &input) -> UniValue {
uint64_t height;
bool success = ParseUInt64(input, &height);
Expand All @@ -315,13 +314,13 @@ UniValue vmmap(const JSONRPCRequest &request) {
}
CrossBoundaryResult result;
auto evmHash = evm_try_get_block_hash_by_number(result, height);
auto evmBlockHash = std::string(evmHash.data(), evmHash.length());
auto evmBlockHash = ensureEVMHashStripped(std::string(evmHash.data(), evmHash.length()));
crossBoundaryOkOrThrow(result);
ResVal<uint256> dvm_block = pcustomcsview->GetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash);
if (!dvm_block) {
throwInvalidParam(dvm_block.msg);
auto dvmBlockHash = pcustomcsview->GetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash);
if (!dvmBlockHash.val.has_value()) {
throwInvalidParam(dvmBlockHash.msg);
}
CBlockIndex *pindex = LookupBlockIndex(*dvm_block.val);
CBlockIndex *pindex = LookupBlockIndex(uint256S(*dvmBlockHash.val));
uint64_t blockNumber = pindex->GetBlockHeader().deprecatedHeight;
return finalizeBlockNumberResult(blockNumber, VMDomainRPCMapType::BlockNumberEVMToDVM, height);
};
Expand All @@ -346,7 +345,7 @@ UniValue vmmap(const JSONRPCRequest &request) {
crossBoundaryOkOrThrow(result);
return finalizeBlockNumberResult(blockNumber, VMDomainRPCMapType::BlockNumberDVMToEVM, height);
};
*/

LOCK(cs_main);

if (type == VMDomainRPCMapType::Auto) {
Expand Down Expand Up @@ -374,13 +373,12 @@ UniValue vmmap(const JSONRPCRequest &request) {
res = pcustomcsview->GetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, input);
break;
}
// TODO(canonbrother): disable for release, more investigation needed
// case VMDomainRPCMapType::BlockNumberDVMToEVM: {
// return handleMapBlockNumberDVMToEVMRequest(input);
// }
// case VMDomainRPCMapType::BlockNumberEVMToDVM: {
// return handleMapBlockNumberEVMToDVMRequest(input);
// }
case VMDomainRPCMapType::BlockNumberDVMToEVM: {
return handleMapBlockNumberDVMToEVMRequest(input);
}
case VMDomainRPCMapType::BlockNumberEVMToDVM: {
return handleMapBlockNumberEVMToDVMRequest(input);
}
default: {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown map type");
}
Expand Down
12 changes: 6 additions & 6 deletions test/functional/feature_evm_vmmap_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ def vmmap_valid_block_number_should_succeed(self):
assert_equal(res["type"], "BlockNumberDVMToEVM")
assert_equal(res["output"], item[1])

# res = self.nodes[0].vmmap(str(item[1]), VMMapType.Auto)
# assert_equal(res['input'], item[1])
# assert_equal(res['type'], 'BlockNumberEVMToDVM')
# assert_equal(res['output'], item[0])
res = self.nodes[0].vmmap(str(item[1]), VMMapType.Auto)
assert_equal(res['input'], item[1])
assert_equal(res['type'], 'BlockNumberEVMToDVM')
assert_equal(res['output'], item[0])

def vmmap_invalid_block_number_should_fail(self):
assert_invalid = lambda *args: assert_raises_rpc_error(
Expand Down Expand Up @@ -368,8 +368,8 @@ def run_test(self):
self.vmmap_valid_tx_should_succeed()
self.vmmap_valid_block_should_succeed()
self.vmmap_invalid_should_fail()
# self.vmmap_valid_block_number_should_succeed()
# self.vmmap_invalid_block_number_should_fail()
self.vmmap_valid_block_number_should_succeed()
self.vmmap_invalid_block_number_should_fail()
self.vmmap_rollback_should_succeed()
self.vmmap_auto_invalid_input_should_fail()
# logvmmap tests
Expand Down
Loading