From 92629c3ef4ba60ea72ee491f7d0dc0d64eb284e1 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 12 Apr 2023 13:36:27 +0800 Subject: [PATCH] evm: eth_mining RPC implementation (#1892) * eth_mining RPC implementation * Formatting --- src/masternodes/evm_ffi.cpp | 5 +++++ src/masternodes/evm_ffi.h | 1 + src/rust/crates/ain-evm-cpp-ffi/src/lib.rs | 9 ++++++++- src/rust/crates/ain-grpc/src/rpc.rs | 10 +++++++++- src/rust/protobuf/rpc/eth.proto | 2 ++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/masternodes/evm_ffi.cpp b/src/masternodes/evm_ffi.cpp index 33248049352..27c57d417a3 100644 --- a/src/masternodes/evm_ffi.cpp +++ b/src/masternodes/evm_ffi.cpp @@ -1,5 +1,10 @@ #include +#include uint64_t getChainId() { return Params().GetConsensus().evmChainId; } + +bool isMining() { + return gArgs.GetBoolArg("-gen", false); +} \ No newline at end of file diff --git a/src/masternodes/evm_ffi.h b/src/masternodes/evm_ffi.h index f96384e5990..358e5740061 100644 --- a/src/masternodes/evm_ffi.h +++ b/src/masternodes/evm_ffi.h @@ -4,5 +4,6 @@ #include uint64_t getChainId(); +bool isMining(); #endif // DEFI_EVM_FFI_H diff --git a/src/rust/crates/ain-evm-cpp-ffi/src/lib.rs b/src/rust/crates/ain-evm-cpp-ffi/src/lib.rs index fca49937016..9793298e5fe 100644 --- a/src/rust/crates/ain-evm-cpp-ffi/src/lib.rs +++ b/src/rust/crates/ain-evm-cpp-ffi/src/lib.rs @@ -6,11 +6,18 @@ mod ffi { include!("masternodes/evm_ffi.h"); fn getChainId() -> u64; + fn isMining() -> bool; } } pub fn get_chain_id() -> Result> { let chain_id = ffi::getChainId(); - return Ok(chain_id); + Ok(chain_id) +} + +pub fn is_mining() -> Result> { + let is_mining = ffi::isMining(); + + Ok(is_mining) } diff --git a/src/rust/crates/ain-grpc/src/rpc.rs b/src/rust/crates/ain-grpc/src/rpc.rs index 6ef55911cce..a731c883469 100644 --- a/src/rust/crates/ain-grpc/src/rpc.rs +++ b/src/rust/crates/ain-grpc/src/rpc.rs @@ -2,7 +2,7 @@ use crate::codegen::rpc::{ ffi::{ EthAccountsResult, EthBlockInfo, EthBlockNumberResult, EthCallInput, EthCallResult, EthGetBalanceInput, EthGetBalanceResult, EthGetBlockByHashInput, EthGetBlockByNumberInput, - EthTransactionInfo, + EthMiningResult, EthTransactionInfo, }, EthService, }; @@ -41,6 +41,8 @@ pub trait EthServiceApi { handler: Arc, input: EthGetBlockByNumberInput, ) -> Result; + + fn Eth_Mining(handler: Arc) -> Result; } impl EthServiceApi for EthService { @@ -185,6 +187,12 @@ impl EthServiceApi for EthService { .collect::>(), }) } + + fn Eth_Mining(handler: Arc) -> Result { + let mining = ain_evm_cpp_ffi::is_mining().unwrap(); + + Ok(EthMiningResult { is_mining: mining }) + } } fn format_hash(hash: H256) -> String { diff --git a/src/rust/protobuf/rpc/eth.proto b/src/rust/protobuf/rpc/eth.proto index 25ac6b37033..ee065b26ee6 100644 --- a/src/rust/protobuf/rpc/eth.proto +++ b/src/rust/protobuf/rpc/eth.proto @@ -23,4 +23,6 @@ service eth { rpc Eth_BlockNumber(google.protobuf.Empty) returns (types.EthBlockNumberResult); rpc Eth_GetBlockByNumber(types.EthGetBlockByNumberInput) returns (types.EthBlockInfo); + + rpc Eth_Mining(google.protobuf.Empty) returns (types.EthMiningResult); }