package database
import (
+ "fmt"
"strconv"
"github.com/golang/groupcache/singleflight"
)
const (
- maxCachedBlockHeaders = 4096
+ maxCachedBlockHeaders = 1024
maxCachedBlockTransactions = 1024
- maxCachedVoteResults = 128
+ maxCachedVoteResults = 144 // int(60 * 60 * 24 * 1000 / consensus.BlockTimeInterval / consensus.RoundVoteBlockNums)
)
type fillBlockHeaderFn func(hash *bc.Hash, height uint64) (*types.BlockHeader, error)
fillBlockTransactionFn func(hash *bc.Hash) ([]*types.Tx, error)
fillVoteResultFn func(seq uint64) (*state.VoteResult, error)
- sf singleflight.Group
+ singleBlockHeader singleflight.Group
+ singleBlockTxs singleflight.Group
+ singleVoteResult singleflight.Group
}
func (c *cache) lookupBlockHeader(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
- if data, ok := c.lruBlockHeaders.Get(*hash); ok {
- return data.(*types.BlockHeader), nil
+ if bH, ok := c.getBlockHeader(hash); ok {
+ return bH, nil
}
- blockHeader, err := c.sf.Do("BlockHeader:"+hash.String(), func() (interface{}, error) {
- blockHeader, err := c.fillBlockHeaderFn(hash, height)
+ blockHeader, err := c.singleBlockHeader.Do(hash.String(), func() (interface{}, error) {
+ bH, err := c.fillBlockHeaderFn(hash, height)
if err != nil {
return nil, err
}
- c.lruBlockHeaders.Add(blockHeader.Hash(), blockHeader)
- return blockHeader, nil
+ if bH == nil {
+ return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
+ }
+
+ c.addBlockHeader(bH)
+ return bH, nil
})
if err != nil {
return nil, err
}
func (c *cache) lookupBlockTxs(hash *bc.Hash) ([]*types.Tx, error) {
- if data, ok := c.lruBlockTxs.Get(*hash); ok {
- return data.([]*types.Tx), nil
+ if bTxs, ok := c.getBlockTransactions(hash); ok {
+ return bTxs, nil
}
- blockTxs, err := c.sf.Do("BlockTxs:"+hash.String(), func() (interface{}, error) {
- blockTxs, err := c.fillBlockTransactionFn(hash)
+ blockTransactions, err := c.singleBlockTxs.Do(hash.String(), func() (interface{}, error) {
+ bTxs, err := c.fillBlockTransactionFn(hash)
if err != nil {
return nil, err
}
- c.lruBlockTxs.Add(hash, blockTxs)
- return blockTxs, nil
+ if bTxs == nil {
+ return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
+ }
+
+ c.addBlockTxs(*hash, bTxs)
+ return bTxs, nil
})
if err != nil {
return nil, err
}
- return blockTxs.([]*types.Tx), nil
+ return blockTransactions.([]*types.Tx), nil
}
func (c *cache) lookupVoteResult(seq uint64) (*state.VoteResult, error) {
- if data, ok := c.lruVoteResults.Get(seq); ok {
- return data.(*state.VoteResult).Fork(), nil
+ if vr, ok := c.getVoteResult(seq); ok {
+ return vr.Fork(), nil
}
seqStr := strconv.FormatUint(seq, 10)
- voteResult, err := c.sf.Do("VoteResult:"+seqStr, func() (interface{}, error) {
- voteResult, err := c.fillVoteResultFn(seq)
+ voteResult, err := c.singleVoteResult.Do(seqStr, func() (interface{}, error) {
+ v, err := c.fillVoteResultFn(seq)
if err != nil {
return nil, err
}
- c.lruVoteResults.Add(voteResult.Seq, voteResult)
- return voteResult, nil
+ if v == nil {
+ return nil, fmt.Errorf("There are no vote result with given seq %s", seqStr)
+ }
+
+ c.addVoteResult(v)
+ return v, nil
})
if err != nil {
return nil, err
return voteResult.(*state.VoteResult).Fork(), nil
}
-func (c *cache) removeBlockHeader(blockHeader *types.BlockHeader) {
- c.lruBlockHeaders.Remove(blockHeader.Hash())
+func (c *cache) getBlockHeader(hash *bc.Hash) (*types.BlockHeader, bool) {
+ blockHeader, ok := c.lruBlockHeaders.Get(*hash)
+ if blockHeader == nil {
+ return nil, ok
+ }
+ return blockHeader.(*types.BlockHeader), ok
+}
+
+func (c *cache) getBlockTransactions(hash *bc.Hash) ([]*types.Tx, bool) {
+ txs, ok := c.lruBlockTxs.Get(*hash)
+ if txs == nil {
+ return nil, ok
+ }
+ return txs.([]*types.Tx), ok
+}
+
+func (c *cache) getVoteResult(seq uint64) (*state.VoteResult, bool) {
+ voteResult, ok := c.lruVoteResults.Get(seq)
+ if voteResult == nil {
+ return nil, ok
+ }
+ return voteResult.(*state.VoteResult), ok
+}
+
+func (c *cache) addBlockHeader(blockHeader *types.BlockHeader) {
+ c.lruBlockHeaders.Add(blockHeader.Hash(), blockHeader)
+}
+
+func (c *cache) addBlockTxs(hash bc.Hash, txs []*types.Tx) {
+ c.lruBlockTxs.Add(hash, txs)
}
-func (c *cache) removeVoteResult(voteResult *state.VoteResult) {
- c.lruVoteResults.Remove(voteResult.Seq)
+func (c *cache) addVoteResult(voteResult *state.VoteResult) {
+ c.lruVoteResults.Add(voteResult.Seq, voteResult)
}
for i := 0; i < 10; i++ {
block := newBlock(uint64(i))
hash := block.Hash()
- if _, ok := cache.lruBlockHeaders.Get(hash); ok {
+ if b, _ := cache.getBlockHeader(&hash); b != nil {
t.Fatalf("find old block")
}
}
for i := 10; i < maxCachedBlockHeaders+10; i++ {
block := newBlock(uint64(i))
hash := block.Hash()
- if _, ok := cache.lruBlockHeaders.Get(hash); !ok {
+ if b, _ := cache.getBlockHeader(&hash); b == nil {
t.Fatalf("can't find new block")
}
}
for i := 0; i < 10; i++ {
voteResult := newVoteResult(uint64(i))
- if _, ok := cache.lruVoteResults.Get(voteResult.Seq); ok {
+ if v, _ := cache.getVoteResult(voteResult.Seq); v != nil {
t.Fatalf("find old vote result")
}
}
for i := 10; i < maxCachedVoteResults+10; i++ {
voteResult := newVoteResult(uint64(i))
- if _, ok := cache.lruVoteResults.Get(voteResult.Seq); !ok {
+ if v, _ := cache.getVoteResult(voteResult.Seq); v == nil {
t.Fatalf("can't find new vote result")
}
}
import (
"encoding/binary"
"encoding/json"
- "fmt"
"time"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
+ "github.com/tendermint/tmlibs/common"
dbm "github.com/vapor/database/leveldb"
"github.com/vapor/database/storage"
if bytes == nil {
return nil
}
-
bsj := &protocol.BlockStoreState{}
if err := json.Unmarshal(bytes, bsj); err != nil {
- log.WithField("err", err).Panic("fail on unmarshal BlockStoreStateJSON")
+ common.PanicCrisis(common.Fmt("Could not unmarshal bytes: %X", bytes))
}
return bsj
}
// GetBlockHeader return the block header by given hash and height
func GetBlockHeader(db dbm.DB, hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
+ block := &types.Block{}
binaryBlockHeader := db.Get(calcBlockHeaderKey(height, hash))
if binaryBlockHeader == nil {
- return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
+ return nil, nil
}
-
- block := &types.Block{}
if err := block.UnmarshalText(binaryBlockHeader); err != nil {
return nil, err
}
+
return &block.BlockHeader, nil
}
// GetBlockTransactions return the block transactions by given hash
func GetBlockTransactions(db dbm.DB, hash *bc.Hash) ([]*types.Tx, error) {
+ block := &types.Block{}
binaryBlockTxs := db.Get(calcBlockTransactionsKey(hash))
if binaryBlockTxs == nil {
- return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
+ return nil, errors.New("The transactions in the block is empty")
}
- block := &types.Block{}
if err := block.UnmarshalText(binaryBlockTxs); err != nil {
return nil, err
}
}
}
+// GetUtxo will search the utxo in db
+func (s *Store) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
+ return getUtxo(s.db, hash)
+}
+
// BlockExist check if the block is stored in disk
func (s *Store) BlockExist(hash *bc.Hash, height uint64) bool {
- _, err := s.cache.lookupBlockHeader(hash, height)
- return err == nil
+ blockHeader, err := s.cache.lookupBlockHeader(hash, height)
+ return err == nil && blockHeader != nil
}
// GetBlock return the block by given hash
// GetBlockHeader return the BlockHeader by given hash
func (s *Store) GetBlockHeader(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
- return s.cache.lookupBlockHeader(hash, height)
+ blockHeader, err := s.cache.lookupBlockHeader(hash, height)
+ if err != nil {
+ return nil, err
+ }
+ return blockHeader, nil
}
// GetBlockTransactions return the Block transactions by given hash
func (s *Store) GetBlockTransactions(hash *bc.Hash) ([]*types.Tx, error) {
- return s.cache.lookupBlockTxs(hash)
-}
-
-// GetStoreStatus return the BlockStoreStateJSON
-func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
- return loadBlockStoreStateJSON(s.db)
+ txs, err := s.cache.lookupBlockTxs(hash)
+ if err != nil {
+ return nil, err
+ }
+ return txs, nil
}
// GetTransactionsUtxo will return all the utxo that related to the input txs
return ts, nil
}
-// GetUtxo will search the utxo in db
-func (s *Store) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
- return getUtxo(s.db, hash)
+// GetStoreStatus return the BlockStoreStateJSON
+func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
+ return loadBlockStoreStateJSON(s.db)
}
// GetVoteResult retrive the voting result in specified vote sequence
// SaveBlock persists a new block in the protocol.
func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
startTime := time.Now()
+
binaryBlockHeader, err := block.MarshalTextForBlockHeader()
if err != nil {
return errors.Wrap(err, "Marshal block header")
blockHash := blockHeader.Hash()
s.db.Set(calcBlockHeaderKey(blockHeader.Height, &blockHash), binaryBlockHeader)
- s.cache.removeBlockHeader(blockHeader)
+
+ // updata blockheader cache
+ if _, ok := s.cache.getBlockHeader(&blockHash); ok {
+ s.cache.addBlockHeader(blockHeader)
+ }
+
return nil
}
}
batch.Set(calcVoteResultKey(vote.Seq), bytes)
- s.cache.removeVoteResult(vote)
+ if _, ok := s.cache.getVoteResult(vote.Seq); ok {
+ s.cache.addVoteResult(vote)
+ }
}
bytes, err := json.Marshal(protocol.BlockStoreState{