Skip to content

Commit 71d2394

Browse files
authored
Run oracle program C tests from Rust (#349)
* link c tests to rust * cleanup * ok * cleanup * pr comments
1 parent 934da77 commit 71d2394

22 files changed

+156
-298
lines changed

README.md

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
# pyth-client
2-
client API for on-chain pyth programs
2+
3+
Pyth oracle program and off-chain client API.
4+
5+
## Oracle Program
6+
7+
The Pyth oracle program lives in the `program/` directory.
8+
It consists of both C and Rust code, but everything can be built and tested using `cargo`.
9+
10+
### Build Instructions
11+
12+
First, make sure you have the [solana tool suite](https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-tool)
13+
installed on your machine. (The build depends on some C makefiles that are in the tool suite.)
14+
15+
Then, simply run `cargo build` to compile the oracle program as a native binary, or `cargo build-bpf` to build a BPF binary
16+
that can be uploaded to the blockchain. This step will produce a program binary `target/deploy/pyth_oracle.so`.
17+
18+
### Testing
19+
20+
Simply run `cargo test`. This command will run several sets of tests:
21+
22+
- Unit tests of individual functions
23+
- Simulated transaction tests against the BPF binary running on a solana simulator
24+
- Exhaustive / randomized test batteries for core oracle functionality
25+
26+
Rust tests live in the `tests/` module of the rust code, and C tests are named something like `test_*.c`.
27+
The C tests are linked into the rust binary so they run as part of `cargo test` as well (see `tests/test_c_code.rs`).
28+
29+
You can also run `cargo test-bpf`, which runs the same tests as `cargo test`, though it's slightly slower and the UX is worse.
30+
31+
### pre-commit hooks
32+
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.
33+
34+
The checks are also performed in the CI to ensure the code follows consistent formatting. Formatting is only currently enforced in the `program/` directory.
35+
You might also need to install the nightly toolchain to run the formatting by running `rustup toolchain install nightly`.
36+
37+
## pythd (off-chain client API)
38+
39+
> :warning: pythd is deprecated and has been replaced by [pyth-agent](https://github.com/pyth-network/pyth-agent).
40+
> This new client is backward compatible with pythd, but more stable and configurable.
41+
42+
`pythd` provides exposes a web API for interacting with the on-chain oracle program.
343

444
### Build Instructions
545

@@ -44,30 +84,6 @@ This command runs a recent pyth-client docker image that already has the necessa
4484
Therefore, once the container is running, all you have to do is run `cd pyth-client && ./scripts/build.sh`.
4585
Note that updates to the `pyth-client` directory made inside the docker container will be persisted to the host filesystem (which is probably desirable).
4686

47-
### Local development
48-
49-
First, make sure you're building on the x86_64 architecture.
50-
On a mac, this command will switch your shell to x86_64:
51-
52-
`env /usr/bin/arch -x86_64 /bin/bash --login`
53-
54-
then in the `program/c` directory, run:
55-
56-
```
57-
make
58-
make cpyth-bpf
59-
make cpyth-native
60-
```
61-
62-
then in the `program/rust` directory, run:
63-
64-
```
65-
cargo build-bpf
66-
cargo test
67-
```
68-
69-
Note that the tests depend on the bpf build!
70-
7187
### Fuzzing
7288

7389
Build a docker image for running fuzz tests:
@@ -130,8 +146,3 @@ root@pyth-dev# usermod -u 1002 -g 1004 -s /bin/bash pyth
130146

131147
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.
132148

133-
### pre-commit hooks
134-
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.
135-
136-
The checks are also performed in the CI to ensure the code follows consistent formatting. Formatting is only currently enforced in the `program/` directory.
137-
You might also need to install the nightly toolchain to run the formatting by running `rustup toolchain install nightly`.

docker/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ RUN ./pyth-client/scripts/patch-solana.sh
6363
# Build and test the oracle program.
6464
RUN cd pyth-client && ./scripts/build-bpf.sh .
6565
RUN cd pyth-client && ./scripts/check-size.sh
66-
# Run aggregation logic tests
67-
RUN cd pyth-client && ./scripts/run-aggregation-tests.sh
6866
RUN cd pyth-client/pyth && poetry install && poetry run python -m pytest
6967

7068
ENTRYPOINT []

program/c/makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endif
1616
# The all target is defined by the solana makefile included above and generates the needed .o file.
1717
.PHONY: cpyth-bpf
1818
cpyth-bpf: all
19-
bash -c "ar rcs $(OUT_DIR)/libcpyth-bpf.a $(OUT_DIR)/**/*.o"
19+
bash -c "ar rcs $(OUT_DIR)/libcpyth-bpf.a $(OUT_DIR)/oracle/*.o"
2020

2121

2222
# 2-Stage Contract Build
@@ -27,3 +27,18 @@ cpyth-bpf: all
2727
cpyth-native:
2828
gcc -c ./src/oracle/native/upd_aggregate.c -o $(OUT_DIR)/cpyth-native.o -fPIC
2929
ar rcs $(OUT_DIR)/libcpyth-native.a $(OUT_DIR)/cpyth-native.o
30+
31+
32+
# Note: there's probably a smart way to do this with wildcards but I (jayant) can't figure it out
33+
.PHONY: test
34+
test:
35+
mkdir -p $(OUT_DIR)/test/
36+
gcc -c ./src/oracle/model/test_price_model.c -o $(OUT_DIR)/test/test_price_model.o -fPIC
37+
gcc -c ./src/oracle/sort/test_sort_stable.c -o $(OUT_DIR)/test/test_sort_stable.o -fPIC
38+
gcc -c ./src/oracle/util/test_align.c -o $(OUT_DIR)/test/test_align.o -fPIC
39+
gcc -c ./src/oracle/util/test_avg.c -o $(OUT_DIR)/test/test_avg.o -fPIC
40+
gcc -c ./src/oracle/util/test_hash.c -o $(OUT_DIR)/test/test_hash.o -fPIC
41+
gcc -c ./src/oracle/util/test_prng.c -o $(OUT_DIR)/test/test_prng.o -fPIC
42+
gcc -c ./src/oracle/util/test_round.c -o $(OUT_DIR)/test/test_round.o -fPIC
43+
gcc -c ./src/oracle/util/test_sar.c -o $(OUT_DIR)/test/test_sar.o -fPIC
44+
ar rcs $(OUT_DIR)/libcpyth-test.a $(OUT_DIR)/test/*.o

program/c/src/oracle/model/clean

Lines changed: 0 additions & 2 deletions
This file was deleted.

program/c/src/oracle/model/run_tests

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ qcmp( void const * _p,
1414
return 0;
1515
}
1616

17-
int
18-
main( int argc,
19-
char ** argv ) {
20-
(void)argc; (void)argv;
17+
int test_price_model() {
2118

2219
prng_t _prng[1];
2320
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );
@@ -29,10 +26,7 @@ main( int argc,
2926
int64_t val [3];
3027
int64_t scratch[N];
3128

32-
int ctr = 0;
3329
for( int iter=0; iter<10000000; iter++ ) {
34-
if( !ctr ) { printf( "Completed %u iterations\n", iter ); ctr = 100000; }
35-
ctr--;
3630

3731
/* Generate a random test */
3832

@@ -63,6 +57,5 @@ main( int argc,
6357

6458
prng_delete( prng_leave( prng ) );
6559

66-
printf( "pass\n" );
6760
return 0;
6861
}

program/c/src/oracle/sort/clean

Lines changed: 0 additions & 2 deletions
This file was deleted.

program/c/src/oracle/sort/run_tests

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
#define SORT_BEFORE(i,j) BEFORE(i,j)
1010
#include "tmpl/sort_stable.c"
1111

12-
int
13-
main( int argc,
14-
char ** argv ) {
15-
(void)argc; (void)argv;
12+
int test_sort_stable() {
1613

1714
# define N 96
1815
int x[N];
@@ -23,7 +20,6 @@ main( int argc,
2320
additional information in the keys to validate stability as well). */
2421

2522
for( int n=0; n<=24; n++ ) {
26-
printf( "Zero-One: Testing n=%i\n", n );
2723
for( long b=0L; b<(1L<<n); b++ ) {
2824
for( int i=0; i<n; i++ ) x[i] = (((int)((b>>i) & 1L))<<16) | i;
2925
for( int i=0; i<n; i++ ) w[i] = x[i];
@@ -49,10 +45,7 @@ main( int argc,
4945
prng_t _prng[1];
5046
prng_t * prng = prng_join( prng_new( _prng, (uint32_t)0, (uint64_t)0 ) );
5147

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

5750
int n = (int)(prng_uint32( prng ) % (uint32_t)(N+1)); /* In [0,N], approx uniform IID */
5851
for( int i=0; i<n; i++ ) x[i] = (int)((prng_uint32( prng ) & UINT32_C( 0x00ff0000 )) | (uint32_t)i);
@@ -74,8 +67,6 @@ main( int argc,
7467
}
7568

7669
prng_delete( prng_leave( prng ) );
77-
78-
printf( "pass\n" );
7970
return 0;
8071
}
8172

program/c/src/oracle/util/clean

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)