2015-03-22 19:00:08 -07:00
|
|
|
package types
|
2014-06-04 01:40:17 -07:00
|
|
|
|
|
|
|
import (
|
2014-09-11 22:44:59 -07:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2014-10-13 20:07:26 -07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-09-03 20:41:57 -07:00
|
|
|
"time"
|
2014-08-10 16:35:08 -07:00
|
|
|
|
2018-02-15 14:26:49 -05:00
|
|
|
wire "github.com/tendermint/tendermint/wire"
|
2017-07-09 14:00:14 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-04-21 18:16:05 -04:00
|
|
|
"github.com/tendermint/tmlibs/merkle"
|
2018-02-03 03:23:10 -05:00
|
|
|
"golang.org/x/crypto/ripemd160"
|
2014-06-04 01:40:17 -07:00
|
|
|
)
|
|
|
|
|
2017-10-16 16:01:32 +02:00
|
|
|
// Block defines the atomic unit of a Tendermint blockchain.
|
2017-12-20 23:53:15 -05:00
|
|
|
// TODO: add Version byte
|
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"`
|
2017-07-09 14:10:00 -04:00
|
|
|
Evidence EvidenceData `json:"evidence"`
|
|
|
|
LastCommit *Commit `json:"last_commit"`
|
2014-06-04 01:40:17 -07:00
|
|
|
}
|
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// MakeBlock returns a new block with an empty header, except what can be computed from itself.
|
|
|
|
// It populates the same set of fields validated by ValidateBasic
|
|
|
|
func MakeBlock(height int64, txs []Tx, commit *Commit) *Block {
|
2016-11-06 01:48:39 +00:00
|
|
|
block := &Block{
|
|
|
|
Header: &Header{
|
2017-12-20 23:53:15 -05:00
|
|
|
Height: height,
|
|
|
|
Time: time.Now(),
|
|
|
|
NumTxs: int64(len(txs)),
|
2016-11-06 01:48:39 +00:00
|
|
|
},
|
|
|
|
LastCommit: commit,
|
|
|
|
Data: &Data{
|
|
|
|
Txs: txs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
block.FillHeader()
|
2017-12-20 23:53:15 -05:00
|
|
|
return block
|
2016-11-06 01:48:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:10:00 -04:00
|
|
|
// AddEvidence appends the given evidence to the block
|
|
|
|
func (b *Block) AddEvidence(evidence []Evidence) {
|
2017-11-19 22:18:43 +00:00
|
|
|
b.Evidence.Evidence = append(b.Evidence.Evidence, evidence...)
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// ValidateBasic performs basic validation that doesn't involve state data.
|
2017-12-20 23:53:15 -05:00
|
|
|
// It checks the internal consistency of the block.
|
|
|
|
func (b *Block) ValidateBasic() error {
|
2017-12-12 13:16:03 +01:00
|
|
|
newTxs := int64(len(b.Data.Txs))
|
|
|
|
if b.NumTxs != newTxs {
|
2017-12-19 12:43:15 -06:00
|
|
|
return fmt.Errorf("Wrong Block.Header.NumTxs. Expected %v, got %v", newTxs, b.NumTxs)
|
2017-12-12 13:16:03 +01:00
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
|
2017-12-19 12:43:15 -06:00
|
|
|
return fmt.Errorf("Wrong Block.Header.LastCommitHash. Expected %v, got %v", 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
|
|
|
|
}
|
2014-12-16 05:40:17 -08:00
|
|
|
}
|
2015-08-07 10:43:24 -07:00
|
|
|
if !bytes.Equal(b.DataHash, b.Data.Hash()) {
|
2017-12-19 12:43:15 -06:00
|
|
|
return fmt.Errorf("Wrong Block.Header.DataHash. Expected %v, got %v", b.DataHash, b.Data.Hash())
|
2015-08-07 10:43:24 -07:00
|
|
|
}
|
2017-07-09 14:10:00 -04:00
|
|
|
if !bytes.Equal(b.EvidenceHash, b.Evidence.Hash()) {
|
|
|
|
return errors.New(cmn.Fmt("Wrong Block.Header.EvidenceHash. Expected %v, got %v", b.EvidenceHash, b.Evidence.Hash()))
|
|
|
|
}
|
2014-08-10 16:35:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// FillHeader fills in any remaining header fields that are a function of the block data
|
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 14:56:05 -05:00
|
|
|
}
|
|
|
|
if b.DataHash == nil {
|
|
|
|
b.DataHash = b.Data.Hash()
|
|
|
|
}
|
2017-07-09 14:10:00 -04:00
|
|
|
if b.EvidenceHash == nil {
|
|
|
|
b.EvidenceHash = b.Evidence.Hash()
|
|
|
|
}
|
2015-08-07 10:43:24 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Hash 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.
|
2018-02-03 03:42:59 -05:00
|
|
|
func (b *Block) Hash() cmn.HexBytes {
|
2017-03-05 02:04:09 -05:00
|
|
|
if b == nil || 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
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// MakePartSet returns a PartSet containing parts of a serialized block.
|
|
|
|
// This is the form in which the block is gossipped to peers.
|
2016-09-16 09:20:07 -07:00
|
|
|
func (b *Block) MakePartSet(partSize int) *PartSet {
|
2018-01-14 19:05:22 -05:00
|
|
|
bz, err := wire.MarshalBinary(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return NewPartSetFromData(bz, partSize)
|
2015-03-24 12:00:27 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// HashesTo is a convenience function that checks if a block hashes to the given argument.
|
|
|
|
// A nil block never hashes to anything, and nothing hashes to a nil hash.
|
2014-09-14 15:37:32 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// String returns a string representation of the block
|
2014-10-13 20:07:26 -07:00
|
|
|
func (b *Block) String() string {
|
2014-12-23 01:35:54 -08:00
|
|
|
return b.StringIndented("")
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// StringIndented returns a string representation of the block
|
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"
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return fmt.Sprintf(`Block{
|
|
|
|
%s %v
|
|
|
|
%s %v
|
|
|
|
%s %v
|
2017-07-09 14:10:00 -04:00
|
|
|
%s %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2014-12-23 01:35:54 -08:00
|
|
|
indent, b.Header.StringIndented(indent+" "),
|
|
|
|
indent, b.Data.StringIndented(indent+" "),
|
2017-07-09 14:10:00 -04:00
|
|
|
indent, b.Evidence.StringIndented(indent+" "),
|
2016-04-02 09:10:16 -07:00
|
|
|
indent, b.LastCommit.StringIndented(indent+" "),
|
2015-01-19 14:08:53 -08:00
|
|
|
indent, b.Hash())
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// StringShort returns a shortened string representation of the block
|
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 {
|
2017-05-17 01:08:41 +02:00
|
|
|
return fmt.Sprintf("Block#%v", b.Hash())
|
2014-10-18 01:42:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Header defines the structure of a Tendermint block header
|
2017-12-20 23:53:15 -05:00
|
|
|
// TODO: limit header size
|
2017-07-09 14:10:00 -04:00
|
|
|
// NOTE: changes to the Header should be duplicated in the abci Header
|
2014-06-05 02:34:45 -07:00
|
|
|
type Header struct {
|
2017-12-20 23:53:15 -05:00
|
|
|
// basic block info
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
NumTxs int64 `json:"num_txs"`
|
|
|
|
|
|
|
|
// prev block info
|
|
|
|
LastBlockID BlockID `json:"last_block_id"`
|
|
|
|
TotalTxs int64 `json:"total_txs"`
|
|
|
|
|
|
|
|
// hashes of block data
|
2018-02-03 03:42:59 -05:00
|
|
|
LastCommitHash cmn.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
|
|
|
|
DataHash cmn.HexBytes `json:"data_hash"` // transactions
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2017-12-25 13:47:16 -05:00
|
|
|
// hashes from the app output from the prev block
|
2018-02-03 03:42:59 -05:00
|
|
|
ValidatorsHash cmn.HexBytes `json:"validators_hash"` // validators for the current block
|
|
|
|
ConsensusHash cmn.HexBytes `json:"consensus_hash"` // consensus params for current block
|
|
|
|
AppHash cmn.HexBytes `json:"app_hash"` // state after txs from the previous block
|
|
|
|
LastResultsHash cmn.HexBytes `json:"last_results_hash"` // root hash of all results from the txs from the previous block
|
2017-07-09 14:10:00 -04:00
|
|
|
|
|
|
|
// consensus info
|
2018-02-03 03:42:59 -05:00
|
|
|
EvidenceHash cmn.HexBytes `json:"evidence_hash"` // evidence included in the block
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Hash returns the hash of the header.
|
2017-10-23 19:51:09 -04:00
|
|
|
// Returns nil if ValidatorHash is missing.
|
2018-02-03 03:42:59 -05:00
|
|
|
func (h *Header) Hash() cmn.HexBytes {
|
2018-03-10 21:44:05 -08:00
|
|
|
if h == nil || len(h.ValidatorsHash) == 0 {
|
2015-03-26 10:58:20 -07:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-03 03:23:10 -05:00
|
|
|
return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
|
|
|
|
"ChainID": wireHasher(h.ChainID),
|
|
|
|
"Height": wireHasher(h.Height),
|
|
|
|
"Time": wireHasher(h.Time),
|
|
|
|
"NumTxs": wireHasher(h.NumTxs),
|
|
|
|
"TotalTxs": wireHasher(h.TotalTxs),
|
|
|
|
"LastBlockID": wireHasher(h.LastBlockID),
|
|
|
|
"LastCommit": wireHasher(h.LastCommitHash),
|
|
|
|
"Data": wireHasher(h.DataHash),
|
|
|
|
"Validators": wireHasher(h.ValidatorsHash),
|
|
|
|
"App": wireHasher(h.AppHash),
|
|
|
|
"Consensus": wireHasher(h.ConsensusHash),
|
|
|
|
"Results": wireHasher(h.LastResultsHash),
|
|
|
|
"Evidence": wireHasher(h.EvidenceHash),
|
2015-08-07 10:43:24 -07:00
|
|
|
})
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// StringIndented returns a string representation of the header
|
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"
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return fmt.Sprintf(`Header{
|
2015-05-29 14:13:45 -04:00
|
|
|
%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
|
2017-12-12 13:16:03 +01:00
|
|
|
%s TotalTxs: %v
|
2016-08-16 14:59:19 -07:00
|
|
|
%s LastBlockID: %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s LastCommit: %v
|
|
|
|
%s Data: %v
|
|
|
|
%s Validators: %v
|
|
|
|
%s App: %v
|
2017-12-22 16:43:45 +01:00
|
|
|
%s Conensus: %v
|
|
|
|
%s Results: %v
|
2017-07-09 14:10:00 -04:00
|
|
|
%s Evidence: %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2015-05-29 14:13:45 -04:00
|
|
|
indent, h.ChainID,
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, h.Height,
|
|
|
|
indent, h.Time,
|
2014-12-23 01:35:54 -08:00
|
|
|
indent, h.NumTxs,
|
2017-12-12 13:16:03 +01:00
|
|
|
indent, h.TotalTxs,
|
2016-08-16 14:59:19 -07:00
|
|
|
indent, h.LastBlockID,
|
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,
|
2017-12-14 10:28:04 +01:00
|
|
|
indent, h.ConsensusHash,
|
2017-12-26 19:53:26 -05:00
|
|
|
indent, h.LastResultsHash,
|
2017-07-09 14:10:00 -04:00
|
|
|
indent, h.EvidenceHash,
|
2015-01-19 14:08:53 -08:00
|
|
|
indent, h.Hash())
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
//-------------------------------------
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Commit contains the evidence that a block was committed by a set of validators.
|
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.
|
2016-09-05 17:33:02 -07:00
|
|
|
BlockID BlockID `json:"blockID"`
|
2015-06-19 15:30:10 -07:00
|
|
|
Precommits []*Vote `json:"precommits"`
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
// Volatile
|
2015-06-26 17:48:24 -07:00
|
|
|
firstPrecommit *Vote
|
2018-02-03 03:42:59 -05:00
|
|
|
hash cmn.HexBytes
|
2017-07-09 14:00:14 -04:00
|
|
|
bitArray *cmn.BitArray
|
2015-06-26 17:48:24 -07:00
|
|
|
}
|
|
|
|
|
2018-03-02 04:10:22 -05:00
|
|
|
// FirstPrecommit returns the first non-nil precommit in the commit.
|
|
|
|
// If all precommits are nil, it returns an empty precommit with height 0.
|
2016-04-02 09:10:16 -07:00
|
|
|
func (commit *Commit) FirstPrecommit() *Vote {
|
|
|
|
if len(commit.Precommits) == 0 {
|
2015-06-26 17:48:24 -07:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
if commit.firstPrecommit != nil {
|
|
|
|
return commit.firstPrecommit
|
2015-06-26 17:48:24 -07:00
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
for _, precommit := range commit.Precommits {
|
2015-06-26 17:48:24 -07:00
|
|
|
if precommit != nil {
|
2016-04-02 09:10:16 -07:00
|
|
|
commit.firstPrecommit = precommit
|
2015-06-26 17:48:24 -07:00
|
|
|
return precommit
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 04:10:22 -05:00
|
|
|
return &Vote{
|
|
|
|
Type: VoteTypePrecommit,
|
|
|
|
}
|
2014-06-05 11:04:56 -07:00
|
|
|
}
|
2014-06-05 02:34:45 -07:00
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Height returns the height of the commit
|
2017-12-01 19:04:53 -06:00
|
|
|
func (commit *Commit) Height() int64 {
|
2016-04-02 09:10:16 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Round returns the round of the commit
|
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
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Type returns the vote type of the commit, which is always VoteTypePrecommit
|
2016-04-02 09:10:16 -07:00
|
|
|
func (commit *Commit) Type() byte {
|
2015-07-05 13:40:59 -07:00
|
|
|
return VoteTypePrecommit
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Size returns the number of votes in the commit
|
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)
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// BitArray returns a BitArray of which validators voted in this commit
|
|
|
|
func (commit *Commit) BitArray() *cmn.BitArray {
|
2016-04-02 09:10:16 -07:00
|
|
|
if commit.bitArray == nil {
|
2017-07-09 14:00:14 -04:00
|
|
|
commit.bitArray = cmn.NewBitArray(len(commit.Precommits))
|
2016-04-02 09:10:16 -07:00
|
|
|
for i, precommit := range commit.Precommits {
|
2017-07-09 14:00:14 -04:00
|
|
|
// TODO: need to check the BlockID otherwise we could be counting conflicts,
|
|
|
|
// not just the one with +2/3 !
|
2016-04-02 09:10:16 -07:00
|
|
|
commit.bitArray.SetIndex(i, precommit != nil)
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
return commit.bitArray
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// GetByIndex returns the vote corresponding to a given validator index
|
2016-04-02 09:10:16 -07:00
|
|
|
func (commit *Commit) GetByIndex(index int) *Vote {
|
|
|
|
return commit.Precommits[index]
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// IsCommit returns true if there is at least one vote
|
2016-04-02 09:10:16 -07:00
|
|
|
func (commit *Commit) IsCommit() bool {
|
2017-05-30 11:00:40 -04:00
|
|
|
return len(commit.Precommits) != 0
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// ValidateBasic performs basic validation that doesn't involve state data.
|
2016-04-02 09:10:16 -07:00
|
|
|
func (commit *Commit) ValidateBasic() error {
|
2016-09-05 17:33:02 -07:00
|
|
|
if commit.BlockID.IsZero() {
|
|
|
|
return errors.New("Commit cannot be for nil block")
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
if len(commit.Precommits) == 0 {
|
|
|
|
return errors.New("No precommits in commit")
|
2014-12-16 05:40:17 -08:00
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
height, round := commit.Height(), commit.Round()
|
2016-11-23 18:20:46 -05:00
|
|
|
|
|
|
|
// validate the precommits
|
2016-04-02 09:10:16 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 05:40:17 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Hash returns the hash of the commit
|
2018-02-03 03:42:59 -05:00
|
|
|
func (commit *Commit) Hash() cmn.HexBytes {
|
2016-04-02 09:10:16 -07:00
|
|
|
if commit.hash == nil {
|
2018-02-03 03:23:10 -05:00
|
|
|
bs := make([]merkle.Hasher, len(commit.Precommits))
|
2016-04-02 09:10:16 -07:00
|
|
|
for i, precommit := range commit.Precommits {
|
2018-02-03 03:23:10 -05:00
|
|
|
bs[i] = wireHasher(precommit)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2018-02-03 03:23:10 -05:00
|
|
|
commit.hash = merkle.SimpleHashFromHashers(bs)
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
return commit.hash
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// StringIndented returns a string representation of the commit
|
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{
|
2016-09-05 17:33:02 -07:00
|
|
|
%s BlockID: %v
|
2015-06-05 14:15:40 -07:00
|
|
|
%s Precommits: %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2016-09-05 17:33:02 -07:00
|
|
|
indent, commit.BlockID,
|
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
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 16:43:18 +02:00
|
|
|
// SignedHeader is a header along with the commits that prove it
|
|
|
|
type SignedHeader struct {
|
|
|
|
Header *Header `json:"header"`
|
|
|
|
Commit *Commit `json:"commit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Data contains the set of transactions included in the block
|
2014-09-11 01:11:41 -07:00
|
|
|
type Data struct {
|
2016-01-06 17:14:20 -08:00
|
|
|
|
|
|
|
// 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 13:01:08 -05:00
|
|
|
Txs Txs `json:"txs"`
|
2014-08-10 16:35:08 -07:00
|
|
|
|
|
|
|
// Volatile
|
2018-02-03 03:42:59 -05:00
|
|
|
hash cmn.HexBytes
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Hash returns the hash of the data
|
2018-02-03 03:42:59 -05:00
|
|
|
func (data *Data) Hash() cmn.HexBytes {
|
2018-03-10 21:44:05 -08:00
|
|
|
if data == nil {
|
|
|
|
return (Txs{}).Hash()
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
if data.hash == nil {
|
2016-03-12 13:01:08 -05:00
|
|
|
data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return data.hash
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// StringIndented returns a string representation of the transactions
|
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"
|
|
|
|
}
|
2017-07-09 14:00:14 -04:00
|
|
|
txStrings := make([]string, cmn.MinInt(len(data.Txs), 21))
|
2014-10-13 20:07:26 -07:00
|
|
|
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
|
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
txStrings[i] = fmt.Sprintf("Tx:%v", tx)
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
return fmt.Sprintf(`Data{
|
|
|
|
%s %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, strings.Join(txStrings, "\n"+indent+" "),
|
|
|
|
indent, data.hash)
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
2016-08-16 14:59:19 -07:00
|
|
|
|
2017-07-09 14:10:00 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// EvidenceData contains any evidence of malicious wrong-doing by validators
|
|
|
|
type EvidenceData struct {
|
2017-11-19 22:18:43 +00:00
|
|
|
Evidence EvidenceList `json:"evidence"`
|
2017-07-09 14:10:00 -04:00
|
|
|
|
|
|
|
// Volatile
|
2018-02-03 03:42:59 -05:00
|
|
|
hash cmn.HexBytes
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
|
2017-08-28 20:01:34 -04:00
|
|
|
// Hash returns the hash of the data.
|
2018-02-03 03:42:59 -05:00
|
|
|
func (data *EvidenceData) Hash() cmn.HexBytes {
|
2017-07-09 14:10:00 -04:00
|
|
|
if data.hash == nil {
|
2017-11-19 22:18:43 +00:00
|
|
|
data.hash = data.Evidence.Hash()
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
return data.hash
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:01:34 -04:00
|
|
|
// StringIndented returns a string representation of the evidence.
|
2017-07-09 14:10:00 -04:00
|
|
|
func (data *EvidenceData) StringIndented(indent string) string {
|
|
|
|
if data == nil {
|
2017-08-28 20:01:34 -04:00
|
|
|
return "nil-Evidence"
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
2017-11-19 22:18:43 +00:00
|
|
|
evStrings := make([]string, cmn.MinInt(len(data.Evidence), 21))
|
|
|
|
for i, ev := range data.Evidence {
|
2017-08-28 20:01:34 -04:00
|
|
|
if i == 20 {
|
2017-11-19 22:18:43 +00:00
|
|
|
evStrings[i] = fmt.Sprintf("... (%v total)", len(data.Evidence))
|
2017-08-28 20:01:34 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
evStrings[i] = fmt.Sprintf("Evidence:%v", ev)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`Data{
|
|
|
|
%s %v
|
|
|
|
%s}#%v`,
|
|
|
|
indent, strings.Join(evStrings, "\n"+indent+" "),
|
|
|
|
indent, data.hash)
|
2017-07-09 14:10:00 -04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-08-16 14:59:19 -07:00
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// BlockID defines the unique ID of a block as its Hash and its PartSetHeader
|
2016-08-16 14:59:19 -07:00
|
|
|
type BlockID struct {
|
2018-02-03 03:42:59 -05:00
|
|
|
Hash cmn.HexBytes `json:"hash"`
|
2016-08-16 14:59:19 -07:00
|
|
|
PartsHeader PartSetHeader `json:"parts"`
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// IsZero returns true if this is the BlockID for a nil-block
|
2016-08-16 14:59:19 -07:00
|
|
|
func (blockID BlockID) IsZero() bool {
|
|
|
|
return len(blockID.Hash) == 0 && blockID.PartsHeader.IsZero()
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Equals returns true if the BlockID matches the given BlockID
|
2016-08-16 14:59:19 -07:00
|
|
|
func (blockID BlockID) Equals(other BlockID) bool {
|
|
|
|
return bytes.Equal(blockID.Hash, other.Hash) &&
|
|
|
|
blockID.PartsHeader.Equals(other.PartsHeader)
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Key returns a machine-readable string representation of the BlockID
|
2016-08-16 14:59:19 -07:00
|
|
|
func (blockID BlockID) Key() string {
|
2018-01-14 19:05:22 -05:00
|
|
|
bz, err := wire.MarshalBinary(blockID.PartsHeader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-08-20 15:08:26 -07:00
|
|
|
}
|
2018-01-14 19:05:22 -05:00
|
|
|
return string(blockID.Hash) + string(bz)
|
2016-08-16 14:59:19 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// String returns a human readable string representation of the BlockID
|
2016-08-16 14:59:19 -07:00
|
|
|
func (blockID BlockID) String() string {
|
2017-05-17 01:08:41 +02:00
|
|
|
return fmt.Sprintf(`%v:%v`, blockID.Hash, blockID.PartsHeader)
|
2016-08-16 14:59:19 -07:00
|
|
|
}
|
2018-02-03 03:23:10 -05:00
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
|
|
type hasher struct {
|
|
|
|
item interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h hasher) Hash() []byte {
|
2018-02-15 14:26:49 -05:00
|
|
|
hasher := ripemd160.New()
|
|
|
|
bz, err := wire.MarshalBinary(h.item)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = hasher.Write(bz)
|
|
|
|
if err != nil {
|
2018-02-03 03:23:10 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return hasher.Sum(nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:05:22 -05:00
|
|
|
func tmHash(item interface{}) []byte {
|
|
|
|
h := hasher{item}
|
|
|
|
return h.Hash()
|
|
|
|
}
|
|
|
|
|
2018-02-03 03:23:10 -05:00
|
|
|
func wireHasher(item interface{}) merkle.Hasher {
|
|
|
|
return hasher{item}
|
|
|
|
}
|