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

View File

@ -4,7 +4,9 @@ import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt"
"io" "io"
"strings"
"time" "time"
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
@ -58,17 +60,16 @@ func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte) erro
} }
func (b *Block) Hash() []byte { func (b *Block) Hash() []byte {
if b.hash != nil { if b.hash == nil {
return b.hash
} else {
hashes := [][]byte{ hashes := [][]byte{
b.Header.Hash(), b.Header.Hash(),
b.Validation.Hash(), b.Validation.Hash(),
b.Data.Hash(), b.Data.Hash(),
} }
// Merkle hash from sub-hashes. // Merkle hash from sub-hashes.
return merkle.HashFromHashes(hashes) b.hash = merkle.HashFromHashes(hashes)
} }
return b.hash
} }
// Convenience. // Convenience.
@ -90,25 +91,38 @@ func (b *Block) MakeNextBlock() *Block {
Header: Header{ Header: Header{
Network: b.Header.Network, Network: b.Header.Network,
Height: b.Header.Height + 1, Height: b.Header.Height + 1,
//Fees: uint64(0),
Time: time.Now(), Time: time.Now(),
LastBlockHash: b.Hash(), LastBlockHash: b.Hash(),
//ValidationStateHash: nil, StateHash: nil,
//AccountStateHash: 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 { type Header struct {
Network string Network string
Height uint32 Height uint32
Fees uint64
Time time.Time Time time.Time
Fees uint64
LastBlockHash []byte LastBlockHash []byte
ValidationStateHash []byte StateHash []byte
AccountStateHash []byte
// Volatile // Volatile
hash []byte hash []byte
@ -121,37 +135,51 @@ func ReadHeader(r io.Reader, n *int64, err *error) (h Header) {
return Header{ return Header{
Network: ReadString(r, n, err), Network: ReadString(r, n, err),
Height: ReadUInt32(r, n, err), Height: ReadUInt32(r, n, err),
Fees: ReadUInt64(r, n, err),
Time: ReadTime(r, n, err), Time: ReadTime(r, n, err),
Fees: ReadUInt64(r, n, err),
LastBlockHash: ReadByteSlice(r, n, err), LastBlockHash: ReadByteSlice(r, n, err),
ValidationStateHash: ReadByteSlice(r, n, err), StateHash: ReadByteSlice(r, n, err),
AccountStateHash: ReadByteSlice(r, n, err),
} }
} }
func (h *Header) WriteTo(w io.Writer) (n int64, err error) { func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
WriteString(w, h.Network, &n, &err) WriteString(w, h.Network, &n, &err)
WriteUInt32(w, h.Height, &n, &err) WriteUInt32(w, h.Height, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
WriteTime(w, h.Time, &n, &err) WriteTime(w, h.Time, &n, &err)
WriteUInt64(w, h.Fees, &n, &err)
WriteByteSlice(w, h.LastBlockHash, &n, &err) WriteByteSlice(w, h.LastBlockHash, &n, &err)
WriteByteSlice(w, h.ValidationStateHash, &n, &err) WriteByteSlice(w, h.StateHash, &n, &err)
WriteByteSlice(w, h.AccountStateHash, &n, &err)
return return
} }
func (h *Header) Hash() []byte { func (h *Header) Hash() []byte {
if h.hash != nil { if h.hash == nil {
return h.hash
} else {
hasher := sha256.New() hasher := sha256.New()
_, err := h.WriteTo(hasher) _, err := h.WriteTo(hasher)
if err != nil { if err != nil {
panic(err) panic(err)
} }
h.hash = hasher.Sum(nil) 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 { func (v *Validation) Hash() []byte {
if v.hash != nil { if v.hash == nil {
return v.hash bs := make([]Binary, len(v.Signatures))
} else { for i, sig := range v.Signatures {
hasher := sha256.New() bs[i] = Binary(sig)
_, err := v.WriteTo(hasher) }
if err != nil { v.hash = merkle.HashFromBinaries(bs)
panic(err)
} }
v.hash = hasher.Sum(nil)
return v.hash 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 { func (data *Data) Hash() []byte {
if data.hash != nil { if data.hash == nil {
return data.hash bs := make([]Binary, len(data.Txs))
} else {
bs := make([]Binary, 0, len(data.Txs))
for i, tx := range data.Txs { for i, tx := range data.Txs {
bs[i] = Binary(tx) bs[i] = Binary(tx)
} }
data.hash = merkle.HashFromBinaries(bs) 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)
} }

View File

@ -15,16 +15,13 @@ func randBaseTx() BaseTx {
return BaseTx{0, RandUInt64Exp(), randSig()} return BaseTx{0, RandUInt64Exp(), randSig()}
} }
func TestBlock(t *testing.T) { func randBlock() *Block {
// Account Txs // Account Txs
sendTx := &SendTx{ sendTx := &SendTx{
BaseTx: randBaseTx(), BaseTx: randBaseTx(),
To: RandUInt64Exp(), To: RandUInt64Exp(),
Amount: RandUInt64Exp(), Amount: RandUInt64Exp(),
} }
nameTx := &NameTx{ nameTx := &NameTx{
BaseTx: randBaseTx(), BaseTx: randBaseTx(),
Name: string(RandBytes(12)), Name: string(RandBytes(12)),
@ -32,16 +29,13 @@ func TestBlock(t *testing.T) {
} }
// Validation Txs // Validation Txs
bondTx := &BondTx{ bondTx := &BondTx{
BaseTx: randBaseTx(), BaseTx: randBaseTx(),
//UnbondTo: RandUInt64Exp(), //UnbondTo: RandUInt64Exp(),
} }
unbondTx := &UnbondTx{ unbondTx := &UnbondTx{
BaseTx: randBaseTx(), BaseTx: randBaseTx(),
} }
dupeoutTx := &DupeoutTx{ dupeoutTx := &DupeoutTx{
VoteA: Vote{ VoteA: Vote{
Height: RandUInt32Exp(), Height: RandUInt32Exp(),
@ -60,7 +54,6 @@ func TestBlock(t *testing.T) {
} }
// Block // Block
block := &Block{ block := &Block{
Header: Header{ Header: Header{
Network: "Tendermint", Network: "Tendermint",
@ -68,8 +61,7 @@ func TestBlock(t *testing.T) {
Fees: RandUInt64Exp(), Fees: RandUInt64Exp(),
Time: RandTime(), Time: RandTime(),
LastBlockHash: RandBytes(32), LastBlockHash: RandBytes(32),
ValidationStateHash: RandBytes(32), StateHash: RandBytes(32),
AccountStateHash: RandBytes(32),
}, },
Validation: Validation{ Validation: Validation{
Signatures: []Signature{randSig(), randSig()}, Signatures: []Signature{randSig(), randSig()},
@ -78,22 +70,52 @@ func TestBlock(t *testing.T) {
Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, dupeoutTx}, Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, dupeoutTx},
}, },
} }
return block
}
// Write the block, read it in again, write it again. func TestBlock(t *testing.T) {
// Then, compare.
// TODO We should compute the hash instead, so Block -> Bytes -> Block and compare hashes.
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 n int64
var err error var err error
block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err) block2 := ReadBlock(bytes.NewReader(block1Bytes), &n, &err)
if err != nil { if err != nil {
t.Errorf("Reading block failed: %v", err) t.Errorf("Reading block failed: %v", err)
} }
if !bytes.Equal(block1.Hash(), block2.Hash()) {
blockBytes2 := BinaryBytes(block2) t.Errorf("Expected write/read to preserve original hash")
t.Logf("\nBlock1:\n%v", block1)
if !bytes.Equal(blockBytes, blockBytes2) { t.Logf("\nBlock2:\n%v", block2)
t.Fatal("Write->Read of block failed.")
} }
} }

View File

@ -20,8 +20,7 @@ func BenchmarkTestCustom(b *testing.B) {
Fees: 123, Fees: 123,
Time: time.Unix(123, 0), Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"), LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"), StateHash: []byte("statehash"),
AccountStateHash: []byte("accounthash"),
} }
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
@ -45,8 +44,7 @@ type HHeader struct {
Fees uint64 `json:"F"` Fees uint64 `json:"F"`
Time uint64 `json:"T"` Time uint64 `json:"T"`
LastBlockHash []byte `json:"PH"` LastBlockHash []byte `json:"PH"`
ValidationStateHash []byte `json:"VH"` StateHash []byte `json:"SH"`
AccountStateHash []byte `json:"DH"`
} }
func BenchmarkTestJSON(b *testing.B) { func BenchmarkTestJSON(b *testing.B) {
@ -58,8 +56,7 @@ func BenchmarkTestJSON(b *testing.B) {
Fees: 123, Fees: 123,
Time: 123, Time: 123,
LastBlockHash: []byte("prevhash"), LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"), StateHash: []byte("statehash"),
AccountStateHash: []byte("accounthash"),
} }
h2 := &HHeader{} h2 := &HHeader{}
@ -87,8 +84,7 @@ func BenchmarkTestGob(b *testing.B) {
Fees: 123, Fees: 123,
Time: time.Unix(123, 0), Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"), LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"), StateHash: []byte("statehash"),
AccountStateHash: []byte("datahash"),
} }
h2 := &Header{} h2 := &Header{}
@ -116,8 +112,7 @@ func BenchmarkTestMsgPack(b *testing.B) {
Fees: 123, Fees: 123,
Time: time.Unix(123, 0), Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"), LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"), StateHash: []byte("statehash"),
AccountStateHash: []byte("datahash"),
} }
h2 := &Header{} h2 := &Header{}
@ -145,8 +140,7 @@ func BenchmarkTestMsgPack2(b *testing.B) {
Fees: 123, Fees: 123,
Time: time.Unix(123, 0), Time: time.Unix(123, 0),
LastBlockHash: []byte("prevhash"), LastBlockHash: []byte("prevhash"),
ValidationStateHash: []byte("validationhash"), StateHash: []byte("statehash"),
AccountStateHash: []byte("accounthash"),
} }
h2 := &Header{} h2 := &Header{}
var mh codec.MsgpackHandle var mh codec.MsgpackHandle

View File

@ -1,8 +1,10 @@
package blocks package blocks
import ( import (
. "github.com/tendermint/tendermint/binary" "fmt"
"io" "io"
. "github.com/tendermint/tendermint/binary"
) )
type Signable interface { type Signable interface {
@ -35,6 +37,12 @@ func (sig Signature) WriteTo(w io.Writer) (n int64, err error) {
return 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) { func ReadSignatures(r io.Reader, n *int64, err *error) (sigs []Signature) {
length := ReadUInt32(r, n, err) length := ReadUInt32(r, n, err)
for i := uint32(0); i < length; i++ { for i := uint32(0); i < length; i++ {

View File

@ -1,9 +1,11 @@
package blocks package blocks
import ( import (
"fmt"
"io"
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"io"
) )
/* /*
@ -21,6 +23,7 @@ type Tx interface {
Signable Signable
GetSequence() uint GetSequence() uint
GetFee() uint64 GetFee() uint64
String() string
} }
const ( const (
@ -108,6 +111,10 @@ func (tx *BaseTx) SetSignature(sig Signature) {
tx.Signature = sig 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 { type SendTx struct {
@ -124,6 +131,10 @@ func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
func (tx *SendTx) String() string {
return fmt.Sprintf("SendTx{%v To:%v Amount:%v}", tx.BaseTx, tx.To, tx.Amount)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type NameTx struct { type NameTx struct {
@ -140,6 +151,10 @@ func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
func (tx *NameTx) String() string {
return fmt.Sprintf("NameTx{%v Name:%v PubKey:%X}", tx.BaseTx, tx.Name, tx.PubKey)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type BondTx struct { type BondTx struct {
@ -154,6 +169,10 @@ func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
func (tx *BondTx) String() string {
return fmt.Sprintf("BondTx{%v}", tx.BaseTx)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type UnbondTx struct { type UnbondTx struct {
@ -166,6 +185,10 @@ func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
func (tx *UnbondTx) String() string {
return fmt.Sprintf("UnbondTx{%v}", tx.BaseTx)
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type DupeoutTx struct { type DupeoutTx struct {
@ -181,3 +204,7 @@ func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
WriteBinary(w, &tx.VoteB, &n, &err) WriteBinary(w, &tx.VoteB, &n, &err)
return return
} }
func (tx *DupeoutTx) String() string {
return fmt.Sprintf("DupeoutTx{%v VoteA:%v VoteB:%v}", tx.BaseTx, tx.VoteA, tx.VoteB)
}

View File

@ -53,8 +53,10 @@ func (mem *Mempool) MakeProposalBlock() (*Block, *State) {
return nextBlock, mem.state return nextBlock, mem.state
} }
// Txs that are present in block are discarded from mempool. // "block" is the new block being committed.
// Txs that have become invalid in the new state are also discarded. // "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded.
func (mem *Mempool) ResetForBlockAndState(block *Block, state *State) { func (mem *Mempool) ResetForBlockAndState(block *Block, state *State) {
mem.mtx.Lock() mem.mtx.Lock()
defer mem.mtx.Unlock() defer mem.mtx.Unlock()

View File

@ -40,18 +40,24 @@ func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, db DB) *IAVLTree {
// The returned tree and the original tree are goroutine independent. // The returned tree and the original tree are goroutine independent.
// That is, they can each run in their own goroutine. // That is, they can each run in their own goroutine.
func (t *IAVLTree) Copy() Tree { func (t *IAVLTree) Copy() Tree {
if t.root == nil {
return &IAVLTree{
keyCodec: t.keyCodec,
valueCodec: t.valueCodec,
root: nil,
ndb: t.ndb,
}
}
if t.ndb != nil && !t.root.persisted { if t.ndb != nil && !t.root.persisted {
panic("It is unsafe to Copy() an unpersisted tree.")
// Saving a tree finalizes all the nodes. // Saving a tree finalizes all the nodes.
// It sets all the hashes recursively, // It sets all the hashes recursively,
// clears all the leftNode/rightNode values recursively, // clears all the leftNode/rightNode values recursively,
// and all the .persisted flags get set. // and all the .persisted flags get set.
// On the other hand, in-memory trees (ndb == nil) panic("It is unsafe to Copy() an unpersisted tree.")
// don't mutate
} else if t.ndb == nil && t.root.hash == nil { } else if t.ndb == nil && t.root.hash == nil {
panic("An in-memory IAVLTree must be hashed first")
// An in-memory IAVLTree is finalized when the hashes are // An in-memory IAVLTree is finalized when the hashes are
// calculated. // calculated.
t.root.hashWithCount(t)
} }
return &IAVLTree{ return &IAVLTree{
keyCodec: t.keyCodec, keyCodec: t.keyCodec,

View File

@ -314,9 +314,12 @@ func (s *State) releaseValidator(accountId uint64) {
} }
} }
// "checkStateHash": If false, instead of checking the resulting
// state.Hash() against block.StateHash, it *sets* the block.StateHash.
// (used for constructing a new proposal)
// NOTE: If an error occurs during block execution, state will be left // NOTE: If an error occurs during block execution, state will be left
// at an invalid state. Copy the state before calling AppendBlock! // at an invalid state. Copy the state before calling AppendBlock!
func (s *State) AppendBlock(b *Block) error { func (s *State) AppendBlock(b *Block, checkStateHash bool) error {
// Basic block validation. // Basic block validation.
err := b.ValidateBasic(s.Height, s.BlockHash) err := b.ValidateBasic(s.Height, s.BlockHash)
if err != nil { if err != nil {
@ -373,15 +376,20 @@ func (s *State) AppendBlock(b *Block) error {
// Increment validator AccumPowers // Increment validator AccumPowers
s.BondedValidators.IncrementAccum() s.BondedValidators.IncrementAccum()
// State hashes should match // Check or set block.StateHash
// XXX include UnbondingValidators.Hash(). stateHash := s.Hash()
if !bytes.Equal(s.BondedValidators.Hash(), b.ValidationStateHash) { if checkStateHash {
return Errorf("Invalid ValidationStateHash. Got %X, block says %X", // State hash should match
s.BondedValidators.Hash(), b.ValidationStateHash) if !bytes.Equal(stateHash, b.StateHash) {
return Errorf("Invalid state hash. Got %X, block says %X",
stateHash, b.StateHash)
} }
if !bytes.Equal(s.AccountDetails.Hash(), b.AccountStateHash) { } else {
return Errorf("Invalid AccountStateHash. Got %X, block says %X", // Set the state hash.
s.AccountDetails.Hash(), b.AccountStateHash) if b.StateHash != nil {
panic("Cannot overwrite block.StateHash")
}
b.StateHash = stateHash
} }
s.Height = b.Height s.Height = b.Height
@ -401,3 +409,14 @@ func (s *State) GetAccountDetail(accountId uint64) *AccountDetail {
func (s *State) SetAccountDetail(accDet *AccountDetail) (updated bool) { func (s *State) SetAccountDetail(accDet *AccountDetail) (updated bool) {
return s.AccountDetails.Set(accDet.Id, accDet) return s.AccountDetails.Set(accDet.Id, accDet)
} }
// Returns a hash that represents the state data,
// excluding Height, BlockHash, and CommitTime.
func (s *State) Hash() []byte {
hashables := []merkle.Hashable{
s.AccountDetails,
s.BondedValidators,
s.UnbondingValidators,
}
return merkle.HashFromHashables(hashables)
}

View File

@ -35,32 +35,67 @@ func randGenesisState(numAccounts int, numValidators int) *State {
} }
} }
s0 := GenesisState(db, time.Now(), accountDetails) s0 := GenesisState(db, time.Now(), accountDetails)
s0.Save(time.Now())
return s0 return s0
} }
func TestCopyState(t *testing.T) {
// Generate a state
s0 := randGenesisState(10, 5)
s0Hash := s0.Hash()
if len(s0Hash) == 0 {
t.Error("Expected state hash")
}
// Check hash of copy
s0Copy := s0.Copy()
if !bytes.Equal(s0Hash, s0Copy.Hash()) {
t.Error("Expected state copy hash to be the same")
}
// Mutate the original.
_, accDet_ := s0.AccountDetails.GetByIndex(0)
accDet := accDet_.(*AccountDetail)
if accDet == nil {
t.Error("Expected state to have an account")
}
accDet.Balance += 1
s0.AccountDetails.Set(accDet.Id, accDet)
if bytes.Equal(s0Hash, s0.Hash()) {
t.Error("Expected state hash to have changed")
}
if !bytes.Equal(s0Hash, s0Copy.Hash()) {
t.Error("Expected state copy hash to have not changed")
}
}
func TestGenesisSaveLoad(t *testing.T) { func TestGenesisSaveLoad(t *testing.T) {
// Generate a state, save & load it. // Generate a state, save & load it.
s0 := randGenesisState(10, 5) s0 := randGenesisState(10, 5)
// Figure out what the next state hashes should be.
s0.BondedValidators.Hash()
s0ValsCopy := s0.BondedValidators.Copy()
s0ValsCopy.IncrementAccum()
nextValidationStateHash := s0ValsCopy.Hash()
nextAccountStateHash := s0.AccountDetails.Hash()
// Mutate the state to append one empty block. // Mutate the state to append one empty block.
block := &Block{ block := &Block{
Header: Header{ Header: Header{
Network: Config.Network, Network: Config.Network,
Height: 1, Height: 1,
ValidationStateHash: nextValidationStateHash, StateHash: nil,
AccountStateHash: nextAccountStateHash,
}, },
Data: Data{ Data: Data{
Txs: []Tx{}, Txs: []Tx{},
}, },
} }
err := s0.AppendBlock(block) // The second argument to AppendBlock() is false,
// which sets Block.Header.StateHash.
err := s0.Copy().AppendBlock(block, false)
if err != nil {
t.Error("Error appending initial block:", err)
}
if len(block.Header.StateHash) == 0 {
t.Error("Expected StateHash but got nothing.")
}
// Now append the block to s0.
// This time we also check the StateHash (as computed above).
err = s0.AppendBlock(block, true)
if err != nil { if err != nil {
t.Error("Error appending initial block:", err) t.Error("Error appending initial block:", err)
} }
@ -92,13 +127,28 @@ func TestGenesisSaveLoad(t *testing.T) {
if !bytes.Equal(s0.BlockHash, s1.BlockHash) { if !bytes.Equal(s0.BlockHash, s1.BlockHash) {
t.Error("BlockHash mismatch") t.Error("BlockHash mismatch")
} }
// Compare BondedValidators // Compare state merkle trees
if s0.BondedValidators.Size() != s1.BondedValidators.Size() { if s0.BondedValidators.Size() != s1.BondedValidators.Size() {
t.Error("BondedValidators Size mismatch") t.Error("BondedValidators Size mismatch")
} }
if s0.BondedValidators.TotalVotingPower() != s1.BondedValidators.TotalVotingPower() { if s0.BondedValidators.TotalVotingPower() != s1.BondedValidators.TotalVotingPower() {
t.Error("BondedValidators TotalVotingPower mismatch") t.Error("BondedValidators TotalVotingPower mismatch")
} }
if bytes.Equal(s0.BondedValidators.Hash(), s1.BondedValidators.Hash()) {
// The BondedValidators hash should have changed because
// each AppendBlock() calls IncrementAccum(),
// changing each validator's Accum.
t.Error("BondedValidators hash should have changed")
}
if s0.UnbondingValidators.Size() != s1.UnbondingValidators.Size() {
t.Error("UnbondingValidators Size mismatch")
}
if s0.UnbondingValidators.TotalVotingPower() != s1.UnbondingValidators.TotalVotingPower() {
t.Error("UnbondingValidators TotalVotingPower mismatch")
}
if !bytes.Equal(s0.UnbondingValidators.Hash(), s1.UnbondingValidators.Hash()) {
t.Error("UnbondingValidators hash mismatch")
}
if !bytes.Equal(s0.AccountDetails.Hash(), s1.AccountDetails.Hash()) { if !bytes.Equal(s0.AccountDetails.Hash(), s1.AccountDetails.Hash()) {
t.Error("AccountDetail mismatch") t.Error("AccountDetail mismatch")
} }