mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-18 10:57:21 -05:00
Merge pull request #33 from comit-network/monerod
This commit is contained in:
commit
713658244d
@ -6,6 +6,7 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
|
digest_auth = "0.2.3"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
port_check = "0.1"
|
port_check = "0.1"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
@ -13,7 +14,7 @@ reqwest = { version = "0.10", default-features = false, features = ["json", "nat
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
spectral = "0.6"
|
spectral = "0.6"
|
||||||
testcontainers = "0.10"
|
testcontainers = "0.11"
|
||||||
tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time"] }
|
tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
url = "2"
|
url = "2"
|
||||||
|
@ -4,10 +4,10 @@ use testcontainers::{
|
|||||||
Image,
|
Image,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
|
||||||
|
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
|
||||||
pub const MONEROD_RPC_PORT: u16 = 48081;
|
pub const MONEROD_RPC_PORT: u16 = 48081;
|
||||||
pub const MINER_WALLET_RPC_PORT: u16 = 48083;
|
pub const WALLET_RPC_PORT: u16 = 48083;
|
||||||
pub const ALICE_WALLET_RPC_PORT: u16 = 48084;
|
|
||||||
pub const BOB_WALLET_RPC_PORT: u16 = 48085;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Monero {
|
pub struct Monero {
|
||||||
@ -15,6 +15,7 @@ pub struct Monero {
|
|||||||
args: Args,
|
args: Args,
|
||||||
ports: Option<Vec<Port>>,
|
ports: Option<Vec<Port>>,
|
||||||
entrypoint: Option<String>,
|
entrypoint: Option<String>,
|
||||||
|
wait_for_message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image for Monero {
|
impl Image for Monero {
|
||||||
@ -31,9 +32,7 @@ impl Image for Monero {
|
|||||||
container
|
container
|
||||||
.logs()
|
.logs()
|
||||||
.stdout
|
.stdout
|
||||||
.wait_for_message(
|
.wait_for_message(&self.wait_for_message)
|
||||||
"The daemon is running offline and will not attempt to sync to the Monero network",
|
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let additional_sleep_period =
|
let additional_sleep_period =
|
||||||
@ -85,6 +84,7 @@ impl Default for Monero {
|
|||||||
args: Args::default(),
|
args: Args::default(),
|
||||||
ports: None,
|
ports: None,
|
||||||
entrypoint: Some("".into()),
|
entrypoint: Some("".into()),
|
||||||
|
wait_for_message: "core RPC server started ok".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,24 +104,45 @@ impl Monero {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_wallet(self, name: &str, rpc_port: u16) -> Self {
|
pub fn wallet(name: &str, daemon_address: String) -> Self {
|
||||||
let wallet = WalletArgs::new(name, rpc_port);
|
let wallet = WalletArgs::new(name, daemon_address, WALLET_RPC_PORT);
|
||||||
let mut wallet_args = self.args.wallets;
|
let default = Monero::default();
|
||||||
wallet_args.push(wallet);
|
|
||||||
Self {
|
Self {
|
||||||
args: Args {
|
args: Args {
|
||||||
monerod: self.args.monerod,
|
image_args: ImageArgs::WalletArgs(wallet),
|
||||||
wallets: wallet_args,
|
|
||||||
},
|
},
|
||||||
..self
|
wait_for_message: "Run server thread name: RPC".to_string(),
|
||||||
|
..default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
monerod: MonerodArgs,
|
image_args: ImageArgs,
|
||||||
wallets: Vec<WalletArgs>,
|
}
|
||||||
|
|
||||||
|
impl Default for Args {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
image_args: ImageArgs::MonerodArgs(MonerodArgs::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum ImageArgs {
|
||||||
|
MonerodArgs(MonerodArgs),
|
||||||
|
WalletArgs(WalletArgs),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImageArgs {
|
||||||
|
fn args(&self) -> String {
|
||||||
|
match self {
|
||||||
|
ImageArgs::MonerodArgs(monerod_args) => monerod_args.args(),
|
||||||
|
ImageArgs::WalletArgs(wallet_args) => wallet_args.args(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -137,6 +158,7 @@ pub struct MonerodArgs {
|
|||||||
pub rpc_bind_port: u16,
|
pub rpc_bind_port: u16,
|
||||||
pub fixed_difficulty: u32,
|
pub fixed_difficulty: u32,
|
||||||
pub data_dir: String,
|
pub data_dir: String,
|
||||||
|
pub log_level: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -165,6 +187,7 @@ impl Default for MonerodArgs {
|
|||||||
rpc_bind_port: MONEROD_RPC_PORT,
|
rpc_bind_port: MONEROD_RPC_PORT,
|
||||||
fixed_difficulty: 1,
|
fixed_difficulty: 1,
|
||||||
data_dir: "/monero".to_string(),
|
data_dir: "/monero".to_string(),
|
||||||
|
log_level: 2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,17 +241,20 @@ impl MonerodArgs {
|
|||||||
args.push(format!("--fixed-difficulty {}", self.fixed_difficulty));
|
args.push(format!("--fixed-difficulty {}", self.fixed_difficulty));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.log_level != 0 {
|
||||||
|
args.push(format!("--log-level {}", self.log_level));
|
||||||
|
}
|
||||||
|
|
||||||
args.join(" ")
|
args.join(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WalletArgs {
|
impl WalletArgs {
|
||||||
pub fn new(wallet_dir: &str, rpc_port: u16) -> Self {
|
pub fn new(wallet_name: &str, daemon_address: String, rpc_port: u16) -> Self {
|
||||||
let daemon_address = format!("localhost:{}", MONEROD_RPC_PORT);
|
|
||||||
WalletArgs {
|
WalletArgs {
|
||||||
disable_rpc_login: true,
|
disable_rpc_login: true,
|
||||||
confirm_external_bind: true,
|
confirm_external_bind: true,
|
||||||
wallet_dir: wallet_dir.into(),
|
wallet_dir: wallet_name.into(),
|
||||||
rpc_bind_ip: "0.0.0.0".into(),
|
rpc_bind_ip: "0.0.0.0".into(),
|
||||||
rpc_bind_port: rpc_port,
|
rpc_bind_port: rpc_port,
|
||||||
daemon_address,
|
daemon_address,
|
||||||
@ -282,10 +308,7 @@ impl IntoIterator for Args {
|
|||||||
args.push("/bin/bash".into());
|
args.push("/bin/bash".into());
|
||||||
args.push("-c".into());
|
args.push("-c".into());
|
||||||
|
|
||||||
let wallet_args: Vec<String> = self.wallets.iter().map(|wallet| wallet.args()).collect();
|
let cmd = format!("{} ", self.image_args.args());
|
||||||
let wallet_args = wallet_args.join(" & ");
|
|
||||||
|
|
||||||
let cmd = format!("{} & {} ", self.monerod.args(), wallet_args);
|
|
||||||
args.push(cmd);
|
args.push(cmd);
|
||||||
|
|
||||||
args.into_iter()
|
args.into_iter()
|
||||||
|
@ -24,17 +24,18 @@
|
|||||||
pub mod image;
|
pub mod image;
|
||||||
pub mod rpc;
|
pub mod rpc;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use serde::Deserialize;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use testcontainers::{clients::Cli, core::Port, Container, Docker};
|
use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs};
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
image::{ALICE_WALLET_RPC_PORT, BOB_WALLET_RPC_PORT, MINER_WALLET_RPC_PORT, MONEROD_RPC_PORT},
|
image::{
|
||||||
|
MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, MONEROD_RPC_PORT, WALLET_RPC_PORT,
|
||||||
|
},
|
||||||
rpc::{
|
rpc::{
|
||||||
monerod,
|
monerod,
|
||||||
wallet::{self, GetAddress, Transfer},
|
wallet::{self, GetAddress, Refreshed, Transfer},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -44,196 +45,253 @@ const BLOCK_TIME_SECS: u64 = 1;
|
|||||||
/// Poll interval when checking if the wallet has synced with monerod.
|
/// Poll interval when checking if the wallet has synced with monerod.
|
||||||
const WAIT_WALLET_SYNC_MILLIS: u64 = 1000;
|
const WAIT_WALLET_SYNC_MILLIS: u64 = 1000;
|
||||||
|
|
||||||
/// Wallet sub-account indices.
|
#[derive(Clone, Debug)]
|
||||||
const ACCOUNT_INDEX_PRIMARY: u32 = 0;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
|
||||||
pub struct Monero {
|
pub struct Monero {
|
||||||
monerod_rpc_port: u16,
|
monerod: Monerod,
|
||||||
miner_wallet_rpc_port: u16,
|
wallets: Vec<MoneroWalletRpc>,
|
||||||
alice_wallet_rpc_port: u16,
|
prefix: String,
|
||||||
bob_wallet_rpc_port: u16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c> Monero {
|
impl<'c> Monero {
|
||||||
/// Starts a new regtest monero container.
|
/// Starts a new regtest monero container setup consisting out of 1 monerod
|
||||||
pub fn new(cli: &'c Cli) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
|
/// node and n wallets. The containers and network will be prefixed, either
|
||||||
let monerod_rpc_port: u16 =
|
/// randomly generated or as defined in `prefix` if provided. There will
|
||||||
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
/// be 1 miner wallet started automatically. Default monerod container
|
||||||
let miner_wallet_rpc_port: u16 =
|
/// name will be: `prefix`_`monerod` Default miner wallet container name
|
||||||
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
/// will be: `prefix`_`miner` Default network will be: `prefix`_`monero`
|
||||||
let alice_wallet_rpc_port: u16 =
|
pub async fn new(
|
||||||
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
cli: &'c Cli,
|
||||||
let bob_wallet_rpc_port: u16 =
|
prefix: Option<String>,
|
||||||
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
additional_wallets: Vec<String>,
|
||||||
|
) -> Result<(Self, Vec<Container<'c, Cli, image::Monero>>)> {
|
||||||
|
let prefix = format!("{}_", prefix.unwrap_or_else(random_prefix));
|
||||||
|
|
||||||
let image = image::Monero::default()
|
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
||||||
.with_mapped_port(Port {
|
let network = format!("{}{}", prefix, MONEROD_DEFAULT_NETWORK);
|
||||||
local: monerod_rpc_port,
|
|
||||||
internal: MONEROD_RPC_PORT,
|
|
||||||
})
|
|
||||||
.with_mapped_port(Port {
|
|
||||||
local: miner_wallet_rpc_port,
|
|
||||||
internal: MINER_WALLET_RPC_PORT,
|
|
||||||
})
|
|
||||||
.with_wallet("miner", MINER_WALLET_RPC_PORT)
|
|
||||||
.with_mapped_port(Port {
|
|
||||||
local: alice_wallet_rpc_port,
|
|
||||||
internal: ALICE_WALLET_RPC_PORT,
|
|
||||||
})
|
|
||||||
.with_wallet("alice", ALICE_WALLET_RPC_PORT)
|
|
||||||
.with_mapped_port(Port {
|
|
||||||
local: bob_wallet_rpc_port,
|
|
||||||
internal: BOB_WALLET_RPC_PORT,
|
|
||||||
})
|
|
||||||
.with_wallet("bob", BOB_WALLET_RPC_PORT);
|
|
||||||
|
|
||||||
println!("running image ...");
|
tracing::info!("Starting monerod... {}", monerod_name);
|
||||||
let docker = cli.run(image);
|
let (monerod, monerod_container) = Monerod::new(cli, monerod_name, network)?;
|
||||||
println!("image ran");
|
let mut containers = vec![monerod_container];
|
||||||
|
let mut wallets = vec![];
|
||||||
|
|
||||||
|
let miner = format!("{}{}", prefix, "miner");
|
||||||
|
tracing::info!("Starting miner wallet... {}", miner);
|
||||||
|
let (miner_wallet, miner_container) = MoneroWalletRpc::new(cli, &miner, &monerod).await?;
|
||||||
|
|
||||||
|
wallets.push(miner_wallet);
|
||||||
|
containers.push(miner_container);
|
||||||
|
for wallet in additional_wallets.iter() {
|
||||||
|
tracing::info!("Starting wallet: {}...", wallet);
|
||||||
|
let wallet = format!("{}{}", prefix, wallet);
|
||||||
|
let (wallet, container) = MoneroWalletRpc::new(cli, &wallet, &monerod).await?;
|
||||||
|
wallets.push(wallet);
|
||||||
|
containers.push(container);
|
||||||
|
}
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
monerod_rpc_port,
|
monerod,
|
||||||
miner_wallet_rpc_port,
|
wallets,
|
||||||
alice_wallet_rpc_port,
|
prefix,
|
||||||
bob_wallet_rpc_port,
|
},
|
||||||
|
containers,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn monerod(&self) -> &Monerod {
|
||||||
|
&self.monerod
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
|
||||||
|
let name = format!("{}{}", self.prefix, name);
|
||||||
|
let wallet = self
|
||||||
|
.wallets
|
||||||
|
.iter()
|
||||||
|
.find(|wallet| wallet.name.eq(&name))
|
||||||
|
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
|
||||||
|
|
||||||
|
Ok(wallet)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn init(&self, wallet_amount: Vec<(&str, u64)>) -> Result<()> {
|
||||||
|
let miner_wallet = self.wallet("miner")?;
|
||||||
|
let miner_address = miner_wallet.address().await?.address;
|
||||||
|
|
||||||
|
// generate the first 70 as bulk
|
||||||
|
let monerod = &self.monerod;
|
||||||
|
let block = monerod.client().generate_blocks(70, &miner_address).await?;
|
||||||
|
tracing::info!("Generated {:?} blocks", block);
|
||||||
|
miner_wallet.refresh().await?;
|
||||||
|
|
||||||
|
for (wallet, amount) in wallet_amount.iter() {
|
||||||
|
if *amount > 0 {
|
||||||
|
let wallet = self.wallet(wallet)?;
|
||||||
|
let address = wallet.address().await?.address;
|
||||||
|
miner_wallet.transfer(&address, *amount).await?;
|
||||||
|
tracing::info!("Funded {} wallet with {}", wallet.name, amount);
|
||||||
|
monerod.client().generate_blocks(10, &miner_address).await?;
|
||||||
|
wallet.refresh().await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
monerod.start_miner(&miner_address).await?;
|
||||||
|
|
||||||
|
tracing::info!("Waiting for miner wallet to catch up...");
|
||||||
|
let block_height = monerod.client().get_block_count().await?;
|
||||||
|
miner_wallet
|
||||||
|
.wait_for_wallet_height(block_height)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn random_prefix() -> String {
|
||||||
|
use rand::Rng;
|
||||||
|
const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
|
||||||
|
const LEN: usize = 4;
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let prefix: String = (0..LEN)
|
||||||
|
.map(|_| {
|
||||||
|
let idx = rng.gen_range(0, CHARSET.len());
|
||||||
|
CHARSET[idx] as char
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Monerod {
|
||||||
|
rpc_port: u16,
|
||||||
|
name: String,
|
||||||
|
network: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct MoneroWalletRpc {
|
||||||
|
rpc_port: u16,
|
||||||
|
name: String,
|
||||||
|
network: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'c> Monerod {
|
||||||
|
/// Starts a new regtest monero container.
|
||||||
|
fn new(
|
||||||
|
cli: &'c Cli,
|
||||||
|
name: String,
|
||||||
|
network: String,
|
||||||
|
) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
|
||||||
|
let monerod_rpc_port: u16 =
|
||||||
|
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
||||||
|
|
||||||
|
let image = image::Monero::default().with_mapped_port(Port {
|
||||||
|
local: monerod_rpc_port,
|
||||||
|
internal: MONEROD_RPC_PORT,
|
||||||
|
});
|
||||||
|
let run_args = RunArgs::default()
|
||||||
|
.with_name(name.clone())
|
||||||
|
.with_network(network.clone());
|
||||||
|
let docker = cli.run_with_args(image, run_args);
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
rpc_port: monerod_rpc_port,
|
||||||
|
name,
|
||||||
|
network,
|
||||||
},
|
},
|
||||||
docker,
|
docker,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn miner_wallet_rpc_client(&self) -> wallet::Client {
|
pub fn client(&self) -> monerod::Client {
|
||||||
wallet::Client::localhost(self.miner_wallet_rpc_port)
|
monerod::Client::localhost(self.rpc_port)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn alice_wallet_rpc_client(&self) -> wallet::Client {
|
/// Spawns a task to mine blocks in a regular interval to the provided
|
||||||
wallet::Client::localhost(self.alice_wallet_rpc_port)
|
/// address
|
||||||
}
|
pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> {
|
||||||
|
let monerod = self.client();
|
||||||
pub fn bob_wallet_rpc_client(&self) -> wallet::Client {
|
let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
|
||||||
wallet::Client::localhost(self.bob_wallet_rpc_port)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn monerod_rpc_client(&self) -> monerod::Client {
|
|
||||||
monerod::Client::localhost(self.monerod_rpc_port)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initialise by creating a wallet, generating some `blocks`, and starting
|
|
||||||
/// a miner thread that mines to the primary account. Also create two
|
|
||||||
/// sub-accounts, one for Alice and one for Bob. If alice/bob_funding is
|
|
||||||
/// some, the value needs to be > 0.
|
|
||||||
pub async fn init(&self, alice_funding: u64, bob_funding: u64) -> Result<()> {
|
|
||||||
let miner_wallet = self.miner_wallet_rpc_client();
|
|
||||||
let alice_wallet = self.alice_wallet_rpc_client();
|
|
||||||
let bob_wallet = self.bob_wallet_rpc_client();
|
|
||||||
let monerod = self.monerod_rpc_client();
|
|
||||||
|
|
||||||
miner_wallet.create_wallet("miner_wallet").await?;
|
|
||||||
alice_wallet.create_wallet("alice_wallet").await?;
|
|
||||||
bob_wallet.create_wallet("bob_wallet").await?;
|
|
||||||
|
|
||||||
let miner = self.get_address_miner().await?.address;
|
|
||||||
let alice = self.get_address_alice().await?.address;
|
|
||||||
let bob = self.get_address_bob().await?.address;
|
|
||||||
|
|
||||||
let _ = monerod.generate_blocks(70, &miner).await?;
|
|
||||||
self.wait_for_miner_wallet_block_height().await?;
|
|
||||||
|
|
||||||
if alice_funding > 0 {
|
|
||||||
self.fund_account(&alice, &miner, alice_funding).await?;
|
|
||||||
self.wait_for_alice_wallet_block_height().await?;
|
|
||||||
let balance = self.get_balance_alice().await?;
|
|
||||||
debug_assert!(balance == alice_funding);
|
|
||||||
}
|
|
||||||
|
|
||||||
if bob_funding > 0 {
|
|
||||||
self.fund_account(&bob, &miner, bob_funding).await?;
|
|
||||||
self.wait_for_bob_wallet_block_height().await?;
|
|
||||||
let balance = self.get_balance_bob().await?;
|
|
||||||
debug_assert!(balance == bob_funding);
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = tokio::spawn(mine(monerod.clone(), miner));
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fund_account(&self, address: &str, miner: &str, funding: u64) -> Result<()> {
|
|
||||||
let monerod = self.monerod_rpc_client();
|
|
||||||
|
|
||||||
self.transfer_from_primary(funding, address).await?;
|
|
||||||
let _ = monerod.generate_blocks(10, miner).await?;
|
|
||||||
self.wait_for_miner_wallet_block_height().await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn wait_for_miner_wallet_block_height(&self) -> Result<()> {
|
|
||||||
self.wait_for_wallet_height(self.miner_wallet_rpc_client())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn wait_for_alice_wallet_block_height(&self) -> Result<()> {
|
|
||||||
self.wait_for_wallet_height(self.alice_wallet_rpc_client())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn wait_for_bob_wallet_block_height(&self) -> Result<()> {
|
|
||||||
self.wait_for_wallet_height(self.bob_wallet_rpc_client())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
// It takes a little while for the wallet to sync with monerod.
|
|
||||||
async fn wait_for_wallet_height(&self, wallet: wallet::Client) -> Result<()> {
|
|
||||||
let monerod = self.monerod_rpc_client();
|
|
||||||
let height = monerod.get_block_count().await?;
|
|
||||||
|
|
||||||
while wallet.block_height().await?.height < height {
|
|
||||||
time::delay_for(Duration::from_millis(WAIT_WALLET_SYNC_MILLIS)).await;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get addresses for the primary account.
|
|
||||||
async fn get_address_miner(&self) -> Result<GetAddress> {
|
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get addresses for the Alice's account.
|
|
||||||
async fn get_address_alice(&self) -> Result<GetAddress> {
|
|
||||||
let wallet = self.alice_wallet_rpc_client();
|
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get addresses for the Bob's account.
|
|
||||||
async fn get_address_bob(&self) -> Result<GetAddress> {
|
|
||||||
let wallet = self.bob_wallet_rpc_client();
|
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the balance of Alice's account.
|
|
||||||
async fn get_balance_alice(&self) -> Result<u64> {
|
|
||||||
let wallet = self.alice_wallet_rpc_client();
|
|
||||||
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the balance of Bob's account.
|
|
||||||
async fn get_balance_bob(&self) -> Result<u64> {
|
|
||||||
let wallet = self.bob_wallet_rpc_client();
|
|
||||||
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Transfers moneroj from the primary account.
|
|
||||||
async fn transfer_from_primary(&self, amount: u64, address: &str) -> Result<Transfer> {
|
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
|
||||||
wallet
|
|
||||||
.transfer(ACCOUNT_INDEX_PRIMARY, amount, address)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'c> MoneroWalletRpc {
|
||||||
|
/// Starts a new wallet container which is attached to
|
||||||
|
/// MONEROD_DEFAULT_NETWORK and MONEROD_DAEMON_CONTAINER_NAME
|
||||||
|
async fn new(
|
||||||
|
cli: &'c Cli,
|
||||||
|
name: &str,
|
||||||
|
monerod: &Monerod,
|
||||||
|
) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
|
||||||
|
let wallet_rpc_port: u16 =
|
||||||
|
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
||||||
|
|
||||||
|
let daemon_address = format!("{}:{}", monerod.name, MONEROD_RPC_PORT);
|
||||||
|
let image = image::Monero::wallet(&name, daemon_address).with_mapped_port(Port {
|
||||||
|
local: wallet_rpc_port,
|
||||||
|
internal: WALLET_RPC_PORT,
|
||||||
|
});
|
||||||
|
|
||||||
|
let network = monerod.network.clone();
|
||||||
|
let run_args = RunArgs::default()
|
||||||
|
.with_name(name)
|
||||||
|
.with_network(network.clone());
|
||||||
|
let docker = cli.run_with_args(image, run_args);
|
||||||
|
|
||||||
|
// create new wallet
|
||||||
|
wallet::Client::localhost(wallet_rpc_port)
|
||||||
|
.create_wallet(name)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
rpc_port: wallet_rpc_port,
|
||||||
|
name: name.to_string(),
|
||||||
|
network,
|
||||||
|
},
|
||||||
|
docker,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn client(&self) -> wallet::Client {
|
||||||
|
wallet::Client::localhost(self.rpc_port)
|
||||||
|
}
|
||||||
|
|
||||||
|
// It takes a little while for the wallet to sync with monerod.
|
||||||
|
pub async fn wait_for_wallet_height(&self, height: u32) -> Result<()> {
|
||||||
|
let mut retry: u8 = 0;
|
||||||
|
while self.client().block_height().await?.height < height {
|
||||||
|
if retry >= 30 {
|
||||||
|
// ~30 seconds
|
||||||
|
bail!("Wallet could not catch up with monerod after 30 retries.")
|
||||||
|
}
|
||||||
|
time::delay_for(Duration::from_millis(WAIT_WALLET_SYNC_MILLIS)).await;
|
||||||
|
retry += 1;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends amount to address
|
||||||
|
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
|
||||||
|
self.client().transfer(0, amount, address).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn address(&self) -> Result<GetAddress> {
|
||||||
|
self.client().get_address(0).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn balance(&self) -> Result<u64> {
|
||||||
|
self.client().refresh().await?;
|
||||||
|
self.client().get_balance(0).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn refresh(&self) -> Result<Refreshed> {
|
||||||
|
self.client().refresh().await
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
||||||
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
|
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
|
||||||
loop {
|
loop {
|
||||||
@ -241,22 +299,3 @@ async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
|
|||||||
monerod.generate_blocks(1, &reward_address).await?;
|
monerod.generate_blocks(1, &reward_address).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should be able to use monero-rs for this but it does not include all
|
|
||||||
// the fields.
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
|
||||||
pub struct BlockHeader {
|
|
||||||
pub block_size: u32,
|
|
||||||
pub depth: u32,
|
|
||||||
pub difficulty: u32,
|
|
||||||
pub hash: String,
|
|
||||||
pub height: u32,
|
|
||||||
pub major_version: u32,
|
|
||||||
pub minor_version: u32,
|
|
||||||
pub nonce: u32,
|
|
||||||
pub num_txes: u32,
|
|
||||||
pub orphan_status: bool,
|
|
||||||
pub prev_hash: String,
|
|
||||||
pub reward: u64,
|
|
||||||
pub timestamp: u32,
|
|
||||||
}
|
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
use crate::{
|
use crate::rpc::{Request, Response};
|
||||||
rpc::{Request, Response},
|
|
||||||
BlockHeader,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
@ -36,11 +33,23 @@ impl Client {
|
|||||||
amount_of_blocks,
|
amount_of_blocks,
|
||||||
wallet_address: wallet_address.to_owned(),
|
wallet_address: wallet_address.to_owned(),
|
||||||
};
|
};
|
||||||
|
let url = self.url.clone();
|
||||||
|
// // Step 1: Get the auth header
|
||||||
|
// let res = self.inner.get(url.clone()).send().await?;
|
||||||
|
// let headers = res.headers();
|
||||||
|
// let wwwauth = headers["www-authenticate"].to_str()?;
|
||||||
|
//
|
||||||
|
// // Step 2: Given the auth header, sign the digest for the real req.
|
||||||
|
// let tmp_url = url.clone();
|
||||||
|
// let context = AuthContext::new("username", "password", tmp_url.path());
|
||||||
|
// let mut prompt = digest_auth::parse(wwwauth)?;
|
||||||
|
// let answer = prompt.respond(&context)?.to_header_string();
|
||||||
|
|
||||||
let request = Request::new("generateblocks", params);
|
let request = Request::new("generateblocks", params);
|
||||||
|
|
||||||
let response = self
|
let response = self
|
||||||
.inner
|
.inner
|
||||||
.post(self.url.clone())
|
.post(url)
|
||||||
.json(&request)
|
.json(&request)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
@ -125,3 +134,22 @@ struct BlockCount {
|
|||||||
count: u32,
|
count: u32,
|
||||||
status: String,
|
status: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We should be able to use monero-rs for this but it does not include all
|
||||||
|
// the fields.
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
pub struct BlockHeader {
|
||||||
|
pub block_size: u32,
|
||||||
|
pub depth: u32,
|
||||||
|
pub difficulty: u32,
|
||||||
|
pub hash: String,
|
||||||
|
pub height: u32,
|
||||||
|
pub major_version: u32,
|
||||||
|
pub minor_version: u32,
|
||||||
|
pub nonce: u32,
|
||||||
|
pub num_txes: u32,
|
||||||
|
pub orphan_status: bool,
|
||||||
|
pub prev_hash: String,
|
||||||
|
pub reward: u64,
|
||||||
|
pub timestamp: u32,
|
||||||
|
}
|
||||||
|
@ -264,6 +264,24 @@ impl Client {
|
|||||||
let r: Response<GenerateFromKeys> = serde_json::from_str(&response)?;
|
let r: Response<GenerateFromKeys> = serde_json::from_str(&response)?;
|
||||||
Ok(r.result)
|
Ok(r.result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn refresh(&self) -> Result<Refreshed> {
|
||||||
|
let request = Request::new("refresh", "");
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.inner
|
||||||
|
.post(self.url.clone())
|
||||||
|
.json(&request)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.text()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
debug!("refresh RPC response: {}", response);
|
||||||
|
|
||||||
|
let r: Response<Refreshed> = serde_json::from_str(&response)?;
|
||||||
|
Ok(r.result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug, Clone)]
|
#[derive(Serialize, Debug, Clone)]
|
||||||
@ -393,3 +411,9 @@ pub struct GenerateFromKeys {
|
|||||||
pub address: String,
|
pub address: String,
|
||||||
pub info: String,
|
pub info: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||||
|
pub struct Refreshed {
|
||||||
|
pub blocks_fetched: u32,
|
||||||
|
pub received_money: bool,
|
||||||
|
}
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
use monero_harness::Monero;
|
|
||||||
use spectral::prelude::*;
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
|
|
||||||
const ALICE_FUND_AMOUNT: u64 = 1_000_000_000_000;
|
|
||||||
const BOB_FUND_AMOUNT: u64 = 0;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn init_accounts_for_alice_and_bob() {
|
|
||||||
let tc = Cli::default();
|
|
||||||
let (monero, _container) = Monero::new(&tc).unwrap();
|
|
||||||
monero
|
|
||||||
.init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let got_balance_alice = monero
|
|
||||||
.alice_wallet_rpc_client()
|
|
||||||
.get_balance(0)
|
|
||||||
.await
|
|
||||||
.expect("failed to get alice's balance");
|
|
||||||
|
|
||||||
let got_balance_bob = monero
|
|
||||||
.bob_wallet_rpc_client()
|
|
||||||
.get_balance(0)
|
|
||||||
.await
|
|
||||||
.expect("failed to get bob's balance");
|
|
||||||
|
|
||||||
assert_that!(got_balance_alice).is_equal_to(ALICE_FUND_AMOUNT);
|
|
||||||
assert_that!(got_balance_bob).is_equal_to(BOB_FUND_AMOUNT);
|
|
||||||
}
|
|
@ -1,21 +1,26 @@
|
|||||||
use monero_harness::Monero;
|
use monero_harness::Monero;
|
||||||
use spectral::prelude::*;
|
use spectral::prelude::*;
|
||||||
|
use std::time::Duration;
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
|
use tokio::time;
|
||||||
fn init_cli() -> Cli {
|
|
||||||
Cli::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn connect_to_monerod() {
|
async fn init_miner_and_mine_to_miner_address() {
|
||||||
let tc = init_cli();
|
let tc = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&tc).unwrap();
|
let (monero, _monerod_container) = Monero::new(&tc, None, vec![]).await.unwrap();
|
||||||
let cli = monero.monerod_rpc_client();
|
|
||||||
|
|
||||||
let header = cli
|
monero.init(vec![]).await.unwrap();
|
||||||
.get_block_header_by_height(0)
|
|
||||||
.await
|
|
||||||
.expect("failed to get block 0");
|
|
||||||
|
|
||||||
assert_that!(header.height).is_equal_to(0);
|
let monerod = monero.monerod();
|
||||||
|
let miner_wallet = monero.wallet("miner").unwrap();
|
||||||
|
|
||||||
|
let got_miner_balance = miner_wallet.balance().await.unwrap();
|
||||||
|
assert_that!(got_miner_balance).is_greater_than(0);
|
||||||
|
|
||||||
|
time::delay_for(Duration::from_millis(1010)).await;
|
||||||
|
|
||||||
|
// after a bit more than 1 sec another block should have been mined
|
||||||
|
let block_height = monerod.client().get_block_count().await.unwrap();
|
||||||
|
|
||||||
|
assert_that(&block_height).is_greater_than(70);
|
||||||
}
|
}
|
||||||
|
@ -3,86 +3,61 @@ use spectral::prelude::*;
|
|||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn wallet_and_accounts() {
|
async fn fund_transfer_and_check_tx_key() {
|
||||||
let tc = Cli::default();
|
let fund_alice: u64 = 1_000_000_000_000;
|
||||||
let (monero, _container) = Monero::new(&tc).unwrap();
|
|
||||||
let cli = monero.miner_wallet_rpc_client();
|
|
||||||
|
|
||||||
println!("creating wallet ...");
|
|
||||||
|
|
||||||
let _ = cli
|
|
||||||
.create_wallet("wallet")
|
|
||||||
.await
|
|
||||||
.expect("failed to create wallet");
|
|
||||||
|
|
||||||
let got = cli.get_balance(0).await.expect("failed to get balance");
|
|
||||||
let want = 0;
|
|
||||||
|
|
||||||
assert_that!(got).is_equal_to(want);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn create_account_and_retrieve_it() {
|
|
||||||
let tc = Cli::default();
|
|
||||||
let (monero, _container) = Monero::new(&tc).unwrap();
|
|
||||||
let cli = monero.miner_wallet_rpc_client();
|
|
||||||
|
|
||||||
let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
|
|
||||||
|
|
||||||
let _ = cli
|
|
||||||
.create_wallet("wallet")
|
|
||||||
.await
|
|
||||||
.expect("failed to create wallet");
|
|
||||||
|
|
||||||
let _ = cli
|
|
||||||
.create_account(label)
|
|
||||||
.await
|
|
||||||
.expect("failed to create account");
|
|
||||||
|
|
||||||
let mut found: bool = false;
|
|
||||||
let accounts = cli
|
|
||||||
.get_accounts("") // Empty filter.
|
|
||||||
.await
|
|
||||||
.expect("failed to get accounts");
|
|
||||||
for account in accounts.subaddress_accounts {
|
|
||||||
if account.label == label {
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert!(found);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn transfer_and_check_tx_key() {
|
|
||||||
let fund_alice = 1_000_000_000_000;
|
|
||||||
let fund_bob = 0;
|
let fund_bob = 0;
|
||||||
|
let send_to_bob = 5_000_000_000;
|
||||||
|
|
||||||
let tc = Cli::default();
|
let tc = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&tc).unwrap();
|
let (monero, _containers) = Monero::new(&tc, Some("test_".to_string()), vec![
|
||||||
let _ = monero.init(fund_alice, fund_bob).await;
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let alice_wallet = monero.wallet("alice").unwrap();
|
||||||
|
let bob_wallet = monero.wallet("bob").unwrap();
|
||||||
|
let miner_wallet = monero.wallet("miner").unwrap();
|
||||||
|
|
||||||
let address_bob = monero
|
let miner_address = miner_wallet.address().await.unwrap().address;
|
||||||
.bob_wallet_rpc_client()
|
|
||||||
.get_address(0)
|
// fund alice
|
||||||
|
monero
|
||||||
|
.init(vec![("alice", fund_alice), ("bob", fund_bob)])
|
||||||
.await
|
.await
|
||||||
.expect("failed to get Bob's address")
|
.unwrap();
|
||||||
.address;
|
|
||||||
|
|
||||||
let transfer_amount = 100;
|
// check alice balance
|
||||||
let transfer = monero
|
alice_wallet.refresh().await.unwrap();
|
||||||
.alice_wallet_rpc_client()
|
let got_alice_balance = alice_wallet.balance().await.unwrap();
|
||||||
.transfer(0, transfer_amount, &address_bob)
|
assert_that(&got_alice_balance).is_equal_to(fund_alice);
|
||||||
|
|
||||||
|
// transfer from alice to bob
|
||||||
|
let bob_address = bob_wallet.address().await.unwrap().address;
|
||||||
|
let transfer = alice_wallet
|
||||||
|
.transfer(&bob_address, send_to_bob)
|
||||||
.await
|
.await
|
||||||
.expect("transfer failed");
|
.unwrap();
|
||||||
|
|
||||||
|
monero
|
||||||
|
.monerod()
|
||||||
|
.client()
|
||||||
|
.generate_blocks(10, &miner_address)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
bob_wallet.refresh().await.unwrap();
|
||||||
|
let got_bob_balance = bob_wallet.balance().await.unwrap();
|
||||||
|
assert_that(&got_bob_balance).is_equal_to(send_to_bob);
|
||||||
|
|
||||||
|
// check if tx was actually seen
|
||||||
let tx_id = transfer.tx_hash;
|
let tx_id = transfer.tx_hash;
|
||||||
let tx_key = transfer.tx_key;
|
let tx_key = transfer.tx_key;
|
||||||
|
let res = bob_wallet
|
||||||
let cli = monero.miner_wallet_rpc_client();
|
.client()
|
||||||
let res = cli
|
.check_tx_key(&tx_id, &tx_key, &bob_address)
|
||||||
.check_tx_key(&tx_id, &tx_key, &address_bob)
|
|
||||||
.await
|
.await
|
||||||
.expect("failed to check tx by key");
|
.expect("failed to check tx by key");
|
||||||
|
|
||||||
assert_that!(res.received).is_equal_to(transfer_amount);
|
assert_that!(res.received).is_equal_to(send_to_bob);
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ atty = "0.2"
|
|||||||
backoff = { version = "0.2", features = ["tokio"] }
|
backoff = { version = "0.2", features = ["tokio"] }
|
||||||
base64 = "0.12"
|
base64 = "0.12"
|
||||||
bitcoin = { version = "0.23", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version.
|
bitcoin = { version = "0.23", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version.
|
||||||
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "f1bbe6a4540d0741f1f4f22577cfeeadbfd7aaaf" }
|
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "3be644cd9512c157d3337a189298b8257ed54d04" }
|
||||||
derivative = "2"
|
derivative = "2"
|
||||||
futures = { version = "0.3", default-features = false }
|
futures = { version = "0.3", default-features = false }
|
||||||
genawaiter = "0.99.1"
|
genawaiter = "0.99.1"
|
||||||
@ -47,4 +47,4 @@ hyper = "0.13"
|
|||||||
port_check = "0.1"
|
port_check = "0.1"
|
||||||
spectral = "0.6"
|
spectral = "0.6"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
testcontainers = "0.10"
|
testcontainers = "0.11"
|
||||||
|
@ -51,11 +51,21 @@ mod e2e_test {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("swap_".to_string()), vec![
|
||||||
monero.init(xmr_alice, xmr_bob).await.unwrap();
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
monero
|
||||||
|
.init(vec![("alice", xmr_alice), ("bob", xmr_bob)])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let alice_xmr_wallet = Arc::new(swap::monero::Wallet(monero.alice_wallet_rpc_client()));
|
let alice_xmr_wallet = Arc::new(swap::monero::Wallet(
|
||||||
let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.bob_wallet_rpc_client()));
|
monero.wallet("alice").unwrap().client(),
|
||||||
|
));
|
||||||
|
let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet("bob").unwrap().client()));
|
||||||
|
|
||||||
let alice_behaviour = alice::Alice::default();
|
let alice_behaviour = alice::Alice::default();
|
||||||
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||||
@ -92,7 +102,7 @@ mod e2e_test {
|
|||||||
|
|
||||||
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
|
||||||
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
bob_xmr_wallet.as_ref().0.refresh().await.unwrap();
|
||||||
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -28,12 +28,12 @@ tracing = "0.1"
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
backoff = { version = "0.2", features = ["tokio"] }
|
backoff = { version = "0.2", features = ["tokio"] }
|
||||||
base64 = "0.12"
|
base64 = "0.12"
|
||||||
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "f1bbe6a4540d0741f1f4f22577cfeeadbfd7aaaf" }
|
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "3be644cd9512c157d3337a189298b8257ed54d04" }
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
monero-harness = { path = "../monero-harness" }
|
monero-harness = { path = "../monero-harness" }
|
||||||
reqwest = { version = "0.10", default-features = false }
|
reqwest = { version = "0.10", default-features = false }
|
||||||
serde_cbor = "0.11"
|
serde_cbor = "0.11"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
testcontainers = "0.10"
|
testcontainers = "0.11"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.2"
|
tracing-subscriber = "0.2"
|
||||||
|
@ -32,7 +32,12 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("hp".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
@ -75,7 +80,7 @@ mod tests {
|
|||||||
|
|
||||||
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
monero.wallet("bob").unwrap().refresh().await.unwrap();
|
||||||
|
|
||||||
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
@ -106,7 +111,12 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("br".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
@ -158,7 +168,7 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
monero.wait_for_alice_wallet_block_height().await.unwrap();
|
monero.wallet("alice").unwrap().refresh().await.unwrap();
|
||||||
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
|
||||||
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
@ -182,7 +192,13 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _containers) = Monero::new(&cli, Some("ap".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
|
@ -130,10 +130,13 @@ pub async fn init_test(
|
|||||||
|
|
||||||
let fund_alice = TEN_XMR;
|
let fund_alice = TEN_XMR;
|
||||||
let fund_bob = 0;
|
let fund_bob = 0;
|
||||||
monero.init(fund_alice, fund_bob).await.unwrap();
|
monero
|
||||||
|
.init(vec![("alice", fund_alice), ("bob", fund_bob)])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let alice_monero_wallet = wallet::monero::Wallet(monero.alice_wallet_rpc_client());
|
let alice_monero_wallet = wallet::monero::Wallet(monero.wallet("alice").unwrap().client());
|
||||||
let bob_monero_wallet = wallet::monero::Wallet(monero.bob_wallet_rpc_client());
|
let bob_monero_wallet = wallet::monero::Wallet(monero.wallet("bob").unwrap().client());
|
||||||
|
|
||||||
let alice_btc_wallet = wallet::bitcoin::Wallet::new("alice", &bitcoind.node_url)
|
let alice_btc_wallet = wallet::bitcoin::Wallet::new("alice", &bitcoind.node_url)
|
||||||
.await
|
.await
|
||||||
|
@ -238,7 +238,12 @@ async fn swap_as_bob(
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn on_chain_happy_path() {
|
async fn on_chain_happy_path() {
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("ochp".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
||||||
@ -304,7 +309,7 @@ async fn on_chain_happy_path() {
|
|||||||
|
|
||||||
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
monero.wallet("bob").unwrap().refresh().await.unwrap();
|
||||||
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -329,7 +334,12 @@ async fn on_chain_happy_path() {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn on_chain_both_refund_if_alice_never_redeems() {
|
async fn on_chain_both_refund_if_alice_never_redeems() {
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("ocbr".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
||||||
@ -396,7 +406,7 @@ async fn on_chain_both_refund_if_alice_never_redeems() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
monero.wait_for_alice_wallet_block_height().await.unwrap();
|
monero.wallet("alice").unwrap().refresh().await.unwrap();
|
||||||
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
|
||||||
|
|
||||||
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
|
||||||
@ -419,7 +429,12 @@ async fn on_chain_both_refund_if_alice_never_redeems() {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() {
|
async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() {
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let (monero, _container) = Monero::new(&cli).unwrap();
|
let (monero, _container) = Monero::new(&cli, Some("ocap".to_string()), vec![
|
||||||
|
"alice".to_string(),
|
||||||
|
"bob".to_string(),
|
||||||
|
])
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
|
||||||
|
Loading…
Reference in New Issue
Block a user