erigon-pulse/cmd/devnet/tests/context.go
Mark Holt b05ffc909d
Fixes for Bor Block Production Synchronization (#9162)
This PR contains 3 fixes for interaction between the Bor mining loop and
the TX pool which where causing the regular creation of blocks with zero
transactions.

* Mining/Tx pool block synchronization
The synchronization of the tx pool between the sync loop and the mining
loop has been changed so that both are triggered by the same event and
synchronized via a sync.Cond rather than a polling loop with a hard
coded loop limit. This means that mining now waits for the pool to be
updated from the previous block before it starts the mining process.
* Txpool Startup consolidated into its MainLoop
Previously the tx pool start process was dynamically triggered at
various points in the code. This has all now been moved to the start of
the main loop. This is necessary to avoid a timing hole which can leave
the mining loop hanging waiting for a previously block broadcast which
it missed due to its delay start.
* Mining listens for block broadcast to avoid duplicate mining
operations
The mining loop for bor has a recommit timer in case blocks re not
produced on time. However in the case of sprint transitions where the
seal publication is delayed this can lead to duplicate block production.
This is suppressed by introducing a `waiting` state which is exited upon
the block being broadcast from the sealing operation.
2024-01-10 17:12:15 +00:00

80 lines
2.5 KiB
Go

package tests
import (
"fmt"
"os"
"runtime"
"strconv"
"testing"
"github.com/ledgerwatch/erigon-lib/chain/networkname"
"github.com/ledgerwatch/erigon/cmd/devnet/devnet"
"github.com/ledgerwatch/erigon/cmd/devnet/networks"
"github.com/ledgerwatch/erigon/cmd/devnet/services"
"github.com/ledgerwatch/erigon/cmd/devnet/services/polygon"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/log/v3"
)
func initDevnet(chainName string, dataDir string, producerCount int, gasLimit uint64, logger log.Logger, consoleLogLevel log.Lvl, dirLogLevel log.Lvl) (devnet.Devnet, error) {
const baseRpcHost = "localhost"
const baseRpcPort = 8545
switch chainName {
case networkname.BorDevnetChainName:
heimdallGrpcAddr := polygon.HeimdallGrpcAddressDefault
const sprintSize uint64 = 0
return networks.NewBorDevnetWithLocalHeimdall(dataDir, baseRpcHost, baseRpcPort, heimdallGrpcAddr, sprintSize, producerCount, gasLimit, logger, consoleLogLevel, dirLogLevel), nil
case networkname.DevChainName:
return networks.NewDevDevnet(dataDir, baseRpcHost, baseRpcPort, producerCount, gasLimit, logger, consoleLogLevel, dirLogLevel), nil
case "":
envChainName, _ := os.LookupEnv("DEVNET_CHAIN")
if envChainName == "" {
envChainName = networkname.DevChainName
}
return initDevnet(envChainName, dataDir, producerCount, gasLimit, logger, consoleLogLevel, dirLogLevel)
default:
return nil, fmt.Errorf("unknown network: '%s'", chainName)
}
}
func ContextStart(t *testing.T, chainName string) (devnet.Context, error) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: TempDir RemoveAll cleanup error: remove dev-0\\clique\\db\\clique\\mdbx.dat: The process cannot access the file because it is being used by another process")
}
debug.RaiseFdLimit()
logger := log.New()
dataDir := t.TempDir()
envProducerCount, _ := os.LookupEnv("PRODUCER_COUNT")
if envProducerCount == "" {
envProducerCount = "1"
}
producerCount, _ := strconv.ParseUint(envProducerCount, 10, 64)
// TODO get log levels from env
var dirLogLevel log.Lvl = log.LvlTrace
var consoleLogLevel log.Lvl = log.LvlCrit
var network devnet.Devnet
network, err := initDevnet(chainName, dataDir, int(producerCount), 0, logger, consoleLogLevel, dirLogLevel)
if err != nil {
return nil, fmt.Errorf("ContextStart initDevnet failed: %w", err)
}
runCtx, err := network.Start(logger)
if err != nil {
return nil, fmt.Errorf("ContextStart devnet start failed: %w", err)
}
t.Cleanup(services.UnsubscribeAll)
t.Cleanup(network.Stop)
return runCtx, nil
}