Skip to content

Commit

Permalink
add tests; update Bloom filter after each deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Feb 29, 2024
1 parent 1b140ef commit 6d03be7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
8 changes: 7 additions & 1 deletion AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ OK: 6/6 Fail: 0/6 Skip: 0/6
+ Dynamic validator set: updateDynamicValidators() test OK
```
OK: 4/4 Fail: 0/4 Skip: 0/4
## ValidatorPubKey Bloom filter
```diff
+ incremental construction with no false positives/negatives OK
+ one-shot construction with no false positives/negatives OK
```
OK: 2/2 Fail: 0/2 Skip: 0/2
## Zero signature sanity checks
```diff
+ SSZ serialization roundtrip of SignedBeaconBlockHeader OK
Expand Down Expand Up @@ -993,4 +999,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9

---TOTAL---
OK: 670/675 Fail: 0/675 Skip: 5/675
OK: 672/677 Fail: 0/677 Skip: 5/677
15 changes: 9 additions & 6 deletions beacon_chain/bloomfilter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import "."/spec/crypto

from stew/bitops2 import getBit, setBit
from "."/spec/datatypes/base import Validator, pubkey
from "."/spec/helpers import bytes_to_uint32

Expand All @@ -20,8 +21,6 @@ type
PubkeyBloomFilter* = object
bloomFilter: array[1 shl pubkeyBloomFilterScale, byte]

from stew/bitops2 import getBit, setBit

iterator bloomFilterHashes(pubkey: ValidatorPubKey): auto =
const pubkeyBloomFilterMask = (1 shl pubkeyBloomFilterScale) - 1
for r in countup(0'u32, 20'u32, 4'u32):
Expand All @@ -30,17 +29,21 @@ iterator bloomFilterHashes(pubkey: ValidatorPubKey): auto =
yield pubkey.blob.toOpenArray(r, r+3).bytes_to_uint32 and
pubkeyBloomFilterMask

func incl*(bloomFilter: var PubkeyBloomFilter, pubkey: ValidatorPubKey) =
for bloomFilterHash in bloomFilterHashes(pubkey):
setBit(bloomFilter.bloomFilter, bloomFilterHash)

func constructBloomFilter*(x: openArray[Validator]): auto =
let res = new PubkeyBloomFilter
for m in x:
for bloomFilterHash in bloomFilterHashes(m.pubkey):
setBit(res[].bloomFilter, bloomFilterHash)
incl(res[], m.pubkey)
res

func mightContain*(bf: PubkeyBloomFilter, pubkey: ValidatorPubKey): bool =
func mightContain*(
bloomFilter: PubkeyBloomFilter, pubkey: ValidatorPubKey): bool =
# Might return false positive, but never false negative
for bloomFilterHash in bloomFilterHashes(pubkey):
if not getBit(bf.bloomFilter, bloomFilterHash):
if not getBit(bloomFilter.bloomFilter, bloomFilterHash):
return false

true
3 changes: 2 additions & 1 deletion beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func findValidatorIndex*(state: ForkyBeaconState, pubkey: ValidatorPubKey):
return Opt[ValidatorIndex].ok((vidx - 1).ValidatorIndex)

from ".."/bloomfilter import
PubkeyBloomFilter, constructBloomFilter, mightContain
PubkeyBloomFilter, constructBloomFilter, incl, mightContain

proc process_deposit*(
cfg: RuntimeConfig, state: var ForkyBeaconState,
Expand Down Expand Up @@ -342,6 +342,7 @@ proc process_deposit*(
return err("process_deposit: too many validators (inactivity_scores)")

doAssert state.validators.len == state.balances.len
bloom_filter.incl pubkey
else:
# Deposits may come with invalid signatures - in that case, they are not
# turned into a validator but still get processed to keep the deposit
Expand Down
17 changes: 8 additions & 9 deletions tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

# All tests except scenarios, which as compiled separately for mainnet and minimal

{.push raises: [].}

import
./testutil

Expand All @@ -15,9 +17,11 @@ import # Unit test
./test_attestation_pool,
./test_beacon_chain_db,
./test_beacon_time,
./test_blockchain_dag,
./test_block_dag,
./test_block_processor,
./test_block_quarantine,
./test_bloom_filter,
./test_conf,
./test_datatypes,
./test_deposit_snapshots,
Expand All @@ -31,6 +35,8 @@ import # Unit test
./test_gossip_validation,
./test_helpers,
./test_honest_validator,
./test_keystore,
./test_keystore_management,
./test_key_splitting,
./test_light_client_processor,
./test_light_client,
Expand All @@ -54,14 +60,7 @@ import # Unit test
./slashing_protection/test_slashing_protection_db,
./test_validator_client

when not defined(i386):
# Avoids "Out of memory" CI failures
import
./test_blockchain_dag,
./test_keystore,
./test_keystore_management

when not defined(windows):
import ./test_keymanager_api
when not defined(windows):
import ./test_keymanager_api

summarizeLongTests("AllTests")

0 comments on commit 6d03be7

Please sign in to comment.