-
Notifications
You must be signed in to change notification settings - Fork 36
/
prysm.go
267 lines (242 loc) · 8.57 KB
/
prysm.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package test_env
import (
"fmt"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/google/uuid"
"github.com/rs/zerolog"
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/docker"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/smartcontractkit/chainlink-testing-framework/mirror"
"github.com/smartcontractkit/chainlink-testing-framework/utils/testcontext"
)
const (
PRYSM_QUERY_RPC_PORT = "3500"
PRYSM_NODE_RPC_PORT = "4000"
defaultPrysmBeaconChainImage = "gcr.io/prysmaticlabs/prysm/beacon-chain:v4.1.1"
defaultPyrsmValidatorImage = "gcr.io/prysmaticlabs/prysm/validator:v4.1.1"
)
type PrysmBeaconChain struct {
EnvComponent
InternalBeaconRpcProvider string
InternalQueryRpcUrl string
ExternalBeaconRpcProvider string
ExternalQueryRpcUrl string
generatedDataHostDir string
gethInternalExecutionURL string
chainConfig *config.EthereumChainConfig
l zerolog.Logger
t *testing.T
}
func NewPrysmBeaconChain(networks []string, chainConfig *config.EthereumChainConfig, customConfigDataDir, gethExecutionURL string, opts ...EnvComponentOption) (*PrysmBeaconChain, error) {
parts := strings.Split(defaultPrysmBeaconChainImage, ":")
g := &PrysmBeaconChain{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "prysm-beacon-chain", uuid.NewString()[0:8]),
Networks: networks,
ContainerImage: parts[0],
ContainerVersion: parts[1],
},
chainConfig: chainConfig,
generatedDataHostDir: customConfigDataDir,
gethInternalExecutionURL: gethExecutionURL,
l: logging.GetTestLogger(nil),
}
g.SetDefaultHooks()
for _, opt := range opts {
opt(&g.EnvComponent)
}
// 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 *PrysmBeaconChain) WithTestInstance(t *testing.T) *PrysmBeaconChain {
g.l = logging.GetTestLogger(t)
g.t = t
return g
}
func (g *PrysmBeaconChain) StartContainer() error {
r, err := g.getContainerRequest(g.Networks)
if err != nil {
return err
}
l := logging.GetTestContainersGoTestLogger(g.t)
ct, err := docker.StartContainerWithRetry(g.l, tc.GenericContainerRequest{
ContainerRequest: *r,
Reuse: true,
Started: true,
Logger: l,
})
if err != nil {
return fmt.Errorf("cannot start prysm beacon chain container: %w", err)
}
host, err := GetHost(testcontext.Get(g.t), ct)
if err != nil {
return err
}
queryPort, err := ct.MappedPort(testcontext.Get(g.t), NatPort(PRYSM_QUERY_RPC_PORT))
if err != nil {
return err
}
externalRcpPort, err := ct.MappedPort(testcontext.Get(g.t), NatPort(PRYSM_NODE_RPC_PORT))
if err != nil {
return err
}
g.Container = ct
g.InternalBeaconRpcProvider = fmt.Sprintf("%s:%s", g.ContainerName, PRYSM_NODE_RPC_PORT)
g.InternalQueryRpcUrl = fmt.Sprintf("%s:%s", g.ContainerName, PRYSM_QUERY_RPC_PORT)
g.ExternalBeaconRpcProvider = FormatHttpUrl(host, externalRcpPort.Port())
g.ExternalQueryRpcUrl = FormatHttpUrl(host, queryPort.Port())
g.l.Info().Str("containerName", g.ContainerName).
Msg("Started Prysm Beacon Chain container")
return nil
}
func (g *PrysmBeaconChain) getContainerRequest(networks []string) (*tc.ContainerRequest, error) {
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
ImagePlatform: "linux/amd64",
Networks: networks,
WaitingFor: tcwait.ForAll(
tcwait.ForLog("Starting beacon node").
WithStartupTimeout(g.chainConfig.GetDefaultWaitDuration()).
WithPollInterval(2 * time.Second),
),
Cmd: []string{
"--accept-terms-of-use",
"--datadir=/consensus-data",
fmt.Sprintf("--chain-config-file=%s/config.yaml", GENERATED_DATA_DIR_INSIDE_CONTAINER),
fmt.Sprintf("--genesis-state=%s/genesis.ssz", GENERATED_DATA_DIR_INSIDE_CONTAINER),
fmt.Sprintf("--execution-endpoint=%s", g.gethInternalExecutionURL),
"--rpc-host=0.0.0.0",
"--grpc-gateway-host=0.0.0.0",
"--grpc-gateway-corsdomain=*",
"--suggested-fee-recipient=0x8943545177806ED17B9F23F0a21ee5948eCaa776",
"--subscribe-all-subnets=true",
fmt.Sprintf("--jwt-secret=%s", JWT_SECRET_FILE_LOCATION_INSIDE_CONTAINER),
// mine, modify when running multi-node
"--minimum-peers-per-subnet=0",
"--min-sync-peers=0",
"--interop-eth1data-votes",
},
ExposedPorts: []string{NatPortFormat(PRYSM_NODE_RPC_PORT), NatPortFormat(PRYSM_QUERY_RPC_PORT)},
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
}
type PrysmValidator struct {
EnvComponent
chainConfig *config.EthereumChainConfig
internalBeaconRpcProvider string
valKeysDir string
generatedDataHostDir string
l zerolog.Logger
t *testing.T
}
func NewPrysmValidator(networks []string, chainConfig *config.EthereumChainConfig, generatedDataHostDir, valKeysDir, internalBeaconRpcProvider string, opts ...EnvComponentOption) (*PrysmValidator, error) {
parts := strings.Split(defaultPyrsmValidatorImage, ":")
g := &PrysmValidator{
EnvComponent: EnvComponent{
ContainerName: fmt.Sprintf("%s-%s", "prysm-validator", uuid.NewString()[0:8]),
Networks: networks,
ContainerImage: parts[0],
ContainerVersion: parts[1],
},
chainConfig: chainConfig,
generatedDataHostDir: generatedDataHostDir,
valKeysDir: valKeysDir,
internalBeaconRpcProvider: internalBeaconRpcProvider,
l: logging.GetTestLogger(nil),
}
g.SetDefaultHooks()
for _, opt := range opts {
opt(&g.EnvComponent)
}
// 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 *PrysmValidator) WithTestInstance(t *testing.T) *PrysmValidator {
g.l = logging.GetTestLogger(t)
g.t = t
return g
}
func (g *PrysmValidator) StartContainer() error {
r, err := g.getContainerRequest(g.Networks)
if err != nil {
return err
}
l := logging.GetTestContainersGoTestLogger(g.t)
ct, err := docker.StartContainerWithRetry(g.l, tc.GenericContainerRequest{
ContainerRequest: *r,
Reuse: true,
Started: true,
Logger: l,
})
if err != nil {
return fmt.Errorf("cannot start prysm validator container: %w", err)
}
g.Container = ct
g.l.Info().Str("containerName", g.ContainerName).
Msg("Started Prysm Validator container")
return nil
}
func (g *PrysmValidator) getContainerRequest(networks []string) (*tc.ContainerRequest, error) {
return &tc.ContainerRequest{
Name: g.ContainerName,
Image: g.GetImageWithVersion(),
Networks: networks,
ImagePlatform: "linux/x86_64",
WaitingFor: tcwait.ForAll(
tcwait.ForLog("Beacon chain started").
WithStartupTimeout(g.chainConfig.GetDefaultWaitDuration()).
WithPollInterval(2 * time.Second),
),
Cmd: []string{
"--accept-terms-of-use",
fmt.Sprintf("--chain-config-file=%s/config.yaml", GENERATED_DATA_DIR_INSIDE_CONTAINER),
fmt.Sprintf("--beacon-rpc-provider=%s", g.internalBeaconRpcProvider),
"--datadir=/consensus-data",
"--suggested-fee-recipient=0x8943545177806ED17B9F23F0a21ee5948eCaa776",
fmt.Sprintf("--wallet-dir=%s/prysm", NODE_0_DIR_INSIDE_CONTAINER),
fmt.Sprintf("--wallet-password-file=%s", VALIDATOR_WALLET_PASSWORD_FILE_INSIDE_CONTAINER),
},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: g.valKeysDir,
Target: GENERATED_VALIDATOR_KEYS_DIR_INSIDE_CONTAINER,
ReadOnly: false,
}, 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
}