2018-08-28 03:01:08 +00:00
|
|
|
package pubsub
|
2018-02-19 14:13:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-06 10:57:14 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-03-24 13:42:37 +00:00
|
|
|
pb "github.com/status-im/go-wakurelay-pubsub/pb"
|
2020-03-05 22:45:37 +00:00
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2018-02-19 14:13:18 +00:00
|
|
|
)
|
|
|
|
|
2019-10-06 10:57:14 +00:00
|
|
|
// NewMessageCache creates a sliding window cache that remembers messages for as
|
|
|
|
// long as `history` slots.
|
|
|
|
//
|
|
|
|
// When queried for messages to advertise, the cache only returns messages in
|
|
|
|
// the last `gossip` slots.
|
|
|
|
//
|
|
|
|
// The `gossip` parameter must be smaller or equal to `history`, or this
|
|
|
|
// function will panic.
|
|
|
|
//
|
|
|
|
// The slack between `gossip` and `history` accounts for the reaction time
|
|
|
|
// between when a message is advertised via IHAVE gossip, and the peer pulls it
|
|
|
|
// via an IWANT command.
|
2018-02-19 19:24:17 +00:00
|
|
|
func NewMessageCache(gossip, history int) *MessageCache {
|
2019-10-06 10:57:14 +00:00
|
|
|
if gossip > history {
|
|
|
|
err := fmt.Errorf("invalid parameters for message cache; gossip slots (%d) cannot be larger than history slots (%d)",
|
|
|
|
gossip, history)
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-02-20 11:56:23 +00:00
|
|
|
return &MessageCache{
|
|
|
|
msgs: make(map[string]*pb.Message),
|
2020-03-05 22:45:37 +00:00
|
|
|
peertx: make(map[string]map[peer.ID]int),
|
2018-02-20 11:56:23 +00:00
|
|
|
history: make([][]CacheEntry, history),
|
|
|
|
gossip: gossip,
|
2019-12-16 02:38:28 +00:00
|
|
|
msgID: DefaultMsgIdFn,
|
2018-02-20 11:56:23 +00:00
|
|
|
}
|
2018-02-19 14:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MessageCache struct {
|
2018-02-20 11:56:23 +00:00
|
|
|
msgs map[string]*pb.Message
|
2020-03-05 22:45:37 +00:00
|
|
|
peertx map[string]map[peer.ID]int
|
2018-02-20 11:56:23 +00:00
|
|
|
history [][]CacheEntry
|
|
|
|
gossip int
|
2019-12-16 02:38:28 +00:00
|
|
|
msgID MsgIdFunction
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:46:30 +00:00
|
|
|
func (mc *MessageCache) SetMsgIdFn(msgID MsgIdFunction) {
|
2019-12-16 02:38:28 +00:00
|
|
|
mc.msgID = msgID
|
2018-02-20 11:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CacheEntry struct {
|
2020-09-29 15:05:54 +00:00
|
|
|
mid string
|
|
|
|
topic string
|
2018-02-19 14:13:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:24:17 +00:00
|
|
|
func (mc *MessageCache) Put(msg *pb.Message) {
|
2019-12-16 02:38:28 +00:00
|
|
|
mid := mc.msgID(msg)
|
2018-02-20 11:56:23 +00:00
|
|
|
mc.msgs[mid] = msg
|
2020-09-29 15:05:54 +00:00
|
|
|
mc.history[0] = append(mc.history[0], CacheEntry{mid: mid, topic: msg.GetTopic()})
|
2018-02-19 14:13:18 +00:00
|
|
|
}
|
2018-02-19 16:45:10 +00:00
|
|
|
|
|
|
|
func (mc *MessageCache) Get(mid string) (*pb.Message, bool) {
|
2018-02-20 11:56:23 +00:00
|
|
|
m, ok := mc.msgs[mid]
|
|
|
|
return m, ok
|
2018-02-19 16:45:10 +00:00
|
|
|
}
|
2018-02-19 19:24:17 +00:00
|
|
|
|
2020-03-05 22:45:37 +00:00
|
|
|
func (mc *MessageCache) GetForPeer(mid string, p peer.ID) (*pb.Message, int, bool) {
|
|
|
|
m, ok := mc.msgs[mid]
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, ok := mc.peertx[mid]
|
|
|
|
if !ok {
|
|
|
|
tx = make(map[peer.ID]int)
|
|
|
|
mc.peertx[mid] = tx
|
|
|
|
}
|
|
|
|
tx[p]++
|
|
|
|
|
|
|
|
return m, tx[p], true
|
|
|
|
}
|
|
|
|
|
2018-02-20 10:08:18 +00:00
|
|
|
func (mc *MessageCache) GetGossipIDs(topic string) []string {
|
2018-02-20 11:56:23 +00:00
|
|
|
var mids []string
|
|
|
|
for _, entries := range mc.history[:mc.gossip] {
|
|
|
|
for _, entry := range entries {
|
2020-09-29 15:05:54 +00:00
|
|
|
if entry.topic == topic {
|
|
|
|
mids = append(mids, entry.mid)
|
2018-02-20 11:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mids
|
2018-02-20 10:08:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:24:17 +00:00
|
|
|
func (mc *MessageCache) Shift() {
|
2018-02-20 11:56:23 +00:00
|
|
|
last := mc.history[len(mc.history)-1]
|
|
|
|
for _, entry := range last {
|
|
|
|
delete(mc.msgs, entry.mid)
|
2020-03-05 22:45:37 +00:00
|
|
|
delete(mc.peertx, entry.mid)
|
2018-02-20 11:56:23 +00:00
|
|
|
}
|
|
|
|
for i := len(mc.history) - 2; i >= 0; i-- {
|
|
|
|
mc.history[i+1] = mc.history[i]
|
|
|
|
}
|
|
|
|
mc.history[0] = nil
|
2018-02-19 19:24:17 +00:00
|
|
|
}
|