From 776c76eb3a2fadf89f28af739c79c9fe5117f5b5 Mon Sep 17 00:00:00 2001 From: sakridge Date: Thu, 16 Sep 2021 16:12:26 +0300 Subject: [PATCH] Only allow votes when root distance gets too high #2 Updates from master --- core/src/banking_stage.rs | 38 +++++++++++++++++++++++++++++++++++--- core/src/replay_stage.rs | 9 ++++++++- runtime/src/bank.rs | 2 +- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index f3662fa0b59e0e..e5c51e9529f1ab 100644 --- a/core/src/banking_stage.rs +++ b/core/src/banking_stage.rs @@ -50,7 +50,6 @@ use solana_sdk::{ use solana_transaction_status::token_balances::{ collect_token_balances, TransactionTokenBalancesSet, }; -use solana_vote_program::vote_transaction; use std::{ borrow::Cow, cmp, @@ -1033,7 +1032,7 @@ impl BankingStage { .filter_map(|tx_index| { let p = &msgs.packets[*tx_index]; let tx: Transaction = limited_deserialize(&p.data[0..p.meta.size]).ok()?; - if votes_only && vote_transaction::parse_vote_transaction(&tx).is_none() { + if votes_only && !solana_runtime::bank::is_simple_vote_transaction(&tx) { return None; } tx.verify_precompiles(libsecp256k1_0_5_upgrade_enabled) @@ -1174,7 +1173,7 @@ impl BankingStage { msgs, &transaction_indexes, bank.libsecp256k1_0_5_upgrade_enabled(), - false, + bank.vote_only_bank(), ); let tx_count = transaction_to_packet_indexes.len(); @@ -2914,4 +2913,37 @@ mod tests { transaction.message_data() ); } + + #[test] + fn test_transactions_from_packets() { + use solana_vote_program::vote_state::Vote; + solana_logger::setup(); + let mut vote_packet = Packet::default(); + let vote_instruction = solana_vote_program::vote_instruction::vote( + &Pubkey::new_unique(), + &Pubkey::new_unique(), + Vote::default(), + ); + let vote_transaction = + Transaction::new_with_payer(&[vote_instruction], Some(&Pubkey::new_unique())); + Packet::populate_packet(&mut vote_packet, None, &vote_transaction).unwrap(); + let mut non_vote = Packet::default(); + let tx = system_transaction::transfer( + &Keypair::new(), + &Pubkey::new_unique(), + 2, + Hash::default(), + ); + Packet::populate_packet(&mut non_vote, None, &tx).unwrap(); + let msgs = Packets::new(vec![non_vote, vote_packet]); + let packet_indexes = [0, 1]; + let (transactions, _transaction_to_packet_indexes) = + BankingStage::transactions_from_packets(&msgs, &packet_indexes, false, true); + assert_eq!(transactions.len(), 1); + assert!(!transactions[0].transaction().signatures.is_empty()); + + let (transactions, _transaction_to_packet_indexes) = + BankingStage::transactions_from_packets(&msgs, &packet_indexes, false, false); + assert_eq!(transactions.len(), 2); + } } diff --git a/core/src/replay_stage.rs b/core/src/replay_stage.rs index 5ab1cd3ed207d3..b115fd451eccf2 100644 --- a/core/src/replay_stage.rs +++ b/core/src/replay_stage.rs @@ -1228,6 +1228,13 @@ impl ReplayStage { ); let root_distance = poh_slot - root_slot; + const MAX_ROOT_DISTANCE_FOR_VOTE_ONLY: Slot = 500; + let vote_only_bank = if root_distance > MAX_ROOT_DISTANCE_FOR_VOTE_ONLY { + datapoint_info!("vote-only-bank", ("slot", poh_slot, i64)); + true + } else { + false + }; let tpu_bank = Self::new_bank_from_parent_with_notify( &parent, @@ -1235,7 +1242,7 @@ impl ReplayStage { root_slot, my_pubkey, rpc_subscriptions, - root_distance > 500, + vote_only_bank, ); let tpu_bank = bank_forks.write().unwrap().insert(tpu_bank); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index a0506ec422c6c0..d6d6f0e4db4ac2 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -5150,7 +5150,7 @@ pub fn goto_end_of_slot(bank: &mut Bank) { } } -fn is_simple_vote_transaction(transaction: &Transaction) -> bool { +pub fn is_simple_vote_transaction(transaction: &Transaction) -> bool { if transaction.message.instructions.len() == 1 { let instruction = &transaction.message.instructions[0]; let program_pubkey =