trie: change the Value.Hash() to return pointer.

When nil is returned by Hash(), the merkle generation will treat it as a
nil node, and ignore the hash along the way.
This commit is contained in:
Tzu-Jung Lee 2018-07-16 23:20:11 -07:00
parent 374a75fea6
commit f4a5c5ee8d
7 changed files with 16 additions and 12 deletions

View file

@ -9,7 +9,7 @@ import (
"github.com/btcsuite/btcd/wire"
)
func calNodeHash(op wire.OutPoint, tookover Height) chainhash.Hash {
func calNodeHash(op wire.OutPoint, tookover Height) *chainhash.Hash {
txHash := chainhash.DoubleHashH(op.Hash[:])
nOut := []byte(strconv.Itoa(int(op.Index)))
@ -24,5 +24,6 @@ func calNodeHash(op wire.OutPoint, tookover Height) chainhash.Hash {
h = append(h, nOutHash[:]...)
h = append(h, heightHash[:]...)
return chainhash.DoubleHashH(h)
hh := chainhash.DoubleHashH(h)
return &hh
}

View file

@ -165,9 +165,9 @@ func (n *Node) FindNextUpdateHeight() Height {
}
// Hash calculates the Hash value based on the OutPoint and at which height it tookover.
func (n *Node) Hash() chainhash.Hash {
func (n *Node) Hash() *chainhash.Hash {
if n.BestClaim() == nil {
return chainhash.Hash{}
return nil
}
return calNodeHash(n.BestClaim().OutPoint, n.Tookover())
}

View file

@ -223,7 +223,7 @@ func (a *args) all() bool {
var showNode = func(showJSON bool) trie.Visit {
return func(prefix trie.Key, val trie.Value) error {
if val == nil {
if val == nil || val.Hash() == nil {
fmt.Printf("%-8s:\n", prefix)
return nil
}

View file

@ -103,8 +103,9 @@ func main() {
type strValue string
func (s strValue) Hash() chainhash.Hash {
return chainhash.DoubleHashH([]byte(s))
func (s strValue) Hash() *chainhash.Hash {
h := chainhash.DoubleHashH([]byte(s))
return &h
}
var (

View file

@ -84,8 +84,9 @@ func merkle(n *node) *chainhash.Hash {
}
}
if n.value != nil {
h := n.value.Hash()
buf = append(buf, h[:]...)
if h := n.value.Hash(); h != nil {
buf = append(buf, h[:]...)
}
}
if len(buf) != 0 {

View file

@ -10,8 +10,9 @@ import (
type strValue string
func (s strValue) Hash() chainhash.Hash {
return chainhash.DoubleHashH([]byte(s))
func (s strValue) Hash() *chainhash.Hash {
h := chainhash.DoubleHashH([]byte(s))
return &h
}
func dump(prefix Key, value Value) error {

View file

@ -16,7 +16,7 @@ type Key []byte
// Value implements value for the MerkleTrie.
type Value interface {
Hash() chainhash.Hash
Hash() *chainhash.Hash
}
// MerkleTrie implements a 256-way prefix tree, which takes Key as key and any value that implements the Value interface.