-
Notifications
You must be signed in to change notification settings - Fork 200
/
gossip_cache.go
92 lines (80 loc) · 2.88 KB
/
gossip_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2017 The celo Authors
// This file is part of the celo library.
//
// The celo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The celo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the celo library. If not, see <http://www.gnu.org/licenses/>.
package istanbul
import (
lru "github.com/hashicorp/golang-lru"
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/log"
)
type GossipCache interface {
MarkMessageProcessedByPeer(peerNodeAddr common.Address, payload []byte)
CheckIfMessageProcessedByPeer(peerNodeAddr common.Address, payload []byte) bool
MarkMessageProcessedBySelf(payload []byte)
CheckIfMessageProcessedBySelf(payload []byte) bool
}
type LRUGossipCache struct {
messageCacheSize int
peerRecentMessages *lru.ARCCache // the cache of peer's recent messages
selfRecentMessages *lru.ARCCache // the cache of self recent messages
}
func NewLRUGossipCache(peerCacheSize, messageCacheSize int) *LRUGossipCache {
logger := log.New()
peerRecentMessages, err := lru.NewARC(peerCacheSize)
if err != nil {
logger.Crit("Failed to create recent messages cache", "err", err)
}
selfRecentMessages, err := lru.NewARC(messageCacheSize)
if err != nil {
logger.Crit("Failed to create known messages cache", "err", err)
}
return &LRUGossipCache{
messageCacheSize: messageCacheSize,
peerRecentMessages: peerRecentMessages,
selfRecentMessages: selfRecentMessages,
}
}
func (gc *LRUGossipCache) MarkMessageProcessedByPeer(peerNodeAddr common.Address, payload []byte) {
ms, ok := gc.peerRecentMessages.Get(peerNodeAddr)
var m *lru.ARCCache
if ok {
m, _ = ms.(*lru.ARCCache)
} else {
m, _ = lru.NewARC(gc.messageCacheSize)
gc.peerRecentMessages.Add(peerNodeAddr, m)
}
payloadHash := RLPHash(payload)
m.Add(payloadHash, true)
}
func (gc *LRUGossipCache) CheckIfMessageProcessedByPeer(peerNodeAddr common.Address, payload []byte) bool {
ms, ok := gc.peerRecentMessages.Get(peerNodeAddr)
var m *lru.ARCCache
if ok {
m, _ = ms.(*lru.ARCCache)
payloadHash := RLPHash(payload)
_, ok := m.Get(payloadHash)
return ok
}
return false
}
func (gc *LRUGossipCache) MarkMessageProcessedBySelf(payload []byte) {
payloadHash := RLPHash(payload)
gc.selfRecentMessages.Add(payloadHash, true)
}
func (gc *LRUGossipCache) CheckIfMessageProcessedBySelf(payload []byte) bool {
payloadHash := RLPHash(payload)
_, ok := gc.selfRecentMessages.Get(payloadHash)
return ok
}