From cec4e6d42c2e71e07ecac54933cd43fc2f73df8f Mon Sep 17 00:00:00 2001 From: wphan Date: Mon, 29 Sep 2025 22:35:24 -0700 Subject: [PATCH 1/4] make working devcontainer and dockerfile --- .devcontainer/Dockerfile | 56 +++++++++++++++++++++------------ .devcontainer/devcontainer.json | 44 ++++++++++++++++++++++++-- README.md | 42 +++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 22 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 2d4688614c..97523ce216 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -4,18 +4,29 @@ # is released on GitHub. # -FROM rust:1.75 +FROM --platform=linux/amd64 rust:1.70.0 ARG DEBIAN_FRONTEND=noninteractive -ARG SOLANA_CLI="1.14.7" -ARG ANCHOR_CLI="0.26.0" -ARG NODE_VERSION="v18.16.0" +ARG SOLANA_CLI="1.16.27" +ARG ANCHOR_CLI="0.29.0" +ARG NODE_VERSION="20.18.x" -ENV HOME="/root" +# Create a non-root user +ARG USERNAME=vscode +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +RUN groupadd --gid $USER_GID $USERNAME \ + && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ + && apt-get update \ + && apt-get install -y sudo \ + && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ + && chmod 0440 /etc/sudoers.d/$USERNAME + +ENV HOME="/home/$USERNAME" ENV PATH="${HOME}/.cargo/bin:${PATH}" ENV PATH="${HOME}/.local/share/solana/install/active_release/bin:${PATH}" -ENV PATH="${HOME}/.nvm/versions/node/${NODE_VERSION}/bin:${PATH}" # Install base utilities. RUN mkdir -p /workdir && mkdir -p /tmp && \ @@ -23,8 +34,7 @@ RUN mkdir -p /workdir && mkdir -p /tmp && \ build-essential git curl wget jq pkg-config python3-pip \ libssl-dev libudev-dev -RUN wget http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb -RUN dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb +# libssl1.1 is not needed for newer versions # Install rust. RUN curl "https://sh.rustup.rs" -sfo rustup.sh && \ @@ -32,23 +42,29 @@ RUN curl "https://sh.rustup.rs" -sfo rustup.sh && \ rustup component add rustfmt clippy # Install node / npm / yarn. -RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash -ENV NVM_DIR="${HOME}/.nvm" -RUN . $NVM_DIR/nvm.sh && \ - nvm install ${NODE_VERSION} && \ - nvm use ${NODE_VERSION} && \ - nvm alias default node && \ +RUN apt-get install -y nodejs npm && \ npm install -g yarn && \ - yarn add ts-mocha + npm install -g ts-mocha && \ + npm install -g typescript && \ + npm install -g mocha -# Install Solana tools. -RUN sh -c "$(curl -sSfL https://release.solana.com/v${SOLANA_CLI}/install)" +# Install Solana tools (x86_64 version). +RUN curl -sSfL https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2 | tar -xjC /tmp && \ + mv /tmp/solana-release/bin/* /usr/local/bin/ && \ + rm -rf /tmp/solana-release # Install anchor. -RUN cargo install --git https://github.com/coral-xyz/anchor avm --locked --force -RUN avm install ${ANCHOR_CLI} && avm use ${ANCHOR_CLI} +RUN cargo install --git https://github.com/coral-xyz/anchor --tag v${ANCHOR_CLI} anchor-cli --locked + +# Switch to the non-root user for the remaining setup +USER $USERNAME RUN solana-keygen new --no-bip39-passphrase +# Set up Solana config for local development +RUN solana config set --url localhost + +# Create necessary directories +RUN mkdir -p $HOME/.config/solana + WORKDIR /workdir -#be sure to add `/root/.avm/bin` to your PATH to be able to run the installed binaries diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d5aa4e718b..5b4eca3291 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,3 +1,43 @@ { - "build": { "dockerfile": "Dockerfile" }, - } \ No newline at end of file + "name": "Drift Protocol Development", + "build": { + "dockerfile": "Dockerfile", + "platform": "linux/amd64" + }, + "workspaceFolder": "/workdir", + "remoteUser": "vscode", + "mounts": [ + "source=${localWorkspaceFolder},target=/workdir,type=bind,consistency=cached", + "source=drift-target,target=/workdir/target,type=volume,consistency=delegated" + ], + "postCreateCommand": "sudo chown -R vscode:vscode /workdir/target 2>/dev/null || true && echo 'Dev container ready! You can now run: anchor build, anchor test, cargo build, etc.' && echo 'To run tests: bash test-scripts/run-anchor-tests.sh'", + "customizations": { + "vscode": { + "extensions": [ + "rust-lang.rust-analyzer", + "ms-vscode.vscode-json", + "tamasfe.even-better-toml" + ], + "settings": { + "rust-analyzer.cargo.buildScripts.enable": true, + "rust-analyzer.procMacro.enable": true, + "terminal.integrated.defaultProfile.linux": "bash" + } + } + }, + "forwardPorts": [8899, 8900], + "portsAttributes": { + "8899": { + "label": "Solana Test Validator", + "onAutoForward": "notify" + }, + "8900": { + "label": "Solana Test Validator RPC", + "onAutoForward": "notify" + } + }, + "containerEnv": { + "ANCHOR_WALLET": "/home/vscode/.config/solana/id.json", + "RUST_LOG": "solana_runtime::message_processor::stable_log=debug" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 28e340d853..0bb99c9b0a 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,48 @@ cargo test bash test-scripts/run-anchor-tests.sh ``` +# Development (with devcontainer) + +We've provided a devcontainer `Dockerfile` to help you spin up a dev environment with the correct versions of Rust, Solana, and Anchor for program development. + +Build the container and tag it `drift-dev`: +``` +cd .devcontainer && docker build -t drift-dev . +``` + +Open a shell to the container: +``` +# Find the container ID first +docker ps + +# Then exec into it +docker exec -it /bin/bash +``` + +Alternatively use an extension provided by your IDE to make use of the dev container. For example on vscode/cursor: + +``` +1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) +2. Type "Dev Containers: Reopen in Container" +3. Select it and wait for the container to build +4. The IDE terminal should be targeting the dev container now +``` + +Use the dev container as you would a local build environment: +``` +# build program +anchor build + +# update idl +anchor build -- --features anchor-test && cp target/idl/drift.json sdk/src/idl/drift.json + +# run cargo tests +cargo test + +# run typescript tests +bash test-scripts/run-anchor-tests.sh +``` + # Bug Bounty Information about the Bug Bounty can be found [here](./bug-bounty/README.md) From 55b530d435718095681f7a1dcf8f9996d887979c Mon Sep 17 00:00:00 2001 From: wphan Date: Tue, 30 Sep 2025 10:14:32 -0700 Subject: [PATCH 2/4] fix node version --- .devcontainer/Dockerfile | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 97523ce216..bae29eae9b 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -10,7 +10,7 @@ ARG DEBIAN_FRONTEND=noninteractive ARG SOLANA_CLI="1.16.27" ARG ANCHOR_CLI="0.29.0" -ARG NODE_VERSION="20.18.x" +ARG NODE_VERSION="20.18.1" # Create a non-root user ARG USERNAME=vscode @@ -31,7 +31,7 @@ ENV PATH="${HOME}/.local/share/solana/install/active_release/bin:${PATH}" # Install base utilities. RUN mkdir -p /workdir && mkdir -p /tmp && \ apt-get update -qq && apt-get upgrade -qq && apt-get install -qq \ - build-essential git curl wget jq pkg-config python3-pip \ + build-essential git curl wget jq pkg-config python3-pip xz-utils \ libssl-dev libudev-dev # libssl1.1 is not needed for newer versions @@ -41,12 +41,14 @@ RUN curl "https://sh.rustup.rs" -sfo rustup.sh && \ sh rustup.sh -y && \ rustup component add rustfmt clippy -# Install node / npm / yarn. -RUN apt-get install -y nodejs npm && \ - npm install -g yarn && \ - npm install -g ts-mocha && \ - npm install -g typescript && \ - npm install -g mocha +# Install node / npm / yarn (pinned) +RUN curl -fsSLO https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz && \ + tar -xJf node-v${NODE_VERSION}-linux-x64.tar.xz -C /usr/local --strip-components=1 && \ + rm node-v${NODE_VERSION}-linux-x64.tar.xz && \ + corepack enable && \ + npm install -g ts-mocha typescript mocha yarn + +RUN node -v && npm -v # Install Solana tools (x86_64 version). RUN curl -sSfL https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2 | tar -xjC /tmp && \ From 916112fafaf7e7fcf88ec752e4b5043e95eb758b Mon Sep 17 00:00:00 2001 From: Nour Alharithi <14929853+moosecat2@users.noreply.github.com> Date: Tue, 30 Sep 2025 19:24:03 +0000 Subject: [PATCH 3/4] run as root --- .devcontainer/Dockerfile | 96 +++++++++++++-------------------- .devcontainer/devcontainer.json | 20 +++---- 2 files changed, 43 insertions(+), 73 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index bae29eae9b..2f9b3dcf3e 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,72 +1,48 @@ # -# Docker image to generate deterministic, verifiable builds of Anchor programs. -# This must be run *after* a given ANCHOR_CLI version is published and a git tag -# is released on GitHub. +# Drift Protocol Dev Container # FROM --platform=linux/amd64 rust:1.70.0 ARG DEBIAN_FRONTEND=noninteractive - ARG SOLANA_CLI="1.16.27" ARG ANCHOR_CLI="0.29.0" ARG NODE_VERSION="20.18.1" -# Create a non-root user -ARG USERNAME=vscode -ARG USER_UID=1000 -ARG USER_GID=$USER_UID - -RUN groupadd --gid $USER_GID $USERNAME \ - && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ - && apt-get update \ - && apt-get install -y sudo \ - && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ - && chmod 0440 /etc/sudoers.d/$USERNAME - -ENV HOME="/home/$USERNAME" -ENV PATH="${HOME}/.cargo/bin:${PATH}" -ENV PATH="${HOME}/.local/share/solana/install/active_release/bin:${PATH}" - -# Install base utilities. -RUN mkdir -p /workdir && mkdir -p /tmp && \ - apt-get update -qq && apt-get upgrade -qq && apt-get install -qq \ - build-essential git curl wget jq pkg-config python3-pip xz-utils \ - libssl-dev libudev-dev - -# libssl1.1 is not needed for newer versions - -# Install rust. -RUN curl "https://sh.rustup.rs" -sfo rustup.sh && \ - sh rustup.sh -y && \ - rustup component add rustfmt clippy - -# Install node / npm / yarn (pinned) -RUN curl -fsSLO https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz && \ - tar -xJf node-v${NODE_VERSION}-linux-x64.tar.xz -C /usr/local --strip-components=1 && \ - rm node-v${NODE_VERSION}-linux-x64.tar.xz && \ - corepack enable && \ - npm install -g ts-mocha typescript mocha yarn - -RUN node -v && npm -v - -# Install Solana tools (x86_64 version). -RUN curl -sSfL https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2 | tar -xjC /tmp && \ - mv /tmp/solana-release/bin/* /usr/local/bin/ && \ - rm -rf /tmp/solana-release - -# Install anchor. -RUN cargo install --git https://github.com/coral-xyz/anchor --tag v${ANCHOR_CLI} anchor-cli --locked - -# Switch to the non-root user for the remaining setup -USER $USERNAME - -RUN solana-keygen new --no-bip39-passphrase - -# Set up Solana config for local development -RUN solana config set --url localhost - -# Create necessary directories -RUN mkdir -p $HOME/.config/solana +ENV HOME="/root" +ENV PATH="/usr/local/cargo/bin:${PATH}" +ENV PATH="/root/.local/share/solana/install/active_release/bin:${PATH}" + +RUN mkdir -p /workdir /tmp && \ + apt-get update -qq && apt-get upgrade -qq && apt-get install -y --no-install-recommends \ + build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \ + libssl-dev libudev-dev bash && \ + rm -rf /var/lib/apt/lists/* + +RUN rustup component add rustfmt clippy + +RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" -o /tmp/node.tar.xz \ + && tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \ + && rm /tmp/node.tar.xz \ + && corepack enable \ + && npm install -g ts-mocha typescript mocha \ + && node -v && npm -v && yarn -v + +# Solana CLI (x86_64 build) +RUN curl -sSfL "https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2" \ + | tar -xjC /tmp \ + && mv /tmp/solana-release/bin/* /usr/local/bin/ \ + && rm -rf /tmp/solana-release + +# Anchor CLI +RUN cargo install --git https://github.com/coral-xyz/anchor --tag "v${ANCHOR_CLI}" anchor-cli --locked + +# Set up Solana key + config for root +RUN solana-keygen new --no-bip39-passphrase --force \ + && solana config set --url localhost + +RUN apt-get update && apt-get install -y zsh curl git \ + && sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \ + && chsh -s /usr/bin/zsh vscode WORKDIR /workdir diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 5b4eca3291..d91b677b22 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,16 +1,16 @@ { - "name": "Drift Protocol Development", + "name": "Drift Protocol Development (amd64, root)", "build": { "dockerfile": "Dockerfile", "platform": "linux/amd64" }, "workspaceFolder": "/workdir", - "remoteUser": "vscode", + "remoteUser": "root", "mounts": [ "source=${localWorkspaceFolder},target=/workdir,type=bind,consistency=cached", "source=drift-target,target=/workdir/target,type=volume,consistency=delegated" ], - "postCreateCommand": "sudo chown -R vscode:vscode /workdir/target 2>/dev/null || true && echo 'Dev container ready! You can now run: anchor build, anchor test, cargo build, etc.' && echo 'To run tests: bash test-scripts/run-anchor-tests.sh'", + "postCreateCommand": "echo 'Dev container ready. Run: anchor build / anchor test / cargo build'", "customizations": { "vscode": { "extensions": [ @@ -27,17 +27,11 @@ }, "forwardPorts": [8899, 8900], "portsAttributes": { - "8899": { - "label": "Solana Test Validator", - "onAutoForward": "notify" - }, - "8900": { - "label": "Solana Test Validator RPC", - "onAutoForward": "notify" - } + "8899": { "label": "Solana Test Validator", "onAutoForward": "notify" }, + "8900": { "label": "Solana Test Validator RPC", "onAutoForward": "notify" } }, "containerEnv": { - "ANCHOR_WALLET": "/home/vscode/.config/solana/id.json", + "ANCHOR_WALLET": "/root/.config/solana/id.json", "RUST_LOG": "solana_runtime::message_processor::stable_log=debug" } -} \ No newline at end of file +} From 0bba1c9dcf8cdf32fcc274c5c2759842419b4e91 Mon Sep 17 00:00:00 2001 From: Nour Alharithi <14929853+moosecat2@users.noreply.github.com> Date: Tue, 30 Sep 2025 23:57:15 +0000 Subject: [PATCH 4/4] use final verison of devcontainer and dockerfile --- .devcontainer/Dockerfile | 5 +++-- .devcontainer/devcontainer.json | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 2f9b3dcf3e..39d1ca340f 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -19,7 +19,8 @@ RUN mkdir -p /workdir /tmp && \ libssl-dev libudev-dev bash && \ rm -rf /var/lib/apt/lists/* -RUN rustup component add rustfmt clippy +RUN rustup install 1.78.0 \ + && rustup component add rustfmt clippy --toolchain 1.78.0 RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" -o /tmp/node.tar.xz \ && tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \ @@ -43,6 +44,6 @@ RUN solana-keygen new --no-bip39-passphrase --force \ RUN apt-get update && apt-get install -y zsh curl git \ && sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \ - && chsh -s /usr/bin/zsh vscode + && chsh -s /usr/bin/zsh root WORKDIR /workdir diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d91b677b22..3228a43367 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,5 +1,5 @@ { - "name": "Drift Protocol Development (amd64, root)", + "name": "Drift Protocol Development", "build": { "dockerfile": "Dockerfile", "platform": "linux/amd64" @@ -10,7 +10,7 @@ "source=${localWorkspaceFolder},target=/workdir,type=bind,consistency=cached", "source=drift-target,target=/workdir/target,type=volume,consistency=delegated" ], - "postCreateCommand": "echo 'Dev container ready. Run: anchor build / anchor test / cargo build'", + "postCreateCommand": "yarn config set ignore-package-manager true && echo 'Dev container ready! You can now run: anchor build, anchor test, cargo build, etc.'", "customizations": { "vscode": { "extensions": [ @@ -19,19 +19,36 @@ "tamasfe.even-better-toml" ], "settings": { + "rust-analyzer.cachePriming.numThreads": 1, "rust-analyzer.cargo.buildScripts.enable": true, "rust-analyzer.procMacro.enable": true, - "terminal.integrated.defaultProfile.linux": "bash" + "rust-analyzer.checkOnSave": true, + "rust-analyzer.check.command": "clippy", + "rust-analyzer.server.extraEnv": { + "NODE_OPTIONS": "--max-old-space-size=4096", + "RUSTUP_TOOLCHAIN": "1.78.0-x86_64-unknown-linux-gnu" + }, + "editor.formatOnSave": true, + "git.ignoreLimitWarning": true } } }, - "forwardPorts": [8899, 8900], + "forwardPorts": [ + 8899, + 8900 + ], "portsAttributes": { - "8899": { "label": "Solana Test Validator", "onAutoForward": "notify" }, - "8900": { "label": "Solana Test Validator RPC", "onAutoForward": "notify" } + "8899": { + "label": "Solana Test Validator", + "onAutoForward": "notify" + }, + "8900": { + "label": "Solana Test Validator RPC", + "onAutoForward": "notify" + } }, "containerEnv": { "ANCHOR_WALLET": "/root/.config/solana/id.json", "RUST_LOG": "solana_runtime::message_processor::stable_log=debug" } -} +} \ No newline at end of file