erigon-pulse/cmd/devnet/requests/request_generator_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

261 lines
7.0 KiB
Go

package requests
import (
"testing"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/stretchr/testify/require"
)
func MockRequestGenerator(reqId int) *requestGenerator {
return &requestGenerator{
reqID: reqId,
client: nil,
}
}
func TestRequestGenerator_GetAdminNodeInfo(t *testing.T) {
testCases := []struct {
reqId int
expected string
}{
{1, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":1}`},
{2, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":2}`},
{3, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":3}`},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.adminNodeInfo()
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetBalance(t *testing.T) {
testCases := []struct {
reqId int
address libcommon.Address
blockNum BlockNumber
expected string
}{
{
1,
libcommon.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
BlockNumbers.Latest,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x67b1d87101671b127f5f8714789c7192f7ad340e","latest"],"id":1}`,
},
{
2,
libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
BlockNumbers.Earliest,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x71562b71999873db5b286df957af199ec94617f7","earliest"],"id":2}`,
},
{
3,
libcommon.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
BlockNumbers.Pending,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x1b5fd2fed153fa7fac43300273c70c068bfa406a","pending"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.getBalance(testCase.address, testCase.blockNum)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetBlockByNumber(t *testing.T) {
testCases := []struct {
reqId int
blockNum uint64
withTxs bool
expected string
}{
{
1,
2,
false,
`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x2",false],"id":1}`,
},
{
2,
16,
false,
`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x10",false],"id":2}`,
},
{
3,
100,
true,
`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x64",true],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.getBlockByNumber(testCase.blockNum, testCase.withTxs)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetLogs(t *testing.T) {
testCases := []struct {
reqId int
fromBlock uint64
toBlock uint64
address libcommon.Address
expected string
}{
{
1,
1843,
1848,
libcommon.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x733","toBlock":"0x738","address":"0x67b1d87101671b127f5f8714789c7192f7ad340e"}],"id":1}`,
},
{
2,
12,
12,
libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0xc","toBlock":"0xc","address":"0x71562b71999873db5b286df957af199ec94617f7"}],"id":2}`,
},
{
3,
0,
123456789,
libcommon.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x0","toBlock":"0x75bcd15","address":"0x1b5fd2fed153fa7fac43300273c70c068bfa406a"}],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.getLogs(testCase.fromBlock, testCase.toBlock, testCase.address)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetTransactionCount(t *testing.T) {
testCases := []struct {
reqId int
address libcommon.Address
blockNum BlockNumber
expected string
}{
{
1,
libcommon.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
BlockNumbers.Latest,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x67b1d87101671b127f5f8714789c7192f7ad340e","latest"],"id":1}`,
},
{
2,
libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
BlockNumbers.Earliest,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x71562b71999873db5b286df957af199ec94617f7","earliest"],"id":2}`,
},
{
3,
libcommon.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
BlockNumbers.Pending,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x1b5fd2fed153fa7fac43300273c70c068bfa406a","pending"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.getTransactionCount(testCase.address, testCase.blockNum)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_SendRawTransaction(t *testing.T) {
testCases := []struct {
reqId int
signedTx []byte
expected string
}{
{
1,
libcommon.HexToHash("0x1cd73c7adf5b31f3cf94c67b9e251e699559d91c27664463fb5978b97f8b2d1b").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x1cd73c7adf5b31f3cf94c67b9e251e699559d91c27664463fb5978b97f8b2d1b"],"id":1}`,
},
{
2,
libcommon.HexToHash("0x1cfe7ce95a1694d8969365cb472ce4a0d3eed812c540fd7708bbe6941e34c4de").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x1cfe7ce95a1694d8969365cb472ce4a0d3eed812c540fd7708bbe6941e34c4de"],"id":2}`,
},
{
3,
libcommon.HexToHash("0x6f9e34c00812a80fa87df26208bbe69411e36d6a9f00b35444ef4181f6c483ca").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x6f9e34c00812a80fa87df26208bbe69411e36d6a9f00b35444ef4181f6c483ca"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.sendRawTransaction(testCase.signedTx)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_TxpoolContent(t *testing.T) {
testCases := []struct {
reqId int
expected string
}{
{1, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":1}`},
{2, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":2}`},
{3, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":3}`},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
_, got := reqGen.txpoolContent()
require.EqualValues(t, testCase.expected, got)
}
}
func TestParseResponse(t *testing.T) {
type Person struct {
Name string
Age int
}
testCases := []struct {
input interface{}
expected string
}{
{
Person{
Name: "Leonard",
Age: 10,
},
`{"Name":"Leonard","Age":10}`,
},
{
struct {
Person struct {
Name string
Age int
}
WorkID string
}{
Person: Person{
Name: "Uzi",
Age: 23,
},
WorkID: "123456",
},
`{"Person":{"Name":"Uzi","Age":23},"WorkID":"123456"}`,
},
}
for _, testCase := range testCases {
got, _ := parseResponse(testCase.input)
require.EqualValues(t, testCase.expected, got)
}
}