Skip to content

Commit 756936b

Browse files
committed
Fix broken tests
1 parent 59ecec1 commit 756936b

3 files changed

Lines changed: 46 additions & 31 deletions

File tree

contracts/near/eth-types/src/eth2.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub struct ExecutionPayloadHeader {
134134
#[cfg_attr(not(target_arch = "wasm32"), serde(with = "serde_utils::quoted_u64"))]
135135
pub timestamp: u64,
136136
pub extra_data: ExtraData,
137+
#[cfg_attr(not(target_arch = "wasm32"), serde(with = "quoted_u256"))]
137138
pub base_fee_per_gas: U256,
138139
pub block_hash: H256,
139140
pub transactions_root: H256,
@@ -261,4 +262,28 @@ pub mod optional_quoted_u64 {
261262
None => Ok(None),
262263
}
263264
}
265+
}
266+
267+
/// Serde module for handling `U256` with quotes
268+
pub mod quoted_u256 {
269+
use crate::U256;
270+
use serde::{Deserialize, Deserializer, Serializer};
271+
272+
pub fn serialize<S>(value: &U256, serializer: S) -> Result<S::Ok, S::Error>
273+
where
274+
S: Serializer,
275+
{
276+
serializer.serialize_str(&format!("{}", value))
277+
}
278+
279+
pub fn deserialize<'de, D>(deserializer: D) -> Result<U256, D::Error>
280+
where
281+
D: Deserializer<'de>,
282+
{
283+
let s: String = String::deserialize(deserializer)?;
284+
// Parse as decimal (base 10)
285+
ethereum_types::U256::from_dec_str(&s)
286+
.map(U256)
287+
.map_err(|e| serde::de::Error::custom(format!("failed to parse U256 from decimal: {}", e)))
288+
}
264289
}

contracts/near/eth-types/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,27 @@ macro_rules! uint_declare_wrapper_and_serde {
141141

142142
impl tree_hash::TreeHash for $name {
143143
fn tree_hash_type() -> tree_hash::TreeHashType {
144-
tree_hash::TreeHashType::Vector
144+
tree_hash::TreeHashType::Basic
145145
}
146146

147147
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
148-
unreachable!("Vector should never be packed.")
148+
let mut bytes = [0u8; 32];
149+
self.0.to_little_endian(&mut bytes);
150+
PackedEncoding::from(bytes)
149151
}
150152

151153
fn tree_hash_packing_factor() -> usize {
152-
unreachable!("Vector should never be packed.")
154+
1
153155
}
154156

155157
fn tree_hash_root(&self) -> tree_hash::Hash256 {
156-
let values_per_chunk = tree_hash::BYTES_PER_CHUNK;
157-
let minimum_chunk_count = ($len + values_per_chunk - 1) / values_per_chunk;
158-
let mut bytes = vec![0u8; $len * 8];
159-
self.0.to_big_endian(&mut bytes);
160-
tree_hash::merkle_root(&bytes, minimum_chunk_count)
158+
let mut bytes = [0u8; 32];
159+
self.0.to_little_endian(&mut bytes);
160+
tree_hash::Hash256::from(bytes)
161161
}
162162
}
163163

164+
164165
};
165166
}
166167

contracts/near/eth2-utility/src/consensus.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ pub struct ProofSize {
2424
pub l1_beacon_block_body_proof_size: usize,
2525
pub l2_execution_payload_proof_size: usize,
2626
pub execution_proof_size: usize,
27-
// Add execution payload validation parameters
28-
pub execution_payload_field_index: usize,
29-
pub beacon_block_body_validation_tree_depth: usize,
3027
}
3128

3229
#[derive(Debug)]
@@ -37,6 +34,8 @@ pub struct GeneralizedIndex {
3734
pub current_sync_committee_tree_index: u32,
3835
pub sync_committee_tree_depth: u32,
3936
pub sync_committee_tree_index: u32,
37+
pub execution_payload_tree_depth: u32,
38+
pub execution_payload_tree_index: u32,
4039
}
4140

4241
#[derive(PartialEq, BorshSerialize, BorshDeserialize, BorshSchema, Clone, Copy, Debug)]
@@ -147,19 +146,6 @@ impl NetworkConfig {
147146
}
148147

