OSDN Git Service

get BlockNode from database (#188)
[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
20         newVoteResult := func(seq uint64) *state.VoteResult {
21                 return &state.VoteResult{
22                         Seq: seq,
23                 }
24         }
25
26         blocks := make(map[bc.Hash]*types.Block)
27         blockHashes := make(map[uint64]*bc.Hash)
28         blockIndexHashes := make(map[uint64][]*bc.Hash)
29         for i := 0; i < maxCachedBlockHeaders+10; i++ {
30                 block := newBlock(uint64(i))
31                 hash := block.Hash()
32                 blocks[hash] = block
33                 blockHashes[block.Height] = &hash
34                 blockIndexHashes[block.Height] = append(blockIndexHashes[block.Height], &hash)
35         }
36
37         voteResults := make(map[uint64]*state.VoteResult)
38         for i := 0; i < maxCachedVoteResults+10; i++ {
39                 voteResult := newVoteResult(uint64(i))
40                 voteResults[voteResult.Seq] = voteResult
41         }
42
43         fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) {
44                 return &blocks[*hash].BlockHeader, nil
45         }
46
47         fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
48                 return blocks[*hash].Transactions, nil
49         }
50
51         fillBlockHashesFn := func(height uint64) ([]*bc.Hash, error) {
52                 return blockIndexHashes[height], nil
53         }
54
55         fillMainChainHashFn := func(height uint64) (*bc.Hash, error) {
56                 return blockHashes[height], nil
57         }
58
59         fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
60                 return voteResults[seq], nil
61         }
62
63         cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillBlockHashesFn, fillMainChainHashFn, fillVoteResultFn)
64         for i := 0; i < maxCachedBlockHeaders+10; i++ {
65                 block := newBlock(uint64(i))
66                 hash := block.Hash()
67                 cache.lookupBlockHeader(&hash)
68         }
69
70         for i := 0; i < 10; i++ {
71                 block := newBlock(uint64(i))
72                 hash := block.Hash()
73                 if _, ok := cache.lruBlockHeaders.Get(hash); ok {
74                         t.Fatalf("find old block header")
75                 }
76         }
77
78         for i := 10; i < maxCachedBlockHeaders+10; i++ {
79                 block := newBlock(uint64(i))
80                 hash := block.Hash()
81                 if _, ok := cache.lruBlockHeaders.Get(hash); !ok {
82                         t.Fatalf("can't find new block header")
83                 }
84         }
85
86         for i := 0; i < maxCachedBlockTransactions+10; i++ {
87                 block := newBlock(uint64(i))
88                 hash := block.Hash()
89                 cache.lookupBlockTxs(&hash)
90         }
91
92         for i := 0; i < 10; i++ {
93                 block := newBlock(uint64(i))
94                 hash := block.Hash()
95                 if _, ok := cache.lruBlockTxs.Get(hash); ok {
96                         t.Fatalf("find old block transactions")
97                 }
98         }
99
100         for i := 10; i < maxCachedBlockTransactions+10; i++ {
101                 block := newBlock(uint64(i))
102                 hash := block.Hash()
103                 if _, ok := cache.lruBlockTxs.Get(hash); !ok {
104                         t.Fatalf("can't find new block transactions")
105                 }
106         }
107
108         for i := 0; i < maxCachedBlockHashes+10; i++ {
109                 block := newBlock(uint64(i))
110                 cache.lookupBlockHashesByHeight(block.Height)
111         }
112
113         for i := 0; i < 10; i++ {
114                 block := newBlock(uint64(i))
115                 if _, ok := cache.lruBlockHashes.Get(block.Height); ok {
116                         t.Fatalf("find old block Hashes for specified height")
117                 }
118         }
119
120         for i := 10; i < maxCachedBlockHashes+10; i++ {
121                 block := newBlock(uint64(i))
122                 if _, ok := cache.lruBlockHashes.Get(block.Height); !ok {
123                         t.Fatalf("can't find new block Hashes for specified height")
124                 }
125         }
126
127         for i := 0; i < maxCachedMainChainHashes+10; i++ {
128                 block := newBlock(uint64(i))
129                 cache.lookupMainChainHash(block.Height)
130         }
131
132         for i := 0; i < 10; i++ {
133                 block := newBlock(uint64(i))
134                 if _, ok := cache.lruMainChainHashes.Get(block.Height); ok {
135                         t.Fatalf("find old main chain block Hash for specified height")
136                 }
137         }
138
139         for i := 10; i < maxCachedMainChainHashes+10; i++ {
140                 block := newBlock(uint64(i))
141                 if _, ok := cache.lruMainChainHashes.Get(block.Height); !ok {
142                         t.Fatalf("can't find new main chain block Hash for specified height")
143                 }
144         }
145
146         for i := 0; i < maxCachedVoteResults+10; i++ {
147                 voteResult := newVoteResult(uint64(i))
148                 cache.lookupVoteResult(voteResult.Seq)
149         }
150
151         for i := 0; i < 10; i++ {
152                 voteResult := newVoteResult(uint64(i))
153                 if _, ok := cache.lruVoteResults.Get(voteResult.Seq); ok {
154                         t.Fatalf("find old vote result")
155                 }
156         }
157
158         for i := 10; i < maxCachedVoteResults+10; i++ {
159                 voteResult := newVoteResult(uint64(i))
160                 if _, ok := cache.lruVoteResults.Get(voteResult.Seq); !ok {
161                         t.Fatalf("can't find new vote result")
162                 }
163         }
164 }