OSDN Git Service

V0.1 vote result cache (#182)
[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         "github.com/vapor/protocol/state"
9 )
10
11 func TestBlockCache(t *testing.T) {
12         newBlock := func(h uint64) *types.Block {
13                 return &types.Block{
14                         BlockHeader: types.BlockHeader{
15                                 Height: h,
16                         },
17                 }
18         }
19         newVoteResult := func(seq uint64) *state.VoteResult {
20                 return &state.VoteResult{
21                         Seq: seq,
22                 }
23         }
24         blocks := make(map[bc.Hash]*types.Block)
25         for i := 0; i < maxCachedBlockHeaders+10; i++ {
26                 block := newBlock(uint64(i))
27                 blocks[block.Hash()] = block
28         }
29         voteResults := make(map[uint64]*state.VoteResult)
30         for i := 0; i < maxCachedVoteResults+10; i++ {
31                 voteResult := newVoteResult(uint64(i))
32                 voteResults[voteResult.Seq] = voteResult
33         }
34
35         fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
36                 return &blocks[*hash].BlockHeader, nil
37         }
38
39         fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
40                 return blocks[*hash].Transactions, nil
41         }
42
43         fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
44                 return voteResults[seq], nil
45         }
46
47         cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
48
49         for i := 0; i < maxCachedBlockHeaders+10; i++ {
50                 block := newBlock(uint64(i))
51                 hash := block.Hash()
52                 cache.lookupBlockHeader(&hash, block.Height)
53         }
54
55         for i := 0; i < 10; i++ {
56                 block := newBlock(uint64(i))
57                 hash := block.Hash()
58                 if b, _ := cache.getBlockHeader(&hash); b != nil {
59                         t.Fatalf("find old block")
60                 }
61         }
62
63         for i := 10; i < maxCachedBlockHeaders+10; i++ {
64                 block := newBlock(uint64(i))
65                 hash := block.Hash()
66                 if b, _ := cache.getBlockHeader(&hash); b == nil {
67                         t.Fatalf("can't find new block")
68                 }
69         }
70
71         for i := 0; i < maxCachedVoteResults+10; i++ {
72                 voteResult := newVoteResult(uint64(i))
73                 cache.lookupVoteResult(voteResult.Seq)
74         }
75
76         for i := 0; i < 10; i++ {
77                 voteResult := newVoteResult(uint64(i))
78                 if v, _ := cache.getVoteResult(voteResult.Seq); v != nil {
79                         t.Fatalf("find old vote result")
80                 }
81         }
82
83         for i := 10; i < maxCachedVoteResults+10; i++ {
84                 voteResult := newVoteResult(uint64(i))
85                 if v, _ := cache.getVoteResult(voteResult.Seq); v == nil {
86                         t.Fatalf("can't find new vote result")
87                 }
88         }
89 }