Skip to content

Commit a2b83fb

Browse files
NikkiAungclaude
andauthored
fix(rpc): sanitize transactions before reading them, as Agave does (solana-foundation#911)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2a01556 commit a2b83fb

8 files changed

Lines changed: 310 additions & 103 deletions

File tree

‎crates/core/src/rpc/full.rs‎

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use solana_pubkey::Pubkey;
2626
use solana_rpc_client_api::response::Response as RpcResponse;
2727
use solana_sdk_ids::compute_budget;
2828
use solana_signature::Signature;
29+
use solana_transaction::versioned::VersionedTransaction;
2930
use solana_transaction_error::TransactionError;
3031
use solana_transaction_status::{
3132
EncodedConfirmedTransactionWithStatusMeta, EncodedTransactionWithStatusMeta,
@@ -39,7 +40,7 @@ use surfpool_types::{
3940
use super::{
4041
RunloopContext, State, SurfnetRpcContext,
4142
utils::{
42-
decode_and_deserialize, decode_rpc_versioned_transaction,
43+
decode_and_deserialize, decode_rpc_versioned_transaction, into_versioned_transaction,
4344
verify_and_parse_signatures_for_address_params, verify_pubkey,
4445
},
4546
};
@@ -1754,11 +1755,13 @@ impl Full for SurfpoolFullRpc {
17541755
let rpc_start = std::time::Instant::now();
17551756

17561757
let config = config.unwrap_or_default();
1757-
let unsanitized_tx = decode_rpc_versioned_transaction(data, config.base.encoding)?;
1758-
let signatures = unsanitized_tx.signatures.clone();
1759-
let signature = signatures[0];
1760-
// Clone the message before moving the transaction, as we'll need it for error reporting
1761-
let tx_message = unsanitized_tx.message.clone();
1758+
let transaction = decode_rpc_versioned_transaction(data, config.base.encoding)?;
1759+
let signature = transaction.signatures()[0];
1760+
// Preflight and error reports read a copy, as the runloop takes the transaction.
1761+
let tx = VersionedTransaction {
1762+
signatures: transaction.signatures().to_vec(),
1763+
message: transaction.get_message().message.clone(),
1764+
};
17621765

17631766
let Some(ctx) = meta else {
17641767
return Err(RpcCustomError::NodeUnhealthy {
@@ -1788,19 +1791,15 @@ impl Full for SurfpoolFullRpc {
17881791
let sigverify = !config
17891792
.skip_sig_verify
17901793
.unwrap_or(svm_reader.skip_signature_verification);
1791-
let duplicate =
1792-
svm_reader
1793-
.processed_slot(&unsanitized_tx)
1794-
.is_some_and(|slot| {
1795-
slot <= svm_reader.slot_for_commitment(&preflight_commitment)
1796-
});
1797-
if sigverify && unsanitized_tx.verify_with_results().contains(&false) {
1794+
let duplicate = svm_reader.processed_slot(&tx).is_some_and(|slot| {
1795+
slot <= svm_reader.slot_for_commitment(&preflight_commitment)
1796+
});
1797+
if sigverify && tx.verify_with_results().contains(&false) {
17981798
Some(TransactionError::SignatureFailure)
17991799
} else if !svm_reader.is_blockhash_visible_at(
1800-
tx_message.recent_blockhash(),
1800+
tx.message.recent_blockhash(),
18011801
&preflight_commitment,
1802-
) || (duplicate
1803-
&& !svm_reader.validate_transaction_blockhash(&unsanitized_tx))
1802+
) || (duplicate && !svm_reader.validate_transaction_blockhash(&tx))
18041803
{
18051804
Some(TransactionError::BlockhashNotFound)
18061805
} else if duplicate {
@@ -1818,7 +1817,7 @@ impl Full for SurfpoolFullRpc {
18181817
Some(error.clone()),
18191818
None,
18201819
false,
1821-
&tx_message,
1820+
&tx.message,
18221821
None,
18231822
Some(0),
18241823
))
@@ -1840,7 +1839,7 @@ impl Full for SurfpoolFullRpc {
18401839
.simnet_commands_tx
18411840
.send(SimnetCommand::ProcessTransaction(
18421841
ctx.id,
1843-
unsanitized_tx,
1842+
transaction,
18441843
status_update_tx,
18451844
config.base.skip_preflight,
18461845
config.skip_sig_verify,
@@ -1880,7 +1879,7 @@ impl Full for SurfpoolFullRpc {
18801879
Some(error.clone()),
18811880
None,
18821881
false,
1883-
&tx_message,
1882+
&tx.message,
18841883
None, // No loaded addresses available in error reporting context
18851884
Some(metadata.loaded_accounts_data_size),
18861885
))
@@ -1972,8 +1971,8 @@ impl Full for SurfpoolFullRpc {
19721971
return SurfpoolError::sig_verify_replace_recent_blockhash_collision().into();
19731972
}
19741973

1975-
let mut unsanitized_tx = match decode_rpc_versioned_transaction(data, config.encoding) {
1976-
Ok(tx) => tx,
1974+
let mut tx = match decode_rpc_versioned_transaction(data, config.encoding) {
1975+
Ok(tx) => into_versioned_transaction(tx),
19771976
Err(e) => return Box::pin(async move { Err(e) }),
19781977
};
19791978

@@ -1987,10 +1986,10 @@ impl Full for SurfpoolFullRpc {
19871986

19881987
Box::pin(async move {
19891988
let loaded_addresses = svm_locker
1990-
.get_loaded_addresses(&remote_ctx, &unsanitized_tx.message)
1989+
.get_loaded_addresses(&remote_ctx, &tx.message)
19911990
.await?;
19921991
let transaction_pubkeys = svm_locker.get_pubkeys_from_message(
1993-
&unsanitized_tx.message,
1992+
&tx.message,
19941993
loaded_addresses.as_ref().map(|l| l.all_loaded_addresses()),
19951994
);
19961995

@@ -2009,16 +2008,15 @@ impl Full for SurfpoolFullRpc {
20092008
let replacement_blockhash = config.replace_recent_blockhash.then(|| {
20102009
let (blockhash, last_valid_block_height) =
20112010
svm_reader.latest_blockhash_at(&config.commitment.unwrap_or_default());
2012-
unsanitized_tx.message.set_recent_blockhash(blockhash);
2011+
tx.message.set_recent_blockhash(blockhash);
20132012
RpcBlockhash {
20142013
blockhash: blockhash.to_string(),
20152014
last_valid_block_height,
20162015
}
20172016
});
20182017
// Clone the message before moving the transaction for result formatting
2019-
let tx_message = unsanitized_tx.message.clone();
2020-
let simulation =
2021-
svm_reader.simulate_transaction(unsanitized_tx, config.sig_verify);
2018+
let tx_message = tx.message.clone();
2019+
let simulation = svm_reader.simulate_transaction(tx, config.sig_verify);
20222020
Ok::<_, jsonrpc_core::Error>((
20232021
read,
20242022
replacement_blockhash,
@@ -2795,7 +2793,7 @@ mod tests {
27952793
BlockHeader, INITIAL_SLOT, LEGACY_FINALIZATION_SLOT_THRESHOLD,
27962794
remote::SurfnetRemoteClient, svm::MAX_RECENT_BLOCKHASHES_STANDARD,
27972795
},
2798-
tests::helpers::TestSetup,
2796+
tests::helpers::{TestSetup, UNSANITARY_TRANSACTION_ERROR, unsanitary_transactions},
27992797
types::{SyntheticBlockhash, TransactionWithStatusMeta},
28002798
};
28012799

@@ -2945,6 +2943,7 @@ mod tests {
29452943
loop {
29462944
match mempool_rx.recv() {
29472945
Ok(SimnetCommand::ProcessTransaction(_, tx, status_tx, _, _)) => {
2946+
let tx = into_versioned_transaction(tx);
29482947
let sig = tx.signatures[0];
29492948
assert!(
29502949
setup.context.svm_locker.is_transaction_pending(&sig),
@@ -3545,6 +3544,76 @@ mod tests {
35453544
assert!(!setup.context.svm_locker.is_transaction_pending(&signature));
35463545
}
35473546

3547+
#[test]
3548+
fn a_malformed_transaction_does_not_stop_block_production() {
3549+
let (setup, clock_events_tx, runloop) =
3550+
new_airdrop_runloop(BlockProductionMode::Transaction);
3551+
let payer = Keypair::new();
3552+
setup.context.svm_locker.with_svm_writer(|svm| {
3553+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL)
3554+
.expect("payer airdrop should be accepted")
3555+
.expect("payer airdrop transaction should succeed");
3556+
});
3557+
let recent_blockhash = setup
3558+
.context
3559+
.svm_locker
3560+
.with_svm_reader(|svm| svm.latest_blockhash());
3561+
let send = |transaction: &VersionedTransaction| {
3562+
hiro_system_kit::nestable_block_on(setup.rpc.send_transaction(
3563+
Some(setup.context.clone()),
3564+
bs58::encode(bincode::serialize(transaction).unwrap()).into_string(),
3565+
Some(SurfpoolRpcSendTransactionConfig {
3566+
base: RpcSendTransactionConfig {
3567+
skip_preflight: true,
3568+
..RpcSendTransactionConfig::default()
3569+
},
3570+
skip_sig_verify: None,
3571+
}),
3572+
))
3573+
};
3574+
3575+
for transaction in unsanitary_transactions(&payer, recent_blockhash) {
3576+
let error = send(&transaction).expect_err("an unsanitary transaction must be refused");
3577+
assert_eq!(error.code, jsonrpc_core::ErrorCode::InvalidParams);
3578+
assert_eq!(error.message, UNSANITARY_TRANSACTION_ERROR);
3579+
}
3580+
let landed = send(&build_legacy_transaction(
3581+
&payer.pubkey(),
3582+
&[&payer],
3583+
&[transfer(&payer.pubkey(), &Pubkey::new_unique(), 1_000_000)],
3584+
&recent_blockhash,
3585+
));
3586+
3587+
stop_airdrop_runloop(&setup, clock_events_tx, runloop);
3588+
assert!(
3589+
landed.is_ok(),
3590+
"the runloop should still process transactions, got {landed:?}"
3591+
);
3592+
}
3593+
3594+
#[tokio::test(flavor = "multi_thread")]
3595+
async fn simulate_transaction_refuses_an_unsanitary_transaction_as_agave_does() {
3596+
let setup = TestSetup::new(SurfpoolFullRpc);
3597+
let recent_blockhash = setup
3598+
.context
3599+
.svm_locker
3600+
.with_svm_reader(|svm| svm.latest_blockhash());
3601+
3602+
for transaction in unsanitary_transactions(&Keypair::new(), recent_blockhash) {
3603+
let error = setup
3604+
.rpc
3605+
.simulate_transaction(
3606+
Some(setup.context.clone()),
3607+
bs58::encode(bincode::serialize(&transaction).unwrap()).into_string(),
3608+
None,
3609+
)
3610+
.await
3611+
.expect_err("an unsanitary transaction must be refused");
3612+
assert_eq!(error.code, jsonrpc_core::ErrorCode::InvalidParams);
3613+
assert_eq!(error.message, UNSANITARY_TRANSACTION_ERROR);
3614+
}
3615+
}
3616+
35483617
#[test]
35493618
fn test_request_airdrop() {
35503619
let pk = Pubkey::new_unique();
@@ -6291,6 +6360,7 @@ mod tests {
62916360
loop {
62926361
match mempool_rx.recv() {
62936362
Ok(SimnetCommand::ProcessTransaction(_, tx, status_tx, _, _)) => {
6363+
let tx = into_versioned_transaction(tx);
62946364
let mut writer = setup.context.svm_locker.0.write().await;
62956365
let slot = writer.get_latest_absolute_slot();
62966366
writer.transactions_queued_for_confirmation.push_back((

0 commit comments

Comments
 (0)