Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add Status encoding and decoding example #15

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ thiserror = "1.0.32"

[dev-dependencies]
hex-literal = "0.3.4"
eyre = "0.6"
anvil = { git = "https://github.com/foundry-rs/foundry" }

# temp
# temp, for foundry
[patch.crates-io]
revm = { git = "https://github.com/onbjerg/revm", branch = "onbjerg/bytecode-hash" }
39 changes: 39 additions & 0 deletions examples/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Construct a [`Status`](ethp2p::Status) and print out its encoding, then decode the [`Status`]
//! message from bytes.

use anvil::Hardfork;
use ethers::types::Chain::Mainnet;
use ethp2p::{EthVersion, Status};
use eyre::Result;
use fastrlp::{Decodable, Encodable};
use foundry_config::Chain;
use hex_literal::hex;
use ruint::uint;

const ETH_GENESIS: [u8; 32] =
hex!("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3");

fn main() -> Result<()> {
let status = Status {
version: EthVersion::Eth67 as u8,
chain: Chain::Named(Mainnet),
total_difficulty: uint!(54928867412924629891081_U256),
blockhash: hex!("6890edf8ad6900a5472c2a7ee3ef795f020ef6f907afb7f4ebf6a92d6aeb1804"),
genesis: ETH_GENESIS,
forkid: Hardfork::Latest.fork_id(),
};

println!("Encoding the following status message:\n{:#?}", status);

let mut encoded_status = vec![];
status.encode(&mut encoded_status);

println!(
"Here is the RLP encoded status message: {}",
hex::encode(&encoded_status)
);

let decoded_status = Status::decode(&mut &encoded_status[..])?;
assert_eq!(decoded_status, status);
Ok(())
}
2 changes: 2 additions & 0 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Debug for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let hexed_blockhash = hex::encode(&self.blockhash);
let hexed_genesis = hex::encode(&self.genesis);
// NOTE: f.alternate() stands for the pretty-print version of debug, {:#?}
if f.alternate() {
write!(
f,
Expand All @@ -73,6 +74,7 @@ impl Debug for Status {
self.forkid
)
} else {
// NOTE: This case will happen for the default version of debug, {:?}
write!(
f,
"Status {{ version: {:?}, chain: {:?}, total_difficulty: {:?}, blockhash: {}, genesis: {}, forkid: {:X?} }}",
Expand Down