From 5f03d9d01de68a9a7106d4b8fcff0070e67e273f Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Tue, 5 Dec 2023 16:17:19 +0300 Subject: [PATCH 1/2] separate constants for average and worst case relay headers --- bin/runtime-common/src/mock.rs | 1 + modules/grandpa/src/mock.rs | 1 + modules/parachains/src/mock.rs | 2 ++ primitives/chain-kusama/src/lib.rs | 1 + primitives/chain-polkadot-bulletin/src/lib.rs | 1 + primitives/chain-polkadot/src/lib.rs | 1 + primitives/chain-rococo/src/lib.rs | 1 + primitives/chain-westend/src/lib.rs | 1 + primitives/header-chain/src/lib.rs | 17 ++++++++++++++++- primitives/polkadot-core/src/lib.rs | 13 +++++++++++-- 10 files changed, 36 insertions(+), 3 deletions(-) diff --git a/bin/runtime-common/src/mock.rs b/bin/runtime-common/src/mock.rs index ffabf7f6e2f..2d34dc272c7 100644 --- a/bin/runtime-common/src/mock.rs +++ b/bin/runtime-common/src/mock.rs @@ -378,6 +378,7 @@ impl ChainWithGrandpa for BridgedUnderlyingChain { const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8; const MAX_HEADER_SIZE: u32 = 256; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; } impl Chain for BridgedUnderlyingParachain { diff --git a/modules/grandpa/src/mock.rs b/modules/grandpa/src/mock.rs index 7efa84971fe..ad61e7498bd 100644 --- a/modules/grandpa/src/mock.rs +++ b/modules/grandpa/src/mock.rs @@ -88,6 +88,7 @@ impl ChainWithGrandpa for TestBridgedChain { const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8; const MAX_HEADER_SIZE: u32 = 256; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; } /// Return test externalities to use in tests. diff --git a/modules/parachains/src/mock.rs b/modules/parachains/src/mock.rs index d95e76f3108..8e7a0d7e400 100644 --- a/modules/parachains/src/mock.rs +++ b/modules/parachains/src/mock.rs @@ -254,6 +254,7 @@ impl ChainWithGrandpa for TestBridgedChain { const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8; const MAX_HEADER_SIZE: u32 = 256; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; } #[derive(Debug)] @@ -285,6 +286,7 @@ impl ChainWithGrandpa for OtherBridgedChain { const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8; const MAX_HEADER_SIZE: u32 = 256; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64; } /// Return test externalities to use in tests. diff --git a/primitives/chain-kusama/src/lib.rs b/primitives/chain-kusama/src/lib.rs index d5748aa132c..7f11daadf1a 100644 --- a/primitives/chain-kusama/src/lib.rs +++ b/primitives/chain-kusama/src/lib.rs @@ -54,6 +54,7 @@ impl ChainWithGrandpa for Kusama { REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY; const MAX_HEADER_SIZE: u32 = MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = WORST_HEADER_SIZE_IN_JUSTIFICATION; } // The SignedExtension used by Kusama. diff --git a/primitives/chain-polkadot-bulletin/src/lib.rs b/primitives/chain-polkadot-bulletin/src/lib.rs index fcc6e90eb1b..d8d1958098a 100644 --- a/primitives/chain-polkadot-bulletin/src/lib.rs +++ b/primitives/chain-polkadot-bulletin/src/lib.rs @@ -209,6 +209,7 @@ impl ChainWithGrandpa for PolkadotBulletin { REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY; const MAX_HEADER_SIZE: u32 = MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = WORST_HEADER_SIZE_IN_JUSTIFICATION; } decl_bridge_finality_runtime_apis!(polkadot_bulletin, grandpa); diff --git a/primitives/chain-polkadot/src/lib.rs b/primitives/chain-polkadot/src/lib.rs index 61c8ca927d8..3f9e2bbc078 100644 --- a/primitives/chain-polkadot/src/lib.rs +++ b/primitives/chain-polkadot/src/lib.rs @@ -54,6 +54,7 @@ impl ChainWithGrandpa for Polkadot { REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY; const MAX_HEADER_SIZE: u32 = MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = WORST_HEADER_SIZE_IN_JUSTIFICATION; } /// The SignedExtension used by Polkadot. diff --git a/primitives/chain-rococo/src/lib.rs b/primitives/chain-rococo/src/lib.rs index 5436ad84646..4c2d5f41f5f 100644 --- a/primitives/chain-rococo/src/lib.rs +++ b/primitives/chain-rococo/src/lib.rs @@ -54,6 +54,7 @@ impl ChainWithGrandpa for Rococo { REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY; const MAX_HEADER_SIZE: u32 = MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = WORST_HEADER_SIZE_IN_JUSTIFICATION; } parameter_types! { diff --git a/primitives/chain-westend/src/lib.rs b/primitives/chain-westend/src/lib.rs index 45c13d60060..fdf1abe6e07 100644 --- a/primitives/chain-westend/src/lib.rs +++ b/primitives/chain-westend/src/lib.rs @@ -54,6 +54,7 @@ impl ChainWithGrandpa for Westend { REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY; const MAX_HEADER_SIZE: u32 = MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = WORST_HEADER_SIZE_IN_JUSTIFICATION; } parameter_types! { diff --git a/primitives/header-chain/src/lib.rs b/primitives/header-chain/src/lib.rs index d2c7ec0759e..4e004b28b19 100644 --- a/primitives/header-chain/src/lib.rs +++ b/primitives/header-chain/src/lib.rs @@ -280,9 +280,22 @@ pub trait ChainWithGrandpa: Chain { /// headers that aren't changing the set. /// /// This isn't a strict limit. The relay may submit justifications with larger headers in its + /// ancestry and the pallet will accept the call. The limit is only used to compute fee, paid + /// by the user at the sending chain. It covers most of cases, but if the actual header, + /// submitted with the messages transaction will be larger than the + /// `AVERAGE_HEADER_SIZE_IN_JUSTIFICATION`, the difference (`WORST_HEADER_SIZE_IN_JUSTIFICATION` + /// - `AVERAGE_HEADER_SIZE_IN_JUSTIFICATION`) must be covered by the sending chain. + const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32; + + /// Worst-case size of the chain header from justification ancestry. We don't expect to see + /// there headers that change GRANDPA authorities set (GRANDPA will probably be able to finalize + /// at least one additional header per session on non test chains), so this is the worst-case + /// size of headers that aren't changing the set. + /// + /// This isn't a strict limit. The relay may submit justifications with larger headers in its /// ancestry and the pallet will accept the call. The limit is only used to compute maximal /// refund amount and doing calls which exceed the limit, may be costly to submitter. - const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32; } impl ChainWithGrandpa for T @@ -298,4 +311,6 @@ where const MAX_HEADER_SIZE: u32 = ::MAX_HEADER_SIZE; const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = ::AVERAGE_HEADER_SIZE_IN_JUSTIFICATION; + const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = + ::WORST_HEADER_SIZE_IN_JUSTIFICATION; } diff --git a/primitives/polkadot-core/src/lib.rs b/primitives/polkadot-core/src/lib.rs index af39b5ab9ba..9ebff80ce4d 100644 --- a/primitives/polkadot-core/src/lib.rs +++ b/primitives/polkadot-core/src/lib.rs @@ -70,7 +70,16 @@ pub const MAX_AUTHORITIES_COUNT: u32 = 1_256; /// reserve here. pub const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 2; -/// Approximate average header size in `votes_ancestries` field of justification on Polkadot-like +/// Average header size in `votes_ancestries` field of justification on Polkadot-like +/// chains. +/// +/// See [`bp-header-chain::ChainWithGrandpa`] for more details. +/// +/// This value comes from recent (February, 2023) Kusama headers. Average is `336` there, but let's +/// have some reserve and make it 1024. +pub const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 1024; + +/// Worst-case header size in `votes_ancestries` field of justification on Polkadot-like /// chains. /// /// See [`bp-header-chain::ChainWithGrandpa`] for more details. @@ -79,7 +88,7 @@ pub const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 2; /// non-mandatory headers has size `40kb` (they contain the BABE epoch descriptor with all /// authorities - just like our mandatory header). Since we assume `2` headers in justification /// votes ancestry, let's set average header to `40kb / 2`. -pub const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 20 * 1024; +pub const WORST_HEADER_SIZE_IN_JUSTIFICATION: u32 = 20 * 1024; /// Approximate maximal header size on Polkadot-like chains. /// From e12442af0ea777b5858f71edca4db3f9b26751f3 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Tue, 5 Dec 2023 17:31:09 +0300 Subject: [PATCH 2/2] fix compilation --- primitives/chain-polkadot-bulletin/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/primitives/chain-polkadot-bulletin/src/lib.rs b/primitives/chain-polkadot-bulletin/src/lib.rs index d8d1958098a..49461ab8fbf 100644 --- a/primitives/chain-polkadot-bulletin/src/lib.rs +++ b/primitives/chain-polkadot-bulletin/src/lib.rs @@ -44,6 +44,7 @@ pub use bp_polkadot_core::{ AccountAddress, AccountId, Balance, Block, BlockNumber, Hash, Hasher, Header, Nonce, Signature, SignedBlock, UncheckedExtrinsic, AVERAGE_HEADER_SIZE_IN_JUSTIFICATION, EXTRA_STORAGE_PROOF_SIZE, MAX_HEADER_SIZE, REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY, + WORST_HEADER_SIZE_IN_JUSTIFICATION, }; /// Maximal number of GRANDPA authorities at Polkadot Bulletin chain.