github.com/klaytn/klaytn@v1.12.1/node/cn/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 "testing" 21 "time" 22 23 "github.com/golang/mock/gomock" 24 "github.com/klaytn/klaytn/blockchain/types" 25 "github.com/klaytn/klaytn/datasync/downloader" 26 "github.com/klaytn/klaytn/node/cn/mocks" 27 "github.com/klaytn/klaytn/params" 28 mocks2 "github.com/klaytn/klaytn/work/mocks" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func newCN(t *testing.T) (*gomock.Controller, *MockBackendProtocolManager, *mocks.MockMiner, *CN) { 33 mockCtrl := gomock.NewController(t) 34 mockProtocolManager := NewMockBackendProtocolManager(mockCtrl) 35 mockMiner := mocks.NewMockMiner(mockCtrl) 36 return mockCtrl, mockProtocolManager, mockMiner, 37 &CN{protocolManager: mockProtocolManager, miner: mockMiner} 38 } 39 40 func TestCN_AddLesServer(t *testing.T) { 41 mockCtrl := gomock.NewController(t) 42 defer mockCtrl.Finish() 43 cn := &CN{} 44 les := mocks.NewMockLesServer(mockCtrl) 45 les.EXPECT().SetBloomBitsIndexer(nil).Times(1) 46 cn.AddLesServer(les) 47 } 48 49 func TestCN_CheckSyncMode(t *testing.T) { 50 c := &Config{SyncMode: downloader.FastSync} 51 assert.NoError(t, checkSyncMode(c)) 52 53 c.SyncMode = downloader.FullSync 54 assert.NoError(t, checkSyncMode(c)) 55 56 c.SyncMode = downloader.LightSync 57 assert.Equal(t, errCNLightSync, checkSyncMode(c)) 58 } 59 60 func TestCN_SetEngineType(t *testing.T) { 61 cc := ¶ms.ChainConfig{} 62 originalEngineType := types.EngineType 63 64 setEngineType(cc) 65 assert.Equal(t, originalEngineType, types.EngineType) 66 67 cc.Clique = ¶ms.CliqueConfig{} 68 setEngineType(cc) 69 assert.Equal(t, types.Engine_Clique, types.EngineType) 70 71 cc.Istanbul = ¶ms.IstanbulConfig{} 72 setEngineType(cc) 73 assert.Equal(t, types.Engine_IBFT, types.EngineType) 74 } 75 76 func TestCN_SetAcceptTxs(t *testing.T) { 77 { 78 mockCtrl, _, _, cn := newCN(t) 79 cn.chainConfig = ¶ms.ChainConfig{} 80 assert.NoError(t, cn.setAcceptTxs()) 81 mockCtrl.Finish() 82 } 83 } 84 85 func TestCN_ResetWithGenesisBlock(t *testing.T) { 86 mockCtrl, _, _, cn := newCN(t) 87 defer mockCtrl.Finish() 88 mockBlockChain := mocks2.NewMockBlockChain(mockCtrl) 89 cn.blockchain = mockBlockChain 90 block := blocks[0] 91 92 mockBlockChain.EXPECT().ResetWithGenesisBlock(block).Times(1) 93 cn.ResetWithGenesisBlock(block) 94 } 95 96 func TestCN_Rewardbase(t *testing.T) { 97 { 98 mockCtrl, _, _, cn := newCN(t) 99 cn.rewardbase = addrs[0] 100 101 rb, err := cn.Rewardbase() 102 assert.Equal(t, addrs[0], rb) 103 assert.Nil(t, err) 104 105 mockCtrl.Finish() 106 } 107 } 108 109 func TestCN_StartMining(t *testing.T) { 110 { 111 mockCtrl, _, mockMiner, cn := newCN(t) 112 mockMiner.EXPECT().Start().Times(1) 113 assert.Nil(t, cn.StartMining(false)) 114 time.Sleep(100 * time.Millisecond) 115 mockCtrl.Finish() 116 } 117 { 118 mockCtrl, mockPM, mockMiner, cn := newCN(t) 119 mockPM.EXPECT().SetAcceptTxs().Times(1) 120 mockMiner.EXPECT().Start().Times(1) 121 assert.Nil(t, cn.StartMining(true)) 122 time.Sleep(100 * time.Millisecond) 123 mockCtrl.Finish() 124 } 125 } 126 127 func TestCN_StopMining(t *testing.T) { 128 mockCtrl, _, mockMiner, cn := newCN(t) 129 mockMiner.EXPECT().Stop().Times(1) 130 cn.StopMining() 131 mockCtrl.Finish() 132 } 133 134 func TestCN_IsMining(t *testing.T) { 135 mockCtrl, _, mockMiner, cn := newCN(t) 136 mockMiner.EXPECT().Mining().Times(1) 137 cn.IsMining() 138 mockCtrl.Finish() 139 } 140 141 func TestCN_ReBroadcastTxs(t *testing.T) { 142 mockCtrl, mockPM, _, cn := newCN(t) 143 defer mockCtrl.Finish() 144 txs := types.Transactions{tx1} 145 mockPM.EXPECT().ReBroadcastTxs(txs).Times(1) 146 cn.ReBroadcastTxs(txs) 147 }