From f3be0218a82a175fb63c2e6fb585b66fdae8b49e Mon Sep 17 00:00:00 2001 From: Bushstar Date: Mon, 24 Apr 2023 08:33:22 +0100 Subject: [PATCH] FFI: Get block difficulty and chain work of block --- lib/ain-cpp-imports/src/bridge.rs | 2 ++ lib/ain-cpp-imports/src/lib.rs | 16 ++++++++++++ lib/ain-grpc/src/rpc.rs | 23 +++++++++++++++-- src/ffi/ffiexports.cpp | 41 +++++++++++++++++++++++++++++++ src/ffi/ffiexports.h | 2 ++ 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/lib/ain-cpp-imports/src/bridge.rs b/lib/ain-cpp-imports/src/bridge.rs index b2a3f40387f..ac4b8dec113 100644 --- a/lib/ain-cpp-imports/src/bridge.rs +++ b/lib/ain-cpp-imports/src/bridge.rs @@ -8,5 +8,7 @@ pub mod ffi { fn publishEthTransaction(data: Vec) -> bool; fn getAccounts() -> Vec; fn getDatadir() -> String; + fn getDifficulty(_block_hash: [u8; 32]) -> u32; + fn getChainWork(_block_hash: [u8; 32]) -> [u8; 32]; } } diff --git a/lib/ain-cpp-imports/src/lib.rs b/lib/ain-cpp-imports/src/lib.rs index 0bc32132332..925cc8d0f06 100644 --- a/lib/ain-cpp-imports/src/lib.rs +++ b/lib/ain-cpp-imports/src/lib.rs @@ -25,6 +25,12 @@ mod ffi { pub fn getDatadir() -> String { unimplemented!("{}", UNIMPL_MSG) } + pub fn getDifficulty(_block_hash: [u8; 32]) -> u32 { + unimplemented!("{}", UNIMPL_MSG) + } + pub fn getChainWork(_block_hash: [u8; 32]) -> [u8; 32] { + unimplemented!("{}", UNIMPL_MSG) + } } pub fn get_chain_id() -> Result> { @@ -52,5 +58,15 @@ pub fn get_datadir() -> Result> { Ok(datadir) } +pub fn get_difficulty(block_hash: [u8; 32]) -> Result> { + let bits = ffi::getDifficulty(block_hash); + Ok(bits) +} + +pub fn get_chainwork(block_hash: [u8; 32]) -> Result<[u8; 32], Box> { + let chainwork = ffi::getChainWork(block_hash); + Ok(chainwork) +} + #[cfg(test)] mod tests {} diff --git a/lib/ain-grpc/src/rpc.rs b/lib/ain-grpc/src/rpc.rs index 071257e4073..3008015c755 100644 --- a/lib/ain-grpc/src/rpc.rs +++ b/lib/ain-grpc/src/rpc.rs @@ -154,11 +154,30 @@ impl MetachainRPCServer for MetachainRPCModule { let hash: H256 = hash.parse().expect("Invalid hash"); - Ok(self + let block: Option = self .handler .storage .get_block_by_hash(&hash) - .map(Into::into)) + .map(Into::into); + + match block { + None => Ok(None), + Some(mut block) => { + let hash_array = hash.to_fixed_bytes(); + let difficulty = ain_cpp_imports::get_difficulty(hash_array); + let chainwork = ain_cpp_imports::get_chainwork(hash_array); + + if let Ok(difficulty) = difficulty { + block.difficulty = U256::from(difficulty); + } + + if let Ok(chainwork) = chainwork { + block.total_difficulty = Some(U256::from(chainwork)); + } + + Ok(Some(block)) + } + } } fn chain_id(&self) -> Result { diff --git a/src/ffi/ffiexports.cpp b/src/ffi/ffiexports.cpp index 39851bbcc26..e5ee01eb2e2 100644 --- a/src/ffi/ffiexports.cpp +++ b/src/ffi/ffiexports.cpp @@ -66,3 +66,44 @@ rust::vec getAccounts() { rust::string getDatadir() { return GetDataDir().c_str(); } + +uint32_t getDifficulty(std::array blockHash) { + uint256 hash{}; + std::copy(blockHash.begin(), blockHash.end(), hash.begin()); + + const CBlockIndex* pblockindex; + uint32_t difficulty{}; + { + LOCK(cs_main); + pblockindex = LookupBlockIndex(hash); + + if (!pblockindex) { + return difficulty; + } + + difficulty = pblockindex->nBits; + } + + return difficulty; +} + +std::array getChainWork(std::array blockHash) { + uint256 hash{}; + std::copy(blockHash.begin(), blockHash.end(), hash.begin()); + + const CBlockIndex* pblockindex; + std::array chainWork{}; + { + LOCK(cs_main); + pblockindex = LookupBlockIndex(hash); + + if (!pblockindex) { + return chainWork; + } + + const auto sourceWork = ArithToUint256(pblockindex->nChainWork); + std::copy(sourceWork.begin(), sourceWork.end(), chainWork.begin()); + } + + return chainWork; +} diff --git a/src/ffi/ffiexports.h b/src/ffi/ffiexports.h index 4e3843a2c1e..6c518593f7c 100644 --- a/src/ffi/ffiexports.h +++ b/src/ffi/ffiexports.h @@ -9,5 +9,7 @@ bool isMining(); bool publishEthTransaction(rust::Vec rawTransaction); rust::vec getAccounts(); rust::string getDatadir(); +uint32_t getDifficulty(std::array blockHash); +std::array getChainWork(std::array blockHash); #endif // DEFI_EVM_FFI_H