Skip to content

Commit f31a788

Browse files
authored
Format (#215)
* 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>
1 parent 542755f commit f31a788

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+130
-71
lines changed

.github/workflows/check-fomatting.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Check Formatting
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
pre-commit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-python@v2
14+
- uses: actions-rs/toolchain@v1
15+
with:
16+
profile: minimal
17+
toolchain: nightly
18+
components: rustfmt
19+
- uses: pre-commit/[email protected]

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks:
5+
- id: trailing-whitespace
6+
files : program/
7+
- id: end-of-file-fixer
8+
files : program/
9+
- id: check-added-large-files
10+
- repo: local
11+
hooks:
12+
- id: cargo-fmt-nightly
13+
name: Cargo Fmt Nightly
14+
language: "rust"
15+
entry: cargo +nightly fmt
16+
pass_filenames: false

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"program/rust"
4+
]

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ root@pyth-dev# usermod -u 1002 -g 1004 -s /bin/bash pyth
107107
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.
108108

109109
To get best experience from C++ IntelliSense, open entire `/home/pyth` in VS Code to include `solana` directory in home for lookup directories.
110+
111+
### pre-commit hooks
112+
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.
113+
114+
The checks are also performed in the CI to ensure the code follows consistent formatting. Formatting is only currently enforced in the `program/` directory.
115+
You might also need to install the nightly toolchain to run the formatting by running `rustup toolchain install nightly`.

program/c/src/oracle/model/clean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
22
rm -rfv bin
3-

program/c/src/oracle/model/price_model.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ price_model_core( uint64_t cnt,
1717
optimized mergesort (merge with an unrolled insertion sorting
1818
network small n base cases). The best case is ~0.5 n lg n compares
1919
and the average and worst cases are ~n lg n compares.
20-
20+
2121
While not completely data oblivious, this has quite low variance in
2222
operation count practically and this is _better_ than quicksort's
2323
average case and quicksort's worst case is a computational
2424
denial-of-service and timing attack vulnerable O(n^2). Unlike
2525
quicksort, this is also stable (but this stability does not
2626
currently matter ... it might be a factor in future models).
27-
27+
2828
A data oblivious sorting network approach might be viable here with
2929
and would have a completely deterministic operations count. It
3030
currently isn't used as the best known practical approaches for
@@ -101,7 +101,7 @@ price_model_core( uint64_t cnt,
101101

102102
*_p50 = avg_2_int64( vl, vr );
103103
}
104-
104+
105105
/* Extract the p75 (this is the mirror image of the p25 case) */
106106

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

111111
return sort_quote;
112112
}
113-

program/c/src/oracle/model/run_tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ $CC test_price_model.c price_model.c -o bin/test_price_model || exit 1
1515
bin/test_price_model || exit 1
1616

1717
echo all tests passed
18-

program/c/src/oracle/model/test_price_model.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ main( int argc,
6666
printf( "pass\n" );
6767
return 0;
6868
}
69-

program/c/src/oracle/oracle.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,3 @@ extern uint64_t c_entrypoint(const uint8_t *input)
563563
}
564564
return dispatch( prm, ka );
565565
}
566-

program/c/src/oracle/oracle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313
// defines to u32 (even with ULL suffix)
1414
const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 1000ULL;
1515

16-
// The size of the "time machine" account defined in the
16+
// The size of the "time machine" account defined in the
1717
// Rust portion of the codebase.
1818
const uint64_t TIME_MACHINE_STRUCT_SIZE = 1864ULL;
1919

program/c/src/oracle/pd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,3 @@ static inline void pd_sqrt( pd_t *r, pd_t *val, const int64_t *f )
188188
#ifdef __cplusplus
189189
}
190190
#endif
191-

program/c/src/oracle/sort/clean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
22
rm -rfv bin
3-

program/c/src/oracle/sort/run_tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ $CC test_sort_stable.c -o bin/test_sort_stable || exit 1
1515
bin/test_sort_stable || exit 1
1616

1717
echo all tests passed
18-

program/c/src/oracle/sort/sort_stable_base_gen.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,3 @@ main( int argc,
9292
sort_gen( n );
9393
return 0;
9494
}
95-

program/c/src/oracle/sort/test_sort_stable.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ main( int argc,
4949
prng_t _prng[1];
5050
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );
5151

52-
int ctr = 0;
52+
int ctr = 0;
5353
for( int iter=0; iter<10000000; iter++ ) {
5454
if( !ctr ) { printf( "Randomized: Completed %i iterations\n", iter ); ctr = 100000; }
5555
ctr--;
56-
56+
5757
int n = (int)(prng_uint32( prng ) % (uint32_t)(N+1)); /* In [0,N], approx uniform IID */
5858
for( int i=0; i<n; i++ ) x[i] = (int)((prng_uint32( prng ) & UINT32_C( 0x00ff0000 )) | (uint32_t)i);
5959
for( int i=0; i<n; i++ ) w[i] = x[i];
@@ -74,10 +74,9 @@ main( int argc,
7474
}
7575

7676
prng_delete( prng_leave( prng ) );
77-
77+
7878
printf( "pass\n" );
7979
return 0;
8080
}
8181

