Skip to content

Commit

Permalink
[e2e] Abstract usage of ginkgo with a new test context (#3254)
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Aug 8, 2024
1 parent c27dd0a commit 562d4f6
Show file tree
Hide file tree
Showing 20 changed files with 611 additions and 472 deletions.
30 changes: 0 additions & 30 deletions tests/colors.go

This file was deleted.

32 changes: 32 additions & 0 deletions tests/context_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package tests

import (
"context"
"time"

"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

// A long default timeout used to timeout failed operations but unlikely to induce
// flaking due to unexpected resource contention.
const DefaultTimeout = 2 * time.Minute

// Helper simplifying use of a timed context by canceling the context with the test context.
func ContextWithTimeout(tc TestContext, duration time.Duration) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), duration)
tc.DeferCleanup(cancel)
return ctx
}

// Helper simplifying use of a timed context configured with the default timeout.
func DefaultContext(tc TestContext) context.Context {
return ContextWithTimeout(tc, DefaultTimeout)
}

// Helper simplifying use via an option of a timed context configured with the default timeout.
func WithDefaultContext(tc TestContext) common.Option {
return common.WithContext(DefaultContext(tc))
}
39 changes: 20 additions & 19 deletions tests/e2e/banff/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/units"
Expand All @@ -20,12 +19,14 @@ import (
)

