-
Notifications
You must be signed in to change notification settings - Fork 37
/
erigon_eth1.go
193 lines (174 loc) · 5.3 KB
/
erigon_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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"
"github.com/smartcontractkit/chainlink-testing-framework/utils/templates"
)
// NewErigonEth1 starts a new Erigon Eth1 node running in Docker
func NewErigonEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) (*Erigon, error) {
parts := strings.Split(defaultErigonEth1Image, ":")
g := &Erigon{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "erigon-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", "erigon-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, nil
}
func (g *Erigon) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
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_Ethash,
}.String()
if err != nil {
return nil, err
}
initFile, err := os.CreateTemp("", "init.sh")
if err != nil {
return nil, err
}
initScriptContent, err := g.buildPowInitScript(generatedData.minerAccount.Address.Hex())
if err != nil {
return nil, err
}
_, err = initFile.WriteString(initScriptContent)
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
}
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
Networks: g.Networks,
ImagePlatform: "linux/x86_64",
ExposedPorts: []string{NatPortFormat(DEFAULT_EVM_NODE_HTTP_PORT)},
WaitingFor: tcwait.ForAll(
tcwait.ForLog("Started P2P networking").
WithStartupTimeout(120*time.Second).
WithPollInterval(1*time.Second),
NewWebSocketStrategy(NatPort(DEFAULT_EVM_NODE_HTTP_PORT), g.l),
),
User: "0:0",
Entrypoint: []string{
"sh",
"/home/erigon/init.sh",
},
Files: []tc.ContainerFile{
{
HostFilePath: initFile.Name(),
ContainerFilePath: "/root/init.sh",
FileMode: 0644,
},
{
HostFilePath: genesisFile.Name(),
ContainerFilePath: "/root/genesis.json",
FileMode: 0644,
},
{
HostFilePath: initFile.Name(),
ContainerFilePath: "/home/erigon/init.sh",
FileMode: 0744,
},
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: keystoreDir,
Target: "/root/.local/share/erigon/keystore/",
ReadOnly: false,
},
)
},
LifecycleHooks: []tc.ContainerLifecycleHooks{
{
PostStarts: g.PostStartsHooks,
PostStops: g.PostStopsHooks,
},
},
}, nil
}
func (g *Erigon) buildPowInitScript(minerAddr string) (string, error) {
initTemplate := `#!/bin/bash
echo "Running erigon init"
erigon init /root/genesis.json
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Erigon init failed with exit code $exit_code"
exit 1
fi
echo "Starting Erigon..."
erigon --http --http.api=eth,erigon,engine,web3,net,debug,trace,txpool,admin --http.addr=0.0.0.0 --http.corsdomain=* --http.vhosts=* --http.port={{.HttpPort}} --ws \
--allow-insecure-unlock --nodiscover --networkid={{.ChainID}} --mine --miner.etherbase={{.MinerAddr}} --fakepow --log.console.verbosity={{.LogLevel}}`
data := struct {
HttpPort string
ChainID int
MinerAddr string
LogLevel string
}{
HttpPort: DEFAULT_EVM_NODE_HTTP_PORT,
ChainID: g.chainConfig.ChainID,
MinerAddr: minerAddr,
LogLevel: g.LogLevel,
}
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
}