6 "github.com/vapor/protocol/bc"
7 "github.com/vapor/protocol/bc/types"
8 "github.com/vapor/protocol/state"
11 func TestBlockCache(t *testing.T) {
12 newBlock := func(h uint64) *types.Block {
14 BlockHeader: types.BlockHeader{
20 newConsensusResult := func(seq uint64) *state.ConsensusResult {
21 return &state.ConsensusResult{
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))
33 blockHashes[block.Height] = &hash
34 blockIndexHashes[block.Height] = append(blockIndexHashes[block.Height], &hash)
37 consensusResults := make(map[uint64]*state.ConsensusResult)
38 for i := 0; i < maxCachedConsensusResults+10; i++ {
39 consensusResult := newConsensusResult(uint64(i))
40 consensusResults[consensusResult.Seq] = consensusResult
43 fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) {
44 return &blocks[*hash].BlockHeader, nil
47 fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
48 return blocks[*hash].Transactions, nil
51 fillBlockHashesFn := func(height uint64) ([]*bc.Hash, error) {
52 return blockIndexHashes[height], nil
55 fillMainChainHashFn := func(height uint64) (*bc.Hash, error) {
56 return blockHashes[height], nil
59 fillConsensusResultFn := func(seq uint64) (*state.ConsensusResult, error) {
60 return consensusResults[seq], nil
63 cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillBlockHashesFn, fillMainChainHashFn, fillConsensusResultFn)
64 for i := 0; i < maxCachedBlockHeaders+10; i++ {
65 block := newBlock(uint64(i))
67 cache.lookupBlockHeader(&hash)
70 for i := 0; i < 10; i++ {
71 block := newBlock(uint64(i))
73 if _, ok := cache.lruBlockHeaders.Get(hash); ok {
74 t.Fatalf("find old block header")
78 for i := 10; i < maxCachedBlockHeaders+10; i++ {
79 block := newBlock(uint64(i))
81 if _, ok := cache.lruBlockHeaders.Get(hash); !ok {
82 t.Fatalf("can't find new block header")
86 for i := 0; i < maxCachedBlockTransactions+10; i++ {
87 block := newBlock(uint64(i))
89 cache.lookupBlockTxs(&hash)
92 for i := 0; i < 10; i++ {
93 block := newBlock(uint64(i))
95 if _, ok := cache.lruBlockTxs.Get(hash); ok {
96 t.Fatalf("find old block transactions")
100 for i := 10; i < maxCachedBlockTransactions+10; i++ {
101 block := newBlock(uint64(i))
103 if _, ok := cache.lruBlockTxs.Get(hash); !ok {
104 t.Fatalf("can't find new block transactions")
108 for i := 0; i < maxCachedBlockHashes+10; i++ {
109 block := newBlock(uint64(i))
110 cache.lookupBlockHashesByHeight(block.Height)
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")
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")
127 for i := 0; i < maxCachedMainChainHashes+10; i++ {
128 block := newBlock(uint64(i))
129 cache.lookupMainChainHash(block.Height)
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")
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")
146 for i := 0; i < maxCachedConsensusResults+10; i++ {
147 consensusResult := newConsensusResult(uint64(i))
148 cache.lookupConsensusResult(consensusResult.Seq)
151 for i := 0; i < 10; i++ {
152 consensusResult := newConsensusResult(uint64(i))
153 if _, ok := cache.lruConsensusResults.Get(consensusResult.Seq); ok {
154 t.Fatalf("find old vote result")
158 for i := 10; i < maxCachedConsensusResults+10; i++ {
159 consensusResult := newConsensusResult(uint64(i))
160 if _, ok := cache.lruConsensusResults.Get(consensusResult.Seq); !ok {
161 t.Fatalf("can't find new vote result")