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" "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[:]) txHash := chainhash.DoubleHashH(op.Hash[:])
nOut := []byte(strconv.Itoa(int(op.Index))) 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, nOutHash[:]...)
h = append(h, heightHash[:]...) 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. // 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 { if n.BestClaim() == nil {
return chainhash.Hash{} return nil
} }
return calNodeHash(n.BestClaim().OutPoint, n.Tookover()) 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 { var showNode = func(showJSON bool) trie.Visit {
return func(prefix trie.Key, val trie.Value) error { return func(prefix trie.Key, val trie.Value) error {
if val == nil { if val == nil || val.Hash() == nil {
fmt.Printf("%-8s:\n", prefix) fmt.Printf("%-8s:\n", prefix)
return nil return nil
} }

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ type Key []byte
// Value implements value for the MerkleTrie. // Value implements value for the MerkleTrie.
type Value interface { 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. // MerkleTrie implements a 256-way prefix tree, which takes Key as key and any value that implements the Value interface.