Skip to content

Commit

Permalink
Renames, delete redundant assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jac0xb committed Jan 7, 2024
1 parent e6fdd6d commit cdfad84
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 331 deletions.
18 changes: 9 additions & 9 deletions programs/lighthouse/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ declare_id!("L1TEVtgA75k273wWz1s6XMmDhQY5i3MwcvKb4VbZzfK");
pub mod lighthouse {
use super::*;

pub fn create_cache_account_v1<'info>(
ctx: Context<'_, '_, '_, 'info, CreateCacheAccountV1<'info>>,
cache_index: u8,
cache_account_size: u64,
pub fn create_memory_account_v1<'info>(
ctx: Context<'_, '_, '_, 'info, CreateMemoryAccountV1<'info>>,
memory_index: u8,
memory_account_size: u64,
) -> Result<()> {
processor::v1::create_cache_account(ctx, cache_index, cache_account_size)
processor::v1::create_memory_account(ctx, memory_index, memory_account_size)
}

pub fn write_v1<'info>(
ctx: Context<'_, '_, '_, 'info, WriteV1<'info>>,
cache_index: u8,
memory_index: u8,
write_type: WriteTypeParameter,
) -> Result<()> {
processor::v1::write(ctx, cache_index, write_type)
processor::v1::write(ctx, memory_index, write_type)
}

pub fn assert_v1<'info>(
ctx: Context<'_, '_, '_, 'info, AssertV1<'info>>,
assertion: Assertion,
config: Option<AssertionConfig>,
config: Option<AssertionConfigV1>,
) -> Result<()> {
processor::v1::assert(&ctx.accounts.target_account, &assertion, config)
}
Expand All @@ -52,7 +52,7 @@ pub mod lighthouse {
pub fn assert_multi_v1<'info>(
ctx: Context<'_, '_, '_, 'info, AssertMultiV1<'info>>,
assertions: Vec<Assertion>,
config: Option<AssertionConfig>,
config: Option<AssertionConfigV1>,
) -> Result<()> {
processor::v1::assert_multi(ctx.remaining_accounts, assertions.as_slice(), config)
}
Expand Down
4 changes: 2 additions & 2 deletions programs/lighthouse/program/src/processor/v1/assert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::LighthouseError,
structs::{Assertion, AssertionConfig},
structs::{Assertion, AssertionConfigV1},
utils::print_assertion_result,
};
use anchor_lang::prelude::*;
Expand All @@ -18,7 +18,7 @@ pub struct AssertCompactV1<'info> {
pub fn assert(
target_account: &AccountInfo<'_>,
assertion: &Assertion,
config: Option<AssertionConfig>,
config: Option<AssertionConfigV1>,
) -> Result<()> {
let include_output = match &config {
Some(config) => config.verbose,
Expand Down
14 changes: 4 additions & 10 deletions programs/lighthouse/program/src/processor/v1/assert_expression.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::structs::{Assertion, AssertionConfig, AssertionState, Expression};
use crate::structs::{Assertion, AssertionConfigV1, AssertionState, Expression};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct AssertExpressionV1<'info> {
system_program: Program<'info, System>,
}

//
// WIP
//
pub fn assert_expression<'info>(
ctx: Context<'_, '_, '_, 'info, AssertExpressionV1<'info>>,
assertions: Vec<Assertion>,
logical_expression: Vec<Expression>,
config: Option<AssertionConfig>,
config: Option<AssertionConfigV1>,
) -> Result<()> {
let remaining_accounts = ctx.remaining_accounts;
let mut assertion_state = AssertionState::new(assertions.clone(), logical_expression)?;
Expand All @@ -27,11 +29,3 @@ pub fn assert_expression<'info>(

Ok(())
}

pub fn truncate_pubkey(pubkey: &Pubkey) -> String {
let mut pubkey_str = pubkey.to_string();
pubkey_str.truncate(5);
pubkey_str.push_str("...");

pubkey_str
}
4 changes: 2 additions & 2 deletions programs/lighthouse/program/src/processor/v1/assert_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

