From c298158e343fcb133871a6f0e7446cef30af86ac Mon Sep 17 00:00:00 2001 From: refcell Date: Wed, 10 Apr 2024 14:26:50 -0500 Subject: [PATCH 01/20] feat(consensus): op-consensus --- crates/op-consensus/Cargo.toml | 45 + crates/op-consensus/README.md | 28 + crates/op-consensus/src/constants.rs | 72 + crates/op-consensus/src/header.rs | 467 +++++++ crates/op-consensus/src/lib.rs | 47 + crates/op-consensus/src/receipt/any.rs | 99 ++ crates/op-consensus/src/receipt/envelope.rs | 192 +++ crates/op-consensus/src/receipt/mod.rs | 139 ++ crates/op-consensus/src/receipt/receipts.rs | 200 +++ crates/op-consensus/src/sealed.rs | 68 + crates/op-consensus/src/signed.rs | 63 + .../op-consensus/src/transaction/eip1559.rs | 409 ++++++ .../op-consensus/src/transaction/eip2930.rs | 366 ++++++ .../op-consensus/src/transaction/eip4844.rs | 1168 +++++++++++++++++ .../src/transaction/eip4844/builder.rs | 426 ++++++ .../src/transaction/eip4844/utils.rs | 77 ++ .../op-consensus/src/transaction/envelope.rs | 545 ++++++++ crates/op-consensus/src/transaction/legacy.rs | 345 +++++ crates/op-consensus/src/transaction/mod.rs | 121 ++ crates/op-consensus/src/transaction/typed.rs | 163 +++ .../testdata/rpc_blob_transaction.rlp | 1 + 21 files changed, 5041 insertions(+) create mode 100644 crates/op-consensus/Cargo.toml create mode 100644 crates/op-consensus/README.md create mode 100644 crates/op-consensus/src/constants.rs create mode 100644 crates/op-consensus/src/header.rs create mode 100644 crates/op-consensus/src/lib.rs create mode 100644 crates/op-consensus/src/receipt/any.rs create mode 100644 crates/op-consensus/src/receipt/envelope.rs create mode 100644 crates/op-consensus/src/receipt/mod.rs create mode 100644 crates/op-consensus/src/receipt/receipts.rs create mode 100644 crates/op-consensus/src/sealed.rs create mode 100644 crates/op-consensus/src/signed.rs create mode 100644 crates/op-consensus/src/transaction/eip1559.rs create mode 100644 crates/op-consensus/src/transaction/eip2930.rs create mode 100644 crates/op-consensus/src/transaction/eip4844.rs create mode 100644 crates/op-consensus/src/transaction/eip4844/builder.rs create mode 100644 crates/op-consensus/src/transaction/eip4844/utils.rs create mode 100644 crates/op-consensus/src/transaction/envelope.rs create mode 100644 crates/op-consensus/src/transaction/legacy.rs create mode 100644 crates/op-consensus/src/transaction/mod.rs create mode 100644 crates/op-consensus/src/transaction/typed.rs create mode 100644 crates/op-consensus/testdata/rpc_blob_transaction.rlp diff --git a/crates/op-consensus/Cargo.toml b/crates/op-consensus/Cargo.toml new file mode 100644 index 00000000..5e4f9362 --- /dev/null +++ b/crates/op-consensus/Cargo.toml @@ -0,0 +1,45 @@ +[package] +name = "op-alloy-consensus" +description = "Optimism alloy consensus types" + +version.workspace = true +edition.workspace = true +rust-version.workspace = true +authors.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +exclude.workspace = true + +[dependencies] +alloy-primitives = { workspace = true, features = ["rlp"] } +alloy-rlp.workspace = true +alloy-eips.workspace = true +alloy-serde = { workspace = true, optional = true } + +sha2 = { version = "0.10", default-features = false } + +# kzg +thiserror = { workspace = true, optional = true } +c-kzg = { workspace = true, features = ["serde"], optional = true } + +# arbitrary +arbitrary = { workspace = true, features = ["derive"], optional = true } + +# serde +serde = { workspace = true, features = ["derive"], optional = true } + +[dev-dependencies] +alloy-signer.workspace = true +arbitrary = { workspace = true, features = ["derive"] } +k256.workspace = true +tokio = { workspace = true, features = ["macros"] } +serde_json.workspace = true + +[features] +default = ["std"] +std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"] +k256 = ["alloy-primitives/k256"] +kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"] +arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary"] +serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"] diff --git a/crates/op-consensus/README.md b/crates/op-consensus/README.md new file mode 100644 index 00000000..ff033c23 --- /dev/null +++ b/crates/op-consensus/README.md @@ -0,0 +1,28 @@ +# alloy-consensus + +Ethereum consensus interface. + +This crate contains constants, types, and functions for implementing Ethereum +EL consensus and communication. This includes headers, blocks, transactions, +[EIP-2718] envelopes, [EIP-2930], [EIP-4844], and more. + +In general a type belongs in this crate if it is committed to in the EL block +header. This includes: + +- transactions +- blocks +- headers +- receipts +- [EIP-2718] envelopes. + +[alloy-network]: ../network +[EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 +[EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 +[EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 + +## Provenance + +Much of this code was ported from [reth-primitives] as part of ongoing alloy +migrations. + +[reth-primitives]: https://github.com/paradigmxyz/reth/tree/main/crates/primitives diff --git a/crates/op-consensus/src/constants.rs b/crates/op-consensus/src/constants.rs new file mode 100644 index 00000000..764ca25f --- /dev/null +++ b/crates/op-consensus/src/constants.rs @@ -0,0 +1,72 @@ +//! Ethereum protocol-related constants +use alloy_primitives::{address, b256, Address, B256}; + +/// The first four bytes of the call data for a function call specifies the function to be called. +pub const SELECTOR_LEN: usize = 4; + +/// Maximum extra data size in a block after genesis +pub const MAXIMUM_EXTRA_DATA_SIZE: usize = 32; + +/// Multiplier for converting gwei to wei. +pub const GWEI_TO_WEI: u64 = 1_000_000_000; + +/// Multiplier for converting finney (milliether) to wei. +pub const FINNEY_TO_WEI: u128 = (GWEI_TO_WEI as u128) * 1_000_000; + +/// Multiplier for converting ether to wei. +pub const ETH_TO_WEI: u128 = FINNEY_TO_WEI * 1000; + +/// Multiplier for converting mgas to gas. +pub const MGAS_TO_GAS: u64 = 1_000_000u64; + +/// The Ethereum mainnet genesis hash. +pub const MAINNET_GENESIS_HASH: B256 = + b256!("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"); + +/// Goerli genesis hash. +pub const GOERLI_GENESIS_HASH: B256 = + b256!("bf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a"); + +/// Sepolia genesis hash. +pub const SEPOLIA_GENESIS_HASH: B256 = + b256!("25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9"); + +/// Holesky genesis hash. +pub const HOLESKY_GENESIS_HASH: B256 = + b256!("ff9006519a8ce843ac9c28549d24211420b546e12ce2d170c77a8cca7964f23d"); + +/// Testnet genesis hash. +pub const DEV_GENESIS_HASH: B256 = + b256!("2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c"); + +/// Optimism goerli genesis hash. +pub const GOERLI_OP_GENESIS: B256 = + b256!("c1fc15cd51159b1f1e5cbc4b82e85c1447ddfa33c52cf1d98d14fba0d6354be1"); + +/// Base goerli genesis hash. +pub const GOERLI_BASE_GENESIS: B256 = + b256!("a3ab140f15ea7f7443a4702da64c10314eb04d488e72974e02e2d728096b4f76"); + +/// Keccak256 over empty array. +pub const KECCAK_EMPTY: B256 = + b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); + +/// Ommer root of empty list. +pub const EMPTY_OMMER_ROOT_HASH: B256 = + b256!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); + +/// Root hash of an empty trie. +pub const EMPTY_ROOT_HASH: B256 = + b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); + +/// Transactions root of empty receipts set. +pub const EMPTY_RECEIPTS: B256 = EMPTY_ROOT_HASH; + +/// Transactions root of empty transactions set. +pub const EMPTY_TRANSACTIONS: B256 = EMPTY_ROOT_HASH; + +/// Withdrawals root of empty withdrawals set. +pub const EMPTY_WITHDRAWALS: B256 = EMPTY_ROOT_HASH; + +/// The address for the beacon roots contract defined in EIP-4788. +pub const BEACON_ROOTS_ADDRESS: Address = address!("000F3df6D732807Ef1319fB7B8bB8522d0Beac02"); diff --git a/crates/op-consensus/src/header.rs b/crates/op-consensus/src/header.rs new file mode 100644 index 00000000..252f0516 --- /dev/null +++ b/crates/op-consensus/src/header.rs @@ -0,0 +1,467 @@ +use crate::Sealable; +use alloy_eips::{ + eip1559::{calc_next_block_base_fee, BaseFeeParams}, + eip4844::{calc_blob_gasprice, calc_excess_blob_gas}, +}; +use alloy_primitives::{b256, keccak256, Address, BlockNumber, Bloom, Bytes, B256, B64, U256}; +use alloy_rlp::{ + length_of_length, Buf, BufMut, Decodable, Encodable, EMPTY_LIST_CODE, EMPTY_STRING_CODE, +}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// Ommer root of empty list. +pub const EMPTY_OMMER_ROOT_HASH: B256 = + b256!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); + +/// Root hash of an empty trie. +pub const EMPTY_ROOT_HASH: B256 = + b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); + +/// Ethereum Block header +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Header { + /// The Keccak 256-bit hash of the parent + /// block’s header, in its entirety; formally Hp. + pub parent_hash: B256, + /// The Keccak 256-bit hash of the ommers list portion of this block; formally Ho. + pub ommers_hash: B256, + /// The 160-bit address to which all fees collected from the successful mining of this block + /// be transferred; formally Hc. + pub beneficiary: Address, + /// The Keccak 256-bit hash of the root node of the state trie, after all transactions are + /// executed and finalisations applied; formally Hr. + pub state_root: B256, + /// The Keccak 256-bit hash of the root node of the trie structure populated with each + /// transaction in the transactions list portion of the block; formally Ht. + pub transactions_root: B256, + /// The Keccak 256-bit hash of the root node of the trie structure populated with the receipts + /// of each transaction in the transactions list portion of the block; formally He. + pub receipts_root: B256, + /// The Keccak 256-bit hash of the withdrawals list portion of this block. + /// + pub withdrawals_root: Option, + /// The Bloom filter composed from indexable information (logger address and log topics) + /// contained in each log entry from the receipt of each transaction in the transactions list; + /// formally Hb. + pub logs_bloom: Bloom, + /// A scalar value corresponding to the difficulty level of this block. This can be calculated + /// from the previous block’s difficulty level and the timestamp; formally Hd. + pub difficulty: U256, + /// A scalar value equal to the number of ancestor blocks. The genesis block has a number of + /// zero; formally Hi. + pub number: BlockNumber, + /// A scalar value equal to the current limit of gas expenditure per block; formally Hl. + pub gas_limit: u128, + /// A scalar value equal to the total gas used in transactions in this block; formally Hg. + pub gas_used: u128, + /// A scalar value equal to the reasonable output of Unix’s time() at this block’s inception; + /// formally Hs. + pub timestamp: u64, + /// A 256-bit hash which, combined with the + /// nonce, proves that a sufficient amount of computation has been carried out on this block; + /// formally Hm. + pub mix_hash: B256, + /// A 64-bit value which, combined with the mixhash, proves that a sufficient amount of + /// computation has been carried out on this block; formally Hn. + pub nonce: u64, + /// A scalar representing EIP1559 base fee which can move up or down each block according + /// to a formula which is a function of gas used in parent block and gas target + /// (block gas limit divided by elasticity multiplier) of parent block. + /// The algorithm results in the base fee per gas increasing when blocks are + /// above the gas target, and decreasing when blocks are below the gas target. The base fee per + /// gas is burned. + pub base_fee_per_gas: Option, + /// The total amount of blob gas consumed by the transactions within the block, added in + /// EIP-4844. + pub blob_gas_used: Option, + /// A running total of blob gas consumed in excess of the target, prior to the block. Blocks + /// with above-target blob gas consumption increase this value, blocks with below-target blob + /// gas consumption decrease it (bounded at 0). This was added in EIP-4844. + pub excess_blob_gas: Option, + /// The hash of the parent beacon block's root is included in execution blocks, as proposed by + /// EIP-4788. + /// + /// This enables trust-minimized access to consensus state, supporting staking pools, bridges, + /// and more. + /// + /// The beacon roots contract handles root storage, enhancing Ethereum's functionalities. + pub parent_beacon_block_root: Option, + /// An arbitrary byte array containing data relevant to this block. This must be 32 bytes or + /// fewer; formally Hx. + pub extra_data: Bytes, +} + +impl Default for Header { + fn default() -> Self { + Header { + parent_hash: Default::default(), + ommers_hash: EMPTY_OMMER_ROOT_HASH, + beneficiary: Default::default(), + state_root: EMPTY_ROOT_HASH, + transactions_root: EMPTY_ROOT_HASH, + receipts_root: EMPTY_ROOT_HASH, + logs_bloom: Default::default(), + difficulty: Default::default(), + number: 0, + gas_limit: 0, + gas_used: 0, + timestamp: 0, + extra_data: Default::default(), + mix_hash: Default::default(), + nonce: 0, + base_fee_per_gas: None, + withdrawals_root: None, + blob_gas_used: None, + excess_blob_gas: None, + parent_beacon_block_root: None, + } + } +} + +impl Sealable for Header { + fn hash(&self) -> B256 { + self.hash_slow() + } +} + +impl Header { + // TODO: re-enable + + // /// Returns the parent block's number and hash + // pub fn parent_num_hash(&self) -> BlockNumHash { + // BlockNumHash { number: self.number.saturating_sub(1), hash: self.parent_hash } + // } + + /// Heavy function that will calculate hash of data and will *not* save the change to metadata. + /// + /// Use [`Header::seal_slow`] and unlock if you need the hash to be persistent. + pub fn hash_slow(&self) -> B256 { + let mut out = Vec::::new(); + self.encode(&mut out); + keccak256(&out) + } + + /// Checks if the header is empty - has no transactions and no ommers + pub fn is_empty(&self) -> bool { + let txs_and_ommers_empty = self.transaction_root_is_empty() && self.ommers_hash_is_empty(); + if let Some(withdrawals_root) = self.withdrawals_root { + txs_and_ommers_empty && withdrawals_root == EMPTY_ROOT_HASH + } else { + txs_and_ommers_empty + } + } + + /// Check if the ommers hash equals to empty hash list. + pub fn ommers_hash_is_empty(&self) -> bool { + self.ommers_hash == EMPTY_OMMER_ROOT_HASH + } + + /// Check if the transaction root equals to empty root. + pub fn transaction_root_is_empty(&self) -> bool { + self.transactions_root == EMPTY_ROOT_HASH + } + + // TODO: re-enable + + // /// Converts all roots in the header to a [BlockBodyRoots] struct. + // pub fn body_roots(&self) -> BlockBodyRoots { + // BlockBodyRoots { + // tx_root: self.transactions_root, + // ommers_hash: self.ommers_hash, + // withdrawals_root: self.withdrawals_root, + // } + // } + + /// Returns the blob fee for _this_ block according to the EIP-4844 spec. + /// + /// Returns `None` if `excess_blob_gas` is None + pub fn blob_fee(&self) -> Option { + self.excess_blob_gas.map(calc_blob_gasprice) + } + + /// Returns the blob fee for the next block according to the EIP-4844 spec. + /// + /// Returns `None` if `excess_blob_gas` is None. + /// + /// See also [Self::next_block_excess_blob_gas] + pub fn next_block_blob_fee(&self) -> Option { + self.next_block_excess_blob_gas().map(calc_blob_gasprice) + } + + /// Calculate base fee for next block according to the EIP-1559 spec. + /// + /// Returns a `None` if no base fee is set, no EIP-1559 support + pub fn next_block_base_fee(&self, base_fee_params: BaseFeeParams) -> Option { + Some(calc_next_block_base_fee( + self.gas_used, + self.gas_limit, + self.base_fee_per_gas?, + base_fee_params, + )) + } + + /// Calculate excess blob gas for the next block according to the EIP-4844 + /// spec. + /// + /// Returns a `None` if no excess blob gas is set, no EIP-4844 support + pub fn next_block_excess_blob_gas(&self) -> Option { + Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) + } + + /// Calculate a heuristic for the in-memory size of the [Header]. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // parent hash + mem::size_of::() + // ommers hash + mem::size_of::
() + // beneficiary + mem::size_of::() + // state root + mem::size_of::() + // transactions root + mem::size_of::() + // receipts root + mem::size_of::>() + // withdrawals root + mem::size_of::() + // logs bloom + mem::size_of::() + // difficulty + mem::size_of::() + // number + mem::size_of::() + // gas limit + mem::size_of::() + // gas used + mem::size_of::() + // timestamp + mem::size_of::() + // mix hash + mem::size_of::() + // nonce + mem::size_of::>() + // base fee per gas + mem::size_of::>() + // blob gas used + mem::size_of::>() + // excess blob gas + mem::size_of::>() + // parent beacon block root + self.extra_data.len() // extra data + } + + fn header_payload_length(&self) -> usize { + let mut length = 0; + length += self.parent_hash.length(); + length += self.ommers_hash.length(); + length += self.beneficiary.length(); + length += self.state_root.length(); + length += self.transactions_root.length(); + length += self.receipts_root.length(); + length += self.logs_bloom.length(); + length += self.difficulty.length(); + length += U256::from(self.number).length(); + length += U256::from(self.gas_limit).length(); + length += U256::from(self.gas_used).length(); + length += self.timestamp.length(); + length += self.extra_data.length(); + length += self.mix_hash.length(); + length += B64::new(self.nonce.to_be_bytes()).length(); + + if let Some(base_fee) = self.base_fee_per_gas { + length += U256::from(base_fee).length(); + } else if self.withdrawals_root.is_some() + || self.blob_gas_used.is_some() + || self.excess_blob_gas.is_some() + || self.parent_beacon_block_root.is_some() + { + length += 1; // EMPTY LIST CODE + } + + if let Some(root) = self.withdrawals_root { + length += root.length(); + } else if self.blob_gas_used.is_some() + || self.excess_blob_gas.is_some() + || self.parent_beacon_block_root.is_some() + { + length += 1; // EMPTY STRING CODE + } + + if let Some(blob_gas_used) = self.blob_gas_used { + length += U256::from(blob_gas_used).length(); + } else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() { + length += 1; // EMPTY LIST CODE + } + + if let Some(excess_blob_gas) = self.excess_blob_gas { + length += U256::from(excess_blob_gas).length(); + } else if self.parent_beacon_block_root.is_some() { + length += 1; // EMPTY LIST CODE + } + + // Encode parent beacon block root length. If new fields are added, the above pattern will + // need to be repeated and placeholder length added. Otherwise, it's impossible to + // tell _which_ fields are missing. This is mainly relevant for contrived cases + // where a header is created at random, for example: + // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are + // post-London, so this is technically not valid. However, a tool like proptest would + // generate a block like this. + if let Some(parent_beacon_block_root) = self.parent_beacon_block_root { + length += parent_beacon_block_root.length(); + } + + length + } +} + +impl Encodable for Header { + fn encode(&self, out: &mut dyn BufMut) { + let list_header = + alloy_rlp::Header { list: true, payload_length: self.header_payload_length() }; + list_header.encode(out); + self.parent_hash.encode(out); + self.ommers_hash.encode(out); + self.beneficiary.encode(out); + self.state_root.encode(out); + self.transactions_root.encode(out); + self.receipts_root.encode(out); + self.logs_bloom.encode(out); + self.difficulty.encode(out); + U256::from(self.number).encode(out); + U256::from(self.gas_limit).encode(out); + U256::from(self.gas_used).encode(out); + self.timestamp.encode(out); + self.extra_data.encode(out); + self.mix_hash.encode(out); + B64::new(self.nonce.to_be_bytes()).encode(out); + + // Encode base fee. Put empty list if base fee is missing, + // but withdrawals root is present. + if let Some(ref base_fee) = self.base_fee_per_gas { + U256::from(*base_fee).encode(out); + } else if self.withdrawals_root.is_some() + || self.blob_gas_used.is_some() + || self.excess_blob_gas.is_some() + || self.parent_beacon_block_root.is_some() + { + out.put_u8(EMPTY_LIST_CODE); + } + + // Encode withdrawals root. Put empty string if withdrawals root is missing, + // but blob gas used is present. + if let Some(ref root) = self.withdrawals_root { + root.encode(out); + } else if self.blob_gas_used.is_some() + || self.excess_blob_gas.is_some() + || self.parent_beacon_block_root.is_some() + { + out.put_u8(EMPTY_STRING_CODE); + } + + // Encode blob gas used. Put empty list if blob gas used is missing, + // but excess blob gas is present. + if let Some(ref blob_gas_used) = self.blob_gas_used { + U256::from(*blob_gas_used).encode(out); + } else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() { + out.put_u8(EMPTY_LIST_CODE); + } + + // Encode excess blob gas. Put empty list if excess blob gas is missing, + // but parent beacon block root is present. + if let Some(ref excess_blob_gas) = self.excess_blob_gas { + U256::from(*excess_blob_gas).encode(out); + } else if self.parent_beacon_block_root.is_some() { + out.put_u8(EMPTY_LIST_CODE); + } + + // Encode parent beacon block root. If new fields are added, the above pattern will need to + // be repeated and placeholders added. Otherwise, it's impossible to tell _which_ + // fields are missing. This is mainly relevant for contrived cases where a header is + // created at random, for example: + // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are + // post-London, so this is technically not valid. However, a tool like proptest would + // generate a block like this. + if let Some(ref parent_beacon_block_root) = self.parent_beacon_block_root { + parent_beacon_block_root.encode(out); + } + } + + fn length(&self) -> usize { + let mut length = 0; + length += self.header_payload_length(); + length += length_of_length(length); + length + } +} + +impl Decodable for Header { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let rlp_head = alloy_rlp::Header::decode(buf)?; + if !rlp_head.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + let started_len = buf.len(); + let mut this = Self { + parent_hash: Decodable::decode(buf)?, + ommers_hash: Decodable::decode(buf)?, + beneficiary: Decodable::decode(buf)?, + state_root: Decodable::decode(buf)?, + transactions_root: Decodable::decode(buf)?, + receipts_root: Decodable::decode(buf)?, + logs_bloom: Decodable::decode(buf)?, + difficulty: Decodable::decode(buf)?, + number: u64::decode(buf)?, + gas_limit: u128::decode(buf)?, + gas_used: u128::decode(buf)?, + timestamp: Decodable::decode(buf)?, + extra_data: Decodable::decode(buf)?, + mix_hash: Decodable::decode(buf)?, + nonce: u64::from_be_bytes(B64::decode(buf)?.0), + base_fee_per_gas: None, + withdrawals_root: None, + blob_gas_used: None, + excess_blob_gas: None, + parent_beacon_block_root: None, + }; + + if started_len - buf.len() < rlp_head.payload_length { + if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { + buf.advance(1) + } else { + this.base_fee_per_gas = Some(U256::decode(buf)?.to::()); + } + } + + // Withdrawals root for post-shanghai headers + if started_len - buf.len() < rlp_head.payload_length { + if buf.first().map(|b| *b == EMPTY_STRING_CODE).unwrap_or_default() { + buf.advance(1) + } else { + this.withdrawals_root = Some(Decodable::decode(buf)?); + } + } + + // Blob gas used and excess blob gas for post-cancun headers + if started_len - buf.len() < rlp_head.payload_length { + if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { + buf.advance(1) + } else { + this.blob_gas_used = Some(U256::decode(buf)?.to::()); + } + } + + if started_len - buf.len() < rlp_head.payload_length { + if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { + buf.advance(1) + } else { + this.excess_blob_gas = Some(U256::decode(buf)?.to::()); + } + } + + // Decode parent beacon block root. If new fields are added, the above pattern will need to + // be repeated and placeholders decoded. Otherwise, it's impossible to tell _which_ + // fields are missing. This is mainly relevant for contrived cases where a header is + // created at random, for example: + // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are + // post-London, so this is technically not valid. However, a tool like proptest would + // generate a block like this. + if started_len - buf.len() < rlp_head.payload_length { + this.parent_beacon_block_root = Some(B256::decode(buf)?); + } + + let consumed = started_len - buf.len(); + if consumed != rlp_head.payload_length { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: rlp_head.payload_length, + got: consumed, + }); + } + Ok(this) + } +} diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs new file mode 100644 index 00000000..77bef5a6 --- /dev/null +++ b/crates/op-consensus/src/lib.rs @@ -0,0 +1,47 @@ +#![doc = include_str!("../README.md")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg", + html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico" +)] +#![warn( + missing_copy_implementations, + missing_debug_implementations, + missing_docs, + unreachable_pub, + clippy::missing_const_for_fn, + rustdoc::all +)] +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![deny(unused_must_use, rust_2018_idioms)] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +pub mod constants; + +mod header; +pub use header::{Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH}; + +mod receipt; +pub use receipt::{AnyReceiptEnvelope, Receipt, ReceiptEnvelope, ReceiptWithBloom, TxReceipt}; + +mod transaction; +pub use transaction::{ + eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder, + SignableTransaction, SimpleCoder, Transaction, TxEip1559, TxEip2930, TxEip4844, + TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction, +}; + +#[cfg(feature = "kzg")] +pub use transaction::BlobTransactionValidationError; + +#[cfg(feature = "kzg")] +pub use alloy_eips::eip4844::env_settings::EnvKzgSettings; + +mod sealed; +pub use sealed::{Sealable, Sealed}; + +mod signed; +pub use signed::Signed; diff --git a/crates/op-consensus/src/receipt/any.rs b/crates/op-consensus/src/receipt/any.rs new file mode 100644 index 00000000..35482caa --- /dev/null +++ b/crates/op-consensus/src/receipt/any.rs @@ -0,0 +1,99 @@ +use crate::ReceiptWithBloom; +use alloy_eips::eip2718::{Decodable2718, Encodable2718}; +use alloy_primitives::{bytes::BufMut, Bloom, Log}; +use alloy_rlp::{Decodable, Encodable}; + +/// Receipt envelope, as defined in [EIP-2718]. +/// +/// This enum distinguishes between tagged and untagged legacy receipts, as the +/// in-protocol merkle tree may commit to EITHER 0-prefixed or raw. Therefore +/// we must ensure that encoding returns the precise byte-array that was +/// decoded, preserving the presence or absence of the `TransactionType` flag. +/// +/// Transaction receipt payloads are specified in their respective EIPs. +/// +/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct AnyReceiptEnvelope { + /// The receipt envelope. + #[cfg_attr(feature = "serde", serde(flatten))] + pub inner: ReceiptWithBloom, + /// The transaction type. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u8_hex"))] + pub r#type: u8, +} + +impl AnyReceiptEnvelope { + /// Returns whether this is a legacy receipt (type 0) + pub const fn is_legacy(&self) -> bool { + self.r#type == 0 + } + + /// Calculate the length of the rlp payload of the network encoded receipt. + pub fn rlp_payload_length(&self) -> usize { + let length = self.inner.length(); + if self.is_legacy() { + length + } else { + length + 1 + } + } + + /// Return true if the transaction was successful. + pub const fn is_success(&self) -> bool { + self.status() + } + + /// Returns the success status of the receipt's transaction. + pub const fn status(&self) -> bool { + self.inner.receipt.status + } + + /// Returns the cumulative gas used at this receipt. + pub const fn cumulative_gas_used(&self) -> u128 { + self.inner.receipt.cumulative_gas_used + } + + /// Return the receipt logs. + pub fn logs(&self) -> &[Log] { + &self.inner.receipt.logs + } + + /// Return the receipt's bloom. + pub const fn logs_bloom(&self) -> &Bloom { + &self.inner.logs_bloom + } +} + +impl Encodable2718 for AnyReceiptEnvelope { + fn type_flag(&self) -> Option { + match self.r#type { + 0 => None, + ty => Some(ty), + } + } + + fn encode_2718_len(&self) -> usize { + self.inner.length() + !self.is_legacy() as usize + } + + fn encode_2718(&self, out: &mut dyn BufMut) { + match self.type_flag() { + None => {} + Some(ty) => out.put_u8(ty), + } + self.inner.encode(out); + } +} + +impl Decodable2718 for AnyReceiptEnvelope { + fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { + let receipt = Decodable::decode(buf)?; + Ok(Self { inner: receipt, r#type: ty }) + } + + fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::typed_decode(0, buf) + } +} diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs new file mode 100644 index 00000000..6890f5a9 --- /dev/null +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -0,0 +1,192 @@ +use crate::{Receipt, ReceiptWithBloom, TxType}; +use alloy_eips::eip2718::{Decodable2718, Encodable2718}; +use alloy_primitives::{Bloom, Log}; +use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; + +/// Receipt envelope, as defined in [EIP-2718]. +/// +/// This enum distinguishes between tagged and untagged legacy receipts, as the +/// in-protocol merkle tree may commit to EITHER 0-prefixed or raw. Therefore +/// we must ensure that encoding returns the precise byte-array that was +/// decoded, preserving the presence or absence of the `TransactionType` flag. +/// +/// Transaction receipt payloads are specified in their respective EIPs. +/// +/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] +#[non_exhaustive] +pub enum ReceiptEnvelope { + /// Receipt envelope with no type flag. + #[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))] + Legacy(ReceiptWithBloom), + /// Receipt envelope with type flag 1, containing a [EIP-2930] receipt. + /// + /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 + #[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))] + Eip2930(ReceiptWithBloom), + /// Receipt envelope with type flag 2, containing a [EIP-1559] receipt. + /// + /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559 + #[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))] + Eip1559(ReceiptWithBloom), + /// Receipt envelope with type flag 2, containing a [EIP-4844] receipt. + /// + /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 + #[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))] + Eip4844(ReceiptWithBloom), +} + +impl ReceiptEnvelope { + /// Return the [`TxType`] of the inner receipt. + pub const fn tx_type(&self) -> TxType { + match self { + Self::Legacy(_) => TxType::Legacy, + Self::Eip2930(_) => TxType::Eip2930, + Self::Eip1559(_) => TxType::Eip1559, + Self::Eip4844(_) => TxType::Eip4844, + } + } + + /// Return true if the transaction was successful. + pub fn is_success(&self) -> bool { + self.status() + } + + /// Returns the success status of the receipt's transaction. + pub fn status(&self) -> bool { + self.as_receipt().unwrap().status + } + + /// Returns the cumulative gas used at this receipt. + pub fn cumulative_gas_used(&self) -> u128 { + self.as_receipt().unwrap().cumulative_gas_used + } + + /// Return the receipt logs. + pub fn logs(&self) -> &[T] { + &self.as_receipt().unwrap().logs + } + + /// Return the receipt's bloom. + pub fn logs_bloom(&self) -> &Bloom { + &self.as_receipt_with_bloom().unwrap().logs_bloom + } + + /// Return the inner receipt with bloom. Currently this is infallible, + /// however, future receipt types may be added. + pub const fn as_receipt_with_bloom(&self) -> Option<&ReceiptWithBloom> { + match self { + Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => Some(t), + } + } + + /// Return the inner receipt. Currently this is infallible, however, future + /// receipt types may be added. + pub const fn as_receipt(&self) -> Option<&Receipt> { + match self { + Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => { + Some(&t.receipt) + } + } + } +} + +impl ReceiptEnvelope { + /// Get the length of the inner receipt in the 2718 encoding. + pub fn inner_length(&self) -> usize { + self.as_receipt_with_bloom().unwrap().length() + } + + /// Calculate the length of the rlp payload of the network encoded receipt. + pub fn rlp_payload_length(&self) -> usize { + let length = self.as_receipt_with_bloom().unwrap().length(); + match self { + Self::Legacy(_) => length, + _ => length + 1, + } + } +} + +impl Encodable for ReceiptEnvelope { + fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { + self.network_encode(out) + } + + fn length(&self) -> usize { + let mut payload_length = self.rlp_payload_length(); + if !self.is_legacy() { + payload_length += length_of_length(payload_length); + } + payload_length + } +} + +impl Decodable for ReceiptEnvelope { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + match Self::network_decode(buf) { + Ok(t) => Ok(t), + Err(_) => Err(alloy_rlp::Error::Custom("Unexpected type")), + } + } +} + +impl Encodable2718 for ReceiptEnvelope { + fn type_flag(&self) -> Option { + match self { + Self::Legacy(_) => None, + Self::Eip2930(_) => Some(TxType::Eip2930 as u8), + Self::Eip1559(_) => Some(TxType::Eip1559 as u8), + Self::Eip4844(_) => Some(TxType::Eip4844 as u8), + } + } + + fn encode_2718_len(&self) -> usize { + self.inner_length() + !self.is_legacy() as usize + } + + fn encode_2718(&self, out: &mut dyn BufMut) { + match self.type_flag() { + None => {} + Some(ty) => out.put_u8(ty), + } + self.as_receipt_with_bloom().unwrap().encode(out); + } +} + +impl Decodable2718 for ReceiptEnvelope { + fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { + let receipt = Decodable::decode(buf)?; + match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? { + TxType::Eip2930 => Ok(Self::Eip2930(receipt)), + TxType::Eip1559 => Ok(Self::Eip1559(receipt)), + TxType::Eip4844 => Ok(Self::Eip4844(receipt)), + TxType::Legacy => { + Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) + } + } + } + + fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self::Legacy(Decodable::decode(buf)?)) + } +} + +#[cfg(any(test, feature = "arbitrary"))] +impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptEnvelope +where + T: arbitrary::Arbitrary<'a>, +{ + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + let receipt = ReceiptWithBloom::::arbitrary(u)?; + + match u.int_in_range(0..=3)? { + 0 => Ok(Self::Legacy(receipt)), + 1 => Ok(Self::Eip2930(receipt)), + 2 => Ok(Self::Eip1559(receipt)), + 3 => Ok(Self::Eip4844(receipt)), + _ => unreachable!(), + } + } +} diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs new file mode 100644 index 00000000..3eda915b --- /dev/null +++ b/crates/op-consensus/src/receipt/mod.rs @@ -0,0 +1,139 @@ +use alloy_primitives::{Bloom, Log}; + +mod any; +pub use any::AnyReceiptEnvelope; + +mod envelope; +pub use envelope::ReceiptEnvelope; + +mod receipts; +pub use receipts::{Receipt, ReceiptWithBloom}; + +/// Receipt is the result of a transaction execution. +pub trait TxReceipt { + /// Returns true if the transaction was successful. + fn success(&self) -> bool; + + /// Returns the bloom filter for the logs in the receipt. This operation + /// may be expensive. + fn bloom(&self) -> Bloom; + + /// Returns the bloom filter for the logs in the receipt, if it is cheap to + /// compute. + fn bloom_cheap(&self) -> Option { + None + } + + /// Returns the cumulative gas used in the block after this transaction was executed. + fn cumulative_gas_used(&self) -> u128; + + /// Returns the logs emitted by this transaction. + fn logs(&self) -> &[Log]; +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_eips::eip2718::Encodable2718; + use alloy_primitives::{address, b256, bytes, hex, Bytes, LogData}; + use alloy_rlp::{Decodable, Encodable}; + + // Test vector from: https://eips.ethereum.org/EIPS/eip-2481 + #[test] + fn encode_legacy_receipt() { + let expected = hex!("f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"); + + let mut data = vec![]; + let receipt = + ReceiptEnvelope::Legacy(ReceiptWithBloom { + receipt: Receipt { + cumulative_gas_used: 0x1u128, + logs: vec![Log { + address: address!("0000000000000000000000000000000000000011"), + data: LogData::new_unchecked( + vec![ + b256!("000000000000000000000000000000000000000000000000000000000000dead"), + b256!("000000000000000000000000000000000000000000000000000000000000beef"), + ], + bytes!("0100ff"), + ), + }], + status: false, + }, + logs_bloom: [0; 256].into(), + }); + + receipt.network_encode(&mut data); + + // check that the rlp length equals the length of the expected rlp + assert_eq!(receipt.length(), expected.len()); + assert_eq!(data, expected); + } + + // Test vector from: https://eips.ethereum.org/EIPS/eip-2481 + #[test] + fn decode_legacy_receipt() { + let data = hex!("f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"); + + // EIP658Receipt + let expected = + ReceiptWithBloom { + receipt: Receipt { + cumulative_gas_used: 0x1u128, + logs: vec![Log { + address: address!("0000000000000000000000000000000000000011"), + data: LogData::new_unchecked( + vec![ + b256!("000000000000000000000000000000000000000000000000000000000000dead"), + b256!("000000000000000000000000000000000000000000000000000000000000beef"), + ], + bytes!("0100ff"), + ), + }], + status: false, + }, + logs_bloom: [0; 256].into(), + }; + + let receipt = ReceiptWithBloom::decode(&mut &data[..]).unwrap(); + assert_eq!(receipt, expected); + } + + #[test] + fn gigantic_receipt() { + let receipt = Receipt { + cumulative_gas_used: 16747627, + status: true, + logs: vec![ + Log { + address: address!("4bf56695415f725e43c3e04354b604bcfb6dfb6e"), + data: LogData::new_unchecked( + vec![b256!( + "c69dc3d7ebff79e41f525be431d5cd3cc08f80eaf0f7819054a726eeb7086eb9" + )], + Bytes::from(vec![1; 0xffffff]), + ), + }, + Log { + address: address!("faca325c86bf9c2d5b413cd7b90b209be92229c2"), + data: LogData::new_unchecked( + vec![b256!( + "8cca58667b1e9ffa004720ac99a3d61a138181963b294d270d91c53d36402ae2" + )], + Bytes::from(vec![1; 0xffffff]), + ), + }, + ], + } + .with_bloom(); + + let mut data = vec![]; + + receipt.encode(&mut data); + let decoded = ReceiptWithBloom::decode(&mut &data[..]).unwrap(); + + // receipt.clone().to_compact(&mut data); + // let (decoded, _) = Receipt::from_compact(&data[..], data.len()); + assert_eq!(decoded, receipt); + } +} diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs new file mode 100644 index 00000000..3acd06e5 --- /dev/null +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -0,0 +1,200 @@ +use super::TxReceipt; +use alloy_primitives::{Bloom, Log}; +use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// Receipt containing result of transaction execution. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct Receipt { + /// If transaction is executed successfully. + /// + /// This is the `statusCode` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity_bool"))] + pub status: bool, + /// Gas used + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub cumulative_gas_used: u128, + /// Log send from contracts. + pub logs: Vec, +} + +impl Receipt { + /// Calculates [`Log`]'s bloom filter. this is slow operation and [ReceiptWithBloom] can + /// be used to cache this value. + pub fn bloom_slow(&self) -> Bloom { + self.logs.iter().collect() + } + + /// Calculates the bloom filter for the receipt and returns the [ReceiptWithBloom] container + /// type. + pub fn with_bloom(self) -> ReceiptWithBloom { + self.into() + } +} + +impl TxReceipt for Receipt { + fn success(&self) -> bool { + self.status + } + + fn bloom(&self) -> Bloom { + self.bloom_slow() + } + + fn cumulative_gas_used(&self) -> u128 { + self.cumulative_gas_used + } + + fn logs(&self) -> &[Log] { + &self.logs + } +} + +/// [`Receipt`] with calculated bloom filter. +/// +/// This convenience type allows us to lazily calculate the bloom filter for a +/// receipt, similar to [`Sealed`]. +/// +/// [`Sealed`]: crate::sealed::Sealed +#[derive(Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct ReceiptWithBloom { + #[cfg_attr(feature = "serde", serde(flatten))] + /// The receipt. + pub receipt: Receipt, + /// The bloom filter. + pub logs_bloom: Bloom, +} + +impl TxReceipt for ReceiptWithBloom { + fn success(&self) -> bool { + self.receipt.status + } + + fn bloom(&self) -> Bloom { + self.logs_bloom + } + + fn bloom_cheap(&self) -> Option { + Some(self.logs_bloom) + } + + fn cumulative_gas_used(&self) -> u128 { + self.receipt.cumulative_gas_used + } + + fn logs(&self) -> &[Log] { + &self.receipt.logs + } +} + +impl From for ReceiptWithBloom { + fn from(receipt: Receipt) -> Self { + let bloom = receipt.bloom_slow(); + ReceiptWithBloom { receipt, logs_bloom: bloom } + } +} + +impl ReceiptWithBloom { + /// Create new [ReceiptWithBloom] + pub const fn new(receipt: Receipt, bloom: Bloom) -> Self { + Self { receipt, logs_bloom: bloom } + } + + /// Consume the structure, returning only the receipt + #[allow(clippy::missing_const_for_fn)] // false positive + pub fn into_receipt(self) -> Receipt { + self.receipt + } + + /// Consume the structure, returning the receipt and the bloom filter + #[allow(clippy::missing_const_for_fn)] // false positive + pub fn into_components(self) -> (Receipt, Bloom) { + (self.receipt, self.logs_bloom) + } + + fn payload_len(&self) -> usize { + self.receipt.status.length() + + self.receipt.cumulative_gas_used.length() + + self.logs_bloom.length() + + self.receipt.logs.length() + } + + /// Returns the rlp header for the receipt payload. + fn receipt_rlp_header(&self) -> alloy_rlp::Header { + alloy_rlp::Header { list: true, payload_length: self.payload_len() } + } + + /// Encodes the receipt data. + fn encode_fields(&self, out: &mut dyn BufMut) { + self.receipt_rlp_header().encode(out); + self.receipt.status.encode(out); + self.receipt.cumulative_gas_used.encode(out); + self.logs_bloom.encode(out); + self.receipt.logs.encode(out); + } + + /// Decodes the receipt payload + fn decode_receipt(buf: &mut &[u8]) -> alloy_rlp::Result { + let b: &mut &[u8] = &mut &**buf; + let rlp_head = alloy_rlp::Header::decode(b)?; + if !rlp_head.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + let started_len = b.len(); + + let success = Decodable::decode(b)?; + let cumulative_gas_used = Decodable::decode(b)?; + let bloom = Decodable::decode(b)?; + let logs = Decodable::decode(b)?; + + let receipt = Receipt { status: success, cumulative_gas_used, logs }; + + let this = Self { receipt, logs_bloom: bloom }; + let consumed = started_len - b.len(); + if consumed != rlp_head.payload_length { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: rlp_head.payload_length, + got: consumed, + }); + } + *buf = *b; + Ok(this) + } +} + +impl alloy_rlp::Encodable for ReceiptWithBloom { + fn encode(&self, out: &mut dyn BufMut) { + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.receipt.status.length() + + self.receipt.cumulative_gas_used.length() + + self.logs_bloom.length() + + self.receipt.logs.length(); + payload_length + length_of_length(payload_length) + } +} + +impl alloy_rlp::Decodable for ReceiptWithBloom { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::decode_receipt(buf) + } +} + +#[cfg(any(test, feature = "arbitrary"))] +impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptWithBloom +where + T: arbitrary::Arbitrary<'a>, +{ + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Ok(Self { receipt: Receipt::::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? }) + } +} diff --git a/crates/op-consensus/src/sealed.rs b/crates/op-consensus/src/sealed.rs new file mode 100644 index 00000000..86d5c85a --- /dev/null +++ b/crates/op-consensus/src/sealed.rs @@ -0,0 +1,68 @@ +use alloy_primitives::B256; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +/// A consensus hashable item, with its memoized hash. +/// +/// We do not implement +pub struct Sealed { + /// The inner item + inner: T, + /// Its hash. + seal: B256, +} + +impl core::ops::Deref for Sealed { + type Target = T; + + fn deref(&self) -> &Self::Target { + self.inner() + } +} + +impl Sealed { + /// Instantiate without performing the hash. This should be used carefully. + pub const fn new_unchecked(inner: T, seal: B256) -> Self { + Self { inner, seal } + } + + /// Decompose into parts. + #[allow(clippy::missing_const_for_fn)] // false positive + pub fn into_parts(self) -> (T, B256) { + (self.inner, self.seal) + } + + /// Get the inner item. + #[inline(always)] + pub const fn inner(&self) -> &T { + &self.inner + } + + /// Get the hash. + #[inline(always)] + pub const fn seal(&self) -> B256 { + self.seal + } + + /// Geth the hash (alias for [`Self::seal`]). + #[inline(always)] + pub const fn hash(&self) -> B256 { + self.seal() + } +} + +/// Sealeable objects. +pub trait Sealable: Sized { + /// Calculate the seal hash, this may be slow. + fn hash(&self) -> B256; + + /// Seal the object by calculating the hash. This may be slow. + fn seal_slow(self) -> Sealed { + let seal = self.hash(); + Sealed::new_unchecked(self, seal) + } + + /// Instantiate an unchecked seal. This should be used with caution. + fn seal_unchecked(self, seal: B256) -> Sealed { + Sealed::new_unchecked(self, seal) + } +} diff --git a/crates/op-consensus/src/signed.rs b/crates/op-consensus/src/signed.rs new file mode 100644 index 00000000..4ef9a8db --- /dev/null +++ b/crates/op-consensus/src/signed.rs @@ -0,0 +1,63 @@ +use crate::transaction::SignableTransaction; +use alloy_primitives::{Signature, B256}; + +/// A transaction with a signature and hash seal. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Signed { + #[cfg_attr(feature = "serde", serde(flatten))] + tx: T, + #[cfg_attr(feature = "serde", serde(flatten))] + signature: Sig, + hash: B256, +} + +impl Signed { + /// Returns a reference to the transaction. + pub const fn tx(&self) -> &T { + &self.tx + } + + /// Returns a reference to the signature. + pub const fn signature(&self) -> &Sig { + &self.signature + } + + /// Returns a reference to the transaction hash. + pub const fn hash(&self) -> &B256 { + &self.hash + } + + /// Splits the transaction into parts. + pub fn into_parts(self) -> (T, Sig, B256) { + (self.tx, self.signature, self.hash) + } + + /// Returns the transaction without signature. + pub fn strip_signature(self) -> T { + self.tx + } +} + +impl, Sig> Signed { + /// Instantiate from a transaction and signature. Does not verify the signature. + pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self { + Self { tx, signature, hash } + } + + /// Calculate the signing hash for the transaction. + pub fn signature_hash(&self) -> B256 { + self.tx.signature_hash() + } +} + +#[cfg(feature = "k256")] +impl> Signed { + /// Recover the signer of the transaction + pub fn recover_signer( + &self, + ) -> Result { + let sighash = self.tx.signature_hash(); + self.signature.recover_address_from_prehash(&sighash) + } +} diff --git a/crates/op-consensus/src/transaction/eip1559.rs b/crates/op-consensus/src/transaction/eip1559.rs new file mode 100644 index 00000000..e96be9d1 --- /dev/null +++ b/crates/op-consensus/src/transaction/eip1559.rs @@ -0,0 +1,409 @@ +use crate::{SignableTransaction, Signed, Transaction, TxType}; +use alloy_eips::eip2930::AccessList; +use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; +use alloy_rlp::{BufMut, Decodable, Encodable, Header}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)). +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxEip1559 { + /// EIP-155: Simple replay attack protection + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub chain_id: ChainId, + /// A scalar value equal to the number of transactions sent by the sender; formally Tn. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub nonce: u64, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_limit: u128, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + /// + /// This is also known as `GasFeeCap` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub max_fee_per_gas: u128, + /// Max Priority fee that transaction is paying + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + /// + /// This is also known as `GasTipCap` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub max_priority_fee_per_gas: u128, + /// The 160-bit address of the message call’s recipient or, for a contract creation + /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. + #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] + pub to: TxKind, + /// A scalar value equal to the number of Wei to + /// be transferred to the message call’s recipient or, + /// in the case of contract creation, as an endowment + /// to the newly created account; formally Tv. + pub value: U256, + /// The accessList specifies a list of addresses and storage keys; + /// these addresses and storage keys are added into the `accessed_addresses` + /// and `accessed_storage_keys` global sets (introduced in EIP-2929). + /// A gas cost is charged, though at a discount relative to the cost of + /// accessing outside the list. + pub access_list: AccessList, + /// Input has two uses depending if transaction is Create or Call (if `to` field is None or + /// Some). pub init: An unlimited size byte array specifying the + /// EVM-code for the account initialisation procedure CREATE, + /// data: An unlimited size byte array specifying the + /// input data of the message call, formally Td. + pub input: Bytes, +} + +impl TxEip1559 { + /// Returns the effective gas price for the given `base_fee`. + pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } + } + + /// Decodes the inner [TxEip1559] fields from RLP bytes. + /// + /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following + /// RLP fields in the following order: + /// + /// - `chain_id` + /// - `nonce` + /// - `max_priority_fee_per_gas` + /// - `max_fee_per_gas` + /// - `gas_limit` + /// - `to` + /// - `value` + /// - `data` (`input`) + /// - `access_list` + pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + chain_id: Decodable::decode(buf)?, + nonce: Decodable::decode(buf)?, + max_priority_fee_per_gas: Decodable::decode(buf)?, + max_fee_per_gas: Decodable::decode(buf)?, + gas_limit: Decodable::decode(buf)?, + to: Decodable::decode(buf)?, + value: Decodable::decode(buf)?, + input: Decodable::decode(buf)?, + access_list: Decodable::decode(buf)?, + }) + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + pub(crate) fn fields_len(&self) -> usize { + let mut len = 0; + len += self.chain_id.length(); + len += self.nonce.length(); + len += self.max_priority_fee_per_gas.length(); + len += self.max_fee_per_gas.length(); + len += self.gas_limit.length(); + len += self.to.length(); + len += self.value.length(); + len += self.input.0.length(); + len += self.access_list.length(); + len + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + pub(crate) fn encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) { + self.chain_id.encode(out); + self.nonce.encode(out); + self.max_priority_fee_per_gas.encode(out); + self.max_fee_per_gas.encode(out); + self.gas_limit.encode(out); + self.to.encode(out); + self.value.encode(out); + self.input.0.encode(out); + self.access_list.encode(out); + } + + /// Returns what the encoded length should be, if the transaction were RLP encoded with the + /// given signature, depending on the value of `with_header`. + /// + /// If `with_header` is `true`, the payload length will include the RLP header length. + /// If `with_header` is `false`, the payload length will not include the RLP header length. + pub(crate) fn encoded_len_with_signature( + &self, + signature: &Signature, + with_header: bool, + ) -> usize { + // this counts the tx fields and signature fields + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + + // this counts: + // * tx type byte + // * inner header length + // * inner payload length + let inner_payload_length = + 1 + Header { list: true, payload_length }.length() + payload_length; + + if with_header { + // header length plus length of the above, wrapped with a string header + Header { list: false, payload_length: inner_payload_length }.length() + + inner_payload_length + } else { + inner_payload_length + } + } + + /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating + /// hash that for eip2718 does not require a rlp header + pub(crate) fn encode_with_signature( + &self, + signature: &Signature, + out: &mut dyn BufMut, + with_header: bool, + ) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + if with_header { + Header { + list: false, + payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, + } + .encode(out); + } + out.put_u8(self.tx_type() as u8); + self.encode_with_signature_fields(signature, out); + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + let tx = Self::decode_fields(buf)?; + let signature = Signature::decode_rlp_vrs(buf)?; + + let signed = tx.into_signed(signature); + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + Ok(signed) + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + let header = Header { list: true, payload_length }; + header.encode(out); + self.encode_fields(out); + signature.write_rlp_vrs(out); + } + + /// Get transaction type + pub(crate) const fn tx_type(&self) -> TxType { + TxType::Eip1559 + } + + /// Calculates a heuristic for the in-memory size of the [TxEip1559] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // chain_id + mem::size_of::() + // nonce + mem::size_of::() + // gas_limit + mem::size_of::() + // max_fee_per_gas + mem::size_of::() + // max_priority_fee_per_gas + self.to.size() + // to + mem::size_of::() + // value + self.access_list.size() + // access_list + self.input.len() // input + } +} + +impl Transaction for TxEip1559 { + fn input(&self) -> &[u8] { + &self.input + } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn chain_id(&self) -> Option { + Some(self.chain_id) + } + + fn nonce(&self) -> u64 { + self.nonce + } + + fn gas_limit(&self) -> u128 { + self.gas_limit + } + + fn gas_price(&self) -> Option { + None + } +} + +impl SignableTransaction for TxEip1559 { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.chain_id = chain_id; + } + + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { + out.put_u8(self.tx_type() as u8); + self.encode(out) + } + + fn payload_len_for_signature(&self) -> usize { + self.length() + 1 + } + + fn into_signed(self, signature: Signature) -> Signed { + let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); + self.encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-1559 transaction. V should indicate the y-parity of the + // signature. + Signed::new_unchecked(self, signature.with_parity_bool(), hash) + } +} + +impl Encodable for TxEip1559 { + fn encode(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.fields_len(); + Header { list: true, payload_length }.length() + payload_length + } +} + +impl Decodable for TxEip1559 { + fn decode(data: &mut &[u8]) -> alloy_rlp::Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + if header.payload_length > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + Self::decode_fields(data) + } +} + +#[cfg(all(test, feature = "k256"))] +mod tests { + use super::TxEip1559; + use crate::SignableTransaction; + use alloy_eips::eip2930::AccessList; + use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256}; + + #[test] + fn recover_signer_eip1559() { + let signer: Address = address!("dd6b8b3dc6b7ad97db52f08a275ff4483e024cea"); + let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); + + let tx = TxEip1559 { + chain_id: 1, + nonce: 0x42, + gas_limit: 44386, + to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), + value: U256::from(0_u64), + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + access_list: AccessList::default(), + }; + + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + + assert_eq!( + tx.signature_hash(), + hex!("0d5688ac3897124635b6cf1bc0e29d6dfebceebdc10a54d74f2ef8b56535b682") + ); + + let signed_tx = tx.into_signed(sig); + assert_eq!(*signed_tx.hash(), hash, "Expected same hash"); + assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass."); + } + + #[test] + fn encode_decode_eip1559() { + let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); + + let tx = TxEip1559 { + chain_id: 1, + nonce: 0x42, + gas_limit: 44386, + to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), + value: U256::from(0_u64), + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + access_list: AccessList::default(), + }; + + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + + let mut buf = vec![]; + tx.encode_with_signature_fields(&sig, &mut buf); + let decoded = TxEip1559::decode_signed_fields(&mut &buf[..]).unwrap(); + assert_eq!(decoded, tx.into_signed(sig)); + assert_eq!(*decoded.hash(), hash); + } +} diff --git a/crates/op-consensus/src/transaction/eip2930.rs b/crates/op-consensus/src/transaction/eip2930.rs new file mode 100644 index 00000000..60303662 --- /dev/null +++ b/crates/op-consensus/src/transaction/eip2930.rs @@ -0,0 +1,366 @@ +use crate::{SignableTransaction, Signed, Transaction, TxType}; +use alloy_eips::eip2930::AccessList; +use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; +use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)). +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxEip2930 { + /// Added as EIP-pub 155: Simple replay attack protection + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub chain_id: ChainId, + /// A scalar value equal to the number of transactions sent by the sender; formally Tn. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub nonce: u64, + /// A scalar value equal to the number of + /// Wei to be paid per unit of gas for all computation + /// costs incurred as a result of the execution of this transaction; formally Tp. + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_price: u128, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_limit: u128, + /// The 160-bit address of the message call’s recipient or, for a contract creation + /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. + #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] + pub to: TxKind, + /// A scalar value equal to the number of Wei to + /// be transferred to the message call’s recipient or, + /// in the case of contract creation, as an endowment + /// to the newly created account; formally Tv. + pub value: U256, + /// The accessList specifies a list of addresses and storage keys; + /// these addresses and storage keys are added into the `accessed_addresses` + /// and `accessed_storage_keys` global sets (introduced in EIP-2929). + /// A gas cost is charged, though at a discount relative to the cost of + /// accessing outside the list. + pub access_list: AccessList, + /// Input has two uses depending if transaction is Create or Call (if `to` field is None or + /// Some). pub init: An unlimited size byte array specifying the + /// EVM-code for the account initialisation procedure CREATE, + /// data: An unlimited size byte array specifying the + /// input data of the message call, formally Td. + pub input: Bytes, +} + +impl TxEip2930 { + /// Calculates a heuristic for the in-memory size of the [TxEip2930] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // chain_id + mem::size_of::() + // nonce + mem::size_of::() + // gas_price + mem::size_of::() + // gas_limit + self.to.size() + // to + mem::size_of::() + // value + self.access_list.size() + // access_list + self.input.len() // input + } + + /// Decodes the inner [TxEip2930] fields from RLP bytes. + /// + /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following + /// RLP fields in the following order: + /// + /// - `chain_id` + /// - `nonce` + /// - `gas_price` + /// - `gas_limit` + /// - `to` + /// - `value` + /// - `data` (`input`) + /// - `access_list` + pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + chain_id: Decodable::decode(buf)?, + nonce: Decodable::decode(buf)?, + gas_price: Decodable::decode(buf)?, + gas_limit: Decodable::decode(buf)?, + to: Decodable::decode(buf)?, + value: Decodable::decode(buf)?, + input: Decodable::decode(buf)?, + access_list: Decodable::decode(buf)?, + }) + } + + /// Outputs the length of the transaction's fields, without a RLP header. + pub(crate) fn fields_len(&self) -> usize { + let mut len = 0; + len += self.chain_id.length(); + len += self.nonce.length(); + len += self.gas_price.length(); + len += self.gas_limit.length(); + len += self.to.length(); + len += self.value.length(); + len += self.input.0.length(); + len += self.access_list.length(); + len + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { + self.chain_id.encode(out); + self.nonce.encode(out); + self.gas_price.encode(out); + self.gas_limit.encode(out); + self.to.encode(out); + self.value.encode(out); + self.input.0.encode(out); + self.access_list.encode(out); + } + + /// Returns what the encoded length should be, if the transaction were RLP encoded with the + /// given signature, depending on the value of `with_header`. + /// + /// If `with_header` is `true`, the payload length will include the RLP header length. + /// If `with_header` is `false`, the payload length will not include the RLP header length. + pub(crate) fn encoded_len_with_signature( + &self, + signature: &Signature, + with_header: bool, + ) -> usize { + // this counts the tx fields and signature fields + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + + // this counts: + // * tx type byte + // * inner header length + // * inner payload length + let inner_payload_length = + 1 + Header { list: true, payload_length }.length() + payload_length; + + if with_header { + // header length plus length of the above, wrapped with a string header + Header { list: false, payload_length: inner_payload_length }.length() + + inner_payload_length + } else { + inner_payload_length + } + } + + /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating + /// hash that for eip2718 does not require a rlp header + pub(crate) fn encode_with_signature( + &self, + signature: &Signature, + out: &mut dyn BufMut, + with_header: bool, + ) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + if with_header { + Header { + list: false, + payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, + } + .encode(out); + } + out.put_u8(self.tx_type() as u8); + self.encode_with_signature_fields(signature, out); + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + let header = Header { list: true, payload_length }; + header.encode(out); + self.encode_fields(out); + signature.write_rlp_vrs(out); + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + let tx = Self::decode_fields(buf)?; + let signature = Signature::decode_rlp_vrs(buf)?; + + let signed = tx.into_signed(signature); + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + Ok(signed) + } + + /// Get transaction type. + pub const fn tx_type(&self) -> TxType { + TxType::Eip2930 + } +} + +impl Transaction for TxEip2930 { + fn input(&self) -> &[u8] { + &self.input + } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn chain_id(&self) -> Option { + Some(self.chain_id) + } + + fn nonce(&self) -> u64 { + self.nonce + } + + fn gas_limit(&self) -> u128 { + self.gas_limit + } + + fn gas_price(&self) -> Option { + Some(self.gas_price) + } +} + +impl SignableTransaction for TxEip2930 { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.chain_id = chain_id; + } + + fn encode_for_signing(&self, out: &mut dyn BufMut) { + out.put_u8(self.tx_type() as u8); + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn payload_len_for_signature(&self) -> usize { + let payload_length = self.fields_len(); + // 'transaction type byte length' + 'header length' + 'payload length' + 1 + Header { list: true, payload_length }.length() + payload_length + } + + fn into_signed(self, signature: Signature) -> Signed { + let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); + self.encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-2930 transaction. V should indicate the y-parity of the + // signature. + Signed::new_unchecked(self, signature.with_parity_bool(), hash) + } +} + +impl Encodable for TxEip2930 { + fn encode(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.fields_len(); + length_of_length(payload_length) + payload_length + } +} + +impl Decodable for TxEip2930 { + fn decode(data: &mut &[u8]) -> alloy_rlp::Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + if header.payload_length > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + Self::decode_fields(data) + } +} + +#[cfg(test)] +mod tests { + use super::TxEip2930; + use crate::{SignableTransaction, TxEnvelope}; + use alloy_primitives::{Address, Bytes, Signature, TxKind, U256}; + use alloy_rlp::{Decodable, Encodable}; + + #[test] + fn test_decode_create() { + // tests that a contract creation tx encodes and decodes properly + let tx = TxEip2930 { + chain_id: 1u64, + nonce: 0, + gas_price: 1, + gas_limit: 2, + to: TxKind::Create, + value: U256::from(3_u64), + input: Bytes::from(vec![1, 2]), + access_list: Default::default(), + }; + let signature = Signature::test_signature(); + + let mut encoded = Vec::new(); + tx.encode_with_signature_fields(&signature, &mut encoded); + + let decoded = TxEip2930::decode_signed_fields(&mut &*encoded).unwrap(); + assert_eq!(decoded, tx.into_signed(signature)); + } + + #[test] + fn test_decode_call() { + let request = TxEip2930 { + chain_id: 1u64, + nonce: 0, + gas_price: 1, + gas_limit: 2, + to: TxKind::Call(Address::default()), + value: U256::from(3_u64), + input: Bytes::from(vec![1, 2]), + access_list: Default::default(), + }; + + let signature = Signature::test_signature(); + + let tx = request.into_signed(signature); + + let envelope = TxEnvelope::Eip2930(tx); + + let mut encoded = Vec::new(); + envelope.encode(&mut encoded); + assert_eq!(encoded.len(), envelope.length()); + + assert_eq!( + alloy_primitives::hex::encode(&encoded), + "b86401f8610180010294000000000000000000000000000000000000000003820102c080a0840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565a025e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1" + ); + + let decoded = TxEnvelope::decode(&mut encoded.as_ref()).unwrap(); + assert_eq!(decoded, envelope); + } +} diff --git a/crates/op-consensus/src/transaction/eip4844.rs b/crates/op-consensus/src/transaction/eip4844.rs new file mode 100644 index 00000000..4f259e6c --- /dev/null +++ b/crates/op-consensus/src/transaction/eip4844.rs @@ -0,0 +1,1168 @@ +mod builder; +pub use builder::{SidecarBuilder, SidecarCoder, SimpleCoder}; + +pub mod utils; + +use crate::{SignableTransaction, Signed, Transaction, TxType}; + +use alloy_eips::{ + eip2930::AccessList, + eip4844::{BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, DATA_GAS_PER_BLOB}, +}; +use alloy_primitives::{ + keccak256, Address, Bytes, ChainId, FixedBytes, Signature, TxKind, B256, U256, +}; +use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; +use core::mem; +use sha2::{Digest, Sha256}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +#[cfg(not(feature = "kzg"))] +pub use alloy_eips::eip4844::{Blob, Bytes48}; + +#[cfg(feature = "kzg")] +use c_kzg::{KzgCommitment, KzgProof, KzgSettings}; +#[cfg(feature = "kzg")] +use std::ops::Deref; + +#[cfg(feature = "kzg")] +pub use c_kzg::{Blob, Bytes48}; + +/// An error that can occur when validating a [TxEip4844Variant]. +#[derive(Debug, thiserror::Error)] +#[cfg(feature = "kzg")] +pub enum BlobTransactionValidationError { + /// Proof validation failed. + #[error("invalid KZG proof")] + InvalidProof, + /// An error returned by [`c_kzg`]. + #[error("KZG error: {0:?}")] + KZGError(#[from] c_kzg::Error), + /// The inner transaction is not a blob transaction. + #[error("unable to verify proof for non blob transaction: {0}")] + NotBlobTransaction(u8), + /// Using a standalone [TxEip4844] instead of the [TxEip4844WithSidecar] variant, which + /// includes the sidecar for validation. + #[error("eip4844 tx variant without sidecar being used for verification. Please use the TxEip4844WithSidecar variant, which includes the sidecar")] + MissingSidecar, + /// The versioned hash is incorrect. + #[error("wrong versioned hash: have {have}, expected {expected}")] + WrongVersionedHash { + /// The versioned hash we got + have: B256, + /// The versioned hash we expected + expected: B256, + }, +} + +/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) +/// +/// A transaction with blob hashes and max blob fee. +/// It can either be a standalone transaction, mainly seen when retrieving historical transactions, +/// or a transaction with a sidecar, which is used when submitting a transaction to the network and +/// when receiving and sending transactions during the gossip stage. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", serde(untagged))] +pub enum TxEip4844Variant { + /// A standalone transaction with blob hashes and max blob fee. + TxEip4844(TxEip4844), + /// A transaction with a sidecar, which contains the blob data, commitments, and proofs. + TxEip4844WithSidecar(TxEip4844WithSidecar), +} + +#[cfg(feature = "serde")] +impl<'de> serde::Deserialize<'de> for TxEip4844Variant { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + #[derive(serde::Deserialize)] + struct TxEip4844SerdeHelper { + #[serde(flatten)] + tx: TxEip4844, + #[serde(flatten)] + sidecar: Option, + } + + let tx = TxEip4844SerdeHelper::deserialize(deserializer)?; + + if let Some(sidecar) = tx.sidecar { + Ok(TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar::from_tx_and_sidecar( + tx.tx, sidecar, + ))) + } else { + Ok(TxEip4844Variant::TxEip4844(tx.tx)) + } + } +} + +impl From for TxEip4844Variant { + fn from(tx: TxEip4844WithSidecar) -> Self { + TxEip4844Variant::TxEip4844WithSidecar(tx) + } +} + +impl From for TxEip4844Variant { + fn from(tx: TxEip4844) -> Self { + TxEip4844Variant::TxEip4844(tx) + } +} + +impl From<(TxEip4844, BlobTransactionSidecar)> for TxEip4844Variant { + fn from((tx, sidecar): (TxEip4844, BlobTransactionSidecar)) -> Self { + TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar::from_tx_and_sidecar( + tx, sidecar, + )) + } +} + +impl TxEip4844Variant { + /// Verifies that the transaction's blob data, commitments, and proofs are all valid. + /// + /// See also [TxEip4844::validate_blob] + #[cfg(feature = "kzg")] + pub fn validate( + &self, + proof_settings: &KzgSettings, + ) -> Result<(), BlobTransactionValidationError> { + match self { + TxEip4844Variant::TxEip4844(_) => Err(BlobTransactionValidationError::MissingSidecar), + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.validate_blob(proof_settings), + } + } + + /// Get the transaction type. + pub const fn tx_type(&self) -> TxType { + TxType::Eip4844 + } + + /// Get access to the inner tx [TxEip4844]. + pub const fn tx(&self) -> &TxEip4844 { + match self { + TxEip4844Variant::TxEip4844(tx) => tx, + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx(), + } + } + + pub(crate) fn fields_len(&self) -> usize { + match self { + TxEip4844Variant::TxEip4844(tx) => tx.fields_len(), + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().fields_len(), + } + } + + /// Encodes the [TxEip4844Variant] fields as RLP, with a tx type. If `with_header` is `false`, + /// the following will be encoded: + /// `tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs])` + /// + /// If `with_header` is `true`, the following will be encoded: + /// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))` + pub(crate) fn encode_with_signature( + &self, + signature: &Signature, + out: &mut dyn BufMut, + with_header: bool, + ) { + let payload_length = match self { + TxEip4844Variant::TxEip4844(tx) => tx.fields_len() + signature.rlp_vrs_len(), + TxEip4844Variant::TxEip4844WithSidecar(tx) => { + let payload_length = tx.tx().fields_len() + signature.rlp_vrs_len(); + let inner_header = Header { list: true, payload_length }; + inner_header.length() + payload_length + tx.sidecar().fields_len() + } + }; + + if with_header { + Header { + list: false, + payload_length: 1 + + Header { list: false, payload_length }.length() + + payload_length, + } + .encode(out); + } + out.put_u8(self.tx_type() as u8); + + match self { + TxEip4844Variant::TxEip4844(tx) => { + tx.encode_with_signature_fields(signature, out); + } + TxEip4844Variant::TxEip4844WithSidecar(tx) => { + tx.encode_with_signature_fields(signature, out); + } + } + } + + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let mut current_buf = *buf; + let _header = Header::decode(&mut current_buf)?; + + // There are two possibilities when decoding a signed EIP-4844 transaction: + // If it's a historical transaction, it will only have the transaction fields, and no + // sidecar. If it's a transaction received during the gossip stage or sent through + // eth_sendRawTransaction, it will have the transaction fields and a sidecar. + // + // To disambiguate, we try to decode two list headers. If there is only one list header, we + // assume it's a historical transaction. If there are two, we know the transaction contains + // a sidecar. + let header = Header::decode(&mut current_buf)?; + if header.list { + let tx = TxEip4844WithSidecar::decode_signed_fields(buf)?; + let (tx, signature, hash) = tx.into_parts(); + return Ok(Signed::new_unchecked( + TxEip4844Variant::TxEip4844WithSidecar(tx), + signature, + hash, + )); + } + + // Since there is not a second list header, this is a historical 4844 transaction without a + // sidecar. + let tx = TxEip4844::decode_signed_fields(buf)?; + let (tx, signature, hash) = tx.into_parts(); + Ok(Signed::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)) + } +} + +impl Transaction for TxEip4844Variant { + fn chain_id(&self) -> Option { + match self { + TxEip4844Variant::TxEip4844(tx) => Some(tx.chain_id), + TxEip4844Variant::TxEip4844WithSidecar(tx) => Some(tx.tx().chain_id), + } + } + + fn gas_limit(&self) -> u128 { + match self { + TxEip4844Variant::TxEip4844(tx) => tx.gas_limit, + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().gas_limit, + } + } + + fn gas_price(&self) -> Option { + None + } + + fn input(&self) -> &[u8] { + match self { + TxEip4844Variant::TxEip4844(tx) => tx.input.as_ref(), + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().input.as_ref(), + } + } + + fn nonce(&self) -> u64 { + match self { + TxEip4844Variant::TxEip4844(tx) => tx.nonce, + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().nonce, + } + } + + fn to(&self) -> TxKind { + let address = match self { + TxEip4844Variant::TxEip4844(tx) => tx.to, + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx.to, + }; + TxKind::Call(address) + } + + fn value(&self) -> U256 { + match self { + TxEip4844Variant::TxEip4844(tx) => tx.value, + TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx.value, + } + } +} + +impl SignableTransaction for TxEip4844Variant { + fn set_chain_id(&mut self, chain_id: ChainId) { + match self { + TxEip4844Variant::TxEip4844(ref mut inner) => { + inner.chain_id = chain_id; + } + TxEip4844Variant::TxEip4844WithSidecar(ref mut inner) => { + inner.tx.chain_id = chain_id; + } + } + } + + fn payload_len_for_signature(&self) -> usize { + let payload_length = self.fields_len(); + // 'transaction type byte length' + 'header length' + 'payload length' + 1 + length_of_length(payload_length) + payload_length + } + + fn into_signed(self, signature: Signature) -> Signed { + let payload_length = 1 + self.fields_len() + signature.rlp_vrs_len(); + let mut buf = Vec::with_capacity(payload_length); + buf.put_u8(TxType::Eip4844 as u8); + // we use the inner tx to encode the fields + self.tx().encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-4844 transaction. V should indicate the y-parity of the + // signature. + Signed::new_unchecked(self, signature.with_parity_bool(), hash) + } + + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { + // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844Variant] + // EIP-2718 payload fields: + // (BLOB_TX_TYPE || + // rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value, + // data, access_list, max_fee_per_blob_gas, blob_versioned_hashes])) + self.tx().encode_for_signing(out); + } +} + +/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) +/// +/// A transaction with blob hashes and max blob fee. It does not have the Blob sidecar. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxEip4844 { + /// Added as EIP-pub 155: Simple replay attack protection + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub chain_id: ChainId, + /// A scalar value equal to the number of transactions sent by the sender; formally Tn. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub nonce: u64, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_limit: u128, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + /// + /// This is also known as `GasFeeCap` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub max_fee_per_gas: u128, + /// Max Priority fee that transaction is paying + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + /// + /// This is also known as `GasTipCap` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub max_priority_fee_per_gas: u128, + /// The 160-bit address of the message call’s recipient. + pub to: Address, + /// A scalar value equal to the number of Wei to + /// be transferred to the message call’s recipient or, + /// in the case of contract creation, as an endowment + /// to the newly created account; formally Tv. + pub value: U256, + /// The accessList specifies a list of addresses and storage keys; + /// these addresses and storage keys are added into the `accessed_addresses` + /// and `accessed_storage_keys` global sets (introduced in EIP-2929). + /// A gas cost is charged, though at a discount relative to the cost of + /// accessing outside the list. + pub access_list: AccessList, + + /// It contains a vector of fixed size hash(32 bytes) + pub blob_versioned_hashes: Vec, + + /// Max fee per data gas + /// + /// aka BlobFeeCap or blobGasFeeCap + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub max_fee_per_blob_gas: u128, + + /// Input has two uses depending if transaction is Create or Call (if `to` field is None or + /// Some). pub init: An unlimited size byte array specifying the + /// EVM-code for the account initialisation procedure CREATE, + /// data: An unlimited size byte array specifying the + /// input data of the message call, formally Td. + pub input: Bytes, +} + +impl TxEip4844 { + /// Returns the effective gas price for the given `base_fee`. + pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } + } + + /// Returns the total gas for all blobs in this transaction. + #[inline] + pub fn blob_gas(&self) -> u64 { + // SAFETY: we don't expect u64::MAX / DATA_GAS_PER_BLOB hashes in a single transaction + self.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB + } + + /// Verifies that the given blob data, commitments, and proofs are all valid for this + /// transaction. + /// + /// Takes as input the [KzgSettings], which should contain the parameters derived from the + /// KZG trusted setup. + /// + /// This ensures that the blob transaction payload has the same number of blob data elements, + /// commitments, and proofs. Each blob data element is verified against its commitment and + /// proof. + /// + /// Returns [BlobTransactionValidationError::InvalidProof] if any blob KZG proof in the response + /// fails to verify, or if the versioned hashes in the transaction do not match the actual + /// commitment versioned hashes. + #[cfg(feature = "kzg")] + pub fn validate_blob( + &self, + sidecar: &BlobTransactionSidecar, + proof_settings: &KzgSettings, + ) -> Result<(), BlobTransactionValidationError> { + // Ensure the versioned hashes and commitments have the same length. + if self.blob_versioned_hashes.len() != sidecar.commitments.len() { + return Err(c_kzg::Error::MismatchLength(format!( + "There are {} versioned commitment hashes and {} commitments", + self.blob_versioned_hashes.len(), + sidecar.commitments.len() + )) + .into()); + } + + // calculate versioned hashes by zipping & iterating + for (versioned_hash, commitment) in + self.blob_versioned_hashes.iter().zip(sidecar.commitments.iter()) + { + let commitment = KzgCommitment::from(*commitment.deref()); + + // calculate & verify versioned hash + let calculated_versioned_hash = kzg_to_versioned_hash(commitment.as_slice()); + if *versioned_hash != calculated_versioned_hash { + return Err(BlobTransactionValidationError::WrongVersionedHash { + have: *versioned_hash, + expected: calculated_versioned_hash, + }); + } + } + + let res = KzgProof::verify_blob_kzg_proof_batch( + sidecar.blobs.as_slice(), + sidecar.commitments.as_slice(), + sidecar.proofs.as_slice(), + proof_settings, + ) + .map_err(BlobTransactionValidationError::KZGError)?; + + if res { + Ok(()) + } else { + Err(BlobTransactionValidationError::InvalidProof) + } + } + + /// Decodes the inner [TxEip4844Variant] fields from RLP bytes. + /// + /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following + /// RLP fields in the following order: + /// + /// - `chain_id` + /// - `nonce` + /// - `max_priority_fee_per_gas` + /// - `max_fee_per_gas` + /// - `gas_limit` + /// - `to` + /// - `value` + /// - `data` (`input`) + /// - `access_list` + /// - `max_fee_per_blob_gas` + /// - `blob_versioned_hashes` + pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + chain_id: Decodable::decode(buf)?, + nonce: Decodable::decode(buf)?, + max_priority_fee_per_gas: Decodable::decode(buf)?, + max_fee_per_gas: Decodable::decode(buf)?, + gas_limit: Decodable::decode(buf)?, + to: Decodable::decode(buf)?, + value: Decodable::decode(buf)?, + input: Decodable::decode(buf)?, + access_list: Decodable::decode(buf)?, + max_fee_per_blob_gas: Decodable::decode(buf)?, + blob_versioned_hashes: Decodable::decode(buf)?, + }) + } + + /// Outputs the length of the transaction's fields, without a RLP header. + pub(crate) fn fields_len(&self) -> usize { + let mut len = 0; + len += self.chain_id.length(); + len += self.nonce.length(); + len += self.gas_limit.length(); + len += self.max_fee_per_gas.length(); + len += self.max_priority_fee_per_gas.length(); + len += self.to.length(); + len += self.value.length(); + len += self.access_list.length(); + len += self.blob_versioned_hashes.length(); + len += self.max_fee_per_blob_gas.length(); + len += self.input.0.length(); + len + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { + self.chain_id.encode(out); + self.nonce.encode(out); + self.max_priority_fee_per_gas.encode(out); + self.max_fee_per_gas.encode(out); + self.gas_limit.encode(out); + self.to.encode(out); + self.value.encode(out); + self.input.0.encode(out); + self.access_list.encode(out); + self.max_fee_per_blob_gas.encode(out); + self.blob_versioned_hashes.encode(out); + } + + /// Calculates a heuristic for the in-memory size of the [TxEip4844Variant] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // chain_id + mem::size_of::() + // nonce + mem::size_of::() + // gas_limit + mem::size_of::() + // max_fee_per_gas + mem::size_of::() + // max_priority_fee_per_gas + mem::size_of::
() + // to + mem::size_of::() + // value + self.access_list.size() + // access_list + self.input.len() + // input + self.blob_versioned_hashes.capacity() * mem::size_of::() + // blob hashes size + mem::size_of::() // max_fee_per_data_gas + } + + /// Returns what the encoded length should be, if the transaction were RLP encoded with the + /// given signature, depending on the value of `with_header`. + /// + /// If `with_header` is `true`, the payload length will include the RLP header length. + /// If `with_header` is `false`, the payload length will not include the RLP header length. + pub(crate) fn encoded_len_with_signature( + &self, + signature: &Signature, + with_header: bool, + ) -> usize { + // this counts the tx fields and signature fields + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + + // this counts: + // * tx type byte + // * inner header length + // * inner payload length + let inner_payload_length = + 1 + Header { list: true, payload_length }.length() + payload_length; + + if with_header { + // header length plus length of the above, wrapped with a string header + Header { list: false, payload_length: inner_payload_length }.length() + + inner_payload_length + } else { + inner_payload_length + } + } + + /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating + /// hash that for eip2718 does not require a rlp header + pub(crate) fn encode_with_signature( + &self, + signature: &Signature, + out: &mut dyn BufMut, + with_header: bool, + ) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + if with_header { + Header { + list: false, + payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, + } + .encode(out); + } + out.put_u8(self.tx_type() as u8); + self.encode_with_signature_fields(signature, out); + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + let header = Header { list: true, payload_length }; + header.encode(out); + self.encode_fields(out); + signature.write_rlp_vrs(out); + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + let tx = Self::decode_fields(buf)?; + let signature = Signature::decode_rlp_vrs(buf)?; + + let signed = tx.into_signed(signature); + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + Ok(signed) + } + + /// Get transaction type + pub const fn tx_type(&self) -> TxType { + TxType::Eip4844 + } + + /// Encodes the EIP-4844 transaction in RLP for signing. + /// + /// This encodes the transaction as: + /// `tx_type || rlp(chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, + /// value, input, access_list, max_fee_per_blob_gas, blob_versioned_hashes)` + /// + /// Note that there is no rlp header before the transaction type byte. + pub fn encode_for_signing(&self, out: &mut dyn BufMut) { + out.put_u8(self.tx_type() as u8); + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + /// Outputs the length of the signature RLP encoding for the transaction. + pub fn payload_len_for_signature(&self) -> usize { + let payload_length = self.fields_len(); + // 'transaction type byte length' + 'header length' + 'payload length' + 1 + Header { list: true, payload_length }.length() + payload_length + } +} + +impl SignableTransaction for TxEip4844 { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.chain_id = chain_id; + } + + fn payload_len_for_signature(&self) -> usize { + self.payload_len_for_signature() + } + + fn into_signed(self, signature: Signature) -> Signed { + let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); + self.encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-4844 transaction. V should indicate the y-parity of the + // signature. + Signed::new_unchecked(self, signature.with_parity_bool(), hash) + } + + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { + self.encode_for_signing(out); + } +} + +impl Transaction for TxEip4844 { + fn input(&self) -> &[u8] { + &self.input + } + + fn to(&self) -> TxKind { + TxKind::Call(self.to) + } + + fn value(&self) -> U256 { + self.value + } + + fn chain_id(&self) -> Option { + Some(self.chain_id) + } + + fn nonce(&self) -> u64 { + self.nonce + } + + fn gas_limit(&self) -> u128 { + self.gas_limit + } + + fn gas_price(&self) -> Option { + None + } +} + +impl Encodable for TxEip4844 { + fn encode(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.fields_len(); + length_of_length(payload_length) + payload_length + } +} + +impl Decodable for TxEip4844 { + fn decode(data: &mut &[u8]) -> alloy_rlp::Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + if header.payload_length > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + Self::decode_fields(data) + } +} + +/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) +/// +/// A transaction with blob hashes and max blob fee, which also includes the +/// [BlobTransactionSidecar]. This is the full type sent over the network as a raw transaction. It +/// wraps a [TxEip4844] to include the sidecar and the ability to decode it properly. +/// +/// This is defined in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#networking) as an element +/// of a `PooledTransactions` response, and is also used as the format for sending raw transactions +/// through the network (eth_sendRawTransaction/eth_sendTransaction). +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxEip4844WithSidecar { + /// The actual transaction. + #[cfg_attr(feature = "serde", serde(flatten))] + pub tx: TxEip4844, + /// The sidecar. + #[cfg_attr(feature = "serde", serde(flatten))] + pub sidecar: BlobTransactionSidecar, +} + +impl TxEip4844WithSidecar { + /// Constructs a new [TxEip4844WithSidecar] from a [TxEip4844] and a [BlobTransactionSidecar]. + pub const fn from_tx_and_sidecar(tx: TxEip4844, sidecar: BlobTransactionSidecar) -> Self { + Self { tx, sidecar } + } + + /// Verifies that the transaction's blob data, commitments, and proofs are all valid. + /// + /// See also [TxEip4844::validate_blob] + #[cfg(feature = "kzg")] + pub fn validate_blob( + &self, + proof_settings: &KzgSettings, + ) -> Result<(), BlobTransactionValidationError> { + self.tx.validate_blob(&self.sidecar, proof_settings) + } + + /// Get the transaction type. + pub const fn tx_type(&self) -> TxType { + self.tx.tx_type() + } + + /// Get access to the inner tx [TxEip4844]. + pub const fn tx(&self) -> &TxEip4844 { + &self.tx + } + + /// Get access to the inner sidecar [BlobTransactionSidecar]. + pub const fn sidecar(&self) -> &BlobTransactionSidecar { + &self.sidecar + } + + /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844]. + pub fn into_tx(self) -> TxEip4844 { + self.tx + } + + /// Consumes the [TxEip4844WithSidecar] and returns the inner sidecar [BlobTransactionSidecar]. + pub fn into_sidecar(self) -> BlobTransactionSidecar { + self.sidecar + } + + /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844] and + /// [BlobTransactionSidecar]. + pub fn into_parts(self) -> (TxEip4844, BlobTransactionSidecar) { + (self.tx, self.sidecar) + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + /// + /// This encodes the following: + /// `rlp([tx_payload, blobs, commitments, proofs])` + /// + /// where `tx_payload` is the RLP encoding of the [TxEip4844] transaction fields: + /// `rlp([chain_id, nonce, max_priority_fee_per_gas, ..., v, r, s])` + pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + let inner_payload_length = self.tx.fields_len() + signature.rlp_vrs_len(); + let inner_header = Header { list: true, payload_length: inner_payload_length }; + + let outer_payload_length = + inner_header.length() + inner_payload_length + self.sidecar.fields_len(); + let outer_header = Header { list: true, payload_length: outer_payload_length }; + + // write the two headers + outer_header.encode(out); + inner_header.encode(out); + + // now write the fields + self.tx.encode_fields(out); + signature.write_rlp_vrs(out); + self.sidecar.encode_inner(out); + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + /// + /// This is the inverse of [TxEip4844WithSidecar::encode_with_signature_fields]. + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + // decode the inner tx + let inner_tx = TxEip4844::decode_signed_fields(buf)?; + + // decode the sidecar + let sidecar = BlobTransactionSidecar::decode_inner(buf)?; + + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + let (tx, signature, hash) = inner_tx.into_parts(); + + // create unchecked signed tx because these checks should have happened during construction + // of `Signed` in `TxEip4844::decode_signed_fields` + Ok(Signed::new_unchecked( + TxEip4844WithSidecar::from_tx_and_sidecar(tx, sidecar), + signature, + hash, + )) + } +} + +impl SignableTransaction for TxEip4844WithSidecar { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.tx.chain_id = chain_id; + } + + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { + // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844] EIP-2718 + // payload fields: + // (BLOB_TX_TYPE || + // rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value, + // data, access_list, max_fee_per_blob_gas, blob_versioned_hashes])) + self.tx.encode_for_signing(out); + } + + fn into_signed(self, signature: Signature) -> Signed { + let mut buf = Vec::with_capacity(self.tx.encoded_len_with_signature(&signature, false)); + // The sidecar is NOT included in the signed payload, only the transaction fields and the + // type byte. Include the type byte. + // + // Include the transaction fields, making sure to __not__ use the sidecar, and __not__ + // encode a header. + self.tx.encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-4844 transaction. V should indicate the y-parity of the + // signature. + Signed::new_unchecked(self, signature.with_parity_bool(), hash) + } + + fn payload_len_for_signature(&self) -> usize { + // The payload length is the length of the `transaction_payload_body` list. + // The sidecar is NOT included. + self.tx.payload_len_for_signature() + } +} + +impl Transaction for TxEip4844WithSidecar { + fn chain_id(&self) -> Option { + self.tx.chain_id() + } + + fn gas_limit(&self) -> u128 { + self.tx.gas_limit() + } + + fn gas_price(&self) -> Option { + self.tx.gas_price() + } + + fn nonce(&self) -> u64 { + self.tx.nonce() + } + + fn to(&self) -> TxKind { + self.tx.to() + } + + fn value(&self) -> U256 { + self.tx.value() + } + + fn input(&self) -> &[u8] { + self.tx.input() + } +} + +/// This represents a set of blobs, and its corresponding commitments and proofs. +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] +#[repr(C)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BlobTransactionSidecar { + /// The blob data. + pub blobs: Vec, + /// The blob commitments. + pub commitments: Vec, + /// The blob proofs. + pub proofs: Vec, +} + +impl BlobTransactionSidecar { + /// Constructs a new [BlobTransactionSidecar] from a set of blobs, commitments, and proofs. + pub fn new(blobs: Vec, commitments: Vec, proofs: Vec) -> Self { + Self { blobs, commitments, proofs } + } + + /// Returns an iterator over the versioned hashes of the commitments. + pub fn versioned_hashes(&self) -> impl Iterator + '_ { + self.commitments.iter().map(|c| kzg_to_versioned_hash(c.as_slice())) + } + + /// Returns the versioned hash for the blob at the given index, if it + /// exists. + pub fn versioned_hash_for_blob(&self, blob_index: usize) -> Option { + self.commitments.get(blob_index).map(|c| kzg_to_versioned_hash(c.as_slice())) + } + + /// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header. + /// + /// This encodes the fields in the following order: + /// - `blobs` + /// - `commitments` + /// - `proofs` + #[inline] + pub(crate) fn encode_inner(&self, out: &mut dyn BufMut) { + BlobTransactionSidecarRlp::wrap_ref(self).encode(out); + } + + /// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header. + /// + /// This decodes the fields in the following order: + /// - `blobs` + /// - `commitments` + /// - `proofs` + pub(crate) fn decode_inner(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(BlobTransactionSidecarRlp::decode(buf)?.unwrap()) + } + + /// Outputs the RLP length of the [BlobTransactionSidecar] fields, without a RLP header. + pub fn fields_len(&self) -> usize { + BlobTransactionSidecarRlp::wrap_ref(self).fields_len() + } + + /// Calculates a size heuristic for the in-memory size of the [BlobTransactionSidecar]. + #[inline] + pub fn size(&self) -> usize { + self.blobs.len() * BYTES_PER_BLOB + // blobs + self.commitments.len() * BYTES_PER_COMMITMENT + // commitments + self.proofs.len() * BYTES_PER_PROOF // proofs + } +} + +impl Encodable for BlobTransactionSidecar { + /// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header. + fn encode(&self, s: &mut dyn BufMut) { + self.encode_inner(s); + } + + fn length(&self) -> usize { + self.fields_len() + } +} + +impl Decodable for BlobTransactionSidecar { + /// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header. + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::decode_inner(buf) + } +} + +// Wrapper for c-kzg rlp +#[repr(C)] +struct BlobTransactionSidecarRlp { + blobs: Vec>, + commitments: Vec>, + proofs: Vec>, +} + +const _: [(); mem::size_of::()] = + [(); mem::size_of::()]; + +impl BlobTransactionSidecarRlp { + const fn wrap_ref(other: &BlobTransactionSidecar) -> &Self { + // SAFETY: Same repr and size + unsafe { &*(other as *const BlobTransactionSidecar).cast::() } + } + + fn unwrap(self) -> BlobTransactionSidecar { + // SAFETY: Same repr and size + unsafe { mem::transmute(self) } + } + + fn encode(&self, out: &mut dyn BufMut) { + // Encode the blobs, commitments, and proofs + self.blobs.encode(out); + self.commitments.encode(out); + self.proofs.encode(out); + } + + fn fields_len(&self) -> usize { + self.blobs.length() + self.commitments.length() + self.proofs.length() + } + + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + blobs: Decodable::decode(buf)?, + commitments: Decodable::decode(buf)?, + proofs: Decodable::decode(buf)?, + }) + } +} + +/// Calculates the versioned hash for a KzgCommitment +/// +/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension) +pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 { + debug_assert_eq!(commitment.len(), 48, "commitment length is not 48"); + let mut res = Sha256::digest(commitment); + res[0] = alloy_eips::eip4844::VERSIONED_HASH_VERSION_KZG; + B256::new(res.into()) +} + +#[cfg(test)] +mod tests { + use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar}; + use crate::{SignableTransaction, TxEnvelope}; + use alloy_primitives::{Signature, U256}; + use alloy_rlp::{Decodable, Encodable}; + + #[cfg(not(feature = "kzg"))] + use alloy_eips::eip4844::{Blob, Bytes48}; + + #[cfg(feature = "kzg")] + use c_kzg::{Blob, Bytes48}; + + #[test] + fn different_sidecar_same_hash() { + // this should make sure that the hash calculated for the `into_signed` conversion does not + // change if the sidecar is different + let tx = TxEip4844 { + chain_id: 1, + nonce: 1, + max_priority_fee_per_gas: 1, + max_fee_per_gas: 1, + gas_limit: 1, + to: Default::default(), + value: U256::from(1), + access_list: Default::default(), + blob_versioned_hashes: vec![Default::default()], + max_fee_per_blob_gas: 1, + input: Default::default(), + }; + let sidecar = BlobTransactionSidecar { + blobs: vec![Blob::from([2; 131072])], + commitments: vec![Bytes48::from([3; 48])], + proofs: vec![Bytes48::from([4; 48])], + }; + let mut tx = TxEip4844WithSidecar { tx, sidecar }; + let signature = Signature::test_signature(); + + // turn this transaction into_signed + let expected_signed = tx.clone().into_signed(signature); + + // change the sidecar, adding a single (blob, commitment, proof) pair + tx.sidecar = BlobTransactionSidecar { + blobs: vec![Blob::from([1; 131072])], + commitments: vec![Bytes48::from([1; 48])], + proofs: vec![Bytes48::from([1; 48])], + }; + + // turn this transaction into_signed + let actual_signed = tx.into_signed(signature); + + // the hashes should be the same + assert_eq!(expected_signed.hash(), actual_signed.hash()); + + // convert to envelopes + let expected_envelope: TxEnvelope = expected_signed.into(); + let actual_envelope: TxEnvelope = actual_signed.into(); + + // now encode the transaction and check the length + let mut buf = Vec::new(); + expected_envelope.encode(&mut buf); + assert_eq!(buf.len(), expected_envelope.length()); + + // ensure it's also the same size that `actual` claims to be, since we just changed the + // sidecar values. + assert_eq!(buf.len(), actual_envelope.length()); + + // now decode the transaction and check the values + let decoded = TxEnvelope::decode(&mut &buf[..]).unwrap(); + assert_eq!(decoded, expected_envelope); + } +} diff --git a/crates/op-consensus/src/transaction/eip4844/builder.rs b/crates/op-consensus/src/transaction/eip4844/builder.rs new file mode 100644 index 00000000..5b21aedf --- /dev/null +++ b/crates/op-consensus/src/transaction/eip4844/builder.rs @@ -0,0 +1,426 @@ +#[cfg(not(feature = "kzg"))] +use alloy_eips::eip4844::Blob; +#[cfg(feature = "kzg")] +use c_kzg::{Blob, KzgCommitment, KzgProof}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +use super::utils::WholeFe; +use alloy_eips::eip4844::{BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB}; +use core::cmp; + +/// A builder for creating a [`BlobTransactionSidecar`]. +/// +/// [`BlobTransactionSidecar`]: crate::BlobTransactionSidecar +#[derive(Debug, Clone)] +pub struct PartialSidecar { + /// The blobs in the sidecar. + blobs: Vec, + /// The number of field elements that we have ingested, total. + fe: usize, +} + +impl Default for PartialSidecar { + fn default() -> Self { + Self::new() + } +} + +impl PartialSidecar { + /// Create a new builder, and push an empty blob to it. This is the default + /// constructor, and allocates space for 2 blobs (256 KiB). If you want to + /// preallocate a specific number of blobs, use + /// [`PartialSidecar::with_capacity`]. + pub fn new() -> Self { + Self::with_capacity(2) + } + + /// Create a new builder, preallocating room for `capacity` blobs, and push + /// an empty blob to it. + pub fn with_capacity(capacity: usize) -> Self { + let mut blobs = Vec::with_capacity(capacity); + blobs.push(Blob::new([0u8; BYTES_PER_BLOB])); + Self { blobs, fe: 0 } + } + + /// Get a reference to the blobs currently in the builder. + pub fn blobs(&self) -> &[Blob] { + &self.blobs + } + + /// Get the number of unused field elements that have been allocated + fn free_fe(&self) -> usize { + self.blobs.len() * FIELD_ELEMENTS_PER_BLOB as usize - self.fe + } + + /// Calculate the length of used field elements IN BYTES in the builder. + /// + /// This is always strictly greater than the number of bytes that have been + /// ingested. + pub const fn len(&self) -> usize { + self.fe * 32 + } + + /// Check if the builder is empty. + pub const fn is_empty(&self) -> bool { + self.fe == 0 + } + + /// Push an empty blob to the builder. + fn push_empty_blob(&mut self) { + self.blobs.push(Blob::new([0u8; BYTES_PER_BLOB])); + } + + /// Allocate enough space for the required number of new field elements. + pub fn alloc_fes(&mut self, required_fe: usize) { + while self.free_fe() < required_fe { + self.push_empty_blob() + } + } + + /// Get the number of used field elements in the current blob. + const fn fe_in_current_blob(&self) -> usize { + self.fe % FIELD_ELEMENTS_PER_BLOB as usize + } + + /// Get the index of the first unused field element in the current blob. + const fn first_unused_fe_index_in_current_blob(&self) -> usize { + self.fe_in_current_blob() + } + + /// Get a mutable reference to the current blob. + fn current_blob_mut(&mut self) -> &mut Blob { + self.blobs.last_mut().expect("never empty") + } + + /// Get a mutable reference to the field element at the given index, in + /// the current blob. + fn fe_at_mut(&mut self, index: usize) -> &mut [u8] { + &mut self.current_blob_mut()[index * 32..(index + 1) * 32] + } + + /// Get a mutable reference to the next unused field element. + fn next_unused_fe_mut(&mut self) -> &mut [u8] { + self.fe_at_mut(self.first_unused_fe_index_in_current_blob()) + } + + /// Ingest a field element into the current blobs. + pub fn ingest_valid_fe(&mut self, data: WholeFe<'_>) { + self.alloc_fes(1); + self.next_unused_fe_mut().copy_from_slice(data.as_ref()); + self.fe += 1; + } + + /// Ingest a partial FE into the current blobs. + /// + /// # Panics + /// + /// If the data is >=32 bytes. Or if there are not enough free FEs to + /// encode the data. + pub fn ingest_partial_fe(&mut self, data: &[u8]) { + let fe = self.next_unused_fe_mut(); + fe[1..1 + data.len()].copy_from_slice(data); + self.fe += 1; + } +} + +/// A strategy for coding and decoding data into sidecars. Coder instances are +/// responsible for encoding and decoding data into and from the sidecar. They +/// are called by the [`SidecarBuilder`] during the [`ingest`], +/// [`take`], and (if `c_kzg` feature enabled) `build` methods. +/// +/// This trait allows different downstream users to use different bit-packing +/// strategies. For example, a simple coder might only use the last 31 bytes of +/// each blob, while a more complex coder might use a more sophisticated +/// strategy to pack data into the low 6 bits of the top byte. +/// +/// [`ingest`]: SidecarBuilder::ingest +/// [`take`]: SidecarBuilder::take +pub trait SidecarCoder { + /// Calculate the number of field elements required to store the given + /// data. + fn required_fe(&self, data: &[u8]) -> usize; + + /// Code a slice of data into the builder. + fn code(&mut self, builder: &mut PartialSidecar, data: &[u8]); + + /// Finish the sidecar, and commit to the data. This method should empty + /// any buffer or scratch space in the coder, and is called by + /// [`SidecarBuilder`]'s `take` and `build` methods. + fn finish(self, builder: &mut PartialSidecar); + + /// Decode all slices of data from the blobs. + fn decode_all(&mut self, blobs: &[Blob]) -> Option>>; +} + +/// Simple coder that only uses the last 31 bytes of each blob. This is the +/// default coder for the [`SidecarBuilder`]. +/// +/// # Note +/// +/// Because this coder sacrifices around 3% of total sidecar space, we do not +/// recommend its use in production. It is provided for convenience and +/// non-prod environments. +/// +/// # Behavior +/// +/// This coder encodes data as follows: +/// - The first byte of every 32-byte word is empty. +/// - Data is pre-pended with a 64-bit big-endian length prefix, which is right padded with zeros to +/// form a complete word. +/// - The rest of the data is packed into the remaining 31 bytes of each word. +/// - If the data is not a multiple of 31 bytes, the last word is right-padded with zeros. +/// +/// This means that the following regions cannot be used to store data, and are +/// considered "wasted": +/// +/// - The first byte of every 32-byte word. +/// - The right padding on the header word containing the data length. +/// - Any right padding on the last word for each piece of data. +#[derive(Debug, Copy, Clone, Default)] +#[non_exhaustive] +pub struct SimpleCoder; + +impl SimpleCoder { + /// Decode an some bytes from an iterator of valid FEs. + /// + /// Returns `Ok(Some(data))` if there is some data. + /// Returns `Ok(None)` if there is no data (length prefix is 0). + /// Returns `Err(())` if there is an error. + fn decode_one<'a>(mut fes: impl Iterator>) -> Result>, ()> { + let first = fes.next().ok_or(())?; + let mut num_bytes = u64::from_be_bytes(first.as_ref()[1..9].try_into().unwrap()) as usize; + + // if no more bytes is 0, we're done + if num_bytes == 0 { + return Ok(None); + } + + let mut res = Vec::with_capacity(num_bytes); + while num_bytes > 0 { + let to_copy = cmp::min(31, num_bytes); + let fe = fes.next().ok_or(())?; + res.extend_from_slice(&fe.as_ref()[1..1 + to_copy]); + num_bytes -= to_copy; + } + Ok(Some(res)) + } +} + +impl SidecarCoder for SimpleCoder { + fn required_fe(&self, data: &[u8]) -> usize { + data.len().div_ceil(31) + 1 + } + + fn code(&mut self, builder: &mut PartialSidecar, mut data: &[u8]) { + if data.is_empty() { + return; + } + + // first FE is the number of following bytes + builder.ingest_partial_fe(&(data.len() as u64).to_be_bytes()); + + // ingest the rest of the data + while !data.is_empty() { + let (left, right) = data.split_at(cmp::min(31, data.len())); + builder.ingest_partial_fe(left); + data = right + } + } + + /// No-op + fn finish(self, _builder: &mut PartialSidecar) {} + + fn decode_all(&mut self, blobs: &[Blob]) -> Option>> { + let mut fes = + blobs.iter().flat_map(|blob| blob.chunks(32).map(WholeFe::new)).map(Option::unwrap); + + let mut res = Vec::new(); + loop { + match Self::decode_one(&mut fes) { + Ok(Some(data)) => res.push(data), + Ok(None) => break, + Err(()) => return None, + } + } + Some(res) + } +} + +/// Build a [`BlobTransactionSidecar`] from an arbitrary amount of data. +/// +/// This is useful for creating a sidecar from a large amount of data, +/// which is then split into blobs. It delays KZG commitments and proofs +/// until all data is ready. +/// +/// [`BlobTransactionSidecar`]: crate::BlobTransactionSidecar +#[derive(Debug, Clone)] +pub struct SidecarBuilder { + /// The blob array we will code data into + inner: PartialSidecar, + /// The coder to use for ingesting and decoding data. + coder: T, +} + +impl Default for SidecarBuilder +where + T: Default + SidecarCoder, +{ + fn default() -> Self { + Self::new() + } +} + +impl SidecarBuilder { + /// Instantiate a new builder and new coder instance. + /// + /// By default, this allocates space for 2 blobs (256 KiB). If you want to + /// preallocate a specific number of blobs, use + /// [`SidecarBuilder::with_capacity`]. + pub fn new() -> Self { + Self::from_coder(T::default()) + } + + /// Create a new builder from a slice of data by calling + /// [`SidecarBuilder::from_coder_and_data`] + pub fn from_slice(data: &[u8]) -> SidecarBuilder { + Self::from_coder_and_data(T::default(), data) + } + + /// Create a new builder with a pre-allocated capacity. This capacity is + /// measured in blobs, each of which is 128 KiB. + pub fn with_capacity(capacity: usize) -> Self { + Self::from_coder_and_capacity(T::default(), capacity) + } +} + +impl SidecarBuilder { + /// Instantiate a new builder with the provided coder and capacity. This + /// capacity is measured in blobs, each of which is 128 KiB. + pub fn from_coder_and_capacity(coder: T, capacity: usize) -> Self { + Self { inner: PartialSidecar::with_capacity(capacity), coder } + } + + /// Instantiate a new builder with the provided coder. + /// + /// This is equivalent to calling + /// [`SidecarBuilder::from_coder_and_capacity`] with a capacity of 1. + /// If you want to preallocate a specific number of blobs, use + /// [`SidecarBuilder::from_coder_and_capacity`]. + pub fn from_coder(coder: T) -> Self { + Self::from_coder_and_capacity(coder, 1) + } + + /// Calculate the length of bytes used by field elements in the builder. + /// + /// This is always strictly greater than the number of bytes that have been + /// ingested. + pub const fn len(&self) -> usize { + self.inner.len() + } + + /// Check if the builder is empty. + pub const fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + /// Create a new builder from a slice of data. + pub fn from_coder_and_data(coder: T, data: &[u8]) -> SidecarBuilder { + let required_fe = coder.required_fe(data); + let mut this = Self::from_coder_and_capacity( + coder, + required_fe.div_ceil(FIELD_ELEMENTS_PER_BLOB as usize), + ); + this.ingest(data); + this + } + + /// Ingest a slice of data into the builder. + pub fn ingest(&mut self, data: &[u8]) { + self.inner.alloc_fes(self.coder.required_fe(data)); + self.coder.code(&mut self.inner, data); + } + + /// Build the sidecar from the data with the provided settings. + #[cfg(feature = "kzg")] + pub fn build_with_settings( + self, + settings: &c_kzg::KzgSettings, + ) -> Result { + let mut commitments = Vec::with_capacity(self.inner.blobs.len()); + let mut proofs = Vec::with_capacity(self.inner.blobs.len()); + for blob in self.inner.blobs.iter() { + let commitment = KzgCommitment::blob_to_kzg_commitment(blob, settings)?; + let proof = KzgProof::compute_blob_kzg_proof(blob, &commitment.to_bytes(), settings)?; + commitments.push(commitment.to_bytes()); + proofs.push(proof.to_bytes()); + } + + Ok(crate::BlobTransactionSidecar { blobs: self.inner.blobs, commitments, proofs }) + } + + /// Build the sidecar from the data, with default (Ethereum Mainnet) + /// settings. + #[cfg(feature = "kzg")] + pub fn build(self) -> Result { + self.build_with_settings(crate::EnvKzgSettings::Default.get()) + } + + /// Take the blobs from the builder, without committing them to a KZG proof. + pub fn take(self) -> Vec { + self.inner.blobs + } +} + +impl FromIterator for SidecarBuilder +where + T: SidecarCoder + Default, + R: AsRef<[u8]>, +{ + fn from_iter>(iter: I) -> Self { + let mut this = Self::new(); + for data in iter { + this.ingest(data.as_ref()); + } + this + } +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB; + + #[test] + fn ingestion_strategy() { + let mut builder = PartialSidecar::new(); + let data = &[vec![1u8; 32], vec![2u8; 372], vec![3u8; 17], vec![4u8; 5]]; + + data.iter().for_each(|data| SimpleCoder.code(&mut builder, data.as_slice())); + + let decoded = SimpleCoder.decode_all(builder.blobs()).unwrap(); + assert_eq!(decoded, data); + } + + #[test] + fn it_ingests() { + // test ingesting a lot of data. + let data = [ + vec![1u8; 32], + vec![2u8; 372], + vec![3u8; 17], + vec![4u8; 5], + vec![5u8; USABLE_BYTES_PER_BLOB + 2], + ]; + + let mut builder = data.iter().collect::>(); + + let expected_fe = data.iter().map(|d| SimpleCoder.required_fe(d)).sum::(); + assert_eq!(builder.len(), expected_fe * 32); + + // consume 2 more + builder.ingest("hello".as_bytes()); + assert_eq!(builder.len(), expected_fe * 32 + 64); + } +} diff --git a/crates/op-consensus/src/transaction/eip4844/utils.rs b/crates/op-consensus/src/transaction/eip4844/utils.rs new file mode 100644 index 00000000..602a581e --- /dev/null +++ b/crates/op-consensus/src/transaction/eip4844/utils.rs @@ -0,0 +1,77 @@ +//! Utilities for working with EIP-4844 field elements and implementing +//! [`SidecarCoder`]. +//! +//! [`SidecarCoder`]: crate::SidecarCoder +use alloy_eips::eip4844::USABLE_BITS_PER_FIELD_ELEMENT; + +/// Determine whether a slice of bytes can be contained in a field element. +pub const fn fits_in_fe(data: &[u8]) -> bool { + match data.len() { + 33.. => false, + 32 => data[0] & 0b1100_0000 == 0, // first two bits must be zero + _ => true, + } +} + +/// Calculate the number of field elements required to store the given +/// number of bytes. +pub const fn minimum_fe_for_bytes(bytes: usize) -> usize { + (bytes * 8).div_ceil(USABLE_BITS_PER_FIELD_ELEMENT) +} + +/// Calculate the number of field elements required to store the given data. +pub const fn minimum_fe(data: &[u8]) -> usize { + minimum_fe_for_bytes(data.len()) +} + +/// A wrapper for a slice of bytes that is a whole, valid field element. +#[derive(Debug, Copy, Clone)] +pub struct WholeFe<'a>(&'a [u8]); + +impl<'a> WholeFe<'a> { + const fn new_unchecked(data: &'a [u8]) -> Self { + Self(data) + } + + /// Instantiate a new `WholeFe` from a slice of bytes, if it is a valid + /// field element. + pub const fn new(data: &'a [u8]) -> Option { + if data.len() == 32 && fits_in_fe(data) { + Some(Self::new_unchecked(data)) + } else { + None + } + } +} + +impl AsRef<[u8]> for WholeFe<'_> { + fn as_ref(&self) -> &[u8] { + self.0 + } +} + +#[cfg(test)] +mod test { + use alloy_eips::eip4844::{FIELD_ELEMENTS_PER_BLOB, USABLE_BYTES_PER_BLOB}; + + use super::*; + #[test] + fn calc_required_fe() { + assert_eq!(minimum_fe(&[0u8; 32]), 2); + assert_eq!(minimum_fe(&[0u8; 31]), 1); + assert_eq!(minimum_fe(&[0u8; 33]), 2); + assert_eq!(minimum_fe(&[0u8; 64]), 3); + assert_eq!(minimum_fe(&[0u8; 65]), 3); + assert_eq!(minimum_fe_for_bytes(USABLE_BYTES_PER_BLOB), FIELD_ELEMENTS_PER_BLOB as usize); + } + + #[test] + fn calc_is_valid_field_element() { + assert!(fits_in_fe(&[0u8; 32])); + assert!(!fits_in_fe(&[0u8; 33])); + + assert!(WholeFe::new(&[0u8; 32]).is_some()); + assert!(WholeFe::new(&[0u8; 33]).is_none()); + assert!(WholeFe::new(&[0u8; 31]).is_none()); + } +} diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs new file mode 100644 index 00000000..2b00c970 --- /dev/null +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -0,0 +1,545 @@ +use crate::{ + Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, +}; +use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; +use alloy_rlp::{Decodable, Encodable, Header}; +use core::mem; + +/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and +/// [2930]. +/// +/// [2718]: https://eips.ethereum.org/EIPS/eip-2718 +/// [1559]: https://eips.ethereum.org/EIPS/eip-1559 +/// [2930]: https://eips.ethereum.org/EIPS/eip-2930 +/// [4844]: https://eips.ethereum.org/EIPS/eip-4844 +#[repr(u8)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] +pub enum TxType { + /// Legacy transaction type. + Legacy = 0, + /// EIP-2930 transaction type. + Eip2930 = 1, + /// EIP-1559 transaction type. + Eip1559 = 2, + /// EIP-4844 transaction type. + Eip4844 = 3, +} + +#[cfg(any(test, feature = "arbitrary"))] +impl<'a> arbitrary::Arbitrary<'a> for TxType { + fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { + Ok(match u.int_in_range(0..=3)? { + 0 => TxType::Legacy, + 1 => TxType::Eip2930, + 2 => TxType::Eip1559, + 3 => TxType::Eip4844, + _ => unreachable!(), + }) + } +} + +impl TryFrom for TxType { + type Error = Eip2718Error; + + fn try_from(value: u8) -> Result { + match value { + // SAFETY: repr(u8) with explicit discriminant + 0..=3 => Ok(unsafe { mem::transmute(value) }), + _ => Err(Eip2718Error::UnexpectedType(value)), + } + } +} + +/// The Ethereum [EIP-2718] Transaction Envelope. +/// +/// # Note: +/// +/// This enum distinguishes between tagged and untagged legacy transactions, as +/// the in-protocol merkle tree may commit to EITHER 0-prefixed or raw. +/// Therefore we must ensure that encoding returns the precise byte-array that +/// was decoded, preserving the presence or absence of the `TransactionType` +/// flag. +/// +/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] +#[non_exhaustive] +pub enum TxEnvelope { + /// An untagged [`TxLegacy`]. + #[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))] + Legacy(Signed), + /// A [`TxEip2930`] tagged with type 1. + #[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))] + Eip2930(Signed), + /// A [`TxEip1559`] tagged with type 2. + #[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))] + Eip1559(Signed), + /// A TxEip4844 tagged with type 3. + /// An EIP-4844 transaction has two network representations: + /// 1 - The transaction itself, which is a regular RLP-encoded transaction and used to retrieve + /// historical transactions.. + /// + /// 2 - The transaction with a sidecar, which is the form used to + /// send transactions to the network. + #[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))] + Eip4844(Signed), +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + Self::Legacy(v) + } +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + Self::Eip2930(v) + } +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + Self::Eip1559(v) + } +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + Self::Eip4844(v) + } +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + let (tx, signature, hash) = v.into_parts(); + Self::Eip4844(Signed::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)) + } +} + +impl From> for TxEnvelope { + fn from(v: Signed) -> Self { + let (tx, signature, hash) = v.into_parts(); + Self::Eip4844(Signed::new_unchecked( + TxEip4844Variant::TxEip4844WithSidecar(tx), + signature, + hash, + )) + } +} + +impl TxEnvelope { + /// Return the [`TxType`] of the inner txn. + pub const fn tx_type(&self) -> TxType { + match self { + Self::Legacy(_) => TxType::Legacy, + Self::Eip2930(_) => TxType::Eip2930, + Self::Eip1559(_) => TxType::Eip1559, + Self::Eip4844(_) => TxType::Eip4844, + } + } + + /// Return the length of the inner txn, __without a type byte__. + pub fn inner_length(&self) -> usize { + match self { + Self::Legacy(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(), + Self::Eip2930(t) => { + let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); + Header { list: true, payload_length }.length() + payload_length + } + Self::Eip1559(t) => { + let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); + Header { list: true, payload_length }.length() + payload_length + } + Self::Eip4844(t) => match t.tx() { + TxEip4844Variant::TxEip4844(tx) => { + let payload_length = tx.fields_len() + t.signature().rlp_vrs_len(); + Header { list: true, payload_length }.length() + payload_length + } + TxEip4844Variant::TxEip4844WithSidecar(tx) => { + let inner_payload_length = tx.tx().fields_len() + t.signature().rlp_vrs_len(); + let inner_header = Header { list: true, payload_length: inner_payload_length }; + + let outer_payload_length = + inner_header.length() + inner_payload_length + tx.sidecar.fields_len(); + let outer_header = Header { list: true, payload_length: outer_payload_length }; + + outer_header.length() + outer_payload_length + } + }, + } + } + + /// Return the RLP payload length of the network-serialized wrapper + fn rlp_payload_length(&self) -> usize { + if let Self::Legacy(t) = self { + let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); + return Header { list: true, payload_length }.length() + payload_length; + } + // length of inner tx body + let inner_length = self.inner_length(); + // with tx type byte + inner_length + 1 + } +} + +impl Encodable for TxEnvelope { + fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { + self.network_encode(out) + } + + fn length(&self) -> usize { + let mut payload_length = self.rlp_payload_length(); + if !self.is_legacy() { + payload_length += Header { list: false, payload_length }.length(); + } + + payload_length + } +} + +impl Decodable for TxEnvelope { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::network_decode(buf) + } +} + +impl Decodable2718 for TxEnvelope { + fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { + match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("unexpected tx type"))? { + TxType::Eip2930 => Ok(Self::Eip2930(TxEip2930::decode_signed_fields(buf)?)), + TxType::Eip1559 => Ok(Self::Eip1559(TxEip1559::decode_signed_fields(buf)?)), + TxType::Eip4844 => Ok(Self::Eip4844(TxEip4844Variant::decode_signed_fields(buf)?)), + TxType::Legacy => { + Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) + } + } + } + + fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(TxEnvelope::Legacy(TxLegacy::decode_signed_fields(buf)?)) + } +} + +impl Encodable2718 for TxEnvelope { + fn type_flag(&self) -> Option { + match self { + Self::Legacy(_) => None, + Self::Eip2930(_) => Some(TxType::Eip2930 as u8), + Self::Eip1559(_) => Some(TxType::Eip1559 as u8), + Self::Eip4844(_) => Some(TxType::Eip4844 as u8), + } + } + + fn encode_2718_len(&self) -> usize { + self.inner_length() + !self.is_legacy() as usize + } + + fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) { + match self { + // Legacy transactions have no difference between network and 2718 + TxEnvelope::Legacy(tx) => tx.tx().encode_with_signature_fields(tx.signature(), out), + TxEnvelope::Eip2930(tx) => { + tx.tx().encode_with_signature(tx.signature(), out, false); + } + TxEnvelope::Eip1559(tx) => { + tx.tx().encode_with_signature(tx.signature(), out, false); + } + TxEnvelope::Eip4844(tx) => { + tx.tx().encode_with_signature(tx.signature(), out, false); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::transaction::SignableTransaction; + use alloy_eips::eip2930::{AccessList, AccessListItem}; + use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256}; + use std::{fs, path::PathBuf, vec}; + + #[cfg(not(feature = "std"))] + use std::vec::Vec; + + #[test] + #[cfg(feature = "k256")] + // Test vector from https://etherscan.io/tx/0xce4dc6d7a7549a98ee3b071b67e970879ff51b5b95d1c340bacd80fa1e1aab31 + fn test_decode_live_1559_tx() { + use alloy_primitives::address; + + let raw_tx = alloy_primitives::hex::decode("02f86f0102843b9aca0085029e7822d68298f094d9e1459a7a482635700cbc20bbaf52d495ab9c9680841b55ba3ac080a0c199674fcb29f353693dd779c017823b954b3c69dffa3cd6b2a6ff7888798039a028ca912de909e7e6cdef9cdcaf24c54dd8c1032946dfa1d85c206b32a9064fe8").unwrap(); + let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + + assert_eq!(res.tx_type(), TxType::Eip1559); + + let tx = match res { + TxEnvelope::Eip1559(tx) => tx, + _ => unreachable!(), + }; + + assert_eq!(tx.tx().to, TxKind::Call(address!("D9e1459A7A482635700cBc20BBAF52D495Ab9C96"))); + let from = tx.recover_signer().unwrap(); + assert_eq!(from, address!("001e2b7dE757bA469a57bF6b23d982458a07eFcE")); + } + + #[test] + #[cfg(feature = "k256")] + // Test vector from https://etherscan.io/tx/0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4 + fn test_decode_live_legacy_tx() { + use alloy_primitives::address; + + let raw_tx = alloy_primitives::hex::decode("f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8").unwrap(); + let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + assert_eq!(res.tx_type(), TxType::Legacy); + + let tx = match res { + TxEnvelope::Legacy(tx) => tx, + _ => unreachable!(), + }; + + assert_eq!(tx.tx().to, TxKind::Call(address!("7a250d5630B4cF539739dF2C5dAcb4c659F2488D"))); + assert_eq!( + tx.hash().to_string(), + "0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4" + ); + let from = tx.recover_signer().unwrap(); + assert_eq!(from, address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4")); + } + + #[test] + #[cfg(feature = "k256")] + // Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 + // Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 + fn test_decode_live_4844_tx() { + use crate::Transaction; + use alloy_primitives::{address, b256}; + + // https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 + let raw_tx = alloy_primitives::hex::decode("0x03f9011d83aa36a7820fa28477359400852e90edd0008252089411e9ca82a3a762b4b5bd264d4173a242e7a770648080c08504a817c800f8a5a0012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921aa00152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4a0013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7a001148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1a0011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e654901a0c8de4cced43169f9aa3d36506363b2d2c44f6c49fc1fd91ea114c86f3757077ea01e11fdd0d1934eda0492606ee0bb80a7bf8f35cc5f86ec60fe5031ba48bfd544").unwrap(); + let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + assert_eq!(res.tx_type(), TxType::Eip4844); + + let tx = match res { + TxEnvelope::Eip4844(tx) => tx, + _ => unreachable!(), + }; + + assert_eq!( + tx.tx().to(), + TxKind::Call(address!("11E9CA82A3a762b4B5bd264d4173a242e7a77064")) + ); + + // Assert this is the correct variant of the EIP-4844 enum, which only contains the tx. + assert!(matches!(tx.tx(), TxEip4844Variant::TxEip4844(_))); + + assert_eq!( + tx.tx().tx().blob_versioned_hashes, + vec![ + b256!("012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921a"), + b256!("0152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4"), + b256!("013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7"), + b256!("01148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1"), + b256!("011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e6549") + ] + ); + + let from = tx.recover_signer().unwrap(); + assert_eq!(from, address!("A83C816D4f9b2783761a22BA6FADB0eB0606D7B2")); + } + + fn test_encode_decode_roundtrip>(tx: T) + where + Signed: Into, + { + let signature = Signature::test_signature(); + let tx_signed = tx.into_signed(signature); + let tx_envelope: TxEnvelope = tx_signed.into(); + let encoded = tx_envelope.encoded_2718(); + let decoded = TxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap(); + assert_eq!(encoded.len(), tx_envelope.encode_2718_len()); + assert_eq!(decoded, tx_envelope); + } + + #[test] + fn test_encode_decode_eip1559() { + let tx = TxEip1559 { + chain_id: 1u64, + nonce: 2, + max_fee_per_gas: 3, + max_priority_fee_per_gas: 4, + gas_limit: 5, + to: TxKind::Call(Address::left_padding_from(&[6])), + value: U256::from(7_u64), + input: Bytes::from(vec![8]), + access_list: Default::default(), + }; + test_encode_decode_roundtrip(tx); + } + + #[test] + fn test_encode_decode_eip2930() { + let tx = TxEip2930 { + chain_id: 1u64, + nonce: 2, + gas_price: 3, + gas_limit: 4, + to: TxKind::Call(Address::left_padding_from(&[5])), + value: U256::from(6_u64), + input: Bytes::from(vec![7]), + access_list: AccessList(vec![AccessListItem { + address: Address::left_padding_from(&[8]), + storage_keys: vec![B256::left_padding_from(&[9])], + }]), + }; + test_encode_decode_roundtrip(tx); + } + + #[test] + fn test_encode_decode_transaction_list() { + let signature = Signature::test_signature(); + let tx = TxEnvelope::Eip1559( + TxEip1559 { + chain_id: 1u64, + nonce: 2, + max_fee_per_gas: 3, + max_priority_fee_per_gas: 4, + gas_limit: 5, + to: TxKind::Call(Address::left_padding_from(&[6])), + value: U256::from(7_u64), + input: Bytes::from(vec![8]), + access_list: Default::default(), + } + .into_signed(signature), + ); + let transactions = vec![tx.clone(), tx]; + let encoded = alloy_rlp::encode(&transactions); + let decoded = Vec::::decode(&mut &encoded[..]).unwrap(); + assert_eq!(transactions, decoded); + } + + #[test] + fn decode_encode_known_rpc_transaction() { + // test data pulled from hive test that sends blob transactions + let network_data_path = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/rpc_blob_transaction.rlp"); + let data = fs::read_to_string(network_data_path).expect("Unable to read file"); + let hex_data = hex::decode(data.trim()).unwrap(); + + let tx: TxEnvelope = TxEnvelope::decode_2718(&mut hex_data.as_slice()).unwrap(); + let encoded = tx.encoded_2718(); + assert_eq!(encoded, hex_data); + assert_eq!(tx.encode_2718_len(), hex_data.len()); + } + + #[cfg(feature = "serde")] + fn test_serde_roundtrip>(tx: T) + where + Signed: Into, + { + let signature = Signature::test_signature(); + let tx_envelope: TxEnvelope = tx.into_signed(signature).into(); + + let serialized = serde_json::to_string(&tx_envelope).unwrap(); + let deserialized: TxEnvelope = serde_json::from_str(&serialized).unwrap(); + + assert_eq!(tx_envelope, deserialized); + } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_roundtrip_legacy() { + let tx = TxLegacy { + chain_id: Some(1), + nonce: 100, + gas_price: 3_000_000_000, + gas_limit: 50_000, + to: TxKind::Call(Address::default()), + value: U256::from(10e18), + input: Bytes::new(), + }; + test_serde_roundtrip(tx); + } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_roundtrip_eip1559() { + let tx = TxEip1559 { + chain_id: 1, + nonce: 100, + max_fee_per_gas: 50_000_000_000, + max_priority_fee_per_gas: 1_000_000_000_000, + gas_limit: 1_000_000, + to: TxKind::Create, + value: U256::from(10e18), + input: Bytes::new(), + access_list: AccessList(vec![AccessListItem { + address: Address::random(), + storage_keys: vec![B256::random()], + }]), + }; + test_serde_roundtrip(tx); + } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_roundtrip_eip2930() { + let tx = TxEip2930 { + chain_id: u64::MAX, + nonce: u64::MAX, + gas_price: u128::MAX, + gas_limit: u128::MAX, + to: TxKind::Call(Address::random()), + value: U256::MAX, + input: Bytes::new(), + access_list: Default::default(), + }; + test_serde_roundtrip(tx); + } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_roundtrip_eip4844() { + use crate::BlobTransactionSidecar; + + let tx = TxEip4844Variant::TxEip4844(TxEip4844 { + chain_id: 1, + nonce: 100, + max_fee_per_gas: 50_000_000_000, + max_priority_fee_per_gas: 1_000_000_000_000, + gas_limit: 1_000_000, + to: Address::random(), + value: U256::from(10e18), + input: Bytes::new(), + access_list: AccessList(vec![AccessListItem { + address: Address::random(), + storage_keys: vec![B256::random()], + }]), + blob_versioned_hashes: vec![B256::random()], + max_fee_per_blob_gas: 0, + }); + test_serde_roundtrip(tx); + + let tx = TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar { + tx: TxEip4844 { + chain_id: 1, + nonce: 100, + max_fee_per_gas: 50_000_000_000, + max_priority_fee_per_gas: 1_000_000_000_000, + gas_limit: 1_000_000, + to: Address::random(), + value: U256::from(10e18), + input: Bytes::new(), + access_list: AccessList(vec![AccessListItem { + address: Address::random(), + storage_keys: vec![B256::random()], + }]), + blob_versioned_hashes: vec![B256::random()], + max_fee_per_blob_gas: 0, + }, + sidecar: BlobTransactionSidecar { ..Default::default() }, + }); + test_serde_roundtrip(tx); + } +} diff --git a/crates/op-consensus/src/transaction/legacy.rs b/crates/op-consensus/src/transaction/legacy.rs new file mode 100644 index 00000000..52eb666c --- /dev/null +++ b/crates/op-consensus/src/transaction/legacy.rs @@ -0,0 +1,345 @@ +use crate::{SignableTransaction, Signed, Transaction}; +use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; +use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// Legacy transaction. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxLegacy { + /// Added as EIP-155: Simple replay attack protection + #[cfg_attr( + feature = "serde", + serde( + default, + with = "alloy_serde::u64_hex_or_decimal_opt", + skip_serializing_if = "Option::is_none", + ) + )] + pub chain_id: Option, + /// A scalar value equal to the number of transactions sent by the sender; formally Tn. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + pub nonce: u64, + /// A scalar value equal to the number of + /// Wei to be paid per unit of gas for all computation + /// costs incurred as a result of the execution of this transaction; formally Tp. + /// + /// As ethereum circulation is around 120mil eth as of 2022 that is around + /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: + /// 340282366920938463463374607431768211455 + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_price: u128, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_limit: u128, + /// The 160-bit address of the message call’s recipient or, for a contract creation + /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. + #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] + pub to: TxKind, + /// A scalar value equal to the number of Wei to + /// be transferred to the message call’s recipient or, + /// in the case of contract creation, as an endowment + /// to the newly created account; formally Tv. + pub value: U256, + /// Input has two uses depending if transaction is Create or Call (if `to` field is None or + /// Some). pub init: An unlimited size byte array specifying the + /// EVM-code for the account initialisation procedure CREATE, + /// data: An unlimited size byte array specifying the + /// input data of the message call, formally Td. + pub input: Bytes, +} + +impl TxLegacy { + /// The EIP-2718 transaction type. + pub const TX_TYPE: isize = 0; + + /// Calculates a heuristic for the in-memory size of the [TxLegacy] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::>() + // chain_id + mem::size_of::() + // nonce + mem::size_of::() + // gas_price + mem::size_of::() + // gas_limit + self.to.size() + // to + mem::size_of::() + // value + self.input.len() // input + } + + /// Outputs the length of the transaction's fields, without a RLP header or length of the + /// eip155 fields. + pub(crate) fn fields_len(&self) -> usize { + let mut len = 0; + len += self.nonce.length(); + len += self.gas_price.length(); + len += self.gas_limit.length(); + len += self.to.length(); + len += self.value.length(); + len += self.input.0.length(); + len + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header or + /// eip155 fields. + pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { + self.nonce.encode(out); + self.gas_price.encode(out); + self.gas_limit.encode(out); + self.to.encode(out); + self.value.encode(out); + self.input.0.encode(out); + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + pub fn encode_with_signature_fields( + &self, + signature: &Signature, + out: &mut dyn alloy_rlp::BufMut, + ) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + let header = Header { list: true, payload_length }; + header.encode(out); + self.encode_fields(out); + signature.write_rlp_vrs(out); + } + + /// Returns what the encoded length should be, if the transaction were RLP encoded with the + /// given signature. + pub(crate) fn encoded_len_with_signature(&self, signature: &Signature) -> usize { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + Header { list: true, payload_length }.length() + payload_length + } + + /// Encodes EIP-155 arguments into the desired buffer. Only encodes values + /// for legacy transactions. + pub(crate) fn encode_eip155_signing_fields(&self, out: &mut dyn BufMut) { + // if this is a legacy transaction without a chain ID, it must be pre-EIP-155 + // and does not need to encode the chain ID for the signature hash encoding + if let Some(id) = self.chain_id { + // EIP-155 encodes the chain ID and two zeroes + id.encode(out); + 0x00u8.encode(out); + 0x00u8.encode(out); + } + } + + /// Outputs the length of EIP-155 fields. Only outputs a non-zero value for EIP-155 legacy + /// transactions. + pub(crate) fn eip155_fields_len(&self) -> usize { + if let Some(id) = self.chain_id { + // EIP-155 encodes the chain ID and two zeroes, so we add 2 to the length of the chain + // ID to get the length of all 3 fields + // len(chain_id) + (0x00) + (0x00) + id.length() + 2 + } else { + // this is either a pre-EIP-155 legacy transaction or a typed transaction + 0 + } + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + let mut tx = Self::decode_fields(buf)?; + let signature = Signature::decode_rlp_vrs(buf)?; + + // extract chain id from signature + let v = signature.v(); + tx.chain_id = v.chain_id(); + + let signed = tx.into_signed(signature); + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + Ok(signed) + } + + /// Decode the RLP fields of the transaction, without decoding an RLP + /// header. + pub(crate) fn decode_fields(data: &mut &[u8]) -> Result { + Ok(TxLegacy { + nonce: Decodable::decode(data)?, + gas_price: Decodable::decode(data)?, + gas_limit: Decodable::decode(data)?, + to: Decodable::decode(data)?, + value: Decodable::decode(data)?, + input: Decodable::decode(data)?, + chain_id: None, + }) + } +} + +impl Transaction for TxLegacy { + fn input(&self) -> &[u8] { + &self.input + } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn chain_id(&self) -> Option { + self.chain_id + } + + fn nonce(&self) -> u64 { + self.nonce + } + + fn gas_limit(&self) -> u128 { + self.gas_limit + } + + fn gas_price(&self) -> Option { + Some(self.gas_price) + } +} + +impl SignableTransaction for TxLegacy { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.chain_id = Some(chain_id); + } + + fn encode_for_signing(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() + self.eip155_fields_len() } + .encode(out); + self.encode_fields(out); + self.encode_eip155_signing_fields(out); + } + + fn payload_len_for_signature(&self) -> usize { + let payload_length = self.fields_len() + self.eip155_fields_len(); + // 'header length' + 'payload length' + Header { list: true, payload_length }.length() + payload_length + } + + fn into_signed(self, signature: Signature) -> Signed { + let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature)); + self.encode_with_signature_fields(&signature, &mut buf); + let hash = keccak256(&buf); + Signed::new_unchecked(self, signature, hash) + } +} + +impl Encodable for TxLegacy { + fn encode(&self, out: &mut dyn BufMut) { + self.encode_for_signing(out) + } + + fn length(&self) -> usize { + let payload_length = self.fields_len() + self.eip155_fields_len(); + // 'header length' + 'payload length' + length_of_length(payload_length) + payload_length + } +} + +impl Decodable for TxLegacy { + fn decode(data: &mut &[u8]) -> Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + let transaction_payload_len = header.payload_length; + + if transaction_payload_len > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + let mut transaction = Self::decode_fields(data)?; + + // If we still have data, it should be an eip-155 encoded chain_id + if !data.is_empty() { + transaction.chain_id = Some(Decodable::decode(data)?); + let _: U256 = Decodable::decode(data)?; // r + let _: U256 = Decodable::decode(data)?; // s + } + + let decoded = remaining_len - data.len(); + if decoded != transaction_payload_len { + return Err(alloy_rlp::Error::UnexpectedLength); + } + + Ok(transaction) + } +} + +#[cfg(all(test, feature = "k256"))] +mod tests { + use crate::{SignableTransaction, TxLegacy}; + use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256}; + + #[test] + fn recover_signer_legacy() { + let signer: Address = hex!("398137383b3d25c92898c656696e41950e47316b").into(); + let hash: B256 = + hex!("bb3a336e3f823ec18197f1e13ee875700f08f03e2cab75f0d0b118dabb44cba0").into(); + + let tx = TxLegacy { + chain_id: Some(1), + nonce: 0x18, + gas_price: 0xfa56ea00, + gas_limit: 119902, + to: TxKind::Call( hex!("06012c8cf97bead5deae237070f9587f8e7a266d").into()), + value: U256::from(0x1c6bf526340000u64), + input: hex!("f7d8c88300000000000000000000000000000000000000000000000000000000000cee6100000000000000000000000000000000000000000000000000000000000ac3e1").into(), + }; + + let sig = Signature::from_scalars_and_parity( + b256!("2a378831cf81d99a3f06a18ae1b6ca366817ab4d88a70053c41d7a8f0368e031"), + b256!("450d831a05b6e418724436c05c155e0a1b7b921015d0fbc2f667aed709ac4fb5"), + 37, + ) + .unwrap(); + + let signed_tx = tx.into_signed(sig); + + assert_eq!(*signed_tx.hash(), hash, "Expected same hash"); + assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass."); + } + + #[test] + // Test vector from https://github.com/alloy-rs/alloy/issues/125 + fn decode_legacy_and_recover_signer() { + let raw_tx = "f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8"; + + let tx = TxLegacy::decode_signed_fields( + &mut alloy_primitives::hex::decode(raw_tx).unwrap().as_slice(), + ) + .unwrap(); + + let recovered = tx.recover_signer().unwrap(); + let expected = address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4"); + + assert_eq!(tx.tx().chain_id, Some(1), "Expected same chain id"); + assert_eq!(expected, recovered, "Expected same signer"); + } +} diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs new file mode 100644 index 00000000..6d6c3755 --- /dev/null +++ b/crates/op-consensus/src/transaction/mod.rs @@ -0,0 +1,121 @@ +use crate::Signed; +use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256}; +use core::any; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +mod eip1559; +pub use eip1559::TxEip1559; + +mod eip2930; +pub use eip2930::TxEip2930; + +mod eip4844; +#[cfg(feature = "kzg")] +pub use eip4844::BlobTransactionValidationError; +pub use eip4844::{ + utils as eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder, + SimpleCoder, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, +}; + +mod envelope; +pub use envelope::{TxEnvelope, TxType}; + +mod legacy; +pub use legacy::TxLegacy; + +mod typed; +pub use typed::TypedTransaction; + +/// Represents a minimal EVM transaction. +pub trait Transaction: any::Any + Send + Sync + 'static { + /// Get `data`. + fn input(&self) -> &[u8]; + + /// Get `to`. + fn to(&self) -> TxKind; + + /// Get `value`. + fn value(&self) -> U256; + + /// Get `chain_id`. + fn chain_id(&self) -> Option; + + /// Get `nonce`. + fn nonce(&self) -> u64; + + /// Get `gas_limit`. + fn gas_limit(&self) -> u128; + + /// Get `gas_price`. + fn gas_price(&self) -> Option; +} + +/// A signable transaction. +/// +/// A transaction can have multiple signature types. This is usually +/// [`alloy_primitives::Signature`], however, it may be different for future EIP-2718 transaction +/// types, or in other networks. For example, in Optimism, the deposit transaction signature is the +/// unit type `()`. +pub trait SignableTransaction: Transaction { + /// Sets `chain_id`. + /// + /// Prefer [`set_chain_id_checked`](Self::set_chain_id_checked). + fn set_chain_id(&mut self, chain_id: ChainId); + + /// Set `chain_id` if it is not already set. Checks that the provided `chain_id` matches the + /// existing `chain_id` if it is already set, returning `false` if they do not match. + fn set_chain_id_checked(&mut self, chain_id: ChainId) -> bool { + match self.chain_id() { + Some(tx_chain_id) => { + if tx_chain_id != chain_id { + return false; + } + self.set_chain_id(chain_id); + } + None => { + self.set_chain_id(chain_id); + } + } + true + } + + /// RLP-encodes the transaction for signing. + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut); + + /// Outputs the length of the signature RLP encoding for the transaction. + fn payload_len_for_signature(&self) -> usize; + + /// RLP-encodes the transaction for signing it. Used to calculate `signature_hash`. + /// + /// See [`SignableTransaction::encode_for_signing`]. + fn encoded_for_signing(&self) -> Vec { + let mut buf = Vec::with_capacity(self.payload_len_for_signature()); + self.encode_for_signing(&mut buf); + buf + } + + /// Calculate the signing hash for the transaction. + fn signature_hash(&self) -> B256 { + keccak256(self.encoded_for_signing()) + } + + /// Convert to a signed transaction by adding a signature and computing the + /// hash. + fn into_signed(self, signature: Signature) -> Signed + where + Self: Sized; +} + +// TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162) +#[doc(hidden)] +impl dyn SignableTransaction { + pub fn __downcast_ref(&self) -> Option<&T> { + if any::Any::type_id(self) == any::TypeId::of::() { + unsafe { Some(&*(self as *const _ as *const T)) } + } else { + None + } + } +} diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs new file mode 100644 index 00000000..441b0240 --- /dev/null +++ b/crates/op-consensus/src/transaction/typed.rs @@ -0,0 +1,163 @@ +use crate::{Transaction, TxEip1559, TxEip2930, TxEip4844Variant, TxEnvelope, TxLegacy, TxType}; +use alloy_primitives::TxKind; + +/// The TypedTransaction enum represents all Ethereum transaction request types. +/// +/// Its variants correspond to specific allowed transactions: +/// 1. Legacy (pre-EIP2718) [`TxLegacy`] +/// 2. EIP2930 (state access lists) [`TxEip2930`] +/// 3. EIP1559 [`TxEip1559`] +/// 4. EIP4844 [`TxEip4844Variant`] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] +pub enum TypedTransaction { + /// Legacy transaction + #[cfg_attr(feature = "serde", serde(rename = "0x00", alias = "0x0"))] + Legacy(TxLegacy), + /// EIP-2930 transaction + #[cfg_attr(feature = "serde", serde(rename = "0x01", alias = "0x1"))] + Eip2930(TxEip2930), + /// EIP-1559 transaction + #[cfg_attr(feature = "serde", serde(rename = "0x02", alias = "0x2"))] + Eip1559(TxEip1559), + /// EIP-4844 transaction + #[cfg_attr(feature = "serde", serde(rename = "0x03", alias = "0x3"))] + Eip4844(TxEip4844Variant), +} + +impl From for TypedTransaction { + fn from(tx: TxLegacy) -> Self { + Self::Legacy(tx) + } +} + +impl From for TypedTransaction { + fn from(tx: TxEip2930) -> Self { + Self::Eip2930(tx) + } +} + +impl From for TypedTransaction { + fn from(tx: TxEip1559) -> Self { + Self::Eip1559(tx) + } +} + +impl From for TypedTransaction { + fn from(tx: TxEip4844Variant) -> Self { + Self::Eip4844(tx) + } +} + +impl From for TypedTransaction { + fn from(envelope: TxEnvelope) -> Self { + match envelope { + TxEnvelope::Legacy(tx) => Self::Legacy(tx.strip_signature()), + TxEnvelope::Eip2930(tx) => Self::Eip2930(tx.strip_signature()), + TxEnvelope::Eip1559(tx) => Self::Eip1559(tx.strip_signature()), + TxEnvelope::Eip4844(tx) => Self::Eip4844(tx.strip_signature()), + } + } +} + +impl TypedTransaction { + /// Return the [`TxType`] of the inner txn. + pub const fn tx_type(&self) -> TxType { + match self { + Self::Legacy(_) => TxType::Legacy, + Self::Eip2930(_) => TxType::Eip2930, + Self::Eip1559(_) => TxType::Eip1559, + Self::Eip4844(_) => TxType::Eip4844, + } + } + + /// Return the inner legacy transaction if it exists. + pub const fn legacy(&self) -> Option<&TxLegacy> { + match self { + Self::Legacy(tx) => Some(tx), + _ => None, + } + } + + /// Return the inner EIP-2930 transaction if it exists. + pub const fn eip2930(&self) -> Option<&TxEip2930> { + match self { + Self::Eip2930(tx) => Some(tx), + _ => None, + } + } + + /// Return the inner EIP-1559 transaction if it exists. + pub const fn eip1559(&self) -> Option<&TxEip1559> { + match self { + Self::Eip1559(tx) => Some(tx), + _ => None, + } + } +} + +impl Transaction for TypedTransaction { + fn chain_id(&self) -> Option { + match self { + Self::Legacy(tx) => tx.chain_id(), + Self::Eip2930(tx) => tx.chain_id(), + Self::Eip1559(tx) => tx.chain_id(), + Self::Eip4844(tx) => tx.chain_id(), + } + } + + fn gas_limit(&self) -> u128 { + match self { + Self::Legacy(tx) => tx.gas_limit(), + Self::Eip2930(tx) => tx.gas_limit(), + Self::Eip1559(tx) => tx.gas_limit(), + Self::Eip4844(tx) => tx.gas_limit(), + } + } + + fn gas_price(&self) -> Option { + match self { + Self::Legacy(tx) => tx.gas_price(), + Self::Eip2930(tx) => tx.gas_price(), + Self::Eip1559(tx) => tx.gas_price(), + Self::Eip4844(tx) => tx.gas_price(), + } + } + + fn input(&self) -> &[u8] { + match self { + Self::Legacy(tx) => tx.input(), + Self::Eip2930(tx) => tx.input(), + Self::Eip1559(tx) => tx.input(), + Self::Eip4844(tx) => tx.input(), + } + } + + fn nonce(&self) -> u64 { + match self { + Self::Legacy(tx) => tx.nonce(), + Self::Eip2930(tx) => tx.nonce(), + Self::Eip1559(tx) => tx.nonce(), + Self::Eip4844(tx) => tx.nonce(), + } + } + + fn to(&self) -> TxKind { + match self { + Self::Legacy(tx) => tx.to(), + Self::Eip2930(tx) => tx.to(), + Self::Eip1559(tx) => tx.to(), + Self::Eip4844(tx) => tx.to(), + } + } + + fn value(&self) -> alloy_primitives::U256 { + match self { + Self::Legacy(tx) => tx.value(), + Self::Eip2930(tx) => tx.value(), + Self::Eip1559(tx) => tx.value(), + Self::Eip4844(tx) => tx.value(), + } + } +} diff --git a/crates/op-consensus/testdata/rpc_blob_transaction.rlp b/crates/op-consensus/testdata/rpc_blob_transaction.rlp new file mode 100644 index 00000000..36232f1c --- /dev/null +++ b/crates/op-consensus/testdata/rpc_blob_transaction.rlp @@ -0,0 +1 @@ +03fa0200fdf88f0780843b9aca008506fc23ac00830186a09400000000000000000000000000000000000001008080c001e1a0010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c44401401a0840650aa8f74d2b07f40067dc33b715078d73422f01da17abdbd11e02bbdfda9a04b2260f6022bf53eadb337b3e59514936f7317d872defb891a708ee279bdca90fa020004ba0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f1b0c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f1b0c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 From 24be12a2d21d9a9f4b1d7054e5ceab0fdfa05611 Mon Sep 17 00:00:00 2001 From: refcell Date: Thu, 11 Apr 2024 08:03:48 -0500 Subject: [PATCH 02/20] feat(consensus): op-consensus --- Cargo.toml | 16 +++++-- Justfile | 42 +++++++++++++++++++ crates/op-consensus/Cargo.toml | 4 +- crates/op-consensus/src/receipt/envelope.rs | 2 +- crates/op-consensus/src/receipt/receipts.rs | 2 +- .../op-consensus/src/transaction/envelope.rs | 2 +- 6 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 Justfile diff --git a/Cargo.toml b/Cargo.toml index 62e730d2..1867ff69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,15 @@ exclude = ["benches/", "tests/"] [workspace.dependencies] # Alloy -alloy-primitives = { version = "0.7.1", default-features = false } alloy = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } +alloy-rlp = { version = "0.3", default-features = false } +alloy-primitives = { version = "0.7.1", default-features = false } +alloy-op-rpc-types = { version = "0.1.0", path = "crates/rpc-types" } alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } -alloy-op-rpc-types = { version = "0.1.0", path = "crates/rpc-types" } +alloy-eips = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } +alloy-serde = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } +alloy-signer = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } # Serde serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } @@ -27,9 +31,15 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] } ## misc-testing arbitrary = { version = "1.3", features = ["derive"] } rand = "0.8" +thiserror = "1.0" proptest = "1.4" proptest-derive = "0.4" +tokio = "1" + +## crypto +c-kzg = { version = "1.0", default-features = false } +k256 = { version = "0.13", default-features = false, features = ["ecdsa"] } [workspace.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] \ No newline at end of file +rustdoc-args = ["--cfg", "docsrs"] diff --git a/Justfile b/Justfile new file mode 100644 index 00000000..e43efabe --- /dev/null +++ b/Justfile @@ -0,0 +1,42 @@ +set positional-arguments + +# default recipe to display help information +default: + @just --list + +# Run all ci checks for the native target +ci: lint-native lint-docs test + +# Test for the native target +test *args='': + cargo nextest run --workspace --all $@ + +# Lint the workspace for all available targets +lint: lint-native lint-docs + +# Fixes the formatting of the workspace +fmt-native-fix: + cargo +nightly fmt --all + +# Check the formatting of the workspace +fmt-native-check: + cargo +nightly fmt --all -- --check + +# Lint the workspace +lint-native: fmt-native-check + cargo +nightly clippy --workspace --all --all-features --all-targets -- -D warnings + +# Lint the Rust documentation +lint-docs: + RUSTDOCFLAGS="-D warnings" cargo doc --all --no-deps --document-private-items + +# Test the Rust documentation +test-docs: + cargo test --doc --all --locked + +# Build the workspace for all available targets +build: build-native + +# Build for the native target +build-native *args='': + cargo build --workspace --all $@ diff --git a/crates/op-consensus/Cargo.toml b/crates/op-consensus/Cargo.toml index 5e4f9362..9b8db4ab 100644 --- a/crates/op-consensus/Cargo.toml +++ b/crates/op-consensus/Cargo.toml @@ -12,7 +12,7 @@ repository.workspace = true exclude.workspace = true [dependencies] -alloy-primitives = { workspace = true, features = ["rlp"] } +alloy-primitives = { workspace = true, features = ["rlp", "rand"] } alloy-rlp.workspace = true alloy-eips.workspace = true alloy-serde = { workspace = true, optional = true } @@ -41,5 +41,5 @@ default = ["std"] std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"] k256 = ["alloy-primitives/k256"] kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"] -arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary"] +arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary", "alloy-primitives/arbitrary"] serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"] diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index 6890f5a9..332d215b 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -173,7 +173,7 @@ impl Decodable2718 for ReceiptEnvelope { } } -#[cfg(any(test, feature = "arbitrary"))] +#[cfg(all(test, feature = "arbitrary"))] impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptEnvelope where T: arbitrary::Arbitrary<'a>, diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index 3acd06e5..e6ec7a36 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -189,7 +189,7 @@ impl alloy_rlp::Decodable for ReceiptWithBloom { } } -#[cfg(any(test, feature = "arbitrary"))] +#[cfg(all(test, feature = "arbitrary"))] impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptWithBloom where T: arbitrary::Arbitrary<'a>, diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index 2b00c970..e0b89afb 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -44,7 +44,7 @@ impl TryFrom for TxType { fn try_from(value: u8) -> Result { match value { // SAFETY: repr(u8) with explicit discriminant - 0..=3 => Ok(unsafe { mem::transmute(value) }), + 0..=3 => Ok(unsafe { mem::transmute::(value) }), _ => Err(Eip2718Error::UnexpectedType(value)), } } From 189f9e5c55215e2687e4c9051a430851c1ddb5d6 Mon Sep 17 00:00:00 2001 From: refcell Date: Thu, 11 Apr 2024 09:13:13 -0500 Subject: [PATCH 03/20] feat(op-consensus): add optimism deposit tx type --- crates/op-consensus/src/lib.rs | 2 +- crates/op-consensus/src/receipt/envelope.rs | 3 + .../op-consensus/src/transaction/envelope.rs | 20 +- crates/op-consensus/src/transaction/mod.rs | 3 + .../op-consensus/src/transaction/optimism.rs | 240 ++++++++++++++++++ crates/op-consensus/src/transaction/typed.rs | 22 +- 6 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 crates/op-consensus/src/transaction/optimism.rs diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 77bef5a6..814e007c 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -30,7 +30,7 @@ pub use receipt::{AnyReceiptEnvelope, Receipt, ReceiptEnvelope, ReceiptWithBloom mod transaction; pub use transaction::{ eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder, - SignableTransaction, SimpleCoder, Transaction, TxEip1559, TxEip2930, TxEip4844, + SignableTransaction, SimpleCoder, Transaction, TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction, }; diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index 332d215b..8d2a77b3 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -165,6 +165,9 @@ impl Decodable2718 for ReceiptEnvelope { TxType::Legacy => { Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) } + TxType::Deposit => { + Err(alloy_rlp::Error::Custom("deposit transactions are not supported")) + } } } diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index e0b89afb..b090dcc1 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -1,5 +1,6 @@ use crate::{ - Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, + Signed, TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, + TxLegacy, }; use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; @@ -23,6 +24,8 @@ pub enum TxType { Eip1559 = 2, /// EIP-4844 transaction type. Eip4844 = 3, + /// Optimism Deposit transaction type. + Deposit = 0x7E, } #[cfg(any(test, feature = "arbitrary"))] @@ -33,6 +36,7 @@ impl<'a> arbitrary::Arbitrary<'a> for TxType { 1 => TxType::Eip2930, 2 => TxType::Eip1559, 3 => TxType::Eip4844, + 0x7E => TxType::Deposit, _ => unreachable!(), }) } @@ -84,6 +88,9 @@ pub enum TxEnvelope { /// send transactions to the network. #[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))] Eip4844(Signed), + /// A [`TxDeposit`] tagged with type 0x7E. + #[cfg_attr(feature = "serde", serde(rename = "0x7E", alias = "0x7E"))] + Deposit(TxDeposit), } impl From> for TxEnvelope { @@ -128,6 +135,12 @@ impl From> for TxEnvelope { } } +impl From for TxEnvelope { + fn from(v: TxDeposit) -> Self { + Self::Deposit(v) + } +} + impl TxEnvelope { /// Return the [`TxType`] of the inner txn. pub const fn tx_type(&self) -> TxType { @@ -136,6 +149,7 @@ impl TxEnvelope { Self::Eip2930(_) => TxType::Eip2930, Self::Eip1559(_) => TxType::Eip1559, Self::Eip4844(_) => TxType::Eip4844, + Self::Deposit(_) => TxType::Deposit, } } @@ -167,6 +181,7 @@ impl TxEnvelope { outer_header.length() + outer_payload_length } }, + Self::Deposit(t) => t.fields_len(), } } @@ -210,6 +225,7 @@ impl Decodable2718 for TxEnvelope { TxType::Eip2930 => Ok(Self::Eip2930(TxEip2930::decode_signed_fields(buf)?)), TxType::Eip1559 => Ok(Self::Eip1559(TxEip1559::decode_signed_fields(buf)?)), TxType::Eip4844 => Ok(Self::Eip4844(TxEip4844Variant::decode_signed_fields(buf)?)), + TxType::Deposit => Ok(Self::Deposit(TxDeposit::decode(buf)?)), TxType::Legacy => { Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) } @@ -228,6 +244,7 @@ impl Encodable2718 for TxEnvelope { Self::Eip2930(_) => Some(TxType::Eip2930 as u8), Self::Eip1559(_) => Some(TxType::Eip1559 as u8), Self::Eip4844(_) => Some(TxType::Eip4844 as u8), + Self::Deposit(_) => Some(TxType::Deposit as u8), } } @@ -248,6 +265,7 @@ impl Encodable2718 for TxEnvelope { TxEnvelope::Eip4844(tx) => { tx.tx().encode_with_signature(tx.signature(), out, false); } + TxEnvelope::Deposit(tx) => tx.encode(out), } } } diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index 6d6c3755..84dece90 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -19,6 +19,9 @@ pub use eip4844::{ SimpleCoder, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, }; +mod optimism; +pub use optimism::TxDeposit; + mod envelope; pub use envelope::{TxEnvelope, TxType}; diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs new file mode 100644 index 00000000..18f6093a --- /dev/null +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -0,0 +1,240 @@ +use crate::Transaction; +use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256}; +use alloy_rlp::{ + Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE, +}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +/// Deposit transactions, also known as deposits are initiated on L1, and executed on L2. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct TxDeposit { + /// Hash that uniquely identifies the source of the deposit. + pub source_hash: B256, + /// The address of the sender account. + pub from: Address, + /// The address of the recipient account, or the null (zero-length) address if the deposited + /// transaction is a contract creation. + pub to: TxKind, + /// The ETH value to mint on L2. + pub mint: Option, + /// The ETH value to send to the recipient account. + pub value: U256, + /// The gas limit for the L2 transaction. + pub gas_limit: u64, + /// Field indicating if this transaction is exempt from the L2 gas limit. + pub is_system_transaction: bool, + /// Input has two uses depending if transaction is Create or Call (if `to` field is None or + /// Some). + pub input: Bytes, +} + +impl TxDeposit { + /// Decodes the inner [TxDeposit] fields from RLP bytes. + /// + /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following + /// RLP fields in the following order: + /// + /// - `source_hash` + /// - `from` + /// - `to` + /// - `mint` + /// - `value` + /// - `gas_limit` + /// - `is_system_transaction` + /// - `input` + pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + source_hash: Decodable::decode(buf)?, + from: Decodable::decode(buf)?, + to: Decodable::decode(buf)?, + mint: if *buf.first().ok_or(DecodeError::InputTooShort)? == EMPTY_STRING_CODE { + buf.advance(1); + None + } else { + Some(Decodable::decode(buf)?) + }, + value: Decodable::decode(buf)?, + gas_limit: Decodable::decode(buf)?, + is_system_transaction: Decodable::decode(buf)?, + input: Decodable::decode(buf)?, + }) + } + + /// Outputs the length of the transaction's fields, without a RLP header or length of the + /// eip155 fields. + pub(crate) fn fields_len(&self) -> usize { + self.source_hash.length() + + self.from.length() + + self.to.length() + + self.mint.map_or(1, |mint| mint.length()) + + self.value.length() + + self.gas_limit.length() + + self.is_system_transaction.length() + + self.input.0.length() + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + /// + pub(crate) fn encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) { + self.source_hash.encode(out); + self.from.encode(out); + self.to.encode(out); + if let Some(mint) = self.mint { + mint.encode(out); + } else { + out.put_u8(EMPTY_STRING_CODE); + } + self.value.encode(out); + self.gas_limit.encode(out); + self.is_system_transaction.encode(out); + self.input.encode(out); + } + + /// Calculates a heuristic for the in-memory size of the [TxDeposit] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // source_hash + mem::size_of::
() + // from + self.to.size() + // to + mem::size_of::>() + // mint + mem::size_of::() + // value + mem::size_of::() + // gas_limit + mem::size_of::() + // is_system_transaction + self.input.len() // input + } +} + +impl Transaction for TxDeposit { + fn input(&self) -> &[u8] { + &self.input + } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn chain_id(&self) -> Option { + None + } + + fn nonce(&self) -> u64 { + 0u64 + } + + fn gas_limit(&self) -> u128 { + self.gas_limit.into() + } + + fn gas_price(&self) -> Option { + None + } +} + +impl Encodable for TxDeposit { + fn encode(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.fields_len(); + Header { list: true, payload_length }.length() + payload_length + } +} + +impl Decodable for TxDeposit { + fn decode(data: &mut &[u8]) -> alloy_rlp::Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + if header.payload_length > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + Self::decode_fields(data) + } +} + +#[cfg(test)] +mod tests { + use super::*; + //use crate::TxEnvelope; + use alloy_primitives::hex; + use alloy_rlp::BytesMut; + + #[test] + fn test_rlp_roundtrip() { + let bytes = Bytes::from_static(&hex!("7ef9015aa044bae9d41b8380d781187b426c6fe43df5fb2fb57bd4466ef6a701e1f01e015694deaddeaddeaddeaddeaddeaddeaddeaddead000194420000000000000000000000000000000000001580808408f0d18001b90104015d8eb900000000000000000000000000000000000000000000000000000000008057650000000000000000000000000000000000000000000000000000000063d96d10000000000000000000000000000000000000000000000000000000000009f35273d89754a1e0387b89520d989d3be9c37c1f32495a88faf1ea05c61121ab0d1900000000000000000000000000000000000000000000000000000000000000010000000000000000000000002d679b567db6187c0c8323fa982cfb88b74dbcc7000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240")); + let tx_a = TxDeposit::decode(&mut bytes.as_ref()).unwrap(); + let mut buf_a = BytesMut::default(); + tx_a.encode(&mut buf_a); + assert_eq!(&buf_a[..], &bytes[..]); + } + + #[test] + fn test_encode_decode_fields() { + let original = TxDeposit { + source_hash: B256::default(), + from: Address::default(), + to: TxKind::default(), + mint: Some(100), + value: U256::default(), + gas_limit: 50000, + is_system_transaction: true, + input: Bytes::default(), + }; + + let mut buffer = BytesMut::new(); + original.encode_fields(&mut buffer); + let decoded = TxDeposit::decode(&mut &buffer[..]).expect("Failed to decode"); + + assert_eq!(original, decoded); + } + + #[test] + fn test_encode_with_and_without_header() { + let tx_deposit = TxDeposit { + source_hash: B256::default(), + from: Address::default(), + to: TxKind::default(), + mint: Some(100), + value: U256::default(), + gas_limit: 50000, + is_system_transaction: true, + input: Bytes::default(), + }; + + let mut buffer_with_header = BytesMut::new(); + tx_deposit.encode(&mut buffer_with_header); + + let mut buffer_without_header = BytesMut::new(); + tx_deposit.encode_fields(&mut buffer_without_header); + + assert!(buffer_with_header.len() > buffer_without_header.len()); + } + + #[test] + fn test_payload_length() { + let tx_deposit = TxDeposit { + source_hash: B256::default(), + from: Address::default(), + to: TxKind::default(), + mint: Some(100), + value: U256::default(), + gas_limit: 50000, + is_system_transaction: true, + input: Bytes::default(), + }; + + assert!(tx_deposit.size() > tx_deposit.fields_len()); + } +} diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index 441b0240..1ef0c294 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -1,4 +1,6 @@ -use crate::{Transaction, TxEip1559, TxEip2930, TxEip4844Variant, TxEnvelope, TxLegacy, TxType}; +use crate::{ + Transaction, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, TxEnvelope, TxLegacy, TxType, +}; use alloy_primitives::TxKind; /// The TypedTransaction enum represents all Ethereum transaction request types. @@ -24,6 +26,9 @@ pub enum TypedTransaction { /// EIP-4844 transaction #[cfg_attr(feature = "serde", serde(rename = "0x03", alias = "0x3"))] Eip4844(TxEip4844Variant), + /// Optimism deposit transaction + #[cfg_attr(feature = "serde", serde(rename = "0x7E", alias = "0x7E"))] + Deposit(TxDeposit), } impl From for TypedTransaction { @@ -50,6 +55,12 @@ impl From for TypedTransaction { } } +impl From for TypedTransaction { + fn from(tx: TxDeposit) -> Self { + Self::Deposit(tx) + } +} + impl From for TypedTransaction { fn from(envelope: TxEnvelope) -> Self { match envelope { @@ -57,6 +68,7 @@ impl From for TypedTransaction { TxEnvelope::Eip2930(tx) => Self::Eip2930(tx.strip_signature()), TxEnvelope::Eip1559(tx) => Self::Eip1559(tx.strip_signature()), TxEnvelope::Eip4844(tx) => Self::Eip4844(tx.strip_signature()), + TxEnvelope::Deposit(tx) => Self::Deposit(tx), } } } @@ -69,6 +81,7 @@ impl TypedTransaction { Self::Eip2930(_) => TxType::Eip2930, Self::Eip1559(_) => TxType::Eip1559, Self::Eip4844(_) => TxType::Eip4844, + Self::Deposit(_) => TxType::Deposit, } } @@ -104,6 +117,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.chain_id(), Self::Eip1559(tx) => tx.chain_id(), Self::Eip4844(tx) => tx.chain_id(), + Self::Deposit(tx) => tx.chain_id(), } } @@ -113,6 +127,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.gas_limit(), Self::Eip1559(tx) => tx.gas_limit(), Self::Eip4844(tx) => tx.gas_limit(), + Self::Deposit(tx) => tx.gas_limit(), } } @@ -122,6 +137,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.gas_price(), Self::Eip1559(tx) => tx.gas_price(), Self::Eip4844(tx) => tx.gas_price(), + Self::Deposit(tx) => tx.gas_price(), } } @@ -131,6 +147,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.input(), Self::Eip1559(tx) => tx.input(), Self::Eip4844(tx) => tx.input(), + Self::Deposit(tx) => tx.input(), } } @@ -140,6 +157,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.nonce(), Self::Eip1559(tx) => tx.nonce(), Self::Eip4844(tx) => tx.nonce(), + Self::Deposit(tx) => tx.nonce(), } } @@ -149,6 +167,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.to(), Self::Eip1559(tx) => tx.to(), Self::Eip4844(tx) => tx.to(), + Self::Deposit(tx) => tx.to(), } } @@ -158,6 +177,7 @@ impl Transaction for TypedTransaction { Self::Eip2930(tx) => tx.value(), Self::Eip1559(tx) => tx.value(), Self::Eip4844(tx) => tx.value(), + Self::Deposit(tx) => tx.value(), } } } From a6167c5fad64cdc86ae726002aeaba2bf34e95c3 Mon Sep 17 00:00:00 2001 From: clabby Date: Fri, 12 Apr 2024 22:15:32 -0400 Subject: [PATCH 04/20] feat(op-consensus): Trim and complete OP modifications --- Justfile | 42 -- crates/op-consensus/Cargo.toml | 4 +- crates/op-consensus/README.md | 23 +- crates/op-consensus/src/constants.rs | 72 --- crates/op-consensus/src/header.rs | 467 ------------------ crates/op-consensus/src/lib.rs | 16 +- crates/op-consensus/src/receipt/any.rs | 99 ---- crates/op-consensus/src/receipt/envelope.rs | 96 ++-- crates/op-consensus/src/receipt/mod.rs | 35 +- crates/op-consensus/src/receipt/receipts.rs | 112 ++++- crates/op-consensus/src/sealed.rs | 68 --- .../op-consensus/src/transaction/eip1559.rs | 8 +- .../op-consensus/src/transaction/eip2930.rs | 14 +- .../op-consensus/src/transaction/eip4844.rs | 28 +- .../op-consensus/src/transaction/envelope.rs | 179 ++++--- crates/op-consensus/src/transaction/legacy.rs | 4 +- crates/op-consensus/src/transaction/mod.rs | 8 +- .../op-consensus/src/transaction/optimism.rs | 26 +- crates/op-consensus/src/transaction/typed.rs | 49 +- 19 files changed, 372 insertions(+), 978 deletions(-) delete mode 100644 Justfile delete mode 100644 crates/op-consensus/src/constants.rs delete mode 100644 crates/op-consensus/src/header.rs delete mode 100644 crates/op-consensus/src/receipt/any.rs delete mode 100644 crates/op-consensus/src/sealed.rs diff --git a/Justfile b/Justfile deleted file mode 100644 index e43efabe..00000000 --- a/Justfile +++ /dev/null @@ -1,42 +0,0 @@ -set positional-arguments - -# default recipe to display help information -default: - @just --list - -# Run all ci checks for the native target -ci: lint-native lint-docs test - -# Test for the native target -test *args='': - cargo nextest run --workspace --all $@ - -# Lint the workspace for all available targets -lint: lint-native lint-docs - -# Fixes the formatting of the workspace -fmt-native-fix: - cargo +nightly fmt --all - -# Check the formatting of the workspace -fmt-native-check: - cargo +nightly fmt --all -- --check - -# Lint the workspace -lint-native: fmt-native-check - cargo +nightly clippy --workspace --all --all-features --all-targets -- -D warnings - -# Lint the Rust documentation -lint-docs: - RUSTDOCFLAGS="-D warnings" cargo doc --all --no-deps --document-private-items - -# Test the Rust documentation -test-docs: - cargo test --doc --all --locked - -# Build the workspace for all available targets -build: build-native - -# Build for the native target -build-native *args='': - cargo build --workspace --all $@ diff --git a/crates/op-consensus/Cargo.toml b/crates/op-consensus/Cargo.toml index 9b8db4ab..225f4e22 100644 --- a/crates/op-consensus/Cargo.toml +++ b/crates/op-consensus/Cargo.toml @@ -12,7 +12,7 @@ repository.workspace = true exclude.workspace = true [dependencies] -alloy-primitives = { workspace = true, features = ["rlp", "rand"] } +alloy-primitives = { workspace = true, features = ["rlp"] } alloy-rlp.workspace = true alloy-eips.workspace = true alloy-serde = { workspace = true, optional = true } @@ -41,5 +41,5 @@ default = ["std"] std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"] k256 = ["alloy-primitives/k256"] kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"] -arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary", "alloy-primitives/arbitrary"] +arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"] serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"] diff --git a/crates/op-consensus/README.md b/crates/op-consensus/README.md index ff033c23..f20dff09 100644 --- a/crates/op-consensus/README.md +++ b/crates/op-consensus/README.md @@ -1,28 +1,21 @@ -# alloy-consensus +# op-alloy-consensus -Ethereum consensus interface. +OP Stack consensus interface. -This crate contains constants, types, and functions for implementing Ethereum -EL consensus and communication. This includes headers, blocks, transactions, -[EIP-2718] envelopes, [EIP-2930], [EIP-4844], and more. +This crate contains constants, types, and functions for implementing Optimism EL consensus and communication. This +includes transactions, [EIP-2718] envelopes, [EIP-2930], [EIP-4844], [deposit transactions][deposit], and receipts. -In general a type belongs in this crate if it is committed to in the EL block -header. This includes: - -- transactions -- blocks -- headers -- receipts -- [EIP-2718] envelopes. +In general a type belongs in this crate if it exists in the `alloy-consensus` crate, but was modified from the base Ethereum protocol in the OP Stack. +For consensus types that are not modified by the OP Stack, the `alloy-consensus` types should be used instead. [alloy-network]: ../network [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 +[deposit]: https://specs.optimism.io/protocol/deposits.html ## Provenance -Much of this code was ported from [reth-primitives] as part of ongoing alloy -migrations. +Much of this code was ported from [reth-primitives] as part of ongoing alloy migrations. [reth-primitives]: https://github.com/paradigmxyz/reth/tree/main/crates/primitives diff --git a/crates/op-consensus/src/constants.rs b/crates/op-consensus/src/constants.rs deleted file mode 100644 index 764ca25f..00000000 --- a/crates/op-consensus/src/constants.rs +++ /dev/null @@ -1,72 +0,0 @@ -//! Ethereum protocol-related constants -use alloy_primitives::{address, b256, Address, B256}; - -/// The first four bytes of the call data for a function call specifies the function to be called. -pub const SELECTOR_LEN: usize = 4; - -/// Maximum extra data size in a block after genesis -pub const MAXIMUM_EXTRA_DATA_SIZE: usize = 32; - -/// Multiplier for converting gwei to wei. -pub const GWEI_TO_WEI: u64 = 1_000_000_000; - -/// Multiplier for converting finney (milliether) to wei. -pub const FINNEY_TO_WEI: u128 = (GWEI_TO_WEI as u128) * 1_000_000; - -/// Multiplier for converting ether to wei. -pub const ETH_TO_WEI: u128 = FINNEY_TO_WEI * 1000; - -/// Multiplier for converting mgas to gas. -pub const MGAS_TO_GAS: u64 = 1_000_000u64; - -/// The Ethereum mainnet genesis hash. -pub const MAINNET_GENESIS_HASH: B256 = - b256!("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"); - -/// Goerli genesis hash. -pub const GOERLI_GENESIS_HASH: B256 = - b256!("bf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a"); - -/// Sepolia genesis hash. -pub const SEPOLIA_GENESIS_HASH: B256 = - b256!("25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9"); - -/// Holesky genesis hash. -pub const HOLESKY_GENESIS_HASH: B256 = - b256!("ff9006519a8ce843ac9c28549d24211420b546e12ce2d170c77a8cca7964f23d"); - -/// Testnet genesis hash. -pub const DEV_GENESIS_HASH: B256 = - b256!("2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c"); - -/// Optimism goerli genesis hash. -pub const GOERLI_OP_GENESIS: B256 = - b256!("c1fc15cd51159b1f1e5cbc4b82e85c1447ddfa33c52cf1d98d14fba0d6354be1"); - -/// Base goerli genesis hash. -pub const GOERLI_BASE_GENESIS: B256 = - b256!("a3ab140f15ea7f7443a4702da64c10314eb04d488e72974e02e2d728096b4f76"); - -/// Keccak256 over empty array. -pub const KECCAK_EMPTY: B256 = - b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); - -/// Ommer root of empty list. -pub const EMPTY_OMMER_ROOT_HASH: B256 = - b256!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); - -/// Root hash of an empty trie. -pub const EMPTY_ROOT_HASH: B256 = - b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); - -/// Transactions root of empty receipts set. -pub const EMPTY_RECEIPTS: B256 = EMPTY_ROOT_HASH; - -/// Transactions root of empty transactions set. -pub const EMPTY_TRANSACTIONS: B256 = EMPTY_ROOT_HASH; - -/// Withdrawals root of empty withdrawals set. -pub const EMPTY_WITHDRAWALS: B256 = EMPTY_ROOT_HASH; - -/// The address for the beacon roots contract defined in EIP-4788. -pub const BEACON_ROOTS_ADDRESS: Address = address!("000F3df6D732807Ef1319fB7B8bB8522d0Beac02"); diff --git a/crates/op-consensus/src/header.rs b/crates/op-consensus/src/header.rs deleted file mode 100644 index 252f0516..00000000 --- a/crates/op-consensus/src/header.rs +++ /dev/null @@ -1,467 +0,0 @@ -use crate::Sealable; -use alloy_eips::{ - eip1559::{calc_next_block_base_fee, BaseFeeParams}, - eip4844::{calc_blob_gasprice, calc_excess_blob_gas}, -}; -use alloy_primitives::{b256, keccak256, Address, BlockNumber, Bloom, Bytes, B256, B64, U256}; -use alloy_rlp::{ - length_of_length, Buf, BufMut, Decodable, Encodable, EMPTY_LIST_CODE, EMPTY_STRING_CODE, -}; -use core::mem; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -/// Ommer root of empty list. -pub const EMPTY_OMMER_ROOT_HASH: B256 = - b256!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); - -/// Root hash of an empty trie. -pub const EMPTY_ROOT_HASH: B256 = - b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); - -/// Ethereum Block header -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Header { - /// The Keccak 256-bit hash of the parent - /// block’s header, in its entirety; formally Hp. - pub parent_hash: B256, - /// The Keccak 256-bit hash of the ommers list portion of this block; formally Ho. - pub ommers_hash: B256, - /// The 160-bit address to which all fees collected from the successful mining of this block - /// be transferred; formally Hc. - pub beneficiary: Address, - /// The Keccak 256-bit hash of the root node of the state trie, after all transactions are - /// executed and finalisations applied; formally Hr. - pub state_root: B256, - /// The Keccak 256-bit hash of the root node of the trie structure populated with each - /// transaction in the transactions list portion of the block; formally Ht. - pub transactions_root: B256, - /// The Keccak 256-bit hash of the root node of the trie structure populated with the receipts - /// of each transaction in the transactions list portion of the block; formally He. - pub receipts_root: B256, - /// The Keccak 256-bit hash of the withdrawals list portion of this block. - /// - pub withdrawals_root: Option, - /// The Bloom filter composed from indexable information (logger address and log topics) - /// contained in each log entry from the receipt of each transaction in the transactions list; - /// formally Hb. - pub logs_bloom: Bloom, - /// A scalar value corresponding to the difficulty level of this block. This can be calculated - /// from the previous block’s difficulty level and the timestamp; formally Hd. - pub difficulty: U256, - /// A scalar value equal to the number of ancestor blocks. The genesis block has a number of - /// zero; formally Hi. - pub number: BlockNumber, - /// A scalar value equal to the current limit of gas expenditure per block; formally Hl. - pub gas_limit: u128, - /// A scalar value equal to the total gas used in transactions in this block; formally Hg. - pub gas_used: u128, - /// A scalar value equal to the reasonable output of Unix’s time() at this block’s inception; - /// formally Hs. - pub timestamp: u64, - /// A 256-bit hash which, combined with the - /// nonce, proves that a sufficient amount of computation has been carried out on this block; - /// formally Hm. - pub mix_hash: B256, - /// A 64-bit value which, combined with the mixhash, proves that a sufficient amount of - /// computation has been carried out on this block; formally Hn. - pub nonce: u64, - /// A scalar representing EIP1559 base fee which can move up or down each block according - /// to a formula which is a function of gas used in parent block and gas target - /// (block gas limit divided by elasticity multiplier) of parent block. - /// The algorithm results in the base fee per gas increasing when blocks are - /// above the gas target, and decreasing when blocks are below the gas target. The base fee per - /// gas is burned. - pub base_fee_per_gas: Option, - /// The total amount of blob gas consumed by the transactions within the block, added in - /// EIP-4844. - pub blob_gas_used: Option, - /// A running total of blob gas consumed in excess of the target, prior to the block. Blocks - /// with above-target blob gas consumption increase this value, blocks with below-target blob - /// gas consumption decrease it (bounded at 0). This was added in EIP-4844. - pub excess_blob_gas: Option, - /// The hash of the parent beacon block's root is included in execution blocks, as proposed by - /// EIP-4788. - /// - /// This enables trust-minimized access to consensus state, supporting staking pools, bridges, - /// and more. - /// - /// The beacon roots contract handles root storage, enhancing Ethereum's functionalities. - pub parent_beacon_block_root: Option, - /// An arbitrary byte array containing data relevant to this block. This must be 32 bytes or - /// fewer; formally Hx. - pub extra_data: Bytes, -} - -impl Default for Header { - fn default() -> Self { - Header { - parent_hash: Default::default(), - ommers_hash: EMPTY_OMMER_ROOT_HASH, - beneficiary: Default::default(), - state_root: EMPTY_ROOT_HASH, - transactions_root: EMPTY_ROOT_HASH, - receipts_root: EMPTY_ROOT_HASH, - logs_bloom: Default::default(), - difficulty: Default::default(), - number: 0, - gas_limit: 0, - gas_used: 0, - timestamp: 0, - extra_data: Default::default(), - mix_hash: Default::default(), - nonce: 0, - base_fee_per_gas: None, - withdrawals_root: None, - blob_gas_used: None, - excess_blob_gas: None, - parent_beacon_block_root: None, - } - } -} - -impl Sealable for Header { - fn hash(&self) -> B256 { - self.hash_slow() - } -} - -impl Header { - // TODO: re-enable - - // /// Returns the parent block's number and hash - // pub fn parent_num_hash(&self) -> BlockNumHash { - // BlockNumHash { number: self.number.saturating_sub(1), hash: self.parent_hash } - // } - - /// Heavy function that will calculate hash of data and will *not* save the change to metadata. - /// - /// Use [`Header::seal_slow`] and unlock if you need the hash to be persistent. - pub fn hash_slow(&self) -> B256 { - let mut out = Vec::::new(); - self.encode(&mut out); - keccak256(&out) - } - - /// Checks if the header is empty - has no transactions and no ommers - pub fn is_empty(&self) -> bool { - let txs_and_ommers_empty = self.transaction_root_is_empty() && self.ommers_hash_is_empty(); - if let Some(withdrawals_root) = self.withdrawals_root { - txs_and_ommers_empty && withdrawals_root == EMPTY_ROOT_HASH - } else { - txs_and_ommers_empty - } - } - - /// Check if the ommers hash equals to empty hash list. - pub fn ommers_hash_is_empty(&self) -> bool { - self.ommers_hash == EMPTY_OMMER_ROOT_HASH - } - - /// Check if the transaction root equals to empty root. - pub fn transaction_root_is_empty(&self) -> bool { - self.transactions_root == EMPTY_ROOT_HASH - } - - // TODO: re-enable - - // /// Converts all roots in the header to a [BlockBodyRoots] struct. - // pub fn body_roots(&self) -> BlockBodyRoots { - // BlockBodyRoots { - // tx_root: self.transactions_root, - // ommers_hash: self.ommers_hash, - // withdrawals_root: self.withdrawals_root, - // } - // } - - /// Returns the blob fee for _this_ block according to the EIP-4844 spec. - /// - /// Returns `None` if `excess_blob_gas` is None - pub fn blob_fee(&self) -> Option { - self.excess_blob_gas.map(calc_blob_gasprice) - } - - /// Returns the blob fee for the next block according to the EIP-4844 spec. - /// - /// Returns `None` if `excess_blob_gas` is None. - /// - /// See also [Self::next_block_excess_blob_gas] - pub fn next_block_blob_fee(&self) -> Option { - self.next_block_excess_blob_gas().map(calc_blob_gasprice) - } - - /// Calculate base fee for next block according to the EIP-1559 spec. - /// - /// Returns a `None` if no base fee is set, no EIP-1559 support - pub fn next_block_base_fee(&self, base_fee_params: BaseFeeParams) -> Option { - Some(calc_next_block_base_fee( - self.gas_used, - self.gas_limit, - self.base_fee_per_gas?, - base_fee_params, - )) - } - - /// Calculate excess blob gas for the next block according to the EIP-4844 - /// spec. - /// - /// Returns a `None` if no excess blob gas is set, no EIP-4844 support - pub fn next_block_excess_blob_gas(&self) -> Option { - Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) - } - - /// Calculate a heuristic for the in-memory size of the [Header]. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::() + // parent hash - mem::size_of::() + // ommers hash - mem::size_of::
() + // beneficiary - mem::size_of::() + // state root - mem::size_of::() + // transactions root - mem::size_of::() + // receipts root - mem::size_of::>() + // withdrawals root - mem::size_of::() + // logs bloom - mem::size_of::() + // difficulty - mem::size_of::() + // number - mem::size_of::() + // gas limit - mem::size_of::() + // gas used - mem::size_of::() + // timestamp - mem::size_of::() + // mix hash - mem::size_of::() + // nonce - mem::size_of::>() + // base fee per gas - mem::size_of::>() + // blob gas used - mem::size_of::>() + // excess blob gas - mem::size_of::>() + // parent beacon block root - self.extra_data.len() // extra data - } - - fn header_payload_length(&self) -> usize { - let mut length = 0; - length += self.parent_hash.length(); - length += self.ommers_hash.length(); - length += self.beneficiary.length(); - length += self.state_root.length(); - length += self.transactions_root.length(); - length += self.receipts_root.length(); - length += self.logs_bloom.length(); - length += self.difficulty.length(); - length += U256::from(self.number).length(); - length += U256::from(self.gas_limit).length(); - length += U256::from(self.gas_used).length(); - length += self.timestamp.length(); - length += self.extra_data.length(); - length += self.mix_hash.length(); - length += B64::new(self.nonce.to_be_bytes()).length(); - - if let Some(base_fee) = self.base_fee_per_gas { - length += U256::from(base_fee).length(); - } else if self.withdrawals_root.is_some() - || self.blob_gas_used.is_some() - || self.excess_blob_gas.is_some() - || self.parent_beacon_block_root.is_some() - { - length += 1; // EMPTY LIST CODE - } - - if let Some(root) = self.withdrawals_root { - length += root.length(); - } else if self.blob_gas_used.is_some() - || self.excess_blob_gas.is_some() - || self.parent_beacon_block_root.is_some() - { - length += 1; // EMPTY STRING CODE - } - - if let Some(blob_gas_used) = self.blob_gas_used { - length += U256::from(blob_gas_used).length(); - } else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() { - length += 1; // EMPTY LIST CODE - } - - if let Some(excess_blob_gas) = self.excess_blob_gas { - length += U256::from(excess_blob_gas).length(); - } else if self.parent_beacon_block_root.is_some() { - length += 1; // EMPTY LIST CODE - } - - // Encode parent beacon block root length. If new fields are added, the above pattern will - // need to be repeated and placeholder length added. Otherwise, it's impossible to - // tell _which_ fields are missing. This is mainly relevant for contrived cases - // where a header is created at random, for example: - // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are - // post-London, so this is technically not valid. However, a tool like proptest would - // generate a block like this. - if let Some(parent_beacon_block_root) = self.parent_beacon_block_root { - length += parent_beacon_block_root.length(); - } - - length - } -} - -impl Encodable for Header { - fn encode(&self, out: &mut dyn BufMut) { - let list_header = - alloy_rlp::Header { list: true, payload_length: self.header_payload_length() }; - list_header.encode(out); - self.parent_hash.encode(out); - self.ommers_hash.encode(out); - self.beneficiary.encode(out); - self.state_root.encode(out); - self.transactions_root.encode(out); - self.receipts_root.encode(out); - self.logs_bloom.encode(out); - self.difficulty.encode(out); - U256::from(self.number).encode(out); - U256::from(self.gas_limit).encode(out); - U256::from(self.gas_used).encode(out); - self.timestamp.encode(out); - self.extra_data.encode(out); - self.mix_hash.encode(out); - B64::new(self.nonce.to_be_bytes()).encode(out); - - // Encode base fee. Put empty list if base fee is missing, - // but withdrawals root is present. - if let Some(ref base_fee) = self.base_fee_per_gas { - U256::from(*base_fee).encode(out); - } else if self.withdrawals_root.is_some() - || self.blob_gas_used.is_some() - || self.excess_blob_gas.is_some() - || self.parent_beacon_block_root.is_some() - { - out.put_u8(EMPTY_LIST_CODE); - } - - // Encode withdrawals root. Put empty string if withdrawals root is missing, - // but blob gas used is present. - if let Some(ref root) = self.withdrawals_root { - root.encode(out); - } else if self.blob_gas_used.is_some() - || self.excess_blob_gas.is_some() - || self.parent_beacon_block_root.is_some() - { - out.put_u8(EMPTY_STRING_CODE); - } - - // Encode blob gas used. Put empty list if blob gas used is missing, - // but excess blob gas is present. - if let Some(ref blob_gas_used) = self.blob_gas_used { - U256::from(*blob_gas_used).encode(out); - } else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() { - out.put_u8(EMPTY_LIST_CODE); - } - - // Encode excess blob gas. Put empty list if excess blob gas is missing, - // but parent beacon block root is present. - if let Some(ref excess_blob_gas) = self.excess_blob_gas { - U256::from(*excess_blob_gas).encode(out); - } else if self.parent_beacon_block_root.is_some() { - out.put_u8(EMPTY_LIST_CODE); - } - - // Encode parent beacon block root. If new fields are added, the above pattern will need to - // be repeated and placeholders added. Otherwise, it's impossible to tell _which_ - // fields are missing. This is mainly relevant for contrived cases where a header is - // created at random, for example: - // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are - // post-London, so this is technically not valid. However, a tool like proptest would - // generate a block like this. - if let Some(ref parent_beacon_block_root) = self.parent_beacon_block_root { - parent_beacon_block_root.encode(out); - } - } - - fn length(&self) -> usize { - let mut length = 0; - length += self.header_payload_length(); - length += length_of_length(length); - length - } -} - -impl Decodable for Header { - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - let rlp_head = alloy_rlp::Header::decode(buf)?; - if !rlp_head.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - let started_len = buf.len(); - let mut this = Self { - parent_hash: Decodable::decode(buf)?, - ommers_hash: Decodable::decode(buf)?, - beneficiary: Decodable::decode(buf)?, - state_root: Decodable::decode(buf)?, - transactions_root: Decodable::decode(buf)?, - receipts_root: Decodable::decode(buf)?, - logs_bloom: Decodable::decode(buf)?, - difficulty: Decodable::decode(buf)?, - number: u64::decode(buf)?, - gas_limit: u128::decode(buf)?, - gas_used: u128::decode(buf)?, - timestamp: Decodable::decode(buf)?, - extra_data: Decodable::decode(buf)?, - mix_hash: Decodable::decode(buf)?, - nonce: u64::from_be_bytes(B64::decode(buf)?.0), - base_fee_per_gas: None, - withdrawals_root: None, - blob_gas_used: None, - excess_blob_gas: None, - parent_beacon_block_root: None, - }; - - if started_len - buf.len() < rlp_head.payload_length { - if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { - buf.advance(1) - } else { - this.base_fee_per_gas = Some(U256::decode(buf)?.to::()); - } - } - - // Withdrawals root for post-shanghai headers - if started_len - buf.len() < rlp_head.payload_length { - if buf.first().map(|b| *b == EMPTY_STRING_CODE).unwrap_or_default() { - buf.advance(1) - } else { - this.withdrawals_root = Some(Decodable::decode(buf)?); - } - } - - // Blob gas used and excess blob gas for post-cancun headers - if started_len - buf.len() < rlp_head.payload_length { - if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { - buf.advance(1) - } else { - this.blob_gas_used = Some(U256::decode(buf)?.to::()); - } - } - - if started_len - buf.len() < rlp_head.payload_length { - if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { - buf.advance(1) - } else { - this.excess_blob_gas = Some(U256::decode(buf)?.to::()); - } - } - - // Decode parent beacon block root. If new fields are added, the above pattern will need to - // be repeated and placeholders decoded. Otherwise, it's impossible to tell _which_ - // fields are missing. This is mainly relevant for contrived cases where a header is - // created at random, for example: - // * A header is created with a withdrawals root, but no base fee. Shanghai blocks are - // post-London, so this is technically not valid. However, a tool like proptest would - // generate a block like this. - if started_len - buf.len() < rlp_head.payload_length { - this.parent_beacon_block_root = Some(B256::decode(buf)?); - } - - let consumed = started_len - buf.len(); - if consumed != rlp_head.payload_length { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: rlp_head.payload_length, - got: consumed, - }); - } - Ok(this) - } -} diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 814e007c..2015b570 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -19,19 +19,14 @@ #[cfg(not(feature = "std"))] extern crate alloc; -pub mod constants; - -mod header; -pub use header::{Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH}; - mod receipt; -pub use receipt::{AnyReceiptEnvelope, Receipt, ReceiptEnvelope, ReceiptWithBloom, TxReceipt}; +pub use receipt::{OpReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; mod transaction; pub use transaction::{ - eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder, - SignableTransaction, SimpleCoder, Transaction, TxDeposit, TxEip1559, TxEip2930, TxEip4844, - TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction, + eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTransaction, OpTxEnvelope, OpTxType, + OpTypedTransaction, SidecarBuilder, SidecarCoder, SignableTransaction, SimpleCoder, TxDeposit, + TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, }; #[cfg(feature = "kzg")] @@ -40,8 +35,5 @@ pub use transaction::BlobTransactionValidationError; #[cfg(feature = "kzg")] pub use alloy_eips::eip4844::env_settings::EnvKzgSettings; -mod sealed; -pub use sealed::{Sealable, Sealed}; - mod signed; pub use signed::Signed; diff --git a/crates/op-consensus/src/receipt/any.rs b/crates/op-consensus/src/receipt/any.rs deleted file mode 100644 index 35482caa..00000000 --- a/crates/op-consensus/src/receipt/any.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::ReceiptWithBloom; -use alloy_eips::eip2718::{Decodable2718, Encodable2718}; -use alloy_primitives::{bytes::BufMut, Bloom, Log}; -use alloy_rlp::{Decodable, Encodable}; - -/// Receipt envelope, as defined in [EIP-2718]. -/// -/// This enum distinguishes between tagged and untagged legacy receipts, as the -/// in-protocol merkle tree may commit to EITHER 0-prefixed or raw. Therefore -/// we must ensure that encoding returns the precise byte-array that was -/// decoded, preserving the presence or absence of the `TransactionType` flag. -/// -/// Transaction receipt payloads are specified in their respective EIPs. -/// -/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 -#[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct AnyReceiptEnvelope { - /// The receipt envelope. - #[cfg_attr(feature = "serde", serde(flatten))] - pub inner: ReceiptWithBloom, - /// The transaction type. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u8_hex"))] - pub r#type: u8, -} - -impl AnyReceiptEnvelope { - /// Returns whether this is a legacy receipt (type 0) - pub const fn is_legacy(&self) -> bool { - self.r#type == 0 - } - - /// Calculate the length of the rlp payload of the network encoded receipt. - pub fn rlp_payload_length(&self) -> usize { - let length = self.inner.length(); - if self.is_legacy() { - length - } else { - length + 1 - } - } - - /// Return true if the transaction was successful. - pub const fn is_success(&self) -> bool { - self.status() - } - - /// Returns the success status of the receipt's transaction. - pub const fn status(&self) -> bool { - self.inner.receipt.status - } - - /// Returns the cumulative gas used at this receipt. - pub const fn cumulative_gas_used(&self) -> u128 { - self.inner.receipt.cumulative_gas_used - } - - /// Return the receipt logs. - pub fn logs(&self) -> &[Log] { - &self.inner.receipt.logs - } - - /// Return the receipt's bloom. - pub const fn logs_bloom(&self) -> &Bloom { - &self.inner.logs_bloom - } -} - -impl Encodable2718 for AnyReceiptEnvelope { - fn type_flag(&self) -> Option { - match self.r#type { - 0 => None, - ty => Some(ty), - } - } - - fn encode_2718_len(&self) -> usize { - self.inner.length() + !self.is_legacy() as usize - } - - fn encode_2718(&self, out: &mut dyn BufMut) { - match self.type_flag() { - None => {} - Some(ty) => out.put_u8(ty), - } - self.inner.encode(out); - } -} - -impl Decodable2718 for AnyReceiptEnvelope { - fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { - let receipt = Decodable::decode(buf)?; - Ok(Self { inner: receipt, r#type: ty }) - } - - fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { - Self::typed_decode(0, buf) - } -} diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index 8d2a77b3..f6b24247 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -1,9 +1,9 @@ -use crate::{Receipt, ReceiptWithBloom, TxType}; +use crate::{OpReceipt, OpReceiptWithBloom, OpTxType}; use alloy_eips::eip2718::{Decodable2718, Encodable2718}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; -/// Receipt envelope, as defined in [EIP-2718]. +/// Receipt envelope, as defined in [EIP-2718], modified for OP Stack chains. /// /// This enum distinguishes between tagged and untagged legacy receipts, as the /// in-protocol merkle tree may commit to EITHER 0-prefixed or raw. Therefore @@ -17,35 +17,41 @@ use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(tag = "type"))] #[non_exhaustive] -pub enum ReceiptEnvelope { +pub enum OpReceiptEnvelope { /// Receipt envelope with no type flag. #[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))] - Legacy(ReceiptWithBloom), + Legacy(OpReceiptWithBloom), /// Receipt envelope with type flag 1, containing a [EIP-2930] receipt. /// /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 #[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))] - Eip2930(ReceiptWithBloom), + Eip2930(OpReceiptWithBloom), /// Receipt envelope with type flag 2, containing a [EIP-1559] receipt. /// /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559 #[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))] - Eip1559(ReceiptWithBloom), - /// Receipt envelope with type flag 2, containing a [EIP-4844] receipt. + Eip1559(OpReceiptWithBloom), + /// Receipt envelope with type flag 3, containing a [EIP-4844] receipt. /// /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 #[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))] - Eip4844(ReceiptWithBloom), + Eip4844(OpReceiptWithBloom), + /// Receipt envelope with type flag 126, containing a [deposit] receipt. + /// + /// [deposit]: https://specs.optimism.io/protocol/deposits.html + #[cfg_attr(feature = "serde", serde(rename = "0x7E", alias = "0x7E"))] + Deposit(OpReceiptWithBloom), } -impl ReceiptEnvelope { +impl OpReceiptEnvelope { /// Return the [`TxType`] of the inner receipt. - pub const fn tx_type(&self) -> TxType { + pub const fn tx_type(&self) -> OpTxType { match self { - Self::Legacy(_) => TxType::Legacy, - Self::Eip2930(_) => TxType::Eip2930, - Self::Eip1559(_) => TxType::Eip1559, - Self::Eip4844(_) => TxType::Eip4844, + Self::Legacy(_) => OpTxType::Legacy, + Self::Eip2930(_) => OpTxType::Eip2930, + Self::Eip1559(_) => OpTxType::Eip1559, + Self::Eip4844(_) => OpTxType::Eip4844, + Self::Deposit(_) => OpTxType::Deposit, } } @@ -74,26 +80,42 @@ impl ReceiptEnvelope { &self.as_receipt_with_bloom().unwrap().logs_bloom } + /// Return the receipt's deposit_nonce. + pub fn deposit_nonce(&self) -> Option { + self.as_receipt().unwrap().deposit_nonce + } + + /// Return the receipt's deposit version. + pub fn deposit_receipt_version(&self) -> Option { + self.as_receipt().unwrap().deposit_receipt_version + } + /// Return the inner receipt with bloom. Currently this is infallible, /// however, future receipt types may be added. - pub const fn as_receipt_with_bloom(&self) -> Option<&ReceiptWithBloom> { + pub const fn as_receipt_with_bloom(&self) -> Option<&OpReceiptWithBloom> { match self { - Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => Some(t), + Self::Legacy(t) + | Self::Eip2930(t) + | Self::Eip1559(t) + | Self::Eip4844(t) + | Self::Deposit(t) => Some(t), } } /// Return the inner receipt. Currently this is infallible, however, future /// receipt types may be added. - pub const fn as_receipt(&self) -> Option<&Receipt> { + pub const fn as_receipt(&self) -> Option<&OpReceipt> { match self { - Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => { - Some(&t.receipt) - } + Self::Legacy(t) + | Self::Eip2930(t) + | Self::Eip1559(t) + | Self::Eip4844(t) + | Self::Deposit(t) => Some(&t.receipt), } } } -impl ReceiptEnvelope { +impl OpReceiptEnvelope { /// Get the length of the inner receipt in the 2718 encoding. pub fn inner_length(&self) -> usize { self.as_receipt_with_bloom().unwrap().length() @@ -109,7 +131,7 @@ impl ReceiptEnvelope { } } -impl Encodable for ReceiptEnvelope { +impl Encodable for OpReceiptEnvelope { fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { self.network_encode(out) } @@ -123,7 +145,7 @@ impl Encodable for ReceiptEnvelope { } } -impl Decodable for ReceiptEnvelope { +impl Decodable for OpReceiptEnvelope { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { match Self::network_decode(buf) { Ok(t) => Ok(t), @@ -132,13 +154,14 @@ impl Decodable for ReceiptEnvelope { } } -impl Encodable2718 for ReceiptEnvelope { +impl Encodable2718 for OpReceiptEnvelope { fn type_flag(&self) -> Option { match self { Self::Legacy(_) => None, - Self::Eip2930(_) => Some(TxType::Eip2930 as u8), - Self::Eip1559(_) => Some(TxType::Eip1559 as u8), - Self::Eip4844(_) => Some(TxType::Eip4844 as u8), + Self::Eip2930(_) => Some(OpTxType::Eip2930 as u8), + Self::Eip1559(_) => Some(OpTxType::Eip1559 as u8), + Self::Eip4844(_) => Some(OpTxType::Eip4844 as u8), + Self::Deposit(_) => Some(OpTxType::Deposit as u8), } } @@ -155,19 +178,17 @@ impl Encodable2718 for ReceiptEnvelope { } } -impl Decodable2718 for ReceiptEnvelope { +impl Decodable2718 for OpReceiptEnvelope { fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { let receipt = Decodable::decode(buf)?; match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? { - TxType::Eip2930 => Ok(Self::Eip2930(receipt)), - TxType::Eip1559 => Ok(Self::Eip1559(receipt)), - TxType::Eip4844 => Ok(Self::Eip4844(receipt)), - TxType::Legacy => { + OpTxType::Legacy => { Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) } - TxType::Deposit => { - Err(alloy_rlp::Error::Custom("deposit transactions are not supported")) - } + OpTxType::Eip2930 => Ok(Self::Eip2930(receipt)), + OpTxType::Eip1559 => Ok(Self::Eip1559(receipt)), + OpTxType::Eip4844 => Ok(Self::Eip4844(receipt)), + OpTxType::Deposit => Ok(Self::Deposit(receipt)), } } @@ -177,18 +198,19 @@ impl Decodable2718 for ReceiptEnvelope { } #[cfg(all(test, feature = "arbitrary"))] -impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptEnvelope +impl<'a, T> arbitrary::Arbitrary<'a> for OpReceiptEnvelope where T: arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - let receipt = ReceiptWithBloom::::arbitrary(u)?; + let receipt = OpReceiptWithBloom::::arbitrary(u)?; match u.int_in_range(0..=3)? { 0 => Ok(Self::Legacy(receipt)), 1 => Ok(Self::Eip2930(receipt)), 2 => Ok(Self::Eip1559(receipt)), 3 => Ok(Self::Eip4844(receipt)), + 0x7E => Ok(Self::Deposit(receipt)), _ => unreachable!(), } } diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index 3eda915b..fdd4eb5c 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -1,16 +1,13 @@ use alloy_primitives::{Bloom, Log}; -mod any; -pub use any::AnyReceiptEnvelope; - mod envelope; -pub use envelope::ReceiptEnvelope; +pub use envelope::OpReceiptEnvelope; mod receipts; -pub use receipts::{Receipt, ReceiptWithBloom}; +pub use receipts::{OpReceipt, OpReceiptWithBloom}; /// Receipt is the result of a transaction execution. -pub trait TxReceipt { +pub trait OpTxReceipt { /// Returns true if the transaction was successful. fn success(&self) -> bool; @@ -29,6 +26,12 @@ pub trait TxReceipt { /// Returns the logs emitted by this transaction. fn logs(&self) -> &[Log]; + + /// Returns the deposit nonce of the transaction. + fn deposit_nonce(&self) -> Option; + + /// Returns the deposit receipt version of the transaction. + fn deposit_receipt_version(&self) -> Option; } #[cfg(test)] @@ -45,8 +48,8 @@ mod tests { let mut data = vec![]; let receipt = - ReceiptEnvelope::Legacy(ReceiptWithBloom { - receipt: Receipt { + OpReceiptEnvelope::Legacy(OpReceiptWithBloom { + receipt: OpReceipt { cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), @@ -59,6 +62,8 @@ mod tests { ), }], status: false, + deposit_nonce: None, + deposit_receipt_version: None, }, logs_bloom: [0; 256].into(), }); @@ -77,8 +82,8 @@ mod tests { // EIP658Receipt let expected = - ReceiptWithBloom { - receipt: Receipt { + OpReceiptWithBloom { + receipt: OpReceipt { cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), @@ -91,17 +96,19 @@ mod tests { ), }], status: false, + deposit_nonce: None, + deposit_receipt_version: None, }, logs_bloom: [0; 256].into(), }; - let receipt = ReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); assert_eq!(receipt, expected); } #[test] fn gigantic_receipt() { - let receipt = Receipt { + let receipt = OpReceipt { cumulative_gas_used: 16747627, status: true, logs: vec![ @@ -124,13 +131,15 @@ mod tests { ), }, ], + deposit_nonce: None, + deposit_receipt_version: None, } .with_bloom(); let mut data = vec![]; receipt.encode(&mut data); - let decoded = ReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let decoded = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); // receipt.clone().to_compact(&mut data); // let (decoded, _) = Receipt::from_compact(&data[..], data.len()); diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index e6ec7a36..319ff70a 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -1,4 +1,4 @@ -use super::TxReceipt; +use super::OpTxReceipt; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -8,9 +8,8 @@ use alloc::vec::Vec; /// Receipt containing result of transaction execution. #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct Receipt { +pub struct OpReceipt { /// If transaction is executed successfully. /// /// This is the `statusCode` @@ -21,9 +20,20 @@ pub struct Receipt { pub cumulative_gas_used: u128, /// Log send from contracts. pub logs: Vec, + /// Deposit nonce for Optimism deposit transactions + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] + pub deposit_nonce: Option, + /// Deposit receipt version for Optimism deposit transactions + /// + /// + /// The deposit receipt version was introduced in Canyon to indicate an update to how + /// receipt hashes should be computed when set. The state transition process + /// ensures this is only set for post-Canyon deposit transactions. + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] + pub deposit_receipt_version: Option, } -impl Receipt { +impl OpReceipt { /// Calculates [`Log`]'s bloom filter. this is slow operation and [ReceiptWithBloom] can /// be used to cache this value. pub fn bloom_slow(&self) -> Bloom { @@ -32,12 +42,12 @@ impl Receipt { /// Calculates the bloom filter for the receipt and returns the [ReceiptWithBloom] container /// type. - pub fn with_bloom(self) -> ReceiptWithBloom { + pub fn with_bloom(self) -> OpReceiptWithBloom { self.into() } } -impl TxReceipt for Receipt { +impl OpTxReceipt for OpReceipt { fn success(&self) -> bool { self.status } @@ -53,9 +63,17 @@ impl TxReceipt for Receipt { fn logs(&self) -> &[Log] { &self.logs } + + fn deposit_nonce(&self) -> Option { + self.deposit_nonce + } + + fn deposit_receipt_version(&self) -> Option { + self.deposit_receipt_version + } } -/// [`Receipt`] with calculated bloom filter. +/// [`Receipt`] with calculated bloom filter, modified for the OP Stack. /// /// This convenience type allows us to lazily calculate the bloom filter for a /// receipt, similar to [`Sealed`]. @@ -64,15 +82,15 @@ impl TxReceipt for Receipt { #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct ReceiptWithBloom { +pub struct OpReceiptWithBloom { #[cfg_attr(feature = "serde", serde(flatten))] /// The receipt. - pub receipt: Receipt, + pub receipt: OpReceipt, /// The bloom filter. pub logs_bloom: Bloom, } -impl TxReceipt for ReceiptWithBloom { +impl OpTxReceipt for OpReceiptWithBloom { fn success(&self) -> bool { self.receipt.status } @@ -92,30 +110,38 @@ impl TxReceipt for ReceiptWithBloom { fn logs(&self) -> &[Log] { &self.receipt.logs } + + fn deposit_nonce(&self) -> Option { + self.receipt.deposit_nonce + } + + fn deposit_receipt_version(&self) -> Option { + self.receipt.deposit_receipt_version + } } -impl From for ReceiptWithBloom { - fn from(receipt: Receipt) -> Self { +impl From for OpReceiptWithBloom { + fn from(receipt: OpReceipt) -> Self { let bloom = receipt.bloom_slow(); - ReceiptWithBloom { receipt, logs_bloom: bloom } + OpReceiptWithBloom { receipt, logs_bloom: bloom } } } -impl ReceiptWithBloom { +impl OpReceiptWithBloom { /// Create new [ReceiptWithBloom] - pub const fn new(receipt: Receipt, bloom: Bloom) -> Self { + pub const fn new(receipt: OpReceipt, bloom: Bloom) -> Self { Self { receipt, logs_bloom: bloom } } /// Consume the structure, returning only the receipt #[allow(clippy::missing_const_for_fn)] // false positive - pub fn into_receipt(self) -> Receipt { + pub fn into_receipt(self) -> OpReceipt { self.receipt } /// Consume the structure, returning the receipt and the bloom filter #[allow(clippy::missing_const_for_fn)] // false positive - pub fn into_components(self) -> (Receipt, Bloom) { + pub fn into_components(self) -> (OpReceipt, Bloom) { (self.receipt, self.logs_bloom) } @@ -124,6 +150,8 @@ impl ReceiptWithBloom { + self.receipt.cumulative_gas_used.length() + self.logs_bloom.length() + self.receipt.logs.length() + + self.receipt.deposit_nonce.map_or(0, |nonce| nonce.length()) + + self.receipt.deposit_receipt_version.map_or(0, |version| version.length()) } /// Returns the rlp header for the receipt payload. @@ -138,6 +166,12 @@ impl ReceiptWithBloom { self.receipt.cumulative_gas_used.encode(out); self.logs_bloom.encode(out); self.receipt.logs.encode(out); + if let Some(nonce) = self.receipt.deposit_nonce { + nonce.encode(out); + } + if let Some(version) = self.receipt.deposit_receipt_version { + version.encode(out); + } } /// Decodes the receipt payload @@ -154,7 +188,18 @@ impl ReceiptWithBloom { let bloom = Decodable::decode(b)?; let logs = Decodable::decode(b)?; - let receipt = Receipt { status: success, cumulative_gas_used, logs }; + let remaining = |b: &[u8]| rlp_head.payload_length - (started_len - b.len()) > 0; + let deposit_nonce = remaining(b).then(|| alloy_rlp::Decodable::decode(b)).transpose()?; + let deposit_receipt_version = + remaining(b).then(|| alloy_rlp::Decodable::decode(b)).transpose()?; + + let receipt = OpReceipt { + status: success, + cumulative_gas_used, + logs, + deposit_nonce, + deposit_receipt_version, + }; let this = Self { receipt, logs_bloom: bloom }; let consumed = started_len - b.len(); @@ -169,7 +214,7 @@ impl ReceiptWithBloom { } } -impl alloy_rlp::Encodable for ReceiptWithBloom { +impl alloy_rlp::Encodable for OpReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_fields(out); } @@ -178,23 +223,44 @@ impl alloy_rlp::Encodable for ReceiptWithBloom { let payload_length = self.receipt.status.length() + self.receipt.cumulative_gas_used.length() + self.logs_bloom.length() - + self.receipt.logs.length(); + + self.receipt.logs.length() + + self.receipt.deposit_nonce.map_or(0, |nonce| nonce.length()) + + self.receipt.deposit_receipt_version.map_or(0, |version| version.length()); payload_length + length_of_length(payload_length) } } -impl alloy_rlp::Decodable for ReceiptWithBloom { +impl alloy_rlp::Decodable for OpReceiptWithBloom { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { Self::decode_receipt(buf) } } #[cfg(all(test, feature = "arbitrary"))] -impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptWithBloom +impl<'a, T> arbitrary::Arbitrary<'a> for OpReceipt +where + T: arbitrary::Arbitrary<'a>, +{ + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + let deposit_nonce = Option::::arbitrary(u)?; + let deposit_receipt_version = + deposit_nonce.is_some().then(|| u64::arbitrary(u)).transpose()?; + Ok(Self { + status: bool::arbitrary(u)?, + cumulative_gas_used: u128::arbitrary(u)?, + logs: Vec::::arbitrary(u)?, + deposit_nonce, + deposit_receipt_version, + }) + } +} + +#[cfg(all(test, feature = "arbitrary"))] +impl<'a, T> arbitrary::Arbitrary<'a> for OpReceiptWithBloom where T: arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Ok(Self { receipt: Receipt::::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? }) + Ok(Self { receipt: OpReceipt::::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? }) } } diff --git a/crates/op-consensus/src/sealed.rs b/crates/op-consensus/src/sealed.rs deleted file mode 100644 index 86d5c85a..00000000 --- a/crates/op-consensus/src/sealed.rs +++ /dev/null @@ -1,68 +0,0 @@ -use alloy_primitives::B256; - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -/// A consensus hashable item, with its memoized hash. -/// -/// We do not implement -pub struct Sealed { - /// The inner item - inner: T, - /// Its hash. - seal: B256, -} - -impl core::ops::Deref for Sealed { - type Target = T; - - fn deref(&self) -> &Self::Target { - self.inner() - } -} - -impl Sealed { - /// Instantiate without performing the hash. This should be used carefully. - pub const fn new_unchecked(inner: T, seal: B256) -> Self { - Self { inner, seal } - } - - /// Decompose into parts. - #[allow(clippy::missing_const_for_fn)] // false positive - pub fn into_parts(self) -> (T, B256) { - (self.inner, self.seal) - } - - /// Get the inner item. - #[inline(always)] - pub const fn inner(&self) -> &T { - &self.inner - } - - /// Get the hash. - #[inline(always)] - pub const fn seal(&self) -> B256 { - self.seal - } - - /// Geth the hash (alias for [`Self::seal`]). - #[inline(always)] - pub const fn hash(&self) -> B256 { - self.seal() - } -} - -/// Sealeable objects. -pub trait Sealable: Sized { - /// Calculate the seal hash, this may be slow. - fn hash(&self) -> B256; - - /// Seal the object by calculating the hash. This may be slow. - fn seal_slow(self) -> Sealed { - let seal = self.hash(); - Sealed::new_unchecked(self, seal) - } - - /// Instantiate an unchecked seal. This should be used with caution. - fn seal_unchecked(self, seal: B256) -> Sealed { - Sealed::new_unchecked(self, seal) - } -} diff --git a/crates/op-consensus/src/transaction/eip1559.rs b/crates/op-consensus/src/transaction/eip1559.rs index e96be9d1..9f4e5bc2 100644 --- a/crates/op-consensus/src/transaction/eip1559.rs +++ b/crates/op-consensus/src/transaction/eip1559.rs @@ -1,4 +1,4 @@ -use crate::{SignableTransaction, Signed, Transaction, TxType}; +use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; use alloy_eips::eip2930::AccessList; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{BufMut, Decodable, Encodable, Header}; @@ -236,8 +236,8 @@ impl TxEip1559 { } /// Get transaction type - pub(crate) const fn tx_type(&self) -> TxType { - TxType::Eip1559 + pub(crate) const fn tx_type(&self) -> OpTxType { + OpTxType::Eip1559 } /// Calculates a heuristic for the in-memory size of the [TxEip1559] transaction. @@ -255,7 +255,7 @@ impl TxEip1559 { } } -impl Transaction for TxEip1559 { +impl OpTransaction for TxEip1559 { fn input(&self) -> &[u8] { &self.input } diff --git a/crates/op-consensus/src/transaction/eip2930.rs b/crates/op-consensus/src/transaction/eip2930.rs index 60303662..3337c93d 100644 --- a/crates/op-consensus/src/transaction/eip2930.rs +++ b/crates/op-consensus/src/transaction/eip2930.rs @@ -1,4 +1,4 @@ -use crate::{SignableTransaction, Signed, Transaction, TxType}; +use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; use alloy_eips::eip2930::AccessList; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; @@ -214,12 +214,12 @@ impl TxEip2930 { } /// Get transaction type. - pub const fn tx_type(&self) -> TxType { - TxType::Eip2930 + pub const fn tx_type(&self) -> OpTxType { + OpTxType::Eip2930 } } -impl Transaction for TxEip2930 { +impl OpTransaction for TxEip2930 { fn input(&self) -> &[u8] { &self.input } @@ -306,7 +306,7 @@ impl Decodable for TxEip2930 { #[cfg(test)] mod tests { use super::TxEip2930; - use crate::{SignableTransaction, TxEnvelope}; + use crate::{OpTxEnvelope, SignableTransaction}; use alloy_primitives::{Address, Bytes, Signature, TxKind, U256}; use alloy_rlp::{Decodable, Encodable}; @@ -349,7 +349,7 @@ mod tests { let tx = request.into_signed(signature); - let envelope = TxEnvelope::Eip2930(tx); + let envelope = OpTxEnvelope::Eip2930(tx); let mut encoded = Vec::new(); envelope.encode(&mut encoded); @@ -360,7 +360,7 @@ mod tests { "b86401f8610180010294000000000000000000000000000000000000000003820102c080a0840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565a025e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1" ); - let decoded = TxEnvelope::decode(&mut encoded.as_ref()).unwrap(); + let decoded = OpTxEnvelope::decode(&mut encoded.as_ref()).unwrap(); assert_eq!(decoded, envelope); } } diff --git a/crates/op-consensus/src/transaction/eip4844.rs b/crates/op-consensus/src/transaction/eip4844.rs index 4f259e6c..c3aedfae 100644 --- a/crates/op-consensus/src/transaction/eip4844.rs +++ b/crates/op-consensus/src/transaction/eip4844.rs @@ -3,7 +3,7 @@ pub use builder::{SidecarBuilder, SidecarCoder, SimpleCoder}; pub mod utils; -use crate::{SignableTransaction, Signed, Transaction, TxType}; +use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; use alloy_eips::{ eip2930::AccessList, @@ -135,8 +135,8 @@ impl TxEip4844Variant { } /// Get the transaction type. - pub const fn tx_type(&self) -> TxType { - TxType::Eip4844 + pub const fn tx_type(&self) -> OpTxType { + OpTxType::Eip4844 } /// Get access to the inner tx [TxEip4844]. @@ -227,7 +227,7 @@ impl TxEip4844Variant { } } -impl Transaction for TxEip4844Variant { +impl OpTransaction for TxEip4844Variant { fn chain_id(&self) -> Option { match self { TxEip4844Variant::TxEip4844(tx) => Some(tx.chain_id), @@ -297,7 +297,7 @@ impl SignableTransaction for TxEip4844Variant { fn into_signed(self, signature: Signature) -> Signed { let payload_length = 1 + self.fields_len() + signature.rlp_vrs_len(); let mut buf = Vec::with_capacity(payload_length); - buf.put_u8(TxType::Eip4844 as u8); + buf.put_u8(OpTxType::Eip4844 as u8); // we use the inner tx to encode the fields self.tx().encode_with_signature(&signature, &mut buf, false); let hash = keccak256(&buf); @@ -648,8 +648,8 @@ impl TxEip4844 { } /// Get transaction type - pub const fn tx_type(&self) -> TxType { - TxType::Eip4844 + pub const fn tx_type(&self) -> OpTxType { + OpTxType::Eip4844 } /// Encodes the EIP-4844 transaction in RLP for signing. @@ -698,7 +698,7 @@ impl SignableTransaction for TxEip4844 { } } -impl Transaction for TxEip4844 { +impl OpTransaction for TxEip4844 { fn input(&self) -> &[u8] { &self.input } @@ -792,7 +792,7 @@ impl TxEip4844WithSidecar { } /// Get the transaction type. - pub const fn tx_type(&self) -> TxType { + pub const fn tx_type(&self) -> OpTxType { self.tx.tx_type() } @@ -929,7 +929,7 @@ impl SignableTransaction for TxEip4844WithSidecar { } } -impl Transaction for TxEip4844WithSidecar { +impl OpTransaction for TxEip4844WithSidecar { fn chain_id(&self) -> Option { self.tx.chain_id() } @@ -1097,7 +1097,7 @@ pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 { #[cfg(test)] mod tests { use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar}; - use crate::{SignableTransaction, TxEnvelope}; + use crate::{OpTxEnvelope, SignableTransaction}; use alloy_primitives::{Signature, U256}; use alloy_rlp::{Decodable, Encodable}; @@ -1149,8 +1149,8 @@ mod tests { assert_eq!(expected_signed.hash(), actual_signed.hash()); // convert to envelopes - let expected_envelope: TxEnvelope = expected_signed.into(); - let actual_envelope: TxEnvelope = actual_signed.into(); + let expected_envelope: OpTxEnvelope = expected_signed.into(); + let actual_envelope: OpTxEnvelope = actual_signed.into(); // now encode the transaction and check the length let mut buf = Vec::new(); @@ -1162,7 +1162,7 @@ mod tests { assert_eq!(buf.len(), actual_envelope.length()); // now decode the transaction and check the values - let decoded = TxEnvelope::decode(&mut &buf[..]).unwrap(); + let decoded = OpTxEnvelope::decode(&mut &buf[..]).unwrap(); assert_eq!(decoded, expected_envelope); } } diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index b090dcc1..f926e5b3 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -6,16 +6,17 @@ use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; use core::mem; -/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and -/// [2930]. +/// Optimism `TransactionType` flags as specified in EIPs [2718], [1559], and +/// [2930], as well as the [deposit transaction spec][deposit-spec] /// /// [2718]: https://eips.ethereum.org/EIPS/eip-2718 /// [1559]: https://eips.ethereum.org/EIPS/eip-1559 /// [2930]: https://eips.ethereum.org/EIPS/eip-2930 /// [4844]: https://eips.ethereum.org/EIPS/eip-4844 +/// [deposit-spec]: https://specs.optimism.io/protocol/deposits.html #[repr(u8)] #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] -pub enum TxType { +pub enum OpTxType { /// Legacy transaction type. Legacy = 0, /// EIP-2930 transaction type. @@ -29,32 +30,32 @@ pub enum TxType { } #[cfg(any(test, feature = "arbitrary"))] -impl<'a> arbitrary::Arbitrary<'a> for TxType { +impl<'a> arbitrary::Arbitrary<'a> for OpTxType { fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { Ok(match u.int_in_range(0..=3)? { - 0 => TxType::Legacy, - 1 => TxType::Eip2930, - 2 => TxType::Eip1559, - 3 => TxType::Eip4844, - 0x7E => TxType::Deposit, + 0 => OpTxType::Legacy, + 1 => OpTxType::Eip2930, + 2 => OpTxType::Eip1559, + 3 => OpTxType::Eip4844, + 0x7E => OpTxType::Deposit, _ => unreachable!(), }) } } -impl TryFrom for TxType { +impl TryFrom for OpTxType { type Error = Eip2718Error; fn try_from(value: u8) -> Result { match value { // SAFETY: repr(u8) with explicit discriminant - 0..=3 => Ok(unsafe { mem::transmute::(value) }), + 0..=3 | 0x7E => Ok(unsafe { mem::transmute::(value) }), _ => Err(Eip2718Error::UnexpectedType(value)), } } } -/// The Ethereum [EIP-2718] Transaction Envelope. +/// The Ethereum [EIP-2718] Transaction Envelope, modified for OP Stack chains. /// /// # Note: /// @@ -69,7 +70,7 @@ impl TryFrom for TxType { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(tag = "type"))] #[non_exhaustive] -pub enum TxEnvelope { +pub enum OpTxEnvelope { /// An untagged [`TxLegacy`]. #[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))] Legacy(Signed), @@ -93,38 +94,38 @@ pub enum TxEnvelope { Deposit(TxDeposit), } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { Self::Legacy(v) } } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { Self::Eip2930(v) } } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { Self::Eip1559(v) } } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { Self::Eip4844(v) } } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { let (tx, signature, hash) = v.into_parts(); Self::Eip4844(Signed::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)) } } -impl From> for TxEnvelope { +impl From> for OpTxEnvelope { fn from(v: Signed) -> Self { let (tx, signature, hash) = v.into_parts(); Self::Eip4844(Signed::new_unchecked( @@ -135,21 +136,21 @@ impl From> for TxEnvelope { } } -impl From for TxEnvelope { +impl From for OpTxEnvelope { fn from(v: TxDeposit) -> Self { Self::Deposit(v) } } -impl TxEnvelope { +impl OpTxEnvelope { /// Return the [`TxType`] of the inner txn. - pub const fn tx_type(&self) -> TxType { + pub const fn tx_type(&self) -> OpTxType { match self { - Self::Legacy(_) => TxType::Legacy, - Self::Eip2930(_) => TxType::Eip2930, - Self::Eip1559(_) => TxType::Eip1559, - Self::Eip4844(_) => TxType::Eip4844, - Self::Deposit(_) => TxType::Deposit, + Self::Legacy(_) => OpTxType::Legacy, + Self::Eip2930(_) => OpTxType::Eip2930, + Self::Eip1559(_) => OpTxType::Eip1559, + Self::Eip4844(_) => OpTxType::Eip4844, + Self::Deposit(_) => OpTxType::Deposit, } } @@ -181,7 +182,10 @@ impl TxEnvelope { outer_header.length() + outer_payload_length } }, - Self::Deposit(t) => t.fields_len(), + Self::Deposit(t) => { + let payload_length = t.fields_len(); + Header { list: true, payload_length }.length() + payload_length + } } } @@ -198,7 +202,7 @@ impl TxEnvelope { } } -impl Encodable for TxEnvelope { +impl Encodable for OpTxEnvelope { fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { self.network_encode(out) } @@ -213,38 +217,38 @@ impl Encodable for TxEnvelope { } } -impl Decodable for TxEnvelope { +impl Decodable for OpTxEnvelope { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { Self::network_decode(buf) } } -impl Decodable2718 for TxEnvelope { +impl Decodable2718 for OpTxEnvelope { fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("unexpected tx type"))? { - TxType::Eip2930 => Ok(Self::Eip2930(TxEip2930::decode_signed_fields(buf)?)), - TxType::Eip1559 => Ok(Self::Eip1559(TxEip1559::decode_signed_fields(buf)?)), - TxType::Eip4844 => Ok(Self::Eip4844(TxEip4844Variant::decode_signed_fields(buf)?)), - TxType::Deposit => Ok(Self::Deposit(TxDeposit::decode(buf)?)), - TxType::Legacy => { + OpTxType::Eip2930 => Ok(Self::Eip2930(TxEip2930::decode_signed_fields(buf)?)), + OpTxType::Eip1559 => Ok(Self::Eip1559(TxEip1559::decode_signed_fields(buf)?)), + OpTxType::Eip4844 => Ok(Self::Eip4844(TxEip4844Variant::decode_signed_fields(buf)?)), + OpTxType::Deposit => Ok(Self::Deposit(TxDeposit::decode(buf)?)), + OpTxType::Legacy => { Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) } } } fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(TxEnvelope::Legacy(TxLegacy::decode_signed_fields(buf)?)) + Ok(OpTxEnvelope::Legacy(TxLegacy::decode_signed_fields(buf)?)) } } -impl Encodable2718 for TxEnvelope { +impl Encodable2718 for OpTxEnvelope { fn type_flag(&self) -> Option { match self { Self::Legacy(_) => None, - Self::Eip2930(_) => Some(TxType::Eip2930 as u8), - Self::Eip1559(_) => Some(TxType::Eip1559 as u8), - Self::Eip4844(_) => Some(TxType::Eip4844 as u8), - Self::Deposit(_) => Some(TxType::Deposit as u8), + Self::Eip2930(_) => Some(OpTxType::Eip2930 as u8), + Self::Eip1559(_) => Some(OpTxType::Eip1559 as u8), + Self::Eip4844(_) => Some(OpTxType::Eip4844 as u8), + Self::Deposit(_) => Some(OpTxType::Deposit as u8), } } @@ -255,17 +259,20 @@ impl Encodable2718 for TxEnvelope { fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) { match self { // Legacy transactions have no difference between network and 2718 - TxEnvelope::Legacy(tx) => tx.tx().encode_with_signature_fields(tx.signature(), out), - TxEnvelope::Eip2930(tx) => { + OpTxEnvelope::Legacy(tx) => tx.tx().encode_with_signature_fields(tx.signature(), out), + OpTxEnvelope::Eip2930(tx) => { tx.tx().encode_with_signature(tx.signature(), out, false); } - TxEnvelope::Eip1559(tx) => { + OpTxEnvelope::Eip1559(tx) => { tx.tx().encode_with_signature(tx.signature(), out, false); } - TxEnvelope::Eip4844(tx) => { + OpTxEnvelope::Eip4844(tx) => { tx.tx().encode_with_signature(tx.signature(), out, false); } - TxEnvelope::Deposit(tx) => tx.encode(out), + OpTxEnvelope::Deposit(tx) => { + out.put_u8(OpTxType::Deposit as u8); + tx.encode(out); + } } } } @@ -288,12 +295,12 @@ mod tests { use alloy_primitives::address; let raw_tx = alloy_primitives::hex::decode("02f86f0102843b9aca0085029e7822d68298f094d9e1459a7a482635700cbc20bbaf52d495ab9c9680841b55ba3ac080a0c199674fcb29f353693dd779c017823b954b3c69dffa3cd6b2a6ff7888798039a028ca912de909e7e6cdef9cdcaf24c54dd8c1032946dfa1d85c206b32a9064fe8").unwrap(); - let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - assert_eq!(res.tx_type(), TxType::Eip1559); + assert_eq!(res.tx_type(), OpTxType::Eip1559); let tx = match res { - TxEnvelope::Eip1559(tx) => tx, + OpTxEnvelope::Eip1559(tx) => tx, _ => unreachable!(), }; @@ -309,11 +316,11 @@ mod tests { use alloy_primitives::address; let raw_tx = alloy_primitives::hex::decode("f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8").unwrap(); - let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - assert_eq!(res.tx_type(), TxType::Legacy); + let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + assert_eq!(res.tx_type(), OpTxType::Legacy); let tx = match res { - TxEnvelope::Legacy(tx) => tx, + OpTxEnvelope::Legacy(tx) => tx, _ => unreachable!(), }; @@ -331,16 +338,16 @@ mod tests { // Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 // Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 fn test_decode_live_4844_tx() { - use crate::Transaction; + use crate::OpTransaction; use alloy_primitives::{address, b256}; // https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 let raw_tx = alloy_primitives::hex::decode("0x03f9011d83aa36a7820fa28477359400852e90edd0008252089411e9ca82a3a762b4b5bd264d4173a242e7a770648080c08504a817c800f8a5a0012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921aa00152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4a0013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7a001148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1a0011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e654901a0c8de4cced43169f9aa3d36506363b2d2c44f6c49fc1fd91ea114c86f3757077ea01e11fdd0d1934eda0492606ee0bb80a7bf8f35cc5f86ec60fe5031ba48bfd544").unwrap(); - let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - assert_eq!(res.tx_type(), TxType::Eip4844); + let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); + assert_eq!(res.tx_type(), OpTxType::Eip4844); let tx = match res { - TxEnvelope::Eip4844(tx) => tx, + OpTxEnvelope::Eip4844(tx) => tx, _ => unreachable!(), }; @@ -369,13 +376,13 @@ mod tests { fn test_encode_decode_roundtrip>(tx: T) where - Signed: Into, + Signed: Into, { let signature = Signature::test_signature(); let tx_signed = tx.into_signed(signature); - let tx_envelope: TxEnvelope = tx_signed.into(); + let tx_envelope: OpTxEnvelope = tx_signed.into(); let encoded = tx_envelope.encoded_2718(); - let decoded = TxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap(); + let decoded = OpTxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap(); assert_eq!(encoded.len(), tx_envelope.encode_2718_len()); assert_eq!(decoded, tx_envelope); } @@ -414,10 +421,29 @@ mod tests { test_encode_decode_roundtrip(tx); } + #[test] + fn test_encode_decode_deposit() { + let tx = TxDeposit { + source_hash: B256::left_padding_from(&[0xde, 0xad]), + from: Address::left_padding_from(&[0xbe, 0xef]), + mint: Some(1), + gas_limit: 2, + to: TxKind::Call(Address::left_padding_from(&[3])), + value: U256::from(4_u64), + input: Bytes::from(vec![5]), + is_system_transaction: false, + }; + let tx_envelope = OpTxEnvelope::Deposit(tx); + let encoded = tx_envelope.encoded_2718(); + let decoded = OpTxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap(); + assert_eq!(encoded.len(), tx_envelope.encode_2718_len()); + assert_eq!(decoded, tx_envelope); + } + #[test] fn test_encode_decode_transaction_list() { let signature = Signature::test_signature(); - let tx = TxEnvelope::Eip1559( + let tx = OpTxEnvelope::Eip1559( TxEip1559 { chain_id: 1u64, nonce: 2, @@ -433,7 +459,7 @@ mod tests { ); let transactions = vec![tx.clone(), tx]; let encoded = alloy_rlp::encode(&transactions); - let decoded = Vec::::decode(&mut &encoded[..]).unwrap(); + let decoded = Vec::::decode(&mut &encoded[..]).unwrap(); assert_eq!(transactions, decoded); } @@ -445,7 +471,7 @@ mod tests { let data = fs::read_to_string(network_data_path).expect("Unable to read file"); let hex_data = hex::decode(data.trim()).unwrap(); - let tx: TxEnvelope = TxEnvelope::decode_2718(&mut hex_data.as_slice()).unwrap(); + let tx: OpTxEnvelope = OpTxEnvelope::decode_2718(&mut hex_data.as_slice()).unwrap(); let encoded = tx.encoded_2718(); assert_eq!(encoded, hex_data); assert_eq!(tx.encode_2718_len(), hex_data.len()); @@ -454,13 +480,13 @@ mod tests { #[cfg(feature = "serde")] fn test_serde_roundtrip>(tx: T) where - Signed: Into, + Signed: Into, { let signature = Signature::test_signature(); - let tx_envelope: TxEnvelope = tx.into_signed(signature).into(); + let tx_envelope: OpTxEnvelope = tx.into_signed(signature).into(); let serialized = serde_json::to_string(&tx_envelope).unwrap(); - let deserialized: TxEnvelope = serde_json::from_str(&serialized).unwrap(); + let deserialized: OpTxEnvelope = serde_json::from_str(&serialized).unwrap(); assert_eq!(tx_envelope, deserialized); } @@ -560,4 +586,25 @@ mod tests { }); test_serde_roundtrip(tx); } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_roundtrip_deposit() { + let tx = TxDeposit { + gas_limit: u128::MAX, + to: TxKind::Call(Address::random()), + value: U256::MAX, + input: Bytes::new(), + source_hash: U256::MAX.into(), + from: Address::random(), + mint: Some(u128::MAX), + is_system_transaction: false, + }; + let tx_envelope = OpTxEnvelope::Deposit(tx); + + let serialized = serde_json::to_string(&tx_envelope).unwrap(); + let deserialized: OpTxEnvelope = serde_json::from_str(&serialized).unwrap(); + + assert_eq!(tx_envelope, deserialized); + } } diff --git a/crates/op-consensus/src/transaction/legacy.rs b/crates/op-consensus/src/transaction/legacy.rs index 52eb666c..bf44c8d1 100644 --- a/crates/op-consensus/src/transaction/legacy.rs +++ b/crates/op-consensus/src/transaction/legacy.rs @@ -1,4 +1,4 @@ -use crate::{SignableTransaction, Signed, Transaction}; +use crate::{OpTransaction, SignableTransaction, Signed}; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result}; use core::mem; @@ -195,7 +195,7 @@ impl TxLegacy { } } -impl Transaction for TxLegacy { +impl OpTransaction for TxLegacy { fn input(&self) -> &[u8] { &self.input } diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index 84dece90..7369447a 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -23,16 +23,16 @@ mod optimism; pub use optimism::TxDeposit; mod envelope; -pub use envelope::{TxEnvelope, TxType}; +pub use envelope::{OpTxEnvelope, OpTxType}; mod legacy; pub use legacy::TxLegacy; mod typed; -pub use typed::TypedTransaction; +pub use typed::OpTypedTransaction; /// Represents a minimal EVM transaction. -pub trait Transaction: any::Any + Send + Sync + 'static { +pub trait OpTransaction: any::Any + Send + Sync + 'static { /// Get `data`. fn input(&self) -> &[u8]; @@ -61,7 +61,7 @@ pub trait Transaction: any::Any + Send + Sync + 'static { /// [`alloy_primitives::Signature`], however, it may be different for future EIP-2718 transaction /// types, or in other networks. For example, in Optimism, the deposit transaction signature is the /// unit type `()`. -pub trait SignableTransaction: Transaction { +pub trait SignableTransaction: OpTransaction { /// Sets `chain_id`. /// /// Prefer [`set_chain_id_checked`](Self::set_chain_id_checked). diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs index 18f6093a..ea09e367 100644 --- a/crates/op-consensus/src/transaction/optimism.rs +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -1,4 +1,4 @@ -use crate::Transaction; +use crate::OpTransaction; use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256}; use alloy_rlp::{ Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE, @@ -19,13 +19,23 @@ pub struct TxDeposit { pub from: Address, /// The address of the recipient account, or the null (zero-length) address if the deposited /// transaction is a contract creation. + #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] pub to: TxKind, /// The ETH value to mint on L2. + #[cfg_attr( + feature = "serde", + serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::u128_hex_or_decimal_opt" + ) + )] pub mint: Option, /// The ETH value to send to the recipient account. pub value: U256, /// The gas limit for the L2 transaction. - pub gas_limit: u64, + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub gas_limit: u128, /// Field indicating if this transaction is exempt from the L2 gas limit. pub is_system_transaction: bool, /// Input has two uses depending if transaction is Create or Call (if `to` field is None or @@ -103,13 +113,13 @@ impl TxDeposit { self.to.size() + // to mem::size_of::>() + // mint mem::size_of::() + // value - mem::size_of::() + // gas_limit + mem::size_of::() + // gas_limit mem::size_of::() + // is_system_transaction self.input.len() // input } } -impl Transaction for TxDeposit { +impl OpTransaction for TxDeposit { fn input(&self) -> &[u8] { &self.input } @@ -131,7 +141,7 @@ impl Transaction for TxDeposit { } fn gas_limit(&self) -> u128 { - self.gas_limit.into() + self.gas_limit } fn gas_price(&self) -> Option { @@ -174,10 +184,10 @@ mod tests { #[test] fn test_rlp_roundtrip() { let bytes = Bytes::from_static(&hex!("7ef9015aa044bae9d41b8380d781187b426c6fe43df5fb2fb57bd4466ef6a701e1f01e015694deaddeaddeaddeaddeaddeaddeaddeaddead000194420000000000000000000000000000000000001580808408f0d18001b90104015d8eb900000000000000000000000000000000000000000000000000000000008057650000000000000000000000000000000000000000000000000000000063d96d10000000000000000000000000000000000000000000000000000000000009f35273d89754a1e0387b89520d989d3be9c37c1f32495a88faf1ea05c61121ab0d1900000000000000000000000000000000000000000000000000000000000000010000000000000000000000002d679b567db6187c0c8323fa982cfb88b74dbcc7000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240")); - let tx_a = TxDeposit::decode(&mut bytes.as_ref()).unwrap(); + let tx_a = TxDeposit::decode(&mut bytes[1..].as_ref()).unwrap(); let mut buf_a = BytesMut::default(); tx_a.encode(&mut buf_a); - assert_eq!(&buf_a[..], &bytes[..]); + assert_eq!(&buf_a[..], &bytes[1..]); } #[test] @@ -195,7 +205,7 @@ mod tests { let mut buffer = BytesMut::new(); original.encode_fields(&mut buffer); - let decoded = TxDeposit::decode(&mut &buffer[..]).expect("Failed to decode"); + let decoded = TxDeposit::decode_fields(&mut &buffer[..]).expect("Failed to decode"); assert_eq!(original, decoded); } diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index 1ef0c294..df00161f 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -1,19 +1,22 @@ use crate::{ - Transaction, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, TxEnvelope, TxLegacy, TxType, + OpTransaction, OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, + TxLegacy, }; use alloy_primitives::TxKind; -/// The TypedTransaction enum represents all Ethereum transaction request types. +/// The TypedTransaction enum represents all Ethereum transaction request types, modified for the OP +/// Stack. /// /// Its variants correspond to specific allowed transactions: /// 1. Legacy (pre-EIP2718) [`TxLegacy`] /// 2. EIP2930 (state access lists) [`TxEip2930`] /// 3. EIP1559 [`TxEip1559`] /// 4. EIP4844 [`TxEip4844Variant`] +/// 4. Deposit [`TxDeposit`] #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(tag = "type"))] -pub enum TypedTransaction { +pub enum OpTypedTransaction { /// Legacy transaction #[cfg_attr(feature = "serde", serde(rename = "0x00", alias = "0x0"))] Legacy(TxLegacy), @@ -31,57 +34,57 @@ pub enum TypedTransaction { Deposit(TxDeposit), } -impl From for TypedTransaction { +impl From for OpTypedTransaction { fn from(tx: TxLegacy) -> Self { Self::Legacy(tx) } } -impl From for TypedTransaction { +impl From for OpTypedTransaction { fn from(tx: TxEip2930) -> Self { Self::Eip2930(tx) } } -impl From for TypedTransaction { +impl From for OpTypedTransaction { fn from(tx: TxEip1559) -> Self { Self::Eip1559(tx) } } -impl From for TypedTransaction { +impl From for OpTypedTransaction { fn from(tx: TxEip4844Variant) -> Self { Self::Eip4844(tx) } } -impl From for TypedTransaction { +impl From for OpTypedTransaction { fn from(tx: TxDeposit) -> Self { Self::Deposit(tx) } } -impl From for TypedTransaction { - fn from(envelope: TxEnvelope) -> Self { +impl From for OpTypedTransaction { + fn from(envelope: OpTxEnvelope) -> Self { match envelope { - TxEnvelope::Legacy(tx) => Self::Legacy(tx.strip_signature()), - TxEnvelope::Eip2930(tx) => Self::Eip2930(tx.strip_signature()), - TxEnvelope::Eip1559(tx) => Self::Eip1559(tx.strip_signature()), - TxEnvelope::Eip4844(tx) => Self::Eip4844(tx.strip_signature()), - TxEnvelope::Deposit(tx) => Self::Deposit(tx), + OpTxEnvelope::Legacy(tx) => Self::Legacy(tx.strip_signature()), + OpTxEnvelope::Eip2930(tx) => Self::Eip2930(tx.strip_signature()), + OpTxEnvelope::Eip1559(tx) => Self::Eip1559(tx.strip_signature()), + OpTxEnvelope::Eip4844(tx) => Self::Eip4844(tx.strip_signature()), + OpTxEnvelope::Deposit(tx) => Self::Deposit(tx), } } } -impl TypedTransaction { +impl OpTypedTransaction { /// Return the [`TxType`] of the inner txn. - pub const fn tx_type(&self) -> TxType { + pub const fn tx_type(&self) -> OpTxType { match self { - Self::Legacy(_) => TxType::Legacy, - Self::Eip2930(_) => TxType::Eip2930, - Self::Eip1559(_) => TxType::Eip1559, - Self::Eip4844(_) => TxType::Eip4844, - Self::Deposit(_) => TxType::Deposit, + Self::Legacy(_) => OpTxType::Legacy, + Self::Eip2930(_) => OpTxType::Eip2930, + Self::Eip1559(_) => OpTxType::Eip1559, + Self::Eip4844(_) => OpTxType::Eip4844, + Self::Deposit(_) => OpTxType::Deposit, } } @@ -110,7 +113,7 @@ impl TypedTransaction { } } -impl Transaction for TypedTransaction { +impl OpTransaction for OpTypedTransaction { fn chain_id(&self) -> Option { match self { Self::Legacy(tx) => tx.chain_id(), From 5b3bd67d05454ed5555383c86fbd1485eb4e50f7 Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 13 Apr 2024 12:21:48 -0400 Subject: [PATCH 05/20] Use upstreamed `Signed` + `SignableTransaction` --- Cargo.toml | 2 +- crates/op-consensus/Cargo.toml | 11 ++- crates/op-consensus/src/lib.rs | 9 +- crates/op-consensus/src/receipt/envelope.rs | 5 +- crates/op-consensus/src/signed.rs | 63 ------------ .../op-consensus/src/transaction/eip1559.rs | 5 +- .../op-consensus/src/transaction/eip2930.rs | 8 +- .../op-consensus/src/transaction/eip4844.rs | 13 +-- .../op-consensus/src/transaction/envelope.rs | 6 +- crates/op-consensus/src/transaction/legacy.rs | 4 +- crates/op-consensus/src/transaction/mod.rs | 96 ------------------- .../op-consensus/src/transaction/optimism.rs | 4 +- crates/op-consensus/src/transaction/typed.rs | 8 +- 13 files changed, 37 insertions(+), 197 deletions(-) delete mode 100644 crates/op-consensus/src/signed.rs diff --git a/Cargo.toml b/Cargo.toml index 1867ff69..0569b718 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ alloy = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } alloy-rlp = { version = "0.3", default-features = false } alloy-primitives = { version = "0.7.1", default-features = false } alloy-op-rpc-types = { version = "0.1.0", path = "crates/rpc-types" } -alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } +alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c", default-features = false} alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } alloy-eips = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } alloy-serde = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } diff --git a/crates/op-consensus/Cargo.toml b/crates/op-consensus/Cargo.toml index 225f4e22..6a21a124 100644 --- a/crates/op-consensus/Cargo.toml +++ b/crates/op-consensus/Cargo.toml @@ -13,6 +13,7 @@ exclude.workspace = true [dependencies] alloy-primitives = { workspace = true, features = ["rlp"] } +alloy-consensus.workspace = true alloy-rlp.workspace = true alloy-eips.workspace = true alloy-serde = { workspace = true, optional = true } @@ -38,8 +39,8 @@ serde_json.workspace = true [features] default = ["std"] -std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"] -k256 = ["alloy-primitives/k256"] -kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"] -arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"] -serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"] +std = ["alloy-eips/std", "alloy-consensus/std", "sha2/std", "c-kzg?/std"] +k256 = ["alloy-primitives/k256", "alloy-consensus/k256"] +kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "alloy-consensus/kzg", "std"] +arbitrary = ["std", "dep:arbitrary", "alloy-consensus/arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"] +serde = ["dep:serde", "alloy-primitives/serde", "alloy-consensus/serde", "dep:alloy-serde", "alloy-eips/serde"] diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 2015b570..3ade3a04 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -24,9 +24,9 @@ pub use receipt::{OpReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt} mod transaction; pub use transaction::{ - eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTransaction, OpTxEnvelope, OpTxType, - OpTypedTransaction, SidecarBuilder, SidecarCoder, SignableTransaction, SimpleCoder, TxDeposit, - TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, + eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTxEnvelope, OpTxType, + OpTypedTransaction, SidecarBuilder, SidecarCoder, SimpleCoder, TxDeposit, TxEip1559, TxEip2930, + TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, }; #[cfg(feature = "kzg")] @@ -34,6 +34,3 @@ pub use transaction::BlobTransactionValidationError; #[cfg(feature = "kzg")] pub use alloy_eips::eip4844::env_settings::EnvKzgSettings; - -mod signed; -pub use signed::Signed; diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index f6b24247..97dd3e37 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -205,13 +205,12 @@ where fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { let receipt = OpReceiptWithBloom::::arbitrary(u)?; - match u.int_in_range(0..=3)? { + match u.int_in_range(0..=4)? { 0 => Ok(Self::Legacy(receipt)), 1 => Ok(Self::Eip2930(receipt)), 2 => Ok(Self::Eip1559(receipt)), 3 => Ok(Self::Eip4844(receipt)), - 0x7E => Ok(Self::Deposit(receipt)), - _ => unreachable!(), + _ => Ok(Self::Deposit(receipt)), } } } diff --git a/crates/op-consensus/src/signed.rs b/crates/op-consensus/src/signed.rs deleted file mode 100644 index 4ef9a8db..00000000 --- a/crates/op-consensus/src/signed.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::transaction::SignableTransaction; -use alloy_primitives::{Signature, B256}; - -/// A transaction with a signature and hash seal. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct Signed { - #[cfg_attr(feature = "serde", serde(flatten))] - tx: T, - #[cfg_attr(feature = "serde", serde(flatten))] - signature: Sig, - hash: B256, -} - -impl Signed { - /// Returns a reference to the transaction. - pub const fn tx(&self) -> &T { - &self.tx - } - - /// Returns a reference to the signature. - pub const fn signature(&self) -> &Sig { - &self.signature - } - - /// Returns a reference to the transaction hash. - pub const fn hash(&self) -> &B256 { - &self.hash - } - - /// Splits the transaction into parts. - pub fn into_parts(self) -> (T, Sig, B256) { - (self.tx, self.signature, self.hash) - } - - /// Returns the transaction without signature. - pub fn strip_signature(self) -> T { - self.tx - } -} - -impl, Sig> Signed { - /// Instantiate from a transaction and signature. Does not verify the signature. - pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self { - Self { tx, signature, hash } - } - - /// Calculate the signing hash for the transaction. - pub fn signature_hash(&self) -> B256 { - self.tx.signature_hash() - } -} - -#[cfg(feature = "k256")] -impl> Signed { - /// Recover the signer of the transaction - pub fn recover_signer( - &self, - ) -> Result { - let sighash = self.tx.signature_hash(); - self.signature.recover_address_from_prehash(&sighash) - } -} diff --git a/crates/op-consensus/src/transaction/eip1559.rs b/crates/op-consensus/src/transaction/eip1559.rs index 9f4e5bc2..66e9008a 100644 --- a/crates/op-consensus/src/transaction/eip1559.rs +++ b/crates/op-consensus/src/transaction/eip1559.rs @@ -1,4 +1,5 @@ -use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; +use crate::OpTxType; +use alloy_consensus::{SignableTransaction, Signed, Transaction}; use alloy_eips::eip2930::AccessList; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{BufMut, Decodable, Encodable, Header}; @@ -255,7 +256,7 @@ impl TxEip1559 { } } -impl OpTransaction for TxEip1559 { +impl Transaction for TxEip1559 { fn input(&self) -> &[u8] { &self.input } diff --git a/crates/op-consensus/src/transaction/eip2930.rs b/crates/op-consensus/src/transaction/eip2930.rs index 3337c93d..d9cfd576 100644 --- a/crates/op-consensus/src/transaction/eip2930.rs +++ b/crates/op-consensus/src/transaction/eip2930.rs @@ -1,4 +1,5 @@ -use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; +use crate::OpTxType; +use alloy_consensus::{SignableTransaction, Signed, Transaction}; use alloy_eips::eip2930::AccessList; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; @@ -219,7 +220,7 @@ impl TxEip2930 { } } -impl OpTransaction for TxEip2930 { +impl Transaction for TxEip2930 { fn input(&self) -> &[u8] { &self.input } @@ -306,7 +307,8 @@ impl Decodable for TxEip2930 { #[cfg(test)] mod tests { use super::TxEip2930; - use crate::{OpTxEnvelope, SignableTransaction}; + use crate::OpTxEnvelope; + use alloy_consensus::SignableTransaction; use alloy_primitives::{Address, Bytes, Signature, TxKind, U256}; use alloy_rlp::{Decodable, Encodable}; diff --git a/crates/op-consensus/src/transaction/eip4844.rs b/crates/op-consensus/src/transaction/eip4844.rs index c3aedfae..7808ccdc 100644 --- a/crates/op-consensus/src/transaction/eip4844.rs +++ b/crates/op-consensus/src/transaction/eip4844.rs @@ -3,8 +3,8 @@ pub use builder::{SidecarBuilder, SidecarCoder, SimpleCoder}; pub mod utils; -use crate::{OpTransaction, OpTxType, SignableTransaction, Signed}; - +use crate::OpTxType; +use alloy_consensus::{SignableTransaction, Signed, Transaction}; use alloy_eips::{ eip2930::AccessList, eip4844::{BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, DATA_GAS_PER_BLOB}, @@ -227,7 +227,7 @@ impl TxEip4844Variant { } } -impl OpTransaction for TxEip4844Variant { +impl Transaction for TxEip4844Variant { fn chain_id(&self) -> Option { match self { TxEip4844Variant::TxEip4844(tx) => Some(tx.chain_id), @@ -698,7 +698,7 @@ impl SignableTransaction for TxEip4844 { } } -impl OpTransaction for TxEip4844 { +impl Transaction for TxEip4844 { fn input(&self) -> &[u8] { &self.input } @@ -929,7 +929,7 @@ impl SignableTransaction for TxEip4844WithSidecar { } } -impl OpTransaction for TxEip4844WithSidecar { +impl Transaction for TxEip4844WithSidecar { fn chain_id(&self) -> Option { self.tx.chain_id() } @@ -1097,7 +1097,8 @@ pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 { #[cfg(test)] mod tests { use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar}; - use crate::{OpTxEnvelope, SignableTransaction}; + use crate::OpTxEnvelope; + use alloy_consensus::SignableTransaction; use alloy_primitives::{Signature, U256}; use alloy_rlp::{Decodable, Encodable}; diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index f926e5b3..d9e71f5c 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -1,7 +1,7 @@ use crate::{ - Signed, TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, - TxLegacy, + TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, }; +use alloy_consensus::Signed; use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; use core::mem; @@ -280,7 +280,7 @@ impl Encodable2718 for OpTxEnvelope { #[cfg(test)] mod tests { use super::*; - use crate::transaction::SignableTransaction; + use alloy_consensus::SignableTransaction; use alloy_eips::eip2930::{AccessList, AccessListItem}; use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256}; use std::{fs, path::PathBuf, vec}; diff --git a/crates/op-consensus/src/transaction/legacy.rs b/crates/op-consensus/src/transaction/legacy.rs index bf44c8d1..9377a99e 100644 --- a/crates/op-consensus/src/transaction/legacy.rs +++ b/crates/op-consensus/src/transaction/legacy.rs @@ -1,4 +1,4 @@ -use crate::{OpTransaction, SignableTransaction, Signed}; +use alloy_consensus::{SignableTransaction, Signed, Transaction}; use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result}; use core::mem; @@ -195,7 +195,7 @@ impl TxLegacy { } } -impl OpTransaction for TxLegacy { +impl Transaction for TxLegacy { fn input(&self) -> &[u8] { &self.input } diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index 7369447a..d2851193 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -1,7 +1,3 @@ -use crate::Signed; -use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256}; -use core::any; - #[cfg(not(feature = "std"))] use alloc::vec::Vec; @@ -30,95 +26,3 @@ pub use legacy::TxLegacy; mod typed; pub use typed::OpTypedTransaction; - -/// Represents a minimal EVM transaction. -pub trait OpTransaction: any::Any + Send + Sync + 'static { - /// Get `data`. - fn input(&self) -> &[u8]; - - /// Get `to`. - fn to(&self) -> TxKind; - - /// Get `value`. - fn value(&self) -> U256; - - /// Get `chain_id`. - fn chain_id(&self) -> Option; - - /// Get `nonce`. - fn nonce(&self) -> u64; - - /// Get `gas_limit`. - fn gas_limit(&self) -> u128; - - /// Get `gas_price`. - fn gas_price(&self) -> Option; -} - -/// A signable transaction. -/// -/// A transaction can have multiple signature types. This is usually -/// [`alloy_primitives::Signature`], however, it may be different for future EIP-2718 transaction -/// types, or in other networks. For example, in Optimism, the deposit transaction signature is the -/// unit type `()`. -pub trait SignableTransaction: OpTransaction { - /// Sets `chain_id`. - /// - /// Prefer [`set_chain_id_checked`](Self::set_chain_id_checked). - fn set_chain_id(&mut self, chain_id: ChainId); - - /// Set `chain_id` if it is not already set. Checks that the provided `chain_id` matches the - /// existing `chain_id` if it is already set, returning `false` if they do not match. - fn set_chain_id_checked(&mut self, chain_id: ChainId) -> bool { - match self.chain_id() { - Some(tx_chain_id) => { - if tx_chain_id != chain_id { - return false; - } - self.set_chain_id(chain_id); - } - None => { - self.set_chain_id(chain_id); - } - } - true - } - - /// RLP-encodes the transaction for signing. - fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut); - - /// Outputs the length of the signature RLP encoding for the transaction. - fn payload_len_for_signature(&self) -> usize; - - /// RLP-encodes the transaction for signing it. Used to calculate `signature_hash`. - /// - /// See [`SignableTransaction::encode_for_signing`]. - fn encoded_for_signing(&self) -> Vec { - let mut buf = Vec::with_capacity(self.payload_len_for_signature()); - self.encode_for_signing(&mut buf); - buf - } - - /// Calculate the signing hash for the transaction. - fn signature_hash(&self) -> B256 { - keccak256(self.encoded_for_signing()) - } - - /// Convert to a signed transaction by adding a signature and computing the - /// hash. - fn into_signed(self, signature: Signature) -> Signed - where - Self: Sized; -} - -// TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162) -#[doc(hidden)] -impl dyn SignableTransaction { - pub fn __downcast_ref(&self) -> Option<&T> { - if any::Any::type_id(self) == any::TypeId::of::() { - unsafe { Some(&*(self as *const _ as *const T)) } - } else { - None - } - } -} diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs index ea09e367..3b7eb305 100644 --- a/crates/op-consensus/src/transaction/optimism.rs +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -1,4 +1,4 @@ -use crate::OpTransaction; +use alloy_consensus::Transaction; use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256}; use alloy_rlp::{ Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE, @@ -119,7 +119,7 @@ impl TxDeposit { } } -impl OpTransaction for TxDeposit { +impl Transaction for TxDeposit { fn input(&self) -> &[u8] { &self.input } diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index df00161f..9ce51d25 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -1,7 +1,5 @@ -use crate::{ - OpTransaction, OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, - TxLegacy, -}; +use crate::{OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, TxLegacy}; +use alloy_consensus::Transaction; use alloy_primitives::TxKind; /// The TypedTransaction enum represents all Ethereum transaction request types, modified for the OP @@ -113,7 +111,7 @@ impl OpTypedTransaction { } } -impl OpTransaction for OpTypedTransaction { +impl Transaction for OpTypedTransaction { fn chain_id(&self) -> Option { match self { Self::Legacy(tx) => tx.chain_id(), From c71623ea2ddae714eda4315c08a7dc97bf24279f Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 13 Apr 2024 12:39:11 -0400 Subject: [PATCH 06/20] Add deposit receipt roundtrip RLP tests --- crates/op-consensus/src/receipt/mod.rs | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index fdd4eb5c..9067a33d 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -145,4 +145,52 @@ mod tests { // let (decoded, _) = Receipt::from_compact(&data[..], data.len()); assert_eq!(decoded, receipt); } + + #[test] + fn regolith_receipt_roundtrip() { + let data = hex!("f9010c0182b741b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0833d3bbf"); + + // Deposit Receipt (post-regolith) + let expected = OpReceiptWithBloom { + receipt: OpReceipt { + cumulative_gas_used: 46913, + logs: vec![], + status: true, + deposit_nonce: Some(4012991), + deposit_receipt_version: None, + }, + logs_bloom: [0; 256].into(), + }; + + let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + assert_eq!(receipt, expected); + + let mut buf = Vec::new(); + receipt.encode(&mut buf); + assert_eq!(buf, &data[..]); + } + + #[test] + fn post_canyon_receipt_roundtrip() { + let data = hex!("f9010d0182b741b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0833d3bbf01"); + + // Deposit Receipt (post-regolith) + let expected = OpReceiptWithBloom { + receipt: OpReceipt { + cumulative_gas_used: 46913, + logs: vec![], + status: true, + deposit_nonce: Some(4012991), + deposit_receipt_version: Some(1), + }, + logs_bloom: [0; 256].into(), + }; + + let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + assert_eq!(receipt, expected); + + let mut buf = Vec::new(); + expected.encode(&mut buf); + assert_eq!(buf, &data[..]); + } } From c5956ea1ec3a205b96b39f76f025d378b2b6f9bc Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 13 Apr 2024 16:23:52 -0400 Subject: [PATCH 07/20] Use upstream Ethereum transaction types from `alloy-consensus` --- crates/op-consensus/Cargo.toml | 13 +- crates/op-consensus/README.md | 7 +- crates/op-consensus/src/lib.rs | 12 +- .../op-consensus/src/transaction/eip1559.rs | 410 ------ .../op-consensus/src/transaction/eip2930.rs | 368 ------ .../op-consensus/src/transaction/eip4844.rs | 1169 ----------------- .../src/transaction/eip4844/builder.rs | 426 ------ .../src/transaction/eip4844/utils.rs | 77 -- .../op-consensus/src/transaction/envelope.rs | 10 +- crates/op-consensus/src/transaction/legacy.rs | 345 ----- crates/op-consensus/src/transaction/mod.rs | 17 - crates/op-consensus/src/transaction/typed.rs | 4 +- .../testdata/rpc_blob_transaction.rlp | 1 - 13 files changed, 13 insertions(+), 2846 deletions(-) delete mode 100644 crates/op-consensus/src/transaction/eip1559.rs delete mode 100644 crates/op-consensus/src/transaction/eip2930.rs delete mode 100644 crates/op-consensus/src/transaction/eip4844.rs delete mode 100644 crates/op-consensus/src/transaction/eip4844/builder.rs delete mode 100644 crates/op-consensus/src/transaction/eip4844/utils.rs delete mode 100644 crates/op-consensus/src/transaction/legacy.rs delete mode 100644 crates/op-consensus/testdata/rpc_blob_transaction.rlp diff --git a/crates/op-consensus/Cargo.toml b/crates/op-consensus/Cargo.toml index 6a21a124..e8b36b73 100644 --- a/crates/op-consensus/Cargo.toml +++ b/crates/op-consensus/Cargo.toml @@ -18,12 +18,6 @@ alloy-rlp.workspace = true alloy-eips.workspace = true alloy-serde = { workspace = true, optional = true } -sha2 = { version = "0.10", default-features = false } - -# kzg -thiserror = { workspace = true, optional = true } -c-kzg = { workspace = true, features = ["serde"], optional = true } - # arbitrary arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -33,14 +27,13 @@ serde = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] alloy-signer.workspace = true arbitrary = { workspace = true, features = ["derive"] } -k256.workspace = true tokio = { workspace = true, features = ["macros"] } serde_json.workspace = true [features] default = ["std"] -std = ["alloy-eips/std", "alloy-consensus/std", "sha2/std", "c-kzg?/std"] +std = ["alloy-eips/std", "alloy-consensus/std"] k256 = ["alloy-primitives/k256", "alloy-consensus/k256"] -kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "alloy-consensus/kzg", "std"] +kzg = ["alloy-eips/kzg", "alloy-consensus/kzg", "std"] arbitrary = ["std", "dep:arbitrary", "alloy-consensus/arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"] -serde = ["dep:serde", "alloy-primitives/serde", "alloy-consensus/serde", "dep:alloy-serde", "alloy-eips/serde"] +serde = ["dep:serde", "dep:alloy-serde", "alloy-primitives/serde", "alloy-consensus/serde", "alloy-eips/serde"] diff --git a/crates/op-consensus/README.md b/crates/op-consensus/README.md index f20dff09..0f8e4079 100644 --- a/crates/op-consensus/README.md +++ b/crates/op-consensus/README.md @@ -3,15 +3,12 @@ OP Stack consensus interface. This crate contains constants, types, and functions for implementing Optimism EL consensus and communication. This -includes transactions, [EIP-2718] envelopes, [EIP-2930], [EIP-4844], [deposit transactions][deposit], and receipts. +includes an extended `OpTxEnvelope` type with [deposit transactions][deposit], and receipts containing OP Stack +specific fields (`deposit_nonce` + `deposit_receipt_version`). In general a type belongs in this crate if it exists in the `alloy-consensus` crate, but was modified from the base Ethereum protocol in the OP Stack. For consensus types that are not modified by the OP Stack, the `alloy-consensus` types should be used instead. -[alloy-network]: ../network -[EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 -[EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 -[EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 [deposit]: https://specs.optimism.io/protocol/deposits.html ## Provenance diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 3ade3a04..636330d9 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -23,14 +23,4 @@ mod receipt; pub use receipt::{OpReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; mod transaction; -pub use transaction::{ - eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTxEnvelope, OpTxType, - OpTypedTransaction, SidecarBuilder, SidecarCoder, SimpleCoder, TxDeposit, TxEip1559, TxEip2930, - TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, -}; - -#[cfg(feature = "kzg")] -pub use transaction::BlobTransactionValidationError; - -#[cfg(feature = "kzg")] -pub use alloy_eips::eip4844::env_settings::EnvKzgSettings; +pub use transaction::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit}; diff --git a/crates/op-consensus/src/transaction/eip1559.rs b/crates/op-consensus/src/transaction/eip1559.rs deleted file mode 100644 index 66e9008a..00000000 --- a/crates/op-consensus/src/transaction/eip1559.rs +++ /dev/null @@ -1,410 +0,0 @@ -use crate::OpTxType; -use alloy_consensus::{SignableTransaction, Signed, Transaction}; -use alloy_eips::eip2930::AccessList; -use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; -use alloy_rlp::{BufMut, Decodable, Encodable, Header}; -use core::mem; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -/// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)). -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct TxEip1559 { - /// EIP-155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub chain_id: ChainId, - /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub nonce: u64, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_limit: u128, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - /// - /// This is also known as `GasFeeCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub max_fee_per_gas: u128, - /// Max Priority fee that transaction is paying - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - /// - /// This is also known as `GasTipCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub max_priority_fee_per_gas: u128, - /// The 160-bit address of the message call’s recipient or, for a contract creation - /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. - #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] - pub to: TxKind, - /// A scalar value equal to the number of Wei to - /// be transferred to the message call’s recipient or, - /// in the case of contract creation, as an endowment - /// to the newly created account; formally Tv. - pub value: U256, - /// The accessList specifies a list of addresses and storage keys; - /// these addresses and storage keys are added into the `accessed_addresses` - /// and `accessed_storage_keys` global sets (introduced in EIP-2929). - /// A gas cost is charged, though at a discount relative to the cost of - /// accessing outside the list. - pub access_list: AccessList, - /// Input has two uses depending if transaction is Create or Call (if `to` field is None or - /// Some). pub init: An unlimited size byte array specifying the - /// EVM-code for the account initialisation procedure CREATE, - /// data: An unlimited size byte array specifying the - /// input data of the message call, formally Td. - pub input: Bytes, -} - -impl TxEip1559 { - /// Returns the effective gas price for the given `base_fee`. - pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } - } - } - } - - /// Decodes the inner [TxEip1559] fields from RLP bytes. - /// - /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following - /// RLP fields in the following order: - /// - /// - `chain_id` - /// - `nonce` - /// - `max_priority_fee_per_gas` - /// - `max_fee_per_gas` - /// - `gas_limit` - /// - `to` - /// - `value` - /// - `data` (`input`) - /// - `access_list` - pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(Self { - chain_id: Decodable::decode(buf)?, - nonce: Decodable::decode(buf)?, - max_priority_fee_per_gas: Decodable::decode(buf)?, - max_fee_per_gas: Decodable::decode(buf)?, - gas_limit: Decodable::decode(buf)?, - to: Decodable::decode(buf)?, - value: Decodable::decode(buf)?, - input: Decodable::decode(buf)?, - access_list: Decodable::decode(buf)?, - }) - } - - /// Encodes only the transaction's fields into the desired buffer, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { - let mut len = 0; - len += self.chain_id.length(); - len += self.nonce.length(); - len += self.max_priority_fee_per_gas.length(); - len += self.max_fee_per_gas.length(); - len += self.gas_limit.length(); - len += self.to.length(); - len += self.value.length(); - len += self.input.0.length(); - len += self.access_list.length(); - len - } - - /// Encodes only the transaction's fields into the desired buffer, without a RLP header. - pub(crate) fn encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) { - self.chain_id.encode(out); - self.nonce.encode(out); - self.max_priority_fee_per_gas.encode(out); - self.max_fee_per_gas.encode(out); - self.gas_limit.encode(out); - self.to.encode(out); - self.value.encode(out); - self.input.0.encode(out); - self.access_list.encode(out); - } - - /// Returns what the encoded length should be, if the transaction were RLP encoded with the - /// given signature, depending on the value of `with_header`. - /// - /// If `with_header` is `true`, the payload length will include the RLP header length. - /// If `with_header` is `false`, the payload length will not include the RLP header length. - pub(crate) fn encoded_len_with_signature( - &self, - signature: &Signature, - with_header: bool, - ) -> usize { - // this counts the tx fields and signature fields - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - - // this counts: - // * tx type byte - // * inner header length - // * inner payload length - let inner_payload_length = - 1 + Header { list: true, payload_length }.length() + payload_length; - - if with_header { - // header length plus length of the above, wrapped with a string header - Header { list: false, payload_length: inner_payload_length }.length() - + inner_payload_length - } else { - inner_payload_length - } - } - - /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating - /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( - &self, - signature: &Signature, - out: &mut dyn BufMut, - with_header: bool, - ) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - if with_header { - Header { - list: false, - payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, - } - .encode(out); - } - out.put_u8(self.tx_type() as u8); - self.encode_with_signature_fields(signature, out); - } - - /// Decodes the transaction from RLP bytes, including the signature. - /// - /// This __does not__ expect the bytes to start with a transaction type byte or string - /// header. - /// - /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let header = Header::decode(buf)?; - if !header.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - - // record original length so we can check encoding - let original_len = buf.len(); - - let tx = Self::decode_fields(buf)?; - let signature = Signature::decode_rlp_vrs(buf)?; - - let signed = tx.into_signed(signature); - if buf.len() + header.payload_length != original_len { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - - Ok(signed) - } - - /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a - /// tx type byte or string header. - /// - /// This __does__ encode a list header and include a signature. - pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - let header = Header { list: true, payload_length }; - header.encode(out); - self.encode_fields(out); - signature.write_rlp_vrs(out); - } - - /// Get transaction type - pub(crate) const fn tx_type(&self) -> OpTxType { - OpTxType::Eip1559 - } - - /// Calculates a heuristic for the in-memory size of the [TxEip1559] transaction. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::() + // chain_id - mem::size_of::() + // nonce - mem::size_of::() + // gas_limit - mem::size_of::() + // max_fee_per_gas - mem::size_of::() + // max_priority_fee_per_gas - self.to.size() + // to - mem::size_of::() + // value - self.access_list.size() + // access_list - self.input.len() // input - } -} - -impl Transaction for TxEip1559 { - fn input(&self) -> &[u8] { - &self.input - } - - fn to(&self) -> TxKind { - self.to - } - - fn value(&self) -> U256 { - self.value - } - - fn chain_id(&self) -> Option { - Some(self.chain_id) - } - - fn nonce(&self) -> u64 { - self.nonce - } - - fn gas_limit(&self) -> u128 { - self.gas_limit - } - - fn gas_price(&self) -> Option { - None - } -} - -impl SignableTransaction for TxEip1559 { - fn set_chain_id(&mut self, chain_id: ChainId) { - self.chain_id = chain_id; - } - - fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { - out.put_u8(self.tx_type() as u8); - self.encode(out) - } - - fn payload_len_for_signature(&self) -> usize { - self.length() + 1 - } - - fn into_signed(self, signature: Signature) -> Signed { - let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); - self.encode_with_signature(&signature, &mut buf, false); - let hash = keccak256(&buf); - - // Drop any v chain id value to ensure the signature format is correct at the time of - // combination for an EIP-1559 transaction. V should indicate the y-parity of the - // signature. - Signed::new_unchecked(self, signature.with_parity_bool(), hash) - } -} - -impl Encodable for TxEip1559 { - fn encode(&self, out: &mut dyn BufMut) { - Header { list: true, payload_length: self.fields_len() }.encode(out); - self.encode_fields(out); - } - - fn length(&self) -> usize { - let payload_length = self.fields_len(); - Header { list: true, payload_length }.length() + payload_length - } -} - -impl Decodable for TxEip1559 { - fn decode(data: &mut &[u8]) -> alloy_rlp::Result { - let header = Header::decode(data)?; - let remaining_len = data.len(); - - if header.payload_length > remaining_len { - return Err(alloy_rlp::Error::InputTooShort); - } - - Self::decode_fields(data) - } -} - -#[cfg(all(test, feature = "k256"))] -mod tests { - use super::TxEip1559; - use crate::SignableTransaction; - use alloy_eips::eip2930::AccessList; - use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256}; - - #[test] - fn recover_signer_eip1559() { - let signer: Address = address!("dd6b8b3dc6b7ad97db52f08a275ff4483e024cea"); - let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); - - let tx = TxEip1559 { - chain_id: 1, - nonce: 0x42, - gas_limit: 44386, - to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), - value: U256::from(0_u64), - input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), - max_fee_per_gas: 0x4a817c800, - max_priority_fee_per_gas: 0x3b9aca00, - access_list: AccessList::default(), - }; - - let sig = Signature::from_scalars_and_parity( - b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), - b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), - false, - ) - .unwrap(); - - assert_eq!( - tx.signature_hash(), - hex!("0d5688ac3897124635b6cf1bc0e29d6dfebceebdc10a54d74f2ef8b56535b682") - ); - - let signed_tx = tx.into_signed(sig); - assert_eq!(*signed_tx.hash(), hash, "Expected same hash"); - assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass."); - } - - #[test] - fn encode_decode_eip1559() { - let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); - - let tx = TxEip1559 { - chain_id: 1, - nonce: 0x42, - gas_limit: 44386, - to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), - value: U256::from(0_u64), - input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), - max_fee_per_gas: 0x4a817c800, - max_priority_fee_per_gas: 0x3b9aca00, - access_list: AccessList::default(), - }; - - let sig = Signature::from_scalars_and_parity( - b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), - b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), - false, - ) - .unwrap(); - - let mut buf = vec![]; - tx.encode_with_signature_fields(&sig, &mut buf); - let decoded = TxEip1559::decode_signed_fields(&mut &buf[..]).unwrap(); - assert_eq!(decoded, tx.into_signed(sig)); - assert_eq!(*decoded.hash(), hash); - } -} diff --git a/crates/op-consensus/src/transaction/eip2930.rs b/crates/op-consensus/src/transaction/eip2930.rs deleted file mode 100644 index d9cfd576..00000000 --- a/crates/op-consensus/src/transaction/eip2930.rs +++ /dev/null @@ -1,368 +0,0 @@ -use crate::OpTxType; -use alloy_consensus::{SignableTransaction, Signed, Transaction}; -use alloy_eips::eip2930::AccessList; -use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; -use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; -use core::mem; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)). -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct TxEip2930 { - /// Added as EIP-pub 155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub chain_id: ChainId, - /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub nonce: u64, - /// A scalar value equal to the number of - /// Wei to be paid per unit of gas for all computation - /// costs incurred as a result of the execution of this transaction; formally Tp. - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_price: u128, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_limit: u128, - /// The 160-bit address of the message call’s recipient or, for a contract creation - /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. - #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] - pub to: TxKind, - /// A scalar value equal to the number of Wei to - /// be transferred to the message call’s recipient or, - /// in the case of contract creation, as an endowment - /// to the newly created account; formally Tv. - pub value: U256, - /// The accessList specifies a list of addresses and storage keys; - /// these addresses and storage keys are added into the `accessed_addresses` - /// and `accessed_storage_keys` global sets (introduced in EIP-2929). - /// A gas cost is charged, though at a discount relative to the cost of - /// accessing outside the list. - pub access_list: AccessList, - /// Input has two uses depending if transaction is Create or Call (if `to` field is None or - /// Some). pub init: An unlimited size byte array specifying the - /// EVM-code for the account initialisation procedure CREATE, - /// data: An unlimited size byte array specifying the - /// input data of the message call, formally Td. - pub input: Bytes, -} - -impl TxEip2930 { - /// Calculates a heuristic for the in-memory size of the [TxEip2930] transaction. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::() + // chain_id - mem::size_of::() + // nonce - mem::size_of::() + // gas_price - mem::size_of::() + // gas_limit - self.to.size() + // to - mem::size_of::() + // value - self.access_list.size() + // access_list - self.input.len() // input - } - - /// Decodes the inner [TxEip2930] fields from RLP bytes. - /// - /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following - /// RLP fields in the following order: - /// - /// - `chain_id` - /// - `nonce` - /// - `gas_price` - /// - `gas_limit` - /// - `to` - /// - `value` - /// - `data` (`input`) - /// - `access_list` - pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(Self { - chain_id: Decodable::decode(buf)?, - nonce: Decodable::decode(buf)?, - gas_price: Decodable::decode(buf)?, - gas_limit: Decodable::decode(buf)?, - to: Decodable::decode(buf)?, - value: Decodable::decode(buf)?, - input: Decodable::decode(buf)?, - access_list: Decodable::decode(buf)?, - }) - } - - /// Outputs the length of the transaction's fields, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { - let mut len = 0; - len += self.chain_id.length(); - len += self.nonce.length(); - len += self.gas_price.length(); - len += self.gas_limit.length(); - len += self.to.length(); - len += self.value.length(); - len += self.input.0.length(); - len += self.access_list.length(); - len - } - - /// Encodes only the transaction's fields into the desired buffer, without a RLP header. - pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { - self.chain_id.encode(out); - self.nonce.encode(out); - self.gas_price.encode(out); - self.gas_limit.encode(out); - self.to.encode(out); - self.value.encode(out); - self.input.0.encode(out); - self.access_list.encode(out); - } - - /// Returns what the encoded length should be, if the transaction were RLP encoded with the - /// given signature, depending on the value of `with_header`. - /// - /// If `with_header` is `true`, the payload length will include the RLP header length. - /// If `with_header` is `false`, the payload length will not include the RLP header length. - pub(crate) fn encoded_len_with_signature( - &self, - signature: &Signature, - with_header: bool, - ) -> usize { - // this counts the tx fields and signature fields - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - - // this counts: - // * tx type byte - // * inner header length - // * inner payload length - let inner_payload_length = - 1 + Header { list: true, payload_length }.length() + payload_length; - - if with_header { - // header length plus length of the above, wrapped with a string header - Header { list: false, payload_length: inner_payload_length }.length() - + inner_payload_length - } else { - inner_payload_length - } - } - - /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating - /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( - &self, - signature: &Signature, - out: &mut dyn BufMut, - with_header: bool, - ) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - if with_header { - Header { - list: false, - payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, - } - .encode(out); - } - out.put_u8(self.tx_type() as u8); - self.encode_with_signature_fields(signature, out); - } - - /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a - /// tx type byte or string header. - /// - /// This __does__ encode a list header and include a signature. - pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - let header = Header { list: true, payload_length }; - header.encode(out); - self.encode_fields(out); - signature.write_rlp_vrs(out); - } - - /// Decodes the transaction from RLP bytes, including the signature. - /// - /// This __does not__ expect the bytes to start with a transaction type byte or string - /// header. - /// - /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let header = Header::decode(buf)?; - if !header.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - - // record original length so we can check encoding - let original_len = buf.len(); - - let tx = Self::decode_fields(buf)?; - let signature = Signature::decode_rlp_vrs(buf)?; - - let signed = tx.into_signed(signature); - if buf.len() + header.payload_length != original_len { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - - Ok(signed) - } - - /// Get transaction type. - pub const fn tx_type(&self) -> OpTxType { - OpTxType::Eip2930 - } -} - -impl Transaction for TxEip2930 { - fn input(&self) -> &[u8] { - &self.input - } - - fn to(&self) -> TxKind { - self.to - } - - fn value(&self) -> U256 { - self.value - } - - fn chain_id(&self) -> Option { - Some(self.chain_id) - } - - fn nonce(&self) -> u64 { - self.nonce - } - - fn gas_limit(&self) -> u128 { - self.gas_limit - } - - fn gas_price(&self) -> Option { - Some(self.gas_price) - } -} - -impl SignableTransaction for TxEip2930 { - fn set_chain_id(&mut self, chain_id: ChainId) { - self.chain_id = chain_id; - } - - fn encode_for_signing(&self, out: &mut dyn BufMut) { - out.put_u8(self.tx_type() as u8); - Header { list: true, payload_length: self.fields_len() }.encode(out); - self.encode_fields(out); - } - - fn payload_len_for_signature(&self) -> usize { - let payload_length = self.fields_len(); - // 'transaction type byte length' + 'header length' + 'payload length' - 1 + Header { list: true, payload_length }.length() + payload_length - } - - fn into_signed(self, signature: Signature) -> Signed { - let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); - self.encode_with_signature(&signature, &mut buf, false); - let hash = keccak256(&buf); - - // Drop any v chain id value to ensure the signature format is correct at the time of - // combination for an EIP-2930 transaction. V should indicate the y-parity of the - // signature. - Signed::new_unchecked(self, signature.with_parity_bool(), hash) - } -} - -impl Encodable for TxEip2930 { - fn encode(&self, out: &mut dyn BufMut) { - Header { list: true, payload_length: self.fields_len() }.encode(out); - self.encode_fields(out); - } - - fn length(&self) -> usize { - let payload_length = self.fields_len(); - length_of_length(payload_length) + payload_length - } -} - -impl Decodable for TxEip2930 { - fn decode(data: &mut &[u8]) -> alloy_rlp::Result { - let header = Header::decode(data)?; - let remaining_len = data.len(); - - if header.payload_length > remaining_len { - return Err(alloy_rlp::Error::InputTooShort); - } - - Self::decode_fields(data) - } -} - -#[cfg(test)] -mod tests { - use super::TxEip2930; - use crate::OpTxEnvelope; - use alloy_consensus::SignableTransaction; - use alloy_primitives::{Address, Bytes, Signature, TxKind, U256}; - use alloy_rlp::{Decodable, Encodable}; - - #[test] - fn test_decode_create() { - // tests that a contract creation tx encodes and decodes properly - let tx = TxEip2930 { - chain_id: 1u64, - nonce: 0, - gas_price: 1, - gas_limit: 2, - to: TxKind::Create, - value: U256::from(3_u64), - input: Bytes::from(vec![1, 2]), - access_list: Default::default(), - }; - let signature = Signature::test_signature(); - - let mut encoded = Vec::new(); - tx.encode_with_signature_fields(&signature, &mut encoded); - - let decoded = TxEip2930::decode_signed_fields(&mut &*encoded).unwrap(); - assert_eq!(decoded, tx.into_signed(signature)); - } - - #[test] - fn test_decode_call() { - let request = TxEip2930 { - chain_id: 1u64, - nonce: 0, - gas_price: 1, - gas_limit: 2, - to: TxKind::Call(Address::default()), - value: U256::from(3_u64), - input: Bytes::from(vec![1, 2]), - access_list: Default::default(), - }; - - let signature = Signature::test_signature(); - - let tx = request.into_signed(signature); - - let envelope = OpTxEnvelope::Eip2930(tx); - - let mut encoded = Vec::new(); - envelope.encode(&mut encoded); - assert_eq!(encoded.len(), envelope.length()); - - assert_eq!( - alloy_primitives::hex::encode(&encoded), - "b86401f8610180010294000000000000000000000000000000000000000003820102c080a0840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565a025e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1" - ); - - let decoded = OpTxEnvelope::decode(&mut encoded.as_ref()).unwrap(); - assert_eq!(decoded, envelope); - } -} diff --git a/crates/op-consensus/src/transaction/eip4844.rs b/crates/op-consensus/src/transaction/eip4844.rs deleted file mode 100644 index 7808ccdc..00000000 --- a/crates/op-consensus/src/transaction/eip4844.rs +++ /dev/null @@ -1,1169 +0,0 @@ -mod builder; -pub use builder::{SidecarBuilder, SidecarCoder, SimpleCoder}; - -pub mod utils; - -use crate::OpTxType; -use alloy_consensus::{SignableTransaction, Signed, Transaction}; -use alloy_eips::{ - eip2930::AccessList, - eip4844::{BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, DATA_GAS_PER_BLOB}, -}; -use alloy_primitives::{ - keccak256, Address, Bytes, ChainId, FixedBytes, Signature, TxKind, B256, U256, -}; -use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; -use core::mem; -use sha2::{Digest, Sha256}; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -#[cfg(not(feature = "kzg"))] -pub use alloy_eips::eip4844::{Blob, Bytes48}; - -#[cfg(feature = "kzg")] -use c_kzg::{KzgCommitment, KzgProof, KzgSettings}; -#[cfg(feature = "kzg")] -use std::ops::Deref; - -#[cfg(feature = "kzg")] -pub use c_kzg::{Blob, Bytes48}; - -/// An error that can occur when validating a [TxEip4844Variant]. -#[derive(Debug, thiserror::Error)] -#[cfg(feature = "kzg")] -pub enum BlobTransactionValidationError { - /// Proof validation failed. - #[error("invalid KZG proof")] - InvalidProof, - /// An error returned by [`c_kzg`]. - #[error("KZG error: {0:?}")] - KZGError(#[from] c_kzg::Error), - /// The inner transaction is not a blob transaction. - #[error("unable to verify proof for non blob transaction: {0}")] - NotBlobTransaction(u8), - /// Using a standalone [TxEip4844] instead of the [TxEip4844WithSidecar] variant, which - /// includes the sidecar for validation. - #[error("eip4844 tx variant without sidecar being used for verification. Please use the TxEip4844WithSidecar variant, which includes the sidecar")] - MissingSidecar, - /// The versioned hash is incorrect. - #[error("wrong versioned hash: have {have}, expected {expected}")] - WrongVersionedHash { - /// The versioned hash we got - have: B256, - /// The versioned hash we expected - expected: B256, - }, -} - -/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) -/// -/// A transaction with blob hashes and max blob fee. -/// It can either be a standalone transaction, mainly seen when retrieving historical transactions, -/// or a transaction with a sidecar, which is used when submitting a transaction to the network and -/// when receiving and sending transactions during the gossip stage. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize))] -#[cfg_attr(feature = "serde", serde(untagged))] -pub enum TxEip4844Variant { - /// A standalone transaction with blob hashes and max blob fee. - TxEip4844(TxEip4844), - /// A transaction with a sidecar, which contains the blob data, commitments, and proofs. - TxEip4844WithSidecar(TxEip4844WithSidecar), -} - -#[cfg(feature = "serde")] -impl<'de> serde::Deserialize<'de> for TxEip4844Variant { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - #[derive(serde::Deserialize)] - struct TxEip4844SerdeHelper { - #[serde(flatten)] - tx: TxEip4844, - #[serde(flatten)] - sidecar: Option, - } - - let tx = TxEip4844SerdeHelper::deserialize(deserializer)?; - - if let Some(sidecar) = tx.sidecar { - Ok(TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar::from_tx_and_sidecar( - tx.tx, sidecar, - ))) - } else { - Ok(TxEip4844Variant::TxEip4844(tx.tx)) - } - } -} - -impl From for TxEip4844Variant { - fn from(tx: TxEip4844WithSidecar) -> Self { - TxEip4844Variant::TxEip4844WithSidecar(tx) - } -} - -impl From for TxEip4844Variant { - fn from(tx: TxEip4844) -> Self { - TxEip4844Variant::TxEip4844(tx) - } -} - -impl From<(TxEip4844, BlobTransactionSidecar)> for TxEip4844Variant { - fn from((tx, sidecar): (TxEip4844, BlobTransactionSidecar)) -> Self { - TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar::from_tx_and_sidecar( - tx, sidecar, - )) - } -} - -impl TxEip4844Variant { - /// Verifies that the transaction's blob data, commitments, and proofs are all valid. - /// - /// See also [TxEip4844::validate_blob] - #[cfg(feature = "kzg")] - pub fn validate( - &self, - proof_settings: &KzgSettings, - ) -> Result<(), BlobTransactionValidationError> { - match self { - TxEip4844Variant::TxEip4844(_) => Err(BlobTransactionValidationError::MissingSidecar), - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.validate_blob(proof_settings), - } - } - - /// Get the transaction type. - pub const fn tx_type(&self) -> OpTxType { - OpTxType::Eip4844 - } - - /// Get access to the inner tx [TxEip4844]. - pub const fn tx(&self) -> &TxEip4844 { - match self { - TxEip4844Variant::TxEip4844(tx) => tx, - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx(), - } - } - - pub(crate) fn fields_len(&self) -> usize { - match self { - TxEip4844Variant::TxEip4844(tx) => tx.fields_len(), - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().fields_len(), - } - } - - /// Encodes the [TxEip4844Variant] fields as RLP, with a tx type. If `with_header` is `false`, - /// the following will be encoded: - /// `tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs])` - /// - /// If `with_header` is `true`, the following will be encoded: - /// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))` - pub(crate) fn encode_with_signature( - &self, - signature: &Signature, - out: &mut dyn BufMut, - with_header: bool, - ) { - let payload_length = match self { - TxEip4844Variant::TxEip4844(tx) => tx.fields_len() + signature.rlp_vrs_len(), - TxEip4844Variant::TxEip4844WithSidecar(tx) => { - let payload_length = tx.tx().fields_len() + signature.rlp_vrs_len(); - let inner_header = Header { list: true, payload_length }; - inner_header.length() + payload_length + tx.sidecar().fields_len() - } - }; - - if with_header { - Header { - list: false, - payload_length: 1 - + Header { list: false, payload_length }.length() - + payload_length, - } - .encode(out); - } - out.put_u8(self.tx_type() as u8); - - match self { - TxEip4844Variant::TxEip4844(tx) => { - tx.encode_with_signature_fields(signature, out); - } - TxEip4844Variant::TxEip4844WithSidecar(tx) => { - tx.encode_with_signature_fields(signature, out); - } - } - } - - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let mut current_buf = *buf; - let _header = Header::decode(&mut current_buf)?; - - // There are two possibilities when decoding a signed EIP-4844 transaction: - // If it's a historical transaction, it will only have the transaction fields, and no - // sidecar. If it's a transaction received during the gossip stage or sent through - // eth_sendRawTransaction, it will have the transaction fields and a sidecar. - // - // To disambiguate, we try to decode two list headers. If there is only one list header, we - // assume it's a historical transaction. If there are two, we know the transaction contains - // a sidecar. - let header = Header::decode(&mut current_buf)?; - if header.list { - let tx = TxEip4844WithSidecar::decode_signed_fields(buf)?; - let (tx, signature, hash) = tx.into_parts(); - return Ok(Signed::new_unchecked( - TxEip4844Variant::TxEip4844WithSidecar(tx), - signature, - hash, - )); - } - - // Since there is not a second list header, this is a historical 4844 transaction without a - // sidecar. - let tx = TxEip4844::decode_signed_fields(buf)?; - let (tx, signature, hash) = tx.into_parts(); - Ok(Signed::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)) - } -} - -impl Transaction for TxEip4844Variant { - fn chain_id(&self) -> Option { - match self { - TxEip4844Variant::TxEip4844(tx) => Some(tx.chain_id), - TxEip4844Variant::TxEip4844WithSidecar(tx) => Some(tx.tx().chain_id), - } - } - - fn gas_limit(&self) -> u128 { - match self { - TxEip4844Variant::TxEip4844(tx) => tx.gas_limit, - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().gas_limit, - } - } - - fn gas_price(&self) -> Option { - None - } - - fn input(&self) -> &[u8] { - match self { - TxEip4844Variant::TxEip4844(tx) => tx.input.as_ref(), - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().input.as_ref(), - } - } - - fn nonce(&self) -> u64 { - match self { - TxEip4844Variant::TxEip4844(tx) => tx.nonce, - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().nonce, - } - } - - fn to(&self) -> TxKind { - let address = match self { - TxEip4844Variant::TxEip4844(tx) => tx.to, - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx.to, - }; - TxKind::Call(address) - } - - fn value(&self) -> U256 { - match self { - TxEip4844Variant::TxEip4844(tx) => tx.value, - TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx.value, - } - } -} - -impl SignableTransaction for TxEip4844Variant { - fn set_chain_id(&mut self, chain_id: ChainId) { - match self { - TxEip4844Variant::TxEip4844(ref mut inner) => { - inner.chain_id = chain_id; - } - TxEip4844Variant::TxEip4844WithSidecar(ref mut inner) => { - inner.tx.chain_id = chain_id; - } - } - } - - fn payload_len_for_signature(&self) -> usize { - let payload_length = self.fields_len(); - // 'transaction type byte length' + 'header length' + 'payload length' - 1 + length_of_length(payload_length) + payload_length - } - - fn into_signed(self, signature: Signature) -> Signed { - let payload_length = 1 + self.fields_len() + signature.rlp_vrs_len(); - let mut buf = Vec::with_capacity(payload_length); - buf.put_u8(OpTxType::Eip4844 as u8); - // we use the inner tx to encode the fields - self.tx().encode_with_signature(&signature, &mut buf, false); - let hash = keccak256(&buf); - - // Drop any v chain id value to ensure the signature format is correct at the time of - // combination for an EIP-4844 transaction. V should indicate the y-parity of the - // signature. - Signed::new_unchecked(self, signature.with_parity_bool(), hash) - } - - fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { - // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844Variant] - // EIP-2718 payload fields: - // (BLOB_TX_TYPE || - // rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value, - // data, access_list, max_fee_per_blob_gas, blob_versioned_hashes])) - self.tx().encode_for_signing(out); - } -} - -/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) -/// -/// A transaction with blob hashes and max blob fee. It does not have the Blob sidecar. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct TxEip4844 { - /// Added as EIP-pub 155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub chain_id: ChainId, - /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub nonce: u64, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_limit: u128, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - /// - /// This is also known as `GasFeeCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub max_fee_per_gas: u128, - /// Max Priority fee that transaction is paying - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - /// - /// This is also known as `GasTipCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub max_priority_fee_per_gas: u128, - /// The 160-bit address of the message call’s recipient. - pub to: Address, - /// A scalar value equal to the number of Wei to - /// be transferred to the message call’s recipient or, - /// in the case of contract creation, as an endowment - /// to the newly created account; formally Tv. - pub value: U256, - /// The accessList specifies a list of addresses and storage keys; - /// these addresses and storage keys are added into the `accessed_addresses` - /// and `accessed_storage_keys` global sets (introduced in EIP-2929). - /// A gas cost is charged, though at a discount relative to the cost of - /// accessing outside the list. - pub access_list: AccessList, - - /// It contains a vector of fixed size hash(32 bytes) - pub blob_versioned_hashes: Vec, - - /// Max fee per data gas - /// - /// aka BlobFeeCap or blobGasFeeCap - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub max_fee_per_blob_gas: u128, - - /// Input has two uses depending if transaction is Create or Call (if `to` field is None or - /// Some). pub init: An unlimited size byte array specifying the - /// EVM-code for the account initialisation procedure CREATE, - /// data: An unlimited size byte array specifying the - /// input data of the message call, formally Td. - pub input: Bytes, -} - -impl TxEip4844 { - /// Returns the effective gas price for the given `base_fee`. - pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } - } - } - } - - /// Returns the total gas for all blobs in this transaction. - #[inline] - pub fn blob_gas(&self) -> u64 { - // SAFETY: we don't expect u64::MAX / DATA_GAS_PER_BLOB hashes in a single transaction - self.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB - } - - /// Verifies that the given blob data, commitments, and proofs are all valid for this - /// transaction. - /// - /// Takes as input the [KzgSettings], which should contain the parameters derived from the - /// KZG trusted setup. - /// - /// This ensures that the blob transaction payload has the same number of blob data elements, - /// commitments, and proofs. Each blob data element is verified against its commitment and - /// proof. - /// - /// Returns [BlobTransactionValidationError::InvalidProof] if any blob KZG proof in the response - /// fails to verify, or if the versioned hashes in the transaction do not match the actual - /// commitment versioned hashes. - #[cfg(feature = "kzg")] - pub fn validate_blob( - &self, - sidecar: &BlobTransactionSidecar, - proof_settings: &KzgSettings, - ) -> Result<(), BlobTransactionValidationError> { - // Ensure the versioned hashes and commitments have the same length. - if self.blob_versioned_hashes.len() != sidecar.commitments.len() { - return Err(c_kzg::Error::MismatchLength(format!( - "There are {} versioned commitment hashes and {} commitments", - self.blob_versioned_hashes.len(), - sidecar.commitments.len() - )) - .into()); - } - - // calculate versioned hashes by zipping & iterating - for (versioned_hash, commitment) in - self.blob_versioned_hashes.iter().zip(sidecar.commitments.iter()) - { - let commitment = KzgCommitment::from(*commitment.deref()); - - // calculate & verify versioned hash - let calculated_versioned_hash = kzg_to_versioned_hash(commitment.as_slice()); - if *versioned_hash != calculated_versioned_hash { - return Err(BlobTransactionValidationError::WrongVersionedHash { - have: *versioned_hash, - expected: calculated_versioned_hash, - }); - } - } - - let res = KzgProof::verify_blob_kzg_proof_batch( - sidecar.blobs.as_slice(), - sidecar.commitments.as_slice(), - sidecar.proofs.as_slice(), - proof_settings, - ) - .map_err(BlobTransactionValidationError::KZGError)?; - - if res { - Ok(()) - } else { - Err(BlobTransactionValidationError::InvalidProof) - } - } - - /// Decodes the inner [TxEip4844Variant] fields from RLP bytes. - /// - /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following - /// RLP fields in the following order: - /// - /// - `chain_id` - /// - `nonce` - /// - `max_priority_fee_per_gas` - /// - `max_fee_per_gas` - /// - `gas_limit` - /// - `to` - /// - `value` - /// - `data` (`input`) - /// - `access_list` - /// - `max_fee_per_blob_gas` - /// - `blob_versioned_hashes` - pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(Self { - chain_id: Decodable::decode(buf)?, - nonce: Decodable::decode(buf)?, - max_priority_fee_per_gas: Decodable::decode(buf)?, - max_fee_per_gas: Decodable::decode(buf)?, - gas_limit: Decodable::decode(buf)?, - to: Decodable::decode(buf)?, - value: Decodable::decode(buf)?, - input: Decodable::decode(buf)?, - access_list: Decodable::decode(buf)?, - max_fee_per_blob_gas: Decodable::decode(buf)?, - blob_versioned_hashes: Decodable::decode(buf)?, - }) - } - - /// Outputs the length of the transaction's fields, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { - let mut len = 0; - len += self.chain_id.length(); - len += self.nonce.length(); - len += self.gas_limit.length(); - len += self.max_fee_per_gas.length(); - len += self.max_priority_fee_per_gas.length(); - len += self.to.length(); - len += self.value.length(); - len += self.access_list.length(); - len += self.blob_versioned_hashes.length(); - len += self.max_fee_per_blob_gas.length(); - len += self.input.0.length(); - len - } - - /// Encodes only the transaction's fields into the desired buffer, without a RLP header. - pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { - self.chain_id.encode(out); - self.nonce.encode(out); - self.max_priority_fee_per_gas.encode(out); - self.max_fee_per_gas.encode(out); - self.gas_limit.encode(out); - self.to.encode(out); - self.value.encode(out); - self.input.0.encode(out); - self.access_list.encode(out); - self.max_fee_per_blob_gas.encode(out); - self.blob_versioned_hashes.encode(out); - } - - /// Calculates a heuristic for the in-memory size of the [TxEip4844Variant] transaction. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::() + // chain_id - mem::size_of::() + // nonce - mem::size_of::() + // gas_limit - mem::size_of::() + // max_fee_per_gas - mem::size_of::() + // max_priority_fee_per_gas - mem::size_of::
() + // to - mem::size_of::() + // value - self.access_list.size() + // access_list - self.input.len() + // input - self.blob_versioned_hashes.capacity() * mem::size_of::() + // blob hashes size - mem::size_of::() // max_fee_per_data_gas - } - - /// Returns what the encoded length should be, if the transaction were RLP encoded with the - /// given signature, depending on the value of `with_header`. - /// - /// If `with_header` is `true`, the payload length will include the RLP header length. - /// If `with_header` is `false`, the payload length will not include the RLP header length. - pub(crate) fn encoded_len_with_signature( - &self, - signature: &Signature, - with_header: bool, - ) -> usize { - // this counts the tx fields and signature fields - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - - // this counts: - // * tx type byte - // * inner header length - // * inner payload length - let inner_payload_length = - 1 + Header { list: true, payload_length }.length() + payload_length; - - if with_header { - // header length plus length of the above, wrapped with a string header - Header { list: false, payload_length: inner_payload_length }.length() - + inner_payload_length - } else { - inner_payload_length - } - } - - /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating - /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( - &self, - signature: &Signature, - out: &mut dyn BufMut, - with_header: bool, - ) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - if with_header { - Header { - list: false, - payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, - } - .encode(out); - } - out.put_u8(self.tx_type() as u8); - self.encode_with_signature_fields(signature, out); - } - - /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a - /// tx type byte or string header. - /// - /// This __does__ encode a list header and include a signature. - pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - let header = Header { list: true, payload_length }; - header.encode(out); - self.encode_fields(out); - signature.write_rlp_vrs(out); - } - - /// Decodes the transaction from RLP bytes, including the signature. - /// - /// This __does not__ expect the bytes to start with a transaction type byte or string - /// header. - /// - /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let header = Header::decode(buf)?; - if !header.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - - // record original length so we can check encoding - let original_len = buf.len(); - - let tx = Self::decode_fields(buf)?; - let signature = Signature::decode_rlp_vrs(buf)?; - - let signed = tx.into_signed(signature); - if buf.len() + header.payload_length != original_len { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - - Ok(signed) - } - - /// Get transaction type - pub const fn tx_type(&self) -> OpTxType { - OpTxType::Eip4844 - } - - /// Encodes the EIP-4844 transaction in RLP for signing. - /// - /// This encodes the transaction as: - /// `tx_type || rlp(chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, - /// value, input, access_list, max_fee_per_blob_gas, blob_versioned_hashes)` - /// - /// Note that there is no rlp header before the transaction type byte. - pub fn encode_for_signing(&self, out: &mut dyn BufMut) { - out.put_u8(self.tx_type() as u8); - Header { list: true, payload_length: self.fields_len() }.encode(out); - self.encode_fields(out); - } - - /// Outputs the length of the signature RLP encoding for the transaction. - pub fn payload_len_for_signature(&self) -> usize { - let payload_length = self.fields_len(); - // 'transaction type byte length' + 'header length' + 'payload length' - 1 + Header { list: true, payload_length }.length() + payload_length - } -} - -impl SignableTransaction for TxEip4844 { - fn set_chain_id(&mut self, chain_id: ChainId) { - self.chain_id = chain_id; - } - - fn payload_len_for_signature(&self) -> usize { - self.payload_len_for_signature() - } - - fn into_signed(self, signature: Signature) -> Signed { - let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); - self.encode_with_signature(&signature, &mut buf, false); - let hash = keccak256(&buf); - - // Drop any v chain id value to ensure the signature format is correct at the time of - // combination for an EIP-4844 transaction. V should indicate the y-parity of the - // signature. - Signed::new_unchecked(self, signature.with_parity_bool(), hash) - } - - fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { - self.encode_for_signing(out); - } -} - -impl Transaction for TxEip4844 { - fn input(&self) -> &[u8] { - &self.input - } - - fn to(&self) -> TxKind { - TxKind::Call(self.to) - } - - fn value(&self) -> U256 { - self.value - } - - fn chain_id(&self) -> Option { - Some(self.chain_id) - } - - fn nonce(&self) -> u64 { - self.nonce - } - - fn gas_limit(&self) -> u128 { - self.gas_limit - } - - fn gas_price(&self) -> Option { - None - } -} - -impl Encodable for TxEip4844 { - fn encode(&self, out: &mut dyn BufMut) { - Header { list: true, payload_length: self.fields_len() }.encode(out); - self.encode_fields(out); - } - - fn length(&self) -> usize { - let payload_length = self.fields_len(); - length_of_length(payload_length) + payload_length - } -} - -impl Decodable for TxEip4844 { - fn decode(data: &mut &[u8]) -> alloy_rlp::Result { - let header = Header::decode(data)?; - let remaining_len = data.len(); - - if header.payload_length > remaining_len { - return Err(alloy_rlp::Error::InputTooShort); - } - - Self::decode_fields(data) - } -} - -/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) -/// -/// A transaction with blob hashes and max blob fee, which also includes the -/// [BlobTransactionSidecar]. This is the full type sent over the network as a raw transaction. It -/// wraps a [TxEip4844] to include the sidecar and the ability to decode it properly. -/// -/// This is defined in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#networking) as an element -/// of a `PooledTransactions` response, and is also used as the format for sending raw transactions -/// through the network (eth_sendRawTransaction/eth_sendTransaction). -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct TxEip4844WithSidecar { - /// The actual transaction. - #[cfg_attr(feature = "serde", serde(flatten))] - pub tx: TxEip4844, - /// The sidecar. - #[cfg_attr(feature = "serde", serde(flatten))] - pub sidecar: BlobTransactionSidecar, -} - -impl TxEip4844WithSidecar { - /// Constructs a new [TxEip4844WithSidecar] from a [TxEip4844] and a [BlobTransactionSidecar]. - pub const fn from_tx_and_sidecar(tx: TxEip4844, sidecar: BlobTransactionSidecar) -> Self { - Self { tx, sidecar } - } - - /// Verifies that the transaction's blob data, commitments, and proofs are all valid. - /// - /// See also [TxEip4844::validate_blob] - #[cfg(feature = "kzg")] - pub fn validate_blob( - &self, - proof_settings: &KzgSettings, - ) -> Result<(), BlobTransactionValidationError> { - self.tx.validate_blob(&self.sidecar, proof_settings) - } - - /// Get the transaction type. - pub const fn tx_type(&self) -> OpTxType { - self.tx.tx_type() - } - - /// Get access to the inner tx [TxEip4844]. - pub const fn tx(&self) -> &TxEip4844 { - &self.tx - } - - /// Get access to the inner sidecar [BlobTransactionSidecar]. - pub const fn sidecar(&self) -> &BlobTransactionSidecar { - &self.sidecar - } - - /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844]. - pub fn into_tx(self) -> TxEip4844 { - self.tx - } - - /// Consumes the [TxEip4844WithSidecar] and returns the inner sidecar [BlobTransactionSidecar]. - pub fn into_sidecar(self) -> BlobTransactionSidecar { - self.sidecar - } - - /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844] and - /// [BlobTransactionSidecar]. - pub fn into_parts(self) -> (TxEip4844, BlobTransactionSidecar) { - (self.tx, self.sidecar) - } - - /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a - /// tx type byte or string header. - /// - /// This __does__ encode a list header and include a signature. - /// - /// This encodes the following: - /// `rlp([tx_payload, blobs, commitments, proofs])` - /// - /// where `tx_payload` is the RLP encoding of the [TxEip4844] transaction fields: - /// `rlp([chain_id, nonce, max_priority_fee_per_gas, ..., v, r, s])` - pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { - let inner_payload_length = self.tx.fields_len() + signature.rlp_vrs_len(); - let inner_header = Header { list: true, payload_length: inner_payload_length }; - - let outer_payload_length = - inner_header.length() + inner_payload_length + self.sidecar.fields_len(); - let outer_header = Header { list: true, payload_length: outer_payload_length }; - - // write the two headers - outer_header.encode(out); - inner_header.encode(out); - - // now write the fields - self.tx.encode_fields(out); - signature.write_rlp_vrs(out); - self.sidecar.encode_inner(out); - } - - /// Decodes the transaction from RLP bytes, including the signature. - /// - /// This __does not__ expect the bytes to start with a transaction type byte or string - /// header. - /// - /// This __does__ expect the bytes to start with a list header and include a signature. - /// - /// This is the inverse of [TxEip4844WithSidecar::encode_with_signature_fields]. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let header = Header::decode(buf)?; - if !header.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - - // record original length so we can check encoding - let original_len = buf.len(); - - // decode the inner tx - let inner_tx = TxEip4844::decode_signed_fields(buf)?; - - // decode the sidecar - let sidecar = BlobTransactionSidecar::decode_inner(buf)?; - - if buf.len() + header.payload_length != original_len { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - - let (tx, signature, hash) = inner_tx.into_parts(); - - // create unchecked signed tx because these checks should have happened during construction - // of `Signed` in `TxEip4844::decode_signed_fields` - Ok(Signed::new_unchecked( - TxEip4844WithSidecar::from_tx_and_sidecar(tx, sidecar), - signature, - hash, - )) - } -} - -impl SignableTransaction for TxEip4844WithSidecar { - fn set_chain_id(&mut self, chain_id: ChainId) { - self.tx.chain_id = chain_id; - } - - fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { - // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844] EIP-2718 - // payload fields: - // (BLOB_TX_TYPE || - // rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value, - // data, access_list, max_fee_per_blob_gas, blob_versioned_hashes])) - self.tx.encode_for_signing(out); - } - - fn into_signed(self, signature: Signature) -> Signed { - let mut buf = Vec::with_capacity(self.tx.encoded_len_with_signature(&signature, false)); - // The sidecar is NOT included in the signed payload, only the transaction fields and the - // type byte. Include the type byte. - // - // Include the transaction fields, making sure to __not__ use the sidecar, and __not__ - // encode a header. - self.tx.encode_with_signature(&signature, &mut buf, false); - let hash = keccak256(&buf); - - // Drop any v chain id value to ensure the signature format is correct at the time of - // combination for an EIP-4844 transaction. V should indicate the y-parity of the - // signature. - Signed::new_unchecked(self, signature.with_parity_bool(), hash) - } - - fn payload_len_for_signature(&self) -> usize { - // The payload length is the length of the `transaction_payload_body` list. - // The sidecar is NOT included. - self.tx.payload_len_for_signature() - } -} - -impl Transaction for TxEip4844WithSidecar { - fn chain_id(&self) -> Option { - self.tx.chain_id() - } - - fn gas_limit(&self) -> u128 { - self.tx.gas_limit() - } - - fn gas_price(&self) -> Option { - self.tx.gas_price() - } - - fn nonce(&self) -> u64 { - self.tx.nonce() - } - - fn to(&self) -> TxKind { - self.tx.to() - } - - fn value(&self) -> U256 { - self.tx.value() - } - - fn input(&self) -> &[u8] { - self.tx.input() - } -} - -/// This represents a set of blobs, and its corresponding commitments and proofs. -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -#[repr(C)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct BlobTransactionSidecar { - /// The blob data. - pub blobs: Vec, - /// The blob commitments. - pub commitments: Vec, - /// The blob proofs. - pub proofs: Vec, -} - -impl BlobTransactionSidecar { - /// Constructs a new [BlobTransactionSidecar] from a set of blobs, commitments, and proofs. - pub fn new(blobs: Vec, commitments: Vec, proofs: Vec) -> Self { - Self { blobs, commitments, proofs } - } - - /// Returns an iterator over the versioned hashes of the commitments. - pub fn versioned_hashes(&self) -> impl Iterator + '_ { - self.commitments.iter().map(|c| kzg_to_versioned_hash(c.as_slice())) - } - - /// Returns the versioned hash for the blob at the given index, if it - /// exists. - pub fn versioned_hash_for_blob(&self, blob_index: usize) -> Option { - self.commitments.get(blob_index).map(|c| kzg_to_versioned_hash(c.as_slice())) - } - - /// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header. - /// - /// This encodes the fields in the following order: - /// - `blobs` - /// - `commitments` - /// - `proofs` - #[inline] - pub(crate) fn encode_inner(&self, out: &mut dyn BufMut) { - BlobTransactionSidecarRlp::wrap_ref(self).encode(out); - } - - /// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header. - /// - /// This decodes the fields in the following order: - /// - `blobs` - /// - `commitments` - /// - `proofs` - pub(crate) fn decode_inner(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(BlobTransactionSidecarRlp::decode(buf)?.unwrap()) - } - - /// Outputs the RLP length of the [BlobTransactionSidecar] fields, without a RLP header. - pub fn fields_len(&self) -> usize { - BlobTransactionSidecarRlp::wrap_ref(self).fields_len() - } - - /// Calculates a size heuristic for the in-memory size of the [BlobTransactionSidecar]. - #[inline] - pub fn size(&self) -> usize { - self.blobs.len() * BYTES_PER_BLOB + // blobs - self.commitments.len() * BYTES_PER_COMMITMENT + // commitments - self.proofs.len() * BYTES_PER_PROOF // proofs - } -} - -impl Encodable for BlobTransactionSidecar { - /// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header. - fn encode(&self, s: &mut dyn BufMut) { - self.encode_inner(s); - } - - fn length(&self) -> usize { - self.fields_len() - } -} - -impl Decodable for BlobTransactionSidecar { - /// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header. - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - Self::decode_inner(buf) - } -} - -// Wrapper for c-kzg rlp -#[repr(C)] -struct BlobTransactionSidecarRlp { - blobs: Vec>, - commitments: Vec>, - proofs: Vec>, -} - -const _: [(); mem::size_of::()] = - [(); mem::size_of::()]; - -impl BlobTransactionSidecarRlp { - const fn wrap_ref(other: &BlobTransactionSidecar) -> &Self { - // SAFETY: Same repr and size - unsafe { &*(other as *const BlobTransactionSidecar).cast::() } - } - - fn unwrap(self) -> BlobTransactionSidecar { - // SAFETY: Same repr and size - unsafe { mem::transmute(self) } - } - - fn encode(&self, out: &mut dyn BufMut) { - // Encode the blobs, commitments, and proofs - self.blobs.encode(out); - self.commitments.encode(out); - self.proofs.encode(out); - } - - fn fields_len(&self) -> usize { - self.blobs.length() + self.commitments.length() + self.proofs.length() - } - - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - Ok(Self { - blobs: Decodable::decode(buf)?, - commitments: Decodable::decode(buf)?, - proofs: Decodable::decode(buf)?, - }) - } -} - -/// Calculates the versioned hash for a KzgCommitment -/// -/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension) -pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 { - debug_assert_eq!(commitment.len(), 48, "commitment length is not 48"); - let mut res = Sha256::digest(commitment); - res[0] = alloy_eips::eip4844::VERSIONED_HASH_VERSION_KZG; - B256::new(res.into()) -} - -#[cfg(test)] -mod tests { - use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar}; - use crate::OpTxEnvelope; - use alloy_consensus::SignableTransaction; - use alloy_primitives::{Signature, U256}; - use alloy_rlp::{Decodable, Encodable}; - - #[cfg(not(feature = "kzg"))] - use alloy_eips::eip4844::{Blob, Bytes48}; - - #[cfg(feature = "kzg")] - use c_kzg::{Blob, Bytes48}; - - #[test] - fn different_sidecar_same_hash() { - // this should make sure that the hash calculated for the `into_signed` conversion does not - // change if the sidecar is different - let tx = TxEip4844 { - chain_id: 1, - nonce: 1, - max_priority_fee_per_gas: 1, - max_fee_per_gas: 1, - gas_limit: 1, - to: Default::default(), - value: U256::from(1), - access_list: Default::default(), - blob_versioned_hashes: vec![Default::default()], - max_fee_per_blob_gas: 1, - input: Default::default(), - }; - let sidecar = BlobTransactionSidecar { - blobs: vec![Blob::from([2; 131072])], - commitments: vec![Bytes48::from([3; 48])], - proofs: vec![Bytes48::from([4; 48])], - }; - let mut tx = TxEip4844WithSidecar { tx, sidecar }; - let signature = Signature::test_signature(); - - // turn this transaction into_signed - let expected_signed = tx.clone().into_signed(signature); - - // change the sidecar, adding a single (blob, commitment, proof) pair - tx.sidecar = BlobTransactionSidecar { - blobs: vec![Blob::from([1; 131072])], - commitments: vec![Bytes48::from([1; 48])], - proofs: vec![Bytes48::from([1; 48])], - }; - - // turn this transaction into_signed - let actual_signed = tx.into_signed(signature); - - // the hashes should be the same - assert_eq!(expected_signed.hash(), actual_signed.hash()); - - // convert to envelopes - let expected_envelope: OpTxEnvelope = expected_signed.into(); - let actual_envelope: OpTxEnvelope = actual_signed.into(); - - // now encode the transaction and check the length - let mut buf = Vec::new(); - expected_envelope.encode(&mut buf); - assert_eq!(buf.len(), expected_envelope.length()); - - // ensure it's also the same size that `actual` claims to be, since we just changed the - // sidecar values. - assert_eq!(buf.len(), actual_envelope.length()); - - // now decode the transaction and check the values - let decoded = OpTxEnvelope::decode(&mut &buf[..]).unwrap(); - assert_eq!(decoded, expected_envelope); - } -} diff --git a/crates/op-consensus/src/transaction/eip4844/builder.rs b/crates/op-consensus/src/transaction/eip4844/builder.rs deleted file mode 100644 index 5b21aedf..00000000 --- a/crates/op-consensus/src/transaction/eip4844/builder.rs +++ /dev/null @@ -1,426 +0,0 @@ -#[cfg(not(feature = "kzg"))] -use alloy_eips::eip4844::Blob; -#[cfg(feature = "kzg")] -use c_kzg::{Blob, KzgCommitment, KzgProof}; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -use super::utils::WholeFe; -use alloy_eips::eip4844::{BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB}; -use core::cmp; - -/// A builder for creating a [`BlobTransactionSidecar`]. -/// -/// [`BlobTransactionSidecar`]: crate::BlobTransactionSidecar -#[derive(Debug, Clone)] -pub struct PartialSidecar { - /// The blobs in the sidecar. - blobs: Vec, - /// The number of field elements that we have ingested, total. - fe: usize, -} - -impl Default for PartialSidecar { - fn default() -> Self { - Self::new() - } -} - -impl PartialSidecar { - /// Create a new builder, and push an empty blob to it. This is the default - /// constructor, and allocates space for 2 blobs (256 KiB). If you want to - /// preallocate a specific number of blobs, use - /// [`PartialSidecar::with_capacity`]. - pub fn new() -> Self { - Self::with_capacity(2) - } - - /// Create a new builder, preallocating room for `capacity` blobs, and push - /// an empty blob to it. - pub fn with_capacity(capacity: usize) -> Self { - let mut blobs = Vec::with_capacity(capacity); - blobs.push(Blob::new([0u8; BYTES_PER_BLOB])); - Self { blobs, fe: 0 } - } - - /// Get a reference to the blobs currently in the builder. - pub fn blobs(&self) -> &[Blob] { - &self.blobs - } - - /// Get the number of unused field elements that have been allocated - fn free_fe(&self) -> usize { - self.blobs.len() * FIELD_ELEMENTS_PER_BLOB as usize - self.fe - } - - /// Calculate the length of used field elements IN BYTES in the builder. - /// - /// This is always strictly greater than the number of bytes that have been - /// ingested. - pub const fn len(&self) -> usize { - self.fe * 32 - } - - /// Check if the builder is empty. - pub const fn is_empty(&self) -> bool { - self.fe == 0 - } - - /// Push an empty blob to the builder. - fn push_empty_blob(&mut self) { - self.blobs.push(Blob::new([0u8; BYTES_PER_BLOB])); - } - - /// Allocate enough space for the required number of new field elements. - pub fn alloc_fes(&mut self, required_fe: usize) { - while self.free_fe() < required_fe { - self.push_empty_blob() - } - } - - /// Get the number of used field elements in the current blob. - const fn fe_in_current_blob(&self) -> usize { - self.fe % FIELD_ELEMENTS_PER_BLOB as usize - } - - /// Get the index of the first unused field element in the current blob. - const fn first_unused_fe_index_in_current_blob(&self) -> usize { - self.fe_in_current_blob() - } - - /// Get a mutable reference to the current blob. - fn current_blob_mut(&mut self) -> &mut Blob { - self.blobs.last_mut().expect("never empty") - } - - /// Get a mutable reference to the field element at the given index, in - /// the current blob. - fn fe_at_mut(&mut self, index: usize) -> &mut [u8] { - &mut self.current_blob_mut()[index * 32..(index + 1) * 32] - } - - /// Get a mutable reference to the next unused field element. - fn next_unused_fe_mut(&mut self) -> &mut [u8] { - self.fe_at_mut(self.first_unused_fe_index_in_current_blob()) - } - - /// Ingest a field element into the current blobs. - pub fn ingest_valid_fe(&mut self, data: WholeFe<'_>) { - self.alloc_fes(1); - self.next_unused_fe_mut().copy_from_slice(data.as_ref()); - self.fe += 1; - } - - /// Ingest a partial FE into the current blobs. - /// - /// # Panics - /// - /// If the data is >=32 bytes. Or if there are not enough free FEs to - /// encode the data. - pub fn ingest_partial_fe(&mut self, data: &[u8]) { - let fe = self.next_unused_fe_mut(); - fe[1..1 + data.len()].copy_from_slice(data); - self.fe += 1; - } -} - -/// A strategy for coding and decoding data into sidecars. Coder instances are -/// responsible for encoding and decoding data into and from the sidecar. They -/// are called by the [`SidecarBuilder`] during the [`ingest`], -/// [`take`], and (if `c_kzg` feature enabled) `build` methods. -/// -/// This trait allows different downstream users to use different bit-packing -/// strategies. For example, a simple coder might only use the last 31 bytes of -/// each blob, while a more complex coder might use a more sophisticated -/// strategy to pack data into the low 6 bits of the top byte. -/// -/// [`ingest`]: SidecarBuilder::ingest -/// [`take`]: SidecarBuilder::take -pub trait SidecarCoder { - /// Calculate the number of field elements required to store the given - /// data. - fn required_fe(&self, data: &[u8]) -> usize; - - /// Code a slice of data into the builder. - fn code(&mut self, builder: &mut PartialSidecar, data: &[u8]); - - /// Finish the sidecar, and commit to the data. This method should empty - /// any buffer or scratch space in the coder, and is called by - /// [`SidecarBuilder`]'s `take` and `build` methods. - fn finish(self, builder: &mut PartialSidecar); - - /// Decode all slices of data from the blobs. - fn decode_all(&mut self, blobs: &[Blob]) -> Option>>; -} - -/// Simple coder that only uses the last 31 bytes of each blob. This is the -/// default coder for the [`SidecarBuilder`]. -/// -/// # Note -/// -/// Because this coder sacrifices around 3% of total sidecar space, we do not -/// recommend its use in production. It is provided for convenience and -/// non-prod environments. -/// -/// # Behavior -/// -/// This coder encodes data as follows: -/// - The first byte of every 32-byte word is empty. -/// - Data is pre-pended with a 64-bit big-endian length prefix, which is right padded with zeros to -/// form a complete word. -/// - The rest of the data is packed into the remaining 31 bytes of each word. -/// - If the data is not a multiple of 31 bytes, the last word is right-padded with zeros. -/// -/// This means that the following regions cannot be used to store data, and are -/// considered "wasted": -/// -/// - The first byte of every 32-byte word. -/// - The right padding on the header word containing the data length. -/// - Any right padding on the last word for each piece of data. -#[derive(Debug, Copy, Clone, Default)] -#[non_exhaustive] -pub struct SimpleCoder; - -impl SimpleCoder { - /// Decode an some bytes from an iterator of valid FEs. - /// - /// Returns `Ok(Some(data))` if there is some data. - /// Returns `Ok(None)` if there is no data (length prefix is 0). - /// Returns `Err(())` if there is an error. - fn decode_one<'a>(mut fes: impl Iterator>) -> Result>, ()> { - let first = fes.next().ok_or(())?; - let mut num_bytes = u64::from_be_bytes(first.as_ref()[1..9].try_into().unwrap()) as usize; - - // if no more bytes is 0, we're done - if num_bytes == 0 { - return Ok(None); - } - - let mut res = Vec::with_capacity(num_bytes); - while num_bytes > 0 { - let to_copy = cmp::min(31, num_bytes); - let fe = fes.next().ok_or(())?; - res.extend_from_slice(&fe.as_ref()[1..1 + to_copy]); - num_bytes -= to_copy; - } - Ok(Some(res)) - } -} - -impl SidecarCoder for SimpleCoder { - fn required_fe(&self, data: &[u8]) -> usize { - data.len().div_ceil(31) + 1 - } - - fn code(&mut self, builder: &mut PartialSidecar, mut data: &[u8]) { - if data.is_empty() { - return; - } - - // first FE is the number of following bytes - builder.ingest_partial_fe(&(data.len() as u64).to_be_bytes()); - - // ingest the rest of the data - while !data.is_empty() { - let (left, right) = data.split_at(cmp::min(31, data.len())); - builder.ingest_partial_fe(left); - data = right - } - } - - /// No-op - fn finish(self, _builder: &mut PartialSidecar) {} - - fn decode_all(&mut self, blobs: &[Blob]) -> Option>> { - let mut fes = - blobs.iter().flat_map(|blob| blob.chunks(32).map(WholeFe::new)).map(Option::unwrap); - - let mut res = Vec::new(); - loop { - match Self::decode_one(&mut fes) { - Ok(Some(data)) => res.push(data), - Ok(None) => break, - Err(()) => return None, - } - } - Some(res) - } -} - -/// Build a [`BlobTransactionSidecar`] from an arbitrary amount of data. -/// -/// This is useful for creating a sidecar from a large amount of data, -/// which is then split into blobs. It delays KZG commitments and proofs -/// until all data is ready. -/// -/// [`BlobTransactionSidecar`]: crate::BlobTransactionSidecar -#[derive(Debug, Clone)] -pub struct SidecarBuilder { - /// The blob array we will code data into - inner: PartialSidecar, - /// The coder to use for ingesting and decoding data. - coder: T, -} - -impl Default for SidecarBuilder -where - T: Default + SidecarCoder, -{ - fn default() -> Self { - Self::new() - } -} - -impl SidecarBuilder { - /// Instantiate a new builder and new coder instance. - /// - /// By default, this allocates space for 2 blobs (256 KiB). If you want to - /// preallocate a specific number of blobs, use - /// [`SidecarBuilder::with_capacity`]. - pub fn new() -> Self { - Self::from_coder(T::default()) - } - - /// Create a new builder from a slice of data by calling - /// [`SidecarBuilder::from_coder_and_data`] - pub fn from_slice(data: &[u8]) -> SidecarBuilder { - Self::from_coder_and_data(T::default(), data) - } - - /// Create a new builder with a pre-allocated capacity. This capacity is - /// measured in blobs, each of which is 128 KiB. - pub fn with_capacity(capacity: usize) -> Self { - Self::from_coder_and_capacity(T::default(), capacity) - } -} - -impl SidecarBuilder { - /// Instantiate a new builder with the provided coder and capacity. This - /// capacity is measured in blobs, each of which is 128 KiB. - pub fn from_coder_and_capacity(coder: T, capacity: usize) -> Self { - Self { inner: PartialSidecar::with_capacity(capacity), coder } - } - - /// Instantiate a new builder with the provided coder. - /// - /// This is equivalent to calling - /// [`SidecarBuilder::from_coder_and_capacity`] with a capacity of 1. - /// If you want to preallocate a specific number of blobs, use - /// [`SidecarBuilder::from_coder_and_capacity`]. - pub fn from_coder(coder: T) -> Self { - Self::from_coder_and_capacity(coder, 1) - } - - /// Calculate the length of bytes used by field elements in the builder. - /// - /// This is always strictly greater than the number of bytes that have been - /// ingested. - pub const fn len(&self) -> usize { - self.inner.len() - } - - /// Check if the builder is empty. - pub const fn is_empty(&self) -> bool { - self.inner.is_empty() - } - - /// Create a new builder from a slice of data. - pub fn from_coder_and_data(coder: T, data: &[u8]) -> SidecarBuilder { - let required_fe = coder.required_fe(data); - let mut this = Self::from_coder_and_capacity( - coder, - required_fe.div_ceil(FIELD_ELEMENTS_PER_BLOB as usize), - ); - this.ingest(data); - this - } - - /// Ingest a slice of data into the builder. - pub fn ingest(&mut self, data: &[u8]) { - self.inner.alloc_fes(self.coder.required_fe(data)); - self.coder.code(&mut self.inner, data); - } - - /// Build the sidecar from the data with the provided settings. - #[cfg(feature = "kzg")] - pub fn build_with_settings( - self, - settings: &c_kzg::KzgSettings, - ) -> Result { - let mut commitments = Vec::with_capacity(self.inner.blobs.len()); - let mut proofs = Vec::with_capacity(self.inner.blobs.len()); - for blob in self.inner.blobs.iter() { - let commitment = KzgCommitment::blob_to_kzg_commitment(blob, settings)?; - let proof = KzgProof::compute_blob_kzg_proof(blob, &commitment.to_bytes(), settings)?; - commitments.push(commitment.to_bytes()); - proofs.push(proof.to_bytes()); - } - - Ok(crate::BlobTransactionSidecar { blobs: self.inner.blobs, commitments, proofs }) - } - - /// Build the sidecar from the data, with default (Ethereum Mainnet) - /// settings. - #[cfg(feature = "kzg")] - pub fn build(self) -> Result { - self.build_with_settings(crate::EnvKzgSettings::Default.get()) - } - - /// Take the blobs from the builder, without committing them to a KZG proof. - pub fn take(self) -> Vec { - self.inner.blobs - } -} - -impl FromIterator for SidecarBuilder -where - T: SidecarCoder + Default, - R: AsRef<[u8]>, -{ - fn from_iter>(iter: I) -> Self { - let mut this = Self::new(); - for data in iter { - this.ingest(data.as_ref()); - } - this - } -} - -#[cfg(test)] -mod tests { - use super::*; - use alloy_eips::eip4844::USABLE_BYTES_PER_BLOB; - - #[test] - fn ingestion_strategy() { - let mut builder = PartialSidecar::new(); - let data = &[vec![1u8; 32], vec![2u8; 372], vec![3u8; 17], vec![4u8; 5]]; - - data.iter().for_each(|data| SimpleCoder.code(&mut builder, data.as_slice())); - - let decoded = SimpleCoder.decode_all(builder.blobs()).unwrap(); - assert_eq!(decoded, data); - } - - #[test] - fn it_ingests() { - // test ingesting a lot of data. - let data = [ - vec![1u8; 32], - vec![2u8; 372], - vec![3u8; 17], - vec![4u8; 5], - vec![5u8; USABLE_BYTES_PER_BLOB + 2], - ]; - - let mut builder = data.iter().collect::>(); - - let expected_fe = data.iter().map(|d| SimpleCoder.required_fe(d)).sum::(); - assert_eq!(builder.len(), expected_fe * 32); - - // consume 2 more - builder.ingest("hello".as_bytes()); - assert_eq!(builder.len(), expected_fe * 32 + 64); - } -} diff --git a/crates/op-consensus/src/transaction/eip4844/utils.rs b/crates/op-consensus/src/transaction/eip4844/utils.rs deleted file mode 100644 index 602a581e..00000000 --- a/crates/op-consensus/src/transaction/eip4844/utils.rs +++ /dev/null @@ -1,77 +0,0 @@ -//! Utilities for working with EIP-4844 field elements and implementing -//! [`SidecarCoder`]. -//! -//! [`SidecarCoder`]: crate::SidecarCoder -use alloy_eips::eip4844::USABLE_BITS_PER_FIELD_ELEMENT; - -/// Determine whether a slice of bytes can be contained in a field element. -pub const fn fits_in_fe(data: &[u8]) -> bool { - match data.len() { - 33.. => false, - 32 => data[0] & 0b1100_0000 == 0, // first two bits must be zero - _ => true, - } -} - -/// Calculate the number of field elements required to store the given -/// number of bytes. -pub const fn minimum_fe_for_bytes(bytes: usize) -> usize { - (bytes * 8).div_ceil(USABLE_BITS_PER_FIELD_ELEMENT) -} - -/// Calculate the number of field elements required to store the given data. -pub const fn minimum_fe(data: &[u8]) -> usize { - minimum_fe_for_bytes(data.len()) -} - -/// A wrapper for a slice of bytes that is a whole, valid field element. -#[derive(Debug, Copy, Clone)] -pub struct WholeFe<'a>(&'a [u8]); - -impl<'a> WholeFe<'a> { - const fn new_unchecked(data: &'a [u8]) -> Self { - Self(data) - } - - /// Instantiate a new `WholeFe` from a slice of bytes, if it is a valid - /// field element. - pub const fn new(data: &'a [u8]) -> Option { - if data.len() == 32 && fits_in_fe(data) { - Some(Self::new_unchecked(data)) - } else { - None - } - } -} - -impl AsRef<[u8]> for WholeFe<'_> { - fn as_ref(&self) -> &[u8] { - self.0 - } -} - -#[cfg(test)] -mod test { - use alloy_eips::eip4844::{FIELD_ELEMENTS_PER_BLOB, USABLE_BYTES_PER_BLOB}; - - use super::*; - #[test] - fn calc_required_fe() { - assert_eq!(minimum_fe(&[0u8; 32]), 2); - assert_eq!(minimum_fe(&[0u8; 31]), 1); - assert_eq!(minimum_fe(&[0u8; 33]), 2); - assert_eq!(minimum_fe(&[0u8; 64]), 3); - assert_eq!(minimum_fe(&[0u8; 65]), 3); - assert_eq!(minimum_fe_for_bytes(USABLE_BYTES_PER_BLOB), FIELD_ELEMENTS_PER_BLOB as usize); - } - - #[test] - fn calc_is_valid_field_element() { - assert!(fits_in_fe(&[0u8; 32])); - assert!(!fits_in_fe(&[0u8; 33])); - - assert!(WholeFe::new(&[0u8; 32]).is_some()); - assert!(WholeFe::new(&[0u8; 33]).is_none()); - assert!(WholeFe::new(&[0u8; 31]).is_none()); - } -} diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index d9e71f5c..a06f483c 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -1,7 +1,7 @@ -use crate::{ - TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, +use crate::TxDeposit; +use alloy_consensus::{ + Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, }; -use alloy_consensus::Signed; use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; use core::mem; @@ -338,7 +338,7 @@ mod tests { // Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 // Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 fn test_decode_live_4844_tx() { - use crate::OpTransaction; + use alloy_consensus::Transaction; use alloy_primitives::{address, b256}; // https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 @@ -545,7 +545,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serde_roundtrip_eip4844() { - use crate::BlobTransactionSidecar; + use alloy_consensus::BlobTransactionSidecar; let tx = TxEip4844Variant::TxEip4844(TxEip4844 { chain_id: 1, diff --git a/crates/op-consensus/src/transaction/legacy.rs b/crates/op-consensus/src/transaction/legacy.rs deleted file mode 100644 index 9377a99e..00000000 --- a/crates/op-consensus/src/transaction/legacy.rs +++ /dev/null @@ -1,345 +0,0 @@ -use alloy_consensus::{SignableTransaction, Signed, Transaction}; -use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; -use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result}; -use core::mem; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -/// Legacy transaction. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct TxLegacy { - /// Added as EIP-155: Simple replay attack protection - #[cfg_attr( - feature = "serde", - serde( - default, - with = "alloy_serde::u64_hex_or_decimal_opt", - skip_serializing_if = "Option::is_none", - ) - )] - pub chain_id: Option, - /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] - pub nonce: u64, - /// A scalar value equal to the number of - /// Wei to be paid per unit of gas for all computation - /// costs incurred as a result of the execution of this transaction; formally Tp. - /// - /// As ethereum circulation is around 120mil eth as of 2022 that is around - /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: - /// 340282366920938463463374607431768211455 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_price: u128, - /// A scalar value equal to the maximum - /// amount of gas that should be used in executing - /// this transaction. This is paid up-front, before any - /// computation is done and may not be increased - /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub gas_limit: u128, - /// The 160-bit address of the message call’s recipient or, for a contract creation - /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. - #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] - pub to: TxKind, - /// A scalar value equal to the number of Wei to - /// be transferred to the message call’s recipient or, - /// in the case of contract creation, as an endowment - /// to the newly created account; formally Tv. - pub value: U256, - /// Input has two uses depending if transaction is Create or Call (if `to` field is None or - /// Some). pub init: An unlimited size byte array specifying the - /// EVM-code for the account initialisation procedure CREATE, - /// data: An unlimited size byte array specifying the - /// input data of the message call, formally Td. - pub input: Bytes, -} - -impl TxLegacy { - /// The EIP-2718 transaction type. - pub const TX_TYPE: isize = 0; - - /// Calculates a heuristic for the in-memory size of the [TxLegacy] transaction. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::>() + // chain_id - mem::size_of::() + // nonce - mem::size_of::() + // gas_price - mem::size_of::() + // gas_limit - self.to.size() + // to - mem::size_of::() + // value - self.input.len() // input - } - - /// Outputs the length of the transaction's fields, without a RLP header or length of the - /// eip155 fields. - pub(crate) fn fields_len(&self) -> usize { - let mut len = 0; - len += self.nonce.length(); - len += self.gas_price.length(); - len += self.gas_limit.length(); - len += self.to.length(); - len += self.value.length(); - len += self.input.0.length(); - len - } - - /// Encodes only the transaction's fields into the desired buffer, without a RLP header or - /// eip155 fields. - pub(crate) fn encode_fields(&self, out: &mut dyn BufMut) { - self.nonce.encode(out); - self.gas_price.encode(out); - self.gas_limit.encode(out); - self.to.encode(out); - self.value.encode(out); - self.input.0.encode(out); - } - - /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a - /// tx type byte or string header. - /// - /// This __does__ encode a list header and include a signature. - pub fn encode_with_signature_fields( - &self, - signature: &Signature, - out: &mut dyn alloy_rlp::BufMut, - ) { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - let header = Header { list: true, payload_length }; - header.encode(out); - self.encode_fields(out); - signature.write_rlp_vrs(out); - } - - /// Returns what the encoded length should be, if the transaction were RLP encoded with the - /// given signature. - pub(crate) fn encoded_len_with_signature(&self, signature: &Signature) -> usize { - let payload_length = self.fields_len() + signature.rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length - } - - /// Encodes EIP-155 arguments into the desired buffer. Only encodes values - /// for legacy transactions. - pub(crate) fn encode_eip155_signing_fields(&self, out: &mut dyn BufMut) { - // if this is a legacy transaction without a chain ID, it must be pre-EIP-155 - // and does not need to encode the chain ID for the signature hash encoding - if let Some(id) = self.chain_id { - // EIP-155 encodes the chain ID and two zeroes - id.encode(out); - 0x00u8.encode(out); - 0x00u8.encode(out); - } - } - - /// Outputs the length of EIP-155 fields. Only outputs a non-zero value for EIP-155 legacy - /// transactions. - pub(crate) fn eip155_fields_len(&self) -> usize { - if let Some(id) = self.chain_id { - // EIP-155 encodes the chain ID and two zeroes, so we add 2 to the length of the chain - // ID to get the length of all 3 fields - // len(chain_id) + (0x00) + (0x00) - id.length() + 2 - } else { - // this is either a pre-EIP-155 legacy transaction or a typed transaction - 0 - } - } - - /// Decodes the transaction from RLP bytes, including the signature. - /// - /// This __does not__ expect the bytes to start with a transaction type byte or string - /// header. - /// - /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { - let header = Header::decode(buf)?; - if !header.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - - // record original length so we can check encoding - let original_len = buf.len(); - - let mut tx = Self::decode_fields(buf)?; - let signature = Signature::decode_rlp_vrs(buf)?; - - // extract chain id from signature - let v = signature.v(); - tx.chain_id = v.chain_id(); - - let signed = tx.into_signed(signature); - if buf.len() + header.payload_length != original_len { - return Err(alloy_rlp::Error::ListLengthMismatch { - expected: header.payload_length, - got: original_len - buf.len(), - }); - } - - Ok(signed) - } - - /// Decode the RLP fields of the transaction, without decoding an RLP - /// header. - pub(crate) fn decode_fields(data: &mut &[u8]) -> Result { - Ok(TxLegacy { - nonce: Decodable::decode(data)?, - gas_price: Decodable::decode(data)?, - gas_limit: Decodable::decode(data)?, - to: Decodable::decode(data)?, - value: Decodable::decode(data)?, - input: Decodable::decode(data)?, - chain_id: None, - }) - } -} - -impl Transaction for TxLegacy { - fn input(&self) -> &[u8] { - &self.input - } - - fn to(&self) -> TxKind { - self.to - } - - fn value(&self) -> U256 { - self.value - } - - fn chain_id(&self) -> Option { - self.chain_id - } - - fn nonce(&self) -> u64 { - self.nonce - } - - fn gas_limit(&self) -> u128 { - self.gas_limit - } - - fn gas_price(&self) -> Option { - Some(self.gas_price) - } -} - -impl SignableTransaction for TxLegacy { - fn set_chain_id(&mut self, chain_id: ChainId) { - self.chain_id = Some(chain_id); - } - - fn encode_for_signing(&self, out: &mut dyn BufMut) { - Header { list: true, payload_length: self.fields_len() + self.eip155_fields_len() } - .encode(out); - self.encode_fields(out); - self.encode_eip155_signing_fields(out); - } - - fn payload_len_for_signature(&self) -> usize { - let payload_length = self.fields_len() + self.eip155_fields_len(); - // 'header length' + 'payload length' - Header { list: true, payload_length }.length() + payload_length - } - - fn into_signed(self, signature: Signature) -> Signed { - let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature)); - self.encode_with_signature_fields(&signature, &mut buf); - let hash = keccak256(&buf); - Signed::new_unchecked(self, signature, hash) - } -} - -impl Encodable for TxLegacy { - fn encode(&self, out: &mut dyn BufMut) { - self.encode_for_signing(out) - } - - fn length(&self) -> usize { - let payload_length = self.fields_len() + self.eip155_fields_len(); - // 'header length' + 'payload length' - length_of_length(payload_length) + payload_length - } -} - -impl Decodable for TxLegacy { - fn decode(data: &mut &[u8]) -> Result { - let header = Header::decode(data)?; - let remaining_len = data.len(); - - let transaction_payload_len = header.payload_length; - - if transaction_payload_len > remaining_len { - return Err(alloy_rlp::Error::InputTooShort); - } - - let mut transaction = Self::decode_fields(data)?; - - // If we still have data, it should be an eip-155 encoded chain_id - if !data.is_empty() { - transaction.chain_id = Some(Decodable::decode(data)?); - let _: U256 = Decodable::decode(data)?; // r - let _: U256 = Decodable::decode(data)?; // s - } - - let decoded = remaining_len - data.len(); - if decoded != transaction_payload_len { - return Err(alloy_rlp::Error::UnexpectedLength); - } - - Ok(transaction) - } -} - -#[cfg(all(test, feature = "k256"))] -mod tests { - use crate::{SignableTransaction, TxLegacy}; - use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256}; - - #[test] - fn recover_signer_legacy() { - let signer: Address = hex!("398137383b3d25c92898c656696e41950e47316b").into(); - let hash: B256 = - hex!("bb3a336e3f823ec18197f1e13ee875700f08f03e2cab75f0d0b118dabb44cba0").into(); - - let tx = TxLegacy { - chain_id: Some(1), - nonce: 0x18, - gas_price: 0xfa56ea00, - gas_limit: 119902, - to: TxKind::Call( hex!("06012c8cf97bead5deae237070f9587f8e7a266d").into()), - value: U256::from(0x1c6bf526340000u64), - input: hex!("f7d8c88300000000000000000000000000000000000000000000000000000000000cee6100000000000000000000000000000000000000000000000000000000000ac3e1").into(), - }; - - let sig = Signature::from_scalars_and_parity( - b256!("2a378831cf81d99a3f06a18ae1b6ca366817ab4d88a70053c41d7a8f0368e031"), - b256!("450d831a05b6e418724436c05c155e0a1b7b921015d0fbc2f667aed709ac4fb5"), - 37, - ) - .unwrap(); - - let signed_tx = tx.into_signed(sig); - - assert_eq!(*signed_tx.hash(), hash, "Expected same hash"); - assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass."); - } - - #[test] - // Test vector from https://github.com/alloy-rs/alloy/issues/125 - fn decode_legacy_and_recover_signer() { - let raw_tx = "f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8"; - - let tx = TxLegacy::decode_signed_fields( - &mut alloy_primitives::hex::decode(raw_tx).unwrap().as_slice(), - ) - .unwrap(); - - let recovered = tx.recover_signer().unwrap(); - let expected = address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4"); - - assert_eq!(tx.tx().chain_id, Some(1), "Expected same chain id"); - assert_eq!(expected, recovered, "Expected same signer"); - } -} diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index d2851193..49ecf808 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -1,28 +1,11 @@ #[cfg(not(feature = "std"))] use alloc::vec::Vec; -mod eip1559; -pub use eip1559::TxEip1559; - -mod eip2930; -pub use eip2930::TxEip2930; - -mod eip4844; -#[cfg(feature = "kzg")] -pub use eip4844::BlobTransactionValidationError; -pub use eip4844::{ - utils as eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder, - SimpleCoder, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, -}; - mod optimism; pub use optimism::TxDeposit; mod envelope; pub use envelope::{OpTxEnvelope, OpTxType}; -mod legacy; -pub use legacy::TxLegacy; - mod typed; pub use typed::OpTypedTransaction; diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index 9ce51d25..89307811 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -1,5 +1,5 @@ -use crate::{OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, TxLegacy}; -use alloy_consensus::Transaction; +use crate::{OpTxEnvelope, OpTxType, TxDeposit}; +use alloy_consensus::{Transaction, TxEip1559, TxEip2930, TxEip4844Variant, TxLegacy}; use alloy_primitives::TxKind; /// The TypedTransaction enum represents all Ethereum transaction request types, modified for the OP diff --git a/crates/op-consensus/testdata/rpc_blob_transaction.rlp b/crates/op-consensus/testdata/rpc_blob_transaction.rlp deleted file mode 100644 index 36232f1c..00000000 --- a/crates/op-consensus/testdata/rpc_blob_transaction.rlp +++ /dev/null @@ -1 +0,0 @@ -03fa0200fdf88f0780843b9aca008506fc23ac00830186a09400000000000000000000000000000000000001008080c001e1a0010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c44401401a0840650aa8f74d2b07f40067dc33b715078d73422f01da17abdbd11e02bbdfda9a04b2260f6022bf53eadb337b3e59514936f7317d872defb891a708ee279bdca90fa020004ba0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f1b0c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f1b0c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 From 7792ae39e0be50ed06274ae478d2d08bec4a9003 Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 13 Apr 2024 16:30:14 -0400 Subject: [PATCH 08/20] `deposit` fn in `OpTypedTransaction` --- crates/op-consensus/src/receipt/envelope.rs | 2 +- crates/op-consensus/src/receipt/mod.rs | 3 + crates/op-consensus/src/receipt/receipts.rs | 10 +- .../op-consensus/src/transaction/envelope.rs | 289 +----------------- crates/op-consensus/src/transaction/mod.rs | 3 - .../op-consensus/src/transaction/optimism.rs | 12 +- crates/op-consensus/src/transaction/typed.rs | 18 +- 7 files changed, 30 insertions(+), 307 deletions(-) diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index 97dd3e37..c684be38 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -44,7 +44,7 @@ pub enum OpReceiptEnvelope { } impl OpReceiptEnvelope { - /// Return the [`TxType`] of the inner receipt. + /// Return the [`OpTxType`] of the inner receipt. pub const fn tx_type(&self) -> OpTxType { match self { Self::Legacy(_) => OpTxType::Legacy, diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index 9067a33d..541def24 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -41,6 +41,9 @@ mod tests { use alloy_primitives::{address, b256, bytes, hex, Bytes, LogData}; use alloy_rlp::{Decodable, Encodable}; + #[cfg(not(feature = "std"))] + use alloc::{vec, vec::Vec}; + // Test vector from: https://eips.ethereum.org/EIPS/eip-2481 #[test] fn encode_legacy_receipt() { diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index 319ff70a..32503430 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -34,13 +34,13 @@ pub struct OpReceipt { } impl OpReceipt { - /// Calculates [`Log`]'s bloom filter. this is slow operation and [ReceiptWithBloom] can + /// Calculates [`Log`]'s bloom filter. this is slow operation and [OpReceiptWithBloom] can /// be used to cache this value. pub fn bloom_slow(&self) -> Bloom { self.logs.iter().collect() } - /// Calculates the bloom filter for the receipt and returns the [ReceiptWithBloom] container + /// Calculates the bloom filter for the receipt and returns the [OpReceiptWithBloom] container /// type. pub fn with_bloom(self) -> OpReceiptWithBloom { self.into() @@ -73,12 +73,12 @@ impl OpTxReceipt for OpReceipt { } } -/// [`Receipt`] with calculated bloom filter, modified for the OP Stack. +/// [`OpReceipt`] with calculated bloom filter, modified for the OP Stack. /// /// This convenience type allows us to lazily calculate the bloom filter for a /// receipt, similar to [`Sealed`]. /// -/// [`Sealed`]: crate::sealed::Sealed +/// [`Sealed`]: alloy_consensus::Sealed #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] @@ -128,7 +128,7 @@ impl From for OpReceiptWithBloom { } impl OpReceiptWithBloom { - /// Create new [ReceiptWithBloom] + /// Create new [OpReceiptWithBloom] pub const fn new(receipt: OpReceipt, bloom: Bloom) -> Self { Self { receipt, logs_bloom: bloom } } diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index a06f483c..2cb2bc15 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -143,7 +143,7 @@ impl From for OpTxEnvelope { } impl OpTxEnvelope { - /// Return the [`TxType`] of the inner txn. + /// Return the [`OpTxType`] of the inner txn. pub const fn tx_type(&self) -> OpTxType { match self { Self::Legacy(_) => OpTxType::Legacy, @@ -280,146 +280,10 @@ impl Encodable2718 for OpTxEnvelope { #[cfg(test)] mod tests { use super::*; - use alloy_consensus::SignableTransaction; - use alloy_eips::eip2930::{AccessList, AccessListItem}; - use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256}; - use std::{fs, path::PathBuf, vec}; + use alloy_primitives::{Address, Bytes, TxKind, B256, U256}; #[cfg(not(feature = "std"))] - use std::vec::Vec; - - #[test] - #[cfg(feature = "k256")] - // Test vector from https://etherscan.io/tx/0xce4dc6d7a7549a98ee3b071b67e970879ff51b5b95d1c340bacd80fa1e1aab31 - fn test_decode_live_1559_tx() { - use alloy_primitives::address; - - let raw_tx = alloy_primitives::hex::decode("02f86f0102843b9aca0085029e7822d68298f094d9e1459a7a482635700cbc20bbaf52d495ab9c9680841b55ba3ac080a0c199674fcb29f353693dd779c017823b954b3c69dffa3cd6b2a6ff7888798039a028ca912de909e7e6cdef9cdcaf24c54dd8c1032946dfa1d85c206b32a9064fe8").unwrap(); - let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - - assert_eq!(res.tx_type(), OpTxType::Eip1559); - - let tx = match res { - OpTxEnvelope::Eip1559(tx) => tx, - _ => unreachable!(), - }; - - assert_eq!(tx.tx().to, TxKind::Call(address!("D9e1459A7A482635700cBc20BBAF52D495Ab9C96"))); - let from = tx.recover_signer().unwrap(); - assert_eq!(from, address!("001e2b7dE757bA469a57bF6b23d982458a07eFcE")); - } - - #[test] - #[cfg(feature = "k256")] - // Test vector from https://etherscan.io/tx/0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4 - fn test_decode_live_legacy_tx() { - use alloy_primitives::address; - - let raw_tx = alloy_primitives::hex::decode("f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8").unwrap(); - let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - assert_eq!(res.tx_type(), OpTxType::Legacy); - - let tx = match res { - OpTxEnvelope::Legacy(tx) => tx, - _ => unreachable!(), - }; - - assert_eq!(tx.tx().to, TxKind::Call(address!("7a250d5630B4cF539739dF2C5dAcb4c659F2488D"))); - assert_eq!( - tx.hash().to_string(), - "0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4" - ); - let from = tx.recover_signer().unwrap(); - assert_eq!(from, address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4")); - } - - #[test] - #[cfg(feature = "k256")] - // Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 - // Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 - fn test_decode_live_4844_tx() { - use alloy_consensus::Transaction; - use alloy_primitives::{address, b256}; - - // https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0 - let raw_tx = alloy_primitives::hex::decode("0x03f9011d83aa36a7820fa28477359400852e90edd0008252089411e9ca82a3a762b4b5bd264d4173a242e7a770648080c08504a817c800f8a5a0012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921aa00152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4a0013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7a001148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1a0011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e654901a0c8de4cced43169f9aa3d36506363b2d2c44f6c49fc1fd91ea114c86f3757077ea01e11fdd0d1934eda0492606ee0bb80a7bf8f35cc5f86ec60fe5031ba48bfd544").unwrap(); - let res = OpTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap(); - assert_eq!(res.tx_type(), OpTxType::Eip4844); - - let tx = match res { - OpTxEnvelope::Eip4844(tx) => tx, - _ => unreachable!(), - }; - - assert_eq!( - tx.tx().to(), - TxKind::Call(address!("11E9CA82A3a762b4B5bd264d4173a242e7a77064")) - ); - - // Assert this is the correct variant of the EIP-4844 enum, which only contains the tx. - assert!(matches!(tx.tx(), TxEip4844Variant::TxEip4844(_))); - - assert_eq!( - tx.tx().tx().blob_versioned_hashes, - vec![ - b256!("012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921a"), - b256!("0152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4"), - b256!("013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7"), - b256!("01148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1"), - b256!("011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e6549") - ] - ); - - let from = tx.recover_signer().unwrap(); - assert_eq!(from, address!("A83C816D4f9b2783761a22BA6FADB0eB0606D7B2")); - } - - fn test_encode_decode_roundtrip>(tx: T) - where - Signed: Into, - { - let signature = Signature::test_signature(); - let tx_signed = tx.into_signed(signature); - let tx_envelope: OpTxEnvelope = tx_signed.into(); - let encoded = tx_envelope.encoded_2718(); - let decoded = OpTxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap(); - assert_eq!(encoded.len(), tx_envelope.encode_2718_len()); - assert_eq!(decoded, tx_envelope); - } - - #[test] - fn test_encode_decode_eip1559() { - let tx = TxEip1559 { - chain_id: 1u64, - nonce: 2, - max_fee_per_gas: 3, - max_priority_fee_per_gas: 4, - gas_limit: 5, - to: TxKind::Call(Address::left_padding_from(&[6])), - value: U256::from(7_u64), - input: Bytes::from(vec![8]), - access_list: Default::default(), - }; - test_encode_decode_roundtrip(tx); - } - - #[test] - fn test_encode_decode_eip2930() { - let tx = TxEip2930 { - chain_id: 1u64, - nonce: 2, - gas_price: 3, - gas_limit: 4, - to: TxKind::Call(Address::left_padding_from(&[5])), - value: U256::from(6_u64), - input: Bytes::from(vec![7]), - access_list: AccessList(vec![AccessListItem { - address: Address::left_padding_from(&[8]), - storage_keys: vec![B256::left_padding_from(&[9])], - }]), - }; - test_encode_decode_roundtrip(tx); - } + use alloc::vec; #[test] fn test_encode_decode_deposit() { @@ -440,153 +304,6 @@ mod tests { assert_eq!(decoded, tx_envelope); } - #[test] - fn test_encode_decode_transaction_list() { - let signature = Signature::test_signature(); - let tx = OpTxEnvelope::Eip1559( - TxEip1559 { - chain_id: 1u64, - nonce: 2, - max_fee_per_gas: 3, - max_priority_fee_per_gas: 4, - gas_limit: 5, - to: TxKind::Call(Address::left_padding_from(&[6])), - value: U256::from(7_u64), - input: Bytes::from(vec![8]), - access_list: Default::default(), - } - .into_signed(signature), - ); - let transactions = vec![tx.clone(), tx]; - let encoded = alloy_rlp::encode(&transactions); - let decoded = Vec::::decode(&mut &encoded[..]).unwrap(); - assert_eq!(transactions, decoded); - } - - #[test] - fn decode_encode_known_rpc_transaction() { - // test data pulled from hive test that sends blob transactions - let network_data_path = - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/rpc_blob_transaction.rlp"); - let data = fs::read_to_string(network_data_path).expect("Unable to read file"); - let hex_data = hex::decode(data.trim()).unwrap(); - - let tx: OpTxEnvelope = OpTxEnvelope::decode_2718(&mut hex_data.as_slice()).unwrap(); - let encoded = tx.encoded_2718(); - assert_eq!(encoded, hex_data); - assert_eq!(tx.encode_2718_len(), hex_data.len()); - } - - #[cfg(feature = "serde")] - fn test_serde_roundtrip>(tx: T) - where - Signed: Into, - { - let signature = Signature::test_signature(); - let tx_envelope: OpTxEnvelope = tx.into_signed(signature).into(); - - let serialized = serde_json::to_string(&tx_envelope).unwrap(); - let deserialized: OpTxEnvelope = serde_json::from_str(&serialized).unwrap(); - - assert_eq!(tx_envelope, deserialized); - } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_roundtrip_legacy() { - let tx = TxLegacy { - chain_id: Some(1), - nonce: 100, - gas_price: 3_000_000_000, - gas_limit: 50_000, - to: TxKind::Call(Address::default()), - value: U256::from(10e18), - input: Bytes::new(), - }; - test_serde_roundtrip(tx); - } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_roundtrip_eip1559() { - let tx = TxEip1559 { - chain_id: 1, - nonce: 100, - max_fee_per_gas: 50_000_000_000, - max_priority_fee_per_gas: 1_000_000_000_000, - gas_limit: 1_000_000, - to: TxKind::Create, - value: U256::from(10e18), - input: Bytes::new(), - access_list: AccessList(vec![AccessListItem { - address: Address::random(), - storage_keys: vec![B256::random()], - }]), - }; - test_serde_roundtrip(tx); - } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_roundtrip_eip2930() { - let tx = TxEip2930 { - chain_id: u64::MAX, - nonce: u64::MAX, - gas_price: u128::MAX, - gas_limit: u128::MAX, - to: TxKind::Call(Address::random()), - value: U256::MAX, - input: Bytes::new(), - access_list: Default::default(), - }; - test_serde_roundtrip(tx); - } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_roundtrip_eip4844() { - use alloy_consensus::BlobTransactionSidecar; - - let tx = TxEip4844Variant::TxEip4844(TxEip4844 { - chain_id: 1, - nonce: 100, - max_fee_per_gas: 50_000_000_000, - max_priority_fee_per_gas: 1_000_000_000_000, - gas_limit: 1_000_000, - to: Address::random(), - value: U256::from(10e18), - input: Bytes::new(), - access_list: AccessList(vec![AccessListItem { - address: Address::random(), - storage_keys: vec![B256::random()], - }]), - blob_versioned_hashes: vec![B256::random()], - max_fee_per_blob_gas: 0, - }); - test_serde_roundtrip(tx); - - let tx = TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar { - tx: TxEip4844 { - chain_id: 1, - nonce: 100, - max_fee_per_gas: 50_000_000_000, - max_priority_fee_per_gas: 1_000_000_000_000, - gas_limit: 1_000_000, - to: Address::random(), - value: U256::from(10e18), - input: Bytes::new(), - access_list: AccessList(vec![AccessListItem { - address: Address::random(), - storage_keys: vec![B256::random()], - }]), - blob_versioned_hashes: vec![B256::random()], - max_fee_per_blob_gas: 0, - }, - sidecar: BlobTransactionSidecar { ..Default::default() }, - }); - test_serde_roundtrip(tx); - } - #[test] #[cfg(feature = "serde")] fn test_serde_roundtrip_deposit() { diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index 49ecf808..9e4cb8f7 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -1,6 +1,3 @@ -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - mod optimism; pub use optimism::TxDeposit; diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs index 3b7eb305..36ef9a95 100644 --- a/crates/op-consensus/src/transaction/optimism.rs +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -5,9 +5,6 @@ use alloy_rlp::{ }; use core::mem; -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - /// Deposit transactions, also known as deposits are initiated on L1, and executed on L2. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -22,14 +19,7 @@ pub struct TxDeposit { #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] pub to: TxKind, /// The ETH value to mint on L2. - #[cfg_attr( - feature = "serde", - serde( - default, - skip_serializing_if = "Option::is_none", - with = "alloy_serde::u128_hex_or_decimal_opt" - ) - )] + #[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::u128_hex_or_decimal_opt"))] pub mint: Option, /// The ETH value to send to the recipient account. pub value: U256, diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index 89307811..61e6657a 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -75,7 +75,7 @@ impl From for OpTypedTransaction { } impl OpTypedTransaction { - /// Return the [`TxType`] of the inner txn. + /// Return the [`OpTxType`] of the inner txn. pub const fn tx_type(&self) -> OpTxType { match self { Self::Legacy(_) => OpTxType::Legacy, @@ -109,6 +109,22 @@ impl OpTypedTransaction { _ => None, } } + + /// Return the inner EIP-4844 transaction if it exists. + pub const fn eip4844(&self) -> Option<&TxEip4844Variant> { + match self { + Self::Eip4844(tx) => Some(tx), + _ => None, + } + } + + /// Return the inner deposit transaction if it exists. + pub const fn deposit(&self) -> Option<&TxDeposit> { + match self { + Self::Deposit(tx) => Some(tx), + _ => None, + } + } } impl Transaction for OpTypedTransaction { From bab562f15b038701fa809189dc619f36df40e9bb Mon Sep 17 00:00:00 2001 From: clabby Date: Tue, 16 Apr 2024 10:21:56 -0400 Subject: [PATCH 09/20] Use upstream alloy --- deny.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/deny.toml b/deny.toml index 63556efa..59f9a5fb 100644 --- a/deny.toml +++ b/deny.toml @@ -52,6 +52,5 @@ license-files = [{ path = "LICENSE", hash = 0x001c7e6c }] [sources] unknown-registry = "deny" unknown-git = "deny" -allow-git = [ - "https://github.com/alloy-rs/alloy", -] +# TODO: Remove `alloy-rs/core` once alloy-contract is stable. This is only used in tests for `sol!`. +allow-git = ["https://github.com/alloy-rs/core", "https://github.com/alloy-rs/alloy"] From b7def111302ee2fd40693cc23379bec797a22544 Mon Sep 17 00:00:00 2001 From: clabby Date: Tue, 16 Apr 2024 10:54:58 -0400 Subject: [PATCH 10/20] Inherit `TxReceipt` trait --- crates/op-consensus/src/receipt/mod.rs | 25 +++------------------ crates/op-consensus/src/receipt/receipts.rs | 9 ++++++-- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index 541def24..34e0eddb 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -1,4 +1,4 @@ -use alloy_primitives::{Bloom, Log}; +use alloy_consensus::TxReceipt; mod envelope; pub use envelope::OpReceiptEnvelope; @@ -7,26 +7,7 @@ mod receipts; pub use receipts::{OpReceipt, OpReceiptWithBloom}; /// Receipt is the result of a transaction execution. -pub trait OpTxReceipt { - /// Returns true if the transaction was successful. - fn success(&self) -> bool; - - /// Returns the bloom filter for the logs in the receipt. This operation - /// may be expensive. - fn bloom(&self) -> Bloom; - - /// Returns the bloom filter for the logs in the receipt, if it is cheap to - /// compute. - fn bloom_cheap(&self) -> Option { - None - } - - /// Returns the cumulative gas used in the block after this transaction was executed. - fn cumulative_gas_used(&self) -> u128; - - /// Returns the logs emitted by this transaction. - fn logs(&self) -> &[Log]; - +pub trait OpTxReceipt: TxReceipt { /// Returns the deposit nonce of the transaction. fn deposit_nonce(&self) -> Option; @@ -38,7 +19,7 @@ pub trait OpTxReceipt { mod tests { use super::*; use alloy_eips::eip2718::Encodable2718; - use alloy_primitives::{address, b256, bytes, hex, Bytes, LogData}; + use alloy_primitives::{address, b256, bytes, hex, Bytes, Log, LogData}; use alloy_rlp::{Decodable, Encodable}; #[cfg(not(feature = "std"))] diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index 32503430..d6916fc4 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -1,4 +1,5 @@ use super::OpTxReceipt; +use alloy_consensus::TxReceipt; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -47,7 +48,7 @@ impl OpReceipt { } } -impl OpTxReceipt for OpReceipt { +impl TxReceipt for OpReceipt { fn success(&self) -> bool { self.status } @@ -63,7 +64,9 @@ impl OpTxReceipt for OpReceipt { fn logs(&self) -> &[Log] { &self.logs } +} +impl OpTxReceipt for OpReceipt { fn deposit_nonce(&self) -> Option { self.deposit_nonce } @@ -90,7 +93,7 @@ pub struct OpReceiptWithBloom { pub logs_bloom: Bloom, } -impl OpTxReceipt for OpReceiptWithBloom { +impl TxReceipt for OpReceiptWithBloom { fn success(&self) -> bool { self.receipt.status } @@ -110,7 +113,9 @@ impl OpTxReceipt for OpReceiptWithBloom { fn logs(&self) -> &[Log] { &self.receipt.logs } +} +impl OpTxReceipt for OpReceiptWithBloom { fn deposit_nonce(&self) -> Option { self.receipt.deposit_nonce } From c128b34693344af656d95a630bc6b0c4b213f1cf Mon Sep 17 00:00:00 2001 From: refcell Date: Mon, 20 May 2024 14:12:40 -0400 Subject: [PATCH 11/20] fix: receipt trait --- crates/op-consensus/src/receipt/receipts.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index d6916fc4..5bfe3d59 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -49,10 +49,6 @@ impl OpReceipt { } impl TxReceipt for OpReceipt { - fn success(&self) -> bool { - self.status - } - fn bloom(&self) -> Bloom { self.bloom_slow() } @@ -64,6 +60,10 @@ impl TxReceipt for OpReceipt { fn logs(&self) -> &[Log] { &self.logs } + + fn status(&self) -> bool { + self.status + } } impl OpTxReceipt for OpReceipt { @@ -94,7 +94,7 @@ pub struct OpReceiptWithBloom { } impl TxReceipt for OpReceiptWithBloom { - fn success(&self) -> bool { + fn status(&self) -> bool { self.receipt.status } From c473395576f5e44cde32aa3535caebdba146f46a Mon Sep 17 00:00:00 2001 From: refcell Date: Mon, 20 May 2024 14:27:45 -0400 Subject: [PATCH 12/20] fix: receipt type name and flattening --- crates/op-consensus/src/lib.rs | 2 +- crates/op-consensus/src/receipt/envelope.rs | 10 +- crates/op-consensus/src/receipt/mod.rs | 114 ++++++++++---------- crates/op-consensus/src/receipt/receipts.rs | 86 +++++++-------- 4 files changed, 103 insertions(+), 109 deletions(-) diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 636330d9..6a229ed8 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -20,7 +20,7 @@ extern crate alloc; mod receipt; -pub use receipt::{OpReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; +pub use receipt::{OpDepositReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; mod transaction; pub use transaction::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit}; diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index c684be38..bcfbd2ba 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -1,4 +1,4 @@ -use crate::{OpReceipt, OpReceiptWithBloom, OpTxType}; +use crate::{OpDepositReceipt, OpReceiptWithBloom, OpTxType}; use alloy_eips::eip2718::{Decodable2718, Encodable2718}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -62,17 +62,17 @@ impl OpReceiptEnvelope { /// Returns the success status of the receipt's transaction. pub fn status(&self) -> bool { - self.as_receipt().unwrap().status + self.as_receipt().unwrap().inner.status } /// Returns the cumulative gas used at this receipt. pub fn cumulative_gas_used(&self) -> u128 { - self.as_receipt().unwrap().cumulative_gas_used + self.as_receipt().unwrap().inner.cumulative_gas_used } /// Return the receipt logs. pub fn logs(&self) -> &[T] { - &self.as_receipt().unwrap().logs + &self.as_receipt().unwrap().inner.logs } /// Return the receipt's bloom. @@ -104,7 +104,7 @@ impl OpReceiptEnvelope { /// Return the inner receipt. Currently this is infallible, however, future /// receipt types may be added. - pub const fn as_receipt(&self) -> Option<&OpReceipt> { + pub const fn as_receipt(&self) -> Option<&OpDepositReceipt> { match self { Self::Legacy(t) | Self::Eip2930(t) diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index 34e0eddb..040aa668 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -4,7 +4,7 @@ mod envelope; pub use envelope::OpReceiptEnvelope; mod receipts; -pub use receipts::{OpReceipt, OpReceiptWithBloom}; +pub use receipts::{OpDepositReceipt, OpReceiptWithBloom}; /// Receipt is the result of a transaction execution. pub trait OpTxReceipt: TxReceipt { @@ -18,6 +18,7 @@ pub trait OpTxReceipt: TxReceipt { #[cfg(test)] mod tests { use super::*; + use alloy_consensus::Receipt; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{address, b256, bytes, hex, Bytes, Log, LogData}; use alloy_rlp::{Decodable, Encodable}; @@ -31,26 +32,27 @@ mod tests { let expected = hex!("f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"); let mut data = vec![]; - let receipt = - OpReceiptEnvelope::Legacy(OpReceiptWithBloom { - receipt: OpReceipt { + let receipt = OpReceiptEnvelope::Legacy(OpReceiptWithBloom { + receipt: OpDepositReceipt { + inner: Receipt { + status: false, cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), data: LogData::new_unchecked( vec![ - b256!("000000000000000000000000000000000000000000000000000000000000dead"), - b256!("000000000000000000000000000000000000000000000000000000000000beef"), - ], + b256!("000000000000000000000000000000000000000000000000000000000000dead"), + b256!("000000000000000000000000000000000000000000000000000000000000beef"), + ], bytes!("0100ff"), ), }], - status: false, - deposit_nonce: None, - deposit_receipt_version: None, }, - logs_bloom: [0; 256].into(), - }); + deposit_nonce: None, + deposit_receipt_version: None, + }, + logs_bloom: [0; 256].into(), + }); receipt.network_encode(&mut data); @@ -67,19 +69,21 @@ mod tests { // EIP658Receipt let expected = OpReceiptWithBloom { - receipt: OpReceipt { - cumulative_gas_used: 0x1u128, - logs: vec![Log { - address: address!("0000000000000000000000000000000000000011"), - data: LogData::new_unchecked( - vec![ - b256!("000000000000000000000000000000000000000000000000000000000000dead"), - b256!("000000000000000000000000000000000000000000000000000000000000beef"), - ], - bytes!("0100ff"), - ), - }], - status: false, + receipt: OpDepositReceipt { + inner: Receipt { + status: false, + cumulative_gas_used: 0x1u128, + logs: vec![Log { + address: address!("0000000000000000000000000000000000000011"), + data: LogData::new_unchecked( + vec![ + b256!("000000000000000000000000000000000000000000000000000000000000dead"), + b256!("000000000000000000000000000000000000000000000000000000000000beef"), + ], + bytes!("0100ff"), + ), + }], + }, deposit_nonce: None, deposit_receipt_version: None, }, @@ -92,29 +96,31 @@ mod tests { #[test] fn gigantic_receipt() { - let receipt = OpReceipt { - cumulative_gas_used: 16747627, - status: true, - logs: vec![ - Log { - address: address!("4bf56695415f725e43c3e04354b604bcfb6dfb6e"), - data: LogData::new_unchecked( - vec![b256!( - "c69dc3d7ebff79e41f525be431d5cd3cc08f80eaf0f7819054a726eeb7086eb9" - )], - Bytes::from(vec![1; 0xffffff]), - ), - }, - Log { - address: address!("faca325c86bf9c2d5b413cd7b90b209be92229c2"), - data: LogData::new_unchecked( - vec![b256!( - "8cca58667b1e9ffa004720ac99a3d61a138181963b294d270d91c53d36402ae2" - )], - Bytes::from(vec![1; 0xffffff]), - ), - }, - ], + let receipt = OpDepositReceipt { + inner: Receipt { + cumulative_gas_used: 16747627, + status: true, + logs: vec![ + Log { + address: address!("4bf56695415f725e43c3e04354b604bcfb6dfb6e"), + data: LogData::new_unchecked( + vec![b256!( + "c69dc3d7ebff79e41f525be431d5cd3cc08f80eaf0f7819054a726eeb7086eb9" + )], + Bytes::from(vec![1; 0xffffff]), + ), + }, + Log { + address: address!("faca325c86bf9c2d5b413cd7b90b209be92229c2"), + data: LogData::new_unchecked( + vec![b256!( + "8cca58667b1e9ffa004720ac99a3d61a138181963b294d270d91c53d36402ae2" + )], + Bytes::from(vec![1; 0xffffff]), + ), + }, + ], + }, deposit_nonce: None, deposit_receipt_version: None, } @@ -136,10 +142,8 @@ mod tests { // Deposit Receipt (post-regolith) let expected = OpReceiptWithBloom { - receipt: OpReceipt { - cumulative_gas_used: 46913, - logs: vec![], - status: true, + receipt: OpDepositReceipt { + inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true }, deposit_nonce: Some(4012991), deposit_receipt_version: None, }, @@ -160,10 +164,8 @@ mod tests { // Deposit Receipt (post-regolith) let expected = OpReceiptWithBloom { - receipt: OpReceipt { - cumulative_gas_used: 46913, - logs: vec![], - status: true, + receipt: OpDepositReceipt { + inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true }, deposit_nonce: Some(4012991), deposit_receipt_version: Some(1), }, diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index 5bfe3d59..41225640 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -1,5 +1,5 @@ use super::OpTxReceipt; -use alloy_consensus::TxReceipt; +use alloy_consensus::{Receipt, TxReceipt}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -10,23 +10,15 @@ use alloc::vec::Vec; #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct OpReceipt { - /// If transaction is executed successfully. - /// - /// This is the `statusCode` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity_bool"))] - pub status: bool, - /// Gas used - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] - pub cumulative_gas_used: u128, - /// Log send from contracts. - pub logs: Vec, +pub struct OpDepositReceipt { + /// The inner receipt type. + #[cfg_attr(feature = "serde", serde(flatten))] + pub inner: Receipt, /// Deposit nonce for Optimism deposit transactions #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] pub deposit_nonce: Option, /// Deposit receipt version for Optimism deposit transactions /// - /// /// The deposit receipt version was introduced in Canyon to indicate an update to how /// receipt hashes should be computed when set. The state transition process /// ensures this is only set for post-Canyon deposit transactions. @@ -34,11 +26,11 @@ pub struct OpReceipt { pub deposit_receipt_version: Option, } -impl OpReceipt { +impl OpDepositReceipt { /// Calculates [`Log`]'s bloom filter. this is slow operation and [OpReceiptWithBloom] can /// be used to cache this value. pub fn bloom_slow(&self) -> Bloom { - self.logs.iter().collect() + self.inner.logs.iter().collect() } /// Calculates the bloom filter for the receipt and returns the [OpReceiptWithBloom] container @@ -48,25 +40,25 @@ impl OpReceipt { } } -impl TxReceipt for OpReceipt { +impl TxReceipt for OpDepositReceipt { fn bloom(&self) -> Bloom { self.bloom_slow() } fn cumulative_gas_used(&self) -> u128 { - self.cumulative_gas_used + self.inner.cumulative_gas_used } fn logs(&self) -> &[Log] { - &self.logs + &self.inner.logs } fn status(&self) -> bool { - self.status + self.inner.status } } -impl OpTxReceipt for OpReceipt { +impl OpTxReceipt for OpDepositReceipt { fn deposit_nonce(&self) -> Option { self.deposit_nonce } @@ -88,14 +80,14 @@ impl OpTxReceipt for OpReceipt { pub struct OpReceiptWithBloom { #[cfg_attr(feature = "serde", serde(flatten))] /// The receipt. - pub receipt: OpReceipt, + pub receipt: OpDepositReceipt, /// The bloom filter. pub logs_bloom: Bloom, } impl TxReceipt for OpReceiptWithBloom { fn status(&self) -> bool { - self.receipt.status + self.receipt.inner.status } fn bloom(&self) -> Bloom { @@ -107,11 +99,11 @@ impl TxReceipt for OpReceiptWithBloom { } fn cumulative_gas_used(&self) -> u128 { - self.receipt.cumulative_gas_used + self.receipt.inner.cumulative_gas_used } fn logs(&self) -> &[Log] { - &self.receipt.logs + &self.receipt.inner.logs } } @@ -125,8 +117,8 @@ impl OpTxReceipt for OpReceiptWithBloom { } } -impl From for OpReceiptWithBloom { - fn from(receipt: OpReceipt) -> Self { +impl From for OpReceiptWithBloom { + fn from(receipt: OpDepositReceipt) -> Self { let bloom = receipt.bloom_slow(); OpReceiptWithBloom { receipt, logs_bloom: bloom } } @@ -134,27 +126,27 @@ impl From for OpReceiptWithBloom { impl OpReceiptWithBloom { /// Create new [OpReceiptWithBloom] - pub const fn new(receipt: OpReceipt, bloom: Bloom) -> Self { + pub const fn new(receipt: OpDepositReceipt, bloom: Bloom) -> Self { Self { receipt, logs_bloom: bloom } } /// Consume the structure, returning only the receipt #[allow(clippy::missing_const_for_fn)] // false positive - pub fn into_receipt(self) -> OpReceipt { + pub fn into_receipt(self) -> OpDepositReceipt { self.receipt } /// Consume the structure, returning the receipt and the bloom filter #[allow(clippy::missing_const_for_fn)] // false positive - pub fn into_components(self) -> (OpReceipt, Bloom) { + pub fn into_components(self) -> (OpDepositReceipt, Bloom) { (self.receipt, self.logs_bloom) } fn payload_len(&self) -> usize { - self.receipt.status.length() - + self.receipt.cumulative_gas_used.length() + self.receipt.inner.status.length() + + self.receipt.inner.cumulative_gas_used.length() + self.logs_bloom.length() - + self.receipt.logs.length() + + self.receipt.inner.logs.length() + self.receipt.deposit_nonce.map_or(0, |nonce| nonce.length()) + self.receipt.deposit_receipt_version.map_or(0, |version| version.length()) } @@ -167,10 +159,10 @@ impl OpReceiptWithBloom { /// Encodes the receipt data. fn encode_fields(&self, out: &mut dyn BufMut) { self.receipt_rlp_header().encode(out); - self.receipt.status.encode(out); - self.receipt.cumulative_gas_used.encode(out); + self.receipt.inner.status.encode(out); + self.receipt.inner.cumulative_gas_used.encode(out); self.logs_bloom.encode(out); - self.receipt.logs.encode(out); + self.receipt.inner.logs.encode(out); if let Some(nonce) = self.receipt.deposit_nonce { nonce.encode(out); } @@ -198,10 +190,8 @@ impl OpReceiptWithBloom { let deposit_receipt_version = remaining(b).then(|| alloy_rlp::Decodable::decode(b)).transpose()?; - let receipt = OpReceipt { - status: success, - cumulative_gas_used, - logs, + let receipt = OpDepositReceipt { + inner: Receipt { status: success, cumulative_gas_used, logs }, deposit_nonce, deposit_receipt_version, }; @@ -225,10 +215,10 @@ impl alloy_rlp::Encodable for OpReceiptWithBloom { } fn length(&self) -> usize { - let payload_length = self.receipt.status.length() - + self.receipt.cumulative_gas_used.length() + let payload_length = self.receipt.inner.status.length() + + self.receipt.inner.cumulative_gas_used.length() + self.logs_bloom.length() - + self.receipt.logs.length() + + self.receipt.inner.logs.length() + self.receipt.deposit_nonce.map_or(0, |nonce| nonce.length()) + self.receipt.deposit_receipt_version.map_or(0, |version| version.length()); payload_length + length_of_length(payload_length) @@ -242,7 +232,7 @@ impl alloy_rlp::Decodable for OpReceiptWithBloom { } #[cfg(all(test, feature = "arbitrary"))] -impl<'a, T> arbitrary::Arbitrary<'a> for OpReceipt +impl<'a, T> arbitrary::Arbitrary<'a> for OpDepositReceipt where T: arbitrary::Arbitrary<'a>, { @@ -251,9 +241,11 @@ where let deposit_receipt_version = deposit_nonce.is_some().then(|| u64::arbitrary(u)).transpose()?; Ok(Self { - status: bool::arbitrary(u)?, - cumulative_gas_used: u128::arbitrary(u)?, - logs: Vec::::arbitrary(u)?, + inner: Receipt { + status: bool::arbitrary(u)?, + cumulative_gas_used: u128::arbitrary(u)?, + logs: Vec::::arbitrary(u)?, + }, deposit_nonce, deposit_receipt_version, }) @@ -266,6 +258,6 @@ where T: arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Ok(Self { receipt: OpReceipt::::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? }) + Ok(Self { receipt: OpDepositReceipt::::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? }) } } From 14be0a4ddb96fc9bc9a96d3cafae975c3018f681 Mon Sep 17 00:00:00 2001 From: refcell Date: Mon, 20 May 2024 14:29:44 -0400 Subject: [PATCH 13/20] fix: doc comments --- crates/op-consensus/src/receipt/receipts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index 41225640..d64d59cf 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -68,7 +68,7 @@ impl OpTxReceipt for OpDepositReceipt { } } -/// [`OpReceipt`] with calculated bloom filter, modified for the OP Stack. +/// [`OpDepositReceipt`] with calculated bloom filter, modified for the OP Stack. /// /// This convenience type allows us to lazily calculate the bloom filter for a /// receipt, similar to [`Sealed`]. From 7092d3147ec1ec5dc59443eb37280f73d902e060 Mon Sep 17 00:00:00 2001 From: refcell Date: Mon, 20 May 2024 14:41:52 -0400 Subject: [PATCH 14/20] fix: u128 conversion --- crates/op-consensus/src/transaction/optimism.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs index 36ef9a95..fd9563a4 100644 --- a/crates/op-consensus/src/transaction/optimism.rs +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -19,12 +19,12 @@ pub struct TxDeposit { #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))] pub to: TxKind, /// The ETH value to mint on L2. - #[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::u128_hex_or_decimal_opt"))] + #[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::u128_opt_via_ruint"))] pub mint: Option, /// The ETH value to send to the recipient account. pub value: U256, /// The gas limit for the L2 transaction. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_limit: u128, /// Field indicating if this transaction is exempt from the L2 gas limit. pub is_system_transaction: bool, From f198fc525b071dcb7fbd3d439ccabca925878442 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 13:43:01 +0200 Subject: [PATCH 15/20] cleanup tx type --- Cargo.toml | 17 ++++++++++------- crates/rpc-types/Cargo.toml | 4 +--- crates/rpc-types/src/lib.rs | 1 + crates/rpc-types/src/transaction/mod.rs | 2 +- crates/rpc-types/src/transaction/tx_type.rs | 21 ++++----------------- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0569b718..fe695e2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,15 +14,18 @@ exclude = ["benches/", "tests/"] [workspace.dependencies] # Alloy -alloy = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } +alloy-op-rpc-types = { version = "0.1.0", path = "crates/rpc-types" } + alloy-rlp = { version = "0.3", default-features = false } alloy-primitives = { version = "0.7.1", default-features = false } -alloy-op-rpc-types = { version = "0.1.0", path = "crates/rpc-types" } -alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c", default-features = false} -alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "55a278c" } -alloy-eips = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } -alloy-serde = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } -alloy-signer = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false } + +alloy = { git = "https://github.com/alloy-rs/alloy" } +alloy-consensus = { git = "https://github.com/alloy-rs/alloy", default-features = false} +alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" } +alloy-rpc-types-eth = { git = "https://github.com/alloy-rs/alloy" } +alloy-eips = { git = "https://github.com/alloy-rs/alloy", default-features = false } +alloy-serde = { git = "https://github.com/alloy-rs/alloy", default-features = false } +alloy-signer = { git = "https://github.com/alloy-rs/alloy", default-features = false } # Serde serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } diff --git a/crates/rpc-types/Cargo.toml b/crates/rpc-types/Cargo.toml index 632666c7..cc3a3bd5 100644 --- a/crates/rpc-types/Cargo.toml +++ b/crates/rpc-types/Cargo.toml @@ -13,11 +13,9 @@ exclude.workspace = true [dependencies] alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] } -alloy-consensus = { workspace = true, features = ["std", "serde"] } -alloy-rpc-types.workspace = true +alloy-rpc-types-eth.workspace = true serde = { workspace = true, features = ["derive"] } -serde_json.workspace = true # arbitrary arbitrary = { version = "1.3", features = ["derive"], optional = true } diff --git a/crates/rpc-types/src/lib.rs b/crates/rpc-types/src/lib.rs index 2ee5bc67..17cf524e 100644 --- a/crates/rpc-types/src/lib.rs +++ b/crates/rpc-types/src/lib.rs @@ -13,5 +13,6 @@ )] #![deny(unused_must_use, rust_2018_idioms)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(test), warn(unused_crate_dependencies))] pub mod transaction; diff --git a/crates/rpc-types/src/transaction/mod.rs b/crates/rpc-types/src/transaction/mod.rs index d0b54253..22465f09 100644 --- a/crates/rpc-types/src/transaction/mod.rs +++ b/crates/rpc-types/src/transaction/mod.rs @@ -14,7 +14,7 @@ pub mod tx_type; pub struct Transaction { /// Ethereum Transaction Types #[serde(flatten)] - pub inner: alloy_rpc_types::Transaction, + pub inner: alloy_rpc_types_eth::Transaction, /// The ETH value to mint on L2 #[serde(default, skip_serializing_if = "Option::is_none")] pub mint: Option, diff --git a/crates/rpc-types/src/transaction/tx_type.rs b/crates/rpc-types/src/transaction/tx_type.rs index 5c26fd93..cb99ecf3 100644 --- a/crates/rpc-types/src/transaction/tx_type.rs +++ b/crates/rpc-types/src/transaction/tx_type.rs @@ -2,19 +2,6 @@ use serde::{Deserialize, Serialize}; -/// Identifier for legacy transaction, however a legacy tx is technically not -/// typed. -pub const LEGACY_TX_TYPE_ID: u8 = 0; - -/// Identifier for an EIP2930 transaction. -pub const EIP2930_TX_TYPE_ID: u8 = 1; - -/// Identifier for an EIP1559 transaction. -pub const EIP1559_TX_TYPE_ID: u8 = 2; - -/// Identifier for an EIP4844 transaction. -pub const EIP4844_TX_TYPE_ID: u8 = 3; - /// Identifier for an Optimism deposit transaction pub const DEPOSIT_TX_TYPE_ID: u8 = 126; @@ -39,10 +26,10 @@ pub enum TxType { impl From for u8 { fn from(value: TxType) -> Self { match value { - TxType::Legacy => LEGACY_TX_TYPE_ID, - TxType::Eip2930 => EIP2930_TX_TYPE_ID, - TxType::Eip1559 => EIP1559_TX_TYPE_ID, - TxType::Eip4844 => EIP4844_TX_TYPE_ID, + TxType::Legacy => 0, + TxType::Eip2930 => 1, + TxType::Eip1559 => 2, + TxType::Eip4844 => 3, TxType::Deposit => DEPOSIT_TX_TYPE_ID, } } From a4eb59252133054bb52a6103d69d2333463b234f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 13:45:24 +0200 Subject: [PATCH 16/20] cleanup tx type --- crates/rpc-types/Cargo.toml | 2 +- crates/rpc-types/src/transaction/mod.rs | 4 --- crates/rpc-types/src/transaction/tx_type.rs | 36 --------------------- 3 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 crates/rpc-types/src/transaction/tx_type.rs diff --git a/crates/rpc-types/Cargo.toml b/crates/rpc-types/Cargo.toml index cc3a3bd5..fb8b8b21 100644 --- a/crates/rpc-types/Cargo.toml +++ b/crates/rpc-types/Cargo.toml @@ -18,7 +18,7 @@ alloy-rpc-types-eth.workspace = true serde = { workspace = true, features = ["derive"] } # arbitrary -arbitrary = { version = "1.3", features = ["derive"], optional = true } +arbitrary = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] alloy-primitives = { workspace = true, features = ["arbitrary"] } diff --git a/crates/rpc-types/src/transaction/mod.rs b/crates/rpc-types/src/transaction/mod.rs index 22465f09..c378b72c 100644 --- a/crates/rpc-types/src/transaction/mod.rs +++ b/crates/rpc-types/src/transaction/mod.rs @@ -3,10 +3,6 @@ use alloy_primitives::B256; use serde::{Deserialize, Serialize}; -pub use self::tx_type::TxType; - -pub mod tx_type; - /// OP Transaction type #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] diff --git a/crates/rpc-types/src/transaction/tx_type.rs b/crates/rpc-types/src/transaction/tx_type.rs deleted file mode 100644 index cb99ecf3..00000000 --- a/crates/rpc-types/src/transaction/tx_type.rs +++ /dev/null @@ -1,36 +0,0 @@ -//! OP transaction identifiers. - -use serde::{Deserialize, Serialize}; - -/// Identifier for an Optimism deposit transaction -pub const DEPOSIT_TX_TYPE_ID: u8 = 126; - -/// Transaction Type -#[derive( - Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize, Hash, -)] -pub enum TxType { - /// Legacy transaction pre EIP-2929 - #[default] - Legacy = 0_isize, - /// AccessList transaction - Eip2930 = 1_isize, - /// Transaction with Priority fee - Eip1559 = 2_isize, - /// Shard Blob Transactions - EIP-4844 - Eip4844 = 3_isize, - /// Optimism Deposit transaction. - Deposit = 126_isize, -} - -impl From for u8 { - fn from(value: TxType) -> Self { - match value { - TxType::Legacy => 0, - TxType::Eip2930 => 1, - TxType::Eip1559 => 2, - TxType::Eip4844 => 3, - TxType::Deposit => DEPOSIT_TX_TYPE_ID, - } - } -} From 1a7519c769aa7b203af869cad59d3a50d65d38c7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 13:54:58 +0200 Subject: [PATCH 17/20] make it compile --- crates/op-consensus/src/lib.rs | 2 +- crates/op-consensus/src/receipt/envelope.rs | 13 +++--- crates/op-consensus/src/receipt/receipts.rs | 20 ++++++--- .../op-consensus/src/transaction/envelope.rs | 41 ++++++++++++------- crates/op-consensus/src/transaction/mod.rs | 2 +- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 6a229ed8..4288f8be 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -23,4 +23,4 @@ mod receipt; pub use receipt::{OpDepositReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; mod transaction; -pub use transaction::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit}; +pub use transaction::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit, DEPOSIT_TX_TYPE_ID}; diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index bcfbd2ba..9e436ff4 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -1,5 +1,5 @@ use crate::{OpDepositReceipt, OpReceiptWithBloom, OpTxType}; -use alloy_eips::eip2718::{Decodable2718, Encodable2718}; +use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -62,7 +62,7 @@ impl OpReceiptEnvelope { /// Returns the success status of the receipt's transaction. pub fn status(&self) -> bool { - self.as_receipt().unwrap().inner.status + todo!() } /// Returns the cumulative gas used at this receipt. @@ -179,11 +179,12 @@ impl Encodable2718 for OpReceiptEnvelope { } impl Decodable2718 for OpReceiptEnvelope { - fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { + fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result { let receipt = Decodable::decode(buf)?; - match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? { + match ty.try_into().map_err(|_| Eip2718Error::UnexpectedType(ty))? { OpTxType::Legacy => { - Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) + Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported") + .into()) } OpTxType::Eip2930 => Ok(Self::Eip2930(receipt)), OpTxType::Eip1559 => Ok(Self::Eip1559(receipt)), @@ -192,7 +193,7 @@ impl Decodable2718 for OpReceiptEnvelope { } } - fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { + fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result { Ok(Self::Legacy(Decodable::decode(buf)?)) } } diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index d64d59cf..a250b1ad 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -1,5 +1,5 @@ use super::OpTxReceipt; -use alloy_consensus::{Receipt, TxReceipt}; +use alloy_consensus::{Eip658Value, Receipt, TxReceipt}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -41,6 +41,14 @@ impl OpDepositReceipt { } impl TxReceipt for OpDepositReceipt { + fn status_or_post_state(&self) -> &Eip658Value { + todo!() + } + + fn status(&self) -> bool { + todo!() + } + fn bloom(&self) -> Bloom { self.bloom_slow() } @@ -52,10 +60,6 @@ impl TxReceipt for OpDepositReceipt { fn logs(&self) -> &[Log] { &self.inner.logs } - - fn status(&self) -> bool { - self.inner.status - } } impl OpTxReceipt for OpDepositReceipt { @@ -86,8 +90,12 @@ pub struct OpReceiptWithBloom { } impl TxReceipt for OpReceiptWithBloom { + fn status_or_post_state(&self) -> &Eip658Value { + todo!() + } + fn status(&self) -> bool { - self.receipt.inner.status + todo!() } fn bloom(&self) -> Bloom { diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index 2cb2bc15..58610b14 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -2,9 +2,11 @@ use crate::TxDeposit; use alloy_consensus::{ Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy, }; -use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718}; +use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718}; use alloy_rlp::{Decodable, Encodable, Header}; -use core::mem; + +/// Identifier for an Optimism deposit transaction +pub const DEPOSIT_TX_TYPE_ID: u8 = 126; // 0x7E /// Optimism `TransactionType` flags as specified in EIPs [2718], [1559], and /// [2930], as well as the [deposit transaction spec][deposit-spec] @@ -26,7 +28,7 @@ pub enum OpTxType { /// EIP-4844 transaction type. Eip4844 = 3, /// Optimism Deposit transaction type. - Deposit = 0x7E, + Deposit = 127, } #[cfg(any(test, feature = "arbitrary"))] @@ -37,7 +39,7 @@ impl<'a> arbitrary::Arbitrary<'a> for OpTxType { 1 => OpTxType::Eip2930, 2 => OpTxType::Eip1559, 3 => OpTxType::Eip4844, - 0x7E => OpTxType::Deposit, + 127 => OpTxType::Deposit, _ => unreachable!(), }) } @@ -47,11 +49,14 @@ impl TryFrom for OpTxType { type Error = Eip2718Error; fn try_from(value: u8) -> Result { - match value { - // SAFETY: repr(u8) with explicit discriminant - 0..=3 | 0x7E => Ok(unsafe { mem::transmute::(value) }), - _ => Err(Eip2718Error::UnexpectedType(value)), - } + Ok(match value { + 0 => Self::Legacy, + 1 => Self::Eip2930, + 2 => Self::Eip1559, + 3 => Self::Eip4844, + 127 => Self::Deposit, + _ => return Err(Eip2718Error::UnexpectedType(value)), + }) } } @@ -219,24 +224,32 @@ impl Encodable for OpTxEnvelope { impl Decodable for OpTxEnvelope { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - Self::network_decode(buf) + match Self::network_decode(buf) { + Ok(t) => Ok(t), + Err(Eip2718Error::RlpError(e)) => Err(e), + Err(Eip2718Error::UnexpectedType(_)) => { + Err(alloy_rlp::Error::Custom("unexpected tx type")) + } + _ => Err(alloy_rlp::Error::Custom("unknown error decoding tx envelope")), + } } } impl Decodable2718 for OpTxEnvelope { - fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result { - match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("unexpected tx type"))? { + fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result { + match ty.try_into().map_err(|_| Eip2718Error::UnexpectedType(ty))? { OpTxType::Eip2930 => Ok(Self::Eip2930(TxEip2930::decode_signed_fields(buf)?)), OpTxType::Eip1559 => Ok(Self::Eip1559(TxEip1559::decode_signed_fields(buf)?)), OpTxType::Eip4844 => Ok(Self::Eip4844(TxEip4844Variant::decode_signed_fields(buf)?)), OpTxType::Deposit => Ok(Self::Deposit(TxDeposit::decode(buf)?)), OpTxType::Legacy => { - Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported")) + Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported") + .into()) } } } - fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result { + fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result { Ok(OpTxEnvelope::Legacy(TxLegacy::decode_signed_fields(buf)?)) } } diff --git a/crates/op-consensus/src/transaction/mod.rs b/crates/op-consensus/src/transaction/mod.rs index 9e4cb8f7..0806e9cc 100644 --- a/crates/op-consensus/src/transaction/mod.rs +++ b/crates/op-consensus/src/transaction/mod.rs @@ -2,7 +2,7 @@ mod optimism; pub use optimism::TxDeposit; mod envelope; -pub use envelope::{OpTxEnvelope, OpTxType}; +pub use envelope::{OpTxEnvelope, OpTxType, DEPOSIT_TX_TYPE_ID}; mod typed; pub use typed::OpTypedTransaction; From 6e79643d07fb474d39e4baecba5715a47c7ff7bb Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 15:05:04 +0200 Subject: [PATCH 18/20] reuse exiting receipt --- crates/op-consensus/src/lib.rs | 2 +- crates/op-consensus/src/receipt/envelope.rs | 109 ++++++++++-------- crates/op-consensus/src/receipt/mod.rs | 41 +++---- crates/op-consensus/src/receipt/receipts.rs | 46 ++++---- .../op-consensus/src/transaction/optimism.rs | 24 ++-- crates/op-consensus/src/transaction/typed.rs | 40 +++---- 6 files changed, 142 insertions(+), 120 deletions(-) diff --git a/crates/op-consensus/src/lib.rs b/crates/op-consensus/src/lib.rs index 4288f8be..a21aad3a 100644 --- a/crates/op-consensus/src/lib.rs +++ b/crates/op-consensus/src/lib.rs @@ -20,7 +20,7 @@ extern crate alloc; mod receipt; -pub use receipt::{OpDepositReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}; +pub use receipt::{OpDepositReceipt, OpDepositReceiptWithBloom, OpReceiptEnvelope, OpTxReceipt}; mod transaction; pub use transaction::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit, DEPOSIT_TX_TYPE_ID}; diff --git a/crates/op-consensus/src/receipt/envelope.rs b/crates/op-consensus/src/receipt/envelope.rs index 9e436ff4..edb8d1f1 100644 --- a/crates/op-consensus/src/receipt/envelope.rs +++ b/crates/op-consensus/src/receipt/envelope.rs @@ -1,4 +1,5 @@ -use crate::{OpDepositReceipt, OpReceiptWithBloom, OpTxType}; +use crate::{OpDepositReceipt, OpDepositReceiptWithBloom, OpTxType}; +use alloy_consensus::{Receipt, ReceiptWithBloom}; use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; @@ -20,27 +21,27 @@ use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; pub enum OpReceiptEnvelope { /// Receipt envelope with no type flag. #[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))] - Legacy(OpReceiptWithBloom), + Legacy(ReceiptWithBloom), /// Receipt envelope with type flag 1, containing a [EIP-2930] receipt. /// /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930 #[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))] - Eip2930(OpReceiptWithBloom), + Eip2930(ReceiptWithBloom), /// Receipt envelope with type flag 2, containing a [EIP-1559] receipt. /// /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559 #[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))] - Eip1559(OpReceiptWithBloom), - /// Receipt envelope with type flag 3, containing a [EIP-4844] receipt. + Eip1559(ReceiptWithBloom), + /// Receipt envelope with type flag 2, containing a [EIP-4844] receipt. /// /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 #[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))] - Eip4844(OpReceiptWithBloom), + Eip4844(ReceiptWithBloom), /// Receipt envelope with type flag 126, containing a [deposit] receipt. /// /// [deposit]: https://specs.optimism.io/protocol/deposits.html #[cfg_attr(feature = "serde", serde(rename = "0x7E", alias = "0x7E"))] - Deposit(OpReceiptWithBloom), + Deposit(OpDepositReceiptWithBloom), } impl OpReceiptEnvelope { @@ -62,55 +63,64 @@ impl OpReceiptEnvelope { /// Returns the success status of the receipt's transaction. pub fn status(&self) -> bool { - todo!() + self.as_receipt().unwrap().status.coerce_status() } /// Returns the cumulative gas used at this receipt. pub fn cumulative_gas_used(&self) -> u128 { - self.as_receipt().unwrap().inner.cumulative_gas_used + self.as_receipt().unwrap().cumulative_gas_used } /// Return the receipt logs. pub fn logs(&self) -> &[T] { - &self.as_receipt().unwrap().inner.logs + &self.as_receipt().unwrap().logs } /// Return the receipt's bloom. - pub fn logs_bloom(&self) -> &Bloom { - &self.as_receipt_with_bloom().unwrap().logs_bloom + pub const fn logs_bloom(&self) -> &Bloom { + match self { + Self::Legacy(t) => &t.logs_bloom, + Self::Eip2930(t) => &t.logs_bloom, + Self::Eip1559(t) => &t.logs_bloom, + Self::Eip4844(t) => &t.logs_bloom, + Self::Deposit(t) => &t.logs_bloom, + } } - /// Return the receipt's deposit_nonce. + /// Return the receipt's deposit_nonce if it is a deposit receipt. pub fn deposit_nonce(&self) -> Option { - self.as_receipt().unwrap().deposit_nonce + self.as_deposit_receipt().and_then(|r| r.deposit_nonce) } - /// Return the receipt's deposit version. + /// Return the receipt's deposit version if it is a deposit receipt. pub fn deposit_receipt_version(&self) -> Option { - self.as_receipt().unwrap().deposit_receipt_version + self.as_deposit_receipt().and_then(|r| r.deposit_receipt_version) + } + + /// Returns the deposit receipt if it is a deposit receipt. + pub const fn as_deposit_receipt_with_bloom(&self) -> Option<&OpDepositReceiptWithBloom> { + match self { + Self::Deposit(t) => Some(t), + _ => None, + } } - /// Return the inner receipt with bloom. Currently this is infallible, - /// however, future receipt types may be added. - pub const fn as_receipt_with_bloom(&self) -> Option<&OpReceiptWithBloom> { + /// Returns the deposit receipt if it is a deposit receipt. + pub const fn as_deposit_receipt(&self) -> Option<&OpDepositReceipt> { match self { - Self::Legacy(t) - | Self::Eip2930(t) - | Self::Eip1559(t) - | Self::Eip4844(t) - | Self::Deposit(t) => Some(t), + Self::Deposit(t) => Some(&t.receipt), + _ => None, } } /// Return the inner receipt. Currently this is infallible, however, future /// receipt types may be added. - pub const fn as_receipt(&self) -> Option<&OpDepositReceipt> { + pub const fn as_receipt(&self) -> Option<&Receipt> { match self { - Self::Legacy(t) - | Self::Eip2930(t) - | Self::Eip1559(t) - | Self::Eip4844(t) - | Self::Deposit(t) => Some(&t.receipt), + Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => { + Some(&t.receipt) + } + Self::Deposit(t) => Some(&t.receipt.inner), } } } @@ -118,12 +128,18 @@ impl OpReceiptEnvelope { impl OpReceiptEnvelope { /// Get the length of the inner receipt in the 2718 encoding. pub fn inner_length(&self) -> usize { - self.as_receipt_with_bloom().unwrap().length() + match self { + Self::Legacy(t) => t.length(), + Self::Eip2930(t) => t.length(), + Self::Eip1559(t) => t.length(), + Self::Eip4844(t) => t.length(), + Self::Deposit(t) => t.length(), + } } /// Calculate the length of the rlp payload of the network encoded receipt. pub fn rlp_payload_length(&self) -> usize { - let length = self.as_receipt_with_bloom().unwrap().length(); + let length = self.inner_length(); match self { Self::Legacy(_) => length, _ => length + 1, @@ -174,22 +190,27 @@ impl Encodable2718 for OpReceiptEnvelope { None => {} Some(ty) => out.put_u8(ty), } - self.as_receipt_with_bloom().unwrap().encode(out); + match self { + Self::Deposit(t) => t.encode(out), + OpReceiptEnvelope::Legacy(t) + | OpReceiptEnvelope::Eip2930(t) + | OpReceiptEnvelope::Eip1559(t) + | OpReceiptEnvelope::Eip4844(t) => t.encode(out), + } } } impl Decodable2718 for OpReceiptEnvelope { fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result { - let receipt = Decodable::decode(buf)?; match ty.try_into().map_err(|_| Eip2718Error::UnexpectedType(ty))? { OpTxType::Legacy => { Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported") .into()) } - OpTxType::Eip2930 => Ok(Self::Eip2930(receipt)), - OpTxType::Eip1559 => Ok(Self::Eip1559(receipt)), - OpTxType::Eip4844 => Ok(Self::Eip4844(receipt)), - OpTxType::Deposit => Ok(Self::Deposit(receipt)), + OpTxType::Eip1559 => Ok(Self::Eip1559(Decodable::decode(buf)?)), + OpTxType::Eip2930 => Ok(Self::Eip2930(Decodable::decode(buf)?)), + OpTxType::Eip4844 => Ok(Self::Eip4844(Decodable::decode(buf)?)), + OpTxType::Deposit => Ok(Self::Deposit(Decodable::decode(buf)?)), } } @@ -204,14 +225,12 @@ where T: arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - let receipt = OpReceiptWithBloom::::arbitrary(u)?; - match u.int_in_range(0..=4)? { - 0 => Ok(Self::Legacy(receipt)), - 1 => Ok(Self::Eip2930(receipt)), - 2 => Ok(Self::Eip1559(receipt)), - 3 => Ok(Self::Eip4844(receipt)), - _ => Ok(Self::Deposit(receipt)), + 0 => Ok(Self::Legacy(ReceiptWithBloom::::arbitrary(u)?)), + 1 => Ok(Self::Eip2930(ReceiptWithBloom::::arbitrary(u)?)), + 2 => Ok(Self::Eip1559(ReceiptWithBloom::::arbitrary(u)?)), + 3 => Ok(Self::Eip4844(ReceiptWithBloom::::arbitrary(u)?)), + _ => Ok(Self::Deposit(OpDepositReceiptWithBloom::::arbitrary(u)?)), } } } diff --git a/crates/op-consensus/src/receipt/mod.rs b/crates/op-consensus/src/receipt/mod.rs index 040aa668..e58c5bdf 100644 --- a/crates/op-consensus/src/receipt/mod.rs +++ b/crates/op-consensus/src/receipt/mod.rs @@ -4,7 +4,7 @@ mod envelope; pub use envelope::OpReceiptEnvelope; mod receipts; -pub use receipts::{OpDepositReceipt, OpReceiptWithBloom}; +pub use receipts::{OpDepositReceipt, OpDepositReceiptWithBloom}; /// Receipt is the result of a transaction execution. pub trait OpTxReceipt: TxReceipt { @@ -18,7 +18,7 @@ pub trait OpTxReceipt: TxReceipt { #[cfg(test)] mod tests { use super::*; - use alloy_consensus::Receipt; + use alloy_consensus::{Receipt, ReceiptWithBloom}; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{address, b256, bytes, hex, Bytes, Log, LogData}; use alloy_rlp::{Decodable, Encodable}; @@ -32,10 +32,10 @@ mod tests { let expected = hex!("f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"); let mut data = vec![]; - let receipt = OpReceiptEnvelope::Legacy(OpReceiptWithBloom { - receipt: OpDepositReceipt { - inner: Receipt { - status: false, + let receipt = + OpReceiptEnvelope::Legacy(ReceiptWithBloom { + receipt: Receipt { + status: false.into(), cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), @@ -48,11 +48,8 @@ mod tests { ), }], }, - deposit_nonce: None, - deposit_receipt_version: None, - }, - logs_bloom: [0; 256].into(), - }); + logs_bloom: [0; 256].into(), + }); receipt.network_encode(&mut data); @@ -68,10 +65,10 @@ mod tests { // EIP658Receipt let expected = - OpReceiptWithBloom { + OpDepositReceiptWithBloom { receipt: OpDepositReceipt { inner: Receipt { - status: false, + status: false.into(), cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), @@ -90,7 +87,7 @@ mod tests { logs_bloom: [0; 256].into(), }; - let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let receipt = OpDepositReceiptWithBloom::decode(&mut &data[..]).unwrap(); assert_eq!(receipt, expected); } @@ -99,7 +96,7 @@ mod tests { let receipt = OpDepositReceipt { inner: Receipt { cumulative_gas_used: 16747627, - status: true, + status: true.into(), logs: vec![ Log { address: address!("4bf56695415f725e43c3e04354b604bcfb6dfb6e"), @@ -129,7 +126,7 @@ mod tests { let mut data = vec![]; receipt.encode(&mut data); - let decoded = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let decoded = OpDepositReceiptWithBloom::decode(&mut &data[..]).unwrap(); // receipt.clone().to_compact(&mut data); // let (decoded, _) = Receipt::from_compact(&data[..], data.len()); @@ -141,16 +138,16 @@ mod tests { let data = hex!("f9010c0182b741b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0833d3bbf"); // Deposit Receipt (post-regolith) - let expected = OpReceiptWithBloom { + let expected = OpDepositReceiptWithBloom { receipt: OpDepositReceipt { - inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true }, + inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true.into() }, deposit_nonce: Some(4012991), deposit_receipt_version: None, }, logs_bloom: [0; 256].into(), }; - let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let receipt = OpDepositReceiptWithBloom::decode(&mut &data[..]).unwrap(); assert_eq!(receipt, expected); let mut buf = Vec::new(); @@ -163,16 +160,16 @@ mod tests { let data = hex!("f9010d0182b741b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0833d3bbf01"); // Deposit Receipt (post-regolith) - let expected = OpReceiptWithBloom { + let expected = OpDepositReceiptWithBloom { receipt: OpDepositReceipt { - inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true }, + inner: Receipt { cumulative_gas_used: 46913, logs: vec![], status: true.into() }, deposit_nonce: Some(4012991), deposit_receipt_version: Some(1), }, logs_bloom: [0; 256].into(), }; - let receipt = OpReceiptWithBloom::decode(&mut &data[..]).unwrap(); + let receipt = OpDepositReceiptWithBloom::decode(&mut &data[..]).unwrap(); assert_eq!(receipt, expected); let mut buf = Vec::new(); diff --git a/crates/op-consensus/src/receipt/receipts.rs b/crates/op-consensus/src/receipt/receipts.rs index a250b1ad..08243ab3 100644 --- a/crates/op-consensus/src/receipt/receipts.rs +++ b/crates/op-consensus/src/receipt/receipts.rs @@ -27,26 +27,32 @@ pub struct OpDepositReceipt { } impl OpDepositReceipt { - /// Calculates [`Log`]'s bloom filter. this is slow operation and [OpReceiptWithBloom] can - /// be used to cache this value. + /// Calculates [`Log`]'s bloom filter. this is slow operation and [OpDepositReceiptWithBloom] + /// can be used to cache this value. pub fn bloom_slow(&self) -> Bloom { self.inner.logs.iter().collect() } - /// Calculates the bloom filter for the receipt and returns the [OpReceiptWithBloom] container - /// type. - pub fn with_bloom(self) -> OpReceiptWithBloom { + /// Calculates the bloom filter for the receipt and returns the [OpDepositReceiptWithBloom] + /// container type. + pub fn with_bloom(self) -> OpDepositReceiptWithBloom { self.into() } } +impl AsRef> for OpDepositReceipt { + fn as_ref(&self) -> &Receipt { + &self.inner + } +} + impl TxReceipt for OpDepositReceipt { fn status_or_post_state(&self) -> &Eip658Value { - todo!() + self.inner.status_or_post_state() } fn status(&self) -> bool { - todo!() + self.inner.status() } fn bloom(&self) -> Bloom { @@ -81,7 +87,7 @@ impl OpTxReceipt for OpDepositReceipt { #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct OpReceiptWithBloom { +pub struct OpDepositReceiptWithBloom { #[cfg_attr(feature = "serde", serde(flatten))] /// The receipt. pub receipt: OpDepositReceipt, @@ -89,13 +95,13 @@ pub struct OpReceiptWithBloom { pub logs_bloom: Bloom, } -impl TxReceipt for OpReceiptWithBloom { +impl TxReceipt for OpDepositReceiptWithBloom { fn status_or_post_state(&self) -> &Eip658Value { - todo!() + self.receipt.status_or_post_state() } fn status(&self) -> bool { - todo!() + self.receipt.status() } fn bloom(&self) -> Bloom { @@ -115,7 +121,7 @@ impl TxReceipt for OpReceiptWithBloom { } } -impl OpTxReceipt for OpReceiptWithBloom { +impl OpTxReceipt for OpDepositReceiptWithBloom { fn deposit_nonce(&self) -> Option { self.receipt.deposit_nonce } @@ -125,15 +131,15 @@ impl OpTxReceipt for OpReceiptWithBloom { } } -impl From for OpReceiptWithBloom { +impl From for OpDepositReceiptWithBloom { fn from(receipt: OpDepositReceipt) -> Self { let bloom = receipt.bloom_slow(); - OpReceiptWithBloom { receipt, logs_bloom: bloom } + OpDepositReceiptWithBloom { receipt, logs_bloom: bloom } } } -impl OpReceiptWithBloom { - /// Create new [OpReceiptWithBloom] +impl OpDepositReceiptWithBloom { + /// Create new [OpDepositReceiptWithBloom] pub const fn new(receipt: OpDepositReceipt, bloom: Bloom) -> Self { Self { receipt, logs_bloom: bloom } } @@ -217,7 +223,7 @@ impl OpReceiptWithBloom { } } -impl alloy_rlp::Encodable for OpReceiptWithBloom { +impl alloy_rlp::Encodable for OpDepositReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_fields(out); } @@ -233,7 +239,7 @@ impl alloy_rlp::Encodable for OpReceiptWithBloom { } } -impl alloy_rlp::Decodable for OpReceiptWithBloom { +impl alloy_rlp::Decodable for OpDepositReceiptWithBloom { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { Self::decode_receipt(buf) } @@ -250,7 +256,7 @@ where deposit_nonce.is_some().then(|| u64::arbitrary(u)).transpose()?; Ok(Self { inner: Receipt { - status: bool::arbitrary(u)?, + status: Eip658Value::arbitrary(u)?, cumulative_gas_used: u128::arbitrary(u)?, logs: Vec::::arbitrary(u)?, }, @@ -261,7 +267,7 @@ where } #[cfg(all(test, feature = "arbitrary"))] -impl<'a, T> arbitrary::Arbitrary<'a> for OpReceiptWithBloom +impl<'a, T> arbitrary::Arbitrary<'a> for OpDepositReceiptWithBloom where T: arbitrary::Arbitrary<'a>, { diff --git a/crates/op-consensus/src/transaction/optimism.rs b/crates/op-consensus/src/transaction/optimism.rs index fd9563a4..e04a4c37 100644 --- a/crates/op-consensus/src/transaction/optimism.rs +++ b/crates/op-consensus/src/transaction/optimism.rs @@ -110,18 +110,6 @@ impl TxDeposit { } impl Transaction for TxDeposit { - fn input(&self) -> &[u8] { - &self.input - } - - fn to(&self) -> TxKind { - self.to - } - - fn value(&self) -> U256 { - self.value - } - fn chain_id(&self) -> Option { None } @@ -137,6 +125,18 @@ impl Transaction for TxDeposit { fn gas_price(&self) -> Option { None } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn input(&self) -> &[u8] { + &self.input + } } impl Encodable for TxDeposit { diff --git a/crates/op-consensus/src/transaction/typed.rs b/crates/op-consensus/src/transaction/typed.rs index 61e6657a..e595bbb3 100644 --- a/crates/op-consensus/src/transaction/typed.rs +++ b/crates/op-consensus/src/transaction/typed.rs @@ -138,6 +138,16 @@ impl Transaction for OpTypedTransaction { } } + fn nonce(&self) -> u64 { + match self { + Self::Legacy(tx) => tx.nonce(), + Self::Eip2930(tx) => tx.nonce(), + Self::Eip1559(tx) => tx.nonce(), + Self::Eip4844(tx) => tx.nonce(), + Self::Deposit(tx) => tx.nonce(), + } + } + fn gas_limit(&self) -> u128 { match self { Self::Legacy(tx) => tx.gas_limit(), @@ -158,26 +168,6 @@ impl Transaction for OpTypedTransaction { } } - fn input(&self) -> &[u8] { - match self { - Self::Legacy(tx) => tx.input(), - Self::Eip2930(tx) => tx.input(), - Self::Eip1559(tx) => tx.input(), - Self::Eip4844(tx) => tx.input(), - Self::Deposit(tx) => tx.input(), - } - } - - fn nonce(&self) -> u64 { - match self { - Self::Legacy(tx) => tx.nonce(), - Self::Eip2930(tx) => tx.nonce(), - Self::Eip1559(tx) => tx.nonce(), - Self::Eip4844(tx) => tx.nonce(), - Self::Deposit(tx) => tx.nonce(), - } - } - fn to(&self) -> TxKind { match self { Self::Legacy(tx) => tx.to(), @@ -197,4 +187,14 @@ impl Transaction for OpTypedTransaction { Self::Deposit(tx) => tx.value(), } } + + fn input(&self) -> &[u8] { + match self { + Self::Legacy(tx) => tx.input(), + Self::Eip2930(tx) => tx.input(), + Self::Eip1559(tx) => tx.input(), + Self::Eip4844(tx) => tx.input(), + Self::Deposit(tx) => tx.input(), + } + } } From fe72a56669bc3be3cbb7eaeb92ce1f4b72db0287 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 15:55:45 +0200 Subject: [PATCH 19/20] fix feature --- crates/rpc-types/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/rpc-types/Cargo.toml b/crates/rpc-types/Cargo.toml index fb8b8b21..f5f891a1 100644 --- a/crates/rpc-types/Cargo.toml +++ b/crates/rpc-types/Cargo.toml @@ -32,6 +32,5 @@ rand.workspace = true arbitrary = [ "dep:arbitrary", "alloy-primitives/arbitrary", - "alloy-consensus/arbitrary", - "alloy-rpc-types/arbitrary", + "alloy-rpc-types-eth/arbitrary", ] \ No newline at end of file From 3485d5b71934f45b242d2cfac5f0cb9f19a9a2f5 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 16:26:59 +0200 Subject: [PATCH 20/20] fix identifier --- crates/op-consensus/src/transaction/envelope.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/op-consensus/src/transaction/envelope.rs b/crates/op-consensus/src/transaction/envelope.rs index 58610b14..bf4023be 100644 --- a/crates/op-consensus/src/transaction/envelope.rs +++ b/crates/op-consensus/src/transaction/envelope.rs @@ -28,7 +28,7 @@ pub enum OpTxType { /// EIP-4844 transaction type. Eip4844 = 3, /// Optimism Deposit transaction type. - Deposit = 127, + Deposit = 126, } #[cfg(any(test, feature = "arbitrary"))] @@ -39,7 +39,7 @@ impl<'a> arbitrary::Arbitrary<'a> for OpTxType { 1 => OpTxType::Eip2930, 2 => OpTxType::Eip1559, 3 => OpTxType::Eip4844, - 127 => OpTxType::Deposit, + 126 => OpTxType::Deposit, _ => unreachable!(), }) } @@ -54,7 +54,7 @@ impl TryFrom for OpTxType { 1 => Self::Eip2930, 2 => Self::Eip1559, 3 => Self::Eip4844, - 127 => Self::Deposit, + 126 => Self::Deposit, _ => return Err(Eip2718Error::UnexpectedType(value)), }) }