Skip to content

Commit aea724a

Browse files
guibescosmajabbour
andauthored
Rust entrypoint (#184)
* added-rust-entry * fixed program path * removed test dockerfile * changed c archive name + used patched Solana + update solana * removed uneeded env variables * updated build script to new c name * Add comment * Reorder for better caching * Better comment * Better hack * removed uneeded sudo commands * added comments to build scripts * more robust way to find C and Rust directories * added comments * made entry-point safe * updated liscence * restructured repo * bumped version number Co-authored-by: Mark Jabbour <[email protected]> Co-authored-by: Guillermo Bescos <guibescos>
1 parent 289e33b commit aea724a

Some content is hidden

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

41 files changed

+84
-31
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ endif()
1212
set( CMAKE_INCLUDE_CURRENT_DIR ON )
1313

1414
# find oracle header files
15-
include_directories( program/src/ )
15+
include_directories( program/c/src/ )
1616

1717
# gcc compiler/linker flags
1818
add_compile_options( -ggdb -Wall -Wextra -Wsign-conversion -Werror -Wno-deprecated-declarations -m64 )
@@ -39,7 +39,7 @@ set( PC_SRC
3939
pc/request.cpp;
4040
pc/rpc_client.cpp;
4141
pc/user.cpp;
42-
program/src/oracle/model/price_model.c
42+
program/c/src/oracle/model/price_model.c
4343
)
4444

4545
set( PC_HDR
@@ -88,7 +88,7 @@ target_link_libraries( pyth_tx ${PC_DEP} )
8888
install( TARGETS pc DESTINATION lib )
8989
install( TARGETS pyth pyth_admin pythd pyth_csv pyth_tx DESTINATION bin )
9090
install( FILES ${PC_HDR} DESTINATION include/pc )
91-
install( FILES program/src/oracle/oracle.h DESTINATION include/oracle )
91+
install( FILES program/c/src/oracle/oracle.h DESTINATION include/oracle )
9292

9393
#
9494
# test programs
@@ -147,4 +147,4 @@ function( add_bpf_lib targ )
147147
endfunction()
148148

149149
# test_oracle.c includes oracle.c
150-
add_bpf_lib( test-oracle-bpf program/src/oracle/test_oracle.c )
150+
add_bpf_lib( test-oracle-bpf program/c/src/oracle/test_oracle.c )

docker/Dockerfile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,37 @@ RUN apt-get install -qq \
2626
RUN echo "pyth ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
2727
RUN useradd -m pyth
2828

29+
# Fixes a bug in the solana docker image
30+
# https://github.com/solana-labs/solana/issues/20577
31+
RUN mkdir /usr/bin/sdk/bpf/dependencies \
32+
&& chmod 777 /usr/bin/sdk/bpf/dependencies
33+
34+
2935
USER pyth
3036
WORKDIR /home/pyth
31-
COPY --chown=pyth:pyth . pyth-client/
37+
38+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
39+
| sh -s -- -y
40+
3241

3342
RUN echo "\n\
34-
export PATH=\"\${PATH}:\${HOME}/pyth-client/build\"\n\
43+
export PATH=\"\${PATH}:\${HOME}/pyth-client/build:\${HOME}/.cargo/bin\"\n\
3544
export PYTHONPATH=\"\${PYTHONPATH:+\$PYTHONPATH:}\${HOME}/pyth-client\"\n\
3645
" >> .profile
3746

47+
COPY --chown=pyth:pyth . pyth-client/
48+
3849
# Build off-chain binaries.
3950
RUN cd pyth-client && ./scripts/build.sh
4051

41-
# Install rust and add ". ~/.cargo/env" to ~/.profile.
42-
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
43-
| sh -s -- -y --default-toolchain none
4452

4553
# Copy solana sdk and apply patch.
4654
RUN mkdir solana
4755
RUN cp -a /usr/bin/sdk solana
4856
RUN ./pyth-client/scripts/patch-solana.sh
4957

5058
# Build and test the oracle program.
51-
RUN cd pyth-client && ./scripts/build-bpf.sh program
59+
RUN cd pyth-client && ./scripts/build-bpf.sh .
5260
RUN /bin/bash -l -c "pytest-3 --pyargs pyth"
5361

5462
ENTRYPOINT []

program/c/makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OUT_DIR := ./target
2+
SOLANA := ../../../solana
3+
include $(SOLANA)/sdk/bpf/c/bpf.mk
4+
cpyth:
5+
bash -c "ar rcs target/libcpyth.a target/**/*.o"
File renamed without changes.

program/src/oracle/oracle.c renamed to program/c/src/oracle/oracle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
548548
}
549549
}
550550

