erigon-pulse/cmd/devnet/devnetutils/utils_test.go
Mark Holt f110102023
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 12:35:22 +01:00

126 lines
3.3 KiB
Go

package devnetutils
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestHexToInt(t *testing.T) {
testCases := []struct {
hexStr string
expected uint64
}{
{"0x0", 0},
{"0x32424", 205860},
{"0x200", 512},
{"0x39", 57},
}
for _, testCase := range testCases {
got := HexToInt(testCase.hexStr)
require.EqualValues(t, testCase.expected, got)
}
}
func TestUniqueIDFromEnode(t *testing.T) {
testCases := []struct {
input string
expectedRes string
shouldError bool
}{
{
input: "",
expectedRes: "",
shouldError: true,
},
{
input: "enode://11c368e7a2775951d66ff155a982844ccd5219d10b53e310001e1e40c6a4e76c2f6e42f39acc1e4015cd3b7428765125214d89b07ca5fa2c19ac94746fc360b0@127.0.0.1:63380?discport=0",
expectedRes: "enode://11c368e7a2775951d66ff155a982844ccd5219d10b53e310001e1e40c6a4e76c2f6e42f39acc1e4015cd3b7428765125214d89b07ca5fa2c19ac94746fc360b0@127.0.0.1:63380",
shouldError: false,
},
{
input: "enode://11c368e7a2775951d66ff155a982844ccd5219d10b53e310001e1e40c6a4e76c2f6e42f39acc1e4015cd3b7428765125214d89b07ca5fa2c19ac94746fc360b0@127.0.0.1:63380",
expectedRes: "enode://11c368e7a2775951d66ff155a982844ccd5219d10b53e310001e1e40c6a4e76c2f6e42f39acc1e4015cd3b7428765125214d89b07ca5fa2c19ac94746fc360b0@127.0.0.1:63380",
shouldError: false,
},
{
input: "enode://11c368e7a2775951d66ff155a982844ccd5219d10b53e310001e1e40c6a4e76c2f6e42f39acc1e4015cd3b7428765125214d89b07ca5fa2c19ac94746fc360b0@127.0.0.1:63380discport=0",
expectedRes: "",
shouldError: true,
},
}
for _, testCase := range testCases {
got, err := UniqueIDFromEnode(testCase.input)
if testCase.shouldError && err == nil {
t.Errorf("expected error to happen, got no error")
}
if !testCase.shouldError && err != nil {
t.Errorf("expected no error, got %s", err)
}
require.EqualValues(t, got, testCase.expectedRes)
}
}
func TestNamespaceAndSubMethodFromMethod(t *testing.T) {
expectedError := fmt.Errorf("invalid string to split")
testCases := []struct {
method string
expectedNamespace string
expectedSubMethod string
shouldError bool
expectedError error
}{
{
"eth_logs",
"eth",
"logs",
false,
nil,
},
{
"ethNewHeads",
"",
"",
true,
expectedError,
},
{
"",
"",
"",
true,
expectedError,
},
}
for _, testCase := range testCases {
namespace, subMethod, err := NamespaceAndSubMethodFromMethod(testCase.method)
require.EqualValues(t, testCase.expectedNamespace, namespace)
require.EqualValues(t, testCase.expectedSubMethod, subMethod)
require.EqualValues(t, testCase.expectedError, err)
if testCase.shouldError {
require.Errorf(t, testCase.expectedError, expectedError.Error())
}
}
}
func TestGenerateTopic(t *testing.T) {
testCases := []struct {
signature string
expected string
}{
{"random string", "0x0d9d89437ff2d48ce95779dc9457bc48287b75a573eddbf50954efac5a97c4b9"},
{"SubscriptionEvent()", "0x67abc7edb0ab50964ef0e90541d39366b9c69f6f714520f2ff4570059ee8ad80"},
{"", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
}
for _, testCase := range testCases {
got := GenerateTopic(testCase.signature)
require.EqualValues(t, testCase.expected, fmt.Sprintf("%s", got[0]))
}
}