Skip to content

Commit

Permalink
Use LastSeen as libp2p seen messages cache strategy
Browse files Browse the repository at this point in the history
By default, the libp2p seen messages cache uses the `FirstSeen` strategy
which expires an entry once TTL elapses from the time it was added.
This means that if a single message is being received frequently and
consistently, pubsub will re-broadcast it every TTL, rather than never
re-broadcasting it.

In the context of the Keep client which additionally uses app-level
retransmissions, that often leads to a strong message amplification in the
broadcast channel which cause a significant increase in the network load.

As the problem is quite common
(see libp2p/go-libp2p-pubsub#502), the libp2p team
added a new `LastSeen` strategy which behaves differently. This strategy
expires an entry once TTL elapses from the last time the message was touched
by a cache write (`Add`) or read (`Has`) operation. That gives the desired
behavior of never re-broadcasting a message that was already seen within the
last TTL period. This reduces the risk of unintended over-amplification.

(cherry picked from commit 89a7b77)
  • Loading branch information
lukasz-zimnoch committed Feb 12, 2024
1 parent cbf53d5 commit 6fc7b5d
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/net/libp2p/channel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/keep-network/keep-core/pkg/net"
"github.com/keep-network/keep-core/pkg/net/retransmission"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubtc "github.com/libp2p/go-libp2p-pubsub/timecache"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peerstore"
)
Expand Down Expand Up @@ -51,6 +52,7 @@ func newChannelManager(
pubsub.WithMessageSignaturePolicy(pubsub.StrictSign),
pubsub.WithPeerOutboundQueueSize(libp2pPeerOutboundQueueSize),
pubsub.WithValidateQueueSize(libp2pValidationQueueSize),
pubsub.WithSeenMessagesStrategy(pubsubtc.Strategy_LastSeen),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6fc7b5d

Please sign in to comment.