Skip to content

Commit

Permalink
Merge pull request #39 from bitfinity-network/EPROD-1042_getbalances
Browse files Browse the repository at this point in the history
[EPROD-1042] Added ic_getGenesisBalances forward call
  • Loading branch information
ufoscout authored Oct 18, 2024
2 parents 048f076 + 94d2efe commit 06cb28e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bin/reth/src/commands/bitfinity_reset_evm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ mod test {
}
}

/// Merge accouts into a single account
/// Merge accounts into a single account
fn merge_accounts(accounts: Vec<RawAccountInfo>) -> RawAccountInfo {
let mut result = accounts[0].clone();
for account in accounts {
Expand Down
72 changes: 71 additions & 1 deletion bin/reth/tests/commands/bitfinity_node_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_db::{init_db, test_utils::tempdir_path};
use reth_node_builder::{NodeBuilder, NodeConfig, NodeHandle};
use reth_node_ethereum::EthereumNode;
use reth_tasks::TaskManager;
use revm_primitives::{Address, U256};
use std::{net::SocketAddr, str::FromStr, sync::Arc};


Expand Down Expand Up @@ -68,6 +69,64 @@ async fn bitfinity_test_node_forward_max_priority_fee_per_gas_requests() {
assert_eq!(result.unwrap().as_u128(), max_priority_fee_per_gas);
}

#[tokio::test]
async fn bitfinity_test_node_forward_eth_get_genesis_balances() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new();
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
start_reth_node(Some(format!("http://{}", eth_server_address)), None).await;

// Act
let result: Vec<(ethereum_json_rpc_client::H160, ethereum_json_rpc_client::U256)> =
reth_client.single_request("eth_getGenesisBalances".to_owned(),
ethereum_json_rpc_client::Params::None, ethereum_json_rpc_client::Id::Num(1)).await.unwrap();

// Assert
assert_eq!(result.len(), 3);

assert_eq!(did::H160::from(result[0].0), Address::from_slice(&[1u8; 20]).into());
assert_eq!(did::U256::from(result[0].1), U256::from(10).into());

assert_eq!(did::H160::from(result[1].0), Address::from_slice(&[2u8; 20]).into());
assert_eq!(did::U256::from(result[1].1), U256::from(20).into());

assert_eq!(did::H160::from(result[2].0), Address::from_slice(&[3u8; 20]).into());
assert_eq!(did::U256::from(result[2].1), U256::from(30).into());

}

#[tokio::test]
async fn bitfinity_test_node_forward_ic_get_genesis_balances() {
// Arrange
let _log = init_logs();

let eth_server = EthImpl::new();
let (_server, eth_server_address) =
mock_eth_server_start(EthServer::into_rpc(eth_server)).await;
let (reth_client, _reth_node) =
start_reth_node(Some(format!("http://{}", eth_server_address)), None).await;

// Act
let result = reth_client.get_genesis_balances().await.unwrap();

// Assert
assert_eq!(result.len(), 3);

assert_eq!(did::H160::from(result[0].0), Address::from_slice(&[1u8; 20]).into());
assert_eq!(did::U256::from(result[0].1), U256::from(10).into());

assert_eq!(did::H160::from(result[1].0), Address::from_slice(&[2u8; 20]).into());
assert_eq!(did::U256::from(result[1].1), U256::from(20).into());

assert_eq!(did::H160::from(result[2].0), Address::from_slice(&[3u8; 20]).into());
assert_eq!(did::U256::from(result[2].1), U256::from(30).into());

}

#[tokio::test]
async fn bitfinity_test_node_forward_send_raw_transaction_requests() {
// Arrange
Expand Down Expand Up @@ -164,7 +223,7 @@ pub mod eth_server {
use alloy_rlp::Bytes;
use did::keccak;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use revm_primitives::{B256, U256};
use revm_primitives::{Address, B256, U256};

#[rpc(server, namespace = "eth")]
pub trait Eth {
Expand All @@ -176,6 +235,9 @@ pub mod eth_server {

#[method(name = "sendRawTransaction")]
async fn send_raw_transaction(&self, tx: Bytes) -> RpcResult<B256>;

#[method(name = "getGenesisBalances", aliases = ["ic_getGenesisBalances"])]
async fn get_genesis_balances(&self) -> RpcResult<Vec<(Address, U256)>>;
}

#[derive(Debug)]
Expand Down Expand Up @@ -210,5 +272,13 @@ pub mod eth_server {
let hash = keccak::keccak_hash(&tx);
Ok(hash.into())
}

async fn get_genesis_balances(&self) -> RpcResult<Vec<(Address, U256)>> {
Ok(vec![
(Address::from_slice(&[1u8; 20]), U256::from(10)),
(Address::from_slice(&[2u8; 20]), U256::from(20)),
(Address::from_slice(&[3u8; 20]), U256::from(30)),
])
}
}
}
12 changes: 12 additions & 0 deletions crates/rpc/rpc-eth-api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ pub trait EthApi {
keys: Vec<JsonStorageKey>,
block_number: Option<BlockId>,
) -> RpcResult<EIP1186AccountProofResponse>;

/// Aliases don't use the namespace.
/// Thus, this will generate `eth_getGenesisBalances` and `ic_getGenesisBalances`.
#[method(name = "getGenesisBalances", aliases = ["ic_getGenesisBalances"])]
async fn get_genesis_balances(&self) -> RpcResult<Vec<(Address, U256)>>;

}

#[async_trait::async_trait]
Expand Down Expand Up @@ -728,4 +734,10 @@ where
_ => e.into(),
})?)
}

/// Handler for `eth_getGenesisBalances` and `ic_getGenesisBalances`
async fn get_genesis_balances(&self) -> RpcResult<Vec<(Address, U256)>> {
trace!(target: "rpc::eth", "Serving eth_getGenesisBalances/ic_getGenesisBalances");
BitfinityEvmRpc::get_genesis_balances(self).await
}
}
22 changes: 21 additions & 1 deletion crates/rpc/rpc-eth-api/src/helpers/bitfinity_evm_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::Future;
use jsonrpsee::core::RpcResult;
use reth_chainspec::ChainSpec;
use reth_rpc_server_types::result::internal_rpc_err;
use revm_primitives::{Bytes, B256, U256};
use revm_primitives::{Address, Bytes, B256, U256};

/// Proxy to the Bitfinity EVM RPC.
pub trait BitfinityEvmRpc {
Expand Down Expand Up @@ -64,6 +64,26 @@ pub trait BitfinityEvmRpc {
Ok(tx_hash.0.into())
}
}

/// Forwards `ic_getGenesisBalances` calls to the Bitfinity EVM
fn get_genesis_balances(&self) -> impl Future<Output = RpcResult<Vec<(Address, U256)>>> + Send {
let chain_spec = self.chain_spec();
async move {
let (rpc_url, client) = get_client(&chain_spec)?;

let balances = client.get_genesis_balances().await.map_err(|e| {
internal_rpc_err(format!(
"failed to forward ic_getGenesisBalances request to {}: {}",
rpc_url, e
))
})?;

Ok(balances
.into_iter()
.map(|(address, balance)| (address.0.into(), U256::from(balance.as_u128())))
.collect())
}
}
}

/// Returns a client for the Bitfinity EVM RPC.
Expand Down

0 comments on commit 06cb28e

Please sign in to comment.