-
Notifications
You must be signed in to change notification settings - Fork 644
/
helpers.go
268 lines (224 loc) · 8.84 KB
/
helpers.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package e2e
import (
"context"
"errors"
"fmt"
"math/big"
"os"
"strings"
"time"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/ethclient"
"github.com/ava-labs/coreth/interfaces"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
ginkgo "github.com/onsi/ginkgo/v2"
)
const (
// A long default timeout used to timeout failed operations but
// unlikely to induce flaking due to unexpected resource
// contention.
DefaultTimeout = 2 * time.Minute
DefaultPollingInterval = tmpnet.DefaultPollingInterval
// Setting this env will disable post-test bootstrap
// checks. Useful for speeding up iteration during test
// development.
SkipBootstrapChecksEnvName = "E2E_SKIP_BOOTSTRAP_CHECKS"
DefaultValidatorStartTimeDiff = tmpnet.DefaultValidatorStartTimeDiff
DefaultGasLimit = uint64(21000) // Standard gas limit
// An empty string prompts the use of the default path which ensures a
// predictable target for github's upload-artifact action.
DefaultNetworkDir = ""
// Directory used to store private networks (specific to a single test)
// under the shared network dir.
PrivateNetworksDirName = "private_networks"
)
// Create a new wallet for the provided keychain against the specified node URI.
func NewWallet(keychain *secp256k1fx.Keychain, nodeURI tmpnet.NodeURI) primary.Wallet {
tests.Outf("{{blue}} initializing a new wallet for node %s with URI: %s {{/}}\n", nodeURI.NodeID, nodeURI.URI)
baseWallet, err := primary.MakeWallet(DefaultContext(), &primary.WalletConfig{
URI: nodeURI.URI,
AVAXKeychain: keychain,
EthKeychain: keychain,
})
require.NoError(ginkgo.GinkgoT(), err)
return primary.NewWalletWithOptions(
baseWallet,
common.WithPostIssuanceFunc(
func(id ids.ID) {
tests.Outf(" issued transaction with ID: %s\n", id)
},
),
)
}
// Create a new eth client targeting the specified node URI.
func NewEthClient(nodeURI tmpnet.NodeURI) ethclient.Client {
tests.Outf("{{blue}} initializing a new eth client for node %s with URI: %s {{/}}\n", nodeURI.NodeID, nodeURI.URI)
nodeAddress := strings.Split(nodeURI.URI, "//")[1]
uri := fmt.Sprintf("ws://%s/ext/bc/C/ws", nodeAddress)
client, err := ethclient.Dial(uri)
require.NoError(ginkgo.GinkgoT(), err)
return client
}
// Helper simplifying use of a timed context by canceling the context on ginkgo teardown.
func ContextWithTimeout(duration time.Duration) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), duration)
ginkgo.DeferCleanup(cancel)
return ctx
}
// Helper simplifying use of a timed context configured with the default timeout.
func DefaultContext() context.Context {
return ContextWithTimeout(DefaultTimeout)
}
// Helper simplifying use via an option of a timed context configured with the default timeout.
func WithDefaultContext() common.Option {
return common.WithContext(DefaultContext())
}
// Re-implementation of testify/require.Eventually that is compatible with ginkgo. testify's
// version calls the condition function with a goroutine and ginkgo assertions don't work
// properly in goroutines.
func Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msg string) {
ticker := time.NewTicker(tick)
defer ticker.Stop()
ctx, cancel := context.WithTimeout(context.Background(), waitFor)
defer cancel()
for !condition() {
select {
case <-ctx.Done():
require.Fail(ginkgo.GinkgoT(), msg)
case <-ticker.C:
}
}
}
// Adds an ephemeral node intended to be used by a single test.
func AddEphemeralNode(network *tmpnet.Network, flags tmpnet.FlagsMap) *tmpnet.Node {
require := require.New(ginkgo.GinkgoT())
node := tmpnet.NewEphemeralNode(flags)
require.NoError(network.StartNode(DefaultContext(), ginkgo.GinkgoWriter, node))
ginkgo.DeferCleanup(func() {
tests.Outf("shutting down ephemeral node %q\n", node.NodeID)
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
require.NoError(node.Stop(ctx))
})
return node
}
// Wait for the given node to report healthy.
func WaitForHealthy(node *tmpnet.Node) {
// Need to use explicit context (vs DefaultContext()) to support use with DeferCleanup
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
require.NoError(ginkgo.GinkgoT(), tmpnet.WaitForHealthy(ctx, node))
}
// Sends an eth transaction, waits for the transaction receipt to be issued
// and checks that the receipt indicates success.
func SendEthTransaction(ethClient ethclient.Client, signedTx *types.Transaction) *types.Receipt {
require := require.New(ginkgo.GinkgoT())
txID := signedTx.Hash()
tests.Outf(" sending eth transaction with ID: %s\n", txID)
require.NoError(ethClient.SendTransaction(DefaultContext(), signedTx))
// Wait for the receipt
var receipt *types.Receipt
Eventually(func() bool {
var err error
receipt, err = ethClient.TransactionReceipt(DefaultContext(), txID)
if errors.Is(err, interfaces.NotFound) {
return false // Transaction is still pending
}
require.NoError(err)
return true
}, DefaultTimeout, DefaultPollingInterval, "failed to see transaction acceptance before timeout")
require.Equal(types.ReceiptStatusSuccessful, receipt.Status)
return receipt
}
// Determines the suggested gas price for the configured client that will
// maximize the chances of transaction acceptance.
func SuggestGasPrice(ethClient ethclient.Client) *big.Int {
gasPrice, err := ethClient.SuggestGasPrice(DefaultContext())
require.NoError(ginkgo.GinkgoT(), err)
// Double the suggested gas price to maximize the chances of
// acceptance. Maybe this can be revisited pending resolution of
// https://github.com/ava-labs/coreth/issues/314.
gasPrice.Add(gasPrice, gasPrice)
return gasPrice
}
// Helper simplifying use via an option of a gas price appropriate for testing.
func WithSuggestedGasPrice(ethClient ethclient.Client) common.Option {
baseFee := SuggestGasPrice(ethClient)
return common.WithBaseFee(baseFee)
}
// Verify that a new node can bootstrap into the network. This function is safe to call
// from `Teardown` by virtue of not depending on ginkgo.DeferCleanup.
func CheckBootstrapIsPossible(network *tmpnet.Network) {
require := require.New(ginkgo.GinkgoT())
if len(os.Getenv(SkipBootstrapChecksEnvName)) > 0 {
tests.Outf("{{yellow}}Skipping bootstrap check due to the %s env var being set", SkipBootstrapChecksEnvName)
return
}
ginkgo.By("checking if bootstrap is possible with the current network state")
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
node := tmpnet.NewEphemeralNode(tmpnet.FlagsMap{})
require.NoError(network.StartNode(ctx, ginkgo.GinkgoWriter, node))
// StartNode will initiate node stop if an error is encountered during start,
// so no further cleanup effort is required if an error is seen here.
// Ensure the node is always stopped at the end of the check
defer func() {
ctx, cancel = context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
require.NoError(node.Stop(ctx))
}()
// Check that the node becomes healthy within timeout
require.NoError(tmpnet.WaitForHealthy(ctx, node))
}
// Start a temporary network with the provided avalanchego binary.
func StartNetwork(
network *tmpnet.Network,
avalancheGoExecPath string,
pluginDir string,
shutdownDelay time.Duration,
reuseNetwork bool,
) {
require := require.New(ginkgo.GinkgoT())
require.NoError(
tmpnet.StartNewNetwork(
DefaultContext(),
ginkgo.GinkgoWriter,
network,
DefaultNetworkDir,
avalancheGoExecPath,
pluginDir,
tmpnet.DefaultNodeCount,
),
)
tests.Outf("{{green}}Successfully started network{{/}}\n")
symlinkPath, err := tmpnet.GetReusableNetworkPathForOwner(network.Owner)
require.NoError(err)
if reuseNetwork {
// Symlink the path of the created network to the default owner path (e.g. latest_avalanchego-e2e)
// to enable easy discovery for reuse.
require.NoError(os.Symlink(network.Dir, symlinkPath))
tests.Outf("{{green}}Symlinked %s to %s to enable reuse{{/}}\n", network.Dir, symlinkPath)
}
ginkgo.DeferCleanup(func() {
if reuseNetwork {
tests.Outf("{{yellow}}Skipping shutdown for network %s (symlinked to %s) to enable reuse{{/}}\n", network.Dir, symlinkPath)
return
}
if shutdownDelay > 0 {
tests.Outf("Waiting %s before network shutdown to ensure final metrics scrape\n", shutdownDelay)
time.Sleep(shutdownDelay)
}
tests.Outf("Shutting down network\n")
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
require.NoError(network.Stop(ctx))
})
}