Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discv5: migrate to minilru #741

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eth.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ requires "nim >= 1.6.0",
"confutils",
"testutils",
"unittest2",
"results"
"results",
"minilru"

let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
Expand Down
45 changes: 4 additions & 41 deletions eth/p2p/discoveryv5/lru.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,9 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.
#

{.push raises: [].}

import std/[tables, lists], results

export results

type
LRUCache*[K, V] = object of RootObj
list: DoublyLinkedList[(K, V)] # Head is MRU k:v and tail is LRU k:v
table: Table[K, DoublyLinkedNode[(K, V)]] # DoublyLinkedNode is already ref
capacity: int

func init*[K, V](T: type LRUCache[K, V], capacity: int): LRUCache[K, V] =
LRUCache[K, V](capacity: capacity) # Table and list init is done default
{.deprecated: "Use minilru directly".}

func get*[K, V](lru: var LRUCache[K, V], key: K): Opt[V] =
let node = lru.table.getOrDefault(key, nil)
if node.isNil:
return Opt.none(V)

lru.list.remove(node)
lru.list.prepend(node)
return Opt.some(node.value[1])

func put*[K, V](lru: var LRUCache[K, V], key: K, value: V) =
let node = lru.table.getOrDefault(key, nil)
if not node.isNil:
lru.list.remove(node)
else:
if lru.table.len >= lru.capacity:
lru.table.del(lru.list.tail.value[0])
lru.list.remove(lru.list.tail)

lru.list.prepend((key, value))
lru.table[key] = lru.list.head

func del*[K, V](lru: var LRUCache[K, V], key: K) =
var node: DoublyLinkedNode[(K, V)]
if lru.table.pop(key, node):
lru.list.remove(node)
{.push raises: [].}

func len*[K, V](lru: LRUCache[K, V]): int =
lru.table.len
import minilru
export minilru
6 changes: 3 additions & 3 deletions eth/p2p/discoveryv5/sessions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import
std/net,
stint, stew/endians2,
node, lru
./node, minilru

export lru
export minilru

const
aesKeySize* = 128 div 8
Expand All @@ -28,7 +28,7 @@ type
AesKey* = array[aesKeySize, byte]
SessionKey* = array[keySize, byte]
SessionValue* = array[sizeof(AesKey) + sizeof(AesKey), byte]
Sessions* = LRUCache[SessionKey, SessionValue]
Sessions* = LruCache[SessionKey, SessionValue]

func makeKey(id: NodeId, address: Address): SessionKey =
var pos = 0
Expand Down
1 change: 0 additions & 1 deletion tests/p2p/all_discv5_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import
./test_enr,
./test_hkdf,
./test_lru,
./test_ip_vote,
./test_routing_table,
./test_discoveryv5_encoding,
Expand Down
134 changes: 0 additions & 134 deletions tests/p2p/test_lru.nim

This file was deleted.