@@ -7,6 +7,7 @@ package e2e
77import (
88 "context"
99 "encoding/json"
10+ "errors"
1011 "fmt"
1112 "math/rand"
1213 "strings"
@@ -16,8 +17,13 @@ import (
1617
1718 "github.com/stretchr/testify/require"
1819
20+ "github.com/ava-labs/coreth/core/types"
1921 "github.com/ava-labs/coreth/ethclient"
22+ "github.com/ava-labs/coreth/interfaces"
2023
24+ "golang.org/x/exp/maps"
25+
26+ "github.com/ava-labs/avalanchego/ids"
2127 "github.com/ava-labs/avalanchego/tests"
2228 "github.com/ava-labs/avalanchego/tests/fixture"
2329 "github.com/ava-labs/avalanchego/tests/fixture/testnet"
@@ -58,7 +64,7 @@ type TestEnvironment struct {
5864 // The directory where the test network configuration is stored
5965 NetworkDir string
6066 // URIs used to access the API endpoints of nodes of the network
61- URIs [ ]string
67+ URIs map [ids. NodeID ]string
6268 // The URI used to access the http server that allocates test data
6369 TestDataServerURI string
6470
@@ -78,7 +84,11 @@ func InitTestEnvironment(envBytes []byte) {
7884// nodes.
7985func (te * TestEnvironment ) GetRandomNodeURI () string {
8086 r := rand .New (rand .NewSource (time .Now ().Unix ())) //#nosec G404
81- return te .URIs [r .Intn (len (te .URIs ))]
87+ nodeIDs := maps .Keys (te .URIs )
88+ nodeID := nodeIDs [r .Intn (len (nodeIDs ))]
89+ uri := te .URIs [nodeID ]
90+ tests .Outf ("{{blue}} targeting node %s with URI: %s{{/}}\n " , nodeID , uri )
91+ return uri
8292}
8393
8494// Retrieve the network to target for testing.
@@ -92,6 +102,7 @@ func (te *TestEnvironment) GetNetwork() testnet.Network {
92102func (te * TestEnvironment ) AllocateFundedKeys (count int ) []* secp256k1.PrivateKey {
93103 keys , err := fixture .AllocateFundedKeys (te .TestDataServerURI , count )
94104 te .require .NoError (err )
105+ tests .Outf ("{{blue}} allocated funded key(s): %+v{{/}}\n " , keys )
95106 return keys
96107}
97108
@@ -102,19 +113,14 @@ func (te *TestEnvironment) AllocateFundedKey() *secp256k1.PrivateKey {
102113
103114// Create a new keychain with the specified number of test keys.
104115func (te * TestEnvironment ) NewKeychain (count int ) * secp256k1fx.Keychain {
105- tests .Outf ("{{blue}} initializing keychain with %d keys {{/}}\n " , count )
106116 keys := te .AllocateFundedKeys (count )
117+ tests .Outf ("{{blue}} initializing keychain with %d key(s) {{/}}\n " , count )
107118 return secp256k1fx .NewKeychain (keys ... )
108119}
109120
110- // Create a new wallet for the provided keychain against a random node URI.
111- func (te * TestEnvironment ) NewWallet (keychain * secp256k1fx.Keychain ) primary.Wallet {
112- return te .NewWalletForURI (keychain , te .GetRandomNodeURI ())
113- }
114-
115121// Create a new wallet for the provided keychain against the specified node URI.
116- func (te * TestEnvironment ) NewWalletForURI (keychain * secp256k1fx.Keychain , uri string ) primary.Wallet {
117- tests .Outf ("{{blue}} initializing a new wallet {{/}}\n " )
122+ func (te * TestEnvironment ) NewWallet (keychain * secp256k1fx.Keychain , uri string ) primary.Wallet {
123+ tests .Outf ("{{blue}} initializing a new wallet for URI: %s {{/}}\n " , uri )
118124 wallet , err := primary .MakeWallet (DefaultContext (), & primary.WalletConfig {
119125 URI : uri ,
120126 AVAXKeychain : keychain ,
@@ -124,13 +130,9 @@ func (te *TestEnvironment) NewWalletForURI(keychain *secp256k1fx.Keychain, uri s
124130 return wallet
125131}
126132
127- // Create a new eth client targeting a random node.
128- func (te * TestEnvironment ) NewEthClient () ethclient.Client {
129- return te .NewEthClientForURI (te .GetRandomNodeURI ())
130- }
131-
132133// Create a new eth client targeting the specified node URI.
133- func (te * TestEnvironment ) NewEthClientForURI (nodeURI string ) ethclient.Client {
134+ func (te * TestEnvironment ) NewEthClient (nodeURI string ) ethclient.Client {
135+ tests .Outf ("{{blue}} initializing a new eth client for URI: %s {{/}}\n " , nodeURI )
134136 nodeAddress := strings .Split (nodeURI , "//" )[1 ]
135137 uri := fmt .Sprintf ("ws://%s/ext/bc/C/ws" , nodeAddress )
136138 client , err := ethclient .Dial (uri )
@@ -198,3 +200,51 @@ func WaitForHealthy(node testnet.Node) {
198200 defer cancel ()
199201 require .NoError (ginkgo .GinkgoT (), testnet .WaitForHealthy (ctx , node ))
200202}
203+
204+ // Interface to allow logging any type of transaction that has an ID method.
205+ type LoggableTx interface {
206+ ID () ids.ID
207+ }
208+
209+ // Ensures transaction id is logged if the transaction is non-nil. Should be
210+ // called before checking for an error to ensure traceability in the event
211+ // of a transaction being submitted but failing to be accepted.
212+ func LogTx (tx LoggableTx ) {
213+ if tx != nil {
214+ tests .Outf (" tx id: %s\n " , tx .ID ())
215+ }
216+ }
217+
218+ // Ensures transaction id is logged if the transaction is non-nil and check
219+ // if an error occurred.
220+ func LogTxAndCheck (tx LoggableTx , err error ) {
221+ LogTx (tx )
222+ require .NoError (ginkgo .GinkgoT (), err )
223+ }
224+
225+ // Sends an eth transaction, waits for the transaction receipt to be issued
226+ // and checks that the receipt indicates success.
227+ func SendEthTransaction (ethClient ethclient.Client , signedTx * types.Transaction ) * types.Receipt {
228+ require := require .New (ginkgo .GinkgoT ())
229+
230+ txID := signedTx .Hash ()
231+ tests .Outf (" eth tx id: %s\n " , txID )
232+
233+ require .NoError (ethClient .SendTransaction (DefaultContext (), signedTx ))
234+
235+ // Wait for the receipt
236+ var receipt * types.Receipt
237+ Eventually (func () bool {
238+ var err error
239+ receipt , err = ethClient .TransactionReceipt (DefaultContext (), txID )
240+ if errors .Is (err , interfaces .NotFound ) {
241+ return false // Transaction is still pending
242+ }
243+ require .NoError (err )
244+ return true
245+ }, DefaultTimeout , DefaultPollingInterval , "failed to see transaction acceptance before timeout" )
246+
247+ // Retrieve the contract address
248+ require .Equal (receipt .Status , types .ReceiptStatusSuccessful )
249+ return receipt
250+ }
0 commit comments