Skip to content

Commit 15810a8

Browse files
author
Ilan
committed
Refactor geth
1 parent afa3436 commit 15810a8

File tree

2 files changed

+44
-79
lines changed

2 files changed

+44
-79
lines changed

framework/components/blockchain/blockchain_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ func TestChains(t *testing.T) {
4545
ChainID: "1337",
4646
},
4747
},
48+
{
49+
name: "Geth",
50+
input: &blockchain.Input{
51+
Type: "geth",
52+
Port: "8211",
53+
ChainID: "1337",
54+
},
55+
},
4856
}
4957

5058
for _, tc := range testCases {

framework/components/blockchain/geth.go

Lines changed: 36 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package blockchain
22

33
import (
4-
"context"
54
"fmt"
65
"os"
7-
"time"
86

97
"github.com/docker/docker/api/types/container"
108
"github.com/docker/docker/api/types/mount"
119

1210
"github.com/smartcontractkit/chainlink-testing-framework/framework"
1311

14-
"github.com/docker/go-connections/nat"
1512
"github.com/testcontainers/testcontainers-go"
16-
"github.com/testcontainers/testcontainers-go/wait"
1713
)
1814

1915
const (
@@ -82,7 +78,7 @@ func defaultGeth(in *Input) {
8278

8379
func newGeth(in *Input) (*Output, error) {
8480
defaultGeth(in)
85-
ctx := context.Background()
81+
req := baseRequest(in, false)
8682
defaultCmd := []string{
8783
"--http.corsdomain=*",
8884
"--http.vhosts=*",
@@ -112,8 +108,7 @@ func newGeth(in *Input) (*Output, error) {
112108
}
113109
entryPoint := append(defaultCmd, in.DockerCmdParamsOverrides...)
114110

115-
containerName := framework.DefaultTCName("blockchain-node")
116-
bindPort := fmt.Sprintf("%s/tcp", in.Port)
111+
bindPort := req.ExposedPorts[0]
117112

118113
initScriptFile, err := os.CreateTemp("", "init_script")
119114
if err != nil {
@@ -152,79 +147,41 @@ func newGeth(in *Input) (*Output, error) {
152147
return nil, err
153148
}
154149

155-
req := testcontainers.ContainerRequest{
156-
AlwaysPullImage: in.PullImage,
157-
Image: in.Image,
158-
Labels: framework.DefaultTCLabels(),
159-
Networks: []string{framework.DefaultNetworkName},
160-
NetworkAliases: map[string][]string{
161-
framework.DefaultNetworkName: {containerName},
150+
req.AlwaysPullImage = in.PullImage
151+
req.Image = in.Image
152+
req.Entrypoint = []string{
153+
"sh", "./root/init.sh",
154+
"--datadir", "/root/.ethereum/devchain",
155+
"--password", "/root/config/password.txt",
156+
}
157+
req.HostConfigModifier = func(h *container.HostConfig) {
158+
h.PortBindings = framework.MapTheSamePort(bindPort)
159+
framework.ResourceLimitsFunc(h, in.ContainerResources)
160+
h.Mounts = append(h.Mounts, mount.Mount{
161+
Type: mount.TypeBind,
162+
Source: keystoreDir,
163+
Target: "/root/.ethereum/devchain/keystore/",
164+
ReadOnly: false,
165+
}, mount.Mount{
166+
Type: mount.TypeBind,
167+
Source: configDir,
168+
Target: "/root/config/",
169+
ReadOnly: false,
170+
})
171+
}
172+
req.Files = []testcontainers.ContainerFile{
173+
{
174+
HostFilePath: initScriptFile.Name(),
175+
ContainerFilePath: "/root/init.sh",
176+
FileMode: 0644,
162177
},
163-
Entrypoint: []string{
164-
"sh", "./root/init.sh",
165-
"--datadir", "/root/.ethereum/devchain",
166-
"--password", "/root/config/password.txt",
178+
{
179+
HostFilePath: genesisFile.Name(),
180+
ContainerFilePath: "/root/genesis.json",
181+
FileMode: 0644,
167182
},
168-
Name: containerName,
169-
ExposedPorts: []string{bindPort},
170-
HostConfigModifier: func(h *container.HostConfig) {
171-
h.PortBindings = framework.MapTheSamePort(bindPort)
172-
framework.ResourceLimitsFunc(h, in.ContainerResources)
173-
h.Mounts = append(h.Mounts, mount.Mount{
174-
Type: mount.TypeBind,
175-
Source: keystoreDir,
176-
Target: "/root/.ethereum/devchain/keystore/",
177-
ReadOnly: false,
178-
}, mount.Mount{
179-
Type: mount.TypeBind,
180-
Source: configDir,
181-
Target: "/root/config/",
182-
ReadOnly: false,
183-
})
184-
},
185-
Files: []testcontainers.ContainerFile{
186-
{
187-
HostFilePath: initScriptFile.Name(),
188-
ContainerFilePath: "/root/init.sh",
189-
FileMode: 0644,
190-
},
191-
{
192-
HostFilePath: genesisFile.Name(),
193-
ContainerFilePath: "/root/genesis.json",
194-
FileMode: 0644,
195-
},
196-
},
197-
WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(15 * time.Second).WithPollInterval(200 * time.Millisecond),
198-
Cmd: entryPoint,
199183
}
200-
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
201-
ContainerRequest: req,
202-
Started: true,
203-
})
204-
if err != nil {
205-
return nil, err
206-
}
207-
host, err := c.Host(ctx)
208-
if err != nil {
209-
return nil, err
210-
}
211-
mp, err := c.MappedPort(ctx, nat.Port(bindPort))
212-
if err != nil {
213-
return nil, err
214-
}
215-
return &Output{
216-
UseCache: true,
217-
Family: "evm",
218-
ChainID: in.ChainID,
219-
ContainerName: containerName,
220-
Container: c,
221-
Nodes: []*Node{
222-
{
223-
HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, mp.Port()),
224-
HostWSUrl: fmt.Sprintf("ws://%s:%s", host, mp.Port()),
225-
DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port),
226-
DockerInternalWSUrl: fmt.Sprintf("ws://%s:%s", containerName, in.Port),
227-
},
228-
},
229-
}, nil
184+
req.Cmd = entryPoint
185+
186+
return createGenericEvmContainer(in, req, false)
230187
}

0 commit comments

Comments
 (0)