github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/benchmarks/sequencer/common/setup/setup.go (about)

     1  package setup
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/0xPolygon/supernets2-node/config/types"
    10  	"github.com/0xPolygon/supernets2-node/event"
    11  	"github.com/0xPolygon/supernets2-node/event/nileventstorage"
    12  	"github.com/0xPolygon/supernets2-node/log"
    13  	"github.com/0xPolygon/supernets2-node/pool"
    14  	"github.com/0xPolygon/supernets2-node/pool/pgpoolstorage"
    15  	"github.com/0xPolygon/supernets2-node/test/benchmarks/sequencer/common/params"
    16  	"github.com/0xPolygon/supernets2-node/test/operations"
    17  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    18  	"github.com/ethereum/go-ethereum/common"
    19  	"github.com/ethereum/go-ethereum/ethclient"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  const (
    24  	sleepDuration                         = 5 * time.Second
    25  	minAllowedGasPriceIntervalMinutes     = 5
    26  	pollMinAllowedGasPriceIntervalSeconds = 15
    27  )
    28  
    29  // Environment sets up the environment for the benchmark
    30  func Environment(ctx context.Context, b *testing.B) (*operations.Manager, *ethclient.Client, *pool.Pool, *bind.TransactOpts) {
    31  	if testing.Short() {
    32  		b.Skip()
    33  	}
    34  
    35  	err := operations.Teardown()
    36  	require.NoError(b, err)
    37  
    38  	params.OpsCfg.State.MaxCumulativeGasUsed = params.MaxCumulativeGasUsed
    39  	opsman, err := operations.NewManager(ctx, params.OpsCfg)
    40  	require.NoError(b, err)
    41  
    42  	err = Components(opsman)
    43  	require.NoError(b, err)
    44  	time.Sleep(sleepDuration)
    45  
    46  	// Load account with balance on local genesis
    47  	auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL2ChainID)
    48  	require.NoError(b, err)
    49  
    50  	// Load params client
    51  	client, err := ethclient.Dial(operations.DefaultL2NetworkURL)
    52  	require.NoError(b, err)
    53  
    54  	st := opsman.State()
    55  	s, err := pgpoolstorage.NewPostgresPoolStorage(params.PoolDbConfig)
    56  	require.NoError(b, err)
    57  	config := pool.Config{
    58  		DB:                             params.PoolDbConfig,
    59  		MinAllowedGasPriceInterval:     types.NewDuration(minAllowedGasPriceIntervalMinutes * time.Minute),
    60  		PollMinAllowedGasPriceInterval: types.NewDuration(pollMinAllowedGasPriceIntervalSeconds * time.Second),
    61  	}
    62  
    63  	eventStorage, err := nileventstorage.NewNilEventStorage()
    64  	require.NoError(b, err)
    65  	eventLog := event.NewEventLog(event.Config{}, eventStorage)
    66  
    67  	pl := pool.NewPool(config, s, st, common.Address{}, params.ChainID, eventLog)
    68  
    69  	// Print Info before send
    70  	senderBalance, err := client.BalanceAt(ctx, auth.From, nil)
    71  	require.NoError(b, err)
    72  	senderNonce, err := client.PendingNonceAt(ctx, auth.From)
    73  	require.NoError(b, err)
    74  
    75  	// Print Initial Stats
    76  	log.Infof("Receiver Addr: %v", params.To.String())
    77  	log.Infof("Sender Addr: %v", auth.From.String())
    78  	log.Infof("Sender Balance: %v", senderBalance.String())
    79  	log.Infof("Sender Nonce: %v", senderNonce)
    80  
    81  	gasPrice, err := client.SuggestGasPrice(ctx)
    82  	require.NoError(b, err)
    83  
    84  	// PrivateKey is the private key of the sender
    85  	// Auth is the auth of the sender
    86  	auth, err = bind.NewKeyedTransactorWithChainID(params.PrivateKey, new(big.Int).SetUint64(params.ChainID))
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	auth.GasPrice = gasPrice
    91  	auth.Nonce = new(big.Int).SetUint64(senderNonce)
    92  
    93  	return opsman, client, pl, auth
    94  }
    95  
    96  // Components runs the network container, starts synchronizer and JSON-RPC components, and approves matic
    97  func Components(opsman *operations.Manager) error {
    98  	// Run network container
    99  	err := opsman.StartNetwork()
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	// Approve matic
   105  	err = operations.ApproveMatic()
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	err = operations.StartComponent("sync")
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	err = operations.StartComponent("json-rpc")
   116  	if err != nil {
   117  		return err
   118  	}
   119  	time.Sleep(sleepDuration)
   120  
   121  	return nil
   122  }
   123  
   124  // BootstrapSequencer starts the sequencer and waits for it to be ready
   125  func BootstrapSequencer(b *testing.B, opsman *operations.Manager) {
   126  	log.Debug("Starting sequencer ....")
   127  	err := operations.StartComponent("seq")
   128  	require.NoError(b, err)
   129  	log.Debug("Sequencer Started!")
   130  }