mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-31 16:21:21 +00:00
f110102023
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.
155 lines
3.0 KiB
Go
155 lines
3.0 KiB
Go
package args
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
type Args []string
|
|
|
|
func AsArgs(args interface{}) (Args, error) {
|
|
|
|
argsValue := reflect.ValueOf(args)
|
|
|
|
if argsValue.Kind() == reflect.Ptr {
|
|
argsValue = argsValue.Elem()
|
|
}
|
|
|
|
if argsValue.Kind() != reflect.Struct {
|
|
return nil, fmt.Errorf("Args type must be struct or struc pointer, got %T", args)
|
|
}
|
|
|
|
return gatherArgs(argsValue, func(v reflect.Value, field reflect.StructField) (string, error) {
|
|
tag := field.Tag.Get("arg")
|
|
|
|
if tag == "-" {
|
|
return "", nil
|
|
}
|
|
|
|
// only process public fields (reflection won't return values of unsafe fields without unsafe operations)
|
|
if r, _ := utf8.DecodeRuneInString(field.Name); !(unicode.IsLetter(r) && unicode.IsUpper(r)) {
|
|
return "", nil
|
|
}
|
|
|
|
var key string
|
|
var positional bool
|
|
|
|
for _, key = range strings.Split(tag, ",") {
|
|
if key == "" {
|
|
continue
|
|
}
|
|
|
|
key = strings.TrimLeft(key, " ")
|
|
|
|
if pos := strings.Index(key, ":"); pos != -1 {
|
|
key = key[:pos]
|
|
}
|
|
|
|
switch {
|
|
case strings.HasPrefix(key, "---"):
|
|
return "", fmt.Errorf("%s.%s: too many hyphens", v.Type().Name(), field.Name)
|
|
case strings.HasPrefix(key, "--"):
|
|
|
|
case strings.HasPrefix(key, "-"):
|
|
if len(key) != 2 {
|
|
return "", fmt.Errorf("%s.%s: short arguments must be one character only", v.Type().Name(), field.Name)
|
|
}
|
|
case key == "positional":
|
|
key = ""
|
|
positional = true
|
|
default:
|
|
return "", fmt.Errorf("unrecognized tag '%s' on field %s", key, tag)
|
|
}
|
|
}
|
|
|
|
if len(key) == 0 && !positional {
|
|
key = "--" + strings.ToLower(field.Name)
|
|
}
|
|
|
|
var value string
|
|
|
|
switch fv := v.FieldByIndex(field.Index); fv.Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
if fv.Int() == 0 {
|
|
break
|
|
}
|
|
fallthrough
|
|
default:
|
|
value = fmt.Sprintf("%v", fv.Interface())
|
|
}
|
|
|
|
flagValue, isFlag := field.Tag.Lookup("flag")
|
|
|
|
if isFlag {
|
|
if value != "true" {
|
|
if flagValue == "true" {
|
|
value = flagValue
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(value) == 0 {
|
|
if defaultString, hasDefault := field.Tag.Lookup("default"); hasDefault {
|
|
value = defaultString
|
|
}
|
|
|
|
if len(value) == 0 {
|
|
return "", nil
|
|
}
|
|
}
|
|
|
|
if len(key) == 0 {
|
|
return value, nil
|
|
}
|
|
|
|
if isFlag {
|
|
if value == "true" {
|
|
return key, nil
|
|
}
|
|
|
|
return "", nil
|
|
}
|
|
|
|
if len(value) == 0 {
|
|
return key, nil
|
|
}
|
|
|
|
return fmt.Sprintf("%s=%s", key, value), nil
|
|
})
|
|
}
|
|
|
|
func gatherArgs(v reflect.Value, visit func(v reflect.Value, field reflect.StructField) (string, error)) (args Args, err error) {
|
|
for i := 0; i < v.NumField(); i++ {
|
|
field := v.Type().Field(i)
|
|
|
|
var gathered Args
|
|
|
|
fieldType := field.Type
|
|
|
|
if fieldType.Kind() == reflect.Ptr {
|
|
fieldType.Elem()
|
|
}
|
|
|
|
if fieldType.Kind() == reflect.Struct {
|
|
gathered, err = gatherArgs(v.FieldByIndex(field.Index), visit)
|
|
} else {
|
|
var value string
|
|
|
|
if value, err = visit(v, field); len(value) > 0 {
|
|
gathered = Args{value}
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
args = append(args, gathered...)
|
|
}
|
|
|
|
return args, nil
|
|
}
|