erigon-pulse/cmd/devnet/commands/block.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

197 lines
5.7 KiB
Go

package commands
import (
"context"
"fmt"
"time"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/cmd/devnet/devnet"
"github.com/ledgerwatch/erigon/cmd/devnet/devnetutils"
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
"github.com/ledgerwatch/erigon/cmd/devnet/scenarios"
"github.com/ledgerwatch/erigon/cmd/devnet/services"
"github.com/ledgerwatch/erigon/common/hexutil"
)
func init() {
scenarios.MustRegisterStepHandlers(
scenarios.StepHandler(SendTxWithDynamicFee),
scenarios.StepHandler(AwaitBlocks),
)
}
func callSendTx(node devnet.Node, value uint64, toAddr, fromAddr string, logger log.Logger) (*libcommon.Hash, error) {
logger.Info("Sending tx", "value", value, "to", toAddr, "from", fromAddr)
// get the latest nonce for the next transaction
nonce, err := services.GetNonce(node, libcommon.HexToAddress(fromAddr))
if err != nil {
logger.Error("failed to get latest nonce", "error", err)
return nil, err
}
// create a non-contract transaction and sign it
signedTx, _, err := services.CreateTransaction(toAddr, value, nonce)
if err != nil {
logger.Error("failed to create a transaction", "error", err)
return nil, err
}
// send the signed transaction
hash, err := node.SendTransaction(signedTx)
if err != nil {
logger.Error("failed to send transaction", "error", err)
return nil, err
}
hashes := map[libcommon.Hash]bool{*hash: true}
if _, err = services.SearchReservesForTransactionHash(hashes, logger); err != nil {
return nil, fmt.Errorf("failed to call contract tx: %v", err)
}
return hash, nil
}
func SendTxWithDynamicFee(ctx context.Context, toAddr, fromAddr string, amount uint64) ([]*libcommon.Hash, error) {
// get the latest nonce for the next transaction
node := devnet.SelectNode(ctx)
logger := devnet.Logger(ctx)
nonce, err := services.GetNonce(node, libcommon.HexToAddress(fromAddr))
if err != nil {
logger.Error("failed to get latest nonce", "error", err)
return nil, err
}
lowerThanBaseFeeTxs, higherThanBaseFeeTxs, err := services.CreateManyEIP1559TransactionsRefWithBaseFee2(ctx, toAddr, &nonce)
if err != nil {
logger.Error("failed CreateManyEIP1559TransactionsRefWithBaseFee", "error", err)
return nil, err
}
higherThanBaseFeeHashlist, err := services.SendManyTransactions(ctx, higherThanBaseFeeTxs)
if err != nil {
logger.Error("failed SendManyTransactions(higherThanBaseFeeTxs)", "error", err)
return nil, err
}
lowerThanBaseFeeHashlist, err := services.SendManyTransactions(ctx, lowerThanBaseFeeTxs)
if err != nil {
logger.Error("failed SendManyTransactions(lowerThanBaseFeeTxs)", "error", err)
return nil, err
}
services.CheckTxPoolContent(ctx, 100, 0, 100)
services.CheckTxPoolContent(ctx, -1, -1, -1)
hashmap := make(map[libcommon.Hash]bool)
for _, hash := range higherThanBaseFeeHashlist {
hashmap[*hash] = true
}
if _, err = services.SearchReservesForTransactionHash(hashmap, logger); err != nil {
return nil, fmt.Errorf("failed to call contract tx: %v", err)
}
logger.Info("SUCCESS: All transactions in pending pool included in blocks")
return append(lowerThanBaseFeeHashlist, higherThanBaseFeeHashlist...), nil
}
func AwaitBlocks(ctx context.Context, sleepTime time.Duration) error {
logger := devnet.Logger(ctx)
for i := 1; i <= 20; i++ {
node := devnet.SelectNode(ctx)
blockNumber, err := node.BlockNumber()
if err != nil {
logger.Error("FAILURE => error getting block number", "error", err)
} else {
logger.Info("Got block number", "blockNum", blockNumber)
}
pendingSize, queuedSize, baseFeeSize, err := node.TxpoolContent()
if err != nil {
logger.Error("FAILURE getting txpool content", "error", err)
} else {
logger.Info("Txpool subpool sizes", "pending", pendingSize, "queued", queuedSize, "basefee", baseFeeSize)
}
time.Sleep(sleepTime)
}
return nil
}
func callContractTx(node devnet.Node, logger log.Logger) (*libcommon.Hash, error) {
// hashset to hold hashes for search after mining
hashes := make(map[libcommon.Hash]bool)
// get the latest nonce for the next transaction
nonce, err := services.GetNonce(node, libcommon.HexToAddress(services.DevAddress))
if err != nil {
logger.Error("failed to get latest nonce", "error", err)
return nil, err
}
// subscriptionContract is the handler to the contract for further operations
signedTx, address, subscriptionContract, transactOpts, err := services.DeploySubsriptionContract(nonce)
if err != nil {
logger.Error("failed to create transaction", "error", err)
return nil, err
}
// send the contract transaction to the node
hash, err := node.SendTransaction(signedTx)
if err != nil {
logger.Error("failed to send transaction", "error", err)
return nil, err
}
hashes[*hash] = true
logger.Info("")
eventHash, err := services.EmitFallbackEvent(node, subscriptionContract, transactOpts, logger)
if err != nil {
logger.Error("failed to emit events", "error", err)
return nil, err
}
hashes[*eventHash] = true
txToBlockMap, err := services.SearchReservesForTransactionHash(hashes, logger)
if err != nil {
return nil, fmt.Errorf("failed to call contract tx: %v", err)
}
blockNum := (*txToBlockMap)[*eventHash]
block, err := node.GetBlockByNumber(devnetutils.HexToInt(blockNum), true)
if err != nil {
return nil, err
}
expectedLog := requests.BuildLog(*eventHash, blockNum, address,
devnetutils.GenerateTopic(services.SolContractMethodSignature), hexutility.Bytes{}, hexutil.Uint(1),
block.Result.Hash, hexutil.Uint(0), false)
if err = node.GetAndCompareLogs(0, 20, expectedLog); err != nil {
return nil, fmt.Errorf("failed to get logs: %v", err)
}
return hash, nil
}