-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evm-reader): Add input-reader retry policy
- Loading branch information
Showing
26 changed files
with
965 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
"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 | ||
case "safe": | ||
defaultBlock = model.DefaultBlockStatusSafe | ||
break | ||
case "pending": | ||
defaultBlock = model.DefaultBlockStatusPending | ||
break | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.