refactor: use eyre for reporting

buddies-main-deployment
James Prestwich 4 years ago
parent 4555178942
commit 740c6321a6
No known key found for this signature in database
GPG Key ID: 75A7F5C06D747046
  1. 158
      rust/Cargo.lock
  2. 2
      rust/optics-base/Cargo.toml
  3. 12
      rust/optics-base/config/default.toml
  4. 42
      rust/optics-base/src/main.rs
  5. 12
      rust/optics-base/src/settings/ethereum.rs
  6. 12
      rust/optics-base/src/settings/mod.rs
  7. 3
      rust/optics-core/src/traits/mod.rs

158
rust/Cargo.lock generated

@ -10,6 +10,21 @@ dependencies = [
"regex",
]
[[package]]
name = "addr2line"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "aho-corasick"
version = "0.7.15"
@ -81,6 +96,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc"
dependencies = [
"addr2line",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.0"
@ -175,6 +204,33 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b29030875fd8376e4a28ef497790d5b4a7843d8d1396bf08ce46f5eec562c5c"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6eee477a4a8a72f4addd4de416eb56d54bc307b284d6601bafdee1f4ea462d1"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "config"
version = "0.10.1"
@ -513,6 +569,16 @@ dependencies = [
"thiserror",
]
[[package]]
name = "eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "ff"
version = "0.8.0"
@ -693,6 +759,12 @@ dependencies = [
"wasi 0.10.1+wasi-snapshot-preview1",
]
[[package]]
name = "gimli"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]]
name = "glob"
version = "0.3.0"
@ -862,6 +934,12 @@ dependencies = [
"serde 1.0.120",
]
[[package]]
name = "indenter"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4d5eb2e114fec2b7fe0fadc22888ad2658789bb7acac4dbee9cf8389f971ec8"
[[package]]
name = "indexmap"
version = "1.6.1"
@ -893,6 +971,15 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "jane-eyre"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ccd957c837e264dcbc6e75b58fcf85fd329a66e4836f820779c40563a2d0be0"
dependencies = [
"color-eyre",
]
[[package]]
name = "js-sys"
version = "0.3.46"
@ -1000,6 +1087,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.7"
@ -1079,6 +1176,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
version = "1.5.2"
@ -1136,6 +1239,7 @@ dependencies = [
"ethers-middleware",
"ethers-providers",
"ethers-signers",
"jane-eyre",
"optics-core",
"serde 1.0.120",
"serde_json",
@ -1159,6 +1263,12 @@ dependencies = [
"tokio",
]
[[package]]
name = "owo-colors"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13370dae44474229701bb69b90b4f4dca6404cb0357a2d50d635f1171dc3aa7b"
[[package]]
name = "parity-scale-codec"
version = "1.3.6"
@ -1564,6 +1674,12 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2"
[[package]]
name = "rustc-demangle"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
[[package]]
name = "rustc-hex"
version = "2.1.0"
@ -1757,6 +1873,15 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "sharded-slab"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79c719719ee05df97490f80a45acfc99e5a30ce98a1e4fb67aee422745ae14e3"
dependencies = [
"lazy_static",
]
[[package]]
name = "signature"
version = "1.2.2"
@ -2002,9 +2127,21 @@ checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [
"cfg-if 1.0.0",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.17"
@ -2014,6 +2151,16 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "tracing-error"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4d7c0b83d4a500748fa5879461652b361edf5c9d51ede2a2ac03875ca185e24"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-futures"
version = "0.2.4"
@ -2024,6 +2171,17 @@ dependencies = [
"tracing",
]
[[package]]
name = "tracing-subscriber"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1fa8f0c8f4c594e4fc9debc1990deab13238077271ba84dd853d54902ee3401"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]]
name = "try-lock"
version = "0.2.3"

@ -23,3 +23,5 @@ ethers-contract = { git = "https://github.com/gakonst/ethers-rs", features = ["a
thiserror = { version = "1.0.22", default-features = false }
async-trait = { version = "0.1.42", default-features = false }
url = { version = "2.2.0", default-features = false }
jane-eyre = "0.3.0"

@ -1,7 +1,7 @@
[home]
slip44 = 52752
address = "0x0000000000000000000000000000000000000000"
chain = "Ethereum"
rpc-style = "Ethereum"
[home.config]
signer = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc"
connection = { type = "Http", url = "http://localhost:8545" }
@ -10,7 +10,13 @@ chain = "Ethereum"
[replicas.Ethereum]
slip44 = 60
address = "0x0000000000000000000000000000000000000000"
chain = "Ethereum"
rpc-style = "Ethereum"
[replicas.Ethereum.config]
connection = { type = "Ws", url = "wss://"}
connection = { type = "Ws", url = "ws://localhost:8545"}
[replicas.EthClassic]
slip44 = 61
address = "0x0000000000000000000000000000000000000000"
rpc-style = "Ethereum"
[replicas.EthClassic.config]
connection = { type = "Ws", url = "ws://localhost:8545"}

@ -18,12 +18,14 @@ pub mod abis;
pub mod settings;
use optics_core::traits::{Home, Replica};
use jane_eyre::{eyre::WrapErr, Result};
use std::collections::HashMap;
/// The global app context.
///
/// We erase all type info to allow easier abstraction across chains without
/// managing insanely large type systems.
/// managing insanely large, annoying type systems.
///
/// Usually this will be bundled in a larger
#[derive(Debug)]
@ -32,23 +34,37 @@ struct ChainConnections {
replicas: HashMap<String, Box<dyn Replica>>,
}
async fn _main(settings: settings::Settings) {
println!("{:?}", &settings);
impl ChainConnections {
pub async fn try_from_settings(settings: &settings::Settings) -> Result<Self> {
let home = settings
.home
.try_into_home()
.await
.wrap_err("failed to instantiate Home")?;
let home = settings.home.try_into_home().await.expect("!home");
let mut replicas = HashMap::new();
// TODO: parallelize if this becomes expensive
for (key, value) in settings.replicas.iter() {
replicas.insert(
key.clone(),
value
.try_into_replica()
.await
.wrap_err_with(|| format!("Failed to instantiate replica named {}", key))?,
);
}
let mut replicas = HashMap::new();
// TODO: parallelize if this becomes expensive
for (key, value) in settings.replicas.iter() {
replicas.insert(
key.clone(),
value.try_into_replica().await.expect("!replica"),
);
let app = ChainConnections { home, replicas };
Ok(app)
}
}
async fn _main(settings: settings::Settings) {
println!("{:?}", &settings);
let app = ChainConnections { home, replicas };
let app = ChainConnections::try_from_settings(&settings).await;
println!("{:?}", &app);
println!("\n{:#?}", &app);
}
fn main() {

@ -1,6 +1,7 @@
use std::convert::TryFrom;
use ethers_core::types::Address;
use jane_eyre::Report;
use optics_core::traits::{Home, Replica};
@ -43,9 +44,7 @@ macro_rules! construct_box_contract {
macro_rules! construct_ws_box_contract {
($contract:ident, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{
let ws = ethers_providers::Ws::connect($url)
.await
.map_err(|_| "!ws connect")?;
let ws = ethers_providers::Ws::connect($url).await?;
let provider = ethers_providers::Provider::new(ws);
construct_box_contract!($contract, $slip44, $address, provider, $signer)
}};
@ -54,8 +53,7 @@ macro_rules! construct_ws_box_contract {
macro_rules! construct_http_box_contract {
($contract:ident, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{
let provider =
ethers_providers::Provider::<ethers_providers::Http>::try_from($url.as_ref())
.map_err(|_| "!url")?;
ethers_providers::Provider::<ethers_providers::Http>::try_from($url.as_ref())?;
construct_box_contract!($contract, $slip44, $address, provider, $signer)
}};
@ -78,7 +76,7 @@ impl EthereumConf {
&self,
slip44: u32,
address: Address,
) -> Result<Box<dyn Home>, String> {
) -> Result<Box<dyn Home>, Report> {
let b: Box<dyn Home> = match &self.connection {
EthereumConnection::Http { url } => {
construct_http_box_contract!(HomeContract, slip44, address, url, self.signer())
@ -95,7 +93,7 @@ impl EthereumConf {
&self,
slip44: u32,
address: Address,
) -> Result<Box<dyn Replica>, String> {
) -> Result<Box<dyn Replica>, Report> {
let b: Box<dyn Replica> = match &self.connection {
EthereumConnection::Http { url } => {
construct_http_box_contract!(ReplicaContract, slip44, address, url, self.signer())

@ -1,4 +1,5 @@
use config::{Config, ConfigError, Environment, File};
use jane_eyre::Report;
use std::{collections::HashMap, env};
use optics_core::traits::{Home, Replica};
@ -13,7 +14,7 @@ use ethereum::EthereumConf;
/// Specify the chain name (enum variant) in toml under the `chain` key
/// Specify the connection details as a toml object under the `connection` key.
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "chain", content = "config")]
#[serde(tag = "rpc-style", content = "config")]
pub enum ChainConf {
/// Ethereum configuration
Ethereum(EthereumConf),
@ -31,20 +32,19 @@ pub struct ChainSetup {
impl ChainSetup {
/// Try to convert the chain setting into a Home contract
pub async fn try_into_home(&self) -> Result<Box<dyn Home>, String> {
pub async fn try_into_home(&self) -> Result<Box<dyn Home>, Report> {
match &self.chain {
ChainConf::Ethereum(conf) => {
conf.try_into_home(self.slip44, self.address.parse().map_err(|_| "!address")?)
.await
conf.try_into_home(self.slip44, self.address.parse()?).await
}
}
}
/// Try to convert the chain setting into a replica contract
pub async fn try_into_replica(&self) -> Result<Box<dyn Replica>, String> {
pub async fn try_into_replica(&self) -> Result<Box<dyn Replica>, Report> {
match &self.chain {
ChainConf::Ethereum(conf) => {
conf.try_into_replica(self.slip44, self.address.parse().map_err(|_| "!address")?)
conf.try_into_replica(self.slip44, self.address.parse()?)
.await
}
}

@ -27,8 +27,9 @@ pub enum State {
pub struct TxOutcome {
/// The txid
pub txid: H256,
/// True if executed, false otherwise
/// True if executed, false otherwise (reverted, etc.)
pub executed: bool,
// TODO: more? What can be abstracted across all chains?
}
impl From<TransactionReceipt> for TxOutcome {

Loading…
Cancel
Save