Skip to content

Commit 0e272fe

Browse files
yrongacatangiu
andauthored
Snowbridge: Refactor with Alloy primitives and clean up code (#9204)
Just some cleanup — not blocking the V2 release if it can’t be included in time. --------- Co-authored-by: Adrian Catangiu <[email protected]>
1 parent 32e8292 commit 0e272fe

File tree

13 files changed

+155
-750
lines changed

13 files changed

+155
-750
lines changed

Cargo.lock

Lines changed: 91 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,10 @@ pallet-oracle-runtime-api = { path = "substrate/frame/honzon/oracle/runtime-api"
636636
Inflector = { version = "0.11.4" }
637637
aes-gcm = { version = "0.10" }
638638
ahash = { version = "0.8.2" }
639-
alloy-core = { version = "1.1.0", default-features = false }
639+
alloy-consensus = { version = "1.0.20", default-features = false }
640+
alloy-core = { version = "1.2.1", default-features = false }
641+
alloy-primitives = { version = "1.2.1", default-features = false }
642+
alloy-rlp = { version = "0.3", default-features = false }
640643
always-assert = { version = "0.1" }
641644
anyhow = { version = "1.0.81", default-features = false }
642645
approx = { version = "0.5.1" }

bridges/snowbridge/pallets/ethereum-client/src/impls.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use super::*;
44
use frame_support::ensure;
55
use snowbridge_beacon_primitives::ExecutionProof;
66

7-
use snowbridge_beacon_primitives::merkle_proof::{generalized_index_length, subtree_index};
8-
use snowbridge_ethereum::Receipt;
7+
use snowbridge_beacon_primitives::{
8+
merkle_proof::{generalized_index_length, subtree_index},
9+
receipt::verify_receipt_proof,
10+
};
911
use snowbridge_verification_primitives::{
1012
VerificationError::{self, *},
1113
Verifier, *,
@@ -21,28 +23,12 @@ impl<T: Config> Verifier for Pallet<T> {
2123
Self::verify_execution_proof(&proof.execution_proof)
2224
.map_err(|e| InvalidExecutionProof(e.into()))?;
2325

24-
let receipt = Self::verify_receipt_inclusion(
26+
Self::verify_receipt_inclusion(
2527
proof.execution_proof.execution_header.receipts_root(),
2628
&proof.receipt_proof.1,
29+
event_log,
2730
)?;
2831

29-
event_log.validate().map_err(|_| InvalidLog)?;
30-
31-
// Convert snowbridge_core::inbound::Log to snowbridge_ethereum::Log.
32-
let event_log = snowbridge_ethereum::Log {
33-
address: event_log.address,
34-
topics: event_log.topics.clone(),
35-
data: event_log.data.clone(),
36-
};
37-
38-
if !receipt.contains_log(&event_log) {
39-
log::error!(
40-
target: "ethereum-client",
41-
"💫 Event log not found in receipt for transaction",
42-
);
43-
return Err(LogNotFound)
44-
}
45-
4632
Ok(())
4733
}
4834
}
@@ -53,20 +39,21 @@ impl<T: Config> Pallet<T> {
5339
pub fn verify_receipt_inclusion(
5440
receipts_root: H256,
5541
receipt_proof: &[Vec<u8>],
56-
) -> Result<Receipt, VerificationError> {
57-
let result = verify_receipt_proof(receipts_root, receipt_proof).ok_or(InvalidProof)?;
58-
59-
match result {
60-
Ok(receipt) => Ok(receipt),
61-
Err(err) => {
62-
log::trace!(
63-
target: "ethereum-client",
64-
"💫 Failed to decode transaction receipt: {}",
65-
err
66-
);
67-
Err(InvalidProof)
68-
},
42+
log: &Log,
43+
) -> Result<(), VerificationError> {
44+
let receipt = verify_receipt_proof(receipts_root, receipt_proof).ok_or(InvalidProof)?;
45+
if !receipt.logs().iter().any(|l| {
46+
l.data.data.0 == log.data &&
47+
l.address.0 == log.address.0 &&
48+
l.topics().len() == log.topics.len()
49+
}) {
50+
log::error!(
51+
target: "ethereum-client",
52+
"💫 Event log not found in receipt for transaction",
53+
);
54+
return Err(LogNotFound)
6955
}
56+
Ok(())
7057
}
7158

7259
/// Validates an execution header with ancestry_proof against a finalized checkpoint on

bridges/snowbridge/pallets/ethereum-client/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ use frame_system::ensure_signed;
4242
use snowbridge_beacon_primitives::{
4343
fast_aggregate_verify,
4444
merkle_proof::{generalized_index_length, subtree_index},
45-
verify_merkle_branch, verify_receipt_proof, BeaconHeader, BlsError, CompactBeaconState,
46-
ForkData, ForkVersion, ForkVersions, PublicKeyPrepared, SigningData,
45+
verify_merkle_branch, BeaconHeader, BlsError, CompactBeaconState, ForkData, ForkVersion,
46+
ForkVersions, PublicKeyPrepared, SigningData,
4747
};
4848
use snowbridge_core::{BasicOperatingMode, RingBufferMap};
4949
use sp_core::H256;

bridges/snowbridge/pallets/ethereum-client/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ fn verify_message_invalid_log() {
803803
assert_ok!(initialize_storage());
804804
assert_err!(
805805
EthereumBeaconClient::verify(&event_log, &proof),
806-
VerificationError::InvalidLog
806+
VerificationError::LogNotFound
807807
);
808808
});
809809
}

bridges/snowbridge/primitives/beacon/src/receipt.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
3+
use snowbridge_ethereum::{mpt, Decodable, ReceiptEnvelope};
34
use sp_core::H256;
45
use sp_io::hashing::keccak_256;
56
use sp_std::prelude::*;
67

7-
use snowbridge_ethereum::{mpt, Receipt};
8-
9-
pub fn verify_receipt_proof(
10-
receipts_root: H256,
11-
proof: &[Vec<u8>],
12-
) -> Option<Result<Receipt, rlp::DecoderError>> {
13-
match apply_merkle_proof(proof) {
14-
Some((root, data)) if root == receipts_root => Some(rlp::decode(&data)),
8+
pub fn verify_receipt_proof(receipts_root: H256, values: &[Vec<u8>]) -> Option<ReceiptEnvelope> {
9+
match apply_merkle_proof(values) {
10+
Some((root, data)) if root == receipts_root =>
11+
ReceiptEnvelope::decode(&mut data.as_slice()).ok(),
1512
Some((_, _)) => None,
1613
None => None,
1714
}
@@ -64,15 +61,9 @@ mod tests {
6461
assert!(verify_receipt_proof(root, &proof_empty).is_none());
6562
assert!(verify_receipt_proof(root, &proof_missing_full_node).is_none());
6663

67-
assert_eq!(
68-
verify_receipt_proof(root, &proof_missing_short_node1),
69-
Some(Err(rlp::DecoderError::Custom("Unsupported receipt type")))
70-
);
64+
assert_eq!(verify_receipt_proof(root, &proof_missing_short_node1), None);
7165

72-
assert_eq!(
73-
verify_receipt_proof(root, &proof_missing_short_node2),
74-
Some(Err(rlp::DecoderError::Custom("Unsupported receipt type")))
75-
);
66+
assert_eq!(verify_receipt_proof(root, &proof_missing_short_node2), None);
7667

7768
assert!(verify_receipt_proof(root, &proof_invalid_encoding).is_none());
7869
assert!(verify_receipt_proof(root, &proof_no_full_node).is_none());

bridges/snowbridge/primitives/ethereum/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ workspace = true
1515
exclude-from-umbrella = true
1616

1717
[dependencies]
18+
alloy-consensus = { workspace = true }
19+
alloy-core = { workspace = true }
20+
alloy-primitives = { workspace = true }
21+
alloy-rlp = { workspace = true }
1822
codec = { features = ["derive"], workspace = true }
1923
ethbloom = { workspace = true }
2024
ethereum-types = { features = ["codec", "rlp", "serialize"], workspace = true }
@@ -31,10 +35,15 @@ sp-std = { workspace = true }
3135

3236
ethabi = { workspace = true }
3337

38+
3439
[features]
3540
default = ["std"]
3641
expensive_tests = []
3742
std = [
43+
"alloy-consensus/std",
44+
"alloy-core/std",
45+
"alloy-primitives/std",
46+
"alloy-rlp/std",
3847
"codec/std",
3948
"ethabi/std",
4049
"ethbloom/std",

0 commit comments

Comments
 (0)