Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peer, main: handle getutreexoproof message #233

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ type MessageListeners struct {
// OnUtreexoHeader is invoked when a peer receives a headers bitcoin message.
OnUtreexoHeader func(p *Peer, msg *wire.MsgUtreexoHeader)

// OnGetUtreexoProof is invoked when a peer receives a utreexo proof bitcoin message.
OnGetUtreexoProof func(p *Peer, msg *wire.MsgGetUtreexoProof)

// OnNotFound is invoked when a peer receives a notfound bitcoin
// message.
OnNotFound func(p *Peer, msg *wire.MsgNotFound)
Expand Down Expand Up @@ -1480,6 +1483,11 @@ out:
p.cfg.Listeners.OnUtreexoHeader(p, msg)
}

case *wire.MsgGetUtreexoProof:
if p.cfg.Listeners.OnGetUtreexoProof != nil {
p.cfg.Listeners.OnGetUtreexoProof(p, msg)
}

case *wire.MsgNotFound:
if p.cfg.Listeners.OnNotFound != nil {
p.cfg.Listeners.OnNotFound(p, msg)
Expand Down
90 changes: 90 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,95 @@ func (sp *serverPeer) OnGetCFCheckpt(_ *peer.Peer, msg *wire.MsgGetCFCheckpt) {
sp.QueueMessage(checkptMsg, nil)
}

// OnGetUtreexoProof is invoked when a peer receives a getutreexoproof bitcoin message.
func (sp *serverPeer) OnGetUtreexoProof(_ *peer.Peer, msg *wire.MsgGetUtreexoProof) {
// Ignore getutreexoproof requests if not in sync.
if !sp.server.syncManager.IsCurrent() {
return
}

// Check if we're a utreexo node. Ignore if we're not.
if sp.server.utreexoProofIndex == nil && sp.server.flatUtreexoProofIndex == nil && cfg.NoUtreexo {
return
}

height, err := sp.server.chain.BlockHeightByHash(&msg.BlockHash)
if err != nil {
chanLog.Debugf("Unable to fetch height for block hash %v: %v",
msg.BlockHash, err)
return
}

// If we're pruned and the requested block is beyond the point where pruned blocks
// are able to serve blocks, just ignore the message.
if cfg.Prune != 0 && height >= sp.server.chain.BestSnapshot().Height-288 {
return
}

block, err := sp.server.chain.BlockByHash(&msg.BlockHash)
if err != nil {
chanLog.Debugf("Unable to fetch block with hash %v: %v",
msg.BlockHash, err)
return
}

// Fetch UData.
var udata *wire.UData
if !cfg.NoUtreexo {
udata = block.MsgBlock().UData
}
if sp.server.utreexoProofIndex != nil {
udata, err = sp.server.utreexoProofIndex.FetchUtreexoProof(&msg.BlockHash)
if err != nil {
chanLog.Debugf("Unable to fetch utreexo proof for block hash %v: %v",
msg.BlockHash, err)
return
}
}
if sp.server.flatUtreexoProofIndex != nil {
udata, err = sp.server.flatUtreexoProofIndex.FetchUtreexoProof(height)
if err != nil {
chanLog.Debugf("Unable to fetch utreexo proof for block hash %v: %v",
msg.BlockHash, err)
return
}
}

_, err = sp.server.chain.ReconstructUData(udata, msg.BlockHash)
if err != nil {
chanLog.Debugf("Unable to fetch utreexo proof for block hash %v: %v",
msg.BlockHash, err)
return
}

// Construct utreexo proof to send.
leafDatas := make([]wire.LeafData, 0, len(msg.LeafIndexes))
for _, idx := range msg.LeafIndexes {
if int(idx) > len(udata.LeafDatas) {
chanLog.Debugf("Unable to fetch utreexo proof for block hash %v: %v",
msg.BlockHash, err)
return
}
leafDatas = append(leafDatas, udata.LeafDatas[idx])
}
proofHashes := make([]utreexo.Hash, 0, len(msg.ProofIndexes))
for _, idx := range msg.ProofIndexes {
if int(idx) >= len(udata.AccProof.Proof) {
chanLog.Debugf("Unable to fetch utreexo proof for block hash %v: %v",
msg.BlockHash, err)
return
}
proofHashes = append(proofHashes, udata.AccProof.Proof[idx])
}
utreexoProof := wire.MsgUtreexoProof{
BlockHash: msg.BlockHash,
ProofHashes: proofHashes,
LeafDatas: leafDatas,
}

sp.QueueMessage(&utreexoProof, nil)
}

// enforceNodeBloomFlag disconnects the peer if the server is not configured to
// allow bloom filters. Additionally, if the peer has negotiated to a protocol
// version that is high enough to observe the bloom filter service support bit,
Expand Down Expand Up @@ -2547,6 +2636,7 @@ func newPeerConfig(sp *serverPeer) *peer.Config {
OnInv: sp.OnInv,
OnHeaders: sp.OnHeaders,
OnUtreexoHeader: sp.OnUtreexoHeader,
OnGetUtreexoProof: sp.OnGetUtreexoProof,
OnGetData: sp.OnGetData,
OnGetBlocks: sp.OnGetBlocks,
OnGetHeaders: sp.OnGetHeaders,
Expand Down
Loading