Skip to content

Commit

Permalink
chore(dot/network): gossip sync.Map to std map (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Mar 23, 2022
1 parent dd80d0d commit 30244a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
19 changes: 13 additions & 6 deletions dot/network/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,35 @@ import (
"sync"

"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
)

// gossip submodule
type gossip struct {
logger log.LeveledLogger
seen *sync.Map
logger log.LeveledLogger
seenMap map[common.Hash]struct{}
seenMutex sync.RWMutex
}

// newGossip creates a new gossip message tracker
func newGossip() *gossip {
return &gossip{
logger: log.NewFromGlobal(log.AddContext("module", "gossip")),
seen: &sync.Map{},
logger: log.NewFromGlobal(log.AddContext("module", "gossip")),
seenMap: make(map[common.Hash]struct{}),
}
}

// hasSeen broadcasts messages that have not been seen
func (g *gossip) hasSeen(msg NotificationsMessage) bool {
// check if message has not been seen
if seen, ok := g.seen.Load(msg.Hash()); !ok || !seen.(bool) {
msgHash := msg.Hash()
g.seenMutex.Lock()
defer g.seenMutex.Unlock()

_, ok := g.seenMap[msgHash]
if !ok {
// set message to has been seen
g.seen.Store(msg.Hash(), true)
g.seenMap[msgHash] = struct{}{}
return false
}

Expand Down
27 changes: 6 additions & 21 deletions dot/network/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,12 @@ func TestGossip(t *testing.T) {

time.Sleep(TestMessageTimeout)

if hasSeenB, ok := nodeB.gossip.seen.Load(announceMessage.Hash()); !ok || hasSeenB.(bool) == false {
t.Error(
"node B did not receive block request message from node A",
"\nreceived:", hasSeenB,
"\nexpected:", true,
)
}
_, ok := nodeB.gossip.seenMap[announceMessage.Hash()]
require.True(t, ok, "node B did not receive block request message from node A")

if hasSeenC, ok := nodeC.gossip.seen.Load(announceMessage.Hash()); !ok || hasSeenC.(bool) == false {
t.Error(
"node C did not receive block request message from node B",
"\nreceived:", hasSeenC,
"\nexpected:", true,
)
}
_, ok = nodeC.gossip.seenMap[announceMessage.Hash()]
require.True(t, ok, "node C did not receive block request message from node B")

if hasSeenA, ok := nodeA.gossip.seen.Load(announceMessage.Hash()); !ok || hasSeenA.(bool) == false {
t.Error(
"node A did not receive block request message from node C",
"\nreceived:", hasSeenA,
"\nexpected:", true,
)
}
_, ok = nodeA.gossip.seenMap[announceMessage.Hash()]
require.True(t, ok, "node A did not receive block request message from node C")
}

0 comments on commit 30244a8

Please sign in to comment.