Skip to content

Commit

Permalink
feat(cli, documents): command line interface improvement
Browse files Browse the repository at this point in the history
Every command accepts arguments, config file settings, env vars. `interledger` and
`interledger-settlement-engines` are almost consistent.

BREAKING CHANGE: Some arguments are now obsolete. On interledger package: server_secret ->
secret_seed, btp_server -> btp_server_url, http_server -> http_server_url, redis_connection ->
redis_url, http_address -> http_bind_address, settlement_address -> settlement_api_bind_address,
btp_address -> btp_bind_address, btp_uri -> btp_server_url, http_url -> http_server_url. On
interledger-settlement-engines package: http_address -> settlement_api_bind_address,
ethereum_endpoint -> ethereum_url, redis_uri -> redis_url.

Signed-off-by: Taiga Nakayama <dora@dora-gt.jp>

interledger#171, interledger#113, interledger#206, interledger#194, interledger#215
  • Loading branch information
dora-gt committed Sep 8, 2019
1 parent a7088a4 commit a48d729
Show file tree
Hide file tree
Showing 21 changed files with 1,191 additions and 619 deletions.
2 changes: 2 additions & 0 deletions crates/interledger-settlement-engines/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ num-traits = "0.2.8"
lazy_static = "1.3.0"
secrecy = { version = "0.4.0", features = ["serde", "bytes"] }
zeroize = { version = "0.10.1", features = ["bytes"] }
config = { version = "0.9.3", features = [ "json", "yaml" ] }
libc = "0.2.62"

[dev-dependencies]
lazy_static = "1.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::types::{Addresses, EthereumAccount, EthereumLedgerTxSigner, EthereumStore};
use super::utils::{filter_transfer_logs, make_tx, sent_to_us, ERC20Transfer};
use super::EthAddress;
use clarity::Signature;
use log::{debug, error, trace};
use sha3::{Digest, Keccak256 as Sha3};
Expand All @@ -9,16 +10,18 @@ use std::iter::FromIterator;
use hyper::StatusCode;
use log::info;
use num_bigint::BigUint;
use redis::ConnectionInfo;
use redis::IntoConnectionInfo;
use reqwest::r#async::{Client, Response as HttpResponse};
use serde::{Deserialize, Serialize};
use serde::{de::Error as DeserializeError, Deserialize, Deserializer, Serialize};
use serde_json::json;
use std::net::SocketAddr;
use std::{
marker::PhantomData,
str::FromStr,
time::{Duration, Instant},
};
use std::{net::SocketAddr, str, u64};
use std::{str, u64};
use tokio::net::TcpListener;
use tokio::timer::Interval;
use tokio_retry::{strategy::ExponentialBackoff, Retry};
Expand All @@ -35,6 +38,7 @@ use web3::{
use crate::stores::{redis_ethereum_ledger::*, LeftoversStore};
use crate::{ApiResponse, CreateAccount, SettlementEngine, SettlementEngineApi};
use interledger_settlement::{Convert, ConvertDetails, Quantity};
use secrecy::Secret;

const MAX_RETRIES: usize = 10;
const ETH_CREATE_ACCOUNT_PREFIX: &[u8] = b"ilp-ethl-create-account-message";
Expand Down Expand Up @@ -983,50 +987,70 @@ fn prefixed_mesage(challenge: Vec<u8>) -> Vec<u8> {

#[doc(hidden)]
#[allow(clippy::all)]
pub fn run_ethereum_engine<R, Si>(
redis_uri: R,
ethereum_endpoint: String,
settlement_port: u16,
private_key: Si,
chain_id: u8,
confirmations: u8,
asset_scale: u8,
poll_frequency: u64,
connector_url: String,
token_address: Option<Address>,
watch_incoming: bool,
) -> impl Future<Item = (), Error = ()>
where
R: IntoConnectionInfo,
Si: EthereumLedgerTxSigner + Clone + Send + Sync + 'static,
{
let redis_uri = redis_uri.into_connection_info().unwrap();
pub fn run_ethereum_engine(opt: EthereumLedgerOpt) -> impl Future<Item = (), Error = ()> {
// TODO make key compatible with
// https://github.com/tendermint/signatory to have HSM sigs

EthereumLedgerRedisStoreBuilder::new(redis_uri.clone())
EthereumLedgerRedisStoreBuilder::new(opt.redis_connection.clone())
.connect()
.and_then(move |ethereum_store| {
let engine =
EthereumLedgerSettlementEngineBuilder::new(ethereum_store.clone(), private_key)
.ethereum_endpoint(&ethereum_endpoint)
.chain_id(chain_id)
.connector_url(&connector_url)
.confirmations(confirmations)
.asset_scale(asset_scale)
.poll_frequency(poll_frequency)
.watch_incoming(watch_incoming)
.token_address(token_address)
EthereumLedgerSettlementEngineBuilder::new(ethereum_store.clone(), opt.private_key)
.ethereum_endpoint(&opt.ethereum_url)
.chain_id(opt.chain_id)
.connector_url(&opt.connector_url)
.confirmations(opt.confirmations)
.asset_scale(opt.asset_scale)
.poll_frequency(opt.poll_frequency)
.watch_incoming(opt.watch_incoming)
.token_address(opt.token_address)
.connect();

let addr = SocketAddr::from(([127, 0, 0, 1], settlement_port));
let listener =
TcpListener::bind(&addr).expect("Unable to bind to Settlement Engine address");
let listener = TcpListener::bind(&opt.settlement_api_bind_address)
.expect("Unable to bind to Settlement Engine address");
let api = SettlementEngineApi::new(engine, ethereum_store);
tokio::spawn(api.serve(listener.incoming()));
info!("Ethereum Settlement Engine listening on: {}", addr);
info!(
"Ethereum Settlement Engine listening on: {}",
&opt.settlement_api_bind_address
);
Ok(())
})
}

#[derive(Deserialize, Clone)]
pub struct EthereumLedgerOpt {
pub private_key: Secret<String>,
pub settlement_api_bind_address: SocketAddr,
pub ethereum_url: String,
pub token_address: Option<EthAddress>,
pub connector_url: String,
#[serde(deserialize_with = "deserialize_redis_connection", alias = "redis_url")]
pub redis_connection: ConnectionInfo,
// Although the length of `chain_id` seems to be not limited on its specs,
// u8 seems sufficient at this point.
pub chain_id: u8,
pub confirmations: u8,
pub asset_scale: u8,
pub poll_frequency: u64,
pub watch_incoming: bool,
}

fn deserialize_redis_connection<'de, D>(deserializer: D) -> Result<ConnectionInfo, D::Error>
where
D: Deserializer<'de>,
{
Url::parse(&String::deserialize(deserializer)?)
.map_err(|err| DeserializeError::custom(format!("Invalid URL: {:?}", err)))?
.into_connection_info()
.map_err(|err| {
DeserializeError::custom(format!(
"Error converting into Redis connection info: {:?}",
err
))
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub mod test_helpers;

// Only expose the engine and the ledger signer
pub use eth_engine::{
run_ethereum_engine, EthereumLedgerSettlementEngine, EthereumLedgerSettlementEngineBuilder,
run_ethereum_engine, EthereumLedgerOpt, EthereumLedgerSettlementEngine,
EthereumLedgerSettlementEngineBuilder,
};
pub use types::{
Addresses as EthereumAddresses, EthereumAccount, EthereumLedgerTxSigner, EthereumStore,
Expand Down
Loading

0 comments on commit a48d729

Please sign in to comment.