8282
#undef BEFORE
83-

program/c/src/oracle/sort/tmpl/sort_stable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ SORT_IMPL(stable_node)( SORT_KEY_T * x,
145145
SORT_KEY_T * tr = t + nl;
146146
SORT_IDX_T nr = n - nl;
147147
SORT_KEY_T * yr = SORT_IMPL(stable_node)( xr,nr, tr );
148-
148+
149149
/* If left subsort result ended up in orig array, merge into temp
150150
array. Otherwise, merge into orig array. */
151151

152152
if( yl==xl ) x = t;
153-
153+
154154
/* At this point, note that yl does not overlap with the location for
155155
merge output at this point. yr might overlap (with the right half)
156156
with the location for merge output but this will still work in that
@@ -193,4 +193,3 @@ SORT_IMPL(stable)( SORT_KEY_T * key,
193193
#undef SORT_IDX_T
194194
#undef SORT_KEY_T
195195
#undef SORT_NAME
196-

program/c/src/oracle/upd_aggregate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,3 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
237237
#ifdef __cplusplus
238238
}
239239
#endif
240-

program/c/src/oracle/util/avg.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ avg_2_int64( int64_t x,
6666
/* Similar considerations as above */
6767

6868
# if 1 /* Fewer ops but less parallel issue */
69-
/* x+y = x+2^63 + y+2^63 - 2^64 ... exact ops
70-
= ux + uy - 2^64 ... exact ops where ux and uy are exactly representable as a 64-bit uint
69+
/* x+y = x+2^63 + y+2^63 - 2^64 ... exact ops
70+
= ux + uy - 2^64 ... exact ops where ux and uy are exactly representable as a 64-bit uint
7171
= uz + 2^64 c - 2^64 ... exact ops where uz is a 64-bit uint and c is in [0,1].
7272
Thus, as before
7373
uz = ux+uy ... c ops
@@ -86,7 +86,7 @@ avg_2_int64( int64_t x,
8686
using the signed and unsigned adds are the same operation binary
8787
in we have:
8888
x+2^63 ... exact ops
89-
==x+/-2^63 mod 2^64 ... exact ops
89+
==x+/-2^63 mod 2^64 ... exact ops
9090
==x+/-2^63 ... c ops */
9191
uint64_t t = (uint64_t)x;
9292
uint64_t z = t + (uint64_t)y;
@@ -165,4 +165,3 @@ PYTH_ORACLE_UTIL_AVG_DECL( avg_int32, int32_t )
165165
#endif
166166

167167
#endif /* _pyth_oracle_util_avg_h_ */
168-

program/c/src/oracle/util/clean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/sh
22
rm -rfv bin
3-

program/c/src/oracle/util/hash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,3 @@ hash_inverse_uint64( uint64_t x ) {
7171
#endif
7272

7373
#endif /* _pyth_oracle_util_hash_h_ */
74-

program/c/src/oracle/util/prng.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
generator. Supports parallel generation, interprocess shared memory
66
usage, checkpointing, random access, reversible, atomic, etc. Passes
77
extremely strict tests of randomness.
8-
8+
99
Assumes hash.h provides a high quality 64<>64-bit integer hash
1010
functions (i.e. full avalanche) with the property hash_uint64(0)==0
1111
and hash_uint64(i) for i in [0,2^64) yields a permutation of [0,2^64)
@@ -88,7 +88,7 @@ prng_private_contract( uint64_t seq ) {
8888
state of a _prng and returns ownership of the underlying memory
8989
region to the caller. There should be no joins in the system on the
9090
prng. Returns a pointer to the underlying memory region.
91-
91+
9292
FIXME: CONSIDER FLATTENING ALIGN? */
9393

9494
static inline uint64_t prng_footprint( void ) { return (uint64_t)sizeof ( prng_t ); }
@@ -143,7 +143,7 @@ prng_idx_set( prng_t * prng,
143143
possible values of of a signed int uniform IID can be obtained by
144144
casting the output of the unsigned generator of the same, assuming
145145
typical twos complement arithmetic platform.)
146-
146+
147147
The theory for this that hash_uint64(i) for i in [0,2^64) specifies a
148148
random looking permutation of the integers in [0,2^64). Returning
149149
the low order bits of this random permutation then yields a high
@@ -207,7 +207,7 @@ static inline int64_t prng_int64( prng_t * prng ) { return (int64_t)( prng_uint6
207207
goes from [i/N,(i+1)/N) where i is in [0,N). For single (double)
208208
precision, "float" ("double"), the largest N for which the range of
209209
each interval is _exactly_ representable is N = 2^24 (2^53).
210-
210+
211211
Given then a uniform IID uint32_t random input, the
212212
prng_uint32_to_float_c0 converter output is as though an continuous
213213
IID uniform random in [0,1) was generated and then rounded down to
@@ -246,7 +246,7 @@ static inline int64_t prng_int64( prng_t * prng ) { return (int64_t)( prng_uint6
246246
Note that it is possible to make converters that will handle exactly
247247
rounding toward all possible floating point representations in [0,1]
248248
but this are significantly more expensive.
249-
249+
250250
Assumes IEEE-754 style float and doubles.
251251
252252
FIXME: ADD UNIT TEST COVERAGE */

program/c/src/oracle/util/run_tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,3 @@ bin/test_avg || exit 1
5252
#bin/test_prng_battery 4 0 0 || exit 1 # BigCrush: Takes >~3 hours (seq 0, idx 0)
5353

5454
echo all tests passed
55-

program/c/src/oracle/util/sar.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,3 @@ sar_int64( int64_t x,
7171
#endif
7272

7373
#endif /* _pyth_oracle_util_sar_h_ */
74-

program/c/src/oracle/util/test_align.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ main( int argc,
1717
prng_t _prng[1];
1818
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );
1919

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

7575
return 0;
7676
}
77-

program/c/src/oracle/util/test_avg.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ main( int argc,
1010
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );
1111

1212
int ctr;
13-
14-
ctr = 0;
13+
14+
ctr = 0;
1515
for( int i=0; i<1000000000; i++ ) {
1616
if( !ctr ) { printf( "reg: Completed %i iterations\n", i ); ctr = 10000000; }
1717
ctr--;
18-
18+
1919
# define TEST(w) do { \
2020
uint##w##_t x = prng_uint##w( prng ); \
2121
uint##w##_t y = prng_uint##w( prng ); \
@@ -140,4 +140,3 @@ main( int argc,
140140

141141
return 0;
142142
}
143-

program/c/src/oracle/util/test_hash.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@ main( int argc,
5656
printf( "pass\n" );
5757
return 0;
5858
}
59-

program/c/src/oracle/util/test_prng.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,3 @@ main( int argc,
174174
printf( "pass\n" );
175175
return 0;
176176
}
177-

program/c/src/oracle/util/test_prng_battery.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,3 @@ main( int argc,
8989

9090
return 0;
9191
}
92-

program/c/src/oracle/util/test_round.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ main( int argc,
2828

2929
return 0;
3030
}
31-

program/c/src/oracle/util/test_sar.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ main( int argc,
1212
/* FIXME: EXPLICT COVERAGE OF EDGE CASES (PROBABLY STATICALLY FULLY
1313
SAMPLED ALREADY THOUGH FOR 8 AND 16 BIT TYPES) */
1414

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

4747
return 0;
4848
}
49-

program/c/src/oracle/util/util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@
88
#include "prng.h" /* includes stdalign.h and hash.h */
99

1010
#endif /* _pyth_oracle_util_util_h_ */
11-

program/rust/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bindings.rs

program/rust/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod build_utils;
22
use bindgen::Builder;
33

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

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

program/rust/build_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ impl<'a> DeriveAdderParserCallback<'a> {
2424
}
2525

2626
//this is required to implement the callback trait
27-
impl UnwindSafe for DeriveAdderParserCallback<'_> {}
27+
impl UnwindSafe for DeriveAdderParserCallback<'_> {
28+
}
2829

2930
impl ParseCallbacks for DeriveAdderParserCallback<'_> {
3031
fn add_derives(&self, _name: &str) -> Vec<String> {

program/rust/src/c_oracle_header.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#![allow(unused_variables)]
66
#![allow(dead_code)]
77
//All the custom trait imports should go here
8-
use borsh::{BorshDeserialize, BorshSerialize};
8+
use borsh::{
9+
BorshDeserialize,
10+
BorshSerialize,
11+
};
912
//bindings.rs is generated by build.rs to include
1013
//things defined in bindings.h
1114
include!("../bindings.rs");

program/rust/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ pub type OracleResult = Result<u64, ProgramError>;
1010
pub enum OracleError {
1111
/// Generic catch all error
1212
#[error("Generic")]
13-
Generic = 600,
13+
Generic = 600,
1414
/// integer casting error
1515
#[error("IntegerCastingError")]
1616
IntegerCastingError = 601,
1717
/// c_entrypoint returned an unexpected value
1818
#[error("UnknownCError")]
19-
UnknownCError = 602,
19+
UnknownCError = 602,
2020
}
2121

2222
impl From<OracleError> for ProgramError {

0 commit comments

Comments
 (0)