alloy_eips/
eip7251.rs

1//! Contains consolidation types, first introduced in the [Prague hardfork](https://github.com/ethereum/execution-apis/blob/main/src/engine/prague.md).
2//!
3//! See also [EIP-7251](https://eips.ethereum.org/EIPS/eip-7251): Increase the MAX_EFFECTIVE_BALANCE
4
5use alloy_primitives::{address, bytes, Address, Bytes, FixedBytes};
6
7/// The address for the EIP-7251 consolidation requests contract.
8pub const CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS: Address =
9    address!("0x0000BBdDc7CE488642fb579F8B00f3a590007251");
10
11/// The code for the EIP-7251 consolidation requests contract.
12pub static CONSOLIDATION_REQUEST_PREDEPLOY_CODE: Bytes = bytes!("3373fffffffffffffffffffffffffffffffffffffffe1460d35760115f54807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1461019a57600182026001905f5b5f82111560685781019083028483029004916001019190604d565b9093900492505050366060146088573661019a573461019a575f5260205ff35b341061019a57600154600101600155600354806004026004013381556001015f358155600101602035815560010160403590553360601b5f5260605f60143760745fa0600101600355005b6003546002548082038060021160e7575060025b5f5b8181146101295782810160040260040181607402815460601b815260140181600101548152602001816002015481526020019060030154905260010160e9565b910180921461013b5790600255610146565b90505f6002555f6003555b5f54807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff141561017357505f5b6001546001828201116101885750505f61018e565b01600190035b5f555f6001556074025ff35b5f5ffd0000");
13
14/// The [EIP-7685](https://eips.ethereum.org/EIPS/eip-7685) request type for consolidation requests.
15pub const CONSOLIDATION_REQUEST_TYPE: u8 = 0x02;
16
17/// The [EIP-7251](https://eips.ethereum.org/EIPS/eip-7251) defined maximum number of consolidation requests per block.
18pub const MAX_CONSOLIDATION_REQUESTS_PER_BLOCK: usize = 2;
19
20/// This structure maps onto the consolidation request object from [EIP-7251](https://eips.ethereum.org/EIPS/eip-7251).
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23#[cfg_attr(feature = "ssz", derive(ssz_derive::Encode, ssz_derive::Decode))]
24#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
25pub struct ConsolidationRequest {
26    /// Source address
27    pub source_address: Address,
28    /// Source public key
29    pub source_pubkey: FixedBytes<48>,
30    /// Target public key
31    pub target_pubkey: FixedBytes<48>,
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use alloy_primitives::hex;
38    use core::str::FromStr;
39
40    #[test]
41    #[cfg(feature = "serde")]
42    fn test_serde_consolidation_request() {
43        // Sample JSON input representing a consolidation request
44        let json_data = r#"{
45            "source_address":"0x007eABCA654E67103dF02f49EbdC5f6Cd9387a07",
46            "source_pubkey":"0xb13ff174911d0137e5f2b739fbf172b22cba35a037ef1edb03683b75c9abf5b271f8d48ad279cc89c7fae91db631c1e7",
47            "target_pubkey":"0xd0e5be6b709f2dc02a49f6e37e0d03b7d832b79b0db1c8bbfd5b81b8e57b79a1282fb99a671b4629a0e0bfffa7cf6d4f"
48        }"#;
49
50        // Deserialize the JSON into a ConsolidationRequest struct
51        let consolidation_request: ConsolidationRequest =
52            serde_json::from_str(json_data).expect("Failed to deserialize");
53
54        // Verify the deserialized content
55        assert_eq!(
56            consolidation_request.source_address,
57            Address::from_str("0x007eABCA654E67103dF02f49EbdC5f6Cd9387a07").unwrap()
58        );
59        assert_eq!(
60            consolidation_request.source_pubkey,
61            FixedBytes::<48>::from(hex!("b13ff174911d0137e5f2b739fbf172b22cba35a037ef1edb03683b75c9abf5b271f8d48ad279cc89c7fae91db631c1e7"))
62        );
63        assert_eq!(
64            consolidation_request.target_pubkey,
65            FixedBytes::<48>::from(hex!("d0e5be6b709f2dc02a49f6e37e0d03b7d832b79b0db1c8bbfd5b81b8e57b79a1282fb99a671b4629a0e0bfffa7cf6d4f"))
66        );
67
68        // Serialize the struct back into JSON
69        let serialized_json =
70            serde_json::to_string(&consolidation_request).expect("Failed to serialize");
71
72        // Check if the serialized JSON matches the expected JSON structure
73        let expected_json = r#"{"source_address":"0x007eabca654e67103df02f49ebdc5f6cd9387a07","source_pubkey":"0xb13ff174911d0137e5f2b739fbf172b22cba35a037ef1edb03683b75c9abf5b271f8d48ad279cc89c7fae91db631c1e7","target_pubkey":"0xd0e5be6b709f2dc02a49f6e37e0d03b7d832b79b0db1c8bbfd5b81b8e57b79a1282fb99a671b4629a0e0bfffa7cf6d4f"}"#;
74        assert_eq!(serialized_json, expected_json);
75    }
76}