github.com/m3shine/gochain@v2.2.26+incompatible/core/state_processor_test.go (about)

     1  package core
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/gochain-io/gochain/common"
    11  	"github.com/gochain-io/gochain/core/state"
    12  	"github.com/gochain-io/gochain/core/types"
    13  	"github.com/gochain-io/gochain/core/vm"
    14  	"github.com/gochain-io/gochain/crypto"
    15  	"github.com/gochain-io/gochain/params"
    16  )
    17  
    18  func BenchmarkStateProcessor_Process(b *testing.B) {
    19  	for _, cnt := range []int{1, 10, 100, 1000, 10000} {
    20  		b.Run(fmt.Sprintf("% 5d", cnt), benchmarkStateProcessor_Process(cnt))
    21  	}
    22  }
    23  
    24  func benchmarkStateProcessor_Process(cnt int) func(b *testing.B) {
    25  	ctx := context.Background()
    26  	key, _ := crypto.GenerateKey()
    27  	address := crypto.PubkeyToAddress(key.PublicKey)
    28  	funds := big.NewInt(1000000000)
    29  
    30  	genesis := &Genesis{
    31  		Config:     params.TestChainConfig,
    32  		Difficulty: big.NewInt(1),
    33  		Alloc:      GenesisAlloc{address: {Balance: funds}},
    34  	}
    35  	txs := make([]*types.Transaction, cnt)
    36  	signer := types.NewEIP155Signer(genesis.Config.ChainId)
    37  	for i := range txs {
    38  		tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(100), 100000, big.NewInt(1), nil)
    39  		tx, _ = types.SignTx(tx, signer, key)
    40  		txs[i] = tx
    41  	}
    42  	bc, err := newTestBlockChainWithGenesis(false, true, genesis)
    43  	block := types.NewBlock(&types.Header{
    44  		GasLimit: bc.GasLimit(),
    45  	}, txs, nil, nil)
    46  
    47  	return func(b *testing.B) {
    48  		// Irregular handling to keep setup separate, but only fail the sub-benchmark.
    49  		if err != nil {
    50  			b.Fatal(err)
    51  		}
    52  		defer bc.Stop()
    53  		var cfg vm.Config
    54  		b.ResetTimer()
    55  		for i := 0; i < b.N; i++ {
    56  			b.StopTimer()
    57  			statedb, err := state.New(bc.CurrentBlock().Root(), bc.stateCache)
    58  			if err != nil {
    59  				b.Fatal(err)
    60  			}
    61  			b.StartTimer()
    62  
    63  			_, _, _, err = bc.Processor().Process(ctx, block, statedb, cfg)
    64  			if err != nil {
    65  				b.Fatal(err)
    66  			}
    67  		}
    68  	}
    69  }
    70  
    71  func TestStateProcessor(t *testing.T) {
    72  	numTxs := 10000
    73  
    74  	ctx := context.Background()
    75  	start := time.Now()
    76  	key, _ := crypto.GenerateKey()
    77  	address := crypto.PubkeyToAddress(key.PublicKey)
    78  	funds := big.NewInt(1000000000)
    79  
    80  	genesis := &Genesis{
    81  		Config:     params.TestChainConfig,
    82  		Difficulty: big.NewInt(1),
    83  		Alloc:      GenesisAlloc{address: {Balance: funds}},
    84  	}
    85  	signer := types.NewEIP155Signer(genesis.Config.ChainId)
    86  
    87  	bc, err := newTestBlockChainWithGenesis(false, true, genesis)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	t.Logf("newTestBlockchain duration: %s", time.Since(start))
    92  	defer bc.Stop()
    93  	cfg := vm.Config{}
    94  	// statedb, err := state.New(bc.CurrentBlock().Root(), bc.stateCache)
    95  	// 	if err != nil {
    96  	// 		t.Fatal(err)
    97  	// 	}
    98  
    99  	txs := make([]*types.Transaction, numTxs)
   100  	for i := 0; i < numTxs; i++ {
   101  		tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(100), 100000, big.NewInt(1), nil)
   102  		tx, _ = types.SignTx(tx, signer, key)
   103  		// txs = append(txs, types.NewTransaction(uint64(i), common.Address{}, big.NewInt(1), uint64(21000), big.NewInt(21000), nil))
   104  		txs[i] = tx
   105  	}
   106  	block := types.NewBlock(&types.Header{
   107  		GasLimit: bc.GasLimit(),
   108  	}, txs, nil, nil)
   109  	statedb, err := state.New(bc.CurrentBlock().Root(), bc.stateCache)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	start = time.Now()
   114  	_, _, _, err = bc.Processor().Process(ctx, block, statedb, cfg)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	t.Logf("process() duration: %s", time.Since(start))
   119  }