Skip to content

Commit 1703c1b

Browse files
Copilotlarp0
andcommitted
Address code review feedback: enhance workflows, add RPC logging macro, fix error handling
Co-authored-by: larp0 <[email protected]>
1 parent 76781e9 commit 1703c1b

File tree

6 files changed

+154
-8
lines changed

6 files changed

+154
-8
lines changed

.github/workflows/audit.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ jobs:
1818
- name: Install Rust
1919
uses: dtolnay/rust-toolchain@stable
2020

21+
- name: Cache cargo-audit
22+
uses: actions/cache@v3
23+
with:
24+
path: ~/.cargo/bin/cargo-audit
25+
key: ${{ runner.os }}-cargo-audit-${{ hashFiles('~/.cargo/bin/cargo-audit') }}
26+
2127
- name: Install cargo-audit
22-
run: cargo install cargo-audit
28+
run: |
29+
if [ ! -f ~/.cargo/bin/cargo-audit ]; then
30+
cargo install cargo-audit
31+
fi
2332
2433
- name: Run cargo-audit
25-
run: cargo audit
34+
run: cargo audit

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ jobs:
4444
run: |
4545
cargo build --release --target ${{ matrix.target }}
4646
47+
- name: Check for dependency drift
48+
run: |
49+
cargo update --dry-run
50+
4751
- name: Run tests
4852
run: |
4953
cargo test --target ${{ matrix.target }}
5054
# Skip tests for cross-compilation targets that can't run natively
51-
if: ${{ !(matrix.os == 'macos-latest' && matrix.target == 'aarch64-apple-darwin' && runner.arch == 'X64') }}
55+
if: ${{ !(matrix.os == 'macos-latest' && matrix.target == 'aarch64-apple-darwin' && runner.arch == 'X64') }}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod config;
22
pub mod error;
3+
#[macro_use]
34
pub mod logging;
45
pub mod protocol;
56
pub mod rpc;

src/logging.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,97 @@ pub fn create_result_summary(result: &Value) -> String {
412412
}
413413
}
414414

415+
/// Macro to reduce repetitive boilerplate around timing and logs for RPC calls
416+
#[macro_export]
417+
macro_rules! log_rpc_call {
418+
($method:expr, $client:expr, $async_block:expr) => {{
419+
let request_id = $crate::logging::new_request_id();
420+
let start_time = std::time::Instant::now();
421+
422+
$crate::logging::log_rpc_request_start(
423+
request_id,
424+
$method,
425+
Some(&$client.url()),
426+
None,
427+
);
428+
429+
match $async_block.await {
430+
Ok(result) => {
431+
let duration = start_time.elapsed().as_millis() as u64;
432+
433+
$crate::logging::log_rpc_request_success(
434+
request_id,
435+
$method,
436+
duration,
437+
Some("request completed"),
438+
);
439+
440+
Ok(result)
441+
}
442+
Err(e) => {
443+
let duration = start_time.elapsed().as_millis() as u64;
444+
let error = $crate::error::McpError::from(e)
445+
.with_request_id(request_id)
446+
.with_method($method)
447+
.with_rpc_url(&$client.url());
448+
449+
$crate::logging::log_rpc_request_failure(
450+
request_id,
451+
$method,
452+
duration,
453+
&error.to_string(),
454+
Some("request failed"),
455+
);
456+
457+
Err(error)
458+
}
459+
}
460+
}};
461+
($method:expr, $client:expr, $async_block:expr, $params:expr) => {{
462+
let request_id = $crate::logging::new_request_id();
463+
let start_time = std::time::Instant::now();
464+
465+
$crate::logging::log_rpc_request_start(
466+
request_id,
467+
$method,
468+
Some(&$client.url()),
469+
Some($params),
470+
);
471+
472+
match $async_block.await {
473+
Ok(result) => {
474+
let duration = start_time.elapsed().as_millis() as u64;
475+
476+
$crate::logging::log_rpc_request_success(
477+
request_id,
478+
$method,
479+
duration,
480+
Some("request completed"),
481+
);
482+
483+
Ok(result)
484+
}
485+
Err(e) => {
486+
let duration = start_time.elapsed().as_millis() as u64;
487+
let error = $crate::error::McpError::from(e)
488+
.with_request_id(request_id)
489+
.with_method($method)
490+
.with_rpc_url(&$client.url());
491+
492+
$crate::logging::log_rpc_request_failure(
493+
request_id,
494+
$method,
495+
duration,
496+
&error.to_string(),
497+
Some("request failed"),
498+
);
499+
500+
Err(error)
501+
}
502+
}
503+
}};
504+
}
505+
415506
#[cfg(test)]
416507
mod tests {
417508
use super::*;

src/rpc/accounts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub async fn get_program_accounts_with_config(
366366
min_context_slot: None,
367367
},
368368
with_context: None,
369-
sort_results: None,
369+
sort_results: None, // Use default sorting behavior
370370
};
371371
match client.get_program_accounts_with_config(program_id, config).await {
372372
Ok(accounts) => {
@@ -421,7 +421,7 @@ pub async fn get_largest_accounts(
421421
let config = solana_client::rpc_config::RpcLargestAccountsConfig {
422422
commitment: None,
423423
filter,
424-
sort_results: None,
424+
sort_results: None, // Use default sorting behavior
425425
};
426426

427427
match client.get_largest_accounts_with_config(config).await {

src/rpc/system.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,50 @@ pub async fn request_airdrop(
464464
client: &RpcClient,
465465
pubkey: &Pubkey,
466466
lamports: u64,
467-
) -> anyhow::Result<Value> {
468-
let signature = client.request_airdrop(pubkey, lamports).await?;
469-
Ok(serde_json::json!({ "signature": signature }))
467+
) -> McpResult<Value> {
468+
let request_id = new_request_id();
469+
let start_time = Instant::now();
470+
let method = "requestAirdrop";
471+
472+
log_rpc_request_start(
473+
request_id,
474+
method,
475+
Some(&client.url()),
476+
Some(&format!("pubkey: {}, lamports: {}", pubkey, lamports)),
477+
);
478+
479+
match client.request_airdrop(pubkey, lamports).await {
480+
Ok(signature) => {
481+
let duration = start_time.elapsed().as_millis() as u64;
482+
let result = serde_json::json!({ "signature": signature });
483+
484+
log_rpc_request_success(
485+
request_id,
486+
method,
487+
duration,
488+
Some("airdrop requested"),
489+
);
490+
491+
Ok(result)
492+
}
493+
Err(e) => {
494+
let duration = start_time.elapsed().as_millis() as u64;
495+
let error = McpError::from(e)
496+
.with_request_id(request_id)
497+
.with_method(method)
498+
.with_rpc_url(&client.url());
499+
500+
log_rpc_request_failure(
501+
request_id,
502+
method,
503+
&error.to_string(),
504+
duration,
505+
None,
506+
);
507+
508+
Err(error)
509+
}
510+
}
470511
}
471512

472513
pub async fn request_airdrop_with_config(

0 commit comments

Comments
 (0)