github.com/klaytn/klaytn@v1.12.1/tests/smartcontract_creation_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package tests
    18  
    19  import (
    20  	"math/big"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/klaytn/klaytn/blockchain/types"
    26  	"github.com/klaytn/klaytn/common"
    27  	"github.com/klaytn/klaytn/common/compiler"
    28  	"github.com/klaytn/klaytn/common/profile"
    29  )
    30  
    31  type testData struct {
    32  	name string
    33  	opt  testOption
    34  }
    35  
    36  func makeContractCreationTransactions(bcdata *BCData, accountMap *AccountMap, signer types.Signer,
    37  	numTransactions int, amount *big.Int, data []byte,
    38  ) (types.Transactions, error) {
    39  	numAddrs := len(bcdata.addrs)
    40  	fromAddrs := bcdata.addrs
    41  	fromNonces := make([]uint64, numAddrs)
    42  
    43  	for i, addr := range fromAddrs {
    44  		fromNonces[i] = accountMap.GetNonce(*addr)
    45  	}
    46  
    47  	txs := make(types.Transactions, 0, numTransactions)
    48  
    49  	for i := 0; i < numTransactions; i++ {
    50  		idx := i % numAddrs
    51  
    52  		txamount := new(big.Int).SetInt64(0)
    53  
    54  		var gasLimit uint64 = 1000000
    55  		gasPrice := new(big.Int).SetInt64(0)
    56  
    57  		tx := types.NewContractCreation(fromNonces[idx], txamount, gasLimit, gasPrice, data)
    58  		signedTx, err := types.SignTx(tx, signer, bcdata.privKeys[idx])
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  
    63  		txs = append(txs, signedTx)
    64  
    65  		fromNonces[idx]++
    66  	}
    67  
    68  	return txs, nil
    69  }
    70  
    71  func genOptions(b *testing.B) ([]testData, error) {
    72  	solFiles := []string{"../contracts/reward/contract/KlaytnReward.sol"}
    73  
    74  	opts := make([]testData, len(solFiles))
    75  	for i, filename := range solFiles {
    76  		contracts, err := compiler.CompileSolidity("", filename)
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  
    81  		for name, contract := range contracts {
    82  			testName := filepath.Base(name)
    83  			opts[i] = testData{testName, testOption{
    84  				b.N, 2000, 4, 1, common.FromHex(contract.Code), makeContractCreationTransactions,
    85  			}}
    86  		}
    87  	}
    88  
    89  	return opts, nil
    90  }
    91  
    92  func deploySmartContract(b *testing.B, opt *testOption, prof *profile.Profiler) {
    93  	// Initialize blockchain
    94  	start := time.Now()
    95  	bcdata, err := NewBCData(opt.numMaxAccounts, opt.numValidators)
    96  	if err != nil {
    97  		b.Fatal(err)
    98  	}
    99  	prof.Profile("main_init_blockchain", time.Now().Sub(start))
   100  	defer bcdata.Shutdown()
   101  
   102  	// Initialize address-balance map for verification
   103  	start = time.Now()
   104  	accountMap := NewAccountMap()
   105  	if err := accountMap.Initialize(bcdata); err != nil {
   106  		b.Fatal(err)
   107  	}
   108  	prof.Profile("main_init_accountMap", time.Now().Sub(start))
   109  
   110  	b.ResetTimer()
   111  	for i := 0; i < b.N/txPerBlock; i++ {
   112  		// fmt.Printf("iteration %d tx %d\n", i, opt.numTransactions)
   113  		err := bcdata.GenABlock(accountMap, opt, txPerBlock, prof)
   114  		if err != nil {
   115  			b.Fatal(err)
   116  		}
   117  	}
   118  
   119  	genBlocks := b.N / txPerBlock
   120  	remainTxs := b.N % txPerBlock
   121  	if remainTxs != 0 {
   122  		err := bcdata.GenABlock(accountMap, opt, remainTxs, prof)
   123  		if err != nil {
   124  			b.Fatal(err)
   125  		}
   126  		genBlocks++
   127  	}
   128  
   129  	bcHeight := int(bcdata.bc.CurrentHeader().Number.Uint64())
   130  	if bcHeight != genBlocks {
   131  		b.Fatalf("generated blocks should be %d, but %d.\n", genBlocks, bcHeight)
   132  	}
   133  }
   134  
   135  func BenchmarkSmartContractDeploy(b *testing.B) {
   136  	prof := profile.NewProfiler()
   137  
   138  	benches, err := genOptions(b)
   139  	if err != nil {
   140  		b.Fatal(err)
   141  	}
   142  
   143  	for _, bench := range benches {
   144  		b.Run(bench.name, func(b *testing.B) {
   145  			bench.opt.numTransactions = b.N
   146  			deploySmartContract(b, &bench.opt, prof)
   147  		})
   148  	}
   149  
   150  	if testing.Verbose() {
   151  		prof.PrintProfileInfo()
   152  	}
   153  }