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

fix(p2p): avoid receiving duplicated blocks #818

Merged
merged 11 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
11 changes: 9 additions & 2 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,28 @@ func (m *Manager) onNodeHealthStatus(event pubsub.Message) {
// TODO: move to gossip.go
// onNewGossippedBlock will take a block and apply it
func (m *Manager) onNewGossipedBlock(event pubsub.Message) {
m.retrieverMutex.Lock() // needed to protect blockCache access
eventData := event.Data().(p2p.GossipedBlock)
block := eventData.Block
commit := eventData.Commit
m.logger.Debug("Received new block via gossip", "height", block.Header.Height, "n cachedBlocks", len(m.blockCache))
m.retrieverMutex.Lock() // needed to protect blockCache access
_, found := m.blockCache[block.Header.Height]
mtsitrin marked this conversation as resolved.
Show resolved Hide resolved
if found {
m.retrieverMutex.Unlock()
return
}
danwt marked this conversation as resolved.
Show resolved Hide resolved

m.logger.Debug("Received new block via gossip", "block height", block.Header.Height, "store height", m.Store.Height(), "n cachedBlocks", len(m.blockCache))
danwt marked this conversation as resolved.
Show resolved Hide resolved

nextHeight := m.Store.NextHeight()
if block.Header.Height >= nextHeight {
m.blockCache[block.Header.Height] = CachedBlock{
Block: &block,
Commit: &commit,
}
m.logger.Debug("caching block", "block height", block.Header.Height, "store height", m.Store.Height())
}
m.retrieverMutex.Unlock() // have to give this up as it's locked again in attempt apply, and we're not re-entrant

err := m.attemptApplyCachedBlocks()
if err != nil {
m.logger.Error("applying cached blocks", "err", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22.2
require (
code.cloudfoundry.org/go-diodes v0.0.0-20220725190411-383eb6634c40
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
github.com/avast/retry-go/v4 v4.5.0
github.com/celestiaorg/celestia-openrpc v0.4.0-rc.1
github.com/celestiaorg/go-cnc v0.4.2
Expand Down Expand Up @@ -248,6 +247,7 @@ require (
)

require (
cosmossdk.io/math v1.3.0 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions mockery.go

This file was deleted.

4 changes: 2 additions & 2 deletions p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ func (c *Client) tryConnect(ctx context.Context, peer peer.AddrInfo) {
func (c *Client) setupGossiping(ctx context.Context) error {
pubsub.GossipSubHistoryGossip = c.conf.GossipCacheSize
pubsub.GossipSubHistoryLength = c.conf.GossipCacheSize
pubsub.GossipSubMaxIHaveMessages = c.conf.GossipCacheSize

ps, err := pubsub.NewGossipSub(ctx, c.Host)
//We add WithSeenMessagesTTL (with 1 year time) option to avoid ever requesting already seen blocks
ps, err := pubsub.NewGossipSub(ctx, c.Host, pubsub.WithSeenMessagesTTL(8760*time.Hour))
Copy link
Contributor

Choose a reason for hiding this comment

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

can u explain this 1 year time? previously u had
time.Duration(int(c.blockTime.Nanoseconds()*int64(c.conf.GossipCacheSize))))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i removed the ttl option and replaced gossipsub version with a new version where seen packets are not expiring

if err != nil {
return err
}
Expand Down
Loading