6 "github.com/vapor/protocol/bc"
7 "github.com/vapor/protocol/bc/types"
10 func TestBlockCache(t *testing.T) {
11 newBlock := func(h uint64) *types.Block {
13 BlockHeader: types.BlockHeader{
18 blocks := make(map[bc.Hash]*types.Block)
19 for i := 0; i < maxCachedBlockHeaders+10; i++ {
20 block := newBlock(uint64(i))
21 blocks[block.Hash()] = block
24 fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
25 return &blocks[*hash].BlockHeader, nil
28 fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
29 return blocks[*hash].Transactions, nil
32 cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn)
34 for i := 0; i < maxCachedBlockHeaders+10; i++ {
35 block := newBlock(uint64(i))
37 cache.lookupBlockHeader(&hash, block.Height)
40 for i := 0; i < 10; i++ {
41 block := newBlock(uint64(i))
43 if b, _ := cache.getBlockHeader(&hash); b != nil {
44 t.Fatalf("find old block")
48 for i := 10; i < maxCachedBlockHeaders+10; i++ {
49 block := newBlock(uint64(i))
51 if b, _ := cache.getBlockHeader(&hash); b == nil {
52 t.Fatalf("can't find new block")