add EIP4844 block database read/write test (#4416)

This commit is contained in:
tersec 2022-12-13 00:56:50 +00:00 committed by GitHub
parent 2b7e2499d9
commit bc996623e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 11 deletions

View File

@ -36,6 +36,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3
+ sanity check Bellatrix states [Preset: mainnet] OK
+ sanity check Bellatrix states, reusing buffers [Preset: mainnet] OK
+ sanity check Capella blocks [Preset: mainnet] OK
+ sanity check EIP4844 blocks [Preset: mainnet] OK
+ sanity check genesis roundtrip [Preset: mainnet] OK
+ sanity check phase 0 blocks [Preset: mainnet] OK
+ sanity check phase 0 getState rollback [Preset: mainnet] OK
@ -43,7 +44,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3
+ sanity check phase 0 states, reusing buffers [Preset: mainnet] OK
+ sanity check state diff roundtrip [Preset: mainnet] OK
```
OK: 17/17 Fail: 0/17 Skip: 0/17
OK: 18/18 Fail: 0/18 Skip: 0/18
## Beacon state [Preset: mainnet]
```diff
+ Smoke test initialize_beacon_state_from_eth1 [Preset: mainnet] OK
@ -608,4 +609,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9
---TOTAL---
OK: 337/342 Fail: 0/342 Skip: 5/342
OK: 338/343 Fail: 0/343 Skip: 5/343

View File

@ -938,7 +938,8 @@ proc getBlock*(
result.err()
proc getBlock*[
X: bellatrix.TrustedSignedBeaconBlock | capella.TrustedSignedBeaconBlock | eip4844.TrustedSignedBeaconBlock](
X: bellatrix.TrustedSignedBeaconBlock | capella.TrustedSignedBeaconBlock |
eip4844.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest,
T: type X): Opt[T] =
# We only store blocks that we trust in the database
@ -1287,7 +1288,7 @@ proc containsBlock*(
proc containsBlock*[
X: altair.TrustedSignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock](
capella.TrustedSignedBeaconBlock | eip4844.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest, T: type X): bool =
db.blocks[X.toFork].contains(key.data).expectDb()
@ -1298,7 +1299,8 @@ proc containsBlock*(db: BeaconChainDB, key: Eth2Digest, fork: BeaconBlockFork):
proc containsBlock*(db: BeaconChainDB, key: Eth2Digest): bool =
static: doAssert high(BeaconBlockFork) == BeaconBlockFork.EIP4844
db.containsBlock(key, capella.TrustedSignedBeaconBlock) or
db.containsBlock(key, eip4844.TrustedSignedBeaconBlock) or
db.containsBlock(key, capella.TrustedSignedBeaconBlock) or
db.containsBlock(key, bellatrix.TrustedSignedBeaconBlock) or
db.containsBlock(key, altair.TrustedSignedBeaconBlock) or
db.containsBlock(key, phase0.TrustedSignedBeaconBlock)

View File

@ -791,12 +791,15 @@ func tx_peek_blob_versioned_hashes(opaque_tx: Transaction):
## `blob_versioned_hashes` offset calculation.
if not (opaque_tx[0] == BLOB_TX_TYPE):
return err("tx_peek_blob_versioned_hashes: invalid opaque transaction type")
let
message_offset = 1 + bytes_to_uint32(opaque_tx.asSeq.toOpenArray(1, 4))
# field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 + 32 = 188
blob_versioned_hashes_offset = (
message_offset + bytes_to_uint32(opaque_tx[(message_offset + 188) .. (message_offset + 191)])
)
let message_offset = 1 + bytes_to_uint32(opaque_tx.asSeq.toOpenArray(1, 4))
if opaque_tx.lenu64 < (message_offset + 192).uint64:
return err("tx_peek_blob_versioned_hashes: opaque transaction too short")
# field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 + 32 = 188
let blob_versioned_hashes_offset = (
message_offset + bytes_to_uint32(
opaque_tx[(message_offset + 188) ..< (message_offset + 192)]))
if blob_versioned_hashes_offset.uint64 > high(int).uint64:
return err("tx_peek_blob_versioned_hashes: blob_versioned_hashes_offset too high")

View File

@ -52,6 +52,15 @@ proc getCapellaStateRef(db: BeaconChainDB, root: Eth2Digest):
if db.getState(root, res[], noRollback):
return res
from ../beacon_chain/spec/datatypes/eip4844 import TrustedSignedBeaconBlock
proc getEIP4844StateRef(db: BeaconChainDB, root: Eth2Digest):
eip4844.NilableBeaconStateRef =
# load beaconstate the way the block pool does it - into an existing instance
let res = (eip4844.BeaconStateRef)()
if db.getState(root, res[], noRollback):
return res
func withDigest(blck: phase0.TrustedBeaconBlock):
phase0.TrustedSignedBeaconBlock =
phase0.TrustedSignedBeaconBlock(
@ -80,6 +89,13 @@ func withDigest(blck: capella.TrustedBeaconBlock):
root: hash_tree_root(blck)
)
func withDigest(blck: eip4844.TrustedBeaconBlock):
eip4844.TrustedSignedBeaconBlock =
eip4844.TrustedSignedBeaconBlock(
message: blck,
root: hash_tree_root(blck)
)
proc getTestStates(stateFork: BeaconStateFork): auto =
let
db = makeTestDB(SLOTS_PER_EPOCH)
@ -124,6 +140,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, phase0.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, phase0.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, phase0.TrustedSignedBeaconBlock)
@ -138,6 +155,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, phase0.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, phase0.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, phase0.TrustedSignedBeaconBlock)
@ -169,6 +187,7 @@ suite "Beacon chain DB" & preset():
db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, altair.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, altair.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, altair.TrustedSignedBeaconBlock)
@ -183,6 +202,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, altair.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, altair.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, altair.TrustedSignedBeaconBlock)
@ -214,6 +234,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, bellatrix.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, bellatrix.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, bellatrix.TrustedSignedBeaconBlock)
@ -228,6 +249,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, bellatrix.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, bellatrix.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, bellatrix.TrustedSignedBeaconBlock)
@ -258,6 +280,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, phase0.TrustedSignedBeaconBlock)
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.containsBlock(root, capella.TrustedSignedBeaconBlock)
db.getBlock(root, capella.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, capella.TrustedSignedBeaconBlock)
@ -273,6 +296,7 @@ suite "Beacon chain DB" & preset():
not db.containsBlock(root, altair.TrustedSignedBeaconBlock)
not db.containsBlock(root, bellatrix.TrustedSignedBeaconBlock)
not db.containsBlock(root, capella.TrustedSignedBeaconBlock)
not db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, capella.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, capella.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, capella.TrustedSignedBeaconBlock)
@ -288,6 +312,53 @@ suite "Beacon chain DB" & preset():
db.close()
test "sanity check EIP4844 blocks" & preset():
let db = BeaconChainDB.new("", inMemory = true)
let
signedBlock = withDigest((eip4844.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)
db.containsBlock(root, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, eip4844.TrustedSignedBeaconBlock).get() == signedBlock
db.getBlockSSZ(root, tmp, eip4844.TrustedSignedBeaconBlock)
db.getBlockSZ(root, tmp2, eip4844.TrustedSignedBeaconBlock)
tmp == SSZ.encode(signedBlock)
tmp2 == encodeFramed(tmp)
uncompressedLenFramed(tmp2).isSome
db.delBlock(root)
check:
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, eip4844.TrustedSignedBeaconBlock)
db.getBlock(root, eip4844.TrustedSignedBeaconBlock).isErr()
not db.getBlockSSZ(root, tmp, eip4844.TrustedSignedBeaconBlock)
not db.getBlockSZ(root, tmp2, eip4844.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)