From 32f3e68ea4f098fc8b68d3609ef067883df8a84b Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Thu, 21 Dec 2023 14:50:15 +0900 Subject: [PATCH] blockchain: add GenerateUDataPartial GenerateUDataPartial allows for the generation of partial udata to serve to other utreexo nodes. --- blockchain/utreexoviewpoint.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/blockchain/utreexoviewpoint.go b/blockchain/utreexoviewpoint.go index 6e7abc69..e083bf40 100644 --- a/blockchain/utreexoviewpoint.go +++ b/blockchain/utreexoviewpoint.go @@ -928,6 +928,39 @@ func (b *BlockChain) VerifyUData(ud *wire.UData, txIns []*wire.TxIn, remember bo return nil } +// GenerateUDataPartial generates a utreexo data based on the current state of the utreexo viewpoint. +// It leaves out the full proof hashes and only fetches the requested positions. +// +// This function is safe for concurrent access. +func (b *BlockChain) GenerateUDataPartial(dels []wire.LeafData, positions []uint64) (*wire.UData, error) { + b.chainLock.RLock() + defer b.chainLock.RUnlock() + + ud := new(wire.UData) + ud.LeafDatas = dels + + // Get the positions of the targets of delHashes. + delHashes, err := wire.HashesFromLeafDatas(ud.LeafDatas) + if err != nil { + return nil, err + } + targets := b.getLeafHashPositions(delHashes) + + // Fetch the requested hashes. + hashes := make([]utreexo.Hash, len(positions)) + for i, pos := range positions { + hashes[i] = b.utreexoView.accumulator.GetHash(pos) + } + + // Put the proof together and return. + ud.AccProof = utreexo.Proof{ + Targets: targets, + Proof: hashes, + } + + return ud, nil +} + // GenerateUData generates a utreexo data based on the current state of the utreexo viewpoint. // // This function is safe for concurrent access.