github.com/MetalBlockchain/subnet-evm@v0.4.9/tests/utils/subnet.go (about) 1 // Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package utils 5 6 import ( 7 "context" 8 "encoding/json" 9 "fmt" 10 "os" 11 "os/exec" 12 "time" 13 14 "github.com/MetalBlockchain/metalgo/api/info" 15 "github.com/MetalBlockchain/metalgo/genesis" 16 "github.com/MetalBlockchain/metalgo/ids" 17 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 18 wallet "github.com/MetalBlockchain/metalgo/wallet/subnet/primary" 19 "github.com/MetalBlockchain/subnet-evm/core" 20 "github.com/MetalBlockchain/subnet-evm/plugin/evm" 21 "github.com/ethereum/go-ethereum/common" 22 "github.com/ethereum/go-ethereum/log" 23 "github.com/onsi/gomega" 24 ) 25 26 func RunHardhatTests(test string, rpcURI string) { 27 log.Info("Sleeping to wait for test ping", "rpcURI", rpcURI) 28 client, err := NewEvmClient(rpcURI, 225, 2) 29 gomega.Expect(err).Should(gomega.BeNil()) 30 31 bal, err := client.FetchBalance(context.Background(), common.HexToAddress("")) 32 gomega.Expect(err).Should(gomega.BeNil()) 33 gomega.Expect(bal.Cmp(common.Big0)).Should(gomega.Equal(0)) 34 35 err = os.Setenv("RPC_URI", rpcURI) 36 gomega.Expect(err).Should(gomega.BeNil()) 37 cmd := exec.Command("npx", "hardhat", "test", fmt.Sprintf("./test/%s.ts", test), "--network", "local") 38 cmd.Dir = "./contract-examples" 39 log.Info("Running hardhat command", "cmd", cmd.String()) 40 41 out, err := cmd.CombinedOutput() 42 fmt.Printf("\nCombined output:\n\n%s\n", string(out)) 43 if err != nil { 44 fmt.Printf("\nErr: %s\n", err.Error()) 45 } 46 gomega.Expect(err).Should(gomega.BeNil()) 47 } 48 49 func CreateNewSubnet(ctx context.Context, genesisFilePath string) string { 50 kc := secp256k1fx.NewKeychain(genesis.EWOQKey) 51 52 // NewWalletFromURI fetches the available UTXOs owned by [kc] on the network 53 // that [LocalAPIURI] is hosting. 54 wallet, err := wallet.NewWalletFromURI(ctx, DefaultLocalNodeURI, kc) 55 gomega.Expect(err).Should(gomega.BeNil()) 56 57 pWallet := wallet.P() 58 59 owner := &secp256k1fx.OutputOwners{ 60 Threshold: 1, 61 Addrs: []ids.ShortID{ 62 genesis.EWOQKey.PublicKey().Address(), 63 }, 64 } 65 66 wd, err := os.Getwd() 67 gomega.Expect(err).Should(gomega.BeNil()) 68 log.Info("Reading genesis file", "filePath", genesisFilePath, "wd", wd) 69 genesisBytes, err := os.ReadFile(genesisFilePath) 70 gomega.Expect(err).Should(gomega.BeNil()) 71 72 log.Info("Creating new subnet") 73 createSubnetTxID, err := pWallet.IssueCreateSubnetTx(owner) 74 gomega.Expect(err).Should(gomega.BeNil()) 75 76 genesis := &core.Genesis{} 77 err = json.Unmarshal(genesisBytes, genesis) 78 gomega.Expect(err).Should(gomega.BeNil()) 79 80 log.Info("Creating new Subnet-EVM blockchain", "genesis", genesis) 81 createChainTxID, err := pWallet.IssueCreateChainTx( 82 createSubnetTxID, 83 genesisBytes, 84 evm.ID, 85 nil, 86 "testChain", 87 ) 88 gomega.Expect(err).Should(gomega.BeNil()) 89 90 // Confirm the new blockchain is ready by waiting for the readiness endpoint 91 infoClient := info.NewClient(DefaultLocalNodeURI) 92 bootstrapped, err := info.AwaitBootstrapped(ctx, infoClient, createChainTxID.String(), 2*time.Second) 93 gomega.Expect(err).Should(gomega.BeNil()) 94 gomega.Expect(bootstrapped).Should(gomega.BeTrue()) 95 96 // Return the blockchainID of the newly created blockchain 97 return createChainTxID.String() 98 } 99 100 func ExecuteHardHatTestOnNewBlockchain(ctx context.Context, test string) { 101 log.Info("Executing HardHat tests on a new blockchain", "test", test) 102 103 genesisFilePath := fmt.Sprintf("./tests/precompile/genesis/%s.json", test) 104 105 blockchainID := CreateNewSubnet(ctx, genesisFilePath) 106 chainURI := fmt.Sprintf("%s/ext/bc/%s/rpc", DefaultLocalNodeURI, blockchainID) 107 108 log.Info("Created subnet successfully", "ChainURI", chainURI) 109 RunHardhatTests(test, chainURI) 110 }