Skip to content

Commit

Permalink
Allow use of custom Ethereum endpoint via ANOMA_ETHEREUM_URL envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
james-chf committed Aug 3, 2022
1 parent 94735d3 commit 32289f4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
3 changes: 3 additions & 0 deletions apps/src/lib/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub const FILENAME: &str = "config.toml";
pub const TENDERMINT_DIR: &str = "tendermint";
/// Chain-specific Anoma DB. Nested in chain dirs.
pub const DB_DIR: &str = "db";
/// Relevant to validator nodes only. If provided, the ledger will monitor for
/// new Ethereum events via this endpoint rather than via a `geth` subprocess.
pub const ENV_VAR_ETHEREUM_URL: &str = "ANOMA_ETHEREUM_URL";
/// Websocket address for Ethereum fullnode RPC
pub const ETHEREUM_URL: &str = "http://127.0.0.1:8545";

Expand Down
93 changes: 58 additions & 35 deletions apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use tower_abci_old::{response, split, Server};

use self::shims::abcipp_shim::AbciService;
use crate::config::utils::num_of_threads;
use crate::config::TendermintMode;
use crate::config::{TendermintMode, ENV_VAR_ETHEREUM_URL};
use crate::node::ledger::broadcaster::Broadcaster;
use crate::node::ledger::shell::{Error, MempoolTxType, Shell};
use crate::node::ledger::shims::abcipp_shim::AbcippShim;
Expand Down Expand Up @@ -310,48 +310,71 @@ async fn run_aux(config: config::Ledger, wasm_dir: PathBuf) {
let (broadcaster_sender, broadcaster_receiver) =
tokio::sync::mpsc::unbounded_channel();

let ethereum_url = config.ethereum_url();
let (ethereum_node, oracle, broadcaster) = if matches!(
config.tendermint.tendermint_mode,
TendermintMode::Validator
) {
let local = tokio::task::LocalSet::new();
// boot up the ethereum node process and wait for it to finish syncing
let (eth_sender, eth_receiver) = unbounded_channel();
let url = ethereum_url.clone();
let (ethereum_node, abort_sender) = local
.run_until(async move {
EthereumNode::new(&url)
.await
.expect("Unable to start the Ethereum fullnode")
})
.await;

// Start Ethereum fullnode
// Channel for signalling shut down to Tendermint process

let abort_send_for_eth = abort_send.clone();
let (eth_abort_send, eth_abort_recv) =
tokio::sync::oneshot::channel::<tokio::sync::oneshot::Sender<()>>();
let abort_send_for_eth = abort_send.clone();
// run geth in the background
let ethereum_node = tokio::spawn(async move {
// On panic or exit, the `Drop` of `AbortSender` will send abort
// message
let aborter = Aborter {
sender: abort_send_for_eth,
who: "Ethereum",
};

let res = ethereum_node::run(ethereum_node, eth_abort_recv)
.map_err(Error::Ethereum)
.await;
tracing::info!("Ethereum fullnode is no longer running.");

drop(aborter);
res
});
// we want any Ethereum endpoint set in the environment to override the
// one set in the config
let ethereum_url = std::env::var(ENV_VAR_ETHEREUM_URL).ok();
let (ethereum_url, ethereum_node, abort_sender) = match ethereum_url {
Some(ethereum_url) => {
tracing::info!(
%ethereum_url,
"Ethereum URL provided in environment, will not start geth"
);
// we return a dummy join handle and sender, so we can share
// logic further down this function with how we'd normally
// handle a `geth` subprocess
let handle = tokio::spawn(async { Ok(()) });
let (abort_sender, _) = tokio::sync::oneshot::channel();
(ethereum_url, handle, abort_sender)
}
None => {
let ethereum_url = config.ethereum_url();
tracing::info!(
%ethereum_url,
"No Ethereum URL provided in configuration, starting geth",
);
// boot up the ethereum node process
let (ethereum_node, abort_sender) = local
.run_until(async {
EthereumNode::new(&ethereum_url)
.await
.expect("Unable to start the Ethereum fullnode")
})
.await;

// run geth in the background
let handle = tokio::spawn(async move {
// On panic or exit, the `Drop` of `AbortSender` will
// send abort message
let aborter = Aborter {
sender: abort_send_for_eth,
who: "Ethereum",
};

let res = ethereum_node::run(ethereum_node, eth_abort_recv)
.map_err(Error::Ethereum)
.await;
tracing::info!("Ethereum fullnode is no longer running.");

drop(aborter);
res
});
(ethereum_url, handle, abort_sender)
}
};

let (event_sender, event_receiver) = unbounded_channel();
let oracle = local.spawn_local(async move {
ethereum_node::run_oracle(&ethereum_url, eth_sender, abort_sender)
tracing::info!("starting Ethereum oracle");
ethereum_node::run_oracle(&ethereum_url, event_sender, abort_sender)
.await
});

Expand All @@ -372,7 +395,7 @@ async fn run_aux(config: config::Ledger, wasm_dir: PathBuf) {
eth_abort_resp_send,
eth_abort_resp_recv,
)),
Some((local, oracle, eth_receiver)),
Some((local, oracle, event_receiver)),
Some((
tokio::spawn(async move {
// Construct a service for broadcasting protocol txs from
Expand Down

0 comments on commit 32289f4

Please sign in to comment.