1use std::time::Duration;
2
3use alloy::{
4 contract::SolCallBuilder,
5 network::ReceiptResponse,
6 primitives::U256,
7 providers::{Provider, ProviderBuilder},
8 rpc::types::TransactionReceipt,
9 sol_types::{GenericContractError, SolCall},
10};
11use anyhow::anyhow;
12use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
13use committable::{Commitment, Committable};
14use tokio::time::sleep;
15use url::Url;
16
17pub mod logging;
18pub mod ser;
19pub mod test_utils;
20
21pub async fn wait_for_http(
22 url: &Url,
23 interval: Duration,
24 max_retries: usize,
25) -> Result<usize, String> {
26 for i in 0..(max_retries + 1) {
27 let res = surf::get(url).await;
28 if res.is_ok() {
29 tracing::debug!("Connected to {url}");
30 return Ok(i);
31 }
32 tracing::debug!("Waiting for {url}, retrying in {interval:?}");
33 sleep(interval).await;
34 }
35 Err(format!("Url {url:?} not available."))
36}
37
38pub async fn wait_for_rpc(
39 url: &Url,
40 interval: Duration,
41 max_retries: usize,
42) -> Result<usize, String> {
43 let retries = wait_for_http(url, interval, max_retries).await?;
44 let client = ProviderBuilder::new().connect_http(url.clone());
45 for i in retries..(max_retries + 1) {
46 if client.get_block_number().await.is_ok() {
47 tracing::debug!("JSON-RPC ready at {url}");
48 return Ok(i);
49 }
50 tracing::debug!("Waiting for JSON-RPC at {url}, retrying in {interval:?}");
51 sleep(interval).await;
52 }
53
54 Err(format!("No JSON-RPC at {url}"))
55}
56
57pub fn commitment_to_u256<T: Committable>(comm: Commitment<T>) -> U256 {
59 let mut buf = vec![];
60 comm.serialize_uncompressed(&mut buf).unwrap();
61 U256::from_le_slice(&buf)
62}
63
64pub fn u256_to_commitment<T: Committable>(comm: U256) -> Result<Commitment<T>, SerializationError> {
66 Commitment::deserialize_uncompressed_unchecked(&*comm.to_le_bytes_vec())
67}
68
69#[macro_export]
71macro_rules! impl_to_fixed_bytes {
72 ($struct_name:ident, $type:ty) => {
73 impl $struct_name {
74 pub(crate) fn to_fixed_bytes(self) -> [u8; core::mem::size_of::<$type>()] {
75 let bytes: [u8; core::mem::size_of::<$type>()] = self.0.to_le_bytes();
76 bytes
77 }
78 }
79 };
80}
81
82pub async fn contract_send<P, C>(
89 call: &SolCallBuilder<P, C>,
90) -> Result<(TransactionReceipt, u64), anyhow::Error>
91where
92 P: Provider,
93 C: SolCall,
94{
95 let pending = match call.send().await {
96 Ok(pending) => pending,
97 Err(err) => {
98 if let Some(e) = err.as_decoded_interface_error::<GenericContractError>() {
99 tracing::error!("contract err: {:?}", e);
100 }
101 return Err(anyhow!("error sending transaction: {:?}", err));
102 },
103 };
104
105 let hash = pending.tx_hash().to_owned();
106 tracing::info!("submitted contract call 0x{:x}", hash);
107
108 let receipt = match pending.get_receipt().await {
109 Ok(r) => r,
110 Err(err) => {
111 return Err(anyhow!(
112 "contract call 0x{hash:x}: error getting transaction receipt: {err}"
113 ))
114 },
115 };
116
117 let block_number = receipt
120 .block_number()
121 .expect("transaction mined but block number not set");
122 Ok((receipt, block_number))
123}
124
125#[cfg(test)]
126mod test {
127 use alloy::{primitives::I256, sol};
128 use anyhow::Result;
129 use committable::RawCommitmentBuilder;
130
131 use super::*;
132
133 sol! {
135 #[allow(missing_docs)]
136 #[sol(rpc, bytecode = "608060405260008055348015601357600080fd5b506103e9806100236000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632baeceb71461005c5780632ccbdbca1461006657806361bc221a14610070578063c3e8b5ca1461008e578063d09de08a14610098575b600080fd5b6100646100a2565b005b61006e610103565b005b61007861013e565b60405161008591906101f9565b60405180910390f35b610096610144565b005b6100a061017f565b005b60016000808282546100b49190610243565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167fdc69c403b972fc566a14058b3b18e1513da476de6ac475716e489fae0cbe4a2660405160405180910390a3565b6040517f23b0db14000000000000000000000000000000000000000000000000000000008152600401610135906102e3565b60405180910390fd5b60005481565b6040517fa5f9ec670000000000000000000000000000000000000000000000000000000081526004016101769061034f565b60405180910390fd5b6001600080828254610191919061036f565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167ff6d1d8d205b41f9fb9549900a8dba5d669d68117a3a2b88c1ebc61163e8117ba60405160405180910390a3565b6000819050919050565b6101f3816101e0565b82525050565b600060208201905061020e60008301846101ea565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061024e826101e0565b9150610259836101e0565b92508282039050818112600084121682821360008512151617156102805761027f610214565b5b92915050565b600082825260208201905092915050565b7f4572726f72204100000000000000000000000000000000000000000000000000600082015250565b60006102cd600783610286565b91506102d882610297565b602082019050919050565b600060208201905081810360008301526102fc816102c0565b9050919050565b7f4572726f72204200000000000000000000000000000000000000000000000000600082015250565b6000610339600783610286565b915061034482610303565b602082019050919050565b600060208201905081810360008301526103688161032c565b9050919050565b600061037a826101e0565b9150610385836101e0565b9250828201905082811215600083121683821260008412151617156103ad576103ac610214565b5b9291505056fea2646970667358221220a878a3c1da1a1170e4496cdbc63bd5ed1587374bcd6cf6d4f1d5b88fa981795d64736f6c63430008190033")]
137 contract CounterWithError {
138 int256 public counter = 0;
139
140 #[derive(Debug)]
142 event Increment(address indexed by, int256 indexed value);
143 #[derive(Debug)]
144 event Decrement(address indexed by, int256 indexed value);
145
146 #[derive(Debug)]
148 error ErrorA(string message);
149 #[derive(Debug)]
150 error ErrorB(string message);
151
152 function increment() public {
154 counter += 1;
155 emit Increment(msg.sender, counter);
156 }
157
158 function decrement() public {
159 counter -= 1;
160 emit Decrement(msg.sender, counter);
161 }
162
163 function revertA() public pure {
164 revert ErrorA("Error A");
165 }
166
167 function revertB() public pure {
168 revert ErrorB("Error B");
169 }
170 }
171 }
172
173 struct TestCommittable;
174
175 impl Committable for TestCommittable {
176 fn commit(&self) -> Commitment<Self> {
177 RawCommitmentBuilder::new("TestCommittable").finalize()
178 }
179 }
180
181 #[test]
182 fn test_commitment_to_u256_round_trip() {
183 assert_eq!(
184 TestCommittable.commit(),
185 u256_to_commitment(commitment_to_u256(TestCommittable.commit())).unwrap()
186 );
187 }
188
189 #[test_log::test(tokio::test)]
190 async fn test_contract_send() -> Result<()> {
191 let provider = ProviderBuilder::new().connect_anvil_with_wallet();
192 let contract = CounterWithError::deploy(provider.clone()).await?;
193
194 let inc_call = contract.increment();
196 let (receipt, block_num) = contract_send(&inc_call).await?;
197 assert_eq!(block_num, 2); assert!(receipt.inner.is_success());
199 assert_eq!(contract.counter().call().await?, I256::ONE);
200
201 let revert_call = contract.revertA();
203 assert!(contract_send(&revert_call).await.is_err());
204
205 Ok(())
206 }
207}