Skip to content

Commit

Permalink
Separate key deduplication logic into standalone crate (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Nov 5, 2024
1 parent 6810892 commit fab7c1f
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 171 deletions.
19 changes: 18 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[workspace]
members = [
"bencher",
"error",
"fuzz/*",
"harness",
"keys",
"test-programs/*",
]
resolver = "2"
Expand All @@ -20,7 +22,9 @@ bincode = "1.3.3"
bs58 = "0.5.1"
mollusk-svm = { path = "harness", version = "0.0.6" }
mollusk-svm-bencher = { path = "bencher", version = "0.0.6" }
mollusk-svm-error = { path = "error", version = "0.0.6" }
mollusk-svm-fuzz-fixture = { path = "fuzz/fixture", version = "0.0.6" }
mollusk-svm-keys = { path = "keys", version = "0.0.6" }
num-format = "0.4.4"
prost = "0.10"
prost-build = "0.10"
Expand Down
14 changes: 14 additions & 0 deletions error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "mollusk-svm-error"
description = "Errors thrown by the Mollusk SVM harness."
documentation = "https://docs.rs/mollusk-svm-error"
authors = { workspace = true }
repository = { workspace = true }
readme = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
version = { workspace = true }

[dependencies]
solana-sdk = { workspace = true }
thiserror = { workspace = true }
File renamed without changes.
3 changes: 3 additions & 0 deletions error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Errors thrown by the Mollusk SVM harness.

pub mod error;
3 changes: 2 additions & 1 deletion harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ fuzz = ["dep:mollusk-svm-fuzz-fixture"]

[dependencies]
bincode = { workspace = true }
mollusk-svm-error = { workspace = true }
mollusk-svm-fuzz-fixture = { workspace = true, optional = true }
mollusk-svm-keys = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-compute-budget = { workspace = true }
solana-program-runtime = { workspace = true }
solana-system-program = { workspace = true }
solana-sdk = { workspace = true }
solana-logger = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
criterion = "0.5.1"
Expand Down
53 changes: 53 additions & 0 deletions harness/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Instruction <-> Transaction account compilation, with key deduplication,
//! privilege handling, and program account stubbing.

use {
mollusk_svm_keys::{
accounts::{
compile_instruction_accounts, compile_instruction_without_data,
compile_transaction_accounts_for_instruction,
},
keys::KeyMap,
},
solana_sdk::{
account::{AccountSharedData, WritableAccount},
instruction::Instruction,
pubkey::Pubkey,
transaction_context::{InstructionAccount, TransactionAccount},
},
};

pub struct CompiledAccounts {
pub program_id_index: u16,
pub instruction_accounts: Vec<InstructionAccount>,
pub transaction_accounts: Vec<TransactionAccount>,
}

pub fn compile_accounts(
instruction: &Instruction,
accounts: &[(Pubkey, AccountSharedData)],
loader_key: Pubkey,
) -> CompiledAccounts {
let stub_out_program_account = move || {
let mut program_account = AccountSharedData::default();
program_account.set_owner(loader_key);
program_account.set_executable(true);
program_account
};

let key_map = KeyMap::compile_from_instruction(instruction);
let compiled_instruction = compile_instruction_without_data(&key_map, instruction);
let instruction_accounts = compile_instruction_accounts(&key_map, &compiled_instruction);
let transaction_accounts = compile_transaction_accounts_for_instruction(
&key_map,
instruction,
accounts,
Some(Box::new(stub_out_program_account)),
);

CompiledAccounts {
program_id_index: compiled_instruction.program_id_index as u16,
instruction_accounts,
transaction_accounts,
}
}
2 changes: 1 addition & 1 deletion harness/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! error reading the file.

use {
crate::error::{MolluskError, MolluskPanic},
mollusk_svm_error::error::{MolluskError, MolluskPanic},
std::{
fs::File,
io::Read,
Expand Down
163 changes: 0 additions & 163 deletions harness/src/keys.rs

This file was deleted.

9 changes: 4 additions & 5 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@
//! * `process_and_validate_instruction`: Process an instruction and perform a
//! series of checks on the result, panicking if any checks fail.

mod error;
mod accounts;
pub mod file;
#[cfg(feature = "fuzz")]
pub mod fuzz;
mod keys;
pub mod program;
pub mod result;
pub mod sysvar;

use {
crate::{
error::{MolluskError, MolluskPanic},
program::ProgramCache,
result::{Check, InstructionResult},
sysvar::Sysvars,
},
keys::CompiledAccounts,
accounts::CompiledAccounts,
mollusk_svm_error::error::{MolluskError, MolluskPanic},
solana_compute_budget::compute_budget::ComputeBudget,
solana_program_runtime::{
invoke_context::{EnvironmentConfig, InvokeContext},
Expand Down Expand Up @@ -154,7 +153,7 @@ impl Mollusk {
program_id_index,
instruction_accounts,
transaction_accounts,
} = crate::keys::compile_accounts(instruction, accounts, loader_key);
} = crate::accounts::compile_accounts(instruction, accounts, loader_key);

let mut transaction_context = TransactionContext::new(
transaction_accounts,
Expand Down
14 changes: 14 additions & 0 deletions keys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "mollusk-svm-keys"
description = "SVM transaction keys utils."
documentation = "https://docs.rs/mollusk-svm-keys"
authors = { workspace = true }
repository = { workspace = true }
readme = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
version = { workspace = true }

[dependencies]
mollusk-svm-error = { workspace = true }
solana-sdk = { workspace = true }
Loading

0 comments on commit fab7c1f

Please sign in to comment.