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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use frame_support::{weights::Weight, RuntimeDebug};
use sp_runtime::{FixedPointNumber, FixedU128};
use sp_std::{ops::RangeInclusive, prelude::*};
use crate::*;
use bp_messages::{
source_chain::{SenderOrigin, TargetHeaderChain},
target_chain::{ProvedMessages, SourceHeaderChain},
InboundLaneData, LaneId, Message, MessageNonce, Parameter,
};
use bp_runtime::{Chain, ChainId, PANGOLIN_CHAIN_ID, PANGORO_CHAIN_ID};
use bridge_runtime_common::{
lanes::PANGORO_PANGOLIN_LANE,
messages::{
source::{
self, FromBridgedChainMessagesDeliveryProof, FromThisChainMessagePayload,
FromThisChainMessageVerifier,
},
target::{
self, FromBridgedChainEncodedMessageCall, FromBridgedChainMessageDispatch,
FromBridgedChainMessagePayload, FromBridgedChainMessagesProof,
},
BridgedChainWithMessages, ChainWithMessages, MessageBridge, ThisChainWithMessages,
},
};
use darwinia_support::evm::{ConcatConverter, DeriveSubstrateAddress};
type ToPangoroMessagesDeliveryProof = FromBridgedChainMessagesDeliveryProof<bp_pangoro::Hash>;
type FromPangoroMessagesProof = FromBridgedChainMessagesProof<bp_pangoro::Hash>;
pub type ToPangoroMessagePayload = FromThisChainMessagePayload<WithPangoroMessageBridge>;
pub type FromPangoroMessagePayload = FromBridgedChainMessagePayload<WithPangoroMessageBridge>;
pub type ToPangoroMessageVerifier =
FromThisChainMessageVerifier<WithPangoroMessageBridge, Runtime, WithPangoroFeeMarket>;
pub type FromPangoroEncodedCall = FromBridgedChainEncodedMessageCall<Call>;
pub type FromPangoroMessageDispatch =
FromBridgedChainMessageDispatch<WithPangoroMessageBridge, Runtime, Ring, WithPangoroDispatch>;
pub const INITIAL_PANGORO_TO_PANGOLIN_CONVERSION_RATE: FixedU128 =
FixedU128::from_inner(FixedU128::DIV);
frame_support::parameter_types! {
pub storage PangoroToPangolinConversionRate: FixedU128 = INITIAL_PANGORO_TO_PANGOLIN_CONVERSION_RATE;
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum PangolinToPangoroMessagesParameter {
PangoroToPangolinConversionRate(FixedU128),
}
impl Parameter for PangolinToPangoroMessagesParameter {
fn save(&self) {
match *self {
PangolinToPangoroMessagesParameter::PangoroToPangolinConversionRate(
ref conversion_rate,
) => PangoroToPangolinConversionRate::set(conversion_rate),
}
}
}
#[derive(Clone, Copy, RuntimeDebug)]
pub struct WithPangoroMessageBridge;
impl MessageBridge for WithPangoroMessageBridge {
type BridgedChain = Pangoro;
type ThisChain = Pangolin;
const BRIDGED_CHAIN_ID: ChainId = PANGORO_CHAIN_ID;
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bp_pangolin::WITH_PANGOLIN_MESSAGES_PALLET_NAME;
const RELAYER_FEE_PERCENT: u32 = 10;
const THIS_CHAIN_ID: ChainId = PANGOLIN_CHAIN_ID;
}
#[derive(Clone, Copy, RuntimeDebug)]
pub struct Pangolin;
impl ChainWithMessages for Pangolin {
type AccountId = bp_pangolin::AccountId;
type Balance = bp_pangolin::Balance;
type Hash = bp_pangolin::Hash;
type Signature = bp_pangolin::Signature;
type Signer = bp_pangolin::AccountPublic;
type Weight = Weight;
}
impl ThisChainWithMessages for Pangolin {
type Call = Call;
type Origin = Origin;
fn is_message_accepted(_send_origin: &Self::Origin, lane: &LaneId) -> bool {
*lane == PANGORO_PANGOLIN_LANE
}
fn maximal_pending_messages_at_outbound_lane() -> MessageNonce {
MessageNonce::MAX
}
}
#[derive(Clone, Copy, RuntimeDebug)]
pub struct Pangoro;
impl ChainWithMessages for Pangoro {
type AccountId = bp_pangoro::AccountId;
type Balance = bp_pangoro::Balance;
type Hash = bp_pangoro::Hash;
type Signature = bp_pangoro::Signature;
type Signer = bp_pangoro::AccountPublic;
type Weight = Weight;
}
impl BridgedChainWithMessages for Pangoro {
fn maximal_extrinsic_size() -> u32 {
bp_pangoro::Pangoro::max_extrinsic_size()
}
fn message_weight_limits(_message_payload: &[u8]) -> RangeInclusive<Weight> {
let upper_limit = target::maximal_incoming_message_dispatch_weight(
bp_pangoro::Pangoro::max_extrinsic_weight(),
);
0..=upper_limit
}
}
impl TargetHeaderChain<ToPangoroMessagePayload, <Self as ChainWithMessages>::AccountId>
for Pangoro
{
type Error = &'static str;
type MessagesDeliveryProof = ToPangoroMessagesDeliveryProof;
fn verify_message(payload: &ToPangoroMessagePayload) -> Result<(), Self::Error> {
source::verify_chain_message::<WithPangoroMessageBridge>(payload)
}
fn verify_messages_delivery_proof(
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_pangolin::AccountId>), Self::Error> {
source::verify_messages_delivery_proof::<
WithPangoroMessageBridge,
Runtime,
WithPangoroGrandpa,
>(proof)
}
}
impl SourceHeaderChain<<Self as ChainWithMessages>::Balance> for Pangoro {
type Error = &'static str;
type MessagesProof = FromPangoroMessagesProof;
fn verify_messages_proof(
proof: Self::MessagesProof,
messages_count: u32,
) -> Result<ProvedMessages<Message<<Self as ChainWithMessages>::Balance>>, Self::Error> {
target::verify_messages_proof::<WithPangoroMessageBridge, Runtime, WithPangoroGrandpa>(
proof,
messages_count,
)
}
}
impl SenderOrigin<crate::AccountId> for crate::Origin {
fn linked_account(&self) -> Option<crate::AccountId> {
match self.caller {
crate::OriginCaller::system(frame_system::RawOrigin::Signed(ref submitter)) =>
Some(submitter.clone()),
crate::OriginCaller::system(frame_system::RawOrigin::Root) => {
Some(ConcatConverter::<_>::derive_substrate_address(&H160([
0x72, 0x6f, 0x6f, 0x74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])))
},
_ => None,
}
}
}