From ce0118cb257ddc77782fa3fea28f8f83541cdb0f Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Thu, 21 Dec 2023 15:34:22 -0300 Subject: [PATCH] fix a bug of builder ignoring docker networks passed to it --- docker/test_env/ethereum_env.go | 46 ++++++++++++---------------- docker/test_env/ethereum_env_test.go | 14 +++++++++ 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/docker/test_env/ethereum_env.go b/docker/test_env/ethereum_env.go index c4b7f0111..9f2834ead 100644 --- a/docker/test_env/ethereum_env.go +++ b/docker/test_env/ethereum_env.go @@ -146,6 +146,7 @@ func (b *EthereumNetworkBuilder) buildNetworkConfig() EthereumNetwork { n.Containers = b.existingConfig.Containers } + n.DockerNetworkNames = b.dockerNetworks n.WaitForFinalization = b.waitForFinalization n.EthereumChainConfig = b.ethereumChainConfig n.t = b.t @@ -293,18 +294,15 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error } var net blockchain.EVMNetwork - var networkNames []string if *en.ConsensusLayer != ConsensusLayer_Prysm { return blockchain.EVMNetwork{}, RpcProvider{}, fmt.Errorf("unsupported consensus layer: %s. Use 'prysm'", *en.ConsensusLayer) } - singleNetwork, err := en.getOrCreateDockerNetworks() + dockerNetworks, err := en.getOrCreateDockerNetworks() if err != nil { return blockchain.EVMNetwork{}, RpcProvider{}, err } - networkNames = append(networkNames, singleNetwork...) - var generatedDataHostDir, valKeysDir string // create host directories and run genesis containers only if we are NOT recreating existing containers @@ -356,13 +354,13 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error var clientErr error switch en.ExecutionLayer { case ExecutionLayer_Geth: - client, clientErr = NewGeth2(singleNetwork, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Geth2)) + client, clientErr = NewGeth2(dockerNetworks, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Geth2)) case ExecutionLayer_Nethermind: - client, clientErr = NewNethermind(singleNetwork, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Nethermind)) + client, clientErr = NewNethermind(dockerNetworks, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Nethermind)) case ExecutionLayer_Erigon: - client, clientErr = NewErigon(singleNetwork, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Erigon)) + client, clientErr = NewErigon(dockerNetworks, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Erigon)) case ExecutionLayer_Besu: - client, clientErr = NewBesu(singleNetwork, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Besu)) + client, clientErr = NewBesu(dockerNetworks, en.EthereumChainConfig, generatedDataHostDir, ConsensusLayer_Prysm, en.setExistingContainerName(ContainerType_Besu)) default: return blockchain.EVMNetwork{}, RpcProvider{}, fmt.Errorf("unsupported execution layer: %s", en.ExecutionLayer) } @@ -378,7 +376,7 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error return blockchain.EVMNetwork{}, RpcProvider{}, err } - beacon, err := NewPrysmBeaconChain(singleNetwork, en.EthereumChainConfig, generatedDataHostDir, client.GetInternalExecutionURL(), en.setExistingContainerName(ContainerType_PrysmBeacon)) + beacon, err := NewPrysmBeaconChain(dockerNetworks, en.EthereumChainConfig, generatedDataHostDir, client.GetInternalExecutionURL(), en.setExistingContainerName(ContainerType_PrysmBeacon)) if err != nil { return blockchain.EVMNetwork{}, RpcProvider{}, err } @@ -389,7 +387,7 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error return blockchain.EVMNetwork{}, RpcProvider{}, err } - validator, err := NewPrysmValidator(singleNetwork, en.EthereumChainConfig, generatedDataHostDir, valKeysDir, beacon. + validator, err := NewPrysmValidator(dockerNetworks, en.EthereumChainConfig, generatedDataHostDir, valKeysDir, beacon. InternalBeaconRpcProvider, en.setExistingContainerName(ContainerType_PrysmVal)) if err != nil { return blockchain.EVMNetwork{}, RpcProvider{}, err @@ -406,7 +404,7 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error return blockchain.EVMNetwork{}, RpcProvider{}, err } - en.DockerNetworkNames = networkNames + en.DockerNetworkNames = dockerNetworks net.ChainID = int64(en.EthereumChainConfig.ChainID) // use a higher value than the default, because eth2 is slower than dev-mode eth1 net.Timeout = blockchain.JSONStrDuration{Duration: time.Duration(4 * time.Minute)} @@ -465,7 +463,6 @@ func (en *EthereumNetwork) startPos() (blockchain.EVMNetwork, RpcProvider, error func (en *EthereumNetwork) startPow() (blockchain.EVMNetwork, RpcProvider, error) { var net blockchain.EVMNetwork - var networkNames []string rpcProvider := RpcProvider{ privateHttpUrls: []string{}, privatelWsUrls: []string{}, @@ -476,20 +473,18 @@ func (en *EthereumNetwork) startPow() (blockchain.EVMNetwork, RpcProvider, error if en.ExecutionLayer != ExecutionLayer_Geth { return blockchain.EVMNetwork{}, RpcProvider{}, fmt.Errorf("unsupported execution layer: %s", en.ExecutionLayer) } - singleNetwork, err := en.getOrCreateDockerNetworks() + dockerNetworks, err := en.getOrCreateDockerNetworks() if err != nil { return blockchain.EVMNetwork{}, RpcProvider{}, err } - geth := NewGeth(singleNetwork, en.EthereumChainConfig, en.setExistingContainerName(ContainerType_Geth)).WithTestInstance(en.t) + geth := NewGeth(dockerNetworks, en.EthereumChainConfig, en.setExistingContainerName(ContainerType_Geth)).WithTestInstance(en.t) network, docker, err := geth.StartContainer() if err != nil { return blockchain.EVMNetwork{}, RpcProvider{}, err } net = network - networkNames = append(networkNames, singleNetwork...) - containers := EthereumNetworkContainers{ { ContainerName: geth.ContainerName, @@ -504,25 +499,22 @@ func (en *EthereumNetwork) startPow() (blockchain.EVMNetwork, RpcProvider, error rpcProvider.publiclHttpUrls = append(rpcProvider.publiclHttpUrls, geth.ExternalHttpUrl) rpcProvider.publicsUrls = append(rpcProvider.publicsUrls, geth.ExternalWsUrl) - en.DockerNetworkNames = networkNames + en.DockerNetworkNames = dockerNetworks return net, rpcProvider, nil } func (en *EthereumNetwork) getOrCreateDockerNetworks() ([]string, error) { - var networkNames []string + if len(en.DockerNetworkNames) != 0 { + return en.DockerNetworkNames, nil + } - if len(en.DockerNetworkNames) == 0 { - network, err := docker.CreateNetwork(logging.GetTestLogger(en.t)) - if err != nil { - return networkNames, err - } - networkNames = []string{network.Name} - } else { - networkNames = en.DockerNetworkNames + network, err := docker.CreateNetwork(logging.GetTestLogger(en.t)) + if err != nil { + return []string{}, err } - return networkNames, nil + return []string{network.Name}, nil } func (en *EthereumNetwork) Describe() string { diff --git a/docker/test_env/ethereum_env_test.go b/docker/test_env/ethereum_env_test.go index e7ce40fe7..05bc2be5d 100644 --- a/docker/test_env/ethereum_env_test.go +++ b/docker/test_env/ethereum_env_test.go @@ -129,3 +129,17 @@ func TestEth2ExecClientFromEnv(t *testing.T) { require.NoError(t, err, "Builder validation failed") require.Equal(t, ExecutionLayer_Besu, cfg.ExecutionLayer, "Execution layer should be Besu") } + +func TestEth2CustomDockerNetworks(t *testing.T) { + networks := []string{"test-network"} + + builder := NewEthereumNetworkBuilder() + cfg, err := builder. + WithConsensusType(ConsensusType_PoS). + WithConsensusLayer(ConsensusLayer_Prysm). + WithExecutionLayer(ExecutionLayer_Geth). + WithDockerNetworks(networks). + Build() + require.NoError(t, err, "Builder validation failed") + require.Equal(t, networks, cfg.DockerNetworkNames, "Incorrect docker networks in config") +}