2022-06-09 06:48:53 +00:00
|
|
|
use std::{sync::{Arc, RwLock}, collections::HashMap};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2022-06-01 07:30:57 +00:00
|
|
|
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
2022-06-24 23:53:41 +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 {
|
|
|
|
i: u16,
|
|
|
|
size: u16,
|
|
|
|
round: usize,
|
|
|
|
rounds: Arc<RwLock<Vec<HashMap<u16, Vec<u8>>>>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalNetwork {
|
|
|
|
fn new(size: u16) -> Vec<LocalNetwork> {
|
|
|
|
let rounds = Arc::new(RwLock::new(vec![]));
|
|
|
|
let mut res = vec![];
|
|
|
|
for i in 1 ..= size {
|
|
|
|
res.push(LocalNetwork { i, size, round: 0, rounds: rounds.clone() });
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Network for LocalNetwork {
|
|
|
|
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Vec<u8>>, NetworkError> {
|
|
|
|
{
|
|
|
|
let mut rounds = self.rounds.write().unwrap();
|
|
|
|
if rounds.len() == self.round {
|
|
|
|
rounds.push(HashMap::new());
|
|
|
|
}
|
|
|
|
rounds[self.round].insert(self.i, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
while {
|
|
|
|
let read = self.rounds.try_read().unwrap();
|
|
|
|
read[self.round].len() != usize::from(self.size)
|
|
|
|
} {
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = self.rounds.try_read().unwrap()[self.round].clone();
|
|
|
|
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-06-09 06:48:53 +00:00
|
|
|
// Mine a block so there's a confirmed height
|
2022-06-28 04:01:20 +00:00
|
|
|
coin.mine_block().await;
|
2022-06-09 08:34:31 +00:00
|
|
|
let height = coin.get_height().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);
|
2022-06-19 16:19:32 +00:00
|
|
|
let threshold = keys[&1].params().t();
|
|
|
|
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-06-09 06:48:53 +00:00
|
|
|
wallet.acknowledge_height(0, height);
|
|
|
|
wallet.add_keys(
|
|
|
|
&WalletKeys::new(Arc::try_unwrap(keys.remove(&i).take().unwrap()).unwrap(), 0)
|
|
|
|
);
|
|
|
|
wallets.push(wallet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the chain to a height where blocks have sufficient confirmations
|
2022-06-09 08:34:31 +00:00
|
|
|
while (height + C::CONFIRMATIONS) > coin.get_height().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-06-09 08:34:31 +00:00
|
|
|
let height = coin.get_height().await.unwrap();
|
2022-06-09 06:48:53 +00:00
|
|
|
wallet.acknowledge_height(1, height - 10);
|
|
|
|
let signable = wallet.prepare_sends(
|
|
|
|
1,
|
2022-06-19 16:19:32 +00:00
|
|
|
vec![(wallet.address(), 10000000000)],
|
|
|
|
fee
|
2022-06-09 06:48:53 +00:00
|
|
|
).await.unwrap().1.swap_remove(0);
|
2022-06-19 16:19:32 +00:00
|
|
|
futures.push(
|
|
|
|
wallet.attempt_send(network, signable, (1 ..= threshold).into_iter().collect::<Vec<_>>())
|
|
|
|
);
|
2022-06-09 06:48:53 +00:00
|
|
|
}
|
2022-06-09 08:34:31 +00:00
|
|
|
|
2022-06-09 06:48:53 +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-06-19 16:19:32 +00:00
|
|
|
let monero = Monero::new("http://127.0.0.1:18081".to_string());
|
|
|
|
let fee = monero.rpc.get_fee().await.unwrap();
|
|
|
|
test_send(monero, fee).await;
|
2022-06-09 08:34:31 +00:00
|
|
|
}
|