erigon-pulse/cmd/devnet/scenarios/run.go

125 lines
2.3 KiB
Go
Raw Normal View History

Devnet scenarios (#7723) This is an update to the devnet code which introduces the concept of configurable scenarios. This replaces the previous hard coded execution function. The intention is that now both the network and the operations to run on the network can be described in a data structure which is configurable and composable. The operating model is to create a network and then ask it to run scenarios: ```go network.Run( runCtx, scenarios.Scenario{ Name: "all", Steps: []*scenarios.Step{ &scenarios.Step{Text: "InitSubscriptions", Args: []any{[]requests.SubMethod{requests.Methods.ETHNewHeads}}}, &scenarios.Step{Text: "PingErigonRpc"}, &scenarios.Step{Text: "CheckTxPoolContent", Args: []any{0, 0, 0}}, &scenarios.Step{Text: "SendTxWithDynamicFee", Args: []any{recipientAddress, services.DevAddress, sendValue}}, &scenarios.Step{Text: "AwaitBlocks", Args: []any{2 * time.Second}}, }, }) ``` The steps here refer to step handlers which can be defined as follows: ```go func init() { scenarios.MustRegisterStepHandlers( scenarios.StepHandler(GetBalance), ) } func GetBalance(ctx context.Context, addr string, blockNum requests.BlockNumber, checkBal uint64) { ... ``` This commit is an initial implementation of the scenario running - which is working, but will need to be enhanced to make it more usable & developable. The current version of the code is working and has been tested with the dev network, and bor withoutheimdall. There is a multi miner bor heimdall configuration but this is yet to be tested. Note that by default the scenario runner picks nodes at random on the network to send transactions to. this causes the dev network to run very slowly as it seems to take a long time to include transactions where the nonce is incremented across nodes. It seems to take a long time for the nonce to catch up in the transaction pool processing. This is yet to be investigated.
2023-06-14 11:35:22 +00:00
package scenarios
import (
"context"
"sync"
"github.com/ledgerwatch/erigon/cmd/devnet/devnetutils"
)
type SimulationInitializer func(*SimulationContext)
func Run(ctx context.Context, scenarios ...*Scenario) error {
if len(scenarios) == 0 {
return nil
}
Devnet scenarios (#7723) This is an update to the devnet code which introduces the concept of configurable scenarios. This replaces the previous hard coded execution function. The intention is that now both the network and the operations to run on the network can be described in a data structure which is configurable and composable. The operating model is to create a network and then ask it to run scenarios: ```go network.Run( runCtx, scenarios.Scenario{ Name: "all", Steps: []*scenarios.Step{ &scenarios.Step{Text: "InitSubscriptions", Args: []any{[]requests.SubMethod{requests.Methods.ETHNewHeads}}}, &scenarios.Step{Text: "PingErigonRpc"}, &scenarios.Step{Text: "CheckTxPoolContent", Args: []any{0, 0, 0}}, &scenarios.Step{Text: "SendTxWithDynamicFee", Args: []any{recipientAddress, services.DevAddress, sendValue}}, &scenarios.Step{Text: "AwaitBlocks", Args: []any{2 * time.Second}}, }, }) ``` The steps here refer to step handlers which can be defined as follows: ```go func init() { scenarios.MustRegisterStepHandlers( scenarios.StepHandler(GetBalance), ) } func GetBalance(ctx context.Context, addr string, blockNum requests.BlockNumber, checkBal uint64) { ... ``` This commit is an initial implementation of the scenario running - which is working, but will need to be enhanced to make it more usable & developable. The current version of the code is working and has been tested with the dev network, and bor withoutheimdall. There is a multi miner bor heimdall configuration but this is yet to be tested. Note that by default the scenario runner picks nodes at random on the network to send transactions to. this causes the dev network to run very slowly as it seems to take a long time to include transactions where the nonce is incremented across nodes. It seems to take a long time for the nonce to catch up in the transaction pool processing. This is yet to be investigated.
2023-06-14 11:35:22 +00:00
return runner{scenarios: scenarios}.runWithOptions(ctx, getDefaultOptions())
}
type runner struct {
randomize bool
stopOnFailure bool
scenarios []*Scenario
simulationInitializer SimulationInitializer
}
func (r *runner) concurrent(ctx context.Context, rate int) (err error) {
var copyLock sync.Mutex
queue := make(chan int, rate)
scenarios := make([]*Scenario, len(r.scenarios))
if r.randomize {
for i := range r.scenarios {
j := devnetutils.RandomInt(i + 1)
scenarios[i] = r.scenarios[j]
}
} else {
copy(scenarios, r.scenarios)
}
simulationContext := SimulationContext{
suite: &suite{
randomize: r.randomize,
defaultContext: ctx,
stepRunners: stepRunners(ctx),
},
}
for i, s := range scenarios {
scenario := *s
queue <- i // reserve space in queue
runScenario := func(err *error, Scenario *Scenario) {
defer func() {
<-queue // free a space in queue
}()
if r.stopOnFailure && *err != nil {
return
}
// Copy base suite.
suite := *simulationContext.suite
if r.simulationInitializer != nil {
sc := SimulationContext{suite: &suite}
r.simulationInitializer(&sc)
}
_, serr := suite.runScenario(&scenario)
if suite.shouldFail(serr) {
copyLock.Lock()
*err = serr
copyLock.Unlock()
}
}
if rate == 1 {
// Running within the same goroutine for concurrency 1
// to preserve original stacks and simplify debugging.
runScenario(&err, &scenario)
} else {
go runScenario(&err, &scenario)
}
}
// wait until last are processed
for i := 0; i < rate; i++ {
queue <- i
}
close(queue)
return err
}
func (runner runner) runWithOptions(ctx context.Context, opt *Options) error {
//var output io.Writer = os.Stdout
//if nil != opt.Output {
// output = opt.Output
//}
if opt.Concurrency < 1 {
opt.Concurrency = 1
}
return runner.concurrent(ctx, opt.Concurrency)
}
type Options struct {
Concurrency int
}
func getDefaultOptions() *Options {
opt := &Options{
Concurrency: 1,
}
return opt
}