From e04d15eb858c69fe8e02b3e67d928bb121872f86 Mon Sep 17 00:00:00 2001 From: Gorka Ludlow Date: Thu, 16 May 2024 15:56:04 -0300 Subject: [PATCH] feat: isolate components (#81) * feat: add nvmrc file to trigger use of expected node version * feat: update cmds to be able to run in macos * feat: add cmds and dockerfile for local blockchain * feat: add cmds and dockerfile for solver * feat: add cmds and dockerfile for job creator * [wip] feat: add cmds and dockerfile for resource provider * [wip] feat: add cmds and dockerfile for bacalhau * [wip] feat: update cmd for mediator * [wip] feat: update cmd for integration tests * feat: add user to hardhat accounts * feat: simplify stack * feat: add deploy scripts --- .github/workflows/devnet_deploy.yml | 147 ++++++++ .github/workflows/devnet_deploy_chain.yml | 69 ++++ .nvmrc | 1 + docker/bacalhau/Dockerfile | 24 ++ docker/chain/Dockerfile | 63 ++++ docker/job-creator/Dockerfile | 18 + docker/resource-provider/Dockerfile | 27 ++ docker/solver/Dockerfile | 32 ++ hardhat/utils/accounts.ts | 5 + stack | 423 +++++++++++----------- 10 files changed, 590 insertions(+), 219 deletions(-) create mode 100644 .github/workflows/devnet_deploy.yml create mode 100644 .github/workflows/devnet_deploy_chain.yml create mode 100644 .nvmrc create mode 100644 docker/bacalhau/Dockerfile create mode 100644 docker/chain/Dockerfile create mode 100644 docker/job-creator/Dockerfile create mode 100644 docker/resource-provider/Dockerfile create mode 100644 docker/solver/Dockerfile diff --git a/.github/workflows/devnet_deploy.yml b/.github/workflows/devnet_deploy.yml new file mode 100644 index 00000000..9ec17271 --- /dev/null +++ b/.github/workflows/devnet_deploy.yml @@ -0,0 +1,147 @@ +name: Deploy Devnet + +on: + push: + branches: + - main + - gorka/isolate-components + +jobs: + contracts: + runs-on: "ubuntu-latest" + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + context: app + + - name: Compile and deploy contracts + id: compile-and-deploy-contracts + env: + WEB3_RPC_URL: ${{ secrets.WEB3_RPC_URL }} + run: | + echo "# TODO" + + solver-build-deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + context: app + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + mask-password: 'true' + + - name: Solver build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_SOLVER: ${{ secrets.ECR_REPOSITORY_SOLVER }} + CLOUDFLARE_TOKEN_SOLVER: ${{ secrets.CLOUDFLARE_TOKEN_SOLVER }} + run: | + docker build \ + -t $ECR_REPOSITORY_SOLVER \ + -f ./docker/solver/Dockerfile \ + --build-arg="expose_via=cloudflare" \ + --build-arg="cloudflare_token=${CLOUDFLARE_TOKEN_SOLVER}"\ + --build-arg="doppler_config=devnet" \ + . + docker tag $ECR_REPOSITORY_SOLVER:latest $ECR_REGISTRY/$ECR_REPOSITORY_SOLVER:latest + docker push $ECR_REGISTRY/$ECR_REPOSITORY_SOLVER:latest + + - name: Solver deploy to EC2 instance + uses: appleboy/ssh-action@master + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_SOLVER: ${{ secrets.ECR_REPOSITORY_SOLVER }} + DOPPLER_TOKEN_SOLVER: ${{ secrets.DOPPLER_TOKEN_SOLVER }} + with: + host: ${{ secrets.EC2_HOST_SOLVER }} + username: ${{ secrets.EC2_USERNAME_SOLVER }} + key: ${{ secrets.EC2_PRIVATE_KEY_SOLVER }} + envs: ECR_REGISTRY, ECR_REPOSITORY_SOLVER, DOPPLER_TOKEN_SOLVER + script_stop: true + script: | + docker stop solver || true + docker rm solver || true + aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_REGISTRY + docker system prune -af + docker pull $ECR_REGISTRY/$ECR_REPOSITORY_SOLVER:latest + docker run \ + -d \ + --restart always \ + --name solver \ + -e DOPPLER_TOKEN=$DOPPLER_TOKEN_SOLVER \ + $ECR_REGISTRY/$ECR_REPOSITORY_SOLVER:latest + + job-creator-build-deploy: + needs: [solver-build-deploy] + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + context: app + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + mask-password: 'true' + + - name: Job creator build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_JOB_CREATOR: ${{ secrets.ECR_REPOSITORY_JOB_CREATOR }} + run: | + docker build \ + -t $ECR_REPOSITORY_JOB_CREATOR \ + -f ./docker/job-creator/Dockerfile \ + --build-arg doppler_config=devnet \ + . + docker tag $ECR_REPOSITORY_JOB_CREATOR:latest $ECR_REGISTRY/$ECR_REPOSITORY_JOB_CREATOR:latest + docker push $ECR_REGISTRY/$ECR_REPOSITORY_JOB_CREATOR:latest + + - name: Job creator deploy to EC2 instance + uses: appleboy/ssh-action@master + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_JOB_CREATOR: ${{ secrets.ECR_REPOSITORY_JOB_CREATOR }} + DOPPLER_TOKEN_JOB_CREATOR: ${{ secrets.DOPPLER_TOKEN_JOB_CREATOR }} + with: + host: ${{ secrets.EC2_HOST_JOB_CREATOR }} + username: ${{ secrets.EC2_USERNAME_JOB_CREATOR }} + key: ${{ secrets.EC2_PRIVATE_KEY_JOB_CREATOR }} + envs: ECR_REGISTRY, ECR_REPOSITORY_JOB_CREATOR, DOPPLER_TOKEN_JOB_CREATOR + script_stop: true + script: | + docker stop job-creator || true + docker rm job-creator || true + aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_REGISTRY + docker system prune -af + docker pull $ECR_REGISTRY/$ECR_REPOSITORY_JOB_CREATOR:latest + docker run \ + -d \ + --restart always \ + --name job-creator \ + -e DOPPLER_TOKEN=$DOPPLER_TOKEN_JOB_CREATOR \ + $ECR_REGISTRY/$ECR_REPOSITORY_JOB_CREATOR:latest diff --git a/.github/workflows/devnet_deploy_chain.yml b/.github/workflows/devnet_deploy_chain.yml new file mode 100644 index 00000000..460160f3 --- /dev/null +++ b/.github/workflows/devnet_deploy_chain.yml @@ -0,0 +1,69 @@ +name: Deploy Devnet chain + +on: workflow_dispatch + +jobs: + chain-build-deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + context: app + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + mask-password: 'true' + + - name: Chain build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_CHAIN: ${{ secrets.ECR_REPOSITORY_CHAIN }} + CLOUDFLARE_TOKEN_HTTP: ${{ secrets.CLOUDFLARE_TOKEN_HTTP }} + CLOUDFLARE_TOKEN_WS: ${{ secrets.CLOUDFLARE_TOKEN_WS }} + ADMIN_ADDRESS: ${{ secrets.ADMIN_ADDRESS }} + run: | + docker build \ + -t $ECR_REPOSITORY_CHAIN \ + -f ./docker/chain/Dockerfile \ + --build-arg="admin_address=${ADMIN_ADDRESS}" \ + --build-arg="expose_via=cloudflare" \ + --build-arg="cloudflare_token_http=${CLOUDFLARE_TOKEN_HTTP}"\ + --build-arg="cloudflare_token_ws=${CLOUDFLARE_TOKEN_WS}"\ + . + docker tag $ECR_REPOSITORY_CHAIN:latest $ECR_REGISTRY/$ECR_REPOSITORY_CHAIN:latest + docker push $ECR_REGISTRY/$ECR_REPOSITORY_CHAIN:latest + + - name: Chain deploy to EC2 instance + uses: appleboy/ssh-action@master + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY_CHAIN: ${{ secrets.ECR_REPOSITORY_CHAIN }} + with: + host: ${{ secrets.EC2_HOST_CHAIN }} + username: ${{ secrets.EC2_USERNAME_CHAIN }} + key: ${{ secrets.EC2_PRIVATE_KEY_CHAIN }} + envs: ECR_REGISTRY, ECR_REPOSITORY_CHAIN + script_stop: true + script: | + docker stop chain || true + docker rm chain || true + aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_REGISTRY + docker system prune -af + docker pull $ECR_REGISTRY/$ECR_REPOSITORY_CHAIN:latest + docker run \ + -d \ + --restart always \ + --name chain \ + -v /data/chain:/data/chain \ + $ECR_REGISTRY/$ECR_REPOSITORY_CHAIN:latest diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..209e3ef4 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +20 diff --git a/docker/bacalhau/Dockerfile b/docker/bacalhau/Dockerfile new file mode 100644 index 00000000..245bcebe --- /dev/null +++ b/docker/bacalhau/Dockerfile @@ -0,0 +1,24 @@ +FROM docker:dind + +ADD https://github.com/bacalhau-project/bacalhau/releases/download/v1.3.0/bacalhau_v1.3.0_linux_amd64.tar.gz . +RUN tar xfv bacalhau_v1.3.0_linux_amd64.tar.gz +RUN mv bacalhau /usr/local/bin + +COPY ./do . +#COPY ./stack . +#COPY ./docker/bacalhau/Dockerfile . + +CMD ["/bin/sh", "./do"] + +# TODO +# contents of do: +# # wait for docker to initialize +# while ! docker -v; do sleep 1; done + +# export BACALHAU_SERVE_IPFS_PATH=/tmp/lilypad/data/ipfs +# TODO +# check if this is needed +# export BACALHAU_API_HOST=localhost +# export LOG_LEVEL=debug + +# bacalhau serve --node-type compute,requester --peer none --private-internal-ipfs=false --job-selection-accept-networked --ipfs-api-listen-addresses=/ip4/0.0.0.0/tcp/0 diff --git a/docker/chain/Dockerfile b/docker/chain/Dockerfile new file mode 100644 index 00000000..ffca2460 --- /dev/null +++ b/docker/chain/Dockerfile @@ -0,0 +1,63 @@ +ARG expose_via=local + +FROM golang:1.22.3 AS base + +ARG arch=amd64 +ARG cloudflare_token_http="not-a-token" +ARG cloudflare_token_ws="not-a-token" +ARG geth_version=v1.13.5 + +WORKDIR /geth + +RUN git clone --quiet --branch ${geth_version} --depth 1 https://github.com/ethereum/go-ethereum . +RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build go run build/ci.go install -static ./cmd/geth + +RUN /geth/build/bin/geth version +RUN mv /geth/build/bin/geth /usr/local/bin/ + +ARG admin_address="0x0" + +RUN touch fund-admin +RUN echo "#!/bin/bash" >> fund-admin +RUN echo "geth --exec \"eth.sendTransaction({from: eth.coinbase, to: \\\"${admin_address}\\\", value: new web3.BigNumber(eth.getBalance(eth.coinbase)).minus(web3.toWei(1, \\\"ether\\\")) })\" attach /data/chain/geth.ipc" >> fund-admin +RUN chmod +x fund-admin + +RUN touch run-node +RUN echo "#!/bin/bash" >> run-node +RUN echo "geth --datadir /data/chain --dev --ws --ws.api web3,eth,net --ws.addr 0.0.0.0 --ws.port 8546 --ws.origins '*' --http --http.api web3,eth,net --http.addr 0.0.0.0 --http.corsdomain '*' --http.port 8545 --http.vhosts '*' &" >> run-node +RUN chmod +x run-node + +RUN touch reset +RUN echo "#!/bin/bash" >> reset +RUN echo "echo '- Kill geth'" >> reset +RUN echo "pkill -INT geth" >> reset +RUN echo "sleep 5" >> reset +RUN echo "echo '- Clear data'" >> reset +RUN echo "rm -rf /data/chain/*" >> reset +RUN echo "echo '- Restart geth'" >> reset +RUN echo "./run-node" >> reset +RUN echo "sleep 5" >> reset +RUN echo "echo '- Fund admin'" >> reset +RUN echo "./fund-admin" >> reset +RUN echo "echo '- Done'" >> reset +RUN chmod +x reset + +RUN touch run +RUN echo "#!/bin/bash" >> run + +FROM base AS expose-cloudflare +RUN curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${arch}.deb +RUN dpkg -i cloudflared.deb +RUN echo "cloudflared tunnel --metrics 0.0.0.0:11111 run --token $cloudflare_token_http --url http://localhost:8545 &" >> run +RUN echo "cloudflared tunnel --metrics 0.0.0.0:11112 run --token $cloudflare_token_ws --url http://localhost:8546 &" >> run + +FROM base AS expose-local +EXPOSE 8545 +EXPOSE 8546 + +FROM expose-$expose_via AS final +RUN echo "./run-node" >> run +RUN echo "sleep infinity" >> run +RUN chmod +x run + +CMD ["/bin/bash", "./run"] diff --git a/docker/job-creator/Dockerfile b/docker/job-creator/Dockerfile new file mode 100644 index 00000000..af3af1a5 --- /dev/null +++ b/docker/job-creator/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:latest as base +WORKDIR /usr/src/app + +ARG doppler_config=dev + +COPY . . +RUN go mod download && go mod verify +RUN go build -v . +RUN go install + +RUN (curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh || wget -t 3 -qO- https://cli.doppler.com/install.sh) | sh + +RUN touch run +RUN echo "#!/bin/bash" >> run +RUN echo "doppler run -p job-creator -c $doppler_config -- lilypad jobcreator" >> run +RUN chmod +x run + +CMD ["/bin/bash", "./run"] diff --git a/docker/resource-provider/Dockerfile b/docker/resource-provider/Dockerfile new file mode 100644 index 00000000..d174caef --- /dev/null +++ b/docker/resource-provider/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:latest as base +WORKDIR /usr/src/app + +ARG doppler_config=dev +ARG DOPPLER_TOKEN_BACALHAU +ARG DOPPLER_TOKEN_RESOURCE_PROVIDER + +COPY . . +RUN go mod download && go mod verify +RUN go build -v . +RUN go install + +RUN (curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh || wget -t 3 -qO- https://cli.doppler.com/install.sh) | sh + +# RUN wget https://github.com/bacalhau-project/bacalhau/releases/download/v1.0.3/bacalhau_v1.0.3_linux_amd64.tar.gz +# RUN tar xfv bacalhau_v1.0.3_linux_amd64.tar.gz +# RUN mv bacalhau /usr/local/bin +# # RUN sysctl -w net.core.rmem_max=7500000 +# # RUN sysctl -w net.core.wmem_max=7500000 + +RUN touch run +RUN echo "#!/bin/bash" >> run +#RUN echo "doppler run --token=$DOPPLER_TOKEN_BACALHAU -p bacalhau -c $doppler_config -- ./stack bacalhau-serve &" >> run +RUN echo "doppler run --token=$DOPPLER_TOKEN_RESOURCE_PROVIDER -p resource-provider -c $doppler_config -- lilypad resource-provider" >> run +RUN chmod +x run + +CMD ["/bin/bash", "./run"] diff --git a/docker/solver/Dockerfile b/docker/solver/Dockerfile new file mode 100644 index 00000000..b1a76d88 --- /dev/null +++ b/docker/solver/Dockerfile @@ -0,0 +1,32 @@ +ARG expose_via=local + +FROM golang:latest as base +WORKDIR /usr/src/app + +ARG arch=amd64 +ARG doppler_config=dev +ARG cloudflare_token="not-a-token" + +COPY . . +RUN go mod download && go mod verify +RUN go build -v . +RUN go install + +RUN (curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh || wget -t 3 -qO- https://cli.doppler.com/install.sh) | sh + +RUN touch run +RUN echo "#!/bin/bash" >> run + +FROM base AS expose-cloudflare +RUN curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${arch}.deb +RUN dpkg -i cloudflared.deb +RUN echo "cloudflared tunnel --metrics 0.0.0.0:11113 run --token $cloudflare_token --url http://localhost:8080 &" >> run + +FROM base AS expose-local +EXPOSE 8080 + +FROM expose-$expose_via AS FINAL +RUN echo "doppler run -p solver -c $doppler_config -- lilypad solver" >> run +RUN chmod +x run + +CMD ["/bin/bash", "./run"] diff --git a/hardhat/utils/accounts.ts b/hardhat/utils/accounts.ts index c63d817c..a91b91d8 100644 --- a/hardhat/utils/accounts.ts +++ b/hardhat/utils/accounts.ts @@ -55,6 +55,11 @@ export const ACCOUNTS: Account[] = [{ address: loadAddress('directory', '0x976EA74026E726554dB657fA54763abd0C3a0aa9'), privateKey: loadPrivateKey('directory', '0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e'), metadata: {}, +}, { + name: 'user', + address: loadAddress('user', '0x1da99b9e884C9e7B15361957577978c1fa66AfBb'), + privateKey: loadPrivateKey('user', 'b3994e7660abe5f65f729bb64163c6cd6b7d0b1a8c67881a7346e3e8c7f026f5'), + metadata: {}, }] // map of account name -> account diff --git a/stack b/stack index 3cfac904..7ec4b62e 100755 --- a/stack +++ b/stack @@ -2,58 +2,79 @@ set -euo pipefail IFS=$'\n\t' -export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -export DEFAULT_DATA_DIR="/data/geth" -export DATA_DIRECTORY=${DATA_DIRECTORY:="$DEFAULT_DATA_DIR"} -export NETWORK=${NETWORK:="geth"} +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +OS_ARCH=$(uname -m | awk '{if ($0 ~ /arm64|aarch64/) print "arm64"; else if ($0 ~ /x86_64|amd64/) print "amd64"; else print "unsupported_arch"}') -# if "$DIR/.env" exists then source it -if [[ -f "$DIR/.env" ]]; then - source "$DIR/.env" -fi - -############################################################################ -############################################################################ -# geth ############################################################################ +# chain ############################################################################ -# run a local geth node in dev mode mounted against a local data directory -function geth() { - docker network ls | grep lilypad || docker network create lilypad - docker run -d --restart always \ - --name geth \ +function chain-docker-build() { + docker build \ + -t chain \ + -f ./docker/chain/Dockerfile \ + --build-arg arch=${OS_ARCH} \ + --build-arg admin_address=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \ + . +} + +function chain-docker-run() { + DATA_DIR=${@:-/data/chain} + + docker run \ + --rm \ + --name chain \ --network lilypad \ + --add-host localhost:host-gateway \ -p 8545:8545 \ -p 8546:8546 \ - -v ${DATA_DIRECTORY}:/data/geth \ - ethereum/client-go:v1.13.5 \ - --datadir /data/geth \ - --dev \ - --ws \ - --ws.api web3,eth,net \ - --ws.addr 0.0.0.0 \ - --ws.port 8546 \ - --ws.origins '*' \ - --http \ - --http.api web3,eth,net \ - --http.addr 0.0.0.0 \ - --http.corsdomain '*' \ - --http.port 8545 \ - --http.vhosts '*' -} - -function geth-command() { - docker exec -i geth geth --exec "$@" attach /data/geth/geth.ipc + -v ${DATA_DIR}:/data/chain \ + chain +} + +function chain-boot() { + echo "- Funding services with ether" + fund-services-ether + echo "- Funding services with ether" + deploy +} + +function deploy() { + clean-deploy + echo "- Compiling contracts" + compile-contracts + echo "- Deploying contracts" + deploy-contracts + echo "- Funding services with tokens" + fund-services-tokens + echo "- Printing balances" + balances +} + + +function chain-fund-admin() { + docker exec -i chain geth --exec "eth.sendTransaction({from: eth.coinbase, to: \"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266\", value: new web3.BigNumber(eth.getBalance(eth.coinbase)).minus(web3.toWei(1, \"ether\")) })" attach /data/chain/geth.ipc +} + +function clean() { + DATA_DIR=${@:-/data/chain} + + docker rm -f $(docker ps -aq) || true + rm -rf ${DATA_DIR}/* + clean-deploy +} + +function clean-deploy() { + rm -rf $(pwd)/hardhat/artifacts + rm -rf $(pwd)/hardhat/cache + rm -rf $(pwd)/hardhat/deployments/geth } -############################################################################ ############################################################################ # faucet ############################################################################ -############################################################################ -# fun the faucet container +# TODO function faucet() { source .env eval $(./stack print-contract-env) @@ -74,90 +95,44 @@ function faucet() { ############################################################################ -############################################################################ -# bacalhau -############################################################################ +# hardhat ############################################################################ -function bacalhau-serve() { - bacalhau serve \ - --node-type compute,requester \ - --peer none \ - --private-internal-ipfs=false \ - --job-selection-accept-networked +function compile-contracts() { + ( + set -euo pipefail + cd hardhat + npx hardhat compile + ) + go-bindings } -# move ALL the money apart from 1 eth to the given admin account -# this expects the .env file to already have been created -function fund-admin() { - if [[ -z "$ADMIN_ADDRESS" ]]; then - echo >&2 "ADMIN_ADDRESS must be set (source ${DIR}/.env)" - exit 1 +function go-bindings() { + # check if the lilypad-solc image exists + # and only build it if it doesn't + if [[ -z $(docker images -q lilypad-solc) ]]; then + docker build -t lilypad-solc hardhat/solc fi - geth-command "eth.sendTransaction({from: eth.coinbase, to: \"${ADMIN_ADDRESS}\", value: new web3.BigNumber(eth.getBalance(eth.coinbase)).minus(web3.toWei(1, \"ether\")) })" -} - -function geth-stop() { - docker rm -f geth 2> /dev/null || true -} - -function clean-deploy() { - rm -rf ${DIR}/hardhat/artifacts - rm -rf ${DIR}/hardhat/cache - rm -rf ${DIR}/hardhat/deployments/geth -} - -function clean() { - docker rm -f $(docker ps -aq) || true - sudo rm -rf ${DATA_DIRECTORY} - clean-deploy -} - -function boot-message() { - echo "" - echo "############################################################################" - echo "# $@" - echo "############################################################################" - echo "" -} - -function deploy() { - clean-deploy - boot-message "Compiling contracts" - compile-contracts - boot-message "Deploying contracts" - deploy-contracts - boot-message "Funding services with tokens" - fund-services-tokens - boot-message "Printing balances" - balances -} + rm -rf pkg/web3/bindings + mkdir -p pkg/web3/bindings + go-binding LilypadToken token + go-binding LilypadPayments payments + go-binding LilypadStorage storage + go-binding LilypadUsers users + go-binding LilypadMediationRandom mediation + go-binding LilypadOnChainJobCreator jobcreator + go-binding LilypadController controller -function boot() { - clean - boot-message "Starting geth" - geth - sleep 5 - boot-message "Funding admin account" - fund-admin - boot-message "Funding services with ether" - fund-services-ether - deploy + echo "- Generated all go bindings pkg/contract/bindings/" } -############################################################################ -############################################################################ -# hardhat -############################################################################ -############################################################################ - function go-binding() { local name="$1" local pkg="$2" # compile the sol files into bytecode and ABI docker run --rm \ - -v $DIR/hardhat:/src \ + -v $(pwd)/hardhat:/src \ -w /src \ --entrypoint solc \ lilypad-solc \ @@ -168,12 +143,12 @@ function go-binding() { "contracts/$name.sol" \ -o artifacts - sudo chown -R $USER:$USER hardhat/artifacts + chown -R $USER hardhat/artifacts mkdir -p hardhat/artifacts/bindings/$pkg # generate the go bindings docker run --rm \ - -v $DIR/hardhat:/src \ + -v $(pwd)/hardhat:/src \ -w /src \ --entrypoint abigen \ lilypad-solc \ @@ -181,49 +156,25 @@ function go-binding() { "--abi=artifacts/$name.abi" \ "--pkg=$pkg" "--out=artifacts/bindings/$pkg/$pkg.go" - sudo chown -R $USER:$USER hardhat/artifacts/bindings/$pkg - sudo chmod 0644 hardhat/artifacts/bindings/$pkg/$pkg.go + chown -R $USER hardhat/artifacts/bindings/$pkg + chmod 0644 hardhat/artifacts/bindings/$pkg/$pkg.go cp -r hardhat/artifacts/bindings/$pkg pkg/web3/bindings/$pkg - echo "Generated go binding hardhat/artifacts/bindings/$pkg/$pkg.go" -} - -function go-bindings() { - # check if the lilypad-solc image exists - # and only build it if it doesn't - if [[ -z $(docker images -q lilypad-solc) ]]; then - docker build -t lilypad-solc hardhat/solc - fi - rm -rf pkg/web3/bindings - mkdir -p pkg/web3/bindings - go-binding LilypadToken token - go-binding LilypadPayments payments - go-binding LilypadStorage storage - go-binding LilypadUsers users - go-binding LilypadMediationRandom mediation - go-binding LilypadOnChainJobCreator jobcreator - go-binding LilypadController controller - - echo "Generated all go bindings pkg/contract/bindings/" -} - -function compile-contracts() { - ( - set -euo pipefail - cd hardhat - npx hardhat compile - ) - go-bindings + echo "- Generated go binding hardhat/artifacts/bindings/$pkg/$pkg.go" } function deploy-contracts() { ( set -euo pipefail cd hardhat - npx hardhat deploy --network "$NETWORK" + npx hardhat deploy --network geth ) } +############################################################################ +# helpers +############################################################################ + function hardhat-script() { ( set -euo pipefail @@ -232,7 +183,6 @@ function hardhat-script() { ) } -# print the env settings for the various accounts and private keys function print-env() { hardhat-script scripts/print-env.ts | grep export } @@ -258,109 +208,144 @@ function balances() { } function run-cowsay-onchain() { - hardhat-script scripts/run-cowsay-onchain.ts + cd hardhat + doppler run -p run-cowsay-onchain -c dev -- npx hardhat run scripts/run-cowsay-onchain.ts +} + +############################################################################ +# solver +############################################################################ + +function solver() { + echo "- Reminder to do doppler setup to project->solver and config->dev" + doppler run -p solver -c dev -- go run . solver +} + +function solver-docker-build() { + docker build \ + -t solver \ + -f ./docker/solver/Dockerfile \ + --build-arg arch=${OS_ARCH} \ + . +} + +function solver-docker-run() { + docker run \ + --rm \ + --name solver \ + --network lilypad \ + --add-host localhost:host-gateway \ + -p 8080:8080 \ + -e DOPPLER_TOKEN="$(doppler configs tokens create -p solver -c dev docker --max-age 1m --plain)" \ + solver } ############################################################################ +# job creator ############################################################################ -# services + +function job-creator() { + echo "- Reminder to do doppler setup to project->job-creator and config->dev" + doppler run -p job-creator -c dev -- go run . jobcreator +} + +function job-creator-docker-build() { + docker build \ + -t job-creator \ + -f ./docker/job-creator/Dockerfile \ + . +} + +function job-creator-docker-run() { + docker run \ + --rm \ + --name job-creator \ + --network lilypad \ + --add-host localhost:host-gateway \ + -e DOPPLER_TOKEN="$(doppler configs tokens create -p job-creator -c dev docker --max-age 1m --plain)" \ + job-creator +} + ############################################################################ +# resource provider ############################################################################ -function run() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_PRIVATE_KEY=$JOB_CREATOR_PRIVATE_KEY - export SERVICE_SOLVER=$SOLVER_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - go run . run "$@" -} - -function runsdxl() { - # Check for the number of arguments provided - if [ $# -lt 3 ]; then - echo "Usage: $0 runsdxl PROMPT=\"\"" - exit 1 - fi +function resource-provider() { + echo "- Reminder to do doppler setup to project->resource-provider and config->dev" + doppler run -p resource-provider -c dev -- go run . resource-provider +} - source .env - eval $(./stack print-local-dev-env) - export WEB3_PRIVATE_KEY=$JOB_CREATOR_PRIVATE_KEY - export SERVICE_SOLVER=$SOLVER_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - local model="$1" - shift # Remove the first argument, so "$@" contains only the second one - local prompt="$@" - go run . run $model -i "PromptEnv=$prompt" +function resource-provider-docker-build() { + docker build \ + -t resource-provider \ + -f ./docker/resource-provider/Dockerfile \ + --build-arg doppler_config=dev \ + --build-arg DOPPLER_TOKEN_RESOURCE_PROVIDER="$(doppler configs tokens create -p resource-provider -c dev resource_provider_docker --plain)" \ + . } -function solver() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_PRIVATE_KEY=$SOLVER_PRIVATE_KEY - export JOB_CREATOR_ADDRESS=$JOB_CREATOR_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - export SERVER_PORT=8080 - export SERVER_URL=http://testnet.lilypad.tech:8080 - go run . solver "$@" +function resource-provider-docker-run() { + docker run \ + --rm \ + --name resource-provider \ + --network lilypad \ + --add-host localhost:host-gateway \ + -e DOPPLER_TOKEN_RESOURCE_PROVIDER="$(doppler configs tokens create -p resource-provider -c dev resource_provider_docker --max-age 1m --plain)" \ + resource-provider } -# we run the jobcreator as the solver -function jobcreator() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_PRIVATE_KEY=$SOLVER_PRIVATE_KEY - export SERVICE_SOLVER=$SOLVER_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - go run . jobcreator "$@" +############################################################################ +# bacalhau node +############################################################################ + +function bacalhau-node(){ + export BACALHAU_SERVE_IPFS_PATH=/tmp/lilypad/data/ipfs + export LOG_LEVEL=debug + bacalhau serve --node-type compute,requester --peer none --private-internal-ipfs=false --job-selection-accept-networked } -function resource-provider() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_RPC_URL=ws://testnet.lilypad.tech:8546 - export WEB3_PRIVATE_KEY=$RESOURCE_PROVIDER_PRIVATE_KEY - export SERVICE_SOLVER=$SOLVER_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - go run . resource-provider "$@" +function bacalhau-docker-build() { + docker build \ + -t bacalhau \ + -f ./docker/bacalhau/Dockerfile \ + . +} + +function bacalhau-docker-run() { + docker run \ + --privileged \ + --rm \ + --name bacalhau \ + --network lilypad \ + --add-host localhost:host-gateway \ + -v /tmp/lilypad/data:/tmp/lilypad/data \ + -p 1234:1234 \ + bacalhau } +############################################################################ +# mediator +############################################################################ + function mediator() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_RPC_URL=ws://testnet.lilypad.tech:8546 - export WEB3_PRIVATE_KEY=$MEDIATOR_PRIVATE_KEY - export WEB3_DIRECTORY_ADDRESS=$DIRECTORY_ADDRESS - export SERVICE_SOLVER=$SOLVER_ADDRESS go run . mediator "$@" } -############################################################################ ############################################################################ # tests ############################################################################ -############################################################################ function unit-tests() { - ( - set -euo pipefail - cd hardhat - npx hardhat test --network hardhat - ) + cd hardhat + npx hardhat test --network hardhat } -# this assumes boot having been run already +# this assumes chain is running +# and chain-fund-admin +# and chain-boot function integration-tests() { - source .env - eval $(./stack print-local-dev-env) - export WEB3_SOLVER_ADDRESS=$SOLVER_ADDRESS - export SERVICE_SOLVER=$SOLVER_ADDRESS - export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS - ( - set -euo pipefail - cd test - go test -v -count 1 . - ) + cd test + doppler run -p integration-tests -c dev -- go test -v -count 1 . } eval "$@"