8000 netsync, peer, server: add fetch utreexo ttls by kcalvinalvin · Pull Request #317 · utreexo/utreexod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

netsync, peer, server: add fetch utreexo ttls #317

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

Merged
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
70 changes: 70 additions & 0 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,11 @@ func (sm *SyncManager) handleBlockMsg(bmsg *blockMsg) {
return
}

// Remove the no longer needed ttl.
//
// TODO: actually make use of the ttl instead of just deleting it here.
delete(sm.queuedTTLs, bmsg.block.Height())

// Meta-data about the new block this peer is reporting. We use this
// below to update this peer's latest block height and the heights of
// other peers based on their last announced block hash. This allows us
Expand Down Expand Up @@ -1001,6 +1006,52 @@ func (sm *SyncManager) lastestCommittedTTLAcc() utreexo.Stump {
return sm.chainParams.TTL.Stump[len(sm.chainParams.TTL.Stump)-1]
}

// fetchUtreexoTTLs constructs a request for ttls that fetches as much as we could
// until we reach the end of the committed ttl accumulator state.
func (sm *SyncManager) fetchUtreexoTTLs(peer *peerpkg.Peer) {
// Nothing to do if there is no start header.
if sm.startHeader == nil {
log.Warnf("fetchUtreexoTTLs called with no start header")
return
}

// Can't fetch if both are nil.
if peer == nil && sm.syncPeer == nil {
log.Warnf("fetchUtreexoTTLs called with syncPeer and peer as nil")
return
}

// Default to the syncPeer unless we're given a peer by the caller.
reqPeer := sm.syncPeer
if peer != nil {
reqPeer = peer
}

peerState, exists := sm.peerStates[reqPeer]
if !exists {
log.Warnf("Don't have peer state for request peer %s", reqPeer.String())
return
}

stump := sm.lastestCommittedTTLAcc()

bestState := sm.chain.BestSnapshot()
gtmsg := wire.CalculateGetUtreexoTTLMsgs(
uint32(stump.NumLeaves), bestState.Height+1,
bestState.Height+wire.MaxUtreexoTTLsPerMsg)

_, found := peerState.requestedUtreexoTTLs[gtmsg]
if !found {
peerState.requestedUtreexoTTLs[gtmsg] = struct{}{}

log.Debugf("fetching ttls %v - %v from peer %v",
gtmsg.StartHeight,
gtmsg.StartHeight+(1<<gtmsg.MaxReceiveExponent),
reqPeer.String())
reqPeer.QueueMessage(&gtmsg, nil)
}
}

// fetchUtreexoSummaries creates and sends a request to the syncPeer for the next
// list of utreexo summaries to be downloaded based on the current list of headers.
// Will fetch from the peer if it's not nil. Otherwise it'll default to the syncPeer.
Expand Down Expand Up @@ -1084,6 +1135,17 @@ func (sm *SyncManager) fetchHeaderBlocks(peer *peerpkg.Peer) {
bestState := sm.chain.BestSnapshot()
length := bestHeaderHeight - bestState.Height

// Check if we have the ttl for the next block lined up to be download.
if sm.chain.IsUtreexoViewActive() &&
bestState.Height+1 <= int32(sm.lastestCommittedTTLAcc().NumLeaves)-1 {

// If we have no ttls, fetch for more.
if len(sm.queuedTTLs) == 0 {
sm.fetchUtreexoTTLs(reqPeer)
return
}
}

// Build up a getdata request for the list of blocks the headers
// describe. The size hint will be limited to wire.MaxInvPerMsg by
// the function, so no need to double check it here.
Expand All @@ -1094,6 +1156,14 @@ func (sm *SyncManager) fetchHeaderBlocks(peer *peerpkg.Peer) {
log.Infof("fetching from %v(%v) to %v(%v) from %v", hash, bestState.Height+1,
bestHeaderHash, bestHeaderHeight, reqPeer.String())
for h := bestState.Height + 1; h <= bestHeaderHeight; h++ {
if sm.chain.IsUtreexoViewActive() {
// Break if we ran out of ttls.
if h <= int32(sm.lastestCommittedTTLAcc().NumLeaves)-1 &&
h > bestState.Height+int32(len(sm.queuedTTLs)) {
break
}
}

hash, err := sm.chain.HeaderHashByHeight(h)
if err != nil {
log.Warnf("error while fetching the block hash for height %v -- %v",
Expand Down
8 changes: 8 additions & 0 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type MessageListeners struct {
// OnGetUtreexoProof is invoked when a peer receives a utreexo proof bitcoin message.
OnGetUtreexoProof func(p *Peer, msg *wire.MsgGetUtreexoProof)

// OnUtreexoTTLs is invoked when a peer receives a utreexo ttls bitcoin message.
OnUtreexoTTLs func(p *Peer, msg *wire.MsgUtreexoTTLs)

// OnGetUtreexoTTLs is invoked when a peer receives a get utreexo ttls bitcoin message.
OnGetUtreexoTTLs func(p *Peer, msg *wire.MsgGetUtreexoTTLs)

Expand Down Expand Up @@ -1508,6 +1511,11 @@ out:
p.cfg.Listeners.OnGetUtreexoTTLs(p, msg)
}

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

case *wire.MsgNotFound:
if p.cfg.Listeners.OnNotFound != nil {
p.cfg.Listeners.OnNotFound(p, msg)
Expand Down
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,11 @@ func (sp *serverPeer) OnUtreexoProof(_ *peer.Peer, msg *wire.MsgUtreexoProof) {
sp.server.syncManager.QueueUtreexoProof(msg, sp.Peer)
}

// OnUtreexoTTLs is invoked when a peer receives a utreexo ttls bitcoin message.
func (sp *serverPeer) OnUtreexoTTLs(_ *peer.Peer, msg *wire.MsgUtreexoTTLs) {
sp.server.syncManager.QueueUtreexoTTLs(msg, sp.Peer)
}

// 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 @@ -2663,6 +2668,7 @@ func newPeerConfig(sp *serverPeer) *peer.Config {
OnHeaders: sp.OnHeaders,
OnUtreexoSummaries: sp.OnUtreexoSummaries,
OnUtreexoProof: sp.OnUtreexoProof,
OnUtreexoTTLs: sp.OnUtreexoTTLs,
OnGetUtreexoProof: sp.OnGetUtreexoProof,
OnGetUtreexoRoot: sp.OnGetUtreexoRoot,
OnGetUtreexoTTLs: sp.OnGetUtreexoTTLs,
Expand Down
Loading
0