github.com/klaytn/klaytn@v1.12.1/tests/rpc_output_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  // Basically, this test is disabled for the CI test. To run this,
    18  // $ go test -run TestRPCOutput -tags RPCOutput
    19  package tests
    20  
    21  import (
    22  	"crypto/ecdsa"
    23  	"encoding/json"
    24  	"fmt"
    25  	"math/big"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/klaytn/klaytn/accounts/abi"
    31  	"github.com/klaytn/klaytn/blockchain/types"
    32  	"github.com/klaytn/klaytn/blockchain/types/accountkey"
    33  	"github.com/klaytn/klaytn/common"
    34  	"github.com/klaytn/klaytn/common/hexutil"
    35  	"github.com/klaytn/klaytn/common/profile"
    36  	"github.com/klaytn/klaytn/consensus/istanbul/backend"
    37  	"github.com/klaytn/klaytn/crypto"
    38  	"github.com/klaytn/klaytn/log"
    39  	"github.com/klaytn/klaytn/networks/rpc"
    40  	"github.com/klaytn/klaytn/params"
    41  	"github.com/klaytn/klaytn/rlp"
    42  	"github.com/stretchr/testify/assert"
    43  )
    44  
    45  // TestRPCOutput prints the RPC output in a JSON format by executing GetBlockWithConsensusInfoByNumber().
    46  // This function makes two blocks.
    47  // The first block contains the following:
    48  // - TxTypeLegacyTransaction
    49  // - TxTypeValueTransfer
    50  // - TxTypeFeeDelegatedValueTransfer
    51  // - TxTypeFeeDelegatedValueTransferWithRatio
    52  // - TxTypeValueTransferMemo
    53  // - TxTypeFeeDelegatedValueTransferMemo
    54  // - TxTypeFeeDelegatedValueTransferMemoWithRatio
    55  // - TxTypeAccountCreation
    56  // The Second block contains the following:
    57  // - TxTypeAccountUpdate
    58  // - TxTypeFeeDelegatedAccountUpdate
    59  // - TxTypeFeeDelegatedAccountUpdateWithRatio
    60  // - TxTypeSmartContractDeploy
    61  // - TxTypeFeeDelegatedSmartContractDeploy
    62  // - TxTypeFeeDelegatedSmartContractDeployWithRatio
    63  // - TxTypeSmartContractExecution
    64  // - TxTypeFeeDelegatedSmartContractExecution
    65  // - TxTypeFeeDelegatedSmartContractExecutionWithRatio
    66  // - TxTypeCancel
    67  // - TxTypeFeeDelegatedCancel
    68  // - TxTypeFeeDelegatedCancelWithRatio
    69  // - TxTypeChainDataAnchoring
    70  func BenchmarkRPCOutput(t *testing.B) {
    71  	log.EnableLogForTest(log.LvlCrit, log.LvlTrace)
    72  	prof := profile.NewProfiler()
    73  
    74  	// Initialize blockchain
    75  	start := time.Now()
    76  	bcdata, err := NewBCData(6, 4)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	prof.Profile("main_init_blockchain", time.Now().Sub(start))
    81  	defer bcdata.Shutdown()
    82  
    83  	// Initialize address-balance map for verification
    84  	start = time.Now()
    85  	accountMap := NewAccountMap()
    86  	if err := accountMap.Initialize(bcdata); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	prof.Profile("main_init_accountMap", time.Now().Sub(start))
    90  
    91  	// reservoir account
    92  	reservoir := &TestAccountType{
    93  		Addr:  *bcdata.addrs[0],
    94  		Keys:  []*ecdsa.PrivateKey{bcdata.privKeys[0]},
    95  		Nonce: uint64(0),
    96  	}
    97  
    98  	reservoir2 := &TestAccountType{
    99  		Addr:  *bcdata.addrs[1],
   100  		Keys:  []*ecdsa.PrivateKey{bcdata.privKeys[1]},
   101  		Nonce: uint64(0),
   102  	}
   103  
   104  	// anonymous account
   105  	anon, err := createAnonymousAccount("98275a145bc1726eb0445433088f5f882f8a4a9499135239cfb4040e78991dab")
   106  	assert.Equal(t, nil, err)
   107  
   108  	// decoupled account
   109  	decoupled, err := createDecoupledAccount("c64f2cd1196e2a1791365b00c4bc07ab8f047b73152e4617c6ed06ac221a4b0c",
   110  		common.HexToAddress("0x75c3098be5e4b63fbac05838daaee378dd48098d"))
   111  	assert.Equal(t, nil, err)
   112  
   113  	// contract address
   114  	contractAddr := common.Address{}
   115  
   116  	if testing.Verbose() {
   117  		fmt.Println("ChainID", (*hexutil.Big)(bcdata.bc.Config().ChainID))
   118  		fmt.Println("reservoirAddr = ", reservoir.Addr.String())
   119  		fmt.Println("reservoirPrvKey = ", (*hexutil.Big)(reservoir.Keys[0].D))
   120  		fmt.Println("reservoir2Addr = ", reservoir2.Addr.String())
   121  		fmt.Println("reservoir2PrvKey= ", (*hexutil.Big)(reservoir2.Keys[0].D))
   122  		fmt.Println("anonAddr = ", anon.Addr.String())
   123  		fmt.Println("anonPrvKey= ", (*hexutil.Big)(anon.Keys[0].D))
   124  		fmt.Println("decoupledAddr = ", decoupled.Addr.String())
   125  		fmt.Println("decoupledPrvKey = ", (*hexutil.Big)(decoupled.Keys[0].D))
   126  	}
   127  
   128  	signer := types.LatestSignerForChainID(bcdata.bc.Config().ChainID)
   129  	gasPrice := new(big.Int).SetUint64(bcdata.bc.Config().UnitPrice)
   130  
   131  	var txs types.Transactions
   132  
   133  	// TxTypeLegacyTransaction
   134  	{
   135  		amount := new(big.Int).SetUint64(100000000000)
   136  		tx := types.NewTransaction(reservoir.Nonce,
   137  			anon.Addr, amount, gasLimit, gasPrice, []byte{})
   138  
   139  		err := tx.SignWithKeys(signer, reservoir.Keys)
   140  		assert.Equal(t, nil, err)
   141  
   142  		txs = append(txs, tx)
   143  
   144  		reservoir.Nonce += 1
   145  	}
   146  
   147  	// TxTypeValueTransfer
   148  	{
   149  		amount := new(big.Int).Mul(big.NewInt(10000), new(big.Int).SetUint64(params.KLAY))
   150  		values := map[types.TxValueKeyType]interface{}{
   151  			types.TxValueKeyNonce:    reservoir.Nonce,
   152  			types.TxValueKeyFrom:     reservoir.Addr,
   153  			types.TxValueKeyTo:       decoupled.Addr,
   154  			types.TxValueKeyAmount:   amount,
   155  			types.TxValueKeyGasLimit: gasLimit,
   156  			types.TxValueKeyGasPrice: gasPrice,
   157  		}
   158  		tx, err := types.NewTransactionWithMap(types.TxTypeValueTransfer, values)
   159  		assert.Equal(t, nil, err)
   160  
   161  		err = tx.SignWithKeys(signer, reservoir.Keys)
   162  		assert.Equal(t, nil, err)
   163  
   164  		txs = append(txs, tx)
   165  
   166  		reservoir.Nonce += 1
   167  	}
   168  
   169  	// TxTypeFeeDelegatedValueTransfer
   170  	{
   171  		amount := new(big.Int).Mul(big.NewInt(10000), new(big.Int).SetUint64(params.KLAY))
   172  		values := map[types.TxValueKeyType]interface{}{
   173  			types.TxValueKeyNonce:    reservoir.Nonce,
   174  			types.TxValueKeyFrom:     reservoir.Addr,
   175  			types.TxValueKeyTo:       decoupled.Addr,
   176  			types.TxValueKeyAmount:   amount,
   177  			types.TxValueKeyGasLimit: gasLimit,
   178  			types.TxValueKeyGasPrice: gasPrice,
   179  			types.TxValueKeyFeePayer: reservoir2.Addr,
   180  		}
   181  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransfer, values)
   182  		assert.Equal(t, nil, err)
   183  
   184  		err = tx.SignWithKeys(signer, reservoir.Keys)
   185  		assert.Equal(t, nil, err)
   186  
   187  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   188  		assert.Equal(t, nil, err)
   189  
   190  		txs = append(txs, tx)
   191  
   192  		reservoir.Nonce += 1
   193  	}
   194  
   195  	// TxTypeFeeDelegatedValueTransferWithRatio
   196  	{
   197  		amount := new(big.Int).SetUint64(10000000)
   198  		values := map[types.TxValueKeyType]interface{}{
   199  			types.TxValueKeyNonce:              reservoir.Nonce,
   200  			types.TxValueKeyFrom:               reservoir.Addr,
   201  			types.TxValueKeyTo:                 decoupled.Addr,
   202  			types.TxValueKeyAmount:             amount,
   203  			types.TxValueKeyGasLimit:           gasLimit,
   204  			types.TxValueKeyGasPrice:           gasPrice,
   205  			types.TxValueKeyFeePayer:           reservoir2.Addr,
   206  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(20),
   207  		}
   208  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferWithRatio, values)
   209  		assert.Equal(t, nil, err)
   210  
   211  		err = tx.SignWithKeys(signer, reservoir.Keys)
   212  		assert.Equal(t, nil, err)
   213  
   214  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   215  		assert.Equal(t, nil, err)
   216  
   217  		txs = append(txs, tx)
   218  
   219  		reservoir.Nonce += 1
   220  	}
   221  
   222  	// TxTypeValueTransferMemo
   223  	{
   224  		amount := new(big.Int).SetUint64(10000000)
   225  		values := map[types.TxValueKeyType]interface{}{
   226  			types.TxValueKeyNonce:    reservoir.Nonce,
   227  			types.TxValueKeyFrom:     reservoir.Addr,
   228  			types.TxValueKeyTo:       decoupled.Addr,
   229  			types.TxValueKeyAmount:   amount,
   230  			types.TxValueKeyGasLimit: gasLimit,
   231  			types.TxValueKeyGasPrice: gasPrice,
   232  			types.TxValueKeyData:     []byte("hello"),
   233  		}
   234  		tx, err := types.NewTransactionWithMap(types.TxTypeValueTransferMemo, values)
   235  		assert.Equal(t, nil, err)
   236  
   237  		err = tx.SignWithKeys(signer, reservoir.Keys)
   238  		assert.Equal(t, nil, err)
   239  
   240  		txs = append(txs, tx)
   241  
   242  		reservoir.Nonce += 1
   243  	}
   244  
   245  	// TxTypeFeeDelegatedValueTransferMemo
   246  	{
   247  		amount := new(big.Int).SetUint64(10000000)
   248  		values := map[types.TxValueKeyType]interface{}{
   249  			types.TxValueKeyNonce:    reservoir.Nonce,
   250  			types.TxValueKeyFrom:     reservoir.Addr,
   251  			types.TxValueKeyTo:       decoupled.Addr,
   252  			types.TxValueKeyAmount:   amount,
   253  			types.TxValueKeyGasLimit: gasLimit,
   254  			types.TxValueKeyGasPrice: gasPrice,
   255  			types.TxValueKeyData:     []byte("hello"),
   256  			types.TxValueKeyFeePayer: reservoir2.Addr,
   257  		}
   258  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemo, values)
   259  		assert.Equal(t, nil, err)
   260  
   261  		err = tx.SignWithKeys(signer, reservoir.Keys)
   262  		assert.Equal(t, nil, err)
   263  
   264  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   265  		assert.Equal(t, nil, err)
   266  
   267  		txs = append(txs, tx)
   268  
   269  		reservoir.Nonce += 1
   270  	}
   271  
   272  	// TxTypeFeeDelegatedValueTransferMemoWithRatio
   273  	{
   274  		amount := new(big.Int).SetUint64(10000000)
   275  		values := map[types.TxValueKeyType]interface{}{
   276  			types.TxValueKeyNonce:              reservoir.Nonce,
   277  			types.TxValueKeyFrom:               reservoir.Addr,
   278  			types.TxValueKeyTo:                 decoupled.Addr,
   279  			types.TxValueKeyAmount:             amount,
   280  			types.TxValueKeyGasLimit:           gasLimit,
   281  			types.TxValueKeyGasPrice:           gasPrice,
   282  			types.TxValueKeyData:               []byte("hello"),
   283  			types.TxValueKeyFeePayer:           reservoir2.Addr,
   284  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(30),
   285  		}
   286  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemoWithRatio, values)
   287  		assert.Equal(t, nil, err)
   288  
   289  		err = tx.SignWithKeys(signer, reservoir.Keys)
   290  		assert.Equal(t, nil, err)
   291  
   292  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   293  		assert.Equal(t, nil, err)
   294  
   295  		txs = append(txs, tx)
   296  
   297  		reservoir.Nonce += 1
   298  	}
   299  
   300  	// Generate the first block!
   301  	if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   302  		t.Fatal(err)
   303  	}
   304  
   305  	// Make a new transaction slice.
   306  	txs = make(types.Transactions, 0, 10)
   307  
   308  	// TxTypeAccountUpdate
   309  	{
   310  		newKey, err := crypto.HexToECDSA("41bd2b972564206658eab115f26ff4db617e6eb39c81a557adc18d8305d2f867")
   311  		if err != nil {
   312  			t.Fatal(err)
   313  		}
   314  
   315  		values := map[types.TxValueKeyType]interface{}{
   316  			types.TxValueKeyNonce:      anon.Nonce,
   317  			types.TxValueKeyFrom:       anon.Addr,
   318  			types.TxValueKeyGasLimit:   gasLimit,
   319  			types.TxValueKeyGasPrice:   gasPrice,
   320  			types.TxValueKeyAccountKey: accountkey.NewAccountKeyPublicWithValue(&newKey.PublicKey),
   321  		}
   322  		tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values)
   323  		assert.Equal(t, nil, err)
   324  
   325  		err = tx.SignWithKeys(signer, anon.Keys)
   326  		assert.Equal(t, nil, err)
   327  
   328  		txs = append(txs, tx)
   329  
   330  		anon.Nonce += 1
   331  
   332  		anon.Keys = []*ecdsa.PrivateKey{newKey}
   333  	}
   334  
   335  	// TxTypeFeeDelegatedAccountUpdate
   336  	{
   337  		newKey, err := crypto.HexToECDSA("41bd2b972564206658eab115f26ff4db617e6eb39c81a557adc18d8305d2f867")
   338  		if err != nil {
   339  			t.Fatal(err)
   340  		}
   341  
   342  		values := map[types.TxValueKeyType]interface{}{
   343  			types.TxValueKeyNonce:      anon.Nonce,
   344  			types.TxValueKeyFrom:       anon.Addr,
   345  			types.TxValueKeyGasLimit:   gasLimit,
   346  			types.TxValueKeyGasPrice:   gasPrice,
   347  			types.TxValueKeyAccountKey: accountkey.NewAccountKeyPublicWithValue(&newKey.PublicKey),
   348  			types.TxValueKeyFeePayer:   reservoir.Addr,
   349  		}
   350  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdate, values)
   351  		assert.Equal(t, nil, err)
   352  
   353  		err = tx.SignWithKeys(signer, anon.Keys)
   354  		assert.Equal(t, nil, err)
   355  
   356  		err = tx.SignFeePayerWithKeys(signer, reservoir.Keys)
   357  		assert.Equal(t, nil, err)
   358  
   359  		txs = append(txs, tx)
   360  
   361  		anon.Nonce += 1
   362  
   363  		anon.Keys = []*ecdsa.PrivateKey{newKey}
   364  	}
   365  
   366  	// TxTypeFeeDelegatedAccountUpdateWithRatio
   367  	{
   368  		newKey, err := crypto.HexToECDSA("ed580f5bd71a2ee4dae5cb43e331b7d0318596e561e6add7844271ed94156b20")
   369  		if err != nil {
   370  			t.Fatal(err)
   371  		}
   372  
   373  		values := map[types.TxValueKeyType]interface{}{
   374  			types.TxValueKeyNonce:              anon.Nonce,
   375  			types.TxValueKeyFrom:               anon.Addr,
   376  			types.TxValueKeyGasLimit:           gasLimit,
   377  			types.TxValueKeyGasPrice:           gasPrice,
   378  			types.TxValueKeyAccountKey:         accountkey.NewAccountKeyPublicWithValue(&newKey.PublicKey),
   379  			types.TxValueKeyFeePayer:           reservoir.Addr,
   380  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(11),
   381  		}
   382  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdateWithRatio, values)
   383  		assert.Equal(t, nil, err)
   384  
   385  		err = tx.SignWithKeys(signer, anon.Keys)
   386  		assert.Equal(t, nil, err)
   387  
   388  		err = tx.SignFeePayerWithKeys(signer, reservoir.Keys)
   389  		assert.Equal(t, nil, err)
   390  
   391  		txs = append(txs, tx)
   392  
   393  		anon.Nonce += 1
   394  
   395  		anon.Keys = []*ecdsa.PrivateKey{newKey}
   396  	}
   397  
   398  	code := "0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029"
   399  	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"}]`
   400  
   401  	// TxTypeSmartContractDeploy
   402  	{
   403  		amount := new(big.Int).SetUint64(0)
   404  		values := map[types.TxValueKeyType]interface{}{
   405  			types.TxValueKeyNonce:         reservoir.Nonce,
   406  			types.TxValueKeyFrom:          reservoir.Addr,
   407  			types.TxValueKeyTo:            (*common.Address)(nil),
   408  			types.TxValueKeyAmount:        amount,
   409  			types.TxValueKeyGasLimit:      gasLimit,
   410  			types.TxValueKeyGasPrice:      common.Big0,
   411  			types.TxValueKeyHumanReadable: false,
   412  			types.TxValueKeyData:          common.FromHex(code),
   413  			types.TxValueKeyCodeFormat:    params.CodeFormatEVM,
   414  		}
   415  		tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values)
   416  		assert.Equal(t, nil, err)
   417  
   418  		err = tx.SignWithKeys(signer, reservoir.Keys)
   419  		assert.Equal(t, nil, err)
   420  
   421  		txs = append(txs, tx)
   422  
   423  		contractAddr = crypto.CreateAddress(reservoir.Addr, reservoir.Nonce)
   424  
   425  		reservoir.Nonce += 1
   426  	}
   427  
   428  	// TxTypeFeeDelegatedSmartContractDeploy
   429  	{
   430  		amount := new(big.Int).SetUint64(0)
   431  		values := map[types.TxValueKeyType]interface{}{
   432  			types.TxValueKeyNonce:         reservoir.Nonce,
   433  			types.TxValueKeyFrom:          reservoir.Addr,
   434  			types.TxValueKeyTo:            (*common.Address)(nil),
   435  			types.TxValueKeyAmount:        amount,
   436  			types.TxValueKeyGasLimit:      gasLimit,
   437  			types.TxValueKeyGasPrice:      common.Big0,
   438  			types.TxValueKeyHumanReadable: false,
   439  			types.TxValueKeyData:          common.FromHex(code),
   440  			types.TxValueKeyFeePayer:      reservoir2.Addr,
   441  			types.TxValueKeyCodeFormat:    params.CodeFormatEVM,
   442  		}
   443  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeploy, values)
   444  		assert.Equal(t, nil, err)
   445  
   446  		err = tx.SignWithKeys(signer, reservoir.Keys)
   447  		assert.Equal(t, nil, err)
   448  
   449  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   450  		assert.Equal(t, nil, err)
   451  
   452  		txs = append(txs, tx)
   453  
   454  		reservoir.Nonce += 1
   455  	}
   456  
   457  	// TxTypeFeeDelegatedSmartContractDeployWithRatio
   458  	{
   459  		amount := new(big.Int).SetUint64(0)
   460  		values := map[types.TxValueKeyType]interface{}{
   461  			types.TxValueKeyNonce:              reservoir.Nonce,
   462  			types.TxValueKeyFrom:               reservoir.Addr,
   463  			types.TxValueKeyTo:                 (*common.Address)(nil),
   464  			types.TxValueKeyAmount:             amount,
   465  			types.TxValueKeyGasLimit:           gasLimit,
   466  			types.TxValueKeyGasPrice:           common.Big0,
   467  			types.TxValueKeyHumanReadable:      false,
   468  			types.TxValueKeyData:               common.FromHex(code),
   469  			types.TxValueKeyFeePayer:           reservoir2.Addr,
   470  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(33),
   471  			types.TxValueKeyCodeFormat:         params.CodeFormatEVM,
   472  		}
   473  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeployWithRatio, values)
   474  		assert.Equal(t, nil, err)
   475  
   476  		err = tx.SignWithKeys(signer, reservoir.Keys)
   477  		assert.Equal(t, nil, err)
   478  
   479  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   480  		assert.Equal(t, nil, err)
   481  
   482  		txs = append(txs, tx)
   483  
   484  		reservoir.Nonce += 1
   485  	}
   486  
   487  	// TxTypeSmartContractExecution
   488  	{
   489  		amountToSend := new(big.Int).SetUint64(10)
   490  
   491  		abii, err := abi.JSON(strings.NewReader(string(abiStr)))
   492  		assert.Equal(t, nil, err)
   493  
   494  		data, err := abii.Pack("reward", reservoir.Addr)
   495  		assert.Equal(t, nil, err)
   496  
   497  		values := map[types.TxValueKeyType]interface{}{
   498  			types.TxValueKeyNonce:    reservoir.Nonce,
   499  			types.TxValueKeyFrom:     reservoir.Addr,
   500  			types.TxValueKeyTo:       contractAddr,
   501  			types.TxValueKeyAmount:   amountToSend,
   502  			types.TxValueKeyGasLimit: gasLimit,
   503  			types.TxValueKeyGasPrice: gasPrice,
   504  			types.TxValueKeyData:     data,
   505  		}
   506  		tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractExecution, values)
   507  		assert.Equal(t, nil, err)
   508  
   509  		err = tx.SignWithKeys(signer, reservoir.Keys)
   510  		assert.Equal(t, nil, err)
   511  
   512  		txs = append(txs, tx)
   513  
   514  		reservoir.Nonce += 1
   515  	}
   516  
   517  	// TxTypeFeeDelegatedSmartContractExecution
   518  	{
   519  		amountToSend := new(big.Int).SetUint64(10)
   520  
   521  		abii, err := abi.JSON(strings.NewReader(string(abiStr)))
   522  		assert.Equal(t, nil, err)
   523  
   524  		data, err := abii.Pack("reward", reservoir.Addr)
   525  		assert.Equal(t, nil, err)
   526  
   527  		values := map[types.TxValueKeyType]interface{}{
   528  			types.TxValueKeyNonce:    reservoir.Nonce,
   529  			types.TxValueKeyFrom:     reservoir.Addr,
   530  			types.TxValueKeyTo:       contractAddr,
   531  			types.TxValueKeyAmount:   amountToSend,
   532  			types.TxValueKeyGasLimit: gasLimit,
   533  			types.TxValueKeyGasPrice: common.Big0,
   534  			types.TxValueKeyData:     data,
   535  			types.TxValueKeyFeePayer: reservoir2.Addr,
   536  		}
   537  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecution, values)
   538  		assert.Equal(t, nil, err)
   539  
   540  		err = tx.SignWithKeys(signer, reservoir.Keys)
   541  		assert.Equal(t, nil, err)
   542  
   543  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   544  		assert.Equal(t, nil, err)
   545  
   546  		txs = append(txs, tx)
   547  
   548  		reservoir.Nonce += 1
   549  	}
   550  
   551  	// TxTypeFeeDelegatedSmartContractExecutionWithRatio
   552  	{
   553  		amountToSend := new(big.Int).SetUint64(10)
   554  
   555  		abii, err := abi.JSON(strings.NewReader(string(abiStr)))
   556  		assert.Equal(t, nil, err)
   557  
   558  		data, err := abii.Pack("reward", reservoir.Addr)
   559  		assert.Equal(t, nil, err)
   560  
   561  		values := map[types.TxValueKeyType]interface{}{
   562  			types.TxValueKeyNonce:              reservoir.Nonce,
   563  			types.TxValueKeyFrom:               reservoir.Addr,
   564  			types.TxValueKeyTo:                 contractAddr,
   565  			types.TxValueKeyAmount:             amountToSend,
   566  			types.TxValueKeyGasLimit:           gasLimit,
   567  			types.TxValueKeyGasPrice:           common.Big0,
   568  			types.TxValueKeyData:               data,
   569  			types.TxValueKeyFeePayer:           reservoir2.Addr,
   570  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(66),
   571  		}
   572  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecutionWithRatio, values)
   573  		assert.Equal(t, nil, err)
   574  
   575  		err = tx.SignWithKeys(signer, reservoir.Keys)
   576  		assert.Equal(t, nil, err)
   577  
   578  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   579  		assert.Equal(t, nil, err)
   580  
   581  		txs = append(txs, tx)
   582  
   583  		reservoir.Nonce += 1
   584  	}
   585  
   586  	// TxTypeCancel
   587  	{
   588  		values := map[types.TxValueKeyType]interface{}{
   589  			types.TxValueKeyNonce:    reservoir.Nonce,
   590  			types.TxValueKeyFrom:     reservoir.Addr,
   591  			types.TxValueKeyGasLimit: gasLimit,
   592  			types.TxValueKeyGasPrice: gasPrice,
   593  		}
   594  		tx, err := types.NewTransactionWithMap(types.TxTypeCancel, values)
   595  		assert.Equal(t, nil, err)
   596  
   597  		err = tx.SignWithKeys(signer, reservoir.Keys)
   598  		assert.Equal(t, nil, err)
   599  
   600  		txs = append(txs, tx)
   601  
   602  		reservoir.Nonce += 1
   603  	}
   604  
   605  	// TxTypeFeeDelegatedCancel
   606  	{
   607  		values := map[types.TxValueKeyType]interface{}{
   608  			types.TxValueKeyNonce:    reservoir.Nonce,
   609  			types.TxValueKeyFrom:     reservoir.Addr,
   610  			types.TxValueKeyGasLimit: gasLimit,
   611  			types.TxValueKeyGasPrice: gasPrice,
   612  			types.TxValueKeyFeePayer: reservoir2.Addr,
   613  		}
   614  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancel, values)
   615  		assert.Equal(t, nil, err)
   616  
   617  		err = tx.SignWithKeys(signer, reservoir.Keys)
   618  		assert.Equal(t, nil, err)
   619  
   620  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   621  		assert.Equal(t, nil, err)
   622  
   623  		txs = append(txs, tx)
   624  
   625  		reservoir.Nonce += 1
   626  	}
   627  
   628  	// TxTypeFeeDelegatedCancelWithRatio
   629  	{
   630  		values := map[types.TxValueKeyType]interface{}{
   631  			types.TxValueKeyNonce:              reservoir.Nonce,
   632  			types.TxValueKeyFrom:               reservoir.Addr,
   633  			types.TxValueKeyGasLimit:           gasLimit,
   634  			types.TxValueKeyGasPrice:           big.NewInt(0),
   635  			types.TxValueKeyFeePayer:           reservoir2.Addr,
   636  			types.TxValueKeyFeeRatioOfFeePayer: types.FeeRatio(88),
   637  		}
   638  		tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancelWithRatio, values)
   639  		assert.Equal(t, nil, err)
   640  
   641  		err = tx.SignWithKeys(signer, reservoir.Keys)
   642  		assert.Equal(t, nil, err)
   643  
   644  		err = tx.SignFeePayerWithKeys(signer, reservoir2.Keys)
   645  		assert.Equal(t, nil, err)
   646  
   647  		txs = append(txs, tx)
   648  
   649  		reservoir.Nonce += 1
   650  	}
   651  
   652  	// TxTypeChainDataAnchoring
   653  	{
   654  		data := &types.AnchoringDataInternalType0{
   655  			BlockHash:     common.HexToHash("0"),
   656  			TxHash:        common.HexToHash("1"),
   657  			ParentHash:    common.HexToHash("2"),
   658  			ReceiptHash:   common.HexToHash("3"),
   659  			StateRootHash: common.HexToHash("4"),
   660  			BlockNumber:   big.NewInt(5),
   661  			TxCount:       big.NewInt(6),
   662  		}
   663  		encodedCCTxData, err := rlp.EncodeToBytes(data)
   664  		if err != nil {
   665  			panic(err)
   666  		}
   667  		blockTxData := &types.AnchoringData{Type: 0, Data: encodedCCTxData}
   668  
   669  		anchoredData, err := rlp.EncodeToBytes(blockTxData)
   670  		if err != nil {
   671  			panic(err)
   672  		}
   673  
   674  		values := map[types.TxValueKeyType]interface{}{
   675  			types.TxValueKeyNonce:        reservoir.Nonce,
   676  			types.TxValueKeyFrom:         reservoir.Addr,
   677  			types.TxValueKeyGasLimit:     gasLimit,
   678  			types.TxValueKeyGasPrice:     gasPrice,
   679  			types.TxValueKeyAnchoredData: anchoredData,
   680  		}
   681  		tx, err := types.NewTransactionWithMap(types.TxTypeChainDataAnchoring, values)
   682  		assert.Equal(t, nil, err)
   683  
   684  		err = tx.SignWithKeys(signer, reservoir.Keys)
   685  		assert.Equal(t, nil, err)
   686  
   687  		txs = append(txs, tx)
   688  
   689  		reservoir.Nonce += 1
   690  	}
   691  
   692  	// Generate the second block!
   693  	if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil {
   694  		t.Fatal(err)
   695  	}
   696  
   697  	// Get APIExtension to execute GetBlockWithConsensusInfoByNumber.
   698  	apis := bcdata.bc.Engine().APIs(bcdata.bc)
   699  	apiExtension, ok := apis[1].Service.(*backend.APIExtension)
   700  	if !ok {
   701  		// checkout the code `consensus/istanbul/backend/engine.go` if it fails.
   702  		t.Fatalf("APIExetension is not the second item of apis. check out the code!")
   703  	}
   704  
   705  	// Print the JSON output of the first block.
   706  	blkNum := rpc.BlockNumber(1)
   707  	out, err := apiExtension.GetBlockWithConsensusInfoByNumber(&blkNum)
   708  	b, _ := json.MarshalIndent(out, "", "\t")
   709  	fmt.Println(string(b))
   710  
   711  	// Print the JSON output of the second block.
   712  	blkNum = rpc.BlockNumber(2)
   713  	out, err = apiExtension.GetBlockWithConsensusInfoByNumber(&blkNum)
   714  	b, _ = json.MarshalIndent(out, "", "\t")
   715  	fmt.Println(string(b))
   716  
   717  	if testing.Verbose() {
   718  		prof.PrintProfileInfo()
   719  	}
   720  }