@@ -3,17 +3,21 @@ package blockchain
33import (
44 "bytes"
55 "fmt"
6+ "strings"
67
78 "github.com/pkg/errors"
89
910 "github.com/btcsuite/btcd/txscript"
1011 "github.com/btcsuite/btcd/wire"
1112 "github.com/btcsuite/btcutil"
1213
14+ "github.com/btcsuite/btcd/chaincfg/chainhash"
1315 "github.com/btcsuite/btcd/claimtrie"
1416 "github.com/btcsuite/btcd/claimtrie/change"
17+ "github.com/btcsuite/btcd/claimtrie/merkletrie"
1518 "github.com/btcsuite/btcd/claimtrie/node"
1619 "github.com/btcsuite/btcd/claimtrie/normalization"
20+ "github.com/btcsuite/btcd/claimtrie/param"
1721)
1822
1923func (b * BlockChain ) SetClaimtrieHeader (block * btcutil.Block , view * UtxoViewpoint ) error {
@@ -182,3 +186,68 @@ func (b *BlockChain) GetClaimsForName(height int32, name string) (string, *node.
182186 n .SortClaimsByBid ()
183187 return string (normalizedName ), n , nil
184188}
189+
190+ func (b * BlockChain ) GetProofForName (name , id string , bid , seq int ) (chainhash.Hash , int32 , * node.Claim , int32 , int32 , string , []merkletrie.HashSidePair , error ) {
191+ // results: block hash, height, claim, bid, takeover, name, pairs, err
192+
193+ b .chainLock .RLock ()
194+ defer b .chainLock .RUnlock ()
195+
196+ tip := b .bestChain .Tip ()
197+
198+ normalizedName := normalization .NormalizeIfNecessary ([]byte (name ), tip .height )
199+
200+ if tip .height < param .ActiveParams .GrandForkHeight {
201+ err := errors .Errorf ("Unable to generate proofs for claims before height %d" ,
202+ param .ActiveParams .GrandForkHeight )
203+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
204+ }
205+
206+ n , err := b .claimTrie .NodeAt (tip .height , normalizedName )
207+ if n == nil && err == nil {
208+ err = errors .Errorf ("Unable to locate a claim with name %s at height %d" , normalizedName , tip .height )
209+ }
210+ if err != nil {
211+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
212+ }
213+
214+ // now find the desired claim
215+ n .SortClaimsByBid ()
216+ var claim * node.Claim
217+ for i , c := range n .Claims {
218+ if c .Status != node .Activated {
219+ continue
220+ }
221+ if bid >= 0 && i == bid {
222+ claim = c
223+ bid = i
224+ break
225+ }
226+ if seq >= 0 && int (c .Sequence ) == seq {
227+ claim = c
228+ bid = i
229+ break
230+ }
231+ if len (id ) > 0 && strings .HasPrefix (c .ClaimID .String (), id ) {
232+ claim = c
233+ bid = i
234+ break
235+ }
236+ }
237+ if claim == nil {
238+ if bid >= 0 {
239+ err = errors .Errorf ("Unable to locate a claim named %s with bid %d at height %d" , normalizedName , bid , tip .height )
240+ }
241+ if seq >= 0 {
242+ err = errors .Errorf ("Unable to locate a claim named %s with sequence %d at height %d" , normalizedName , seq , tip .height )
243+ }
244+ if len (id ) > 0 {
245+ err = errors .Errorf ("Unable to locate a claim named %s with ID %s at height %d" , normalizedName , id , tip .height )
246+ }
247+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
248+ }
249+
250+ pairs := b .claimTrie .MerklePath (normalizedName , n , bid )
251+
252+ return tip .hash , tip .height , claim , int32 (bid ), n .TakenOverAt , string (normalizedName ), pairs , nil
253+ }
0 commit comments