mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
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:
@@ -101,5 +101,5 @@ allow-git = [
|
|||||||
"https://github.com/serai-dex/substrate",
|
"https://github.com/serai-dex/substrate",
|
||||||
"https://github.com/alloy-rs/alloy",
|
"https://github.com/alloy-rs/alloy",
|
||||||
"https://github.com/monero-rs/base58-monero",
|
"https://github.com/monero-rs/base58-monero",
|
||||||
"https://github.com/kayabaNerve/dockertest-rs",
|
"https://github.com/orcalabs/dockertest-rs",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ impl<N: Network<Scheduler = Self>> SchedulerTrait<N> for Scheduler<N> {
|
|||||||
assert!(self.coins.contains(&utxo.balance().coin));
|
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![];
|
let mut plans = vec![];
|
||||||
for chunk in payments.as_slice().chunks(N::MAX_OUTPUTS) {
|
for chunk in payments.as_slice().chunks(N::MAX_OUTPUTS) {
|
||||||
// Once we rotate, all further payments should be scheduled via the new multisig
|
// Once we rotate, all further payments should be scheduled via the new multisig
|
||||||
@@ -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())
|
.and_then(|key_bytes| <N::Curve as Ciphersuite>::read_G(&mut key_bytes.as_slice()).ok())
|
||||||
.unwrap_or(self.key);
|
.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));
|
LastNonce::set(txn, &(nonce + 1));
|
||||||
Plan {
|
Plan {
|
||||||
key: current_key,
|
key: current_key,
|
||||||
|
|||||||
@@ -719,22 +719,6 @@ impl<D: Db> Network for Ethereum<D> {
|
|||||||
// Publish this using a dummy account we fund with magic RPC commands
|
// Publish this using a dummy account we fund with magic RPC commands
|
||||||
#[cfg(test)]
|
#[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 = self.router().await;
|
||||||
let router = router.as_ref().unwrap();
|
let router = router.as_ref().unwrap();
|
||||||
|
|
||||||
@@ -747,17 +731,30 @@ impl<D: Db> Network for Ethereum<D> {
|
|||||||
completion.signature(),
|
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;
|
if self.provider.get_transaction_by_hash(*tx.hash()).await.unwrap().is_none() {
|
||||||
let sig =
|
self
|
||||||
k256::ecdsa::SigningKey::from(k256::elliptic_curve::NonZeroScalar::new(key).unwrap())
|
.provider
|
||||||
.sign_prehash_recoverable(tx.signature_hash().as_ref())
|
.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();
|
.unwrap();
|
||||||
|
|
||||||
let mut bytes = vec![];
|
let (tx, sig, _) = tx.into_parts();
|
||||||
tx.encode_with_signature_fields(&sig.into(), &mut bytes);
|
let mut bytes = vec![];
|
||||||
let _ = self.provider.send_raw_transaction(&bytes).await.ok().unwrap();
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -801,41 +798,50 @@ impl<D: Db> Network for Ethereum<D> {
|
|||||||
block: usize,
|
block: usize,
|
||||||
eventuality: &Self::Eventuality,
|
eventuality: &Self::Eventuality,
|
||||||
) -> Self::Transaction {
|
) -> Self::Transaction {
|
||||||
match eventuality.1 {
|
// We mine 96 blocks to ensure the 32 blocks relevant are finalized
|
||||||
RouterCommand::UpdateSeraiKey { nonce, .. } | RouterCommand::Execute { nonce, .. } => {
|
// Back-check the prior two epochs in response to this
|
||||||
let router = self.router().await;
|
// TODO: Review why this is sub(3) and not sub(2)
|
||||||
let router = router.as_ref().unwrap();
|
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 block = u64::try_from(block).unwrap();
|
||||||
let filter = router
|
let filter = router
|
||||||
.key_updated_filter()
|
.key_updated_filter()
|
||||||
.from_block(block * 32)
|
.from_block(block * 32)
|
||||||
.to_block(((block + 1) * 32) - 1)
|
.to_block(((block + 1) * 32) - 1)
|
||||||
.topic1(nonce);
|
.topic1(nonce);
|
||||||
let logs = self.provider.get_logs(&filter).await.unwrap();
|
let logs = self.provider.get_logs(&filter).await.unwrap();
|
||||||
if let Some(log) = logs.first() {
|
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
|
return self
|
||||||
.provider
|
.provider
|
||||||
.get_transaction_by_hash(log.clone().transaction_hash.unwrap())
|
.get_transaction_by_hash(logs[0].transaction_hash.unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.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)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -432,9 +432,12 @@ pub trait Network: 'static + Send + Sync + Clone + PartialEq + Debug {
|
|||||||
|
|
||||||
let plan_id = plan.id();
|
let plan_id = plan.id();
|
||||||
let Plan { key, inputs, mut payments, change, scheduler_addendum } = plan;
|
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>() -
|
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 {
|
let Some(tx_fee) = self.needed_fee(block_number, &inputs, &payments, &change).await? else {
|
||||||
// This Plan is not fulfillable
|
// This Plan is not fulfillable
|
||||||
|
|||||||
@@ -431,5 +431,7 @@ mod ethereum {
|
|||||||
ethereum_key_gen,
|
ethereum_key_gen,
|
||||||
ethereum_scanner,
|
ethereum_scanner,
|
||||||
ethereum_no_deadlock_in_multisig_completed,
|
ethereum_no_deadlock_in_multisig_completed,
|
||||||
|
ethereum_signer,
|
||||||
|
ethereum_wallet,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,16 @@ macro_rules! test_network {
|
|||||||
$key_gen: ident,
|
$key_gen: ident,
|
||||||
$scanner: ident,
|
$scanner: ident,
|
||||||
$no_deadlock_in_multisig_completed: ident,
|
$no_deadlock_in_multisig_completed: ident,
|
||||||
|
$signer: ident,
|
||||||
|
$wallet: ident,
|
||||||
) => {
|
) => {
|
||||||
use core::{pin::Pin, future::Future};
|
use core::{pin::Pin, future::Future};
|
||||||
use $crate::tests::{
|
use $crate::tests::{
|
||||||
init_logger,
|
init_logger,
|
||||||
key_gen::test_key_gen,
|
key_gen::test_key_gen,
|
||||||
scanner::{test_scanner, test_no_deadlock_in_multisig_completed},
|
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
|
// 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;
|
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]
|
#[test]
|
||||||
fn $signer() {
|
fn $signer() {
|
||||||
@@ -102,6 +87,34 @@ macro_rules! test_utxo_network {
|
|||||||
test_wallet(new_network).await;
|
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]
|
#[test]
|
||||||
fn $addresses() {
|
fn $addresses() {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use rand_core::{RngCore, OsRng};
|
use rand_core::{RngCore, OsRng};
|
||||||
|
|
||||||
|
use ciphersuite::group::GroupEncoding;
|
||||||
use frost::{
|
use frost::{
|
||||||
Participant, ThresholdKeys,
|
Participant, ThresholdKeys,
|
||||||
dkg::tests::{key_gen, clone_without},
|
dkg::tests::{key_gen, clone_without},
|
||||||
@@ -17,14 +18,15 @@ use serai_client::{
|
|||||||
|
|
||||||
use messages::sign::*;
|
use messages::sign::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
Payment, Plan,
|
Payment,
|
||||||
networks::{Output, Transaction, Eventuality, UtxoNetwork},
|
networks::{Output, Transaction, Eventuality, Network},
|
||||||
|
key_gen::NetworkKeyDb,
|
||||||
multisigs::scheduler::Scheduler,
|
multisigs::scheduler::Scheduler,
|
||||||
signer::Signer,
|
signer::Signer,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
pub async fn sign<N: UtxoNetwork>(
|
pub async fn sign<N: Network>(
|
||||||
network: N,
|
network: N,
|
||||||
session: Session,
|
session: Session,
|
||||||
mut keys_txs: HashMap<
|
mut keys_txs: HashMap<
|
||||||
@@ -154,57 +156,55 @@ pub async fn sign<N: UtxoNetwork>(
|
|||||||
typed_claim
|
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>>>,
|
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);
|
let mut keys = key_gen(&mut OsRng);
|
||||||
for keys in keys.values_mut() {
|
for keys in keys.values_mut() {
|
||||||
N::tweak_keys(keys);
|
N::tweak_keys(keys);
|
||||||
}
|
}
|
||||||
let key = keys[&Participant::new(1).unwrap()].group_key();
|
let key = keys[&Participant::new(1).unwrap()].group_key();
|
||||||
|
|
||||||
let db = MemDb::new();
|
let mut db = MemDb::new();
|
||||||
let network = new_network(db).await;
|
{
|
||||||
|
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
|
let outputs = network
|
||||||
.get_outputs(&network.test_send(N::external_address(&network, key).await).await, key)
|
.get_outputs(&network.test_send(N::external_address(&network, key).await).await, key)
|
||||||
.await;
|
.await;
|
||||||
let sync_block = network.get_latest_block_number().await.unwrap() - N::CONFIRMATIONS;
|
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 keys_txs = HashMap::new();
|
||||||
let mut eventualities = vec![];
|
let mut eventualities = vec![];
|
||||||
for (i, keys) in keys.drain() {
|
for (i, keys) in keys.drain() {
|
||||||
let (signable, eventuality) = network
|
let (signable, eventuality) =
|
||||||
.prepare_send(
|
network.prepare_send(sync_block, plan.clone(), 0).await.unwrap().tx.unwrap();
|
||||||
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();
|
|
||||||
|
|
||||||
eventualities.push(eventuality.clone());
|
eventualities.push(eventuality.clone());
|
||||||
keys_txs.insert(i, (keys, (signable, eventuality)));
|
keys_txs.insert(i, (keys, (signable, eventuality)));
|
||||||
@@ -222,11 +222,21 @@ pub async fn test_signer<N: UtxoNetwork>(
|
|||||||
key,
|
key,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(outputs.len(), 2);
|
// Don't run if Ethereum as the received output will revert by the contract
|
||||||
// Adjust the amount for the fees
|
// (and therefore not actually exist)
|
||||||
let amount = amount - tx.fee(&network).await;
|
if N::NETWORK != NetworkId::Ethereum {
|
||||||
// Check either output since Monero will randomize its output order
|
assert_eq!(outputs.len(), 1 + usize::from(u8::from(plan.change.is_some())));
|
||||||
assert!((outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount));
|
// 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
|
// Check the eventualities pass
|
||||||
for eventuality in eventualities {
|
for eventuality in eventualities {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use rand_core::OsRng;
|
use rand_core::OsRng;
|
||||||
|
|
||||||
|
use ciphersuite::group::GroupEncoding;
|
||||||
use frost::{Participant, dkg::tests::key_gen};
|
use frost::{Participant, dkg::tests::key_gen};
|
||||||
|
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
@@ -16,16 +17,17 @@ use serai_client::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Payment, Plan,
|
Payment, Plan,
|
||||||
networks::{Output, Transaction, Eventuality, Block, UtxoNetwork},
|
networks::{Output, Transaction, Eventuality, Block, Network},
|
||||||
|
key_gen::NetworkKeyDb,
|
||||||
multisigs::{
|
multisigs::{
|
||||||
scanner::{ScannerEvent, Scanner},
|
scanner::{ScannerEvent, Scanner},
|
||||||
scheduler::Scheduler,
|
scheduler::{self, Scheduler},
|
||||||
},
|
},
|
||||||
tests::sign,
|
tests::sign,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Tests the Scanner, Scheduler, and Signer together
|
// 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>>>,
|
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
|
||||||
) {
|
) {
|
||||||
let mut keys = key_gen(&mut OsRng);
|
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 key = keys[&Participant::new(1).unwrap()].group_key();
|
||||||
|
|
||||||
let mut db = MemDb::new();
|
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 network = new_network(db.clone()).await;
|
||||||
|
|
||||||
// Mine blocks so there's a confirmed block
|
// Mine blocks so there's a confirmed block
|
||||||
@@ -98,7 +105,13 @@ pub async fn test_wallet<N: UtxoNetwork>(
|
|||||||
txn.commit();
|
txn.commit();
|
||||||
assert_eq!(plans.len(), 1);
|
assert_eq!(plans.len(), 1);
|
||||||
assert_eq!(plans[0].key, key);
|
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!(
|
assert_eq!(
|
||||||
plans[0].payments,
|
plans[0].payments,
|
||||||
vec![Payment {
|
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![];
|
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 tx = network.get_transaction_by_eventuality(block_number, &eventualities[0]).await;
|
||||||
let block = network.get_block(block_number).await.unwrap();
|
let block = network.get_block(block_number).await.unwrap();
|
||||||
let outputs = network.get_outputs(&block, key).await;
|
let outputs = network.get_outputs(&block, key).await;
|
||||||
assert_eq!(outputs.len(), 2);
|
|
||||||
let amount = amount - tx.fee(&network).await;
|
// Don't run if Ethereum as the received output will revert by the contract
|
||||||
assert!((outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount));
|
// (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 {
|
for eventuality in eventualities {
|
||||||
let completion = network.confirm_completion(&eventuality, &claim).await.unwrap().unwrap();
|
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;
|
network.mine_block().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
if N::NETWORK != NetworkId::Ethereum {
|
||||||
ScannerEvent::Block { is_retirement_block, block: block_id, outputs: these_outputs } => {
|
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||||
scanner.multisig_completed.send(false).unwrap();
|
ScannerEvent::Block { is_retirement_block, block: block_id, outputs: these_outputs } => {
|
||||||
assert!(!is_retirement_block);
|
scanner.multisig_completed.send(false).unwrap();
|
||||||
assert_eq!(block_id, block.id());
|
assert!(!is_retirement_block);
|
||||||
assert_eq!(these_outputs, outputs);
|
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
|
// Check the Scanner DB can reload the outputs
|
||||||
let mut txn = db.txn();
|
let mut txn = db.txn();
|
||||||
assert_eq!(scanner.ack_block(&mut txn, block.id()).await.1, outputs);
|
assert_eq!(scanner.ack_block(&mut txn, block.id()).await.1, outputs);
|
||||||
scanner.release_lock().await;
|
scanner.release_lock().await;
|
||||||
txn.commit();
|
txn.commit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user