2020-09-28 02:18:50 -04: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;
|
|
|
|
|
2021-04-19 03:26:11 -04:00
|
|
|
use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
|
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
2021-05-10 20:30:13 -04:00
|
|
|
use monero_rpc::monerod;
|
|
|
|
use monero_rpc::monerod::MonerodRpc as _;
|
|
|
|
use monero_rpc::wallet::{self, GetAddress, MoneroWalletRpc as _, Refreshed, Transfer};
|
2021-01-20 19:20:57 -05:00
|
|
|
use std::time::Duration;
|
2021-05-10 20:30:13 -04:00
|
|
|
use testcontainers::clients::Cli;
|
|
|
|
use testcontainers::{Container, Docker, RunArgs};
|
2021-01-20 19:20:57 -05:00
|
|
|
use tokio::time;
|
2020-09-28 02:18:50 -04: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 18:02:28 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2020-10-19 21:18:27 -04:00
|
|
|
pub struct Monero {
|
2020-11-02 00:00:35 -05:00
|
|
|
monerod: Monerod,
|
|
|
|
wallets: Vec<MoneroWalletRpc>,
|
|
|
|
}
|
|
|
|
impl<'c> Monero {
|
|
|
|
/// Starts a new regtest monero container setup consisting out of 1 monerod
|
2021-03-02 18:56:49 -05:00
|
|
|
/// node and n wallets. The docker container and network will be prefixed
|
|
|
|
/// with a randomly generated `prefix`. One miner wallet is started
|
|
|
|
/// automatically.
|
|
|
|
/// monerod container name is: `prefix`_`monerod`
|
|
|
|
/// network is: `prefix`_`monero`
|
|
|
|
/// miner wallet container name is: `miner`
|
2020-11-02 00:00:35 -05:00
|
|
|
pub async fn new(
|
|
|
|
cli: &'c Cli,
|
2021-04-15 22:05:12 -04:00
|
|
|
additional_wallets: Vec<&'static str>,
|
2021-04-19 03:26:11 -04:00
|
|
|
) -> Result<(
|
|
|
|
Self,
|
|
|
|
Container<'c, Cli, image::Monerod>,
|
|
|
|
Vec<Container<'c, Cli, image::MoneroWalletRpc>>,
|
|
|
|
)> {
|
2021-03-02 18:56:49 -05:00
|
|
|
let prefix = format!("{}_", random_prefix());
|
2020-11-02 19:21:44 -05:00
|
|
|
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
|
|
|
let network = format!("{}{}", prefix, MONEROD_DEFAULT_NETWORK);
|
2020-11-02 00:00:35 -05:00
|
|
|
|
2021-03-02 18:56:49 -05:00
|
|
|
tracing::info!("Starting monerod: {}", monerod_name);
|
2020-11-02 00:00:35 -05:00
|
|
|
let (monerod, monerod_container) = Monerod::new(cli, monerod_name, network)?;
|
2021-04-19 03:26:11 -04:00
|
|
|
let mut containers = vec![];
|
2020-11-02 00:00:35 -05:00
|
|
|
let mut wallets = vec![];
|
|
|
|
|
2021-03-02 18:56:49 -05:00
|
|
|
let miner = "miner";
|
|
|
|
tracing::info!("Starting miner wallet: {}", miner);
|
2021-04-13 02:17:33 -04:00
|
|
|
let (miner_wallet, miner_container) =
|
2022-03-12 06:05:36 -05:00
|
|
|
MoneroWalletRpc::new(cli, miner, &monerod, prefix.clone()).await?;
|
2020-11-02 00:00:35 -05:00
|
|
|
|
|
|
|
wallets.push(miner_wallet);
|
|
|
|
containers.push(miner_container);
|
|
|
|
for wallet in additional_wallets.iter() {
|
2021-03-02 18:56:49 -05:00
|
|
|
tracing::info!("Starting wallet: {}", wallet);
|
2021-05-28 02:02:14 -04:00
|
|
|
|
|
|
|
// Create new wallet, the RPC sometimes has startup problems so we allow retries
|
|
|
|
// (drop the container that failed and try again) Times out after
|
|
|
|
// trying for 5 minutes
|
|
|
|
let (wallet, container) = tokio::time::timeout(Duration::from_secs(300), async {
|
|
|
|
loop {
|
2022-03-12 06:05:36 -05:00
|
|
|
let result = MoneroWalletRpc::new(cli, wallet, &monerod, prefix.clone()).await;
|
2021-05-28 02:02:14 -04:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(tuple) => { return tuple; }
|
|
|
|
Err(e) => { tracing::warn!("Monero wallet RPC emitted error {} - retrying to create wallet in 2 seconds...", e); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).await.context("All retry attempts for creating a wallet exhausted")?;
|
|
|
|
|
2020-11-02 00:00:35 -05:00
|
|
|
wallets.push(wallet);
|
|
|
|
containers.push(container);
|
|
|
|
}
|
|
|
|
|
2021-04-19 03:26:11 -04:00
|
|
|
Ok((Self { monerod, wallets }, monerod_container, containers))
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn monerod(&self) -> &Monerod {
|
|
|
|
&self.monerod
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
|
|
|
|
let wallet = self
|
|
|
|
.wallets
|
|
|
|
.iter()
|
2020-11-02 02:58:52 -05:00
|
|
|
.find(|wallet| wallet.name.eq(&name))
|
2020-11-02 00:00:35 -05:00
|
|
|
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
|
|
|
|
|
|
|
|
Ok(wallet)
|
|
|
|
}
|
|
|
|
|
2021-04-08 04:56:26 -04:00
|
|
|
pub async fn init_miner(&self) -> Result<()> {
|
2020-11-02 05:12:38 -05:00
|
|
|
let miner_wallet = self.wallet("miner")?;
|
|
|
|
let miner_address = miner_wallet.address().await?.address;
|
|
|
|
|
2022-08-09 06:36:18 -04:00
|
|
|
// generate the first 120 as bulk
|
|
|
|
let amount_of_blocks = 120;
|
2020-11-02 05:12:38 -05:00
|
|
|
let monerod = &self.monerod;
|
2021-04-15 04:39:59 -04:00
|
|
|
let res = monerod
|
|
|
|
.client()
|
2022-08-09 06:36:18 -04:00
|
|
|
.generateblocks(amount_of_blocks, miner_address.clone())
|
2021-04-15 04:39:59 -04:00
|
|
|
.await?;
|
2020-12-09 18:10:23 -05:00
|
|
|
tracing::info!("Generated {:?} blocks", res.blocks.len());
|
2020-11-02 05:12:38 -05:00
|
|
|
miner_wallet.refresh().await?;
|
|
|
|
|
2021-04-08 04:56:26 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn init_wallet(&self, name: &str, amount_in_outputs: Vec<u64>) -> Result<()> {
|
|
|
|
let miner_wallet = self.wallet("miner")?;
|
|
|
|
let miner_address = miner_wallet.address().await?.address;
|
|
|
|
let monerod = &self.monerod;
|
|
|
|
|
|
|
|
let wallet = self.wallet(name)?;
|
|
|
|
let address = wallet.address().await?.address;
|
|
|
|
|
|
|
|
for amount in amount_in_outputs {
|
|
|
|
if amount > 0 {
|
|
|
|
miner_wallet.transfer(&address, amount).await?;
|
2020-11-02 19:46:25 -05:00
|
|
|
tracing::info!("Funded {} wallet with {}", wallet.name, amount);
|
2021-04-15 04:39:59 -04:00
|
|
|
monerod
|
|
|
|
.client()
|
|
|
|
.generateblocks(10, miner_address.clone())
|
|
|
|
.await?;
|
2020-11-02 19:46:25 -05:00
|
|
|
wallet.refresh().await?;
|
|
|
|
}
|
2020-11-02 05:12:38 -05:00
|
|
|
}
|
|
|
|
|
2021-04-08 04:56:26 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn start_miner(&self) -> Result<()> {
|
|
|
|
let miner_wallet = self.wallet("miner")?;
|
|
|
|
let miner_address = miner_wallet.address().await?.address;
|
|
|
|
let monerod = &self.monerod;
|
|
|
|
|
2020-11-02 05:12:38 -05:00
|
|
|
monerod.start_miner(&miner_address).await?;
|
|
|
|
|
|
|
|
tracing::info!("Waiting for miner wallet to catch up...");
|
2021-04-15 04:39:59 -04:00
|
|
|
let block_height = monerod.client().get_block_count().await?.count;
|
2020-11-30 15:41:22 -05:00
|
|
|
miner_wallet
|
|
|
|
.wait_for_wallet_height(block_height)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-11-02 05:12:38 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-08 04:56:26 -04:00
|
|
|
|
|
|
|
pub async fn init_and_start_miner(&self) -> Result<()> {
|
|
|
|
self.init_miner().await?;
|
|
|
|
self.start_miner().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:25:22 -05:00
|
|
|
fn random_prefix() -> String {
|
|
|
|
use rand::Rng;
|
2021-04-15 23:11:26 -04:00
|
|
|
|
|
|
|
rand::thread_rng()
|
|
|
|
.sample_iter(rand::distributions::Alphanumeric)
|
|
|
|
.take(4)
|
|
|
|
.collect()
|
2020-11-02 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 00:00:35 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Monerod {
|
2020-11-01 18:02:28 -05:00
|
|
|
name: String,
|
2020-11-02 00:00:35 -05:00
|
|
|
network: String,
|
2021-04-18 20:10:46 -04:00
|
|
|
client: monerod::Client,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2020-11-02 00:00:35 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct MoneroWalletRpc {
|
|
|
|
name: String,
|
2021-04-18 20:10:46 -04:00
|
|
|
client: wallet::Client,
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> Monerod {
|
2020-09-28 02:18:50 -04:00
|
|
|
/// Starts a new regtest monero container.
|
2020-11-02 00:00:35 -05:00
|
|
|
fn new(
|
|
|
|
cli: &'c Cli,
|
|
|
|
name: String,
|
|
|
|
network: String,
|
2021-04-19 03:26:11 -04:00
|
|
|
) -> Result<(Self, Container<'c, Cli, image::Monerod>)> {
|
|
|
|
let image = image::Monerod::default();
|
2020-10-28 21:52:29 -04:00
|
|
|
let run_args = RunArgs::default()
|
2020-11-02 00:00:35 -05:00
|
|
|
.with_name(name.clone())
|
2021-04-19 03:26:11 -04:00
|
|
|
.with_network(network.clone());
|
|
|
|
let container = cli.run_with_args(image, run_args);
|
|
|
|
let monerod_rpc_port = container
|
|
|
|
.get_host_port(RPC_PORT)
|
|
|
|
.context("port not exposed")?;
|
2020-11-01 18:02:28 -05:00
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
2020-11-02 00:00:35 -05:00
|
|
|
name,
|
|
|
|
network,
|
2021-04-18 20:10:46 -04:00
|
|
|
client: monerod::Client::localhost(monerod_rpc_port)?,
|
2020-11-01 18:02:28 -05:00
|
|
|
},
|
2021-04-19 03:26:11 -04:00
|
|
|
container,
|
2020-11-01 18:02:28 -05:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2021-04-18 20:10:46 -04:00
|
|
|
pub fn client(&self) -> &monerod::Client {
|
|
|
|
&self.client
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<()> {
|
2021-04-18 20:10:46 -04:00
|
|
|
let monerod = self.client().clone();
|
2020-11-02 05:12:38 -05:00
|
|
|
let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
|
2020-11-02 00:00:35 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> MoneroWalletRpc {
|
2020-11-01 22:42:08 -05:00
|
|
|
/// Starts a new wallet container which is attached to
|
|
|
|
/// MONEROD_DEFAULT_NETWORK and MONEROD_DAEMON_CONTAINER_NAME
|
2020-11-02 00:00:35 -05:00
|
|
|
async fn new(
|
2020-11-01 18:02:28 -05:00
|
|
|
cli: &'c Cli,
|
|
|
|
name: &str,
|
2020-11-02 00:00:35 -05:00
|
|
|
monerod: &Monerod,
|
2021-04-13 02:17:33 -04:00
|
|
|
prefix: String,
|
2021-04-19 03:26:11 -04:00
|
|
|
) -> Result<(Self, Container<'c, Cli, image::MoneroWalletRpc>)> {
|
|
|
|
let daemon_address = format!("{}:{}", monerod.name, RPC_PORT);
|
2022-03-12 06:05:36 -05:00
|
|
|
let image = image::MoneroWalletRpc::new(name, daemon_address);
|
2020-11-01 18:02:28 -05:00
|
|
|
|
2020-11-02 00:00:35 -05:00
|
|
|
let network = monerod.network.clone();
|
2020-11-01 18:02:28 -05:00
|
|
|
let run_args = RunArgs::default()
|
2021-04-13 02:17:33 -04:00
|
|
|
// prefix the container name so we can run multiple tests
|
|
|
|
.with_name(format!("{}{}", prefix, name))
|
2021-04-19 03:26:11 -04:00
|
|
|
.with_network(network.clone());
|
|
|
|
let container = cli.run_with_args(image, run_args);
|
|
|
|
let wallet_rpc_port = container
|
|
|
|
.get_host_port(RPC_PORT)
|
|
|
|
.context("port not exposed")?;
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2021-04-18 20:10:46 -04:00
|
|
|
let client = wallet::Client::localhost(wallet_rpc_port)?;
|
|
|
|
|
2021-05-28 02:02:14 -04:00
|
|
|
client
|
|
|
|
.create_wallet(name.to_owned(), "English".to_owned())
|
|
|
|
.await?;
|
2020-11-01 18:02:28 -05:00
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
|
|
|
name: name.to_string(),
|
2021-04-18 20:10:46 -04:00
|
|
|
client,
|
2020-11-01 18:02:28 -05:00
|
|
|
},
|
2021-04-19 03:26:11 -04:00
|
|
|
container,
|
2020-11-01 18:02:28 -05:00
|
|
|
))
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 20:10:46 -04:00
|
|
|
pub fn client(&self) -> &wallet::Client {
|
|
|
|
&self.client
|
2020-11-01 18:02:28 -05:00
|
|
|
}
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-11-01 18:02:28 -05: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;
|
2021-04-15 04:39:59 -04:00
|
|
|
while self.client().get_height().await?.height < height {
|
2020-11-01 18:02:28 -05:00
|
|
|
if retry >= 30 {
|
|
|
|
// ~30 seconds
|
|
|
|
bail!("Wallet could not catch up with monerod after 30 retries.")
|
|
|
|
}
|
2021-01-15 00:58:16 -05:00
|
|
|
time::sleep(Duration::from_millis(WAIT_WALLET_SYNC_MILLIS)).await;
|
2020-11-01 18:02:28 -05:00
|
|
|
retry += 1;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-11-01 22:42:08 -05:00
|
|
|
|
|
|
|
/// Sends amount to address
|
|
|
|
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
|
2022-08-27 06:26:55 -04:00
|
|
|
self.client().transfer_single(0, amount, address).await
|
2020-11-01 22:42:08 -05:00
|
|
|
}
|
2020-11-01 18:02:28 -05:00
|
|
|
|
2020-11-02 00:00:35 -05:00
|
|
|
pub async fn address(&self) -> Result<GetAddress> {
|
2021-04-15 04:39:59 -04:00
|
|
|
Ok(self.client().get_address(0).await?)
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn balance(&self) -> Result<u64> {
|
2020-11-02 19:49:53 -05:00
|
|
|
self.client().refresh().await?;
|
2021-04-15 04:39:59 -04:00
|
|
|
let balance = self.client().get_balance(0).await?.balance;
|
|
|
|
|
|
|
|
Ok(balance)
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
2020-11-02 05:12:38 -05:00
|
|
|
|
|
|
|
pub async fn refresh(&self) -> Result<Refreshed> {
|
2021-04-15 04:39:59 -04:00
|
|
|
Ok(self.client().refresh().await?)
|
2020-11-02 05:12:38 -05:00
|
|
|
}
|
2020-11-02 00:00:35 -05:00
|
|
|
}
|
2020-11-01 18:02:28 -05:00
|
|
|
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
|
|
|
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
|
|
|
|
loop {
|
2021-01-15 00:58:16 -05:00
|
|
|
time::sleep(Duration::from_secs(BLOCK_TIME_SECS)).await;
|
2021-04-15 04:39:59 -04:00
|
|
|
monerod.generateblocks(1, reward_address.clone()).await?;
|
2020-11-01 18:02:28 -05:00
|
|
|
}
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|