@@ -6,6 +6,8 @@ package e2e
6
6
7
7
import (
8
8
"context"
9
+ "encoding/json"
10
+ "fmt"
9
11
"math/rand"
10
12
"os"
11
13
"time"
@@ -19,6 +21,8 @@ import (
19
21
"github.com/ava-labs/avalanchego/tests/fixture/testnet"
20
22
"github.com/ava-labs/avalanchego/tests/fixture/testnet/local"
21
23
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
24
+ "github.com/ava-labs/avalanchego/vms/secp256k1fx"
25
+ "github.com/ava-labs/avalanchego/wallet/subnet/primary"
22
26
)
23
27
24
28
const (
@@ -41,7 +45,7 @@ const (
41
45
42
46
// Env is used to access shared test fixture. Intended to be
43
47
// initialized by SynchronizedBeforeSuite.
44
- var Env TestEnvironment
48
+ var Env * TestEnvironment
45
49
46
50
type TestEnvironment struct {
47
51
// The directory where the test network configuration is stored
@@ -50,6 +54,17 @@ type TestEnvironment struct {
50
54
URIs []string
51
55
// The URI used to access the http server that allocates test data
52
56
TestDataServerURI string
57
+
58
+ require * require.Assertions
59
+ }
60
+
61
+ func InitTestEnvironment (envBytes []byte ) {
62
+ require := require .New (ginkgo .GinkgoT ())
63
+ require .Nil (Env , "env already initialized" )
64
+ Env = & TestEnvironment {
65
+ require : require ,
66
+ }
67
+ require .NoError (json .Unmarshal (envBytes , Env ))
53
68
}
54
69
55
70
// Retrieve a random URI to naively attempt to spread API load across
@@ -62,7 +77,7 @@ func (te *TestEnvironment) GetRandomNodeURI() string {
62
77
// Retrieve the network to target for testing.
63
78
func (te * TestEnvironment ) GetNetwork () testnet.Network {
64
79
network , err := local .ReadNetwork (te .NetworkDir )
65
- require .NoError (ginkgo . GinkgoT (), err )
80
+ te . require .NoError (err )
66
81
return network
67
82
}
68
83
@@ -78,6 +93,24 @@ func (te *TestEnvironment) AllocateFundedKey() *secp256k1.PrivateKey {
78
93
return te .AllocateFundedKeys (1 )[0 ]
79
94
}
80
95
96
+ // Create a new keychain with the specified number of test keys.
97
+ func (te * TestEnvironment ) NewKeychain (count int ) * secp256k1fx.Keychain {
98
+ tests .Outf (fmt .Sprintf ("{{blue}} initializing keychain with %d keys {{/}}\n " , count ))
99
+ keys := te .AllocateFundedKeys (count )
100
+ return secp256k1fx .NewKeychain (keys ... )
101
+ }
102
+
103
+ // Create a new wallet for the provided keychain.
104
+ func (te * TestEnvironment ) NewWallet (keychain * secp256k1fx.Keychain ) primary.Wallet {
105
+ tests .Outf ("{{blue}} initializing a new wallet {{/}}\n " )
106
+ ctx , cancel := context .WithTimeout (context .Background (), DefaultWalletCreationTimeout )
107
+ defer cancel ()
108
+ wallet , err := primary .NewWalletFromURI (ctx , te .GetRandomNodeURI (), keychain )
109
+ te .require .NoError (err )
110
+ tests .Outf ("{{green}}wallet initialized{{/}}\n " )
111
+ return wallet
112
+ }
113
+
81
114
// Add a temporary node that is only intended to be used by a single
82
115
// test. Its ID and URI are not intended to be returned from The
83
116
// Network instance to avoid accessibility from other tests.
0 commit comments