Skip to content

Commit

Permalink
Merge pull request #257 from kcalvinalvin/2025-02-06-add-fetch-msg-ut…
Browse files Browse the repository at this point in the history
…reexo-root

indexers: add FetchMsgUtreexoRoot
  • Loading branch information
kcalvinalvin authored Feb 6, 2025
2 parents c3a245a + 629a4dd commit eff027b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions blockchain/indexers/flatutreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,40 @@ func (idx *FlatUtreexoProofIndex) updateRootsState() error {
return idx.utreexoRootsState.Modify([]utreexo.Leaf{{Hash: rootHash}}, nil, utreexo.Proof{})
}

// FetchMsgUtreexoRoot returns a complete utreexoroot bitcoin message on the requested block.
func (idx *FlatUtreexoProofIndex) FetchMsgUtreexoRoot(blockHash *chainhash.Hash) (*wire.MsgUtreexoRoot, error) {
height, err := idx.chain.BlockHeightByHash(blockHash)
if err != nil {
return nil, err
}

stump, err := idx.fetchRoots(height)
if err != nil {
return nil, err
}

bytes, err := blockchain.SerializeUtreexoRoots(stump.NumLeaves, stump.Roots)
if err != nil {
return nil, err
}
rootHash := sha256.Sum256(bytes)

proof, err := idx.utreexoRootsState.Prove([]utreexo.Hash{rootHash})
if err != nil {
return nil, err
}

msg := &wire.MsgUtreexoRoot{
NumLeaves: stump.NumLeaves,
Target: proof.Targets[0],
BlockHash: *blockHash,
Roots: stump.Roots,
Proof: proof.Proof,
}

return msg, nil
}

// flatFilePath returns the path to the flatfile.
func flatFilePath(dataDir, dataName string) string {
flatFileName := dataName + "_" + flatFileNameSuffix
Expand Down

0 comments on commit eff027b

Please sign in to comment.