feat: add caller_program_id to ProgramInput

This commit is contained in:
moudyellaz 2026-04-02 15:35:02 +02:00 committed by Moudy
parent d105a51c04
commit 087baebcca
31 changed files with 60 additions and 163 deletions

View File

@ -18,8 +18,7 @@ type Instruction = Vec<u8>;
fn main() {
// Read inputs
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: greeting,
},
@ -51,11 +50,5 @@ fn main() {
// with the NSSA program rules.
// WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be
// called to commit the output.
ProgramOutput::new(
self_program_id,
instruction_data,
vec![pre_state],
vec![post_state],
)
.write();
ProgramOutput::new(self_program_id, instruction_data, vec![pre_state], vec![post_state]).write();
}

View File

@ -18,8 +18,7 @@ type Instruction = Vec<u8>;
fn main() {
// Read inputs
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: greeting,
},
@ -58,11 +57,5 @@ fn main() {
// with the NSSA program rules.
// WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be
// called to commit the output.
ProgramOutput::new(
self_program_id,
instruction_data,
vec![pre_state],
vec![post_state],
)
.write();
ProgramOutput::new(self_program_id, instruction_data, vec![pre_state], vec![post_state]).write();
}

View File

@ -65,8 +65,7 @@ fn move_data(from_pre: AccountWithMetadata, to_pre: AccountWithMetadata) -> Vec<
fn main() {
// Read input accounts.
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (function_id, data),
},

View File

@ -26,8 +26,7 @@ fn hello_world_program_id() -> ProgramId {
fn main() {
// Read inputs
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (),
},
@ -56,12 +55,7 @@ fn main() {
// Write the outputs.
// WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be
// called to commit the output.
ProgramOutput::new(
self_program_id,
instruction_data,
vec![pre_state],
vec![post_state],
)
.with_chained_calls(vec![chained_call])
.write();
ProgramOutput::new(self_program_id, instruction_data, vec![pre_state], vec![post_state])
.with_chained_calls(vec![chained_call])
.write();
}

View File

@ -32,8 +32,7 @@ fn hello_world_program_id() -> ProgramId {
fn main() {
// Read inputs
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (),
},
@ -69,12 +68,7 @@ fn main() {
// Write the outputs.
// WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be
// called to commit the output.
ProgramOutput::new(
self_program_id,
instruction_data,
vec![pre_state],
vec![post_state],
)
.with_chained_calls(vec![chained_call])
.write();
ProgramOutput::new(self_program_id, instruction_data, vec![pre_state], vec![post_state])
.with_chained_calls(vec![chained_call])
.write();
}

View File

@ -17,6 +17,7 @@ pub type ProgramId = [u32; 8];
pub type InstructionData = Vec<u32>;
pub struct ProgramInput<T> {
pub self_program_id: ProgramId,
pub caller_program_id: Option<ProgramId>,
pub pre_states: Vec<AccountWithMetadata>,
pub instruction: T,
}
@ -421,12 +422,14 @@ pub fn compute_authorized_pdas(
#[must_use]
pub fn read_nssa_inputs<T: DeserializeOwned>() -> (ProgramInput<T>, InstructionData) {
let self_program_id: ProgramId = env::read();
let caller_program_id: Option<ProgramId> = env::read();
let pre_states: Vec<AccountWithMetadata> = env::read();
let instruction_words: InstructionData = env::read();
let instruction = T::deserialize(&mut Deserializer::new(instruction_words.as_ref())).unwrap();
(
ProgramInput {
self_program_id,
caller_program_id,
pre_states,
instruction,
},

View File

@ -87,15 +87,16 @@ pub fn execute_and_prove(
pda_seeds: vec![],
};
let mut chained_calls = VecDeque::from_iter([(initial_call, initial_program)]);
let mut chained_calls = VecDeque::from_iter([(initial_call, initial_program, None)]);
let mut chain_calls_counter = 0;
while let Some((chained_call, program)) = chained_calls.pop_front() {
while let Some((chained_call, program, caller_program_id)) = chained_calls.pop_front() {
if chain_calls_counter >= MAX_NUMBER_CHAINED_CALLS {
return Err(NssaError::MaxChainedCallsDepthExceeded);
}
let inner_receipt = execute_and_prove_program(
program,
caller_program_id,
&chained_call.pre_states,
&chained_call.instruction_data,
)?;
@ -115,7 +116,7 @@ pub fn execute_and_prove(
let next_program = dependencies
.get(&new_call.program_id)
.ok_or(NssaError::InvalidProgramBehavior)?;
chained_calls.push_front((new_call, next_program));
chained_calls.push_front((new_call, next_program, Some(chained_call.program_id)));
}
chain_calls_counter = chain_calls_counter
@ -153,12 +154,13 @@ pub fn execute_and_prove(
fn execute_and_prove_program(
program: &Program,
caller_program_id: Option<ProgramId>,
pre_states: &[AccountWithMetadata],
instruction_data: &InstructionData,
) -> Result<Receipt, NssaError> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
Program::write_inputs(program.id(), pre_states, instruction_data, &mut env_builder)?;
Program::write_inputs(program.id(), caller_program_id, pre_states, instruction_data, &mut env_builder)?;
let env = env_builder.build().unwrap();
// Prove the program

View File

@ -52,13 +52,14 @@ impl Program {
pub(crate) fn execute(
&self,
caller_program_id: Option<ProgramId>,
pre_states: &[AccountWithMetadata],
instruction_data: &InstructionData,
) -> Result<ProgramOutput, NssaError> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
env_builder.session_limit(Some(MAX_NUM_CYCLES_PUBLIC_EXECUTION));
Self::write_inputs(self.id, pre_states, instruction_data, &mut env_builder)?;
Self::write_inputs(self.id, caller_program_id, pre_states, instruction_data, &mut env_builder)?;
let env = env_builder.build().unwrap();
// Execute the program (without proving)
@ -79,6 +80,7 @@ impl Program {
/// Writes inputs to `env_builder` in the order expected by the programs.
pub(crate) fn write_inputs(
program_id: ProgramId,
caller_program_id: Option<ProgramId>,
pre_states: &[AccountWithMetadata],
instruction_data: &[u32],
env_builder: &mut ExecutorEnvBuilder,
@ -86,6 +88,9 @@ impl Program {
env_builder
.write(&program_id)
.map_err(|e| NssaError::ProgramWriteInputFailed(e.to_string()))?;
env_builder
.write(&caller_program_id)
.map_err(|e| NssaError::ProgramWriteInputFailed(e.to_string()))?;
let pre_states = pre_states.to_vec();
env_builder
.write(&pre_states)

View File

@ -148,7 +148,7 @@ impl PublicTransaction {
chained_call.program_id, chained_call.pre_states, chained_call.instruction_data
);
let mut program_output =
program.execute(&chained_call.pre_states, &chained_call.instruction_data)?;
program.execute(caller_program_id, &chained_call.pre_states, &chained_call.instruction_data)?;
debug!(
"Program {:?} output: {:?}",
chained_call.program_id, program_output

View File

@ -13,8 +13,7 @@ use nssa_core::program::{ProgramInput, ProgramOutput, read_nssa_inputs};
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction,
},
@ -153,12 +152,7 @@ fn main() {
}
};
ProgramOutput::new(
self_program_id,
instruction_words,
pre_states_clone,
post_states,
)
.with_chained_calls(chained_calls)
.write();
ProgramOutput::new(self_program_id, instruction_words, pre_states_clone, post_states)
.with_chained_calls(chained_calls)
.write();
}

View File

@ -3,8 +3,7 @@ use nssa_core::program::{ProgramInput, ProgramOutput, read_nssa_inputs};
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction,
},
@ -57,12 +56,7 @@ fn main() {
}
};
ProgramOutput::new(
self_program_id,
instruction_words,
pre_states_clone,
post_states,
)
.with_chained_calls(chained_calls)
.write();
ProgramOutput::new(self_program_id, instruction_words, pre_states_clone, post_states)
.with_chained_calls(chained_calls)
.write();
}

View File

@ -66,8 +66,7 @@ fn transfer(
fn main() {
// Read input accounts.
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: balance_to_move,
},

View File

@ -45,8 +45,7 @@ fn main() {
// Read input accounts.
// It is expected to receive only two accounts: [pinata_account, winner_account]
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: solution,
},

View File

@ -51,8 +51,7 @@ fn main() {
// It is expected to receive three accounts: [pinata_definition, pinata_token_holding,
// winner_token_holding]
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: solution,
},

View File

@ -11,8 +11,7 @@ use token_program::core::Instruction;
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction,
},
@ -82,11 +81,5 @@ fn main() {
}
};
ProgramOutput::new(
self_program_id,
instruction_words,
pre_states_clone,
post_states,
)
.write();
ProgramOutput::new(self_program_id, instruction_words, pre_states_clone, post_states).write();
}

View File

@ -4,8 +4,7 @@ type Instruction = u128;
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: balance_to_burn,
},

View File

@ -12,8 +12,7 @@ type Instruction = (u128, ProgramId, u32, Option<PdaSeed>);
/// program.
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (balance, auth_transfer_id, num_chain_calls, pda_seed),
},

View File

@ -5,8 +5,7 @@ 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,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (data_opt, should_claim),
},
@ -34,11 +33,5 @@ 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();
}

View File

@ -4,8 +4,7 @@ type Instruction = ();
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (),
},
@ -18,11 +17,5 @@ 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();
}