use crate::{
error::LighthouseError,
structs::{Assertion, AssertionConfig},
structs::{Assertion, AssertionConfigV1},
utils::print_assertion_result,
};

Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct AssertMultiV1<'info> {
pub fn assert_multi(
remaining_accounts: &[AccountInfo<'_>],
assertions: &[Assertion],
config: Option<AssertionConfig>,
config: Option<AssertionConfigV1>,
) -> Result<()> {
let include_output = match &config {
Some(config) => config.verbose,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use anchor_lang::prelude::*;
use borsh::BorshDeserialize;

use crate::state::memory::MemoryAccount;

#[derive(Accounts)]
#[instruction(memory_index: u8, memory_account_size: u64)]
pub struct CreateMemoryAccountV1<'info> {
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
#[account(
init,
seeds=[
b"memory".as_ref(),
signer.key.as_ref(),
&[memory_index],
],
bump,
payer=signer,
space= 8 + memory_account_size as usize
)]
pub memory_account: AccountLoader<'info, MemoryAccount>,
pub rent: Sysvar<'info, Rent>,
}

pub fn create_memory_account<'info>(
ctx: Context<'_, '_, '_, 'info, CreateMemoryAccountV1<'info>>,
_memory_index: u8,
_memory_account_size: u64,

) -> Result<()> {
let _ = &mut ctx.accounts.memory_account.load_init()?;

Ok(())
}
4 changes: 2 additions & 2 deletions programs/lighthouse/program/src/processor/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod assert;
pub mod assert_expression;
pub mod assert_multi;
pub mod create_cache_account;
pub mod create_memory_account;
pub mod write;

pub use assert::*;
pub use assert_expression::*;
pub use assert_multi::*;
pub use create_cache_account::*;
pub use create_memory_account::*;
pub use write::*;
48 changes: 24 additions & 24 deletions programs/lighthouse/program/src/processor/v1/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use crate::error::LighthouseError;
use crate::structs::{AccountInfoData, WriteType, WriteTypeParameter};

#[derive(Accounts)]
#[instruction(cache_index: u8)]
#[instruction(memory_index: u8)]
pub struct WriteV1<'info> {
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
#[account(
mut,
seeds=[
b"cache".as_ref(),
b"memory".as_ref(),
signer.key.as_ref(),
&[cache_index],
&[memory_index],
],
bump
)]
pub cache_account: UncheckedAccount<'info>,
pub memory_account: UncheckedAccount<'info>,
}

pub fn write<'info>(
Expand All @@ -33,31 +33,31 @@ pub fn write<'info>(
return Err(LighthouseError::UnauthorizedIxEntry.into());
}

let cache_ref = &mut ctx.accounts.cache_account.try_borrow_mut_data()?;
let cache_data_length = cache_ref.len();
let memory_ref = &mut ctx.accounts.memory_account.try_borrow_mut_data()?;
let memory_data_length = memory_ref.len();

