@@ -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 {
@@ -181,3 +185,68 @@ func (b *BlockChain) GetClaimsForName(height int32, name string) (string, *node.
181185 n .SortClaimsByBid ()
182186 return string (normalizedName ), n , nil
183187}
188+
189+ func (b * BlockChain ) GetProofForName (name , id string , bid , seq int ) (chainhash.Hash , int32 , * node.Claim , int32 , int32 , string , []merkletrie.HashSidePair , error ) {
190+ // results: block hash, height, claim, bid, takeover, name, pairs, err
191+
192+ b .chainLock .RLock ()
193+ defer b .chainLock .RUnlock ()
194+
195+ tip := b .bestChain .Tip ()
196+
197+ normalizedName := normalization .NormalizeIfNecessary ([]byte (name ), tip .height )
198+
199+ if tip .height < param .ActiveParams .GrandForkHeight {
200+ err := errors .Errorf ("Unable to generate proofs for claims before height %d" ,
201+ param .ActiveParams .GrandForkHeight )
202+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
203+ }
204+
205+ n , err := b .claimTrie .NodeAt (tip .height , normalizedName )
206+ if n == nil && err == nil {
207+ err = errors .Errorf ("Unable to locate a claim with name %s at height %d" , normalizedName , tip .height )
208+ }
209+ if err != nil {
210+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
211+ }
212+
213+ // now find the desired claim
214+ n .SortClaimsByBid ()
215+ var claim * node.Claim
216+ for i , c := range n .Claims {
217+ if c .Status != node .Activated {
218+ continue
219+ }
220+ if bid >= 0 && i == bid {
221+ claim = c
222+ bid = i
223+ break
224+ }
225+ if seq >= 0 && int (c .Sequence ) == seq {
226+ claim = c
227+ bid = i
228+ break
229+ }
230+ if len (id ) > 0 && strings .HasPrefix (c .ClaimID .String (), id ) {
231+ claim = c
232+ bid = i
233+ break
234+ }
235+ }
236+ if claim == nil {
237+ if bid >= 0 {
238+ err = errors .Errorf ("Unable to locate a claim named %s with bid %d at height %d" , normalizedName , bid , tip .height )
239+ }
240+ if seq >= 0 {
241+ err = errors .Errorf ("Unable to locate a claim named %s with sequence %d at height %d" , normalizedName , seq , tip .height )
242+ }
243+ if len (id ) > 0 {
244+ err = errors .Errorf ("Unable to locate a claim named %s with ID %s at height %d" , normalizedName , id , tip .height )
245+ }
246+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
247+ }
248+
249+ pairs := b .claimTrie .MerklePath (normalizedName , n , bid )
250+
251+ return tip .hash , tip .height , claim , int32 (bid ), n .TakenOverAt , string (normalizedName ), pairs , nil
252+ }
0 commit comments