merge latest capella changes

This commit is contained in:
realbigsean 2023-01-16 09:17:18 -05:00
commit ddcd10b194
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
8 changed files with 70 additions and 25 deletions

View File

@ -91,9 +91,8 @@ impl Default for ChainConfig {
count_unrealized_full: CountUnrealizedFull::default(), count_unrealized_full: CountUnrealizedFull::default(),
checkpoint_sync_url_timeout: 60, checkpoint_sync_url_timeout: 60,
prepare_payload_lookahead: Duration::from_secs(4), prepare_payload_lookahead: Duration::from_secs(4),
// TODO(capella): disabled until withdrawal verification is implemented // This value isn't actually read except in tests.
// See: https://github.com/sigp/lighthouse/issues/3870 optimistic_finalized_sync: true,
optimistic_finalized_sync: false,
} }
} }
} }

View File

@ -1,4 +1,5 @@
use crate::{ use crate::{
json_structures::JsonWithdrawal,
keccak::{keccak256, KeccakHasher}, keccak::{keccak256, KeccakHasher},
metrics, Error, ExecutionLayer, metrics, Error, ExecutionLayer,
}; };
@ -6,8 +7,8 @@ use ethers_core::utils::rlp::RlpStream;
use keccak_hash::KECCAK_EMPTY_LIST_RLP; use keccak_hash::KECCAK_EMPTY_LIST_RLP;
use triehash::ordered_trie_root; use triehash::ordered_trie_root;
use types::{ use types::{
map_execution_block_header_fields, Address, EthSpec, ExecutionBlockHash, ExecutionBlockHeader, map_execution_block_header_fields_except_withdrawals, Address, EthSpec, ExecutionBlockHash,
ExecutionPayloadRef, Hash256, Hash64, Uint256, ExecutionBlockHeader, ExecutionPayloadRef, Hash256, Hash64, Uint256,
}; };
impl<T: EthSpec> ExecutionLayer<T> { impl<T: EthSpec> ExecutionLayer<T> {
@ -24,11 +25,23 @@ impl<T: EthSpec> ExecutionLayer<T> {
payload.transactions().iter().map(|txn_bytes| &**txn_bytes), payload.transactions().iter().map(|txn_bytes| &**txn_bytes),
); );
// Calculate withdrawals root (post-Capella).
let rlp_withdrawals_root = if let Ok(withdrawals) = payload.withdrawals() {
Some(ordered_trie_root::<KeccakHasher, _>(
withdrawals.iter().map(|withdrawal| {
rlp_encode_withdrawal(&JsonWithdrawal::from(withdrawal.clone()))
}),
))
} else {
None
};
// Construct the block header. // Construct the block header.
let exec_block_header = ExecutionBlockHeader::from_payload( let exec_block_header = ExecutionBlockHeader::from_payload(
payload, payload,
KECCAK_EMPTY_LIST_RLP.as_fixed_bytes().into(), KECCAK_EMPTY_LIST_RLP.as_fixed_bytes().into(),
rlp_transactions_root, rlp_transactions_root,
rlp_withdrawals_root,
); );
// Hash the RLP encoding of the block header. // Hash the RLP encoding of the block header.
@ -47,13 +60,27 @@ impl<T: EthSpec> ExecutionLayer<T> {
} }
} }
/// RLP encode a withdrawal.
pub fn rlp_encode_withdrawal(withdrawal: &JsonWithdrawal) -> Vec<u8> {
let mut rlp_stream = RlpStream::new();
rlp_stream.begin_list(4);
rlp_stream.append(&withdrawal.index);
rlp_stream.append(&withdrawal.validator_index);
rlp_stream.append(&withdrawal.address);
rlp_stream.append(&withdrawal.amount);
rlp_stream.out().into()
}
/// RLP encode an execution block header. /// RLP encode an execution block header.
pub fn rlp_encode_block_header(header: &ExecutionBlockHeader) -> Vec<u8> { pub fn rlp_encode_block_header(header: &ExecutionBlockHeader) -> Vec<u8> {
let mut rlp_header_stream = RlpStream::new(); let mut rlp_header_stream = RlpStream::new();
rlp_header_stream.begin_unbounded_list(); rlp_header_stream.begin_unbounded_list();
map_execution_block_header_fields!(&header, |_, field| { map_execution_block_header_fields_except_withdrawals!(&header, |_, field| {
rlp_header_stream.append(field); rlp_header_stream.append(field);
}); });
if let Some(withdrawals_root) = &header.withdrawals_root {
rlp_header_stream.append(withdrawals_root);
}
rlp_header_stream.finalize_unbounded_list(); rlp_header_stream.finalize_unbounded_list();
rlp_header_stream.out().into() rlp_header_stream.out().into()
} }
@ -99,6 +126,7 @@ mod test {
mix_hash: Hash256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(), mix_hash: Hash256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
nonce: Hash64::zero(), nonce: Hash64::zero(),
base_fee_per_gas: 0x036b_u64.into(), base_fee_per_gas: 0x036b_u64.into(),
withdrawals_root: None,
}; };
let expected_rlp = "f90200a0e0a94a7a3c9617401586b1a27025d2d9671332d22d540e0af72b069170380f2aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794ba5e000000000000000000000000000000000000a0ec3c94b18b8a1cff7d60f8d258ec723312932928626b4c9355eb4ab3568ec7f7a050f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accfa029b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200000188016345785d8a00008301553482079e42a0000000000000000000000000000000000000000000000000000000000000000088000000000000000082036b"; let expected_rlp = "f90200a0e0a94a7a3c9617401586b1a27025d2d9671332d22d540e0af72b069170380f2aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794ba5e000000000000000000000000000000000000a0ec3c94b18b8a1cff7d60f8d258ec723312932928626b4c9355eb4ab3568ec7f7a050f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accfa029b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200000188016345785d8a00008301553482079e42a0000000000000000000000000000000000000000000000000000000000000000088000000000000000082036b";
let expected_hash = let expected_hash =
@ -126,6 +154,7 @@ mod test {
mix_hash: Hash256::from_str("0000000000000000000000000000000000000000000000000000000000020000").unwrap(), mix_hash: Hash256::from_str("0000000000000000000000000000000000000000000000000000000000020000").unwrap(),
nonce: Hash64::zero(), nonce: Hash64::zero(),
base_fee_per_gas: 0x036b_u64.into(), base_fee_per_gas: 0x036b_u64.into(),
withdrawals_root: None,
}; };
let expected_rlp = "f901fda0927ca537f06c783a3a2635b8805eef1c8c2124f7444ad4a3389898dd832f2dbea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794ba5e000000000000000000000000000000000000a0e97859b065bd8dbbb4519c7cb935024de2484c2b7f881181b4360492f0b06b82a050f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accfa029b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800188016345785d8a00008301553482079e42a0000000000000000000000000000000000000000000000000000000000002000088000000000000000082036b"; let expected_rlp = "f901fda0927ca537f06c783a3a2635b8805eef1c8c2124f7444ad4a3389898dd832f2dbea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794ba5e000000000000000000000000000000000000a0e97859b065bd8dbbb4519c7cb935024de2484c2b7f881181b4360492f0b06b82a050f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accfa029b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800188016345785d8a00008301553482079e42a0000000000000000000000000000000000000000000000000000000000002000088000000000000000082036b";
let expected_hash = let expected_hash =
@ -154,6 +183,7 @@ mod test {
mix_hash: Hash256::from_str("bf5289894b2ceab3549f92f063febbac896b280ddb18129a57cff13113c11b13").unwrap(), mix_hash: Hash256::from_str("bf5289894b2ceab3549f92f063febbac896b280ddb18129a57cff13113c11b13").unwrap(),
nonce: Hash64::zero(), nonce: Hash64::zero(),
base_fee_per_gas: 0x34187b238_u64.into(), base_fee_per_gas: 0x34187b238_u64.into(),
withdrawals_root: None,
}; };
let expected_hash = let expected_hash =
Hash256::from_str("6da69709cd5a34079b6604d29cd78fc01dacd7c6268980057ad92a2bede87351") Hash256::from_str("6da69709cd5a34079b6604d29cd78fc01dacd7c6268980057ad92a2bede87351")

