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

feat(SPV-742): add peer manager #247

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

arkadiuszos4chain
Copy link
Contributor

@arkadiuszos4chain arkadiuszos4chain commented Apr 26, 2024

Pull Request Checklist

  • πŸ“– I created my PR using provided : CODE_STANDARDS
  • πŸ“– I have read the short Code of Conduct: CODE_OF_CONDUCT
  • 🏠 I tested my changes locally.
  • βœ… I have provided tests for my changes.
  • πŸ“ I have used conventional commits.
  • πŸ“— I have updated any related documentation.
  • πŸ’Ύ PR was issued based on the Github or Jira issue.

@arkadiuszos4chain arkadiuszos4chain self-assigned this Apr 26, 2024
Copy link
Contributor

Manual Tests

ℹ️ Remember to ask team members to perform manual tests and to assign tested label after testing.

internal/transports/p2p/server.go Fixed Show fixed Hide fixed
internal/transports/p2p/server.go Fixed Show fixed Hide fixed
@arkadiuszos4chain arkadiuszos4chain marked this pull request as ready for review May 1, 2024 08:09
@arkadiuszos4chain arkadiuszos4chain requested a review from a team as a code owner May 1, 2024 08:09
@@ -42,7 +42,7 @@ var version = "development"

type P2PServer interface {
Start() error
Shutdown() error
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it a good practice to stay with error return when shutting down? Probably we can't do anything with that, but at least we have a message and we can possibly kill it manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can see that neither implementation returns any error - so... why stay here with an error?

internal/chaincfg/params.go Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/server.go Outdated Show resolved Hide resolved
transports/p2p/connmgr/seed.go Outdated Show resolved Hide resolved
config/defaults.go Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/peer/peers_collection.go Outdated Show resolved Hide resolved
Comment on lines 25 to 28
addrFitlerFn := IsRoutable
if acceptLocalAddresses {
addrFitlerFn = IsRoutableWithLocal
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of having two separate functions IsRoutable and IsRoutableWithLocal and having this variable addrFitlerFn (funny typo there BTW πŸ˜„ ), maybe it's better to just add a param to function:

func IsRoutable(na *wire.NetAddr, checkLocal bool) bool

This would allow to remove the field addrFilterFn from AddressBook struct and just use IsRoutable function directly

internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/address_book_test.go Outdated Show resolved Hide resolved
internal/transports/p2p/server.go Outdated Show resolved Hide resolved
internal/transports/p2p/peer/peers_collection.go Outdated Show resolved Hide resolved
internal/transports/p2p/server.go Outdated Show resolved Hide resolved
internal/transports/p2p/server.go Show resolved Hide resolved
internal/transports/p2p/server.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/network.go Outdated Show resolved Hide resolved
internal/transports/p2p/peer/peer.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/address_book.go Outdated Show resolved Hide resolved
internal/transports/p2p/network/network.go Outdated Show resolved Hide resolved
Comment on lines 16 to 17
addrs []*knownAddress // addrs is a slice containing known addresses
addrsLookup map[string]int // addrLookup is a map for fast lookup of addresses, maps address key to index in addrs slice
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the reason to have both.
IMHO all we need is map[string]*knownAddress
and when doing random peek, we can just use maps.Values(..)[random]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will have ~2k addresses. I'm not sure that allocating new array of this size every time we want random address is a good idea.

}

// UpsertAddrs updates or adds multiple addresses.
func (a *AddressBook) UpsertAddrs(address []*wire.NetAddress) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (a *AddressBook) UpsertAddrs(address []*wire.NetAddress) {
func (a *AddressBook) AddAll(address []*wire.NetAddress) {

}
return peer, nil
return peer
}

func (p *Peer) Connect() error {
err := p.updatePeerAddr()
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually this could be done in the NewPeer method

internal/transports/p2p/server.go Outdated Show resolved Hide resolved
internal/transports/p2p/server.go Outdated Show resolved Hide resolved
Comment on lines 253 to 264
s.ctxWg.Add(1) // Wait on shutdown

if s.inboundPeers.Space() == 0 {
s.log.Debug().Msg("[observeInboundPeers] nothing to do")
s.noWaitingSleep(sleeDuration)
continue
}

s.log.Info().Msgf("listening for inbound connections on port %d", s.chainParams.DefaultPort)
s.waitForIncomingConnection() // Accept connection one-by-one to gracefully handle shutdown

s.ctxWg.Done()
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks a little complicated.
IMHO we should run in go routine an infinite loop that
will accept connections,
try to add it to PeersCollection (or check if there is a space)
if it is ok to add it, then use peer.Connect
if it is not ok, then disconnect the connection

And in case of exiting, all we need to do is: s.listener.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clever, changed. But, to be honest, I'm not sure if it's as error-proof as the previous implementation

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.

5 participants