-
Notifications
You must be signed in to change notification settings - Fork 36
/
besu_eth1.go
160 lines (143 loc) · 4.62 KB
/
besu_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
package test_env
import (
"fmt"
"os"
"strings"
"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"
)
// NewBesuEth1 starts a new Besu Eth1 node running in Docker
func NewBesuEth1(networks []string, chainConfig *config.EthereumChainConfig, opts ...EnvComponentOption) (*Besu, error) {
parts := strings.Split(defaultBesuEth1Image, ":")
g := &Besu{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "besu-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", "besu-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 *Besu) getEth1ContainerRequest() (*tc.ContainerRequest, error) {
err := g.prepareEth1FilesAndDirs()
if err != nil {
return nil, err
}
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
ExposedPorts: []string{
NatPortFormat(DEFAULT_EVM_NODE_HTTP_PORT),
NatPortFormat(DEFAULT_EVM_NODE_WS_PORT)},
Networks: g.Networks,
WaitingFor: tcwait.ForAll(
tcwait.ForLog("WebSocketService | Websocket service started"),
NewWebSocketStrategy(NatPort(DEFAULT_EVM_NODE_WS_PORT), g.l),
NewHTTPStrategy("/", NatPort(DEFAULT_EVM_NODE_HTTP_PORT)).WithStatusCode(201),
),
Entrypoint: []string{
"besu",
"--genesis-file", "/opt/besu/genesis/genesis.json",
"--host-allowlist", "*",
"--rpc-http-enabled",
"--rpc-http-cors-origins", "*",
"--rpc-http-api", "ADMIN,DEBUG,WEB3,ETH,TXPOOL,CLIQUE,MINER,NET",
"--rpc-http-host", "0.0.0.0",
fmt.Sprintf("--rpc-http-port=%s", DEFAULT_EVM_NODE_HTTP_PORT),
"--rpc-ws-enabled",
"--rpc-ws-api", "ADMIN,DEBUG,WEB3,ETH,TXPOOL,CLIQUE,MINER,NET",
"--rpc-ws-host", "0.0.0.0",
fmt.Sprintf("--rpc-ws-port=%s", DEFAULT_EVM_NODE_WS_PORT),
"--miner-enabled=true",
"--miner-coinbase", RootFundingAddr,
fmt.Sprintf("--network-id=%d", g.chainConfig.ChainID),
fmt.Sprintf("--logging=%s", strings.ToUpper(g.LogLevel)),
},
Files: []tc.ContainerFile{
{
HostFilePath: g.genesisPath,
ContainerFilePath: "/opt/besu/genesis/genesis.json",
FileMode: 0644,
},
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: g.keystorePath,
Target: "/opt/besu/nodedata/keystore/",
ReadOnly: false,
}, mount.Mount{
Type: mount.TypeBind,
Source: g.rootPath,
Target: "/opt/besu/nodedata/",
ReadOnly: false,
})
},
LifecycleHooks: []tc.ContainerLifecycleHooks{
{
PostStarts: g.PostStartsHooks,
PostStops: g.PostStopsHooks,
},
},
}, nil
}
func (g *Besu) prepareEth1FilesAndDirs() error {
keystorePath, err := os.MkdirTemp("", "keystore")
if err != nil {
return err
}
g.keystorePath = keystorePath
toFund := g.chainConfig.AddressesToFund
toFund = append(toFund, RootFundingAddr)
generatedData, err := generateKeystoreAndExtraData(keystorePath, toFund)
if err != nil {
return err
}
g.accountAddr = generatedData.minerAccount.Address.Hex()
err = os.WriteFile(g.keystorePath+"/password.txt", []byte(""), 0600)
if err != nil {
return err
}
genesisJsonStr, err := templates.BuildBesuGenesisJsonForNonDevChain(fmt.Sprint(g.chainConfig.ChainID),
generatedData.accountsToFund, "0x")
if err != nil {
return err
}
f, err := os.CreateTemp("", "genesis_json")
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(genesisJsonStr)
if err != nil {
return err
}
g.genesisPath = f.Name()
configDir, err := os.MkdirTemp("", "config")
if err != nil {
return err
}
g.rootPath = configDir
return nil
}