var _ = ginkgo.Describe("[Banff]", func() {
require := require.New(ginkgo.GinkgoT())
tc := e2e.NewTestContext()
require := require.New(tc)

ginkgo.It("can send custom assets X->P and P->X",
func() {
keychain := e2e.Env.NewKeychain(1)
wallet := e2e.NewWallet(keychain, e2e.Env.GetRandomNodeURI())
env := e2e.GetEnv(tc)
keychain := env.NewKeychain(1)
wallet := e2e.NewWallet(tc, keychain, env.GetRandomNodeURI())

// Get the P-chain and the X-chain wallets
pWallet := wallet.P()
Expand All @@ -43,7 +44,7 @@ var _ = ginkgo.Describe("[Banff]", func() {
}

var assetID ids.ID
ginkgo.By("create new X-chain asset", func() {
tc.By("create new X-chain asset", func() {
assetTx, err := xWallet.IssueCreateAssetTx(
"RnM",
"RNM",
Expand All @@ -56,15 +57,15 @@ var _ = ginkgo.Describe("[Banff]", func() {
},
},
},
e2e.WithDefaultContext(),
tc.WithDefaultContext(),
)
require.NoError(err)
assetID = assetTx.ID()

tests.Outf("{{green}}created new X-chain asset{{/}}: %s\n", assetID)
tc.Outf("{{green}}created new X-chain asset{{/}}: %s\n", assetID)
})

ginkgo.By("export new X-chain asset to P-chain", func() {
tc.By("export new X-chain asset to P-chain", func() {
tx, err := xWallet.IssueExportTx(
constants.PlatformChainID,
[]*avax.TransferableOutput{
Expand All @@ -78,25 +79,25 @@ var _ = ginkgo.Describe("[Banff]", func() {
},
},
},
e2e.WithDefaultContext(),
tc.WithDefaultContext(),
)
require.NoError(err)

tests.Outf("{{green}}issued X-chain export{{/}}: %s\n", tx.ID())
tc.Outf("{{green}}issued X-chain export{{/}}: %s\n", tx.ID())
})

ginkgo.By("import new asset from X-chain on the P-chain", func() {
tc.By("import new asset from X-chain on the P-chain", func() {
tx, err := pWallet.IssueImportTx(
xChainID,
owner,
e2e.WithDefaultContext(),
tc.WithDefaultContext(),
)
require.NoError(err)

tests.Outf("{{green}}issued P-chain import{{/}}: %s\n", tx.ID())
tc.Outf("{{green}}issued P-chain import{{/}}: %s\n", tx.ID())
})

ginkgo.By("export asset from P-chain to the X-chain", func() {
tc.By("export asset from P-chain to the X-chain", func() {
tx, err := pWallet.IssueExportTx(
xChainID,
[]*avax.TransferableOutput{
Expand All @@ -110,22 +111,22 @@ var _ = ginkgo.Describe("[Banff]", func() {
},
},
},
e2e.WithDefaultContext(),
tc.WithDefaultContext(),
)
require.NoError(err)

tests.Outf("{{green}}issued P-chain export{{/}}: %s\n", tx.ID())
tc.Outf("{{green}}issued P-chain export{{/}}: %s\n", tx.ID())
})

ginkgo.By("import asset from P-chain on the X-chain", func() {
tc.By("import asset from P-chain on the X-chain", func() {
tx, err := xWallet.IssueImportTx(
constants.PlatformChainID,
owner,
e2e.WithDefaultContext(),
tc.WithDefaultContext(),
)
require.NoError(err)

tests.Outf("{{green}}issued X-chain import{{/}}: %s\n", tx.ID())
tc.Outf("{{green}}issued X-chain import{{/}}: %s\n", tx.ID())
})
})
})
56 changes: 28 additions & 28 deletions tests/e2e/c/dynamic_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
Expand All @@ -26,7 +25,8 @@ import (
// well as its ABI contained in `hashing_contract.go`.

var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
require := require.New(ginkgo.GinkgoT())
tc := e2e.NewTestContext()
require := require.New(tc)

// Need a gas limit much larger than the standard 21_000 to enable
// the contract to induce a gas price increase
Expand All @@ -36,24 +36,24 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
gasTip := big.NewInt(1000 * params.GWei)

ginkgo.It("should ensure that the gas price is affected by load", func() {
ginkgo.By("creating a new private network to ensure isolation from other tests")
tc.By("creating a new private network to ensure isolation from other tests")
privateNetwork := tmpnet.NewDefaultNetwork("avalanchego-e2e-dynamic-fees")
e2e.Env.StartPrivateNetwork(privateNetwork)
e2e.GetEnv(tc).StartPrivateNetwork(privateNetwork)

ginkgo.By("allocating a pre-funded key")
tc.By("allocating a pre-funded key")
key := privateNetwork.PreFundedKeys[0]
ethAddress := evm.GetEthAddress(key)

ginkgo.By("initializing a coreth client")
tc.By("initializing a coreth client")
node := privateNetwork.Nodes[0]
nodeURI := tmpnet.NodeURI{
NodeID: node.NodeID,
URI: node.URI,
}
ethClient := e2e.NewEthClient(nodeURI)
ethClient := e2e.NewEthClient(tc, nodeURI)

ginkgo.By("initializing a transaction signer")
cChainID, err := ethClient.ChainID(e2e.DefaultContext())
tc.By("initializing a transaction signer")
cChainID, err := ethClient.ChainID(tc.DefaultContext())
require.NoError(err)
signer := types.NewEIP155Signer(cChainID)
ecdsaKey := key.ToECDSA()
Expand All @@ -64,9 +64,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
}

var contractAddress common.Address
ginkgo.By("deploying an expensive contract", func() {
tc.By("deploying an expensive contract", func() {
// Create transaction
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
require.NoError(err)
compiledContract := common.Hex2Bytes(hashingCompiledContract)
tx := types.NewTx(&types.LegacyTx{
Expand All @@ -79,36 +79,36 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {

// Send the transaction and wait for acceptance
signedTx := sign(tx)
receipt := e2e.SendEthTransaction(ethClient, signedTx)
receipt := e2e.SendEthTransaction(tc, ethClient, signedTx)

contractAddress = receipt.ContractAddress
})

var gasPrice *big.Int
ginkgo.By("calling the expensive contract repeatedly until a gas price increase is detected", func() {
tc.By("calling the expensive contract repeatedly until a gas price increase is detected", func() {
// Evaluate the bytes representation of the contract
hashingABI, err := abi.JSON(strings.NewReader(hashingABIJson))
require.NoError(err)
contractData, err := hashingABI.Pack("hashIt")
require.NoError(err)

var initialGasPrice *big.Int
e2e.Eventually(func() bool {
tc.Eventually(func() bool {
// Check the gas price
var err error
gasPrice, err = ethClient.SuggestGasPrice(e2e.DefaultContext())
gasPrice, err = ethClient.SuggestGasPrice(tc.DefaultContext())
require.NoError(err)
if initialGasPrice == nil {
initialGasPrice = gasPrice
tests.Outf("{{blue}}initial gas price is %v{{/}}\n", initialGasPrice)
tc.Outf("{{blue}}initial gas price is %v{{/}}\n", initialGasPrice)
} else if gasPrice.Cmp(initialGasPrice) > 0 {
// Gas price has increased
tests.Outf("{{blue}}gas price has increased to %v{{/}}\n", gasPrice)
tc.Outf("{{blue}}gas price has increased to %v{{/}}\n", gasPrice)
return true
}

// Create the transaction
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
require.NoError(err)
tx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
Expand All @@ -121,33 +121,33 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {

// Send the transaction and wait for acceptance
signedTx := sign(tx)
_ = e2e.SendEthTransaction(ethClient, signedTx)
_ = e2e.SendEthTransaction(tc, ethClient, signedTx)

// The gas price will be checked at the start of the next iteration
return false
}, e2e.DefaultTimeout, e2e.DefaultPollingInterval, "failed to see gas price increase before timeout")
})

ginkgo.By("waiting for the gas price to decrease...", func() {
tc.By("waiting for the gas price to decrease...", func() {
initialGasPrice := gasPrice
e2e.Eventually(func() bool {
tc.Eventually(func() bool {
var err error
gasPrice, err = ethClient.SuggestGasPrice(e2e.DefaultContext())
gasPrice, err = ethClient.SuggestGasPrice(tc.DefaultContext())
require.NoError(err)
tests.Outf("{{blue}}.{{/}}")
tc.Outf("{{blue}}.{{/}}")
return initialGasPrice.Cmp(gasPrice) > 0
}, e2e.DefaultTimeout, e2e.DefaultPollingInterval, "failed to see gas price decrease before timeout")
tests.Outf("\n{{blue}}gas price has decreased to %v{{/}}\n", gasPrice)
tc.Outf("\n{{blue}}gas price has decreased to %v{{/}}\n", gasPrice)
})

ginkgo.By("sending funds at the current gas price", func() {
tc.By("sending funds at the current gas price", func() {
// Create a recipient address
recipientKey, err := secp256k1.NewPrivateKey()
require.NoError(err)
recipientEthAddress := evm.GetEthAddress(recipientKey)

// Create transaction
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
require.NoError(err)
tx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
Expand All @@ -159,9 +159,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {

// Send the transaction and wait for acceptance
signedTx := sign(tx)
_ = e2e.SendEthTransaction(ethClient, signedTx)
_ = e2e.SendEthTransaction(tc, ethClient, signedTx)
})

_ = e2e.CheckBootstrapIsPossible(privateNetwork)
_ = e2e.CheckBootstrapIsPossible(tc, privateNetwork)
})
})
Loading

0 comments on commit 562d4f6

Please sign in to comment.