const maxCachedBlocks = 30
-func newBlockCache(fillFn func(hash *bc.Hash) (*types.Block, error)) blockCache {
+func newBlockCache(fillFn func(hash *bc.Hash, height uint64) (*types.Block, error)) blockCache {
return blockCache{
lru: lru.New(maxCachedBlocks),
fillFn: fillFn,
type blockCache struct {
mu sync.Mutex
lru *lru.Cache
- fillFn func(hash *bc.Hash) (*types.Block, error)
+ fillFn func(hash *bc.Hash, height uint64) (*types.Block, error)
single singleflight.Group
}
-func (c *blockCache) lookup(hash *bc.Hash) (*types.Block, error) {
+func (c *blockCache) lookup(hash *bc.Hash, height uint64) (*types.Block, error) {
if b, ok := c.get(hash); ok {
return b, nil
}
block, err := c.single.Do(hash.String(), func() (interface{}, error) {
- b, err := c.fillFn(hash)
+ b, err := c.fillFn(hash, height)
if err != nil {
return nil, err
}
const logModule = "leveldb"
var (
- blockStoreKey = []byte("blockStore")
- blockPrefix = []byte("B:")
- blockHeaderPrefix = []byte("BH:")
- txStatusPrefix = []byte("BTS:")
- voteResultPrefix = []byte("VR:")
+ blockStoreKey = []byte("blockStore")
+ blockPrefix = []byte("B:")
+ blockHeaderPrefix = []byte("BH:")
+ blockTransactonsPrefix = []byte("BTXS:")
+ txStatusPrefix = []byte("BTS:")
+ voteResultPrefix = []byte("VR:")
)
func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState {
return append(key, hash.Bytes()...)
}
+func calcBlockTransactionsKey(hash *bc.Hash) []byte {
+ return append(blockTransactonsPrefix, hash.Bytes()...)
+}
+
func calcTxStatusKey(hash *bc.Hash) []byte {
return append(txStatusPrefix, hash.Bytes()...)
}
return append(voteResultPrefix, buf[:]...)
}
-// GetBlock return the block by given hash
-func GetBlock(db dbm.DB, hash *bc.Hash) (*types.Block, error) {
+// GetBlock return the block by given hash and height
+func GetBlock(db dbm.DB, hash *bc.Hash, height uint64) (*types.Block, error) {
bytez := db.Get(calcBlockKey(hash))
if bytez == nil {
return nil, nil
// NewStore creates and returns a new Store object.
func NewStore(db dbm.DB) *Store {
- cache := newBlockCache(func(hash *bc.Hash) (*types.Block, error) {
- return GetBlock(db, hash)
+ cache := newBlockCache(func(hash *bc.Hash, height uint64) (*types.Block, error) {
+ return GetBlock(db, hash, height)
})
return &Store{
db: db,
}
// BlockExist check if the block is stored in disk
-func (s *Store) BlockExist(hash *bc.Hash) bool {
- block, err := s.cache.lookup(hash)
+func (s *Store) BlockExist(hash *bc.Hash, height uint64) bool {
+ block, err := s.cache.lookup(hash, height)
return err == nil && block != nil
}
// GetBlock return the block by given hash
-func (s *Store) GetBlock(hash *bc.Hash) (*types.Block, error) {
- return s.cache.lookup(hash)
+func (s *Store) GetBlock(hash *bc.Hash, height uint64) (*types.Block, error) {
+ return s.cache.lookup(hash, height)
+}
+
+// GetBlockHeader return the BlockHeader by given hash
+func (s *Store) GetBlockHeader(*bc.Hash, uint64) (*types.BlockHeader, error) {
+ return nil, nil
}
// GetTransactionsUtxo will return all the utxo that related to the input txs
// SaveBlock persists a new block in the protocol.
func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
startTime := time.Now()
- binaryBlock, err := block.MarshalText()
+ /*
+ binaryBlock, err := block.MarshalText()
+ if err != nil {
+ return errors.Wrap(err, "Marshal block meta")
+ }
+ */
+
+ binaryBlockHeader, err := block.BlockHeader.MarshalText()
if err != nil {
- return errors.Wrap(err, "Marshal block meta")
+ return errors.Wrap(err, "Marshal block header")
}
- binaryBlockHeader, err := block.BlockHeader.MarshalText()
+ binaryBlockTxs, err := block.MarshalTextForTransactions()
if err != nil {
return errors.Wrap(err, "Marshal block header")
}
blockHash := block.Hash()
batch := s.db.NewBatch()
- batch.Set(calcBlockKey(&blockHash), binaryBlock)
+ //batch.Set(calcBlockKey(&blockHash), binaryBlock)
batch.Set(calcBlockHeaderKey(block.Height, &blockHash), binaryBlockHeader)
+ batch.Set(calcBlockTransactionsKey(&blockHash), binaryBlockTxs)
batch.Set(calcTxStatusKey(&blockHash), binaryTxStatus)
batch.Write()
return nil
}
+// SaveBlockHeader persists a new block header in the protocol.
+func (s *Store) SaveBlockHeader(blockHeader *types.BlockHeader) error {
+ startTime := time.Now()
+
+ binaryBlockHeader, err := blockHeader.MarshalText()
+ if err != nil {
+ return errors.Wrap(err, "Marshal block header")
+ }
+
+ blockHash := blockHeader.Hash()
+ batch := s.db.NewBatch()
+ batch.Set(calcBlockHeaderKey(blockHeader.Height, &blockHash), binaryBlockHeader)
+ batch.Write()
+
+ log.WithFields(log.Fields{
+ "module": logModule,
+ "height": blockHeader.Height,
+ "hash": blockHash.String(),
+ "duration": time.Since(startTime),
+ }).Info("blockHeader saved on disk")
+ return nil
+}
+
// SaveChainStatus save the core's newest status && delete old status
func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteResults []*state.VoteResult) error {
batch := s.db.NewBatch()
return err
}
- block, err := c.store.GetBlock(&blockNode.Hash)
+ blockHeader, err := c.store.GetBlockHeader(&blockNode.Hash, blockNode.Height)
if err != nil {
return err
}
- block.Set(nodeOrder, signature)
+ blockHeader.Set(nodeOrder, signature)
- txStatus, err := c.store.GetTransactionStatus(&blockNode.Hash)
- if err != nil {
- return err
- }
-
- if err := c.store.SaveBlock(block, txStatus); err != nil {
+ if err := c.store.SaveBlockHeader(blockHeader); err != nil {
return err
}
return nil
}
+// MarshalTextForTransactions fulfills the json.Marshaler interface.
+func (b *Block) MarshalTextForTransactions() ([]byte, error) {
+ buf := bufpool.Get()
+ defer bufpool.Put(buf)
+
+ ew := errors.NewWriter(buf)
+ ew.Write([]byte{SerBlockTransactions})
+
+ if _, err := blockchain.WriteVarint31(ew, uint64(len(b.Transactions))); err != nil {
+ return nil, err
+ }
+
+ for _, tx := range b.Transactions {
+ if _, err := tx.WriteTo(ew); err != nil {
+ return nil, err
+ }
+ }
+
+ if err := ew.Err(); err != nil {
+ return nil, err
+ }
+
+ enc := make([]byte, hex.EncodedLen(buf.Len()))
+ hex.Encode(enc, buf.Bytes())
+ return enc, nil
+}
+
+// UnmarshalTextForTransactions fulfills the encoding.TextUnmarshaler interface.
+func (b *Block) UnmarshalTextForTransactions(text []byte) error {
+ decoded := make([]byte, hex.DecodedLen(len(text)))
+ if _, err := hex.Decode(decoded, text); err != nil {
+ return err
+ }
+
+ r := blockchain.NewReader(decoded)
+ var serflags [1]byte
+ io.ReadFull(r, serflags[:])
+ if serflags[0] != SerBlockTransactions {
+ return fmt.Errorf("unsupported serialization flags 0x%x", serflags)
+ }
+
+ n, err := blockchain.ReadVarint31(r)
+ if err != nil {
+ return errors.Wrap(err, "reading number of transactions")
+ }
+
+ for ; n > 0; n-- {
+ data := TxData{}
+ if err = data.readFrom(r); err != nil {
+ return errors.Wrapf(err, "reading transaction %d", len(b.Transactions))
+ }
+
+ b.Transactions = append(b.Transactions, NewTx(data))
+ }
+
+ if trailing := r.Len(); trailing > 0 {
+ return fmt.Errorf("trailing garbage (%d bytes)", trailing)
+ }
+ return nil
+}
+
func (b *Block) readFrom(r *blockchain.Reader) error {
serflags, err := b.BlockHeader.readFrom(r)
if err != nil {
// GetBlockByHash return a block by given hash
func (c *Chain) GetBlockByHash(hash *bc.Hash) (*types.Block, error) {
- return c.store.GetBlock(hash)
+ node := c.index.GetNode(hash)
+ if node == nil {
+ return nil, errors.New("can't find block in given hash")
+ }
+ return c.store.GetBlock(hash, node.Height)
}
// GetBlockByHeight return a block header by given height
if node == nil {
return nil, errors.New("can't find block in given height")
}
- return c.store.GetBlock(&node.Hash)
+ return c.store.GetBlock(&node.Hash, height)
}
// GetHeaderByHash return a block header by given hash
}
for _, detachNode := range detachNodes {
- b, err := c.store.GetBlock(&detachNode.Hash)
+ b, err := c.store.GetBlock(&detachNode.Hash, detachNode.Height)
if err != nil {
return err
}
}
for _, attachNode := range attachNodes {
- b, err := c.store.GetBlock(&attachNode.Hash)
+ b, err := c.store.GetBlock(&attachNode.Hash, attachNode.Height)
if err != nil {
return err
}
// The start time of the last round of product block
lastRoundStartTime := startTimestamp + (blockTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
// Order of blocker
- return (blockTimestamp - lastRoundStartTime)/(consensus.BlockNumEachNode*consensus.BlockTimeInterval)
+ return (blockTimestamp - lastRoundStartTime) / (consensus.BlockNumEachNode * consensus.BlockTimeInterval)
}
func (c *consensusNodeManager) getPrevRoundLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {
// getVoteResult return the vote result
// seq represent the sequence of vote
// blockNode represent the chain in which the result of the vote is located
-// Voting results need to be adjusted according to the chain
+// Voting results need to be adjusted according to the chain
func (c *consensusNodeManager) getVoteResult(seq uint64, blockNode *state.BlockNode) (*state.VoteResult, error) {
voteResult, err := c.store.GetVoteResult(seq)
if err != nil {
var forChainRollback, mainChainRollBack bool
if forChainRollback = forkChainNode.Height >= mainChainNode.Height; forChainRollback {
attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
- }
+ }
if mainChainRollBack = forkChainNode.Height <= mainChainNode.Height; mainChainRollBack {
detachNodes = append(detachNodes, mainChainNode)
}
}
for _, node := range detachNodes {
- block, err := c.store.GetBlock(&node.Hash)
+ block, err := c.store.GetBlock(&node.Hash, node.Height)
if err != nil {
return err
}
}
for _, node := range attachNodes {
- block, err := c.store.GetBlock(&node.Hash)
+ block, err := c.store.GetBlock(&node.Hash, node.Height)
if err != nil {
return err
}
// Store provides storage interface for blockchain data
type Store interface {
- BlockExist(*bc.Hash) bool
+ BlockExist(*bc.Hash, uint64) bool
- GetBlock(*bc.Hash) (*types.Block, error)
+ GetBlock(*bc.Hash, uint64) (*types.Block, error)
+ GetBlockHeader(*bc.Hash, uint64) (*types.BlockHeader, error)
GetStoreStatus() *BlockStoreState
GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error)
GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error
LoadBlockIndex(uint64) (*state.BlockIndex, error)
SaveBlock(*types.Block, *bc.TransactionStatus) error
+ SaveBlockHeader(*types.BlockHeader) error
SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, []*state.VoteResult) error
}
type mockStore struct{}
-func (s *mockStore) BlockExist(hash *bc.Hash) bool { return false }
-func (s *mockStore) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
+func (s *mockStore) BlockExist(hash *bc.Hash, height uint64) bool { return false }
+func (s *mockStore) GetBlock(*bc.Hash, uint64) (*types.Block, error) { return nil, nil }
func (s *mockStore) GetStoreStatus() *BlockStoreState { return nil }
func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
type mockStore1 struct{}
-func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
-func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
+func (s *mockStore1) BlockExist(hash *bc.Hash, height uint64) bool { return false }
+func (s *mockStore1) GetBlock(*bc.Hash, uint64) (*types.Block, error) { return nil, nil }
func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {