github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/consensusmgr/block_fetcher_test.go (about) 1 package consensusmgr 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/bytom/bytom/netsync/peers" 8 "github.com/bytom/bytom/protocol/bc" 9 "github.com/bytom/bytom/protocol/bc/types" 10 "github.com/bytom/bytom/protocol/casper" 11 ) 12 13 type peerMgr struct { 14 } 15 16 func (pm *peerMgr) IsBanned(ip string, level byte, reason string) bool { 17 return false 18 } 19 20 func (pm *peerMgr) StopPeerGracefully(string) { 21 return 22 } 23 24 type chain struct { 25 blocks []uint64 26 } 27 28 func newChain() *chain { 29 blocks := make([]uint64, 1, 1) 30 blocks[0] = 99 31 return &chain{ 32 blocks: blocks, 33 } 34 } 35 36 func (c *chain) BestBlockHeight() uint64 { 37 return c.blocks[len(c.blocks)-1] 38 } 39 40 func (c *chain) GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error) { 41 return nil, nil 42 } 43 44 func (c *chain) ProcessBlock(block *types.Block) (bool, error) { 45 c.blocks = append(c.blocks, block.Height) 46 return false, nil 47 } 48 49 func (c *chain) ProcessBlockVerification(*casper.ValidCasperSignMsg) error { 50 return nil 51 } 52 53 func TestBlockFetcher(t *testing.T) { 54 peers := peers.NewPeerSet(&peerMgr{}) 55 testCase := []struct { 56 blockMsg *blockMsg 57 height uint64 58 }{ 59 { 60 blockMsg: &blockMsg{ 61 block: &types.Block{ 62 BlockHeader: types.BlockHeader{ 63 Height: 100, 64 }, 65 }, 66 }, 67 height: 100, 68 }, 69 { 70 blockMsg: &blockMsg{ 71 block: &types.Block{ 72 BlockHeader: types.BlockHeader{ 73 Height: 101, 74 }, 75 }, 76 }, 77 height: 101, 78 }, 79 { 80 blockMsg: &blockMsg{ 81 block: &types.Block{ 82 BlockHeader: types.BlockHeader{ 83 Height: 105, 84 }, 85 }, 86 }, 87 height: 101, 88 }, 89 { 90 blockMsg: &blockMsg{ 91 block: &types.Block{ 92 BlockHeader: types.BlockHeader{ 93 Height: 200, 94 }, 95 }, 96 }, 97 height: 101, 98 }, 99 { 100 blockMsg: &blockMsg{ 101 block: &types.Block{ 102 BlockHeader: types.BlockHeader{ 103 Height: 104, 104 }, 105 }, 106 }, 107 height: 101, 108 }, 109 { 110 blockMsg: &blockMsg{ 111 block: &types.Block{ 112 BlockHeader: types.BlockHeader{ 113 Height: 103, 114 }, 115 }, 116 }, 117 height: 101, 118 }, 119 { 120 blockMsg: &blockMsg{ 121 block: &types.Block{ 122 BlockHeader: types.BlockHeader{ 123 Height: 102, 124 }, 125 }, 126 }, 127 height: 105, 128 }, 129 } 130 fetcher := newBlockFetcher(newChain(), peers) 131 go fetcher.blockProcessorLoop() 132 for i, c := range testCase { 133 fetcher.processNewBlock(c.blockMsg) 134 time.Sleep(10 * time.Millisecond) 135 chainHeight := fetcher.chain.BestBlockHeight() 136 if chainHeight != c.height { 137 t.Fatalf("test block fetcher error. index %d expected chain height %d but got %d", i, chainHeight, c.height) 138 } 139 } 140 } 141 142 func TestAddBlockMsg(t *testing.T) { 143 peers := peers.NewPeerSet(&peerMgr{}) 144 testPeer := "peer1" 145 testCase := []struct { 146 blocksMsg []*blockMsg 147 limit int 148 queueSize int 149 msgSetSize int 150 msgCounter int 151 }{ 152 //normal test 153 { 154 blocksMsg: []*blockMsg{ 155 { 156 block: &types.Block{ 157 BlockHeader: types.BlockHeader{ 158 Height: 100, 159 }, 160 }, 161 peerID: testPeer, 162 }, 163 { 164 block: &types.Block{ 165 BlockHeader: types.BlockHeader{ 166 Height: 101, 167 }, 168 }, 169 peerID: testPeer, 170 }, 171 { 172 block: &types.Block{ 173 BlockHeader: types.BlockHeader{ 174 Height: 102, 175 }, 176 }, 177 peerID: testPeer, 178 }, 179 }, 180 limit: 5, 181 queueSize: 3, 182 msgSetSize: 3, 183 msgCounter: 3, 184 }, 185 // test DOS 186 { 187 blocksMsg: []*blockMsg{ 188 { 189 block: &types.Block{ 190 BlockHeader: types.BlockHeader{ 191 Version: 1, 192 Height: 100, 193 }, 194 }, 195 peerID: testPeer, 196 }, 197 { 198 block: &types.Block{ 199 BlockHeader: types.BlockHeader{ 200 Version: 2, 201 Height: 100, 202 }, 203 }, 204 peerID: testPeer, 205 }, 206 { 207 block: &types.Block{ 208 BlockHeader: types.BlockHeader{ 209 Version: 3, 210 Height: 100, 211 }, 212 }, 213 peerID: testPeer, 214 }, 215 { 216 block: &types.Block{ 217 BlockHeader: types.BlockHeader{ 218 Version: 4, 219 Height: 100, 220 }, 221 }, 222 peerID: testPeer, 223 }, 224 }, 225 limit: 3, 226 queueSize: 3, 227 msgSetSize: 3, 228 msgCounter: 3, 229 }, 230 231 // test msg height does not meet the requirements 232 { 233 blocksMsg: []*blockMsg{ 234 { 235 block: &types.Block{ 236 BlockHeader: types.BlockHeader{ 237 Version: 1, 238 Height: 98, 239 }, 240 }, 241 peerID: testPeer, 242 }, 243 { 244 block: &types.Block{ 245 BlockHeader: types.BlockHeader{ 246 Version: 2, 247 Height: 97, 248 }, 249 }, 250 peerID: testPeer, 251 }, 252 { 253 block: &types.Block{ 254 BlockHeader: types.BlockHeader{ 255 Version: 3, 256 Height: 164, 257 }, 258 }, 259 peerID: testPeer, 260 }, 261 { 262 block: &types.Block{ 263 BlockHeader: types.BlockHeader{ 264 Version: 4, 265 Height: 165, 266 }, 267 }, 268 peerID: testPeer, 269 }, 270 }, 271 limit: 5, 272 queueSize: 0, 273 msgSetSize: 0, 274 msgCounter: 0, 275 }, 276 } 277 278 for i, c := range testCase { 279 fetcher := newBlockFetcher(newChain(), peers) 280 for _, msg := range c.blocksMsg { 281 fetcher.add(msg, c.limit) 282 } 283 284 if fetcher.queue.Size() != c.queueSize { 285 t.Fatalf("index: %d queue size err got %d: want %d", i, fetcher.queue.Size(), c.queueSize) 286 } 287 288 if len(fetcher.msgSet) != c.msgSetSize { 289 t.Fatalf("index: %d msg set size err got %d: want %d", i, len(fetcher.msgSet), c.msgSetSize) 290 } 291 292 if fetcher.msgCounter[testPeer] != c.msgCounter { 293 t.Fatalf("index: %d peer msg counter err got %d: want %d", i, fetcher.msgCounter[testPeer], c.msgCounter) 294 } 295 } 296 }