github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/block_cache.go (about) 1 package storages 2 3 import ( 4 "fmt" 5 6 lru "github.com/hashicorp/golang-lru" 7 "github.com/sixexorg/magnetic-ring/common" 8 "github.com/sixexorg/magnetic-ring/core/mainchain/types" 9 ) 10 11 const ( 12 BLOCK_CAHE_SIZE = 10 //Block cache size 13 TRANSACTION_CACHE_SIZE = 10000 //Transaction cache size 14 ) 15 16 //Value of transaction cache 17 type TransactionCacheaValue struct { 18 Tx *types.Transaction 19 Height uint64 20 } 21 22 //BlockCache with block cache and transaction hash 23 type BlockCache struct { 24 blockCache *lru.ARCCache 25 transactionCache *lru.ARCCache 26 sigCache *lru.ARCCache 27 } 28 29 //NewBlockCache return BlockCache instance 30 func NewBlockCache() (*BlockCache, error) { 31 blockCache, err := lru.NewARC(BLOCK_CAHE_SIZE) 32 if err != nil { 33 return nil, fmt.Errorf("NewARC block error %s", err) 34 } 35 transactionCache, err := lru.NewARC(TRANSACTION_CACHE_SIZE) 36 if err != nil { 37 return nil, fmt.Errorf("NewARC header error %s", err) 38 } 39 sigCache, err := lru.NewARC(BLOCK_CAHE_SIZE) 40 if err != nil { 41 return nil, fmt.Errorf("NewARC sig error %s", err) 42 } 43 return &BlockCache{ 44 blockCache: blockCache, 45 transactionCache: transactionCache, 46 sigCache: sigCache, 47 }, nil 48 } 49 50 //AddBlock to cache 51 func (this *BlockCache) AddBlock(block *types.Block) { 52 blockHash := block.Hash() 53 this.blockCache.Add(string(blockHash.ToBytes()), block) 54 } 55 56 //GetBlock return block by block hash from cache 57 func (this *BlockCache) GetBlock(blockHash common.Hash) *types.Block { 58 block, ok := this.blockCache.Get(string(blockHash.ToBytes())) 59 if !ok { 60 return nil 61 } 62 return block.(*types.Block) 63 } 64 65 //ContainBlock retuen whether block is in cache 66 func (this *BlockCache) ContainBlock(blockHash common.Hash) bool { 67 return this.blockCache.Contains(string(blockHash.ToBytes())) 68 } 69 70 //AddTransaction add transaction to block cache 71 func (this *BlockCache) AddTransaction(tx *types.Transaction, height uint64) { 72 txHash := tx.Hash() 73 this.transactionCache.Add(string(txHash.ToBytes()), &TransactionCacheaValue{ 74 Tx: tx, 75 Height: height, 76 }) 77 } 78 func (this *BlockCache) AddSigData(sig *types.SigData, height uint64) { 79 this.sigCache.Add(string(height), sig) 80 } 81 func (this *BlockCache) GetSigData(height uint64) (sig *types.SigData) { 82 value, ok := this.sigCache.Get(string(height)) 83 if !ok { 84 return nil 85 } 86 return value.(*types.SigData) 87 } 88 89 //GetTransaction return transaction by transaction hash from cache 90 func (this *BlockCache) GetTransaction(txHash common.Hash) (*types.Transaction, uint64) { 91 value, ok := this.transactionCache.Get(string(txHash.ToBytes())) 92 if !ok { 93 return nil, 0 94 } 95 txValue := value.(*TransactionCacheaValue) 96 return txValue.Tx, txValue.Height 97 } 98 99 //ContainTransaction return whether transaction is in cache 100 func (this *BlockCache) ContainTransaction(txHash common.Hash) bool { 101 return this.transactionCache.Contains(string(txHash.ToBytes())) 102 }