Skip to content

Commit 495502d

Browse files
committed
Add rent exemption check
1 parent f6c572f commit 495502d

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

program/src/oracle/oracle.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@
55
#include "oracle.h"
66
#include "upd_aggregate.h"
77

8+
// Returns the minimum number of lamports required to make an account
9+
// with dlen bytes of data rent exempt. These values were calculated
10+
// using the getMinimumBalanceForRentExemption RPC call, and are
11+
// guaranteed never to increase.
12+
static uint64_t rent_exempt_amount( uint64_t dlen )
13+
{
14+
switch ( dlen )
15+
{
16+
case sizeof( pc_map_table_t ):
17+
return 143821440;
18+
case PC_PROD_ACC_SIZE:
19+
return 4454400;
20+
case sizeof( pc_price_t ):
21+
return 23942400;
22+
default:
23+
return UINT64_MAX;
24+
}
25+
}
26+
27+
static bool is_rent_exempt( uint64_t lamports, uint64_t dlen )
28+
{
29+
return lamports >= rent_exempt_amount( dlen );
30+
}
31+
832
static bool valid_funding_account( SolAccountInfo *ka )
933
{
1034
return ka->is_signer &&
@@ -18,7 +42,8 @@ static bool valid_signable_account( SolParameters *prm,
1842
return ka->is_signer &&
1943
ka->is_writable &&
2044
SolPubkey_same( ka->owner, prm->program_id ) &&
21-
ka->data_len >= dlen;
45+
ka->data_len >= dlen &&
46+
is_rent_exempt( *ka->lamports, dlen );
2247
}
2348

2449
static bool valid_writable_account( SolParameters *prm,
@@ -27,7 +52,8 @@ static bool valid_writable_account( SolParameters *prm,
2752
{
2853
return ka->is_writable &&
2954
SolPubkey_same( ka->owner, prm->program_id ) &&
30-
ka->data_len >= dlen;
55+
ka->data_len >= dlen &&
56+
is_rent_exempt( *ka->lamports, dlen );
3157
}
3258

3359
static uint64_t init_mapping( SolParameters *prm, SolAccountInfo *ka )

program/src/oracle/test_oracle.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ char heap_start[8192];
44
#include "sort.c"
55
#include <criterion/criterion.h>
66

7+
uint64_t MAPPING_ACCOUNT_LAMPORTS = 143821440;
8+
uint64_t PRODUCT_ACCOUNT_LAMPORTS = 4454400;
9+
uint64_t PRICE_ACCOUNT_LAMPORTS = 23942400;
10+
711
Test(oracle, init_mapping) {
812

913
// start with perfect inputs
@@ -30,7 +34,7 @@ Test(oracle, init_mapping) {
3034
.executable = false
3135
},{
3236
.key = &mkey,
33-
.lamports = &pqty,
37+
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
3438
.data_len = sizeof( pc_map_table_t ),
3539
.data = (uint8_t*)mptr,
3640
.owner = &p_id,
@@ -96,7 +100,7 @@ Test(oracle, add_mapping ) {
96100
SolPubkey pkey = {.x = { 1, }};
97101
SolPubkey tkey = {.x = { 2, }};
98102
SolPubkey mkey = {.x = { 3, }};
99-
uint64_t pqty = 100, tqty = 100;
103+
uint64_t pqty = 100;
100104
pc_map_table_t mptr[1];
101105
sol_memset( mptr, 0, sizeof( pc_map_table_t ) );
102106
SolAccountInfo acc[] = {{
@@ -111,7 +115,7 @@ Test(oracle, add_mapping ) {
111115
.executable = false
112116
},{
113117
.key = &tkey,
114-
.lamports = &pqty,
118+
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
115119
.data_len = sizeof( pc_map_table_t ),
116120
.data = (uint8_t*)tptr,
117121
.owner = &p_id,
@@ -121,7 +125,7 @@ Test(oracle, add_mapping ) {
121125
.executable = false
122126
},{
123127
.key = &mkey,
124-
.lamports = &tqty,
128+
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
125129
.data_len = sizeof( pc_map_table_t ),
126130
.data = (uint8_t*)mptr,
127131
.owner = &p_id,
@@ -186,7 +190,7 @@ Test(oracle, add_product) {
186190
.executable = false
187191
},{
188192
.key = &mkey,
189-
.lamports = &pqty,
193+
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
190194
.data_len = sizeof( pc_map_table_t ),
191195
.data = (uint8_t*)mptr,
192196
.owner = &p_id,
@@ -196,7 +200,7 @@ Test(oracle, add_product) {
196200
.executable = false
197201
},{
198202
.key = &skey,
199-
.lamports = &pqty,
203+
.lamports = &PRODUCT_ACCOUNT_LAMPORTS,
200204
.data_len = PC_PROD_ACC_SIZE,
201205
.data = (uint8_t*)sptr,
202206
.owner = &p_id,
@@ -259,7 +263,7 @@ Test( oracle, add_publisher ) {
259263
SolPubkey p_id = {.x = { 0xff, }};
260264
SolPubkey pkey = {.x = { 1, }};
261265
SolPubkey skey = {.x = { 3, }};
262-
uint64_t pqty = 100, sqty = 200;
266+
uint64_t pqty = 100;
263267
pc_price_t sptr[1];
264268
sol_memset( sptr, 0, sizeof( pc_price_t ) );
265269
sptr->magic_ = PC_MAGIC;
@@ -278,7 +282,7 @@ Test( oracle, add_publisher ) {
278282
.executable = false
279283
},{
280284
.key = &skey,
281-
.lamports = &sqty,
285+
.lamports = &PRICE_ACCOUNT_LAMPORTS,
282286
.data_len = sizeof( pc_price_t ),
283287
.data = (uint8_t*)sptr,
284288
.owner = &p_id,
@@ -358,7 +362,7 @@ Test( oracle, upd_test ) {
358362
.executable = false
359363
},{
360364
.key = &mkey,
361-
.lamports = &pqty,
365+
.lamports = &PRICE_ACCOUNT_LAMPORTS,
362366
.data_len = sizeof( pc_price_t ),
363367
.data = (uint8_t*)mptr,
364368
.owner = &p_id,
@@ -436,7 +440,7 @@ Test( oracle, upd_price ) {
436440
.executable = false
437441
},{
438442
.key = &skey,
439-
.lamports = &sqty,
443+
.lamports = &PRICE_ACCOUNT_LAMPORTS,
440444
.data_len = sizeof( pc_price_t ),
441445
.data = (uint8_t*)sptr,
442446
.owner = &p_id,
@@ -547,7 +551,7 @@ Test( oracle, upd_price_no_fail_on_error ) {
547551
.executable = false
548552
},{
549553
.key = &skey,
550-
.lamports = &sqty,
554+
.lamports = &PRICE_ACCOUNT_LAMPORTS,
551555
.data_len = sizeof( pc_price_t ),
552556
.data = (uint8_t*)sptr,
553557
.owner = &p_id,
@@ -745,7 +749,7 @@ Test( oracle, del_publisher ) {
745749
};
746750
SolPubkey pkey = {.x = { 1, }};
747751
SolPubkey skey = {.x = { 3, }};
748-
uint64_t pqty = 100, sqty = 200;
752+
uint64_t pqty = 100;
749753
pc_price_t sptr[1];
750754
sol_memset( sptr, 0, sizeof( pc_price_t ) );
751755
sptr->magic_ = PC_MAGIC;
@@ -768,7 +772,7 @@ Test( oracle, del_publisher ) {
768772
.executable = false
769773
},{
770774
.key = &skey,
771-
.lamports = &sqty,
775+
.lamports = &PRICE_ACCOUNT_LAMPORTS,
772776
.data_len = sizeof( pc_price_t ),
773777
.data = (uint8_t*)sptr,
774778
.owner = (SolPubkey*)&p_id,

0 commit comments

Comments
 (0)