2022-11-03 02:45:36 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
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
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/devnet"
|
2022-11-03 02:45:36 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/devnetutils"
|
2023-06-04 19:53:05 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
|
2023-07-18 08:47:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/scenarios"
|
2022-11-03 02:45:36 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2023-05-20 13:48:16 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-11-03 02:45:36 +00:00
|
|
|
)
|
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
func init() {
|
|
|
|
scenarios.RegisterStepHandlers(
|
|
|
|
scenarios.StepHandler(InitSubscriptions),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-20 22:10:18 +00:00
|
|
|
var subscriptions map[string]map[requests.SubMethod]*Subscription
|
|
|
|
|
|
|
|
func GetSubscription(chainName string, method requests.SubMethod) *Subscription {
|
|
|
|
if methods, ok := subscriptions[chainName]; ok {
|
|
|
|
if subscription, ok := methods[method]; ok {
|
|
|
|
return subscription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Subscription houses the client subscription, name and channel for its delivery
|
|
|
|
type Subscription struct {
|
|
|
|
Client *rpc.Client
|
|
|
|
ClientSub *rpc.ClientSubscription
|
|
|
|
Name requests.SubMethod
|
|
|
|
SubChan chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSubscription returns a new Subscription instance
|
|
|
|
func NewSubscription(name requests.SubMethod) *Subscription {
|
|
|
|
return &Subscription{
|
|
|
|
Name: name,
|
|
|
|
SubChan: make(chan interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitSubscriptions(ctx context.Context, methods []requests.SubMethod) {
|
|
|
|
logger := devnet.Logger(ctx)
|
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
logger.Trace("CONNECTING TO WEBSOCKETS AND SUBSCRIBING TO METHODS...")
|
2023-07-20 22:10:18 +00:00
|
|
|
if err := subscribeAll(ctx, methods); err != nil {
|
2023-05-22 07:46:50 +00:00
|
|
|
logger.Error("failed to subscribe to all methods", "error", err)
|
2023-01-10 17:43:58 +00:00
|
|
|
return
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|
2023-01-10 17:43:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 02:45:36 +00:00
|
|
|
// subscribe connects to a websocket client and returns the subscription handler and a channel buffer
|
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
|
|
|
func subscribe(client *rpc.Client, method requests.SubMethod, args ...interface{}) (*Subscription, error) {
|
|
|
|
methodSub := NewSubscription(method)
|
2023-01-10 17:43:58 +00:00
|
|
|
|
|
|
|
namespace, subMethod, err := devnetutils.NamespaceAndSubMethodFromMethod(string(method))
|
2022-11-03 02:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get namespace and submethod from method: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := append([]interface{}{subMethod}, args...)
|
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
sub, err := client.Subscribe(context.Background(), namespace, methodSub.SubChan, arr...)
|
2022-11-03 02:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("client failed to subscribe: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
methodSub.ClientSub = sub
|
2023-07-20 22:10:18 +00:00
|
|
|
methodSub.Client = client
|
2023-01-10 17:43:58 +00:00
|
|
|
|
|
|
|
return methodSub, nil
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 22:10:18 +00:00
|
|
|
func subscribeToMethod(target string, method requests.SubMethod, logger log.Logger) (*Subscription, error) {
|
|
|
|
client, err := rpc.DialWebsocket(context.Background(), "ws://"+target, "", logger)
|
|
|
|
|
2022-11-03 02:45:36 +00:00
|
|
|
if err != nil {
|
2023-01-10 17:43:58 +00:00
|
|
|
return nil, fmt.Errorf("failed to dial websocket: %v", err)
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
sub, err := subscribe(client, method)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error subscribing to method: %v", err)
|
|
|
|
}
|
2022-11-03 02:45:36 +00:00
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeAll closes all the client subscriptions and empties their global subscription channel
|
|
|
|
func UnsubscribeAll() {
|
2023-07-20 22:10:18 +00:00
|
|
|
if subscriptions == nil {
|
2023-05-15 14:30:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-20 22:10:18 +00:00
|
|
|
|
|
|
|
for _, methods := range subscriptions {
|
|
|
|
|
|
|
|
for _, methodSub := range methods {
|
|
|
|
if methodSub != nil {
|
|
|
|
methodSub.ClientSub.Unsubscribe()
|
|
|
|
for len(methodSub.SubChan) > 0 {
|
|
|
|
<-methodSub.SubChan
|
|
|
|
}
|
|
|
|
methodSub.SubChan = nil // avoid memory leak
|
2023-01-10 17:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 17:43:58 +00:00
|
|
|
// subscribeAll subscribes to the range of methods provided
|
2023-07-20 22:10:18 +00:00
|
|
|
func subscribeAll(ctx context.Context, methods []requests.SubMethod) error {
|
|
|
|
subscriptions = map[string]map[requests.SubMethod]*Subscription{}
|
|
|
|
logger := devnet.Logger(ctx)
|
|
|
|
|
|
|
|
for _, network := range devnet.Networks(ctx) {
|
|
|
|
subscriptions[network.Chain] = map[requests.SubMethod]*Subscription{}
|
|
|
|
|
|
|
|
for _, method := range methods {
|
|
|
|
sub, err := subscribeToMethod(devnet.HTTPHost(network.Nodes[0]), method, logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
subscriptions[network.Chain][method] = sub
|
2023-01-10 17:43:58 +00:00
|
|
|
}
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|
2023-01-10 17:43:58 +00:00
|
|
|
|
|
|
|
return nil
|
2022-11-03 02:45:36 +00:00
|
|
|
}
|