Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions prdoc/pr_10297.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: "pallet_revive: use real storage for block number when building the eth genesis block"
doc:
- audience: Runtime Dev
description: |-
When building the eth genesis block, query the real storage item that stores the block number instead of using zero.
If the chainspec does not customise it, it will remain zeroed. Was previously fixed in https://github.com/paritytech/polkadot-sdk/pull/10225
and regression introduced in https://github.com/paritytech/polkadot-sdk/pull/10271. Adds a unit test and comment to prevent further regressions.
crates:
- name: pallet-revive
bump: patch
2 changes: 2 additions & 0 deletions substrate/frame/revive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pallet-timestamp = { workspace = true, default-features = true }
pallet-utility = { workspace = true, default-features = true }
proptest = { workspace = true }
sp-keystore = { workspace = true, default-features = true }
sp-state-machine = { workspace = true }
sp-tracing = { workspace = true, default-features = true }

[features]
Expand Down Expand Up @@ -127,6 +128,7 @@ std = [
"sp-io/std",
"sp-keystore/std",
"sp-runtime/std",
"sp-state-machine/std",
"sp-version/std",
"subxt-signer",
]
Expand Down
6 changes: 5 additions & 1 deletion substrate/frame/revive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,11 @@ pub mod pallet {
}

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

// Set debug settings.
if let Some(settings) = self.debug_settings.as_ref() {
Expand Down
15 changes: 13 additions & 2 deletions substrate/frame/revive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
use sp_runtime::{
generic::Header,
traits::{BlakeTwo256, Convert, IdentityLookup, One},
AccountId32, BuildStorage, MultiAddress, MultiSignature, Perbill,
AccountId32, BuildStorage, MultiAddress, MultiSignature, Perbill, Storage,
};

pub type Address = MultiAddress<AccountId32, u32>;
Expand Down Expand Up @@ -439,6 +439,7 @@ pub struct ExtBuilder {
storage_version: Option<StorageVersion>,
code_hashes: Vec<sp_core::H256>,
genesis_config: Option<crate::GenesisConfig<Test>>,
genesis_state_overrides: Option<Storage>,
}

impl Default for ExtBuilder {
Expand All @@ -448,6 +449,7 @@ impl Default for ExtBuilder {
storage_version: None,
code_hashes: vec![],
genesis_config: Some(crate::GenesisConfig::<Test>::default()),
genesis_state_overrides: None,
}
}
}
Expand All @@ -469,10 +471,19 @@ impl ExtBuilder {
pub fn set_associated_consts(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
}
pub fn with_genesis_state_overrides(mut self, storage: Storage) -> Self {
self.genesis_state_overrides = Some(storage);
self
}
pub fn build(self) -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
self.set_associated_consts();
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut t = self.genesis_state_overrides.unwrap_or_default();

frame_system::GenesisConfig::<Test>::default()
.assimilate_storage(&mut t)
.unwrap();

let checking_account = Pallet::<Test>::checking_account();

pallet_balances::GenesisConfig::<Test> {
Expand Down
23 changes: 22 additions & 1 deletion substrate/frame/revive/src/tests/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::{
evm::{block_hash::EthereumBlockBuilder, fees::InfoT, Block, TransactionSigned},
test_utils::{builder::Contract, deposit_limit, ALICE},
tests::{assert_ok, builder, Contracts, ExtBuilder, RuntimeOrigin, Test},
tests::{assert_ok, builder, Contracts, ExtBuilder, RuntimeOrigin, System, Test, Timestamp},
BalanceWithDust, Code, Config, EthBlock, EthBlockBuilderFirstValues, EthBlockBuilderIR,
EthereumBlock, Pallet, ReceiptGasInfo, ReceiptInfoData,
};
Expand All @@ -31,6 +31,7 @@ use frame_support::traits::{
Hooks,
};
use pallet_revive_fixtures::compile_module;
use sp_state_machine::BasicExternalities;

#[test]
fn on_initialize_clears_storage() {
Expand All @@ -52,6 +53,26 @@ fn on_initialize_clears_storage() {
});
}

#[test]
fn genesis_block_number_and_timestamp_fetched_from_storage() {
let mut ext = BasicExternalities::new_empty();
ext.execute_with(|| {
System::set_block_number(10);
Timestamp::set_timestamp(10000000);
});
let storage = ext.into_storages();

ExtBuilder::default()
.with_genesis_state_overrides(storage)
.build()
.execute_with(|| {
let block = EthereumBlock::<Test>::get();
// The timestamp is divided by 1000 (converted to seconds)
assert_eq!(block.timestamp, 10000.into());
assert_eq!(block.number, 10.into());
});
}

#[test]
fn transactions_are_captured() {
let (binary, _) = compile_module("dummy").unwrap();
Expand Down