From 7a3cb135d4bc24a41ed2a41bd47697138f00e18c Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Tue, 26 Sep 2023 10:57:21 +1000 Subject: [PATCH] Fix tests and add `BlockContents` decoding. Remove unused `builder_threshold` field in `ApiTesterConfig`. --- beacon_node/http_api/tests/tests.rs | 24 ++++++------- common/eth2/src/types.rs | 52 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index 47bfef3e2..8a6634f80 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -78,7 +78,6 @@ struct ApiTester { struct ApiTesterConfig { spec: ChainSpec, retain_historic_states: bool, - builder_threshold: Option, } impl Default for ApiTesterConfig { @@ -88,7 +87,6 @@ impl Default for ApiTesterConfig { Self { spec, retain_historic_states: false, - builder_threshold: None, } } } @@ -405,7 +403,6 @@ impl ApiTester { pub async fn new_mev_tester_no_builder_threshold() -> Self { let mut config = ApiTesterConfig { - builder_threshold: Some(0), retain_historic_states: false, spec: E::default_spec(), }; @@ -2585,18 +2582,22 @@ impl ApiTester { .unwrap() .expect("block bytes"); - let block = - BeaconBlock::>::from_ssz_bytes(&block_bytes, &self.chain.spec) - .expect("block bytes can be decoded"); + let block_contents = + BlockContents::>::from_ssz_bytes(&block_bytes, &self.chain.spec) + .expect("block contents bytes can be decoded"); - let signed_block = block.sign(&sk, &fork, genesis_validators_root, &self.chain.spec); + let signed_block_contents = + block_contents.sign(&sk, &fork, genesis_validators_root, &self.chain.spec); self.client - .post_beacon_blocks_ssz(&signed_block) + .post_beacon_blocks_ssz(&signed_block_contents) .await .unwrap(); - assert_eq!(self.chain.head_beacon_block().as_ref(), &signed_block); + assert_eq!( + self.chain.head_beacon_block().as_ref(), + signed_block_contents.signed_block() + ); self.chain.slot_clock.set_slot(slot.as_u64() + 1); } @@ -2800,7 +2801,7 @@ impl ApiTester { &block_contents_bytes, &self.chain.spec, ) - .expect("block bytes can be decoded"); + .expect("block contents bytes can be decoded"); let signed_block_contents = block_contents.sign(&sk, &fork, genesis_validators_root, &self.chain.spec); @@ -4135,7 +4136,6 @@ impl ApiTester { self.mock_builder .as_ref() .unwrap() - .builder .add_operation(Operation::Value(Uint256::from( DEFAULT_MOCK_EL_PAYLOAD_VALUE_WEI + 1, ))); @@ -5350,7 +5350,6 @@ async fn builder_payload_chosen_by_profit() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn builder_works_post_capella() { let mut config = ApiTesterConfig { - builder_threshold: Some(0), retain_historic_states: false, spec: E::default_spec(), }; @@ -5371,7 +5370,6 @@ async fn builder_works_post_capella() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn builder_works_post_deneb() { let mut config = ApiTesterConfig { - builder_threshold: Some(0), retain_historic_states: false, spec: E::default_spec(), }; diff --git a/common/eth2/src/types.rs b/common/eth2/src/types.rs index 32cffe56c..389b25a4a 100644 --- a/common/eth2/src/types.rs +++ b/common/eth2/src/types.rs @@ -1484,6 +1484,58 @@ pub type BlockContentsTuple = ( ); impl> BlockContents { + pub fn new( + block: BeaconBlock, + blobs: Option>, + ) -> Self { + match (Payload::block_type(), blobs) { + (BlockType::Full, Some(blobs)) => { + Self::BlockAndBlobSidecars(BeaconBlockAndBlobSidecars { + block: block, + blob_sidecars: blobs, + }) + } + (BlockType::Blinded, Some(blobs)) => { + Self::BlindedBlockAndBlobSidecars(BlindedBeaconBlockAndBlobSidecars { + blinded_block: block, + blinded_blob_sidecars: blobs, + }) + } + (_, None) => Self::Block(block), + } + } + + /// SSZ decode with fork variant determined by slot. + pub fn from_ssz_bytes(bytes: &[u8], spec: &ChainSpec) -> Result { + let slot_len = ::ssz_fixed_len(); + let slot_bytes = bytes + .get(0..slot_len) + .ok_or(DecodeError::InvalidByteLength { + len: bytes.len(), + expected: slot_len, + })?; + + let slot = Slot::from_ssz_bytes(slot_bytes)?; + let fork_at_slot = spec.fork_name_at_slot::(slot); + + match fork_at_slot { + ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => { + BeaconBlock::from_ssz_bytes(bytes, spec).map(|block| BlockContents::Block(block)) + } + ForkName::Deneb => { + let mut builder = ssz::SszDecoderBuilder::new(bytes); + builder.register_anonymous_variable_length_item()?; + builder.register_type::>()?; + + let mut decoder = builder.build()?; + let block = + decoder.decode_next_with(|bytes| BeaconBlock::from_ssz_bytes(bytes, spec))?; + let blobs = decoder.decode_next()?; + Ok(BlockContents::new(block, Some(blobs))) + } + } + } + pub fn block(&self) -> &BeaconBlock { match self { BlockContents::BlockAndBlobSidecars(block_and_sidecars) => &block_and_sidecars.block,