github.com/turingchain2020/turingchain@v1.1.21/blockchain/cache_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package blockchain 6 7 import ( 8 "testing" 9 10 "github.com/turingchain2020/turingchain/common/crypto" 11 "github.com/turingchain2020/turingchain/queue" 12 13 "github.com/turingchain2020/turingchain/types" 14 "github.com/turingchain2020/turingchain/util" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestBlockCache(t *testing.T) { 19 20 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 21 _, priv := util.Genaddress() 22 cacheSize := int64(5) 23 blockHashCacheSize := int64(10) 24 cfg.GetModuleConfig().BlockChain.DefCacheSize = cacheSize 25 cache := newBlockCache(cfg, blockHashCacheSize) 26 currHeight := int64(-1) 27 // add 20 block 28 for i := 0; i < int(blockHashCacheSize+2*cacheSize); i++ { 29 block := util.CreateCoinsBlock(cfg, priv, 1) 30 block.Height = int64(i) 31 cache.AddBlock(&types.BlockDetail{Block: block}) 32 currHeight++ 33 } 34 // rollback 4 block 35 for i := 0; i < int(cacheSize)-1; i++ { 36 cache.DelBlock(currHeight) 37 currHeight-- 38 } 39 40 require.Equal(t, int64(15), currHeight) 41 require.Equal(t, 32, len(cache.GetBlockHash(currHeight))) 42 require.Equal(t, 0, len(cache.GetBlockHash(currHeight+1))) 43 44 require.Equal(t, 6, len(cache.hashCache)) 45 require.Equal(t, 1, len(cache.blockCache)) 46 require.Equal(t, currHeight, cache.currentHeight) 47 require.Equal(t, 0, len(cache.GetBlockHash(9))) 48 require.Equal(t, 32, len(cache.GetBlockHash(10))) 49 require.NotNil(t, cache.GetBlockByHeight(currHeight)) 50 require.Nil(t, cache.GetBlockByHeight(currHeight-1)) 51 require.NotNil(t, cache.GetBlockByHash(cache.GetBlockHash(currHeight))) 52 } 53 54 func initTxHashCache(txHeightRange int64) (*txHashCache, *BlockChain, *types.TuringchainConfig) { 55 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 56 cfg.GetModuleConfig().BlockChain.DefCacheSize = txHeightRange * 4 57 chain := New(cfg) 58 chain.blockStore = &BlockStore{blockCache: chain.blockCache} 59 q := queue.New("channel") 60 q.SetConfig(cfg) 61 chain.client = q.Client() 62 cache := newTxHashCache(chain, txHeightRange, txHeightRange) 63 chain.txHeightCache = cache 64 return cache, chain, cfg 65 66 } 67 68 func newTestTxs(cfg *types.TuringchainConfig, priv crypto.PrivKey, currHeight, txHeightRange int64) []*types.Transaction { 69 70 txs := util.GenNoneTxs(cfg, priv, 4) 71 //分别设置,最低txHeight, 最高txHeight, 正常txHeight,非txHeight交易 72 util.UpdateExpireWithTxHeight(txs[0], priv, currHeight-txHeightRange) 73 util.UpdateExpireWithTxHeight(txs[1], priv, currHeight) 74 util.UpdateExpireWithTxHeight(txs[2], priv, currHeight+txHeightRange) 75 return txs 76 } 77 78 func addTestBlock(chain *BlockChain, cfg *types.TuringchainConfig, priv crypto.PrivKey, startHeight, txHeightRange int64, blockNum int) { 79 80 for i := 0; i < blockNum; i++ { 81 txs := newTestTxs(cfg, priv, startHeight, txHeightRange) 82 block := &types.BlockDetail{Block: &types.Block{Txs: txs, Height: startHeight}} 83 chain.blockCache.AddBlock(block) 84 chain.txHeightCache.Add(block.GetBlock()) 85 startHeight++ 86 } 87 } 88 89 func TestTxHashCache_Add(t *testing.T) { 90 91 var txHeightRange int64 = 5 92 tc, chain, cfg := initTxHashCache(txHeightRange) 93 _, priv := util.Genaddress() 94 95 addTestBlock(chain, cfg, priv, 0, txHeightRange, 30) 96 currHeight := tc.currBlockHeight 97 require.Equal(t, 29, int(currHeight)) 98 for i := 15; i < 34; i++ { 99 _, ok := tc.txHashes[int64(i)] 100 require.True(t, ok) 101 } 102 require.Equal(t, 20, len(tc.txHashes)) 103 txs := newTestTxs(cfg, priv, currHeight+1, txHeightRange) 104 tc.Add(&types.Block{Txs: txs, Height: currHeight + 1}) 105 106 require.Nil(t, tc.txHashes[15]) 107 require.NotNil(t, tc.txHashes[35]) 108 } 109 110 func TestTxHashCache_Del(t *testing.T) { 111 112 var txHeightRange int64 = 5 113 tc, chain, cfg := initTxHashCache(txHeightRange) 114 _, priv := util.Genaddress() 115 116 addTestBlock(chain, cfg, priv, 0, txHeightRange, 10) 117 currHeight := tc.currBlockHeight 118 for i := 0; i < 5; i++ { 119 tc.Del(currHeight) 120 currHeight-- 121 } 122 require.True(t, currHeight == 4) 123 require.Equal(t, 9, len(tc.txHashes)) 124 for i := 1; i < 10; i++ { 125 require.NotNil(t, tc.txHashes[int64(i)]) 126 } 127 addTestBlock(chain, cfg, priv, currHeight+1, txHeightRange, 20) 128 currHeight = tc.currBlockHeight 129 for i := 0; i < 5; i++ { 130 tc.Del(currHeight) 131 currHeight-- 132 } 133 require.True(t, currHeight == 19) 134 require.Equal(t, 20, len(tc.txHashes)) 135 for i := 5; i < 24; i++ { 136 require.NotNil(t, tc.txHashes[int64(i)]) 137 } 138 } 139 140 func TestTxHashCache_Contains(t *testing.T) { 141 142 var txHeightRange int64 = 5 143 tc, chain, cfg := initTxHashCache(txHeightRange) 144 _, priv := util.Genaddress() 145 146 addTestBlock(chain, cfg, priv, 0, txHeightRange, 30) 147 currHeight := tc.currBlockHeight 148 for i := 0; i < 5; i++ { 149 tc.Del(currHeight) 150 currHeight-- 151 } 152 require.True(t, currHeight == 24) 153 containsRes := []bool{true, true, true, false} 154 for i := 15; i < 25; i++ { 155 txs := chain.blockCache.GetBlockByHeight(int64(i)).GetBlock().GetTxs() 156 require.Equal(t, 4, len(txs)) 157 for idx, tx := range txs { 158 exist := tc.Contains(tx.Hash(), types.GetTxHeight(cfg, tx.Expire, tc.currBlockHeight)) 159 require.Equalf(t, containsRes[idx], exist, "height=%d, txIndex=%d", i, idx) 160 } 161 } 162 163 for i := 25; i < 30; i++ { 164 txs := chain.blockCache.GetBlockByHeight(int64(i)).GetBlock().GetTxs() 165 for _, tx := range txs { 166 exist := tc.Contains(tx.Hash(), types.GetTxHeight(cfg, tx.Expire, tc.currBlockHeight)) 167 require.False(t, exist) 168 } 169 } 170 } 171 172 func TestNoneCache(t *testing.T) { 173 c := &noneCache{} 174 c.Add(nil) 175 c.Del(0) 176 require.False(t, c.Contains([]byte("test"), 10)) 177 }