let (mut cache_offset, write_type) = match write_type {
WriteTypeParameter::WriteU8(cache_offset, write_type) => {
(cache_offset as usize, write_type)
let (mut memory_offset, write_type) = match write_type {
WriteTypeParameter::WriteU8(memory_offset, write_type) => {
(memory_offset as usize, write_type)
}
WriteTypeParameter::WriteU16(cache_offset, write_type) => {
(cache_offset as usize, write_type)
WriteTypeParameter::WriteU16(memory_offset, write_type) => {
(memory_offset as usize, write_type)
}
WriteTypeParameter::WriteU32(cache_offset, write_type) => {
(cache_offset as usize, write_type)
WriteTypeParameter::WriteU32(memory_offset, write_type) => {
(memory_offset as usize, write_type)
}
};

cache_offset = cache_offset.checked_add(8).ok_or_else(|| {
msg!("Cache offset overflowed");
memory_offset = memory_offset.checked_add(8).ok_or_else(|| {
msg!("Memory offset overflowed");
LighthouseError::OutOfRange
})?;

let data_length = write_type
.size(ctx.remaining_accounts.first())
.ok_or(LighthouseError::InvalidDataLength)?;
if cache_data_length < (cache_offset + data_length) {
msg!("Cache offset overflowed");
if memory_data_length < (memory_offset + data_length) {
msg!("Memory offset overflowed");
return Err(LighthouseError::OutOfRange.into());
}

Expand All @@ -67,18 +67,18 @@ pub fn write<'info>(
}
WriteType::DataValue(borsh_value) => {
let data_slice = &(borsh_value.serialize())[0..data_length];
cache_ref[cache_offset..(cache_offset + data_length)]
memory_ref[memory_offset..(memory_offset + data_length)]
.copy_from_slice(data_slice.as_ref());
}
WriteType::AccountBalance => {
let source_account = ctx.remaining_accounts.first();

if let Some(target_account) = source_account {
if (cache_offset + data_length) < cache_data_length {
if (memory_offset + data_length) < memory_data_length {
let data = target_account.lamports();
let data_slice = &data.to_le_bytes();

cache_ref[cache_offset..(cache_offset + data_length)]
memory_ref[memory_offset..(memory_offset + data_length)]
.copy_from_slice(data_slice.as_ref());
} else {
return Err(LighthouseError::NotEnoughAccounts.into());
Expand Down Expand Up @@ -128,14 +128,14 @@ pub fn write<'info>(
}
}

if (cache_offset + data_length) < cache_data_length {
if (memory_offset + data_length) < memory_data_length {
let data = target_account.try_borrow_data().map_err(|err| {
msg!("Error: {:?}", err);
LighthouseError::AccountBorrowFailed
})?;
let data_slice = &data[account_offset..(account_offset + data_length)];

cache_ref[cache_offset..(cache_offset + data_length)]
memory_ref[memory_offset..(memory_offset + data_length)]
.copy_from_slice(data_slice.as_ref());
} else {
return Err(LighthouseError::NotEnoughAccounts.into());
Expand All @@ -148,7 +148,7 @@ pub fn write<'info>(
let target_account = ctx.remaining_accounts.first();

if let Some(target_account) = target_account {
if (cache_offset + data_length) < cache_data_length {
if (memory_offset + data_length) < memory_data_length {
let account_info = AccountInfoData {
key: *target_account.key,
is_signer: target_account.is_signer,
Expand All @@ -163,7 +163,7 @@ pub fn write<'info>(
let data = account_info.try_to_vec()?; // TODO: map this unwrap error
let data_slice = &data[0..data_length];

cache_ref[cache_offset..(cache_offset + data_length)]
memory_ref[memory_offset..(memory_offset + data_length)]
.copy_from_slice(data_slice.as_ref());
} else {
return Err(LighthouseError::NotEnoughAccounts.into());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::prelude::*;

#[account(zero_copy)]
pub struct CacheAccount {}
pub struct MemoryAccount {}
4 changes: 2 additions & 2 deletions programs/lighthouse/program/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod cache;
pub mod memory;

pub use cache::*;
pub use memory::*;
21 changes: 1 addition & 20 deletions programs/lighthouse/program/src/structs/assert/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
use anchor_spl::token::{self, spl_token::state::Account};

#[derive(BorshDeserialize, BorshSerialize, Debug)]
pub struct AssertionConfig {
pub struct AssertionConfigV1 {
pub verbose: bool,
}

Expand Down Expand Up @@ -54,11 +54,6 @@ pub enum Assertion {
// account data offset, borsh type, operator
AccountData(u16, Operator, DataValue),

// balance, operator
AccountBalance(u64, Operator),

AccountOwnedBy(Pubkey, Operator),

// token balance, operator
LegacyTokenAccountField(LegacyTokenAccountDataField, Operator),
}
Expand All @@ -69,12 +64,6 @@ impl Assertion {
Assertion::AccountData(offset, operator, value) => {
format!("AccountData[{}|{:?}|{:?}]", offset, operator, value)
}
Assertion::AccountBalance(balance, operator) => {
format!("AccountBalance[{}|{:?}]", balance, operator)
}
Assertion::AccountOwnedBy(pubkey, operator) => {
format!("AccountOwnedBy[{}|{:?}]", pubkey, operator)
}
Assertion::LegacyTokenAccountField(field, operator) => {
format!("LegacyTokenAccountField[{:?}|{:?}]", field, operator)
}
Expand All @@ -90,9 +79,6 @@ impl Assertion {
include_output: bool,
) -> Result<Box<EvaluationResult>> {
match &self {
Assertion::AccountOwnedBy(pubkey, operator) => {
Ok(operator.evaluate(&target_account.owner, &pubkey, include_output))
}
Assertion::AccountData(account_offset, operator, memory_value) => {
let account_data = target_account.try_borrow_data()?;

Expand All @@ -103,11 +89,6 @@ impl Assertion {
include_output,
)?)
}
Assertion::AccountBalance(balance_value, operator) => Ok(operator.evaluate(
&**target_account.try_borrow_lamports()?,
balance_value,
include_output,
)),
Assertion::LegacyTokenAccountField(token_account_field, operator) => {
if target_account.data_is_empty() {
return Err(LighthouseError::AccountNotInitialized.into());
Expand Down
Loading

0 comments on commit cdfad84

Please sign in to comment.