|
1 | 1 | package node |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/btcsuite/btcd/chaincfg/chainhash" |
5 | | - "github.com/lbryio/chain/claimtrie/change" |
6 | | - "github.com/lbryio/chain/claimtrie/param" |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/binary" |
| 6 | + |
| 7 | + "github.com/lbryio/lbcd/chaincfg/chainhash" |
| 8 | + "github.com/lbryio/lbcd/claimtrie/change" |
| 9 | + "github.com/lbryio/lbcd/claimtrie/param" |
7 | 10 | ) |
8 | 11 |
|
9 | 12 | type HashV2Manager struct { |
@@ -50,13 +53,58 @@ func (nm *HashV3Manager) AppendChange(chg change.Change) { |
50 | 53 | nm.Manager.AppendChange(chg) |
51 | 54 | } |
52 | 55 |
|
| 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 | + |
53 | 101 | func (nm *HashV3Manager) Hash(name []byte) (*chainhash.Hash, int32) { |
54 | 102 |
|
55 | 103 | if nm.Height() >= param.ActiveParams.GrandForkHeight { |
56 | 104 | if len(name) == 0 { |
57 | 105 | return nil, 0 // empty name's claims are not included in the hash |
58 | 106 | } |
59 | | - // return nm.detailHash() |
| 107 | + return nm.bidSeqNameHash(name) |
60 | 108 | } |
61 | 109 |
|
62 | 110 | return nm.Manager.Hash(name) |
|
0 commit comments