Skip to content

Commit 94e9954

Browse files
authored
Rust/more tests (#234)
* Simplistic trait * Remove irrelevant file * Rename * Delete * Add functions to initialize tests * Restore rust oracle * Add comment * Stage * Fix warnings * Prune C code * Add price * Cleanup imports * Upd product uses the new syntax * Formatted * Add size check to previous tests * Update tests for min pub * Remove import
1 parent 251eba9 commit 94e9954

File tree

9 files changed

+399
-211
lines changed

9 files changed

+399
-211
lines changed

program/c/src/oracle/oracle.c

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -59,55 +59,6 @@ static bool valid_writable_account( SolParameters *prm,
5959
is_rent_exempt( *ka->lamports, ka->data_len );
6060
}
6161

62-
static uint64_t add_price( SolParameters *prm, SolAccountInfo *ka )
63-
{
64-
// Validate command parameters
65-
cmd_add_price_t *cptr = (cmd_add_price_t*)prm->data;
66-
if ( prm->data_len != sizeof( cmd_add_price_t ) ||
67-
cptr->expo_ > PC_MAX_NUM_DECIMALS ||
68-
cptr->expo_ < -PC_MAX_NUM_DECIMALS ||
69-
cptr->ptype_ == PC_PTYPE_UNKNOWN ) {
70-
return ERROR_INVALID_ARGUMENT;
71-
}
72-
73-
// Account (1) is the product account that we're going to add to
74-
// Account (2) is the new price account
75-
// Verify that these are signed, writable accounts with correct ownership
76-
// and size
77-
if ( prm->ka_num != 3 ||
78-
!valid_funding_account( &ka[0] ) ||
79-
!valid_signable_account( prm, &ka[1], PC_PROD_ACC_SIZE ) ||
80-
!valid_signable_account( prm, &ka[2], sizeof( pc_price_t ) ) ) {
81-
return ERROR_INVALID_ARGUMENT;
82-
}
83-
84-
// Verify that the product account is valid
85-
// and that the new price account is uninitialized
86-
pc_prod_t *pptr = (pc_prod_t*)ka[1].data;
87-
pc_price_t *sptr = (pc_price_t*)ka[2].data;
88-
if ( pptr->magic_ != PC_MAGIC ||
89-
pptr->ver_ != cptr->ver_ ||
90-
pptr->type_ != PC_ACCTYPE_PRODUCT ||
91-
sptr->magic_ != 0 ) {
92-
return ERROR_INVALID_ARGUMENT;
93-
}
94-
95-
// Initialize symbol account
96-
sol_memset( sptr, 0, sizeof( pc_price_t ) );
97-
sptr->magic_ = PC_MAGIC;
98-
sptr->ver_ = cptr->ver_;
99-
sptr->type_ = PC_ACCTYPE_PRICE;
100-
sptr->size_ = sizeof( pc_price_t ) - sizeof( sptr->comp_ );
101-
sptr->expo_ = cptr->expo_;
102-
sptr->ptype_ = cptr->ptype_;
103-
pc_pub_key_assign( &sptr->prod_, (pc_pub_key_t*)ka[1].key );
104-
105-
// bind price account to product account
106-
pc_pub_key_assign( &sptr->next_, &pptr->px_acc_ );
107-
pc_pub_key_assign( &pptr->px_acc_, (pc_pub_key_t*)ka[2].key );
108-
return SUCCESS;
109-
}
110-
11162
static uint64_t init_price( SolParameters *prm, SolAccountInfo *ka )
11263
{
11364
// Validate command parameters
@@ -156,53 +107,6 @@ static uint64_t init_price( SolParameters *prm, SolAccountInfo *ka )
156107
return SUCCESS;
157108
}
158109

159-
static uint64_t add_publisher( SolParameters *prm, SolAccountInfo *ka )
160-
{
161-
// Validate command parameters
162-
cmd_add_publisher_t *cptr = (cmd_add_publisher_t*)prm->data;
163-
if ( prm->data_len != sizeof( cmd_add_publisher_t ) ||
164-
pc_pub_key_is_zero( &cptr->pub_ ) ) {
165-
return ERROR_INVALID_ARGUMENT;
166-
}
167-
168-
// Account (1) is the price account
169-
// Verify that this is signed, writable with correct ownership
170-
// and size
171-
if ( prm->ka_num != 2 ||
172-
!valid_funding_account( &ka[0] ) ||
173-
!valid_signable_account( prm, &ka[1], sizeof( pc_price_t ) ) ) {
174-
return ERROR_INVALID_ARGUMENT;
175-
}
176-
177-
// Verify that symbol account is initialized and corresponds to the
178-
// same symbol and price-type in the instruction parameters
179-
pc_price_t *sptr = (pc_price_t*)ka[1].data;
180-
if ( sptr->magic_ != PC_MAGIC ||
181-
sptr->ver_ != cptr->ver_ ||
182-
sptr->type_ != PC_ACCTYPE_PRICE ) {
183-
return ERROR_INVALID_ARGUMENT;
184-
}
185-
186-
// try to add publisher
187-
for(uint32_t i=0; i != sptr->num_; ++i ) {
188-
pc_price_comp_t *iptr = &sptr->comp_[i];
189-
if ( pc_pub_key_equal( &iptr->pub_, &cptr->pub_ ) ) {
190-
return ERROR_INVALID_ARGUMENT;
191-
}
192-
}
193-
if ( sptr->num_ >= PC_COMP_SIZE ) {
194-
return ERROR_INVALID_ARGUMENT;
195-
}
196-
pc_price_comp_t *iptr = &sptr->comp_[sptr->num_++];
197-
sol_memset( iptr, 0, sizeof( pc_price_comp_t ) );
198-
pc_pub_key_assign( &iptr->pub_, &cptr->pub_ );
199-
200-
// update size of account
201-
sptr->size_ = sizeof( pc_price_t ) - sizeof( sptr->comp_ ) +
202-
sptr->num_ * sizeof( pc_price_comp_t );
203-
return SUCCESS;
204-
}
205-
206110
// remove publisher from price node
207111
static uint64_t del_publisher( SolParameters *prm, SolAccountInfo *ka )
208112
{
@@ -353,8 +257,8 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
353257
case e_cmd_add_mapping: return ERROR_INVALID_ARGUMENT;
354258
case e_cmd_add_product: return ERROR_INVALID_ARGUMENT;
355259
case e_cmd_upd_product: return ERROR_INVALID_ARGUMENT;
356-
case e_cmd_add_price: return add_price( prm, ka );
357-
case e_cmd_add_publisher: return add_publisher( prm, ka );
260+
case e_cmd_add_price: return ERROR_INVALID_ARGUMENT;
261+
case e_cmd_add_publisher: return ERROR_INVALID_ARGUMENT;
358262
case e_cmd_del_publisher: return del_publisher( prm, ka );
359263
case e_cmd_init_price: return init_price( prm, ka );
360264
case e_cmd_init_test: return ERROR_INVALID_ARGUMENT;

program/c/src/oracle/test_oracle.c

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,6 @@ uint64_t MAPPING_ACCOUNT_LAMPORTS = 143821440;
77
uint64_t PRODUCT_ACCOUNT_LAMPORTS = 4454400;
88
uint64_t PRICE_ACCOUNT_LAMPORTS = 23942400;
99

10-
Test( oracle, add_publisher ) {
11-
// start with perfect inputs
12-
cmd_add_publisher_t idata = {
13-
.ver_ = PC_VERSION,
14-
.cmd_ = e_cmd_add_publisher,
15-
.pub_ = { .k8_ = { 3UL, 4UL, 5UL, 6UL } }
16-
};
17-
SolPubkey p_id = {.x = { 0xff, }};
18-
SolPubkey pkey = {.x = { 1, }};
19-
SolPubkey skey = {.x = { 3, }};
20-
uint64_t pqty = 100;
21-
pc_price_t sptr[1];
22-
sol_memset( sptr, 0, sizeof( pc_price_t ) );
23-
sptr->magic_ = PC_MAGIC;
24-
sptr->ver_ = PC_VERSION;
25-
sptr->type_ = PC_ACCTYPE_PRICE;
26-
sptr->ptype_ = PC_PTYPE_PRICE;
27-
SolAccountInfo acc[] = {{
28-
.key = &pkey,
29-
.lamports = &pqty,
30-
.data_len = 0,
31-
.data = NULL,
32-
.owner = NULL,
33-
.rent_epoch = 0,
34-
.is_signer = true,
35-
.is_writable = true,
36-
.executable = false
37-
},{
38-
.key = &skey,
39-
.lamports = &pqty,
40-
.data_len = sizeof( pc_price_t ),
41-
.data = (uint8_t*)sptr,
42-
.owner = &p_id,
43-
.rent_epoch = 0,
44-
.is_signer = true,
45-
.is_writable = true,
46-
.executable = false
47-
} };
48-
SolParameters prm = {
49-
.ka = acc,
50-
.ka_num = 2,
51-
.data = (const uint8_t*)&idata,
52-
.data_len = sizeof( idata ),
53-
.program_id = &p_id
54-
};
55-
56-
// Expect the instruction to fail, because the price account isn't rent exempt
57-
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
58-
59-
// Now give the price account enough lamports to be rent exempt
60-
acc[1].lamports = &PRICE_ACCOUNT_LAMPORTS;
61-
62-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
63-
cr_assert( sptr->num_ == 1 );
64-
cr_assert( pc_pub_key_equal( &idata.pub_, &sptr->comp_[0].pub_ ) );
65-
// cant add twice
66-
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
67-
68-
// invalid params
69-
sol_memset( sptr, 0, sizeof( pc_price_t ) );
70-
sptr->magic_ = PC_MAGIC;
71-
sptr->ver_ = PC_VERSION;
72-
sptr->type_ = PC_ACCTYPE_PRICE;
73-
// bad price account
74-
sptr->magic_ = 0;
75-
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
76-
sptr->magic_ = PC_MAGIC;
77-
78-
// fill up price node
79-
for( unsigned i = 0;; ++i ) {
80-
idata.pub_.k8_[0] = 10 + i;
81-
uint64_t rc = dispatch( &prm, acc );
82-
if ( rc != SUCCESS ) {
83-
cr_assert( i == ( unsigned )(PC_COMP_SIZE) );
84-
break;
85-
}
86-
cr_assert( sptr->num_ == i + 1 );
87-
cr_assert( pc_pub_key_equal( &idata.pub_, &sptr->comp_[i].pub_ ) );
88-
cr_assert( rc == SUCCESS );
89-
}
90-
}
91-
9210
Test(oracle, pc_size ) {
9311
cr_assert( sizeof( pc_pub_key_t ) == 32 );
9412
cr_assert( sizeof( pc_map_table_t ) ==

program/rust/src/c_oracle_header.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bytemuck::{
77
Pod,
88
Zeroable,
99
};
10+
use solana_program::pubkey::Pubkey;
1011
use std::mem::size_of;
1112
//bindings.rs is generated by build.rs to include
1213
//things defined in bindings.h
@@ -51,6 +52,15 @@ impl PythAccount for pc_price_t {
5152
const INITIAL_SIZE: u32 = PC_PRICE_T_COMP_OFFSET as u32;
5253
}
5354

55+
impl pc_pub_key_t {
56+
pub fn new_unique() -> pc_pub_key_t {
57+
let solana_unique = Pubkey::new_unique();
58+
pc_pub_key_t {
59+
k1_: solana_unique.to_bytes(),
60+
}
61+
}
62+
}
63+
5464

5565
#[cfg(target_endian = "little")]
5666
unsafe impl Zeroable for pc_acc {

program/rust/src/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod test_add_mapping;
2+
mod test_add_price;
23
mod test_add_product;
4+
mod test_add_publisher;
35
mod test_init_mapping;
46
mod test_set_min_pub;
57
mod test_upd_product;

0 commit comments

Comments
 (0)