MCPcopy Create free account
hub / github.com/encodeous/nylon / ValidateCounter

Method ValidateCounter

polyamide/replay/replay.go:37–62  ·  view source on GitHub ↗

ValidateCounter checks if the counter should be accepted. Overlimit counters (>= limit) are always rejected.

(counter, limit uint64)

Source from the content-addressed store, hash-verified

35// ValidateCounter checks if the counter should be accepted.
36// Overlimit counters (>= limit) are always rejected.
37func (f *Filter) ValidateCounter(counter, limit uint64) bool {
38 if counter >= limit {
39 return false
40 }
41 indexBlock := counter >> blockBitLog
42 if counter > f.last { // move window forward
43 current := f.last >> blockBitLog
44 diff := indexBlock - current
45 if diff > ringBlocks {
46 diff = ringBlocks // cap diff to clear the whole ring
47 }
48 for i := current + 1; i <= current+diff; i++ {
49 f.ring[i&blockMask] = 0
50 }
51 f.last = counter
52 } else if f.last-counter > windowSize { // behind current window
53 return false
54 }
55 // check and set bit
56 indexBlock &= blockMask
57 indexBit := counter & bitMask
58 old := f.ring[indexBlock]
59 new := old | 1<<indexBit
60 f.ring[indexBlock] = new
61 return old != new
62}

Callers 2

TestReplayFunction · 0.95

Calls

no outgoing calls

Tested by 1

TestReplayFunction · 0.76