Get processor signer/wallet tests working for Ethereum

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).
This commit is contained in:
Luke Parker
2024-05-11 00:11:14 -04:00
parent 02c4417a46
commit d27d93480a
8 changed files with 203 additions and 141 deletions

View File

@@ -431,5 +431,7 @@ mod ethereum {
ethereum_key_gen,
ethereum_scanner,
ethereum_no_deadlock_in_multisig_completed,
ethereum_signer,
ethereum_wallet,
);
}

View File

@@ -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
@@ -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() {
@@ -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() {

View File

@@ -3,6 +3,7 @@ use std::collections::HashMap;
use rand_core::{RngCore, OsRng};
use ciphersuite::group::GroupEncoding;
use frost::{
Participant, ThresholdKeys,
dkg::tests::{key_gen, clone_without},
@@ -17,14 +18,15 @@ use serai_client::{
use messages::sign::*;
use crate::{
Payment, Plan,
networks::{Output, Transaction, Eventuality, UtxoNetwork},
Payment,
networks::{Output, Transaction, Eventuality, Network},
key_gen::NetworkKeyDb,
multisigs::scheduler::Scheduler,
signer::Signer,
};
#[allow(clippy::type_complexity)]
pub async fn sign<N: UtxoNetwork>(
pub async fn sign<N: Network>(
network: N,
session: Session,
mut keys_txs: HashMap<
@@ -154,57 +156,55 @@ pub async fn sign<N: UtxoNetwork>(
typed_claim
}
pub async fn test_signer<N: UtxoNetwork>(
pub async fn test_signer<N: Network>(
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
) where
<N::Scheduler as Scheduler<N>>::Addendum: From<()>,
{
) {
let mut keys = key_gen(&mut OsRng);
for keys in keys.values_mut() {
N::tweak_keys(keys);
}
let key = keys[&Participant::new(1).unwrap()].group_key();
let db = MemDb::new();
let network = new_network(db).await;
let mut db = MemDb::new();
{
let mut txn = db.txn();
NetworkKeyDb::set(&mut txn, Session(0), &key.to_bytes().as_ref().to_vec());
txn.commit();
}
let network = new_network(db.clone()).await;
let outputs = network
.get_outputs(&network.test_send(N::external_address(&network, key).await).await, key)
.await;
let sync_block = network.get_latest_block_number().await.unwrap() - N::CONFIRMATIONS;
let amount = 2 * N::DUST;
let amount = (2 * N::DUST) + 1000;
let plan = {
let mut txn = db.txn();
let mut scheduler = N::Scheduler::new::<MemDb>(&mut txn, key, N::NETWORK);
let payments = vec![Payment {
address: N::external_address(&network, key).await,
data: None,
balance: Balance {
coin: match N::NETWORK {
NetworkId::Serai => panic!("test_signer called with Serai"),
NetworkId::Bitcoin => Coin::Bitcoin,
NetworkId::Ethereum => Coin::Ether,
NetworkId::Monero => Coin::Monero,
},
amount: Amount(amount),
},
}];
let mut plans = scheduler.schedule::<MemDb>(&mut txn, outputs.clone(), payments, key, false);
assert_eq!(plans.len(), 1);
plans.swap_remove(0)
};
let mut keys_txs = HashMap::new();
let mut eventualities = vec![];
for (i, keys) in keys.drain() {
let (signable, eventuality) = network
.prepare_send(
sync_block,
Plan {
key,
inputs: outputs.clone(),
payments: vec![Payment {
address: N::external_address(&network, key).await,
data: None,
balance: Balance {
coin: match N::NETWORK {
NetworkId::Serai => panic!("test_signer called with Serai"),
NetworkId::Bitcoin => Coin::Bitcoin,
NetworkId::Ethereum => Coin::Ether,
NetworkId::Monero => Coin::Monero,
},
amount: Amount(amount),
},
}],
change: Some(N::change_address(key).unwrap()),
scheduler_addendum: ().into(),
},
0,
)
.await
.unwrap()
.tx
.unwrap();
let (signable, eventuality) =
network.prepare_send(sync_block, plan.clone(), 0).await.unwrap().tx.unwrap();
eventualities.push(eventuality.clone());
keys_txs.insert(i, (keys, (signable, eventuality)));
@@ -222,11 +222,21 @@ pub async fn test_signer<N: UtxoNetwork>(
key,
)
.await;
assert_eq!(outputs.len(), 2);
// Adjust the amount for the fees
let amount = amount - tx.fee(&network).await;
// Check either output since Monero will randomize its output order
assert!((outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount));
// Don't run if Ethereum as the received output will revert by the contract
// (and therefore not actually exist)
if N::NETWORK != NetworkId::Ethereum {
assert_eq!(outputs.len(), 1 + usize::from(u8::from(plan.change.is_some())));
// Adjust the amount for the fees
let amount = amount - tx.fee(&network).await;
if plan.change.is_some() {
// Check either output since Monero will randomize its output order
assert!(
(outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount)
);
} else {
assert!(outputs[0].balance().amount.0 == amount);
}
}
// Check the eventualities pass
for eventuality in eventualities {

View File

@@ -3,6 +3,7 @@ use std::collections::HashMap;
use rand_core::OsRng;
use ciphersuite::group::GroupEncoding;
use frost::{Participant, dkg::tests::key_gen};
use tokio::time::timeout;
@@ -16,16 +17,17 @@ use serai_client::{
use crate::{
Payment, Plan,
networks::{Output, Transaction, Eventuality, Block, UtxoNetwork},
networks::{Output, Transaction, Eventuality, Block, Network},
key_gen::NetworkKeyDb,
multisigs::{
scanner::{ScannerEvent, Scanner},
scheduler::Scheduler,
scheduler::{self, Scheduler},
},
tests::sign,
};
// Tests the Scanner, Scheduler, and Signer together
pub async fn test_wallet<N: UtxoNetwork>(
pub async fn test_wallet<N: Network>(
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
) {
let mut keys = key_gen(&mut OsRng);
@@ -35,6 +37,11 @@ pub async fn test_wallet<N: UtxoNetwork>(
let key = keys[&Participant::new(1).unwrap()].group_key();
let mut db = MemDb::new();
{
let mut txn = db.txn();
NetworkKeyDb::set(&mut txn, Session(0), &key.to_bytes().as_ref().to_vec());
txn.commit();
}
let network = new_network(db.clone()).await;
// Mine blocks so there's a confirmed block
@@ -98,7 +105,13 @@ pub async fn test_wallet<N: UtxoNetwork>(
txn.commit();
assert_eq!(plans.len(), 1);
assert_eq!(plans[0].key, key);
assert_eq!(plans[0].inputs, outputs);
if std::any::TypeId::of::<N::Scheduler>() ==
std::any::TypeId::of::<scheduler::smart_contract::Scheduler<N>>()
{
assert_eq!(plans[0].inputs, vec![]);
} else {
assert_eq!(plans[0].inputs, outputs);
}
assert_eq!(
plans[0].payments,
vec![Payment {
@@ -115,7 +128,7 @@ pub async fn test_wallet<N: UtxoNetwork>(
}
}]
);
assert_eq!(plans[0].change, Some(N::change_address(key).unwrap()));
assert_eq!(plans[0].change, N::change_address(key));
{
let mut buf = vec![];
@@ -144,9 +157,22 @@ pub async fn test_wallet<N: UtxoNetwork>(
let tx = network.get_transaction_by_eventuality(block_number, &eventualities[0]).await;
let block = network.get_block(block_number).await.unwrap();
let outputs = network.get_outputs(&block, key).await;
assert_eq!(outputs.len(), 2);
let amount = amount - tx.fee(&network).await;
assert!((outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount));
// Don't run if Ethereum as the received output will revert by the contract
// (and therefore not actually exist)
if N::NETWORK != NetworkId::Ethereum {
assert_eq!(outputs.len(), 1 + usize::from(u8::from(plans[0].change.is_some())));
// Adjust the amount for the fees
let amount = amount - tx.fee(&network).await;
if plans[0].change.is_some() {
// Check either output since Monero will randomize its output order
assert!(
(outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount)
);
} else {
assert!(outputs[0].balance().amount.0 == amount);
}
}
for eventuality in eventualities {
let completion = network.confirm_completion(&eventuality, &claim).await.unwrap().unwrap();
@@ -157,21 +183,23 @@ pub async fn test_wallet<N: UtxoNetwork>(
network.mine_block().await;
}
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
ScannerEvent::Block { is_retirement_block, block: block_id, outputs: these_outputs } => {
scanner.multisig_completed.send(false).unwrap();
assert!(!is_retirement_block);
assert_eq!(block_id, block.id());
assert_eq!(these_outputs, outputs);
if N::NETWORK != NetworkId::Ethereum {
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
ScannerEvent::Block { is_retirement_block, block: block_id, outputs: these_outputs } => {
scanner.multisig_completed.send(false).unwrap();
assert!(!is_retirement_block);
assert_eq!(block_id, block.id());
assert_eq!(these_outputs, outputs);
}
ScannerEvent::Completed(_, _, _, _, _) => {
panic!("unexpectedly got eventuality completion");
}
}
ScannerEvent::Completed(_, _, _, _, _) => {
panic!("unexpectedly got eventuality completion");
}
}
// Check the Scanner DB can reload the outputs
let mut txn = db.txn();
assert_eq!(scanner.ack_block(&mut txn, block.id()).await.1, outputs);
scanner.release_lock().await;
txn.commit();
// Check the Scanner DB can reload the outputs
let mut txn = db.txn();
assert_eq!(scanner.ack_block(&mut txn, block.id()).await.1, outputs);
scanner.release_lock().await;
txn.commit();
}
}