prettyprint block, unified state hash, test block mutation.

This commit is contained in:
Jae Kwon
2014-10-13 20:07:26 -07:00
parent 810aeb7bcb
commit c8f996f345
9 changed files with 316 additions and 141 deletions
+91 -44
View File
@@ -4,7 +4,9 @@ import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"io"
"strings"
"time"
. "github.com/tendermint/tendermint/binary"
@@ -58,17 +60,16 @@ func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte) erro
}
func (b *Block) Hash() []byte {
if b.hash != nil {
return b.hash
} else {
if b.hash == nil {
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
}
// Merkle hash from sub-hashes.
return merkle.HashFromHashes(hashes)
b.hash = merkle.HashFromHashes(hashes)
}
return b.hash
}
// Convenience.
@@ -88,27 +89,40 @@ func (b *Block) HashesTo(hash []byte) bool {
func (b *Block) MakeNextBlock() *Block {
return &Block{
Header: Header{
Network: b.Header.Network,
Height: b.Header.Height + 1,
//Fees: uint64(0),
Network: b.Header.Network,
Height: b.Header.Height + 1,
Time: time.Now(),
LastBlockHash: b.Hash(),
//ValidationStateHash: nil,
//AccountStateHash: nil,
StateHash: nil,
},
}
}
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)
}
//-----------------------------------------------------------------------------
type Header struct {
Network string
Height uint32
Fees uint64
Time time.Time
LastBlockHash []byte
ValidationStateHash []byte
AccountStateHash []byte
Network string
Height uint32
Time time.Time
Fees uint64
LastBlockHash []byte
StateHash []byte
// Volatile
hash []byte
@@ -119,39 +133,53 @@ func ReadHeader(r io.Reader, n *int64, err *error) (h Header) {
return Header{}
}
return Header{
Network: ReadString(r, n, err),
Height: ReadUInt32(r, n, err),
Fees: ReadUInt64(r, n, err),
Time: ReadTime(r, n, err),
LastBlockHash: ReadByteSlice(r, n, err),
ValidationStateHash: ReadByteSlice(r, n, err),
AccountStateHash: ReadByteSlice(r, n, err),
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),
StateHash: ReadByteSlice(r, n, err),
}
}
func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
WriteString(w, h.Network, &n, &err)
WriteUInt32(w, h.Height, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
WriteTime(w, h.Time, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
WriteByteSlice(w, h.LastBlockHash, &n, &err)
WriteByteSlice(w, h.ValidationStateHash, &n, &err)
WriteByteSlice(w, h.AccountStateHash, &n, &err)
WriteByteSlice(w, h.StateHash, &n, &err)
return
}
func (h *Header) Hash() []byte {
if h.hash != nil {
return h.hash
} else {
if h.hash == nil {
hasher := sha256.New()
_, err := h.WriteTo(hasher)
if err != nil {
panic(err)
}
h.hash = hasher.Sum(nil)
return h.hash
}
return h.hash
}
func (h *Header) StringWithIndent(indent string) string {
return fmt.Sprintf(`Header{
%s Network: %v
%s Height: %v
%s Time: %v
%s Fees: %v
%s LastBlockHash: %X
%s StateHash: %X
%s}#%X`,
indent, h.Network,
indent, h.Height,
indent, h.Time,
indent, h.Fees,
indent, h.LastBlockHash,
indent, h.StateHash,
indent, h.hash)
}
//-----------------------------------------------------------------------------
@@ -183,17 +211,26 @@ func (v *Validation) WriteTo(w io.Writer) (n int64, err error) {
}
func (v *Validation) Hash() []byte {
if v.hash != nil {
return v.hash
} else {
hasher := sha256.New()
_, err := v.WriteTo(hasher)
if err != nil {
panic(err)
if v.hash == nil {
bs := make([]Binary, len(v.Signatures))
for i, sig := range v.Signatures {
bs[i] = Binary(sig)
}
v.hash = hasher.Sum(nil)
return v.hash
v.hash = merkle.HashFromBinaries(bs)
}
return v.hash
}
func (v *Validation) StringWithIndent(indent string) string {
sigStrings := make([]string, len(v.Signatures))
for i, sig := range v.Signatures {
sigStrings[i] = sig.String()
}
return fmt.Sprintf(`Validation{
%s %v
%s}#%X`,
indent, strings.Join(sigStrings, "\n"+indent+" "),
indent, v.hash)
}
//-----------------------------------------------------------------------------
@@ -223,14 +260,24 @@ func (data *Data) WriteTo(w io.Writer) (n int64, err error) {
}
func (data *Data) Hash() []byte {
if data.hash != nil {
return data.hash
} else {
bs := make([]Binary, 0, len(data.Txs))
if data.hash == nil {
bs := make([]Binary, len(data.Txs))
for i, tx := range data.Txs {
bs[i] = Binary(tx)
}
data.hash = merkle.HashFromBinaries(bs)
return data.hash
}
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)
}
+47 -25
View File
@@ -15,16 +15,13 @@ func randBaseTx() BaseTx {
return BaseTx{0, RandUInt64Exp(), randSig()}
}
func TestBlock(t *testing.T) {
func randBlock() *Block {
// Account Txs
sendTx := &SendTx{
BaseTx: randBaseTx(),
To: RandUInt64Exp(),
Amount: RandUInt64Exp(),
}
nameTx := &NameTx{
BaseTx: randBaseTx(),
Name: string(RandBytes(12)),
@@ -32,16 +29,13 @@ func TestBlock(t *testing.T) {
}
// Validation Txs
bondTx := &BondTx{
BaseTx: randBaseTx(),
//UnbondTo: RandUInt64Exp(),
}
unbondTx := &UnbondTx{
BaseTx: randBaseTx(),
}
dupeoutTx := &DupeoutTx{
VoteA: Vote{
Height: RandUInt32Exp(),
@@ -60,16 +54,14 @@ func TestBlock(t *testing.T) {
}
// Block
block := &Block{
Header: Header{
Network: "Tendermint",
Height: RandUInt32Exp(),
Fees: RandUInt64Exp(),
Time: RandTime(),
LastBlockHash: RandBytes(32),
ValidationStateHash: RandBytes(32),
AccountStateHash: RandBytes(32),
Network: "Tendermint",
Height: RandUInt32Exp(),
Fees: RandUInt64Exp(),
Time: RandTime(),
LastBlockHash: RandBytes(32),
StateHash: RandBytes(32),
},
Validation: Validation{
Signatures: []Signature{randSig(), randSig()},
@@ -78,22 +70,52 @@ func TestBlock(t *testing.T) {
Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, dupeoutTx},
},
}
return block
}
// Write the block, read it in again, write it again.
// Then, compare.
// TODO We should compute the hash instead, so Block -> Bytes -> Block and compare hashes.
func TestBlock(t *testing.T) {
blockBytes := BinaryBytes(block)
block := randBlock()
// Mutate the block and ensure that the hash changed.
lastHash := block.Hash()
expectChange := func(mutateFn func(b *Block), message string) {
// mutate block
mutateFn(block)
// nuke hashes
block.hash = nil
block.Header.hash = nil
block.Validation.hash = nil
block.Data.hash = nil
// compare
if bytes.Equal(lastHash, block.Hash()) {
t.Error(message)
} else {
lastHash = block.Hash()
}
}
expectChange(func(b *Block) { b.Header.Network = "blah" }, "Expected hash to depend on Network")
expectChange(func(b *Block) { b.Header.Height += 1 }, "Expected hash to depend on Height")
expectChange(func(b *Block) { b.Header.Fees += 1 }, "Expected hash to depend on Fees")
expectChange(func(b *Block) { b.Header.Time = RandTime() }, "Expected hash to depend on Time")
expectChange(func(b *Block) { b.Header.LastBlockHash = RandBytes(32) }, "Expected hash to depend on LastBlockHash")
expectChange(func(b *Block) { b.Header.StateHash = RandBytes(32) }, "Expected hash to depend on StateHash")
expectChange(func(b *Block) { b.Validation.Signatures[0].SignerId += 1 }, "Expected hash to depend on Validation Signature")
expectChange(func(b *Block) { b.Validation.Signatures[0].Bytes = RandBytes(32) }, "Expected hash to depend on Validation Signature")
expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).SignerId += 1 }, "Expected hash to depend on tx Signature")
expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).Amount += 1 }, "Expected hash to depend on send tx Amount")
// Write the block, read it in again, check hash.
block1 := randBlock()
block1Bytes := BinaryBytes(block1)
var n int64
var err error
block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err)
block2 := ReadBlock(bytes.NewReader(block1Bytes), &n, &err)
if err != nil {
t.Errorf("Reading block failed: %v", err)
}
blockBytes2 := BinaryBytes(block2)
if !bytes.Equal(blockBytes, blockBytes2) {
t.Fatal("Write->Read of block failed.")
if !bytes.Equal(block1.Hash(), block2.Hash()) {
t.Errorf("Expected write/read to preserve original hash")
t.Logf("\nBlock1:\n%v", block1)
t.Logf("\nBlock2:\n%v", block2)
}
}
+36 -42
View File
@@ -15,13 +15,12 @@ func BenchmarkTestCustom(b *testing.B) {
b.StopTimer()
h := &Header{
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"),
AccountStateHash: []byte("accounthash"),
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
StateHash: []byte("statehash"),
}
buf := bytes.NewBuffer(nil)
@@ -40,26 +39,24 @@ func BenchmarkTestCustom(b *testing.B) {
}
type HHeader struct {
Network string `json:"N"`
Height uint64 `json:"H"`
Fees uint64 `json:"F"`
Time uint64 `json:"T"`
LastBlockHash []byte `json:"PH"`
ValidationStateHash []byte `json:"VH"`
AccountStateHash []byte `json:"DH"`
Network string `json:"N"`
Height uint64 `json:"H"`
Fees uint64 `json:"F"`
Time uint64 `json:"T"`
LastBlockHash []byte `json:"PH"`
StateHash []byte `json:"SH"`
}
func BenchmarkTestJSON(b *testing.B) {
b.StopTimer()
h := &HHeader{
Network: "Header",
Height: 123,
Fees: 123,
Time: 123,
LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"),
AccountStateHash: []byte("accounthash"),
Network: "Header",
Height: 123,
Fees: 123,
Time: 123,
LastBlockHash: []byte("prevhash"),
StateHash: []byte("statehash"),
}
h2 := &HHeader{}
@@ -82,13 +79,12 @@ func BenchmarkTestGob(b *testing.B) {
b.StopTimer()
h := &Header{
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"),
AccountStateHash: []byte("datahash"),
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
StateHash: []byte("statehash"),
}
h2 := &Header{}
@@ -111,13 +107,12 @@ func BenchmarkTestMsgPack(b *testing.B) {
b.StopTimer()
h := &Header{
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"),
AccountStateHash: []byte("datahash"),
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
StateHash: []byte("statehash"),
}
h2 := &Header{}
@@ -140,13 +135,12 @@ func BenchmarkTestMsgPack2(b *testing.B) {
b.StopTimer()
h := &Header{
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"),
AccountStateHash: []byte("accounthash"),
Network: "Header",
Height: 123,
Fees: 123,
Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"),
StateHash: []byte("statehash"),
}
h2 := &Header{}
var mh codec.MsgpackHandle
+9 -1
View File
@@ -1,8 +1,10 @@
package blocks
import (
. "github.com/tendermint/tendermint/binary"
"fmt"
"io"
. "github.com/tendermint/tendermint/binary"
)
type Signable interface {
@@ -35,6 +37,12 @@ func (sig Signature) WriteTo(w io.Writer) (n int64, err error) {
return
}
func (sig Signature) String() string {
return fmt.Sprintf("Signature{%v:%X}", sig.SignerId, sig.Bytes)
}
//-------------------------------------
func ReadSignatures(r io.Reader, n *int64, err *error) (sigs []Signature) {
length := ReadUInt32(r, n, err)
for i := uint32(0); i < length; i++ {
+28 -1
View File
@@ -1,9 +1,11 @@
package blocks
import (
"fmt"
"io"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"io"
)
/*
@@ -21,6 +23,7 @@ type Tx interface {
Signable
GetSequence() uint
GetFee() uint64
String() string
}
const (
@@ -108,6 +111,10 @@ func (tx *BaseTx) SetSignature(sig Signature) {
tx.Signature = sig
}
func (tx *BaseTx) String() string {
return fmt.Sprintf("{S:%v F:%v Sig:%X}", tx.Sequence, tx.Fee, tx.Signature)
}
//-----------------------------------------------------------------------------
type SendTx struct {
@@ -124,6 +131,10 @@ func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
return
}
func (tx *SendTx) String() string {
return fmt.Sprintf("SendTx{%v To:%v Amount:%v}", tx.BaseTx, tx.To, tx.Amount)
}
//-----------------------------------------------------------------------------
type NameTx struct {
@@ -140,6 +151,10 @@ func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
return
}
func (tx *NameTx) String() string {
return fmt.Sprintf("NameTx{%v Name:%v PubKey:%X}", tx.BaseTx, tx.Name, tx.PubKey)
}
//-----------------------------------------------------------------------------
type BondTx struct {
@@ -154,6 +169,10 @@ func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
return
}
func (tx *BondTx) String() string {
return fmt.Sprintf("BondTx{%v}", tx.BaseTx)
}
//-----------------------------------------------------------------------------
type UnbondTx struct {
@@ -166,6 +185,10 @@ func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
return
}
func (tx *UnbondTx) String() string {
return fmt.Sprintf("UnbondTx{%v}", tx.BaseTx)
}
//-----------------------------------------------------------------------------
type DupeoutTx struct {
@@ -181,3 +204,7 @@ func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
WriteBinary(w, &tx.VoteB, &n, &err)
return
}
func (tx *DupeoutTx) String() string {
return fmt.Sprintf("DupeoutTx{%v VoteA:%v VoteB:%v}", tx.BaseTx, tx.VoteA, tx.VoteB)
}