|
@ -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()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|