Browse Source

Compare blockhashes in stageBlock()

pull/43/merge
Jae Kwon 9 years ago
parent
commit
fd0646fc4f
4 changed files with 22 additions and 7 deletions
  1. +2
    -1
      consensus/state.go
  2. +1
    -0
      state/state.go
  3. +18
    -5
      types/block.go
  4. +1
    -1
      vm/test/fake_app_state.go

+ 2
- 1
consensus/state.go View File

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


+ 1
- 0
state/state.go View File

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


+ 18
- 5
types/block.go View File

@ -55,16 +55,24 @@ func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
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 {
if b.Header == nil || b.Validation == nil || b.Data == nil {
return nil
}
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
hashHeader := b.Header.Hash()
hashValidation := b.Validation.Hash()
hashData := 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)
}
@ -125,7 +133,12 @@ type Header struct {
StateHash []byte
}
// NOTE: hash is nil if required fields are missing.
func (h *Header) Hash() []byte {
if len(h.StateHash) == 0 {
return nil
}
buf := new(bytes.Buffer)
hasher, n, err := sha256.New(), new(int64), new(error)
binary.WriteBinary(h, buf, n, err)


+ 1
- 1
vm/test/fake_app_state.go View File

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


Loading…
Cancel
Save