Skip to content

Commit

Permalink
feat(evm-reader): Add input-reader retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Jul 10, 2024
1 parent 122c40f commit 4ebb51e
Show file tree
Hide file tree
Showing 26 changed files with 965 additions and 367 deletions.
22 changes: 22 additions & 0 deletions cmd/cartesi-rollups-cli/root/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const examples = `# Run all deps:
cartesi-rollups-cli run-deps`

var depsConfig = deps.NewDefaultDepsConfig()
var disablePostgres = false
var disableDevnet = false
var verbose = false

func init() {
Expand All @@ -42,6 +44,14 @@ func init() {
deps.DefaultPostgresPassword,
"Postgres password")

Cmd.Flags().BoolVar(&depsConfig.Postgres.SaveData, "postgres-save-data",
deps.DefaultPostgresSaveData,
"Postgres enable save data to local storage")

Cmd.Flags().StringVar(&depsConfig.Postgres.DataDir, "postrgres-data-dir",
deps.DefaultPostgresDataDir,
"Postgres local data dir")

Cmd.Flags().StringVar(&depsConfig.Devnet.DockerImage, "devnet-docker-image",
deps.DefaultDevnetDockerImage,
"Devnet docker image name")
Expand All @@ -58,6 +68,10 @@ func init() {
deps.DefaultDevnetNoMining,
"Devnet disable mining")

Cmd.Flags().BoolVar(&disablePostgres, "disable-postgres", false, "Disable Postgres")

Cmd.Flags().BoolVar(&disableDevnet, "disable-devnet", false, "Disable Devnet")

Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose logs")
}

Expand All @@ -77,6 +91,14 @@ func run(cmd *cobra.Command, args []string) {
slog.SetDefault(logger)
}

if disablePostgres {
depsConfig.Postgres = nil
}

if disableDevnet {
depsConfig.Devnet = nil
}

depsContainers, err := deps.Run(ctx, *depsConfig)
cobra.CheckErr(err)

Expand Down
191 changes: 191 additions & 0 deletions cmd/cartesi-rollups-evm-reader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main

import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/cartesi/rollups-node/internal/evmreader/service"
"github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/repository"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
)

var (
// Should be overridden during the final release build with ldflags
// to contain the actual version number
buildVersion = "devel"
)

const (
CMD_NAME = "evm-reader"
)

var Cmd = &cobra.Command{
Use: CMD_NAME,
Short: "Runs EVM Reader",
Long: `Runs EVM Reader in standalone mode`,
Run: run,
}

type Config struct {
defaultBlock string
postgresEnpoint string
blockchainHttpEndpoint string
blockchainWsEnpoint string
inputBoxAddress string
inputBoxDeploymentBlockNumber uint64
}

var verbose bool
var config Config

func init() {

Cmd.Flags().StringVarP(&config.defaultBlock,
"default-block",
"d",
"latest",
"Default block to be used when fetching for new blocks. One of 'latest','safe','pending','finalized'")

Cmd.Flags().StringVarP(&config.postgresEnpoint,
"postgres-endpoint",
"p",
"postgres://postgres:password@localhost:5432/postgres",
"Postgres endpoint")

Cmd.Flags().StringVarP(&config.blockchainHttpEndpoint,
"blockchain-http-enpoint",
"b",
"http://localhost:8545",
"Blockchain HTTP Enpoint")

Cmd.Flags().StringVarP(&config.blockchainWsEnpoint,
"blockchain-ws-enpoint",
"w",
"ws://localhost:8545",
"Blockchain WS Enpoint")

Cmd.Flags().StringVarP(&config.inputBoxAddress,
"inputbox-address",
"i",
"0xA1b8EB1F13d8D5Db976a653BbDF8972cfD14691C",
"Input Box contract address")

Cmd.Flags().Uint64VarP(&config.inputBoxDeploymentBlockNumber,
"inputbox-block-number",
"n",
16,

Check failure on line 87 in cmd/cartesi-rollups-evm-reader/main.go

View workflow job for this annotation

GitHub Actions / test-go

Magic number: 16, in <argument> detected (mnd)
"Input Box deployment block number")

Cmd.Flags().BoolVarP(&verbose,
"verbose",
"v",
false,
"enable verbose logging")
}

func main() {
err := Cmd.Execute()
if err != nil {
os.Exit(1)
}
}

