From 6c48642ff9cd7f422c7a85bcdaac9e12da835a1e Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Mon, 19 Jan 2015 14:08:53 -0800 Subject: [PATCH] don't cache the block/header hashes. fixes a cache invalidation bug --- block/block.go | 47 ++++++++++++++++++---------------------------- consensus/state.go | 6 +++++- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/block/block.go b/block/block.go index 30ef0b5bf..79f99ef68 100644 --- a/block/block.go +++ b/block/block.go @@ -19,9 +19,6 @@ type Block struct { *Header *Validation *Data - - // Volatile - hash []byte } // 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 { - 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. @@ -95,7 +89,7 @@ func (b *Block) StringIndented(indent string) string { indent, b.Header.StringIndented(indent+" "), indent, b.Validation.StringIndented(indent+" "), indent, b.Data.StringIndented(indent+" "), - indent, b.hash) + indent, b.Hash()) } func (b *Block) StringShort() string { @@ -117,25 +111,20 @@ type Header struct { LastBlockHash []byte LastBlockParts PartSetHeader StateHash []byte - - // Volatile - 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 { @@ -157,7 +146,7 @@ func (h *Header) StringIndented(indent string) string { indent, h.LastBlockHash, indent, h.LastBlockParts, indent, h.StateHash, - indent, h.hash) + indent, h.Hash()) } //----------------------------------------------------------------------------- diff --git a/consensus/state.go b/consensus/state.go index b7307407e..2d992918e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -635,7 +635,11 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) { } // 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)) pol = cs.LockedPOL // If exists, is a PoUnlock.