github.com/klaytn/klaytn@v1.10.2/consensus/istanbul/core/prepare_test.go (about) 1 package core 2 3 import ( 4 "testing" 5 6 "github.com/golang/mock/gomock" 7 "github.com/klaytn/klaytn/blockchain/types" 8 "github.com/klaytn/klaytn/common" 9 "github.com/klaytn/klaytn/consensus/istanbul" 10 mock_istanbul "github.com/klaytn/klaytn/consensus/istanbul/mocks" 11 "github.com/klaytn/klaytn/fork" 12 "github.com/klaytn/klaytn/params" 13 ) 14 15 func TestCore_sendPrepare(t *testing.T) { 16 fork.SetHardForkBlockNumberConfig(¶ms.ChainConfig{}) 17 defer fork.ClearHardForkBlockNumberConfig() 18 19 validatorAddrs, validatorKeyMap := genValidators(6) 20 mockBackend, mockCtrl := newMockBackend(t, validatorAddrs) 21 22 istConfig := istanbul.DefaultConfig 23 istConfig.ProposerPolicy = istanbul.WeightedRandom 24 25 istCore := New(mockBackend, istConfig).(*core) 26 if err := istCore.Start(); err != nil { 27 t.Fatal(err) 28 } 29 defer istCore.Stop() 30 31 lastProposal, lastProposer := mockBackend.LastProposal() 32 proposal, err := genBlock(lastProposal.(*types.Block), validatorKeyMap[validatorAddrs[0]]) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 istCore.current.Preprepare = &istanbul.Preprepare{ 38 View: istCore.currentView(), 39 Proposal: proposal, 40 } 41 42 mockCtrl.Finish() 43 44 // invalid case - not committee 45 { 46 // Increase round number until the owner of istanbul.core is not a member of the committee 47 for istCore.valSet.CheckInSubList(lastProposal.Hash(), istCore.currentView(), istCore.Address()) { 48 istCore.current.round.Add(istCore.current.round, common.Big1) 49 istCore.valSet.CalcProposer(lastProposer, istCore.current.round.Uint64()) 50 } 51 52 mockCtrl := gomock.NewController(t) 53 mockBackend := mock_istanbul.NewMockBackend(mockCtrl) 54 mockBackend.EXPECT().Sign(gomock.Any()).Return(nil, nil).Times(0) 55 mockBackend.EXPECT().Broadcast(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(0) 56 57 istCore.backend = mockBackend 58 istCore.sendPrepare() 59 60 // methods of mockBackend should be executed given times 61 mockCtrl.Finish() 62 } 63 64 // valid case 65 { 66 // Increase round number until the owner of istanbul.core become a member of the committee 67 for !istCore.valSet.CheckInSubList(lastProposal.Hash(), istCore.currentView(), istCore.Address()) { 68 istCore.current.round.Add(istCore.current.round, common.Big1) 69 istCore.valSet.CalcProposer(lastProposer, istCore.current.round.Uint64()) 70 } 71 72 mockCtrl := gomock.NewController(t) 73 mockBackend := mock_istanbul.NewMockBackend(mockCtrl) 74 mockBackend.EXPECT().Sign(gomock.Any()).Return(nil, nil).Times(1) 75 mockBackend.EXPECT().Broadcast(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) 76 77 istCore.backend = mockBackend 78 istCore.sendPrepare() 79 80 // methods of mockBackend should be executed given times 81 mockCtrl.Finish() 82 } 83 }