Skip to content

Commit

Permalink
add database electra block tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Jul 1, 2024
1 parent 68eabc0 commit 3680740
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 10 deletions.
5 changes: 3 additions & 2 deletions AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ OK: 4/4 Fail: 0/4 Skip: 0/4
+ sanity check Deneb blocks [Preset: mainnet] OK
+ sanity check Deneb states [Preset: mainnet] OK
+ sanity check Deneb states, reusing buffers [Preset: mainnet] OK
+ sanity check Electra blocks [Preset: mainnet] OK
+ sanity check blobs [Preset: mainnet] OK
+ sanity check genesis roundtrip [Preset: mainnet] OK
+ sanity check phase 0 blocks [Preset: mainnet] OK
Expand All @@ -62,7 +63,7 @@ OK: 4/4 Fail: 0/4 Skip: 0/4
+ sanity check phase 0 states, reusing buffers [Preset: mainnet] OK
+ sanity check state diff roundtrip [Preset: mainnet] OK
```
OK: 25/25 Fail: 0/25 Skip: 0/25
OK: 26/26 Fail: 0/26 Skip: 0/26
## Beacon state [Preset: mainnet]
```diff
+ Smoke test initialize_beacon_state_from_eth1 [Preset: mainnet] OK
Expand Down Expand Up @@ -1032,4 +1033,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9

---TOTAL---
OK: 689/694 Fail: 0/694 Skip: 5/694
OK: 690/695 Fail: 0/695 Skip: 5/695
3 changes: 2 additions & 1 deletion beacon_chain/beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,8 @@ proc containsBlock*(

proc containsBlock*[
X: altair.TrustedSignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock | deneb.TrustedSignedBeaconBlock](
capella.TrustedSignedBeaconBlock | deneb.TrustedSignedBeaconBlock |
electra.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest, T: type X): bool =
db.blocks[X.kind].contains(key.data).expectDb()

Expand Down
1 change: 0 additions & 1 deletion beacon_chain/gossip_processing/gossip_validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,6 @@ proc validateAggregate*(
Future[Result[
tuple[attestingIndices: seq[ValidatorIndex], sig: CookedSig],
ValidationError]] {.async: (raises: [CancelledError]).} =
debugComment "is not"
template aggregate_and_proof: untyped = signedAggregateAndProof.message
template aggregate: untyped = aggregate_and_proof.aggregate

Expand Down
3 changes: 0 additions & 3 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ switch("warning", "CaseTransition:off")
# do its (N)RVO pass: https://github.com/nim-lang/RFCs/issues/230
switch("warning", "ObservableStores:off")

# Too many false positives for "Warning: method has lock level <unknown>, but another method has 0 [LockLevel]"
switch("warning", "LockLevel:off")

# Too many right now to read compiler output. Warnings are legitimate, but
# should be fixed out-of-band of `unstable` branch.
switch("warning", "BareExcept:off")
Expand Down
2 changes: 2 additions & 0 deletions ncli/ncli_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ proc cmdDumpState(conf: DbConf) =
bellatrixState = (ref bellatrix.HashedBeaconState)()
capellaState = (ref capella.HashedBeaconState)()
denebState = (ref deneb.HashedBeaconState)()
electraState = (ref electra.HashedBeaconState)()

for stateRoot in conf.stateRoot:
if shouldShutDown: quit QuitSuccess
Expand All @@ -401,6 +402,7 @@ proc cmdDumpState(conf: DbConf) =
doit(bellatrixState[])
doit(capellaState[])
doit(denebState[])
doit(electraState[])

echo "Couldn't load ", stateRoot

Expand Down
68 changes: 67 additions & 1 deletion tests/test_beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func withDigest(blck: deneb.TrustedBeaconBlock):
root: hash_tree_root(blck)
)

func withDigest(blck: electra.TrustedBeaconBlock):
electra.TrustedSignedBeaconBlock =
electra.TrustedSignedBeaconBlock(
message: blck,
root: hash_tree_root(blck)
)

proc getTestStates(consensusFork: ConsensusFork): auto =
let
db = makeTestDB(SLOTS_PER_EPOCH)
Expand All @@ -113,7 +120,7 @@ proc getTestStates(consensusFork: ConsensusFork): auto =

testStates

debugComment "add some electra states, and test electra state/block loading/etc"
debugComment "add some electra states, and test electra state loading/etc"

# Each set of states gets used twice, so scope them to module
let
Expand Down Expand Up @@ -153,6 +160,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, phase0.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, phase0.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, phase0.TrustedSignedBeaconBlock)
Expand All @@ -168,6 +176,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, phase0.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, phase0.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, phase0.TrustedSignedBeaconBlock)
Expand Down Expand Up @@ -200,6 +209,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, altair.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, altair.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, altair.TrustedSignedBeaconBlock)
Expand All @@ -215,6 +225,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, altair.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, altair.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, altair.TrustedSignedBeaconBlock)
Expand Down Expand Up @@ -247,6 +258,7 @@ suite "Beacon chain DB" & preset():
db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, bellatrix.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, bellatrix.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, bellatrix.TrustedSignedBeaconBlock)
Expand All @@ -262,6 +274,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, bellatrix.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, bellatrix.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, bellatrix.TrustedSignedBeaconBlock)
Expand Down Expand Up @@ -293,6 +306,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.containsBlock(root, capella.TrustedSignedBeaconBlock)
db.getBlock(root, capella.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, capella.TrustedSignedBeaconBlock)
Expand All @@ -309,6 +323,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, capella.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, capella.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, capella.TrustedSignedBeaconBlock)
Expand Down Expand Up @@ -341,6 +356,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, deneb.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, deneb.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, deneb.TrustedSignedBeaconBlock)
Expand All @@ -356,6 +372,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, deneb.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, deneb.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, deneb.TrustedSignedBeaconBlock)
Expand All @@ -371,6 +388,55 @@ suite "Beacon chain DB" & preset():

