Skip to content

Commit baa36f1

Browse files
authored
fix genesis eth block builder number again (#10297)
Redoes #10225 for the genesis block number. It was broken again by #10271
1 parent cbc649e commit baa36f1

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prdoc/pr_10297.prdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: "pallet_revive: use real storage for block number when building the eth genesis block"
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
When building the eth genesis block, query the real storage item that stores the block number instead of using zero.
6+
If the chainspec does not customise it, it will remain zeroed. Was previously fixed in https://github.com/paritytech/polkadot-sdk/pull/10225
7+
and regression introduced in https://github.com/paritytech/polkadot-sdk/pull/10271. Adds a unit test and comment to prevent further regressions.
8+
crates:
9+
- name: pallet-revive
10+
bump: patch

substrate/frame/revive/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pallet-timestamp = { workspace = true, default-features = true }
8282
pallet-utility = { workspace = true, default-features = true }
8383
proptest = { workspace = true }
8484
sp-keystore = { workspace = true, default-features = true }
85+
sp-state-machine = { workspace = true }
8586
sp-tracing = { workspace = true, default-features = true }
8687

8788
[features]
@@ -127,6 +128,7 @@ std = [
127128
"sp-io/std",
128129
"sp-keystore/std",
129130
"sp-runtime/std",
131+
"sp-state-machine/std",
130132
"sp-version/std",
131133
"subxt-signer",
132134
]

substrate/frame/revive/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,11 @@ pub mod pallet {
829829
}
830830

831831
// Build genesis block
832-
block_storage::on_finalize_build_eth_block::<T>(0u32.into());
832+
block_storage::on_finalize_build_eth_block::<T>(
833+
// Make sure to use the block number from storage instead of the hardcoded 0.
834+
// This enables testing tools like anvil to customise the genesis block number.
835+
frame_system::Pallet::<T>::block_number(),
836+
);
833837

834838
// Set debug settings.
835839
if let Some(settings) = self.debug_settings.as_ref() {

substrate/frame/revive/src/tests.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
5050
use sp_runtime::{
5151
generic::Header,
5252
traits::{BlakeTwo256, Convert, IdentityLookup, One},
53-
AccountId32, BuildStorage, MultiAddress, MultiSignature, Perbill,
53+
AccountId32, BuildStorage, MultiAddress, MultiSignature, Perbill, Storage,
5454
};
5555

5656
pub type Address = MultiAddress<AccountId32, u32>;
@@ -439,6 +439,7 @@ pub struct ExtBuilder {
439439
storage_version: Option<StorageVersion>,
440440
code_hashes: Vec<sp_core::H256>,
441441
genesis_config: Option<crate::GenesisConfig<Test>>,
442+
genesis_state_overrides: Option<Storage>,
442443
}
443444

444445
impl Default for ExtBuilder {
@@ -448,6 +449,7 @@ impl Default for ExtBuilder {
448449
storage_version: None,
449450
code_hashes: vec![],
450451
genesis_config: Some(crate::GenesisConfig::<Test>::default()),
452+
genesis_state_overrides: None,
451453
}
452454
}
453455
}
@@ -469,10 +471,19 @@ impl ExtBuilder {
469471
pub fn set_associated_consts(&self) {
470472
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
471473
}
474+
pub fn with_genesis_state_overrides(mut self, storage: Storage) -> Self {
475+
self.genesis_state_overrides = Some(storage);
476+
self
477+
}
472478
pub fn build(self) -> sp_io::TestExternalities {
473479
sp_tracing::try_init_simple();
474480
self.set_associated_consts();
475-
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
481+
let mut t = self.genesis_state_overrides.unwrap_or_default();
482+
483+
frame_system::GenesisConfig::<Test>::default()
484+
.assimilate_storage(&mut t)
485+
.unwrap();
486+
476487
let checking_account = Pallet::<Test>::checking_account();
477488

478489
pallet_balances::GenesisConfig::<Test> {

substrate/frame/revive/src/tests/block_hash.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use crate::{
2121
evm::{block_hash::EthereumBlockBuilder, fees::InfoT, Block, TransactionSigned},
2222
test_utils::{builder::Contract, deposit_limit, ALICE},
23-
tests::{assert_ok, builder, Contracts, ExtBuilder, RuntimeOrigin, Test},
23+
tests::{assert_ok, builder, Contracts, ExtBuilder, RuntimeOrigin, System, Test, Timestamp},
2424
BalanceWithDust, Code, Config, EthBlock, EthBlockBuilderFirstValues, EthBlockBuilderIR,
2525
EthereumBlock, Pallet, ReceiptGasInfo, ReceiptInfoData,
2626
};
@@ -31,6 +31,7 @@ use frame_support::traits::{
3131
Hooks,
3232
};
3333
use pallet_revive_fixtures::compile_module;
34+
use sp_state_machine::BasicExternalities;
3435

3536
#[test]
3637
fn on_initialize_clears_storage() {
@@ -52,6 +53,26 @@ fn on_initialize_clears_storage() {
5253
});
5354
}
5455

56+
#[test]
57+
fn genesis_block_number_and_timestamp_fetched_from_storage() {
58+
let mut ext = BasicExternalities::new_empty();
59+
ext.execute_with(|| {
60+
System::set_block_number(10);
61+
Timestamp::set_timestamp(10000000);
62+
});
63+
let storage = ext.into_storages();
64+
65+
ExtBuilder::default()
66+
.with_genesis_state_overrides(storage)
67+
.build()
68+
.execute_with(|| {
69+
let block = EthereumBlock::<Test>::get();
70+
// The timestamp is divided by 1000 (converted to seconds)
71+
assert_eq!(block.timestamp, 10000.into());
72+
assert_eq!(block.number, 10.into());
73+
});
74+
}
75+
5576
#[test]
5677
fn transactions_are_captured() {
5778
let (binary, _) = compile_module("dummy").unwrap();

0 commit comments

Comments
 (0)