|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + types::{Address, Bytes, H256, U256, U64}, |
| 5 | + utils::{from_int_or_hex, from_int_or_hex_opt}, |
| 6 | +}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +/// This represents the chain configuration, specifying the genesis block, header fields, and hard |
| 10 | +/// fork switch blocks. |
| 11 | +#[derive(Clone, Debug, Default, Serialize, Deserialize)] |
| 12 | +#[serde(rename_all = "camelCase")] |
| 13 | +pub struct Genesis { |
| 14 | + /// The fork configuration for this network. |
| 15 | + pub config: ChainConfig, |
| 16 | + |
| 17 | + /// The genesis header nonce. |
| 18 | + pub nonce: U64, |
| 19 | + |
| 20 | + /// The genesis header timestamp. |
| 21 | + pub timestamp: U64, |
| 22 | + |
| 23 | + /// The genesis header extra data. |
| 24 | + pub extra_data: Bytes, |
| 25 | + |
| 26 | + /// The genesis header gas limit. |
| 27 | + pub gas_limit: U64, |
| 28 | + |
| 29 | + /// The genesis header difficulty. |
| 30 | + #[serde(deserialize_with = "from_int_or_hex")] |
| 31 | + pub difficulty: U256, |
| 32 | + |
| 33 | + /// The genesis header mix hash. |
| 34 | + pub mix_hash: H256, |
| 35 | + |
| 36 | + /// The genesis header coinbase address. |
| 37 | + pub coinbase: Address, |
| 38 | + |
| 39 | + /// The initial state of the genesis block. |
| 40 | + pub alloc: HashMap<Address, GenesisAccount>, |
| 41 | +} |
| 42 | + |
| 43 | +/// An account in the state of the genesis block. |
| 44 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 45 | +pub struct GenesisAccount { |
| 46 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 47 | + pub nonce: Option<u64>, |
| 48 | + pub balance: U256, |
| 49 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 50 | + pub code: Option<Bytes>, |
| 51 | + #[serde(flatten, skip_serializing_if = "Option::is_none")] |
| 52 | + pub storage: Option<HashMap<H256, H256>>, |
| 53 | +} |
| 54 | + |
| 55 | +/// Represents a node's chain configuration. |
| 56 | +/// |
| 57 | +/// See [geth's `ChainConfig` |
| 58 | +/// struct](https://github.com/ethereum/go-ethereum/blob/64dccf7aa411c5c7cd36090c3d9b9892945ae813/params/config.go#L349) |
| 59 | +/// for the source of each field. |
| 60 | +#[derive(Clone, Debug, Deserialize, Serialize, Default)] |
| 61 | +#[serde(default, rename_all = "camelCase")] |
| 62 | +pub struct ChainConfig { |
| 63 | + /// The network's chain ID. |
| 64 | + pub chain_id: u64, |
| 65 | + |
| 66 | + /// The homestead switch block (None = no fork, 0 = already homestead). |
| 67 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 68 | + pub homestead_block: Option<u64>, |
| 69 | + |
| 70 | + /// The DAO fork switch block (None = no fork). |
| 71 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 72 | + pub dao_fork_block: Option<u64>, |
| 73 | + |
| 74 | + /// Whether or not the node supports the DAO hard-fork. |
| 75 | + pub dao_fork_support: bool, |
| 76 | + |
| 77 | + /// The EIP-150 hard fork block (None = no fork). |
| 78 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 79 | + pub eip150_block: Option<u64>, |
| 80 | + |
| 81 | + /// The EIP-150 hard fork hash. |
| 82 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 83 | + pub eip150_hash: Option<H256>, |
| 84 | + |
| 85 | + /// The EIP-155 hard fork block. |
| 86 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 87 | + pub eip155_block: Option<u64>, |
| 88 | + |
| 89 | + /// The EIP-158 hard fork block. |
| 90 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 91 | + pub eip158_block: Option<u64>, |
| 92 | + |
| 93 | + /// The Byzantium hard fork block. |
| 94 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 95 | + pub byzantium_block: Option<u64>, |
| 96 | + |
| 97 | + /// The Constantinople hard fork block. |
| 98 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 99 | + pub constantinople_block: Option<u64>, |
| 100 | + |
| 101 | + /// The Petersburg hard fork block. |
| 102 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 103 | + pub petersburg_block: Option<u64>, |
| 104 | + |
| 105 | + /// The Istanbul hard fork block. |
| 106 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 107 | + pub istanbul_block: Option<u64>, |
| 108 | + |
| 109 | + /// The Muir Glacier hard fork block. |
| 110 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 111 | + pub muir_glacier_block: Option<u64>, |
| 112 | + |
| 113 | + /// The Berlin hard fork block. |
| 114 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 115 | + pub berlin_block: Option<u64>, |
| 116 | + |
| 117 | + /// The London hard fork block. |
| 118 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 119 | + pub london_block: Option<u64>, |
| 120 | + |
| 121 | + /// The Arrow Glacier hard fork block. |
| 122 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 123 | + pub arrow_glacier_block: Option<u64>, |
| 124 | + |
| 125 | + /// The Gray Glacier hard fork block. |
| 126 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 127 | + pub gray_glacier_block: Option<u64>, |
| 128 | + |
| 129 | + /// Virtual fork after the merge to use as a network splitter. |
| 130 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 131 | + pub merge_netsplit_block: Option<u64>, |
| 132 | + |
| 133 | + /// The Shanghai hard fork block. |
| 134 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 135 | + pub shanghai_block: Option<u64>, |
| 136 | + |
| 137 | + /// The Cancun hard fork block. |
| 138 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 139 | + pub cancun_block: Option<u64>, |
| 140 | + |
| 141 | + /// Total difficulty reached that triggers the merge consensus upgrade. |
| 142 | + #[serde(skip_serializing_if = "Option::is_none", deserialize_with = "from_int_or_hex_opt")] |
| 143 | + pub terminal_total_difficulty: Option<U256>, |
| 144 | + |
| 145 | + /// A flag specifying that the network already passed the terminal total difficulty. Its |
| 146 | + /// purpose is to disable legacy sync without having seen the TTD locally. |
| 147 | + pub terminal_total_difficulty_passed: bool, |
| 148 | + |
| 149 | + /// Ethash parameters. |
| 150 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 151 | + pub ethash: Option<EthashConfig>, |
| 152 | + |
| 153 | + /// Clique parameters. |
| 154 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 155 | + pub clique: Option<CliqueConfig>, |
| 156 | +} |
| 157 | + |
| 158 | +/// Empty consensus configuration for proof-of-work networks. |
| 159 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 160 | +pub struct EthashConfig {} |
| 161 | + |
| 162 | +/// Consensus configuration for Clique. |
| 163 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 164 | +pub struct CliqueConfig { |
| 165 | + /// Number of seconds between blocks to enforce. |
| 166 | + pub period: u64, |
| 167 | + |
| 168 | + /// Epoch length to reset votes and checkpoints. |
| 169 | + pub epoch: u64, |
| 170 | +} |
0 commit comments