Skip to content

Commit

Permalink
chore: reorder bincode modules (alloy-rs#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 18, 2024
1 parent a75979d commit bf35895
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 157 deletions.
66 changes: 33 additions & 33 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,39 +781,6 @@ impl BlockHeader for Header {
}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;

#[test]
fn test_header_serde_json_roundtrip() {
let raw = r#"{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","ommersHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","beneficiary":"0x0000000000000000000000000000000000000000","stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x0","gasLimit":"0x0","gasUsed":"0x0","timestamp":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x1","extraData":"0x"}"#;
let header = Header {
base_fee_per_gas: Some(1),
withdrawals_root: Some(EMPTY_ROOT_HASH),
..Default::default()
};

let encoded = serde_json::to_string(&header).unwrap();
assert_eq!(encoded, raw);

let decoded: Header = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded, header);

// Create a vector to store the encoded RLP
let mut encoded_rlp = Vec::new();

// Encode the header data
decoded.encode(&mut encoded_rlp);

// Decode the RLP data
let decoded_rlp = Header::decode(&mut encoded_rlp.as_slice()).unwrap();

// Check that the decoded RLP data matches the original header data
assert_eq!(decoded_rlp, decoded);
}
}

/// Bincode-compatibl [`Header`] serde implementation.
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(super) mod serde_bincode_compat {
Expand Down Expand Up @@ -972,3 +939,36 @@ pub(super) mod serde_bincode_compat {
}
}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;

#[test]
fn test_header_serde_json_roundtrip() {
let raw = r#"{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","ommersHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","beneficiary":"0x0000000000000000000000000000000000000000","stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x0","gasLimit":"0x0","gasUsed":"0x0","timestamp":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x1","extraData":"0x"}"#;
let header = Header {
base_fee_per_gas: Some(1),
withdrawals_root: Some(EMPTY_ROOT_HASH),
..Default::default()
};

let encoded = serde_json::to_string(&header).unwrap();
assert_eq!(encoded, raw);

let decoded: Header = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded, header);

// Create a vector to store the encoded RLP
let mut encoded_rlp = Vec::new();

// Encode the header data
decoded.encode(&mut encoded_rlp);

// Decode the RLP data
let decoded_rlp = Header::decode(&mut encoded_rlp.as_slice()).unwrap();

// Check that the decoded RLP data matches the original header data
assert_eq!(decoded_rlp, decoded);
}
}
144 changes: 72 additions & 72 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,78 +378,6 @@ impl Decodable for TxEip1559 {
}
}

#[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, 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: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
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: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
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);
}
}

/// Bincode-compatible [`TxEip1559`] serde implementation.
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(super) mod serde_bincode_compat {
Expand Down Expand Up @@ -569,3 +497,75 @@ pub(super) mod serde_bincode_compat {
}
}
}

#[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, 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: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
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: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
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);
}
}
104 changes: 52 additions & 52 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,58 +344,6 @@ impl Decodable for TxLegacy {
}
}

#[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");
}
}

/// Bincode-compatible [`TxLegacy`] serde implementation.
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(super) mod serde_bincode_compat {
Expand Down Expand Up @@ -509,3 +457,55 @@ pub(super) mod serde_bincode_compat {
}
}
}

#[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");
}
}

0 comments on commit bf35895

Please sign in to comment.