referrerpolicy=no-referrer-when-downgrade
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
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.

//! Adapter that allows using `pallet-bridge-relayers` as a signed extension in the
//! bridge with remote GRANDPA chain.

use crate::{
	extension::verify_messages_call_succeeded, Config as BridgeRelayersConfig, LOG_TARGET,
};

use bp_relayers::{BatchCallUnpacker, ExtensionCallData, ExtensionCallInfo, ExtensionConfig};
use bp_runtime::{Chain, StaticStrProvider};
use frame_support::dispatch::{DispatchInfo, PostDispatchInfo};
use frame_system::Config as SystemConfig;
use pallet_bridge_grandpa::{
	CallSubType as BridgeGrandpaCallSubtype, Config as BridgeGrandpaConfig,
	SubmitFinalityProofHelper,
};
use pallet_bridge_messages::{
	CallSubType as BridgeMessagesCallSubType, Config as BridgeMessagesConfig, LaneIdOf,
};
use sp_runtime::{
	traits::{Dispatchable, Get},
	transaction_validity::{TransactionPriority, TransactionValidityError},
	Saturating,
};
use sp_std::marker::PhantomData;

/// Adapter to be used in signed extension configuration, when bridging with remote
/// chains that are using GRANDPA finality.
pub struct WithGrandpaChainExtensionConfig<
	// signed extension identifier
	IdProvider,
	// runtime that implements `BridgeMessagesConfig<BridgeMessagesPalletInstance>`, which
	// uses `BridgeGrandpaConfig<BridgeGrandpaPalletInstance>` to receive messages and
	// confirmations from the remote chain.
	Runtime,
	// batch call unpacker
	BatchCallUnpacker,
	// instance of the `pallet-bridge-grandpa`, tracked by this extension
	BridgeGrandpaPalletInstance,
	// instance of BridgedChain `pallet-bridge-messages`, tracked by this extension
	BridgeMessagesPalletInstance,
	// instance of `pallet-bridge-relayers`, tracked by this extension
	BridgeRelayersPalletInstance,
	// message delivery transaction priority boost for every additional message
	PriorityBoostPerMessage,
>(
	PhantomData<(
		IdProvider,
		Runtime,
		BatchCallUnpacker,
		BridgeGrandpaPalletInstance,
		BridgeMessagesPalletInstance,
		BridgeRelayersPalletInstance,
		PriorityBoostPerMessage,
	)>,
);

impl<ID, R, BCU, GI, MI, RI, P> ExtensionConfig
	for WithGrandpaChainExtensionConfig<ID, R, BCU, GI, MI, RI, P>
where
	ID: StaticStrProvider,
	R: BridgeRelayersConfig<RI>
		+ BridgeMessagesConfig<MI, BridgedChain = pallet_bridge_grandpa::BridgedChain<R, GI>>
		+ BridgeGrandpaConfig<GI>,
	BCU: BatchCallUnpacker<R>,
	GI: 'static,
	MI: 'static,
	RI: 'static,
	P: Get<TransactionPriority>,
	R::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>
		+ BridgeGrandpaCallSubtype<R, GI>
		+ BridgeMessagesCallSubType<R, MI>,
{
	type IdProvider = ID;
	type Runtime = R;
	type BridgeMessagesPalletInstance = MI;
	type BridgeRelayersPalletInstance = RI;
	type PriorityBoostPerMessage = P;
	type RemoteGrandpaChainBlockNumber = pallet_bridge_grandpa::BridgedBlockNumber<R, GI>;
	type LaneId = LaneIdOf<R, Self::BridgeMessagesPalletInstance>;

	fn parse_and_check_for_obsolete_call(
		call: &R::RuntimeCall,
	) -> Result<
		Option<ExtensionCallInfo<Self::RemoteGrandpaChainBlockNumber, Self::LaneId>>,
		TransactionValidityError,
	> {
		let calls = BCU::unpack(call, 2);
		let total_calls = calls.len();
		let mut calls = calls.into_iter().map(Self::check_obsolete_parsed_call).rev();

		let msgs_call = calls.next().transpose()?.and_then(|c| c.call_info());
		let relay_finality_call =
			calls.next().transpose()?.and_then(|c| c.submit_finality_proof_info());

		Ok(match (total_calls, relay_finality_call, msgs_call) {
			(2, Some(relay_finality_call), Some(msgs_call)) =>
				Some(ExtensionCallInfo::RelayFinalityAndMsgs(relay_finality_call, msgs_call)),
			(1, None, Some(msgs_call)) => Some(ExtensionCallInfo::Msgs(msgs_call)),
			_ => None,
		})
	}

	fn check_obsolete_parsed_call(
		call: &R::RuntimeCall,
	) -> Result<&R::RuntimeCall, TransactionValidityError> {
		call.check_obsolete_submit_finality_proof()?;
		call.check_obsolete_call()?;
		Ok(call)
	}

	fn check_call_result(
		call_info: &ExtensionCallInfo<Self::RemoteGrandpaChainBlockNumber, Self::LaneId>,
		call_data: &mut ExtensionCallData,
		relayer: &R::AccountId,
	) -> bool {
		verify_submit_finality_proof_succeeded::<Self, GI>(call_info, call_data, relayer) &&
			verify_messages_call_succeeded::<Self>(call_info, call_data, relayer)
	}
}

/// If the batch call contains the GRANDPA chain state update call, verify that it
/// has been successful.
///
/// Only returns false when GRANDPA chain state update call has failed.
pub(crate) fn verify_submit_finality_proof_succeeded<C, GI>(
	call_info: &ExtensionCallInfo<C::RemoteGrandpaChainBlockNumber, C::LaneId>,
	call_data: &mut ExtensionCallData,
	relayer: &<C::Runtime as SystemConfig>::AccountId,
) -> bool
where
	C: ExtensionConfig,
	GI: 'static,
	C::Runtime: BridgeGrandpaConfig<GI>,
	<C::Runtime as BridgeGrandpaConfig<GI>>::BridgedChain:
		Chain<BlockNumber = C::RemoteGrandpaChainBlockNumber>,
{
	let Some(finality_proof_info) = call_info.submit_finality_proof_info() else { return true };

	if !SubmitFinalityProofHelper::<C::Runtime, GI>::was_successful(
		finality_proof_info.block_number,
	) {
		// we only refund relayer if all calls have updated chain state
		log::trace!(
			target: LOG_TARGET,
			"{}.{:?}: relayer {:?} has submitted invalid GRANDPA chain finality proof",
			C::IdProvider::STR,
			call_info.messages_call_info().lane_id(),
			relayer,
		);
		return false
	}

	// there's a conflict between how bridge GRANDPA pallet works and a `utility.batchAll`
	// transaction. If relay chain header is mandatory, the GRANDPA pallet returns
	// `Pays::No`, because such transaction is mandatory for operating the bridge. But
	// `utility.batchAll` transaction always requires payment. But in both cases we'll
	// refund relayer - either explicitly here, or using `Pays::No` if he's choosing
	// to submit dedicated transaction.

	// submitter has means to include extra weight/bytes in the `submit_finality_proof`
	// call, so let's subtract extra weight/size to avoid refunding for this extra stuff
	call_data.extra_weight.saturating_accrue(finality_proof_info.extra_weight);
	call_data.extra_size.saturating_accrue(finality_proof_info.extra_size);

	true
}