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: make peer meet query.Peer interface #2287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kcalvinalvin
Copy link
Collaborator

query.Peer is used for downloading blocks out of order during headers first download. Methods SubscribeRecvMsg() and OnDisconnect() are added to abide by the interface.

This PR is part of #2226 and is split here so that it's easier to review.

@kcalvinalvin kcalvinalvin force-pushed the 2024-12-10-meet-peer-query-interface branch from cd4d77c to 2dde721 Compare December 10, 2024 04:33
@coveralls
Copy link

coveralls commented Dec 10, 2024

Pull Request Test Coverage Report for Build 12290679740

Details

  • 2 of 32 (6.25%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 57.251%

Changes Missing Coverage Covered Lines Changed/Added Lines %
peer/peer.go 2 32 6.25%
Totals Coverage Status
Change from base Build 12253655920: -0.03%
Covered Lines: 29914
Relevant Lines: 52251

💛 - Coveralls

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Straight forward diff.

One thought I had is if we should actually overhaul the the query.Peer interface to better suit our needs, based on the new set of requirements. Given that you've started to use it to implement the out of order block download, do you find it lacking in any notable ways?

// messages received from this peer will be sent on the returned
// channel. A closure is also returned, that should be called to cancel
// the subscription.
func (p *Peer) SubscribeRecvMsg() (<-chan wire.Message, func()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be thread safe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's thread safe now :)

peer/peer.go Outdated
// Cancellation is just removing the channel from the subscribers list.
idx := len(p.subscribers) - 1
cancel := func() {
p.subscribers = append(p.subscribers[:idx],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use slice.Delete here: https://pkg.go.dev/slices#Delete

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the go.mod to 1.18? go build fails because go.mod specifies 1.17

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no need since I changed this to a map

peer/peer.go Outdated
// channel. A closure is also returned, that should be called to cancel
// the subscription.
func (p *Peer) SubscribeRecvMsg() (<-chan wire.Message, func()) {
msgChan := make(chan wire.Message, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so a buffer of 1 will mean that writeMessage will block anytime a subscriber fails to immediately read out a message. If that goroutine blocks, then all outbound message writing will also halt.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed so that it's no longer buffered but has each msg send as a new goroutine

peer/peer.go Outdated
@@ -1402,6 +1424,10 @@ out:
// needed.
rmsg, buf, err := p.readMessage(p.wireEncoding)
idleTimer.Stop()
// Send the received message to all the subscribers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: lack of newline above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

peer/peer.go Outdated
@@ -1402,6 +1424,10 @@ out:
// needed.
rmsg, buf, err := p.readMessage(p.wireEncoding)
idleTimer.Stop()
// Send the received message to all the subscribers.
for _, sub := range p.subscribers {
sub <- rmsg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we only do this in the case of a non-error case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@kcalvinalvin
Copy link
Collaborator Author

Straight forward diff.

One thought I had is if we should actually overhaul the the query.Peer interface to better suit our needs, based on the new set of requirements. Given that you've started to use it to implement the out of order block download, do you find it lacking in any notable ways?

Biggest thing I'm noticing is:

1: Hard to punish slow peers on the btcd side since there's no really good way of figuring out which peer is slow or which peer timed out on a block. That information is kept on the query package.

2: The query package can't disconnect off of slow peers since the ability to disconnect off of a peer is kept on the btcd side. The best it can do is just not ask for blocks from that peer.

Also this is not exactly about the Peer interface but:

3: Peer string passed through HandleResponse https://github.com/lightninglabs/neutrino/blob/81d6cd2d2da5b7ce01664ae71d8fce027cd27de4/query/interface.go#L141 is lacking as it makes it annoying on the btcd side. You have to search through the peerStates map to get access to the methods on the peer struct like Disconnect().

query.Peer is used for downloading blocks out of order during headers
first download.  Methods SubscribeRecvMsg() and OnDisconnect() are added
to abide by the interface.
@kcalvinalvin kcalvinalvin force-pushed the 2024-12-10-meet-peer-query-interface branch from 2dde721 to 32d7f0c Compare December 12, 2024 05:53
@kcalvinalvin
Copy link
Collaborator Author

Addressed everything in the new push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants