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 # 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

View File

@ -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]