mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-06 08:07:11 +00:00
prettyprint block, unified state hash, test block mutation.
This commit is contained in:
+29
-10
@@ -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
|
||||
// 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.
|
||||
err := b.ValidateBasic(s.Height, s.BlockHash)
|
||||
if err != nil {
|
||||
@@ -373,15 +376,20 @@ func (s *State) AppendBlock(b *Block) error {
|
||||
// Increment validator AccumPowers
|
||||
s.BondedValidators.IncrementAccum()
|
||||
|
||||
// State hashes should match
|
||||
// XXX include UnbondingValidators.Hash().
|
||||
if !bytes.Equal(s.BondedValidators.Hash(), b.ValidationStateHash) {
|
||||
return Errorf("Invalid ValidationStateHash. Got %X, block says %X",
|
||||
s.BondedValidators.Hash(), b.ValidationStateHash)
|
||||
}
|
||||
if !bytes.Equal(s.AccountDetails.Hash(), b.AccountStateHash) {
|
||||
return Errorf("Invalid AccountStateHash. Got %X, block says %X",
|
||||
s.AccountDetails.Hash(), b.AccountStateHash)
|
||||
// Check or set block.StateHash
|
||||
stateHash := s.Hash()
|
||||
if checkStateHash {
|
||||
// State hash should match
|
||||
if !bytes.Equal(stateHash, b.StateHash) {
|
||||
return Errorf("Invalid state hash. Got %X, block says %X",
|
||||
stateHash, b.StateHash)
|
||||
}
|
||||
} else {
|
||||
// Set the state hash.
|
||||
if b.StateHash != nil {
|
||||
panic("Cannot overwrite block.StateHash")
|
||||
}
|
||||
b.StateHash = stateHash
|
||||
}
|
||||
|
||||
s.Height = b.Height
|
||||
@@ -401,3 +409,14 @@ func (s *State) GetAccountDetail(accountId uint64) *AccountDetail {
|
||||
func (s *State) SetAccountDetail(accDet *AccountDetail) (updated bool) {
|
||||
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)
|
||||
}
|
||||
|
||||
+62
-12
@@ -35,32 +35,67 @@ func randGenesisState(numAccounts int, numValidators int) *State {
|
||||
}
|
||||
}
|
||||
s0 := GenesisState(db, time.Now(), accountDetails)
|
||||
s0.Save(time.Now())
|
||||
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) {
|
||||
|
||||
// Generate a state, save & load it.
|
||||
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.
|
||||
block := &Block{
|
||||
Header: Header{
|
||||
Network: Config.Network,
|
||||
Height: 1,
|
||||
ValidationStateHash: nextValidationStateHash,
|
||||
AccountStateHash: nextAccountStateHash,
|
||||
Network: Config.Network,
|
||||
Height: 1,
|
||||
StateHash: nil,
|
||||
},
|
||||
Data: Data{
|
||||
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 {
|
||||
t.Error("Error appending initial block:", err)
|
||||
}
|
||||
@@ -92,13 +127,28 @@ func TestGenesisSaveLoad(t *testing.T) {
|
||||
if !bytes.Equal(s0.BlockHash, s1.BlockHash) {
|
||||
t.Error("BlockHash mismatch")
|
||||
}
|
||||
// Compare BondedValidators
|
||||
// Compare state merkle trees
|
||||
if s0.BondedValidators.Size() != s1.BondedValidators.Size() {
|
||||
t.Error("BondedValidators Size mismatch")
|
||||
}
|
||||
if s0.BondedValidators.TotalVotingPower() != s1.BondedValidators.TotalVotingPower() {
|
||||
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()) {
|
||||
t.Error("AccountDetail mismatch")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user