Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 1 addition & 41 deletions program/c/src/oracle/oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,46 +59,6 @@ static bool valid_writable_account( SolParameters *prm,
is_rent_exempt( *ka->lamports, ka->data_len );
}

static uint64_t add_mapping( SolParameters *prm, SolAccountInfo *ka )
{
// Account (1) is the tail or last mapping account in the chain
// Account (2) is the new mapping account and will become the new tail
// Verify that these are signed, writable accounts with correct ownership
// and size
if ( prm->ka_num != 3 ||
!valid_funding_account( &ka[0] ) ||
!valid_signable_account( prm, &ka[1], sizeof( pc_map_table_t ) ) ||
!valid_signable_account( prm, &ka[2], sizeof( pc_map_table_t ) ) ) {
return ERROR_INVALID_ARGUMENT;
}
// Verify that last mapping account in chain is initialized, full
// and not pointing to a another account in the chain
// Also verify that the new account is uninitialized
cmd_hdr_t *hdr = (cmd_hdr_t*)prm->data;
pc_map_table_t *pptr = (pc_map_table_t*)ka[1].data;
pc_map_table_t *nptr = (pc_map_table_t*)ka[2].data;
if ( pptr->magic_ != PC_MAGIC ||
pptr->ver_ != hdr->ver_ ||
pptr->type_ != PC_ACCTYPE_MAPPING ||
nptr->magic_ != 0 ||
pptr->num_ < PC_MAP_TABLE_SIZE ||
nptr->num_ != 0 ||
!pc_pub_key_is_zero( &pptr->next_ ) ) {
return ERROR_INVALID_ARGUMENT;
}
// Initialize new account and set version number
sol_memset( nptr, 0, sizeof( pc_map_table_t ) );
nptr->magic_ = PC_MAGIC;
nptr->ver_ = hdr->ver_;
nptr->type_ = PC_ACCTYPE_MAPPING;
nptr->size_ = sizeof( pc_map_table_t ) - sizeof( nptr->prod_ );

// Set last mapping account to point to this mapping account
pc_pub_key_t *nkey = (pc_pub_key_t*)ka[2].key;
pc_pub_key_assign( &pptr->next_, nkey );
return SUCCESS;
}