View File

@ -331,8 +331,8 @@ pub struct JsonWithdrawal {
#[serde(with = "eth2_serde_utils::u64_hex_be")] #[serde(with = "eth2_serde_utils::u64_hex_be")]
pub validator_index: u64, pub validator_index: u64,
pub address: Address, pub address: Address,
#[serde(with = "eth2_serde_utils::u256_hex_be")] #[serde(with = "eth2_serde_utils::u64_hex_be")]
pub amount: Uint256, pub amount: u64,
} }
impl From<Withdrawal> for JsonWithdrawal { impl From<Withdrawal> for JsonWithdrawal {
@ -341,21 +341,18 @@ impl From<Withdrawal> for JsonWithdrawal {
index: withdrawal.index, index: withdrawal.index,
validator_index: withdrawal.validator_index, validator_index: withdrawal.validator_index,
address: withdrawal.address, address: withdrawal.address,
amount: Uint256::from((withdrawal.amount as u128) * 1000000000u128), amount: withdrawal.amount,
} }
} }
} }
impl From<JsonWithdrawal> for Withdrawal { impl From<JsonWithdrawal> for Withdrawal {
fn from(jw: JsonWithdrawal) -> Self { fn from(jw: JsonWithdrawal) -> Self {
// This comparison is done to avoid a scenario where the EE gives us too large a number and we
// panic when attempting to cast to a `u64`.
let amount = std::cmp::max(jw.amount / 1000000000, Uint256::from(u64::MAX));
Self { Self {
index: jw.index, index: jw.index,
validator_index: jw.validator_index, validator_index: jw.validator_index,
address: jw.address, address: jw.address,
amount: amount.as_u64(), amount: jw.amount,
} }
} }
} }

View File

