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

evm: eth_getBlockTransactionCountBy RPC implementations #1905

Merged
merged 7 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 43 additions & 1 deletion lib/ain-grpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::codegen::rpc::{
ffi::{
EthAccountsResult, EthBlockInfo, EthBlockNumberResult, EthCallInput, EthCallResult,
EthChainIdResult, EthGetBalanceInput, EthGetBalanceResult, EthGetBlockByHashInput,
EthGetBlockByNumberInput, EthMiningResult, EthTransactionInfo,
EthGetBlockByNumberInput, EthGetBlockTransactionCountByHashInput,
EthGetBlockTransactionCountByHashResult, EthGetBlockTransactionCountByNumberInput,
EthGetBlockTransactionCountByNumberResult, EthMiningResult, EthTransactionInfo,
},
EthService,
};
Expand Down Expand Up @@ -45,6 +47,16 @@ pub trait EthServiceApi {
) -> Result<EthBlockInfo, jsonrpsee_core::Error>;

fn Eth_Mining(handler: Arc<Handlers>) -> Result<EthMiningResult, jsonrpsee_core::Error>;

fn Eth_GetBlockTransactionCountByHash(
handler: Arc<Handlers>,
input: EthGetBlockTransactionCountByHashInput,
) -> Result<EthGetBlockTransactionCountByHashResult, jsonrpsee_core::Error>;

fn Eth_GetBlockTransactionCountByNumber(
handler: Arc<Handlers>,
input: EthGetBlockTransactionCountByNumberInput,
) -> Result<EthGetBlockTransactionCountByNumberResult, jsonrpsee_core::Error>;
}

impl EthServiceApi for EthService {
Expand Down Expand Up @@ -211,6 +223,36 @@ impl EthServiceApi for EthService {

Ok(EthMiningResult { is_mining: mining })
}

fn Eth_GetBlockTransactionCountByHash(
handler: Arc<Handlers>,
input: EthGetBlockTransactionCountByHashInput,
) -> Result<EthGetBlockTransactionCountByHashResult, jsonrpsee_core::Error> {
let EthGetBlockTransactionCountByHashInput { block_hash } = input;

let block_hash = block_hash.parse().expect("Invalid hash");
let block = handler.block.get_block_by_hash(block_hash).unwrap();
let count = block.transactions.len();

Ok(EthGetBlockTransactionCountByHashResult {
number_transaction: format!("{:#x}", count),
})
}

fn Eth_GetBlockTransactionCountByNumber(
handler: Arc<Handlers>,
input: EthGetBlockTransactionCountByNumberInput,
) -> Result<EthGetBlockTransactionCountByNumberResult, jsonrpsee_core::Error> {
let EthGetBlockTransactionCountByNumberInput { block_number } = input;

let number: usize = block_number.parse().ok().unwrap();
let block = handler.block.get_block_by_number(number).unwrap();
let count = block.transactions.len();

Ok(EthGetBlockTransactionCountByNumberResult {
number_transaction: format!("{:#x}", count),
})
}
}

fn format_hash(hash: H256) -> String {
Expand Down
5 changes: 5 additions & 0 deletions lib/proto/rpc/eth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ service eth {
rpc Eth_GetBlockByNumber(types.EthGetBlockByNumberInput) returns (types.EthBlockInfo);

rpc Eth_Mining(google.protobuf.Empty) returns (types.EthMiningResult);

rpc Eth_GetBlockTransactionCountByHash(types.EthGetBlockTransactionCountByHashInput) returns (types.EthGetBlockTransactionCountByHashResult);

rpc Eth_GetBlockTransactionCountByNumber(types.EthGetBlockTransactionCountByNumberInput) returns (types.EthGetBlockTransactionCountByNumberResult);

}