diff --git a/protocol-testing/.dockerignore b/protocol-testing/.dockerignore new file mode 100644 index 0000000..f776a6a --- /dev/null +++ b/protocol-testing/.dockerignore @@ -0,0 +1,3 @@ +*/target/ +**/target/ +**/src/ diff --git a/protocol-testing/docker-compose.yaml b/protocol-testing/docker-compose.yaml index 297bf5d..124c581 100644 --- a/protocol-testing/docker-compose.yaml +++ b/protocol-testing/docker-compose.yaml @@ -14,5 +14,20 @@ services: shm_size: "1gb" volumes: - postgres_data:/var/lib/postgresql/data + + test-runner: + build: + context: . + dockerfile: run.Dockerfile + depends_on: + - db + environment: + RUST_LOG: "info" + DATABASE_URL: postgres://postgres:mypassword@db:5432/tycho_indexer_0 + RPC_URL: "${RPC_URL}" + SUBSTREAMS_API_TOKEN: "${SUBSTREAMS_API_TOKEN}" + AUTH_API_KEY: "${AUTH_API_KEY}" + entrypoint: ["/entrypoint.sh", "${PROTOCOLS}"] + volumes: postgres_data: diff --git a/protocol-testing/entrypoint.sh b/protocol-testing/entrypoint.sh new file mode 100644 index 0000000..ecfb401 --- /dev/null +++ b/protocol-testing/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +if [ "$#" -lt 1 ]; then + echo "Usage: $0 test1 [test2 ...]" + exit 1 +fi + +for test in "$@"; do + echo "Running test: /app/substreams/$test" + tycho-protocol-sdk --package-path "/app/substreams/$test" --db-url "$DATABASE_URL" +done diff --git a/protocol-testing/run.Dockerfile b/protocol-testing/run.Dockerfile new file mode 100644 index 0000000..fe6a262 --- /dev/null +++ b/protocol-testing/run.Dockerfile @@ -0,0 +1,53 @@ +# Stage 1: Build tycho-indexer +FROM rust:1.89-bookworm AS tycho-indexer-builder + +WORKDIR /build +RUN apt-get update && apt-get install -y git + +RUN git clone --depth 1 --branch "0.82.0" https://github.com/propeller-heads/tycho-indexer.git +WORKDIR /build/tycho-indexer +RUN cargo build --release --bin tycho-indexer + +# Stage 2: Build protocol-testing and substreams +FROM rust:1.89-bookworm AS protocol-sdk-builder + +WORKDIR /build +RUN apt-get update && apt-get install -y git + +RUN git clone --depth 1 --branch "testing-sdk/ab/ENG-4985-test-with-docker" https://github.com/propeller-heads/tycho-protocol-sdk.git + +WORKDIR /build/tycho-protocol-sdk/protocol-testing +RUN cargo build --release + +WORKDIR /build/tycho-protocol-sdk/substreams +RUN cargo build --target wasm32-unknown-unknown --release + +# Stage 3: Install substreams CLI +FROM debian:bookworm AS substreams-cli-builder + +WORKDIR /build +RUN apt-get update && apt-get install -y curl + +# Download and install Substreams CLI +RUN curl -L https://github.com/streamingfast/substreams/releases/download/v1.16.4/substreams_linux_arm64.tar.gz \ + | tar -zxf - + +# Stage 4: Final image +FROM debian:bookworm + +WORKDIR /app + +RUN apt-get update && apt-get install -y ca-certificates libssl-dev libpq-dev + +# Copy binaries from previous stages +COPY --from=tycho-indexer-builder /build/tycho-indexer/target/release/tycho-indexer /usr/local/bin/tycho-indexer +COPY --from=protocol-sdk-builder /build/tycho-protocol-sdk/protocol-testing/target/release/protocol-testing /usr/local/bin/tycho-protocol-sdk +COPY --from=protocol-sdk-builder /build/tycho-protocol-sdk/substreams /app/substreams +COPY --from=protocol-sdk-builder /build/tycho-protocol-sdk/proto /app/proto +COPY --from=substreams-cli-builder /build/substreams /usr/local/bin/substreams + +# Entrypoint script to run tests +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/protocol-testing/src/main.rs b/protocol-testing/src/main.rs index 597c850..77ac316 100644 --- a/protocol-testing/src/main.rs +++ b/protocol-testing/src/main.rs @@ -6,7 +6,10 @@ mod tycho_rpc; mod tycho_runner; mod utils; +use std::path::{Path, PathBuf}; + use clap::Parser; +use miette::miette; use tracing_subscriber::EnvFilter; use crate::test_runner::TestRunner; @@ -15,8 +18,12 @@ use crate::test_runner::TestRunner; #[command(version, about = "Run indexer within a specified range of blocks")] struct Args { /// Name of the package to test - #[arg(long)] - package: String, + #[arg(long, required_unless_present = "package_path", conflicts_with = "package_path")] + package: Option, + + /// Path to the package to test + #[arg(long, required_unless_present = "package", conflicts_with = "package")] + package_path: Option, /// Enable tycho logs #[arg(long, default_value_t = true)] @@ -31,6 +38,16 @@ struct Args { vm_traces: bool, } +impl Args { + fn package_path(&self) -> miette::Result { + match (self.package_path.as_ref(), self.package.as_ref()) { + (Some(path), _) => Ok(Path::new(path).to_path_buf()), + (_, Some(package)) => Ok(Path::new("../substreams").join(package)), + _ => Err(miette!("Either package or package_path must be provided")), + } + } +} + fn main() -> miette::Result<()> { tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -39,7 +56,8 @@ fn main() -> miette::Result<()> { let args = Args::parse(); - let test_runner = TestRunner::new(args.package, args.tycho_logs, args.db_url, args.vm_traces); + let test_runner = + TestRunner::new(args.package_path()?, args.tycho_logs, args.db_url, args.vm_traces); test_runner.run_tests() } diff --git a/protocol-testing/src/test_runner.rs b/protocol-testing/src/test_runner.rs index 8336d26..f1c2d8c 100644 --- a/protocol-testing/src/test_runner.rs +++ b/protocol-testing/src/test_runner.rs @@ -46,8 +46,7 @@ pub struct TestRunner { } impl TestRunner { - pub fn new(package: String, tycho_logs: bool, db_url: String, vm_traces: bool) -> Self { - let substreams_path = PathBuf::from("../substreams").join(&package); + pub fn new(substreams_path: PathBuf, tycho_logs: bool, db_url: String, vm_traces: bool) -> Self { let repo_root = env::current_dir().expect("Failed to get current directory"); let evm_path = repo_root.join("../evm"); let adapter_contract_builder = diff --git a/substreams/rust-toolchain.toml b/substreams/rust-toolchain.toml index 15b4897..a018203 100644 --- a/substreams/rust-toolchain.toml +++ b/substreams/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.83.0" +channel = "stable" components = [ "rustfmt" ] targets = [ "wasm32-unknown-unknown" ]