Skip to content

Commit 2d81559

Browse files
authored
changed c return value of update price to indicate aggregation (#190) (#191)
* changed c return value of update price to indicate aggregation (#190) * changed c return value of update price to indicate aggregation * forgot to add bindings * removed the need for casting * fixed update_no_fail * removed unnecessary docker packages * made successful aggregate update greater than 15 * timeless coments * clarified c_oracle_header comment * added comment clarifying that 0 is success
1 parent 44fe4f5 commit 2d81559

File tree

7 files changed

+49
-14
lines changed

7 files changed

+49
-14
lines changed

docker/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ RUN apt-get install -qq \
2626
qtchooser \
2727
qt5-qmake \
2828
qtbase5-dev-tools \
29-
libqt5websockets5-dev
30-
29+
libqt5websockets5-dev\
30+
libclang-dev
31+
3132
# Install jcon-cpp library
3233
RUN git clone https://github.com/joncol/jcon-cpp.git /jcon-cpp && cd /jcon-cpp && git checkout 2235654e39c7af505d7158bf996e47e37a23d6e3 && mkdir build && cd build && cmake .. && make -j4 && make install
3334

program/c/src/oracle/oracle.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,11 @@ static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka )
486486
cptr->pub_slot_ <= fptr->pub_slot_ ) {
487487
return ERROR_INVALID_ARGUMENT;
488488
}
489-
489+
bool updated_aggregate = false;
490490
// update aggregate price as necessary
491491
if ( sptr->slot_ > pptr->agg_.pub_slot_ ) {
492492
upd_aggregate( pptr, sptr->slot_, sptr->unix_timestamp_ );
493+
updated_aggregate = true;
493494
}
494495

495496
// update component price if required
@@ -511,13 +512,15 @@ static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka )
511512
fptr->status_ = status;
512513
fptr->pub_slot_ = cptr->pub_slot_;
513514
}
515+
if (updated_aggregate){
516+
return SUCCESSFULLY_UPDATED_AGGREGATE;
517+
}
514518
return SUCCESS;
515519
}
516520

517521
static uint64_t upd_price_no_fail_on_error( SolParameters *prm, SolAccountInfo *ka )
518522
{
519-
upd_price( prm, ka );
520-
return SUCCESS;
523+
return upd_price( prm, ka ) == SUCCESSFULLY_UPDATED_AGGREGATE? SUCCESSFULLY_UPDATED_AGGREGATE : SUCCESS;
521524
}
522525

523526
static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )

