-
Notifications
You must be signed in to change notification settings - Fork 36
/
nethermind_eth1.go
174 lines (160 loc) · 5.32 KB
/
nethermind_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
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"
)
// NewNethermindEth1 starts a new Nethermin Eth1 node running in Docker
func NewNethermindEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) (*Nethermind, error) {
parts := strings.Split(defaultNethermindEth1Image, ":")
g := &Nethermind{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "nethermind-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", "nethermind-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 *Nethermind) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
keystorePath, err := os.MkdirTemp("", "keystore")
if err != nil {
return nil, err
}
toFund := g.chainConfig.AddressesToFund
toFund = append(toFund, RootFundingAddr)
generatedData, err := generateKeystoreAndExtraData(keystorePath, toFund)
if err != nil {
return nil, err
}
genesisJsonStr, err := templates.NethermindPoAGenesisJsonTemplate{
ChainId: fmt.Sprintf("%d", g.chainConfig.ChainID),
AccountAddr: generatedData.accountsToFund,
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
}
// create empty cfg file since if we don't pass any
// default mainnet.cfg will be used
noneCfg, err := os.CreateTemp("", "none.cfg")
if err != nil {
return nil, err
}
_, err = noneCfg.WriteString("{}")
if err != nil {
return nil, err
}
passFile, err := os.CreateTemp("", "password.txt")
if err != nil {
return nil, err
}
err = os.WriteFile(passFile.Name(), []byte(""), 0600)
if err != nil {
return nil, err
}
rootFile, err := os.CreateTemp(keystorePath, RootFundingAddr)
if err != nil {
return nil, err
}
_, err = rootFile.WriteString(RootFundingWallet)
if err != nil {
return nil, err
}
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
Networks: g.Networks,
AlwaysPullImage: true,
// ImagePlatform: "linux/x86_64", //don't even try this on Apple Silicon, the node won't start due to .NET error
ExposedPorts: []string{NatPortFormat(DEFAULT_EVM_NODE_HTTP_PORT), NatPortFormat(DEFAULT_EVM_NODE_WS_PORT)},
WaitingFor: tcwait.ForAll(
tcwait.ForLog("Nethermind initialization completed").
WithStartupTimeout(120 * time.Second).
WithPollInterval(1 * time.Second),
),
Cmd: []string{
"--config=/none.cfg",
"--Init.ChainSpecPath=/chainspec.json",
"--Init.DiscoveryEnabled=false",
"--Init.WebSocketsEnabled=true",
fmt.Sprintf("--JsonRpc.WebSocketsPort=%s", DEFAULT_EVM_NODE_WS_PORT),
"--JsonRpc.Enabled=true",
"--JsonRpc.EnabledModules=net,consensus,eth,subscribe,web3,admin,trace,txpool",
"--JsonRpc.Host=0.0.0.0",
fmt.Sprintf("--JsonRpc.Port=%s", DEFAULT_EVM_NODE_HTTP_PORT),
"--KeyStore.KeyStoreDirectory=/keystore",
fmt.Sprintf("--KeyStore.BlockAuthorAccount=%s", generatedData.minerAccount.Address.Hex()),
fmt.Sprintf("--KeyStore.UnlockAccounts=%s", generatedData.minerAccount.Address.Hex()),
"--KeyStore.PasswordFiles=/password.txt",
"--Mining.Enabled=true",
"--Init.PeerManagerEnabled=false",
"--HealthChecks.Enabled=true", // default slug /health
fmt.Sprintf("--log=%s", strings.ToUpper(g.LogLevel)),
},
Files: []tc.ContainerFile{
{
HostFilePath: genesisFile.Name(),
ContainerFilePath: "/chainspec.json",
FileMode: 0644,
},
{
HostFilePath: noneCfg.Name(),
ContainerFilePath: "/none.cfg",
FileMode: 0644,
},
{
HostFilePath: passFile.Name(),
ContainerFilePath: "/password.txt",
FileMode: 0644,
},
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: keystorePath,
Target: "/keystore",
ReadOnly: false,
})
},
LifecycleHooks: []tc.ContainerLifecycleHooks{
{
PostStarts: g.PostStartsHooks,
PostStops: g.PostStopsHooks,
},
},
}, nil
}