Skip to content

Commit

Permalink
tmpnet: Merge tmpnet/local to tmpnet package
Browse files Browse the repository at this point in the history
This is the first step in a refactor in support of deploying temporary
networks to kubernetes.
  • Loading branch information
marun committed Dec 10, 2023
1 parent ed93dc9 commit 5bb8d41
Show file tree
Hide file tree
Showing 19 changed files with 499 additions and 581 deletions.
8 changes: 4 additions & 4 deletions tests/e2e/c/dynamic_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
privateNetwork := e2e.Env.NewPrivateNetwork()

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

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

Expand Down
18 changes: 8 additions & 10 deletions tests/e2e/faultinjection/duplicate_node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ var _ = ginkgo.Describe("Duplicate node handling", func() {

ginkgo.It("should ensure that a given Node ID (i.e. staking keypair) can be used at most once on a network", func() {
network := e2e.Env.GetNetwork()
nodes := network.GetNodes()

ginkgo.By("creating new node")
node1 := e2e.AddEphemeralNode(network, tmpnet.FlagsMap{})
e2e.WaitForHealthy(node1)

ginkgo.By("checking that the new node is connected to its peers")
checkConnectedPeers(nodes, node1)
checkConnectedPeers(network.Nodes, node1)

ginkgo.By("creating a second new node with the same staking keypair as the first new node")
node1Flags := node1.GetConfig().Flags
node1Flags := node1.Flags
node2Flags := tmpnet.FlagsMap{
config.StakingTLSKeyContentKey: node1Flags[config.StakingTLSKeyContentKey],
config.StakingCertContentKey: node1Flags[config.StakingCertContentKey],
Expand All @@ -56,37 +55,36 @@ var _ = ginkgo.Describe("Duplicate node handling", func() {
e2e.WaitForHealthy(node2)

ginkgo.By("checking that the second new node is connected to its peers")
checkConnectedPeers(nodes, node2)
checkConnectedPeers(network.Nodes, node2)

// A bootstrap check was already performed by the second node.
})
})

// Check that a new node is connected to existing nodes and vice versa
func checkConnectedPeers(existingNodes []tmpnet.Node, newNode tmpnet.Node) {
func checkConnectedPeers(existingNodes []*tmpnet.Node, newNode *tmpnet.Node) {
require := require.New(ginkgo.GinkgoT())

// Collect the node ids of the new node's peers
infoClient := info.NewClient(newNode.GetProcessContext().URI)
infoClient := info.NewClient(newNode.NodeProcessContext.URI)
peers, err := infoClient.Peers(e2e.DefaultContext())
require.NoError(err)
peerIDs := set.NewSet[ids.NodeID](len(existingNodes))
for _, peer := range peers {
peerIDs.Add(peer.ID)
}

newNodeID := newNode.GetID()
for _, existingNode := range existingNodes {
// Check that the existing node is a peer of the new node
require.True(peerIDs.Contains(existingNode.GetID()))
require.True(peerIDs.Contains(existingNode.ID))

// Check that the new node is a peer
infoClient := info.NewClient(existingNode.GetProcessContext().URI)
infoClient := info.NewClient(existingNode.NodeProcessContext.URI)
peers, err := infoClient.Peers(e2e.DefaultContext())
require.NoError(err)
isPeer := false
for _, peer := range peers {
if peer.ID == newNodeID {
if peer.ID == newNode.ID {
isPeer = true
break
}
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
network := e2e.Env.GetNetwork()

ginkgo.By("checking that the network has a compatible minimum stake duration", func() {
minStakeDuration := cast.ToDuration(network.GetConfig().DefaultFlags[config.MinStakeDurationKey])
minStakeDuration := cast.ToDuration(network.DefaultFlags[config.MinStakeDurationKey])
require.Equal(tmpnet.DefaultMinStakeDuration, minStakeDuration)
})

Expand Down Expand Up @@ -91,7 +91,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
e2e.WaitForHealthy(node)

ginkgo.By("retrieving new node's id and pop")
infoClient := info.NewClient(node.GetProcessContext().URI)
infoClient := info.NewClient(node.NodeProcessContext.URI)
nodeID, nodePOP, err := infoClient.GetNodeID(e2e.DefaultContext())
require.NoError(err)

Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/p/staking_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
network := e2e.Env.GetNetwork()

ginkgo.By("checking that the network has a compatible minimum stake duration", func() {
minStakeDuration := cast.ToDuration(network.GetConfig().DefaultFlags[config.MinStakeDurationKey])
minStakeDuration := cast.ToDuration(network.DefaultFlags[config.MinStakeDurationKey])
require.Equal(tmpnet.DefaultMinStakeDuration, minStakeDuration)
})

Expand Down Expand Up @@ -94,16 +94,16 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
pWallet := baseWallet.P()

ginkgo.By("retrieving alpha node id and pop")
alphaInfoClient := info.NewClient(alphaNode.GetProcessContext().URI)
alphaInfoClient := info.NewClient(alphaNode.NodeProcessContext.URI)
alphaNodeID, alphaPOP, err := alphaInfoClient.GetNodeID(e2e.DefaultContext())
require.NoError(err)

ginkgo.By("retrieving beta node id and pop")
betaInfoClient := info.NewClient(betaNode.GetProcessContext().URI)
betaInfoClient := info.NewClient(betaNode.NodeProcessContext.URI)
betaNodeID, betaPOP, err := betaInfoClient.GetNodeID(e2e.DefaultContext())
require.NoError(err)

pvmClient := platformvm.NewClient(alphaNode.GetProcessContext().URI)
pvmClient := platformvm.NewClient(alphaNode.NodeProcessContext.URI)

const (
delegationPercent = 0.10 // 10%
Expand Down
19 changes: 9 additions & 10 deletions tests/fixture/e2e/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet/local"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand Down Expand Up @@ -60,14 +59,14 @@ func NewTestEnvironment(flagVars *FlagVars) *TestEnvironment {
networkDir := flagVars.NetworkDir()

// Load or create a test network
var network *local.LocalNetwork
var network *tmpnet.Network
if len(networkDir) > 0 {
var err error
network, err = local.ReadNetwork(networkDir)
network, err = tmpnet.ReadNetwork(networkDir)
require.NoError(err)
tests.Outf("{{yellow}}Using an existing network configured at %s{{/}}\n", network.Dir)
} else {
network = StartLocalNetwork(flagVars.AvalancheGoExecPath(), DefaultNetworkDir)
network = StartNetwork(flagVars.AvalancheGoExecPath(), DefaultNetworkDir)
}

uris := network.GetURIs()
Expand All @@ -92,13 +91,13 @@ func NewTestEnvironment(flagVars *FlagVars) *TestEnvironment {
func (te *TestEnvironment) GetRandomNodeURI() tmpnet.NodeURI {
r := rand.New(rand.NewSource(time.Now().Unix())) //#nosec G404
nodeURI := te.URIs[r.Intn(len(te.URIs))]
tests.Outf("{{blue}} targeting node %s with URI: %s{{/}}\n", nodeURI.NodeID, nodeURI.URI)
tests.Outf("{{blue}} targeting node %s with URI: %s{{/}}\n", nodeURI.ID, nodeURI.URI)
return nodeURI
}

// Retrieve the network to target for testing.
func (te *TestEnvironment) GetNetwork() tmpnet.Network {
network, err := local.ReadNetwork(te.NetworkDir)
func (te *TestEnvironment) GetNetwork() *tmpnet.Network {
network, err := tmpnet.ReadNetwork(te.NetworkDir)
te.require.NoError(err)
return network
}
Expand All @@ -123,15 +122,15 @@ func (te *TestEnvironment) NewKeychain(count int) *secp256k1fx.Keychain {
}

// Create a new private network that is not shared with other tests.
func (te *TestEnvironment) NewPrivateNetwork() tmpnet.Network {
func (te *TestEnvironment) NewPrivateNetwork() *tmpnet.Network {
// Load the shared network to retrieve its path and exec path
sharedNetwork, err := local.ReadNetwork(te.NetworkDir)
sharedNetwork, err := tmpnet.ReadNetwork(te.NetworkDir)
te.require.NoError(err)

// The private networks dir is under the shared network dir to ensure it
// will be included in the artifact uploaded in CI.
privateNetworksDir := filepath.Join(sharedNetwork.Dir, PrivateNetworksDirName)
te.require.NoError(os.MkdirAll(privateNetworksDir, perms.ReadWriteExecute))

return StartLocalNetwork(sharedNetwork.ExecPath, privateNetworksDir)
return StartNetwork(sharedNetwork.ExecPath, privateNetworksDir)
}
10 changes: 5 additions & 5 deletions tests/fixture/e2e/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"os"

"github.com/ava-labs/avalanchego/tests/fixture/tmpnet/local"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
)

type FlagVars struct {
Expand All @@ -24,7 +24,7 @@ func (v *FlagVars) NetworkDir() string {
if len(v.networkDir) > 0 {
return v.networkDir
}
return os.Getenv(local.NetworkDirEnvName)
return os.Getenv(tmpnet.NetworkDirEnvName)
}

func (v *FlagVars) AvalancheGoExecPath() string {
Expand All @@ -40,14 +40,14 @@ func RegisterFlags() *FlagVars {
flag.StringVar(
&vars.avalancheGoExecPath,
"avalanchego-path",
os.Getenv(local.AvalancheGoPathEnvName),
fmt.Sprintf("avalanchego executable path (required if not using an existing network). Also possible to configure via the %s env variable.", local.AvalancheGoPathEnvName),
os.Getenv(tmpnet.AvalancheGoPathEnvName),
fmt.Sprintf("avalanchego executable path (required if not using an existing network). Also possible to configure via the %s env variable.", tmpnet.AvalancheGoPathEnvName),
)
flag.StringVar(
&vars.networkDir,
"network-dir",
"",
fmt.Sprintf("[optional] the dir containing the configuration of an existing network to target for testing. Will only be used if --use-existing-network is specified. Also possible to configure via the %s env variable.", local.NetworkDirEnvName),
fmt.Sprintf("[optional] the dir containing the configuration of an existing network to target for testing. Will only be used if --use-existing-network is specified. Also possible to configure via the %s env variable.", tmpnet.NetworkDirEnvName),
)
flag.BoolVar(
&vars.useExistingNetwork,
Expand Down
25 changes: 12 additions & 13 deletions tests/fixture/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"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/tests/fixture/tmpnet/local"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
Expand Down Expand Up @@ -63,7 +62,7 @@ const (

// 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)
tests.Outf("{{blue}} initializing a new wallet for node %s with URI: %s {{/}}\n", nodeURI.ID, nodeURI.URI)
baseWallet, err := primary.MakeWallet(DefaultContext(), &primary.WalletConfig{
URI: nodeURI.URI,
AVAXKeychain: keychain,
Expand All @@ -82,7 +81,7 @@ func NewWallet(keychain *secp256k1fx.Keychain, nodeURI tmpnet.NodeURI) primary.W

// 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)
tests.Outf("{{blue}} initializing a new eth client for node %s with URI: %s {{/}}\n", nodeURI.ID, nodeURI.URI)
nodeAddress := strings.Split(nodeURI.URI, "//")[1]
uri := fmt.Sprintf("ws://%s/ext/bc/C/ws", nodeAddress)
client, err := ethclient.Dial(uri)
Expand Down Expand Up @@ -128,7 +127,7 @@ func Eventually(condition func() bool, waitFor time.Duration, tick time.Duration
// Add an ephemeral node that is only intended to be used by a single test. Its ID and
// URI are not intended to be returned from the Network instance to minimize
// accessibility from other tests.
func AddEphemeralNode(network tmpnet.Network, flags tmpnet.FlagsMap) tmpnet.Node {
func AddEphemeralNode(network *tmpnet.Network, flags tmpnet.FlagsMap) *tmpnet.Node {
require := require.New(ginkgo.GinkgoT())

node, err := network.AddEphemeralNode(ginkgo.GinkgoWriter, flags)
Expand All @@ -137,15 +136,15 @@ func AddEphemeralNode(network tmpnet.Network, flags tmpnet.FlagsMap) tmpnet.Node
// Ensure node is stopped on teardown. It's configuration is not removed to enable
// collection in CI to aid in troubleshooting failures.
ginkgo.DeferCleanup(func() {
tests.Outf("Shutting down ephemeral node %s\n", node.GetID())
tests.Outf("Shutting down ephemeral node %s\n", node.ID)
require.NoError(node.Stop())
})

return node
}

// Wait for the given node to report healthy.
func WaitForHealthy(node tmpnet.Node) {
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()
Expand Down Expand Up @@ -197,7 +196,7 @@ func WithSuggestedGasPrice(ethClient ethclient.Client) common.Option {
}

// Verify that a new node can bootstrap into the network.
func CheckBootstrapIsPossible(network tmpnet.Network) {
func CheckBootstrapIsPossible(network *tmpnet.Network) {
require := require.New(ginkgo.GinkgoT())

if len(os.Getenv(SkipBootstrapChecksEnvName)) > 0 {
Expand All @@ -214,23 +213,23 @@ func CheckBootstrapIsPossible(network tmpnet.Network) {
require.NoError(err)

defer func() {
tests.Outf("Shutting down ephemeral node %s\n", node.GetID())
tests.Outf("Shutting down ephemeral node %s\n", node.ID)
require.NoError(node.Stop())
}()

WaitForHealthy(node)
}

// Start a local test-managed network with the provided avalanchego binary.
func StartLocalNetwork(avalancheGoExecPath string, networkDir string) *local.LocalNetwork {
// Start a temporary network with the provided avalanchego binary.
func StartNetwork(avalancheGoExecPath string, networkDir string) *tmpnet.Network {
require := require.New(ginkgo.GinkgoT())

network, err := local.StartNetwork(
network, err := tmpnet.StartNetwork(
DefaultContext(),
ginkgo.GinkgoWriter,
networkDir,
&local.LocalNetwork{
LocalConfig: local.LocalConfig{
&tmpnet.Network{
NodeRuntimeConfig: tmpnet.NodeRuntimeConfig{
ExecPath: avalancheGoExecPath,
},
},
Expand Down
Loading

0 comments on commit 5bb8d41

Please sign in to comment.