new claim hash function includes bid, seq, name

This commit is contained in:
Brannon King 2021-08-12 13:47:22 -04:00
parent 32e001c446
commit 494a39c1ab
2 changed files with 54 additions and 5 deletions

View file

@ -280,7 +280,8 @@ func (ct *ClaimTrie) AppendBlock() error {
}
func (ct *ClaimTrie) updateTrieForHashForkIfNecessary() bool {
if ct.height != param.ActiveParams.AllClaimsInMerkleForkHeight {
if ct.height != param.ActiveParams.AllClaimsInMerkleForkHeight &&
ct.height != param.ActiveParams.GrandForkHeight {
return false
}

View file

@ -1,9 +1,12 @@
package node
import (
"crypto/sha256"
"encoding/binary"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lbryio/chain/claimtrie/change"
"github.com/lbryio/chain/claimtrie/param"
"github.com/btcsuite/btcd/claimtrie/change"
"github.com/btcsuite/btcd/claimtrie/param"
)
type HashV2Manager struct {
@ -50,13 +53,58 @@ func (nm *HashV3Manager) AppendChange(chg change.Change) {
nm.Manager.AppendChange(chg)
}
func calculateBidSeqNameHash(name []byte, c *Claim, bid, takeover int32) (*chainhash.Hash, error) {
s := sha256.New()
s.Write(c.OutPoint.Hash[:])
var temp [4]byte
binary.BigEndian.PutUint32(temp[:], c.OutPoint.Index)
s.Write(temp[:])
binary.BigEndian.PutUint32(temp[:], uint32(bid))
s.Write(temp[:])
binary.BigEndian.PutUint32(temp[:], uint32(c.Sequence))
s.Write(temp[:])
binary.BigEndian.PutUint32(temp[:], uint32(takeover))
s.Write(temp[:])
s.Write(name)
var m [sha256.Size]byte
return chainhash.NewHash(s.Sum(m[:0]))
}
func (nm *HashV3Manager) bidSeqNameHash(name []byte) (*chainhash.Hash, int32) {
n, err := nm.NodeAt(nm.Height(), name)
if err != nil || n == nil {
return nil, 0
}
n.SortClaimsByBid()
claimHashes := make([]*chainhash.Hash, 0, len(n.Claims))
for i, c := range n.Claims {
if c.Status == Activated {
h, _ := calculateBidSeqNameHash(name, c, int32(i), n.TakenOverAt)
claimHashes = append(claimHashes, h)
}
}
if len(claimHashes) > 0 {
return ComputeMerkleRoot(claimHashes), n.NextUpdate(nm.Height())
}
return nil, n.NextUpdate(nm.Height())
}
func (nm *HashV3Manager) Hash(name []byte) (*chainhash.Hash, int32) {
if nm.Height() >= param.ActiveParams.GrandForkHeight {
if len(name) == 0 {
return nil, 0 // empty name's claims are not included in the hash
}
// return nm.detailHash()
return nm.bidSeqNameHash(name)
}
return nm.Manager.Hash(name)