static uint64_t add_product( SolParameters *prm, SolAccountInfo *ka )
{
// Account (1) is the mapping account that we're going to add to and
Expand Down Expand Up @@ -513,7 +473,7 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
case e_cmd_upd_price_no_fail_on_error: return upd_price_no_fail_on_error( prm, ka );
// init_mapping is overridden in Rust, but still implemented here to make the C unit tests pass.
case e_cmd_init_mapping: return ERROR_INVALID_ARGUMENT;
case e_cmd_add_mapping: return add_mapping( prm, ka );
case e_cmd_add_mapping: return ERROR_INVALID_ARGUMENT;
case e_cmd_add_product: return add_product( prm, ka );
case e_cmd_upd_product: return upd_product( prm, ka );
case e_cmd_add_price: return add_price( prm, ka );
Expand Down
75 changes: 0 additions & 75 deletions program/c/src/oracle/test_oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,6 @@ uint64_t MAPPING_ACCOUNT_LAMPORTS = 143821440;
uint64_t PRODUCT_ACCOUNT_LAMPORTS = 4454400;
uint64_t PRICE_ACCOUNT_LAMPORTS = 23942400;

Test(oracle, add_mapping ) {
// start with perfect inputs
cmd_hdr_t idata = {
.ver_ = PC_VERSION,
.cmd_ = e_cmd_add_mapping
};
pc_map_table_t tptr[1];
sol_memset( tptr, 0, sizeof( pc_map_table_t ) );
tptr->magic_ = PC_MAGIC;
tptr->ver_ = PC_VERSION;
tptr->num_ = PC_MAP_TABLE_SIZE;
tptr->type_ = PC_ACCTYPE_MAPPING;

SolPubkey p_id = {.x = { 0xff, }};
SolPubkey pkey = {.x = { 1, }};
SolPubkey tkey = {.x = { 2, }};
SolPubkey mkey = {.x = { 3, }};
uint64_t pqty = 100;
pc_map_table_t mptr[1];
sol_memset( mptr, 0, sizeof( pc_map_table_t ) );
SolAccountInfo acc[] = {{
.key = &pkey,
.lamports = &pqty,
.data_len = 0,
.data = NULL,
.owner = NULL,
.rent_epoch = 0,
.is_signer = true,
.is_writable = true,
.executable = false
},{
.key = &tkey,
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
.data_len = sizeof( pc_map_table_t ),
.data = (uint8_t*)tptr,
.owner = &p_id,
.rent_epoch = 0,
.is_signer = true,
.is_writable = true,
.executable = false
},{
.key = &mkey,
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
.data_len = sizeof( pc_map_table_t ),
.data = (uint8_t*)mptr,
.owner = &p_id,
.rent_epoch = 0,
.is_signer = true,
.is_writable = true,
.executable = false
}};
SolParameters prm = {
.ka = acc,
.ka_num = 3,
.data = (const uint8_t*)&idata,
.data_len = sizeof( idata ),
.program_id = &p_id
};
cr_assert( SUCCESS == dispatch( &prm, acc ) );
cr_assert( mptr->magic_ == PC_MAGIC );
cr_assert( mptr->ver_ == PC_VERSION );
cr_assert( pc_pub_key_equal( &tptr->next_, (pc_pub_key_t*)&mkey ) );
cr_assert( pc_pub_key_is_zero( &mptr->next_ ) );
pc_pub_key_set_zero( &tptr->next_ );
sol_memset( mptr, 0, sizeof( pc_map_table_t ) );
tptr->num_ = 0;
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
cr_assert( pc_pub_key_is_zero( &tptr->next_ ) );
tptr->num_ = PC_MAP_TABLE_SIZE;
tptr->magic_ = 0;
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
tptr->magic_ = PC_MAGIC;
cr_assert( SUCCESS == dispatch( &prm, acc ) );
}

Test(oracle, add_product) {
// start with perfect inputs
cmd_add_product_t idata = {
Expand Down
4 changes: 3 additions & 1 deletion program/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ mod error;
mod log;
mod processor;
mod rust_oracle;
mod test_oracle;
mod time_machine_types;
mod utils;

#[cfg(test)]
mod tests;

use crate::c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE;
use crate::error::{
OracleError,
Expand Down
1 change: 0 additions & 1 deletion program/rust/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
.try_into()
.map_err(|_| OracleError::IntegerCastingError)?;


match instruction_id {
command_t_e_cmd_upd_price | command_t_e_cmd_agg_price => {
let instruction: &cmd_upd_price = load::<cmd_upd_price>(instruction_data)?;
Expand Down
7 changes: 3 additions & 4 deletions program/rust/src/rust_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub fn update_version(
// Ok(SUCCESS)
}


/// initialize the first mapping account in a new linked-list of mapping accounts
/// accounts[0] funding account [signer writable]
/// accounts[1] new mapping account [signer writable]
Expand Down Expand Up @@ -208,7 +207,7 @@ pub fn clear_account(account: &AccountInfo) -> Result<(), ProgramError> {
/// Mutably borrow the data in `account` as a mapping account, validating that the account
/// is properly formatted. Any mutations to the returned value will be reflected in the
/// account data. Use this to read already-initialized accounts.
fn load_mapping_account_mut<'a>(
pub fn load_mapping_account_mut<'a>(
account: &'a AccountInfo,
expected_version: u32,
) -> Result<RefMut<'a, pc_map_table_t>, ProgramError> {
Expand All @@ -226,7 +225,7 @@ fn load_mapping_account_mut<'a>(

/// Initialize account as a new mapping account. This function will zero out any existing data in
/// the account.
fn initialize_mapping_account(account: &AccountInfo, version: u32) -> Result<(), ProgramError> {
pub fn initialize_mapping_account(account: &AccountInfo, version: u32) -> Result<(), ProgramError> {
clear_account(account)?;

let mut mapping_account = load_account_as_mut::<pc_map_table_t>(account)?;
Expand Down Expand Up @@ -259,6 +258,6 @@ fn load_product_account_mut<'a>(
}

// Assign pubkey bytes from source to target, fails if source is not 32 bytes
fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
pub fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
unsafe { target.k1_.copy_from_slice(source) }
}
182 changes: 0 additions & 182 deletions program/rust/src/test_oracle.rs

This file was deleted.

2 changes: 2 additions & 0 deletions program/rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod test_add_mapping;
mod test_init_mapping;
Loading