Skip to content

Commit 51d14c8

Browse files
authored
Merge pull request #62 from bitfinity-network/feat/pow-hashing-validation
POW hash calculation
2 parents 63a580b + 689b9b0 commit 51d14c8

File tree

8 files changed

+1368
-693
lines changed

8 files changed

+1368
-693
lines changed

Cargo.lock

Lines changed: 587 additions & 651 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,5 @@ ic-certificate-verification = "3"
665665
ic-certification = "3"
666666
hex = "0.4"
667667
lightspeed_scheduler = { version = "0.60.0", features = ["tracing"] }
668+
anyhow = "1"
669+
jsonrpc-core = "18.0.0"

bin/reth/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ lightspeed_scheduler = { workspace = true, features = ["tracing"] }
110110
tempfile.workspace = true
111111

112112
# bitfinity dev dependencies
113+
alloy-genesis.workspace = true
113114
async-trait = { workspace = true }
114115
dirs.workspace = true
115116
ethereum-json-rpc-client = { workspace = true, features = ["reqwest"] }

bin/reth/src/commands/bitfinity_import.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ impl BitfinityImportCommand {
228228
let config = self.rpc_config();
229229
let client = BitfinityEvmClient::client(config).await?;
230230

231-
let confirmator = BitfinityBlockConfirmation::new(client, provider_factory);
231+
let confirmer = BitfinityBlockConfirmation::new(client, provider_factory);
232232
let blocks = remote_client.unsafe_blocks(block)?;
233233

234-
confirmator.confirm_blocks(&blocks).await
234+
confirmer.confirm_blocks(&blocks).await
235235
}
236236

237237
/// Imports the blocks up to the given block hash of the `remove_client`.

bin/reth/tests/commands/bitfinity_import_it.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ use super::{
1313
};
1414
use alloy_eips::BlockNumberOrTag;
1515

16+
use did::H256;
1617
use ethereum_json_rpc_client::{reqwest::ReqwestClient, EthJsonRpcClient};
1718

1819
use reth::commands::bitfinity_import::BitfinityImportCommand;
1920
use reth_provider::{BlockNumReader, BlockReader, BlockReaderIdExt};
21+
use reth_trie::StateRoot;
22+
use reth_trie_db::DatabaseStateRoot;
23+
use revm_primitives::{Address, U256};
2024

25+
use std::sync::Arc;
2126
use std::time::Duration;
2227