View File

@ -5,8 +5,7 @@ type Instruction = Vec<u8>;
/// A program that modifies the account data by setting bytes sent in instruction.
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: data,
},

View File

@ -6,14 +6,7 @@ use nssa_core::{
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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;

View File

@ -13,8 +13,7 @@ type Instruction = (u128, ProgramId);
/// but sets the `is_authorized` field of the first account to true.
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (balance, transfer_program_id),
},

View File

@ -3,14 +3,7 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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;

View File

@ -3,14 +3,7 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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;

View File

@ -63,8 +63,7 @@ fn transfer(
fn main() {
// Read input accounts.
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: balance_to_move,
},

View File

@ -3,14 +3,7 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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;

View File

@ -3,14 +3,7 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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()

View File

@ -3,14 +3,7 @@ use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nss
type Instruction = ();
fn main() {
let (
ProgramInput {
self_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;

View File

@ -4,8 +4,7 @@ type Instruction = u128;
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: balance,
},

View File

@ -7,8 +7,7 @@ type Instruction = (BlockValidityWindow, TimestampValidityWindow);
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (block_validity_window, timestamp_validity_window),
},

View File

@ -15,8 +15,7 @@ type Instruction = (BlockValidityWindow, ProgramId, BlockValidityWindow);
fn main() {
let (
ProgramInput {
self_program_id,
ProgramInput { self_program_id, caller_program_id: _,
pre_states,
instruction: (block_validity_window, chained_program_id, chained_block_validity_window),
},