Skip to content

Commit 0bd0530

Browse files
committed
fix: log
1 parent cde075c commit 0bd0530

2 files changed

Lines changed: 108 additions & 91 deletions

File tree

cranker/src/lib.rs

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ impl InterceptorCranker {
7979
}
8080

8181
pub async fn start(&self) {
82-
info!("Starting InterceptorCranker service");
8382
let mut interval_timer = time::interval(self.interval);
8483
let mut tick: u64 = 0;
85-
info!("Set interval timer to {} seconds", self.interval.as_secs());
8684

8785
loop {
8886
interval_timer.tick().await;
89-
info!("Tick: Starting new processing cycle");
87+
info!("Cranker tick tick={tick}");
9088
match emit_heartbeat(self.rpc_client.clone(), tick, &self.cluster_name).await {
9189
Ok(_) => tick += 1,
9290
Err(e) => emit_error(format!("Failed to emit heartbeat: {e}"), &self.cluster_name),
@@ -108,19 +106,18 @@ impl InterceptorCranker {
108106
}
109107

110108
match self.process_expired_receipts().await {
111-
Ok(_) => info!("Successfully processed expired receipts"),
109+
Ok(_) => info!("Processed expired receipts tick={tick}"),
112110
Err(e) => emit_error(
113-
format!("Error processing receipts: {e}"),
111+
format!("Failed to process expired receipts: {e}"),
114112
&self.cluster_name,
115113
),
116114
}
117115
}
118116
}
119117

120118
async fn process_expired_receipts(&self) -> Result<(), CrankerError> {
121-
info!("Starting to process expired receipts");
122119
let receipts = self.get_deposit_receipts().await?;
123-
info!("Found {} deposit receipts", receipts.len());
120+
info!("Fetched deposit receipts count={}", receipts.len());
124121

125122
let now = SystemTime::now()
126123
.duration_since(UNIX_EPOCH)
@@ -133,52 +130,41 @@ impl InterceptorCranker {
133130
let mut claimed_receipts: u64 = 0;
134131

135132
for receipt in receipts {
136-
// Get raw bytes using bytemuck and interpret as little-endian
137133
let deposit_time = u64::from(receipt.deposit_time);
138134
let cool_down = u64::from(receipt.cool_down_seconds);
139135

140136
info!(
141-
"Receipt {} raw bytes:\n\
142-
Interpreted values:\n\
143-
deposit_time: {}\n\
144-
cool_down: {}\n\
145-
current_time: {}",
137+
"Processing deposit receipt base={} deposit_time={} cool_down={} now={}",
146138
receipt.base, deposit_time, cool_down, now
147139
);
148140
emit_deposit_receipt(&receipt, &self.cluster_name);
149141

150142
if deposit_time > now {
151143
info!(
152-
"Receipt {} not yet expired (future deposit time). Current time: {}, Deposit time: {}",
153-
receipt.base,
154-
now,
155-
deposit_time
156-
);
144+
"Skipping receipt status=future_deposit base={} deposit_time={} now={}",
145+
receipt.base, deposit_time, now
146+
);
157147
future_deposits += 1;
158148
continue;
159149
}
160150

161-
// Safe addition check
162151
match deposit_time.checked_add(cool_down) {
163152
Some(expiry_time) => {
164153
if now > expiry_time {
165154
info!(
166-
"Receipt {} is expired. Current time: {}, Expiry time: {}",
167-
receipt.base, now, expiry_time
155+
"Receipt expired base={} expiry_time={} now={}",
156+
receipt.base, expiry_time, now
168157
);
169158
match self.claim_pool_tokens(&receipt).await {
170159
Ok(_) => {
171-
info!("Successfully claimed tokens for receipt {}", receipt.base);
160+
info!("Claimed pool tokens base={}", receipt.base);
172161
let mut metrics = self.metrics.lock().unwrap();
173162
metrics.successful_claims += 1;
174163
claimed_receipts += 1;
175164
}
176165
Err(e) => {
177166
emit_error(
178-
format!(
179-
"Failed to claim tokens for receipt {}: {}",
180-
receipt.base, e
181-
),
167+
format!("Failed to claim pool tokens base={}: {}", receipt.base, e),
182168
&self.cluster_name,
183169
);
184170
let mut metrics = self.metrics.lock().unwrap();
@@ -187,19 +173,20 @@ impl InterceptorCranker {
187173
}
188174
} else {
189175
info!(
190-
"Receipt {} not yet expired. Current time: {}, Expiry time: {}",
191-
receipt.base, now, expiry_time
176+
"Skipping receipt status=not_yet_expired base={} expiry_time={} now={}",
177+
receipt.base, expiry_time, now
192178
);
193179
not_yet_expired_receipts += 1;
194180
}
195181
}
196182
None => {
197-
emit_error(format!(
198-
"Receipt {} has invalid timing values - would overflow. Deposit time: {}, Cool down: {}",
199-
receipt.base,
200-
deposit_time,
201-
cool_down
202-
), &self.cluster_name);
183+
emit_error(
184+
format!(
185+
"Skipping receipt status=overflow base={} deposit_time={} cool_down={}",
186+
receipt.base, deposit_time, cool_down
187+
),
188+
&self.cluster_name,
189+
);
203190
}
204191
}
205192
}
@@ -217,7 +204,6 @@ impl InterceptorCranker {
217204

218205
async fn get_deposit_receipts(&self) -> Result<Vec<DepositReceipt>, CrankerError> {
219206
let discriminator = StakeDepositInterceptorDiscriminators::DepositReceipt as u8;
220-
info!("Searching for deposit receipts");
221207

222208
let accounts = self
223209
.rpc_client
@@ -239,7 +225,7 @@ impl InterceptorCranker {
239225
.await
240226
.map_err(CrankerError::RpcError)?;
241227

242-
info!("Found {} raw accounts", accounts.len());
228+
info!("Fetched program accounts count={}", accounts.len());
243229

244230
Ok(accounts
245231
.into_iter()
@@ -248,11 +234,7 @@ impl InterceptorCranker {
248234
match DepositReceipt::try_from_slice_unchecked(account_data.as_slice()) {
249235
Ok(receipt) => {
250236
info!(
251-
"Found receipt:\n\
252-
Account pubkey: {}\n\
253-
Receipt base: {}\n\
254-
Receipt stake pool: {}\n\
255-
Derived PDA: {}",
237+
"Decoded deposit receipt account={} base={} stake_pool={} derived_pda={}",
256238
pubkey,
257239
receipt.base,
258240
receipt.stake_pool,
@@ -263,12 +245,11 @@ impl InterceptorCranker {
263245
)
264246
.0
265247
);
266-
267248
Some(*receipt)
268249
}
269250
Err(e) => {
270251
emit_error(
271-
format!("Failed to deserialize receipt for {pubkey}: {e}"),
252+
format!("Failed to deserialize receipt account={pubkey}: {e}"),
272253
&self.cluster_name,
273254
);
274255
None
@@ -279,7 +260,7 @@ impl InterceptorCranker {
279260
}
280261

281262
async fn claim_pool_tokens(&self, receipt: &DepositReceipt) -> Result<(), CrankerError> {
282-
info!("Starting detailed claim debug for receipt {}", receipt.base);
263+
info!("Claiming pool tokens base={}", receipt.base);
283264

284265
let stake_pool_deposit_authority = self
285266
.get_stake_pool_deposit_authority(&receipt.stake_pool_deposit_stake_authority)
@@ -288,13 +269,12 @@ impl InterceptorCranker {
288269
let owner_ata =
289270
get_associated_token_address(&receipt.owner, &stake_pool_deposit_authority.pool_mint);
290271

291-
// Check if account exists
292272
match self.rpc_client.get_account(&owner_ata).await {
293273
Ok(_) => {
294-
info!("Owner token account exists: {owner_ata}");
274+
info!("Owner token account exists ata={owner_ata}");
295275
}
296276
Err(_) => {
297-
info!("Creating owner token account: {owner_ata}");
277+
info!("Creating owner token account ata={owner_ata}");
298278
let create_ata_ix = create_associated_token_account(
299279
&self.payer.pubkey(),
300280
&receipt.owner,
@@ -313,7 +293,7 @@ impl InterceptorCranker {
313293
self.rpc_client
314294
.send_and_confirm_transaction(&create_ata_tx)
315295
.await?;
316-
info!("Created owner ata token account");
296+
info!("Created owner token account ata={owner_ata}");
317297
}
318298
}
319299

@@ -322,19 +302,12 @@ impl InterceptorCranker {
322302
&stake_pool_deposit_authority.pool_mint,
323303
);
324304

325-
// Check if account exists
326305
match self.rpc_client.get_account(&fee_wallet_token_account).await {
327306
Ok(_) => {
328-
info!(
329-
"Fee wallet token account exists: {}",
330-
fee_wallet_token_account
331-
);
307+
info!("Fee wallet token account exists ata={fee_wallet_token_account}");
332308
}
333309
Err(_) => {
334-
info!(
335-
"Creating fee wallet token account: {}",
336-
fee_wallet_token_account
337-
);
310+
info!("Creating fee wallet token account ata={fee_wallet_token_account}");
338311
let create_ata_ix = create_associated_token_account(
339312
&self.payer.pubkey(),
340313
&stake_pool_deposit_authority.fee_wallet,
@@ -353,7 +326,7 @@ impl InterceptorCranker {
353326
self.rpc_client
354327
.send_and_confirm_transaction(&create_ata_tx)
355328
.await?;
356-
info!("Created fee wallet token account");
329+
info!("Created fee wallet token account ata={fee_wallet_token_account}");
357330
}
358331
}
359332

@@ -388,18 +361,12 @@ impl InterceptorCranker {
388361
.await
389362
{
390363
Ok(sig) => {
391-
info!(
392-
"Successfully claimed pool tokens for receipt {}. Transaction signature: {}",
393-
receipt.base, sig
394-
);
364+
info!("Claimed pool tokens base={} signature={}", receipt.base, sig);
395365
Ok(())
396366
}
397367
Err(e) => {
398368
emit_error(
399-
format!(
400-
"Failed to claim pool tokens for receipt {}. Error: {}",
401-
receipt.base, e
402-
),
369+
format!("Failed to claim pool tokens base={}: {}", receipt.base, e),
403370
&self.cluster_name,
404371
);
405372
Err(CrankerError::RpcError(e))

cranker/src/main.rs

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ::{
77
solana_metrics::set_host_id,
88
solana_sdk::{
99
pubkey::Pubkey,
10-
signature::{read_keypair_file, Signer}, // Added Signer trait
10+
signature::{read_keypair_file, Signer},
1111
},
1212
stake_deposit_interceptor_cranker::{CrankerConfig, InterceptorCranker},
1313
tracing::{info, Level},
@@ -72,40 +72,52 @@ impl TryFrom<Cli> for CrankerConfig {
7272
}
7373
}
7474

75+
/// Strips path, query, and userinfo from an RPC URL so embedded API keys
76+
/// never reach the logs.
77+
fn redact_url(url: &str) -> String {
78+
match url.split_once("://") {
79+
Some((scheme, rest)) => {
80+
let host = rest
81+
.split(['/', '?', '#'])
82+
.next()
83+
.unwrap_or("")
84+
.rsplit('@')
85+
.next()
86+
.unwrap_or("");
87+
format!("{scheme}://{host}/<redacted>")
88+
}
89+
None => "<redacted>".to_string(),
90+
}
91+
}
92+
7593
#[tokio::main]
7694
async fn main() -> Result<(), Box<dyn std::error::Error>> {
77-
// Initialize logging with a simpler configuration
78-
tracing_subscriber::fmt() // Use fully qualified path
95+
dotenv().ok();
96+
97+
tracing_subscriber::fmt()
7998
.with_max_level(Level::INFO)
8099
.with_file(true)
81100
.with_line_number(true)
82101
.with_thread_ids(true)
83-
.pretty()
102+
.compact()
84103
.init();
85104

86-
info!("Logger initialized");
87-
88-
// Load .env file before parsing so clap can read from it
89-
dotenv().ok();
90-
info!("Environment loaded");
105+
info!("Starting interceptor cranker service");
91106

92-
// Parse configuration from CLI args / environment
93107
let config = CrankerConfig::try_from(Cli::parse())?;
94108

95-
info!("Configuration loaded successfully:");
96-
info!("RPC URL: {}", config.rpc_url);
97-
info!("Program ID: {}", config.program_id);
98-
info!("Payer: {}", config.payer.as_ref().pubkey()); // Signer trait now in scope
99-
info!("Interval: {}s", config.interval.as_secs());
100-
info!("Cluster: {}", config.cluster);
101-
info!("Region: {}", config.region);
109+
info!("rpc_url={}", redact_url(&config.rpc_url));
110+
info!("program_id={}", config.program_id);
111+
info!("payer={}", config.payer.as_ref().pubkey());
112+
info!("interval_seconds={}", config.interval.as_secs());
113+
info!("cluster={}", config.cluster);
114+
info!("region={}", config.region);
102115
info!(
103-
"Stake Pool Deposit Stake Authority: {}",
116+
"stake_pool_deposit_stake_authority={}",
104117
config.stake_pool_deposit_stake_authority
105118
);
106-
info!("Whitelist: {}", config.whitelist);
119+
info!("whitelist={}", config.whitelist);
107120

108-
// Set host ID
109121
let hostname_cmd = Command::new("hostname")
110122
.output()
111123
.expect("Failed to execute hostname command");
@@ -119,13 +131,51 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
119131
config.region, config.cluster, hostname
120132
));
121133

122-
// Initialize cranker
123134
let cranker = InterceptorCranker::new(config);
124-
info!("Cranker initialized");
125-
126-
// Start processing
127-
info!("Starting cranker service...");
135+
info!("Starting cranker service");
128136
cranker.start().await;
129137

130138
Ok(())
131139
}
140+
141+
#[cfg(test)]
142+
mod tests {
143+
use super::redact_url;
144+
145+
#[test]
146+
fn test_redact_url_strips_path_and_query() {
147+
assert_eq!(
148+
redact_url("https://mainnet.helius-rpc.com/?api-key=SECRET"),
149+
"https://mainnet.helius-rpc.com/<redacted>"
150+
);
151+
assert_eq!(
152+
redact_url("https://example.com/rpc/SECRET"),
153+
"https://example.com/<redacted>"
154+
);
155+
}
156+
157+
#[test]
158+
fn test_redact_url_strips_userinfo() {
159+
assert_eq!(
160+
redact_url("https://SECRET@example.com/rpc"),
161+
"https://example.com/<redacted>"
162+
);
163+
assert_eq!(
164+
redact_url("https://user:pass@example.com:8899"),
165+
"https://example.com:8899/<redacted>"
166+
);
167+
}
168+
169+
#[test]
170+
fn test_redact_url_keeps_host_and_port() {
171+
assert_eq!(
172+
redact_url("http://127.0.0.1:8899"),
173+
"http://127.0.0.1:8899/<redacted>"
174+
);
175+
}
176+
177+
#[test]
178+
fn test_redact_url_without_scheme() {
179+
assert_eq!(redact_url("not a url"), "<redacted>");
180+
}
181+
}

0 commit comments

Comments
 (0)