Skip to content

Commit

Permalink
hijack secp256k1 enablement feature plumbing for libsecp256k1 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson authored and mergify[bot] committed Jul 15, 2021
1 parent 568660b commit 3a85b77
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 103 deletions.
12 changes: 9 additions & 3 deletions banks-server/src/banks_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ impl BanksServer {
}
}

fn verify_transaction(transaction: &Transaction) -> transaction::Result<()> {
fn verify_transaction(
transaction: &Transaction,
libsecp256k1_0_5_upgrade_enabled: bool,
) -> transaction::Result<()> {
if let Err(err) = transaction.verify() {
Err(err)
} else if let Err(err) = transaction.verify_precompiles() {
} else if let Err(err) = transaction.verify_precompiles(libsecp256k1_0_5_upgrade_enabled) {
Err(err)
} else {
Ok(())
Expand Down Expand Up @@ -212,7 +215,10 @@ impl Banks for BanksServer {
transaction: Transaction,
commitment: CommitmentLevel,
) -> Option<transaction::Result<()>> {
if let Err(err) = verify_transaction(&transaction) {
if let Err(err) = verify_transaction(
&transaction,
self.bank(commitment).libsecp256k1_0_5_upgrade_enabled(),
) {
return Some(Err(err));
}

Expand Down
11 changes: 5 additions & 6 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ impl BankingStage {
fn transactions_from_packets(
msgs: &Packets,
transaction_indexes: &[usize],
secp256k1_program_enabled: bool,
libsecp256k1_0_5_upgrade_enabled: bool,
cost_tracker: &Arc<RwLock<CostTracker>>,
banking_stage_stats: &BankingStageStats,
) -> (Vec<HashedTransaction<'static>>, Vec<usize>, Vec<usize>) {
Expand All @@ -1070,9 +1070,8 @@ 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 secp256k1_program_enabled {
tx.verify_precompiles().ok()?;
}
tx.verify_precompiles(libsecp256k1_0_5_upgrade_enabled)
.ok()?;
Some((tx, *tx_index))
})
.collect();
Expand Down Expand Up @@ -1180,7 +1179,7 @@ impl BankingStage {
Self::transactions_from_packets(
msgs,
&packet_indexes,
bank.secp256k1_program_enabled(),
bank.libsecp256k1_0_5_upgrade_enabled(),
cost_tracker,
banking_stage_stats,
);
Expand Down Expand Up @@ -1292,7 +1291,7 @@ impl BankingStage {
Self::transactions_from_packets(
msgs,
transaction_indexes,
bank.secp256k1_program_enabled(),
bank.libsecp256k1_0_5_upgrade_enabled(),
cost_tracker,
banking_stage_stats,
);
Expand Down
10 changes: 4 additions & 6 deletions entry/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub trait EntrySlice {
fn verify_and_hash_transactions(
&self,
skip_verification: bool,
secp256k1_program_enabled: bool,
libsecp256k1_0_5_upgrade_enabled: bool,
verify_tx_signatures_len: bool,
) -> Option<Vec<EntryType<'_>>>;
}
Expand Down Expand Up @@ -515,7 +515,7 @@ impl EntrySlice for [Entry] {
fn verify_and_hash_transactions<'a>(
&'a self,
skip_verification: bool,
secp256k1_program_enabled: bool,
libsecp256k1_0_5_upgrade_enabled: bool,
verify_tx_signatures_len: bool,
) -> Option<Vec<EntryType<'a>>> {
let verify_and_hash = |tx: &'a Transaction| -> Option<HashedTransaction<'a>> {
Expand All @@ -524,10 +524,8 @@ impl EntrySlice for [Entry] {
if size > PACKET_DATA_SIZE as u64 {
return None;
}
if secp256k1_program_enabled {
// Verify tx precompiles if secp256k1 program is enabled.
tx.verify_precompiles().ok()?;
}
tx.verify_precompiles(libsecp256k1_0_5_upgrade_enabled)
.ok()?;
if verify_tx_signatures_len && !tx.verify_signatures_len() {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2404,13 +2404,13 @@ fn main() {
let mut store_failed_count = 0;
if force_enabled_count >= 1 {
if base_bank
.get_account(&feature_set::secp256k1_program_enabled::id())
.get_account(&feature_set::spl_token_v2_multisig_fix::id())
.is_some()
{
// steal some lamports from the pretty old feature not to affect
// capitalizaion, which doesn't affect inflation behavior!
base_bank.store_account(
&feature_set::secp256k1_program_enabled::id(),
&feature_set::spl_token_v2_multisig_fix::id(),
&AccountSharedData::default(),
);
force_enabled_count -= 1;
Expand Down
2 changes: 1 addition & 1 deletion ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ pub fn confirm_slot(
let check_start = Instant::now();
let check_result = entries.verify_and_hash_transactions(
skip_verification,
bank.secp256k1_program_enabled(),
bank.libsecp256k1_0_5_upgrade_enabled(),
bank.verify_tx_signatures_len_enabled(),
);
if check_result.is_none() {
Expand Down
4 changes: 2 additions & 2 deletions programs/secp256k1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod test {
Hash::default(),
);

assert!(tx.verify_precompiles().is_ok());
assert!(tx.verify_precompiles(false).is_ok());

let index = thread_rng().gen_range(0, secp_instruction.data.len());
secp_instruction.data[index] = secp_instruction.data[index].wrapping_add(12);
Expand All @@ -54,6 +54,6 @@ pub mod test {
&[&mint_keypair],
Hash::default(),
);
assert!(tx.verify_precompiles().is_err());
assert!(tx.verify_precompiles(false).is_err());
}
}
18 changes: 13 additions & 5 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,12 +1899,15 @@ impl JsonRpcRequestProcessor {
}
}

fn verify_transaction(transaction: &Transaction) -> Result<()> {
fn verify_transaction(
transaction: &Transaction,
libsecp256k1_0_5_upgrade_enabled: bool,
) -> Result<()> {
if transaction.verify().is_err() {
return Err(RpcCustomError::TransactionSignatureVerificationFailure.into());
}

if let Err(e) = transaction.verify_precompiles() {
if let Err(e) = transaction.verify_precompiles(libsecp256k1_0_5_upgrade_enabled) {
return Err(RpcCustomError::TransactionPrecompileVerificationFailure(e).into());
}

Expand Down Expand Up @@ -3001,7 +3004,10 @@ pub mod rpc_full {
}

if !config.skip_preflight {
if let Err(e) = verify_transaction(&transaction) {
if let Err(e) = verify_transaction(
&transaction,
preflight_bank.libsecp256k1_0_5_upgrade_enabled(),
) {
return Err(e);
}

Expand Down Expand Up @@ -3064,18 +3070,20 @@ pub mod rpc_full {
let encoding = config.encoding.unwrap_or(UiTransactionEncoding::Base58);
let (_, mut transaction) = deserialize_transaction(data, encoding)?;

let bank = &*meta.bank(config.commitment);
if config.sig_verify {
if config.replace_recent_blockhash {
return Err(Error::invalid_params(
"sigVerify may not be used with replaceRecentBlockhash",
));
}

if let Err(e) = verify_transaction(&transaction) {
if let Err(e) =
verify_transaction(&transaction, bank.libsecp256k1_0_5_upgrade_enabled())
{
return Err(e);
}
}
let bank = &*meta.bank(config.commitment);
if config.replace_recent_blockhash {
transaction.message.recent_blockhash = bank.last_blockhash();
}
Expand Down
8 changes: 2 additions & 6 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use solana_sdk::{
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
clock::{BankId, Slot, INITIAL_RENT_EPOCH},
feature_set::{self, FeatureSet},
fee_calculator::{FeeCalculator, FeeConfig},
fee_calculator::FeeCalculator,
genesis_config::ClusterType,
hash::Hash,
message::Message,
Expand Down Expand Up @@ -424,10 +424,6 @@ impl Accounts {
rent_collector: &RentCollector,
feature_set: &FeatureSet,
) -> Vec<TransactionLoadResult> {
let fee_config = FeeConfig {
secp256k1_program_enabled: feature_set
.is_active(&feature_set::secp256k1_program_enabled::id()),
};
txs.zip(lock_results)
.map(|etx| match etx {
(tx, (Ok(()), nonce_rollback)) => {
Expand All @@ -440,7 +436,7 @@ impl Accounts {
.cloned()
});
let fee = if let Some(fee_calculator) = fee_calculator {
fee_calculator.calculate_fee_with_config(tx.message(), &fee_config)
fee_calculator.calculate_fee(tx.message())
} else {
return (Err(TransactionError::BlockhashNotFound), None);
};
Expand Down
38 changes: 17 additions & 21 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use solana_sdk::{
epoch_schedule::EpochSchedule,
feature,
feature_set::{self, FeatureSet},
fee_calculator::{FeeCalculator, FeeConfig, FeeRateGovernor},
fee_calculator::{FeeCalculator, FeeRateGovernor},
genesis_config::{ClusterType, GenesisConfig},
hard_forks::HardForks,
hash::{extend_and_hash, hashv, Hash},
Expand Down Expand Up @@ -3374,10 +3374,6 @@ impl Bank {
let hash_queue = self.blockhash_queue.read().unwrap();
let mut fees = 0;

let fee_config = FeeConfig {
secp256k1_program_enabled: self.secp256k1_program_enabled(),
};

let results = txs
.zip(executed)
.map(|(tx, (res, nonce_rollback))| {
Expand All @@ -3395,7 +3391,7 @@ impl Bank {
});
let fee_calculator = fee_calculator.ok_or(TransactionError::BlockhashNotFound)?;

let fee = fee_calculator.calculate_fee_with_config(tx.message(), &fee_config);
let fee = fee_calculator.calculate_fee(tx.message());

let message = tx.message();
match *res {
Expand Down Expand Up @@ -5095,11 +5091,6 @@ impl Bank {
self.rc.accounts.accounts_db.shrink_candidate_slots()
}

pub fn secp256k1_program_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::secp256k1_program_enabled::id())
}

pub fn no_overflow_rent_distribution_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::no_overflow_rent_distribution::id())
Expand All @@ -5120,6 +5111,11 @@ impl Bank {
.is_active(&feature_set::verify_tx_signatures_len::id())
}

pub fn libsecp256k1_0_5_upgrade_enabled(&self) -> bool {
self.feature_set
.is_active(&feature_set::libsecp256k1_0_5_upgrade_enabled::id())
}

// Check if the wallclock time from bank creation to now has exceeded the allotted
// time for transaction processing
pub fn should_bank_still_be_processing_txs(
Expand Down Expand Up @@ -5722,7 +5718,7 @@ pub(crate) mod tests {
cluster_type: ClusterType::MainnetBeta,
..GenesisConfig::default()
}));
let sysvar_and_native_proram_delta0 = 10;
let sysvar_and_native_proram_delta0 = 11;
assert_eq!(
bank0.capitalization(),
42 * 42 + sysvar_and_native_proram_delta0
Expand Down Expand Up @@ -7424,10 +7420,10 @@ pub(crate) mod tests {
// not being eagerly-collected for exact rewards calculation
bank0.restore_old_behavior_for_fragile_tests();

let sysvar_and_native_proram_delta0 = 10;
let sysvar_and_native_program_delta0 = 11;
assert_eq!(
bank0.capitalization(),
42 * 1_000_000_000 + sysvar_and_native_proram_delta0
42 * 1_000_000_000 + sysvar_and_native_program_delta0
);
assert!(bank0.rewards.read().unwrap().is_empty());

Expand Down Expand Up @@ -7547,7 +7543,7 @@ pub(crate) mod tests {
// not being eagerly-collected for exact rewards calculation
bank.restore_old_behavior_for_fragile_tests();

let sysvar_and_native_proram_delta = 10;
let sysvar_and_native_proram_delta = 11;
assert_eq!(
bank.capitalization(),
42 * 1_000_000_000 + sysvar_and_native_proram_delta
Expand Down Expand Up @@ -10783,25 +10779,25 @@ pub(crate) mod tests {
if bank.slot == 0 {
assert_eq!(
bank.hash().to_string(),
"Cn7Wmi7w1n9NbK7RGnTQ4LpbJ2LtoJoc1sufiTwb57Ya"
"BfvaoHkrQwrkQo7T1mW6jmJXveRy11rut8bva2H1Rt5H"
);
}
if bank.slot == 32 {
assert_eq!(
bank.hash().to_string(),
"BXupB8XsZukMTnDbKshJ8qPCydWnc8BKtSj7YTJ6gAH"
"JBGPApnSMPKZaYiR16v46XSSGcKxy8kCbVtN1CG1XDxW"
);
}
if bank.slot == 64 {
assert_eq!(
bank.hash().to_string(),
"EDkKefgSMSV1NhxnGnJP7R5AGZ2JZD6oxnoZtGuEGBCU"
"BDCt9cGPfxpgJXzp8Tq1nX1zSqpbs8xrkAFyRhmXKiuX"
);
}
if bank.slot == 128 {
assert_eq!(
bank.hash().to_string(),
"AtWu4tubU9zGFChfHtQghQx3RVWtMQu6Rj49rQymFc4z"
"4zUpK4VUhKLaPUgeMMSeDR2w827goriRL5NndJxGDVmz"
);
break;
}
Expand Down Expand Up @@ -10951,7 +10947,7 @@ pub(crate) mod tests {
// No more slots should be shrunk
assert_eq!(bank2.shrink_candidate_slots(), 0);
// alive_counts represents the count of alive accounts in the three slots 0,1,2
assert_eq!(alive_counts, vec![9, 1, 7]);
assert_eq!(alive_counts, vec![10, 1, 7]);
}

#[test]
Expand Down Expand Up @@ -10999,7 +10995,7 @@ pub(crate) mod tests {
.map(|_| bank.process_stale_slot_with_budget(0, force_to_return_alive_account))
.sum();
// consumed_budgets represents the count of alive accounts in the three slots 0,1,2
assert_eq!(consumed_budgets, 10);
assert_eq!(consumed_budgets, 11);
}

#[test]
Expand Down
16 changes: 6 additions & 10 deletions runtime/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
system_instruction_processor,
};
use solana_sdk::{
feature_set,
instruction::InstructionError,
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
pubkey::Pubkey,
Expand Down Expand Up @@ -64,6 +63,11 @@ fn genesis_builtins() -> Vec<Builtin> {
solana_config_program::id(),
with_program_logging!(solana_config_program::config_processor::process_instruction),
),
Builtin::new(
"secp256k1_program",
solana_sdk::secp256k1_program::id(),
solana_secp256k1_program::process_instruction,
),
]
}

Expand All @@ -82,15 +86,7 @@ pub enum ActivationType {
/// normal child Bank creation.
/// https://github.com/solana-labs/solana/blob/84b139cc94b5be7c9e0c18c2ad91743231b85a0d/runtime/src/bank.rs#L1723
fn feature_builtins() -> Vec<(Builtin, Pubkey, ActivationType)> {
vec![(
Builtin::new(
"secp256k1_program",
solana_sdk::secp256k1_program::id(),
solana_secp256k1_program::process_instruction,
),
feature_set::secp256k1_program_enabled::id(),
ActivationType::NewProgram,
)]
vec![]
}

pub(crate) fn get() -> Builtins {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/non_circulating_supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ mod tests {
..GenesisConfig::default()
};
let mut bank = Arc::new(Bank::new(&genesis_config));
let sysvar_and_native_program_delta = 10;
let sysvar_and_native_program_delta = 11;
assert_eq!(
bank.capitalization(),
(num_genesis_accounts + num_non_circulating_accounts + num_stake_accounts) * balance
Expand Down
Loading

0 comments on commit 3a85b77

Please sign in to comment.