github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/database/cache_test.go (about) 1 package database 2 3 import ( 4 "testing" 5 6 "github.com/bytom/bytom/protocol/bc" 7 "github.com/bytom/bytom/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 blockIndexHashes := make(map[uint64][]*bc.Hash) 20 for i := 0; i < maxCachedBlockHeaders+10; i++ { 21 block := newBlock(uint64(i)) 22 hash := block.Hash() 23 24 blocks[block.Hash()] = block 25 blockIndexHashes[block.Height] = append(blockIndexHashes[block.Height], &hash) 26 } 27 28 fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) { 29 return &blocks[*hash].BlockHeader, nil 30 } 31 32 fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) { 33 return blocks[*hash].Transactions, nil 34 } 35 36 fillBlockHashesFn := func(height uint64) ([]*bc.Hash, error) { 37 return blockIndexHashes[height], nil 38 } 39 40 fillMainChainHashFn := func(height uint64) (*bc.Hash, error) { 41 return blockIndexHashes[height][0], nil 42 } 43 44 cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillBlockHashesFn, fillMainChainHashFn, nil) 45 46 for i := 0; i < maxCachedBlockHeaders+10; i++ { 47 block := newBlock(uint64(i)) 48 hash := block.Hash() 49 cache.lookupBlockHeader(&hash) 50 } 51 }