github.com/MetalBlockchain/subnet-evm@v0.6.3/tests/utils/proposervm.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  	"context"
     8  	"crypto/ecdsa"
     9  	"math/big"
    10  
    11  	"github.com/MetalBlockchain/subnet-evm/core/types"
    12  	"github.com/MetalBlockchain/subnet-evm/ethclient"
    13  	"github.com/MetalBlockchain/subnet-evm/params"
    14  	"github.com/ethereum/go-ethereum/common"
    15  	"github.com/ethereum/go-ethereum/crypto"
    16  	"github.com/ethereum/go-ethereum/log"
    17  )
    18  
    19  const numTriggerTxs = 2 // Number of txs needed to activate the proposer VM fork
    20  
    21  // IssueTxsToActivateProposerVMFork issues transactions at the current
    22  // timestamp, which should be after the ProposerVM activation time (aka
    23  // ApricotPhase4). This should generate a PostForkBlock because its parent block
    24  // (genesis) has a timestamp (0) that is greater than or equal to the fork
    25  // activation time of 0. Therefore, subsequent blocks should be built with
    26  // BuildBlockWithContext.
    27  func IssueTxsToActivateProposerVMFork(
    28  	ctx context.Context, chainID *big.Int, fundedKey *ecdsa.PrivateKey,
    29  	client ethclient.Client,
    30  ) error {
    31  	addr := crypto.PubkeyToAddress(fundedKey.PublicKey)
    32  	nonce, err := client.NonceAt(ctx, addr, nil)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	newHeads := make(chan *types.Header, 1)
    38  	sub, err := client.SubscribeNewHead(ctx, newHeads)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	defer sub.Unsubscribe()
    43  
    44  	gasPrice := big.NewInt(params.MinGasPrice)
    45  	txSigner := types.LatestSignerForChainID(chainID)
    46  	for i := 0; i < numTriggerTxs; i++ {
    47  		tx := types.NewTransaction(
    48  			nonce, addr, common.Big1, params.TxGas, gasPrice, nil)
    49  		triggerTx, err := types.SignTx(tx, txSigner, fundedKey)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		if err := client.SendTransaction(ctx, triggerTx); err != nil {
    54  			return err
    55  		}
    56  		<-newHeads // wait for block to be accepted
    57  		nonce++
    58  	}
    59  	log.Info(
    60  		"Built sufficient blocks to activate proposerVM fork",
    61  		"txCount", numTriggerTxs,
    62  	)
    63  	return nil
    64  }