Browse Source

validate block.ValidatorsHash

pull/999/head
Ethan Buchman 7 years ago
parent
commit
bfcb40bf6b
2 changed files with 16 additions and 4 deletions
  1. +10
    -4
      state/execution.go
  2. +6
    -0
      state/execution_test.go

+ 10
- 4
state/execution.go View File

@ -249,11 +249,12 @@ func (s *State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*
} }
func (s *State) validateBlock(b *types.Block) error { func (s *State) validateBlock(b *types.Block) error {
// Basic block validation.
// validate internal consistency
if err := b.ValidateBasic(); err != nil { if err := b.ValidateBasic(); err != nil {
return err return err
} }
// validate basic info
if b.ChainID != s.ChainID { if b.ChainID != s.ChainID {
return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID) return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID)
} }
@ -268,14 +269,16 @@ func (s *State) validateBlock(b *types.Block) error {
} }
*/ */
// validate prev block info
if !b.LastBlockID.Equals(s.LastBlockID) {
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
}
newTxs := int64(len(b.Data.Txs)) newTxs := int64(len(b.Data.Txs))
if b.TotalTxs != s.LastBlockTotalTx+newTxs { if b.TotalTxs != s.LastBlockTotalTx+newTxs {
return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs) return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs)
} }
if !b.LastBlockID.Equals(s.LastBlockID) {
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
}
// validate app info
if !bytes.Equal(b.AppHash, s.AppHash) { if !bytes.Equal(b.AppHash, s.AppHash) {
return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash) return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash)
} }
@ -285,6 +288,9 @@ func (s *State) validateBlock(b *types.Block) error {
if !bytes.Equal(b.LastResultsHash, s.LastResultsHash) { if !bytes.Equal(b.LastResultsHash, s.LastResultsHash) {
return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", s.LastResultsHash, b.LastResultsHash) return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", s.LastResultsHash, b.LastResultsHash)
} }
if !bytes.Equal(b.ValidatorsHash, s.Validators.Hash()) {
return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", s.Validators.Hash(), b.ValidatorsHash)
}
// Validate block LastCommit. // Validate block LastCommit.
if b.Height == 1 { if b.Height == 1 {


+ 6
- 0
state/execution_test.go View File

@ -73,6 +73,12 @@ func TestValidateBlock(t *testing.T) {
block.LastResultsHash = []byte("wrong results hash") block.LastResultsHash = []byte("wrong results hash")
err = state.ValidateBlock(block) err = state.ValidateBlock(block)
require.Error(t, err) require.Error(t, err)
// wrong validators hash fails
block = makeBlock(state, 1)
block.ValidatorsHash = []byte("wrong validators hash")
err = state.ValidateBlock(block)
require.Error(t, err)
} }
func TestApplyBlock(t *testing.T) { func TestApplyBlock(t *testing.T) {


Loading…
Cancel
Save