signet_test_utils/specs/
ru_spec.rs1use super::{sign_tx_with_key_pair, simple_send};
2#[cfg(doc)]
3use crate::specs::HostBlockSpec;
4use alloy::{
5 consensus::{BlobTransactionSidecar, SidecarBuilder, SimpleCoder, TxEnvelope},
6 eips::eip2718::Encodable2718,
7 primitives::{keccak256, Address, Bytes, B256, U256},
8 rlp::Encodable,
9 signers::local::PrivateKeySigner,
10};
11use signet_constants::test_utils::*;
12use signet_extract::{Extractable, Extracts};
13use signet_types::constants::{KnownChains, ParseChainError, SignetSystemConstants};
14use signet_zenith::Zenith::{self};
15use std::str::FromStr;
16
17#[derive(Debug, Clone)]
26pub struct RuBlockSpec {
27 pub constants: SignetSystemConstants,
29 pub tx: Vec<Vec<u8>>,
31 pub gas_limit: Option<u64>,
33 pub reward_address: Option<Address>,
35}
36
37impl RuBlockSpec {
38 pub const fn new(constants: SignetSystemConstants) -> Self {
40 Self { constants, tx: vec![], gas_limit: None, reward_address: None }
41 }
42
43 pub const fn mainnet() -> Self {
45 Self::new(SignetSystemConstants::mainnet())
46 }
47
48 pub const fn parmigiana() -> Self {
50 Self::new(SignetSystemConstants::parmigiana())
51 }
52
53 pub const fn gouda() -> Self {
55 Self::new(SignetSystemConstants::gouda())
56 }
57
58 #[deprecated(note = "Pecorino is being deprecated in favor of Parmigiana")]
60 #[allow(deprecated)]
61 pub const fn pecorino() -> Self {
62 Self::new(SignetSystemConstants::pecorino())
63 }
64
65 pub const fn test() -> Self {
67 Self::new(SignetSystemConstants::test())
68 }
69
70 pub const fn with_gas_limit(mut self, gas_limit: u64) -> Self {
72 self.gas_limit = Some(gas_limit);
73 self
74 }
75
76 pub const fn with_reward_address(mut self, reward_address: Address) -> Self {
78 self.reward_address = Some(reward_address);
79 self
80 }
81
82 pub fn add_tx(&mut self, tx: &TxEnvelope) {
84 self.tx.push(tx.encoded_2718());
85 }
86
87 pub fn add_alloy_tx(&mut self, tx: &TxEnvelope) {
89 self.tx.push(tx.encoded_2718());
90 }
91
92 pub fn add_invalid_tx(&mut self, tx: impl Into<Bytes>) {
94 self.tx.push(tx.into().into());
95 }
96
97 pub fn tx(mut self, tx: &TxEnvelope) -> Self {
99 self.add_tx(tx);
100 self
101 }
102
103 pub fn alloy_tx(mut self, tx: &TxEnvelope) -> Self {
105 self.add_alloy_tx(tx);
106 self
107 }
108
109 pub fn add_simple_send(
111 &mut self,
112 wallet: &PrivateKeySigner,
113 to: Address,
114 amount: U256,
115 nonce: u64,
116 ) -> TxEnvelope {
117 let tx = sign_tx_with_key_pair(
118 wallet,
119 simple_send(to, amount, nonce, self.constants.ru_chain_id()),
120 );
121 self.add_tx(&tx);
122 tx
123 }
124
125 pub fn to_sidecar(&self) -> (B256, BlobTransactionSidecar) {
127 let mut buf = vec![];
128 Vec::<Vec<u8>>::encode(&self.tx, &mut buf);
129
130 let sidecar = SidecarBuilder::<SimpleCoder>::from_slice(&buf).build().unwrap();
131 (keccak256(&buf), sidecar)
132 }
133
134 pub fn to_block_submitted(&self) -> (Zenith::BlockSubmitted, BlobTransactionSidecar) {
136 let (bdh, sidecar) = self.to_sidecar();
137
138 let block_submitted = Zenith::BlockSubmitted {
139 sequencer: Address::repeat_byte(3),
140 rollupChainId: U256::from(self.constants.ru_chain_id()),
141 gasLimit: U256::from(self.gas_limit.unwrap_or(100_000_000)),
142 rewardAddress: self.reward_address.unwrap_or(DEFAULT_REWARD_ADDRESS),
143 blockDataHash: bdh,
144 };
145
146 (block_submitted, sidecar)
147 }
148
149 pub fn assert_conforms<C: Extractable>(&self, extracts: &Extracts<'_, C>) {
151 let submitted = extracts.submitted.as_ref().unwrap();
152
153 if let Some(gas_limit) = self.gas_limit {
154 assert_eq!(submitted.gas_limit(), gas_limit)
155 }
156
157 if let Some(reward_address) = self.reward_address {
158 assert_eq!(submitted.reward_address(), reward_address)
159 }
160 }
161}
162
163impl TryFrom<KnownChains> for RuBlockSpec {
164 type Error = ParseChainError;
165
166 fn try_from(chain: KnownChains) -> Result<Self, Self::Error> {
167 match chain {
168 KnownChains::Mainnet => Ok(Self::mainnet()),
169 KnownChains::Parmigiana => Ok(Self::parmigiana()),
170 KnownChains::Gouda => Ok(Self::gouda()),
171 #[allow(deprecated)]
172 KnownChains::Pecorino => Ok(Self::pecorino()),
173 KnownChains::Test => Ok(Self::test()),
174 }
175 }
176}
177
178impl FromStr for RuBlockSpec {
179 type Err = ParseChainError;
180
181 fn from_str(s: &str) -> Result<Self, Self::Err> {
182 s.parse::<KnownChains>()?.try_into()
183 }
184}