tendermint/blockchain/store.go

285 lines
8.9 KiB
Go
Raw Normal View History

package blockchain
2014-08-10 16:35:08 -07:00
import (
"bytes"
"encoding/json"
"fmt"
"io"
2016-12-19 21:45:45 -08:00
"sync"
2014-08-10 16:35:08 -07:00
2017-11-30 11:08:38 -08:00
wire "github.com/tendermint/go-wire"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
2017-10-03 15:49:20 -07:00
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
2014-08-10 16:35:08 -07:00
)
/*
2014-12-23 01:35:54 -08:00
Simple low level store for blocks.
There are three types of information stored:
- BlockMeta: Meta information about each block
- Block part: Parts of each block, aggregated w/ PartSet
2016-04-02 09:10:16 -07:00
- Commit: The commit part of each block, for gossiping precommit votes
2014-12-23 01:35:54 -08:00
2015-06-05 14:15:40 -07:00
Currently the precommit signatures are duplicated in the Block parts as
2016-04-02 09:10:16 -07:00
well as the Commit. In the future this may change, perhaps by moving
the Commit data outside the Block.
2015-06-16 21:16:58 -07:00
2017-10-23 16:46:14 -07:00
// NOTE: BlockStore methods will panic if they encounter errors
// deserializing loaded data, indicating probable corruption on disk.
2014-08-10 16:35:08 -07:00
*/
type BlockStore struct {
2016-12-19 21:45:45 -08:00
db dbm.DB
2016-12-22 18:51:58 -08:00
mtx sync.RWMutex
height int64
2014-08-10 16:35:08 -07:00
}
// NewBlockStore loads a blockStore's JSON serialized form from the
// database, db to retrieve the starting height of the blockstore
// and backs db as the internal database of the blockstore.
func NewBlockStore(db dbm.DB) *BlockStore {
2014-12-23 01:35:54 -08:00
bsjson := LoadBlockStoreStateJSON(db)
2014-08-10 16:35:08 -07:00
return &BlockStore{
height: bsjson.Height,
db: db,
}
}
// Height() returns the last known contiguous block height.
func (bs *BlockStore) Height() int64 {
2016-12-22 18:51:58 -08:00
bs.mtx.RLock()
defer bs.mtx.RUnlock()
2014-08-10 16:35:08 -07:00
return bs.height
}
// GetReader conveniently wraps the result of the database
// lookup for key key into an io.Reader. If no result is found,
// it returns nil otherwise it creates an io.Reader.
// Its utility is mainly for use with wire.ReadBinary.
func (bs *BlockStore) GetReader(key []byte) io.Reader {
bytez := bs.db.Get(key)
if bytez == nil {
2014-08-10 16:35:08 -07:00
return nil
}
return bytes.NewReader(bytez)
}
// LoadBlock retrieves the serialized block, keyed by height in the
// store's database. If the data at the requested height is not found,
// it returns nil. However, if the block meta data is found but
// cannot be deserialized by wire.ReadBinary, it panics.
// The serialized data consists of the BlockMeta data and different
// parts that are reassembled by their internal Data. If the final
// reassembled data cannot be deserialized by wire.ReadBinary, it panics.
func (bs *BlockStore) LoadBlock(height int64) *types.Block {
2015-11-10 13:10:43 -08:00
var n int
2014-10-18 01:42:33 -07:00
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockMetaKey(height))
if r == nil {
2015-06-16 21:16:58 -07:00
return nil
2015-01-07 01:15:39 -08:00
}
2017-02-14 12:33:14 -08:00
blockMeta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading block meta: %v", err))
}
bytez := []byte{}
2017-02-14 12:33:14 -08:00
for i := 0; i < blockMeta.BlockID.PartsHeader.Total; i++ {
part := bs.LoadBlockPart(height, i)
bytez = append(bytez, part.Bytes...)
}
2015-11-10 13:10:43 -08:00
block := wire.ReadBinary(&types.Block{}, bytes.NewReader(bytez), 0, &n, &err).(*types.Block)
2014-10-18 01:42:33 -07:00
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading block: %v", err))
2014-10-18 01:42:33 -07:00
}
return block
2014-08-10 16:35:08 -07:00
}
// LoadBlockPart tries to load a blockPart from the
// backing database, keyed by height and index.
// If it doesn't find the requested blockPart, it
// returns nil. Otherwise, If the found part is
// corrupted/not deserializable by wire.ReadBinary, it panics.
func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part {
2015-11-10 13:10:43 -08:00
var n int
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockPartKey(height, index))
if r == nil {
2015-06-16 21:16:58 -07:00
return nil
2015-01-07 01:15:39 -08:00
}
2015-11-10 13:10:43 -08:00
part := wire.ReadBinary(&types.Part{}, r, 0, &n, &err).(*types.Part)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading block part: %v", err))
}
return part
}
// LoadBlockMeta tries to load a block meta from the backing database,
// keyed by height. The block meta must have been wire.Binary serialized.
// If it doesn't find the requested meta, it returns nil. Otherwise,
// if the found data cannot be deserialized by wire.ReadBinary, it panics.
func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
2015-11-10 13:10:43 -08:00
var n int
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockMetaKey(height))
if r == nil {
2015-06-16 21:16:58 -07:00
return nil
2015-01-07 01:15:39 -08:00
}
2017-02-14 12:33:14 -08:00
blockMeta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading block meta: %v", err))
}
2017-02-14 12:33:14 -08:00
return blockMeta
}
// LoadBlockCommit tries to load a commit from the backing database,
// keyed by height. The commit must have been wire.Binary serialized.
// If it doesn't find the requested commit in the database, it returns nil.
// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics.
//
2015-06-19 15:30:10 -07:00
// The +2/3 and other Precommit-votes for block at `height`.
2016-04-02 09:10:16 -07:00
// This Commit comes from block.LastCommit for `height+1`.
func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit {
2015-11-10 13:10:43 -08:00
var n int
var err error
2016-04-02 09:10:16 -07:00
r := bs.GetReader(calcBlockCommitKey(height))
2015-01-07 01:15:39 -08:00
if r == nil {
2015-06-16 21:16:58 -07:00
return nil
2015-01-07 01:15:39 -08:00
}
2016-04-02 09:10:16 -07:00
commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading commit: %v", err))
}
2016-04-02 09:10:16 -07:00
return commit
}
// LoadSeenCommit tries to load the seen commit from the backing database,
// keyed by height. The commit must have been wire.Binary serialized.
// If it doesn't find the requested commit in the database, it returns nil.
// Otherwise, if the found data cannot be deserialized by wire.ReadBinary, it panics.
//
2015-06-05 14:15:40 -07:00
// NOTE: the Precommit-vote heights are for the block at `height`
func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit {
2015-11-10 13:10:43 -08:00
var n int
var err error
2016-04-02 09:10:16 -07:00
r := bs.GetReader(calcSeenCommitKey(height))
if r == nil {
2015-06-16 21:16:58 -07:00
return nil
}
2016-04-02 09:10:16 -07:00
commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Error reading commit: %v", err))
}
2016-04-02 09:10:16 -07:00
return commit
}
2016-04-02 09:10:16 -07:00
// blockParts: Must be parts of the block
// seenCommit: The +2/3 precommits that were seen which committed at height.
// If all the nodes restart after committing a block,
// we need this to reload the precommits to catch-up nodes to the
// most recent height. Otherwise they'd stall at H-1.
func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
if block == nil {
PanicSanity("BlockStore can only save a non-nil block")
}
2014-09-14 15:37:32 -07:00
height := block.Height
2016-12-22 18:51:58 -08:00
if height != bs.Height()+1 {
2017-10-03 15:49:20 -07:00
cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height))
2014-08-10 16:35:08 -07:00
}
if !blockParts.IsComplete() {
2017-10-03 15:49:20 -07:00
cmn.PanicSanity(cmn.Fmt("BlockStore can only save complete block part sets"))
}
2014-12-23 01:35:54 -08:00
// Save block meta
2017-02-14 12:33:14 -08:00
blockMeta := types.NewBlockMeta(block, blockParts)
metaBytes := wire.BinaryBytes(blockMeta)
bs.db.Set(calcBlockMetaKey(height), metaBytes)
2014-12-23 01:35:54 -08:00
// Save block parts
for i := 0; i < blockParts.Total(); i++ {
2016-12-22 18:51:58 -08:00
bs.saveBlockPart(height, i, blockParts.GetPart(i))
}
2014-12-23 01:35:54 -08:00
2016-04-02 09:10:16 -07:00
// Save block commit (duplicate and separate from the Block)
blockCommitBytes := wire.BinaryBytes(block.LastCommit)
bs.db.Set(calcBlockCommitKey(height-1), blockCommitBytes)
2016-04-02 09:10:16 -07:00
// Save seen commit (seen +2/3 precommits for block)
2016-11-19 16:32:35 -08:00
// NOTE: we can delete this at a later height
2016-04-02 09:10:16 -07:00
seenCommitBytes := wire.BinaryBytes(seenCommit)
bs.db.Set(calcSeenCommitKey(height), seenCommitBytes)
2014-12-23 01:35:54 -08:00
// Save new BlockStoreStateJSON descriptor
BlockStoreStateJSON{Height: height}.Save(bs.db)
// Done!
2016-12-19 21:45:45 -08:00
bs.mtx.Lock()
bs.height = height
2016-12-19 21:45:45 -08:00
bs.mtx.Unlock()
2016-12-06 02:52:07 -08:00
// Flush
bs.db.SetSync(nil, nil)
2014-08-10 16:35:08 -07:00
}
func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part) {
2016-12-22 18:51:58 -08:00
if height != bs.Height()+1 {
2017-10-03 15:49:20 -07:00
cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height))
}
2015-07-25 15:45:45 -07:00
partBytes := wire.BinaryBytes(part)
bs.db.Set(calcBlockPartKey(height, index), partBytes)
}
//-----------------------------------------------------------------------------
func calcBlockMetaKey(height int64) []byte {
return []byte(fmt.Sprintf("H:%v", height))
2014-08-10 16:35:08 -07:00
}
func calcBlockPartKey(height int64, partIndex int) []byte {
return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
}
func calcBlockCommitKey(height int64) []byte {
2016-04-02 09:10:16 -07:00
return []byte(fmt.Sprintf("C:%v", height))
}
func calcSeenCommitKey(height int64) []byte {
2016-04-02 09:10:16 -07:00
return []byte(fmt.Sprintf("SC:%v", height))
}
//-----------------------------------------------------------------------------
var blockStoreKey = []byte("blockStore")
2014-12-23 01:35:54 -08:00
type BlockStoreStateJSON struct {
Height int64
}
// Save JSON marshals the blockStore state to the database, saving it synchronously.
func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
bytes, err := json.Marshal(bsj)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicSanity(cmn.Fmt("Could not marshal state bytes: %v", err))
}
2016-12-06 02:52:07 -08:00
db.SetSync(blockStoreKey, bytes)
}
// LoadBlockStoreStateJSON JSON unmarshals the
// blockStore state from the database, keyed by
// key "blockStore". If it cannot lookup the state,
// it returns the zero value BlockStoreStateJSON.
func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
bytes := db.Get(blockStoreKey)
if bytes == nil {
2014-12-23 01:35:54 -08:00
return BlockStoreStateJSON{
Height: 0,
}
}
2014-12-23 01:35:54 -08:00
bsj := BlockStoreStateJSON{}
err := json.Unmarshal(bytes, &bsj)
if err != nil {
2017-10-03 15:49:20 -07:00
cmn.PanicCrisis(cmn.Fmt("Could not unmarshal bytes: %X", bytes))
}
return bsj
}