Skip to content

Commit

Permalink
add fast node to test
Browse files Browse the repository at this point in the history
  • Loading branch information
alecps committed Aug 19, 2024
1 parent 81e7371 commit 51f1f14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
24 changes: 16 additions & 8 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/celo-org/celo-blockchain/core/types"
"github.com/celo-org/celo-blockchain/eth/downloader"
"github.com/celo-org/celo-blockchain/eth/tracers"
"github.com/celo-org/celo-blockchain/log"
"github.com/celo-org/celo-blockchain/mycelo/env"
Expand Down Expand Up @@ -392,15 +393,19 @@ func runStopNetworkAtL2BlockTest(ctx context.Context, t *testing.T, network test
func TestStopNetworkAtl2BlockSimple(t *testing.T) {
numValidators := 3
numFullNodes := 2
numFastNodes := 1
ac := test.AccountConfig(numValidators, 2)
gingerbreadBlock := common.Big0
l2BlockOG := big.NewInt(3)
gc, ec, err := test.BuildConfig(ac, gingerbreadBlock, l2BlockOG)
require.NoError(t, err)
network, _, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
network, shutdown, err := test.AddNetworkFullNodes(network, ec, uint64(numFullNodes))
network, _, err = test.AddNonValidatorNodes(network, ec, uint64(numFullNodes), downloader.FullSync)
require.NoError(t, err)
network, shutdown, err := test.AddNonValidatorNodes(network, ec, uint64(numFastNodes), downloader.FastSync)
require.NoError(t, err)

defer shutdown()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*40)
Expand All @@ -412,15 +417,19 @@ func TestStopNetworkAtl2BlockSimple(t *testing.T) {
func TestStopNetworkAtL2Block(t *testing.T) {
numValidators := 3
numFullNodes := 2
numFastNodes := 1
ac := test.AccountConfig(numValidators, 2)
gingerbreadBlock := common.Big0
l2BlockOG := big.NewInt(3)
gc, ec, err := test.BuildConfig(ac, gingerbreadBlock, l2BlockOG)
require.NoError(t, err)
network, _, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
network, shutdown, err := test.AddNetworkFullNodes(network, ec, uint64(numFullNodes))
network, _, err = test.AddNonValidatorNodes(network, ec, uint64(numFullNodes), downloader.FullSync)
require.NoError(t, err)
network, shutdown, err := test.AddNonValidatorNodes(network, ec, uint64(numFastNodes), downloader.FastSync)
require.NoError(t, err)

defer shutdown()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*400)
Expand All @@ -431,7 +440,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
shutdown()

// Restart nodes with --l2-migration-block set to the next block
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 1, 1, 1})
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 1, 1, 1, 1})
require.NoError(t, err)

l2BlockPlusOne := new(big.Int).Add(l2BlockOG, big.NewInt(1))
Expand All @@ -441,7 +450,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
shutdown()

// Restart nodes with --l2-migration-block set to the same block
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 1, 1, 1})
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 1, 1, 1, 1})
require.NoError(t, err)

runStopNetworkAtL2BlockTest(ctx, t, network, l2BlockPlusOne)
Expand All @@ -451,7 +460,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
// Restart nodes with different --l2-migration-block offsets
// If 2/3 validators (validators are the first 3 nodes in the network array)
// have the same migration block, the network should not be able to add any more blocks
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 2, 2, 2})
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 1, 2, 2, 2, 2})
require.NoError(t, err)

runStopNetworkAtL2BlockTest(ctx, t, network, l2BlockPlusOne)
Expand All @@ -461,7 +470,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
// Restart nodes with different --l2-migration-block offsets
// If 2/3 validators (validators are the first 3 nodes in the network array)
// have a greater migration block, the rest of the network should be able to add more blocks
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 2, 2, 2, 2})
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{1, 2, 2, 2, 2, 2})
require.NoError(t, err)

l2BlockPlusTwo := new(big.Int).Add(l2BlockOG, big.NewInt(2))
Expand All @@ -472,8 +481,7 @@ func TestStopNetworkAtL2Block(t *testing.T) {
shutdown()

// Restart nodes with --l2-migration-block set to a prev block
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{-1, -1, -1, -1, -1})
// TODO(Alec) it would actually be nice to return errors here, require.Error(t, err, core.ErrL2Migration.Error())
err = network.RestartNetworkWithMigrationBlockOffsets(l2BlockOG, []int64{-1, -1, -1, -1, -1, -1})
require.NoError(t, err)

// The network should be unchanged
Expand Down
32 changes: 21 additions & 11 deletions test/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ type Node struct {
SentTxs []*types.Transaction
}

// NewFullNode creates a new running (non-validator) node with the provided config.
func NewFullNode(
// NewNonValidatorNode creates a new running non-validator node with the provided config.
func NewNonValidatorNode(
nc *node.Config,
ec *eth.Config,
genesis *core.Genesis,
syncmode downloader.SyncMode,
) (*Node, error) {

// Copy the node config so we can modify it without damaging the original
Expand Down Expand Up @@ -145,6 +146,14 @@ func NewFullNode(
ecCopy.Genesis = genesis
ecCopy.NetworkId = genesis.Config.ChainID.Uint64()
ecCopy.Istanbul.Validator = false
ecCopy.SyncMode = syncmode

// We set these values here to avoid a panic in the eth service when these values are unset during fast sync
if syncmode == downloader.FastSync {
ecCopy.TrieCleanCache = 5
ecCopy.TrieDirtyCache = 5
ecCopy.SnapshotCache = 5
}

node := &Node{
Config: &ncCopy,
Expand Down Expand Up @@ -500,25 +509,26 @@ func NewNetwork(accounts *env.AccountsConfig, gc *genesis.Config, ec *eth.Config
return network, shutdown, nil
}

func AddNetworkFullNodes(network Network, ec *eth.Config, numFullNodes uint64) (Network, func(), error) {
var fullNodes Network = make([]*Node, numFullNodes)
// AddNonValidatorNodes Adds non-validator nodes to the network with the specified sync mode.
func AddNonValidatorNodes(network Network, ec *eth.Config, numNodes uint64, syncmode downloader.SyncMode) (Network, func(), error) {
var nodes Network = make([]*Node, numNodes)
genesis := network[0].EthConfig.Genesis

for i := uint64(0); i < numFullNodes; i++ {
n, err := NewFullNode(baseNodeConfig, ec, genesis)
for i := uint64(0); i < numNodes; i++ {
n, err := NewNonValidatorNode(baseNodeConfig, ec, genesis, syncmode)
if err != nil {
return nil, nil, fmt.Errorf("failed to build full node for network: %v", err)
}
fullNodes[i] = n
nodes[i] = n
}

// Connect nodes to each other
for i := range fullNodes {
fullNodes[i].AddPeers(network...)
fullNodes[i].AddPeers(fullNodes[i+1:]...)
for i := range nodes {
nodes[i].AddPeers(network...)
nodes[i].AddPeers(nodes[i+1:]...)
}

network = append(network, fullNodes...)
network = append(network, nodes...)

// Give nodes some time to connect. Also there is a race condition in
// miner.worker its field snapshotBlock is set only when new transactions
Expand Down

0 comments on commit 51f1f14

Please sign in to comment.