2020-10-07 20:49:53 -04:00
|
|
|
use crate::harness::{transport::Transport, wallet};
|
2020-09-29 01:36:50 -04:00
|
|
|
use anyhow::Result;
|
|
|
|
use rand::{CryptoRng, RngCore};
|
|
|
|
use xmr_btc::{alice, bob};
|
|
|
|
|
|
|
|
// TODO: merge this with bob node
|
|
|
|
// This struct is responsible for I/O
|
2020-10-19 21:18:27 -04:00
|
|
|
pub struct AliceNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
transport: Transport<alice::Message, bob::Message>,
|
|
|
|
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
2020-10-19 21:18:27 -04:00
|
|
|
pub monero_wallet: wallet::monero::Wallet,
|
2020-09-29 01:36:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:18:27 -04:00
|
|
|
impl AliceNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
pub fn new(
|
|
|
|
transport: Transport<alice::Message, bob::Message>,
|
|
|
|
bitcoin_wallet: wallet::bitcoin::Wallet,
|
2020-10-19 21:18:27 -04:00
|
|
|
monero_wallet: wallet::monero::Wallet,
|
|
|
|
) -> AliceNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
Self {
|
|
|
|
transport,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:18:27 -04:00
|
|
|
pub async fn run_alice_until<R: RngCore + CryptoRng>(
|
|
|
|
alice: &mut AliceNode,
|
2020-09-29 01:36:50 -04:00
|
|
|
initial_state: alice::State,
|
|
|
|
is_state: fn(&alice::State) -> bool,
|
|
|
|
rng: &mut R,
|
|
|
|
) -> Result<alice::State> {
|
|
|
|
let mut result = initial_state;
|
|
|
|
loop {
|
|
|
|
result = alice::next_state(
|
|
|
|
&alice.bitcoin_wallet,
|
|
|
|
&alice.monero_wallet,
|
|
|
|
&mut alice.transport,
|
|
|
|
result,
|
|
|
|
rng,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
if is_state(&result) {
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: merge this with alice node
|
|
|
|
// This struct is responsible for I/O
|
2020-10-19 21:18:27 -04:00
|
|
|
pub struct BobNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
transport: Transport<bob::Message, alice::Message>,
|
|
|
|
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
2020-10-19 21:18:27 -04:00
|
|
|
pub monero_wallet: wallet::monero::Wallet,
|
2020-09-29 01:36:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:18:27 -04:00
|
|
|
impl BobNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
pub fn new(
|
|
|
|
transport: Transport<bob::Message, alice::Message>,
|
|
|
|
bitcoin_wallet: wallet::bitcoin::Wallet,
|
2020-10-19 21:18:27 -04:00
|
|
|
monero_wallet: wallet::monero::Wallet,
|
|
|
|
) -> BobNode {
|
2020-09-29 01:36:50 -04:00
|
|
|
Self {
|
|
|
|
transport,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:18:27 -04:00
|
|
|
pub async fn run_bob_until<R: RngCore + CryptoRng>(
|
|
|
|
bob: &mut BobNode,
|
2020-09-29 01:36:50 -04:00
|
|
|
initial_state: bob::State,
|
|
|
|
is_state: fn(&bob::State) -> bool,
|
|
|
|
rng: &mut R,
|
|
|
|
) -> Result<bob::State> {
|
|
|
|
let mut result = initial_state;
|
|
|
|
loop {
|
|
|
|
result = bob::next_state(
|
|
|
|
&bob.bitcoin_wallet,
|
|
|
|
&bob.monero_wallet,
|
|
|
|
&mut bob.transport,
|
|
|
|
result,
|
|
|
|
rng,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
if is_state(&result) {
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|