Skip to content

Commit

Permalink
Get processor signer/wallet tests working for Ethereum
Browse files Browse the repository at this point in the history
They are handicapped by the fact Ethereum self-sends don't show up as outputs,
yet that's fundamental (unless we add a *harmful* fallback function).
  • Loading branch information
kayabaNerve committed May 11, 2024
1 parent 02c4417 commit d27d934
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 142 deletions.
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ allow-git = [
"https://github.com/serai-dex/substrate",
"https://github.com/alloy-rs/alloy",
"https://github.com/monero-rs/base58-monero",
"https://github.com/kayabaNerve/dockertest-rs",
"https://github.com/orcalabs/dockertest-rs",
]
4 changes: 2 additions & 2 deletions processor/src/multisigs/scheduler/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<N: Network<Scheduler = Self>> SchedulerTrait<N> for Scheduler<N> {
assert!(self.coins.contains(&utxo.balance().coin));
}

let mut nonce = LastNonce::get(txn).map_or(0, |nonce| nonce + 1);
let mut nonce = LastNonce::get(txn).map_or(1, |nonce| nonce + 1);
let mut plans = vec![];
for chunk in payments.as_slice().chunks(N::MAX_OUTPUTS) {
// Once we rotate, all further payments should be scheduled via the new multisig
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<N: Network<Scheduler = Self>> SchedulerTrait<N> for Scheduler<N> {
.and_then(|key_bytes| <N::Curve as Ciphersuite>::read_G(&mut key_bytes.as_slice()).ok())
.unwrap_or(self.key);

let nonce = LastNonce::get(txn).map_or(0, |nonce| nonce + 1);
let nonce = LastNonce::get(txn).map_or(1, |nonce| nonce + 1);
LastNonce::set(txn, &(nonce + 1));
Plan {
key: current_key,
Expand Down
110 changes: 58 additions & 52 deletions processor/src/networks/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,22 +719,6 @@ impl<D: Db> Network for Ethereum<D> {
// Publish this using a dummy account we fund with magic RPC commands
#[cfg(test)]
{
use rand_core::OsRng;
use ciphersuite::group::ff::Field;

let key = <Secp256k1 as Ciphersuite>::F::random(&mut OsRng);
let address = ethereum_serai::crypto::address(&(Secp256k1::generator() * key));

// Set a 1.1 ETH balance
self
.provider
.raw_request::<_, ()>(
"anvil_setBalance".into(),
[Address(address).to_string(), "1100000000000000000".into()],
)
.await
.unwrap();

let router = self.router().await;
let router = router.as_ref().unwrap();

Expand All @@ -747,17 +731,30 @@ impl<D: Db> Network for Ethereum<D> {
completion.signature(),
),
};
tx.gas_price = 100_000_000_000u128;
tx.gas_limit = 1_000_000u64.into();
tx.gas_price = 1_000_000_000u64.into();
let tx = ethereum_serai::crypto::deterministically_sign(&tx);

use ethereum_serai::alloy_consensus::SignableTransaction;
let sig =
k256::ecdsa::SigningKey::from(k256::elliptic_curve::NonZeroScalar::new(key).unwrap())
.sign_prehash_recoverable(tx.signature_hash().as_ref())
if self.provider.get_transaction_by_hash(*tx.hash()).await.unwrap().is_none() {
self
.provider
.raw_request::<_, ()>(
"anvil_setBalance".into(),
[
tx.recover_signer().unwrap().to_string(),
(U256::from(tx.tx().gas_limit) * U256::from(tx.tx().gas_price)).to_string(),
],
)
.await
.unwrap();

let mut bytes = vec![];
tx.encode_with_signature_fields(&sig.into(), &mut bytes);
let _ = self.provider.send_raw_transaction(&bytes).await.ok().unwrap();
let (tx, sig, _) = tx.into_parts();
let mut bytes = vec![];
tx.encode_with_signature_fields(&sig, &mut bytes);
let pending_tx = self.provider.send_raw_transaction(&bytes).await.unwrap();
self.mine_block().await;
assert!(pending_tx.get_receipt().await.unwrap().status());
}

Ok(())
}
Expand Down Expand Up @@ -801,41 +798,50 @@ impl<D: Db> Network for Ethereum<D> {
block: usize,
eventuality: &Self::Eventuality,
) -> Self::Transaction {
match eventuality.1 {
RouterCommand::UpdateSeraiKey { nonce, .. } | RouterCommand::Execute { nonce, .. } => {
let router = self.router().await;
let router = router.as_ref().unwrap();

let block = u64::try_from(block).unwrap();
let filter = router
.key_updated_filter()
.from_block(block * 32)
.to_block(((block + 1) * 32) - 1)
.topic1(nonce);
let logs = self.provider.get_logs(&filter).await.unwrap();
if let Some(log) = logs.first() {
// We mine 96 blocks to ensure the 32 blocks relevant are finalized
// Back-check the prior two epochs in response to this
// TODO: Review why this is sub(3) and not sub(2)
for block in block.saturating_sub(3) ..= block {
match eventuality.1 {
RouterCommand::UpdateSeraiKey { nonce, .. } | RouterCommand::Execute { nonce, .. } => {
let router = self.router().await;
let router = router.as_ref().unwrap();

let block = u64::try_from(block).unwrap();
let filter = router
.key_updated_filter()
.from_block(block * 32)
.to_block(((block + 1) * 32) - 1)
.topic1(nonce);
let logs = self.provider.get_logs(&filter).await.unwrap();
if let Some(log) = logs.first() {
return self
.provider
.get_transaction_by_hash(log.clone().transaction_hash.unwrap())
.await
.unwrap()
.unwrap();
};

let filter = router
.executed_filter()
.from_block(block * 32)
.to_block(((block + 1) * 32) - 1)
.topic1(nonce);
let logs = self.provider.get_logs(&filter).await.unwrap();
if logs.is_empty() {
continue;
}
return self
.provider
.get_transaction_by_hash(log.clone().transaction_hash.unwrap())
.get_transaction_by_hash(logs[0].transaction_hash.unwrap())
.await
.unwrap()
.unwrap();
};

let filter = router
.executed_filter()
.from_block(block * 32)
.to_block(((block + 1) * 32) - 1)
.topic1(nonce);
let logs = self.provider.get_logs(&filter).await.unwrap();
self
.provider
.get_transaction_by_hash(logs[0].transaction_hash.unwrap())
.await
.unwrap()
.unwrap()
}
}
}
panic!("couldn't find completion in any three of checked blocks");
}

#[cfg(test)]
Expand Down
7 changes: 5 additions & 2 deletions processor/src/networks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,12 @@ pub trait Network: 'static + Send + Sync + Clone + PartialEq + Debug {

let plan_id = plan.id();
let Plan { key, inputs, mut payments, change, scheduler_addendum } = plan;
let theoretical_change_amount =
let theoretical_change_amount = if change.is_some() {
inputs.iter().map(|input| input.balance().amount.0).sum::<u64>() -
payments.iter().map(|payment| payment.balance.amount.0).sum::<u64>();
payments.iter().map(|payment| payment.balance.amount.0).sum::<u64>()
} else {
0
};

let Some(tx_fee) = self.needed_fee(block_number, &inputs, &payments, &change).await? else {
// This Plan is not fulfillable
Expand Down
2 changes: 2 additions & 0 deletions processor/src/tests/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,7 @@ mod ethereum {
ethereum_key_gen,
ethereum_scanner,
ethereum_no_deadlock_in_multisig_completed,
ethereum_signer,
ethereum_wallet,
);
}
51 changes: 32 additions & 19 deletions processor/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ macro_rules! test_network {
$key_gen: ident,
$scanner: ident,
$no_deadlock_in_multisig_completed: ident,
$signer: ident,
$wallet: ident,
) => {
use core::{pin::Pin, future::Future};
use $crate::tests::{
init_logger,
key_gen::test_key_gen,
scanner::{test_scanner, test_no_deadlock_in_multisig_completed},
signer::test_signer,
wallet::test_wallet,
};

// This doesn't interact with a node and accordingly doesn't need to be spawn one
Expand Down Expand Up @@ -63,25 +67,6 @@ macro_rules! test_network {
test_no_deadlock_in_multisig_completed(new_network).await;
});
}
};
}

#[macro_export]
macro_rules! test_utxo_network {
(
$N: ty,
$docker: ident,
$network: ident,
$key_gen: ident,
$scanner: ident,
$no_deadlock_in_multisig_completed: ident,
$signer: ident,
$wallet: ident,
$addresses: ident,
) => {
use $crate::tests::{signer::test_signer, wallet::test_wallet, addresses::test_addresses};

test_network!($N, $docker, $network, $key_gen, $scanner, $no_deadlock_in_multisig_completed,);

#[test]
fn $signer() {
Expand All @@ -102,6 +87,34 @@ macro_rules! test_utxo_network {
test_wallet(new_network).await;
});
}
};
}

#[macro_export]
macro_rules! test_utxo_network {
(
$N: ty,
$docker: ident,
$network: ident,
$key_gen: ident,
$scanner: ident,
$no_deadlock_in_multisig_completed: ident,
$signer: ident,
$wallet: ident,
$addresses: ident,
) => {
use $crate::tests::addresses::test_addresses;

test_network!(
$N,
$docker,
$network,
$key_gen,
$scanner,
$no_deadlock_in_multisig_completed,
$signer,
$wallet,
);

#[test]
fn $addresses() {
Expand Down
Loading

0 comments on commit d27d934

Please sign in to comment.