-
Notifications
You must be signed in to change notification settings - Fork 37
/
geth_eth2.go
173 lines (154 loc) · 5.58 KB
/
geth_eth2.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
163
164
165
166
167
168
169
170
171
172
173
package test_env
import (
"bytes"
"fmt"
"html/template"
"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"
)
// NewGethEth2 starts a new Geth Eth2 node running in Docker
func NewGethEth2(networks []string, chainConfig *config.EthereumChainConfig, generatedDataHostDir string, consensusLayer config.ConsensusLayer, opts ...EnvComponentOption) (*Geth, error) {
parts := strings.Split(defaultGethEth2Image, ":")
g := &Geth{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "geth-eth2", uuid.NewString()[0:8]),
Networks: networks,
ContainerImage: parts[0],
ContainerVersion: parts[1],
},
chainConfig: chainConfig,
generatedDataHostDir: generatedDataHostDir,
consensusLayer: consensusLayer,
l: logging.GetTestLogger(nil),
ethereumVersion: config.EthereumVersion_Eth2,
}
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-eth2", 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, nil
}
func (g *Geth) getEth2ContainerRequest() (*tc.ContainerRequest, error) {
initFile, err := os.CreateTemp("", "init.sh")
if err != nil {
return nil, err
}
initScriptContent, err := g.buildEth2dInitScript()
if err != nil {
return nil, err
}
_, err = initFile.WriteString(initScriptContent)
if err != nil {
return nil, err
}
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
Networks: g.Networks,
ImagePlatform: "linux/x86_64",
ExposedPorts: []string{NatPortFormat(DEFAULT_EVM_NODE_HTTP_PORT), NatPortFormat(DEFAULT_EVM_NODE_WS_PORT), NatPortFormat(ETH2_EXECUTION_PORT)},
WaitingFor: tcwait.ForAll(
tcwait.ForLog("WebSocket enabled").
WithStartupTimeout(120 * time.Second).
WithPollInterval(1 * time.Second),
),
Entrypoint: []string{
"sh",
"/init.sh",
},
Files: []tc.ContainerFile{
{
HostFilePath: initFile.Name(),
ContainerFilePath: "/init.sh",
FileMode: 0744,
},
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: g.generatedDataHostDir,
Target: GENERATED_DATA_DIR_INSIDE_CONTAINER,
ReadOnly: false,
})
},
LifecycleHooks: []tc.ContainerLifecycleHooks{
{
PostStarts: g.PostStartsHooks,
PostStops: g.PostStopsHooks,
},
},
}, nil
}
func (g *Geth) buildEth2dInitScript() (string, error) {
initTemplate := `#!/bin/bash
mkdir -p {{.ExecutionDir}}
# copy general keystore to execution directory, because Geth doesn't allow to specify keystore location
echo "Copying keystore to {{.ExecutionDir}}/keystore"
cp -R {{.KeystoreDirLocation}} {{.ExecutionDir}}/keystore
echo "Creating sk.json file"
echo "2e0834786285daccd064ca17f1654f67b4aef298acbb82cef9ec422fb4975622" > {{.ExecutionDir}}/sk.json
echo "Running geth init"
# geth init --state.scheme=path --datadir={{.ExecutionDir}} {{.GeneratedDataDir}}/genesis.json
geth init --datadir={{.ExecutionDir}} {{.GeneratedDataDir}}/genesis.json
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Geth init failed with exit code $exit_code"
exit 1
fi
echo "Starting Geth..."
geth --http --http.api=eth,net,web3,debug --http.addr=0.0.0.0 --http.corsdomain=* \
--http.vhosts=* --http.port={{.HttpPort}} --ws --ws.api=admin,debug,web3,eth,txpool,net \
--ws.addr=0.0.0.0 --ws.origins=* --ws.port={{.WsPort}} --authrpc.vhosts=* \
--authrpc.addr=0.0.0.0 --authrpc.jwtsecret={{.JwtFileLocation}} --datadir={{.ExecutionDir}} \
--rpc.allow-unprotected-txs --rpc.txfeecap=0 --allow-insecure-unlock \
--password={{.PasswordFileLocation}} --nodiscover --syncmode=full --networkid={{.ChainID}} \
--graphql --graphql.corsdomain=* --unlock=0x123463a4b065722e99115d6c222f267d9cabb524 --verbosity={{.Verbosity}}`
verbosity, err := g.logLevelToVerbosity()
if err != nil {
return "", err
}
data := struct {
HttpPort string
WsPort string
ChainID int
GeneratedDataDir string
JwtFileLocation string
PasswordFileLocation string
KeystoreDirLocation string
ExecutionDir string
Verbosity int
}{
HttpPort: DEFAULT_EVM_NODE_HTTP_PORT,
WsPort: DEFAULT_EVM_NODE_WS_PORT,
ChainID: g.chainConfig.ChainID,
GeneratedDataDir: GENERATED_DATA_DIR_INSIDE_CONTAINER,
JwtFileLocation: JWT_SECRET_FILE_LOCATION_INSIDE_CONTAINER,
PasswordFileLocation: ACCOUNT_PASSWORD_FILE_INSIDE_CONTAINER,
KeystoreDirLocation: KEYSTORE_DIR_LOCATION_INSIDE_CONTAINER,
ExecutionDir: "/execution-data",
Verbosity: verbosity,
}
t, err := template.New("init").Parse(initTemplate)
if err != nil {
fmt.Println("Error parsing template:", err)
os.Exit(1)
}
var buf bytes.Buffer
err = t.Execute(&buf, data)
return buf.String(), err
}