Skip to content

Commit

Permalink
Refactor instruction processor to accept SVMInstruction (#2603)
Browse files Browse the repository at this point in the history
* refactor to make process_compute_budget_instructions accept SVMInstruction
  • Loading branch information
tao-stones authored Aug 15, 2024
1 parent 993d223 commit d4df4f8
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 57 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ solana-send-transaction-service = { workspace = true }
solana-short-vec = { workspace = true }
solana-streamer = { workspace = true }
solana-svm = { workspace = true }
solana-svm-transaction = { workspace = true }
solana-timings = { workspace = true }
solana-tpu-client = { workspace = true }
solana-transaction-status = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use {
transaction_processing_result::TransactionProcessingResultExtensions,
transaction_processor::{ExecutionRecordingConfig, TransactionProcessingConfig},
},
solana_svm_transaction::svm_message::SVMMessage,
solana_timings::ExecuteTimings,
std::{
sync::{atomic::Ordering, Arc},
Expand Down Expand Up @@ -555,9 +556,9 @@ impl Consumer {
.sanitized_transactions()
.iter()
.filter_map(|transaction| {
process_compute_budget_instructions(
transaction.message().program_instructions_iter(),
)
process_compute_budget_instructions(SVMMessage::program_instructions_iter(
transaction,
))
.ok()
.map(|limits| limits.compute_unit_price)
})
Expand Down Expand Up @@ -751,7 +752,7 @@ impl Consumer {
) -> Result<(), TransactionError> {
let fee_payer = message.fee_payer();
let fee_budget_limits = FeeBudgetLimits::from(process_compute_budget_instructions(
message.program_instructions_iter(),
SVMMessage::program_instructions_iter(message),
)?);
let fee = solana_fee::calculate_fee(
message,
Expand Down
4 changes: 3 additions & 1 deletion core/src/banking_stage/immutable_deserialized_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
},
},
solana_short_vec::decode_shortu16_len,
solana_svm_transaction::instruction::SVMInstruction,
std::{cmp::Ordering, collections::HashSet, mem::size_of},
thiserror::Error,
};
Expand Down Expand Up @@ -64,7 +65,8 @@ impl ImmutableDeserializedPacket {
} = process_compute_budget_instructions(
sanitized_transaction
.get_message()
.program_instructions_iter(),
.program_instructions_iter()
.map(|(pubkey, ix)| (pubkey, SVMInstruction::from(ix))),
)
.map_err(|_| DeserializedPacketError::PrioritizationFailure)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use {
transaction::SanitizedTransaction,
},
solana_svm::transaction_error_metrics::TransactionErrorMetrics,
solana_svm_transaction::svm_message::SVMMessage,
std::{
sync::{Arc, RwLock},
time::{Duration, Instant},
Expand Down Expand Up @@ -534,7 +535,7 @@ impl SchedulerController {
.is_ok()
})
.filter_map(|(packet, tx)| {
process_compute_budget_instructions(tx.message().program_instructions_iter())
process_compute_budget_instructions(SVMMessage::program_instructions_iter(&tx))
.map(|compute_budget| (packet, tx, compute_budget.into()))
.ok()
})
Expand Down
1 change: 1 addition & 0 deletions cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ solana-frozen-abi-macro = { workspace = true, optional = true }
solana-metrics = { workspace = true }
solana-runtime-transaction = { workspace = true }
solana-sdk = { workspace = true }
solana-svm-transaction = { workspace = true }
solana-vote-program = { workspace = true }

[lib]
Expand Down
10 changes: 5 additions & 5 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use {
system_program,
transaction::SanitizedTransaction,
},
solana_svm_transaction::svm_message::SVMMessage,
};

pub struct CostModel;
Expand Down Expand Up @@ -150,7 +151,7 @@ impl CostModel {

fn get_transaction_cost(
tx_cost: &mut UsageCostDetails,
transaction: &SanitizedTransaction,
transaction: &impl SVMMessage,
feature_set: &FeatureSet,
) {
let mut programs_execution_costs = 0u64;
Expand All @@ -159,7 +160,7 @@ impl CostModel {
let mut compute_unit_limit_is_set = false;
let mut has_user_space_instructions = false;

for (program_id, instruction) in transaction.message().program_instructions_iter() {
for (program_id, instruction) in transaction.program_instructions_iter() {
let ix_execution_cost =
if let Some(builtin_cost) = BUILTIN_INSTRUCTION_COSTS.get(program_id) {
*builtin_cost
Expand All @@ -177,7 +178,7 @@ impl CostModel {

if compute_budget::check_id(program_id) {
if let Ok(ComputeBudgetInstruction::SetComputeUnitLimit(_)) =
try_from_slice_unchecked(&instruction.data)
try_from_slice_unchecked(instruction.data)
{
compute_unit_limit_is_set = true;
}
Expand All @@ -186,8 +187,7 @@ impl CostModel {

// if failed to process compute_budget instructions, the transaction will not be executed
// by `bank`, therefore it should be considered as no execution cost by cost model.
match process_compute_budget_instructions(transaction.message().program_instructions_iter())
{
match process_compute_budget_instructions(transaction.program_instructions_iter()) {
Ok(compute_budget_limits) => {
// if tx contained user-space instructions and a more accurate estimate available correct it,
// where "user-space instructions" must be specifically checked by
Expand Down
5 changes: 5 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions programs/sbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ solana-sbf-rust-realloc-invoke-dep = { path = "rust/realloc_invoke_dep", version
solana-sdk = { path = "../../sdk", version = "=2.1.0" }
solana-secp256k1-recover = { path = "../../curves/secp256k1-recover", version = "=2.1.0" }
solana-svm = { path = "../../svm", version = "=2.1.0" }
solana-svm-transaction = { path = "../../svm-transaction", version = "=2.1.0" }
solana-timings = { path = "../../timings", version = "=2.1.0" }
solana-transaction-status = { path = "../../transaction-status", version = "=2.1.0" }
solana-type-overrides = { path = "../../type-overrides", version = "=2.1.0" }
Expand Down Expand Up @@ -117,6 +118,7 @@ solana-sbf-rust-realloc-dep = { workspace = true }
solana-sbf-rust-realloc-invoke-dep = { workspace = true }
solana-sdk = { workspace = true, features = ["dev-context-only-utils"] }
solana-svm = { workspace = true }
solana-svm-transaction = { workspace = true }
solana-timings = { workspace = true }
solana-transaction-status = { workspace = true }
solana-type-overrides = { workspace = true }
Expand Down
13 changes: 9 additions & 4 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use {
transaction_execution_result::InnerInstruction,
transaction_processor::ExecutionRecordingConfig,
},
solana_svm_transaction::svm_message::SVMMessage,
solana_timings::ExecuteTimings,
solana_transaction_status::{
map_inner_instructions, ConfirmedTransactionWithStatusMeta, TransactionStatusMeta,
Expand Down Expand Up @@ -3874,8 +3875,10 @@ fn test_program_fees() {
)
.unwrap();
let fee_budget_limits = FeeBudgetLimits::from(
process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default(),
process_compute_budget_instructions(SVMMessage::program_instructions_iter(
&sanitized_message,
))
.unwrap_or_default(),
);
let expected_normal_fee = solana_fee::calculate_fee(
&sanitized_message,
Expand Down Expand Up @@ -3904,8 +3907,10 @@ fn test_program_fees() {
)
.unwrap();
let fee_budget_limits = FeeBudgetLimits::from(
process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default(),
process_compute_budget_instructions(SVMMessage::program_instructions_iter(
&sanitized_message,
))
.unwrap_or_default(),
);
let expected_prioritized_fee = solana_fee::calculate_fee(
&sanitized_message,
Expand Down
1 change: 1 addition & 0 deletions runtime-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = { workspace = true }
log = { workspace = true }
solana-compute-budget = { workspace = true }
solana-sdk = { workspace = true }
solana-svm-transaction = { workspace = true }
thiserror = { workspace = true }

[lib]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
system_instruction::{self},
transaction::{SanitizedTransaction, Transaction},
},
solana_svm_transaction::svm_message::SVMMessage,
};

const NUM_TRANSACTIONS_PER_ITER: usize = 1024;
Expand All @@ -34,7 +35,7 @@ fn bench_process_compute_budget_instructions_empty(c: &mut Criterion) {
bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
tx.message().program_instructions_iter()
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
Expand Down Expand Up @@ -62,7 +63,7 @@ fn bench_process_compute_budget_instructions_no_builtins(c: &mut Criterion) {
bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
tx.message().program_instructions_iter()
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
Expand All @@ -85,7 +86,7 @@ fn bench_process_compute_budget_instructions_compute_budgets(c: &mut Criterion)
bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
tx.message().program_instructions_iter()
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
Expand All @@ -111,7 +112,7 @@ fn bench_process_compute_budget_instructions_builtins(c: &mut Criterion) {
bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
tx.message().program_instructions_iter()
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
Expand Down Expand Up @@ -148,7 +149,7 @@ fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) {
bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
tx.message().program_instructions_iter()
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
Expand Down
Loading

0 comments on commit d4df4f8

Please sign in to comment.