Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: add simnet flag #285

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -65,6 +66,7 @@ type Config struct {
ValidatorAPIAddr string
BeaconNodeAddr string
JaegerAddr string
Simnet bool
SimnetVMock bool

TestConfig TestConfig
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions app/simnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func bindRunFlags(flags *pflag.FlagSet, config *app.Config) {
flags.StringVar(&config.ValidatorAPIAddr, "validator-api-address", "127.0.0.1:3500", "Listening address (ip and port) for validator-facing traffic proxying the beacon-node API")
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 connects to an internal mock beacon node.")
flags.BoolVar(&config.SimnetVMock, "simnet-validator-mock", false, "Enables an internal mock validator client when running simnet.")
}

func bindGeneralFlags(flags *pflag.FlagSet, dataDir *string) {
Expand Down
3 changes: 2 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ 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-validator-mock Enables mock validator when running simnet.
--simnet Enables simnet, starts and connects to an internal mock beacon node.
--simnet-validator-mock Enables an internal mock validator client 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")

````
Expand Down