diff --git a/Cargo.lock b/Cargo.lock index e82fee2..b108fcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,21 +80,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.6.11" @@ -407,20 +392,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets 0.52.0", -] - [[package]] name = "cidr" version = "0.1.1" @@ -797,11 +768,10 @@ dependencies = [ [[package]] name = "eth-prototype" -version = "0.1.0" +version = "0.0.0" dependencies = [ "aes 0.8.4", "byteorder", - "chrono", "ctr 0.9.2", "devp2p", "ecb", @@ -813,7 +783,6 @@ dependencies = [ "log", "num", "postgres", - "primitive-types 0.13.1", "rand_core 0.6.4", "rlp 0.6.1", "secp256k1 0.20.3", @@ -843,7 +812,7 @@ dependencies = [ "serde_json", "sha3 0.10.8", "thiserror", - "uint 0.9.5", + "uint", ] [[package]] @@ -854,7 +823,7 @@ checksum = "bfb684ac8fa8f6c5759f788862bb22ec6fe3cb392f6bfd08e3c64b603661e3f8" dependencies = [ "crunchy", "fixed-hash 0.7.0", - "impl-rlp 0.3.0", + "impl-rlp", "tiny-keccak", ] @@ -866,7 +835,7 @@ checksum = "c22d4b5885b6aa2fe5e8b9329fb8d232bf739e434e6b87347c63bdd00c120f60" dependencies = [ "crunchy", "fixed-hash 0.8.0", - "impl-rlp 0.3.0", + "impl-rlp", "impl-serde", "tiny-keccak", ] @@ -879,9 +848,9 @@ checksum = "f64b5df66a228d85e4b17e5d6c6aa43b0310898ffe8a85988c4c032357aaabfd" dependencies = [ "ethbloom 0.11.1", "fixed-hash 0.7.0", - "impl-rlp 0.3.0", + "impl-rlp", "primitive-types 0.9.1", - "uint 0.9.5", + "uint", ] [[package]] @@ -892,10 +861,10 @@ checksum = "02d215cbf040552efcbe99a38372fe80ab9d00268e20012b79fcd0f073edd8ee" dependencies = [ "ethbloom 0.13.0", "fixed-hash 0.8.0", - "impl-rlp 0.3.0", + "impl-rlp", "impl-serde", "primitive-types 0.12.2", - "uint 0.9.5", + "uint", ] [[package]] @@ -1342,29 +1311,6 @@ dependencies = [ "tokio-native-tls", ] -[[package]] -name = "iana-time-zone" -version = "0.1.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "idna" version = "0.4.0" @@ -1403,15 +1349,6 @@ dependencies = [ "parity-scale-codec 3.6.9", ] -[[package]] -name = "impl-codec" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b67aa010c1e3da95bf151bd8b4c059b2ed7e75387cdb969b4f8f2723a43f9941" -dependencies = [ - "parity-scale-codec 3.6.9", -] - [[package]] name = "impl-rlp" version = "0.3.0" @@ -1421,15 +1358,6 @@ dependencies = [ "rlp 0.5.2", ] -[[package]] -name = "impl-rlp" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54ed8ad1f3877f7e775b8cbf30ed1bd3209a95401817f19a0eb4402d13f8cf90" -dependencies = [ - "rlp 0.6.1", -] - [[package]] name = "impl-serde" version = "0.4.0" @@ -2015,8 +1943,8 @@ checksum = "06345ee39fbccfb06ab45f3a1a5798d9dafa04cb8921a76d227040003a234b0e" dependencies = [ "fixed-hash 0.7.0", "impl-codec 0.5.1", - "impl-rlp 0.3.0", - "uint 0.9.5", + "impl-rlp", + "uint", ] [[package]] @@ -2027,21 +1955,9 @@ checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" dependencies = [ "fixed-hash 0.8.0", "impl-codec 0.6.0", - "impl-rlp 0.3.0", + "impl-rlp", "impl-serde", - "uint 0.9.5", -] - -[[package]] -name = "primitive-types" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d15600a7d856470b7d278b3fe0e311fe28c2526348549f8ef2ff7db3299c87f5" -dependencies = [ - "fixed-hash 0.8.0", - "impl-codec 0.7.0", - "impl-rlp 0.4.0", - "uint 0.10.0", + "uint", ] [[package]] @@ -3182,18 +3098,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "uint" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "909988d098b2f738727b161a106cfc7cab00c539c2687a8836f8e565976fb53e" -dependencies = [ - "byteorder", - "crunchy", - "hex", - "static_assertions", -] - [[package]] name = "unicode-bidi" version = "0.3.15" @@ -3433,15 +3337,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets 0.52.0", -] - [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 92b7c3f..863432d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "eth-prototype" -version = "0.1.0" -edition = "2021" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[bin]] @@ -28,14 +27,12 @@ secp256k1 = { version = "0.28.1", features = ["recovery", "global-context", "ran snap = "1.1.0" postgres = { version = "0.19.9" } tokio-postgres = { version = "0.7" } -primitive-types = { version = "0.13.1", features = ["impl-rlp"] } log = "0.4.22" env_logger = "0.11.6" toml = "0.8.19" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0" } num = "0.4.3" -chrono = "0.4.39" tokio = { version = "1", features = ["full"] } [dev-dependencies] @@ -46,3 +43,11 @@ ethereum-types = "0.14.1" tx-from-scratch = { git = "https://github.com/Kuly14/tx-from-scratch" } web3 = "0.19.0" + +[profile.release] +opt-level = 3 # maximum compiler optimizations +lto = "thin" # cross-crate link-time optimization (good balance of speed vs compile time) +codegen-units = 1 # single codegen unit for better optimization at the cost of compile time +debug = true # keep debug symbols for readable stack traces +strip = "none" # do not strip anything from the binary +panic = "unwind" # keep stack unwinding on panic diff --git a/src/connection.rs b/src/connection.rs index f8505c1..c56bcb1 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,6 +1,6 @@ use rand_core::RngCore; -use secp256k1::rand; use secp256k1::SecretKey; +use secp256k1::rand; use std::error; use std::net::SocketAddr; use std::str::FromStr; diff --git a/src/database.rs b/src/database.rs index d986bf1..6aecd03 100644 --- a/src/database.rs +++ b/src/database.rs @@ -118,120 +118,140 @@ pub fn save_blocks( let mut receipts_string: String = String::new(); info!("starting to format blocks and transactions"); - blocks.iter().for_each(|(b, txs, ommers, withdrawals, receipts)| { - let tmp = format!( - "\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{};\\\\x{};{};\\\\x{}\n", // Important! We don't end with a ';' - hex::encode(&b.hash), - hex::encode(&b.parent_hash), - hex::encode(&b.ommers_hash), - hex::encode(&b.coinbase), - hex::encode(&b.state_root), - hex::encode(&b.txs_root), - hex::encode(&b.receipts_root), - hex::encode(&b.bloom), - b.difficulty, - b.number, - b.gas_limit, - b.gas_used, - b.time, - hex::encode(&b.extradata), - hex::encode(&b.mix_digest), - hex::encode(&b.block_nonce), - b.basefee_per_gas, - hex::encode(&b.withdrawals_root), - ); - - blocks_string.push_str(&tmp); - let mut index = 0; - txs.iter().for_each(|t| { + blocks + .iter() + .for_each(|(b, txs, ommers, withdrawals, receipts)| { let tmp = format!( - "{};\\\\x{};{};\\\\x{};{};{};{};{};{};{};\\\\x{};\\\\x{};{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{}\n", // Important! We don't end with a ';' - index, - hex::encode(&t.txid), - t.tx_type, + "\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{};\\\\x{};{};\\\\x{}\n", // Important! We don't end with a ';' hex::encode(&b.hash), - serde_json::to_string(&t.chain_id).unwrap(), - t.nonce, - if let Some(gas_price) = &t.gas_price { gas_price.to_string() } else { "null".to_string() }, - if let Some(max_priority_fee_per_gas) = t.max_priority_fee_per_gas { max_priority_fee_per_gas.to_string() } else { "null".to_string() }, - if let Some(max_fee_per_gas) = t.max_fee_per_gas { max_fee_per_gas.to_string() } else { "null".to_string() }, - t.gas_limit, - hex::encode(&t.from), - hex::encode(&t.to), - t.value.to_string(), - hex::encode(&t.data), - serde_json::to_string(&t.access_list).unwrap(), - if let Some(max_fee_per_blob_gas) = t.max_fee_per_blob_gas { max_fee_per_blob_gas.to_string() } else { "null".to_string() }, - serde_json::to_value(&t.blob_versioned_hashes).unwrap(), - serde_json::to_value(&t.authorization_list).unwrap(), - t.v, - hex::encode(&t.r), - hex::encode(&t.s), + hex::encode(&b.parent_hash), + hex::encode(&b.ommers_hash), + hex::encode(&b.coinbase), + hex::encode(&b.state_root), + hex::encode(&b.txs_root), + hex::encode(&b.receipts_root), + hex::encode(&b.bloom), + b.difficulty, + b.number, + b.gas_limit, + b.gas_used, + b.time, + hex::encode(&b.extradata), + hex::encode(&b.mix_digest), + hex::encode(&b.block_nonce), + b.basefee_per_gas, + hex::encode(&b.withdrawals_root), ); - index += 1; - // if "to" adddress is empty, calculates the transaction address - if t.to.is_empty() { - let tx_address: Vec = utils::calculate_tx_addr(&t.from, &t.nonce); + blocks_string.push_str(&tmp); + let mut index = 0; + txs.iter().for_each(|t| { let tmp = format!( - "\\\\x{};\\\\x{}\n", // Important! We don't end with a ';' - hex::encode(&t.txid), - hex::encode(&tx_address)); - contracts_string.push_str(&tmp); - } - - // verifying if we are not going over the 1Gigabyte limit if yes we start a new copy in query - if transactions_string.as_bytes().len() + tmp.as_bytes().len() > 1000000000 { - transactions_strings.push(transactions_string.clone()); - transactions_string = String::new(); - } - transactions_string.push_str(&tmp); - }); - ommers.iter().for_each(|om| { - let tmp = format!( - "\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{};\\\\x{};{};\\\\x{}\n", // Important! We don't end with a ';' - hex::encode(&om.hash), - hex::encode(&b.hash), - hex::encode(&om.coinbase), - hex::encode(&om.state_root), - hex::encode(&om.txs_root), - hex::encode(&om.receipts_root), - hex::encode(&om.bloom), - om.difficulty, - om.number, - om.gas_limit, - om.gas_used, - om.time, - hex::encode(&om.extradata), - hex::encode(&om.mix_digest), - hex::encode(&om.block_nonce), - om.basefee_per_gas, - hex::encode(&om.withdrawals_root), - ); - ommers_string.push_str(&tmp); - }); - withdrawals.iter().for_each(|wd| { - let tmp = format!( - "\\\\x{};{};{};\\\\x{};{}\n", // Important! We don't end with a ';' - hex::encode(&b.hash), - wd.index, - wd.validator_index, - hex::encode(&wd.address), - wd.amount, - ); - withdrawals_string.push_str(&tmp); - }); - receipts.iter().enumerate().for_each(|(i, r)| { - let tmp = format!("\\\\x{};{};\\\\x{};{};{}\n", // Important! We don't end with a ';' - hex::encode(&txs[i].txid), - r.tx_type, - hex::encode(&r.post_state_or_status), - r.cumulative_gas, - serde_json::to_value(&r.logs).unwrap(), - ); - receipts_string.push_str(&tmp); + "{};\\\\x{};{};\\\\x{};{};{};{};{};{};{};\\\\x{};\\\\x{};{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{}\n", // Important! We don't end with a ';' + index, + hex::encode(&t.txid), + t.tx_type, + hex::encode(&b.hash), + serde_json::to_string(&t.chain_id).unwrap(), + t.nonce, + if let Some(gas_price) = &t.gas_price { + gas_price.to_string() + } else { + "null".to_string() + }, + if let Some(max_priority_fee_per_gas) = t.max_priority_fee_per_gas { + max_priority_fee_per_gas.to_string() + } else { + "null".to_string() + }, + if let Some(max_fee_per_gas) = t.max_fee_per_gas { + max_fee_per_gas.to_string() + } else { + "null".to_string() + }, + t.gas_limit, + hex::encode(&t.from), + hex::encode(&t.to), + t.value.to_string(), + hex::encode(&t.data), + serde_json::to_string(&t.access_list).unwrap(), + if let Some(max_fee_per_blob_gas) = t.max_fee_per_blob_gas { + max_fee_per_blob_gas.to_string() + } else { + "null".to_string() + }, + serde_json::to_value(&t.blob_versioned_hashes).unwrap(), + serde_json::to_value(&t.authorization_list).unwrap(), + t.v, + hex::encode(&t.r), + hex::encode(&t.s), + ); + index += 1; + + // if "to" adddress is empty, calculates the transaction address + if t.to.is_empty() { + let tx_address: Vec = utils::calculate_tx_addr(&t.from, &t.nonce); + let tmp = format!( + "\\\\x{};\\\\x{}\n", // Important! We don't end with a ';' + hex::encode(&t.txid), + hex::encode(&tx_address) + ); + contracts_string.push_str(&tmp); + } + + // verifying if we are not going over the 1Gigabyte limit if yes we start a new copy in query + if transactions_string.as_bytes().len() + tmp.as_bytes().len() > 1000000000 { + transactions_strings.push(transactions_string.clone()); + transactions_string = String::new(); + } + transactions_string.push_str(&tmp); + }); + ommers.iter().for_each(|om| { + let tmp = format!( + "\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};\\\\x{};{};{};{};{};{};\\\\x{};\\\\x{};\\\\x{};{};\\\\x{}\n", // Important! We don't end with a ';' + hex::encode(&om.hash), + hex::encode(&b.hash), + hex::encode(&om.coinbase), + hex::encode(&om.state_root), + hex::encode(&om.txs_root), + hex::encode(&om.receipts_root), + hex::encode(&om.bloom), + om.difficulty, + om.number, + om.gas_limit, + om.gas_used, + om.time, + hex::encode(&om.extradata), + hex::encode(&om.mix_digest), + hex::encode(&om.block_nonce), + om.basefee_per_gas, + hex::encode(&om.withdrawals_root), + ); + ommers_string.push_str(&tmp); + }); + withdrawals.iter().for_each(|wd| { + let tmp = format!( + "\\\\x{};{};{};\\\\x{};{}\n", // Important! We don't end with a ';' + hex::encode(&b.hash), + wd.index, + wd.validator_index, + hex::encode(&wd.address), + wd.amount, + ); + withdrawals_string.push_str(&tmp); + }); + receipts.iter().enumerate().for_each(|(i, r)| { + let tmp = format!( + "\\\\x{};{};\\\\x{};{};{}\n", // Important! We don't end with a ';' + hex::encode(&txs[i].txid), + r.tx_type, + hex::encode(&r.post_state_or_status), + r.cumulative_gas, + serde_json::to_value(&r.logs).unwrap(), + ); + receipts_string.push_str(&tmp); + }); }); - }); transactions_strings.push(transactions_string.clone()); diff --git a/src/eth.rs b/src/eth.rs index 9a90af6..db653d4 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -499,7 +499,7 @@ pub fn create_upgrade_status_message() -> Vec { mod tests { use crate::eth::parse_eth69_status_message; - use super::{create_upgrade_status_message, BASE_PROTOCOL_OFFSET}; + use super::{BASE_PROTOCOL_OFFSET, create_upgrade_status_message}; #[test] fn test_create_upgrade_status_message() { diff --git a/src/mac.rs b/src/mac.rs index 70e7318..37aa03a 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -1,4 +1,4 @@ -use aes::cipher::{block_padding::NoPadding, BlockEncryptMut, KeyInit}; +use aes::cipher::{BlockEncryptMut, KeyInit, block_padding::NoPadding}; use sha3::{Digest, Keccak256}; pub struct MAC { diff --git a/src/main.rs b/src/main.rs index e589d43..bad6bc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::collections::VecDeque; use std::env; -use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; use std::time::Duration; use tokio::task::JoinSet; @@ -179,7 +179,9 @@ fn main() { Some((last, batch.last().unwrap().0.hash.clone())); if network.genesis_hash.to_vec() == batch.last().unwrap().0.hash.to_vec() { - info!("We are synced ! We are creating the indexes on the tables... This will take a while."); + info!( + "We are synced ! We are creating the indexes on the tables... This will take a while." + ); utils::open_exec_sql_file(&network_arg, &mut postgres_client); break 'recv; } diff --git a/src/utils.rs b/src/utils.rs index 5aea401..e5cc4e1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ use aes::cipher::{KeyIvInit, StreamCipher}; use byteorder::ByteOrder; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; -use hmac_sha256::{Hash, HMAC}; +use hmac_sha256::{HMAC, Hash}; use postgres::Client; use rand_core::{OsRng, RngCore}; use rlp::RlpStream; @@ -447,7 +447,7 @@ pub fn handle_ack_message( #[cfg(test)] mod tests { use super::calculate_tx_addr; - use super::{ecdh_x, Aes256Ctr64BE}; + use super::{Aes256Ctr64BE, ecdh_x}; use crate::mac::MAC; use aes::cipher::KeyIvInit; use sha3::Digest; diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 9ed7ec6..b256b34 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -4,7 +4,7 @@ use eth_prototype::database; use eth_prototype::types; use eth_prototype::{mac, message, utils}; use log::info; -use secp256k1_20::{PublicKey, SecretKey, SECP256K1}; +use secp256k1_20::{PublicKey, SECP256K1, SecretKey}; use sha3::{Digest, Keccak256}; #[test]