Update group_manager and tests

This commit is contained in:
stubbsta 2026-06-04 11:28:35 +02:00
parent 66218fae22
commit 2867ebb74f
No known key found for this signature in database
2 changed files with 11 additions and 18 deletions

View File

@ -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
# so we can't use it in this test.
const credentialCount = 5
const credentialCount = RlnContractRootCacheSize
let credentials = generateCredentials(credentialCount)
(waitFor manager.init()).isOkOr:
raiseAssert $error
@ -140,7 +140,7 @@ suite "Onchain group manager":
raiseAssert "Failed to fetch merkle root cache before: " & error
check:
merkleRootCacheBefore.len == 5 * 32
merkleRootCacheBefore.len == RlnContractRootCacheSize * 32
merkleRootCacheBefore.allIt(it == 0'u8)
manager.validRoots.len() == 0
@ -154,20 +154,20 @@ suite "Onchain group manager":
raiseAssert "Failed to fetch merkle root cache after: " & error
check:
merkleRootCacheAfter.len == 5 * 32
merkleRootCacheAfter.len == RlnContractRootCacheSize * 32
not merkleRootCacheAfter.allIt(it == 0'u8)
manager.validRoots.len() == credentialCount
manager.validRoots.items().toSeq().allIt(it != default(MerkleNode))
test "trackRootChanges: oldest roots are evicted once the window is exceeded":
const
initialCount = 5
additionalCount = 6
initialCount = AcceptableRootWindowSize - RlnContractRootCacheSize
additionalCount = RlnContractRootCacheSize + 1 # one more than the cache size to ensure eviction occurs
let credentials = generateCredentials(initialCount + additionalCount)
(waitFor manager.init()).isOkOr:
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:
(waitFor manager.register(credentials[i], UserMessageLimit(20))).isOkOr:
assert false, "Failed to register credential " & $i & ": " & error
@ -185,7 +185,7 @@ suite "Onchain group manager":
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.
check:
manager.validRoots.len() == AcceptableRootWindowSize

View File

@ -196,18 +196,11 @@ proc updateRecentRoots*(g: OnchainGroupManager): Future[bool] {.async.} =
return false
# 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
# Find the largest n (<= overlap) such that last n of validRoots == first n of newRootsDequeOrder
for n in countdown(overlap, 1):
var ok = true
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
for startIdx in (g.validRoots.len - overlap) ..< g.validRoots.len:
if g.validRoots[startIdx] == newRootsDequeOrder[0]:
matchLen = g.validRoots.len - startIdx
break
let toAdd = newRootsDequeOrder[matchLen ..< newRootsDequeOrder.len]