Skip to content

Commit 5cd74ac

Browse files
authored
Merge pull request #1676 from keep-network/logging-tweaks
Logging tweaks for net package
2 parents 59e7dd3 + 5445913 commit 5cd74ac

File tree

12 files changed

+79
-47
lines changed

12 files changed

+79
-47
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/google/gofuzz v1.1.0
1515
github.com/ipfs/go-datastore v0.1.1
1616
github.com/ipfs/go-log v0.0.1
17-
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200420163504-f5aa53962524
17+
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec
1818
github.com/keep-network/keep-common v0.3.0-rc
1919
github.com/libp2p/go-addr-util v0.0.1
2020
github.com/libp2p/go-libp2p v0.4.1

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ github.com/karalabe/usb v0.0.0-20191104083709-911d15fe12a9 h1:ZHuwnjpP8LsVsUYqTq
244244
github.com/karalabe/usb v0.0.0-20191104083709-911d15fe12a9/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
245245
github.com/keep-network/cli v1.20.0 h1:mEufpPsovOVdduTTkk+a2CxS3crIrGFqUsvs58gSik8=
246246
github.com/keep-network/cli v1.20.0/go.mod h1:nzsst4JjU+rGE8Q5J839fYxectxWHpLhxKNohQWtQhA=
247-
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200420163504-f5aa53962524 h1:iOBE6U3YmgJ+VB2DnO5bNH9rmVWSiGUY1xgyjKOEGH8=
248-
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200420163504-f5aa53962524/go.mod h1:MtZSKNavpxpPY0VLt2sl118IxptrGQDUb/MBu+j2Yow=
247+
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec h1:2pXAsi4OUUjZKr5ds5UOF2IxXN+jVW0WetVO+czkf+A=
248+
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec/go.mod h1:xR8jf3/VJAjh3nWu5tFe8Yxnt2HvWsqZHfGef1P5oDk=
249249
github.com/keep-network/keep-common v0.3.0-rc h1:e2Vdq3TyQ6dGRNZQBykvfYZgKZwNKS+Z8DYEwoMIiuc=
250250
github.com/keep-network/keep-common v0.3.0-rc/go.mod h1:0uY+hufkP66nFnL+3GXMwWXRiKKsFyXFQTby1xR7AwY=
251251
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -313,7 +313,6 @@ github.com/libp2p/go-libp2p-kad-dht v0.3.0 h1:KUJaqW3kkHP6zcL0s1CDg+yO0NYNNPkXmG
313313
github.com/libp2p/go-libp2p-kad-dht v0.3.0/go.mod h1:7nBsfkMq2zN1qPs6n/fNopJfvmK9NZRNicRrCnwQR8o=
314314
github.com/libp2p/go-libp2p-kbucket v0.2.1 h1:q9Jfwww9XnXc1K9dyYuARJxJvIvhgYVaQCuziO/dF3c=
315315
github.com/libp2p/go-libp2p-kbucket v0.2.1/go.mod h1:/Rtu8tqbJ4WQ2KTCOMJhggMukOLNLNPY1EtEWWLxUvc=
316-
github.com/libp2p/go-libp2p-loggables v0.0.1/go.mod h1:lDipDlBNYbpyqyPX/KcoO+eq0sJYEVR2JgOexcivchg=
317316
github.com/libp2p/go-libp2p-loggables v0.1.0 h1:h3w8QFfCt2UJl/0/NW4K829HX/0S4KD31PQ7m8UXXO8=
318317
github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90=
319318
github.com/libp2p/go-libp2p-mplex v0.2.0/go.mod h1:Ejl9IyjvXJ0T9iqUTE1jpYATQ9NM3g+OtR+EMMODbKo=