program/c/src/oracle/oracle.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
extern "C" {
77
#endif
88

9+
//A return value indicating that the aggregate price was updated
10+
//this triggers SMA trackers to update
11+
//values 0-14 are defined in solana_sdk.h (v1.10.31 )
12+
//used consts instead of define because bingen always turns
13+
// defines to u32 (even with ULL suffix)
14+
const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 1000ULL;
15+
916
// magic number at head of account
1017
#define PC_MAGIC 0xa1b2c3d4
1118

program/c/src/oracle/test_oracle.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ Test( oracle, upd_price ) {
409409
.data_len = sizeof( idata ),
410410
.program_id = &p_id
411411
};
412-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
412+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
413413
cr_assert( sptr->comp_[0].latest_.price_ == 42L );
414414
cr_assert( sptr->comp_[0].latest_.conf_ == 2L );
415415
cr_assert( sptr->comp_[0].latest_.pub_slot_ == 1 );
@@ -426,7 +426,7 @@ Test( oracle, upd_price ) {
426426
idata.price_ = 81;
427427
idata.pub_slot_ = 2;
428428
cvar.slot_ = 3;
429-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
429+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
430430
cr_assert( sptr->comp_[0].latest_.price_ == 81L );
431431
cr_assert( sptr->comp_[0].agg_.price_ == 42L );
432432
cr_assert( sptr->comp_[0].latest_.pub_slot_ == 2 );
@@ -436,7 +436,7 @@ Test( oracle, upd_price ) {
436436
// next price doesnt change but slot does
437437
cvar.slot_ = 4;
438438
idata.pub_slot_ = 3;
439-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
439+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
440440
cr_assert( sptr->comp_[0].latest_.price_ == 81L );
441441
cr_assert( sptr->comp_[0].agg_.price_ == 81L );
442442
cr_assert( sptr->comp_[0].latest_.pub_slot_ == 3 );
@@ -446,7 +446,7 @@ Test( oracle, upd_price ) {
446446
// next price doesnt change and neither does aggregate but slot does
447447
idata.pub_slot_ = 4;
448448
cvar.slot_ = 5;
449-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
449+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
450450
cr_assert( sptr->comp_[0].latest_.price_ == 81L );
451451
cr_assert( sptr->comp_[0].agg_.price_ == 81L );
452452
cr_assert( sptr->comp_[0].latest_.pub_slot_ == 4 );
@@ -466,7 +466,7 @@ Test( oracle, upd_price ) {
466466
cvar.slot_ = 6;
467467
idata.price_ = 50;
468468
idata.conf_ = 6;
469-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
469+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
470470
cr_assert( sptr->comp_[0].latest_.price_ == 50L );
471471
cr_assert( sptr->comp_[0].latest_.conf_ == 6L );
472472
cr_assert( sptr->comp_[0].latest_.status_ == PC_STATUS_UNKNOWN );
@@ -478,7 +478,7 @@ Test( oracle, upd_price ) {
478478
// Crank one more time and aggregate should be unknown
479479
idata.pub_slot_ = 6;
480480
cvar.slot_ = 7;
481-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
481+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
482482
cr_assert( sptr->agg_.status_ == PC_STATUS_UNKNOWN );
483483
}
484484

@@ -559,7 +559,7 @@ Test( oracle, upd_price_no_fail_on_error ) {
559559
pc_pub_key_assign( &sptr->comp_[0].pub_, (pc_pub_key_t*)&pkey );
560560

561561
// The update should now succeed, and have an effect.
562-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
562+
cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) );
563563
cr_assert( sptr->comp_[0].latest_.price_ == 42L );
564564
cr_assert( sptr->comp_[0].latest_.conf_ == 9L );
565565
cr_assert( sptr->comp_[0].latest_.pub_slot_ == 1 );

program/rust/src/bindings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#define static_assert _Static_assert
4+
5+
typedef signed char int8_t;
6+
typedef unsigned char uint8_t;
7+
typedef signed short int16_t;
8+
typedef unsigned short uint16_t;
9+
typedef signed int int32_t;
10+
typedef unsigned int uint32_t;
11+
typedef signed long int int64_t;
12+
typedef unsigned long int uint64_t;
13+
14+
#include "../../c/src/oracle/oracle.h"

program/rust/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
//c_oracle_header is auto generated by build_bpf.sh
2+
//to reflect the current status of oracle.h
3+
mod c_oracle_header;
4+
15
#[link(name = "cpyth")]
26
extern "C" {
37
fn c_entrypoint(input: *mut u8) -> u64;
48
}
59

610
#[no_mangle]
711
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
8-
unsafe{
9-
return c_entrypoint(input);
12+
let c_ret_val = unsafe{c_entrypoint(input)};
13+
if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE{
14+
//0 is the SUCCESS value for solana
15+
return 0;
16+
} else {
17+
return c_ret_val;
1018
}
1119
}

scripts/build-bpf.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ rm ./target/*-keypair.json
4040

4141
#build Rust and link it with C
4242
cd "${RUST_DIR}"
43+
cargo install bindgen
44+
bindgen ./src/bindings.h -o ./src/c_oracle_header.rs
4345
cargo clean
4446
cargo build-bpf
4547
sha256sum ./target/**/*.so

0 commit comments

Comments
 (0)