Browse Source

don't cache the block/header hashes. fixes a cache invalidation bug

pull/39/head
Jae Kwon 10 years ago
parent
commit
6c48642ff9
2 changed files with 23 additions and 30 deletions
  1. +18
    -29
      block/block.go
  2. +5
    -1
      consensus/state.go

+ 18
- 29
block/block.go View File

@ -19,9 +19,6 @@ type Block struct {
*Header *Header
*Validation *Validation
*Data *Data
// Volatile
hash []byte
} }
// Basic validation that doesn't involve state data. // Basic validation that doesn't involve state data.
@ -57,16 +54,13 @@ func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
} }
func (b *Block) Hash() []byte { func (b *Block) Hash() []byte {
if b.hash == nil {
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
}
// Merkle hash from sub-hashes.
b.hash = merkle.HashFromHashes(hashes)
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
b.Data.Hash(),
} }
return b.hash
// Merkle hash from sub-hashes.
return merkle.HashFromHashes(hashes)
} }
// Convenience. // Convenience.
@ -95,7 +89,7 @@ func (b *Block) StringIndented(indent string) string {
indent, b.Header.StringIndented(indent+" "), indent, b.Header.StringIndented(indent+" "),
indent, b.Validation.StringIndented(indent+" "), indent, b.Validation.StringIndented(indent+" "),
indent, b.Data.StringIndented(indent+" "), indent, b.Data.StringIndented(indent+" "),
indent, b.hash)
indent, b.Hash())
} }
func (b *Block) StringShort() string { func (b *Block) StringShort() string {
@ -117,25 +111,20 @@ type Header struct {
LastBlockHash []byte LastBlockHash []byte
LastBlockParts PartSetHeader LastBlockParts PartSetHeader
StateHash []byte StateHash []byte
// Volatile
hash []byte
} }
func (h *Header) Hash() []byte { func (h *Header) Hash() []byte {
if h.hash == nil {
buf := new(bytes.Buffer)
hasher, n, err := sha256.New(), new(int64), new(error)
binary.WriteBinary(h, buf, n, err)
if *err != nil {
panic(err)
}
log.Debug("Hashing", "bytes", buf.Bytes())
hasher.Write(buf.Bytes())
h.hash = hasher.Sum(nil)
log.Debug("Hashing got", "hash", h.hash)
buf := new(bytes.Buffer)
hasher, n, err := sha256.New(), new(int64), new(error)
binary.WriteBinary(h, buf, n, err)
if *err != nil {
panic(err)
} }
return h.hash
log.Debug("Hashing", "bytes", buf.Bytes())
hasher.Write(buf.Bytes())
hash := hasher.Sum(nil)
log.Debug("Hashing got", "hash", hash)
return hash
} }
func (h *Header) StringIndented(indent string) string { func (h *Header) StringIndented(indent string) string {
@ -157,7 +146,7 @@ func (h *Header) StringIndented(indent string) string {
indent, h.LastBlockHash, indent, h.LastBlockHash,
indent, h.LastBlockParts, indent, h.LastBlockParts,
indent, h.StateHash, indent, h.StateHash,
indent, h.hash)
indent, h.Hash())
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------


+ 5
- 1
consensus/state.go View File

@ -635,7 +635,11 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
} }
// Set the blk.Header.StateHash. // Set the blk.Header.StateHash.
cs.state.SetBlockStateHash(block)
err := cs.state.SetBlockStateHash(block)
if err != nil {
log.Error("Error setting state hash", "error", err)
return
}
blockParts = blk.NewPartSetFromData(binary.BinaryBytes(block)) blockParts = blk.NewPartSetFromData(binary.BinaryBytes(block))
pol = cs.LockedPOL // If exists, is a PoUnlock. pol = cs.LockedPOL // If exists, is a PoUnlock.


Loading…
Cancel
Save