2022-07-15 05:26:07 +00:00
|
|
|
use std::{
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
collections::HashMap,
|
|
|
|
};
|
2022-06-09 06:48:53 +00:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2022-06-01 07:30:57 +00:00
|
|
|
|
2022-07-27 09:43:23 +00:00
|
|
|
use rand_core::OsRng;
|
2022-06-01 07:30:57 +00:00
|
|
|
|
2023-02-23 11:50:45 +00:00
|
|
|
use frost::Participant;
|
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
use crate::{
|
|
|
|
NetworkError, Network,
|
|
|
|
coin::{Coin, Monero},
|
|
|
|
wallet::{WalletKeys, MemCoinDb, Wallet},
|
|
|
|
};
|
2022-06-09 06:48:53 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct LocalNetwork {
|
2023-02-23 11:50:45 +00:00
|
|
|
i: Participant,
|
2022-06-09 06:48:53 +00:00
|
|
|
size: u16,
|
|
|
|
round: usize,
|
2023-01-01 09:18:23 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2023-02-23 11:50:45 +00:00
|
|
|
rounds: Arc<RwLock<Vec<HashMap<Participant, Vec<u8>>>>>,
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalNetwork {
|
|
|
|
fn new(size: u16) -> Vec<LocalNetwork> {
|
|
|
|
let rounds = Arc::new(RwLock::new(vec![]));
|
|
|
|
let mut res = vec![];
|
|
|
|
for i in 1 ..= size {
|
2023-02-23 11:50:45 +00:00
|
|
|
res.push(LocalNetwork {
|
|
|
|
i: Participant::new(i).unwrap(),
|
|
|
|
size,
|
|
|
|
round: 0,
|
|
|
|
rounds: rounds.clone(),
|
|
|
|
});
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Network for LocalNetwork {
|
2023-02-23 11:50:45 +00:00
|
|
|
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<Participant, Vec<u8>>, NetworkError> {
|
2022-06-09 06:48:53 +00:00
|
|
|
{
|
|
|
|
let mut rounds = self.rounds.write().unwrap();
|
|
|
|
if rounds.len() == self.round {
|
|
|
|
rounds.push(HashMap::new());
|
|
|
|
}
|
2022-10-26 04:17:25 +00:00
|
|
|
rounds[self.round].insert(self.i, data);
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while {
|
|
|
|
let read = self.rounds.try_read().unwrap();
|
|
|
|
read[self.round].len() != usize::from(self.size)
|
|
|
|
} {
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:48:11 +00:00
|
|
|
let mut res = self.rounds.try_read().unwrap()[self.round].clone();
|
|
|
|
res.remove(&self.i);
|
2022-06-09 06:48:53 +00:00
|
|
|
self.round += 1;
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 08:36:19 +00:00
|
|
|
|
2022-06-19 16:19:32 +00:00
|
|
|
async fn test_send<C: Coin + Clone>(coin: C, fee: C::Fee) {
|
2022-10-16 01:39:06 +00:00
|
|
|
// Mine blocks so there's a confirmed block
|
2022-06-28 04:01:20 +00:00
|
|
|
coin.mine_block().await;
|
2022-10-16 01:39:06 +00:00
|
|
|
let latest = coin.get_latest_block_number().await.unwrap();
|
2022-06-09 06:48:53 +00:00
|
|
|
|
2022-06-09 08:34:31 +00:00
|
|
|
let mut keys = frost::tests::key_gen::<_, C::Curve>(&mut OsRng);
|
2023-02-23 11:50:45 +00:00
|
|
|
let threshold = keys[&Participant::new(1).unwrap()].params().t();
|
2022-06-19 16:19:32 +00:00
|
|
|
let mut networks = LocalNetwork::new(threshold);
|
|
|
|
|
2022-06-09 06:48:53 +00:00
|
|
|
let mut wallets = vec![];
|
2022-06-19 16:19:32 +00:00
|
|
|
for i in 1 ..= threshold {
|
2022-06-09 08:34:31 +00:00
|
|
|
let mut wallet = Wallet::new(MemCoinDb::new(), coin.clone());
|
2022-10-16 01:39:06 +00:00
|
|
|
wallet.acknowledge_block(0, latest);
|
2023-02-23 11:50:45 +00:00
|
|
|
wallet.add_keys(&WalletKeys::new(keys.remove(&Participant::new(i).unwrap()).unwrap(), 0));
|
2022-06-09 06:48:53 +00:00
|
|
|
wallets.push(wallet);
|
|
|
|
}
|
|
|
|
|
2022-10-16 01:39:06 +00:00
|
|
|
// Get the chain to a length where blocks have sufficient confirmations
|
|
|
|
while (latest + (C::CONFIRMATIONS - 1)) > coin.get_latest_block_number().await.unwrap() {
|
2022-06-28 04:01:20 +00:00
|
|
|
coin.mine_block().await;
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for wallet in wallets.iter_mut() {
|
|
|
|
// Poll to activate the keys
|
|
|
|
wallet.poll().await.unwrap();
|
|
|
|
}
|
|
|
|
|
2022-06-09 08:34:31 +00:00
|
|
|
coin.test_send(wallets[0].address()).await;
|
2022-06-09 06:48:53 +00:00
|
|
|
|
|
|
|
let mut futures = vec![];
|
2022-06-10 13:36:07 +00:00
|
|
|
for (network, wallet) in networks.iter_mut().zip(wallets.iter_mut()) {
|
2022-06-09 06:48:53 +00:00
|
|
|
wallet.poll().await.unwrap();
|
|
|
|
|
2022-10-16 01:39:06 +00:00
|
|
|
let latest = coin.get_latest_block_number().await.unwrap();
|
|
|
|
wallet.acknowledge_block(1, latest - (C::CONFIRMATIONS - 1));
|
2022-07-15 05:26:07 +00:00
|
|
|
let signable = wallet
|
|
|
|
.prepare_sends(1, vec![(wallet.address(), 10000000000)], fee)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.swap_remove(0);
|
2022-12-09 00:04:35 +00:00
|
|
|
futures.push(wallet.attempt_send(network, signable));
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
2022-06-09 08:34:31 +00:00
|
|
|
|
2022-07-15 05:26:07 +00:00
|
|
|
println!("{:?}", hex::encode(futures::future::join_all(futures).await.swap_remove(0).unwrap().0));
|
2022-05-26 08:36:19 +00:00
|
|
|
}
|
2022-06-09 08:34:31 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn monero() {
|
2022-08-02 19:52:27 +00:00
|
|
|
let monero = Monero::new("http://127.0.0.1:18081".to_string()).await;
|
2022-10-16 03:21:43 +00:00
|
|
|
let fee = monero.get_fee().await;
|
2022-06-19 16:19:32 +00:00
|
|
|
test_send(monero, fee).await;
|
2022-06-09 08:34:31 +00:00
|
|
|
}
|