1use std::time::Duration;
2
3use alloy::{
4 contract::SolCallBuilder,
5 primitives::U256,
6 providers::{Provider, ProviderBuilder},
7 rpc::types::TransactionReceipt,
8 sol_types::{GenericContractError, SolCall},
9};
10use anyhow::anyhow;
11use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
12use committable::{Commitment, Committable};
13use tokio::time::sleep;
14use url::Url;
15
16pub mod logging;
17pub mod ser;
18pub mod test_utils;
19
20pub async fn wait_for_http(
21 url: &Url,
22 interval: Duration,
23 max_retries: usize,
24) -> Result<usize, String> {
25 for i in 0..(max_retries + 1) {
26 let res = surf::get(url).await;
27 if res.is_ok() {
28 tracing::debug!("Connected to {url}");
29 return Ok(i);
30 }
31 tracing::debug!("Waiting for {url}, retrying in {interval:?}");
32 sleep(interval).await;
33 }
34 Err(format!("Url {url:?} not available."))
35}
36
37pub async fn wait_for_rpc(
38 url: &Url,
39 interval: Duration,
40 max_retries: usize,
41) -> Result<usize, String> {
42 let retries = wait_for_http(url, interval, max_retries).await?;
43 let client = ProviderBuilder::new().on_http(url.clone());
44 for i in retries..(max_retries + 1) {
45 if client.get_block_number().await.is_ok() {
46 tracing::debug!("JSON-RPC ready at {url}");
47 return Ok(i);
48 }
49 tracing::debug!("Waiting for JSON-RPC at {url}, retrying in {interval:?}");
50 sleep(interval).await;
51 }
52
53 Err(format!("No JSON-RPC at {url}"))
54}
55
56pub fn commitment_to_u256<T: Committable>(comm: Commitment<T>) -> U256 {
58 let mut buf = vec![];
59 comm.serialize_uncompressed(&mut buf).unwrap();
60 U256::from_le_slice(&buf)
61}
62
63pub fn u256_to_commitment<T: Committable>(comm: U256) -> Result<Commitment<T>, SerializationError> {
65 Commitment::deserialize_uncompressed_unchecked(&*comm.to_le_bytes_vec())
66}
67
68#[macro_export]
70macro_rules! impl_to_fixed_bytes {
71 ($struct_name:ident, $type:ty) => {
72 impl $struct_name {
73 pub(crate) fn to_fixed_bytes(self) -> [u8; core::mem::size_of::<$type>()] {
74 let bytes: [u8; core::mem::size_of::<$type>()] = self.0.to_le_bytes();
75 bytes
76 }
77 }
78 };
79}
80
81pub async fn contract_send<T, P, C>(
88 call: &SolCallBuilder<T, P, C>,
89) -> Result<(TransactionReceipt, u64), anyhow::Error>
90where
91 P: Provider,
92 C: SolCall,
93{
94 let pending = match call.send().await {
95 Ok(pending) => pending,
96 Err(err) => {
97 if let Some(e) = err.as_decoded_interface_error::<GenericContractError>() {
98 tracing::error!("contract err: {:?}", e);
99 }
100 return Err(anyhow!("error sending transaction: {:?}", err));
101 },
102 };
103
104 let hash = pending.tx_hash().to_owned();
105 tracing::info!("submitted contract call 0x{:x}", hash);
106
107 let receipt = match pending.get_receipt().await {
108 Ok(r) => r,
109 Err(err) => {
110 return Err(anyhow!(
111 "contract call 0x{hash:x}: error getting transaction receipt: {err}"
112 ))
113 },
114 };
115
116 let block_number = receipt
119 .block_number
120 .expect("transaction mined but block number not set");
121 Ok((receipt, block_number))
122}
123
124#[cfg(test)]
125mod test {
126 use alloy::{primitives::I256, sol};
127 use anyhow::Result;
128 use committable::RawCommitmentBuilder;
129 use test_utils::setup_test;
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 #[tokio::test]
190 async fn test_contract_send() -> Result<()> {
191 setup_test();
192 let provider = ProviderBuilder::new().on_anvil_with_wallet();
193 let contract = CounterWithError::deploy(provider.clone()).await?;
194
195 let inc_call = contract.increment();
197 let (receipt, block_num) = contract_send(&inc_call).await?;
198 assert_eq!(block_num, 2); assert!(receipt.inner.is_success());
200 assert_eq!(contract.counter().call().await?.counter, I256::ONE);
201
202 let revert_call = contract.revertA();
204 assert!(contract_send(&revert_call).await.is_err());
205
206 Ok(())
207 }
208}