@@ -3,16 +3,20 @@ 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"
19+ "github.com/btcsuite/btcd/claimtrie/param"
1620)
1721
1822func (b * BlockChain ) ParseClaimScripts (block * btcutil.Block , bn * blockNode , view * UtxoViewpoint ,
@@ -166,3 +170,68 @@ func (b *BlockChain) GetClaimsForName(height int32, name string) (string, *node.
166170 n .SortClaimsByBid ()
167171 return string (normalizedName ), n , nil
168172}
173+
174+ func (b * BlockChain ) GetProofForName (name , id string , bid , seq int ) (chainhash.Hash , int32 , * node.Claim , int32 , int32 , string , []merkletrie.HashSidePair , error ) {
175+ // results: block hash, height, claim, bid, takeover, name, pairs, err
176+
177+ b .chainLock .RLock ()
178+ defer b .chainLock .RUnlock ()
179+
180+ tip := b .bestChain .Tip ()
181+
182+ normalizedName := node .NormalizeIfNecessary ([]byte (name ), tip .height )
183+
184+ if tip .height < param .ActiveParams .GrandForkHeight {
185+ err := errors .Errorf ("Unable to generate proofs for claims before height %d" ,
186+ param .ActiveParams .GrandForkHeight )
187+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
188+ }
189+
190+ n , err := b .claimTrie .NodeAt (tip .height , normalizedName )
191+ if n == nil && err == nil {
192+ err = errors .Errorf ("Unable to locate a claim with name %s at height %d" , normalizedName , tip .height )
193+ }
194+ if err != nil {
195+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
196+ }
197+
198+ // now find the desired claim
199+ n .SortClaimsByBid ()
200+ var claim * node.Claim
201+ for i , c := range n .Claims {
202+ if c .Status != node .Activated {
203+ continue
204+ }
205+ if bid >= 0 && i == bid {
206+ claim = c
207+ bid = i
208+ break
209+ }
210+ if seq >= 0 && int (c .Sequence ) == seq {
211+ claim = c
212+ bid = i
213+ break
214+ }
215+ if len (id ) > 0 && strings .HasPrefix (c .ClaimID .String (), id ) {
216+ claim = c
217+ bid = i
218+ break
219+ }
220+ }
221+ if claim == nil {
222+ if bid >= 0 {
223+ err = errors .Errorf ("Unable to locate a claim named %s with bid %d at height %d" , normalizedName , bid , tip .height )
224+ }
225+ if seq >= 0 {
226+ err = errors .Errorf ("Unable to locate a claim named %s with sequence %d at height %d" , normalizedName , seq , tip .height )
227+ }
228+ if len (id ) > 0 {
229+ err = errors .Errorf ("Unable to locate a claim named %s with ID %s at height %d" , normalizedName , id , tip .height )
230+ }
231+ return tip .hash , tip .height , nil , 0 , 0 , string (normalizedName ), nil , err
232+ }
233+
234+ pairs := b .claimTrie .MerklePath (normalizedName , n , bid )
235+
236+ return tip .hash , tip .height , claim , int32 (bid ), n .TakenOverAt , string (normalizedName ), pairs , nil
237+ }
0 commit comments