github.com/klaytn/klaytn@v1.10.2/node/cn/api_backend_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 cn
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/golang/mock/gomock"
    24  	"github.com/klaytn/klaytn/blockchain"
    25  	"github.com/klaytn/klaytn/blockchain/state"
    26  	"github.com/klaytn/klaytn/blockchain/types"
    27  	"github.com/klaytn/klaytn/common"
    28  	mocks3 "github.com/klaytn/klaytn/event/mocks"
    29  	"github.com/klaytn/klaytn/networks/rpc"
    30  	mocks2 "github.com/klaytn/klaytn/node/cn/mocks"
    31  	"github.com/klaytn/klaytn/params"
    32  	"github.com/klaytn/klaytn/storage/database"
    33  	"github.com/klaytn/klaytn/work/mocks"
    34  	"github.com/stretchr/testify/assert"
    35  	"golang.org/x/net/context"
    36  )
    37  
    38  func newCNAPIBackend(t *testing.T) (*gomock.Controller, *mocks.MockBlockChain, *mocks2.MockMiner, *CNAPIBackend) {
    39  	mockCtrl := gomock.NewController(t)
    40  
    41  	mockBlockChain := mocks.NewMockBlockChain(mockCtrl)
    42  	mockMiner := mocks2.NewMockMiner(mockCtrl)
    43  
    44  	cn := &CN{blockchain: mockBlockChain, miner: mockMiner}
    45  
    46  	return mockCtrl, mockBlockChain, mockMiner, &CNAPIBackend{cn: cn}
    47  }
    48  
    49  func TestCNAPIBackend_GetTxAndLookupInfoInCache(t *testing.T) {
    50  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
    51  	defer mockCtrl.Finish()
    52  
    53  	hash := hashes[0]
    54  
    55  	expectedTx := tx1
    56  	expectedBlockHash := hashes[1]
    57  	expectedBlockNum := uint64(111)
    58  	expectedIndex := uint64(999)
    59  
    60  	mockBlockChain.EXPECT().GetTxAndLookupInfoInCache(hash).Times(1).Return(expectedTx, expectedBlockHash, expectedBlockNum, expectedIndex)
    61  	tx, blockHash, blockNumber, index := api.GetTxAndLookupInfoInCache(hash)
    62  
    63  	assert.Equal(t, expectedTx, tx)
    64  	assert.Equal(t, expectedBlockHash, blockHash)
    65  	assert.Equal(t, expectedBlockNum, blockNumber)
    66  	assert.Equal(t, expectedIndex, index)
    67  }
    68  
    69  func TestCNAPIBackend_GetTxLookupInfoAndReceipt(t *testing.T) {
    70  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
    71  	defer mockCtrl.Finish()
    72  
    73  	hash := hashes[0]
    74  
    75  	expectedTx := tx1
    76  	expectedBlockHash := hashes[1]
    77  	expectedBlockNum := uint64(111)
    78  	expectedIndex := uint64(999)
    79  	expectedReceipt := newReceipt(123)
    80  
    81  	mockBlockChain.EXPECT().GetTxLookupInfoAndReceipt(hash).Times(1).Return(expectedTx, expectedBlockHash, expectedBlockNum, expectedIndex, expectedReceipt)
    82  	tx, blockHash, blockNumber, index, receipt := api.GetTxLookupInfoAndReceipt(context.Background(), hash)
    83  
    84  	assert.Equal(t, expectedTx, tx)
    85  	assert.Equal(t, expectedBlockHash, blockHash)
    86  	assert.Equal(t, expectedBlockNum, blockNumber)
    87  	assert.Equal(t, expectedIndex, index)
    88  	assert.Equal(t, expectedReceipt, receipt)
    89  }
    90  
    91  func TestCNAPIBackend_GetBlockReceiptsInCache(t *testing.T) {
    92  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
    93  	defer mockCtrl.Finish()
    94  
    95  	hash := hashes[0]
    96  	expectedReceipts := types.Receipts{newReceipt(111), newReceipt(222)}
    97  
    98  	mockBlockChain.EXPECT().GetBlockReceiptsInCache(hash).Return(expectedReceipts).Times(1)
    99  
   100  	assert.Equal(t, expectedReceipts, api.GetBlockReceiptsInCache(hash))
   101  }
   102  
   103  func TestCNAPIBackend_GetTxLookupInfoAndReceiptInCache(t *testing.T) {
   104  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   105  	defer mockCtrl.Finish()
   106  
   107  	hash := hashes[0]
   108  
   109  	expectedTx := tx1
   110  	expectedBlockHash := hashes[1]
   111  	expectedBlockNum := uint64(111)
   112  	expectedIndex := uint64(999)
   113  	expectedReceipt := newReceipt(123)
   114  
   115  	mockBlockChain.EXPECT().GetTxLookupInfoAndReceiptInCache(hash).Times(1).Return(expectedTx, expectedBlockHash, expectedBlockNum, expectedIndex, expectedReceipt)
   116  	tx, blockHash, blockNumber, index, receipt := api.GetTxLookupInfoAndReceiptInCache(hash)
   117  
   118  	assert.Equal(t, expectedTx, tx)
   119  	assert.Equal(t, expectedBlockHash, blockHash)
   120  	assert.Equal(t, expectedBlockNum, blockNumber)
   121  	assert.Equal(t, expectedIndex, index)
   122  	assert.Equal(t, expectedReceipt, receipt)
   123  }
   124  
   125  func TestCNAPIBackend_ChainConfig(t *testing.T) {
   126  	mockCtrl, _, _, api := newCNAPIBackend(t)
   127  	defer mockCtrl.Finish()
   128  
   129  	assert.Nil(t, api.ChainConfig())
   130  
   131  	emptyConfig := &params.ChainConfig{}
   132  	api.cn.chainConfig = &*emptyConfig
   133  	assert.Equal(t, emptyConfig, api.ChainConfig())
   134  
   135  	nonEmptyConfig := &params.ChainConfig{ChainID: big.NewInt(123)}
   136  	api.cn.chainConfig = &*nonEmptyConfig
   137  	assert.Equal(t, nonEmptyConfig, api.ChainConfig())
   138  }
   139  
   140  func TestCNAPIBackend_CurrentBlock(t *testing.T) {
   141  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   142  	defer mockCtrl.Finish()
   143  
   144  	block := newBlock(123)
   145  	mockBlockChain.EXPECT().CurrentBlock().Return(block).Times(1)
   146  
   147  	assert.Equal(t, block, api.CurrentBlock())
   148  }
   149  
   150  func TestCNAPIBackend_SetHead(t *testing.T) {
   151  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   152  	defer mockCtrl.Finish()
   153  
   154  	mockDownloader := mocks2.NewMockProtocolManagerDownloader(mockCtrl)
   155  	mockDownloader.EXPECT().Cancel().Times(1)
   156  	pm := &ProtocolManager{downloader: mockDownloader}
   157  	api.cn.protocolManager = pm
   158  	number := uint64(123)
   159  	mockBlockChain.EXPECT().SetHead(number).Times(1)
   160  
   161  	api.SetHead(number)
   162  	block := newBlock(int(number))
   163  	expectedHeader := block.Header()
   164  	mockBlockChain.EXPECT().CurrentHeader().Return(expectedHeader).Times(1)
   165  	assert.Equal(t, expectedHeader, mockBlockChain.CurrentHeader())
   166  }
   167  
   168  func TestCNAPIBackend_HeaderByNumber(t *testing.T) {
   169  	blockNum := uint64(123)
   170  	block := newBlock(int(blockNum))
   171  	expectedHeader := block.Header()
   172  	{
   173  		mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
   174  		mockMiner.EXPECT().PendingBlock().Return(block).Times(1)
   175  
   176  		header, err := api.HeaderByNumber(context.Background(), rpc.PendingBlockNumber)
   177  
   178  		assert.Equal(t, expectedHeader, header)
   179  		assert.NoError(t, err)
   180  
   181  		mockCtrl.Finish()
   182  	}
   183  	{
   184  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   185  		mockBlockChain.EXPECT().CurrentBlock().Return(block).Times(1)
   186  
   187  		header, err := api.HeaderByNumber(context.Background(), rpc.LatestBlockNumber)
   188  
   189  		assert.Equal(t, expectedHeader, header)
   190  		assert.NoError(t, err)
   191  
   192  		mockCtrl.Finish()
   193  	}
   194  	{
   195  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   196  		mockBlockChain.EXPECT().GetHeaderByNumber(blockNum).Return(nil).Times(1)
   197  
   198  		header, err := api.HeaderByNumber(context.Background(), rpc.BlockNumber(blockNum))
   199  
   200  		assert.Nil(t, header)
   201  		assert.Error(t, err)
   202  
   203  		mockCtrl.Finish()
   204  	}
   205  	{
   206  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   207  		mockBlockChain.EXPECT().GetHeaderByNumber(blockNum).Return(expectedHeader).Times(1)
   208  
   209  		header, err := api.HeaderByNumber(context.Background(), rpc.BlockNumber(blockNum))
   210  
   211  		assert.Equal(t, expectedHeader, header)
   212  		assert.NoError(t, err)
   213  
   214  		mockCtrl.Finish()
   215  	}
   216  }
   217  
   218  func TestCNAPIBackend_HeaderByNumberOrHash(t *testing.T) {
   219  	block := newBlock(123)
   220  	expectedHeader := block.Header()
   221  	{
   222  		mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
   223  		mockMiner.EXPECT().PendingBlock().Return(block).Times(1)
   224  
   225  		header, err := api.HeaderByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.PendingBlockNumber))
   226  
   227  		assert.Equal(t, expectedHeader, header)
   228  		assert.NoError(t, err)
   229  
   230  		mockCtrl.Finish()
   231  	}
   232  	{
   233  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   234  		mockBlockChain.EXPECT().CurrentBlock().Return(block).Times(1)
   235  
   236  		header, err := api.HeaderByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
   237  
   238  		assert.Equal(t, expectedHeader, header)
   239  		assert.NoError(t, err)
   240  
   241  		mockCtrl.Finish()
   242  	}
   243  	{
   244  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   245  		mockBlockChain.EXPECT().GetHeaderByNumber(uint64(123)).Return(expectedHeader).Times(1)
   246  
   247  		header, err := api.HeaderByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.BlockNumber(123)))
   248  
   249  		assert.Equal(t, expectedHeader, header)
   250  		assert.NoError(t, err)
   251  
   252  		mockCtrl.Finish()
   253  	}
   254  	{
   255  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   256  		mockBlockChain.EXPECT().GetHeaderByHash(hash1).Return(expectedHeader).Times(1)
   257  
   258  		header, err := api.HeaderByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithHash(hash1, false))
   259  
   260  		assert.Equal(t, expectedHeader, header)
   261  		assert.NoError(t, err)
   262  
   263  		mockCtrl.Finish()
   264  	}
   265  }
   266  
   267  func TestCNAPIBackend_HeaderByHash(t *testing.T) {
   268  	{
   269  		blockNum := uint64(123)
   270  		block := newBlock(int(blockNum))
   271  		expectedHeader := block.Header()
   272  
   273  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   274  		mockBlockChain.EXPECT().GetHeaderByHash(hash1).Return(expectedHeader).Times(1)
   275  
   276  		header, err := api.HeaderByHash(context.Background(), hash1)
   277  
   278  		assert.Equal(t, expectedHeader, header)
   279  		assert.NoError(t, err)
   280  
   281  		mockCtrl.Finish()
   282  	}
   283  }
   284  
   285  func TestCNAPIBackend_BlockByNumber(t *testing.T) {
   286  	blockNum := uint64(123)
   287  	block := newBlock(int(blockNum))
   288  	expectedBlock := block
   289  	{
   290  		mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
   291  		mockMiner.EXPECT().PendingBlock().Return(block).Times(1)
   292  
   293  		block, err := api.BlockByNumber(context.Background(), rpc.PendingBlockNumber)
   294  
   295  		assert.Equal(t, expectedBlock, block)
   296  		assert.NoError(t, err)
   297  
   298  		mockCtrl.Finish()
   299  	}
   300  	{
   301  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   302  		mockBlockChain.EXPECT().CurrentBlock().Return(block).Times(1)
   303  
   304  		block, err := api.BlockByNumber(context.Background(), rpc.LatestBlockNumber)
   305  
   306  		assert.Equal(t, expectedBlock, block)
   307  		assert.NoError(t, err)
   308  
   309  		mockCtrl.Finish()
   310  	}
   311  	{
   312  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   313  		mockBlockChain.EXPECT().GetBlockByNumber(blockNum).Return(nil).Times(1)
   314  
   315  		block, err := api.BlockByNumber(context.Background(), rpc.BlockNumber(blockNum))
   316  
   317  		assert.Nil(t, block)
   318  		assert.Error(t, err)
   319  
   320  		mockCtrl.Finish()
   321  	}
   322  	{
   323  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   324  		mockBlockChain.EXPECT().GetBlockByNumber(blockNum).Return(expectedBlock).Times(1)
   325  
   326  		block, err := api.BlockByNumber(context.Background(), rpc.BlockNumber(blockNum))
   327  
   328  		assert.Equal(t, expectedBlock, block)
   329  		assert.NoError(t, err)
   330  
   331  		mockCtrl.Finish()
   332  	}
   333  }
   334  
   335  func TestCNAPIBackend_BlockByNumberOrHash(t *testing.T) {
   336  	blockNum := uint64(123)
   337  	block := newBlock(int(blockNum))
   338  	expectedBlock := block
   339  	{
   340  		mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
   341  		mockMiner.EXPECT().PendingBlock().Return(block).Times(1)
   342  
   343  		block, err := api.BlockByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.PendingBlockNumber))
   344  
   345  		assert.Equal(t, expectedBlock, block)
   346  		assert.NoError(t, err)
   347  
   348  		mockCtrl.Finish()
   349  	}
   350  	{
   351  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   352  		mockBlockChain.EXPECT().CurrentBlock().Return(expectedBlock).Times(1)
   353  
   354  		block, err := api.BlockByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
   355  
   356  		assert.Equal(t, expectedBlock, block)
   357  		assert.NoError(t, err)
   358  
   359  		mockCtrl.Finish()
   360  	}
   361  	{
   362  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   363  		mockBlockChain.EXPECT().GetBlockByNumber(uint64(123)).Return(nil).Times(1)
   364  
   365  		block, err := api.BlockByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithNumber(rpc.BlockNumber(123)))
   366  
   367  		assert.Nil(t, block)
   368  		assert.Error(t, err)
   369  
   370  		mockCtrl.Finish()
   371  	}
   372  	{
   373  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   374  		mockBlockChain.EXPECT().GetBlockByHash(hash1).Return(expectedBlock).Times(1)
   375  
   376  		block, err := api.BlockByNumberOrHash(context.Background(), rpc.NewBlockNumberOrHashWithHash(hash1, false))
   377  
   378  		assert.Equal(t, expectedBlock, block)
   379  		assert.NoError(t, err)
   380  
   381  		mockCtrl.Finish()
   382  	}
   383  }
   384  
   385  func TestCNAPIBackend_StateAndHeaderByNumber(t *testing.T) {
   386  	blockNum := uint64(123)
   387  	block := newBlock(int(blockNum))
   388  
   389  	stateDB, err := state.New(common.Hash{}, state.NewDatabase(database.NewMemoryDBManager()), nil)
   390  	if err != nil {
   391  		t.Fatal(err)
   392  	}
   393  	stateDB.SetNonce(addrs[0], 123)
   394  	stateDB.SetNonce(addrs[1], 321)
   395  
   396  	expectedHeader := block.Header()
   397  	{
   398  		mockCtrl, _, mockMiner, api := newCNAPIBackend(t)
   399  		mockMiner.EXPECT().Pending().Return(block, stateDB).Times(1)
   400  
   401  		returnedStateDB, header, err := api.StateAndHeaderByNumber(context.Background(), rpc.PendingBlockNumber)
   402  
   403  		assert.Equal(t, stateDB, returnedStateDB)
   404  		assert.Equal(t, expectedHeader, header)
   405  		assert.NoError(t, err)
   406  
   407  		mockCtrl.Finish()
   408  	}
   409  	{
   410  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   411  
   412  		mockBlockChain.EXPECT().GetHeaderByNumber(blockNum).Return(nil).Times(1)
   413  		returnedStateDB, header, err := api.StateAndHeaderByNumber(context.Background(), rpc.BlockNumber(blockNum))
   414  
   415  		assert.Nil(t, returnedStateDB)
   416  		assert.Nil(t, header)
   417  		assert.Error(t, err)
   418  
   419  		mockCtrl.Finish()
   420  	}
   421  	{
   422  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   423  
   424  		mockBlockChain.EXPECT().GetHeaderByNumber(blockNum).Return(expectedHeader).Times(1)
   425  		mockBlockChain.EXPECT().StateAt(expectedHeader.Root).Return(stateDB, nil).Times(1)
   426  		returnedStateDB, header, err := api.StateAndHeaderByNumber(context.Background(), rpc.BlockNumber(blockNum))
   427  
   428  		assert.Equal(t, stateDB, returnedStateDB)
   429  		assert.Equal(t, expectedHeader, header)
   430  		assert.NoError(t, err)
   431  
   432  		mockCtrl.Finish()
   433  	}
   434  }
   435  
   436  func TestCNAPIBackend_GetBlock(t *testing.T) {
   437  	block := newBlock(123)
   438  	hash := hashes[0]
   439  	{
   440  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   441  		mockBlockChain.EXPECT().GetBlockByHash(hash).Return(nil).Times(1)
   442  
   443  		returnedBlock, err := api.BlockByHash(context.Background(), hash)
   444  		assert.Nil(t, returnedBlock)
   445  		assert.Error(t, err)
   446  
   447  		mockCtrl.Finish()
   448  	}
   449  	{
   450  		mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   451  		mockBlockChain.EXPECT().GetBlockByHash(hash).Return(block).Times(1)
   452  
   453  		returnedBlock, err := api.BlockByHash(context.Background(), hash)
   454  		assert.Equal(t, block, returnedBlock)
   455  		assert.NoError(t, err)
   456  
   457  		mockCtrl.Finish()
   458  	}
   459  }
   460  
   461  func TestCNAPIBackend_GetTxAndLookupInfo(t *testing.T) {
   462  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   463  	defer mockCtrl.Finish()
   464  
   465  	hash := hashes[0]
   466  
   467  	expectedTx := tx1
   468  	expectedBlockHash := hashes[1]
   469  	expectedBlockNum := uint64(111)
   470  	expectedIndex := uint64(999)
   471  
   472  	mockBlockChain.EXPECT().GetTxAndLookupInfo(hash).Times(1).Return(expectedTx, expectedBlockHash, expectedBlockNum, expectedIndex)
   473  	tx, blockHash, blockNumber, index := api.GetTxAndLookupInfo(hash)
   474  
   475  	assert.Equal(t, expectedTx, tx)
   476  	assert.Equal(t, expectedBlockHash, blockHash)
   477  	assert.Equal(t, expectedBlockNum, blockNumber)
   478  	assert.Equal(t, expectedIndex, index)
   479  }
   480  
   481  func TestCNAPIBackend_GetBlockReceipts(t *testing.T) {
   482  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   483  	defer mockCtrl.Finish()
   484  
   485  	hash := hashes[0]
   486  	expectedReceipts := types.Receipts{newReceipt(111), newReceipt(222)}
   487  
   488  	mockBlockChain.EXPECT().GetReceiptsByBlockHash(hash).Return(expectedReceipts).Times(1)
   489  
   490  	assert.Equal(t, expectedReceipts, api.GetBlockReceipts(context.Background(), hash))
   491  }
   492  
   493  func TestCNAPIBackend_GetLogs(t *testing.T) {
   494  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   495  	defer mockCtrl.Finish()
   496  
   497  	hash := hashes[0]
   498  	expectedLogs := [][]*types.Log{{{BlockNumber: 123}}, {{BlockNumber: 321}}}
   499  	mockBlockChain.EXPECT().GetLogsByHash(hash).Return(expectedLogs).Times(1)
   500  
   501  	logs, err := api.GetLogs(context.Background(), hash)
   502  	assert.Equal(t, expectedLogs, logs)
   503  	assert.NoError(t, err)
   504  }
   505  
   506  func TestCNAPIBackend_GetTd(t *testing.T) {
   507  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   508  	defer mockCtrl.Finish()
   509  
   510  	td := big.NewInt(123)
   511  	hash := hashes[0]
   512  	mockBlockChain.EXPECT().GetTdByHash(hash).Return(td).Times(1)
   513  
   514  	assert.Equal(t, td, api.GetTd(hash))
   515  }
   516  
   517  func TestCNAPIBackend_SubscribeEvents(t *testing.T) {
   518  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   519  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   520  	api.cn.txPool = mockTxPool
   521  	defer mockCtrl.Finish()
   522  
   523  	rmCh := make(chan<- blockchain.RemovedLogsEvent)
   524  	ceCh := make(chan<- blockchain.ChainEvent)
   525  	chCh := make(chan<- blockchain.ChainHeadEvent)
   526  	csCh := make(chan<- blockchain.ChainSideEvent)
   527  	leCh := make(chan<- []*types.Log)
   528  	txCh := make(chan<- blockchain.NewTxsEvent)
   529  
   530  	sub := mocks3.NewMockSubscription(mockCtrl)
   531  
   532  	mockBlockChain.EXPECT().SubscribeRemovedLogsEvent(rmCh).Return(sub).Times(1)
   533  	mockBlockChain.EXPECT().SubscribeChainEvent(ceCh).Return(sub).Times(1)
   534  	mockBlockChain.EXPECT().SubscribeChainHeadEvent(chCh).Return(sub).Times(1)
   535  	mockBlockChain.EXPECT().SubscribeChainSideEvent(csCh).Return(sub).Times(1)
   536  	mockBlockChain.EXPECT().SubscribeLogsEvent(leCh).Return(sub).Times(1)
   537  
   538  	mockTxPool.EXPECT().SubscribeNewTxsEvent(txCh).Return(sub).Times(1)
   539  
   540  	assert.Equal(t, sub, api.SubscribeRemovedLogsEvent(rmCh))
   541  	assert.Equal(t, sub, api.SubscribeChainEvent(ceCh))
   542  	assert.Equal(t, sub, api.SubscribeChainHeadEvent(chCh))
   543  	assert.Equal(t, sub, api.SubscribeChainSideEvent(csCh))
   544  	assert.Equal(t, sub, api.SubscribeLogsEvent(leCh))
   545  
   546  	assert.Equal(t, sub, api.SubscribeNewTxsEvent(txCh))
   547  }
   548  
   549  func TestCNAPIBackend_SendTx(t *testing.T) {
   550  	mockCtrl, _, _, api := newCNAPIBackend(t)
   551  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   552  	mockTxPool.EXPECT().AddLocal(tx1).Return(expectedErr).Times(1)
   553  	api.cn.txPool = mockTxPool
   554  
   555  	defer mockCtrl.Finish()
   556  
   557  	assert.Equal(t, expectedErr, api.SendTx(context.Background(), tx1))
   558  }
   559  
   560  func TestCNAPIBackend_GetPoolTransactions(t *testing.T) {
   561  	{
   562  		mockCtrl, _, _, api := newCNAPIBackend(t)
   563  		mockTxPool := mocks.NewMockTxPool(mockCtrl)
   564  		mockTxPool.EXPECT().Pending().Return(nil, expectedErr).Times(1)
   565  		api.cn.txPool = mockTxPool
   566  
   567  		txs, ReturnedErr := api.GetPoolTransactions()
   568  		assert.Nil(t, txs)
   569  		assert.Equal(t, expectedErr, ReturnedErr)
   570  		mockCtrl.Finish()
   571  	}
   572  	{
   573  		mockCtrl, _, _, api := newCNAPIBackend(t)
   574  		mockTxPool := mocks.NewMockTxPool(mockCtrl)
   575  
   576  		pendingTxs := map[common.Address]types.Transactions{addrs[0]: {tx1}}
   577  		mockTxPool.EXPECT().Pending().Return(pendingTxs, nil).Times(1)
   578  		api.cn.txPool = mockTxPool
   579  
   580  		txs, ReturnedErr := api.GetPoolTransactions()
   581  		assert.Equal(t, types.Transactions{tx1}, txs)
   582  		assert.NoError(t, ReturnedErr)
   583  		mockCtrl.Finish()
   584  	}
   585  }
   586  
   587  func TestCNAPIBackend_GetPoolTransaction(t *testing.T) {
   588  	hash := hashes[0]
   589  
   590  	mockCtrl, _, _, api := newCNAPIBackend(t)
   591  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   592  	mockTxPool.EXPECT().Get(hash).Return(tx1).Times(1)
   593  	api.cn.txPool = mockTxPool
   594  
   595  	defer mockCtrl.Finish()
   596  
   597  	assert.Equal(t, tx1, api.GetPoolTransaction(hash))
   598  }
   599  
   600  func TestCNAPIBackend_GetPoolNonce(t *testing.T) {
   601  	addr := addrs[0]
   602  	nonce := uint64(123)
   603  
   604  	mockCtrl, _, _, api := newCNAPIBackend(t)
   605  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   606  	mockTxPool.EXPECT().GetPendingNonce(addr).Return(nonce).Times(1)
   607  	api.cn.txPool = mockTxPool
   608  
   609  	defer mockCtrl.Finish()
   610  
   611  	assert.Equal(t, nonce, api.GetPoolNonce(context.Background(), addr))
   612  }
   613  
   614  func TestCNAPIBackend_Stats(t *testing.T) {
   615  	pending := 123
   616  	queued := 321
   617  
   618  	mockCtrl, _, _, api := newCNAPIBackend(t)
   619  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   620  	mockTxPool.EXPECT().Stats().Return(pending, queued).Times(1)
   621  	api.cn.txPool = mockTxPool
   622  
   623  	defer mockCtrl.Finish()
   624  
   625  	p, q := api.Stats()
   626  	assert.Equal(t, pending, p)
   627  	assert.Equal(t, queued, q)
   628  }
   629  
   630  func TestCNAPIBackend_TxPoolContent(t *testing.T) {
   631  	pending := map[common.Address]types.Transactions{addrs[0]: {tx1}}
   632  	queued := map[common.Address]types.Transactions{addrs[1]: {tx1}}
   633  
   634  	mockCtrl, _, _, api := newCNAPIBackend(t)
   635  	mockTxPool := mocks.NewMockTxPool(mockCtrl)
   636  	mockTxPool.EXPECT().Content().Return(pending, queued).Times(1)
   637  	api.cn.txPool = mockTxPool
   638  
   639  	defer mockCtrl.Finish()
   640  
   641  	p, q := api.TxPoolContent()
   642  	assert.Equal(t, pending, p)
   643  	assert.Equal(t, queued, q)
   644  }
   645  
   646  func TestCNAPIBackend_IsParallelDBWrite(t *testing.T) {
   647  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   648  	defer mockCtrl.Finish()
   649  
   650  	mockBlockChain.EXPECT().IsParallelDBWrite().Return(true).Times(1)
   651  	assert.True(t, api.IsParallelDBWrite())
   652  }
   653  
   654  func TestCNAPIBackend_IsSenderTxHashIndexingEnabled(t *testing.T) {
   655  	mockCtrl, mockBlockChain, _, api := newCNAPIBackend(t)
   656  	defer mockCtrl.Finish()
   657  
   658  	mockBlockChain.EXPECT().IsSenderTxHashIndexingEnabled().Return(true).Times(1)
   659  	assert.True(t, api.IsSenderTxHashIndexingEnabled())
   660  }