Skip to content

Commit afa3436

Browse files
author
Ilan
committed
Refactor Besu
1 parent 4d8ef3f commit afa3436

File tree

5 files changed

+47
-91
lines changed

5 files changed

+47
-91
lines changed

framework/components/blockchain/anvil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func defaultAnvil(in *Input) {
2525
// newAnvil deploy foundry anvil node
2626
func newAnvil(in *Input) (*Output, error) {
2727
defaultAnvil(in)
28-
req := baseRequest(in)
28+
req := baseRequest(in, false)
2929

3030
req.Image = in.Image
3131
req.AlwaysPullImage = in.PullImage
@@ -38,5 +38,5 @@ func newAnvil(in *Input) (*Output, error) {
3838

3939
framework.L.Info().Any("Cmd", strings.Join(entryPoint, " ")).Msg("Creating anvil with command")
4040

41-
return createGenericEvmContainer(in, req)
41+
return createGenericEvmContainer(in, req, false)
4242
}

framework/components/blockchain/anvil_zksync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RUN curl -L https://raw.githubusercontent.com/matter-labs/foundry-zksync/main/in
4343
// see: https://foundry-book.zksync.io/getting-started/installation#using-foundry-with-docker
4444
func newAnvilZksync(in *Input) (*Output, error) {
4545
defaultAnvilZksync(in)
46-
req := baseRequest(in)
46+
req := baseRequest(in, false)
4747

4848
tempDir, err := os.MkdirTemp(".", "anvil-zksync-dockercontext")
4949
if err != nil {
@@ -76,7 +76,7 @@ func newAnvilZksync(in *Input) (*Output, error) {
7676

7777
framework.L.Info().Any("Cmd", strings.Join(req.Entrypoint, " ")).Msg("Creating anvil zkSync with command")
7878

79-
output, err := createGenericEvmContainer(in, req)
79+
output, err := createGenericEvmContainer(in, req, false)
8080
if err != nil {
8181
return nil, err
8282
}
Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package blockchain
22

3-
import (
4-
"context"
5-
"fmt"
6-
"time"
7-
8-
"github.com/docker/docker/api/types/container"
9-
10-
"github.com/smartcontractkit/chainlink-testing-framework/framework"
11-
12-
"github.com/docker/go-connections/nat"
13-
"github.com/testcontainers/testcontainers-go"
14-
"github.com/testcontainers/testcontainers-go/wait"
15-
)
16-
173
const (
184
DefaultBesuPrivateKey1 = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"
195
DefaultBesuPrivateKey2 = "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3"
@@ -37,7 +23,11 @@ func defaultBesu(in *Input) {
3723

3824
func newBesu(in *Input) (*Output, error) {
3925
defaultBesu(in)
40-
ctx := context.Background()
26+
req := baseRequest(in, true)
27+
28+
req.Image = in.Image
29+
req.AlwaysPullImage = in.PullImage
30+
4131
defaultCmd := []string{
4232
"--network=dev",
4333
"--miner-enabled",
@@ -53,64 +43,7 @@ func newBesu(in *Input) (*Output, error) {
5343
"--data-path=/tmp/tmpDatdir",
5444
}
5545
entryPoint := append(defaultCmd, in.DockerCmdParamsOverrides...)
46+
req.Cmd = entryPoint
5647

57-
containerName := framework.DefaultTCName("blockchain-node")
58-
bindPort := fmt.Sprintf("%s/tcp", in.Port)
59-
bindPortWs := fmt.Sprintf("%s/tcp", in.WSPort)
60-
61-
req := testcontainers.ContainerRequest{
62-
AlwaysPullImage: in.PullImage,
63-
Image: in.Image,
64-
Name: containerName,
65-
ExposedPorts: []string{bindPort, bindPortWs},
66-
Networks: []string{framework.DefaultNetworkName},
67-
NetworkAliases: map[string][]string{
68-
framework.DefaultNetworkName: {containerName},
69-
},
70-
Labels: framework.DefaultTCLabels(),
71-
HostConfigModifier: func(h *container.HostConfig) {
72-
h.PortBindings = framework.MapTheSamePort(bindPortWs, bindPort)
73-
framework.ResourceLimitsFunc(h, in.ContainerResources)
74-
},
75-
WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(15 * time.Second).WithPollInterval(200 * time.Millisecond),
76-
Cmd: entryPoint,
77-
}
78-
79-
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
80-
ContainerRequest: req,
81-
Started: true,
82-
})
83-
if err != nil {
84-
return nil, err
85-
}
86-
87-
host, err := c.Host(ctx)
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
mp, err := c.MappedPort(ctx, nat.Port(bindPort))
93-
if err != nil {
94-
return nil, err
95-
}
96-
mpWs, err := c.MappedPort(ctx, nat.Port(bindPortWs))
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
return &Output{
102-
UseCache: true,
103-
ChainID: in.ChainID,
104-
Family: "evm",
105-
ContainerName: containerName,
106-
Container: c,
107-
Nodes: []*Node{
108-
{
109-
HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, mp.Port()),
110-
HostWSUrl: fmt.Sprintf("ws://%s:%s", host, mpWs.Port()),
111-
DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port),
112-
DockerInternalWSUrl: fmt.Sprintf("ws://%s:%s", containerName, in.WSPort),
113-
},
114-
},
115-
}, nil
48+
return createGenericEvmContainer(in, req, true)
11649
}

framework/components/blockchain/blockchain_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ func TestChains(t *testing.T) {
3636
ChainID: "260",
3737
},
3838
},
39+
{
40+
name: "Besu",
41+
input: &blockchain.Input{
42+
Type: "besu",
43+
Port: "8111",
44+
WSPort: "8112",
45+
ChainID: "1337",
46+
},
47+
},
3948
}
4049

4150
for _, tc := range testCases {
@@ -53,6 +62,7 @@ func testChain(t *testing.T, input *blockchain.Input) {
5362
require.NoError(t, err)
5463

5564
rpcUrl := output.Nodes[0].HostHTTPUrl
65+
t.Logf("Testing RPC: %s", rpcUrl)
5666
reqBody := `{"jsonrpc": "2.0", "method": "eth_chainId", "params": [], "id": 1}`
5767
resp, err := http.Post(rpcUrl, "application/json", strings.NewReader(reqBody)) // nolint:gosec
5868
require.NoError(t, err)

framework/components/blockchain/containers.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ import (
1313
"github.com/smartcontractkit/chainlink-testing-framework/framework"
1414
)
1515

16-
func baseRequest(in *Input) testcontainers.ContainerRequest {
16+
func baseRequest(in *Input, useWS bool) testcontainers.ContainerRequest {
1717
containerName := framework.DefaultTCName("blockchain-node")
1818
bindPort := fmt.Sprintf("%s/tcp", in.Port)
19+
exposedPorts := []string{bindPort}
20+
if useWS {
21+
exposedPorts = append(exposedPorts, fmt.Sprintf("%s/tcp", in.WSPort))
22+
}
1923

2024
return testcontainers.ContainerRequest{
21-
Labels: framework.DefaultTCLabels(),
22-
Name: containerName,
23-
ExposedPorts: []string{bindPort},
24-
HostConfigModifier: func(h *container.HostConfig) {
25-
h.PortBindings = framework.MapTheSamePort(bindPort)
26-
framework.ResourceLimitsFunc(h, in.ContainerResources)
27-
},
25+
Name: containerName,
26+
Labels: framework.DefaultTCLabels(),
2827
Networks: []string{framework.DefaultNetworkName},
2928
NetworkAliases: map[string][]string{
3029
framework.DefaultNetworkName: {containerName},
3130
},
32-
WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(10 * time.Second).WithPollInterval(200 * time.Millisecond),
31+
ExposedPorts: exposedPorts,
32+
HostConfigModifier: func(h *container.HostConfig) {
33+
h.PortBindings = framework.MapTheSamePort(exposedPorts...)
34+
framework.ResourceLimitsFunc(h, in.ContainerResources)
35+
},
36+
WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(15 * time.Second).WithPollInterval(200 * time.Millisecond),
3337
}
3438
}
3539

36-
func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest) (*Output, error) {
40+
func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest, useWS bool) (*Output, error) {
3741
ctx := context.Background()
38-
3942
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
4043
ContainerRequest: req,
4144
Started: true,
@@ -57,7 +60,7 @@ func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest) (
5760

5861
containerName := req.Name
5962

60-
return &Output{
63+
output := Output{
6164
UseCache: true,
6265
Family: "evm",
6366
ChainID: in.ChainID,
@@ -71,5 +74,15 @@ func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest) (
7174
DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port),
7275
},
7376
},
74-
}, nil
77+
}
78+
79+
if useWS {
80+
mp, err := c.MappedPort(ctx, nat.Port(req.ExposedPorts[1]))
81+
if err != nil {
82+
return nil, err
83+
}
84+
output.Nodes[0].HostWSUrl = fmt.Sprintf("ws://%s:%s", host, mp.Port())
85+
}
86+
87+
return &output, nil
7588
}

0 commit comments

Comments
 (0)