Implemented method for interface.

pull/4455/head
frozen 1 year ago
parent 0e4568253a
commit a6ddc19fed
No known key found for this signature in database
GPG Key ID: 5391C63E79B03EDE
  1. 9
      internal/utils/blockedpeers/manager.go
  2. 12
      internal/utils/lrucache/lrucache.go
  3. 27
      internal/utils/lrucache/lrucache_test.go
  4. 4
      p2p/host.go

@ -18,6 +18,7 @@ func NewManager(size int) *Manager {
func (m *Manager) IsBanned(key libp2p_peer.ID, now time.Time) bool {
future, ok := m.internal.Get(key)
if ok {
return future.After(now) // future > now
}
@ -31,3 +32,11 @@ func (m *Manager) Ban(key libp2p_peer.ID, future time.Time) {
func (m *Manager) Contains(key libp2p_peer.ID) bool {
return m.internal.Contains(key)
}
func (m *Manager) Len() int {
return m.internal.Len()
}
func (m *Manager) Keys() []libp2p_peer.ID {
return m.internal.Keys()
}

@ -31,3 +31,15 @@ func (c *Cache[K, V]) Set(key K, value V) {
func (c *Cache[K, V]) Contains(key K) bool {
return c.cache.Contains(key)
}
func (c *Cache[K, V]) Len() int {
return c.cache.Len()
}
func (c *Cache[K, V]) Keys() []K {
out := make([]K, 0, c.cache.Len())
for _, v := range c.cache.Keys() {
out = append(out, v.(K))
}
return out
}

@ -0,0 +1,27 @@
package lrucache
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestKeys(t *testing.T) {
c := NewCache[int, int](10)
for i := 0; i < 3; i++ {
c.Set(i, i)
}
m := map[int]int{
0: 0,
1: 1,
2: 2,
}
keys := c.Keys()
m2 := map[int]int{}
for _, k := range keys {
m2[k] = k
}
require.Equal(t, m, m2)
}

@ -494,9 +494,7 @@ func (host *HostV2) ListPeer(topic string) []libp2p_peer.ID {
// ListBlockedPeer returns list of blocked peer
func (host *HostV2) ListBlockedPeer() []libp2p_peer.ID {
// TODO: this is a place holder for now
peers := make([]libp2p_peer.ID, 0)
return peers
return host.banned.Keys()
}
// GetPeerCount ...

Loading…
Cancel
Save