2021-04-19 07:26:11 +00:00
|
|
|
use std::collections::HashMap;
|
2021-05-11 00:30:13 +00:00
|
|
|
use testcontainers::core::{Container, Docker, WaitForMessage};
|
|
|
|
use testcontainers::Image;
|
2020-09-28 06:18:50 +00:00
|
|
|
|
2020-11-01 23:02:28 +00:00
|
|
|
pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
|
|
|
|
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
|
2021-04-19 07:26:11 +00:00
|
|
|
|
|
|
|
/// The port we use for all RPC communication.
|
|
|
|
///
|
|
|
|
/// This is the default when running monerod.
|
|
|
|
/// For `monero-wallet-rpc` we always need to specify a port. To make things
|
|
|
|
/// simpler, we just specify the same one. They are in different containers so
|
|
|
|
/// this doesn't matter.
|
|
|
|
pub const RPC_PORT: u16 = 18081;
|
2020-09-28 06:18:50 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-04-19 07:26:11 +00:00
|
|
|
pub struct Monerod {
|
|
|
|
args: MonerodArgs,
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl Image for Monerod {
|
|
|
|
type Args = MonerodArgs;
|
2020-09-28 06:18:50 +00:00
|
|
|
type EnvVars = HashMap<String, String>;
|
|
|
|
type Volumes = HashMap<String, String>;
|
|
|
|
type EntryPoint = str;
|
|
|
|
|
|
|
|
fn descriptor(&self) -> String {
|
2021-04-19 07:19:03 +00:00
|
|
|
"xmrto/monero:v0.17.2.0".to_owned()
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
|
|
|
container
|
|
|
|
.logs()
|
|
|
|
.stdout
|
2021-04-28 01:44:55 +00:00
|
|
|
.wait_for_message("RPC server started ok")
|
2020-09-28 06:18:50 +00:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn args(&self) -> <Self as Image>::Args {
|
|
|
|
self.args.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn volumes(&self) -> Self::Volumes {
|
|
|
|
HashMap::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_vars(&self) -> Self::EnvVars {
|
|
|
|
HashMap::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
2021-04-19 07:26:11 +00:00
|
|
|
Self { args }
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn entrypoint(&self) -> Option<String> {
|
2021-04-19 07:26:11 +00:00
|
|
|
Some("".to_owned()) // an empty entrypoint disables the entrypoint
|
|
|
|
// script and gives us full control
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl Default for Monerod {
|
2020-09-28 06:18:50 +00:00
|
|
|
fn default() -> Self {
|
2021-04-19 07:26:11 +00:00
|
|
|
Self {
|
|
|
|
args: MonerodArgs::default(),
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MoneroWalletRpc {
|
|
|
|
args: MoneroWalletRpcArgs,
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl Image for MoneroWalletRpc {
|
|
|
|
type Args = MoneroWalletRpcArgs;
|
|
|
|
type EnvVars = HashMap<String, String>;
|
|
|
|
type Volumes = HashMap<String, String>;
|
|
|
|
type EntryPoint = str;
|
|
|
|
|
|
|
|
fn descriptor(&self) -> String {
|
|
|
|
"xmrto/monero:v0.17.2.0".to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
|
|
|
|
container
|
|
|
|
.logs()
|
|
|
|
.stdout
|
2021-04-28 01:44:55 +00:00
|
|
|
.wait_for_message("Run server thread name: RPC")
|
2021-04-19 07:26:11 +00:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn args(&self) -> <Self as Image>::Args {
|
|
|
|
self.args.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn volumes(&self) -> Self::Volumes {
|
|
|
|
HashMap::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_vars(&self) -> Self::EnvVars {
|
|
|
|
HashMap::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_args(self, args: <Self as Image>::Args) -> Self {
|
|
|
|
Self { args }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn entrypoint(&self) -> Option<String> {
|
|
|
|
Some("".to_owned()) // an empty entrypoint disables the entrypoint
|
|
|
|
// script and gives us full control
|
|
|
|
}
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl Default for MoneroWalletRpc {
|
2020-11-01 23:02:28 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-04-19 07:26:11 +00:00
|
|
|
args: MoneroWalletRpcArgs::default(),
|
2020-11-01 23:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl MoneroWalletRpc {
|
|
|
|
pub fn new(name: &str, daemon_address: String) -> Self {
|
|
|
|
Self {
|
|
|
|
args: MoneroWalletRpcArgs::new(name, daemon_address),
|
2020-11-01 23:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:18:50 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MonerodArgs {
|
|
|
|
pub regtest: bool,
|
|
|
|
pub offline: bool,
|
|
|
|
pub rpc_payment_allow_free_loopback: bool,
|
|
|
|
pub confirm_external_bind: bool,
|
|
|
|
pub no_igd: bool,
|
|
|
|
pub hide_my_port: bool,
|
|
|
|
pub rpc_bind_ip: String,
|
|
|
|
pub fixed_difficulty: u32,
|
|
|
|
pub data_dir: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MonerodArgs {
|
|
|
|
fn default() -> Self {
|
2021-04-19 07:26:11 +00:00
|
|
|
Self {
|
2020-09-28 06:18:50 +00:00
|
|
|
regtest: true,
|
|
|
|
offline: true,
|
|
|
|
rpc_payment_allow_free_loopback: true,
|
|
|
|
confirm_external_bind: true,
|
|
|
|
no_igd: true,
|
|
|
|
hide_my_port: true,
|
|
|
|
rpc_bind_ip: "0.0.0.0".to_string(),
|
|
|
|
fixed_difficulty: 1,
|
|
|
|
data_dir: "/monero".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl IntoIterator for MonerodArgs {
|
|
|
|
type Item = String;
|
|
|
|
type IntoIter = ::std::vec::IntoIter<String>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
|
|
|
|
let mut args = vec![
|
|
|
|
"monerod".to_string(),
|
|
|
|
"--log-level=4".to_string(),
|
|
|
|
"--non-interactive".to_string(),
|
|
|
|
];
|
2020-09-28 06:18:50 +00:00
|
|
|
|
|
|
|
if self.regtest {
|
|
|
|
args.push("--regtest".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.offline {
|
|
|
|
args.push("--offline".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.rpc_payment_allow_free_loopback {
|
|
|
|
args.push("--rpc-payment-allow-free-loopback".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.confirm_external_bind {
|
|
|
|
args.push("--confirm-external-bind".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.no_igd {
|
|
|
|
args.push("--no-igd".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.hide_my_port {
|
|
|
|
args.push("--hide-my-port".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.rpc_bind_ip.is_empty() {
|
2021-04-19 07:26:11 +00:00
|
|
|
args.push(format!("--rpc-bind-ip={}", self.rpc_bind_ip));
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.data_dir.is_empty() {
|
2021-04-19 07:26:11 +00:00
|
|
|
args.push(format!("--data-dir={}", self.data_dir));
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.fixed_difficulty != 0 {
|
2021-04-19 07:26:11 +00:00
|
|
|
args.push(format!("--fixed-difficulty={}", self.fixed_difficulty));
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
args.into_iter()
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 23:02:28 +00:00
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct MoneroWalletRpcArgs {
|
|
|
|
pub disable_rpc_login: bool,
|
|
|
|
pub confirm_external_bind: bool,
|
|
|
|
pub wallet_dir: String,
|
|
|
|
pub rpc_bind_ip: String,
|
|
|
|
pub daemon_address: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MoneroWalletRpcArgs {
|
|
|
|
fn default() -> Self {
|
|
|
|
unimplemented!("A default instance for `MoneroWalletRpc` doesn't make sense because we always need to connect to a node.")
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
impl MoneroWalletRpcArgs {
|
|
|
|
pub fn new(wallet_name: &str, daemon_address: String) -> Self {
|
|
|
|
Self {
|
2020-09-28 06:18:50 +00:00
|
|
|
disable_rpc_login: true,
|
|
|
|
confirm_external_bind: true,
|
2020-11-02 03:42:08 +00:00
|
|
|
wallet_dir: wallet_name.into(),
|
2020-09-28 06:18:50 +00:00
|
|
|
rpc_bind_ip: "0.0.0.0".into(),
|
|
|
|
daemon_address,
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 07:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for MoneroWalletRpcArgs {
|
|
|
|
type Item = String;
|
|
|
|
type IntoIter = ::std::vec::IntoIter<String>;
|
2020-09-28 06:18:50 +00:00
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
|
|
|
|
let mut args = vec![
|
|
|
|
"monero-wallet-rpc".to_string(),
|
|
|
|
format!("--wallet-dir={}", self.wallet_dir),
|
|
|
|
format!("--daemon-address={}", self.daemon_address),
|
|
|
|
format!("--rpc-bind-port={}", RPC_PORT),
|
|
|
|
"--log-level=4".to_string(),
|
|
|
|
];
|
2020-09-28 06:18:50 +00:00
|
|
|
|
|
|
|
if self.disable_rpc_login {
|
|
|
|
args.push("--disable-rpc-login".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.confirm_external_bind {
|
|
|
|
args.push("--confirm-external-bind".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.rpc_bind_ip.is_empty() {
|
2021-04-19 07:26:11 +00:00
|
|
|
args.push(format!("--rpc-bind-ip={}", self.rpc_bind_ip));
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:26:11 +00:00
|
|
|
args.into_iter()
|
2020-09-28 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|