-
Notifications
You must be signed in to change notification settings - Fork 114
/
parachain_node.go
396 lines (348 loc) · 12.3 KB
/
parachain_node.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package polkadot
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"cosmossdk.io/math"
"github.com/avast/retry-go/v4"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/docker/docker/client"
"github.com/icza/dyno"
p2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
gsrpc "github.com/misko9/go-substrate-rpc-client/v4"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"go.uber.org/zap"
)
// Increase parachain wallet amount due to their additional precision
const parachainScaling = int64(1_000)
// ParachainNode defines the properties required for running a polkadot parachain node.
type ParachainNode struct {
log *zap.Logger
TestName string
Index int
NetworkID string
containerLifecycle *dockerutil.ContainerLifecycle
VolumeName string
DockerClient *client.Client
Image ibc.DockerImage
Chain ibc.Chain
Bin string
NodeKey p2pcrypto.PrivKey
ChainID string
Flags []string
RelayChainFlags []string
api *gsrpc.SubstrateAPI
hostWsPort string
hostRpcPort string
}
type ParachainNodes []*ParachainNode
// Name returns the name of the test node container.
func (pn *ParachainNode) Name() string {
return fmt.Sprintf("%s-%d-%s-%s", pn.Bin, pn.Index, pn.ChainID, dockerutil.SanitizeContainerName(pn.TestName))
}
// HostName returns the docker hostname of the test container.
func (pn *ParachainNode) HostName() string {
return dockerutil.CondenseHostName(pn.Name())
}
// Bind returns the home folder bind point for running the node.
func (pn *ParachainNode) Bind() []string {
return []string{fmt.Sprintf("%s:%s", pn.VolumeName, pn.NodeHome())}
}
// NodeHome returns the working directory within the docker image,
// the path where the docker volume is mounted.
func (pn *ParachainNode) NodeHome() string {
return "/home/heighliner"
}
// ParachainChainSpecFileName returns the relative path to the chain spec file
// within the parachain container.
func (pn *ParachainNode) ParachainChainSpecFileName() string {
return fmt.Sprintf("%s.json", pn.ChainID)
}
// ParachainChainSpecFilePathFull returns the full path to the chain spec file
// within the parachain container
func (pn *ParachainNode) ParachainChainSpecFilePathFull() string {
return filepath.Join(pn.NodeHome(), pn.ParachainChainSpecFileName())
}
// RawRelayChainSpecFilePathFull returns the full path to the raw relay chain spec file
// within the container.
func (pn *ParachainNode) RawRelayChainSpecFilePathFull() string {
return filepath.Join(pn.NodeHome(), fmt.Sprintf("%s-raw.json", pn.Chain.Config().ChainID))
}
// RawRelayChainSpecFilePathRelative returns the relative path to the raw relay chain spec file
// within the container.
func (pn *ParachainNode) RawRelayChainSpecFilePathRelative() string {
return fmt.Sprintf("%s-raw.json", pn.Chain.Config().ChainID)
}
// PeerID returns the public key of the node key for p2p.
func (pn *ParachainNode) PeerID() (string, error) {
id, err := peer.IDFromPrivateKey(pn.NodeKey)
if err != nil {
return "", err
}
return id.String(), nil
}
// MultiAddress returns the p2p multiaddr of the node.
func (pn *ParachainNode) MultiAddress() (string, error) {
peerId, err := pn.PeerID()
if err != nil {
return "", err
}
return fmt.Sprintf("/dns4/%s/tcp/%s/p2p/%s", pn.HostName(), strings.Split(nodePort, "/")[0], peerId), nil
}
type GetParachainIDResponse struct {
ParachainID int `json:"para_id"`
}
// GenerateDefaultChainSpec runs build-spec to get the default chain spec into something malleable
func (pn *ParachainNode) GenerateDefaultChainSpec(ctx context.Context) ([]byte, error) {
cmd := []string{
pn.Bin,
"build-spec",
fmt.Sprintf("--chain=%s", pn.ChainID),
}
res := pn.Exec(ctx, cmd, nil)
if res.Err != nil {
return nil, res.Err
}
return res.Stdout, nil
}
// GenerateParachainGenesisFile creates the default chain spec, modifies it and returns it.
// The modified chain spec is then written to each Parachain node
func (pn *ParachainNode) GenerateParachainGenesisFile(ctx context.Context, additionalGenesisWallets ...ibc.WalletAmount) ([]byte, error) {
defaultChainSpec, err := pn.GenerateDefaultChainSpec(ctx)
if err != nil {
return nil, fmt.Errorf("error generating default parachain chain spec: %w", err)
}
var chainSpec interface{}
err = json.Unmarshal(defaultChainSpec, &chainSpec)
if err != nil {
return nil, fmt.Errorf("error unmarshaling parachain chain spec: %w", err)
}
balances, err := dyno.GetSlice(chainSpec, "genesis", "runtime", "balances", "balances")
if err != nil {
return nil, fmt.Errorf("error getting balances from parachain chain spec: %w", err)
}
for _, wallet := range additionalGenesisWallets {
balances = append(balances,
[]interface{}{wallet.Address, wallet.Amount.MulRaw(parachainScaling).Uint64()},
)
}
if err := dyno.Set(chainSpec, balances, "genesis", "runtime", "balances", "balances"); err != nil {
return nil, fmt.Errorf("error setting parachain balances: %w", err)
}
editedChainSpec, err := json.MarshalIndent(chainSpec, "", " ")
if err != nil {
return nil, fmt.Errorf("error marshaling modified parachain chain spec: %w", err)
}
return editedChainSpec, nil
}
// ParachainID retrieves the node parachain ID.
func (pn *ParachainNode) ParachainID(ctx context.Context) (int, error) {
cmd := []string{
pn.Bin,
"build-spec",
fmt.Sprintf("--chain=%s", pn.ChainID),
}
res := pn.Exec(ctx, cmd, nil)
if res.Err != nil {
return -1, res.Err
}
out := GetParachainIDResponse{}
if err := json.Unmarshal([]byte(res.Stdout), &out); err != nil {
return -1, err
}
return out.ParachainID, nil
}
// ExportGenesisWasm exports the genesis wasm json for the configured chain ID.
func (pn *ParachainNode) ExportGenesisWasm(ctx context.Context) (string, error) {
cmd := []string{
pn.Bin,
"export-genesis-wasm",
fmt.Sprintf("--chain=%s", pn.ParachainChainSpecFilePathFull()),
}
res := pn.Exec(ctx, cmd, nil)
if res.Err != nil {
return "", res.Err
}
return string(res.Stdout), nil
}
// ExportGenesisState exports the genesis state json for the configured chain ID.
func (pn *ParachainNode) ExportGenesisState(ctx context.Context) (string, error) {
cmd := []string{
pn.Bin,
"export-genesis-state",
fmt.Sprintf("--chain=%s", pn.ParachainChainSpecFilePathFull()),
}
res := pn.Exec(ctx, cmd, nil)
if res.Err != nil {
return "", res.Err
}
return string(res.Stdout), nil
}
func (pn *ParachainNode) logger() *zap.Logger {
return pn.log.With(
zap.String("chain_id", pn.ChainID),
zap.String("test", pn.TestName),
)
}
// CreateNodeContainer assembles a parachain node docker container ready to launch.
func (pn *ParachainNode) CreateNodeContainer(ctx context.Context) error {
nodeKey, err := pn.NodeKey.Raw()
if err != nil {
return fmt.Errorf("error getting ed25519 node key: %w", err)
}
multiAddress, err := pn.MultiAddress()
if err != nil {
return err
}
cmd := []string{
pn.Bin,
fmt.Sprintf("--ws-port=%s", strings.Split(wsPort, "/")[0]),
"--collator",
fmt.Sprintf("--node-key=%s", hex.EncodeToString(nodeKey[0:32])),
fmt.Sprintf("--%s", IndexedName[pn.Index]),
"--unsafe-ws-external",
"--unsafe-rpc-external",
"--prometheus-external",
"--rpc-cors=all",
"--ws-external",
"--rpc-external",
"--rpc-methods=unsafe",
"--log=ibc_transfer=trace,pallet_ibc=trace,grandpa-verifier=trace,runtime=trace",
"--force-authoring",
"--enable-offchain-indexing=true",
"--pruning=archive",
fmt.Sprintf("--prometheus-port=%s", strings.Split(prometheusPort, "/")[0]),
fmt.Sprintf("--listen-addr=/ip4/0.0.0.0/tcp/%s", strings.Split(nodePort, "/")[0]),
fmt.Sprintf("--public-addr=%s", multiAddress),
"--base-path", pn.NodeHome(),
fmt.Sprintf("--chain=%s", pn.ParachainChainSpecFilePathFull()),
}
cmd = append(cmd, pn.Flags...)
cmd = append(cmd, "--", fmt.Sprintf("--chain=%s", pn.RawRelayChainSpecFilePathFull()))
cmd = append(cmd, pn.RelayChainFlags...)
return pn.containerLifecycle.CreateContainer(ctx, pn.TestName, pn.NetworkID, pn.Image, exposedPorts, pn.Bind(), nil, pn.HostName(), cmd, nil)
}
// StopContainer stops the relay chain node container, waiting at most 30 seconds.
func (pn *ParachainNode) StopContainer(ctx context.Context) error {
return pn.containerLifecycle.StopContainer(ctx)
}
// StartContainer starts the container after it is built by CreateNodeContainer.
func (pn *ParachainNode) StartContainer(ctx context.Context) error {
if err := pn.containerLifecycle.StartContainer(ctx); err != nil {
return err
}
hostPorts, err := pn.containerLifecycle.GetHostPorts(ctx, wsPort, rpcPort)
if err != nil {
return err
}
// Set the host ports once since they will not change after the container has started.
pn.hostWsPort, pn.hostRpcPort = hostPorts[0], hostPorts[1]
explorerUrl := fmt.Sprintf("\033[4;34mhttps://polkadot.js.org/apps?rpc=ws://%s#/explorer\033[0m",
strings.Replace(pn.hostWsPort, "localhost", "127.0.0.1", 1))
pn.log.Info(explorerUrl, zap.String("container", pn.Name()))
var api *gsrpc.SubstrateAPI
if err = retry.Do(func() error {
var err error
api, err = gsrpc.NewSubstrateAPI("ws://" + pn.hostWsPort)
return err
}, retry.Context(ctx), RtyAtt, RtyDel, RtyErr); err != nil {
return err
}
pn.api = api
return nil
}
// Exec run a container for a specific job and block until the container exits.
func (pn *ParachainNode) Exec(ctx context.Context, cmd []string, env []string) dockerutil.ContainerExecResult {
job := dockerutil.NewImage(pn.log, pn.DockerClient, pn.NetworkID, pn.TestName, pn.Image.Repository, pn.Image.Version)
opts := dockerutil.ContainerOptions{
Binds: pn.Bind(),
Env: env,
User: pn.Image.UidGid,
}
return job.Run(ctx, cmd, opts)
}
func (pn *ParachainNode) GetBalance(ctx context.Context, address string, denom string) (math.Int, error) {
return GetBalance(pn.api, address)
}
// GetIbcBalance returns the Coins type of ibc coins in account
func (pn *ParachainNode) GetIbcBalance(ctx context.Context, address string, denom uint64) (sdktypes.Coin, error) {
res, err := pn.api.RPC.IBC.QueryBalanceWithAddress(ctx, address, denom)
if err != nil {
return sdktypes.Coin{}, err
}
return res, nil
}
// SendFunds sends funds to a wallet from a user account.
// Implements Chain interface.
func (pn *ParachainNode) SendFunds(ctx context.Context, keyName string, amount ibc.WalletAmount) error {
kp, err := pn.Chain.(*PolkadotChain).GetKeyringPair(keyName)
if err != nil {
return err
}
pn.log.Info(
"ParachainNode SendFunds",
zap.String("From", kp.Address),
zap.String("To", amount.Address),
zap.String("Amount", amount.Amount.String()),
)
hash, err := SendFundsTx(pn.api, kp, amount)
if err != nil {
return err
}
pn.log.Info("Transfer sent", zap.String("hash", fmt.Sprintf("%#x", hash)), zap.String("container", pn.Name()))
return nil
}
// SendIbcFunds sends funds to a wallet from a user account.
func (pn *ParachainNode) SendIbcFunds(
ctx context.Context,
channelID string,
keyName string,
amount ibc.WalletAmount,
options ibc.TransferOptions,
) error {
kp, err := pn.Chain.(*PolkadotChain).GetKeyringPair(keyName)
if err != nil {
return err
}
pn.log.Info(
"ParachainNode SendIbcFunds",
zap.String("From", kp.Address),
zap.String("To", amount.Address),
zap.String("Amount", amount.Amount.String()),
)
hash, err := SendIbcFundsTx(pn.api, kp, channelID, amount, options)
if err != nil {
pn.log.Info("IBC Transfer not sent", zap.String("hash", fmt.Sprintf("%#x", hash)), zap.String("container", pn.Name()))
return err
}
pn.log.Info("IBC Transfer sent", zap.String("hash", fmt.Sprintf("%#x", hash)), zap.String("container", pn.Name()))
return nil
}
// MintFunds mints an asset for a user on parachain, keyName must be the owner of the asset
func (pn *ParachainNode) MintFunds(
keyName string,
amount ibc.WalletAmount,
) error {
kp, err := pn.Chain.(*PolkadotChain).GetKeyringPair(keyName)
if err != nil {
return err
}
pn.log.Info(
"ParachainNode MintFunds",
zap.String("From", kp.Address),
zap.String("To", amount.Address),
zap.String("Amount", amount.Amount.String()),
)
hash, err := MintFundsTx(pn.api, kp, amount)
if err != nil {
pn.log.Info("MintFunds not sent", zap.String("hash", fmt.Sprintf("%#x", hash)), zap.String("container", pn.Name()))
return err
}
pn.log.Info("MintFunds sent", zap.String("hash", fmt.Sprintf("%#x", hash)), zap.String("container", pn.Name()))
return nil
}