2022-09-30 21:04:34 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-15 15:30:56 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-05-21 15:33:11 +01:00
|
|
|
"path/filepath"
|
2023-05-17 19:39:21 +07:00
|
|
|
dbg "runtime/debug"
|
|
|
|
"sync"
|
2022-10-11 13:34:32 +01:00
|
|
|
"time"
|
2022-10-06 20:06:03 +01:00
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/commands"
|
2022-10-06 20:06:03 +01:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/devnetutils"
|
2023-01-12 09:58:21 +07:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/models"
|
2022-10-06 20:06:03 +01:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/node"
|
2023-05-29 20:35:45 +01:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
|
2023-01-10 17:43:58 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/services"
|
2023-05-16 10:53:50 +01:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-05-15 15:30:56 +01:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils/flags"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-09-30 21:04:34 +01:00
|
|
|
)
|
|
|
|
|
2023-05-15 15:30:56 +01:00
|
|
|
var DataDirFlag = flags.DirectoryFlag{
|
|
|
|
Name: "datadir",
|
|
|
|
Usage: "Data directory for the devnet",
|
|
|
|
Value: flags.DirectoryString(""),
|
|
|
|
Required: true,
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:53:50 +01:00
|
|
|
type PanicHandler struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ph PanicHandler) Log(r *log.Record) error {
|
|
|
|
fmt.Printf("Stack: %s\n", dbg.Stack())
|
|
|
|
os.Exit(1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 21:04:34 +01:00
|
|
|
func main() {
|
2023-01-10 17:43:58 +00:00
|
|
|
|
2023-05-15 15:30:56 +01:00
|
|
|
debug.RaiseFdLimit()
|
|
|
|
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Version = params.VersionWithCommit(params.GitCommit)
|
|
|
|
app.Action = func(ctx *cli.Context) error {
|
|
|
|
return action(ctx)
|
|
|
|
}
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
&DataDirFlag,
|
|
|
|
}
|
|
|
|
|
|
|
|
app.After = func(ctx *cli.Context) error {
|
|
|
|
// unsubscribe from all the subscriptions made
|
|
|
|
services.UnsubscribeAll()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 17:43:58 +00:00
|
|
|
|
2023-05-15 15:30:56 +01:00
|
|
|
func action(ctx *cli.Context) error {
|
|
|
|
dataDir := ctx.String("datadir")
|
2023-05-21 15:33:11 +01:00
|
|
|
logsDir := filepath.Join(dataDir, "logs")
|
|
|
|
if err := os.MkdirAll(logsDir, os.ModeDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-16 10:53:50 +01:00
|
|
|
logger := logging.SetupLoggerCtx("devnet", ctx, false /* rootLogger */)
|
2023-05-29 20:35:45 +01:00
|
|
|
reqGen := requests.NewRequestGenerator(logger)
|
2023-05-16 10:53:50 +01:00
|
|
|
|
|
|
|
// Make root logger fail
|
2023-05-20 14:48:16 +01:00
|
|
|
log.Root().SetHandler(PanicHandler{})
|
2023-05-16 10:53:50 +01:00
|
|
|
|
2023-05-15 15:30:56 +01:00
|
|
|
// clear all the dev files
|
|
|
|
if err := devnetutils.ClearDevDB(dataDir, logger); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 21:04:34 +01:00
|
|
|
// wait group variable to prevent main function from terminating until routines are finished
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// start the first erigon node in a go routine
|
2023-05-29 20:35:45 +01:00
|
|
|
node.Start(reqGen, &wg, dataDir, logger)
|
2022-09-30 21:04:34 +01:00
|
|
|
|
2023-01-16 18:59:01 +00:00
|
|
|
// send a quit signal to the quit channels when done making checks
|
|
|
|
node.QuitOnSignal(&wg)
|
|
|
|
|
2022-10-11 13:34:32 +01:00
|
|
|
// sleep for seconds to allow the nodes fully start up
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
// start up the subscription services for the different sub methods
|
2023-05-20 14:48:16 +01:00
|
|
|
services.InitSubscriptions([]models.SubMethod{models.ETHNewHeads}, logger)
|
2023-01-10 17:43:58 +00:00
|
|
|
|
2022-10-11 13:34:32 +01:00
|
|
|
// execute all rpc methods amongst the two nodes
|
2023-05-29 20:35:45 +01:00
|
|
|
commands.ExecuteAllMethods(reqGen, logger)
|
2022-10-11 13:34:32 +01:00
|
|
|
|
2022-09-30 21:04:34 +01:00
|
|
|
// wait for all goroutines to complete before exiting
|
|
|
|
wg.Wait()
|
2023-05-15 15:30:56 +01:00
|
|
|
return nil
|
2022-09-30 21:04:34 +01:00
|
|
|
}
|