db.close()

test "sanity check Electra blocks" & preset():
let db = BeaconChainDB.new("", inMemory = true)

let
signedBlock = withDigest((electra.TrustedBeaconBlock)())
root = hash_tree_root(signedBlock.message)

db.putBlock(signedBlock)

var tmp, tmp2: seq[byte]
check:
db.containsBlock(root)
not db.containsBlock(root, phase0.TrustedSignedBeaconBlock)
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, electra.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, electra.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, electra.TrustedSignedBeaconBlock)
tmp == SSZ.encode(signedBlock)
tmp2 == encodeFramed(tmp)
uncompressedLenFramed(tmp2).isSome

check:
db.delBlock(ConsensusFork.Electra, root)
not db.containsBlock(root)
not db.containsBlock(root, phase0.TrustedSignedBeaconBlock)
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, deneb.TrustedSignedBeaconBlock)
not db.containsBlock(root, electra.TrustedSignedBeaconBlock)
db.getBlock(root, electra.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, electra.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, electra.TrustedSignedBeaconBlock)

db.putStateRoot(root, signedBlock.message.slot, root)
var root2 = root
root2.data[0] = root.data[0] + 1
db.putStateRoot(root, signedBlock.message.slot + 1, root2)

check:
db.getStateRoot(root, signedBlock.message.slot).get() == root
db.getStateRoot(root, signedBlock.message.slot + 1).get() == root2

db.close()

test "sanity check phase 0 states" & preset():
let db = makeTestDB(SLOTS_PER_EPOCH)

Expand Down
2 changes: 0 additions & 2 deletions tests/testblockutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ proc addTestBlock*(
cfg, state, getStateField(state, slot) + 1, cache, info, flags).expect(
"can advance 1")

debugComment "add consolidations support to addTestBlock"

let
proposer_index = get_beacon_proposer_index(
state, cache, getStateField(state, slot)).expect("valid proposer index")
Expand Down

0 comments on commit 3680740

Please sign in to comment.