github.com/klaytn/klaytn@v1.10.2/tests/tx_gas_calculation_test.go (about)

     1  // Copyright 2019 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  	"crypto/ecdsa"
    21  	"math/big"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/klaytn/klaytn/accounts/abi"
    27  	"github.com/klaytn/klaytn/blockchain/types"
    28  	"github.com/klaytn/klaytn/blockchain/types/accountkey"
    29  	"github.com/klaytn/klaytn/common"
    30  	"github.com/klaytn/klaytn/common/profile"
    31  	"github.com/klaytn/klaytn/crypto"
    32  	"github.com/klaytn/klaytn/log"
    33  	"github.com/klaytn/klaytn/params"
    34  	"github.com/stretchr/testify/assert"
    35  )
    36  
    37  var code = "0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029"
    38  
    39  type TestAccount interface {
    40  	GetAddr() common.Address
    41  	GetTxKeys() []*ecdsa.PrivateKey
    42  	GetUpdateKeys() []*ecdsa.PrivateKey
    43  	GetFeeKeys() []*ecdsa.PrivateKey
    44  	GetNonce() uint64
    45  	GetAccKey() accountkey.AccountKey
    46  	GetValidationGas(r accountkey.RoleType) uint64
    47  	AddNonce()
    48  	SetNonce(uint64)
    49  	SetAddr(common.Address)
    50  }
    51  
    52  type genTransaction func(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64)
    53  
    54  func TestGasCalculation(t *testing.T) {
    55  	testFunctions := []struct {
    56  		Name  string
    57  		genTx genTransaction
    58  	}{
    59  		{"LegacyTransaction", genLegacyTransaction},
    60  		{"AccessListTransaction", genAccessListTransaction},
    61  		{"DynamicFeeTransaction", genDynamicFeeTransaction},
    62  
    63  		{"ValueTransfer", genValueTransfer},
    64  		{"ValueTransferWithMemo", genValueTransferWithMemo},
    65  		{"AccountUpdate", genAccountUpdate},
    66  		{"SmartContractDeploy", genSmartContractDeploy},
    67  		{"SmartContractExecution", genSmartContractExecution},
    68  		{"Cancel", genCancel},
    69  		{"ChainDataAnchoring", genChainDataAnchoring},
    70  
    71  		{"FeeDelegatedValueTransfer", genFeeDelegatedValueTransfer},
    72  		{"FeeDelegatedValueTransferWithMemo", genFeeDelegatedValueTransferWithMemo},
    73  		{"FeeDelegatedAccountUpdate", genFeeDelegatedAccountUpdate},
    74  		{"FeeDelegatedSmartContractDeploy", genFeeDelegatedSmartContractDeploy},
    75  		{"FeeDelegatedSmartContractExecution", genFeeDelegatedSmartContractExecution},
    76  		{"FeeDelegatedCancel", genFeeDelegatedCancel},
    77  		{"FeeDelegatedChainDataAnchoring", genFeeDelegatedChainDataAnchoring},
    78  
    79  		{"FeeDelegatedWithRatioValueTransfer", genFeeDelegatedWithRatioValueTransfer},
    80  		{"FeeDelegatedWithRatioValueTransferWithMemo", genFeeDelegatedWithRatioValueTransferWithMemo},
    81  		{"FeeDelegatedWithRatioAccountUpdate", genFeeDelegatedWithRatioAccountUpdate},
    82  		{"FeeDelegatedWithRatioSmartContractDeploy", genFeeDelegatedWithRatioSmartContractDeploy},
    83  		{"FeeDelegatedWithRatioSmartContractExecution", genFeeDelegatedWithRatioSmartContractExecution},
    84  		{"FeeDelegatedWithRatioCancel", genFeeDelegatedWithRatioCancel},
    85  		{"FeeDelegatedWithRatioChainDataAnchoring", genFeeDelegatedWithRatioChainDataAnchoring},
    86  	}
    87  
    88  	accountTypes := []struct {
    89  		Type    string
    90  		account TestAccount
    91  	}{
    92  		{"KlaytnLegacy", genKlaytnLegacyAccount(t)},
    93  		{"Public", genPublicAccount(t)},
    94  		{"MultiSig", genMultiSigAccount(t)},
    95  		{"RoleBasedWithPublic", genRoleBasedWithPublicAccount(t)},
    96  		{"RoleBasedWithMultiSig", genRoleBasedWithMultiSigAccount(t)},
    97  	}
    98  
    99  	log.EnableLogForTest(log.LvlCrit, log.LvlTrace)
   100  	prof := profile.NewProfiler()
   101  
   102  	// Initialize blockchain
   103  	start := time.Now()
   104  	bcdata, err := NewBCData(6, 4)
   105  	assert.Equal(t, nil, err)
   106  	bcdata.bc.Config().IstanbulCompatibleBlock = big.NewInt(0)
   107  	bcdata.bc.Config().LondonCompatibleBlock = big.NewInt(0)
   108  	bcdata.bc.Config().EthTxTypeCompatibleBlock = big.NewInt(0)
   109  	prof.Profile("main_init_blockchain", time.Now().Sub(start))
   110  
   111  	defer bcdata.Shutdown()
   112  
   113  	// Initialize address-balance map for verification
   114  	start = time.Now()
   115  	accountMap := NewAccountMap()
   116  	if err := accountMap.Initialize(bcdata); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	prof.Profile("main_init_accountMap", time.Now().Sub(start))
   120  
   121  	// reservoir account
   122  	var reservoir TestAccount
   123  	reservoir = &TestAccountType{
   124  		Addr:  *bcdata.addrs[0],
   125  		Keys:  []*ecdsa.PrivateKey{bcdata.privKeys[0]},
   126  		Nonce: uint64(0),
   127  	}
   128  
   129  	signer := types.LatestSignerForChainID(bcdata.bc.Config().ChainID)
   130  	gasPrice := new(big.Int).SetUint64(bcdata.bc.Config().UnitPrice)
   131  
   132  	// Preparing step. Send KLAY to a KlaytnAcount.
   133  	{
   134  		var txs types.Transactions
   135  
   136  		amount := new(big.Int).Mul(big.NewInt(3000), new(big.Int).SetUint64(params.KLAY))
   137  		tx := types.NewTransaction(reservoir.GetNonce(),
   138  			accountTypes[0].account.GetAddr(), amount, gasLimit, gasPrice, []byte{})
   139  
   140  		err := tx.SignWithKeys(signer, reservoir.GetTxKeys())
   141  		assert.Equal(t, nil, err)
   142  		txs = append(txs, tx)
   143  
   144  		if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   145  			t.Fatal(err)
   146  		}
   147  		reservoir.AddNonce()
   148  	}
   149  
   150  	// Preparing step. Send KLAY to KlaytnAcounts.
   151  	for i := 1; i < len(accountTypes); i++ {
   152  		// create an account which account key will be replaced to one of account key types.
   153  		anon, err := createAnonymousAccount(getRandomPrivateKeyString(t))
   154  		assert.Equal(t, nil, err)
   155  
   156  		{
   157  			var txs types.Transactions
   158  
   159  			amount := new(big.Int).Mul(big.NewInt(3000), new(big.Int).SetUint64(params.KLAY))
   160  			tx := types.NewTransaction(reservoir.GetNonce(),
   161  				anon.Addr, amount, gasLimit, gasPrice, []byte{})
   162  
   163  			err := tx.SignWithKeys(signer, reservoir.GetTxKeys())
   164  			assert.Equal(t, nil, err)
   165  			txs = append(txs, tx)
   166  
   167  			if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   168  				t.Fatal(err)
   169  			}
   170  			reservoir.AddNonce()
   171  		}
   172  
   173  		// update the account's key
   174  		{
   175  			var txs types.Transactions
   176  
   177  			values := map[types.TxValueKeyType]interface{}{
   178  				types.TxValueKeyNonce:      anon.Nonce,
   179  				types.TxValueKeyFrom:       anon.Addr,
   180  				types.TxValueKeyGasLimit:   gasLimit,
   181  				types.TxValueKeyGasPrice:   gasPrice,
   182  				types.TxValueKeyAccountKey: accountTypes[i].account.GetAccKey(),
   183  			}
   184  			tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values)
   185  			assert.Equal(t, nil, err)
   186  
   187  			err = tx.SignWithKeys(signer, anon.Keys)
   188  			assert.Equal(t, nil, err)
   189  
   190  			txs = append(txs, tx)
   191  
   192  			if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   193  				t.Fatal(err)
   194  			}
   195  			anon.AddNonce()
   196  		}
   197  
   198  		accountTypes[i].account.SetAddr(anon.Addr)
   199  		accountTypes[i].account.SetNonce(anon.Nonce)
   200  	}
   201  
   202  	// For smart contract
   203  	contract, err := createAnonymousAccount("a5c9a50938a089618167c9d67dbebc0deaffc3c76ddc6b40c2777ae59438e989")
   204  	contract.Addr = common.Address{}
   205  
   206  	{
   207  		var txs types.Transactions
   208  
   209  		amount := new(big.Int).SetUint64(0)
   210  		values := map[types.TxValueKeyType]interface{}{
   211  			types.TxValueKeyNonce:         reservoir.GetNonce(),
   212  			types.TxValueKeyFrom:          reservoir.GetAddr(),
   213  			types.TxValueKeyTo:            (*common.Address)(nil),
   214  			types.TxValueKeyAmount:        amount,
   215  			types.TxValueKeyGasLimit:      gasLimit,
   216  			types.TxValueKeyGasPrice:      gasPrice,
   217  			types.TxValueKeyHumanReadable: false,
   218  			types.TxValueKeyData:          common.FromHex(code),
   219  			types.TxValueKeyCodeFormat:    params.CodeFormatEVM,
   220  		}
   221  		tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values)
   222  		assert.Equal(t, nil, err)
   223  
   224  		err = tx.SignWithKeys(signer, reservoir.GetTxKeys())
   225  		assert.Equal(t, nil, err)
   226  
   227  		txs = append(txs, tx)
   228  
   229  		if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   230  			t.Fatal(err)
   231  		}
   232  
   233  		contract.Addr = crypto.CreateAddress(reservoir.GetAddr(), reservoir.GetNonce())
   234  
   235  		reservoir.AddNonce()
   236  	}
   237  
   238  	for _, f := range testFunctions {
   239  		for _, sender := range accountTypes {
   240  			toAccount := reservoir
   241  			senderRole := accountkey.RoleTransaction
   242  
   243  			// LegacyTransaction can be used only by the KlaytnAccount with AccountKeyLegacy.
   244  			if sender.Type != "KlaytnLegacy" && (strings.Contains(f.Name, "Legacy") || strings.Contains(f.Name, "Access") || strings.Contains(f.Name, "Dynamic")) {
   245  				continue
   246  			}
   247  
   248  			if strings.Contains(f.Name, "AccountUpdate") {
   249  				senderRole = accountkey.RoleAccountUpdate
   250  			}
   251  
   252  			// Set contract's address with SmartContractExecution
   253  			if strings.Contains(f.Name, "SmartContractExecution") {
   254  				toAccount = contract
   255  			}
   256  
   257  			if !strings.Contains(f.Name, "FeeDelegated") {
   258  				// For NonFeeDelegated Transactions
   259  				Name := f.Name + "/" + sender.Type + "Sender"
   260  				t.Run(Name, func(t *testing.T) {
   261  					tx, intrinsic := f.genTx(t, signer, sender.account, toAccount, nil, gasPrice)
   262  					acocuntValidationGas := sender.account.GetValidationGas(senderRole)
   263  					testGasValidation(t, bcdata, tx, intrinsic+acocuntValidationGas)
   264  				})
   265  			} else {
   266  				// For FeeDelegated(WithRatio) Transactions
   267  				for _, payer := range accountTypes {
   268  					Name := f.Name + "/" + sender.Type + "Sender/" + payer.Type + "Payer"
   269  					t.Run(Name, func(t *testing.T) {
   270  						tx, intrinsic := f.genTx(t, signer, sender.account, toAccount, payer.account, gasPrice)
   271  						acocuntsValidationGas := sender.account.GetValidationGas(senderRole) + payer.account.GetValidationGas(accountkey.RoleFeePayer)
   272  						testGasValidation(t, bcdata, tx, intrinsic+acocuntsValidationGas)
   273  					})
   274  				}
   275  			}
   276  
   277  		}
   278  	}
   279  }
   280  
   281  func testGasValidation(t *testing.T, bcdata *BCData, tx *types.Transaction, validationGas uint64) {
   282  	receipt, err := applyTransaction(t, bcdata, tx)
   283  	assert.Equal(t, nil, err)
   284  
   285  	assert.Equal(t, receipt.Status, types.ReceiptStatusSuccessful)
   286  
   287  	assert.Equal(t, validationGas, receipt.GasUsed)
   288  }
   289  
   290  func genLegacyTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   291  	intrinsic := getIntrinsicGas(types.TxTypeLegacyTransaction)
   292  	amount := big.NewInt(100000)
   293  	tx := types.NewTransaction(from.GetNonce(), to.GetAddr(), amount, gasLimit, gasPrice, []byte{})
   294  
   295  	err := tx.SignWithKeys(signer, from.GetTxKeys())
   296  	assert.Equal(t, nil, err)
   297  
   298  	return tx, intrinsic
   299  }
   300  
   301  func genAccessListTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   302  	values, intrinsic := genMapForAccessListTransaction(from, to, gasPrice, types.TxTypeEthereumAccessList)
   303  	tx, err := types.NewTransactionWithMap(types.TxTypeEthereumAccessList, values)
   304  	assert.Equal(t, nil, err)
   305  
   306  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   307  	assert.Equal(t, nil, err)
   308  
   309  	return tx, intrinsic
   310  }
   311  
   312  func genDynamicFeeTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   313  	values, intrinsic := genMapForDynamicFeeTransaction(from, to, gasPrice, types.TxTypeEthereumDynamicFee)
   314  	tx, err := types.NewTransactionWithMap(types.TxTypeEthereumDynamicFee, values)
   315  	assert.Equal(t, nil, err)
   316  
   317  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   318  	assert.Equal(t, nil, err)
   319  
   320  	return tx, intrinsic
   321  }
   322  
   323  func genValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   324  	values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeValueTransfer)
   325  	tx, err := types.NewTransactionWithMap(types.TxTypeValueTransfer, values)
   326  	assert.Equal(t, nil, err)
   327  
   328  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   329  	assert.Equal(t, nil, err)
   330  
   331  	return tx, intrinsic
   332  }
   333  
   334  func genFeeDelegatedValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   335  	values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransfer)
   336  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   337  
   338  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransfer, values)
   339  	assert.Equal(t, nil, err)
   340  
   341  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   342  	assert.Equal(t, nil, err)
   343  
   344  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   345  	assert.Equal(t, nil, err)
   346  
   347  	return tx, intrinsic
   348  }
   349  
   350  func genFeeDelegatedWithRatioValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   351  	values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferWithRatio)
   352  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   353  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   354  
   355  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferWithRatio, values)
   356  	assert.Equal(t, nil, err)
   357  
   358  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   359  	assert.Equal(t, nil, err)
   360  
   361  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   362  	assert.Equal(t, nil, err)
   363  
   364  	return tx, intrinsic
   365  }
   366  
   367  func genValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   368  	values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeValueTransferMemo)
   369  
   370  	tx, err := types.NewTransactionWithMap(types.TxTypeValueTransferMemo, values)
   371  	assert.Equal(t, nil, err)
   372  
   373  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   374  	assert.Equal(t, nil, err)
   375  
   376  	return tx, gasPayloadWithGas
   377  }
   378  
   379  func genFeeDelegatedValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   380  	values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferMemo)
   381  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   382  
   383  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemo, values)
   384  	assert.Equal(t, nil, err)
   385  
   386  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   387  	assert.Equal(t, nil, err)
   388  
   389  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   390  	assert.Equal(t, nil, err)
   391  
   392  	return tx, gasPayloadWithGas
   393  }
   394  
   395  func genFeeDelegatedWithRatioValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   396  	values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferMemoWithRatio)
   397  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   398  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   399  
   400  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemoWithRatio, values)
   401  	assert.Equal(t, nil, err)
   402  
   403  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   404  	assert.Equal(t, nil, err)
   405  
   406  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   407  	assert.Equal(t, nil, err)
   408  
   409  	return tx, gasPayloadWithGas
   410  }
   411  
   412  func genAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   413  	newAccount, gasKey, _ := genNewAccountWithGas(t, from)
   414  
   415  	values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeAccountUpdate)
   416  
   417  	tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values)
   418  	assert.Equal(t, nil, err)
   419  
   420  	err = tx.SignWithKeys(signer, from.GetUpdateKeys())
   421  	assert.Equal(t, nil, err)
   422  
   423  	return tx, intrinsic + gasKey
   424  }
   425  
   426  func genFeeDelegatedAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   427  	newAccount, gasKey, _ := genNewAccountWithGas(t, from)
   428  
   429  	values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeFeeDelegatedAccountUpdate)
   430  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   431  
   432  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdate, values)
   433  	assert.Equal(t, nil, err)
   434  
   435  	err = tx.SignWithKeys(signer, from.GetUpdateKeys())
   436  	assert.Equal(t, nil, err)
   437  
   438  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   439  	assert.Equal(t, nil, err)
   440  
   441  	return tx, intrinsic + gasKey
   442  }
   443  
   444  func genFeeDelegatedWithRatioAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   445  	newAccount, gasKey, _ := genNewAccountWithGas(t, from)
   446  
   447  	values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeFeeDelegatedAccountUpdateWithRatio)
   448  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   449  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   450  
   451  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdateWithRatio, values)
   452  	assert.Equal(t, nil, err)
   453  
   454  	err = tx.SignWithKeys(signer, from.GetUpdateKeys())
   455  	assert.Equal(t, nil, err)
   456  
   457  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   458  	assert.Equal(t, nil, err)
   459  
   460  	return tx, intrinsic + gasKey
   461  }
   462  
   463  func genSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   464  	values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeSmartContractDeploy)
   465  	if values == nil {
   466  		t.Fatalf("failed to genMapForDeploy")
   467  	}
   468  
   469  	tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values)
   470  	assert.Equal(t, nil, err)
   471  
   472  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   473  	assert.Equal(t, nil, err)
   474  
   475  	return tx, intrinsicGas
   476  }
   477  
   478  func genFeeDelegatedSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   479  	values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractDeploy)
   480  	if values == nil {
   481  		t.Fatalf("failed to genMapForDeploy")
   482  	}
   483  
   484  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   485  
   486  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeploy, values)
   487  	assert.Equal(t, nil, err)
   488  
   489  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   490  	assert.Equal(t, nil, err)
   491  
   492  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   493  	assert.Equal(t, nil, err)
   494  
   495  	return tx, intrinsicGas
   496  }
   497  
   498  func genFeeDelegatedWithRatioSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   499  	values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractDeployWithRatio)
   500  	if values == nil {
   501  		t.Fatalf("failed to genMapForDeploy")
   502  	}
   503  
   504  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   505  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   506  
   507  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeployWithRatio, values)
   508  	assert.Equal(t, nil, err)
   509  
   510  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   511  	assert.Equal(t, nil, err)
   512  
   513  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   514  	assert.Equal(t, nil, err)
   515  
   516  	return tx, intrinsicGas
   517  }
   518  
   519  func genSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   520  	values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeSmartContractExecution)
   521  	if values == nil {
   522  		t.Fatalf("failed to genMapForExecution")
   523  	}
   524  
   525  	tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractExecution, values)
   526  
   527  	assert.Equal(t, nil, err)
   528  
   529  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   530  	assert.Equal(t, nil, err)
   531  
   532  	return tx, intrinsicGas
   533  }
   534  
   535  func genFeeDelegatedSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   536  	values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractExecution)
   537  	if values == nil {
   538  		t.Fatalf("failed to genMapForExecution")
   539  	}
   540  
   541  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   542  
   543  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecution, values)
   544  
   545  	assert.Equal(t, nil, err)
   546  
   547  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   548  	assert.Equal(t, nil, err)
   549  
   550  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   551  	assert.Equal(t, nil, err)
   552  
   553  	return tx, intrinsicGas
   554  }
   555  
   556  func genFeeDelegatedWithRatioSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   557  	values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractExecutionWithRatio)
   558  	if values == nil {
   559  		t.Fatalf("failed to genMapForExecution")
   560  	}
   561  
   562  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   563  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   564  
   565  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecutionWithRatio, values)
   566  
   567  	assert.Equal(t, nil, err)
   568  
   569  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   570  	assert.Equal(t, nil, err)
   571  
   572  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   573  	assert.Equal(t, nil, err)
   574  
   575  	return tx, intrinsicGas
   576  }
   577  
   578  func genCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   579  	values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeCancel)
   580  
   581  	tx, err := types.NewTransactionWithMap(types.TxTypeCancel, values)
   582  	assert.Equal(t, nil, err)
   583  
   584  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   585  	assert.Equal(t, nil, err)
   586  
   587  	return tx, intrinsic
   588  }
   589  
   590  func genFeeDelegatedCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   591  	values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeFeeDelegatedCancel)
   592  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   593  
   594  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancel, values)
   595  	assert.Equal(t, nil, err)
   596  
   597  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   598  	assert.Equal(t, nil, err)
   599  
   600  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   601  	assert.Equal(t, nil, err)
   602  
   603  	return tx, intrinsic
   604  }
   605  
   606  func genFeeDelegatedWithRatioCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   607  	values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeFeeDelegatedCancelWithRatio)
   608  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   609  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   610  
   611  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancelWithRatio, values)
   612  	assert.Equal(t, nil, err)
   613  
   614  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   615  	assert.Equal(t, nil, err)
   616  
   617  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   618  	assert.Equal(t, nil, err)
   619  
   620  	return tx, intrinsic
   621  }
   622  
   623  func genChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   624  	values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeChainDataAnchoring)
   625  
   626  	tx, err := types.NewTransactionWithMap(types.TxTypeChainDataAnchoring, values)
   627  	assert.Equal(t, nil, err)
   628  
   629  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   630  	assert.Equal(t, nil, err)
   631  
   632  	return tx, intrinsic
   633  }
   634  
   635  func genFeeDelegatedChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   636  	values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeFeeDelegatedChainDataAnchoring)
   637  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   638  
   639  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoring, values)
   640  	assert.Equal(t, nil, err)
   641  
   642  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   643  	assert.Equal(t, nil, err)
   644  
   645  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   646  	assert.Equal(t, nil, err)
   647  
   648  	return tx, intrinsic
   649  }
   650  
   651  func genFeeDelegatedWithRatioChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) {
   652  	values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeFeeDelegatedChainDataAnchoringWithRatio)
   653  	values[types.TxValueKeyFeePayer] = payer.GetAddr()
   654  	values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30)
   655  
   656  	tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoringWithRatio, values)
   657  	assert.Equal(t, nil, err)
   658  
   659  	err = tx.SignWithKeys(signer, from.GetTxKeys())
   660  	assert.Equal(t, nil, err)
   661  
   662  	err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys())
   663  	assert.Equal(t, nil, err)
   664  
   665  	return tx, intrinsic
   666  }
   667  
   668  // Generate map functions
   669  func genMapForLegacyTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   670  	intrinsic := getIntrinsicGas(txType)
   671  	amount := big.NewInt(100000)
   672  	data := []byte{0x11, 0x22}
   673  	gasPayload := uint64(len(data)) * params.TxDataGas
   674  
   675  	values := map[types.TxValueKeyType]interface{}{
   676  		types.TxValueKeyNonce:    from.GetNonce(),
   677  		types.TxValueKeyTo:       to.GetAddr(),
   678  		types.TxValueKeyAmount:   amount,
   679  		types.TxValueKeyData:     data,
   680  		types.TxValueKeyGasLimit: gasLimit,
   681  		types.TxValueKeyGasPrice: gasPrice,
   682  	}
   683  	return values, intrinsic + gasPayload
   684  }
   685  
   686  func genMapForAccessListTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   687  	intrinsic := getIntrinsicGas(txType)
   688  	amount := big.NewInt(100000)
   689  	data := []byte{0x11, 0x22}
   690  	gasPayload := uint64(len(data)) * params.TxDataGas
   691  	accessList := types.AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}}
   692  	toAddress := to.GetAddr()
   693  
   694  	gasPayload += uint64(len(accessList)) * params.TxAccessListAddressGas
   695  	gasPayload += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
   696  
   697  	values := map[types.TxValueKeyType]interface{}{
   698  		types.TxValueKeyNonce:      from.GetNonce(),
   699  		types.TxValueKeyTo:         &toAddress,
   700  		types.TxValueKeyAmount:     amount,
   701  		types.TxValueKeyData:       data,
   702  		types.TxValueKeyGasLimit:   gasLimit,
   703  		types.TxValueKeyGasPrice:   gasPrice,
   704  		types.TxValueKeyAccessList: accessList,
   705  		types.TxValueKeyChainID:    big.NewInt(1),
   706  	}
   707  	return values, intrinsic + gasPayload
   708  }
   709  
   710  func genMapForDynamicFeeTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   711  	intrinsic := getIntrinsicGas(txType)
   712  	amount := big.NewInt(100000)
   713  	data := []byte{0x11, 0x22}
   714  	gasPayload := uint64(len(data)) * params.TxDataGas
   715  	accessList := types.AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}}
   716  	toAddress := to.GetAddr()
   717  
   718  	gasPayload += uint64(len(accessList)) * params.TxAccessListAddressGas
   719  	gasPayload += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas
   720  
   721  	values := map[types.TxValueKeyType]interface{}{
   722  		types.TxValueKeyNonce:      from.GetNonce(),
   723  		types.TxValueKeyTo:         &toAddress,
   724  		types.TxValueKeyAmount:     amount,
   725  		types.TxValueKeyData:       data,
   726  		types.TxValueKeyGasLimit:   gasLimit,
   727  		types.TxValueKeyGasFeeCap:  gasPrice,
   728  		types.TxValueKeyGasTipCap:  gasPrice,
   729  		types.TxValueKeyAccessList: accessList,
   730  		types.TxValueKeyChainID:    big.NewInt(1),
   731  	}
   732  	return values, intrinsic + gasPayload
   733  }
   734  
   735  func genMapForValueTransfer(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   736  	intrinsic := getIntrinsicGas(txType)
   737  	amount := big.NewInt(100000)
   738  
   739  	values := map[types.TxValueKeyType]interface{}{
   740  		types.TxValueKeyNonce:    from.GetNonce(),
   741  		types.TxValueKeyFrom:     from.GetAddr(),
   742  		types.TxValueKeyTo:       to.GetAddr(),
   743  		types.TxValueKeyAmount:   amount,
   744  		types.TxValueKeyGasLimit: gasLimit,
   745  		types.TxValueKeyGasPrice: gasPrice,
   746  	}
   747  	return values, intrinsic
   748  }
   749  
   750  func genMapForValueTransferWithMemo(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   751  	intrinsic := getIntrinsicGas(txType)
   752  
   753  	nonZeroData := []byte{1, 2, 3, 4}
   754  	zeroData := []byte{0, 0, 0, 0}
   755  
   756  	data := append(nonZeroData, zeroData...)
   757  
   758  	amount := big.NewInt(100000)
   759  
   760  	values := map[types.TxValueKeyType]interface{}{
   761  		types.TxValueKeyNonce:    from.GetNonce(),
   762  		types.TxValueKeyFrom:     from.GetAddr(),
   763  		types.TxValueKeyTo:       to.GetAddr(),
   764  		types.TxValueKeyAmount:   amount,
   765  		types.TxValueKeyGasLimit: gasLimit,
   766  		types.TxValueKeyGasPrice: gasPrice,
   767  		types.TxValueKeyData:     data,
   768  	}
   769  
   770  	gasPayload := uint64(len(data)) * params.TxDataGas
   771  
   772  	return values, intrinsic + gasPayload
   773  }
   774  
   775  func genMapForCreate(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   776  	intrinsic := getIntrinsicGas(txType)
   777  	amount := big.NewInt(0)
   778  
   779  	values := map[types.TxValueKeyType]interface{}{
   780  		types.TxValueKeyNonce:         from.GetNonce(),
   781  		types.TxValueKeyFrom:          from.GetAddr(),
   782  		types.TxValueKeyTo:            to.GetAddr(),
   783  		types.TxValueKeyAmount:        amount,
   784  		types.TxValueKeyGasLimit:      gasLimit,
   785  		types.TxValueKeyGasPrice:      gasPrice,
   786  		types.TxValueKeyHumanReadable: false,
   787  		types.TxValueKeyAccountKey:    to.GetAccKey(),
   788  	}
   789  	return values, intrinsic + uint64(len(to.GetTxKeys()))*params.TxAccountCreationGasPerKey
   790  }
   791  
   792  func genMapForUpdate(from TestAccount, to TestAccount, gasPrice *big.Int, newKeys accountkey.AccountKey, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   793  	intrinsic := getIntrinsicGas(txType)
   794  
   795  	values := map[types.TxValueKeyType]interface{}{
   796  		types.TxValueKeyNonce:      from.GetNonce(),
   797  		types.TxValueKeyFrom:       from.GetAddr(),
   798  		types.TxValueKeyGasLimit:   gasLimit,
   799  		types.TxValueKeyGasPrice:   gasPrice,
   800  		types.TxValueKeyAccountKey: newKeys,
   801  	}
   802  	return values, intrinsic
   803  }
   804  
   805  func genMapForDeploy(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   806  	amount := new(big.Int).SetUint64(0)
   807  	values := map[types.TxValueKeyType]interface{}{
   808  		types.TxValueKeyNonce:         from.GetNonce(),
   809  		types.TxValueKeyAmount:        amount,
   810  		types.TxValueKeyGasLimit:      gasLimit,
   811  		types.TxValueKeyGasPrice:      gasPrice,
   812  		types.TxValueKeyHumanReadable: false,
   813  		types.TxValueKeyFrom:          from.GetAddr(),
   814  		types.TxValueKeyData:          common.FromHex(code),
   815  		types.TxValueKeyCodeFormat:    params.CodeFormatEVM,
   816  		types.TxValueKeyTo:            (*common.Address)(nil),
   817  	}
   818  
   819  	intrinsicGas := getIntrinsicGas(txType)
   820  	intrinsicGas += uint64(0x175fd)
   821  
   822  	gasPayloadWithGas, err := types.IntrinsicGasPayload(intrinsicGas, common.FromHex(code))
   823  	if err != nil {
   824  		return nil, 0
   825  	}
   826  
   827  	return values, gasPayloadWithGas
   828  }
   829  
   830  func genMapForExecution(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   831  	abiStr := `[{"constant":true,"inputs":[],"name":"totalAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"}],"name":"reward","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"safeWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"}]`
   832  	abii, err := abi.JSON(strings.NewReader(string(abiStr)))
   833  	if err != nil {
   834  		return nil, 0
   835  	}
   836  
   837  	data, err := abii.Pack("reward", to.GetAddr())
   838  	if err != nil {
   839  		return nil, 0
   840  	}
   841  
   842  	amount := new(big.Int).SetUint64(10)
   843  
   844  	values := map[types.TxValueKeyType]interface{}{
   845  		types.TxValueKeyNonce:    from.GetNonce(),
   846  		types.TxValueKeyFrom:     from.GetAddr(),
   847  		types.TxValueKeyTo:       to.GetAddr(),
   848  		types.TxValueKeyAmount:   amount,
   849  		types.TxValueKeyGasLimit: gasLimit,
   850  		types.TxValueKeyGasPrice: gasPrice,
   851  		types.TxValueKeyData:     data,
   852  	}
   853  
   854  	intrinsicGas := getIntrinsicGas(txType)
   855  	intrinsicGas += uint64(0x9ec4)
   856  
   857  	gasPayloadWithGas, err := types.IntrinsicGasPayload(intrinsicGas, data)
   858  	if err != nil {
   859  		return nil, 0
   860  	}
   861  
   862  	return values, gasPayloadWithGas
   863  }
   864  
   865  func genMapForCancel(from TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   866  	intrinsic := getIntrinsicGas(txType)
   867  
   868  	values := map[types.TxValueKeyType]interface{}{
   869  		types.TxValueKeyNonce:    from.GetNonce(),
   870  		types.TxValueKeyFrom:     from.GetAddr(),
   871  		types.TxValueKeyGasLimit: gasLimit,
   872  		types.TxValueKeyGasPrice: gasPrice,
   873  	}
   874  	return values, intrinsic
   875  }
   876  
   877  func genMapForChainDataAnchoring(from TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) {
   878  	intrinsic := getIntrinsicGas(txType)
   879  	data := []byte{0x11, 0x22}
   880  	gasPayload := uint64(len(data)) * params.TxDataGas
   881  
   882  	values := map[types.TxValueKeyType]interface{}{
   883  		types.TxValueKeyNonce:        from.GetNonce(),
   884  		types.TxValueKeyFrom:         from.GetAddr(),
   885  		types.TxValueKeyGasLimit:     gasLimit,
   886  		types.TxValueKeyGasPrice:     gasPrice,
   887  		types.TxValueKeyAnchoredData: data,
   888  	}
   889  	return values, intrinsic + gasPayload
   890  }
   891  
   892  func genKlaytnLegacyAccount(t *testing.T) TestAccount {
   893  	// For KlaytnLegacy
   894  	klaytnLegacy, err := createAnonymousAccount(getRandomPrivateKeyString(t))
   895  	assert.Equal(t, nil, err)
   896  
   897  	return klaytnLegacy
   898  }
   899  
   900  func genPublicAccount(t *testing.T) TestAccount {
   901  	// For AccountKeyPublic
   902  	var newAddress common.Address
   903  	newAddress.SetBytesFromFront([]byte(getRandomPrivateKeyString(t)))
   904  
   905  	publicAccount, err := createDecoupledAccount(getRandomPrivateKeyString(t), newAddress)
   906  	assert.Equal(t, nil, err)
   907  
   908  	return publicAccount
   909  }
   910  
   911  func genMultiSigAccount(t *testing.T) TestAccount {
   912  	// For AccountKeyWeightedMultiSig
   913  	var newAddress common.Address
   914  	newAddress.SetBytesFromFront([]byte(getRandomPrivateKeyString(t)))
   915  
   916  	multisigAccount, err := createMultisigAccount(uint(2),
   917  		[]uint{1, 1, 1},
   918  		[]string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, newAddress)
   919  	assert.Equal(t, nil, err)
   920  
   921  	return multisigAccount
   922  }
   923  
   924  func genRoleBasedWithPublicAccount(t *testing.T) TestAccount {
   925  	// For AccountKeyRoleBased With AccountKeyPublic
   926  	var roleBasedWithPublicAddr common.Address
   927  	roleBasedWithPublicAddr.SetBytesFromFront([]byte(getRandomPrivateKeyString(t)))
   928  
   929  	roleBasedWithPublic, err := createRoleBasedAccountWithAccountKeyPublic(
   930  		[]string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, roleBasedWithPublicAddr)
   931  	assert.Equal(t, nil, err)
   932  
   933  	return roleBasedWithPublic
   934  }
   935  
   936  func genRoleBasedWithMultiSigAccount(t *testing.T) TestAccount {
   937  	// For AccountKeyRoleBased With AccountKeyWeightedMultiSig
   938  	p := genMultiSigParamForRoleBased(t)
   939  
   940  	var roleBasedWithMultiSigAddr common.Address
   941  	roleBasedWithMultiSigAddr.SetBytesFromFront([]byte(getRandomPrivateKeyString(t)))
   942  
   943  	roleBasedWithMultiSig, err := createRoleBasedAccountWithAccountKeyWeightedMultiSig(
   944  		[]TestCreateMultisigAccountParam{p[0], p[1], p[2]}, roleBasedWithMultiSigAddr)
   945  	assert.Equal(t, nil, err)
   946  
   947  	return roleBasedWithMultiSig
   948  }
   949  
   950  // Generate new Account functions for testing AccountUpdate
   951  func genNewAccountWithGas(t *testing.T, testAccount TestAccount) (TestAccount, uint64, bool) {
   952  	var newAccount TestAccount
   953  	gas := uint64(0)
   954  	readableGas := uint64(0)
   955  	readable := false
   956  
   957  	// AccountKeyLegacy
   958  	if testAccount.GetAccKey() == nil || testAccount.GetAccKey().Type() == accountkey.AccountKeyTypeLegacy {
   959  		anon, err := createAnonymousAccount(getRandomPrivateKeyString(t))
   960  		assert.Equal(t, nil, err)
   961  
   962  		return anon, gas + readableGas, readable
   963  	}
   964  
   965  	// newAccount
   966  	newAccount, err := createAnonymousAccount(getRandomPrivateKeyString(t))
   967  	assert.Equal(t, nil, err)
   968  
   969  	switch testAccount.GetAccKey().Type() {
   970  	case accountkey.AccountKeyTypePublic:
   971  		publicAccount, err := createDecoupledAccount(getRandomPrivateKeyString(t), newAccount.GetAddr())
   972  		assert.Equal(t, nil, err)
   973  
   974  		newAccount = publicAccount
   975  		gas += uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey
   976  	case accountkey.AccountKeyTypeWeightedMultiSig:
   977  		multisigAccount, err := createMultisigAccount(uint(2), []uint{1, 1, 1},
   978  			[]string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, newAccount.GetAddr())
   979  		assert.Equal(t, nil, err)
   980  
   981  		newAccount = multisigAccount
   982  		gas += uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey
   983  	case accountkey.AccountKeyTypeRoleBased:
   984  		p := genMultiSigParamForRoleBased(t)
   985  
   986  		newRoleBasedWithMultiSig, err := createRoleBasedAccountWithAccountKeyWeightedMultiSig(
   987  			[]TestCreateMultisigAccountParam{p[0], p[1], p[2]}, newAccount.GetAddr())
   988  		assert.Equal(t, nil, err)
   989  
   990  		newAccount = newRoleBasedWithMultiSig
   991  		gas = uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey
   992  		gas += uint64(len(newAccount.GetUpdateKeys())) * params.TxAccountCreationGasPerKey
   993  		gas += uint64(len(newAccount.GetFeeKeys())) * params.TxAccountCreationGasPerKey
   994  	}
   995  
   996  	return newAccount, gas + readableGas, readable
   997  }
   998  
   999  func getRandomPrivateKeyString(t *testing.T) string {
  1000  	key, err := crypto.GenerateKey()
  1001  	assert.Equal(t, nil, err)
  1002  	keyBytes := crypto.FromECDSA(key)
  1003  
  1004  	return common.Bytes2Hex(keyBytes)
  1005  }
  1006  
  1007  // Return multisig parameters for creating RoleBased with MultiSig
  1008  func genMultiSigParamForRoleBased(t *testing.T) []TestCreateMultisigAccountParam {
  1009  	var params []TestCreateMultisigAccountParam
  1010  	param1 := TestCreateMultisigAccountParam{
  1011  		Threshold: uint(2),
  1012  		Weights:   []uint{1, 1, 1},
  1013  		PrvKeys:   []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)},
  1014  	}
  1015  	params = append(params, param1)
  1016  
  1017  	param2 := TestCreateMultisigAccountParam{
  1018  		Threshold: uint(2),
  1019  		Weights:   []uint{1, 1, 1},
  1020  		PrvKeys:   []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)},
  1021  	}
  1022  	params = append(params, param2)
  1023  
  1024  	param3 := TestCreateMultisigAccountParam{
  1025  		Threshold: uint(2),
  1026  		Weights:   []uint{1, 1, 1},
  1027  		PrvKeys:   []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)},
  1028  	}
  1029  	params = append(params, param3)
  1030  
  1031  	return params
  1032  }
  1033  
  1034  func getIntrinsicGas(txType types.TxType) uint64 {
  1035  	var intrinsic uint64
  1036  
  1037  	switch txType {
  1038  	case types.TxTypeLegacyTransaction:
  1039  		intrinsic = params.TxGas
  1040  	case types.TxTypeEthereumAccessList:
  1041  		intrinsic = params.TxGas
  1042  	case types.TxTypeEthereumDynamicFee:
  1043  		intrinsic = params.TxGas
  1044  	case types.TxTypeValueTransfer:
  1045  		intrinsic = params.TxGasValueTransfer
  1046  	case types.TxTypeFeeDelegatedValueTransfer:
  1047  		intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegated
  1048  	case types.TxTypeFeeDelegatedValueTransferWithRatio:
  1049  		intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegatedWithRatio
  1050  	case types.TxTypeValueTransferMemo:
  1051  		intrinsic = params.TxGasValueTransfer
  1052  	case types.TxTypeFeeDelegatedValueTransferMemo:
  1053  		intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegated
  1054  	case types.TxTypeFeeDelegatedValueTransferMemoWithRatio:
  1055  		intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegatedWithRatio
  1056  	case types.TxTypeAccountUpdate:
  1057  		intrinsic = params.TxGasAccountUpdate
  1058  	case types.TxTypeFeeDelegatedAccountUpdate:
  1059  		intrinsic = params.TxGasAccountUpdate + params.TxGasFeeDelegated
  1060  	case types.TxTypeFeeDelegatedAccountUpdateWithRatio:
  1061  		intrinsic = params.TxGasAccountUpdate + params.TxGasFeeDelegatedWithRatio
  1062  	case types.TxTypeSmartContractDeploy:
  1063  		intrinsic = params.TxGasContractCreation
  1064  	case types.TxTypeFeeDelegatedSmartContractDeploy:
  1065  		intrinsic = params.TxGasContractCreation + params.TxGasFeeDelegated
  1066  	case types.TxTypeFeeDelegatedSmartContractDeployWithRatio:
  1067  		intrinsic = params.TxGasContractCreation + params.TxGasFeeDelegatedWithRatio
  1068  	case types.TxTypeSmartContractExecution:
  1069  		intrinsic = params.TxGasContractExecution
  1070  	case types.TxTypeFeeDelegatedSmartContractExecution:
  1071  		intrinsic = params.TxGasContractExecution + params.TxGasFeeDelegated
  1072  	case types.TxTypeFeeDelegatedSmartContractExecutionWithRatio:
  1073  		intrinsic = params.TxGasContractExecution + params.TxGasFeeDelegatedWithRatio
  1074  	case types.TxTypeChainDataAnchoring:
  1075  		intrinsic = params.TxChainDataAnchoringGas
  1076  	case types.TxTypeFeeDelegatedChainDataAnchoring:
  1077  		intrinsic = params.TxChainDataAnchoringGas + params.TxGasFeeDelegated
  1078  	case types.TxTypeFeeDelegatedChainDataAnchoringWithRatio:
  1079  		intrinsic = params.TxChainDataAnchoringGas + params.TxGasFeeDelegatedWithRatio
  1080  	case types.TxTypeCancel:
  1081  		intrinsic = params.TxGasCancel
  1082  	case types.TxTypeFeeDelegatedCancel:
  1083  		intrinsic = params.TxGasCancel + params.TxGasFeeDelegated
  1084  	case types.TxTypeFeeDelegatedCancelWithRatio:
  1085  		intrinsic = params.TxGasCancel + params.TxGasFeeDelegatedWithRatio
  1086  	}
  1087  
  1088  	return intrinsic
  1089  }
  1090  
  1091  // Implement TestAccount interface for TestAccountType
  1092  func (t *TestAccountType) GetAddr() common.Address {
  1093  	return t.Addr
  1094  }
  1095  
  1096  func (t *TestAccountType) GetTxKeys() []*ecdsa.PrivateKey {
  1097  	return t.Keys
  1098  }
  1099  
  1100  func (t *TestAccountType) GetUpdateKeys() []*ecdsa.PrivateKey {
  1101  	return t.Keys
  1102  }
  1103  
  1104  func (t *TestAccountType) GetFeeKeys() []*ecdsa.PrivateKey {
  1105  	return t.Keys
  1106  }
  1107  
  1108  func (t *TestAccountType) GetNonce() uint64 {
  1109  	return t.Nonce
  1110  }
  1111  
  1112  func (t *TestAccountType) GetAccKey() accountkey.AccountKey {
  1113  	return t.AccKey
  1114  }
  1115  
  1116  func (t *TestAccountType) SetNonce(nonce uint64) {
  1117  	t.Nonce = nonce
  1118  }
  1119  
  1120  func (t *TestAccountType) SetAddr(addr common.Address) {
  1121  	t.Addr = addr
  1122  }
  1123  
  1124  // Return SigValidationGas depends on AccountType
  1125  func (t *TestAccountType) GetValidationGas(r accountkey.RoleType) uint64 {
  1126  	if t.GetAccKey() == nil {
  1127  		return 0
  1128  	}
  1129  
  1130  	var gas uint64
  1131  
  1132  	switch t.GetAccKey().Type() {
  1133  	case accountkey.AccountKeyTypeLegacy:
  1134  		gas = 0
  1135  	case accountkey.AccountKeyTypePublic:
  1136  		gas = (1 - 1) * params.TxValidationGasPerKey
  1137  	case accountkey.AccountKeyTypeWeightedMultiSig:
  1138  		gas = uint64(len(t.GetTxKeys())-1) * params.TxValidationGasPerKey
  1139  	}
  1140  
  1141  	return gas
  1142  }
  1143  
  1144  func (t *TestAccountType) AddNonce() {
  1145  	t.Nonce += 1
  1146  }
  1147  
  1148  // Implement TestAccount interface for TestRoleBasedAccountType
  1149  func (t *TestRoleBasedAccountType) GetAddr() common.Address {
  1150  	return t.Addr
  1151  }
  1152  
  1153  func (t *TestRoleBasedAccountType) GetTxKeys() []*ecdsa.PrivateKey {
  1154  	return t.TxKeys
  1155  }
  1156  
  1157  func (t *TestRoleBasedAccountType) GetUpdateKeys() []*ecdsa.PrivateKey {
  1158  	return t.UpdateKeys
  1159  }
  1160  
  1161  func (t *TestRoleBasedAccountType) GetFeeKeys() []*ecdsa.PrivateKey {
  1162  	return t.FeeKeys
  1163  }
  1164  
  1165  func (t *TestRoleBasedAccountType) GetNonce() uint64 {
  1166  	return t.Nonce
  1167  }
  1168  
  1169  func (t *TestRoleBasedAccountType) GetAccKey() accountkey.AccountKey {
  1170  	return t.AccKey
  1171  }
  1172  
  1173  func (t *TestRoleBasedAccountType) SetNonce(nonce uint64) {
  1174  	t.Nonce = nonce
  1175  }
  1176  
  1177  func (t *TestRoleBasedAccountType) SetAddr(addr common.Address) {
  1178  	t.Addr = addr
  1179  }
  1180  
  1181  // Return SigValidationGas depends on AccountType
  1182  func (t *TestRoleBasedAccountType) GetValidationGas(r accountkey.RoleType) uint64 {
  1183  	if t.GetAccKey() == nil {
  1184  		return 0
  1185  	}
  1186  
  1187  	var gas uint64
  1188  
  1189  	switch r {
  1190  	case accountkey.RoleTransaction:
  1191  		gas = uint64(len(t.GetTxKeys())-1) * params.TxValidationGasPerKey
  1192  	case accountkey.RoleAccountUpdate:
  1193  		gas = uint64(len(t.GetUpdateKeys())-1) * params.TxValidationGasPerKey
  1194  	case accountkey.RoleFeePayer:
  1195  		gas = uint64(len(t.GetFeeKeys())-1) * params.TxValidationGasPerKey
  1196  	}
  1197  
  1198  	return gas
  1199  }
  1200  
  1201  func (t *TestRoleBasedAccountType) AddNonce() {
  1202  	t.Nonce += 1
  1203  }