2014-06-04 01:40:17 -07:00
|
|
|
package blocks
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
. "github.com/tendermint/tendermint/account"
|
2014-07-01 14:50:24 -07:00
|
|
|
. "github.com/tendermint/tendermint/binary"
|
2014-11-01 22:42:04 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-09-11 22:44:59 -07:00
|
|
|
. "github.com/tendermint/tendermint/config"
|
2014-07-01 14:50:24 -07:00
|
|
|
"github.com/tendermint/tendermint/merkle"
|
2014-06-04 01:40:17 -07:00
|
|
|
)
|
|
|
|
|
2014-06-05 02:34:45 -07:00
|
|
|
type Block struct {
|
2014-11-01 22:42:04 -07:00
|
|
|
*Header
|
|
|
|
*Validation
|
|
|
|
*Data
|
2014-08-10 16:35:08 -07:00
|
|
|
|
|
|
|
// Volatile
|
|
|
|
hash []byte
|
2014-06-04 01:40:17 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 22:44:59 -07:00
|
|
|
// Basic validation that doesn't involve state data.
|
2014-12-09 18:49:04 -08:00
|
|
|
func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
|
2014-10-31 21:30:52 -07:00
|
|
|
lastBlockParts PartSetHeader, lastBlockTime time.Time) error {
|
|
|
|
if b.Network != Config.Network {
|
|
|
|
return errors.New("Invalid block network")
|
|
|
|
}
|
|
|
|
if b.Height != lastBlockHeight+1 {
|
|
|
|
return errors.New("Invalid block height")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b.LastBlockHash, lastBlockHash) {
|
|
|
|
return errors.New("Invalid block hash")
|
2014-09-11 22:44:59 -07:00
|
|
|
}
|
2014-10-31 21:30:52 -07:00
|
|
|
if !b.LastBlockParts.Equals(lastBlockParts) {
|
|
|
|
return errors.New("Invalid block parts header")
|
2014-09-11 22:44:59 -07:00
|
|
|
}
|
2014-10-31 21:30:52 -07:00
|
|
|
if !b.Time.After(lastBlockTime) {
|
|
|
|
return errors.New("Invalid block time")
|
2014-09-11 22:44:59 -07:00
|
|
|
}
|
|
|
|
// XXX more validation
|
2014-08-10 16:35:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Hash() []byte {
|
2014-10-13 20:07:26 -07:00
|
|
|
if b.hash == nil {
|
2014-09-03 20:41:57 -07:00
|
|
|
hashes := [][]byte{
|
|
|
|
b.Header.Hash(),
|
|
|
|
b.Validation.Hash(),
|
2014-09-11 01:11:41 -07:00
|
|
|
b.Data.Hash(),
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
// Merkle hash from sub-hashes.
|
2014-10-13 20:07:26 -07:00
|
|
|
b.hash = merkle.HashFromHashes(hashes)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return b.hash
|
2014-08-10 16:35:08 -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 {
|
|
|
|
return b.StringWithIndent("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) StringWithIndent(indent string) string {
|
|
|
|
return fmt.Sprintf(`Block{
|
|
|
|
%s %v
|
|
|
|
%s %v
|
|
|
|
%s %v
|
|
|
|
%s}#%X`,
|
|
|
|
indent, b.Header.StringWithIndent(indent+" "),
|
|
|
|
indent, b.Validation.StringWithIndent(indent+" "),
|
|
|
|
indent, b.Data.StringWithIndent(indent+" "),
|
|
|
|
indent, b.hash)
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:42:33 -07:00
|
|
|
func (b *Block) Description() string {
|
|
|
|
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 {
|
2014-10-30 03:32:09 -07:00
|
|
|
Network string
|
2014-12-09 18:49:04 -08:00
|
|
|
Height uint
|
2014-10-30 03:32:09 -07:00
|
|
|
Time time.Time
|
|
|
|
Fees uint64
|
|
|
|
LastBlockHash []byte
|
|
|
|
LastBlockParts PartSetHeader
|
|
|
|
StateHash []byte
|
2014-08-10 16:35:08 -07:00
|
|
|
|
|
|
|
// Volatile
|
|
|
|
hash []byte
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
func (h *Header) Hash() []byte {
|
2014-10-13 20:07:26 -07:00
|
|
|
if h.hash == nil {
|
2014-12-09 18:49:04 -08:00
|
|
|
hasher, n, err := sha256.New(), new(int64), new(error)
|
|
|
|
WriteBinary(h, hasher, n, err)
|
|
|
|
if *err != nil {
|
2014-08-10 16:35:08 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
h.hash = hasher.Sum(nil)
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return h.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Header) StringWithIndent(indent string) string {
|
|
|
|
return fmt.Sprintf(`Header{
|
2014-10-30 03:32:09 -07:00
|
|
|
%s Network: %v
|
|
|
|
%s Height: %v
|
|
|
|
%s Time: %v
|
|
|
|
%s Fees: %v
|
|
|
|
%s LastBlockHash: %X
|
|
|
|
%s LastBlockParts: %v
|
|
|
|
%s StateHash: %X
|
2014-10-13 20:07:26 -07:00
|
|
|
%s}#%X`,
|
|
|
|
indent, h.Network,
|
|
|
|
indent, h.Height,
|
|
|
|
indent, h.Time,
|
|
|
|
indent, h.Fees,
|
|
|
|
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,
|
|
|
|
indent, h.hash)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
type Commit struct {
|
|
|
|
// It's not strictly needed here, but consider adding address here for convenience
|
|
|
|
Round uint
|
|
|
|
Signature SignatureEd25519
|
|
|
|
}
|
2014-08-10 16:35:08 -07:00
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (commit Commit) IsZero() bool {
|
|
|
|
return commit.Round == 0 && commit.Signature.IsZero()
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (commit Commit) String() string {
|
|
|
|
return fmt.Sprintf("Commit{R:%v %X}", commit.Round, Fingerprint(commit.Signature.Bytes))
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type Validation struct {
|
|
|
|
Commits []Commit
|
|
|
|
|
|
|
|
// Volatile
|
|
|
|
hash []byte
|
|
|
|
bitArray BitArray
|
2014-06-05 11:04:56 -07:00
|
|
|
}
|
2014-06-05 02:34:45 -07:00
|
|
|
|
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 {
|
2014-12-09 18:49:04 -08:00
|
|
|
bs := make([]interface{}, len(v.Commits))
|
2014-10-30 03:32:09 -07:00
|
|
|
for i, commit := range v.Commits {
|
2014-12-09 18:49:04 -08:00
|
|
|
bs[i] = commit
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
v.hash = merkle.HashFromBinaries(bs)
|
|
|
|
}
|
|
|
|
return v.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validation) StringWithIndent(indent string) string {
|
2014-10-30 03:32:09 -07:00
|
|
|
commitStrings := make([]string, len(v.Commits))
|
|
|
|
for i, commit := range v.Commits {
|
|
|
|
commitStrings[i] = commit.String()
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return fmt.Sprintf(`Validation{
|
|
|
|
%s %v
|
|
|
|
%s}#%X`,
|
2014-10-30 03:32:09 -07:00
|
|
|
indent, strings.Join(commitStrings, "\n"+indent+" "),
|
2014-10-13 20:07:26 -07:00
|
|
|
indent, v.hash)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2014-11-01 22:42:04 -07:00
|
|
|
func (v *Validation) BitArray() BitArray {
|
|
|
|
if v.bitArray.IsZero() {
|
|
|
|
v.bitArray = NewBitArray(uint(len(v.Commits)))
|
2014-12-09 18:49:04 -08:00
|
|
|
for i, commit := range v.Commits {
|
|
|
|
v.bitArray.SetIndex(uint(i), !commit.IsZero())
|
2014-11-01 22:42:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.bitArray
|
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Data struct {
|
2014-07-01 14:50:24 -07:00
|
|
|
Txs []Tx
|
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 {
|
2014-12-09 18:49:04 -08:00
|
|
|
bs[i] = tx
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
2014-09-14 15:37:32 -07:00
|
|
|
data.hash = merkle.HashFromBinaries(bs)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
return data.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data *Data) StringWithIndent(indent string) string {
|
|
|
|
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
|
|
|
}
|