OSDN Git Service

split block (#180)
[bytom/vapor.git] / database / cache_test.go
1 package database
2
3 import (
4         "testing"
5
6         "github.com/vapor/protocol/bc"
7         "github.com/vapor/protocol/bc/types"
8 )
9
10 func TestBlockCache(t *testing.T) {
11         newBlock := func(h uint64) *types.Block {
12                 return &types.Block{
13                         BlockHeader: types.BlockHeader{
14                                 Height: h,
15                         },
16                 }
17         }
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
22         }
23
24         fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
25                 return &blocks[*hash].BlockHeader, nil
26         }
27
28         fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
29                 return blocks[*hash].Transactions, nil
30         }
31
32         cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn)
33
34         for i := 0; i < maxCachedBlockHeaders+10; i++ {
35                 block := newBlock(uint64(i))
36                 hash := block.Hash()
37                 cache.lookupBlockHeader(&hash, block.Height)
38         }
39
40         for i := 0; i < 10; i++ {
41                 block := newBlock(uint64(i))
42                 hash := block.Hash()
43                 if b, _ := cache.getBlockHeader(&hash); b != nil {
44                         t.Fatalf("find old block")
45                 }
46         }
47
48         for i := 10; i < maxCachedBlockHeaders+10; i++ {
49                 block := newBlock(uint64(i))
50                 hash := block.Hash()
51                 if b, _ := cache.getBlockHeader(&hash); b == nil {
52                         t.Fatalf("can't find new block")
53                 }
54         }
55 }