-
Notifications
You must be signed in to change notification settings - Fork 3
/
env_test.go
498 lines (450 loc) · 15.8 KB
/
env_test.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//go:build integrationtests
// +build integrationtests
package processes_test
import (
"context"
"sync"
"testing"
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"
"github.com/pkg/errors"
rippledata "github.com/rubblelabs/ripple/data"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
"github.com/CoreumFoundation/coreum-tools/pkg/retry"
coreumapp "github.com/CoreumFoundation/coreum/v3/app"
coreumconfig "github.com/CoreumFoundation/coreum/v3/pkg/config"
coreumintegration "github.com/CoreumFoundation/coreum/v3/testutil/integration"
integrationtests "github.com/CoreumFoundation/coreumbridge-xrpl/integration-tests"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/coreum"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/runner"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/xrpl"
)
const XRPLTokenDecimals = 15
// RunnerEnvConfig is runner environment config.
type RunnerEnvConfig struct {
AwaitTimeout time.Duration
SigningThreshold int
RelayerNumber int
MaliciousRelayerNumber int
DisableMasterKey bool
UsedTicketSequenceThreshold int
TrustSetLimitAmount sdkmath.Int
}
// DefaultRunnerEnvConfig returns default runner environment config.
func DefaultRunnerEnvConfig() RunnerEnvConfig {
return RunnerEnvConfig{
AwaitTimeout: 15 * time.Second,
SigningThreshold: 2,
RelayerNumber: 3,
MaliciousRelayerNumber: 0,
DisableMasterKey: true,
UsedTicketSequenceThreshold: 150,
TrustSetLimitAmount: sdkmath.NewIntWithDecimal(1, 30),
}
}
// RunnerEnv is runner environment used for the integration tests.
type RunnerEnv struct {
Cfg RunnerEnvConfig
bridgeXRPLAddress rippledata.Account
ContractClient *coreum.ContractClient
Chains integrationtests.Chains
ContractOwner sdk.AccAddress
Runners []*runner.Runner
ProcessErrorsMu sync.RWMutex
ProcessErrors []error
}
// NewRunnerEnv returns new instance of the RunnerEnv.
func NewRunnerEnv(ctx context.Context, t *testing.T, cfg RunnerEnvConfig, chains integrationtests.Chains) *RunnerEnv {
relayerCoreumAddresses := genCoreumRelayers(
ctx,
t,
chains.Coreum,
cfg.RelayerNumber,
)
bridgeXRPLAddress, relayerXRPLAddresses, relayerXRPLPubKeys := genBridgeXRPLAccountWithRelayers(
ctx,
t,
chains.XRPL,
cfg.RelayerNumber,
uint32(cfg.SigningThreshold),
cfg.DisableMasterKey,
)
contractRelayer := make([]coreum.Relayer, 0, cfg.RelayerNumber)
for i := 0; i < cfg.RelayerNumber; i++ {
contractRelayer = append(contractRelayer, coreum.Relayer{
CoreumAddress: relayerCoreumAddresses[i],
XRPLAddress: relayerXRPLAddresses[i].String(),
XRPLPubKey: relayerXRPLPubKeys[i].String(),
})
}
contractOwner, contractClient := integrationtests.DeployAndInstantiateContract(
ctx,
t,
chains,
contractRelayer,
cfg.SigningThreshold,
cfg.UsedTicketSequenceThreshold,
cfg.TrustSetLimitAmount,
)
runners := make([]*runner.Runner, 0, cfg.RelayerNumber)
// add correct relayers
for i := 0; i < cfg.RelayerNumber-cfg.MaliciousRelayerNumber; i++ {
runners = append(
runners,
createDevRunner(
t,
chains,
bridgeXRPLAddress,
relayerXRPLAddresses[i],
contractClient.GetContractAddress(),
relayerCoreumAddresses[i],
),
)
}
// add malicious relayers
// we keep the relayer indexes to make all config valid apart from the XRPL signing
for i := cfg.RelayerNumber - cfg.MaliciousRelayerNumber; i < cfg.RelayerNumber; i++ {
maliciousXRPLAddress := chains.XRPL.GenAccount(ctx, t, 0)
runners = append(
runners,
createDevRunner(
t,
chains,
bridgeXRPLAddress,
maliciousXRPLAddress,
contractClient.GetContractAddress(),
relayerCoreumAddresses[i],
),
)
}
runnerEnv := &RunnerEnv{
Cfg: cfg,
bridgeXRPLAddress: bridgeXRPLAddress,
ContractClient: contractClient,
Chains: chains,
ContractOwner: contractOwner,
Runners: runners,
ProcessErrorsMu: sync.RWMutex{},
ProcessErrors: make([]error, 0),
}
t.Cleanup(func() {
runnerEnv.RequireNoErrors(t)
})
return runnerEnv
}
// StartAllRunnerProcesses starts all relayer processes.
func (r *RunnerEnv) StartAllRunnerProcesses(ctx context.Context, t *testing.T) {
errCh := make(chan error, len(r.Runners))
go func() {
for {
select {
case <-ctx.Done():
if !errors.Is(ctx.Err(), context.Canceled) {
r.ProcessErrorsMu.Lock()
r.ProcessErrors = append(r.ProcessErrors, ctx.Err())
r.ProcessErrorsMu.Unlock()
}
return
case err := <-errCh:
r.ProcessErrorsMu.Lock()
r.ProcessErrors = append(r.ProcessErrors, err)
r.ProcessErrorsMu.Unlock()
}
}
}()
for _, relayerRunner := range r.Runners {
go func(relayerRunner *runner.Runner) {
// disable restart on error to handler unexpected errors
xrplTxObserverProcess := relayerRunner.Processes.XRPLTxObserver
xrplTxObserverProcess.IsRestartableOnError = false
xrplTxSubmitterProcess := relayerRunner.Processes.XRPLTxSubmitter
xrplTxSubmitterProcess.IsRestartableOnError = false
err := relayerRunner.Processor.StartProcesses(ctx, xrplTxObserverProcess, xrplTxSubmitterProcess)
if err != nil && !errors.Is(err, context.Canceled) {
t.Logf("Unexpected error on process start:%s", err)
errCh <- err
}
}(relayerRunner)
}
}
// AwaitNoPendingOperations waits for no pendoing contract transactions.
func (r *RunnerEnv) AwaitNoPendingOperations(ctx context.Context, t *testing.T) {
t.Helper()
r.AwaitState(ctx, t, func(t *testing.T) error {
operations, err := r.ContractClient.GetPendingOperations(ctx)
require.NoError(t, err)
if len(operations) != 0 {
return errors.Errorf("there are still pending operatrions: %+v", operations)
}
return nil
})
}
// AwaitCoreumBalance waits for expected coreum balance.
func (r *RunnerEnv) AwaitCoreumBalance(
ctx context.Context,
t *testing.T,
coreumChain integrationtests.CoreumChain,
address sdk.AccAddress,
expectedBalance sdk.Coin,
) {
t.Helper()
awaitContext, awaitContextCancel := context.WithTimeout(ctx, r.Cfg.AwaitTimeout)
t.Cleanup(awaitContextCancel)
require.NoError(t, coreumChain.AwaitForBalance(awaitContext, t, address, expectedBalance))
}
// AwaitState waits for stateChecker function to rerun nil and retires in case of failure.
func (r *RunnerEnv) AwaitState(ctx context.Context, t *testing.T, stateChecker func(t *testing.T) error) {
t.Helper()
retryCtx, retryCancel := context.WithTimeout(ctx, r.Cfg.AwaitTimeout)
defer retryCancel()
err := retry.Do(retryCtx, 500*time.Millisecond, func() error {
if err := stateChecker(t); err != nil {
return retry.Retryable(err)
}
return nil
})
require.NoError(t, err)
}
// AllocateTickets allocate initial tickets amount.
func (r *RunnerEnv) AllocateTickets(
ctx context.Context,
t *testing.T,
numberOfTicketsToAllocate uint32,
) {
bridgeXRPLAccountInfo, err := r.Chains.XRPL.RPCClient().AccountInfo(ctx, r.bridgeXRPLAddress)
require.NoError(t, err)
r.Chains.XRPL.FundAccountForTicketAllocation(ctx, t, r.bridgeXRPLAddress, numberOfTicketsToAllocate)
_, err = r.ContractClient.RecoverTickets(ctx, r.ContractOwner, *bridgeXRPLAccountInfo.AccountData.Sequence, &numberOfTicketsToAllocate)
require.NoError(t, err)
require.NoError(t, err)
r.AwaitNoPendingOperations(ctx, t)
availableTickets, err := r.ContractClient.GetAvailableTickets(ctx)
require.NoError(t, err)
require.Len(t, availableTickets, int(numberOfTicketsToAllocate))
}
// RegisterXRPLOriginatedToken registers XRPL currency and awaits for the trust set ot be set.
func (r *RunnerEnv) RegisterXRPLOriginatedToken(
ctx context.Context,
t *testing.T,
issuer rippledata.Account,
currency rippledata.Currency,
sendingPrecision int32,
maxHoldingAmount sdkmath.Int,
) coreum.XRPLToken {
r.Chains.Coreum.FundAccountWithOptions(ctx, t, r.ContractOwner, coreumintegration.BalancesOptions{
Amount: r.Chains.Coreum.QueryAssetFTParams(ctx, t).IssueFee.Amount,
})
_, err := r.ContractClient.RegisterXRPLToken(ctx, r.ContractOwner, issuer.String(), xrpl.ConvertCurrencyToString(currency), sendingPrecision, maxHoldingAmount)
require.NoError(t, err)
// await for the trust set
r.AwaitNoPendingOperations(ctx, t)
registeredXRPLToken, err := r.ContractClient.GetXRPLToken(ctx, issuer.String(), xrpl.ConvertCurrencyToString(currency))
require.NoError(t, err)
require.NotNil(t, registeredXRPLToken)
require.Equal(t, coreum.TokenStateEnabled, registeredXRPLToken.State)
return *registeredXRPLToken
}
// RequireNoErrors check whether the runner err received runner errors.
func (r *RunnerEnv) RequireNoErrors(t *testing.T) {
r.ProcessErrorsMu.RLock()
defer r.ProcessErrorsMu.RUnlock()
require.Empty(t, r.ProcessErrors, "Found unexpected process errors after the execution")
}
// SendXRPLPaymentTx sends Payment transaction.
func (r *RunnerEnv) SendXRPLPaymentTx(
ctx context.Context,
t *testing.T,
senderAcc, recipientAcc rippledata.Account,
amount rippledata.Amount,
memo rippledata.Memo,
) {
xrpPaymentTx := rippledata.Payment{
Destination: recipientAcc,
Amount: amount,
TxBase: rippledata.TxBase{
TransactionType: rippledata.PAYMENT,
Memos: rippledata.Memos{
memo,
},
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &xrpPaymentTx, senderAcc))
}
// SendXRPLPartialPaymentTx sends Payment transaction with partial payment.
func (r *RunnerEnv) SendXRPLPartialPaymentTx(
ctx context.Context,
t *testing.T,
senderAcc, recipientAcc rippledata.Account,
amount rippledata.Amount,
maxAmount rippledata.Amount,
memo rippledata.Memo,
) {
xrpPaymentTx := rippledata.Payment{
Destination: recipientAcc,
Amount: amount,
SendMax: &maxAmount,
TxBase: rippledata.TxBase{
TransactionType: rippledata.PAYMENT,
Memos: rippledata.Memos{
memo,
},
Flags: lo.ToPtr(rippledata.TxPartialPayment),
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &xrpPaymentTx, senderAcc))
}
func (r *RunnerEnv) EnableXRPLAccountRippling(ctx context.Context, t *testing.T, account rippledata.Account) {
// enable rippling on this account's trust lines.
accountSetTx := rippledata.AccountSet{
SetFlag: lo.ToPtr(uint32(rippledata.TxDefaultRipple)),
TxBase: rippledata.TxBase{
TransactionType: rippledata.ACCOUNT_SET,
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &accountSetTx, account))
}
func (r *RunnerEnv) SendXRPLMaxTrustSetTx(
ctx context.Context,
t *testing.T,
account rippledata.Account,
issuer rippledata.Account,
currency rippledata.Currency,
) {
value, err := rippledata.NewValue("1000000000000", false)
require.NoError(t, err)
trustSetTx := rippledata.TrustSet{
LimitAmount: rippledata.Amount{
Value: value,
Currency: currency,
Issuer: issuer,
},
TxBase: rippledata.TxBase{
TransactionType: rippledata.TRUST_SET,
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &trustSetTx, account))
}
func genCoreumRelayers(
ctx context.Context,
t *testing.T,
coreumChain integrationtests.CoreumChain,
relayersCount int,
) []sdk.AccAddress {
t.Helper()
addresses := make([]sdk.AccAddress, 0, relayersCount)
for i := 0; i < relayersCount; i++ {
relayerAddress := coreumChain.GenAccount()
coreumChain.FundAccountWithOptions(ctx, t, relayerAddress, coreumintegration.BalancesOptions{
Amount: sdkmath.NewIntFromUint64(1_000_000),
})
addresses = append(addresses, relayerAddress)
}
return addresses
}
func genBridgeXRPLAccountWithRelayers(
ctx context.Context,
t *testing.T,
xrplChain integrationtests.XRPLChain,
signersCount int,
signerQuorum uint32,
disableMasterKey bool,
) (rippledata.Account, []rippledata.Account, []rippledata.PublicKey) {
t.Helper()
// some fee to cover simple txs all extras must be allocated in the test
bridgeXRPLAddress := xrplChain.GenAccount(ctx, t, 0.5)
t.Logf("Bridge account is generated, address:%s", bridgeXRPLAddress.String())
signerEntries := make([]rippledata.SignerEntry, 0, signersCount)
signerAccounts := make([]rippledata.Account, 0, signersCount)
signerPubKeys := make([]rippledata.PublicKey, 0, signersCount)
for i := 0; i < signersCount; i++ {
signerAcc := xrplChain.GenAccount(ctx, t, 0)
signerAccounts = append(signerAccounts, signerAcc)
t.Logf("Signer %d is generated, address:%s", i+1, signerAcc.String())
signerEntries = append(signerEntries, rippledata.SignerEntry{
SignerEntry: rippledata.SignerEntryItem{
Account: &signerAcc,
SignerWeight: lo.ToPtr(uint16(1)),
},
})
signerPubKeys = append(signerPubKeys, xrplChain.GetSignerPubKey(t, signerAcc))
}
// fund for the signers SignerListSet
xrplChain.FundAccountForSignerListSet(ctx, t, bridgeXRPLAddress, signersCount)
signerListSetTx := rippledata.SignerListSet{
SignerQuorum: signerQuorum,
SignerEntries: signerEntries,
TxBase: rippledata.TxBase{
TransactionType: rippledata.SIGNER_LIST_SET,
},
}
require.NoError(t, xrplChain.AutoFillSignAndSubmitTx(ctx, t, &signerListSetTx, bridgeXRPLAddress))
t.Logf("The bridge signers set is updated")
if disableMasterKey {
// disable master key
disableMasterKeyTx := rippledata.AccountSet{
TxBase: rippledata.TxBase{
Account: bridgeXRPLAddress,
TransactionType: rippledata.ACCOUNT_SET,
},
SetFlag: lo.ToPtr(uint32(rippledata.TxSetDisableMaster)),
}
require.NoError(t, xrplChain.AutoFillSignAndSubmitTx(ctx, t, &disableMasterKeyTx, bridgeXRPLAddress))
t.Logf("Bridge account master key is disabled")
}
return bridgeXRPLAddress, signerAccounts, signerPubKeys
}
func createDevRunner(
t *testing.T,
chains integrationtests.Chains,
bridgeXRPLAddress rippledata.Account,
xrplRelayerAcc rippledata.Account,
contractAddress sdk.AccAddress,
coreumRelayerAddress sdk.AccAddress,
) *runner.Runner {
t.Helper()
const (
relayerCoreumKeyName = "coreum"
relayerXRPLKeyName = "xrpl"
)
encodingConfig := coreumconfig.NewEncodingConfig(coreumapp.ModuleBasics)
kr := keyring.NewInMemory(encodingConfig.Codec)
// reimport coreum key
coreumKr := chains.Coreum.ClientContext.Keyring()
keyInfo, err := coreumKr.KeyByAddress(coreumRelayerAddress)
require.NoError(t, err)
pass := uuid.NewString()
armor, err := coreumKr.ExportPrivKeyArmor(keyInfo.Name, pass)
require.NoError(t, err)
require.NoError(t, kr.ImportPrivKey(relayerCoreumKeyName, armor, pass))
// reimport XRPL key
xrplKr := chains.XRPL.GetSignerKeyring()
keyInfo, err = xrplKr.Key(xrplRelayerAcc.String())
require.NoError(t, err)
armor, err = xrplKr.ExportPrivKeyArmor(keyInfo.Name, pass)
require.NoError(t, err)
require.NoError(t, kr.ImportPrivKey(relayerXRPLKeyName, armor, pass))
relayerRunnerCfg := runner.DefaultConfig()
relayerRunnerCfg.LoggingConfig.Level = "debug"
relayerRunnerCfg.XRPL.BridgeAccount = bridgeXRPLAddress.String()
relayerRunnerCfg.XRPL.MultiSignerKeyName = relayerXRPLKeyName
relayerRunnerCfg.XRPL.RPC.URL = chains.XRPL.Config().RPCAddress
// make the scanner fast
relayerRunnerCfg.XRPL.Scanner.RetryDelay = 500 * time.Millisecond
relayerRunnerCfg.Coreum.GRPC.URL = chains.Coreum.Config().GRPCAddress
relayerRunnerCfg.Coreum.RelayerKeyName = relayerCoreumKeyName
relayerRunnerCfg.Coreum.Contract.ContractAddress = contractAddress.String()
// We use high gas adjustment since our relayers might send transactions in one block.
// They estimate gas based on the same state, but since transactions are executed one by one the next transaction uses
// the state different from the one it used for the estimation as a result the out-of-gas error might appear.
relayerRunnerCfg.Coreum.Contract.GasAdjustment = 2
relayerRunnerCfg.Coreum.Network.ChainID = chains.Coreum.ChainSettings.ChainID
// make operation fetcher fast
relayerRunnerCfg.Processes.XRPLTxSubmitter.RepeatDelay = 500 * time.Millisecond
relayerRunner, err := runner.NewRunner(relayerRunnerCfg, kr)
require.NoError(t, err)
return relayerRunner
}