Skip to content

Commit

Permalink
Format (#215)
Browse files Browse the repository at this point in the history
* Format

* CI

* Fixed

* Add comment to README

* Format config

* Fix build

* Log time and ema (#212)

* Log time and ema

* Fix ema deserialize

* Revert format

* Add expo

* Fix comments

* Unused import

* Revert "Log time and ema (#212)" (#216)

This reverts commit f0bb36c.

Co-authored-by: Guillermo Bescos <guibescos>
  • Loading branch information
guibescos authored Jul 29, 2022
1 parent 542755f commit f31a788
Show file tree
Hide file tree
Showing 42 changed files with 130 additions and 71 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/check-fomatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Check Formatting

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: rustfmt
- uses: pre-commit/[email protected]
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
files : program/
- id: end-of-file-fixer
files : program/
- id: check-added-large-files
- repo: local
hooks:
- id: cargo-fmt-nightly
name: Cargo Fmt Nightly
language: "rust"
entry: cargo +nightly fmt
pass_filenames: false
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"program/rust"
]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ root@pyth-dev# usermod -u 1002 -g 1004 -s /bin/bash pyth
Finally, in docker extension inside VS Code click right and choose "Attach VS Code". If you're using a remote host in VS Code make sure to let this connection be open.

To get best experience from C++ IntelliSense, open entire `/home/pyth` in VS Code to include `solana` directory in home for lookup directories.

### pre-commit hooks
pre-commit is a tool that checks and fixes simple issues (formatting, ...) before each commit. You can install it by following [their website](https://pre-commit.com/). In order to enable checks for this repo run `pre-commit install` from command-line in the root of this repo.

The checks are also performed in the CI to ensure the code follows consistent formatting. Formatting is only currently enforced in the `program/` directory.
You might also need to install the nightly toolchain to run the formatting by running `rustup toolchain install nightly`.
1 change: 0 additions & 1 deletion program/c/src/oracle/model/clean
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/sh
rm -rfv bin

7 changes: 3 additions & 4 deletions program/c/src/oracle/model/price_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ price_model_core( uint64_t cnt,
optimized mergesort (merge with an unrolled insertion sorting
network small n base cases). The best case is ~0.5 n lg n compares
and the average and worst cases are ~n lg n compares.
While not completely data oblivious, this has quite low variance in
operation count practically and this is _better_ than quicksort's
average case and quicksort's worst case is a computational
denial-of-service and timing attack vulnerable O(n^2). Unlike
quicksort, this is also stable (but this stability does not
currently matter ... it might be a factor in future models).
A data oblivious sorting network approach might be viable here with
and would have a completely deterministic operations count. It
currently isn't used as the best known practical approaches for
Expand Down Expand Up @@ -101,7 +101,7 @@ price_model_core( uint64_t cnt,

*_p50 = avg_2_int64( vl, vr );
}

/* Extract the p75 (this is the mirror image of the p25 case) */

uint64_t p75_idx = cnt - ((uint64_t)1) - p25_idx;
Expand All @@ -110,4 +110,3 @@ price_model_core( uint64_t cnt,

return sort_quote;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/model/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ $CC test_price_model.c price_model.c -o bin/test_price_model || exit 1
bin/test_price_model || exit 1

echo all tests passed

1 change: 0 additions & 1 deletion program/c/src/oracle/model/test_price_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,3 @@ main( int argc,
printf( "pass\n" );
return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,3 @@ extern uint64_t c_entrypoint(const uint8_t *input)
}
return dispatch( prm, ka );
}

2 changes: 1 addition & 1 deletion program/c/src/oracle/oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" {
// defines to u32 (even with ULL suffix)
const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 1000ULL;

// The size of the "time machine" account defined in the
// The size of the "time machine" account defined in the
// Rust portion of the codebase.
const uint64_t TIME_MACHINE_STRUCT_SIZE = 1864ULL;

Expand Down
1 change: 0 additions & 1 deletion program/c/src/oracle/pd.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,3 @@ static inline void pd_sqrt( pd_t *r, pd_t *val, const int64_t *f )
#ifdef __cplusplus
}
#endif

1 change: 0 additions & 1 deletion program/c/src/oracle/sort/clean
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/sh
rm -rfv bin

1 change: 0 additions & 1 deletion program/c/src/oracle/sort/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ $CC test_sort_stable.c -o bin/test_sort_stable || exit 1
bin/test_sort_stable || exit 1

echo all tests passed

1 change: 0 additions & 1 deletion program/c/src/oracle/sort/sort_stable_base_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,3 @@ main( int argc,
sort_gen( n );
return 0;
}

7 changes: 3 additions & 4 deletions program/c/src/oracle/sort/test_sort_stable.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ main( int argc,
prng_t _prng[1];
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );

int ctr = 0;
int ctr = 0;
for( int iter=0; iter<10000000; iter++ ) {
if( !ctr ) { printf( "Randomized: Completed %i iterations\n", iter ); ctr = 100000; }
ctr--;

int n = (int)(prng_uint32( prng ) % (uint32_t)(N+1)); /* In [0,N], approx uniform IID */
for( int i=0; i<n; i++ ) x[i] = (int)((prng_uint32( prng ) & UINT32_C( 0x00ff0000 )) | (uint32_t)i);
for( int i=0; i<n; i++ ) w[i] = x[i];
Expand All @@ -74,10 +74,9 @@ main( int argc,
}

prng_delete( prng_leave( prng ) );

printf( "pass\n" );
return 0;
}

#undef BEFORE

5 changes: 2 additions & 3 deletions program/c/src/oracle/sort/tmpl/sort_stable.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ SORT_IMPL(stable_node)( SORT_KEY_T * x,
SORT_KEY_T * tr = t + nl;
SORT_IDX_T nr = n - nl;
SORT_KEY_T * yr = SORT_IMPL(stable_node)( xr,nr, tr );

/* If left subsort result ended up in orig array, merge into temp
array. Otherwise, merge into orig array. */

if( yl==xl ) x = t;

/* At this point, note that yl does not overlap with the location for
merge output at this point. yr might overlap (with the right half)
with the location for merge output but this will still work in that
Expand Down Expand Up @@ -193,4 +193,3 @@ SORT_IMPL(stable)( SORT_KEY_T * key,
#undef SORT_IDX_T
#undef SORT_KEY_T
#undef SORT_NAME

1 change: 0 additions & 1 deletion program/c/src/oracle/upd_aggregate.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,3 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
#ifdef __cplusplus
}
#endif

7 changes: 3 additions & 4 deletions program/c/src/oracle/util/avg.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ avg_2_int64( int64_t x,
/* Similar considerations as above */

# if 1 /* Fewer ops but less parallel issue */
/* x+y = x+2^63 + y+2^63 - 2^64 ... exact ops
= ux + uy - 2^64 ... exact ops where ux and uy are exactly representable as a 64-bit uint
/* x+y = x+2^63 + y+2^63 - 2^64 ... exact ops
= ux + uy - 2^64 ... exact ops where ux and uy are exactly representable as a 64-bit uint
= uz + 2^64 c - 2^64 ... exact ops where uz is a 64-bit uint and c is in [0,1].
Thus, as before
uz = ux+uy ... c ops
Expand All @@ -86,7 +86,7 @@ avg_2_int64( int64_t x,
using the signed and unsigned adds are the same operation binary
in we have:
x+2^63 ... exact ops
==x+/-2^63 mod 2^64 ... exact ops
==x+/-2^63 mod 2^64 ... exact ops
==x+/-2^63 ... c ops */
uint64_t t = (uint64_t)x;
uint64_t z = t + (uint64_t)y;
Expand Down Expand Up @@ -165,4 +165,3 @@ PYTH_ORACLE_UTIL_AVG_DECL( avg_int32, int32_t )
#endif

#endif /* _pyth_oracle_util_avg_h_ */

1 change: 0 additions & 1 deletion program/c/src/oracle/util/clean
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/sh
rm -rfv bin

1 change: 0 additions & 1 deletion program/c/src/oracle/util/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ hash_inverse_uint64( uint64_t x ) {
#endif

#endif /* _pyth_oracle_util_hash_h_ */

10 changes: 5 additions & 5 deletions program/c/src/oracle/util/prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
generator. Supports parallel generation, interprocess shared memory
usage, checkpointing, random access, reversible, atomic, etc. Passes
extremely strict tests of randomness.
Assumes hash.h provides a high quality 64<>64-bit integer hash
functions (i.e. full avalanche) with the property hash_uint64(0)==0
and hash_uint64(i) for i in [0,2^64) yields a permutation of [0,2^64)
Expand Down Expand Up @@ -88,7 +88,7 @@ prng_private_contract( uint64_t seq ) {
state of a _prng and returns ownership of the underlying memory
region to the caller. There should be no joins in the system on the
prng. Returns a pointer to the underlying memory region.
FIXME: CONSIDER FLATTENING ALIGN? */

static inline uint64_t prng_footprint( void ) { return (uint64_t)sizeof ( prng_t ); }
Expand Down Expand Up @@ -143,7 +143,7 @@ prng_idx_set( prng_t * prng,
possible values of of a signed int uniform IID can be obtained by
casting the output of the unsigned generator of the same, assuming
typical twos complement arithmetic platform.)
The theory for this that hash_uint64(i) for i in [0,2^64) specifies a
random looking permutation of the integers in [0,2^64). Returning
the low order bits of this random permutation then yields a high
Expand Down Expand Up @@ -207,7 +207,7 @@ static inline int64_t prng_int64( prng_t * prng ) { return (int64_t)( prng_uint6
goes from [i/N,(i+1)/N) where i is in [0,N). For single (double)
precision, "float" ("double"), the largest N for which the range of
each interval is _exactly_ representable is N = 2^24 (2^53).
Given then a uniform IID uint32_t random input, the
prng_uint32_to_float_c0 converter output is as though an continuous
IID uniform random in [0,1) was generated and then rounded down to
Expand Down Expand Up @@ -246,7 +246,7 @@ static inline int64_t prng_int64( prng_t * prng ) { return (int64_t)( prng_uint6
Note that it is possible to make converters that will handle exactly
rounding toward all possible floating point representations in [0,1]
but this are significantly more expensive.
Assumes IEEE-754 style float and doubles.
FIXME: ADD UNIT TEST COVERAGE */
Expand Down
1 change: 0 additions & 1 deletion program/c/src/oracle/util/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,3 @@ bin/test_avg || exit 1
#bin/test_prng_battery 4 0 0 || exit 1 # BigCrush: Takes >~3 hours (seq 0, idx 0)

echo all tests passed

1 change: 0 additions & 1 deletion program/c/src/oracle/util/sar.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ sar_int64( int64_t x,
#endif

#endif /* _pyth_oracle_util_sar_h_ */

3 changes: 1 addition & 2 deletions program/c/src/oracle/util/test_align.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ main( int argc,
prng_t _prng[1];
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );

int ctr = 0;
int ctr = 0;
for( int i=0; i<1000000000; i++ ) {
if( !ctr ) { printf( "Completed %i iterations\n", i ); ctr = 10000000; }
ctr--;
Expand Down Expand Up @@ -74,4 +74,3 @@ main( int argc,

return 0;
}

7 changes: 3 additions & 4 deletions program/c/src/oracle/util/test_avg.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ main( int argc,
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );

int ctr;
ctr = 0;

ctr = 0;
for( int i=0; i<1000000000; i++ ) {
if( !ctr ) { printf( "reg: Completed %i iterations\n", i ); ctr = 10000000; }
ctr--;

# define TEST(w) do { \
uint##w##_t x = prng_uint##w( prng ); \
uint##w##_t y = prng_uint##w( prng ); \
Expand Down Expand Up @@ -140,4 +140,3 @@ main( int argc,

return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/util/test_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ main( int argc,
printf( "pass\n" );
return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/util/test_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,3 @@ main( int argc,
printf( "pass\n" );
return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/util/test_prng_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,3 @@ main( int argc,

return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/util/test_round.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ main( int argc,

return 0;
}

3 changes: 1 addition & 2 deletions program/c/src/oracle/util/test_sar.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ main( int argc,
/* FIXME: EXPLICT COVERAGE OF EDGE CASES (PROBABLY STATICALLY FULLY
SAMPLED ALREADY THOUGH FOR 8 AND 16 BIT TYPES) */

int ctr = 0;
int ctr = 0;
for( int i=0; i<1000000000; i++ ) {
if( !ctr ) { printf( "Completed %i iterations\n", i ); ctr = 10000000; }
ctr--;
Expand Down Expand Up @@ -46,4 +46,3 @@ main( int argc,

return 0;
}

1 change: 0 additions & 1 deletion program/c/src/oracle/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
#include "prng.h" /* includes stdalign.h and hash.h */

#endif /* _pyth_oracle_util_util_h_ */

1 change: 1 addition & 0 deletions program/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bindings.rs
2 changes: 1 addition & 1 deletion program/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod build_utils;
use bindgen::Builder;

fn main() {
println!("cargo:rustc-link-search=../c/target");
println!("cargo:rustc-link-search=./program/c/target");

let borsh_derives = ["BorshSerialize".to_string(), "BorshDeserialize".to_string()];

Expand Down
3 changes: 2 additions & 1 deletion program/rust/build_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ impl<'a> DeriveAdderParserCallback<'a> {
}

//this is required to implement the callback trait
impl UnwindSafe for DeriveAdderParserCallback<'_> {}
impl UnwindSafe for DeriveAdderParserCallback<'_> {
}

impl ParseCallbacks for DeriveAdderParserCallback<'_> {
fn add_derives(&self, _name: &str) -> Vec<String> {
Expand Down
5 changes: 4 additions & 1 deletion program/rust/src/c_oracle_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#![allow(unused_variables)]
#![allow(dead_code)]
//All the custom trait imports should go here
use borsh::{BorshDeserialize, BorshSerialize};
use borsh::{
BorshDeserialize,
BorshSerialize,
};
//bindings.rs is generated by build.rs to include
//things defined in bindings.h
include!("../bindings.rs");
4 changes: 2 additions & 2 deletions program/rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub type OracleResult = Result<u64, ProgramError>;
pub enum OracleError {
/// Generic catch all error
#[error("Generic")]
Generic = 600,
Generic = 600,
/// integer casting error
#[error("IntegerCastingError")]
IntegerCastingError = 601,
/// c_entrypoint returned an unexpected value
#[error("UnknownCError")]
UnknownCError = 602,
UnknownCError = 602,
}

impl From<OracleError> for ProgramError {
Expand Down
Loading

0 comments on commit f31a788

Please sign in to comment.