Skip to content

Commit

Permalink
arbitrum-client: fix build errors & warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Nov 21, 2023
1 parent efcd729 commit 3c5c76c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 59 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions arbitrum-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ ark-ff = "0.4.0"
alloy-primitives = "0.3.1"
alloy-sol-types = "0.3.1"
num-bigint = { workspace = true }
num-traits = "0.2"
lazy_static = "1.4.0"

# === Workspace Dependencies === #
constants = { path = "../constants" }
circuit-types = { path = "../circuit-types" }
common = { path = "../common" }
renegade-crypto = { path = "../renegade-crypto" }

# === Serde === #
serde = { workspace = true }
Expand Down
50 changes: 27 additions & 23 deletions arbitrum-client/src/client/event_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
//! emitted by the darkpool contract
use alloy_sol_types::SolCall;
use circuit_types::{traits::BaseType, SizedWalletShare};
use common::types::merkle::{MerkleAuthenticationPath, MerkleTreeCoords};
use circuit_types::SizedWalletShare;
use common::types::merkle::MerkleAuthenticationPath;
use constants::{Scalar, MERKLE_HEIGHT};
use ethers::types::TxHash;
use ethers::{
abi::AbiEncode,
middleware::Middleware,
types::{TxHash, H256},
};
use num_bigint::BigUint;
use num_traits::ToPrimitive;

