mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Example/polygon p2p (#6393)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
19
examples/polygon-p2p/Cargo.toml
Normal file
19
examples/polygon-p2p/Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "polygon-p2p"
|
||||
version = "0.0.0"
|
||||
publish = false
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
secp256k1.workspace = true
|
||||
tokio.workspace = true
|
||||
reth-network.workspace = true
|
||||
reth-primitives.workspace = true
|
||||
serde_json.workspace = true
|
||||
reth-tracing.workspace = true
|
||||
tokio-stream.workspace = true
|
||||
reth-provider = { workspace = true, features = ["test-utils"] }
|
||||
reth-discv4 = { workspace = true, features = ["test-utils"] }
|
||||
50
examples/polygon-p2p/src/chain_cfg.rs
Normal file
50
examples/polygon-p2p/src/chain_cfg.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use reth_primitives::{
|
||||
b256, BaseFeeParams, Chain, ChainSpec, ForkCondition, ForkTimestamps, Hardfork, Head,
|
||||
NodeRecord, B256,
|
||||
};
|
||||
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
const SHANGAI_BLOCK: u64 = 50523000;
|
||||
|
||||
pub(crate) fn polygon_chain_spec() -> Arc<ChainSpec> {
|
||||
const GENESIS: B256 = b256!("a9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b");
|
||||
|
||||
ChainSpec {
|
||||
chain: Chain::from_id(137),
|
||||
// <https://github.com/maticnetwork/bor/blob/d521b8e266b97efe9c8fdce8167e9dd77b04637d/builder/files/genesis-mainnet-v1.json>
|
||||
genesis: serde_json::from_str(include_str!("./genesis.json")).expect("deserialize genesis"),
|
||||
genesis_hash: Some(GENESIS),
|
||||
fork_timestamps: ForkTimestamps::default().shanghai(1681338455),
|
||||
paris_block_and_final_difficulty: None,
|
||||
hardforks: BTreeMap::from([
|
||||
(Hardfork::Petersburg, ForkCondition::Block(0)),
|
||||
(Hardfork::Istanbul, ForkCondition::Block(3395000)),
|
||||
(Hardfork::MuirGlacier, ForkCondition::Block(3395000)),
|
||||
(Hardfork::Berlin, ForkCondition::Block(14750000)),
|
||||
(Hardfork::London, ForkCondition::Block(23850000)),
|
||||
(Hardfork::Shanghai, ForkCondition::Block(SHANGAI_BLOCK)),
|
||||
]),
|
||||
deposit_contract: None,
|
||||
base_fee_params: reth_primitives::BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
|
||||
snapshot_block_interval: 500_000,
|
||||
prune_delete_limit: 0,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Polygon mainnet boot nodes <https://github.com/maticnetwork/bor/blob/master/params/bootnodes.go#L79>
|
||||
static BOOTNODES : [&str; 4] = [
|
||||
"enode://b8f1cc9c5d4403703fbf377116469667d2b1823c0daf16b7250aa576bacf399e42c3930ccfcb02c5df6879565a2b8931335565f0e8d3f8e72385ecf4a4bf160a@3.36.224.80:30303",
|
||||
"enode://8729e0c825f3d9cad382555f3e46dcff21af323e89025a0e6312df541f4a9e73abfa562d64906f5e59c51fe6f0501b3e61b07979606c56329c020ed739910759@54.194.245.5:30303",
|
||||
"enode://76316d1cb93c8ed407d3332d595233401250d48f8fbb1d9c65bd18c0495eca1b43ec38ee0ea1c257c0abb7d1f25d649d359cdfe5a805842159cfe36c5f66b7e8@52.78.36.216:30303",
|
||||
"enode://681ebac58d8dd2d8a6eef15329dfbad0ab960561524cf2dfde40ad646736fe5c244020f20b87e7c1520820bc625cfb487dd71d63a3a3bf0baea2dbb8ec7c79f1@34.240.245.39:30303",
|
||||
];
|
||||
|
||||
pub(crate) fn head() -> Head {
|
||||
Head { number: SHANGAI_BLOCK, ..Default::default() }
|
||||
}
|
||||
|
||||
pub(crate) fn boot_nodes() -> Vec<NodeRecord> {
|
||||
BOOTNODES[..].iter().map(|s| s.parse().unwrap()).collect()
|
||||
}
|
||||
109
examples/polygon-p2p/src/genesis.json
Normal file
109
examples/polygon-p2p/src/genesis.json
Normal file
File diff suppressed because one or more lines are too long
80
examples/polygon-p2p/src/main.rs
Normal file
80
examples/polygon-p2p/src/main.rs
Normal file
@ -0,0 +1,80 @@
|
||||
//! Example for how hook into the polygon p2p network
|
||||
//!
|
||||
//! Run with
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! cargo run -p polygon-p2p
|
||||
//! ```
|
||||
//!
|
||||
//! This launch the regular reth node overriding the engine api payload builder with our custom.
|
||||
//!
|
||||
//! Credits to: <https://blog.merkle.io/blog/fastest-transaction-network-eth-polygon-bsc>
|
||||
use chain_cfg::{boot_nodes, head, polygon_chain_spec};
|
||||
use reth_discv4::Discv4ConfigBuilder;
|
||||
use reth_network::{
|
||||
config::NetworkMode, NetworkConfig, NetworkEvent, NetworkEvents, NetworkManager,
|
||||
};
|
||||
use reth_tracing::{
|
||||
tracing::info, tracing_subscriber::filter::LevelFilter, LayerInfo, LogFormat, RethTracer,
|
||||
Tracer,
|
||||
};
|
||||
use secp256k1::{rand, SecretKey};
|
||||
use std::{
|
||||
net::{Ipv4Addr, SocketAddr},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio_stream::StreamExt;
|
||||
pub mod chain_cfg;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// The ECDSA private key used to create our enode identifier.
|
||||
let secret_key = SecretKey::new(&mut rand::thread_rng());
|
||||
|
||||
let _ = RethTracer::new()
|
||||
.with_stdout(LayerInfo::new(
|
||||
LogFormat::Terminal,
|
||||
"info".to_string(),
|
||||
LevelFilter::INFO.into(),
|
||||
Some("always".to_string()),
|
||||
))
|
||||
.init();
|
||||
|
||||
// The local address we want to bind to
|
||||
let local_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 30303);
|
||||
|
||||
// The network configuration
|
||||
let net_cfg = NetworkConfig::builder(secret_key)
|
||||
.chain_spec(polygon_chain_spec())
|
||||
.set_head(head())
|
||||
.network_mode(NetworkMode::Work)
|
||||
.listener_addr(local_addr)
|
||||
.build_with_noop_provider();
|
||||
|
||||
// Set Discv4 lookup interval to 1 second
|
||||
let mut discv4_cfg = Discv4ConfigBuilder::default();
|
||||
let interval = Duration::from_secs(1);
|
||||
discv4_cfg.add_boot_nodes(boot_nodes()).lookup_interval(interval);
|
||||
let net_cfg = net_cfg.set_discovery_v4(discv4_cfg.build());
|
||||
|
||||
let net_manager = NetworkManager::new(net_cfg).await.unwrap();
|
||||
|
||||
// The network handle is our entrypoint into the network.
|
||||
let net_handle = net_manager.handle();
|
||||
let mut events = net_handle.event_listener();
|
||||
|
||||
// NetworkManager is a long running task, let's spawn it
|
||||
tokio::spawn(net_manager);
|
||||
info!("Looking for Polygon peers...");
|
||||
|
||||
while let Some(evt) = events.next().await {
|
||||
// For the sake of the example we only print the session established event
|
||||
// with the chain specific details
|
||||
if let NetworkEvent::SessionEstablished { status, client_version, .. } = evt {
|
||||
let chain = status.chain;
|
||||
info!(?chain, ?client_version, "Session established with a new peer.");
|
||||
}
|
||||
// More events here
|
||||
}
|
||||
// We will be disconnected from peers since we are not able to answer to network requests
|
||||
}
|
||||
Reference in New Issue
Block a user