mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-06-06 22:19:30 +00:00
Update group_manager and tests
This commit is contained in:
parent
66218fae22
commit
2867ebb74f
@ -131,7 +131,7 @@ suite "Onchain group manager":
|
|||||||
# relies on a busy loop rather than event-based monitoring. but that busy loop fetch root every 5 seconds
|
# relies on a busy loop rather than event-based monitoring. but that busy loop fetch root every 5 seconds
|
||||||
# so we can't use it in this test.
|
# so we can't use it in this test.
|
||||||
|
|
||||||
const credentialCount = 5
|
const credentialCount = RlnContractRootCacheSize
|
||||||
let credentials = generateCredentials(credentialCount)
|
let credentials = generateCredentials(credentialCount)
|
||||||
(waitFor manager.init()).isOkOr:
|
(waitFor manager.init()).isOkOr:
|
||||||
raiseAssert $error
|
raiseAssert $error
|
||||||
@ -140,7 +140,7 @@ suite "Onchain group manager":
|
|||||||
raiseAssert "Failed to fetch merkle root cache before: " & error
|
raiseAssert "Failed to fetch merkle root cache before: " & error
|
||||||
|
|
||||||
check:
|
check:
|
||||||
merkleRootCacheBefore.len == 5 * 32
|
merkleRootCacheBefore.len == RlnContractRootCacheSize * 32
|
||||||
merkleRootCacheBefore.allIt(it == 0'u8)
|
merkleRootCacheBefore.allIt(it == 0'u8)
|
||||||
manager.validRoots.len() == 0
|
manager.validRoots.len() == 0
|
||||||
|
|
||||||
@ -154,20 +154,20 @@ suite "Onchain group manager":
|
|||||||
raiseAssert "Failed to fetch merkle root cache after: " & error
|
raiseAssert "Failed to fetch merkle root cache after: " & error
|
||||||
|
|
||||||
check:
|
check:
|
||||||
merkleRootCacheAfter.len == 5 * 32
|
merkleRootCacheAfter.len == RlnContractRootCacheSize * 32
|
||||||
not merkleRootCacheAfter.allIt(it == 0'u8)
|
not merkleRootCacheAfter.allIt(it == 0'u8)
|
||||||
manager.validRoots.len() == credentialCount
|
manager.validRoots.len() == credentialCount
|
||||||
manager.validRoots.items().toSeq().allIt(it != default(MerkleNode))
|
manager.validRoots.items().toSeq().allIt(it != default(MerkleNode))
|
||||||
|
|
||||||
test "trackRootChanges: oldest roots are evicted once the window is exceeded":
|
test "trackRootChanges: oldest roots are evicted once the window is exceeded":
|
||||||
const
|
const
|
||||||
initialCount = 5
|
initialCount = AcceptableRootWindowSize - RlnContractRootCacheSize
|
||||||
additionalCount = 6
|
additionalCount = RlnContractRootCacheSize + 1 # one more than the cache size to ensure eviction occurs
|
||||||
let credentials = generateCredentials(initialCount + additionalCount)
|
let credentials = generateCredentials(initialCount + additionalCount)
|
||||||
(waitFor manager.init()).isOkOr:
|
(waitFor manager.init()).isOkOr:
|
||||||
raiseAssert $error
|
raiseAssert $error
|
||||||
|
|
||||||
# Register the first 5 credentials and snapshot the 3 oldest roots.
|
# Register the first credentials and snapshot the 3 oldest roots.
|
||||||
for i in 0 ..< initialCount:
|
for i in 0 ..< initialCount:
|
||||||
(waitFor manager.register(credentials[i], UserMessageLimit(20))).isOkOr:
|
(waitFor manager.register(credentials[i], UserMessageLimit(20))).isOkOr:
|
||||||
assert false, "Failed to register credential " & $i & ": " & error
|
assert false, "Failed to register credential " & $i & ": " & error
|
||||||
@ -185,7 +185,7 @@ suite "Onchain group manager":
|
|||||||
|
|
||||||
let rootsAfter = manager.validRoots.items().toSeq()
|
let rootsAfter = manager.validRoots.items().toSeq()
|
||||||
|
|
||||||
# 51 registrations into a window of 50 evicts exactly the single oldest root,
|
# AcceptableRootWindowSize + 1 registrations evicts exactly the single oldest root,
|
||||||
# so only the first of the original three is gone; the other two remain.
|
# so only the first of the original three is gone; the other two remain.
|
||||||
check:
|
check:
|
||||||
manager.validRoots.len() == AcceptableRootWindowSize
|
manager.validRoots.len() == AcceptableRootWindowSize
|
||||||
|
|||||||
@ -196,18 +196,11 @@ proc updateRecentRoots*(g: OnchainGroupManager): Future[bool] {.async.} =
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
# Determine overlap with existing tail so we only append truly new roots
|
# Determine overlap with existing tail so we only append truly new roots
|
||||||
var overlap = min(g.validRoots.len, newRootsDequeOrder.len)
|
let overlap = min(g.validRoots.len, newRootsDequeOrder.len)
|
||||||
var matchLen = 0
|
var matchLen = 0
|
||||||
# Find the largest n (<= overlap) such that last n of validRoots == first n of newRootsDequeOrder
|
for startIdx in (g.validRoots.len - overlap) ..< g.validRoots.len:
|
||||||
for n in countdown(overlap, 1):
|
if g.validRoots[startIdx] == newRootsDequeOrder[0]:
|
||||||
var ok = true
|
matchLen = g.validRoots.len - startIdx
|
||||||
let startIdx = g.validRoots.len - n
|
|
||||||
for i in 0 ..< n:
|
|
||||||
if g.validRoots[startIdx + i] != newRootsDequeOrder[i]:
|
|
||||||
ok = false
|
|
||||||
break
|
|
||||||
if ok:
|
|
||||||
matchLen = n
|
|
||||||
break
|
break
|
||||||
|
|
||||||
let toAdd = newRootsDequeOrder[matchLen ..< newRootsDequeOrder.len]
|
let toAdd = newRootsDequeOrder[matchLen ..< newRootsDequeOrder.len]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user