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

p2p: add peer name to networking component contexts #652

Merged
merged 2 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions core/consensus/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (c *Component) Propose(ctx context.Context, duty core.Duty, data core.Unsig
func (c *Component) makeHandler(ctx context.Context) func(s network.Stream) {
ctx = log.WithTopic(ctx, "qbft")
return func(s network.Stream) {
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))
Copy link
Contributor

Choose a reason for hiding this comment

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

you should use the log package for this to work: ctx = log.WithCtx(ctx, z.Str("peer", p2p.PeerName(s.Conn().RemotePeer()))

defer s.Close()

b, err := io.ReadAll(s)
Expand Down
2 changes: 2 additions & 0 deletions core/consensus/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/obolnetwork/charon/core"
pbv1 "github.com/obolnetwork/charon/core/corepb/v1"
"github.com/obolnetwork/charon/core/qbft"
"github.com/obolnetwork/charon/p2p"
)

// transport encapsulates receiving and broadcasting for a consensus instance/duty.
Expand Down Expand Up @@ -115,6 +116,7 @@ func (t *transport) Broadcast(ctx context.Context, typ qbft.MsgType, duty core.D
continue
}

ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(t.component.tcpNode.ID()))
Copy link
Contributor

Choose a reason for hiding this comment

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

this is broadcasting, this ticket is for receiving. p2pSender will handle sending logs.

err := send(ctx, t.component.tcpNode, p.ID, msg.ToConsensusMsg())
if err != nil {
log.Warn(ctx, "Failed sending message", err)
Expand Down
3 changes: 3 additions & 0 deletions core/leadercast/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/p2p"
)

const protocol = "/charon/leadercast/1.0.0"
Expand Down Expand Up @@ -63,6 +64,8 @@ type p2pTransport struct {
// handle implements p2p network.StreamHandler processing new incoming messages.
func (t *p2pTransport) handle(s network.Stream) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))

defer cancel()
defer s.Close()

Expand Down
2 changes: 2 additions & 0 deletions core/parsigex/parsigex.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/core"
pbv1 "github.com/obolnetwork/charon/core/corepb/v1"
"github.com/obolnetwork/charon/p2p"
)

const protocol = "/charon/parsigex/1.0.0"
Expand Down Expand Up @@ -58,6 +59,7 @@ type ParSigEx struct {
func (m *ParSigEx) handle(s network.Stream) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx = log.WithTopic(ctx, "parsigex")
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))
defer cancel()
defer s.Close()

Expand Down
2 changes: 2 additions & 0 deletions dkg/frostp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func newFrostP2P(ctx context.Context, tcpNode host.Host, peers map[uint32]peer.I
}

tcpNode.SetStreamHandler(round1Protocol(clusterID), func(s network.Stream) {
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))
defer s.Close()

b, err := io.ReadAll(s)
Expand Down Expand Up @@ -78,6 +79,7 @@ func newFrostP2P(ctx context.Context, tcpNode host.Host, peers map[uint32]peer.I
})

tcpNode.SetStreamHandler(round2Protocol(clusterID), func(s network.Stream) {
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))
defer s.Close()

b, err := io.ReadAll(s)
Expand Down
1 change: 1 addition & 0 deletions dkg/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type keycastP2P struct {
// ServeShares serves the dealer shares to other nodes on request. It returns when the context is closed.
func (t keycastP2P) ServeShares(ctx context.Context, handler func(nodeIdx int) (msg []byte, err error)) {
t.tcpNode.SetStreamHandler(getProtocol(t.clusterID), func(s network.Stream) {
ctx = context.WithValue(ctx, p2p.PeerCtxKey, p2p.PeerName(s.Conn().RemotePeer()))
defer s.Close()

var (
Expand Down
2 changes: 2 additions & 0 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/obolnetwork/charon/app/errors"
)

const PeerCtxKey = "peer"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it okay to let other packages depend on p2p just for the context key? other workaround is adding //nolint:revive to all the changes above

Copy link
Contributor

Choose a reason for hiding this comment

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

nah, this is not worth it. We use string literals in many places without needing to add nolint...


// Peer represents a peer in the libp2p network, either a charon node or a relay.
type Peer struct {
// ENR defines the networking information of the peer.
Expand Down