nimbus-eth2/tests/test_bitseqs.nim

164 lines
3.0 KiB
Nim
Raw Normal View History

{.used.}
import
unittest2,
std/[sequtils, strformat],
../beacon_chain/ssz/bitseqs,
./testutil
suite "Bit fields":
test "roundtrips BitArray":
var
a = BitArray[100]()
b = BitArray[100]()
c = BitArray[100]()
check:
not a[0]
a.setBit 1
check:
not a[0]
a[1]
toSeq(a.oneIndices()) == [1]
a + b == a
a - b == a
a - a == c # empty
b + a == a
b - b == c # b is empty
b.setBit 2
check:
(a + b)[2]
(b - a)[2]
not (b - a)[1]
a.setBit 99
check:
(a + b)[99]
(b - a)[2]
not (b - a)[1]
not (b - a)[99]
toSeq(a.oneIndices()) == [1, 99]
a.incl(b)
check:
not a[0]
a[1]
a[2]
a.clear()
check:
not a[1]
test "roundtrips BitSeq":
var
a = BitSeq.init(100)
b = BitSeq.init(100)
check:
not a[0]
Revamp attestation pool This is a revamp of the attestation pool that cleans up several aspects of attestation processing as the network grows larger and block space becomes more precious. The aim is to better exploit the divide between attestation subnets and aggregations by keeping the two kinds separate until it's time to either produce a block or aggregate. This means we're no longer eagerly combining single-vote attestations, but rather wait until the last moment, and then try to add singles to all aggregates, including those coming from the network. Importantly, the branch improves on poor aggregate quality and poor attestation packing in cases where block space is running out. A basic greed scoring mechanism is used to select attestations for blocks - attestations are added based on how much many new votes they bring to the table. * Collect single-vote attestations separately and store these until it's time to make aggregates * Create aggregates based on single-vote attestations * Select _best_ aggregate rather than _first_ aggregate when on aggregation duty * Top up all aggregates with singles when it's time make the attestation cut, thus improving the chances of grabbing the best aggregates out there * Improve aggregation test coverage * Improve bitseq operations * Simplify aggregate signature creation * Make attestation cache temporary instead of storing it in attestation pool - most of the time, blocks are not being produced, no need to keep the data around * Remove redundant aggregate storage that was used only for RPC * Use tables to avoid some linear seeks when looking up attestation data * Fix long cleanup on large slot jumps * Avoid some pointers * Speed up iterating all attestations for a slot (fixes #2490)
2021-04-12 20:25:09 +00:00
a.isZeros()
a.setBit 1
check:
not a[0]
a[1]
Revamp attestation pool This is a revamp of the attestation pool that cleans up several aspects of attestation processing as the network grows larger and block space becomes more precious. The aim is to better exploit the divide between attestation subnets and aggregations by keeping the two kinds separate until it's time to either produce a block or aggregate. This means we're no longer eagerly combining single-vote attestations, but rather wait until the last moment, and then try to add singles to all aggregates, including those coming from the network. Importantly, the branch improves on poor aggregate quality and poor attestation packing in cases where block space is running out. A basic greed scoring mechanism is used to select attestations for blocks - attestations are added based on how much many new votes they bring to the table. * Collect single-vote attestations separately and store these until it's time to make aggregates * Create aggregates based on single-vote attestations * Select _best_ aggregate rather than _first_ aggregate when on aggregation duty * Top up all aggregates with singles when it's time make the attestation cut, thus improving the chances of grabbing the best aggregates out there * Improve aggregation test coverage * Improve bitseq operations * Simplify aggregate signature creation * Make attestation cache temporary instead of storing it in attestation pool - most of the time, blocks are not being produced, no need to keep the data around * Remove redundant aggregate storage that was used only for RPC * Use tables to avoid some linear seeks when looking up attestation data * Fix long cleanup on large slot jumps * Avoid some pointers * Speed up iterating all attestations for a slot (fixes #2490)
2021-04-12 20:25:09 +00:00
a.countOnes() == 1
a.countZeros() == 99
not a.isZeros()
a.countOverlap(a) == 1
b.setBit 2
Revamp attestation pool This is a revamp of the attestation pool that cleans up several aspects of attestation processing as the network grows larger and block space becomes more precious. The aim is to better exploit the divide between attestation subnets and aggregations by keeping the two kinds separate until it's time to either produce a block or aggregate. This means we're no longer eagerly combining single-vote attestations, but rather wait until the last moment, and then try to add singles to all aggregates, including those coming from the network. Importantly, the branch improves on poor aggregate quality and poor attestation packing in cases where block space is running out. A basic greed scoring mechanism is used to select attestations for blocks - attestations are added based on how much many new votes they bring to the table. * Collect single-vote attestations separately and store these until it's time to make aggregates * Create aggregates based on single-vote attestations * Select _best_ aggregate rather than _first_ aggregate when on aggregation duty * Top up all aggregates with singles when it's time make the attestation cut, thus improving the chances of grabbing the best aggregates out there * Improve aggregation test coverage * Improve bitseq operations * Simplify aggregate signature creation * Make attestation cache temporary instead of storing it in attestation pool - most of the time, blocks are not being produced, no need to keep the data around * Remove redundant aggregate storage that was used only for RPC * Use tables to avoid some linear seeks when looking up attestation data * Fix long cleanup on large slot jumps * Avoid some pointers * Speed up iterating all attestations for a slot (fixes #2490)
2021-04-12 20:25:09 +00:00
a.incl(b)
check:
not a[0]
a[1]
a[2]
Revamp attestation pool This is a revamp of the attestation pool that cleans up several aspects of attestation processing as the network grows larger and block space becomes more precious. The aim is to better exploit the divide between attestation subnets and aggregations by keeping the two kinds separate until it's time to either produce a block or aggregate. This means we're no longer eagerly combining single-vote attestations, but rather wait until the last moment, and then try to add singles to all aggregates, including those coming from the network. Importantly, the branch improves on poor aggregate quality and poor attestation packing in cases where block space is running out. A basic greed scoring mechanism is used to select attestations for blocks - attestations are added based on how much many new votes they bring to the table. * Collect single-vote attestations separately and store these until it's time to make aggregates * Create aggregates based on single-vote attestations * Select _best_ aggregate rather than _first_ aggregate when on aggregation duty * Top up all aggregates with singles when it's time make the attestation cut, thus improving the chances of grabbing the best aggregates out there * Improve aggregation test coverage * Improve bitseq operations * Simplify aggregate signature creation * Make attestation cache temporary instead of storing it in attestation pool - most of the time, blocks are not being produced, no need to keep the data around * Remove redundant aggregate storage that was used only for RPC * Use tables to avoid some linear seeks when looking up attestation data * Fix long cleanup on large slot jumps * Avoid some pointers * Speed up iterating all attestations for a slot (fixes #2490)
2021-04-12 20:25:09 +00:00
a.countOverlap(a) == 2
a.countOverlap(b) == 1
b.countOverlap(a) == 1
b.countOverlap(b) == 1
a.clear()
check:
not a[1]
test "iterating words":
for bitCount in [8, 3, 7, 8, 14, 15, 16, 19, 260]:
checkpoint &"trying bit count {bitCount}"
var
a = BitSeq.init(bitCount)
b = BitSeq.init(bitCount)
bitsInWord = sizeof(uint) * 8
expectedWordCount = (bitCount div bitsInWord) + 1
for i in 0 ..< expectedWordCount:
let every3rdBit = i * sizeof(uint) * 8 + 2
a[every3rdBit] = true
b[every3rdBit] = true
for word in words(a):
check word == 4
word = 2
for wa, wb in words(a, b):
check wa == 2 and wb == 4
wa = 1
wb = 2
for i in 0 ..< expectedWordCount:
for j in 0 ..< bitsInWord:
let bitPos = i * bitsInWord + j
if bitPos < bitCount:
check a[j] == (j == 0)
check b[j] == (j == 1)
test "overlaps":
for bitCount in [1, 62, 63, 64, 91, 127, 128, 129]:
checkpoint &"trying bit count {bitCount}"
var
a = BitSeq.init(bitCount)
b = BitSeq.init(bitCount)
for pos in [4, 8, 9, 12, 29, 32, 63, 64, 67]:
if pos + 2 < bitCount:
a.setBit(pos)
b.setBit(pos + 2)
check:
not a.overlaps(b)
not b.overlaps(a)
Revamp attestation pool This is a revamp of the attestation pool that cleans up several aspects of attestation processing as the network grows larger and block space becomes more precious. The aim is to better exploit the divide between attestation subnets and aggregations by keeping the two kinds separate until it's time to either produce a block or aggregate. This means we're no longer eagerly combining single-vote attestations, but rather wait until the last moment, and then try to add singles to all aggregates, including those coming from the network. Importantly, the branch improves on poor aggregate quality and poor attestation packing in cases where block space is running out. A basic greed scoring mechanism is used to select attestations for blocks - attestations are added based on how much many new votes they bring to the table. * Collect single-vote attestations separately and store these until it's time to make aggregates * Create aggregates based on single-vote attestations * Select _best_ aggregate rather than _first_ aggregate when on aggregation duty * Top up all aggregates with singles when it's time make the attestation cut, thus improving the chances of grabbing the best aggregates out there * Improve aggregation test coverage * Improve bitseq operations * Simplify aggregate signature creation * Make attestation cache temporary instead of storing it in attestation pool - most of the time, blocks are not being produced, no need to keep the data around * Remove redundant aggregate storage that was used only for RPC * Use tables to avoid some linear seeks when looking up attestation data * Fix long cleanup on large slot jumps * Avoid some pointers * Speed up iterating all attestations for a slot (fixes #2490)
2021-04-12 20:25:09 +00:00
a.countOverlap(b) == 0
test "isZeros":
template carryOutTests(N: static int) =
var a = BitArray[N]()
check a.isZeros()
for i in 0 ..< N:
var b = a
b.setBit(i)
check(not b.isZeros())
carryOutTests(1)
carryOutTests(10)
carryOutTests(31)
carryOutTests(32)
carryOutTests(63)
carryOutTests(64)
carryOutTests(65)
carryOutTests(95)
carryOutTests(96)
carryOutTests(97)
carryOutTests(12494)