Skip to content

Commit 83c84bf

Browse files
committed
use postgres in chain scraper
1 parent 71090c8 commit 83c84bf

File tree

11 files changed

+63
-70
lines changed

11 files changed

+63
-70
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/nyxd-scraper-psql/src/storage/block_storage.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ use crate::storage::transaction::PostgresStorageTransaction;
1111
use async_trait::async_trait;
1212
use nyxd_scraper_shared::storage::helpers::log_db_operation_time;
1313
use nyxd_scraper_shared::storage::{NyxdScraperStorage, NyxdScraperStorageError};
14-
use sqlx::postgres::PgConnectOptions;
1514
use sqlx::types::time::{OffsetDateTime, PrimitiveDateTime};
16-
use sqlx::ConnectOptions;
17-
use std::fmt::Debug;
18-
use std::path::Path;
1915
use tokio::time::Instant;
2016
use tracing::{debug, error, info, instrument};
2117

common/nyxd-scraper-psql/src/storage/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::storage::models::{CommitSignature, Validator};
55
use nyxd_scraper_shared::storage::helpers::log_db_operation_time;
66
use sqlx::types::time::PrimitiveDateTime;
7-
use sqlx::types::{Json, JsonValue};
7+
use sqlx::types::JsonValue;
88
use sqlx::{Executor, Postgres};
99
use tokio::time::Instant;
1010
use tracing::{instrument, trace};

nyx-chain-watcher/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ nym-bin-common = { path = "../common/bin-common", features = ["output_format"] }
2424
nym-network-defaults = { path = "../common/network-defaults" }
2525
nym-task = { path = "../common/task" }
2626
nym-validator-client = { path = "../common/client-libs/validator-client" }
27-
nyxd-scraper-sqlite = { path = "../common/nyxd-scraper-sqlite" }
27+
nyxd-scraper-psql = { path = "../common/nyxd-scraper-psql" }
2828
reqwest = { workspace = true, features = ["rustls-tls"] }
2929
schemars = { workspace = true }
3030
serde = { workspace = true, features = ["derive"] }

