OSDN Git Service

fix: waive charging storage gas for cross-chain (#162)
[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 < maxCachedBlocks+10; i++ {
20                 block := newBlock(uint64(i))
21                 blocks[block.Hash()] = block
22         }
23
24         cache := newBlockCache(func(hash *bc.Hash) (*types.Block, error) {
25                 return blocks[*hash], nil
26         })
27
28         for i := 0; i < maxCachedBlocks+10; i++ {
29                 block := newBlock(uint64(i))
30                 hash := block.Hash()
31                 cache.lookup(&hash)
32         }
33
34         for i := 0; i < 10; i++ {
35                 block := newBlock(uint64(i))
36                 hash := block.Hash()
37                 if b, _ := cache.get(&hash); b != nil {
38                         t.Fatalf("find old block")
39                 }
40         }
41
42         for i := 10; i < maxCachedBlocks+10; i++ {
43                 block := newBlock(uint64(i))
44                 hash := block.Hash()
45                 if b, _ := cache.get(&hash); b == nil {
46                         t.Fatalf("can't find new block")
47                 }
48         }
49 }