Skip to content

Commit ea7d936

Browse files
committed
new claim hash function includes bid, seq, name
1 parent dd7391c commit ea7d936

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

claimtrie/claimtrie.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ func (ct *ClaimTrie) AppendBlock() error {
276276
}
277277

278278
func (ct *ClaimTrie) updateTrieForHashForkIfNecessary() bool {
279-
if ct.height != param.ActiveParams.AllClaimsInMerkleForkHeight {
279+
if ct.height != param.ActiveParams.AllClaimsInMerkleForkHeight &&
280+
ct.height != param.ActiveParams.GrandForkHeight {
280281
return false
281282
}
282283

claimtrie/node/hash_manager.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package node
22

33
import (
4+
"crypto/sha256"
5+
"encoding/binary"
6+
47
"github.com/btcsuite/btcd/chaincfg/chainhash"
5-
"github.com/lbryio/chain/claimtrie/change"
6-
"github.com/lbryio/chain/claimtrie/param"
8+
"github.com/btcsuite/btcd/claimtrie/change"
9+
"github.com/btcsuite/btcd/claimtrie/param"
710
)
811

912
type HashV2Manager struct {
@@ -50,14 +53,59 @@ func (nm *HashV3Manager) AppendChange(chg change.Change) {
5053
nm.Manager.AppendChange(chg)
5154
}
5255

56+
func calculateBidSeqNameHash(name []byte, c *Claim, bid, takeover int32) (*chainhash.Hash, error) {
57+
58+
s := sha256.New()
59+
60+
s.Write(c.OutPoint.Hash[:])
61+
62+
var temp [4]byte
63+
binary.BigEndian.PutUint32(temp[:], c.OutPoint.Index)
64+
s.Write(temp[:])
65+
66+
binary.BigEndian.PutUint32(temp[:], uint32(bid))
67+
s.Write(temp[:])
68+
69+
binary.BigEndian.PutUint32(temp[:], uint32(c.Sequence))
70+
s.Write(temp[:])
71+
72+
binary.BigEndian.PutUint32(temp[:], uint32(takeover))
73+
s.Write(temp[:])
74+
75+
s.Write(name)
76+
77+
var m [sha256.Size]byte
78+
return chainhash.NewHash(s.Sum(m[:0]))
79+
}
80+
81+
func (nm *HashV3Manager) bidSeqNameHash(name []byte) (*chainhash.Hash, int32) {
82+
n, err := nm.NodeAt(nm.Height(), name)
83+
if err != nil || n == nil {
84+
return nil, 0
85+
}
86+
87+
n.SortClaimsByBid()
88+
claimHashes := make([]*chainhash.Hash, 0, len(n.Claims))
89+
for i, c := range n.Claims {
90+
if c.Status == Activated {
91+
h, _ := calculateBidSeqNameHash(name, c, int32(i), n.TakenOverAt)
92+
claimHashes = append(claimHashes, h)
93+
}
94+
}
95+
if len(claimHashes) > 0 {
96+
return ComputeMerkleRoot(claimHashes), n.NextUpdate(nm.Height())
97+
}
98+
return nil, n.NextUpdate(nm.Height())
99+
}
100+
53101
func (nm *HashV3Manager) Hash(name []byte) (*chainhash.Hash, int32) {
54102

55103
if nm.Height() >= param.ActiveParams.GrandForkHeight {
56104
if len(name) == 0 {
57105
return nil, 0 // empty name's claims are not included in the hash
58106
}
59-
// return nm.detailHash()
107+
return nm.bidSeqNameHash(name)
60108
}
61109

62110
return nm.Manager.Hash(name)
63-
}
111+
}

0 commit comments

Comments
 (0)