Skip to content

Commit

Permalink
feat: Add infrastructure to run Node
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Oct 24, 2023
1 parent b9922b7 commit 3348331
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.DS_Store
**/.idea/
node-execution/deployments/**
node-execution/devnet/deployments/**
22 changes: 22 additions & 0 deletions node-execution/anvil/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# syntax=docker.io/docker/dockerfile:1.4
FROM debian:bookworm-20230814-slim

# install curl and jq (for healthcheck support)
RUN <<EOF
apt-get update
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends ca-certificates curl git jq xxd
rm -rf /var/lib/apt/lists/*
EOF

# download pre-compiled binaries
RUN curl -sSL https://github.com/foundry-rs/foundry/releases/download/nightly/foundry_nightly_linux_$(dpkg --print-architecture).tar.gz | \
tar -zx -C /usr/local/bin

# healthcheck script using net_listening JSON-RPC method
COPY eth_isready /usr/local/bin
COPY eth_dump /usr/local/bin
COPY eth_load /usr/local/bin

HEALTHCHECK CMD eth_isready

CMD ["anvil"]
6 changes: 6 additions & 0 deletions node-execution/anvil/docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target "docker-metadata-action" {}
target "docker-platforms" {}

target "default" {
inherits = ["docker-metadata-action", "docker-platforms"]
}
3 changes: 3 additions & 0 deletions node-execution/anvil/docker-bake.override.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target "default" {
tags = ["cartesi/anvil:devel"]
}
6 changes: 6 additions & 0 deletions node-execution/anvil/docker-bake.platforms.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target "docker-platforms" {
platforms = [
"linux/amd64",
"linux/arm64"
]
}
Empty file added node-execution/devnet/LICENSE
Empty file.
40 changes: 40 additions & 0 deletions node-execution/devnet/cmd/prepareAnvil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// prepareAnvilCmd represents the prepareAnvil command
var prepareAnvilCmd = &cobra.Command{
Use: "prepareAnvil",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("prepareAnvil called")
},
}

func init() {
rootCmd.AddCommand(prepareAnvilCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// prepareAnvilCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// prepareAnvilCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
51 changes: 51 additions & 0 deletions node-execution/devnet/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "devnet-builder",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.devnet-builder.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


49 changes: 49 additions & 0 deletions node-execution/devnet/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: "3.9"

services:

anvil:
image: docker.io/cartesi/anvil:devel
command:
[
"anvil",
"--host",
"0.0.0.0"
]
healthcheck:
test: ["CMD", "eth_isready"]
interval: 10s
timeout: 1s
retries: 5
ports:
- "8545:8545"

hardhat:
image: cartesi/rollups-hardhat:1.0.0
environment:
- RPC_URL=http://anvil:8545
command:
[
"deploy",
"--network",
"localhost",
"--export",
"/opt/cartesi/share/deployments/localhost.json"
]
init: true
depends_on:
anvil:
condition: service_healthy
healthcheck:
test:
[
"CMD",
"test",
"-f",
"/opt/cartesi/share/deployments/localhost.json"
]
interval: 30s
timeout: 30s
retries: 5
volumes:
- ./deployments:/app/rollups/deployments
9 changes: 9 additions & 0 deletions node-execution/devnet/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module devnet-builder

go 1.21.3

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions node-execution/devnet/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
138 changes: 138 additions & 0 deletions node-execution/devnet/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

import path from "path";
import { HardhatUserConfig } from "hardhat/config";
import { HttpNetworkUserConfig } from "hardhat/types";
import { getSingletonFactoryInfo } from "@safe-global/safe-singleton-factory";

import "@nomiclabs/hardhat-ethers";
import "@nomicfoundation/hardhat-verify";
import "@typechain/hardhat";
import "hardhat-deploy";
import "hardhat-gas-reporter";

import {
Chain,
arbitrum,
arbitrumGoerli,
mainnet,
optimism,
optimismGoerli,
sepolia,
} from "viem/chains";

// read MNEMONIC from env variable
let mnemonic = process.env.MNEMONIC;

const ppath = (packageName: string, pathname: string) => {
return path.join(
path.dirname(require.resolve(`${packageName}/package.json`)),
pathname,
);
};

const networkConfig = (chain: Chain): HttpNetworkUserConfig => {
let url = process.env.RPC_URL || chain.rpcUrls.public.http.at(0);

// support for infura and alchemy URLs through env variables
if (process.env.INFURA_ID && chain.rpcUrls.infura?.http) {
url = `${chain.rpcUrls.infura.http}/${process.env.INFURA_ID}`;
} else if (process.env.ALCHEMY_ID && chain.rpcUrls.alchemy?.http) {
url = `${chain.rpcUrls.alchemy.http}/${process.env.ALCHEMY_ID}`;
}

return {
chainId: chain.id,
url,
accounts: mnemonic ? { mnemonic } : undefined,
};
};

const config: HardhatUserConfig = {
networks: {
hardhat: mnemonic ? { accounts: { mnemonic } } : {},
localhost: {
url: process.env.RPC_URL || "http://192.168.0.192:8545",
accounts: mnemonic ? { mnemonic } : undefined,
},
arbitrum: networkConfig(arbitrum),
arbitrum_goerli: networkConfig(arbitrumGoerli),
mainnet: networkConfig(mainnet),
sepolia: networkConfig(sepolia),
optimism: networkConfig(optimism),
optimism_goerli: networkConfig(optimismGoerli),
},
solidity: {
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
},
},
},
paths: {
artifacts: "artifacts",
deploy: "deploy",
deployments: "deployments",
},
deterministicDeployment: (network: string) => {
// networks will use another deterministic deployment proxy
// https://github.com/safe-global/safe-singleton-factory
const chainId = parseInt(network);
const info = getSingletonFactoryInfo(chainId);
if (info) {
return {
factory: info.address,
deployer: info.signerAddress,
funding: (
BigInt(info.gasPrice) * BigInt(info.gasLimit)
).toString(),
signedTx: info.transaction,
};
} else {
console.warn(
`unsupported deterministic deployment for network ${network}`,
);
return undefined;
}
},
typechain: {
outDir: "src/types",
target: "ethers-v5",
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
external: {
contracts: [
{
artifacts: ppath("@cartesi/util", "/export/artifacts"),
deploy: ppath("@cartesi/util", "/dist/deploy"),
},
],
deployments: {
localhost: ["deployments/localhost"],
arbitrum: [ppath("@cartesi/util", "/deployments/arbitrum")],
arbitrum_goerli: [
ppath("@cartesi/util", "/deployments/arbitrum_goerli"),
],
mainnet: [ppath("@cartesi/util", "/deployments/mainnet")],
optimism: [ppath("@cartesi/util", "/deployments/optimism")],
optimism_goerli: [
ppath("@cartesi/util", "/deployments/optimism_goerli"),
],
sepolia: [ppath("@cartesi/util", "/deployments/sepolia")],
},
},
namedAccounts: {
deployer: {
default: 0,
},
},
gasReporter: {
enabled: process.env.REPORT_GAS ? true : false,
},
};

export default config;
11 changes: 11 additions & 0 deletions node-execution/devnet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

import "devnet-builder/cmd"

func main() {
cmd.Execute()
}
Loading

0 comments on commit 3348331

Please sign in to comment.