From 79bde3d57f50a08f43b5a72f2f600b4141ccaa06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 7 Jun 2021 14:30:14 +0200 Subject: [PATCH] Do not include `:code` in the storage proof if not required This is basically a hack that prevents the inclusion of `:code` in the storage proof. A proper fix requires some changes to the trie crate to not include nodes that aren't read. --- client/collator/src/lib.rs | 19 +++++++++++++++++-- pallets/parachain-system/src/lib.rs | 12 ++++++++++++ polkadot-parachains/rococo-runtime/src/lib.rs | 2 +- polkadot-parachains/shell-runtime/src/lib.rs | 2 +- polkadot-parachains/src/chain_spec.rs | 5 +++++ .../statemine-runtime/src/lib.rs | 11 ++++++++++- .../statemint-runtime/src/lib.rs | 2 +- .../westmint-runtime/src/lib.rs | 11 ++++++++++- test/runtime/src/lib.rs | 2 +- test/service/src/chain_spec.rs | 1 + 10 files changed, 59 insertions(+), 8 deletions(-) diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index db22d89ebc6..57d512d32ba 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -333,6 +333,8 @@ mod tests { use polkadot_overseer::{AllSubsystems, HeadSupportsParachains, Overseer}; use sp_consensus::BlockOrigin; use sp_core::{testing::TaskExecutor, Pair}; + use sp_runtime::traits::BlakeTwo256; + use sp_state_machine::Backend; struct AlwaysSupportsParachains; impl HeadSupportsParachains for AlwaysSupportsParachains { @@ -376,7 +378,7 @@ mod tests { } #[test] - fn collates_produces_a_block() { + fn collates_produces_a_block_and_storage_proof_does_not_contains_code() { let _ = env_logger::try_init(); let spawner = TaskExecutor::new(); @@ -432,8 +434,21 @@ mod tests { let block_data = collation.proof_of_validity.block_data; - let block = Block::decode(&mut &block_data.0[..]).expect("Is a valid block"); + let block = + ParachainBlockData::::decode(&mut &block_data.0[..]).expect("Is a valid block"); assert_eq!(1, *block.header().number()); + + // Ensure that we did not include `:code` in the proof. + let db = block.storage_proof().clone().into_memory_db(); + + let backend = + sp_state_machine::new_in_mem::().update_backend(*header.state_root(), db); + + // Should return an error, as it was not included while building the proof. + assert!(backend + .storage(sp_core::storage::well_known_keys::CODE) + .unwrap_err() + .contains("Trie lookup error: Database missing expected key")); } } diff --git a/pallets/parachain-system/src/lib.rs b/pallets/parachain-system/src/lib.rs index e52169ef187..eb03bb0feda 100644 --- a/pallets/parachain-system/src/lib.rs +++ b/pallets/parachain-system/src/lib.rs @@ -556,6 +556,18 @@ pub mod pallet { matches!(call, Call::set_validation_data(_)) } } + + #[pallet::genesis_config] + #[derive(Default)] + pub struct GenesisConfig; + + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + //TODO: Remove after https://github.com/paritytech/cumulus/issues/479 + sp_io::storage::set(b":c", &[]); + } + } } impl Pallet { diff --git a/polkadot-parachains/rococo-runtime/src/lib.rs b/polkadot-parachains/rococo-runtime/src/lib.rs index 9ddb69cfc36..1cc30fdace2 100644 --- a/polkadot-parachains/rococo-runtime/src/lib.rs +++ b/polkadot-parachains/rococo-runtime/src/lib.rs @@ -442,7 +442,7 @@ construct_runtime! { RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage}, TransactionPayment: pallet_transaction_payment::{Pallet, Storage}, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event, ValidateUnsigned} = 20, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event, ValidateUnsigned} = 20, ParachainInfo: parachain_info::{Pallet, Storage, Config} = 21, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event} = 30, diff --git a/polkadot-parachains/shell-runtime/src/lib.rs b/polkadot-parachains/shell-runtime/src/lib.rs index 42a06220e8a..547115cafe4 100644 --- a/polkadot-parachains/shell-runtime/src/lib.rs +++ b/polkadot-parachains/shell-runtime/src/lib.rs @@ -226,7 +226,7 @@ construct_runtime! { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Pallet, Call, Storage, Config, Event}, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event, ValidateUnsigned}, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event, ValidateUnsigned}, ParachainInfo: parachain_info::{Pallet, Storage, Config}, // DMP handler. diff --git a/polkadot-parachains/src/chain_spec.rs b/polkadot-parachains/src/chain_spec.rs index 470b93da3e3..22094c695d0 100644 --- a/polkadot-parachains/src/chain_spec.rs +++ b/polkadot-parachains/src/chain_spec.rs @@ -178,6 +178,7 @@ fn testnet_genesis( authorities: initial_authorities, }, cumulus_pallet_aura_ext: Default::default(), + cumulus_pallet_parachain_system: Default::default(), } } @@ -190,6 +191,7 @@ fn shell_testnet_genesis(parachain_id: ParaId) -> shell_runtime::GenesisConfig { changes_trie_config: Default::default(), }, parachain_info: shell_runtime::ParachainInfoConfig { parachain_id }, + cumulus_pallet_parachain_system: Default::default(), } } @@ -366,6 +368,7 @@ fn statemint_genesis( // of this. pallet_aura: Default::default(), cumulus_pallet_aura_ext: Default::default(), + cumulus_pallet_parachain_system: Default::default(), } } @@ -540,6 +543,7 @@ fn statemine_genesis( }, pallet_aura: Default::default(), cumulus_pallet_aura_ext: Default::default(), + cumulus_pallet_parachain_system: Default::default(), } } @@ -722,5 +726,6 @@ fn westmint_genesis( // of this. pallet_aura: Default::default(), cumulus_pallet_aura_ext: Default::default(), + cumulus_pallet_parachain_system: Default::default(), } } diff --git a/polkadot-parachains/statemine-runtime/src/lib.rs b/polkadot-parachains/statemine-runtime/src/lib.rs index 41d852bace1..52adfc3dcc7 100644 --- a/polkadot-parachains/statemine-runtime/src/lib.rs +++ b/polkadot-parachains/statemine-runtime/src/lib.rs @@ -672,7 +672,7 @@ construct_runtime!( { // System support stuff. System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event} = 1, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event} = 1, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage} = 2, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent} = 3, ParachainInfo: parachain_info::{Pallet, Storage, Config} = 4, @@ -734,8 +734,17 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPallets, + OnRuntimeUpgrade, >; +pub struct OnRuntimeUpgrade; +impl frame_support::traits::OnRuntimeUpgrade for OnRuntimeUpgrade { + fn on_runtime_upgrade() -> u64 { + sp_io::storage::set(b":c", &[]); + RocksDbWeight::get().writes(1) + } +} + impl_runtime_apis! { impl sp_consensus_aura::AuraApi for Runtime { fn slot_duration() -> sp_consensus_aura::SlotDuration { diff --git a/polkadot-parachains/statemint-runtime/src/lib.rs b/polkadot-parachains/statemint-runtime/src/lib.rs index 7fa0484814a..f51bc0bd383 100644 --- a/polkadot-parachains/statemint-runtime/src/lib.rs +++ b/polkadot-parachains/statemint-runtime/src/lib.rs @@ -615,7 +615,7 @@ construct_runtime!( { // System support stuff. System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event} = 1, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event} = 1, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage} = 2, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent} = 3, ParachainInfo: parachain_info::{Pallet, Storage, Config} = 4, diff --git a/polkadot-parachains/westmint-runtime/src/lib.rs b/polkadot-parachains/westmint-runtime/src/lib.rs index 792d75d28aa..52bfe3ee0f5 100644 --- a/polkadot-parachains/westmint-runtime/src/lib.rs +++ b/polkadot-parachains/westmint-runtime/src/lib.rs @@ -605,7 +605,7 @@ construct_runtime!( { // System support stuff; System: frame_system::{Pallet, Call, Config, Storage, Event}, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event}, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage}, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, ParachainInfo: parachain_info::{Pallet, Storage, Config}, @@ -665,8 +665,17 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPallets, + OnRuntimeUpgrade >; +pub struct OnRuntimeUpgrade; +impl frame_support::traits::OnRuntimeUpgrade for OnRuntimeUpgrade { + fn on_runtime_upgrade() -> u64 { + sp_io::storage::set(b":c", &[]); + RocksDbWeight::get().writes(1) + } +} + impl_runtime_apis! { impl sp_consensus_aura::AuraApi for Runtime { fn slot_duration() -> sp_consensus_aura::SlotDuration { diff --git a/test/runtime/src/lib.rs b/test/runtime/src/lib.rs index 65b58703663..339c2369c89 100644 --- a/test/runtime/src/lib.rs +++ b/test/runtime/src/lib.rs @@ -269,11 +269,11 @@ construct_runtime! { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Pallet, Call, Storage, Config, Event}, + ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event, Config}, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, Sudo: pallet_sudo::{Pallet, Call, Storage, Config, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage}, - ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event}, TransactionPayment: pallet_transaction_payment::{Pallet, Storage}, } } diff --git a/test/service/src/chain_spec.rs b/test/service/src/chain_spec.rs index b0ca039fe50..530469f0268 100644 --- a/test/service/src/chain_spec.rs +++ b/test/service/src/chain_spec.rs @@ -111,6 +111,7 @@ fn testnet_genesis( .to_vec(), ..Default::default() }, + cumulus_pallet_parachain_system: Default::default(), pallet_balances: cumulus_test_runtime::BalancesConfig { balances: endowed_accounts .iter()