pkg/net/ephemeral/private_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func GenerateKeyPair() (*KeyPair, error) {
2929
ecdsaKey, err := btcec.NewPrivateKey(curve())
3030
if err != nil {
3131
return nil, fmt.Errorf(
32-
"could not generate new ephemeral keypair [%v]",
32+
"could not generate new ephemeral keypair: [%v]",
3333
err,
3434
)
3535
}
@@ -58,7 +58,7 @@ func UnmarshalPrivateKey(bytes []byte) *PrivateKey {
5858
func UnmarshalPublicKey(bytes []byte) (*PublicKey, error) {
5959
pubKey, err := btcec.ParsePubKey(bytes, curve())
6060
if err != nil {
61-
return nil, fmt.Errorf("could not parse ephemeral public key [%v]", err)
61+
return nil, fmt.Errorf("could not parse ephemeral public key: [%v]", err)
6262
}
6363

6464
return (*PublicKey)(pubKey), nil

pkg/net/libp2p/authenticated_connection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func newAuthenticatedInboundConnection(
5656
// close the conn before returning (if it hasn't already)
5757
// otherwise we leak.
5858
ac.Close()
59-
return nil, fmt.Errorf("connection handshake failed [%v]", err)
59+
return nil, fmt.Errorf("connection handshake failed: [%v]", err)
6060
}
6161

6262
if err := ac.checkFirewallRules(); err != nil {
@@ -82,7 +82,7 @@ func newAuthenticatedOutboundConnection(
8282
remotePublicKey, err := remotePeerID.ExtractPublicKey()
8383
if err != nil {
8484
return nil, fmt.Errorf(
85-
"could not create new authenticated outbound connection [%v]",
85+
"could not create new authenticated outbound connection: [%v]",
8686
err,
8787
)
8888
}
@@ -98,7 +98,7 @@ func newAuthenticatedOutboundConnection(
9898

9999
if err := ac.runHandshakeAsInitiator(); err != nil {
100100
ac.Close()
101-
return nil, fmt.Errorf("connection handshake failed [%v]", err)
101+
return nil, fmt.Errorf("connection handshake failed: [%v]", err)
102102
}
103103

104104
if err := ac.checkFirewallRules(); err != nil {
@@ -425,7 +425,7 @@ func (ac *authenticatedConnection) verify(
425425
ok, err := pubKey.Verify(messageBytes, signatureBytes)
426426
if err != nil {
427427
return fmt.Errorf(
428-
"failed to verify signature [0x%v] for sender [%v] with err [%v]",
428+
"failed to verify signature [0x%v] for sender [%v]: [%v]",
429429
hex.EncodeToString(signatureBytes),
430430
actualSender.Pretty(),
431431
err,
@@ -434,7 +434,7 @@ func (ac *authenticatedConnection) verify(
434434

435435
if !ok {
436436
return fmt.Errorf(
437-
"invalid signature [0x%v] on message from sender [%v] ",
437+
"invalid signature [0x%v] on message from sender [%v]",
438438
hex.EncodeToString(signatureBytes),
439439
actualSender.Pretty(),
440440
)

pkg/net/libp2p/channel.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *channel) Recv(ctx context.Context, handler func(m net.Message)) {
103103
for {
104104
select {
105105
case <-ctx.Done():
106-
logger.Debug("context is done, removing handler")
106+
logger.Debug("context is done; removing message handler")
107107
c.removeHandler(messageHandler)
108108
return
109109

@@ -210,7 +210,7 @@ func (c *channel) subscriptionWorker(ctx context.Context) {
210210
select {
211211
case c.incomingMessageQueue <- message:
212212
default:
213-
logger.Warningf("consumers too slow, dropping message")
213+
logger.Warningf("message workers are too slow; dropping message")
214214
}
215215
}
216216
}
@@ -264,7 +264,7 @@ func (c *channel) processContainerMessage(
264264
// sender identifier we grab from the message (inner layer).
265265
if proposedSender != senderIdentifier.id {
266266
return fmt.Errorf(
267-
"Outer layer sender [%v] does not match inner layer sender [%v]",
267+
"outer layer sender [%v] does not match inner layer sender [%v]",
268268
proposedSender,
269269
senderIdentifier,
270270
)
@@ -299,7 +299,8 @@ func (c *channel) getUnmarshalingContainerByType(messageType string) (net.Tagged
299299
unmarshaler, found := c.unmarshalersByType[messageType]
300300
if !found {
301301
return nil, fmt.Errorf(
302-
"couldn't find unmarshaler for type %s", messageType,
302+
"couldn't find unmarshaler for type [%s]",
303+
messageType,
303304
)
304305
}
305306

@@ -316,7 +317,7 @@ func (c *channel) deliver(message net.Message) {
316317
select {
317318
case handler.channel <- message:
318319
default:
319-
logger.Warningf("handler too slow, dropping message")
320+
logger.Warningf("message handler is too slow; dropping message")
320321
}
321322
}
322323
}

pkg/net/libp2p/identity.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func createIdentity(privateKey libp2pcrypto.PrivKey) (*identity, error) {
2727
peerID, err := peer.IDFromPublicKey(privateKey.GetPublic())
2828
if err != nil {
2929
return nil, fmt.Errorf(
30-
"could not transform public key to peer's identity [%v]", err,
30+
"could not transform public key to peer's identity: [%v]",
31+
err,
3132
)
3233
}
3334

@@ -66,15 +67,18 @@ func (i *identity) Unmarshal(bytes []byte) error {
6667
)
6768

6869
if err = pbIdentity.Unmarshal(bytes); err != nil {
69-
return fmt.Errorf("unmarshalling failed with error %s", err)
70+
return fmt.Errorf("unmarshalling failed: [%v]", err)
7071
}
7172
i.pubKey, err = libp2pcrypto.UnmarshalPublicKey(pbIdentity.PubKey)
7273
if err != nil {
7374
return err
7475
}
7576
pid, err = peer.IDFromPublicKey(i.pubKey)
7677
if err != nil {
77-
return fmt.Errorf("Failed to generate valid libp2p identity with err: %s", err)
78+
return fmt.Errorf(
79+
"failed to generate valid libp2p identity: [%v]",
80+
err,
81+
)
7882
}
7983
i.id = pid
8084

pkg/net/libp2p/libp2p.go

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353
// FirewallCheckTick is the amount of time between periodic checks of all
5454
// firewall rules against all peers connected to this one.
5555
FirewallCheckTick = time.Minute * 10
56+
// ConnectedPeersCheckTick is the amount of time between periodic checks of
57+
// the number of connected peers.
58+
ConnectedPeersCheckTick = time.Minute * 1
5659
)
5760

5861
// Config defines the configuration for the libp2p network provider.
@@ -134,6 +137,14 @@ type connectionManager struct {
134137
host.Host
135138
}
136139

140+
func newConnectionManager(ctx context.Context, host host.Host) *connectionManager {
141+
connectionManager := &connectionManager{host}
142+
143+
go connectionManager.monitorConnectedPeers(ctx)
144+
145+
return connectionManager
146+
}
147+
137148
func (cm *connectionManager) ConnectedPeers() []string {
138149
var peers []string
139150
for _, connectedPeer := range cm.Network().Peers() {
@@ -146,7 +157,7 @@ func (cm *connectionManager) GetPeerPublicKey(connectedPeer string) (*key.Networ
146157
peerID, err := peer.IDB58Decode(connectedPeer)
147158
if err != nil {
148159
return nil, fmt.Errorf(
149-
"Failed to decode peer ID from [%s] with error: [%v]",
160+
"failed to decode peer ID from [%s]: [%v]",
150161
connectedPeer,
151162
err,
152163
)
@@ -155,7 +166,7 @@ func (cm *connectionManager) GetPeerPublicKey(connectedPeer string) (*key.Networ
155166
peerPublicKey, err := peerID.ExtractPublicKey()
156167
if err != nil {
157168
return nil, fmt.Errorf(
158-
"Failed to extract peer [%s] public key with error: [%v]",
169+
"failed to extract peer [%s] public key: [%v]",
159170
connectedPeer,
160171
err,
161172
)
@@ -167,7 +178,7 @@ func (cm *connectionManager) GetPeerPublicKey(connectedPeer string) (*key.Networ
167178
func (cm *connectionManager) DisconnectPeer(peerHash string) {
168179
peerID, err := peer.IDB58Decode(peerHash)
169180
if err != nil {
170-
logger.Errorf("failed to decode peer hash: [%v] [%v]", peerHash, err)
181+
logger.Errorf("failed to decode peer hash [%v]: [%v]", peerHash, err)
171182
return
172183
}
173184

@@ -191,6 +202,23 @@ func (cm *connectionManager) AddrStrings() []string {
191202
return multiaddrStrings
192203
}
193204

205+
func (cm *connectionManager) monitorConnectedPeers(ctx context.Context) {
206+
ticker := time.NewTicker(ConnectedPeersCheckTick)
207+
defer ticker.Stop()
208+
209+
for {
210+
select {
211+
case <-ticker.C:
212+
connectedPeers := cm.ConnectedPeers()
213+
214+
logger.Infof("number of connected peers: [%v]", len(connectedPeers))
215+
logger.Debugf("connected peers: [%v]", connectedPeers)
216+
case <-ctx.Done():
217+
return
218+
}
219+
}
220+
}
221+
194222
// ConnectOptions allows to set various options used by libp2p.
195223
type ConnectOptions struct {
196224
RoutingTableRefreshPeriod time.Duration
@@ -285,21 +313,21 @@ func Connect(
285313
}
286314

287315
if len(config.Peers) == 0 {
288-
logger.Infof("node's peers list is empty")
316+
logger.Infof("bootstrap peers list is empty")
289317
}
290318

291-
if err := provider.bootstrap(
292-
ctx,
293-
config.Peers,
294-
); err != nil {
295-
return nil, fmt.Errorf("Failed to bootstrap nodes with err: %v", err)
319+
if err := provider.bootstrap(ctx, config.Peers); err != nil {
320+
return nil, fmt.Errorf("bootstrap failed: [%v]", err)
296321
}
297322

298-
provider.connectionManager = &connectionManager{provider.host}
323+
provider.connectionManager = newConnectionManager(ctx, provider.host)
299324

300-
// Instantiates and starts the connection management background process
325+
// Instantiates and starts the connection management background process.
301326
watchtower.NewGuard(
302-
ctx, FirewallCheckTick, firewall, provider.connectionManager,
327+
ctx,
328+
FirewallCheckTick,
329+
firewall,
330+
provider.connectionManager,
303331
)
304332

305333
return provider, nil
@@ -326,7 +354,7 @@ func discoverAndListen(
326354
)
327355
if err != nil {
328356
return nil, fmt.Errorf(
329-
"could not create authenticated transport [%v]",
357+
"could not create authenticated transport: [%v]",
330358
err,
331359
)
332360
}

pkg/net/libp2p/unicast_channel.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ func (uc *unicastChannel) getUnmarshalingContainerByType(messageType string) (
295295
unmarshaler, found := uc.unmarshalersByType[messageType]
296296
if !found {
297297
return nil, fmt.Errorf(
298-
"couldn't find unmarshaler for type %s", messageType,
298+
"couldn't find unmarshaler for type [%s]",
299+
messageType,
299300
)
300301
}
301302

pkg/net/libp2p/unicast_channel_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (ucm *unicastChannelManager) getUnicastChannelWithHandshake(
9292
) {
9393
err := ucm.trialHandshake(peerID)
9494
if err != nil {
95-
return nil, fmt.Errorf("handshake error: [%v]", err)
95+
return nil, fmt.Errorf("unicast channel handshake error: [%v]", err)
9696
}
9797

9898
channel, _, err := ucm.getUnicastChannel(peerID)

pkg/net/retransmission/retransmission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ScheduleRetransmissions(
3030
ticker.onTick(ctx, func() {
3131
go func() {
3232
if err := retransmit(); err != nil {
33-
logger.Errorf("could not retransmit message [%v]", err)
33+
logger.Errorf("could not retransmit message: [%v]", err)
3434
}
3535
}()
3636
})

0 commit comments

Comments
 (0)