2019-12-19 06:19:15 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-12-19 06:19:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type blockReceiptTracker struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
|
|
|
|
// using an LRU cache because i don't want to handle all the edge cases for
|
|
|
|
// manual cleanup and maintenance of a fixed size set
|
|
|
|
cache *lru.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerSet struct {
|
|
|
|
peers map[peer.ID]time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlockReceiptTracker() *blockReceiptTracker {
|
|
|
|
c, _ := lru.New(512)
|
|
|
|
return &blockReceiptTracker{
|
|
|
|
cache: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (brt *blockReceiptTracker) Add(p peer.ID, ts *types.TipSet) {
|
|
|
|
brt.lk.Lock()
|
|
|
|
defer brt.lk.Unlock()
|
|
|
|
|
|
|
|
val, ok := brt.cache.Get(ts.Key())
|
|
|
|
if !ok {
|
|
|
|
pset := &peerSet{
|
|
|
|
peers: map[peer.ID]time.Time{
|
2020-07-10 14:43:14 +00:00
|
|
|
p: build.Clock.Now(),
|
2019-12-19 06:19:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
brt.cache.Add(ts.Key(), pset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:43:14 +00:00
|
|
|
val.(*peerSet).peers[p] = build.Clock.Now()
|
2019-12-19 06:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (brt *blockReceiptTracker) GetPeers(ts *types.TipSet) []peer.ID {
|
|
|
|
brt.lk.Lock()
|
|
|
|
defer brt.lk.Unlock()
|
|
|
|
|
|
|
|
val, ok := brt.cache.Get(ts.Key())
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := val.(*peerSet)
|
|
|
|
|
|
|
|
out := make([]peer.ID, 0, len(ps.peers))
|
|
|
|
for p := range ps.peers {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return ps.peers[out[i]].Before(ps.peers[out[j]])
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|