From cd339b42ba84d0c1cb2dcc482f22fc5dc9f5feeb Mon Sep 17 00:00:00 2001 From: canonbrother Date: Fri, 18 Aug 2023 17:15:43 +0800 Subject: [PATCH 1/3] hashlookup bn d->e --- src/masternodes/errors.h | 8 ++++++++ src/masternodes/rpc_evm.cpp | 31 ++++++++++++++++++------------- src/rpc/blockchain.cpp | 2 +- src/rpc/blockchain.h | 4 ++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/masternodes/errors.h b/src/masternodes/errors.h index 4f50f30cf9..c5ca4cf980 100644 --- a/src/masternodes/errors.h +++ b/src/masternodes/errors.h @@ -541,6 +541,14 @@ class DeFiErrors { static Res InvalidBlockNumberString(const std::string &number) { return Res::Err("Invalid block number: %s", number); } + + static Res VmmapScriptPubKeyNotFound() { + return Res::Err("ScriptPubKey not found"); + } + + static Res VmmapScriptPubKeyInvalid() { + return Res::Err("ScriptPubKey is invalid"); + } }; #endif // DEFI_MASTERNODES_ERRORS_H diff --git a/src/masternodes/rpc_evm.cpp b/src/masternodes/rpc_evm.cpp index 2f5eff52aa..9df18938c6 100644 --- a/src/masternodes/rpc_evm.cpp +++ b/src/masternodes/rpc_evm.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include enum class VMDomainRPCMapType { @@ -234,13 +236,13 @@ 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) { uint64_t height; @@ -296,7 +298,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); @@ -336,17 +338,21 @@ UniValue vmmap(const JSONRPCRequest &request) { throwInvalidParam(DeFiErrors::InvalidBlockNumberString(input).msg); } CBlockIndex *pindex = ::ChainActive()[height]; - auto evmBlockHash = - pcustomcsview->GetVMDomainBlockEdge(VMDomainEdge::DVMToEVM, pindex->GetBlockHash().GetHex()); - if (!evmBlockHash.val.has_value()) { - throwInvalidParam(evmBlockHash.msg); + const CBlock block = GetBlockChecked(pindex); + CScript scriptPubKey = block.vtx[0]->vout[1].scriptPubKey; + if (scriptPubKey.size() == 0) { + throwInvalidParam(DeFiErrors::VmmapScriptPubKeyNotFound()); + } + auto xvm = XVM::TryFrom(scriptPubKey); + if (!xvm) { + throwInvalidParam(DeFiErrors::VmmapScriptPubKeyInvalid()); } CrossBoundaryResult result; - uint64_t blockNumber = evm_try_get_block_number_by_hash(result, *evmBlockHash.val); + uint64_t blockNumber = evm_try_get_block_number_by_hash(result, xvm->evm.blockHash); crossBoundaryOkOrThrow(result); return finalizeBlockNumberResult(blockNumber, VMDomainRPCMapType::BlockNumberDVMToEVM, height); }; -*/ + LOCK(cs_main); if (type == VMDomainRPCMapType::Auto) { @@ -374,10 +380,9 @@ 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::BlockNumberDVMToEVM: { + return handleMapBlockNumberDVMToEVMRequest(input); + } // case VMDomainRPCMapType::BlockNumberEVMToDVM: { // return handleMapBlockNumberEVMToDVMRequest(input); // } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9eabe391ca..152e44d827 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1024,7 +1024,7 @@ static UniValue getblockheader(const JSONRPCRequest& request) return blockheaderToJSON(tip, pblockindex); } -static CBlock GetBlockChecked(const CBlockIndex* pblockindex) +CBlock GetBlockChecked(const CBlockIndex* pblockindex) { CBlock block; if (IsBlockPruned(pblockindex)) { diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index 308c6ef720..a8becfff41 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -11,6 +11,8 @@ #include #include +#include + extern RecursiveMutex cs_main; class CBlock; @@ -50,4 +52,6 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex /** Used by getblockstats to get feerates at different percentiles by weight */ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector>& scores, int64_t total_weight); +CBlock GetBlockChecked(const CBlockIndex* pblockindex); + #endif From 63cdba559bd3074657b5897d7832a494174c8014 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Fri, 18 Aug 2023 17:43:09 +0800 Subject: [PATCH 2/3] hashlookup bn e->d --- lib/ain-rs-exports/src/evm.rs | 21 +++++++++++++++++++++ lib/ain-rs-exports/src/lib.rs | 4 ++++ src/masternodes/rpc_evm.cpp | 15 ++++----------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/ain-rs-exports/src/evm.rs b/lib/ain-rs-exports/src/evm.rs index 549e609b4a..69b3ea0153 100644 --- a/lib/ain-rs-exports/src/evm.rs +++ b/lib/ain-rs-exports/src/evm.rs @@ -592,6 +592,27 @@ pub fn evm_try_get_block_number_by_hash(result: &mut ffi::CrossBoundaryResult, h } } +pub fn evm_try_get_dvm_block_number_by_number( + result: &mut ffi::CrossBoundaryResult, + number: u64, +) -> u64 { + match SERVICES.evm.storage.get_block_by_number(&U256::from(number)) { + Ok(Some(block)) => { + let extra_data = block.header.extra_data; + let data = String::from_utf8(extra_data.to_vec()).unwrap(); + let Some(data) = data.split_whitespace().nth(1) else { + return cross_boundary_error_return(result, format!("Expected `DFI: `, actual: {:?}", data)); + }; + let Ok(dvm_block_number) = data.parse::() else { + return cross_boundary_error_return(result, "Parse integer error"); + }; + cross_boundary_success_return(result, dvm_block_number) + } + Ok(None) => cross_boundary_error_return(result, "Invalid block number"), + Err(e) => cross_boundary_error_return(result, e.to_string()), + } +} + pub fn evm_try_get_block_header_by_hash( result: &mut ffi::CrossBoundaryResult, hash: &str, diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index ea71cee313..ebb8298832 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -175,6 +175,10 @@ pub mod ffi { result: &mut CrossBoundaryResult, hash: &str, ) -> EVMBlockHeader; + fn evm_try_get_dvm_block_number_by_number( + result: &mut CrossBoundaryResult, + height: u64, + ) -> u64; fn evm_try_get_block_count(result: &mut CrossBoundaryResult) -> u64; fn evm_try_get_tx_by_hash( result: &mut CrossBoundaryResult, diff --git a/src/masternodes/rpc_evm.cpp b/src/masternodes/rpc_evm.cpp index 9df18938c6..adfac49319 100644 --- a/src/masternodes/rpc_evm.cpp +++ b/src/masternodes/rpc_evm.cpp @@ -316,15 +316,8 @@ UniValue vmmap(const JSONRPCRequest &request) { throwInvalidParam(DeFiErrors::InvalidBlockNumberString(input).msg.c_str()); } CrossBoundaryResult result; - auto evmHash = evm_try_get_block_hash_by_number(result, height); - auto evmBlockHash = std::string(evmHash.data(), evmHash.length()); + uint64_t blockNumber = evm_try_get_dvm_block_number_by_number(result, height); crossBoundaryOkOrThrow(result); - ResVal dvm_block = pcustomcsview->GetVMDomainBlockEdge(VMDomainEdge::EVMToDVM, evmBlockHash); - if (!dvm_block) { - throwInvalidParam(dvm_block.msg); - } - CBlockIndex *pindex = LookupBlockIndex(*dvm_block.val); - uint64_t blockNumber = pindex->GetBlockHeader().deprecatedHeight; return finalizeBlockNumberResult(blockNumber, VMDomainRPCMapType::BlockNumberEVMToDVM, height); }; @@ -383,9 +376,9 @@ UniValue vmmap(const JSONRPCRequest &request) { case VMDomainRPCMapType::BlockNumberDVMToEVM: { return handleMapBlockNumberDVMToEVMRequest(input); } - // case VMDomainRPCMapType::BlockNumberEVMToDVM: { - // return handleMapBlockNumberEVMToDVMRequest(input); - // } + case VMDomainRPCMapType::BlockNumberEVMToDVM: { + return handleMapBlockNumberEVMToDVMRequest(input); + } default: { throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown map type"); } From 321a5685dcf654e25a2b3aab401839e74fd6034e Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 21 Aug 2023 11:40:14 +0800 Subject: [PATCH 3/3] fix defierror --- src/masternodes/rpc_evm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masternodes/rpc_evm.cpp b/src/masternodes/rpc_evm.cpp index adfac49319..21310a61dc 100644 --- a/src/masternodes/rpc_evm.cpp +++ b/src/masternodes/rpc_evm.cpp @@ -334,11 +334,11 @@ UniValue vmmap(const JSONRPCRequest &request) { const CBlock block = GetBlockChecked(pindex); CScript scriptPubKey = block.vtx[0]->vout[1].scriptPubKey; if (scriptPubKey.size() == 0) { - throwInvalidParam(DeFiErrors::VmmapScriptPubKeyNotFound()); + throwInvalidParam(DeFiErrors::VmmapScriptPubKeyNotFound().msg.c_str()); } auto xvm = XVM::TryFrom(scriptPubKey); if (!xvm) { - throwInvalidParam(DeFiErrors::VmmapScriptPubKeyInvalid()); + throwInvalidParam(DeFiErrors::VmmapScriptPubKeyInvalid().msg.c_str()); } CrossBoundaryResult result; uint64_t blockNumber = evm_try_get_block_number_by_hash(result, xvm->evm.blockHash);