Skip to content

Commit 7b953fd

Browse files
committed
log -> tracing
1 parent 4f31297 commit 7b953fd

File tree

12 files changed

+110
-96
lines changed

12 files changed

+110
-96
lines changed

.env.example

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# Example configuration for Sepolia + Celestia Mocha.
22
TENDERMINT_RPC_URL=https://rpc.celestia-mocha.com/
3-
CHAIN_ID=11155111
4-
RPC_URL=https://ethereum-sepolia.publicnode.com/
5-
CONTRACT_ADDRESS=
3+
64
# Key for relaying to the contract.
75
PRIVATE_KEY=
86

9-
NETWORK_PRIVATE_KEY=
107
# Optional: Set if you're using a custom RPC URL.
11-
NETWORK_RPC_URL=
8+
NETWORK_RPC_URL=
9+
NETWORK_PRIVATE_KEY=
10+
11+
BLOCK_UPDATE_INTERVAL=
12+
LOOP_INTERVAL_MINS=
13+
14+
# Optional: Set if you're using the KMS relayer.
15+
SECURE_RELAYER_API_KEY=
16+
SECURE_RELAYER_ENDPOINT=
17+
USE_KMS_RELAYER=

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ pgo-data.profdata
1919
.idea
2020

2121
filtered_transactions/**.csv
22-
filtered_transactions
22+
filtered_transactions
23+
24+
chains.json

Cargo.lock

Lines changed: 2 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ dotenv = "0.15.0"
2020
subtle-encoding = "0.5.1"
2121
anyhow = "1.0.82"
2222
clap = { version = "4.0", features = ["derive", "env"] }
23-
log = "0.4.21"
2423
hex = "0.4.3"
2524
futures = "0.3.30"
26-
env_logger = "0.11.3"
2725
serde_json = "1"
2826
chrono = "0.4.38"
2927
csv = "1.3.1"

script/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ dotenv = { workspace = true }
3939
subtle-encoding = { workspace = true }
4040
anyhow.workspace = true
4141
clap = { workspace = true }
42-
log = { workspace = true }
4342
hex = { workspace = true }
4443
futures = { workspace = true }
45-
env_logger = { workspace = true }
4644
serde_json = { workspace = true }
4745
chrono = { workspace = true }
4846
csv = { workspace = true }
4947
async-trait = "0.1.86"
48+
tracing = "0.1.41"
49+
tracing-subscriber = "0.3.19"
5050

5151
[build-dependencies]
5252
sp1-build = "4.0.0-rc.8"

script/bin/costs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use clap::Parser;
1515
use futures::StreamExt;
1616
use reqwest::Url;
1717
use std::{cmp::Ordering, collections::HashMap, env, fs, str::FromStr};
18+
use tracing_subscriber::EnvFilter;
1819

1920
#[derive(Parser, Debug, Clone)]
2021
#[command(about = "Get transaction costs for an address in a given month")]
@@ -153,7 +154,11 @@ async fn get_receipts_for_chain(
153154
async fn main() -> Result<()> {
154155
env::set_var("RUST_LOG", "info");
155156
dotenv::dotenv().ok();
156-
env_logger::init();
157+
158+
// Setup tracing.
159+
tracing_subscriber::fmt::fmt()
160+
.with_env_filter(EnvFilter::from_default_env())
161+
.init();
157162

158163
let args = CostScriptArgs::parse();
159164

script/bin/genesis.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
//!
99
1010
use clap::Parser;
11-
use log::info;
1211
use sp1_blobstream_script::util::*;
1312
use sp1_blobstream_script::TendermintRPCClient;
1413
use sp1_sdk::{HashableKey, Prover, ProverClient};
1514
use std::env;
15+
use tracing::info;
16+
use tracing_subscriber::EnvFilter;
1617
const BLOBSTREAMX_ELF: &[u8] = include_bytes!("../../elf/blobstream-elf");
1718

1819
#[derive(Parser, Debug, Clone)]
@@ -26,7 +27,12 @@ pub struct GenesisArgs {
2627
pub async fn main() {
2728
env::set_var("RUST_LOG", "info");
2829
dotenv::dotenv().ok();
29-
env_logger::init();
30+
31+
// Setup tracing.
32+
tracing_subscriber::fmt::fmt()
33+
.with_env_filter(EnvFilter::from_default_env())
34+
.init();
35+
3036
let data_fetcher = TendermintRPCClient::default();
3137
let args = GenesisArgs::parse();
3238

script/bin/operator.rs

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use alloy::{
2-
consensus::SignableTransaction,
3-
network::{EthereumWallet, Network, ReceiptResponse, TxSigner},
2+
network::{EthereumWallet, Network, ReceiptResponse},
43
primitives::{Address, B256},
54
providers::{Provider, ProviderBuilder},
6-
signers::{local::PrivateKeySigner, Signer},
5+
signers::local::PrivateKeySigner,
76
sol,
87
transports::Transport,
98
};
109
use anyhow::Result;
11-
use log::{error, info};
1210
use sp1_blobstream_primitives::get_header_update_verdict;
1311
use sp1_blobstream_script::util::{
1412
fetch_input_for_blobstream_proof, find_block_to_request, get_latest_block_height,
@@ -22,8 +20,10 @@ use sp1_sdk::{
2220
use std::{env, sync::Arc};
2321
use std::{marker::PhantomData, time::Duration};
2422
use tendermint_light_client_verifier::Verdict;
23+
use tracing::{error, info};
24+
use tracing_subscriber::EnvFilter;
2525

26-
use signer::MaybeSigner;
26+
use signer::MaybeWallet;
2727

2828
sol! {
2929
#[allow(missing_docs)]
@@ -69,7 +69,6 @@ impl<P: Provider<T, N>, T: Transport + Clone, N: Network> SP1BlobstreamOperator<
6969
///
7070
/// # Panics
7171
/// - If the chain id cannot be retrieved from the provider.
72-
/// - If the signer is not provided and were not using the KMS relayer.
7372
pub async fn new(
7473
provider: P,
7574
contract_address: Address,
@@ -288,7 +287,7 @@ impl ChainConfig {
288287
///
289288
/// If neither are set, it will try to use [`Self::from_env`].
290289
fn fetch() -> Result<Vec<Self>> {
291-
const DEFAULT_PATH: &str = "../chains.json";
290+
const DEFAULT_PATH: &str = "chains.json";
292291

293292
let path = env::var("CHAINS_PATH").unwrap_or(DEFAULT_PATH.to_string());
294293

@@ -297,13 +296,16 @@ impl ChainConfig {
297296

298297
/// Tries to read from the `CHAINS` environment variable.
299298
fn from_env() -> Result<Vec<Self>> {
300-
let chains = env::var("CHAINS").expect("CHAINS not set.");
299+
let chains = env::var("CHAINS")?;
301300

302301
Ok(serde_json::from_str(&chains)?)
303302
}
304303

305304
fn from_file(path: &str) -> Result<Vec<Self>> {
306-
let file = std::fs::read_to_string(path)?;
305+
tracing::debug!("Reading chains from file: {}", path);
306+
307+
let file = std::fs::read_to_string(path)
308+
.inspect_err(|e| println!("Error reading file: {:?}", e))?;
307309

308310
Ok(serde_json::from_str(&file)?)
309311
}
@@ -312,29 +314,35 @@ impl ChainConfig {
312314
#[tokio::main]
313315
async fn main() {
314316
dotenv::dotenv().ok();
315-
env_logger::init();
316317

317-
let prover = ProverClient::builder().cpu().build();
318-
let (pk, vk) = prover.setup(TENDERMINT_ELF);
319-
let pk = Arc::new(pk);
318+
// Setup tracing.
319+
tracing_subscriber::fmt::fmt()
320+
.with_env_filter(EnvFilter::from_default_env())
321+
.init();
320322

321323
// Succinct deployments use the `CHAINS` environment variable.
322-
let config = ChainConfig::from_env().expect("Failed to fetch chain config.");
324+
let config = ChainConfig::fetch().expect("Failed to fetch chain config");
323325
let maybe_private_key: Option<PrivateKeySigner> = env::var("PRIVATE_KEY")
324326
.ok()
325327
.map(|s| s.parse().expect("Failed to parse PRIVATE_KEY"));
326328

329+
// Setup the KMS relayer config.
327330
let use_kms_relayer: bool = env::var("USE_KMS_RELAYER")
328331
.map(|s| s.parse().expect("USE_KMS_RELAYER failed to parse"))
329-
.expect("USE_KMS_RELAYER not set.");
332+
.expect("USE_KMS_RELAYER not set");
330333

331334
// Ensure we have a signer if we're not using the KMS relayer.
332335
if !use_kms_relayer && maybe_private_key.is_none() {
333336
panic!("PRIVATE_KEY is not set but USE_KMS_RELAYER is false.");
334337
}
335338

336339
// Setup our signer.
337-
let signer = EthereumWallet::new(MaybeSigner::new(maybe_private_key));
340+
let signer = MaybeWallet::new(maybe_private_key.map(EthereumWallet::new));
341+
342+
// Setup the prover and program keys.
343+
let prover = ProverClient::builder().cpu().build();
344+
let (pk, vk) = prover.setup(TENDERMINT_ELF);
345+
let pk = Arc::new(pk);
338346

339347
// Setup all the tasks.
340348
// These futures should never resolve, so we just await them in the main thread.
@@ -357,11 +365,14 @@ async fn main() {
357365
)
358366
.await;
359367

368+
let span = tracing::span!(tracing::Level::INFO, "operator", chain = c.name);
369+
let _guard = span.enter();
370+
360371
loop {
361372
let request_interval_mins = get_loop_interval_mins();
362373
tokio::select! {
363374
_ = tokio::time::sleep(tokio::time::Duration::from_secs(60 * LOOP_TIMEOUT_MINS)) => {
364-
log::error!("Operator took longer than {} minutes to run.", LOOP_TIMEOUT_MINS);
375+
tracing::error!("Operator took longer than {} minutes to run.", LOOP_TIMEOUT_MINS);
365376
continue;
366377
}
367378
e = operator.run() => {
@@ -383,49 +394,61 @@ async fn main() {
383394
// Run all the tasks.
384395
futures::future::try_join_all(handles).await.unwrap();
385396

386-
error!("All operators finished.");
397+
info!("All operators finished.");
387398
}
388399

400+
/// We want our operators to be generic over a single provider (which is generic over a signer).
401+
/// Using this signer, we get one concrete type as a provider, even if we dont have a private key.
389402
mod signer {
390-
use alloy::{consensus::SignableTransaction, network::TxSigner, primitives::Address};
391-
use std::marker::PhantomData;
403+
use alloy::{
404+
consensus::{TxEnvelope, TypedTransaction},
405+
network::{Network, NetworkWallet},
406+
primitives::Address,
407+
};
392408

393409
/// A signer than panics if called and not set.
394-
pub struct MaybeSigner<Sig, S> {
395-
signer: Option<S>,
396-
_phantom: PhantomData<Sig>,
397-
}
410+
#[derive(Clone, Debug)]
411+
pub struct MaybeWallet<W>(Option<W>);
398412

399-
impl<Sig, S> MaybeSigner<Sig, S> {
400-
pub fn new(signer: Option<S>) -> Self {
401-
Self {
402-
signer,
403-
_phantom: PhantomData,
404-
}
413+
impl<W> MaybeWallet<W> {
414+
pub fn new(signer: Option<W>) -> Self {
415+
Self(signer)
405416
}
406417
}
407418

408-
#[async_trait::async_trait]
409-
impl<Sig, S> TxSigner<Sig> for MaybeSigner<Sig, S>
419+
impl<W, N> NetworkWallet<N> for MaybeWallet<W>
410420
where
411-
S: TxSigner<Sig> + Send + Sync,
412-
Sig: Send + Sync,
421+
W: NetworkWallet<N>,
422+
N: Network<UnsignedTx = TypedTransaction, TxEnvelope = TxEnvelope>,
413423
{
414-
fn address(&self) -> Address {
415-
self.signer
424+
fn default_signer_address(&self) -> Address {
425+
self.0
426+
.as_ref()
427+
.expect("No signer set")
428+
.default_signer_address()
429+
}
430+
431+
fn has_signer_for(&self, address: &Address) -> bool {
432+
self.0
416433
.as_ref()
417-
.expect("Signer should be set")
418-
.address()
434+
.expect("No signer set")
435+
.has_signer_for(address)
436+
}
437+
438+
fn signer_addresses(&self) -> impl Iterator<Item = Address> {
439+
self.0.as_ref().expect("No signer set").signer_addresses()
419440
}
420441

421-
async fn sign_transaction(
442+
#[doc(alias = "sign_tx_from")]
443+
async fn sign_transaction_from(
422444
&self,
423-
tx: &mut dyn SignableTransaction<Sig>,
424-
) -> alloy::signers::Result<Sig> {
425-
self.signer
445+
sender: Address,
446+
tx: TypedTransaction,
447+
) -> alloy::signers::Result<TxEnvelope> {
448+
self.0
426449
.as_ref()
427-
.expect("Signer should be set")
428-
.sign_transaction(tx)
450+
.expect("No signer set")
451+
.sign_transaction_from(sender, tx)
429452
.await
430453
}
431454
}

script/bin/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clap::Parser;
2-
use log::debug;
32
use sp1_blobstream_script::util::fetch_input_for_blobstream_proof;
43
use sp1_blobstream_script::{TendermintRPCClient, TENDERMINT_ELF};
54
use sp1_sdk::{ProverClient, SP1Stdin};
65
use tokio::runtime;
6+
use tracing::debug;
77

88
#[derive(Parser, Debug)]
99
#[clap(author, version, about, long_about = None)]

0 commit comments

Comments
 (0)