Cleanup unsupported sysvars (#16390)

* Cleanup unsupported sysvars

* fix ser description
This commit is contained in:
Jack May
2021-04-06 00:08:03 -07:00
committed by GitHub
parent 03d3ae1cb9
commit 92f4018b07
12 changed files with 90 additions and 39 deletions

View File

@ -211,6 +211,10 @@ pub enum InstructionError {
/// Program arithmetic overflowed
#[error("Program arithmetic overflowed")]
ArithmeticOverflow,
/// Unsupported sysvar
#[error("Unsupported sysvar")]
UnsupportedSysvar,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]

View File

@ -384,15 +384,15 @@ impl Message {
// [0..2 - num_instructions
//
// Then a table of offsets of where to find them in the data
// 3..2*num_instructions table of instruction offsets
// 3..2 * num_instructions table of instruction offsets
//
// Each instruction is then encoded as:
// 0..2 - num_accounts
// 3 - meta_byte -> (bit 0 signer, bit 1 is_writable)
// 4..36 - pubkey - 32 bytes
// 36..64 - program_id
// 33..34 - data len - u16
// 35..data_len - data
// 2 - meta_byte -> (bit 0 signer, bit 1 is_writable)
// 3..35 - pubkey - 32 bytes
// 35..67 - program_id
// 67..69 - data len - u16
// 69..data_len - data
pub fn serialize_instructions(&self, demote_sysvar_write_locks: bool) -> Vec<u8> {
// 64 bytes is a reasonable guess, calculating exactly is slower in benchmarks
let mut data = Vec::with_capacity(self.instructions.len() * (32 * 2));

View File

@ -43,6 +43,8 @@ pub enum ProgramError {
BorshIoError(String),
#[error("An account does not have enough lamports to be rent-exempt")]
AccountNotRentExempt,
#[error("Unsupported sysvar")]
UnsupportedSysvar,
}
pub trait PrintProgramError {
@ -79,6 +81,7 @@ impl PrintProgramError for ProgramError {
Self::InvalidSeeds => msg!("Error: InvalidSeeds"),
Self::BorshIoError(_) => msg!("Error: BorshIoError"),
Self::AccountNotRentExempt => msg!("Error: AccountNotRentExempt"),
Self::UnsupportedSysvar => msg!("Error: UnsupportedSysvar"),
}
}
}
@ -107,6 +110,7 @@ pub const MAX_SEED_LENGTH_EXCEEDED: u64 = to_builtin!(13);
pub const INVALID_SEEDS: u64 = to_builtin!(14);
pub const BORSH_IO_ERROR: u64 = to_builtin!(15);
pub const ACCOUNT_NOT_RENT_EXEMPT: u64 = to_builtin!(16);
pub const UNSUPPORTED_SYSVAR: u64 = to_builtin!(17);
impl From<ProgramError> for u64 {
fn from(error: ProgramError) -> Self {
@ -126,6 +130,7 @@ impl From<ProgramError> for u64 {
ProgramError::InvalidSeeds => INVALID_SEEDS,
ProgramError::BorshIoError(_) => BORSH_IO_ERROR,
ProgramError::AccountNotRentExempt => ACCOUNT_NOT_RENT_EXEMPT,
ProgramError::UnsupportedSysvar => UNSUPPORTED_SYSVAR,
ProgramError::Custom(error) => {
if error == 0 {
@ -154,6 +159,7 @@ impl From<u64> for ProgramError {
ACCOUNT_BORROW_FAILED => ProgramError::AccountBorrowFailed,
MAX_SEED_LENGTH_EXCEEDED => ProgramError::MaxSeedLengthExceeded,
INVALID_SEEDS => ProgramError::InvalidSeeds,
UNSUPPORTED_SYSVAR => ProgramError::UnsupportedSysvar,
CUSTOM_ZERO => ProgramError::Custom(0),
_ => ProgramError::Custom(error as u32),
}
@ -180,6 +186,7 @@ impl TryFrom<InstructionError> for ProgramError {
Self::Error::MaxSeedLengthExceeded => Ok(Self::MaxSeedLengthExceeded),
Self::Error::BorshIoError(err) => Ok(Self::BorshIoError(err)),
Self::Error::AccountNotRentExempt => Ok(Self::AccountNotRentExempt),
Self::Error::UnsupportedSysvar => Ok(Self::UnsupportedSysvar),
_ => Err(error),
}
}
@ -206,6 +213,7 @@ where
ACCOUNT_BORROW_FAILED => InstructionError::AccountBorrowFailed,
MAX_SEED_LENGTH_EXCEEDED => InstructionError::MaxSeedLengthExceeded,
INVALID_SEEDS => InstructionError::InvalidSeeds,
UNSUPPORTED_SYSVAR => InstructionError::UnsupportedSysvar,
_ => {
// A valid custom error has no bits set in the upper 32
if error >> BUILTIN_BIT_SHIFT == 0 {

View File

@ -1,14 +1,14 @@
#![allow(clippy::integer_arithmetic)]
//! This account contains the serialized transaction instructions
use crate::{instruction::Instruction, sanitize::SanitizeError, sysvar::Sysvar};
use crate::{instruction::Instruction, sanitize::SanitizeError};
pub type Instructions = Vec<Instruction>;
// Instructions Sysvar, dummy type, use the associated helpers instead of the Sysvar trait
pub struct Instructions();
crate::declare_sysvar_id!("Sysvar1nstructions1111111111111111111111111", Instructions);
impl Sysvar for Instructions {}
/// Load the current instruction's index from the Instructions Sysvar data
pub fn load_current_index(data: &[u8]) -> u16 {
let mut instr_fixed_data = [0u8; 2];
let len = data.len();
@ -16,11 +16,13 @@ pub fn load_current_index(data: &[u8]) -> u16 {
u16::from_le_bytes(instr_fixed_data)
}
/// Store the current instruction's index in the Instructions Sysvar data
pub fn store_current_index(data: &mut [u8], instruction_index: u16) {
let last_index = data.len() - 2;
data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
}
/// Load an instruction at the specified index
pub fn load_instruction_at(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
crate::message::Message::deserialize_instruction(index, data)
}

View File

@ -4,7 +4,7 @@
//!
pub use crate::slot_hashes::SlotHashes;
use crate::sysvar::Sysvar;
use crate::{account_info::AccountInfo, program_error::ProgramError, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarS1otHashes111111111111111111111111111", SlotHashes);
@ -14,6 +14,10 @@ impl Sysvar for SlotHashes {
// hard-coded so that we don't have to construct an empty
20_488 // golden, update if MAX_ENTRIES changes
}
fn from_account_info(_account_info: &AccountInfo) -> Result<Self, ProgramError> {
// This sysvar is too large to bincode::deserialize in-program
Err(ProgramError::UnsupportedSysvar)
}
}
#[cfg(test)]

View File

@ -1,9 +1,11 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries a bitvector of slots present over the past
//! epoch
//! epoch
//!
pub use crate::slot_history::SlotHistory;
pub use crate::{
account_info::AccountInfo, program_error::ProgramError, slot_history::SlotHistory,
};
use crate::sysvar::Sysvar;
@ -15,6 +17,10 @@ impl Sysvar for SlotHistory {
// hard-coded so that we don't have to construct an empty
131_097 // golden, update if MAX_ENTRIES changes
}
fn from_account_info(_account_info: &AccountInfo) -> Result<Self, ProgramError> {
// This sysvar is too large to bincode::deserialize in-program
Err(ProgramError::UnsupportedSysvar)
}
}
#[cfg(test)]