Skip to content

Commit 7911477

Browse files
committed
Refactor: extract is_valid_price_upd into seperate method
1 parent ee0e8b3 commit 7911477

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

program/src/oracle/oracle.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,32 @@ static uint32_t find_comp_idx( SolAccountInfo *publish_account, SolAccountInfo *
489489
return i;
490490
}
491491

492+
static bool is_valid_price_upd( cmd_upd_price_t *cptr, SolAccountInfo *price_account, uint32_t comp_idx )
493+
{
494+
// Verify that symbol account is initialized and corresponds to the
495+
// same symbol in the instruction parameters
496+
pc_price_t *pptr = (pc_price_t*)price_account->data;
497+
if ( pptr->magic_ != PC_MAGIC ||
498+
pptr->ver_ != cptr->ver_ ||
499+
pptr->type_ != PC_ACCTYPE_PRICE ) {
500+
return false;
501+
}
502+
503+
// Verify that the price component index is valid
504+
if ( comp_idx >= pptr->num_ ) {
505+
return false;
506+
}
507+
508+
// Reject if this price corresponds to the same or earlier time
509+
pc_price_info_t *fptr = &pptr->comp_[comp_idx].latest_;
510+
if ( cptr->cmd_ == e_cmd_upd_price &&
511+
cptr->pub_slot_ <= fptr->pub_slot_ ) {
512+
return false;
513+
}
514+
515+
return true;
516+
}
517+
492518
static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka )
493519
{
494520
// Validate command parameters
@@ -515,26 +541,9 @@ static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka )
515541
return ERROR_INVALID_ARGUMENT;
516542
}
517543

544+
// Fail the transaction if the price update is invalid
518545
uint32_t comp_idx = find_comp_idx( publish_account, price_account );
519-
520-
// Verify that symbol account is initialized and corresponds to the
521-
// same symbol in the instruction parameters
522-
pc_price_t *pptr = (pc_price_t*)price_account->data;
523-
if ( pptr->magic_ != PC_MAGIC ||
524-
pptr->ver_ != cptr->ver_ ||
525-
pptr->type_ != PC_ACCTYPE_PRICE ) {
526-
return ERROR_INVALID_ARGUMENT;
527-
}
528-
529-
// verify that publisher is valid
530-
if ( comp_idx == pptr->num_ ) {
531-
return ERROR_INVALID_ARGUMENT;
532-
}
533-
534-
// reject if this price corresponds to the same or earlier time
535-
pc_price_info_t *fptr = &pptr->comp_[comp_idx].latest_;
536-
if ( cptr->cmd_ == e_cmd_upd_price &&
537-
cptr->pub_slot_ <= fptr->pub_slot_ ) {
546+
if ( !is_valid_price_upd( cptr, price_account, comp_idx ) ) {
538547
return ERROR_INVALID_ARGUMENT;
539548
}
540549

0 commit comments

Comments
 (0)