GithubHelp home page GithubHelp logo

mollusk's Introduction

Mollusk

SVM program test harness.

Harness

The harness is designed to directly invoke the loaded executable program using the BPF Loader, bypassing any transaction sanitization and runtime checks, and instead directly processing the instruction with the BPF Loader.

let program_id = Pubkey::new_unique();
let key1 = Pubkey::new_unique();
let key2 = Pubkey::new_unique();

let instruction = Instruction::new_with_bytes(
    program_id,
    &[],
    vec![
        AccountMeta::new(key1, false),
        AccountMeta::new_readonly(key2, false),
    ],
);

let accounts = vec![
    (key1, AccountSharedData::new(10_000, 0, &system_program::id())),
    (key2, AccountSharedData::new(10_000, 0, &system_program::id())),
];

let mollusk = Mollusk::new(program_id, "my_program");

let result = mollusk.process_instruction(&instruction, &accounts);

You can also use the Checks API provided by Mollusk for easy post-execution checks, rather than writing them manually. The API method process_and_validate_instruction will still return the result, allowing you to perform further checks if you desire.

Note: Mollusk::default() will use the System program as the program to invoke.

let sender = Pubkey::new_unique();
let recipient = Pubkey::new_unique();

let base_lamports = 100_000_000u64;
let transfer_amount = 42_000u64;

let instruction = system_instruction::transfer(&sender, &recipient, transfer_amount);
let accounts = [
    (
        sender,
        AccountSharedData::new(base_lamports, 0, &system_program::id()),
    ),
    (
        recipient,
        AccountSharedData::new(base_lamports, 0, &system_program::id()),
    ),
];
let checks = vec![
    Check::success(),
    Check::compute_units(system_processor::DEFAULT_COMPUTE_UNITS),
    Check::account(&sender)
        .lamports(base_lamports - transfer_amount)
        .build(),
    Check::account(&recipient)
        .lamports(base_lamports + transfer_amount)
        .build(),
];

Mollusk::default().process_and_validate_instruction(&instruction, &accounts, &checks);

Bencher

Mollusk also offers a compute unit usage bencher for profiling a program's compute unit usage.

Example:

// If using with `cargo bench`, tell Mollusk where to find the program.
std::env::set_var("SBF_OUT_DIR", "../target/deploy");

// Optionally disable logging.
solana_logger::setup_with("");

/* Instruction & accounts setup ... */

let mollusk = Mollusk::new(&program_id, "my_program");

MolluskComputeUnitBencher::new(mollusk)
    .bench(("bench0", &instruction0, &accounts0))
    .bench(("bench1", &instruction1, &accounts1))
    .bench(("bench2", &instruction2, &accounts2))
    .bench(("bench3", &instruction3, &accounts3))
    .must_pass(true)
    .out_dir("../target/benches")
    .execute();

You can invoke this benchmark test with cargo bench. Don't forget to add a bench to your project's Cargo.toml.

[[bench]]
name = "compute_units"
harness = false

Mollusk will output bench details to the output directory in Markdown.

Note: Delta is the change since the last time the bench was run.

Name CUs Delta
bench0 450 --
bench1 579 -129
bench2 1,204 +754
bench3 2,811 +2,361

mollusk's People

Contributors

buffalojoec avatar cavemanloverboy avatar waitfreemaxi avatar

Stargazers

Marche avatar Datt Goswami avatar Kevin Rodríguez avatar Will Kennedy avatar Zhe avatar 0xStone avatar xiaodao avatar  avatar jess avatar Mike Hale avatar Jimii avatar Burbo avatar Arihant Bansal avatar Yihau Chen avatar Ademola avatar Bryan Zierk avatar Andrew Fitzgerald avatar Sammy Harris avatar Abhishek Faliya avatar Kulture avatar _ravecat_ avatar Beta avatar Danny Povolotski avatar Kristian Quirapas avatar Samuel Vanderwaal avatar Frances He avatar  avatar  avatar Raghav Bansal avatar Nagaprasad V R avatar Kyle Espinola avatar  avatar John Rezk avatar Rohan Suri avatar aditya yadav avatar  avatar Brooks avatar hisu87 avatar Brandt Cormorant avatar  avatar

Watchers

Danny Povolotski avatar  avatar Kristian Quirapas avatar

mollusk's Issues

`accounts: Vec<_>`

Why is this a vector?

mollusk/harness/src/lib.rs

Lines 158 to 162 in e861087

pub fn process_instruction(
&self,
instruction: &Instruction,
accounts: Vec<(Pubkey, AccountSharedData)>,
) -> InstructionResult {

Should make this a slice so that devs don't have to clone (and so you don't have to clone internally on each loop iteration).

Support CPI to other BPF programs

Because of this little hack, CPI only works to builtin programs right now.

const PROGRAM_ACCOUNTS_LEN: usize = 1;
const PROGRAM_INDICES: &[u16] = &[0];

We'll need to add some logic to extract program accounts, similar to:

https://github.com/anza-xyz/agave/blob/2443048c5cd926581fdc2a26a25006120d16477c/svm/src/transaction_processor.rs#L492

Note this would also involve adding the target BPF program to the cache.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.