2020-09-28 06:18:50 +00:00
|
|
|
#![warn(
|
|
|
|
unused_extern_crates,
|
|
|
|
missing_debug_implementations,
|
|
|
|
missing_copy_implementations,
|
|
|
|
rust_2018_idioms,
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::cast_sign_loss,
|
|
|
|
clippy::fallible_impl_from,
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
clippy::cast_possible_wrap,
|
|
|
|
clippy::dbg_macro
|
|
|
|
)]
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
|
|
//! # monero-harness
|
|
|
|
//!
|
|
|
|
//! A simple lib to start a monero container (incl. monerod and
|
|
|
|
//! monero-wallet-rpc). Provides initialisation methods to generate blocks,
|
|
|
|
//! create and fund accounts, and start a continuous mining task mining blocks
|
|
|
|
//! every BLOCK_TIME_SECS seconds.
|
|
|
|
//!
|
|
|
|
//! Also provides standalone JSON RPC clients for monerod and monero-wallet-rpc.
|
|
|
|
|
|
|
|
pub mod image;
|
|
|
|
pub mod rpc;
|
|
|
|
|
2020-11-01 23:02:28 +00:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2020-09-28 06:18:50 +00:00
|
|
|
use std::time::Duration;
|
2020-10-29 01:52:29 +00:00
|
|
|
use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs};
|
2020-09-28 06:18:50 +00:00
|
|
|
use tokio::time;
|
|
|
|
|
|
|
|
use crate::{
|
2020-11-01 23:02:28 +00:00
|
|
|
image::{
|
|
|
|
MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, MONEROD_RPC_PORT, WALLET_RPC_PORT,
|
|
|
|
},
|
2020-09-28 06:18:50 +00:00
|
|
|
rpc::{
|
|
|
|
monerod,
|
2020-11-02 10:12:38 +00:00
|
|
|
wallet::{self, GetAddress, Refreshed, Transfer},
|
2020-09-28 06:18:50 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// How often we mine a block.
|
|
|
|
const BLOCK_TIME_SECS: u64 = 1;
|
|
|
|
|
|
|
|
/// Poll interval when checking if the wallet has synced with monerod.
|
|
|
|
const WAIT_WALLET_SYNC_MILLIS: u64 = 1000;
|
|
|
|
|
2020-11-01 23:02:28 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-10-20 01:18:27 +00:00
|
|
|
pub struct Monero {
|
2020-11-02 05:00:35 +00:00
|
|
|
monerod: Monerod,
|
|
|
|
wallets: Vec<MoneroWalletRpc>,
|
2020-11-03 00:21:44 +00:00
|
|
|
prefix: String,
|
2020-11-02 05:00:35 +00:00
|
|
|
}
|
|
|
|
impl<'c> Monero {
|
|
|
|
/// Starts a new regtest monero container setup consisting out of 1 monerod
|
2020-11-03 00:21:44 +00:00
|
|
|
/// node and n wallets. The containers and network will be prefixed with
|
|
|
|
/// `prefix` if provided. There will be 1 miner wallet started
|
2020-11-02 05:00:35 +00:00
|
|
|
/// automatically. Default monerod container name will be: `monerod`
|
|
|
|
/// Default miner wallet container name will be: `miner`
|
2020-11-02 07:58:52 +00:00
|
|
|
/// Default network will be: `monero`
|
2020-11-03 00:21:44 +00:00
|
|
|
/// Each default will be prefixed with `prefix`.
|
2020-11-02 05:00:35 +00:00
|
|
|
pub async fn new(
|
|
|
|
cli: &'c Cli,
|
2020-11-03 00:21:44 +00:00
|
|
|
prefix: Option<String>,
|
2020-11-02 05:00:35 +00:00
|
|
|
additional_wallets: Vec<String>,
|
|
|
|
) -> Result<(Self, Vec<Container<'c, Cli, image::Monero>>)> {
|
2020-11-03 00:21:44 +00:00
|
|
|
let prefix = prefix.unwrap_or_else(|| "".to_string());
|
2020-11-02 07:58:52 +00:00
|
|
|
|
2020-11-03 00:21:44 +00:00
|
|
|
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
|
|
|
let network = format!("{}{}", prefix, MONEROD_DEFAULT_NETWORK);
|
2020-11-02 05:00:35 +00:00
|
|
|
|
|
|
|
tracing::info!("Starting monerod...");
|
|
|
|
let (monerod, monerod_container) = Monerod::new(cli, monerod_name, network)?;
|
|
|
|
let mut containers = vec![monerod_container];
|
|
|
|
let mut wallets = vec![];
|
|
|
|
|
2020-11-02 10:12:38 +00:00
|
|
|
tracing::info!("Starting miner wallet...");
|
2020-11-03 00:21:44 +00:00
|
|
|
let miner = format!("{}{}", prefix, "miner");
|
2020-11-02 07:58:52 +00:00
|
|
|
let (miner_wallet, miner_container) = MoneroWalletRpc::new(cli, &miner, &monerod).await?;
|
2020-11-02 05:00:35 +00:00
|
|
|
|
|
|
|
wallets.push(miner_wallet);
|
|
|
|
containers.push(miner_container);
|
|
|
|
for wallet in additional_wallets.iter() {
|
|
|
|
tracing::info!("Starting wallet: {}...", wallet);
|
2020-11-03 00:21:44 +00:00
|
|
|
let wallet = format!("{}{}", prefix, wallet);
|
2020-11-02 07:58:52 +00:00
|
|
|
let (wallet, container) = MoneroWalletRpc::new(cli, &wallet, &monerod).await?;
|
2020-11-02 05:00:35 +00:00
|
|
|
wallets.push(wallet);
|
|
|
|
containers.push(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
|
|
|
monerod,
|
|
|
|
wallets,
|
2020-11-03 00:21:44 +00:00
|
|
|
prefix,
|
2020-11-02 05:00:35 +00:00
|
|
|
},
|
|
|
|
containers,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn monerod(&self) -> &Monerod {
|
|
|
|
&self.monerod
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
|
2020-11-03 00:21:44 +00:00
|
|
|
let name = format!("{}{}", self.prefix, name);
|
2020-11-02 05:00:35 +00:00
|
|
|
let wallet = self
|
|
|
|
.wallets
|
|
|
|
.iter()
|
2020-11-02 07:58:52 +00:00
|
|
|
.find(|wallet| wallet.name.eq(&name))
|
2020-11-02 05:00:35 +00:00
|
|
|
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
|
|
|
|
|
|
|
|
Ok(wallet)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:46:25 +00:00
|
|
|
pub async fn init(&self, wallet_amount: Vec<(&str, u64)>) -> Result<()> {
|
2020-11-02 10:12:38 +00:00
|
|
|
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.inner().generate_blocks(70, &miner_address).await?;
|
|
|
|
tracing::info!("Generated {:?} blocks", block);
|
|
|
|
miner_wallet.refresh().await?;
|
|
|
|
|
2020-11-03 00:46:25 +00:00
|
|
|
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.inner().generate_blocks(10, &miner_address).await?;
|
|
|
|
wallet.refresh().await?;
|
|
|
|
}
|
2020-11-02 10:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
monerod.start_miner(&miner_address).await?;
|
|
|
|
|
|
|
|
tracing::info!("Waiting for miner wallet to catch up...");
|
|
|
|
let block_height = monerod.inner().get_block_count().await?;
|
|
|
|
miner_wallet
|
|
|
|
.wait_for_wallet_height(block_height)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-02 05:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Monerod {
|
2020-11-01 23:02:28 +00:00
|
|
|
rpc_port: u16,
|
|
|
|
name: String,
|
2020-11-02 05:00:35 +00:00
|
|
|
network: String,
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct MoneroWalletRpc {
|
|
|
|
rpc_port: u16,
|
|
|
|
name: String,
|
|
|
|
network: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> Monerod {
|
2020-09-28 06:18:50 +00:00
|
|
|
/// Starts a new regtest monero container.
|
2020-11-02 05:00:35 +00:00
|
|
|
fn new(
|
|
|
|
cli: &'c Cli,
|
|
|
|
name: String,
|
|
|
|
network: String,
|
|
|
|
) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
|
2020-10-23 00:28:58 +00:00
|
|
|
let monerod_rpc_port: u16 =
|
|
|
|
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
|
2020-09-28 06:18:50 +00:00
|
|
|
|
2020-10-29 01:52:29 +00:00
|
|
|
let image = image::Monero::default().with_mapped_port(Port {
|
|
|
|
local: monerod_rpc_port,
|
|
|
|
internal: MONEROD_RPC_PORT,
|
|
|
|
});
|
|
|
|
let run_args = RunArgs::default()
|
2020-11-02 05:00:35 +00:00
|
|
|
.with_name(name.clone())
|
|
|
|
.with_network(network.clone());
|
2020-11-01 23:02:28 +00:00
|
|
|
let docker = cli.run_with_args(image, run_args);
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
|
|
|
rpc_port: monerod_rpc_port,
|
2020-11-02 05:00:35 +00:00
|
|
|
name,
|
|
|
|
network,
|
2020-11-01 23:02:28 +00:00
|
|
|
},
|
|
|
|
docker,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
pub fn inner(&self) -> monerod::Client {
|
|
|
|
monerod::Client::localhost(self.rpc_port)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawns a task to mine blocks in a regular interval to the provided
|
|
|
|
/// address
|
|
|
|
pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> {
|
|
|
|
let monerod = self.inner();
|
2020-11-02 10:12:38 +00:00
|
|
|
let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
|
2020-11-02 05:00:35 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> MoneroWalletRpc {
|
2020-11-02 03:42:08 +00:00
|
|
|
/// Starts a new wallet container which is attached to
|
|
|
|
/// MONEROD_DEFAULT_NETWORK and MONEROD_DAEMON_CONTAINER_NAME
|
2020-11-02 05:00:35 +00:00
|
|
|
async fn new(
|
2020-11-01 23:02:28 +00:00
|
|
|
cli: &'c Cli,
|
|
|
|
name: &str,
|
2020-11-02 05:00:35 +00:00
|
|
|
monerod: &Monerod,
|
2020-11-01 23:02:28 +00:00
|
|
|
) -> 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"))?;
|
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
let daemon_address = format!("{}:{}", monerod.name, MONEROD_RPC_PORT);
|
|
|
|
let image = image::Monero::wallet(&name, daemon_address).with_mapped_port(Port {
|
2020-11-01 23:02:28 +00:00
|
|
|
local: wallet_rpc_port,
|
|
|
|
internal: WALLET_RPC_PORT,
|
|
|
|
});
|
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
let network = monerod.network.clone();
|
2020-11-01 23:02:28 +00:00
|
|
|
let run_args = RunArgs::default()
|
|
|
|
.with_name(name)
|
2020-11-02 05:00:35 +00:00
|
|
|
.with_network(network.clone());
|
2020-10-29 01:52:29 +00:00
|
|
|
let docker = cli.run_with_args(image, run_args);
|
2020-09-28 06:18:50 +00:00
|
|
|
|
2020-11-01 23:02:28 +00:00
|
|
|
// 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(),
|
2020-11-02 05:00:35 +00:00
|
|
|
network,
|
2020-11-01 23:02:28 +00:00
|
|
|
},
|
|
|
|
docker,
|
|
|
|
))
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
pub fn inner(&self) -> wallet::Client {
|
2020-11-01 23:02:28 +00:00
|
|
|
wallet::Client::localhost(self.rpc_port)
|
|
|
|
}
|
2020-09-28 06:18:50 +00:00
|
|
|
|
2020-11-01 23:02:28 +00:00
|
|
|
// 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;
|
2020-11-02 05:00:35 +00:00
|
|
|
while self.inner().block_height().await?.height < height {
|
2020-11-01 23:02:28 +00:00
|
|
|
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(())
|
|
|
|
}
|
2020-11-02 03:42:08 +00:00
|
|
|
|
|
|
|
/// Sends amount to address
|
|
|
|
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
|
2020-11-03 00:46:25 +00:00
|
|
|
self.inner().transfer(0, amount, address).await
|
2020-11-02 03:42:08 +00:00
|
|
|
}
|
2020-11-01 23:02:28 +00:00
|
|
|
|
2020-11-02 05:00:35 +00:00
|
|
|
pub async fn address(&self) -> Result<GetAddress> {
|
|
|
|
self.inner().get_address(0).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn balance(&self) -> Result<u64> {
|
2020-11-02 07:58:52 +00:00
|
|
|
self.inner().refresh().await?;
|
2020-11-02 05:00:35 +00:00
|
|
|
self.inner().get_balance(0).await
|
|
|
|
}
|
2020-11-02 10:12:38 +00:00
|
|
|
|
|
|
|
pub async fn refresh(&self) -> Result<Refreshed> {
|
|
|
|
self.inner().refresh().await
|
|
|
|
}
|
2020-11-02 05:00:35 +00:00
|
|
|
}
|
2020-11-01 23:02:28 +00:00
|
|
|
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
|
|
|
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
|
|
|
|
loop {
|
|
|
|
time::delay_for(Duration::from_secs(BLOCK_TIME_SECS)).await;
|
|
|
|
monerod.generate_blocks(1, &reward_address).await?;
|
|
|
|
}
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|