Add bool return to entrypoint signature to permit programs to fail transactions

This commit is contained in:
Michael Vines
2018-10-13 19:31:16 -07:00
parent 9fc30f6db4
commit d3b4dfe104
6 changed files with 30 additions and 22 deletions

View File

@ -5,16 +5,18 @@ use bincode::deserialize;
use solana_program_interface::account::KeyedAccount;
#[no_mangle]
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, data: &[u8]) {
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, data: &[u8]) -> bool {
let tokens: i64 = deserialize(data).unwrap();
if infos[0].account.tokens >= tokens {
infos[0].account.tokens -= tokens;
infos[1].account.tokens += tokens;
true
} else {
println!(
"Insufficient funds, asked {}, only had {}",
tokens, infos[0].account.tokens
);
false
}
}

View File

@ -3,7 +3,8 @@ extern crate solana_program_interface;
use solana_program_interface::account::KeyedAccount;
#[no_mangle]
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, data: &[u8]) {
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, data: &[u8]) -> bool {
println!("noop: AccountInfos: {:#?}", infos);
println!("noop: data: {:#?}", data);
true
}

View File

@ -45,10 +45,11 @@ fn run_lua(keyed_accounts: &mut Vec<KeyedAccount>, code: &str, data: &[u8]) -> R
}
#[no_mangle]
pub extern "C" fn process(keyed_accounts: &mut Vec<KeyedAccount>, data: &[u8]) {
pub extern "C" fn process(keyed_accounts: &mut Vec<KeyedAccount>, data: &[u8]) -> bool {
let code_data = keyed_accounts[0].account.userdata.clone();
let code = str::from_utf8(&code_data).unwrap();
run_lua(keyed_accounts, &code, data).unwrap();
true
}
#[cfg(test)]