referrerpolicy=no-referrer-when-downgrade

snowbridge_verification_primitives/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3//! Types for representing inbound messages
4#![cfg_attr(not(feature = "std"), no_std)]
5use codec::{Decode, DecodeWithMemTracking, Encode};
6use frame_support::PalletError;
7use scale_info::TypeInfo;
8use snowbridge_beacon_primitives::{BeaconHeader, ExecutionProof};
9use sp_core::{RuntimeDebug, H160, H256};
10use sp_std::prelude::*;
11
12/// A trait for verifying inbound messages from Ethereum.
13pub trait Verifier {
14	fn verify(event: &Log, proof: &Proof) -> Result<(), VerificationError>;
15}
16
17#[derive(Clone, Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PalletError, TypeInfo)]
18#[cfg_attr(feature = "std", derive(PartialEq))]
19pub enum VerificationError {
20	/// Execution header is missing
21	HeaderNotFound,
22	/// Event log was not found in the verified transaction receipt
23	LogNotFound,
24	/// Event log has an invalid format
25	InvalidLog,
26	/// Unable to verify the transaction receipt with the provided proof
27	InvalidProof,
28	/// Unable to verify the execution header with ancestry proof
29	InvalidExecutionProof(#[codec(skip)] &'static str),
30}
31
32/// A bridge message from the Gateway contract on Ethereum
33#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, TypeInfo)]
34pub struct EventProof {
35	/// Event log emitted by Gateway contract
36	pub event_log: Log,
37	/// Inclusion proof for a transaction receipt containing the event log
38	pub proof: Proof,
39}
40
41const MAX_TOPICS: usize = 4;
42
43#[derive(Clone, RuntimeDebug)]
44pub enum LogValidationError {
45	TooManyTopics,
46}
47
48/// Event log
49#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, TypeInfo)]
50pub struct Log {
51	pub address: H160,
52	pub topics: Vec<H256>,
53	pub data: Vec<u8>,
54}
55
56impl Log {
57	pub fn validate(&self) -> Result<(), LogValidationError> {
58		if self.topics.len() > MAX_TOPICS {
59			return Err(LogValidationError::TooManyTopics)
60		}
61		Ok(())
62	}
63}
64
65/// Inclusion proof for a transaction receipt
66#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, TypeInfo)]
67pub struct Proof {
68	// Proof keys and values (receipts tree)
69	pub receipt_proof: (Vec<Vec<u8>>, Vec<Vec<u8>>),
70	// Proof that an execution header was finalized by the beacon chain
71	pub execution_proof: ExecutionProof,
72}
73
74#[derive(Clone, RuntimeDebug)]
75pub struct EventFixture {
76	pub event: EventProof,
77	pub finalized_header: BeaconHeader,
78	pub block_roots_root: H256,
79}