Add tick height syscall (#4497)

* Remove tick_height from entrypoint signature

* Impl tick_height syscall and use in storage program

* Properly remove tick height from bpf handling
This commit is contained in:
Tyera Eulberg
2019-05-31 16:29:21 -06:00
committed by GitHub
parent ce04d2bfc2
commit 64e8a21d73
22 changed files with 153 additions and 107 deletions

View File

@ -199,7 +199,6 @@ typedef struct {
uint64_t ka_num; /** Number of SolKeyedAccount entries in `ka` */
const uint8_t *data; /** pointer to the instruction data */
uint64_t data_len; /** Length in bytes of the instruction data */
uint64_t tick_height; /** Current ledger tick */
const SolPubkey *program_id; /** program_id of the currently executing program */
} SolParameters;
@ -258,8 +257,6 @@ SOL_FN_PREFIX bool sol_deserialize(
params->data = input;
input += params->data_len;
params->tick_height = *(uint64_t *) input;
input += sizeof(uint64_t);
params->program_id = (SolPubkey *) input;
input += sizeof(SolPubkey);
@ -299,8 +296,6 @@ SOL_FN_PREFIX void sol_log_array(const uint8_t *array, int len) {
* @param params Pointer to a SolParameters structure
*/
SOL_FN_PREFIX void sol_log_params(const SolParameters *params) {
sol_log("- Tick height:");
sol_log_64(params->tick_height, 0, 0, 0, 0);
sol_log("- Program identifier:");
sol_log_key(params->program_id);

View File

@ -32,8 +32,6 @@ pub struct SolKeyedAccount<'a> {
/// Information about the state of the cluster immediately before the program
/// started executing the current instruction
pub struct SolClusterInfo<'a> {
/// Current ledger tick
pub tick_height: u64,
///program_id of the currently executing program
pub program_id: SolPubkey<'a>,
}
@ -142,12 +140,6 @@ pub unsafe fn deserialize<'a>(
let data = { from_raw_parts(input.add(offset), data_length) };
offset += data_length;
// Tick height
#[allow(clippy::cast_ptr_alignment)]
let tick_height = *(input.add(offset) as *const u64);
offset += size_of::<u64>();
// Id
let program_id = {
@ -157,7 +149,6 @@ pub unsafe fn deserialize<'a>(
};
let info = SolClusterInfo {
tick_height,
program_id,
};

View File

@ -11,7 +11,6 @@ pub type Entrypoint = unsafe extern "C" fn(
program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
tick_height: u64,
) -> Result<(), InstructionError>;
// Convenience macro to define the native program entrypoint. Supply a fn to this macro that
@ -24,9 +23,8 @@ macro_rules! solana_entrypoint(
program_id: &solana_sdk::pubkey::Pubkey,
keyed_accounts: &mut [solana_sdk::account::KeyedAccount],
data: &[u8],
tick_height: u64
) -> Result<(), solana_sdk::instruction::InstructionError> {
$entrypoint(program_id, keyed_accounts, data, tick_height)
$entrypoint(program_id, keyed_accounts, data)
}
)
);

View File

@ -4,6 +4,7 @@ use crate::pubkey::Pubkey;
pub mod fees;
pub mod slot_hashes;
pub mod tick_height;
/// "Sysca11111111111111111111111111111111111111"
/// owner pubkey for syscall accounts

View File

@ -0,0 +1,64 @@
//! This account contains the current cluster tick height
//!
use crate::account::Account;
use crate::account_utils::State;
use crate::pubkey::Pubkey;
use crate::syscall;
use bincode::serialized_size;
/// "Sysca11TickHeight11111111111111111111111111"
/// tick_height account pubkey
const ID: [u8; 32] = [
6, 167, 211, 138, 69, 219, 242, 63, 162, 206, 168, 232, 212, 90, 152, 107, 220, 251, 113, 215,
208, 229, 34, 163, 11, 168, 45, 109, 60, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&ID)
}
pub fn check_id(pubkey: &Pubkey) -> bool {
pubkey.as_ref() == ID
}
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct TickHeight(u64);
impl TickHeight {
pub fn from(account: &Account) -> Option<u64> {
account.state().ok().map(|res: Self| res.0)
}
pub fn to(tick_height: u64, account: &mut Account) -> Option<()> {
account.set_state(&TickHeight(tick_height)).ok()
}
pub fn size_of() -> usize {
serialized_size(&TickHeight::default()).unwrap() as usize
}
}
pub fn create_account(lamports: u64) -> Account {
Account::new(lamports, TickHeight::size_of(), &syscall::id())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tick_height_id() {
let name = "Sysca11TickHeight11111111111111111111111111";
// To get the bytes above:
// dbg!((name, bs58::decode(name).into_vec().unwrap()));
assert_eq!(name, id().to_string());
assert!(check_id(&id()));
}
#[test]
fn test_tick_height_create_account() {
let account = create_account(1);
let tick_height = TickHeight::from(&account).unwrap();
assert_eq!(tick_height, 0);
}
}