Expose tick_height to native programs

This commit is contained in:
Michael Vines
2018-11-13 18:18:40 -08:00
parent 0bf2ff6138
commit 5a8938209b
7 changed files with 18 additions and 15 deletions

View File

@ -137,7 +137,7 @@ fn deserialize_parameters(keyed_accounts: &mut [KeyedAccount], buffer: &[u8]) {
} }
solana_entrypoint!(entrypoint); solana_entrypoint!(entrypoint);
fn entrypoint(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8]) -> bool { fn entrypoint(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8], _tick_height: u64) -> bool {
static INIT: Once = ONCE_INIT; static INIT: Once = ONCE_INIT;
INIT.call_once(|| { INIT.call_once(|| {
// env_logger can only be initialized once // env_logger can only be initialized once

View File

@ -16,7 +16,7 @@ use std::sync::{Once, ONCE_INIT};
mod token_program; mod token_program;
solana_entrypoint!(entrypoint); solana_entrypoint!(entrypoint);
fn entrypoint(info: &mut [KeyedAccount], input: &[u8]) -> bool { fn entrypoint(info: &mut [KeyedAccount], input: &[u8], _tick_height: u64) -> bool {
// env_logger can only be initialized once // env_logger can only be initialized once
static INIT: Once = ONCE_INIT; static INIT: Once = ONCE_INIT;
INIT.call_once(env_logger::init); INIT.call_once(env_logger::init);

View File

@ -53,7 +53,7 @@ fn run_lua(keyed_accounts: &mut [KeyedAccount], code: &str, data: &[u8]) -> Resu
} }
solana_entrypoint!(entrypoint); solana_entrypoint!(entrypoint);
fn entrypoint(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8]) -> bool { fn entrypoint(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8], _tick_height: u64) -> bool {
static INIT: Once = ONCE_INIT; static INIT: Once = ONCE_INIT;
INIT.call_once(|| { INIT.call_once(|| {
// env_logger can only be initialized once // env_logger can only be initialized once
@ -174,11 +174,11 @@ mod tests {
(bob_pubkey, Account::new(1, 0, owner)), (bob_pubkey, Account::new(1, 0, owner)),
]; ];
let data = serialize(&10u64).unwrap(); let data = serialize(&10u64).unwrap();
process(&mut create_keyed_accounts(&mut accounts), &data); process(&mut create_keyed_accounts(&mut accounts), &data, 0);
assert_eq!(accounts[1].1.tokens, 90); assert_eq!(accounts[1].1.tokens, 90);
assert_eq!(accounts[2].1.tokens, 11); assert_eq!(accounts[2].1.tokens, 11);
process(&mut create_keyed_accounts(&mut accounts), &data); process(&mut create_keyed_accounts(&mut accounts), &data, 0);
assert_eq!(accounts[1].1.tokens, 80); assert_eq!(accounts[1].1.tokens, 80);
assert_eq!(accounts[2].1.tokens, 21); assert_eq!(accounts[2].1.tokens, 21);
} }
@ -222,7 +222,7 @@ mod tests {
(Pubkey::default(), Account::new(1, 0, owner)), (Pubkey::default(), Account::new(1, 0, owner)),
]; ];
let mut keyed_accounts = create_keyed_accounts(&mut accounts); let mut keyed_accounts = create_keyed_accounts(&mut accounts);
process(&mut keyed_accounts, &[]); process(&mut keyed_accounts, &[], 0);
// Verify deterministic ordering of a serialized Lua table. // Verify deterministic ordering of a serialized Lua table.
assert_eq!( assert_eq!(
str::from_utf8(&keyed_accounts[3].account.userdata).unwrap(), str::from_utf8(&keyed_accounts[3].account.userdata).unwrap(),
@ -281,19 +281,19 @@ mod tests {
).as_bytes() ).as_bytes()
.to_vec(); .to_vec();
process(&mut keyed_accounts, &data); process(&mut keyed_accounts, &data, 0);
assert_eq!(keyed_accounts[4].account.tokens, 1); assert_eq!(keyed_accounts[4].account.tokens, 1);
let data = format!(r#""{}""#, carol_pubkey).into_bytes(); let data = format!(r#""{}""#, carol_pubkey).into_bytes();
process(&mut keyed_accounts, &data); process(&mut keyed_accounts, &data, 0);
assert_eq!(keyed_accounts[4].account.tokens, 1); assert_eq!(keyed_accounts[4].account.tokens, 1);
let data = format!(r#""{}""#, dan_pubkey).into_bytes(); let data = format!(r#""{}""#, dan_pubkey).into_bytes();
process(&mut keyed_accounts, &data); process(&mut keyed_accounts, &data, 0);
assert_eq!(keyed_accounts[4].account.tokens, 101); // Pay day! assert_eq!(keyed_accounts[4].account.tokens, 101); // Pay day!
let data = format!(r#""{}""#, erin_pubkey).into_bytes(); let data = format!(r#""{}""#, erin_pubkey).into_bytes();
process(&mut keyed_accounts, &data); process(&mut keyed_accounts, &data, 0);
assert_eq!(keyed_accounts[4].account.tokens, 101); // No change! assert_eq!(keyed_accounts[4].account.tokens, 101); // No change!
} }
} }

View File

@ -4,8 +4,9 @@ extern crate solana_sdk;
use solana_sdk::account::KeyedAccount; use solana_sdk::account::KeyedAccount;
solana_entrypoint!(entrypoint); solana_entrypoint!(entrypoint);
fn entrypoint(keyed_accounts: &mut [KeyedAccount], data: &[u8]) -> bool { fn entrypoint(keyed_accounts: &mut [KeyedAccount], data: &[u8], tick_height: u64) -> bool {
println!("noop: keyed_accounts: {:#?}", keyed_accounts); println!("noop: keyed_accounts: {:#?}", keyed_accounts);
println!("noop: data: {:?}", data); println!("noop: data: {:?}", data);
println!("noop: tick_height: {:?}", tick_height);
true true
} }

View File

@ -5,7 +5,8 @@ pub const ENTRYPOINT: &str = "process";
// Native program ENTRYPOINT prototype // Native program ENTRYPOINT prototype
pub type Entrypoint = pub type Entrypoint =
unsafe extern "C" fn(keyed_accounts: &mut [KeyedAccount], data: &[u8]) -> bool; unsafe extern "C" fn(keyed_accounts: &mut [KeyedAccount], data: &[u8], tick_height: u64)
-> bool;
// Convenience macro to define the native program entrypoint. Supply a fn to this macro that // Convenience macro to define the native program entrypoint. Supply a fn to this macro that
// conforms to the `Entrypoint` type signature. // conforms to the `Entrypoint` type signature.
@ -13,8 +14,8 @@ pub type Entrypoint =
macro_rules! solana_entrypoint( macro_rules! solana_entrypoint(
($entrypoint:ident) => ( ($entrypoint:ident) => (
#[no_mangle] #[no_mangle]
pub extern "C" fn process(keyed_accounts: &mut [KeyedAccount], data: &[u8]) -> bool { pub extern "C" fn process(keyed_accounts: &mut [KeyedAccount], data: &[u8], tick_height: u64) -> bool {
return $entrypoint(keyed_accounts, data); return $entrypoint(keyed_accounts, data, tick_height);
} }
) )
); );

View File

@ -846,6 +846,7 @@ impl Bank {
if !native_loader::process_instruction( if !native_loader::process_instruction(
&mut keyed_accounts, &mut keyed_accounts,
&tx.instructions[instruction_index].userdata, &tx.instructions[instruction_index].userdata,
self.tick_height(),
) { ) {
return Err(BankError::ProgramRuntimeError(instruction_index as u8)); return Err(BankError::ProgramRuntimeError(instruction_index as u8));
} }

View File

@ -85,7 +85,7 @@ pub fn process_instruction(
return false; return false;
} }
}; };
return entrypoint(&mut keyed_accounts[1..], ix_userdata); return entrypoint(&mut keyed_accounts[1..], ix_userdata, tick_height);
}, },
Err(e) => { Err(e) => {
warn!("Unable to load: {:?}", e); warn!("Unable to load: {:?}", e);