nyx-chain-watcher/src/chain_scraper/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::env::vars::{
66
use crate::http::state::BankScraperModuleState;
77
use async_trait::async_trait;
88
use nym_validator_client::nyxd::{Any, Coin, CosmosCoin, Hash, Msg, MsgSend, Name};
9-
use nyxd_scraper_sqlite::{
10-
MsgModule, NyxdScraperTransaction, ParsedTransactionResponse, PruningOptions, ScraperError,
11-
SqliteNyxdScraper,
9+
use nyxd_scraper_psql::{
10+
MsgModule, NyxdScraperTransaction, ParsedTransactionResponse, PostgresNyxdScraper,
11+
PruningOptions, ScraperError,
1212
};
1313
use sqlx::SqlitePool;
1414
use std::fs;
@@ -18,7 +18,7 @@ pub(crate) async fn run_chain_scraper(
1818
config: &crate::config::Config,
1919
db_pool: SqlitePool,
2020
shared_state: BankScraperModuleState,
21-
) -> anyhow::Result<SqliteNyxdScraper> {
21+
) -> anyhow::Result<PostgresNyxdScraper> {
2222
let websocket_url = std::env::var("NYXD_WS").expect("NYXD_WS not defined");
2323

2424
let rpc_url = std::env::var("NYXD").expect("NYXD not defined");
@@ -47,16 +47,16 @@ pub(crate) async fn run_chain_scraper(
4747

4848
if nuke_db {
4949
warn!("☢️☢️☢️ NUKING THE SCRAPER DATABASE");
50-
fs::remove_file(config.chain_scraper_database_path())?;
50+
fs::remove_file(config.chain_scraper_connection_string())?;
5151
}
5252

53-
let scraper = SqliteNyxdScraper::builder(nyxd_scraper_sqlite::Config {
53+
let scraper = PostgresNyxdScraper::builder(nyxd_scraper_psql::Config {
5454
websocket_url,
5555
rpc_url,
56-
database_storage: config.chain_scraper_database_path().into(),
56+
database_storage: config.chain_scraper_connection_string.clone(),
5757
pruning_options: PruningOptions::nothing(),
5858
store_precommits: false,
59-
start_block: nyxd_scraper_sqlite::StartingBlockOpts {
59+
start_block: nyxd_scraper_psql::StartingBlockOpts {
6060
start_block_height,
6161
use_best_effort_start_height,
6262
},

nyx-chain-watcher/src/cli/commands/init.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,46 @@ use crate::cli::DEFAULT_NYX_CHAIN_WATCHER_ID;
55
use crate::config::payments_watcher::HttpAuthenticationOptions::AuthorizationBearerToken;
66
use crate::config::payments_watcher::PaymentWatcherConfig;
77
use crate::config::{default_config_filepath, Config, ConfigBuilder, PaymentWatchersConfig};
8+
use crate::env::vars::*;
89
use crate::error::NyxChainWatcherError;
910
use nym_config::save_unformatted_config_to_file;
1011
use nym_validator_client::nyxd::AccountId;
1112
use std::str::FromStr;
1213

1314
#[derive(clap::Args, Debug)]
14-
pub(crate) struct Args {}
15+
pub(crate) struct Args {
16+
/// (Override) Postgres connection string for chain scraper history
17+
#[arg(long, env = NYX_CHAIN_WATCHER_HISTORY_DATABASE_PATH, alias = "chain_history_db_path")]
18+
pub(crate) chain_history_db_connection_string: String,
19+
}
1520

16-
pub(crate) async fn execute(_args: Args) -> Result<(), NyxChainWatcherError> {
21+
pub(crate) async fn execute(args: Args) -> Result<(), NyxChainWatcherError> {
1722
let config_path = default_config_filepath();
1823
let data_dir = Config::default_data_directory(&config_path)?;
1924

20-
let builder = ConfigBuilder::new(config_path.clone(), data_dir).with_payment_watcher_config(
21-
PaymentWatchersConfig {
22-
watchers: vec![PaymentWatcherConfig {
23-
id: DEFAULT_NYX_CHAIN_WATCHER_ID.to_string(),
24-
webhook_url: "https://webhook.site".to_string(),
25-
watch_for_transfer_recipient_accounts: vec![AccountId::from_str(
26-
"n17g9a2pwwkg8m60wf59pq6mv0c2wusg9ukparkz",
27-
)
28-
.unwrap()],
29-
authentication: Some(AuthorizationBearerToken {
30-
token: "1234".to_string(),
31-
}),
32-
description: None,
33-
watch_for_chain_message_types: vec![
34-
"/cosmos.bank.v1beta1.MsgSend".to_string(),
35-
"/ibc.applications.transfer.v1.MsgTransfer".to_string(),
36-
],
37-
}],
38-
},
39-
);
25+
let builder = ConfigBuilder::new(
26+
config_path.clone(),
27+
data_dir,
28+
args.chain_history_db_connection_string,
29+
)
30+
.with_payment_watcher_config(PaymentWatchersConfig {
31+
watchers: vec![PaymentWatcherConfig {
32+
id: DEFAULT_NYX_CHAIN_WATCHER_ID.to_string(),
33+
webhook_url: "https://webhook.site".to_string(),
34+
watch_for_transfer_recipient_accounts: vec![AccountId::from_str(
35+
"n17g9a2pwwkg8m60wf59pq6mv0c2wusg9ukparkz",
36+
)
37+
.unwrap()],
38+
authentication: Some(AuthorizationBearerToken {
39+
token: "1234".to_string(),
40+
}),
41+
description: None,
42+
watch_for_chain_message_types: vec![
43+
"/cosmos.bank.v1beta1.MsgSend".to_string(),
44+
"/ibc.applications.transfer.v1.MsgTransfer".to_string(),
45+
],
46+
}],
47+
});
4048

4149
let config = builder.build();
4250

nyx-chain-watcher/src/cli/commands/run/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ pub(crate) struct Args {
1010
#[arg(long, env = NYX_CHAIN_WATCHER_DATABASE_PATH)]
1111
pub(crate) chain_watcher_db_path: Option<String>,
1212

13-
/// (Override) SQLite database file path for chain scraper history
14-
#[arg(long, env = NYX_CHAIN_WATCHER_HISTORY_DATABASE_PATH)]
15-
pub(crate) chain_history_db_path: Option<String>,
13+
/// (Override) Postgres connection string for chain scraper history
14+
#[arg(long, env = NYX_CHAIN_WATCHER_HISTORY_DATABASE_PATH, alias = "chain_history_db_path")]
15+
pub(crate) chain_history_db_connection_string: String,
1616

1717
/// (Override) Watch for transfers to these recipient accounts
1818
#[clap(

nyx-chain-watcher/src/cli/commands/run/config.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub(crate) fn get_run_config(args: Args) -> Result<Config, NyxChainWatcherError>
1313
mut watch_for_chain_message_types,
1414
webhook_auth,
1515
ref chain_watcher_db_path,
16-
ref chain_history_db_path,
1716
webhook_url,
17+
..
1818
} = args;
1919

2020
// if there are no args set, then try load the config
@@ -42,18 +42,17 @@ pub(crate) fn get_run_config(args: Args) -> Result<Config, NyxChainWatcherError>
4242
let config_path = default_config_filepath();
4343
let data_dir = Config::default_data_directory(&config_path)?;
4444

45-
let mut builder = ConfigBuilder::new(config_path, data_dir);
45+
let mut builder = ConfigBuilder::new(
46+
config_path,
47+
data_dir,
48+
args.chain_history_db_connection_string,
49+
);
4650

4751
if let Some(db_path) = chain_watcher_db_path {
4852
info!("Overriding database url with '{db_path}'");
4953
builder = builder.with_db_path(db_path.clone());
5054
}
5155

52-
if let Some(db_path) = chain_history_db_path {
53-
info!("Overriding chain history database url with '{db_path}'");
54-
builder = builder.with_chain_scraper_db_path(db_path.clone());
55-
}
56-
5756
if let Some(webhook_url) = webhook_url {
5857
let authentication =
5958
webhook_auth.map(|token| HttpAuthenticationOptions::AuthorizationBearerToken { token });

nyx-chain-watcher/src/cli/commands/run/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(crate) async fn execute(args: Args, http_port: u16) -> Result<(), NyxChainWa
125125
);
126126
info!(
127127
"Chain History Database path is {:?}",
128-
std::path::Path::new(&config.chain_scraper_database_path()).canonicalize()
128+
std::path::Path::new(&config.chain_scraper_connection_string()).canonicalize()
129129
);
130130

131131
// Ensure parent directory exists

nyx-chain-watcher/src/config/mod.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::error::NyxChainWatcherError;
2020
const DEFAULT_NYM_CHAIN_WATCHER_DIR: &str = "nym-chain-watcher";
2121

2222
pub(crate) const DEFAULT_NYM_CHAIN_WATCHER_DB_FILENAME: &str = "nyx_chain_watcher.sqlite";
23-
pub(crate) const DEFAULT_NYM_CHAIN_SCRAPER_HISTORY_DB_FILENAME: &str = "chain_history.sqlite";
2423

2524
/// Derive default path to nym-chain-watcher's config directory.
2625
/// It should get resolved to `$HOME/.nym/nym-chain-watcher/config`
@@ -44,22 +43,25 @@ pub struct ConfigBuilder {
4443

4544
pub db_path: Option<String>,
4645

47-
pub chain_scraper_db_path: Option<String>,
48-
46+
pub chain_scraper_connection_string: String,
4947
pub payment_watcher_config: Option<PaymentWatchersConfig>,
5048

5149
pub logging: Option<LoggingSettings>,
5250
}
5351

5452
impl ConfigBuilder {
55-
pub fn new(config_path: PathBuf, data_dir: PathBuf) -> Self {
53+
pub fn new(
54+
config_path: PathBuf,
55+
data_dir: PathBuf,
56+
chain_scraper_connection_string: String,
57+
) -> Self {
5658
ConfigBuilder {
5759
config_path,
5860
data_dir,
5961
payment_watcher_config: None,
6062
logging: None,
6163
db_path: None,
62-
chain_scraper_db_path: None,
64+
chain_scraper_connection_string,
6365
}
6466
}
6567

@@ -68,11 +70,6 @@ impl ConfigBuilder {
6870
self
6971
}
7072

71-
pub fn with_chain_scraper_db_path(mut self, chain_scraper_db_path: String) -> Self {
72-
self.chain_scraper_db_path = Some(chain_scraper_db_path);
73-
self
74-
}
75-
7673
#[allow(dead_code)]
7774
pub fn with_payment_watcher_config(
7875
mut self,
@@ -95,7 +92,7 @@ impl ConfigBuilder {
9592
payment_watcher_config: self.payment_watcher_config.unwrap_or_default(),
9693
data_dir: self.data_dir,
9794
db_path: self.db_path,
98-
chain_scraper_db_path: self.chain_scraper_db_path,
95+
chain_scraper_connection_string: self.chain_scraper_connection_string,
9996
}
10097
}
10198
}
@@ -113,8 +110,7 @@ pub struct Config {
113110
#[serde(skip)]
114111
db_path: Option<String>,
115112

116-
#[serde(skip)]
117-
chain_scraper_db_path: Option<String>,
113+
pub chain_scraper_connection_string: String,
118114

119115
#[serde(default)]
120116
pub payment_watcher_config: PaymentWatchersConfig,
@@ -208,14 +204,8 @@ impl Config {
208204
})
209205
}
210206

211-
pub fn chain_scraper_database_path(&self) -> String {
212-
self.chain_scraper_db_path.clone().unwrap_or_else(|| {
213-
let mut path = self.data_dir.clone().to_path_buf();
214-
path.push(DEFAULT_NYM_CHAIN_SCRAPER_HISTORY_DB_FILENAME);
215-
path.to_str()
216-
.unwrap_or(DEFAULT_NYM_CHAIN_SCRAPER_HISTORY_DB_FILENAME)
217-
.to_string()
218-
})
207+
pub fn chain_scraper_connection_string(&self) -> String {
208+
self.chain_scraper_connection_string.clone()
219209
}
220210

221211
// simple wrapper that reads config file and assigns path location

0 commit comments

Comments
 (0)