2328
#[tokio::test]
@@ -374,7 +379,15 @@ async fn bitfinity_test_should_confirm_and_import_unsafe_blocks() {
374379
const UNSAFE_BLOCKS: u64 = 3;
375380
const MAX_BLOCKS: u64 = 10;
376381

377-
let mut eth_server = EthImpl::new_with_max_block(MAX_BLOCKS);
382+
let genesis_balances =
383+
vec![(Address::from_slice(&[0u8; 20]).into(), U256::from(1_000_000_u64))];
384+
385+
let state_root = compute_state_root(&genesis_balances);
386+
387+
let mut eth_server = EthImpl::new_with_max_block(MAX_BLOCKS)
388+
.with_genesis_balances(genesis_balances)
389+
.with_state_root(state_root.into());
390+
378391
let bf_evm_server = eth_server.bf_impl(UNSAFE_BLOCKS);
379392

380393
let (_server, eth_server_address) = mock_multi_server_start([
@@ -422,8 +435,14 @@ async fn bitfinity_test_should_import_until_last_confirmed() {
422435
const UNSAFE_BLOCKS: u64 = 3;
423436
const MAX_BLOCKS: u64 = 10;
424437
const CONFIRM_UNTIL: u64 = 8;
438+
let genesis_balances =
439+
vec![(Address::from_slice(&[0u8; 20]).into(), U256::from(1_000_000_u64))];
425440

426-
let mut eth_server = EthImpl::new_with_max_block(MAX_BLOCKS);
441+
let state_root = compute_state_root(&genesis_balances);
442+
443+
let mut eth_server = EthImpl::new_with_max_block(MAX_BLOCKS)
444+
.with_genesis_balances(genesis_balances)
445+
.with_state_root(state_root.into());
427446
let mut bf_evm_server = eth_server.bf_impl(UNSAFE_BLOCKS);
428447
bf_evm_server.confirm_until = CONFIRM_UNTIL;
429448

@@ -463,3 +482,39 @@ async fn bitfinity_test_should_import_until_last_confirmed() {
463482
let last_imported_block = provider.last_block_number().unwrap();
464483
assert_eq!(last_imported_block, CONFIRM_UNTIL);
465484
}
485+
486+
/// Compute state root for genesis Balances
487+
fn compute_state_root(genesis_balances: &[(Address, U256)]) -> H256 {
488+
use alloy_genesis::Genesis;
489+
use alloy_genesis::GenesisAccount;
490+
use reth_chainspec::ChainSpec;
491+
use reth_db_common::init::init_genesis;
492+
use reth_provider::test_utils::create_test_provider_factory_with_chain_spec;
493+
494+
let chain_spec = Arc::new(ChainSpec {
495+
chain: 1_u64.into(),
496+
genesis: Genesis {
497+
alloc: genesis_balances
498+
.iter()
499+
.map(|(address, balance)| {
500+
(*address, GenesisAccount { balance: *balance, ..Default::default() })
501+
})
502+
.collect(),
503+
..Default::default()
504+
},
505+
hardforks: Default::default(),
506+
genesis_hash: Default::default(),
507+
paris_block_and_final_difficulty: None,
508+
deposit_contract: None,
509+
..Default::default()
510+
});
511+
512+
let factory = create_test_provider_factory_with_chain_spec(chain_spec);
513+
init_genesis(&factory).unwrap();
514+
515+
let provider = factory.provider().unwrap();
516+
517+
let tx = provider.tx_ref();
518+
519+
StateRoot::from_tx(tx).root().unwrap().into()
520+
}

bin/reth/tests/commands/bitfinity_node_it.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async fn bitfinity_test_node_forward_eth_get_genesis_balances() {
118118
// Arrange
119119
let _log = init_logs();
120120

121-
let eth_server = EthImpl::with_genesis_balances(vec![
121+
let eth_server = EthImpl::new().with_genesis_balances(vec![
122122
(Address::from_slice(&[1u8; 20]), U256::from(10)),
123123
(Address::from_slice(&[2u8; 20]), U256::from(20)),
124124
(Address::from_slice(&[3u8; 20]), U256::from(30)),
@@ -156,7 +156,7 @@ async fn bitfinity_test_node_forward_ic_get_genesis_balances() {
156156
// Arrange
157157
let _log = init_logs();
158158

159-
let eth_server = EthImpl::with_genesis_balances(vec![
159+
let eth_server = EthImpl::new().with_genesis_balances(vec![
160160
(Address::from_slice(&[1u8; 20]), U256::from(10)),
161161
(Address::from_slice(&[2u8; 20]), U256::from(20)),
162162
(Address::from_slice(&[3u8; 20]), U256::from(30)),
@@ -347,7 +347,9 @@ pub async fn mock_eth_server_start(methods: impl Into<Methods>) -> (ServerHandle
347347
}
348348

349349
/// Starts a local mock server that combines methods from different sources.
350-
pub async fn mock_multi_server_start(methods: impl IntoIterator<Item = Methods>) -> (ServerHandle, SocketAddr) {
350+
pub async fn mock_multi_server_start(
351+
methods: impl IntoIterator<Item = Methods>,
352+
) -> (ServerHandle, SocketAddr) {
351353
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
352354
let server = Server::builder().build(addr).await.unwrap();
353355

@@ -373,6 +375,7 @@ pub mod eth_server {
373375
use did::{keccak, BlockConfirmationData, BlockConfirmationResult, BlockNumber, H256, U64};
374376
use ethereum_json_rpc_client::CertifiedResult;
375377
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
378+
376379
use reth_trie::EMPTY_ROOT_HASH;
377380
use revm_primitives::{Address, B256, U256};
378381

@@ -424,7 +427,10 @@ pub mod eth_server {
424427
trait BfEvm {
425428
/// Send block confirmation request.
426429
#[method(name = "sendConfirmBlock")]
427-
async fn confirm_block(&self, data: BlockConfirmationData) -> RpcResult<BlockConfirmationResult>;
430+
async fn confirm_block(
431+
&self,
432+
data: BlockConfirmationData,
433+
) -> RpcResult<BlockConfirmationResult>;
428434
}
429435

430436
/// Eth server implementation for local testing
@@ -447,6 +453,8 @@ pub mod eth_server {
447453
pub genesis_balances: Vec<(Address, U256)>,
448454
/// Unsafe blocks count
449455
pub unsafe_blocks_count: u64,
456+
/// Custom state root hash
457+
pub custom_state_root: H256,
450458
}
451459

452460
impl EthImpl {
@@ -483,6 +491,7 @@ pub mod eth_server {
483491
block_task,
484492
genesis_balances: vec![],
485493
unsafe_blocks_count: 0,
494+
custom_state_root: EMPTY_ROOT_HASH.into(),
486495
}
487496
}
488497

@@ -494,18 +503,21 @@ pub mod eth_server {
494503
}
495504

496505
/// Set the genesis balances
497-
pub fn with_genesis_balances(balances: Vec<(Address, U256)>) -> Self {
498-
let mut instance = Self::new();
499-
instance.genesis_balances = balances;
500-
instance
506+
pub fn with_genesis_balances(mut self, balances: Vec<(Address, U256)>) -> Self {
507+
self.genesis_balances = balances;
508+
self
509+
}
510+
511+
/// Set a custom state root hash
512+
pub fn with_state_root(mut self, state_root: did::H256) -> Self {
513+
self.custom_state_root = state_root;
514+
self
501515
}
502516

503517
/// Returns an implementation of Bitfinity EVM canister API
504518
pub fn bf_impl(&mut self, unsafe_blocks_count: u64) -> BfEvmImpl {
505519
self.unsafe_blocks_count = unsafe_blocks_count;
506-
BfEvmImpl {
507-
confirm_until: u64::MAX,
508-
}
520+
BfEvmImpl { confirm_until: u64::MAX }
509521
}
510522
}
511523

@@ -550,7 +562,7 @@ pub mod eth_server {
550562
timestamp: did::U256::from(1234567890_u64 + block_num),
551563
gas_limit: did::U256::from(30_000_000_u64),
552564
base_fee_per_gas: Some(did::U256::from(7_u64)),
553-
state_root: EMPTY_ROOT_HASH.into(),
565+
state_root: self.custom_state_root.clone(),
554566
receipts_root: EMPTY_RECEIPTS.into(),
555567
transactions_root: EMPTY_TRANSACTIONS.into(),
556568
parent_hash: if block_num == 0 {
@@ -625,7 +637,10 @@ pub mod eth_server {
625637

626638
#[async_trait::async_trait]
627639
impl BfEvmServer for BfEvmImpl {
628-
async fn confirm_block(&self, data: BlockConfirmationData) -> RpcResult<BlockConfirmationResult> {
640+
async fn confirm_block(
641+
&self,
642+
data: BlockConfirmationData,
643+
) -> RpcResult<BlockConfirmationResult> {
629644
if data.block_number > self.confirm_until {
630645
Ok(BlockConfirmationResult::NotConfirmed)
631646
} else {

crates/bitfinity-block-confirmation/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ reth-engine-tree.workspace = true
2626
reth-evm.workspace = true
2727
reth-evm-ethereum.workspace = true
2828
reth-node-types.workspace = true
29-
reth-provider.workspace = true
29+
reth-provider = { workspace = true, features = ["test-utils"] }
3030
reth-primitives.workspace = true
3131
reth-primitives-traits.workspace = true
3232
reth-revm.workspace = true
3333
reth-trie.workspace = true
3434
reth-trie-db.workspace = true
3535
tracing.workspace = true
36+
reth-rpc-eth-types.workspace = true
3637

3738
[dev-dependencies]
3839
alloy-consensus.workspace = true
@@ -45,3 +46,9 @@ reth-db-common.workspace = true
4546
tokio.workspace = true
4647
serde.workspace = true
4748
tempfile.workspace = true
49+
reth-testing-utils.workspace = true
50+
reth-ethereum-engine-primitives.workspace = true
51+
anyhow.workspace = true
52+
jsonrpc-core.workspace = true
53+
serde_json.workspace = true
54+
alloy-network.workspace = true

0 commit comments

Comments
 (0)