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

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