func run(cmd *cobra.Command, args []string) {
startTime := time.Now()

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

//config := config.FromEnv()

// setup log
logLevel := slog.LevelInfo
if verbose {
logLevel = slog.LevelDebug
}

opts := &tint.Options{
Level: logLevel,
AddSource: logLevel == slog.LevelDebug,
TimeFormat: "2006-01-02T15:04:05.000", // RFC3339 with milliseconds and without timezone
}
handler := tint.NewHandler(os.Stdout, opts)
logger := slog.New(handler)
slog.SetDefault(logger)
slog.Info("Starting the Cartesi Rollups Node's EVM Reader", "version", buildVersion, "config", config)

repository.RunMigrations(fmt.Sprintf("%v?sslmode=disable", config.postgresEnpoint))

database, err := repository.Connect(ctx, config.postgresEnpoint)
if err != nil {
slog.Error("EVM Reader exited with an error", "error", err)
os.Exit(1)
}

var defaultBlock model.DefaultBlock
switch config.defaultBlock {
case "latest":
defaultBlock = model.DefaultBlockStatusLatest
break

Check failure on line 140 in cmd/cartesi-rollups-evm-reader/main.go

View workflow job for this annotation

GitHub Actions / test-go

S1023: redundant break statement (gosimple)
case "safe":
defaultBlock = model.DefaultBlockStatusSafe
break

Check failure on line 143 in cmd/cartesi-rollups-evm-reader/main.go

View workflow job for this annotation

GitHub Actions / test-go

S1023: redundant break statement (gosimple)
case "pending":
defaultBlock = model.DefaultBlockStatusPending
break

Check failure on line 146 in cmd/cartesi-rollups-evm-reader/main.go

View workflow job for this annotation

GitHub Actions / test-go

S1023: redundant break statement (gosimple)
case "finalized":
defaultBlock = model.DefaultBlockStatusFinalized
break
default:
slog.Error("Invalid default block value", "default block", config.defaultBlock)
os.Exit(1)
}

// setup database
nodePersistentConfig := model.NodePersistentConfig{
DefaultBlock: defaultBlock,
InputBoxDeploymentBlock: config.inputBoxDeploymentBlockNumber,
InputBoxAddress: common.HexToAddress(config.inputBoxAddress),
}
err = database.InsertDatabaseConfig(ctx, &nodePersistentConfig)
if err != nil {
slog.Error("EVM Reader exited with an error", "error", err)
os.Exit(1)
}
database.Close()

// create EVM Reader Service
service := service.NewEvmReaderService(
config.blockchainHttpEndpoint,
config.blockchainHttpEndpoint,
config.postgresEnpoint,
)

// logs startup time
ready := make(chan struct{}, 1)
go func() {
select {
case <-ready:
duration := time.Since(startTime)
slog.Info("EVM Reader is ready", "after", duration)
case <-ctx.Done():
}
}()

// start service
if err := service.Start(ctx, ready); err != nil {
slog.Error("EVM Reader exited with an error", "error", err)
os.Exit(1)
}
}
4 changes: 4 additions & 0 deletions docs/cli/cartesi-rollups-cli_run-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ cartesi-rollups-cli run-deps
--devnet-docker-image string Devnet docker image name (default "cartesi/rollups-node-devnet:devel")
--devnet-mapped-port string Devnet local listening port number (default "8545")
--devnet-no-mining Devnet disable mining
--disable-devnet Disable Devnet
--disable-postgres Disable Postgres
-h, --help help for run-deps
--postgres-docker-image string Postgress docker image name (default "postgres:16-alpine")
--postgres-mapped-port string Postgres local listening port number (default "5432")
--postgres-password string Postgres password (default "password")
--postgres-save-data Postgres enable save data to local storage
--postrgres-data-dir string Postgres local data dir (default "./postgres-data")
-v, --verbose verbose logs
```

Expand Down
9 changes: 9 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ WebSocket endpoint for the blockchain RPC provider.

* **Type:** `string`

## `CARTESI_DEFAULT_BLOCK`

The default block to be used when requesting new blocks.
One of 'latest', 'pending', 'safe', 'finalized'


* **Type:** `DefaultBlock`
* **Default:** `"finalized"`

## `CARTESI_LEGACY_BLOCKCHAIN_ENABLED`

If set to true the node will send transactions using the legacy gas fee model
Expand Down
15 changes: 15 additions & 0 deletions internal/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
DefaultPostgresPort = "5432"
DefaultPostgresUser = "postgres"
DefaultPostgresPassword = "password"
DefaultPostgresSaveData = false
DefaultPostgresDataDir = "./postgres-data"
DefaultDevnetDockerImage = "cartesi/rollups-node-devnet:devel"
DefaultDevnetPort = "8545"
DefaultDevnetBlockTime = "1"
Expand All @@ -48,6 +50,8 @@ type PostgresConfig struct {
DockerImage string
Port string
Password string
SaveData bool
DataDir string
}

type DevnetConfig struct {
Expand All @@ -65,6 +69,8 @@ func NewDefaultDepsConfig() *DepsConfig {
DefaultPostgresDockerImage,
DefaultPostgresPort,
DefaultPostgresPassword,
DefaultPostgresSaveData,
DefaultPostgresDataDir,
},
&DevnetConfig{
DefaultDevnetDockerImage,
Expand Down Expand Up @@ -178,6 +184,15 @@ func Run(ctx context.Context, depsConfig DepsConfig) (*DepsContainers, error) {
postgresExposedPorts = strings.Join([]string{
depsConfig.Postgres.Port, ":", postgresExposedPorts}, "")
}

// var mounts []testcontainers.ContainerMount
// if depsConfig.Postgres.SaveData {
// mounts = append(mounts, testcontainers.GenericVolumeMountSource{
// Name: "app-data",

// }
// }

postgresReq := testcontainers.ContainerRequest{
Image: depsConfig.Postgres.DockerImage,
ExposedPorts: []string{postgresExposedPorts},
Expand Down
Loading

0 comments on commit 4ebb51e

Please sign in to comment.