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"
|
2014-10-13 20:07:26 -07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-04-06 17:02:29 -07:00
|
|
|
"sync"
|
2014-09-03 20:41:57 -07:00
|
|
|
"time"
|
2014-08-10 16:35:08 -07:00
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-07-01 01:40:03 -04:00
|
|
|
"github.com/tendermint/tendermint/crypto/merkle"
|
2019-01-13 23:56:36 +01:00
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
2018-07-01 22:36:49 -04:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2018-10-17 15:30:53 -04:00
|
|
|
"github.com/tendermint/tendermint/version"
|
2014-06-04 01:40:17 -07:00
|
|
|
)
|
|
|
|
|
2018-08-08 16:03:58 +04:00
|
|
|
const (
|
|
|
|
// MaxHeaderBytes is a maximum header size (including amino overhead).
|
2018-10-31 12:42:05 -04:00
|
|
|
MaxHeaderBytes int64 = 653
|
2018-08-08 16:03:58 +04:00
|
|
|
|
2018-08-30 13:07:39 +04:00
|
|
|
// MaxAminoOverheadForBlock - maximum amino overhead to encode a block (up to
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
// MaxBlockSizeBytes in size) not including it's parts except Data.
|
2018-11-11 16:09:33 +01:00
|
|
|
// This means it also excludes the overhead for individual transactions.
|
|
|
|
// To compute individual transactions' overhead use types.ComputeAminoOverhead(tx types.Tx, fieldNum int).
|
2018-08-31 15:55:30 +04:00
|
|
|
//
|
|
|
|
// Uvarint length of MaxBlockSizeBytes: 4 bytes
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
// 2 fields (2 embedded): 2 bytes
|
|
|
|
// Uvarint length of Data.Txs: 4 bytes
|
|
|
|
// Data.Txs field: 1 byte
|
2018-09-21 13:00:36 +04:00
|
|
|
MaxAminoOverheadForBlock int64 = 11
|
2018-08-08 16:03:58 +04:00
|
|
|
)
|
|
|
|
|
2017-10-16 16:01:32 +02:00
|
|
|
// Block defines the atomic unit of a Tendermint blockchain.
|
2014-06-05 02:34:45 -07:00
|
|
|
type Block struct {
|
2018-04-06 17:02:29 -07:00
|
|
|
mtx sync.Mutex
|
2018-07-13 12:05:54 +04: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-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.
|
2018-11-01 07:07:18 +01:00
|
|
|
// Further validation is done using state#ValidateBlock.
|
2017-12-20 23:53:15 -05:00
|
|
|
func (b *Block) ValidateBasic() error {
|
2018-04-06 17:03:31 -07:00
|
|
|
if b == nil {
|
2018-11-01 07:07:18 +01:00
|
|
|
return errors.New("nil block")
|
2018-04-06 17:03:31 -07:00
|
|
|
}
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
if len(b.ChainID) > MaxChainIDLen {
|
|
|
|
return fmt.Errorf("ChainID is too long. Max is %d, got %d", MaxChainIDLen, len(b.ChainID))
|
|
|
|
}
|
|
|
|
|
2018-10-12 09:03:58 +04:00
|
|
|
if b.Height < 0 {
|
2018-11-01 07:07:18 +01:00
|
|
|
return errors.New("Negative Header.Height")
|
|
|
|
} else if b.Height == 0 {
|
|
|
|
return errors.New("Zero Header.Height")
|
2018-10-12 09:03:58 +04:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// NOTE: Timestamp validation is subtle and handled elsewhere.
|
|
|
|
|
2017-12-12 13:16:03 +01:00
|
|
|
newTxs := int64(len(b.Data.Txs))
|
|
|
|
if b.NumTxs != newTxs {
|
2018-11-01 07:07:18 +01:00
|
|
|
return fmt.Errorf("Wrong Header.NumTxs. Expected %v, got %v",
|
2018-07-30 12:59:16 +04:00
|
|
|
newTxs,
|
|
|
|
b.NumTxs,
|
|
|
|
)
|
2017-12-12 13:16:03 +01:00
|
|
|
}
|
2018-11-01 07:07:18 +01:00
|
|
|
|
|
|
|
// TODO: fix tests so we can do this
|
|
|
|
/*if b.TotalTxs < b.NumTxs {
|
|
|
|
return fmt.Errorf("Header.TotalTxs (%d) is less than Header.NumTxs (%d)", b.TotalTxs, b.NumTxs)
|
|
|
|
}*/
|
|
|
|
if b.TotalTxs < 0 {
|
|
|
|
return errors.New("Negative Header.TotalTxs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.LastBlockID.ValidateBasic(); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.LastBlockID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the last commit and its hash.
|
|
|
|
if b.Header.Height > 1 {
|
|
|
|
if b.LastCommit == nil {
|
|
|
|
return errors.New("nil LastCommit")
|
|
|
|
}
|
|
|
|
if err := b.LastCommit.ValidateBasic(); err != nil {
|
|
|
|
return fmt.Errorf("Wrong LastCommit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ValidateHash(b.LastCommitHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.LastCommitHash: %v", err)
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
|
2018-11-01 07:07:18 +01:00
|
|
|
return fmt.Errorf("Wrong Header.LastCommitHash. Expected %v, got %v",
|
2018-07-30 12:59:16 +04:00
|
|
|
b.LastCommit.Hash(),
|
2018-11-01 07:07:18 +01:00
|
|
|
b.LastCommitHash,
|
2018-07-30 12:59:16 +04:00
|
|
|
)
|
2015-08-07 10:43:24 -07:00
|
|
|
}
|
2018-11-01 07:07:18 +01:00
|
|
|
|
|
|
|
// Validate the hash of the transactions.
|
|
|
|
// NOTE: b.Data.Txs may be nil, but b.Data.Hash()
|
|
|
|
// still works fine
|
|
|
|
if err := ValidateHash(b.DataHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.DataHash: %v", 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()) {
|
2018-07-30 12:59:16 +04:00
|
|
|
return fmt.Errorf(
|
2018-11-01 07:07:18 +01:00
|
|
|
"Wrong Header.DataHash. Expected %v, got %v",
|
2018-07-30 12:59:16 +04:00
|
|
|
b.Data.Hash(),
|
2018-11-01 07:07:18 +01:00
|
|
|
b.DataHash,
|
2018-07-30 12:59:16 +04:00
|
|
|
)
|
2015-08-07 10:43:24 -07:00
|
|
|
}
|
2018-11-01 07:07:18 +01:00
|
|
|
|
|
|
|
// Basic validation of hashes related to application data.
|
|
|
|
// Will validate fully against state in state#ValidateBlock.
|
|
|
|
if err := ValidateHash(b.ValidatorsHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.ValidatorsHash: %v", err)
|
|
|
|
}
|
|
|
|
if err := ValidateHash(b.NextValidatorsHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.NextValidatorsHash: %v", err)
|
|
|
|
}
|
|
|
|
if err := ValidateHash(b.ConsensusHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.ConsensusHash: %v", err)
|
|
|
|
}
|
|
|
|
// NOTE: AppHash is arbitrary length
|
|
|
|
if err := ValidateHash(b.LastResultsHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.LastResultsHash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate evidence and its hash.
|
|
|
|
if err := ValidateHash(b.EvidenceHash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Header.EvidenceHash: %v", err)
|
|
|
|
}
|
|
|
|
// NOTE: b.Evidence.Evidence may be nil, but we're just looping.
|
|
|
|
for i, ev := range b.Evidence.Evidence {
|
|
|
|
if err := ev.ValidateBasic(); err != nil {
|
|
|
|
return fmt.Errorf("Invalid evidence (#%d): %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 14:10:00 -04:00
|
|
|
if !bytes.Equal(b.EvidenceHash, b.Evidence.Hash()) {
|
2018-11-01 07:07:18 +01:00
|
|
|
return fmt.Errorf("Wrong Header.EvidenceHash. Expected %v, got %v",
|
2018-07-30 12:59:16 +04:00
|
|
|
b.EvidenceHash,
|
|
|
|
b.Evidence.Hash(),
|
|
|
|
)
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
2018-11-01 07:07:18 +01:00
|
|
|
|
|
|
|
if len(b.ProposerAddress) != crypto.AddressSize {
|
|
|
|
return fmt.Errorf("Expected len(Header.ProposerAddress) to be %d, got %d",
|
|
|
|
crypto.AddressSize, len(b.ProposerAddress))
|
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-06 17:02:29 -07:00
|
|
|
// fillHeader fills in any remaining header fields that are a function of the block data
|
|
|
|
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 {
|
2018-04-06 17:02:29 -07:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
|
2019-06-21 07:58:32 +02:00
|
|
|
if b.LastCommit == nil {
|
2015-03-22 12:46:53 -07:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-06 17:02:29 -07:00
|
|
|
b.fillHeader()
|
2015-08-07 10:43:24 -07:00
|
|
|
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.
|
2018-07-10 18:15:12 +04:00
|
|
|
// CONTRACT: partSize is greater than zero.
|
2016-09-16 09:20:07 -07:00
|
|
|
func (b *Block) MakePartSet(partSize int) *PartSet {
|
2018-04-09 15:04:59 +03:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
|
2018-04-05 07:05:45 -07:00
|
|
|
// We prefix the byte length, so that unmarshaling
|
|
|
|
// can easily happen via a reader.
|
2018-10-25 03:34:01 +02:00
|
|
|
bz, err := cdc.MarshalBinaryLengthPrefixed(b)
|
2018-01-14 19:05:22 -05:00
|
|
|
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.
|
2018-05-14 16:32:19 -04:00
|
|
|
// Returns false if the block is nil or the hash is empty.
|
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
|
|
|
}
|
|
|
|
|
2018-06-14 16:09:32 +04:00
|
|
|
// Size returns size of the block in bytes.
|
|
|
|
func (b *Block) Size() int {
|
|
|
|
bz, err := cdc.MarshalBinaryBare(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(bz)
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}
|
2018-04-02 10:21:17 +02:00
|
|
|
return fmt.Sprintf("Block#%v", b.Hash())
|
2014-10-18 01:42:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 21:07:20 +08:00
|
|
|
//-----------------------------------------------------------
|
|
|
|
// These methods are for Protobuf Compatibility
|
|
|
|
|
|
|
|
// Marshal returns the amino encoding.
|
|
|
|
func (b *Block) Marshal() ([]byte, error) {
|
|
|
|
return cdc.MarshalBinaryBare(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalTo calls Marshal and copies to the given buffer.
|
|
|
|
func (b *Block) MarshalTo(data []byte) (int, error) {
|
|
|
|
bs, err := b.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return copy(data, bs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal deserializes from amino encoded form.
|
|
|
|
func (b *Block) Unmarshal(bs []byte) error {
|
|
|
|
return cdc.UnmarshalBinaryBare(bs, b)
|
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-09-12 23:44:43 +04:00
|
|
|
// MaxDataBytes returns the maximum size of block's data.
|
2018-09-21 13:00:36 +04:00
|
|
|
//
|
|
|
|
// XXX: Panics on negative result.
|
|
|
|
func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
|
|
|
|
maxDataBytes := maxBytes -
|
2018-09-12 23:44:43 +04:00
|
|
|
MaxAminoOverheadForBlock -
|
|
|
|
MaxHeaderBytes -
|
2018-09-21 13:00:36 +04:00
|
|
|
int64(valsCount)*MaxVoteBytes -
|
|
|
|
int64(evidenceCount)*MaxEvidenceBytes
|
|
|
|
|
|
|
|
if maxDataBytes < 0 {
|
|
|
|
panic(fmt.Sprintf(
|
2019-03-04 13:24:44 +04:00
|
|
|
"Negative MaxDataBytes. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
|
2018-09-21 13:00:36 +04:00
|
|
|
maxBytes,
|
|
|
|
-(maxDataBytes - maxBytes),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxDataBytes
|
|
|
|
|
2018-09-12 23:44:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxDataBytesUnknownEvidence returns the maximum size of block's data when
|
2019-01-17 21:46:40 -05:00
|
|
|
// evidence count is unknown. MaxEvidencePerBlock will be used for the size
|
2018-09-12 23:44:43 +04:00
|
|
|
// of evidence.
|
2018-09-21 13:00:36 +04:00
|
|
|
//
|
|
|
|
// XXX: Panics on negative result.
|
|
|
|
func MaxDataBytesUnknownEvidence(maxBytes int64, valsCount int) int64 {
|
2019-01-17 21:46:40 -05:00
|
|
|
_, maxEvidenceBytes := MaxEvidencePerBlock(maxBytes)
|
2018-09-21 13:00:36 +04:00
|
|
|
maxDataBytes := maxBytes -
|
2018-09-12 23:44:43 +04:00
|
|
|
MaxAminoOverheadForBlock -
|
|
|
|
MaxHeaderBytes -
|
2018-09-21 13:00:36 +04:00
|
|
|
int64(valsCount)*MaxVoteBytes -
|
2019-01-17 21:46:40 -05:00
|
|
|
maxEvidenceBytes
|
2018-09-21 13:00:36 +04:00
|
|
|
|
|
|
|
if maxDataBytes < 0 {
|
|
|
|
panic(fmt.Sprintf(
|
2019-03-04 13:24:44 +04:00
|
|
|
"Negative MaxDataBytesUnknownEvidence. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
|
2018-09-21 13:00:36 +04:00
|
|
|
maxBytes,
|
|
|
|
-(maxDataBytes - maxBytes),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxDataBytes
|
2018-09-12 23:44:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-10-23 13:21:47 -04:00
|
|
|
// Header defines the structure of a Tendermint block header.
|
2018-10-19 11:39:27 -04:00
|
|
|
// NOTE: changes to the Header should be duplicated in:
|
2018-10-23 13:21:47 -04:00
|
|
|
// - header.Hash()
|
|
|
|
// - abci.Header
|
|
|
|
// - /docs/spec/blockchain/blockchain.md
|
2014-06-05 02:34:45 -07:00
|
|
|
type Header struct {
|
2017-12-20 23:53:15 -05:00
|
|
|
// basic block info
|
2018-10-17 15:30:53 -04:00
|
|
|
Version version.Consensus `json:"version"`
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
NumTxs int64 `json:"num_txs"`
|
|
|
|
TotalTxs int64 `json:"total_txs"`
|
2017-12-20 23:53:15 -05:00
|
|
|
|
|
|
|
// prev block info
|
|
|
|
LastBlockID BlockID `json:"last_block_id"`
|
|
|
|
|
|
|
|
// 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-05-29 01:03:03 -07:00
|
|
|
ValidatorsHash cmn.HexBytes `json:"validators_hash"` // validators for the current block
|
|
|
|
NextValidatorsHash cmn.HexBytes `json:"next_validators_hash"` // validators for the next 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-07-30 12:59:16 +04:00
|
|
|
EvidenceHash cmn.HexBytes `json:"evidence_hash"` // evidence included in the block
|
|
|
|
ProposerAddress Address `json:"proposer_address"` // original proposer of the block
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 13:21:47 -04:00
|
|
|
// Populate the Header with state-derived data.
|
|
|
|
// Call this after MakeBlock to complete the Header.
|
|
|
|
func (h *Header) Populate(
|
|
|
|
version version.Consensus, chainID string,
|
|
|
|
timestamp time.Time, lastBlockID BlockID, totalTxs int64,
|
|
|
|
valHash, nextValHash []byte,
|
|
|
|
consensusHash, appHash, lastResultsHash []byte,
|
|
|
|
proposerAddress Address,
|
|
|
|
) {
|
|
|
|
h.Version = version
|
|
|
|
h.ChainID = chainID
|
|
|
|
h.Time = timestamp
|
|
|
|
h.LastBlockID = lastBlockID
|
|
|
|
h.TotalTxs = totalTxs
|
|
|
|
h.ValidatorsHash = valHash
|
|
|
|
h.NextValidatorsHash = nextValHash
|
|
|
|
h.ConsensusHash = consensusHash
|
|
|
|
h.AppHash = appHash
|
|
|
|
h.LastResultsHash = lastResultsHash
|
|
|
|
h.ProposerAddress = proposerAddress
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:00:14 -04:00
|
|
|
// Hash returns the hash of the header.
|
2018-10-19 11:39:27 -04:00
|
|
|
// It computes a Merkle tree from the header fields
|
|
|
|
// ordered as they appear in the Header.
|
2018-04-03 22:34:18 +03:00
|
|
|
// Returns nil if ValidatorHash is missing,
|
|
|
|
// since a Header is not valid unless there is
|
2018-07-10 18:15:12 +04:00
|
|
|
// a ValidatorsHash (corresponding to the validator set).
|
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-10-19 11:39:27 -04:00
|
|
|
return merkle.SimpleHashFromByteSlices([][]byte{
|
|
|
|
cdcEncode(h.Version),
|
|
|
|
cdcEncode(h.ChainID),
|
|
|
|
cdcEncode(h.Height),
|
|
|
|
cdcEncode(h.Time),
|
|
|
|
cdcEncode(h.NumTxs),
|
|
|
|
cdcEncode(h.TotalTxs),
|
|
|
|
cdcEncode(h.LastBlockID),
|
|
|
|
cdcEncode(h.LastCommitHash),
|
|
|
|
cdcEncode(h.DataHash),
|
|
|
|
cdcEncode(h.ValidatorsHash),
|
|
|
|
cdcEncode(h.NextValidatorsHash),
|
|
|
|
cdcEncode(h.ConsensusHash),
|
|
|
|
cdcEncode(h.AppHash),
|
|
|
|
cdcEncode(h.LastResultsHash),
|
|
|
|
cdcEncode(h.EvidenceHash),
|
|
|
|
cdcEncode(h.ProposerAddress),
|
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{
|
2018-10-17 15:30:53 -04:00
|
|
|
%s Version: %v
|
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
|
2018-05-29 01:03:03 -07:00
|
|
|
%s NextValidators: %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s App: %v
|
2019-09-18 14:10:00 +02:00
|
|
|
%s Consensus: %v
|
2017-12-22 16:43:45 +01:00
|
|
|
%s Results: %v
|
2017-07-09 14:10:00 -04:00
|
|
|
%s Evidence: %v
|
2018-07-30 12:59:16 +04:00
|
|
|
%s Proposer: %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2018-10-17 15:30:53 -04:00
|
|
|
indent, h.Version,
|
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,
|
2018-05-29 01:03:03 -07:00
|
|
|
indent, h.NextValidatorsHash,
|
2015-12-01 20:12:01 -08:00
|
|
|
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,
|
2018-07-30 12:59:16 +04:00
|
|
|
indent, h.ProposerAddress,
|
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
|
|
|
//-------------------------------------
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
// CommitSig is a vote included in a Commit.
|
|
|
|
// For now, it is identical to a vote,
|
|
|
|
// but in the future it will contain fewer fields
|
|
|
|
// to eliminate the redundancy in commits.
|
|
|
|
// See https://github.com/tendermint/tendermint/issues/1648.
|
|
|
|
type CommitSig Vote
|
|
|
|
|
|
|
|
// String returns the underlying Vote.String()
|
|
|
|
func (cs *CommitSig) String() string {
|
|
|
|
return cs.toVote().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// toVote converts the CommitSig to a vote.
|
2019-02-08 18:40:41 -05:00
|
|
|
// TODO: deprecate for #1648. Converting to Vote will require
|
|
|
|
// access to ValidatorSet.
|
2019-02-04 13:01:59 -05:00
|
|
|
func (cs *CommitSig) toVote() *Vote {
|
|
|
|
if cs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
v := Vote(*cs)
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:01:35 -04: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.
|
2019-02-04 13:01:59 -05:00
|
|
|
BlockID BlockID `json:"block_id"`
|
|
|
|
Precommits []*CommitSig `json:"precommits"`
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2019-02-08 18:40:41 -05:00
|
|
|
// memoized in first call to corresponding method
|
|
|
|
// NOTE: can't memoize in constructor because constructor
|
|
|
|
// isn't used for unmarshaling
|
2019-02-04 13:01:59 -05:00
|
|
|
height int64
|
|
|
|
round int
|
|
|
|
hash cmn.HexBytes
|
|
|
|
bitArray *cmn.BitArray
|
2015-06-26 17:48:24 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 18:40:41 -05:00
|
|
|
// NewCommit returns a new Commit with the given blockID and precommits.
|
|
|
|
// TODO: memoize ValidatorSet in constructor so votes can be easily reconstructed
|
|
|
|
// from CommitSig after #1648.
|
|
|
|
func NewCommit(blockID BlockID, precommits []*CommitSig) *Commit {
|
|
|
|
return &Commit{
|
|
|
|
BlockID: blockID,
|
|
|
|
Precommits: precommits,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:01:35 -04:00
|
|
|
// Construct a VoteSet from the Commit and validator set. Panics
|
|
|
|
// if precommits from the commit can't be added to the voteset.
|
|
|
|
// Inverse of VoteSet.MakeCommit().
|
|
|
|
func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSet {
|
|
|
|
height, round, typ := commit.Height(), commit.Round(), PrecommitType
|
|
|
|
voteSet := NewVoteSet(chainID, height, round, typ, vals)
|
|
|
|
for idx, precommit := range commit.Precommits {
|
|
|
|
if precommit == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
added, err := voteSet.AddVote(commit.GetVote(idx))
|
|
|
|
if !added || err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return voteSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVote converts the CommitSig for the given valIdx to a Vote.
|
|
|
|
// Returns nil if the precommit at valIdx is nil.
|
|
|
|
// Panics if valIdx >= commit.Size().
|
|
|
|
func (commit *Commit) GetVote(valIdx int) *Vote {
|
|
|
|
commitSig := commit.Precommits[valIdx]
|
|
|
|
if commitSig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: this commitSig might be for a nil blockID,
|
|
|
|
// so we can't just use commit.BlockID here.
|
|
|
|
// For #1648, CommitSig will need to indicate what BlockID it's for !
|
|
|
|
blockID := commitSig.BlockID
|
|
|
|
commit.memoizeHeightRound()
|
|
|
|
return &Vote{
|
|
|
|
Type: PrecommitType,
|
|
|
|
Height: commit.height,
|
|
|
|
Round: commit.round,
|
|
|
|
BlockID: blockID,
|
|
|
|
Timestamp: commitSig.Timestamp,
|
|
|
|
ValidatorAddress: commitSig.ValidatorAddress,
|
|
|
|
ValidatorIndex: valIdx,
|
|
|
|
Signature: commitSig.Signature,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
// VoteSignBytes constructs the SignBytes for the given CommitSig.
|
|
|
|
// The only unique part of the SignBytes is the Timestamp - all other fields
|
|
|
|
// signed over are otherwise the same for all validators.
|
2019-05-05 12:01:35 -04:00
|
|
|
// Panics if valIdx >= commit.Size().
|
|
|
|
func (commit *Commit) VoteSignBytes(chainID string, valIdx int) []byte {
|
|
|
|
return commit.GetVote(valIdx).SignBytes(chainID)
|
2019-02-04 13:01:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// memoizeHeightRound memoizes the height and round of the commit using
|
|
|
|
// the first non-nil vote.
|
2019-05-31 07:25:21 +09:00
|
|
|
// Should be called before any attempt to access `commit.height` or `commit.round`.
|
2019-02-04 13:01:59 -05:00
|
|
|
func (commit *Commit) memoizeHeightRound() {
|
2016-04-02 09:10:16 -07:00
|
|
|
if len(commit.Precommits) == 0 {
|
2019-02-04 13:01:59 -05:00
|
|
|
return
|
2015-06-26 17:48:24 -07:00
|
|
|
}
|
2019-02-04 13:01:59 -05:00
|
|
|
if commit.height > 0 {
|
|
|
|
return
|
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 {
|
2019-02-04 13:01:59 -05:00
|
|
|
commit.height = precommit.Height
|
|
|
|
commit.round = precommit.Round
|
|
|
|
return
|
2015-06-26 17:48:24 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 13:01:59 -05: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 {
|
2019-02-04 13:01:59 -05:00
|
|
|
commit.memoizeHeightRound()
|
|
|
|
return commit.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 {
|
2019-02-04 13:01:59 -05:00
|
|
|
commit.memoizeHeightRound()
|
|
|
|
return commit.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 {
|
2018-10-13 01:21:46 +02:00
|
|
|
return byte(PrecommitType)
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
// GetByIndex returns the vote corresponding to a given validator index.
|
2019-02-08 18:40:41 -05:00
|
|
|
// Panics if `index >= commit.Size()`.
|
2019-02-04 13:01:59 -05:00
|
|
|
// Implements VoteSetReader.
|
2019-05-05 12:01:35 -04:00
|
|
|
func (commit *Commit) GetByIndex(valIdx int) *Vote {
|
|
|
|
return commit.GetVote(valIdx)
|
2015-07-05 13:40:59 -07:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:59 -05: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.
|
2018-06-09 04:25:48 -07:00
|
|
|
// Does not actually check the cryptographic signatures.
|
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
|
|
|
|
2018-06-09 04:25:48 -07: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
|
|
|
|
}
|
2018-06-09 04:25:48 -07:00
|
|
|
// Ensure that all votes are precommits.
|
2018-10-13 01:21:46 +02:00
|
|
|
if precommit.Type != PrecommitType {
|
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)
|
|
|
|
}
|
2018-06-09 04:25:48 -07:00
|
|
|
// Ensure that all heights are the same.
|
2015-06-21 19:11:21 -07:00
|
|
|
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)
|
|
|
|
}
|
2018-06-09 04:25:48 -07:00
|
|
|
// Ensure that all rounds are the same.
|
2015-06-21 19:11:21 -07:00
|
|
|
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 {
|
2018-07-10 18:15:12 +04:00
|
|
|
if commit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-02 09:10:16 -07:00
|
|
|
if commit.hash == nil {
|
2018-10-10 09:46:09 -07:00
|
|
|
bs := make([][]byte, len(commit.Precommits))
|
2016-04-02 09:10:16 -07:00
|
|
|
for i, precommit := range commit.Precommits {
|
2018-10-10 09:46:09 -07:00
|
|
|
bs[i] = cdcEncode(precommit)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2018-10-10 09:46:09 -07:00
|
|
|
commit.hash = merkle.SimpleHashFromByteSlices(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
|
2018-06-09 04:25:48 -07:00
|
|
|
%s Precommits:
|
|
|
|
%s %v
|
2017-05-17 01:08:41 +02:00
|
|
|
%s}#%v`,
|
2016-09-05 17:33:02 -07:00
|
|
|
indent, commit.BlockID,
|
2018-06-09 04:25:48 -07:00
|
|
|
indent,
|
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-06-09 04:25:48 -07:00
|
|
|
// SignedHeader is a header along with the commits that prove it.
|
2019-01-21 09:21:04 -05:00
|
|
|
// It is the basis of the lite client.
|
2017-10-25 16:43:18 +02:00
|
|
|
type SignedHeader struct {
|
2018-06-09 04:25:48 -07:00
|
|
|
*Header `json:"header"`
|
|
|
|
Commit *Commit `json:"commit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateBasic does basic consistency checks and makes sure the header
|
|
|
|
// and commit are consistent.
|
|
|
|
//
|
|
|
|
// NOTE: This does not actually check the cryptographic signatures. Make
|
2018-08-02 03:10:50 -07:00
|
|
|
// sure to use a Verifier to validate the signatures actually provide a
|
2018-06-09 04:25:48 -07:00
|
|
|
// significantly strong proof for this header's validity.
|
|
|
|
func (sh SignedHeader) ValidateBasic(chainID string) error {
|
|
|
|
|
|
|
|
// Make sure the header is consistent with the commit.
|
|
|
|
if sh.Header == nil {
|
|
|
|
return errors.New("SignedHeader missing header.")
|
|
|
|
}
|
|
|
|
if sh.Commit == nil {
|
|
|
|
return errors.New("SignedHeader missing commit (precommit votes).")
|
|
|
|
}
|
2018-10-17 15:30:53 -04:00
|
|
|
|
2018-06-09 04:25:48 -07:00
|
|
|
// Check ChainID.
|
|
|
|
if sh.ChainID != chainID {
|
|
|
|
return fmt.Errorf("Header belongs to another chain '%s' not '%s'",
|
|
|
|
sh.ChainID, chainID)
|
|
|
|
}
|
|
|
|
// Check Height.
|
|
|
|
if sh.Commit.Height() != sh.Height {
|
|
|
|
return fmt.Errorf("SignedHeader header and commit height mismatch: %v vs %v",
|
|
|
|
sh.Height, sh.Commit.Height())
|
|
|
|
}
|
|
|
|
// Check Hash.
|
|
|
|
hhash := sh.Hash()
|
|
|
|
chash := sh.Commit.BlockID.Hash
|
|
|
|
if !bytes.Equal(hhash, chash) {
|
|
|
|
return fmt.Errorf("SignedHeader commit signs block %X, header is block %X",
|
|
|
|
chash, hhash)
|
|
|
|
}
|
|
|
|
// ValidateBasic on the Commit.
|
|
|
|
err := sh.Commit.ValidateBasic()
|
|
|
|
if err != nil {
|
2019-08-08 12:31:13 +02:00
|
|
|
return errors.Wrap(err, "commit.ValidateBasic failed during SignedHeader.ValidateBasic")
|
2018-06-09 04:25:48 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh SignedHeader) String() string {
|
|
|
|
return sh.StringIndented("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringIndented returns a string representation of the SignedHeader.
|
|
|
|
func (sh SignedHeader) StringIndented(indent string) string {
|
|
|
|
return fmt.Sprintf(`SignedHeader{
|
|
|
|
%s %v
|
|
|
|
%s %v
|
|
|
|
%s}`,
|
|
|
|
indent, sh.Header.StringIndented(indent+" "),
|
|
|
|
indent, sh.Commit.StringIndented(indent+" "),
|
|
|
|
indent)
|
2017-10-25 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
}
|
2018-06-27 11:52:47 +04:00
|
|
|
txStrings[i] = fmt.Sprintf("%X (%d bytes)", tx.Hash(), len(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)
|
|
|
|
}
|
2018-06-27 11:52:47 +04:00
|
|
|
return fmt.Sprintf(`EvidenceData{
|
2017-08-28 20:01:34 -04:00
|
|
|
%s %v
|
|
|
|
%s}#%v`,
|
|
|
|
indent, strings.Join(evStrings, "\n"+indent+" "),
|
|
|
|
indent, data.hash)
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// 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-04-05 05:17:43 -07:00
|
|
|
bz, err := cdc.MarshalBinaryBare(blockID.PartsHeader)
|
2018-01-14 19:05:22 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (blockID BlockID) ValidateBasic() error {
|
|
|
|
// Hash can be empty in case of POLBlockID in Proposal.
|
|
|
|
if err := ValidateHash(blockID.Hash); err != nil {
|
|
|
|
return fmt.Errorf("Wrong Hash")
|
|
|
|
}
|
|
|
|
if err := blockID.PartsHeader.ValidateBasic(); err != nil {
|
|
|
|
return fmt.Errorf("Wrong PartsHeader: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 23:56:36 +01:00
|
|
|
// IsZero returns true if this is the BlockID of a nil block.
|
|
|
|
func (blockID BlockID) IsZero() bool {
|
|
|
|
return len(blockID.Hash) == 0 &&
|
|
|
|
blockID.PartsHeader.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsComplete returns true if this is a valid BlockID of a non-nil block.
|
|
|
|
func (blockID BlockID) IsComplete() bool {
|
|
|
|
return len(blockID.Hash) == tmhash.Size &&
|
|
|
|
blockID.PartsHeader.Total > 0 &&
|
|
|
|
len(blockID.PartsHeader.Hash) == tmhash.Size
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|