Avoid unnecessary slashing protection when publishing blocks (#3188)

## Issue Addressed

#3141 

## Proposed Changes

Changes the algorithm for proposing blocks from

```
For each BN (first success):
   - Produce a block
   - Sign the block and store its root in the slashing protection DB
   - Publish the block
```
to
```
For each BN (first success):
   - Produce a block
Sign the block and store its root in the slashing protection DB
For each BN (first success):
   - Publish the block
```

Separating the producing from the publishing makes sure that we only add a signed block once to the slashing DB.
This commit is contained in:
tim gretler 2022-05-18 06:50:51 +00:00
parent 0428018cc1
commit 053625f113

View File

@ -328,7 +328,8 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
let self_ref = &self; let self_ref = &self;
let proposer_index = self.validator_store.validator_index(&validator_pubkey); let proposer_index = self.validator_store.validator_index(&validator_pubkey);
let validator_pubkey_ref = &validator_pubkey; let validator_pubkey_ref = &validator_pubkey;
let signed_block = self // Request block from first responsive beacon node.
let block = self
.beacon_nodes .beacon_nodes
.first_success(RequireSynced::No, |beacon_node| async move { .first_success(RequireSynced::No, |beacon_node| async move {
let get_timer = metrics::start_timer_vec( let get_timer = metrics::start_timer_vec(
@ -378,14 +379,19 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
)); ));
} }
Ok::<_, BlockError>(block)
})
.await?;
let signed_block = self_ref let signed_block = self_ref
.validator_store .validator_store
.sign_block::<Payload>(*validator_pubkey_ref, block, current_slot) .sign_block::<Payload>(*validator_pubkey_ref, block, current_slot)
.await .await
.map_err(|e| { .map_err(|e| BlockError::Recoverable(format!("Unable to sign block: {:?}", e)))?;
BlockError::Recoverable(format!("Unable to sign block: {:?}", e))
})?;
// Publish block with first available beacon node.
self.beacon_nodes
.first_success(RequireSynced::No, |beacon_node| async {
let _post_timer = metrics::start_timer_vec( let _post_timer = metrics::start_timer_vec(
&metrics::BLOCK_SERVICE_TIMES, &metrics::BLOCK_SERVICE_TIMES,
&[metrics::BEACON_BLOCK_HTTP_POST], &[metrics::BEACON_BLOCK_HTTP_POST],
@ -412,10 +418,6 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
})?, })?,
} }
Ok::<_, BlockError>(signed_block)
})
.await?;
info!( info!(
log, log,
"Successfully published block"; "Successfully published block";
@ -424,7 +426,9 @@ impl<T: SlotClock + 'static, E: EthSpec> BlockService<T, E> {
"graffiti" => ?graffiti.map(|g| g.as_utf8_lossy()), "graffiti" => ?graffiti.map(|g| g.as_utf8_lossy()),
"slot" => signed_block.slot().as_u64(), "slot" => signed_block.slot().as_u64(),
); );
Ok::<_, BlockError>(())
})
.await?;
Ok(()) Ok(())
} }
} }