github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/orgchain/storages/block_cache.go (about)

     1  package storages
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/golang-lru"
     7  	"github.com/sixexorg/magnetic-ring/common"
     8  	"github.com/sixexorg/magnetic-ring/core/orgchain/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  }
    27  
    28  //NewBlockCache return BlockCache instance
    29  func NewBlockCache() (*BlockCache, error) {
    30  	blockCache, err := lru.NewARC(BLOCK_CAHE_SIZE)
    31  	if err != nil {
    32  		return nil, fmt.Errorf("NewARC block error %s", err)
    33  	}
    34  	transactionCache, err := lru.NewARC(TRANSACTION_CACHE_SIZE)
    35  	if err != nil {
    36  		return nil, fmt.Errorf("NewARC header error %s", err)
    37  	}
    38  	return &BlockCache{
    39  		blockCache:       blockCache,
    40  		transactionCache: transactionCache,
    41  	}, nil
    42  }
    43  
    44  //AddBlock to cache
    45  func (this *BlockCache) AddBlock(block *types.Block) {
    46  	blockHash := block.Hash()
    47  	this.blockCache.Add(string(blockHash.ToBytes()), block)
    48  }
    49  
    50  //GetBlock return block by block hash from cache
    51  func (this *BlockCache) GetBlock(blockHash common.Hash) *types.Block {
    52  	block, ok := this.blockCache.Get(string(blockHash.ToBytes()))
    53  	if !ok {
    54  		return nil
    55  	}
    56  	return block.(*types.Block)
    57  }
    58  
    59  //ContainBlock retuen whether block is in cache
    60  func (this *BlockCache) ContainBlock(blockHash common.Hash) bool {
    61  	return this.blockCache.Contains(string(blockHash.ToBytes()))
    62  }
    63  
    64  //AddTransaction add transaction to block cache
    65  func (this *BlockCache) AddTransaction(tx *types.Transaction, height uint64) {
    66  	txHash := tx.Hash()
    67  	this.transactionCache.Add(string(txHash.ToBytes()), &TransactionCacheaValue{
    68  		Tx:     tx,
    69  		Height: height,
    70  	})
    71  }
    72  
    73  //GetTransaction return transaction by transaction hash from cache
    74  func (this *BlockCache) GetTransaction(txHash common.Hash) (*types.Transaction, uint64) {
    75  	value, ok := this.transactionCache.Get(string(txHash.ToBytes()))
    76  	if !ok {
    77  		return nil, 0
    78  	}
    79  	txValue := value.(*TransactionCacheaValue)
    80  	return txValue.Tx, txValue.Height
    81  }
    82  
    83  //ContainTransaction return whether transaction is in cache
    84  func (this *BlockCache) ContainTransaction(txHash common.Hash) bool {
    85  	return this.transactionCache.Contains(string(txHash.ToBytes()))
    86  }