Skip to content

Commit

Permalink
add default_cost as mandatory field for Builtin (solana-labs#30639)
Browse files Browse the repository at this point in the history
* add default_cost as mandatory field for Builtin

* updated tests

* set zkp program default to VerifyTransfer CUs

---------

Co-authored-by: Jon Cinque <[email protected]>
  • Loading branch information
2 people authored and nickfrosty committed Mar 12, 2023
1 parent 95196af commit 1bca025
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 112 deletions.
2 changes: 1 addition & 1 deletion ledger/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use solana_runtime::builtins::{Builtin, BuiltinFeatureTransition, Builtins};

macro_rules! to_builtin {
($b:expr) => {
Builtin::new(&$b.0, $b.1, $b.2)
Builtin::new(&$b.0, $b.1, $b.2, $b.3)
};
}

Expand Down
11 changes: 10 additions & 1 deletion program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub type ProcessInstructionWithContext = fn(&mut InvokeContext) -> Result<(), In
pub struct BuiltinProgram {
pub program_id: Pubkey,
pub process_instruction: ProcessInstructionWithContext,
pub default_compute_unit_cost: u64,
}

impl std::fmt::Debug for BuiltinProgram {
Expand All @@ -55,7 +56,11 @@ impl std::fmt::Debug for BuiltinProgram {
// https://github.com/rust-lang/rust/issues/50280
// https://users.rust-lang.org/t/display-function-pointer/17073/2
let erased_instruction: ErasedProcessInstructionWithContext = self.process_instruction;
write!(f, "{}: {:p}", self.program_id, erased_instruction)
write!(
f,
"{}: {:p} CUs: {}",
self.program_id, erased_instruction, self.default_compute_unit_cost
)
}
}

Expand Down Expand Up @@ -1073,10 +1078,12 @@ mod tests {
BuiltinProgram {
program_id: solana_sdk::pubkey::new_rand(),
process_instruction: mock_process_instruction,
default_compute_unit_cost: 0,
},
BuiltinProgram {
program_id: solana_sdk::pubkey::new_rand(),
process_instruction: mock_ix_processor,
default_compute_unit_cost: 0,
},
];
assert!(!format!("{builtin_programs:?}").is_empty());
Expand Down Expand Up @@ -1262,6 +1269,7 @@ mod tests {
let builtin_programs = &[BuiltinProgram {
program_id: callee_program_id,
process_instruction: mock_process_instruction,
default_compute_unit_cost: 0,
}];

let owned_account = AccountSharedData::new(42, 1, &callee_program_id);
Expand Down Expand Up @@ -1420,6 +1428,7 @@ mod tests {
let builtin_programs = [BuiltinProgram {
program_id: program_key,
process_instruction: mock_process_instruction,
default_compute_unit_cost: 0,
}];

let mut transaction_context =
Expand Down
13 changes: 9 additions & 4 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl ProgramTest {
let add_native = |this: &mut ProgramTest, process_fn: ProcessInstructionWithContext| {
info!("\"{}\" program loaded as native code", program_name);
this.builtins
.push(Builtin::new(program_name, program_id, process_fn));
.push(Builtin::new(program_name, program_id, process_fn, 0));
};

let warn_invalid_program_name = || {
Expand Down Expand Up @@ -695,8 +695,12 @@ impl ProgramTest {
process_instruction: ProcessInstructionWithContext,
) {
info!("\"{}\" builtin program", program_name);
self.builtins
.push(Builtin::new(program_name, program_id, process_instruction));
self.builtins.push(Builtin::new(
program_name,
program_id,
process_instruction,
0,
));
}

/// Deactivate a runtime feature.
Expand Down Expand Up @@ -789,7 +793,7 @@ impl ProgramTest {
// Add loaders
macro_rules! add_builtin {
($b:expr) => {
bank.add_builtin(&$b.0, &$b.1, $b.2)
bank.add_builtin(&$b.0, &$b.1, $b.2, $b.3)
};
}
add_builtin!(solana_bpf_loader_deprecated_program!());
Expand All @@ -812,6 +816,7 @@ impl ProgramTest {
&builtin.name,
&builtin.id,
builtin.process_instruction_with_context,
0,
);
}

Expand Down
1 change: 1 addition & 0 deletions programs/bpf_loader/src/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ solana_sdk::declare_builtin!(
solana_sdk::bpf_loader_deprecated::ID,
solana_bpf_loader_deprecated_program,
solana_bpf_loader_program::process_instruction,
1140,
deprecated::id
);
3 changes: 2 additions & 1 deletion programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ use {
solana_sdk::declare_builtin!(
solana_sdk::bpf_loader::ID,
solana_bpf_loader_program,
solana_bpf_loader_program::process_instruction
solana_bpf_loader_program::process_instruction,
570
);

/// Errors returned by functions the BPF Loader registers with the VM
Expand Down
1 change: 1 addition & 0 deletions programs/bpf_loader/src/upgradeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ solana_sdk::declare_builtin!(
solana_sdk::bpf_loader_upgradeable::ID,
solana_bpf_loader_upgradeable_program,
solana_bpf_loader_program::process_instruction,
2370,
upgradeable::id
);
1 change: 1 addition & 0 deletions programs/bpf_loader/src/upgradeable_with_jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ solana_sdk::declare_builtin!(
solana_sdk::bpf_loader_upgradeable::ID,
solana_bpf_loader_upgradeable_program_with_jit,
solana_bpf_loader_program::process_instruction_jit,
2370,
upgradeable_with_jit::id
);
3 changes: 2 additions & 1 deletion programs/bpf_loader/src/with_jit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
solana_sdk::declare_builtin!(
solana_sdk::bpf_loader::ID,
solana_bpf_loader_program_with_jit,
solana_bpf_loader_program::process_instruction_jit
solana_bpf_loader_program::process_instruction_jit,
570
);
4 changes: 2 additions & 2 deletions programs/sbf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
..
} = create_genesis_config(50);
let mut bank = Bank::new_for_benches(&genesis_config);
let (name, id, entrypoint) = solana_bpf_loader_program!();
bank.add_builtin(&name, &id, entrypoint);
let (name, id, entrypoint, cost) = solana_bpf_loader_program!();
bank.add_builtin(&name, &id, entrypoint, cost);
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);

Expand Down
Loading

0 comments on commit 1bca025

Please sign in to comment.