github.com/MetalBlockchain/subnet-evm@v0.6.3/tests/utils/tmpnet.go (about)

     1  // Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package utils
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  
    10  	"github.com/MetalBlockchain/metalgo/config"
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/tests/fixture/tmpnet"
    13  
    14  	"github.com/MetalBlockchain/subnet-evm/plugin/evm"
    15  )
    16  
    17  func NewTmpnetNodes(count int) []*tmpnet.Node {
    18  	nodes := make([]*tmpnet.Node, count)
    19  	for i := range nodes {
    20  		node := tmpnet.NewNode("")
    21  		node.EnsureKeys()
    22  		nodes[i] = node
    23  	}
    24  	return nodes
    25  }
    26  
    27  func NewTmpnetNetwork(nodes []*tmpnet.Node, flags tmpnet.FlagsMap, subnets ...*tmpnet.Subnet) *tmpnet.Network {
    28  	defaultFlags := tmpnet.FlagsMap{}
    29  	defaultFlags.SetDefaults(flags)
    30  	defaultFlags.SetDefaults(tmpnet.FlagsMap{
    31  		// Remove when vendored tmpnet default is `off`. tmpnet nodes are run headless so stdout logging is unnecessary.
    32  		config.LogDisplayLevelKey:            "off",
    33  		config.ProposerVMUseCurrentHeightKey: true,
    34  	})
    35  	return &tmpnet.Network{
    36  		DefaultFlags: defaultFlags,
    37  		Nodes:        nodes,
    38  		Subnets:      subnets,
    39  	}
    40  }
    41  
    42  // Create the configuration that will enable creation and access to a
    43  // subnet created on a temporary network.
    44  func NewTmpnetSubnet(name string, genesisPath string, chainConfig tmpnet.FlagsMap, nodes ...*tmpnet.Node) *tmpnet.Subnet {
    45  	if len(nodes) == 0 {
    46  		panic("a subnet must be validated by at least one node")
    47  	}
    48  
    49  	validatorIDs := make([]ids.NodeID, len(nodes))
    50  	for i, node := range nodes {
    51  		validatorIDs[i] = node.NodeID
    52  	}
    53  
    54  	genesisBytes, err := os.ReadFile(genesisPath)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  
    59  	chainConfigBytes, err := json.Marshal(chainConfig)
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  
    64  	return &tmpnet.Subnet{
    65  		Name: name,
    66  		Chains: []*tmpnet.Chain{
    67  			{
    68  				VMID:         evm.ID,
    69  				Genesis:      genesisBytes,
    70  				Config:       string(chainConfigBytes),
    71  				PreFundedKey: tmpnet.HardhatKey,
    72  			},
    73  		},
    74  		ValidatorIDs: validatorIDs,
    75  	}
    76  }