551-
extern uint64_t entrypoint(const uint8_t *input)
551+
extern uint64_t c_entrypoint(const uint8_t *input)
552552
{
553553
SolAccountInfo ka[4];
554554
SolParameters prm[1];
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

program/makefile

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

program/rust/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cargo-features = ["edition2021"]
2+
3+
[package]
4+
name = "pyth-oracle"
5+
version = "2.13.1"
6+
edition = "2021"
7+
license = "Apache 2.0"
8+
publish = false
9+
10+
[dependencies]
11+
solana-program = "=1.10.29"
12+
13+
[lib]
14+
crate-type = ["cdylib", "lib"]

program/rust/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-link-search=../c/target");
3+
}

program/rust/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[link(name = "cpyth")]
2+
extern "C" {
3+
fn c_entrypoint(input: *mut u8) -> u64;
4+
}
5+
6+
#[no_mangle]
7+
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
8+
unsafe{
9+
return c_entrypoint(input);
10+
}
11+
}

pyth/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def solana_program_deploy(
191191
cmd = [
192192
'solana', 'program', 'deploy',
193193
os.path.abspath(
194-
os.path.join(__this_dir, '..', '..', 'target', 'oracle.so')
194+
os.path.join(__this_dir, '..', '..', 'target', 'deploy','pyth_oracle.so')
195195
),
196196
'--commitment', 'finalized',
197197
'--url', 'localhost',

scripts/build-bpf.sh

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,45 @@
88

99
set -eu
1010

11-
BUILD_DIR="$( cd "${1:-.}" && pwd )"
11+
PYTH_DIR=$( cd "${1:-.}" && pwd)
1212

13-
if [[ ! -f "${BUILD_DIR}/makefile" ]]
14-
then
15-
if [[ -f "${BUILD_DIR}/program/makefile" ]]
16-
then
17-
BUILD_DIR="${BUILD_DIR}/program"
18-
else
19-
>&2 echo "Not a makefile dir: ${BUILD_DIR}"
20-
exit 1
21-
fi
22-
fi
13+
#find the makefile in pyth-client
14+
#ASSUMES THAT there is only one makefile there
15+
C_DIR="$( find $PYTH_DIR | grep makefile)"
16+
C_DIR=$(dirname $C_DIR)
17+
18+
#finds Cargo.toml in pyth-client
19+
#ASSUMES THAT there is only one Cargo.toml there
20+
RUST_DIR="$( find $PYTH_DIR | grep Cargo.toml )"
21+
RUST_DIR=$(dirname $RUST_DIR)
2322

2423
if ! which cargo 2> /dev/null
2524
then
2625
# shellcheck disable=SC1090
2726
source "${CARGO_HOME:-$HOME/.cargo}/env"
2827
fi
2928

29+
3030
set -x
3131

32-
cd "${BUILD_DIR}"
32+
#build the C code and make an archive file out of it
33+
cd "${C_DIR}"
3334
export V="${V:-1}"
34-
make clean
35-
make "${@:2}"
36-
sha256sum ../target/*.so
37-
rm ../target/*-keypair.json
35+
make clean
36+
make "${@:2}"
37+
make cpyth
38+
rm ./target/*-keypair.json
39+
40+
41+
#build Rust and link it with C
42+
cd "${RUST_DIR}"
43+
cargo clean
44+
cargo build-bpf
45+
sha256sum ./target/**/*.so
46+
rm ./target/**/*-keypair.json
47+
rm -r $PYTH_DIR/target || true
48+
mv ./target $PYTH_DIR/target
49+
50+
51+
52+

0 commit comments

Comments
 (0)