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
|
|
|
|
2015-10-22 17:39:06 -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
|
|
|
)
|
|
|
|
|
2014-06-05 02:34:45 -07:00
|
|
|
type Block struct {
|
2015-06-19 15:30:10 -07:00
|
|
|
*Header `json:"header"`
|
|
|
|
*Data `json:"data"`
|
|
|
|
LastValidation *Validation `json:"last_validation"`
|
2014-06-04 01:40:17 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 22:44:59 -07:00
|
|
|
// Basic validation that doesn't involve state data.
|
2015-06-25 20:28:34 -07:00
|
|
|
func (b *Block) ValidateBasic(chainID string, lastBlockHeight int, lastBlockHash []byte,
|
2014-10-31 21:30:52 -07:00
|
|
|
lastBlockParts PartSetHeader, lastBlockTime time.Time) error {
|
2015-05-29 17:53:57 -04: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))
|
2014-10-31 21:30:52 -07:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// TODO: validate Fees
|
2015-06-25 20:28:34 -07:00
|
|
|
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))
|
2014-10-31 21:30:52 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2014-10-31 21:30:52 -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
|
|
|
}
|
2015-08-07 10:43:24 -07:00
|
|
|
if !bytes.Equal(b.LastValidationHash, b.LastValidation.Hash()) {
|
|
|
|
return errors.New(Fmt("Wrong Block.Header.LastValidationHash. Expected %X, got %X", b.LastValidationHash, b.LastValidation.Hash()))
|
|
|
|
}
|
2014-12-17 01:37:13 -08:00
|
|
|
if b.Header.Height != 1 {
|
2015-06-19 15:30:10 -07:00
|
|
|
if err := b.LastValidation.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()) {
|
|
|
|
return errors.New(Fmt("Wrong Block.Header.DataHash. Expected %X, got %X", b.DataHash, b.Data.Hash()))
|
|
|
|
}
|
|
|
|
// NOTE: the StateHash is validated later.
|
2014-08-10 16:35:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 10:43:24 -07:00
|
|
|
func (b *Block) FillHeader() {
|
|
|
|
b.LastValidationHash = b.LastValidation.Hash()
|
|
|
|
b.DataHash = b.Data.Hash()
|
|
|
|
}
|
|
|
|
|
2015-03-26 10:58:20 -07:00
|
|
|
// Computes and returns the block hash.
|
|
|
|
// If the block is incomplete (e.g. missing Header.StateHash)
|
|
|
|
// then the hash is nil, to prevent the usage of that hash.
|
2014-08-10 16:35:08 -07:00
|
|
|
func (b *Block) Hash() []byte {
|
2015-06-19 15:30:10 -07:00
|
|
|
if b.Header == nil || b.Data == nil || b.LastValidation == 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
|
|
|
}
|
|
|
|
|
2015-03-24 12:00:27 -07:00
|
|
|
func (b *Block) MakePartSet() *PartSet {
|
2015-07-25 15:45:45 -07:00
|
|
|
return NewPartSetFromData(wire.BinaryBytes(b))
|
2015-03-24 12:00:27 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
%s}#%X`,
|
2014-12-23 01:35:54 -08:00
|
|
|
indent, b.Header.StringIndented(indent+" "),
|
|
|
|
indent, b.Data.StringIndented(indent+" "),
|
2015-06-19 15:30:10 -07:00
|
|
|
indent, b.LastValidation.StringIndented(indent+" "),
|
2015-01-19 14:08:53 -08:00
|
|
|
indent, b.Hash())
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-08-07 10:43:24 -07:00
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Fees int64 `json:"fees"`
|
|
|
|
NumTxs int `json:"num_txs"`
|
|
|
|
LastBlockHash []byte `json:"last_block_hash"`
|
|
|
|
LastBlockParts PartSetHeader `json:"last_block_parts"`
|
|
|
|
LastValidationHash []byte `json:"last_validation_hash"`
|
|
|
|
DataHash []byte `json:"data_hash"`
|
|
|
|
StateHash []byte `json:"state_hash"`
|
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-03-26 10:58:20 -07:00
|
|
|
if len(h.StateHash) == 0 {
|
|
|
|
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,
|
|
|
|
"Fees": h.Fees,
|
|
|
|
"NumTxs": h.NumTxs,
|
|
|
|
"LastBlock": h.LastBlockHash,
|
|
|
|
"LastBlockParts": h.LastBlockParts,
|
|
|
|
"LastValidation": h.LastValidationHash,
|
|
|
|
"Data": h.DataHash,
|
|
|
|
"State": h.StateHash,
|
|
|
|
})
|
2014-10-13 20:07:26 -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"
|
|
|
|
}
|
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
|
|
|
|
%s Fees: %v
|
2015-06-28 18:48:30 -07:00
|
|
|
%s NumTxs: %v
|
2014-10-30 03:32:09 -07:00
|
|
|
%s LastBlockHash: %X
|
|
|
|
%s LastBlockParts: %v
|
|
|
|
%s StateHash: %X
|
2014-10-13 20:07:26 -07:00
|
|
|
%s}#%X`,
|
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,
|
|
|
|
indent, h.Fees,
|
2014-12-23 01:35:54 -08:00
|
|
|
indent, h.NumTxs,
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, h.LastBlockHash,
|
2014-10-30 03:32:09 -07:00
|
|
|
indent, h.LastBlockParts,
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, h.StateHash,
|
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
|
|
|
//-------------------------------------
|
|
|
|
|
2015-06-19 15:30:10 -07:00
|
|
|
// NOTE: Validation is empty for height 1, but never nil.
|
2014-12-09 18:49:04 -08:00
|
|
|
type Validation 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"`
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
// Volatile
|
2015-06-26 17:48:24 -07:00
|
|
|
firstPrecommit *Vote
|
|
|
|
hash []byte
|
|
|
|
bitArray *BitArray
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) FirstPrecommit() *Vote {
|
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if v.firstPrecommit != nil {
|
|
|
|
return v.firstPrecommit
|
|
|
|
}
|
|
|
|
for _, precommit := range v.Precommits {
|
|
|
|
if precommit != nil {
|
|
|
|
v.firstPrecommit = precommit
|
|
|
|
return precommit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-06-05 11:04:56 -07:00
|
|
|
}
|
2014-06-05 02:34:45 -07:00
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (v *Validation) Height() int {
|
2015-06-19 15:30:10 -07:00
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2015-06-26 17:48:24 -07:00
|
|
|
return v.FirstPrecommit().Height
|
2015-06-19 15:30:10 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (v *Validation) Round() int {
|
2015-06-19 15:30:10 -07:00
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2015-06-26 17:48:24 -07:00
|
|
|
return v.FirstPrecommit().Round
|
2015-06-19 15:30:10 -07:00
|
|
|
}
|
|
|
|
|
2015-07-05 13:40:59 -07:00
|
|
|
func (v *Validation) Type() byte {
|
|
|
|
return VoteTypePrecommit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) Size() int {
|
2015-07-05 15:47:30 -07:00
|
|
|
if v == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2015-07-05 13:40:59 -07:00
|
|
|
return len(v.Precommits)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) BitArray() *BitArray {
|
|
|
|
if v.bitArray == nil {
|
|
|
|
v.bitArray = NewBitArray(len(v.Precommits))
|
|
|
|
for i, precommit := range v.Precommits {
|
|
|
|
v.bitArray.SetIndex(i, precommit != nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.bitArray
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) GetByIndex(index int) *Vote {
|
|
|
|
return v.Precommits[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) IsCommit() bool {
|
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-12-16 05:40:17 -08:00
|
|
|
func (v *Validation) ValidateBasic() error {
|
2015-06-05 14:15:40 -07:00
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return errors.New("No precommits in validation")
|
2014-12-16 05:40:17 -08:00
|
|
|
}
|
2015-06-21 19:11:21 -07:00
|
|
|
height, round := v.Height(), v.Round()
|
|
|
|
for _, precommit := range v.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 {
|
|
|
|
return fmt.Errorf("Invalid validation vote. Expected precommit, got %v",
|
|
|
|
precommit.Type)
|
|
|
|
}
|
|
|
|
// Ensure that all heights are the same
|
|
|
|
if precommit.Height != height {
|
|
|
|
return fmt.Errorf("Invalid validation precommit height. Expected %v, got %v",
|
|
|
|
height, precommit.Height)
|
|
|
|
}
|
|
|
|
// Ensure that all rounds are the same
|
|
|
|
if precommit.Round != round {
|
|
|
|
return fmt.Errorf("Invalid validation precommit round. Expected %v, got %v",
|
|
|
|
round, precommit.Round)
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 05:40:17 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
func (v *Validation) Hash() []byte {
|
2014-10-13 20:07:26 -07:00
|
|
|
if v.hash == nil {
|
2015-06-24 14:04:40 -07:00
|
|
|
bs := make([]interface{}, len(v.Precommits))
|
2015-06-05 14:15:40 -07:00
|
|
|
for i, precommit := range v.Precommits {
|
2015-06-24 14:04:40 -07:00
|
|
|
bs[i] = precommit
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2015-06-18 20:19:39 -07:00
|
|
|
v.hash = merkle.SimpleHashFromBinaries(bs)
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|
|
|
|
return v.hash
|
|
|
|
}
|
|
|
|
|
2014-12-23 01:35:54 -08:00
|
|
|
func (v *Validation) StringIndented(indent string) string {
|
2015-03-22 12:46:53 -07:00
|
|
|
if v == nil {
|
|
|
|
return "nil-Validation"
|
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
precommitStrings := make([]string, len(v.Precommits))
|
|
|
|
for i, precommit := range v.Precommits {
|
|
|
|
precommitStrings[i] = precommit.String()
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return fmt.Sprintf(`Validation{
|
2015-06-05 14:15:40 -07:00
|
|
|
%s Precommits: %v
|
2014-10-13 20:07:26 -07:00
|
|
|
%s}#%X`,
|
2015-06-05 14:15:40 -07:00
|
|
|
indent, strings.Join(precommitStrings, "\n"+indent+" "),
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, v.hash)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Data struct {
|
2015-05-01 17:26:49 -07:00
|
|
|
Txs []Tx `json:"txs"`
|
2014-08-10 16:35:08 -07:00
|
|
|
|
|
|
|
// Volatile
|
|
|
|
hash []byte
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
func (data *Data) Hash() []byte {
|
2014-10-13 20:07:26 -07:00
|
|
|
if data.hash == nil {
|
2015-11-01 11:34:08 -08:00
|
|
|
txs := make([]interface{}, len(data.Txs))
|
2014-09-11 01:11:41 -07:00
|
|
|
for i, tx := range data.Txs {
|
2015-11-01 11:34:08 -08:00
|
|
|
txs[i] = tx
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2015-11-01 11:34:08 -08:00
|
|
|
data.hash = merkle.SimpleHashFromBinaries(txs) // NOTE: leaves are TxIDs.
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -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"
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
txStrings := make([]string, len(data.Txs))
|
|
|
|
for i, tx := range data.Txs {
|
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
|
|
|
|
%s}#%X`,
|
|
|
|
indent, strings.Join(txStrings, "\n"+indent+" "),
|
|
|
|
indent, data.hash)
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|