Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing the Voting Program

Pre-alpha. The devnet pre-alpha is a mock network with no MPC. Read the warning in the introduction first.

What You’ll Learn

  • How to write Mollusk instruction-level tests for dWallet programs
  • How to build and verify account data at specific byte offsets
  • Testing error conditions with exact error codes (non-member, wrong council, wrong dWallet, double vote, closed proposal)
  • Testing the CPI path — and the confused-deputy attack — with LiteSVM against the real dWallet program

Test Matrix (Mollusk)

TestInstructionExpected Result
test_create_council_successCreateCouncilPDA created at the canonical address with dwallet, quorum, members
test_create_council_rejects_non_ownerCreateCouncilCustom(2) NotDWalletOwner
test_create_council_rejects_dwallet_not_owned_by_dwallet_programCreateCouncilCustom(4) InvalidDWalletAccount
test_create_council_rejects_quorum_above_member_countCreateCouncilCustom(5) InvalidQuorum
test_create_council_rejects_duplicate_memberCreateCouncilCustom(6) DuplicateMember
test_create_council_rejects_non_canonical_addressCreateCouncilCustom(3) InvalidCouncil
test_create_proposal_successCreateProposalPDA created; quorum copied from the council
test_create_proposal_rejects_non_memberCreateProposalCustom(1) NotAMember
test_create_proposal_rejects_council_of_another_dwalletCreateProposalCustom(3) InvalidCouncil
test_create_proposal_rejects_forged_council_accountCreateProposalCustom(3) InvalidCouncil
test_create_proposal_already_existsCreateProposalFails (account in use)
test_cast_vote_yes_success / _no_successCastVotecounters incremented, status=Open
test_cast_vote_rejects_non_memberCastVoteCustom(1) NotAMember
test_cast_vote_rejects_council_of_another_dwalletCastVoteCustom(3) InvalidCouncil
test_cast_vote_at_quorum_rejects_wrong_dwalletCastVoteCustom(0) DWalletMismatch, before any CPI
test_cast_vote_double_vote_failsCastVoteFails (VoteRecord exists)
test_cast_vote_closed_proposal_failsCastVoteCustom(7) ProposalClosed

The Anchor variant reports the same conditions as Anchor errors (6003 NotAMember, 6004 DWalletMismatch, 6005 NotDWalletOwner, …; a council at the wrong PDA fails Anchor’s ConstraintSeeds, 2006).

Test Matrix (LiteSVM)

TestExpected Result
test_voting_full_lifecyclecouncil → transfer → proposal → 3 votes → SignatureVoucher created with the right fields
test_voting_below_quorum_no_voucherno SignatureVoucher before quorum
test_no_votes_dont_trigger_quorum“no” votes never issue a voucher
test_outsider_cannot_vote_or_recreate_counciloutsider vote / proposal NotAMember; council (re)creation NotDWalletOwner
test_quorum_cannot_be_redirected_to_another_councils_dwalletattacker’s own council reaching quorum with the victim’s dWallet as CPI target fails DWalletMismatch; no voucher for the victim
test_create_proposal_refuses_a_non_canonical_voucher_bumpa proposal naming a voucher bump other than the canonical one is refused
test_quorum_vote_refuses_a_substituted_voucher_accountthe quorum vote refuses a voucher account other than the proposal’s PDA
test_a_member_cannot_vote_twice_with_a_second_bumpa second vote with another valid vote-record bump fails InvalidBump (11); no second record, the count stays 1
test_create_proposal_refuses_a_non_canonical_proposal_bumpa proposal at a non-canonical bump fails InvalidBump; nothing is written there
test_dwallet_context_refuses_a_program_that_is_not_ikaat quorum, a program other than the ika program in the dWallet-program slot fails IncorrectProgramId; no voucher

Three tests guard attacks that would otherwise work:

  • The redirect test: with the dWallet check removed from the program, the attacker’s transaction succeeds and issues a voucher on the victim’s dWallet.
  • The second-bump test: with the canonical-bump check removed, the member’s second vote succeeds and counts twice.
  • The ika-program test: with DWalletContext’s program check removed, the call goes to whatever program the caller named.

Each of them was run against the program with its check removed, and failed there.

The Pinocchio and native variants run all ten. The Anchor variant runs five: the full lifecycle, below quorum, outsider, redirect and ika-program tests. It needs no bump tests, because Anchor derives every canonical bump itself.

Running Tests

# from the repository root
# SBF builds the tests load (the ika program itself is shipped in bin/)
cargo build-sbf --tools-version v1.54 --arch v3 --manifest-path examples/voting/pinocchio/Cargo.toml
cargo build-sbf --tools-version v1.54 --arch v3 --manifest-path examples/voting/native/Cargo.toml
cargo build-sbf --tools-version v1.54 --arch v3 --manifest-path examples/voting/anchor/Cargo.toml

# Mollusk + LiteSVM
cargo test -p ika-example-voting-pinocchio
cargo test -p ika-example-voting-native
cargo test -p ika-example-voting-anchor

Key Patterns

Building Test Account Data

Tests pre-populate account data with exact byte layouts:

#![allow(unused)]
fn main() {
fn build_council_data(dwallet: &Pubkey, quorum: u32, members: &[Pubkey], bump: u8) -> Vec<u8> {
    let mut data = vec![0u8; COUNCIL_LEN]; // 361 bytes
    data[0] = COUNCIL_DISCRIMINATOR;       // 3
    data[1] = 1;                           // version
    data[CN_DWALLET..CN_DWALLET + 32].copy_from_slice(dwallet.as_ref());
    data[CN_QUORUM..CN_QUORUM + 4].copy_from_slice(&quorum.to_le_bytes());
    // ... member_count, bump, members
    data
}
}

A fixture() helper builds a dWallet address, a stand-in dWallet program, an owner and a 3-member council around them so each test states only what it changes.

Asserting Exact Error Codes

#![allow(unused)]
fn main() {
fn assert_custom_error(result: &InstructionResult, code: u32) {
    match &result.program_result {
        ProgramResult::Failure(err) => assert_eq!(*err, ProgramError::Custom(code)),
        other => panic!("expected Custom({code}) failure, got {other:?}"),
    }
}
}

assert!(result.program_result.is_err()) is not enough for a negative test: the instruction may fail for an unrelated reason (a fake token program, a wrong bump) while the check you meant to test no longer fires.

Testing Double Vote Prevention

The VoteRecord PDA prevents double voting. If the PDA already exists, CreateAccount fails:

#![allow(unused)]
fn main() {
// Pre-populate VoteRecord (voter already voted)
let existing_vr = build_vote_record_data(&voter, &proposal_id, 1, vr_bump);

let result = mollusk.process_instruction(&ix, &[
    (proposal_pda, program_account(&program_id, proposal_data)),
    (vote_record_pda, program_account(&program_id, existing_vr)), // exists!
    // ...
]);
assert!(matches!(result.program_result, ProgramResult::Failure(_)));
}

Source

  • Pinocchio tests: examples/voting/pinocchio/tests/{mollusk,litesvm}.rs
  • Native tests: examples/voting/native/tests/{mollusk,litesvm}.rs
  • Anchor tests: examples/voting/anchor/tests/{mollusk,litesvm}.rs