use crate::{
abi::{
Expand All @@ -16,14 +21,10 @@ use crate::{
constants::{DEFAULT_AUTHENTICATION_PATH, SELECTOR_LEN},
errors::ArbitrumClientError,
helpers::{
deserialize_calldata, keccak_hash_scalar, parse_shares_from_new_wallet,
parse_shares_from_process_match_settle, parse_shares_from_update_wallet,
keccak_hash_scalar, parse_shares_from_new_wallet, parse_shares_from_process_match_settle,
parse_shares_from_update_wallet,
},
serde_def_types::SerdeScalarField,
types::{
MatchPayload, ValidMatchSettleStatement, ValidWalletCreateStatement,
ValidWalletUpdateStatement,
},
};

use super::ArbitrumClient;
Expand Down Expand Up @@ -62,31 +63,33 @@ impl ArbitrumClient {
// Construct a set that holds pairs of (depth, index) values in the
// authentication path; i.e. the tree coordinates of the sibling nodes
// in the authentication path
let mut authentication_path_coords =
let authentication_path_coords =
MerkleAuthenticationPath::construct_path_coords(leaf_index.clone(), MERKLE_HEIGHT);

// For each coordinate in the authentication path,
// find the last value it was updated to
let mut path = *DEFAULT_AUTHENTICATION_PATH;
for coords in authentication_path_coords {
let height = H256::from_slice((coords.height as u8).encode().as_slice());
let index = H256::from_slice(coords.index.to_u128().unwrap().encode().as_slice());
let events = self
.darkpool_event_source
.event::<NodeChangedFilter>()
.topic1(coords.height.into())
.topic2(coords.index.into())
.topic1(height)
.topic2(index)
.from_block(self.deploy_block)
.query()
.await
.map_err(|e| ArbitrumClientError::EventQuerying(e.to_string()))?;

let value = events.last().map(|event| {
postcard::from_bytes::<SerdeScalarField>(&event.new_value)
.map_err(|e| ArbitrumClientError::Serde(e.to_string()))?
.0 // Scalar
.map_err(|e| ArbitrumClientError::Serde(e.to_string()))
.map(|s| s.0 /* Scalar */)
});

if let Some(value) = value {
path[MERKLE_HEIGHT - coords.height] = value;
path[MERKLE_HEIGHT - coords.height] = Scalar::new(value?);
}
}

Expand Down Expand Up @@ -131,17 +134,18 @@ impl ArbitrumClient {
.client()
.get_transaction(tx_hash)
.await
.map_err(|e| ArbitrumClientError::TxNotFound(e.to_string()))?;
.map_err(|e| ArbitrumClientError::TxQuerying(e.to_string()))?
.ok_or(ArbitrumClientError::TxNotFound(tx_hash.to_string()))?;

let calldata: Vec<u8> = tx.input.into();
let selector = &calldata[..SELECTOR_LEN];
let calldata: Vec<u8> = tx.input.to_vec();
let selector: [u8; 4] = calldata[..SELECTOR_LEN].try_into().unwrap();
match selector {
&<newWalletCall as SolCall>::SELECTOR => parse_shares_from_new_wallet(&calldata),
&<updateWalletCall as SolCall>::SELECTOR => parse_shares_from_update_wallet(&calldata),
&<processMatchSettleCall as SolCall>::SELECTOR => {
parse_shares_from_process_match_settle(&calldata)
<newWalletCall as SolCall>::SELECTOR => parse_shares_from_new_wallet(&calldata),
<updateWalletCall as SolCall>::SELECTOR => parse_shares_from_update_wallet(&calldata),
<processMatchSettleCall as SolCall>::SELECTOR => {
parse_shares_from_process_match_settle(&calldata, public_blinder_share)
},
_ => return Err(ArbitrumClientError::InvalidSelector),
_ => Err(ArbitrumClientError::InvalidSelector),
}
}
}
4 changes: 2 additions & 2 deletions arbitrum-client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ethers::{

use crate::{
abi::{DarkpoolContract, DarkpoolEventSource},
constants::{Chain, DEVNET_RPC_URL, TESTNET_RPC_URL},
constants::Chain,
errors::{ArbitrumClientConfigError, ArbitrumClientError},
};

Expand Down Expand Up @@ -60,7 +60,7 @@ impl ArbitrumClientConfig {
/// Constructs an RPC client capable of signing transactions from the
/// configuration
async fn get_rpc_client(&self) -> Result<Arc<SignerHttpProvider>, ArbitrumClientConfigError> {
let provider = Provider::<Http>::try_from(self.rpc_url)
let provider = Provider::<Http>::try_from(&self.rpc_url)
.map_err(|e| ArbitrumClientConfigError::RpcClientInitialization(e.to_string()))?;

let wallet = LocalWallet::from_str(&self.arb_priv_key)
Expand Down
31 changes: 15 additions & 16 deletions arbitrum-client/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
use std::marker::PhantomData;

use ark_ff::BigInt;
use ark_ff::{BigInt, Fp};
use constants::{Scalar, MERKLE_HEIGHT};
use lazy_static::lazy_static;
use num_bigint::BigUint;
use renegade_crypto::hash::compute_poseidon_hash;

/// The chain environment
Expand All @@ -19,20 +18,6 @@ pub enum Chain {
Devnet,
}

/// The value of an empty leaf in the Merkle tree,
/// computed as the Keccak-256 hash of the string "renegade",
/// reduced modulo the scalar field order when interpreted as a
/// big-endian unsigned integer
pub const EMPTY_LEAF_VALUE: Scalar = Scalar(Fp(
BigInt([
14542100412480080699,
1005430062575839833,
8810205500711505764,
2121377557688093532,
]),
PhantomData,
));

/// The number of bytes in a Solidity function selector
pub const SELECTOR_LEN: usize = 4;

Expand All @@ -41,6 +26,20 @@ lazy_static! {
// | Merkle Tree Metadata |
// ------------------------

/// The value of an empty leaf in the Merkle tree,
/// computed as the Keccak-256 hash of the string "renegade",
/// reduced modulo the scalar field order when interpreted as a
/// big-endian unsigned integer
pub static ref EMPTY_LEAF_VALUE: Scalar = Scalar::new(Fp(
BigInt([
14542100412480080699,
1005430062575839833,
8810205500711505764,
2121377557688093532,
]),
PhantomData,
));

/// The default values of an authentication path; i.e. the values in the path before any
/// path elements are changed by insertions
///
Expand Down
2 changes: 2 additions & 0 deletions arbitrum-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum ArbitrumClientError {
EventQuerying(String),
/// Error thrown when a commitment can't be found in the Merkle tree
CommitmentNotFound,
/// Error thrown when getting a transaction fails
TxQuerying(String),
/// Error thrown when a transaction can't be found
TxNotFound(String),
/// Error thrown when a transaction's selector doesn't match
Expand Down
46 changes: 28 additions & 18 deletions arbitrum-client/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,60 +40,70 @@ pub fn keccak_hash_scalar(scalar: Scalar) -> Result<H256, ArbitrumClientError> {
Ok(keccak256(scalar_bytes).into())
}

/// Parses wallet shares from the calldata of a `newWallet` call
pub fn parse_shares_from_new_wallet(
calldata: &[u8],
) -> Result<SizedWalletShare, ArbitrumClientError> {
let call = newWalletCall::decode(&calldata, true /* validate */)
let call = newWalletCall::decode(calldata, true /* validate */)
.map_err(|e| ArbitrumClientError::Serde(e.to_string()))?;

let mut statement = deserialize_calldata::<ValidWalletCreateStatement>(
let statement = deserialize_calldata::<ValidWalletCreateStatement>(
&call.valid_wallet_create_statement_bytes.into(),
)?;

Ok(SizedWalletShare::from_scalars(
&mut statement.public_wallet_shares,
))
let mut shares = statement.public_wallet_shares.into_iter().map(Scalar::new);

Ok(SizedWalletShare::from_scalars(&mut shares))
}

/// Parses wallet shares from the calldata of an `updateWallet` call
pub fn parse_shares_from_update_wallet(
calldata: &[u8],
) -> Result<SizedWalletShare, ArbitrumClientError> {
let call = updateWalletCall::decode(&calldata, true /* validate */)
let call = updateWalletCall::decode(calldata, true /* validate */)
.map_err(|e| ArbitrumClientError::Serde(e.to_string()))?;

let mut statement = deserialize_calldata::<ValidWalletUpdateStatement>(
let statement = deserialize_calldata::<ValidWalletUpdateStatement>(
&call.valid_wallet_update_statement_bytes.into(),
)?;

Ok(SizedWalletShare::from_scalars(
&mut statement.new_public_shares,
))
let mut shares = statement.new_public_shares.into_iter().map(Scalar::new);

Ok(SizedWalletShare::from_scalars(&mut shares))
}

/// Parses wallet shares from the calldata of a `processMatchSettle` call
pub fn parse_shares_from_process_match_settle(
calldata: &[u8],
public_blinder_share: Scalar,
) -> Result<SizedWalletShare, ArbitrumClientError> {
let call = processMatchSettleCall::decode(&calldata, true /* validate */)
let call = processMatchSettleCall::decode(calldata, true /* validate */)
.map_err(|e| ArbitrumClientError::Serde(e.to_string()))?;

let party_0_match_payload =
deserialize_calldata::<MatchPayload>(&call.party_0_match_payload.into())?;
let party_1_match_payload =
deserialize_calldata::<MatchPayload>(&call.party_1_match_payload.into())?;

let mut valid_match_settle_statement = deserialize_calldata::<ValidMatchSettleStatement>(
let valid_match_settle_statement = deserialize_calldata::<ValidMatchSettleStatement>(
&call.valid_match_settle_statement_bytes.into(),
)?;

let target_share = public_blinder_share.inner();
if party_0_match_payload.wallet_blinder_share == target_share {
Ok(SizedWalletShare::from_scalars(
&mut valid_match_settle_statement.party0_modified_shares,
))
let mut shares = valid_match_settle_statement
.party0_modified_shares
.into_iter()
.map(Scalar::new);

Ok(SizedWalletShare::from_scalars(&mut shares))
} else if party_1_match_payload.wallet_blinder_share == target_share {
Ok(SizedWalletShare::from_scalars(
&mut valid_match_settle_statement.party1_modified_shares,
))
let mut shares = valid_match_settle_statement
.party1_modified_shares
.into_iter()
.map(Scalar::new);

Ok(SizedWalletShare::from_scalars(&mut shares))
} else {
Err(ArbitrumClientError::BlinderNotFound)
}
Expand Down

0 comments on commit 3c5c76c

Please sign in to comment.