fix: apply formatting and rebuild artifacts

This commit is contained in:
Moudy
2026-04-03 01:17:42 +02:00
parent 88e3b368c3
commit 74e16db68f
54 changed files with 319 additions and 91 deletions
+3 -1
View File
@@ -4,7 +4,9 @@ type Instruction = u128;
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: balance_to_burn,
},
@@ -12,7 +12,9 @@ type Instruction = (u128, ProgramId, u32, Option<PdaSeed>);
/// program.
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (balance, auth_transfer_id, num_chain_calls, pda_seed),
},
@@ -5,7 +5,9 @@ type Instruction = (Option<Vec<u8>>, bool);
/// A program that optionally modifies the account data and optionally claims it.
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (data_opt, should_claim),
},
@@ -33,5 +35,11 @@ fn main() {
AccountPostState::new(account_post)
};
ProgramOutput::new(self_program_id, instruction_words, vec![pre], vec![post_state]).write();
ProgramOutput::new(
self_program_id,
instruction_words,
vec![pre],
vec![post_state],
)
.write();
}
+10 -2
View File
@@ -4,7 +4,9 @@ type Instruction = ();
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (),
},
@@ -17,5 +19,11 @@ fn main() {
let account_post = AccountPostState::new_claimed(pre.account.clone(), Claim::Authorized);
ProgramOutput::new(self_program_id, instruction_words, vec![pre], vec![account_post]).write();
ProgramOutput::new(
self_program_id,
instruction_words,
vec![pre],
vec![account_post],
)
.write();
}
@@ -5,7 +5,9 @@ type Instruction = Vec<u8>;
/// A program that modifies the account data by setting bytes sent in instruction.
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: data,
},
@@ -6,7 +6,15 @@ use nssa_core::{
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let Ok([pre]) = <[_; 1]>::try_from(pre_states) else {
return;
@@ -10,12 +10,12 @@
//! In a real flash swap, this would contain the user's arbitrage or other logic.
//! In this test program, it is controlled by `return_funds`:
//!
//! - `return_funds = true`: emits a token transfer (receiver → vault) to return the funds.
//! The invariant check will pass and the transaction will succeed.
//! - `return_funds = true`: emits a token transfer (receiver → vault) to return the funds. The
//! invariant check will pass and the transaction will succeed.
//!
//! - `return_funds = false`: emits no transfers. Funds stay with the receiver.
//! The invariant check will fail (vault balance < initial), causing full atomic rollback.
//! This simulates a malicious or buggy callback that does not repay the flash loan.
//! - `return_funds = false`: emits no transfers. Funds stay with the receiver. The invariant check
//! will fail (vault balance < initial), causing full atomic rollback. This simulates a malicious
//! or buggy callback that does not repay the flash loan.
//!
//! # Note on caller_program_id
//!
@@ -16,18 +16,18 @@
//! 2. User callback (arbitrary logic, e.g. arbitrage)
//! 3. Self-call to `InvariantCheck` (using `self_program_id` to reference itself)
//!
//! - `InvariantCheck` (internal): enforces that the vault balance was restored after
//! the callback. Uses `caller_program_id == Some(self_program_id)` to prevent standalone
//! calls (this is the visibility enforcement mechanism).
//! - `InvariantCheck` (internal): enforces that the vault balance was restored after the callback.
//! Uses `caller_program_id == Some(self_program_id)` to prevent standalone calls (this is the
//! visibility enforcement mechanism).
//!
//! # What this demonstrates
//!
//! - `self_program_id`: enables a program to chain back to itself (step 3 above)
//! - `caller_program_id`: enables a program to restrict which callers can invoke an instruction
//! - Pre-simulated intermediate states: the initiator must compute expected intermediate
//! account states and embed them in the instruction. The node validates them deterministically.
//! - Atomic rollback: if the callback doesn't return funds, the invariant check fails,
//! and all state changes from steps 1 and 2 are rolled back automatically.
//! - Pre-simulated intermediate states: the initiator must compute expected intermediate account
//! states and embed them in the instruction. The node validates them deterministically.
//! - Atomic rollback: if the callback doesn't return funds, the invariant check fails, and all
//! state changes from steps 1 and 2 are rolled back automatically.
//!
//! # Tests
//!
@@ -132,8 +132,9 @@ fn main() {
// Chained call 3: Self-call to enforce the invariant.
// Uses `self_program_id` to reference this program, the key feature that enables
// the "prep → callback → assert" pattern without a separate checker program.
// If the callback did not return funds, vault_after_callback.balance < min_vault_balance
// and this call will panic, rolling back the entire transaction.
// If the callback did not return funds, vault_after_callback.balance <
// min_vault_balance and this call will panic, rolling back the entire
// transaction.
let invariant_instruction =
risc0_zkvm::serde::to_vec(&FlashSwapInstruction::InvariantCheck {
min_vault_balance,
@@ -13,7 +13,9 @@ type Instruction = (u128, ProgramId);
/// but sets the `is_authorized` field of the first account to true.
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (balance, transfer_program_id),
},
+9 -1
View File
@@ -3,7 +3,15 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let Ok([pre]) = <[_; 1]>::try_from(pre_states) else {
return;
@@ -3,7 +3,15 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let Ok([pre1, pre2]) = <[_; 2]>::try_from(pre_states) else {
return;
@@ -63,7 +63,9 @@ fn transfer(
fn main() {
// Read input accounts.
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: balance_to_move,
},
@@ -3,7 +3,15 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let Ok([pre]) = <[_; 1]>::try_from(pre_states) else {
return;
+9 -1
View File
@@ -3,7 +3,15 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let post_states = pre_states
.iter()
@@ -3,7 +3,15 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (ProgramInput { self_program_id, caller_program_id: _, pre_states, .. }, instruction_words) = read_nssa_inputs::<Instruction>();
let (
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
..
},
instruction_words,
) = read_nssa_inputs::<Instruction>();
let Ok([pre]) = <[_; 1]>::try_from(pre_states) else {
return;
@@ -4,7 +4,9 @@ type Instruction = u128;
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: balance,
},
@@ -7,7 +7,9 @@ type Instruction = (BlockValidityWindow, TimestampValidityWindow);
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (block_validity_window, timestamp_validity_window),
},
@@ -15,7 +15,9 @@ type Instruction = (BlockValidityWindow, ProgramId, BlockValidityWindow);
fn main() {
let (
ProgramInput { self_program_id, caller_program_id: _,
ProgramInput {
self_program_id,
caller_program_id: _,
pre_states,
instruction: (block_validity_window, chained_program_id, chained_block_validity_window),
},