forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
e2e.go
113 lines (94 loc) · 3.54 KB
/
e2e.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// e2e implements the e2e tests.
package e2e
import (
"context"
"encoding/json"
"math/rand"
"time"
ginkgo "github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/require"
"github.com/MetalBlockchain/metalgo/tests"
"github.com/MetalBlockchain/metalgo/tests/fixture"
"github.com/MetalBlockchain/metalgo/tests/fixture/testnet"
"github.com/MetalBlockchain/metalgo/tests/fixture/testnet/local"
"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
"github.com/MetalBlockchain/metalgo/wallet/subnet/primary"
)
const (
// Enough for primary.NewWallet to fetch initial UTXOs.
DefaultWalletCreationTimeout = 5 * time.Second
// Defines default tx confirmation timeout.
// Enough for test/custom networks.
DefaultConfirmTxTimeout = 20 * time.Second
// A long default timeout used to timeout failed operations but
// unlikely to induce flaking due to unexpected resource
// contention.
DefaultTimeout = 2 * time.Minute
// Interval appropriate for network operations that should be
// retried periodically but not too often.
DefaultPollingInterval = 500 * time.Millisecond
)
// Env is used to access shared test fixture. Intended to be
// initialized by SynchronizedBeforeSuite.
var Env *TestEnvironment
type TestEnvironment struct {
// The directory where the test network configuration is stored
NetworkDir string
// URIs used to access the API endpoints of nodes of the network
URIs []string
// The URI used to access the http server that allocates test data
TestDataServerURI string
require *require.Assertions
}
func InitTestEnvironment(envBytes []byte) {
require := require.New(ginkgo.GinkgoT())
require.Nil(Env, "env already initialized")
Env = &TestEnvironment{
require: require,
}
require.NoError(json.Unmarshal(envBytes, Env))
}
// Retrieve a random URI to naively attempt to spread API load across
// nodes.
func (te *TestEnvironment) GetRandomNodeURI() string {
r := rand.New(rand.NewSource(time.Now().Unix())) //#nosec G404
return te.URIs[r.Intn(len(te.URIs))]
}
// Retrieve the network to target for testing.
func (te *TestEnvironment) GetNetwork() testnet.Network {
network, err := local.ReadNetwork(te.NetworkDir)
te.require.NoError(err)
return network
}
// Retrieve the specified number of funded keys allocated for the caller's exclusive use.
func (te *TestEnvironment) AllocateFundedKeys(count int) []*secp256k1.PrivateKey {
keys, err := fixture.AllocateFundedKeys(te.TestDataServerURI, count)
te.require.NoError(err)
return keys
}
// Retrieve a funded key allocated for the caller's exclusive use.
func (te *TestEnvironment) AllocateFundedKey() *secp256k1.PrivateKey {
return te.AllocateFundedKeys(1)[0]
}
// Create a new keychain with the specified number of test keys.
func (te *TestEnvironment) NewKeychain(count int) *secp256k1fx.Keychain {
tests.Outf("{{blue}} initializing keychain with %d keys {{/}}\n", count)
keys := te.AllocateFundedKeys(count)
return secp256k1fx.NewKeychain(keys...)
}
// Create a new wallet for the provided keychain.
func (te *TestEnvironment) NewWallet(keychain *secp256k1fx.Keychain) primary.Wallet {
tests.Outf("{{blue}} initializing a new wallet {{/}}\n")
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
URI: te.GetRandomNodeURI(),
AVAXKeychain: keychain,
EthKeychain: keychain,
})
te.require.NoError(err)
return wallet
}