@@ -24,8 +24,8 @@ use crate::justification::{
24
24
GrandpaJustification , JustificationVerificationContext , JustificationVerificationError ,
25
25
} ;
26
26
use bp_runtime:: {
27
- BasicOperatingMode , Chain , HashOf , HasherOf , StorageProofError , UnderlyingChainProvider ,
28
- UnverifiedStorageProof , VerifiedStorageProof ,
27
+ BasicOperatingMode , Chain , HashOf , HasherOf , HeaderOf , StorageProofError ,
28
+ UnderlyingChainProvider , UnverifiedStorageProof , VerifiedStorageProof ,
29
29
} ;
30
30
use codec:: { Codec , Decode , Encode , EncodeLike , MaxEncodedLen } ;
31
31
use core:: { clone:: Clone , cmp:: Eq , default:: Default , fmt:: Debug } ;
@@ -270,36 +270,28 @@ pub trait ChainWithGrandpa: Chain {
270
270
/// to submitter.
271
271
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY : u32 ;
272
272
273
- /// Maximal size of the chain header. The header may be the header that enacts new GRANDPA
274
- /// authorities set (so it has large digest inside).
273
+ /// Maximal size of the mandatory chain header. Mandatory header is the header that enacts new
274
+ /// GRANDPA authorities set (so it has large digest inside).
275
275
///
276
276
/// This isn't a strict limit. The relay may submit larger headers and the pallet will accept
277
277
/// the call. The limit is only used to compute maximal refund amount and doing calls which
278
278
/// exceed the limit, may be costly to submitter.
279
- const MAX_HEADER_SIZE : u32 ;
279
+ const MAX_MANDATORY_HEADER_SIZE : u32 ;
280
280
281
- /// Average size of the chain header from justification ancestry . We don't expect to see there
282
- /// headers that change GRANDPA authorities set (GRANDPA will probably be able to finalize at
283
- /// least one additional header per session on non test chains), so this is average size of
284
- /// headers that aren't changing the set.
281
+ /// Average size of the chain header. We don't expect to see there headers that change GRANDPA
282
+ /// authorities set (GRANDPA will probably be able to finalize at least one additional header
283
+ /// per session on non test chains), so this is average size of headers that aren't changing the
284
+ /// set.
285
285
///
286
- /// This isn't a strict limit. The relay may submit justifications with larger headers in its
287
- /// ancestry and the pallet will accept the call. The limit is only used to compute fee, paid
288
- /// by the user at the sending chain. It covers most of cases, but if the actual header,
289
- /// submitted with the messages transaction will be larger than the
290
- /// `AVERAGE_HEADER_SIZE_IN_JUSTIFICATION`, the difference (`WORST_HEADER_SIZE_IN_JUSTIFICATION`
291
- /// - `AVERAGE_HEADER_SIZE_IN_JUSTIFICATION`) must be covered by the sending chain.
292
- const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION : u32 ;
293
-
294
- /// Worst-case size of the chain header from justification ancestry. We don't expect to see
295
- /// there headers that change GRANDPA authorities set (GRANDPA will probably be able to finalize
296
- /// at least one additional header per session on non test chains), so this is the worst-case
297
- /// size of headers that aren't changing the set.
286
+ /// This isn't a strict limit. The relay may submit justifications with larger headers and the
287
+ /// pallet will accept the call. However, if the total size of all `submit_finality_proof`
288
+ /// arguments exceeds the maximal size, computed using this average size, relayer will only get
289
+ /// partial refund.
298
290
///
299
- /// This isn't a strict limit. The relay may submit justifications with larger headers in its
300
- /// ancestry and the pallet will accept the call. The limit is only used to compute maximal
301
- /// refund amount and doing calls which exceed the limit, may be costly to submitter .
302
- const WORST_HEADER_SIZE_IN_JUSTIFICATION : u32 ;
291
+ /// We expect some headers on production chains that are above this size. But they are rare and
292
+ /// if rellayer cares about its profitability, we expect it'll select other headers for
293
+ /// submission .
294
+ const AVERAGE_HEADER_SIZE : u32 ;
303
295
}
304
296
305
297
impl < T > ChainWithGrandpa for T
@@ -312,9 +304,70 @@ where
312
304
const MAX_AUTHORITIES_COUNT : u32 = <T :: Chain as ChainWithGrandpa >:: MAX_AUTHORITIES_COUNT ;
313
305
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY : u32 =
314
306
<T :: Chain as ChainWithGrandpa >:: REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY ;
315
- const MAX_HEADER_SIZE : u32 = <T :: Chain as ChainWithGrandpa >:: MAX_HEADER_SIZE ;
316
- const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION : u32 =
317
- <T :: Chain as ChainWithGrandpa >:: AVERAGE_HEADER_SIZE_IN_JUSTIFICATION ;
318
- const WORST_HEADER_SIZE_IN_JUSTIFICATION : u32 =
319
- <T :: Chain as ChainWithGrandpa >:: WORST_HEADER_SIZE_IN_JUSTIFICATION ;
307
+ const MAX_MANDATORY_HEADER_SIZE : u32 =
308
+ <T :: Chain as ChainWithGrandpa >:: MAX_MANDATORY_HEADER_SIZE ;
309
+ const AVERAGE_HEADER_SIZE : u32 = <T :: Chain as ChainWithGrandpa >:: AVERAGE_HEADER_SIZE ;
310
+ }
311
+
312
+ /// Returns maximal expected size of `submit_finality_proof` call arguments.
313
+ pub fn max_expected_submit_finality_proof_arguments_size < C : ChainWithGrandpa > (
314
+ is_mandatory_finality_target : bool ,
315
+ precommits : u32 ,
316
+ ) -> u32 {
317
+ let max_expected_justification_size =
318
+ GrandpaJustification :: < HeaderOf < C > > :: max_reasonable_size :: < C > ( precommits) ;
319
+
320
+ // call arguments are header and justification
321
+ let max_expected_finality_target_size = if is_mandatory_finality_target {
322
+ C :: MAX_MANDATORY_HEADER_SIZE
323
+ } else {
324
+ C :: AVERAGE_HEADER_SIZE
325
+ } ;
326
+ max_expected_finality_target_size. saturating_add ( max_expected_justification_size)
327
+ }
328
+
329
+ #[ cfg( test) ]
330
+ mod tests {
331
+ use super :: * ;
332
+ use bp_runtime:: ChainId ;
333
+ use frame_support:: weights:: Weight ;
334
+ use sp_runtime:: { testing:: H256 , traits:: BlakeTwo256 , MultiSignature , StateVersion } ;
335
+
336
+ struct TestChain ;
337
+
338
+ impl Chain for TestChain {
339
+ const ID : ChainId = * b"test" ;
340
+ const STATE_VERSION : StateVersion = StateVersion :: V1 ;
341
+ type BlockNumber = u32 ;
342
+ type Hash = H256 ;
343
+ type Hasher = BlakeTwo256 ;
344
+ type Header = sp_runtime:: generic:: Header < u32 , BlakeTwo256 > ;
345
+ type AccountId = u64 ;
346
+ type Balance = u64 ;
347
+ type Nonce = u64 ;
348
+ type Signature = MultiSignature ;
349
+
350
+ fn max_extrinsic_size ( ) -> u32 {
351
+ 0
352
+ }
353
+ fn max_extrinsic_weight ( ) -> Weight {
354
+ Weight :: zero ( )
355
+ }
356
+ }
357
+
358
+ impl ChainWithGrandpa for TestChain {
359
+ const WITH_CHAIN_GRANDPA_PALLET_NAME : & ' static str = "Test" ;
360
+ const MAX_AUTHORITIES_COUNT : u32 = 128 ;
361
+ const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY : u32 = 2 ;
362
+ const MAX_MANDATORY_HEADER_SIZE : u32 = 100_000 ;
363
+ const AVERAGE_HEADER_SIZE : u32 = 1_024 ;
364
+ }
365
+
366
+ #[ test]
367
+ fn max_expected_submit_finality_proof_arguments_size_respects_mandatory_argument ( ) {
368
+ assert ! (
369
+ max_expected_submit_finality_proof_arguments_size:: <TestChain >( true , 100 ) >
370
+ max_expected_submit_finality_proof_arguments_size:: <TestChain >( false , 100 ) ,
371
+ ) ;
372
+ }
320
373
}
0 commit comments