feat: add base fee and blob fields to genesis (#4516)

This commit is contained in:
Dan Cline
2023-09-07 15:03:37 -04:00
committed by GitHub
parent 8d29a49dd8
commit f31706dd9b
2 changed files with 60 additions and 5 deletions

View File

@@ -306,11 +306,13 @@ impl ChainSpec {
// If Cancun is activated at genesis, we set:
// * parent beacon block root to 0x0
// * blob gas used to 0x0
// * excess blob gas to 0x0
// * blob gas used to provided genesis or 0x0
// * excess blob gas to provided genesis or 0x0
let (parent_beacon_block_root, blob_gas_used, excess_blob_gas) =
if self.fork(Hardfork::Cancun).active_at_timestamp(self.genesis.timestamp) {
(Some(H256::zero()), Some(0), Some(0))
let blob_gas_used = self.genesis.blob_gas_used.unwrap_or(0);
let excess_blob_gas = self.genesis.excess_blob_gas.unwrap_or(0);
(Some(H256::zero()), Some(blob_gas_used), Some(excess_blob_gas))
} else {
(None, None, None)
};
@@ -346,8 +348,11 @@ impl ChainSpec {
/// Get the initial base fee of the genesis block.
pub fn initial_base_fee(&self) -> Option<u64> {
// If the base fee is set in the genesis block, we use that instead of the default.
let genesis_base_fee = self.genesis.base_fee_per_gas.unwrap_or(EIP1559_INITIAL_BASE_FEE);
// If London is activated at genesis, we set the initial base fee as per EIP-1559.
(self.fork(Hardfork::London).active_at_block(0)).then_some(EIP1559_INITIAL_BASE_FEE)
(self.fork(Hardfork::London).active_at_block(0)).then_some(genesis_base_fee)
}
/// Get the hash of the genesis block.
@@ -1833,13 +1838,14 @@ Post-merge hard forks (timestamp based):
#[test]
fn test_parse_genesis_json() {
let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x1337"}"#;
let genesis: Genesis = serde_json::from_str(s).unwrap();
let acc = genesis
.alloc
.get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
.unwrap();
assert_eq!(acc.balance, U256::from(1));
assert_eq!(genesis.base_fee_per_gas, Some(0x1337));
}
#[test]

View File

@@ -38,6 +38,30 @@ pub struct Genesis {
pub coinbase: Address,
/// The initial state of accounts in the genesis block.
pub alloc: HashMap<Address, GenesisAccount>,
// NOTE: the following fields:
// * base_fee_per_gas
// * excess_blob_gas
// * blob_gas_used
// should NOT be set in a real genesis file, but are included here for compatibility with
// consensus tests, which have genesis files with these fields populated.
/// The genesis header base fee
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_stringified_u64_opt"
)]
pub base_fee_per_gas: Option<u64>,
/// The genesis header excess blob gas
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_stringified_u64_opt"
)]
pub excess_blob_gas: Option<u64>,
/// The genesis header blob gas used
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_stringified_u64_opt"
)]
pub blob_gas_used: Option<u64>,
}
impl Genesis {
@@ -83,6 +107,24 @@ impl Genesis {
self
}
/// Set the base fee.
pub fn with_base_fee(mut self, base_fee: Option<u64>) -> Self {
self.base_fee_per_gas = base_fee;
self
}
/// Set the excess blob gas.
pub fn with_excess_blob_gas(mut self, excess_blob_gas: Option<u64>) -> Self {
self.excess_blob_gas = excess_blob_gas;
self
}
/// Set the blob gas used.
pub fn with_blob_gas_used(mut self, blob_gas_used: Option<u64>) -> Self {
self.blob_gas_used = blob_gas_used;
self
}
/// Add accounts to the genesis block. If the address is already present,
/// the account is updated.
pub fn extend_accounts(
@@ -413,6 +455,10 @@ mod ethers_compat {
mix_hash: genesis.mix_hash.0.into(),
coinbase: genesis.coinbase.0.into(),
extra_data: genesis.extra_data.0.into(),
base_fee_per_gas: genesis.base_fee_per_gas.map(|fee| fee.as_u64()),
// TODO: if/when ethers has cancun fields they should be added here
excess_blob_gas: None,
blob_gas_used: None,
alloc,
}
}
@@ -1126,6 +1172,9 @@ mod tests {
timestamp: 0x123456,
extra_data: Bytes::from_str("0xfafbfcfd").unwrap(),
gas_limit: 0x2fefd8,
base_fee_per_gas: None,
excess_blob_gas: None,
blob_gas_used: None,
alloc: HashMap::from_iter(vec![
(
Address::from_str("0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6").unwrap(),