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 {
- common.PanicCrisis(common.Fmt("Could not unmarshal bytes: %X", bytes))
+ log.WithField("err", err).Panic("fail on unmarshal BlockStoreStateJSON")
}
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, nil
+ return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
}
+
+ 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, errors.New("The transactions in the block is empty")
+ return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
}
+ 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 {
- blockHeader, err := s.cache.lookupBlockHeader(hash, height)
- return err == nil && blockHeader != nil
+ _, err := s.cache.lookupBlockHeader(hash, height)
+ return err == 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) {
- blockHeader, err := s.cache.lookupBlockHeader(hash, height)
- if err != nil {
- return nil, err
- }
- return blockHeader, nil
+ return s.cache.lookupBlockHeader(hash, height)
}
// GetBlockTransactions return the Block transactions by given hash
func (s *Store) GetBlockTransactions(hash *bc.Hash) ([]*types.Tx, error) {
- txs, err := s.cache.lookupBlockTxs(hash)
- if err != nil {
- return nil, err
- }
- return txs, nil
+ return s.cache.lookupBlockTxs(hash)
+}
+
+// GetStoreStatus return the BlockStoreStateJSON
+func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
+ return loadBlockStoreStateJSON(s.db)
}
// GetTransactionsUtxo will return all the utxo that related to the input txs
return ts, nil
}
-// GetStoreStatus return the BlockStoreStateJSON
-func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
- return loadBlockStoreStateJSON(s.db)
+// GetUtxo will search the utxo in db
+func (s *Store) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
+ return getUtxo(s.db, hash)
}
// 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)
-
- // updata blockheader cache
- if _, ok := s.cache.getBlockHeader(&blockHash); ok {
- s.cache.addBlockHeader(blockHeader)
- }
-
+ s.cache.removeBlockHeader(blockHeader)
return nil
}
}
batch.Set(calcVoteResultKey(vote.Seq), bytes)
- if _, ok := s.cache.getVoteResult(vote.Seq); ok {
- s.cache.addVoteResult(vote)
- }
+ s.cache.removeVoteResult(vote)
}
bytes, err := json.Marshal(protocol.BlockStoreState{