tendermint/types/block.go

363 lines
9.2 KiB
Go
Raw Normal View History

package types
2014-06-04 01:40:17 -07:00
import (
2014-09-11 22:44:59 -07:00
"bytes"
"errors"
"fmt"
"strings"
"time"
2014-08-10 16:35:08 -07:00
. "github.com/tendermint/go-common"
"github.com/tendermint/go-merkle"
"github.com/tendermint/go-wire"
2014-06-04 01:40:17 -07:00
)
2015-11-10 13:10:43 -08:00
const MaxBlockSize = 22020096 // 21MB TODO make it configurable
2014-06-05 02:34:45 -07:00
type Block struct {
2016-04-02 09:10:16 -07:00
*Header `json:"header"`
*Data `json:"data"`
LastCommit *Commit `json:"last_commit"`
2014-06-04 01:40:17 -07:00
}
2014-09-11 22:44:59 -07:00
// Basic validation that doesn't involve state data.
func (b *Block) ValidateBasic(chainID string, lastBlockHeight int, lastBlockHash []byte,
lastBlockParts PartSetHeader, lastBlockTime time.Time, appHash []byte) error {
2015-05-29 14:53:57 -07:00
if b.ChainID != chainID {
2015-07-09 21:46:15 -07:00
return errors.New(Fmt("Wrong Block.Header.ChainID. Expected %v, got %v", chainID, b.ChainID))
}
if b.Height != lastBlockHeight+1 {
2015-07-09 21:46:15 -07:00
return errors.New(Fmt("Wrong Block.Header.Height. Expected %v, got %v", lastBlockHeight+1, b.Height))
2014-12-23 01:35:54 -08:00
}
2015-08-07 10:43:24 -07:00
/* TODO: Determine bounds for Time
See blockchain/reactor "stopSyncingDurationMinutes"
if !b.Time.After(lastBlockTime) {
return errors.New("Invalid Block.Header.Time")
}
*/
if b.NumTxs != len(b.Data.Txs) {
2015-07-09 21:46:15 -07:00
return errors.New(Fmt("Wrong Block.Header.NumTxs. Expected %v, got %v", len(b.Data.Txs), b.NumTxs))
}
if !bytes.Equal(b.LastBlockHash, lastBlockHash) {
2015-07-09 21:46:15 -07:00
return errors.New(Fmt("Wrong Block.Header.LastBlockHash. Expected %X, got %X", lastBlockHash, b.LastBlockHash))
2014-09-11 22:44:59 -07:00
}
if !b.LastBlockParts.Equals(lastBlockParts) {
2015-07-09 21:46:15 -07:00
return errors.New(Fmt("Wrong Block.Header.LastBlockParts. Expected %v, got %v", lastBlockParts, b.LastBlockParts))
2014-09-11 22:44:59 -07:00
}
2016-04-02 09:10:16 -07:00
if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
return errors.New(Fmt("Wrong Block.Header.LastCommitHash. Expected %X, got %X", b.LastCommitHash, b.LastCommit.Hash()))
2015-08-07 10:43:24 -07:00
}
2014-12-17 01:37:13 -08:00
if b.Header.Height != 1 {
2016-04-02 09:10:16 -07:00
if err := b.LastCommit.ValidateBasic(); err != nil {
2014-12-17 01:37:13 -08:00
return err
}
}
2015-08-07 10:43:24 -07:00
if !bytes.Equal(b.DataHash, b.Data.Hash()) {
return errors.New(Fmt("Wrong Block.Header.DataHash. Expected %X, got %X", b.DataHash, b.Data.Hash()))
}
if !bytes.Equal(b.AppHash, appHash) {
return errors.New(Fmt("Wrong Block.Header.AppHash. Expected %X, got %X", appHash, b.AppHash))
}
2015-12-01 20:12:01 -08:00
// NOTE: the AppHash and ValidatorsHash are validated later.
2014-08-10 16:35:08 -07:00
return nil
}
2015-08-07 10:43:24 -07:00
func (b *Block) FillHeader() {
2016-04-02 09:10:16 -07:00
if b.LastCommitHash == nil {
b.LastCommitHash = b.LastCommit.Hash()
2015-12-13 11:56:05 -08:00
}
if b.DataHash == nil {
b.DataHash = b.Data.Hash()
}
2015-08-07 10:43:24 -07:00
}
2015-03-26 10:58:20 -07:00
// Computes and returns the block hash.
2015-12-01 20:12:01 -08:00
// If the block is incomplete, block hash is nil for safety.
2014-08-10 16:35:08 -07:00
func (b *Block) Hash() []byte {
2016-05-08 15:00:58 -07:00
// fmt.Println(">>", b.Data)
2016-04-02 09:10:16 -07:00
if b.Header == nil || b.Data == nil || b.LastCommit == nil {
2015-03-22 12:46:53 -07:00
return nil
}
2015-08-07 10:43:24 -07:00
b.FillHeader()
return b.Header.Hash()
2014-08-10 16:35:08 -07:00
}
func (b *Block) MakePartSet() *PartSet {
2015-07-25 15:45:45 -07:00
return NewPartSetFromData(wire.BinaryBytes(b))
}
2014-09-14 15:37:32 -07:00
// Convenience.
// A nil block never hashes to anything.
// Nothing hashes to a nil hash.
func (b *Block) HashesTo(hash []byte) bool {
if len(hash) == 0 {
return false
2014-08-10 16:35:08 -07:00
}
2014-09-14 15:37:32 -07:00
if b == nil {
return false
}
return bytes.Equal(b.Hash(), hash)
2014-08-10 16:35:08 -07:00
}
func (b *Block) String() string {
2014-12-23 01:35:54 -08:00
return b.StringIndented("")
}
2014-12-23 01:35:54 -08:00
func (b *Block) StringIndented(indent string) string {
2015-03-22 12:46:53 -07:00
if b == nil {
return "nil-Block"
}
return fmt.Sprintf(`Block{
%s %v
%s %v
%s %v
%s}#%X`,
2014-12-23 01:35:54 -08:00
indent, b.Header.StringIndented(indent+" "),
indent, b.Data.StringIndented(indent+" "),
2016-04-02 09:10:16 -07:00
indent, b.LastCommit.StringIndented(indent+" "),
indent, b.Hash())
}
2014-12-23 01:35:54 -08:00
func (b *Block) StringShort() string {
2014-10-18 01:42:33 -07:00
if b == nil {
return "nil-Block"
} else {
return fmt.Sprintf("Block#%X", b.Hash())
}
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type Header struct {
2016-04-02 09:10:16 -07:00
ChainID string `json:"chain_id"`
Height int `json:"height"`
Time time.Time `json:"time"`
NumTxs int `json:"num_txs"`
LastBlockHash []byte `json:"last_block_hash"`
LastBlockParts PartSetHeader `json:"last_block_parts"`
LastCommitHash []byte `json:"last_commit_hash"`
DataHash []byte `json:"data_hash"`
ValidatorsHash []byte `json:"validators_hash"`
AppHash []byte `json:"app_hash"` // state merkle root of txs from the previous block
2014-06-05 02:34:45 -07:00
}
2015-03-26 10:58:20 -07:00
// NOTE: hash is nil if required fields are missing.
2014-08-10 16:35:08 -07:00
func (h *Header) Hash() []byte {
2015-12-01 20:12:01 -08:00
if len(h.ValidatorsHash) == 0 {
2015-03-26 10:58:20 -07:00
return nil
}
2015-08-07 10:43:24 -07:00
return merkle.SimpleHashFromMap(map[string]interface{}{
"ChainID": h.ChainID,
"Height": h.Height,
"Time": h.Time,
"NumTxs": h.NumTxs,
"LastBlock": h.LastBlockHash,
"LastBlockParts": h.LastBlockParts,
2016-04-02 09:10:16 -07:00
"LastCommit": h.LastCommitHash,
2015-08-07 10:43:24 -07:00
"Data": h.DataHash,
2015-12-01 20:12:01 -08:00
"Validators": h.ValidatorsHash,
"App": h.AppHash,
2015-08-07 10:43:24 -07:00
})
}
2014-12-23 01:35:54 -08:00
func (h *Header) StringIndented(indent string) string {
2015-03-22 12:46:53 -07:00
if h == nil {
return "nil-Header"
}
return fmt.Sprintf(`Header{
%s ChainID: %v
2014-10-30 03:32:09 -07:00
%s Height: %v
%s Time: %v
2016-04-02 09:10:16 -07:00
%s NumTxs: %v
2015-12-01 20:12:01 -08:00
%s LastBlock: %X
2014-10-30 03:32:09 -07:00
%s LastBlockParts: %v
2016-04-02 09:10:16 -07:00
%s LastCommit: %X
2015-12-01 20:12:01 -08:00
%s Data: %X
%s Validators: %X
%s App: %X
%s}#%X`,
indent, h.ChainID,
indent, h.Height,
indent, h.Time,
2014-12-23 01:35:54 -08:00
indent, h.NumTxs,
indent, h.LastBlockHash,
2014-10-30 03:32:09 -07:00
indent, h.LastBlockParts,
2016-04-02 09:10:16 -07:00
indent, h.LastCommitHash,
2015-12-01 20:12:01 -08:00
indent, h.DataHash,
indent, h.ValidatorsHash,
indent, h.AppHash,
indent, h.Hash())
2014-08-10 16:35:08 -07:00
}
//-------------------------------------
2016-04-02 09:10:16 -07:00
// NOTE: Commit is empty for height 1, but never nil.
type Commit struct {
2015-06-19 15:30:10 -07:00
// NOTE: The Precommits are in order of address to preserve the bonded ValidatorSet order.
// Any peer with a block can gossip precommits by index with a peer without recalculating the
// active ValidatorSet.
Precommits []*Vote `json:"precommits"`
// Volatile
firstPrecommit *Vote
hash []byte
bitArray *BitArray
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) FirstPrecommit() *Vote {
if len(commit.Precommits) == 0 {
return nil
}
2016-04-02 09:10:16 -07:00
if commit.firstPrecommit != nil {
return commit.firstPrecommit
}
2016-04-02 09:10:16 -07:00
for _, precommit := range commit.Precommits {
if precommit != nil {
2016-04-02 09:10:16 -07:00
commit.firstPrecommit = precommit
return precommit
}
}
return nil
2014-06-05 11:04:56 -07:00
}
2014-06-05 02:34:45 -07:00
2016-04-02 09:10:16 -07:00
func (commit *Commit) Height() int {
if len(commit.Precommits) == 0 {
2015-06-19 15:30:10 -07:00
return 0
}
2016-04-02 09:10:16 -07:00
return commit.FirstPrecommit().Height
2015-06-19 15:30:10 -07:00
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) Round() int {
if len(commit.Precommits) == 0 {
2015-06-19 15:30:10 -07:00
return 0
}
2016-04-02 09:10:16 -07:00
return commit.FirstPrecommit().Round
2015-06-19 15:30:10 -07:00
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) Type() byte {
return VoteTypePrecommit
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) Size() int {
if commit == nil {
2015-07-05 15:47:30 -07:00
return 0
}
2016-04-02 09:10:16 -07:00
return len(commit.Precommits)
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) BitArray() *BitArray {
if commit.bitArray == nil {
commit.bitArray = NewBitArray(len(commit.Precommits))
for i, precommit := range commit.Precommits {
commit.bitArray.SetIndex(i, precommit != nil)
}
}
2016-04-02 09:10:16 -07:00
return commit.bitArray
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) GetByIndex(index int) *Vote {
return commit.Precommits[index]
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) IsCommit() bool {
if len(commit.Precommits) == 0 {
return false
}
return true
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) ValidateBasic() error {
if len(commit.Precommits) == 0 {
return errors.New("No precommits in commit")
}
2016-04-02 09:10:16 -07:00
height, round := commit.Height(), commit.Round()
for _, precommit := range commit.Precommits {
2015-06-24 14:04:40 -07:00
// It's OK for precommits to be missing.
if precommit == nil {
continue
}
2015-06-21 19:11:21 -07:00
// Ensure that all votes are precommits
if precommit.Type != VoteTypePrecommit {
2016-04-02 09:10:16 -07:00
return fmt.Errorf("Invalid commit vote. Expected precommit, got %v",
2015-06-21 19:11:21 -07:00
precommit.Type)
}
// Ensure that all heights are the same
if precommit.Height != height {
2016-04-02 09:10:16 -07:00
return fmt.Errorf("Invalid commit precommit height. Expected %v, got %v",
2015-06-21 19:11:21 -07:00
height, precommit.Height)
}
// Ensure that all rounds are the same
if precommit.Round != round {
2016-04-02 09:10:16 -07:00
return fmt.Errorf("Invalid commit precommit round. Expected %v, got %v",
2015-06-21 19:11:21 -07:00
round, precommit.Round)
}
}
return nil
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) Hash() []byte {
if commit.hash == nil {
bs := make([]interface{}, len(commit.Precommits))
for i, precommit := range commit.Precommits {
2015-06-24 14:04:40 -07:00
bs[i] = precommit
2014-08-10 16:35:08 -07:00
}
2016-04-02 09:10:16 -07:00
commit.hash = merkle.SimpleHashFromBinaries(bs)
}
2016-04-02 09:10:16 -07:00
return commit.hash
}
2016-04-02 09:10:16 -07:00
func (commit *Commit) StringIndented(indent string) string {
if commit == nil {
return "nil-Commit"
2015-03-22 12:46:53 -07:00
}
2016-04-02 09:10:16 -07:00
precommitStrings := make([]string, len(commit.Precommits))
for i, precommit := range commit.Precommits {
2015-06-05 14:15:40 -07:00
precommitStrings[i] = precommit.String()
2014-08-10 16:35:08 -07:00
}
2016-04-02 09:10:16 -07:00
return fmt.Sprintf(`Commit{
2015-06-05 14:15:40 -07:00
%s Precommits: %v
%s}#%X`,
2015-06-05 14:15:40 -07:00
indent, strings.Join(precommitStrings, "\n"+indent+" "),
2016-04-02 09:10:16 -07:00
indent, commit.hash)
2014-08-10 16:35:08 -07:00
}
//-----------------------------------------------------------------------------
type Data struct {
// Txs that will be applied by state @ block.Height+1.
// NOTE: not all txs here are valid. We're just agreeing on the order first.
// This means that block.AppHash does not include these txs.
2016-03-12 10:01:08 -08:00
Txs Txs `json:"txs"`
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-05 02:34:45 -07:00
}
func (data *Data) Hash() []byte {
if data.hash == nil {
2016-03-12 10:01:08 -08:00
data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
2014-07-01 14:50:24 -07:00
}
return data.hash
}
2014-12-23 01:35:54 -08:00
func (data *Data) StringIndented(indent string) string {
2015-03-22 12:46:53 -07:00
if data == nil {
return "nil-Data"
}
2015-12-09 17:09:06 -08:00
txStrings := make([]string, MinInt(len(data.Txs), 21))
for i, tx := range data.Txs {
2015-12-09 17:09:06 -08:00
if i == 20 {
txStrings[i] = fmt.Sprintf("... (%v total)", len(data.Txs))
break
}
txStrings[i] = fmt.Sprintf("Tx:%v", tx)
}
return fmt.Sprintf(`Data{
%s %v
%s}#%X`,
indent, strings.Join(txStrings, "\n"+indent+" "),
indent, data.hash)
2014-06-05 02:34:45 -07:00
}