Skip to content

Commit d7f04c6

Browse files
committed
Address comments.
1 parent f3a8f3b commit d7f04c6

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

dot/network/message_cache.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package network
22

33
import (
4+
"errors"
45
"time"
56

67
"github.com/ChainSafe/gossamer/lib/common"
@@ -31,8 +32,8 @@ func newMessageCache(config ristretto.Config, ttl time.Duration) (*messageCache,
3132
return &messageCache{cache: cache, ttl: ttl}, nil
3233
}
3334

34-
// Put appends peer ID and message data and stores it in cache with TTL.
35-
func (m *messageCache) Put(peer peer.ID, msg []byte) (bool, error) {
35+
// put appends peer ID and message data and stores it in cache with TTL.
36+
func (m *messageCache) put(peer peer.ID, msg NotificationsMessage) (bool, error) {
3637
key, err := generateCacheKey(peer, msg)
3738
if err != nil {
3839
return false, err
@@ -47,8 +48,8 @@ func (m *messageCache) Put(peer peer.ID, msg []byte) (bool, error) {
4748
return ok, nil
4849
}
4950

50-
// Exists checks if <peer ID, message data> exist in cache.
51-
func (m *messageCache) Exists(peer peer.ID, msg []byte) bool {
51+
// exists checks if <peer ID, message> exist in cache.
52+
func (m *messageCache) exists(peer peer.ID, msg NotificationsMessage) bool {
5253
key, err := generateCacheKey(peer, msg)
5354
if err != nil {
5455
return false
@@ -58,8 +59,12 @@ func (m *messageCache) Exists(peer peer.ID, msg []byte) bool {
5859
return ok
5960
}
6061

61-
func generateCacheKey(peer peer.ID, msg []byte) ([]byte, error) {
62-
peerMsgHash, err := common.Blake2bHash(append([]byte(peer), msg...))
62+
func generateCacheKey(peer peer.ID, msg NotificationsMessage) ([]byte, error) {
63+
if msg.IsHandshake() {
64+
return nil, errors.New("cache does not support handshake messages")
65+
}
66+
67+
peerMsgHash, err := common.Blake2bHash(append([]byte(peer), msg.Hash().ToBytes()...))
6368
if err != nil {
6469
return nil, err
6570
}

dot/network/message_cache_test.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package network
22

33
import (
4+
"math/big"
45
"testing"
56
"time"
67

8+
"github.com/ChainSafe/gossamer/dot/types"
9+
"github.com/ChainSafe/gossamer/lib/common"
710
"github.com/dgraph-io/ristretto"
811
"github.com/libp2p/go-libp2p-core/peer"
912
"github.com/stretchr/testify/require"
@@ -19,22 +22,53 @@ func TestMessageCache(t *testing.T) {
1922
return int64(1)
2023
},
2124
}, 800*time.Millisecond)
25+
require.NoError(t, err)
2226

2327
peerID := peer.ID("gossamer")
24-
msgData := []byte("testData")
25-
require.NoError(t, err)
28+
msg := &BlockAnnounceMessage{
29+
ParentHash: common.Hash{1},
30+
Number: big.NewInt(77),
31+
StateRoot: common.Hash{2},
32+
ExtrinsicsRoot: common.Hash{3},
33+
Digest: types.Digest{},
34+
}
2635

27-
ok, err := msgCache.Put(peerID, msgData)
36+
ok, err := msgCache.put(peerID, msg)
2837
require.NoError(t, err)
2938
require.True(t, ok)
3039

3140
time.Sleep(750 * time.Millisecond)
3241

33-
ok = msgCache.Exists(peerID, msgData)
42+
ok = msgCache.exists(peerID, msg)
3443
require.True(t, ok)
3544

3645
time.Sleep(50 * time.Millisecond)
3746

38-
ok = msgCache.Exists(peerID, msgData)
47+
ok = msgCache.exists(peerID, msg)
48+
require.False(t, ok)
49+
}
50+
51+
func TestMessageCacheError(t *testing.T) {
52+
cacheSize := 64 << 20 // 64 MB
53+
msgCache, err := newMessageCache(ristretto.Config{
54+
NumCounters: int64(float64(cacheSize) * 0.05 * 2),
55+
MaxCost: int64(float64(cacheSize) * 0.95),
56+
BufferItems: 64,
57+
Cost: func(value interface{}) int64 {
58+
return int64(1)
59+
},
60+
}, 800*time.Millisecond)
61+
require.NoError(t, err)
62+
63+
peerID := peer.ID("gossamer")
64+
msg := &BlockAnnounceHandshake{
65+
Roles: 4,
66+
BestBlockNumber: 77,
67+
BestBlockHash: common.Hash{1},
68+
GenesisHash: common.Hash{2},
69+
}
70+
71+
ok, err := msgCache.put(peerID, msg)
72+
require.Error(t, err, "cache does not support handshake messages")
3973
require.False(t, ok)
4074
}

dot/network/notifications.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,14 @@ func (s *Service) broadcastExcluding(info *notificationsProtocol, excluding peer
243243
err = s.host.send(peer, info.protocolID, hs)
244244
} else {
245245
if s.host.messageCache != nil {
246-
var encMsg []byte
247-
encMsg, err = msg.Encode()
246+
var added bool
247+
added, err = s.host.messageCache.put(peer, msg)
248248
if err != nil {
249-
logger.Error("failed to encode message", "peer", peer, "error", err)
249+
logger.Error("failed to add message to cache", "peer", peer, "error", err)
250250
continue
251251
}
252252

253-
var added bool
254-
added, err = s.host.messageCache.Put(peer, encMsg)
255-
if err != nil || !added {
256-
logger.Error("failed to add message to cache", "peer", peer, "error", err)
253+
if !added {
257254
continue
258255
}
259256
}

0 commit comments

Comments
 (0)