149148
pub fn compute_proof_size(&self, epoch: Epoch) -> ProofSize {
150-
if epoch >= self.electra_fork_epoch {
151-
return ProofSize {
152-
beacon_block_body_tree_depth: 5, // Electra increases tree depth
153-
l1_beacon_block_body_tree_execution_payload_index: 9,
154-
l2_execution_payload_tree_execution_block_index: 12,
155-
l1_beacon_block_body_proof_size: 5,
156-
l2_execution_payload_proof_size: 5,
157-
execution_proof_size: 10,
158-
execution_payload_field_index: 9, // May change in future forks
159-
beacon_block_body_validation_tree_depth: 5,
160-
};
161-
}
162-
163149
if epoch >= self.deneb_fork_epoch {
164150
return ProofSize {
165151
beacon_block_body_tree_depth: 4,
@@ -168,8 +154,6 @@ impl NetworkConfig {
168154
l1_beacon_block_body_proof_size: 4,
169155
l2_execution_payload_proof_size: 5,
170156
execution_proof_size: 9,
171-
execution_payload_field_index: 9,
172-
beacon_block_body_validation_tree_depth: 4,
173157
};
174158
}
175159

@@ -180,8 +164,6 @@ impl NetworkConfig {
180164
l1_beacon_block_body_proof_size: 4,
181165
l2_execution_payload_proof_size: 4,
182166
execution_proof_size: 8,
183-
execution_payload_field_index: 9,
184-
beacon_block_body_validation_tree_depth: 4,
185167
}
186168
}
187169

@@ -196,6 +178,9 @@ impl NetworkConfig {
196178
pub const CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA: u32 = 86;
197179
pub const NEXT_SYNC_COMMITTEE_INDEX_ELECTRA: u32 = 87;
198180

181+
// Spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/light-client/sync-protocol.md
182+
pub const EXECUTION_PAYLOAD_GINDEX_ELECTRA: u32 = 25;
183+
199184
let epoch = compute_epoch_at_slot(slot);
200185

201186
if epoch >= self.electra_fork_epoch {
@@ -208,6 +193,8 @@ impl NetworkConfig {
208193
),
209194
sync_committee_tree_depth: floorlog2(NEXT_SYNC_COMMITTEE_INDEX_ELECTRA),
210195
sync_committee_tree_index: get_subtree_index(NEXT_SYNC_COMMITTEE_INDEX_ELECTRA),
196+
execution_payload_tree_depth: floorlog2(EXECUTION_PAYLOAD_GINDEX_ELECTRA),
197+
execution_payload_tree_index: get_subtree_index(EXECUTION_PAYLOAD_GINDEX_ELECTRA),
211198
}
212199
} else {
213200
GeneralizedIndex {
@@ -217,6 +204,8 @@ impl NetworkConfig {
217204
current_sync_committee_tree_index: get_subtree_index(CURRENT_SYNC_COMMITTEE_INDEX),
218205
sync_committee_tree_depth: floorlog2(NEXT_SYNC_COMMITTEE_INDEX),
219206
sync_committee_tree_index: get_subtree_index(NEXT_SYNC_COMMITTEE_INDEX),
207+
execution_payload_tree_depth: floorlog2(EXECUTION_PAYLOAD_GINDEX),
208+
execution_payload_tree_index: get_subtree_index(EXECUTION_PAYLOAD_GINDEX),
220209
}
221210
}
222211
}
@@ -296,13 +285,13 @@ impl NetworkConfig {
296285
}
297286

298287
// Use fork-aware proof parameters
299-
let proof_size = self.compute_proof_size(epoch);
288+
let generalized_index = self.get_generalized_index_constants(epoch);
300289

301290
verify_merkle_proof(
302291
self.get_lc_execution_root(header),
303292
&header.execution_branch,
304-
proof_size.beacon_block_body_validation_tree_depth,
305-
proof_size.execution_payload_field_index,
293+
generalized_index.execution_payload_tree_depth.try_into().unwrap(),
294+
generalized_index.execution_payload_tree_index.try_into().unwrap(),
306295
header.beacon.body_root,
307296
)
308297
}

0 commit comments

Comments
 (0)