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{
19 newVoteResult := func(seq uint64) *state.VoteResult {
20 return &state.VoteResult{
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
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
35 fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
36 return &blocks[*hash].BlockHeader, nil
39 fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) {
40 return blocks[*hash].Transactions, nil
43 fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
44 return voteResults[seq], nil
47 cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
49 for i := 0; i < maxCachedBlockHeaders+10; i++ {
50 block := newBlock(uint64(i))
52 cache.lookupBlockHeader(&hash, block.Height)
55 for i := 0; i < 10; i++ {
56 block := newBlock(uint64(i))
58 if _, ok := cache.lruBlockHeaders.Get(hash); ok {
59 t.Fatalf("find old block")
63 for i := 10; i < maxCachedBlockHeaders+10; i++ {
64 block := newBlock(uint64(i))
66 if _, ok := cache.lruBlockHeaders.Get(hash); !ok {
67 t.Fatalf("can't find new block")
71 for i := 0; i < maxCachedVoteResults+10; i++ {
72 voteResult := newVoteResult(uint64(i))
73 cache.lookupVoteResult(voteResult.Seq)
76 for i := 0; i < 10; i++ {
77 voteResult := newVoteResult(uint64(i))
78 if _, ok := cache.lruVoteResults.Get(voteResult.Seq); ok {
79 t.Fatalf("find old vote result")
83 for i := 10; i < maxCachedVoteResults+10; i++ {
84 voteResult := newVoteResult(uint64(i))
85 if _, ok := cache.lruVoteResults.Get(voteResult.Seq); !ok {
86 t.Fatalf("can't find new vote result")