tendermint/blocks/block.go

289 lines
6.2 KiB
Go
Raw Normal View History

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"
"fmt"
2014-08-10 16:35:08 -07:00
"io"
"strings"
"time"
2014-08-10 16:35:08 -07:00
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
. "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 {
*Header
*Validation
*Data
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
2014-06-04 01:40:17 -07:00
}
func ReadBlock(r io.Reader, n *int64, err *error) *Block {
2014-07-01 14:50:24 -07:00
return &Block{
Header: ReadHeader(r, n, err),
Validation: ReadValidation(r, n, err),
Data: ReadData(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-06-04 01:40:17 -07:00
}
2014-08-10 16:35:08 -07:00
func (b *Block) WriteTo(w io.Writer) (n int64, err error) {
WriteBinary(w, b.Header, &n, &err)
WriteBinary(w, b.Validation, &n, &err)
WriteBinary(w, b.Data, &n, &err)
2014-08-10 16:35:08 -07:00
return
}
2014-09-11 22:44:59 -07:00
// Basic validation that doesn't involve state data.
func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte,
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
}
if !b.LastBlockParts.Equals(lastBlockParts) {
return errors.New("Invalid block parts header")
2014-09-11 22:44:59 -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 {
if b.hash == nil {
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
2014-08-10 16:35:08 -07:00
}
// Merkle hash from sub-hashes.
b.hash = merkle.HashFromHashes(hashes)
2014-08-10 16:35:08 -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
}
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
Height uint32
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
}
func ReadHeader(r io.Reader, n *int64, err *error) *Header {
if *err != nil {
return nil
}
return &Header{
2014-10-30 03:32:09 -07:00
Network: ReadString(r, n, err),
Height: ReadUInt32(r, n, err),
Time: ReadTime(r, n, err),
Fees: ReadUInt64(r, n, err),
LastBlockHash: ReadByteSlice(r, n, err),
LastBlockParts: ReadPartSetHeader(r, n, err),
StateHash: ReadByteSlice(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
2014-09-11 22:44:59 -07:00
WriteString(w, h.Network, &n, &err)
WriteUInt32(w, h.Height, &n, &err)
WriteTime(w, h.Time, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
2014-09-11 22:44:59 -07:00
WriteByteSlice(w, h.LastBlockHash, &n, &err)
2014-10-30 03:32:09 -07:00
WriteBinary(w, h.LastBlockParts, &n, &err)
WriteByteSlice(w, h.StateHash, &n, &err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (h *Header) Hash() []byte {
if h.hash == nil {
2014-08-10 16:35:08 -07:00
hasher := sha256.New()
_, err := h.WriteTo(hasher)
if err != nil {
panic(err)
}
h.hash = hasher.Sum(nil)
}
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
%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,
indent, h.StateHash,
indent, h.hash)
2014-08-10 16:35:08 -07:00
}
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type Validation struct {
2014-10-30 03:32:09 -07:00
Commits []RoundSignature
2014-08-10 16:35:08 -07:00
// Volatile
hash []byte
bitArray BitArray
2014-06-05 02:34:45 -07:00
}
func ReadValidation(r io.Reader, n *int64, err *error) *Validation {
return &Validation{
2014-10-30 03:32:09 -07:00
Commits: ReadRoundSignatures(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
2014-10-30 03:32:09 -07:00
WriteRoundSignatures(w, v.Commits, &n, &err)
2014-07-01 14:50:24 -07:00
return
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 {
if v.hash == nil {
2014-10-30 03:32:09 -07:00
bs := make([]Binary, len(v.Commits))
for i, commit := range v.Commits {
bs[i] = Binary(commit)
2014-08-10 16:35:08 -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
}
return fmt.Sprintf(`Validation{
%s %v
%s}#%X`,
2014-10-30 03:32:09 -07:00
indent, strings.Join(commitStrings, "\n"+indent+" "),
indent, v.hash)
2014-08-10 16:35:08 -07:00
}
func (v *Validation) BitArray() BitArray {
if v.bitArray.IsZero() {
v.bitArray = NewBitArray(uint(len(v.Commits)))
for i, rsig := range v.Commits {
v.bitArray.SetIndex(uint(i), !rsig.IsZero())
}
}
return v.bitArray
}
//-----------------------------------------------------------------------------
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
}
func ReadData(r io.Reader, n *int64, err *error) *Data {
numTxs := ReadUInt32(r, n, err)
2014-07-01 14:50:24 -07:00
txs := make([]Tx, 0, numTxs)
2014-08-30 16:28:51 -07:00
for i := uint32(0); i < numTxs; i++ {
txs = append(txs, ReadTx(r, n, err))
2014-07-01 14:50:24 -07:00
}
return &Data{Txs: txs}
2014-06-05 02:34:45 -07:00
}
func (data *Data) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, uint32(len(data.Txs)), &n, &err)
for _, tx := range data.Txs {
WriteBinary(w, tx, &n, &err)
2014-07-01 14:50:24 -07:00
}
return
2014-06-05 02:34:45 -07:00
}
func (data *Data) Hash() []byte {
if data.hash == nil {
bs := make([]Binary, len(data.Txs))
for i, tx := range data.Txs {
2014-08-10 16:35:08 -07:00
bs[i] = Binary(tx)
}
2014-09-14 15:37:32 -07:00
data.hash = merkle.HashFromBinaries(bs)
2014-07-01 14:50:24 -07:00
}
return data.hash
}
func (data *Data) StringWithIndent(indent string) string {
txStrings := make([]string, len(data.Txs))
for i, tx := range data.Txs {
txStrings[i] = tx.String()
}
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
}