Skip to content

Commit

Permalink
FFI: Get block difficulty and chain work of block
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Apr 24, 2023
1 parent a3e9cde commit f3be021
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ pub mod ffi {
fn publishEthTransaction(data: Vec<u8>) -> bool;
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
fn getDifficulty(_block_hash: [u8; 32]) -> u32;
fn getChainWork(_block_hash: [u8; 32]) -> [u8; 32];
}
}
16 changes: 16 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, Box<dyn Error>> {
Expand Down Expand Up @@ -52,5 +58,15 @@ pub fn get_datadir() -> Result<String, Box<dyn Error>> {
Ok(datadir)
}

pub fn get_difficulty(block_hash: [u8; 32]) -> Result<u32, Box<dyn Error>> {
let bits = ffi::getDifficulty(block_hash);
Ok(bits)
}

pub fn get_chainwork(block_hash: [u8; 32]) -> Result<[u8; 32], Box<dyn Error>> {
let chainwork = ffi::getChainWork(block_hash);
Ok(chainwork)
}

#[cfg(test)]
mod tests {}
23 changes: 21 additions & 2 deletions lib/ain-grpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,30 @@ impl MetachainRPCServer for MetachainRPCModule {

let hash: H256 = hash.parse().expect("Invalid hash");

Ok(self
let block: Option<RpcBlock> = 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<String> {
Expand Down
41 changes: 41 additions & 0 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,44 @@ rust::vec<rust::string> getAccounts() {
rust::string getDatadir() {
return GetDataDir().c_str();
}

uint32_t getDifficulty(std::array<uint8_t, 32> 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<uint8_t, 32> getChainWork(std::array<uint8_t, 32> blockHash) {
uint256 hash{};
std::copy(blockHash.begin(), blockHash.end(), hash.begin());

const CBlockIndex* pblockindex;
std::array<uint8_t, 32> 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;
}
2 changes: 2 additions & 0 deletions src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ bool isMining();
bool publishEthTransaction(rust::Vec<uint8_t> rawTransaction);
rust::vec<rust::string> getAccounts();
rust::string getDatadir();
uint32_t getDifficulty(std::array<uint8_t, 32> blockHash);
std::array<uint8_t, 32> getChainWork(std::array<uint8_t, 32> blockHash);

#endif // DEFI_EVM_FFI_H

0 comments on commit f3be021

Please sign in to comment.