-
Notifications
You must be signed in to change notification settings - Fork 36
/
geth_eth1.go
162 lines (151 loc) · 4.67 KB
/
geth_eth1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package test_env
import (
"encoding/hex"
"fmt"
"os"
"strings"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/google/uuid"
tc "github.com/testcontainers/testcontainers-go"
tcwait "github.com/testcontainers/testcontainers-go/wait"
"github.com/smartcontractkit/chainlink-testing-framework/config"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/smartcontractkit/chainlink-testing-framework/mirror"
"github.com/smartcontractkit/chainlink-testing-framework/utils/templates"
)
// NewGethEth1 starts a new Geth Eth1 node running in Docker
func NewGethEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) *Geth {
parts := strings.Split(defaultGethEth1Image, ":")
g := &Geth{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "geth-eth1", uuid.NewString()[0:8]),
Networks: networks,
ContainerImage: parts[0],
ContainerVersion: parts[1],
},
chainConfig: chainConfig,
l: logging.GetTestLogger(nil),
ethereumVersion: config.EthereumVersion_Eth1,
}
g.SetDefaultHooks()
for _, opt := range opts {
opt(&g.EnvComponent)
}
if !g.WasRecreated {
// set the container name again after applying functional options as version might have changed
g.EnvComponent.ContainerName = fmt.Sprintf("%s-%s-%s", "geth-eth1", strings.Replace(g.ContainerVersion, ".", "_", -1), uuid.NewString()[0:8])
}
// if the internal docker repo is set then add it to the version
g.EnvComponent.ContainerImage = mirror.AddMirrorToImageIfSet(g.EnvComponent.ContainerImage)
return g
}
func (g *Geth) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
initScriptFile, err := os.CreateTemp("", "init_script")
if err != nil {
return nil, err
}
_, err = initScriptFile.WriteString(templates.InitGethScript)
if err != nil {
return nil, err
}
keystoreDir, err := os.MkdirTemp("", "keystore")
if err != nil {
return nil, err
}
toFund := g.chainConfig.AddressesToFund
toFund = append(toFund, RootFundingAddr)
generatedData, err := generateKeystoreAndExtraData(keystoreDir, toFund)
if err != nil {
return nil, err
}
genesisJsonStr, err := templates.GenesisJsonTemplate{
ChainId: fmt.Sprintf("%d", g.chainConfig.ChainID),
AccountAddr: generatedData.accountsToFund,
Consensus: templates.GethGenesisConsensus_Clique,
ExtraData: fmt.Sprintf("0x%s", hex.EncodeToString(generatedData.extraData)),
}.String()
if err != nil {
return nil, err
}
genesisFile, err := os.CreateTemp("", "genesis_json")
if err != nil {
return nil, err
}
_, err = genesisFile.WriteString(genesisJsonStr)
if err != nil {
return nil, err
}
key1File, err := os.CreateTemp(keystoreDir, "key1")
if err != nil {
return nil, err
}
_, err = key1File.WriteString(RootFundingWallet)
if err != nil {
return nil, err
}
configDir, err := os.MkdirTemp("", "config")
if err != nil {
return nil, err
}
err = os.WriteFile(configDir+"/password.txt", []byte(""), 0600)
if err != nil {
return nil, err
}
entrypoint, err := g.getEntryPointAndKeystoreLocation(generatedData.minerAccount.Address.Hex())
if err != nil {
return nil, err
}
websocketMsg, err := g.getWebsocketEnabledMessage()
if err != nil {
return nil, err
}
return &tc.ContainerRequest{
Name: g.ContainerName,
AlwaysPullImage: true,
Image: g.GetImageWithVersion(),
ExposedPorts: []string{NatPortFormat(DEFAULT_EVM_NODE_HTTP_PORT), NatPortFormat(DEFAULT_EVM_NODE_WS_PORT)},
Networks: g.Networks,
WaitingFor: tcwait.ForAll(
NewHTTPStrategy("/", NatPort(DEFAULT_EVM_NODE_HTTP_PORT)),
tcwait.ForLog(websocketMsg),
tcwait.ForLog("Started P2P networking").
WithStartupTimeout(120*time.Second).
WithPollInterval(1*time.Second),
NewWebSocketStrategy(NatPort(DEFAULT_EVM_NODE_WS_PORT), g.l),
),
Entrypoint: entrypoint,
Files: []tc.ContainerFile{
{
HostFilePath: initScriptFile.Name(),
ContainerFilePath: "/root/init.sh",
FileMode: 0644,
},
{
HostFilePath: genesisFile.Name(),
ContainerFilePath: "/root/genesis.json",
FileMode: 0644,
},
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: keystoreDir,
Target: "/root/.ethereum/devchain/keystore/",
ReadOnly: false,
}, mount.Mount{
Type: mount.TypeBind,
Source: configDir,
Target: "/root/config/",
ReadOnly: false,
})
},
LifecycleHooks: []tc.ContainerLifecycleHooks{
{
PostStarts: g.PostStartsHooks,
PostStops: g.PostStopsHooks,
},
},
}, nil
}