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 ToPangolinMessagesDeliveryProof = FromBridgedChainMessagesDeliveryProof<bp_pangolin::Hash>;
type FromPangolinMessagesProof = FromBridgedChainMessagesProof<bp_pangolin::Hash>;
pub type ToPangolinMessagePayload = FromThisChainMessagePayload<WithPangolinMessageBridge>;
pub type FromPangolinMessagePayload = FromBridgedChainMessagePayload<WithPangolinMessageBridge>;
pub type ToPangolinMessageVerifier =
FromThisChainMessageVerifier<WithPangolinMessageBridge, Runtime, WithPangolinFeeMarket>;
pub type FromPangolinEncodedCall = FromBridgedChainEncodedMessageCall<Call>;
pub type FromPangolinMessageDispatch =
FromBridgedChainMessageDispatch<WithPangolinMessageBridge, Runtime, Ring, WithPangolinDispatch>;
pub const INITIAL_PANGOLIN_TO_PANGORO_CONVERSION_RATE: FixedU128 =
FixedU128::from_inner(FixedU128::DIV);
frame_support::parameter_types! {
pub storage PangolinToPangoroConversionRate: FixedU128 = INITIAL_PANGOLIN_TO_PANGORO_CONVERSION_RATE;
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum PangoroToPangolinMessagesParameter {
PangolinToPangoroConversionRate(FixedU128),
}
impl Parameter for PangoroToPangolinMessagesParameter {
fn save(&self) {
match *self {
PangoroToPangolinMessagesParameter::PangolinToPangoroConversionRate(
ref conversion_rate,
) => PangolinToPangoroConversionRate::set(conversion_rate),
}
}
}
#[derive(Clone, Copy, RuntimeDebug)]
pub struct WithPangolinMessageBridge;
impl MessageBridge for WithPangolinMessageBridge {
type BridgedChain = Pangolin;
type ThisChain = Pangoro;
const BRIDGED_CHAIN_ID: ChainId = PANGOLIN_CHAIN_ID;
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bp_pangoro::WITH_PANGORO_MESSAGES_PALLET_NAME;
const RELAYER_FEE_PERCENT: u32 = 10;
const THIS_CHAIN_ID: ChainId = PANGORO_CHAIN_ID;
}
#[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 ThisChainWithMessages for Pangoro {
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 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 BridgedChainWithMessages for Pangolin {
fn maximal_extrinsic_size() -> u32 {
bp_pangolin::Pangolin::max_extrinsic_size()
}
fn message_weight_limits(_message_payload: &[u8]) -> RangeInclusive<Weight> {
let upper_limit = target::maximal_incoming_message_dispatch_weight(
bp_pangolin::Pangolin::max_extrinsic_weight(),
);
0..=upper_limit
}
}
impl TargetHeaderChain<ToPangolinMessagePayload, <Self as ChainWithMessages>::AccountId>
for Pangolin
{
type Error = &'static str;
type MessagesDeliveryProof = ToPangolinMessagesDeliveryProof;
fn verify_message(payload: &ToPangolinMessagePayload) -> Result<(), Self::Error> {
source::verify_chain_message::<WithPangolinMessageBridge>(payload)
}
fn verify_messages_delivery_proof(
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_pangoro::AccountId>), Self::Error> {
source::verify_messages_delivery_proof::<
WithPangolinMessageBridge,
Runtime,
WithPangolinGrandpa,
>(proof)
}
}
impl SourceHeaderChain<<Self as ChainWithMessages>::Balance> for Pangolin {
type Error = &'static str;
type MessagesProof = FromPangolinMessagesProof;
fn verify_messages_proof(
proof: Self::MessagesProof,
messages_count: u32,
) -> Result<ProvedMessages<Message<<Self as ChainWithMessages>::Balance>>, Self::Error> {
target::verify_messages_proof::<WithPangolinMessageBridge, Runtime, WithPangolinGrandpa>(
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,
}
}
}