diff --git a/app/app.go b/app/app.go index 7ce5f986b..2dedfdf88 100644 --- a/app/app.go +++ b/app/app.go @@ -23,6 +23,7 @@ import ( "net/http/pprof" "time" + eth2client "github.com/attestantio/go-eth2-client" eth2v1 "github.com/attestantio/go-eth2-client/api/v1" eth2http "github.com/attestantio/go-eth2-client/http" eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0" @@ -65,6 +66,7 @@ type Config struct { ValidatorAPIAddr string BeaconNodeAddr string JaegerAddr string + Simnet bool SimnetVMock bool TestConfig TestConfig @@ -84,8 +86,6 @@ type TestConfig struct { ParSigExFunc func() core.ParSigEx // LcastTransportFunc provides an in-memory leader cast transport. LcastTransportFunc func() leadercast.Transport - // DisableSimnet disables the simnet. - DisableSimnet bool // SimnetKeys provides private key shares for the simnet validatormock signer. SimnetKeys []*bls_sig.SecretKey // SimnetBMockOpts defines additional simnet beacon mock options. @@ -139,7 +139,7 @@ func Run(ctx context.Context, conf Config) (err error) { wireMonitoringAPI(life, conf.MonitoringAddr, localEnode) - if err := wireSimNetCoreWorkflow(life, conf, manifest, nodeIdx, tcpNode); err != nil { + if err := wireCoreWorkflow(ctx, life, conf, manifest, nodeIdx, tcpNode); err != nil { return err } @@ -199,13 +199,9 @@ func wireP2P(ctx context.Context, life *lifecycle.Manager, conf Config, manifest return tcpNode, localEnode, nil } -// wireSimNetCoreWorkflow wires a simnet core workflow including a beaconmock and validatormock. -//nolint:cyclop -func wireSimNetCoreWorkflow(life *lifecycle.Manager, conf Config, manifest Manifest, nodeIdx NodeIdx, tcpNode host.Host) error { - if conf.TestConfig.DisableSimnet { - return nil - } - +// wireCoreWorkflow wires the core workflow components. +//nolint:cyclop,revive +func wireCoreWorkflow(ctx context.Context, life *lifecycle.Manager, conf Config, manifest Manifest, nodeIdx NodeIdx, tcpNode host.Host) error { // Convert and prep public keys and public shares var ( corePubkeys []core.PubKey @@ -243,25 +239,40 @@ func wireSimNetCoreWorkflow(life *lifecycle.Manager, conf Config, manifest Manif pubshares = append(pubshares, eth2Share) } - // Configure the beacon mock. - opts := []beaconmock.Option{ - beaconmock.WithSlotDuration(time.Second), - beaconmock.WithDeterministicDuties(100), - beaconmock.WithValidatorSet(createMockValidators(pubkeys)), - } - opts = append(opts, conf.TestConfig.SimnetBMockOpts...) - bmock, err := beaconmock.New(opts...) - if err != nil { - return err + // Configure the beacon node api. + var eth2Cl eth2client.Service + if conf.Simnet { + // Configure the beacon mock. + opts := []beaconmock.Option{ + beaconmock.WithSlotDuration(time.Second), + beaconmock.WithDeterministicDuties(100), + beaconmock.WithValidatorSet(createMockValidators(pubkeys)), + } + opts = append(opts, conf.TestConfig.SimnetBMockOpts...) + bmock, err := beaconmock.New(opts...) + if err != nil { + return err + } + conf.BeaconNodeAddr = bmock.HTTPAddr() + eth2Cl = bmock + life.RegisterStop(lifecycle.StopBeaconMock, lifecycle.HookFuncErr(bmock.Close)) + } else { + var err error + eth2Cl, err = eth2http.New(ctx, + eth2http.WithLogLevel(1), + eth2http.WithAddress(conf.BeaconNodeAddr), + ) + if err != nil { + return errors.Wrap(err, "new eth2 http client") + } } - conf.BeaconNodeAddr = bmock.HTTPAddr() - sched, err := scheduler.New(corePubkeys, bmock) + sched, err := scheduler.New(corePubkeys, eth2Cl) if err != nil { return err } - fetch, err := fetcher.New(bmock) + fetch, err := fetcher.New(eth2Cl) if err != nil { return err } @@ -277,7 +288,7 @@ func wireSimNetCoreWorkflow(life *lifecycle.Manager, conf Config, manifest Manif dutyDB := dutydb.NewMemDB() - vapi, err := validatorapi.NewComponent(bmock, pubSharesByKey, nodeIdx.ShareIdx) + vapi, err := validatorapi.NewComponent(eth2Cl, pubSharesByKey, nodeIdx.ShareIdx) if err != nil { return err } @@ -299,7 +310,7 @@ func wireSimNetCoreWorkflow(life *lifecycle.Manager, conf Config, manifest Manif aggSigDB := aggsigdb.Stub{} - broadcaster, err := bcast.New(bmock) + broadcaster, err := bcast.New(eth2Cl) if err != nil { return err } @@ -318,7 +329,6 @@ func wireSimNetCoreWorkflow(life *lifecycle.Manager, conf Config, manifest Manif life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartLeaderCast, lifecycle.HookFunc(consensus.Run)) life.RegisterStart(lifecycle.AsyncBackground, lifecycle.StartScheduler, lifecycle.HookFuncErr(sched.Run)) life.RegisterStop(lifecycle.StopScheduler, lifecycle.HookFuncMin(sched.Stop)) - life.RegisterStop(lifecycle.StopBeaconMock, lifecycle.HookFuncErr(bmock.Close)) return nil } @@ -418,7 +428,7 @@ func (h httpServeHook) Call(context.Context) error { // wireValidatorMock wires the validator mock if enabled. The validator mock attestions // will be triggered by scheduler's DutyAttester. It connects via http validatorapi.Router. func wireValidatorMock(conf Config, pubshares []eth2p0.BLSPubKey, sched core.Scheduler) error { - if !conf.SimnetVMock { + if !conf.Simnet || !conf.SimnetVMock { return nil } diff --git a/app/app_test.go b/app/app_test.go index 3e3985875..f115d8993 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -117,13 +117,13 @@ func pingCluster(t *testing.T, test pingTest) { for i := 0; i < n; i++ { conf := app.Config{ + Simnet: true, MonitoringAddr: testutil.AvailableAddr(t).String(), // Random monitoring address ValidatorAPIAddr: testutil.AvailableAddr(t).String(), // Random validatorapi address TestConfig: app.TestConfig{ - Manifest: &manifest, - P2PKey: p2pKeys[i], - PingCallback: asserter.Callback(t, i), - DisableSimnet: true, + Manifest: &manifest, + P2PKey: p2pKeys[i], + PingCallback: asserter.Callback(t, i), }, P2P: p2p.Config{ UDPBootnodes: test.Bootnodes, diff --git a/app/simnet_test.go b/app/simnet_test.go index 5f489f43c..8ea4ba918 100644 --- a/app/simnet_test.go +++ b/app/simnet_test.go @@ -125,6 +125,7 @@ func testSimnet(t *testing.T, args simnetArgs) { ) for i := 0; i < args.N; i++ { conf := app.Config{ + Simnet: true, SimnetVMock: args.VMocks[i], MonitoringAddr: testutil.AvailableAddr(t).String(), // Random monitoring address ValidatorAPIAddr: args.VAPIAddrs[i], diff --git a/cmd/run.go b/cmd/run.go index 20dbd1a1b..e030fd957 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -55,6 +55,7 @@ func bindRunFlags(flags *pflag.FlagSet, config *app.Config) { flags.StringVar(&config.MonitoringAddr, "monitoring-address", "127.0.0.1:8088", "Listening address (ip and port) for the monitoring API (prometheus, pprof)") flags.StringVar(&config.JaegerAddr, "jaeger-address", "", "Listening address for jaeger tracing") flags.BoolVar(&config.SimnetVMock, "simnet-validator-mock", false, "Enables mock validator when running simnet.") + flags.BoolVar(&config.Simnet, "simnet", false, "Enables simnet, starts and connect to a mock beacon node.") } func bindGeneralFlags(flags *pflag.FlagSet, dataDir *string) { diff --git a/docs/configuration.md b/docs/configuration.md index c8b7c81e1..4c4979c4c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -36,6 +36,7 @@ Flags: --p2p-peerdb string Path to store a discv5 peer database. Empty default results in in-memory database. --p2p-tcp-address strings Comma-separated list of listening TCP addresses (ip and port) for LibP2P traffic (default [127.0.0.1:13900]) --p2p-udp-address string Listening UDP address (ip and port) for Discv5 discovery (default "127.0.0.1:30309") + --simnet Enables simnet, starts and connect to a mock beacon node. --simnet-validator-mock Enables mock validator when running simnet. --validator-api-address string Listening address (ip and port) for validator-facing traffic proxying the beacon-node API (default "127.0.0.1:3500")