depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit b9033f99d70bd4cf73cadc762d14c359e8977da8
parent dac7fd3be2b9eba0d77de176a049e05971a601fb
Author: Antoine A <>
Date:   Thu, 10 Mar 2022 16:25:11 +0100

Small improvements

Diffstat:
Mbtc-wire/src/loops/analysis.rs | 1-
Mbtc-wire/src/loops/watcher.rs | 3+--
Mbtc-wire/src/loops/worker.rs | 1+
Mbtc-wire/src/main.rs | 1+
Mbtc-wire/src/rpc.rs | 2++
Meth-wire/src/bin/eth-wire-utils.rs | 6+++---
Meth-wire/src/lib.rs | 4++++
Meth-wire/src/main.rs | 3++-
Meth-wire/src/rpc.rs | 96+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Mwire-gateway/src/main.rs | 1+
10 files changed, 75 insertions(+), 43 deletions(-)

diff --git a/btc-wire/src/loops/analysis.rs b/btc-wire/src/loops/analysis.rs @@ -23,7 +23,6 @@ use common::{ }; use crate::WireState; - use super::LoopResult; /// Analyse blockchain behavior and adapt confirmations in real time diff --git a/btc-wire/src/loops/watcher.rs b/btc-wire/src/loops/watcher.rs @@ -1,5 +1,3 @@ -use std::time::Duration; - /* This file is part of TALER Copyright (C) 2022 Taler Systems SA @@ -17,6 +15,7 @@ use std::time::Duration; */ use common::{log::log::error, reconnect::AutoReconnectDb}; use btc_wire::rpc::AutoRpcCommon; +use std::time::Duration; use super::LoopResult; diff --git a/btc-wire/src/loops/worker.rs b/btc-wire/src/loops/worker.rs @@ -46,6 +46,7 @@ use crate::{ use super::{LoopError, LoopResult}; +/// Synchronize local db with blockchain and perform transactions pub fn worker(mut rpc: AutoRpcWallet, mut db: AutoReconnectDb, state: &WireState) { let mut lifetime = state.lifetime; let mut status = true; diff --git a/btc-wire/src/main.rs b/btc-wire/src/main.rs @@ -175,4 +175,5 @@ fn run(config: Option<PathBuf>) { analysis(rpc_analysis, db_analysis, state) }); worker(rpc_worker, db_worker, state); + info!("btc-wire stopped"); } diff --git a/btc-wire/src/rpc.rs b/btc-wire/src/rpc.rs @@ -39,6 +39,7 @@ use crate::btc_config::{BitcoinConfig, BtcAuth}; pub type AutoRpcWallet = AutoReconnect<(BitcoinConfig, &'static str), Rpc>; +/// Create a reconnecting rpc connection with an unlocked wallet pub fn auto_rpc_wallet(config: BitcoinConfig, wallet: &'static str) -> AutoRpcWallet { AutoReconnect::new( (config, wallet), @@ -58,6 +59,7 @@ pub fn auto_rpc_wallet(config: BitcoinConfig, wallet: &'static str) -> AutoRpcWa pub type AutoRpcCommon = AutoReconnect<BitcoinConfig, Rpc>; +/// Create a reconnecting rpc connection pub fn auto_rpc_common(config: BitcoinConfig) -> AutoRpcCommon { AutoReconnect::new( config, diff --git a/eth-wire/src/bin/eth-wire-utils.rs b/eth-wire/src/bin/eth-wire-utils.rs @@ -212,7 +212,7 @@ fn main() { ); exit(1); } else { - std::thread::sleep(Duration::from_secs(1)) + std::thread::sleep(Duration::from_secs(5)) } } } @@ -227,7 +227,7 @@ fn main() { if start.elapsed() > Duration::from_secs(60) { panic!("Connect timeout"); } - std::thread::sleep(Duration::from_secs(1)) + std::thread::sleep(Duration::from_secs(5)) } } Cmd::Disconnect { datadir } => { @@ -241,7 +241,7 @@ fn main() { if start.elapsed() > Duration::from_secs(60) { panic!("Disconnect timeout"); } - std::thread::sleep(Duration::from_secs(1)) + std::thread::sleep(Duration::from_secs(5)) } } Cmd::Abandon { from } => { diff --git a/eth-wire/src/lib.rs b/eth-wire/src/lib.rs @@ -41,7 +41,9 @@ pub mod rpc; mod rpc_utils; pub mod taler_util; +/// An extended geth JSON-RPC api client who can send and retrieve metadata with their transaction pub trait RpcExtended: RpcClient { + /// Perform a Taler deposit fn deposit( &mut self, from: Address, @@ -60,6 +62,7 @@ pub trait RpcExtended: RpcClient { }) } + /// Perform a Taler withdraw fn withdraw( &mut self, from: Address, @@ -79,6 +82,7 @@ pub trait RpcExtended: RpcClient { }) } + /// Perform a Taler bounce fn bounce(&mut self, hash: H256, bounce_fee: U256) -> rpc::Result<Option<H256>> { let tx = self .get_transaction(&hash)? diff --git a/eth-wire/src/main.rs b/eth-wire/src/main.rs @@ -17,7 +17,7 @@ use std::path::PathBuf; use clap::StructOpt; -use common::{named_spawn, password, postgres::NoTls, reconnect::auto_reconnect_db}; +use common::{named_spawn, password, postgres::NoTls, reconnect::auto_reconnect_db, log::log::info}; use eth_wire::{ load_taler_config, rpc::{auto_rpc_common, auto_rpc_wallet, Rpc, RpcClient}, @@ -163,4 +163,5 @@ fn run(config: Option<PathBuf>) { }); worker(rpc_worker, db_worker, state); + info!("eth-wire stopped"); } diff --git a/eth-wire/src/rpc.rs b/eth-wire/src/rpc.rs @@ -34,6 +34,7 @@ use self::hex::Hex; pub type AutoRpcWallet = AutoReconnect<(PathBuf, Address), Rpc>; +/// Create a reconnecting rpc connection with an unlocked wallet pub fn auto_rpc_wallet(ipc_path: PathBuf, address: Address) -> AutoRpcWallet { AutoReconnect::new( (ipc_path, address), @@ -52,6 +53,7 @@ pub fn auto_rpc_wallet(ipc_path: PathBuf, address: Address) -> AutoRpcWallet { pub type AutoRpcCommon = AutoReconnect<PathBuf, Rpc>; +/// Create a reconnecting rpc connection pub fn auto_rpc_common(ipc_path: PathBuf) -> AutoRpcCommon { AutoReconnect::new( ipc_path, @@ -99,9 +101,7 @@ pub type Result<T> = std::result::Result<T, Error>; const EMPTY: [(); 0] = []; -pub trait RpcTrait {} - -/// Bitcoin RPC connection +/// Ethereum RPC connection pub struct Rpc { id: u64, conn: BufWriter<UnixStream>, @@ -110,6 +110,7 @@ pub struct Rpc { } impl Rpc { + /// Start a RPC connection, path can be datadir or ipc path pub fn new(path: impl AsRef<Path>) -> io::Result<Self> { let path = path.as_ref(); @@ -128,7 +129,6 @@ impl Rpc { } fn send(&mut self, method: &str, params: &impl serde::Serialize) -> Result<()> { - // TODO rethink timeout let request = RpcRequest { method, id: self.id, @@ -146,6 +146,7 @@ impl Rpc { T: serde::de::DeserializeOwned + Debug, { loop { + // Double buffer size if full if self.cursor == self.read_buf.len() { self.read_buf.resize(self.cursor * 2, 0); } @@ -292,6 +293,7 @@ impl<N: Debug + DeserializeOwned> RpcClient for RpcStream<'_, N> { { self.rpc.send(method, params)?; loop { + // Buffer notifications until response let response: NotificationOrResponse<T, N> = self.rpc.receive()?; match response { NotificationOrResponse::Notification(n) => { @@ -312,22 +314,26 @@ pub trait RpcClient { where T: serde::de::DeserializeOwned + Debug; + /* ----- Account management ----- */ + + /// List registered acount fn list_accounts(&mut self) -> Result<Vec<Address>> { self.call("personal_listAccounts", &EMPTY) } + /// Create a new encrypted account fn new_account(&mut self, passwd: &str) -> Result<Address> { self.call("personal_newAccount", &[passwd]) } - fn import_account(&mut self, hex: &str, passwd: &str) -> Result<bool> { - self.call("personal_importRawKey", &(hex, passwd)) - } - + /// Unlock an existinf acount fn unlock_account(&mut self, account: &Address, passwd: &str) -> Result<bool> { self.call("personal_unlockAccount", &(account, passwd, 0)) } + /* ----- Getter ----- */ + + /// Get a transaction by hash fn get_transaction(&mut self, hash: &H256) -> Result<Option<Transaction>> { match self.call("eth_getTransactionByHash", &[hash]) { Err(Error::Null) => Ok(None), @@ -335,21 +341,15 @@ pub trait RpcClient { } } + /// Get a transaction receipt by hash fn get_transaction_receipt(&mut self, hash: &H256) -> Result<Option<TransactionReceipt>> { match self.call("eth_getTransactionReceipt", &[hash]) { Err(Error::Null) => Ok(None), r => r, } } - - fn fill_transaction(&mut self, req: &TransactionRequest) -> Result<Filled> { - self.call("eth_fillTransaction", &[req]) - } - - fn send_transaction(&mut self, req: &TransactionRequest) -> Result<H256> { - self.call("eth_sendTransaction", &[req]) - } - + + /// Get block by hash fn block(&mut self, hash: &H256) -> Result<Option<Block>> { match self.call("eth_getBlockByHash", &(hash, &true)) { Err(Error::Null) => Ok(None), @@ -357,10 +357,46 @@ pub trait RpcClient { } } + /// Get pending transactions fn pending_transactions(&mut self) -> Result<Vec<Transaction>> { self.call("eth_pendingTransactions", &EMPTY) } + + /// Get latest block + fn latest_block(&mut self) -> Result<Block> { + self.call("eth_getBlockByNumber", &("latest", &true)) + } + + /// Get earliest block (genesis if not pruned) + fn earliest_block(&mut self) -> Result<Block> { + self.call("eth_getBlockByNumber", &("earliest", &true)) + } + /// Get account balance + fn get_balance(&mut self, addr: &Address) -> Result<U256> { + self.call("eth_getBalance", &(addr, "latest")) + } + + /// Get node info + fn node_info(&mut self) -> Result<NodeInfo> { + self.call("admin_nodeInfo", &EMPTY) + } + + /* ----- Transactions ----- */ + + /// Fill missing options from transaction request with default values + fn fill_transaction(&mut self, req: &TransactionRequest) -> Result<Filled> { + self.call("eth_fillTransaction", &[req]) + } + + /// Send ethereum transaction + fn send_transaction(&mut self, req: &TransactionRequest) -> Result<H256> { + self.call("eth_sendTransaction", &[req]) + } + + /* ----- Miner ----- */ + + /// Start mining fn miner_start(&mut self) -> Result<()> { match self.call("miner_start", &[8]) { Err(Error::Null) => Ok(()), @@ -368,6 +404,7 @@ pub trait RpcClient { } } + /// Stop mining fn miner_stop(&mut self) -> Result<()> { match self.call("miner_stop", &EMPTY) { Err(Error::Null) => Ok(()), @@ -375,35 +412,22 @@ pub trait RpcClient { } } - fn latest_block(&mut self) -> Result<Block> { - self.call("eth_getBlockByNumber", &("latest", &true)) - } - - fn earliest_block(&mut self) -> Result<Block> { - self.call("eth_getBlockByNumber", &("earliest", &true)) - } - - fn get_balance(&mut self, addr: &Address) -> Result<U256> { - self.call("eth_getBalance", &(addr, "latest")) - } - /* ----- Peer management ----- */ - fn node_info(&mut self) -> Result<NodeInfo> { - self.call("admin_nodeInfo", &EMPTY) - } - + /// Add peer to the peer list fn add_peer(&mut self, url: &Url) -> Result<bool> { self.call("admin_addPeer", &[url]) } + /// Remove a peer to the peer list fn remove_peer(&mut self, url: &Url) -> Result<bool> { self.call("admin_removePeer", &[url]) } - fn count_peer(&mut self) -> Result<usize> { - let peers: Vec<Nothing> = self.call("admin_peers", &EMPTY)?; - Ok(peers.len()) + /// Get peer count + fn count_peer(&mut self) -> Result<u32> { + let peers: U64 = self.call("net_peerCount", &EMPTY)?; + Ok(peers.as_u32()) } } diff --git a/wire-gateway/src/main.rs b/wire-gateway/src/main.rs @@ -217,6 +217,7 @@ async fn main() { error!("server: {}", e); } }; + info!("wire-gateway stopped"); } /// Check if an url if a valid payto url for the configured currency