mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Processor scanner tests for Ethereum
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use core::time::Duration;
|
||||
use core::{time::Duration, pin::Pin, future::Future};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::OsRng;
|
||||
@@ -82,8 +82,9 @@ async fn spend<N: UtxoNetwork, D: Db>(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn test_addresses<N: UtxoNetwork>(network: N)
|
||||
where
|
||||
pub async fn test_addresses<N: UtxoNetwork>(
|
||||
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
|
||||
) where
|
||||
<N::Scheduler as Scheduler<N>>::Addendum: From<()>,
|
||||
{
|
||||
let mut keys = frost::tests::key_gen::<_, N::Curve>(&mut OsRng);
|
||||
@@ -92,12 +93,14 @@ where
|
||||
}
|
||||
let key = keys[&Participant::new(1).unwrap()].group_key();
|
||||
|
||||
let mut db = MemDb::new();
|
||||
let network = new_network(db.clone()).await;
|
||||
|
||||
// Mine blocks so there's a confirmed block
|
||||
for _ in 0 .. N::CONFIRMATIONS {
|
||||
network.mine_block().await;
|
||||
}
|
||||
|
||||
let mut db = MemDb::new();
|
||||
let (mut scanner, current_keys) = Scanner::new(network.clone(), db.clone());
|
||||
assert!(current_keys.is_empty());
|
||||
let mut txn = db.txn();
|
||||
|
||||
@@ -3,6 +3,8 @@ use dockertest::{
|
||||
TestBodySpecification, DockerOperations, DockerTest,
|
||||
};
|
||||
|
||||
use serai_db::MemDb;
|
||||
|
||||
#[cfg(feature = "bitcoin")]
|
||||
mod bitcoin {
|
||||
use std::sync::Arc;
|
||||
@@ -33,8 +35,6 @@ mod bitcoin {
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use serai_db::MemDb;
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
networks::{Network, Bitcoin, Output, OutputType, Block},
|
||||
@@ -57,7 +57,7 @@ mod bitcoin {
|
||||
fn test_receive_data_from_input() {
|
||||
let docker = spawn_bitcoin();
|
||||
docker.run(|ops| async move {
|
||||
let btc = bitcoin(&ops).await;
|
||||
let btc = bitcoin(&ops).await(MemDb::new()).await;
|
||||
|
||||
// generate a multisig address to receive the coins
|
||||
let mut keys = frost::tests::key_gen::<_, <Bitcoin as Network>::Curve>(&mut OsRng)
|
||||
@@ -208,23 +208,26 @@ mod bitcoin {
|
||||
test
|
||||
}
|
||||
|
||||
async fn bitcoin(ops: &DockerOperations) -> Bitcoin {
|
||||
async fn bitcoin(
|
||||
ops: &DockerOperations,
|
||||
) -> impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = Bitcoin>>> {
|
||||
let handle = ops.handle("serai-dev-bitcoin").host_port(8332).unwrap();
|
||||
let bitcoin = Bitcoin::new(format!("http://serai:seraidex@{}:{}", handle.0, handle.1)).await;
|
||||
let url = format!("http://serai:seraidex@{}:{}", handle.0, handle.1);
|
||||
let bitcoin = Bitcoin::new(url.clone()).await;
|
||||
bitcoin.fresh_chain().await;
|
||||
bitcoin
|
||||
move |_db| Box::pin(Bitcoin::new(url.clone()))
|
||||
}
|
||||
|
||||
test_network!(
|
||||
test_utxo_network!(
|
||||
Bitcoin,
|
||||
spawn_bitcoin,
|
||||
bitcoin,
|
||||
bitcoin_key_gen,
|
||||
bitcoin_scanner,
|
||||
bitcoin_no_deadlock_in_multisig_completed,
|
||||
bitcoin_signer,
|
||||
bitcoin_wallet,
|
||||
bitcoin_addresses,
|
||||
bitcoin_no_deadlock_in_multisig_completed,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -252,24 +255,181 @@ mod monero {
|
||||
test
|
||||
}
|
||||
|
||||
async fn monero(ops: &DockerOperations) -> Monero {
|
||||
async fn monero(
|
||||
ops: &DockerOperations,
|
||||
) -> impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = Monero>>> {
|
||||
let handle = ops.handle("serai-dev-monero").host_port(18081).unwrap();
|
||||
let monero = Monero::new(format!("http://serai:seraidex@{}:{}", handle.0, handle.1)).await;
|
||||
let url = format!("http://serai:seraidex@{}:{}", handle.0, handle.1);
|
||||
let monero = Monero::new(url.clone()).await;
|
||||
while monero.get_latest_block_number().await.unwrap() < 150 {
|
||||
monero.mine_block().await;
|
||||
}
|
||||
monero
|
||||
move |_db| Box::pin(Monero::new(url.clone()))
|
||||
}
|
||||
|
||||
test_network!(
|
||||
test_utxo_network!(
|
||||
Monero,
|
||||
spawn_monero,
|
||||
monero,
|
||||
monero_key_gen,
|
||||
monero_scanner,
|
||||
monero_no_deadlock_in_multisig_completed,
|
||||
monero_signer,
|
||||
monero_wallet,
|
||||
monero_addresses,
|
||||
monero_no_deadlock_in_multisig_completed,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "ethereum")]
|
||||
mod ethereum {
|
||||
use super::*;
|
||||
|
||||
use ciphersuite::{Ciphersuite, Secp256k1};
|
||||
|
||||
use serai_client::validator_sets::primitives::Session;
|
||||
|
||||
use crate::networks::Ethereum;
|
||||
|
||||
fn spawn_ethereum() -> DockerTest {
|
||||
serai_docker_tests::build("ethereum".to_string());
|
||||
|
||||
let composition = TestBodySpecification::with_image(
|
||||
Image::with_repository("serai-dev-ethereum").pull_policy(PullPolicy::Never),
|
||||
)
|
||||
.set_start_policy(StartPolicy::Strict)
|
||||
.set_log_options(Some(LogOptions {
|
||||
action: LogAction::Forward,
|
||||
policy: LogPolicy::OnError,
|
||||
source: LogSource::Both,
|
||||
}))
|
||||
.set_publish_all_ports(true);
|
||||
|
||||
let mut test = DockerTest::new();
|
||||
test.provide_container(composition);
|
||||
test
|
||||
}
|
||||
|
||||
async fn ethereum(
|
||||
ops: &DockerOperations,
|
||||
) -> impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = Ethereum<MemDb>>>> {
|
||||
use std::sync::Arc;
|
||||
use ethereum_serai::{
|
||||
alloy_core::primitives::U256,
|
||||
alloy_simple_request_transport::SimpleRequest,
|
||||
alloy_rpc_client::ClientBuilder,
|
||||
alloy_provider::{Provider, RootProvider},
|
||||
deployer::Deployer,
|
||||
};
|
||||
|
||||
let handle = ops.handle("serai-dev-ethereum").host_port(8545).unwrap();
|
||||
let url = format!("http://{}:{}", handle.0, handle.1);
|
||||
tokio::time::sleep(core::time::Duration::from_secs(15)).await;
|
||||
|
||||
{
|
||||
let provider = Arc::new(RootProvider::new(
|
||||
ClientBuilder::default().transport(SimpleRequest::new(url.clone()), true),
|
||||
));
|
||||
provider.raw_request::<_, ()>("evm_setAutomine".into(), [false]).await.unwrap();
|
||||
provider.raw_request::<_, ()>("anvil_mine".into(), [96]).await.unwrap();
|
||||
|
||||
// Perform deployment
|
||||
{
|
||||
// Make sure the Deployer constructor returns None, as it doesn't exist yet
|
||||
assert!(Deployer::new(provider.clone()).await.unwrap().is_none());
|
||||
|
||||
// Deploy the Deployer
|
||||
let tx = Deployer::deployment_tx();
|
||||
|
||||
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 (tx, sig, _) = tx.into_parts();
|
||||
let mut bytes = vec![];
|
||||
tx.encode_with_signature_fields(&sig, &mut bytes);
|
||||
|
||||
let pending_tx = provider.send_raw_transaction(&bytes).await.unwrap();
|
||||
provider.raw_request::<_, ()>("anvil_mine".into(), [96]).await.unwrap();
|
||||
//tokio::time::sleep(core::time::Duration::from_secs(15)).await;
|
||||
let receipt = pending_tx.get_receipt().await.unwrap();
|
||||
assert!(receipt.status());
|
||||
|
||||
let _ = Deployer::new(provider.clone())
|
||||
.await
|
||||
.expect("network error")
|
||||
.expect("deployer wasn't deployed");
|
||||
}
|
||||
}
|
||||
|
||||
move |db| {
|
||||
let url = url.clone();
|
||||
Box::pin(async move {
|
||||
{
|
||||
let db = db.clone();
|
||||
let url = url.clone();
|
||||
// Spawn a task to deploy the proper Router when the time comes
|
||||
tokio::spawn(async move {
|
||||
let key = loop {
|
||||
let Some(key) = crate::key_gen::NetworkKeyDb::get(&db, Session(0)) else {
|
||||
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
|
||||
continue;
|
||||
};
|
||||
break ethereum_serai::crypto::PublicKey::new(
|
||||
Secp256k1::read_G(&mut key.as_slice()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
};
|
||||
let provider = Arc::new(RootProvider::new(
|
||||
ClientBuilder::default().transport(SimpleRequest::new(url.clone()), true),
|
||||
));
|
||||
let deployer = Deployer::new(provider.clone()).await.unwrap().unwrap();
|
||||
|
||||
let mut tx = deployer.deploy_router(&key);
|
||||
tx.gas_limit = 1_000_000u64.into();
|
||||
tx.gas_price = 1_000_000_000u64.into();
|
||||
let tx = ethereum_serai::crypto::deterministically_sign(&tx);
|
||||
|
||||
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 (tx, sig, _) = tx.into_parts();
|
||||
let mut bytes = vec![];
|
||||
tx.encode_with_signature_fields(&sig, &mut bytes);
|
||||
let pending_tx = provider.send_raw_transaction(&bytes).await.unwrap();
|
||||
provider.raw_request::<_, ()>("anvil_mine".into(), [96]).await.unwrap();
|
||||
let receipt = pending_tx.get_receipt().await.unwrap();
|
||||
assert!(receipt.status());
|
||||
|
||||
let _router = deployer.find_router(provider.clone(), &key).await.unwrap().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
Ethereum::new(db, url.clone()).await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
test_network!(
|
||||
Ethereum<MemDb>,
|
||||
spawn_ethereum,
|
||||
ethereum,
|
||||
ethereum_key_gen,
|
||||
ethereum_scanner,
|
||||
ethereum_no_deadlock_in_multisig_completed,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
mod key_gen;
|
||||
pub(crate) use key_gen::test_key_gen;
|
||||
|
||||
mod scanner;
|
||||
pub(crate) use scanner::{test_scanner, test_no_deadlock_in_multisig_completed};
|
||||
|
||||
mod signer;
|
||||
pub(crate) use signer::{sign, test_signer};
|
||||
pub(crate) use signer::sign;
|
||||
|
||||
mod cosigner;
|
||||
mod batch_signer;
|
||||
|
||||
mod wallet;
|
||||
pub(crate) use wallet::test_wallet;
|
||||
|
||||
mod addresses;
|
||||
pub(crate) use addresses::test_addresses;
|
||||
|
||||
// Effective Once
|
||||
static INIT_LOGGER_CELL: OnceLock<()> = OnceLock::new();
|
||||
@@ -27,22 +23,21 @@ fn init_logger() {
|
||||
#[macro_export]
|
||||
macro_rules! test_network {
|
||||
(
|
||||
$N: ident,
|
||||
$N: ty,
|
||||
$docker: ident,
|
||||
$network: ident,
|
||||
$key_gen: ident,
|
||||
$scanner: ident,
|
||||
$signer: ident,
|
||||
$wallet: ident,
|
||||
$addresses: ident,
|
||||
$no_deadlock_in_multisig_completed: ident,
|
||||
) => {
|
||||
use core::{pin::Pin, future::Future};
|
||||
use $crate::tests::{
|
||||
init_logger, test_key_gen, test_scanner, test_no_deadlock_in_multisig_completed, test_signer,
|
||||
test_wallet, test_addresses,
|
||||
init_logger,
|
||||
key_gen::test_key_gen,
|
||||
scanner::{test_scanner, test_no_deadlock_in_multisig_completed},
|
||||
};
|
||||
|
||||
// This doesn't interact with a node and accordingly doesn't need to be run
|
||||
// This doesn't interact with a node and accordingly doesn't need to be spawn one
|
||||
#[tokio::test]
|
||||
async fn $key_gen() {
|
||||
init_logger();
|
||||
@@ -54,34 +49,8 @@ macro_rules! test_network {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
test_scanner($network(&ops).await).await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $signer() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
test_signer($network(&ops).await).await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $wallet() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
test_wallet($network(&ops).await).await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $addresses() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
test_addresses($network(&ops).await).await;
|
||||
let new_network = $network(&ops).await;
|
||||
test_scanner(new_network).await;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,7 +59,57 @@ macro_rules! test_network {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
test_no_deadlock_in_multisig_completed($network(&ops).await).await;
|
||||
let new_network = $network(&ops).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]
|
||||
fn $signer() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
let new_network = $network(&ops).await;
|
||||
test_signer(new_network).await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $wallet() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
let new_network = $network(&ops).await;
|
||||
test_wallet(new_network).await;
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn $addresses() {
|
||||
init_logger();
|
||||
let docker = $docker();
|
||||
docker.run(|ops| async move {
|
||||
let new_network = $network(&ops).await;
|
||||
test_addresses(new_network).await;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
use core::time::Duration;
|
||||
use core::{pin::Pin, time::Duration, future::Future};
|
||||
use std::sync::Arc;
|
||||
|
||||
use ciphersuite::Ciphersuite;
|
||||
use rand_core::OsRng;
|
||||
|
||||
use ciphersuite::{group::GroupEncoding, Ciphersuite};
|
||||
use frost::{Participant, tests::key_gen};
|
||||
|
||||
use tokio::{sync::Mutex, time::timeout};
|
||||
|
||||
use serai_db::{DbTxn, Db, MemDb};
|
||||
use serai_client::validator_sets::primitives::Session;
|
||||
|
||||
use crate::{
|
||||
networks::{OutputType, Output, Block, UtxoNetwork},
|
||||
networks::{OutputType, Output, Block, Network},
|
||||
key_gen::NetworkKeyDb,
|
||||
multisigs::scanner::{ScannerEvent, Scanner, ScannerHandle},
|
||||
};
|
||||
|
||||
pub async fn new_scanner<N: UtxoNetwork, D: Db>(
|
||||
pub async fn new_scanner<N: Network, D: Db>(
|
||||
network: &N,
|
||||
db: &D,
|
||||
group_key: <N::Curve as Ciphersuite>::G,
|
||||
@@ -40,18 +42,27 @@ pub async fn new_scanner<N: UtxoNetwork, D: Db>(
|
||||
scanner
|
||||
}
|
||||
|
||||
pub async fn test_scanner<N: UtxoNetwork>(network: N) {
|
||||
pub async fn test_scanner<N: Network>(
|
||||
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
|
||||
) {
|
||||
let mut keys =
|
||||
frost::tests::key_gen::<_, N::Curve>(&mut OsRng).remove(&Participant::new(1).unwrap()).unwrap();
|
||||
N::tweak_keys(&mut keys);
|
||||
let group_key = keys.group_key();
|
||||
|
||||
let mut db = MemDb::new();
|
||||
{
|
||||
let mut txn = db.txn();
|
||||
NetworkKeyDb::set(&mut txn, Session(0), &group_key.to_bytes().as_ref().to_vec());
|
||||
txn.commit();
|
||||
}
|
||||
let network = new_network(db.clone()).await;
|
||||
|
||||
// Mine blocks so there's a confirmed block
|
||||
for _ in 0 .. N::CONFIRMATIONS {
|
||||
network.mine_block().await;
|
||||
}
|
||||
|
||||
let db = MemDb::new();
|
||||
let first = Arc::new(Mutex::new(true));
|
||||
let scanner = new_scanner(&network, &db, group_key, &first).await;
|
||||
|
||||
@@ -101,13 +112,17 @@ pub async fn test_scanner<N: UtxoNetwork>(network: N) {
|
||||
.is_err());
|
||||
}
|
||||
|
||||
pub async fn test_no_deadlock_in_multisig_completed<N: UtxoNetwork>(network: N) {
|
||||
pub async fn test_no_deadlock_in_multisig_completed<N: Network>(
|
||||
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
|
||||
) {
|
||||
let mut db = MemDb::new();
|
||||
let network = new_network(db.clone()).await;
|
||||
|
||||
// Mine blocks so there's a confirmed block
|
||||
for _ in 0 .. N::CONFIRMATIONS {
|
||||
network.mine_block().await;
|
||||
}
|
||||
|
||||
let mut db = MemDb::new();
|
||||
let (mut scanner, current_keys) = Scanner::new(network.clone(), db.clone());
|
||||
assert!(current_keys.is_empty());
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use core::{pin::Pin, future::Future};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
@@ -153,8 +154,9 @@ pub async fn sign<N: UtxoNetwork>(
|
||||
typed_claim
|
||||
}
|
||||
|
||||
pub async fn test_signer<N: UtxoNetwork>(network: N)
|
||||
where
|
||||
pub async fn test_signer<N: UtxoNetwork>(
|
||||
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);
|
||||
@@ -163,6 +165,9 @@ where
|
||||
}
|
||||
let key = keys[&Participant::new(1).unwrap()].group_key();
|
||||
|
||||
let db = MemDb::new();
|
||||
let network = new_network(db).await;
|
||||
|
||||
let outputs = network
|
||||
.get_outputs(&network.test_send(N::external_address(&network, key).await).await, key)
|
||||
.await;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::{time::Duration, collections::HashMap};
|
||||
use core::{time::Duration, pin::Pin, future::Future};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::OsRng;
|
||||
|
||||
@@ -24,12 +25,9 @@ use crate::{
|
||||
};
|
||||
|
||||
// Tests the Scanner, Scheduler, and Signer together
|
||||
pub async fn test_wallet<N: UtxoNetwork>(network: N) {
|
||||
// Mine blocks so there's a confirmed block
|
||||
for _ in 0 .. N::CONFIRMATIONS {
|
||||
network.mine_block().await;
|
||||
}
|
||||
|
||||
pub async fn test_wallet<N: UtxoNetwork>(
|
||||
new_network: impl Fn(MemDb) -> Pin<Box<dyn Send + Future<Output = N>>>,
|
||||
) {
|
||||
let mut keys = key_gen(&mut OsRng);
|
||||
for keys in keys.values_mut() {
|
||||
N::tweak_keys(keys);
|
||||
@@ -37,6 +35,13 @@ pub async fn test_wallet<N: UtxoNetwork>(network: N) {
|
||||
let key = keys[&Participant::new(1).unwrap()].group_key();
|
||||
|
||||
let mut db = MemDb::new();
|
||||
let network = new_network(db.clone()).await;
|
||||
|
||||
// Mine blocks so there's a confirmed block
|
||||
for _ in 0 .. N::CONFIRMATIONS {
|
||||
network.mine_block().await;
|
||||
}
|
||||
|
||||
let (mut scanner, current_keys) = Scanner::new(network.clone(), db.clone());
|
||||
assert!(current_keys.is_empty());
|
||||
let (block_id, outputs) = {
|
||||
|
||||
Reference in New Issue
Block a user