mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-08-11 07:23:16 +00:00
refactor program input
This commit is contained in:
parent
c5a4e83e3e
commit
d1ebb831ef
@ -7,17 +7,25 @@ pub type ProgramId = [u32; 8];
|
|||||||
pub type InstructionData = Vec<u32>;
|
pub type InstructionData = Vec<u32>;
|
||||||
pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8];
|
pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8];
|
||||||
|
|
||||||
|
pub struct ProgramInput<T> {
|
||||||
|
pub pre_states: Vec<AccountWithMetadata>,
|
||||||
|
pub instruction: T,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct ProgramOutput {
|
pub struct ProgramOutput {
|
||||||
pub pre_states: Vec<AccountWithMetadata>,
|
pub pre_states: Vec<AccountWithMetadata>,
|
||||||
pub post_states: Vec<Account>,
|
pub post_states: Vec<Account>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_nssa_inputs<T: DeserializeOwned>() -> (Vec<AccountWithMetadata>, T) {
|
pub fn read_nssa_inputs<T: DeserializeOwned>() -> ProgramInput<T> {
|
||||||
let pre_states: Vec<AccountWithMetadata> = env::read();
|
let pre_states: Vec<AccountWithMetadata> = env::read();
|
||||||
let words: InstructionData = env::read();
|
let words: InstructionData = env::read();
|
||||||
let instruction_data = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap();
|
let instruction = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap();
|
||||||
(pre_states, instruction_data)
|
ProgramInput {
|
||||||
|
pre_states,
|
||||||
|
instruction,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_nssa_outputs(pre_states: Vec<AccountWithMetadata>, post_states: Vec<Account>) {
|
pub fn write_nssa_outputs(pre_states: Vec<AccountWithMetadata>, post_states: Vec<Account>) {
|
||||||
|
|||||||
@ -1,16 +1,17 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = u128;
|
|
||||||
|
|
||||||
/// A transfer of balance program.
|
/// A transfer of balance program.
|
||||||
/// To be used both in public and private contexts.
|
/// To be used both in public and private contexts.
|
||||||
fn main() {
|
fn main() {
|
||||||
// Read input accounts.
|
// Read input accounts.
|
||||||
// It is expected to receive only two accounts: [sender_account, receiver_account]
|
// It is expected to receive only two accounts: [sender_account, receiver_account]
|
||||||
let (input_accounts, balance_to_move) = read_nssa_inputs::<Instruction>();
|
let ProgramInput {
|
||||||
|
pre_states,
|
||||||
|
instruction: balance_to_move,
|
||||||
|
} = read_nssa_inputs();
|
||||||
|
|
||||||
// Continue only if input_accounts is an array of two elements
|
// Continue only if input_accounts is an array of two elements
|
||||||
let [sender, receiver] = match input_accounts.try_into() {
|
let [sender, receiver] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = u128;
|
type Instruction = u128;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, balance_to_burn) = read_nssa_inputs::<Instruction>();
|
let ProgramInput {
|
||||||
|
pre_states,
|
||||||
|
instruction: balance_to_burn,
|
||||||
|
} = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
use nssa_core::{
|
use nssa_core::{
|
||||||
account::Account,
|
account::Account,
|
||||||
program::{read_nssa_inputs, write_nssa_outputs},
|
program::{read_nssa_inputs, write_nssa_outputs, ProgramInput},
|
||||||
};
|
};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre1, _] = match input_accounts.try_into() {
|
let [pre1, _] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = ();
|
type Instruction = ();
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
|
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [pre] = match input_accounts.try_into() {
|
let [pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
||||||
|
|
||||||
type Instruction = u128;
|
type Instruction = u128;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (input_accounts, balance) = read_nssa_inputs::<Instruction>();
|
let ProgramInput {
|
||||||
|
pre_states,
|
||||||
|
instruction: balance,
|
||||||
|
} = read_nssa_inputs::<Instruction>();
|
||||||
|
|
||||||
let [sender_pre, receiver_pre] = match input_accounts.try_into() {
|
let [sender_pre, receiver_pre] = match pre_states.try_into() {
|
||||||
Ok(array) => array,
|
Ok(array) => array,
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user