Skip to content

Commit

Permalink
Add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Bridgerz committed May 23, 2024
1 parent 3ed035f commit 9089ef0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sui-bridge-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false
edition = "2021"

[dependencies]
serde.workspace = true
diesel = { version = "2.1.4", features = ["postgres", "r2d2", "serde_json"] }
ethers = "2.0"
tokio = { workspace = true, features = ["full"] }
Expand All @@ -21,6 +22,7 @@ bin-version.workspace = true
anyhow.workspace = true
mysten-metrics.workspace = true
bcs.workspace = true
serde_yaml.workspace = true

[dev-dependencies]
sui-types = { workspace = true, features = ["test-utils"] }
Expand Down
57 changes: 18 additions & 39 deletions crates/sui-bridge-indexer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
use anyhow::Result;
use std::{fs, path::Path};

// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use serde::Deserialize;

use clap::*;
use std::path::PathBuf;

#[derive(Parser, Clone, Debug)]
#[clap(
name = "Bridge Indexer",
about = "Run an indexer for the bridge.\n\
It uses the data ingestion framework to read sui transactions and listens\n\
to Ethereum events in order to generate data related to the bridge.\n\
Data is written to postgres tables and can be used for dashboards and general checks\n\
on bridge health.",
rename_all = "kebab-case"
)]
pub struct BridgeIndexerConfig {
/// URL of the sui remote store.
#[clap(long, short = 'r', required = true)]
pub remote_store_url: Option<String>,
/// URL for Eth fullnode.
#[clap(long, short = 'e', required = true)]
/// config as loaded from `config.yaml`.
#[derive(Debug, Deserialize)]
pub struct Config {
pub remote_store_url: String,
pub eth_rpc_url: String,
/// URL of the DB instance holding indexed bridge data.
#[clap(long, short = 'd', required = true)]
pub db_url: String,
/// Path to the file where the progress store is stored.
#[clap(
long,
short = 'p',
default_value = "/tmp/progress_store",
global = true
)]
pub progress_store_file: PathBuf,
/// Path to the directory where the checkpoints are stored.
#[clap(long, short = 'c', default_value = "/tmp", global = true)]
pub checkpoints_path: PathBuf,
/// Number of worker threads to use.
#[clap(long, short = 't', default_value = "1", global = true)]
pub concurrency: usize,
/// Address of the SuiBridge contract
#[clap(long, required = true, short = 'a')]
pub progress_store_file: String,
pub checkpoints_path: String,
pub concurrency: u64,
pub eth_sui_bridge_contract_address: String,
/// Block to start indexing from
#[clap(long, required = true, short = 's')]
pub start_block: u64,
}

/// Load the config to run.
pub fn load_config(path: &Path) -> Result<Config> {
let reader = fs::File::open(path)?;
let config: Config = serde_yaml::from_reader(reader)?;
Ok(config)
}
18 changes: 18 additions & 0 deletions crates/sui-bridge-indexer/src/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# config.yaml format:

# URL of the remote store
# remote_store_url: <url>
# URL for Ethereum RPC
# eth_rpc_url: <url>
# Database connection URL
# db_url: <url>
# File to store the progress
# progress_store_file: <path>
# Path to the checkpoints
# checkpoints_path: <path>
# Number of concurrent operations
# concurrency: 1
# Ethereum to Sui bridge contract address
# eth_sui_bridge_contract_address: <contract_address>
# Starting block number
# start_block: <indexing_start_block>
34 changes: 26 additions & 8 deletions crates/sui-bridge-indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use clap::Parser;
use clap::*;
use ethers::types::Address as EthAddress;
use mysten_metrics::spawn_logged_monitored_task;
use mysten_metrics::start_prometheus_server;
use prometheus::Registry;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::net::IpAddr;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use sui_bridge::{
Expand All @@ -21,18 +23,34 @@ use sui_bridge::{
};
use sui_bridge_indexer::postgres_writer::get_connection_pool;
use sui_bridge_indexer::{
config::BridgeIndexerConfig, worker::process_eth_transaction, worker::BridgeWorker,
config::load_config, worker::process_eth_transaction, worker::BridgeWorker,
};
use sui_data_ingestion_core::{
DataIngestionMetrics, FileProgressStore, IndexerExecutor, ReaderOptions, WorkerPool,
};
use tokio::sync::oneshot;
use tracing::info;

#[derive(Parser, Clone, Debug)]
#[clap(group(ArgGroup::new("input").required(true).args(&["config_path"])))]
struct Args {
/// Path to a yaml config
#[clap(long, short, default_value = "config.yaml")]
config_path: Option<PathBuf>,
}

#[tokio::main]
async fn main() -> Result<()> {
let config = BridgeIndexerConfig::parse();
info!("Parsed config: {:#?}", config);
let args = Args::parse();

// load config
let config_path = if let Some(path) = args.config_path {
path.join("config.yaml")
} else {
env::current_dir().unwrap().join("config.yaml")
};

let config = load_config(&config_path).unwrap();

// start metrics server
let (_exit_sender, exit_receiver) = oneshot::channel();
Expand Down Expand Up @@ -88,18 +106,18 @@ async fn main() -> Result<()> {
);

// start sui side
let progress_store = FileProgressStore::new(config.progress_store_file);
let progress_store = FileProgressStore::new(config.progress_store_file.into());
let mut executor = IndexerExecutor::new(progress_store, 1 /* workflow types */, metrics);
let worker_pool = WorkerPool::new(
BridgeWorker::new(vec![], config.db_url.clone()),
"bridge worker".into(),
config.concurrency,
config.concurrency as usize,
);
executor.register(worker_pool).await?;
executor
.run(
config.checkpoints_path,
config.remote_store_url,
config.checkpoints_path.into(),
Some(config.remote_store_url),
vec![], // optional remote store access options
ReaderOptions::default(),
exit_receiver,
Expand Down

0 comments on commit 9089ef0

Please sign in to comment.