1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::{ExecutionSource, GatewayVendor, SpeedMode};
use codec::{Decode, Encode};
use frame_support::sp_runtime::traits::Zero;
use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::TypeInfo;
use sp_runtime::DispatchError;
use sp_std::marker::PhantomData;
use t3rn_abi::types::Bytes;

#[derive(Clone, Eq, Decode, Encode, PartialEq, Debug, TypeInfo)]
pub enum HeightResult<BlockNumber> {
    Height(BlockNumber),
    NotActive,
}

#[derive(Clone, Eq, Decode, Encode, PartialEq, Debug, TypeInfo)]
pub enum HeaderResult {
    Header(Bytes),
    NotActive,
}

#[derive(Clone, Eq, Decode, Encode, PartialEq, Debug, TypeInfo)]
pub struct InclusionReceipt<BlockNumber> {
    pub height: BlockNumber,
    pub including_header: Bytes,
    pub message: Bytes,
}

#[derive(Clone, Eq, Decode, Encode, PartialEq, Debug, TypeInfo)]
pub struct LightClientHeartbeat<T: frame_system::Config> {
    pub last_heartbeat: BlockNumberFor<T>,
    pub last_finalized_height: BlockNumberFor<T>,
    pub last_rational_height: BlockNumberFor<T>,
    pub last_fast_height: BlockNumberFor<T>,
    pub is_halted: bool,
    pub ever_initialized: bool,
}

impl<T: frame_system::Config> Default for LightClientHeartbeat<T> {
    fn default() -> Self {
        LightClientHeartbeat {
            last_heartbeat: Zero::zero(),
            last_finalized_height: Zero::zero(),
            last_rational_height: Zero::zero(),
            last_fast_height: Zero::zero(),
            is_halted: false,
            ever_initialized: false,
        }
    }
}

pub trait LightClientAsyncAPI<T: frame_system::Config> {
    fn on_new_epoch(
        verifier: GatewayVendor,
        new_epoch: BlockNumberFor<T>,
        current_hearbeat: LightClientHeartbeat<T>,
    );
}

pub struct LightClientAsyncAPIEmptyMock<T> {
    _phantom: PhantomData<T>,
}

impl<T: frame_system::Config> LightClientAsyncAPI<T> for LightClientAsyncAPIEmptyMock<T> {
    fn on_new_epoch(
        _verifier: GatewayVendor,
        _new_epoch: BlockNumberFor<T>,
        _current_hearbeat: LightClientHeartbeat<T>,
    ) {
    }
}

pub trait LightClient<T: frame_system::Config> {
    fn get_latest_finalized_header(&self) -> HeaderResult;

    fn get_fast_height(&self) -> HeightResult<BlockNumberFor<T>>;

    fn get_rational_height(&self) -> HeightResult<BlockNumberFor<T>>;

    fn get_finalized_height(&self) -> HeightResult<BlockNumberFor<T>>;

    fn get_latest_finalized_header_precompile(&self) -> Bytes;

    fn get_fast_height_precompile(&self) -> BlockNumberFor<T>;

    fn get_rational_height_precompile(&self) -> BlockNumberFor<T>;

    fn get_finalized_height_precompile(&self) -> BlockNumberFor<T>;

    fn get_latest_heartbeat(&self) -> Result<LightClientHeartbeat<T>, DispatchError>;

    fn initialize(
        &self,
        origin: T::RuntimeOrigin,
        gateway_id: [u8; 4],
        encoded_registration_data: Bytes,
    ) -> Result<(), DispatchError>;

    fn turn_on(&self, origin: T::RuntimeOrigin) -> Result<bool, DispatchError>;

    fn turn_off(&self, origin: T::RuntimeOrigin) -> Result<bool, DispatchError>;

    fn submit_encoded_headers(&self, encoded_headers_data: Bytes) -> Result<bool, DispatchError>;

    fn verify_event_inclusion(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        source: Option<ExecutionSource>,
        message: Bytes,
    ) -> Result<InclusionReceipt<BlockNumberFor<T>>, DispatchError>;

    fn verify_state_inclusion(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        message: Bytes,
    ) -> Result<InclusionReceipt<BlockNumberFor<T>>, DispatchError>;

    fn verify_tx_inclusion(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        message: Bytes,
    ) -> Result<InclusionReceipt<BlockNumberFor<T>>, DispatchError>;

    fn verify_event_inclusion_precompile(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        source: ExecutionSource,
        message: Bytes,
    ) -> Result<Bytes, DispatchError>;

    fn verify_state_inclusion_precompile(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        message: Bytes,
    ) -> Result<Bytes, DispatchError>;

    fn verify_tx_inclusion_precompile(
        &self,
        gateway_id: [u8; 4],
        speed_mode: SpeedMode,
        message: Bytes,
    ) -> Result<Bytes, DispatchError>;
}