tendermint/block/store.go

247 lines
6.8 KiB
Go
Raw Normal View History

2014-12-17 01:37:13 -08:00
package block
2014-08-10 16:35:08 -07:00
import (
"bytes"
"encoding/json"
"fmt"
2014-08-10 16:35:08 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
2014-10-18 01:42:33 -07:00
db_ "github.com/tendermint/tendermint/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
- Validation: The Validation part of each block, for gossiping commit votes
Currently the commit signatures are duplicated in the Block parts as
well as the Validation. In the future this may change, perhaps by moving
the Validation data outside the Block.
2014-08-10 16:35:08 -07:00
*/
type BlockStore struct {
height uint
2014-10-18 01:42:33 -07:00
db db_.DB
2014-08-10 16:35:08 -07:00
}
2014-10-18 01:42:33 -07:00
func NewBlockStore(db db_.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() uint {
2014-08-10 16:35:08 -07:00
return bs.height
}
2014-12-29 15:14:54 -08:00
func (bs *BlockStore) GetReader(key []byte) Unreader {
bytez := bs.db.Get(key)
if bytez == nil {
2014-08-10 16:35:08 -07:00
return nil
}
return bytes.NewReader(bytez)
}
func (bs *BlockStore) LoadBlock(height uint) *Block {
var n int64
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 {
panic(Fmt("Block does not exist at height %v", height))
}
meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Error reading block meta: %v", err))
}
bytez := []byte{}
for i := uint(0); i < meta.Parts.Total; i++ {
part := bs.LoadBlockPart(height, i)
bytez = append(bytez, part.Bytes...)
}
block := ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
2014-10-18 01:42:33 -07:00
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Error reading block: %v", err))
2014-10-18 01:42:33 -07:00
}
return block
2014-08-10 16:35:08 -07:00
}
func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part {
var n int64
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockPartKey(height, index))
if r == nil {
panic(Fmt("BlockPart does not exist for height %v index %v", height, index))
}
part := ReadBinary(&Part{}, r, &n, &err).(*Part)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Error reading block part: %v", err))
}
return part
}
func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta {
var n int64
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockMetaKey(height))
if r == nil {
panic(Fmt("BlockMeta does not exist for height %v", height))
}
meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Error reading block meta: %v", err))
}
return meta
}
// NOTE: the Commit-vote heights are for the block at `height-1`
// Since these are included in the subsequent block, the height
// is off by 1.
func (bs *BlockStore) LoadBlockValidation(height uint) *Validation {
var n int64
var err error
2015-01-07 01:15:39 -08:00
r := bs.GetReader(calcBlockValidationKey(height))
if r == nil {
panic(Fmt("BlockValidation does not exist for height %v", height))
2015-01-07 01:15:39 -08:00
}
validation := ReadBinary(&Validation{}, r, &n, &err).(*Validation)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Error reading validation: %v", err))
}
return validation
}
// NOTE: the Commit-vote heights are for the block at `height`
func (bs *BlockStore) LoadSeenValidation(height uint) *Validation {
var n int64
var err error
r := bs.GetReader(calcSeenValidationKey(height))
if r == nil {
panic(Fmt("SeenValidation does not exist for height %v", height))
}
validation := ReadBinary(&Validation{}, r, &n, &err).(*Validation)
if err != nil {
panic(Fmt("Error reading validation: %v", err))
}
return validation
}
// blockParts: Must be parts of the block
// seenValidation: The +2/3 commits that were seen which finalized the height.
// If all the nodes restart after committing a block,
// we need this to reload the commits to catch-up nodes to the
// most recent height. Otherwise they'd stall at H-1.
// Also good to have to debug consensus issues & punish wrong-signers
// whose commits weren't included in the block.
func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet, seenValidation *Validation) {
2014-09-14 15:37:32 -07:00
height := block.Height
2014-08-10 16:35:08 -07:00
if height != bs.height+1 {
2014-12-29 18:09:06 -08:00
panic(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() {
2014-12-29 18:09:06 -08:00
panic(Fmt("BlockStore can only save complete block part sets"))
}
2014-12-23 01:35:54 -08:00
// Save block meta
2014-12-23 01:35:54 -08:00
meta := makeBlockMeta(block, blockParts)
metaBytes := BinaryBytes(meta)
bs.db.Set(calcBlockMetaKey(height), metaBytes)
2014-12-23 01:35:54 -08:00
// Save block parts
for i := uint(0); i < blockParts.Total(); i++ {
bs.saveBlockPart(height, i, blockParts.GetPart(i))
}
2014-12-23 01:35:54 -08:00
// Save block validation (duplicate and separate from the Block)
blockValidationBytes := BinaryBytes(block.Validation)
bs.db.Set(calcBlockValidationKey(height), blockValidationBytes)
// Save seen validation (seen +2/3 commits)
seenValidationBytes := BinaryBytes(seenValidation)
bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
2014-12-23 01:35:54 -08:00
// Save new BlockStoreStateJSON descriptor
BlockStoreStateJSON{Height: height}.Save(bs.db)
// Done!
bs.height = height
2014-08-10 16:35:08 -07:00
}
func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) {
if height != bs.height+1 {
2014-12-29 18:09:06 -08:00
panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
}
partBytes := BinaryBytes(part)
bs.db.Set(calcBlockPartKey(height, index), partBytes)
}
//-----------------------------------------------------------------------------
type BlockMeta struct {
2014-12-23 01:35:54 -08:00
Hash []byte // The block hash
Header *Header // The block's Header
2014-12-23 01:35:54 -08:00
Parts PartSetHeader // The PartSetHeader, for transfer
}
func makeBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
return &BlockMeta{
Hash: block.Hash(),
Header: block.Header,
Parts: blockParts.Header(),
}
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
func calcBlockMetaKey(height uint) []byte {
return []byte(fmt.Sprintf("H:%v", height))
2014-08-10 16:35:08 -07:00
}
func calcBlockPartKey(height uint, partIndex uint) []byte {
return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
}
func calcBlockValidationKey(height uint) []byte {
return []byte(fmt.Sprintf("V:%v", height))
}
func calcSeenValidationKey(height uint) []byte {
return []byte(fmt.Sprintf("SV:%v", height))
}
//-----------------------------------------------------------------------------
var blockStoreKey = []byte("blockStore")
2014-12-23 01:35:54 -08:00
type BlockStoreStateJSON struct {
Height uint
}
2014-12-23 01:35:54 -08:00
func (bsj BlockStoreStateJSON) Save(db db_.DB) {
bytes, err := json.Marshal(bsj)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Could not marshal state bytes: %v", err))
}
db.Set(blockStoreKey, bytes)
}
2014-12-23 01:35:54 -08:00
func LoadBlockStoreStateJSON(db db_.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 {
2014-12-29 18:09:06 -08:00
panic(Fmt("Could not unmarshal bytes: %X", bytes))
}
return bsj
}