github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/consensus/dpos/bft_test.go (about) 1 // Copyright 2019 The go-vnt Authors 2 // This file is part of the go-vnt library. 3 // 4 // The go-vnt 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 go-vnt 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 go-vnt library. If not, see <http://www.gnu.org/licenses/>. 16 17 package dpos 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/vntchain/go-vnt/common" 24 "github.com/vntchain/go-vnt/core/types" 25 "github.com/vntchain/go-vnt/params" 26 ) 27 28 func newDefaultBft() *BftManager { 29 cfg := ¶ms.DposConfig{ 30 WitnessesNum: 4, 31 Period: 2, 32 } 33 34 dp := New(cfg, nil) 35 return newBftManager(dp) 36 } 37 38 func TestHandleBftMsg_FutureMsg(t *testing.T) { 39 bft := newDefaultBft() 40 41 // set state 42 bft.h = big.NewInt(10) 43 bft.r = 0 44 bft.producing = 1 45 46 tests := []struct { 47 h int64 48 r int 49 }{{h: 100, r: 0}, {10, 10}} 50 51 for i, te := range tests { 52 // Create msg 53 h1 := big.NewInt(te.h) 54 r := uint32(te.r) 55 hd1 := &types.Header{ 56 Number: h1, 57 } 58 b1 := types.NewBlockWithHeader(hd1) 59 prepre := &types.PreprepareMsg{ 60 Block: b1, 61 Round: r, 62 } 63 64 t.Logf("header hash:%x", b1.Hash()) 65 66 if err := bft.handleBftMsg(prepre); err != nil { 67 t.Errorf("Should success. handle msg error: %s", err) 68 } 69 70 // Msg should in msg pool 71 if msg, err := bft.mp.getPrePrepareMsg(h1, prepre.Round); err != nil { 72 t.Error("Pre-prepare msg should in msg pool, but not find") 73 } else if msg.Hash() != prepre.Hash() { 74 t.Errorf("Pre-prepare msg want: %x, got:%x", hd1.Hash(), msg.Hash()) 75 } 76 t.Logf("test case: %d passed", i) 77 } 78 } 79 80 func TestHandleBftMsg_OutdatedMsg(t *testing.T) { 81 bft := newDefaultBft() 82 83 // set state 84 bft.h = big.NewInt(10) 85 bft.r = 1 86 bft.producing = 1 87 88 tests := []struct { 89 h int64 90 r int 91 }{{h: 2, r: 0}, {10, 0}} 92 93 for i, te := range tests { 94 // Create msg 95 h1 := big.NewInt(te.h) 96 r := uint32(te.r) 97 hd1 := &types.Header{ 98 Number: h1, 99 } 100 b1 := types.NewBlockWithHeader(hd1) 101 prepre := &types.PreprepareMsg{ 102 Block: b1, 103 Round: r, 104 } 105 106 t.Logf("header hash:%x", b1.Hash()) 107 108 if err := bft.handleBftMsg(prepre); err == nil { 109 t.Error("Should handle msg error, but success") 110 } 111 112 // Msg should in msg pool 113 if msg, err := bft.mp.getPrePrepareMsg(h1, prepre.Round); err == nil { 114 t.Errorf("Pre-prepare msg should not in msg pool, but find: %x", msg.Hash()) 115 } 116 t.Logf("test case: %d passed", i) 117 } 118 } 119 120 func TestHandleBftMsg_NoProducing(t *testing.T) { 121 bft := newDefaultBft() 122 123 // set state 124 bft.h = big.NewInt(10) 125 bft.r = 1 126 bft.producing = 0 127 128 // Create msg 129 h1 := big.NewInt(10) 130 r := uint32(1) 131 hd1 := &types.Header{ 132 Number: h1, 133 } 134 b1 := types.NewBlockWithHeader(hd1) 135 prepre := &types.PreprepareMsg{ 136 Block: b1, 137 Round: r, 138 } 139 140 t.Logf("header hash:%x", b1.Hash()) 141 142 if err := bft.handleBftMsg(prepre); err != nil { 143 t.Errorf("Should success. handle msg error: %s", err) 144 } 145 146 // Msg should in msg pool 147 if msg, err := bft.mp.getPrePrepareMsg(h1, prepre.Round); err != nil { 148 t.Error("Pre-prepare msg should in msg pool, but not find") 149 } else if msg.Hash() != prepre.Hash() { 150 t.Errorf("Pre-prepare msg want: %x, got:%x", hd1.Hash(), msg.Hash()) 151 } 152 } 153 154 func Test_ValidWitness(t *testing.T) { 155 bft := newDefaultBft() 156 addrStr := []string{ 157 "0x122369f04f32269598789998de33e3d56e2c507a", 158 "0x42a875ac43f2b4e6d17f54d288071f5952bf8911", 159 "0x3dcf0b3787c31b2bdf62d5bc9128a79c2bb18829", 160 "0xbf66d398226f200467cd27b14e85b25a8c232384", 161 } 162 163 addrs := make([]common.Address, len(addrStr)) 164 for i, st := range addrStr { 165 addrs[i] = common.HexToAddress(st) 166 bft.witnessList[addrs[i]] = struct{}{} 167 } 168 169 for i, addr := range addrs { 170 if bft.validWitness(addr) == false { 171 t.Errorf("%dth, addr should in list, but not. addr = %x", i, addr.Hex()) 172 } 173 } 174 175 invalidAddrStr := []string{ 176 "0x122369f04f32269598789998de33e3d56e2c50DD", 177 "0x0000000000000000000000000000000000000000", 178 } 179 for i, addr := range invalidAddrStr { 180 if bft.validWitness(common.HexToAddress(addr)) == true { 181 t.Errorf("%dth, addr should not in list, but in. addr = %s", i, addr) 182 } 183 } 184 }