Compare blockhashes in stageBlock()

This commit is contained in:
Jae Kwon 2015-03-26 10:58:20 -07:00
parent bd767c1fab
commit fd0646fc4f
4 changed files with 22 additions and 7 deletions

View File

@ -1015,7 +1015,8 @@ func (cs *ConsensusState) stageBlock(block *types.Block, blockParts *types.PartS
} }
// Already staged? // Already staged?
if cs.stagedBlock == block { blockHash := block.Hash()
if cs.stagedBlock != nil && len(blockHash) != 0 && bytes.Equal(cs.stagedBlock.Hash(), blockHash) {
return nil return nil
} }

View File

@ -633,6 +633,7 @@ func (s *State) AppendBlock(block *types.Block, blockPartsHeader types.PartSetHe
return nil return nil
} }
// Mutates the block in place and updates it with new state hash.
func (s *State) SetBlockStateHash(block *types.Block) error { func (s *State) SetBlockStateHash(block *types.Block) error {
sCopy := s.Copy() sCopy := s.Copy()
err := sCopy.appendBlock(block, types.PartSetHeader{}) err := sCopy.appendBlock(block, types.PartSetHeader{})

View File

@ -55,16 +55,24 @@ func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
return nil return nil
} }
// Computes and returns the block hash.
// If the block is incomplete (e.g. missing Header.StateHash)
// then the hash is nil, to prevent the usage of that hash.
func (b *Block) Hash() []byte { func (b *Block) Hash() []byte {
if b.Header == nil || b.Validation == nil || b.Data == nil { if b.Header == nil || b.Validation == nil || b.Data == nil {
return nil return nil
} }
hashes := [][]byte{ hashHeader := b.Header.Hash()
b.Header.Hash(), hashValidation := b.Validation.Hash()
b.Validation.Hash(), hashData := b.Data.Hash()
b.Data.Hash(),
// If hashHeader is nil, required fields are missing.
if len(hashHeader) == 0 {
return nil
} }
// Merkle hash from sub-hashes.
// Merkle hash from subhashes.
hashes := [][]byte{hashHeader, hashValidation, hashData}
return merkle.HashFromHashes(hashes) return merkle.HashFromHashes(hashes)
} }
@ -125,7 +133,12 @@ type Header struct {
StateHash []byte StateHash []byte
} }
// NOTE: hash is nil if required fields are missing.
func (h *Header) Hash() []byte { func (h *Header) Hash() []byte {
if len(h.StateHash) == 0 {
return nil
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := sha256.New(), new(int64), new(error)
binary.WriteBinary(h, buf, n, err) binary.WriteBinary(h, buf, n, err)

View File

@ -1,4 +1,4 @@
package main package vm
import ( import (
"fmt" "fmt"