diff --git a/docker/Dockerfile b/docker/Dockerfile index 3a8ced9e1..fb7394d90 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -26,8 +26,9 @@ RUN apt-get install -qq \ qtchooser \ qt5-qmake \ qtbase5-dev-tools \ - libqt5websockets5-dev - + libqt5websockets5-dev\ + libclang-dev + # Install jcon-cpp library 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 diff --git a/program/c/src/oracle/oracle.c b/program/c/src/oracle/oracle.c index 4833f7ea4..803d929b5 100644 --- a/program/c/src/oracle/oracle.c +++ b/program/c/src/oracle/oracle.c @@ -486,10 +486,11 @@ static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka ) cptr->pub_slot_ <= fptr->pub_slot_ ) { return ERROR_INVALID_ARGUMENT; } - + bool updated_aggregate = false; // update aggregate price as necessary if ( sptr->slot_ > pptr->agg_.pub_slot_ ) { upd_aggregate( pptr, sptr->slot_, sptr->unix_timestamp_ ); + updated_aggregate = true; } // update component price if required @@ -511,13 +512,15 @@ static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka ) fptr->status_ = status; fptr->pub_slot_ = cptr->pub_slot_; } + if (updated_aggregate){ + return SUCCESSFULLY_UPDATED_AGGREGATE; + } return SUCCESS; } static uint64_t upd_price_no_fail_on_error( SolParameters *prm, SolAccountInfo *ka ) { - upd_price( prm, ka ); - return SUCCESS; + return upd_price( prm, ka ) == SUCCESSFULLY_UPDATED_AGGREGATE? SUCCESSFULLY_UPDATED_AGGREGATE : SUCCESS; } static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka ) diff --git a/program/c/src/oracle/oracle.h b/program/c/src/oracle/oracle.h index eba19b516..4778219fa 100644 --- a/program/c/src/oracle/oracle.h +++ b/program/c/src/oracle/oracle.h @@ -6,6 +6,13 @@ extern "C" { #endif +//a new custom return value to indicate to rust that aggregate was updated +//this triggers SMA trackers to update +//values 0-14 are defined in solana_sdk.h +//used consts instead of define because bingen always turns +// defines to u32 (even with ULL suffix) +const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 15ULL; + // magic number at head of account #define PC_MAGIC 0xa1b2c3d4 diff --git a/program/c/src/oracle/test_oracle.c b/program/c/src/oracle/test_oracle.c index 603c23390..3be8d4a74 100644 --- a/program/c/src/oracle/test_oracle.c +++ b/program/c/src/oracle/test_oracle.c @@ -409,7 +409,7 @@ Test( oracle, upd_price ) { .data_len = sizeof( idata ), .program_id = &p_id }; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 42L ); cr_assert( sptr->comp_[0].latest_.conf_ == 2L ); cr_assert( sptr->comp_[0].latest_.pub_slot_ == 1 ); @@ -426,7 +426,7 @@ Test( oracle, upd_price ) { idata.price_ = 81; idata.pub_slot_ = 2; cvar.slot_ = 3; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 81L ); cr_assert( sptr->comp_[0].agg_.price_ == 42L ); cr_assert( sptr->comp_[0].latest_.pub_slot_ == 2 ); @@ -436,7 +436,7 @@ Test( oracle, upd_price ) { // next price doesnt change but slot does cvar.slot_ = 4; idata.pub_slot_ = 3; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 81L ); cr_assert( sptr->comp_[0].agg_.price_ == 81L ); cr_assert( sptr->comp_[0].latest_.pub_slot_ == 3 ); @@ -446,7 +446,7 @@ Test( oracle, upd_price ) { // next price doesnt change and neither does aggregate but slot does idata.pub_slot_ = 4; cvar.slot_ = 5; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 81L ); cr_assert( sptr->comp_[0].agg_.price_ == 81L ); cr_assert( sptr->comp_[0].latest_.pub_slot_ == 4 ); @@ -466,7 +466,7 @@ Test( oracle, upd_price ) { cvar.slot_ = 6; idata.price_ = 50; idata.conf_ = 6; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 50L ); cr_assert( sptr->comp_[0].latest_.conf_ == 6L ); cr_assert( sptr->comp_[0].latest_.status_ == PC_STATUS_UNKNOWN ); @@ -478,7 +478,7 @@ Test( oracle, upd_price ) { // Crank one more time and aggregate should be unknown idata.pub_slot_ = 6; cvar.slot_ = 7; - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->agg_.status_ == PC_STATUS_UNKNOWN ); } @@ -559,7 +559,7 @@ Test( oracle, upd_price_no_fail_on_error ) { pc_pub_key_assign( &sptr->comp_[0].pub_, (pc_pub_key_t*)&pkey ); // The update should now succeed, and have an effect. - cr_assert( SUCCESS == dispatch( &prm, acc ) ); + cr_assert( SUCCESSFULLY_UPDATED_AGGREGATE == dispatch( &prm, acc ) ); cr_assert( sptr->comp_[0].latest_.price_ == 42L ); cr_assert( sptr->comp_[0].latest_.conf_ == 9L ); cr_assert( sptr->comp_[0].latest_.pub_slot_ == 1 ); diff --git a/program/rust/src/bindings.h b/program/rust/src/bindings.h new file mode 100644 index 000000000..91431edf5 --- /dev/null +++ b/program/rust/src/bindings.h @@ -0,0 +1,14 @@ +#pragma once + +#define static_assert _Static_assert + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed int int32_t; +typedef unsigned int uint32_t; +typedef signed long int int64_t; +typedef unsigned long int uint64_t; + +#include "../../c/src/oracle/oracle.h" \ No newline at end of file diff --git a/program/rust/src/lib.rs b/program/rust/src/lib.rs index 45ca4263e..71e5ecdeb 100644 --- a/program/rust/src/lib.rs +++ b/program/rust/src/lib.rs @@ -1,3 +1,6 @@ +//this is auto generated by build_bpf.sh +mod c_oracle_header; + #[link(name = "cpyth")] extern "C" { fn c_entrypoint(input: *mut u8) -> u64; @@ -5,7 +8,12 @@ extern "C" { #[no_mangle] pub extern "C" fn entrypoint(input: *mut u8) -> u64 { - unsafe{ - return c_entrypoint(input); + let c_ret_val = unsafe{c_entrypoint(input)}; + if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE{ + return 0; + } else { + return c_ret_val; } + + } \ No newline at end of file diff --git a/scripts/build-bpf.sh b/scripts/build-bpf.sh index 2d7022e7d..e7a552352 100755 --- a/scripts/build-bpf.sh +++ b/scripts/build-bpf.sh @@ -40,6 +40,8 @@ rm ./target/*-keypair.json #build Rust and link it with C cd "${RUST_DIR}" +cargo install bindgen +bindgen ./src/bindings.h -o ./src/c_oracle_header.rs cargo clean cargo build-bpf sha256sum ./target/**/*.so