Skip to content

Commit 0d051b3

Browse files
authored
add Deneb support to REST (de)serialization (#4770)
1 parent aa04d45 commit 0d051b3

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,17 @@ proc readValue*[BlockType: ForkedBeaconBlock](
10031003
reader.raiseUnexpectedValue("Incorrect capella block format")
10041004
value = ForkedBeaconBlock.init(res.get()).BlockType
10051005
of ConsensusFork.Deneb:
1006-
reader.raiseUnexpectedValue($denebImplementationMissing)
1006+
let res =
1007+
try:
1008+
some(RestJson.decode(string(data.get()),
1009+
deneb.BeaconBlock,
1010+
requireAllFields = true,
1011+
allowUnknownFields = true))
1012+
except SerializationError:
1013+
none[deneb.BeaconBlock]()
1014+
if res.isNone():
1015+
reader.raiseUnexpectedValue("Incorrect deneb block format")
1016+
value = ForkedBeaconBlock.init(res.get()).BlockType
10071017

10081018
proc readValue*[BlockType: ForkedBlindedBeaconBlock](
10091019
reader: var JsonReader[RestJson],
@@ -1066,7 +1076,17 @@ proc readValue*[BlockType: ForkedBlindedBeaconBlock](
10661076
value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Capella,
10671077
capellaData: res)
10681078
of ConsensusFork.Deneb:
1069-
reader.raiseUnexpectedValue($denebImplementationMissing)
1079+
let res =
1080+
try:
1081+
RestJson.decode(string(data.get()),
1082+
capella_mev.BlindedBeaconBlock,
1083+
requireAllFields = true,
1084+
allowUnknownFields = true)
1085+
except SerializationError as exc:
1086+
reader.raiseUnexpectedValue("Incorrect deneb block format, [" &
1087+
exc.formatMsg("BlindedBlock") & "]")
1088+
value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Deneb,
1089+
denebData: res)
10701090

10711091
proc readValue*[BlockType: Web3SignerForkedBeaconBlock](
10721092
reader: var JsonReader[RestJson],
@@ -1136,7 +1156,19 @@ proc readValue*[BlockType: Web3SignerForkedBeaconBlock](
11361156
kind: ConsensusFork.Capella,
11371157
capellaData: res.get())
11381158
of ConsensusFork.Deneb:
1139-
reader.raiseUnexpectedValue($denebImplementationMissing)
1159+
let res =
1160+
try:
1161+
some(RestJson.decode(string(data.get()),
1162+
BeaconBlockHeader,
1163+
requireAllFields = true,
1164+
allowUnknownFields = true))
1165+
except SerializationError:
1166+
none[BeaconBlockHeader]()
1167+
if res.isNone():
1168+
reader.raiseUnexpectedValue("Incorrect deneb block format")
1169+
value = Web3SignerForkedBeaconBlock(
1170+
kind: ConsensusFork.Deneb,
1171+
denebData: res.get())
11401172

11411173
proc writeValue*[
11421174
BlockType: Web3SignerForkedBeaconBlock](
@@ -1493,7 +1525,15 @@ proc readValue*(reader: var JsonReader[RestJson],
14931525
)
14941526
)
14951527
of ConsensusFork.Deneb:
1496-
reader.raiseUnexpectedValue($denebImplementationMissing)
1528+
ForkedBeaconBlock.init(
1529+
deneb.BeaconBlock(
1530+
slot: slot.get(),
1531+
proposer_index: proposer_index.get(),
1532+
parent_root: parent_root.get(),
1533+
state_root: state_root.get(),
1534+
body: body.denebBody
1535+
)
1536+
)
14971537
)
14981538

14991539
## RestPublishedSignedBeaconBlock
@@ -1554,7 +1594,12 @@ proc readValue*(reader: var JsonReader[RestJson],
15541594
)
15551595
)
15561596
of ConsensusFork.Deneb:
1557-
reader.raiseUnexpectedValue($denebImplementationMissing)
1597+
ForkedSignedBeaconBlock.init(
1598+
deneb.SignedBeaconBlock(
1599+
message: blck.denebData,
1600+
signature: signature.get()
1601+
)
1602+
)
15581603
)
15591604

15601605
## ForkedSignedBeaconBlock
@@ -1648,7 +1693,17 @@ proc readValue*(reader: var JsonReader[RestJson],
16481693
reader.raiseUnexpectedValue("Incorrect capella block format")
16491694
value = ForkedSignedBeaconBlock.init(res.get())
16501695
of ConsensusFork.Deneb:
1651-
reader.raiseUnexpectedValue($denebImplementationMissing)
1696+
let res =
1697+
try:
1698+
some(RestJson.decode(string(data.get()),
1699+
deneb.SignedBeaconBlock,
1700+
requireAllFields = true,
1701+
allowUnknownFields = true))
1702+
except SerializationError:
1703+
none[deneb.SignedBeaconBlock]()
1704+
if res.isNone():
1705+
reader.raiseUnexpectedValue("Incorrect deneb block format")
1706+
value = ForkedSignedBeaconBlock.init(res.get())
16521707
withBlck(value):
16531708
blck.root = hash_tree_root(blck.message)
16541709

@@ -2731,7 +2786,14 @@ proc decodeBody*(
27312786
return err("Unexpected deserialization error")
27322787
ok(RestPublishedSignedBeaconBlock(ForkedSignedBeaconBlock.init(blck)))
27332788
of ConsensusFork.Deneb:
2734-
return err($denebImplementationMissing)
2789+
let blck =
2790+
try:
2791+
SSZ.decode(body.data, deneb.SignedBeaconBlock)
2792+
except SerializationError:
2793+
return err("Unable to deserialize data")
2794+
except CatchableError:
2795+
return err("Unexpected deserialization error")
2796+
ok(RestPublishedSignedBeaconBlock(ForkedSignedBeaconBlock.init(blck)))
27352797
else:
27362798
return err("Unsupported or invalid content media type")
27372799

0 commit comments

Comments
 (0)