@ -1226,13 +1226,26 @@ impl<T: BeaconChainTypes> Worker<T> {
"peer" => %peer_id, "peer" => %peer_id,
"error" => ?e "error" => ?e
); );
self.propagate_validation_result(message_id, peer_id, MessageAcceptance::Reject); // We ignore pre-capella messages without penalizing peers.
// We penalize the peer slightly to prevent overuse of invalids. if matches!(e, BeaconChainError::BlsToExecutionChangeBadFork(_)) {
self.gossip_penalize_peer( self.propagate_validation_result(
peer_id, message_id,
PeerAction::HighToleranceError, peer_id,
"invalid_bls_to_execution_change", MessageAcceptance::Ignore,
); );
} else {
// We penalize the peer slightly to prevent overuse of invalids.
self.propagate_validation_result(
message_id,
peer_id,
MessageAcceptance::Reject,
);
self.gossip_penalize_peer(
peer_id,
PeerAction::HighToleranceError,
"invalid_bls_to_execution_change",
);
}
return; return;
} }
}; };

View File

@ -161,10 +161,9 @@ pub fn bls_execution_change_signature_set<'a, T: EthSpec>(
signed_address_change: &'a SignedBlsToExecutionChange, signed_address_change: &'a SignedBlsToExecutionChange,
spec: &'a ChainSpec, spec: &'a ChainSpec,
) -> Result<SignatureSet<'a>> { ) -> Result<SignatureSet<'a>> {
let domain = spec.get_domain( let domain = spec.compute_domain(
state.current_epoch(),
Domain::BlsToExecutionChange, Domain::BlsToExecutionChange,
&state.fork(), spec.genesis_fork_version,
state.genesis_validators_root(), state.genesis_validators_root(),
); );
let message = signed_address_change.message.signing_root(domain); let message = signed_address_change.message.signing_root(domain);

View File

@ -24,7 +24,9 @@ use metastruct::metastruct;
/// ///
/// Credit to Reth for the type definition. /// Credit to Reth for the type definition.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[metastruct(mappings(map_execution_block_header_fields()))] #[metastruct(mappings(map_execution_block_header_fields_except_withdrawals(exclude(
withdrawals_root
))))]
pub struct ExecutionBlockHeader { pub struct ExecutionBlockHeader {
pub parent_hash: Hash256, pub parent_hash: Hash256,
pub ommers_hash: Hash256, pub ommers_hash: Hash256,
@ -42,6 +44,7 @@ pub struct ExecutionBlockHeader {
pub mix_hash: Hash256, pub mix_hash: Hash256,
pub nonce: Hash64, pub nonce: Hash64,
pub base_fee_per_gas: Uint256, pub base_fee_per_gas: Uint256,
pub withdrawals_root: Option<Hash256>,
} }
impl ExecutionBlockHeader { impl ExecutionBlockHeader {
@ -49,6 +52,7 @@ impl ExecutionBlockHeader {
payload: ExecutionPayloadRef<E>, payload: ExecutionPayloadRef<E>,
rlp_empty_list_root: Hash256, rlp_empty_list_root: Hash256,
rlp_transactions_root: Hash256, rlp_transactions_root: Hash256,
rlp_withdrawals_root: Option<Hash256>,
) -> Self { ) -> Self {
// Most of these field mappings are defined in EIP-3675 except for `mixHash`, which is // Most of these field mappings are defined in EIP-3675 except for `mixHash`, which is
// defined in EIP-4399. // defined in EIP-4399.
@ -69,6 +73,7 @@ impl ExecutionBlockHeader {
mix_hash: payload.prev_randao(), mix_hash: payload.prev_randao(),
nonce: Hash64::zero(), nonce: Hash64::zero(),
base_fee_per_gas: payload.base_fee_per_gas(), base_fee_per_gas: payload.base_fee_per_gas(),
withdrawals_root: rlp_withdrawals_root,
} }
} }
} }

View File

@ -1,4 +1,4 @@
TESTS_TAG := v1.3.0-rc.0 TESTS_TAG := v1.3.0-rc.1
TESTS = general minimal mainnet TESTS = general minimal mainnet
TARBALLS = $(patsubst %,%-$(TESTS_TAG).tar.gz,$(TESTS)) TARBALLS = $(patsubst %,%-$(TESTS_TAG).tar.gz,$(TESTS))

View File

@ -39,6 +39,8 @@ excluded_paths = [
"tests/.*/.*/ssz_static/LightClientOptimistic", "tests/.*/.*/ssz_static/LightClientOptimistic",
# LightClientFinalityUpdate # LightClientFinalityUpdate
"tests/.*/.*/ssz_static/LightClientFinalityUpdate", "tests/.*/.*/ssz_static/LightClientFinalityUpdate",
# LightClientHeader
"tests/.*/.*/ssz_static/LightClientHeader",
# One of the EF researchers likes to pack the tarballs on a Mac # One of the EF researchers likes to pack the tarballs on a Mac
".*\.DS_Store.*", ".*\.DS_Store.*",
# More Mac weirdness. # More Mac weirdness.