erigon-pulse/cmd/devnet/devnetutils/utils_test.go
Mark Holt 415cf86250
refactor to allow switchable consensus and multiple communicating nodes (#7646)
This branch is intended to allow the devnet to be used for testing
multiple consents types beyond the default clique. It is initially being
used to test Bor consensus for polygon.

It also has the following refactoring:

### 1.  Network configuration

The two node arg building functions miningNodeArgs and nonMiningNodeArgs
have been replaced with a configuration struct which is used to
configure:

```go
network := &node.Network{
		DataDir: dataDir,
		Chain:   networkname.DevChainName,
		//Chain:              networkname.BorDevnetChainName,
		Logger:             logger,
		BasePrivateApiAddr: "localhost:9090",
		BaseRPCAddr:        "localhost:8545",
		Nodes: []node.NetworkNode{
			&node.Miner{},
			&node.NonMiner{},
		},
	}
```
and start multiple nodes

```go
network.Start()
```
Network start will create a network of nodes ensuring that all nodes are
configured with non clashing network ports set via command line
arguments on start-up.

### 2. Request Routing

The `RequestRouter` has been updated to take a 'target' rather than
using a static dispatcher which routes to a single node on the network.
Each node in the network has its own request generator so command and
services have more flexibility in request routing and
`ExecuteAllMethods` currently takes the `node.Network` as an argument
and can pick which node (node 0 for the moment) to send requests to.
2023-06-04 20:53:05 +01:00

109 lines
3.0 KiB
Go

package devnetutils
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
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]))
}
}