Skip to content

Commit

Permalink
Implemented method for interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen committed Jul 5, 2023
1 parent ea815e6 commit 02a8df4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
9 changes: 9 additions & 0 deletions internal/utils/blockedpeers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}
12 changes: 12 additions & 0 deletions internal/utils/lrucache/lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions internal/utils/lrucache/lrucache_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 1 addition & 3 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,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 ...
Expand Down

0 comments on commit 02a8df4

Please sign in to comment.