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-08-10 16:35:08 -07:00
|
|
|
"crypto/sha256"
|
2014-09-11 22:44:59 -07:00
|
|
|
"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-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
"github.com/tendermint/tendermint/merkle"
|
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-05-29 17:53:57 -04:00
|
|
|
func (b *Block) ValidateBasic(chainID string, lastBlockHeight uint, 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-05-29 14:13:45 -04:00
|
|
|
return errors.New("Wrong Block.Header.ChainID")
|
2014-10-31 21:30:52 -07:00
|
|
|
}
|
|
|
|
if b.Height != lastBlockHeight+1 {
|
2014-12-23 01:35:54 -08:00
|
|
|
return errors.New("Wrong Block.Header.Height")
|
|
|
|
}
|
|
|
|
if b.NumTxs != uint(len(b.Data.Txs)) {
|
|
|
|
return errors.New("Wrong Block.Header.NumTxs")
|
2014-10-31 21:30:52 -07:00
|
|
|
}
|
|
|
|
if !bytes.Equal(b.LastBlockHash, lastBlockHash) {
|
2014-12-23 01:35:54 -08:00
|
|
|
return errors.New("Wrong Block.Header.LastBlockHash")
|
2014-09-11 22:44:59 -07:00
|
|
|
}
|
2014-10-31 21:30:52 -07:00
|
|
|
if !b.LastBlockParts.Equals(lastBlockParts) {
|
2014-12-23 01:35:54 -08:00
|
|
|
return errors.New("Wrong Block.Header.LastBlockParts")
|
2014-09-11 22:44:59 -07:00
|
|
|
}
|
2015-03-25 11:33:39 -07:00
|
|
|
/* TODO: Determine bounds
|
|
|
|
See blockchain/reactor "stopSyncingDurationMinutes"
|
|
|
|
|
2014-12-23 01:35:54 -08:00
|
|
|
if !b.Time.After(lastBlockTime) {
|
|
|
|
return errors.New("Invalid Block.Header.Time")
|
|
|
|
}
|
|
|
|
*/
|
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
|
|
|
}
|
2014-09-11 22:44:59 -07:00
|
|
|
// XXX more validation
|
2014-08-10 16:35:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-03-26 10:58:20 -07:00
|
|
|
hashHeader := b.Header.Hash()
|
|
|
|
hashData := b.Data.Hash()
|
2015-06-19 15:30:10 -07:00
|
|
|
hashLastValidation := b.LastValidation.Hash()
|
2015-03-26 10:58:20 -07:00
|
|
|
|
|
|
|
// If hashHeader is nil, required fields are missing.
|
|
|
|
if len(hashHeader) == 0 {
|
|
|
|
return nil
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2015-03-26 10:58:20 -07:00
|
|
|
|
|
|
|
// Merkle hash from subhashes.
|
2015-06-19 15:30:10 -07:00
|
|
|
hashes := [][]byte{hashHeader, hashData, hashLastValidation}
|
2015-06-18 20:19:39 -07:00
|
|
|
return merkle.SimpleHashFromHashes(hashes)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:00:27 -07:00
|
|
|
func (b *Block) MakePartSet() *PartSet {
|
|
|
|
return NewPartSetFromData(binary.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
|
|
|
}
|
|
|
|
|
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-05-29 14:13:45 -04:00
|
|
|
ChainID string `json:"chain_id"`
|
2015-05-01 17:26:49 -07:00
|
|
|
Height uint `json:"height"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Fees uint64 `json:"fees"`
|
|
|
|
NumTxs uint `json:"num_txs"`
|
|
|
|
LastBlockHash []byte `json:"last_block_hash"`
|
|
|
|
LastBlockParts PartSetHeader `json:"last_block_parts"`
|
|
|
|
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-01-19 14:08:53 -08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
hasher, n, err := sha256.New(), new(int64), new(error)
|
|
|
|
binary.WriteBinary(h, buf, n, err)
|
|
|
|
if *err != nil {
|
|
|
|
panic(err)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2015-01-19 14:08:53 -08:00
|
|
|
hasher.Write(buf.Bytes())
|
|
|
|
hash := hasher.Sum(nil)
|
|
|
|
return hash
|
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
|
2014-12-23 01:35:54 -08: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
|
|
|
|
hash []byte
|
2015-05-05 22:53:06 -07:00
|
|
|
bitArray *BitArray
|
2014-06-05 11:04:56 -07:00
|
|
|
}
|
2014-06-05 02:34:45 -07:00
|
|
|
|
2015-06-19 15:30:10 -07:00
|
|
|
func (v *Validation) Height() uint {
|
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return v.Precommits[0].Height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) Round() uint {
|
|
|
|
if len(v.Precommits) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return v.Precommits[0].Round
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-05 22:53:06 -07:00
|
|
|
func (v *Validation) BitArray() *BitArray {
|
|
|
|
if v.bitArray == nil {
|
2015-06-05 14:15:40 -07:00
|
|
|
v.bitArray = NewBitArray(uint(len(v.Precommits)))
|
|
|
|
for i, precommit := range v.Precommits {
|
2015-06-21 19:11:21 -07:00
|
|
|
v.bitArray.SetIndex(uint(i), precommit != nil)
|
2014-11-01 22:42:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.bitArray
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-09 18:49:04 -08:00
|
|
|
bs := make([]interface{}, len(data.Txs))
|
2014-09-11 01:11:41 -07:00
|
|
|
for i, tx := range data.Txs {
|
2015-05-29 17:53:57 -04:00
|
|
|
bs[i] = account.SignBytes(config.GetString("chain_id"), tx)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2015-06-18 20:19:39 -07:00
|
|
|
data.hash = merkle.SimpleHashFromBinaries(bs)
|
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
|
|
|
}
|