Skip to content

Commit 98286ad

Browse files
Fix Asset Hub collator crashing when starting from genesis (#1788)
1 parent a808a3a commit 98286ad

File tree

2 files changed

+154
-7
lines changed

2 files changed

+154
-7
lines changed

cumulus/polkadot-parachain/src/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,21 +836,21 @@ pub fn run() -> Result<()> {
836836
info!("Is collating: {}", if config.role.is_authority() { "yes" } else { "no" });
837837

838838
match config.chain_spec.runtime() {
839-
Runtime::AssetHubPolkadot => crate::service::start_generic_aura_node::<
839+
Runtime::AssetHubPolkadot => crate::service::start_asset_hub_node::<
840840
asset_hub_polkadot_runtime::RuntimeApi,
841841
AssetHubPolkadotAuraId,
842842
>(config, polkadot_config, collator_options, id, hwbench)
843843
.await
844844
.map(|r| r.0)
845845
.map_err(Into::into),
846-
Runtime::AssetHubKusama => crate::service::start_generic_aura_node::<
846+
Runtime::AssetHubKusama => crate::service::start_asset_hub_node::<
847847
asset_hub_kusama_runtime::RuntimeApi,
848848
AuraId,
849849
>(config, polkadot_config, collator_options, id, hwbench)
850850
.await
851851
.map(|r| r.0)
852852
.map_err(Into::into),
853-
Runtime::AssetHubWestend => crate::service::start_generic_aura_node::<
853+
Runtime::AssetHubWestend => crate::service::start_asset_hub_node::<
854854
asset_hub_westend_runtime::RuntimeApi,
855855
AuraId,
856856
>(config, polkadot_config, collator_options, id, hwbench)

cumulus/polkadot-parachain/src/service.rs

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use codec::Codec;
17+
use codec::{Codec, Decode};
1818
use cumulus_client_cli::CollatorOptions;
1919
use cumulus_client_collator::service::CollatorService;
2020
use cumulus_client_consensus_aura::collators::{
@@ -44,7 +44,7 @@ use crate::rpc;
4444
pub use parachains_common::{AccountId, Balance, Block, BlockNumber, Hash, Header, Nonce};
4545

4646
use cumulus_client_consensus_relay_chain::Verifier as RelayChainVerifier;
47-
use futures::lock::Mutex;
47+
use futures::{lock::Mutex, prelude::*};
4848
use sc_consensus::{
4949
import_queue::{BasicQueue, Verifier as VerifierT},
5050
BlockImportParams, ImportQueue,
@@ -54,10 +54,14 @@ use sc_network::{config::FullNetworkConfiguration, NetworkBlock};
5454
use sc_network_sync::SyncingService;
5555
use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient, TaskManager};
5656
use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};
57-
use sp_api::{ApiExt, ConstructRuntimeApi};
57+
use sp_api::{ApiExt, ConstructRuntimeApi, ProvideRuntimeApi};
5858
use sp_consensus_aura::AuraApi;
59+
use sp_core::traits::SpawnEssentialNamed;
5960
use sp_keystore::KeystorePtr;
60-
use sp_runtime::{app_crypto::AppCrypto, traits::Header as HeaderT};
61+
use sp_runtime::{
62+
app_crypto::AppCrypto,
63+
traits::{Block as BlockT, Header as HeaderT},
64+
};
6165
use std::{marker::PhantomData, sync::Arc, time::Duration};
6266
use substrate_prometheus_endpoint::Registry;
6367

@@ -1389,6 +1393,149 @@ where
13891393
.await
13901394
}
13911395

1396+
/// Start a shell node which should later transition into an Aura powered parachain node. Asset Hub
1397+
/// uses this because at genesis, Asset Hub was on the `shell` runtime which didn't have Aura and
1398+
/// needs to sync and upgrade before it can run `AuraApi` functions.
1399+
pub async fn start_asset_hub_node<RuntimeApi, AuraId: AppCrypto + Send + Codec + Sync>(
1400+
parachain_config: Configuration,
1401+
polkadot_config: Configuration,
1402+
collator_options: CollatorOptions,
1403+
para_id: ParaId,
1404+
hwbench: Option<sc_sysinfo::HwBench>,
1405+
) -> sc_service::error::Result<(TaskManager, Arc<ParachainClient<RuntimeApi>>)>
1406+
where
1407+
RuntimeApi: ConstructRuntimeApi<Block, ParachainClient<RuntimeApi>> + Send + Sync + 'static,
1408+
RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
1409+
+ sp_api::Metadata<Block>
1410+
+ sp_session::SessionKeys<Block>
1411+
+ sp_api::ApiExt<Block>
1412+
+ sp_offchain::OffchainWorkerApi<Block>
1413+
+ sp_block_builder::BlockBuilder<Block>
1414+
+ cumulus_primitives_core::CollectCollationInfo<Block>
1415+
+ sp_consensus_aura::AuraApi<Block, <<AuraId as AppCrypto>::Pair as Pair>::Public>
1416+
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
1417+
+ frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
1418+
<<AuraId as AppCrypto>::Pair as Pair>::Signature:
1419+
TryFrom<Vec<u8>> + std::hash::Hash + sp_runtime::traits::Member + Codec,
1420+
{
1421+
start_node_impl::<RuntimeApi, _, _, _>(
1422+
parachain_config,
1423+
polkadot_config,
1424+
collator_options,
1425+
CollatorSybilResistance::Resistant, // Aura
1426+
para_id,
1427+
|_| Ok(RpcModule::new(())),
1428+
aura_build_import_queue::<_, AuraId>,
1429+
|client,
1430+
block_import,
1431+
prometheus_registry,
1432+
telemetry,
1433+
task_manager,
1434+
relay_chain_interface,
1435+
transaction_pool,
1436+
sync_oracle,
1437+
keystore,
1438+
relay_chain_slot_duration,
1439+
para_id,
1440+
collator_key,
1441+
overseer_handle,
1442+
announce_block| {
1443+
let relay_chain_interface2 = relay_chain_interface.clone();
1444+
1445+
let collator_service = CollatorService::new(
1446+
client.clone(),
1447+
Arc::new(task_manager.spawn_handle()),
1448+
announce_block,
1449+
client.clone(),
1450+
);
1451+
1452+
let spawner = task_manager.spawn_handle();
1453+
1454+
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
1455+
spawner,
1456+
client.clone(),
1457+
transaction_pool,
1458+
prometheus_registry,
1459+
telemetry.clone(),
1460+
);
1461+
1462+
let collation_future = Box::pin(async move {
1463+
// Start collating with the `shell` runtime while waiting for an upgrade to an Aura
1464+
// compatible runtime.
1465+
let mut request_stream = cumulus_client_collator::relay_chain_driven::init(
1466+
collator_key.clone(),
1467+
para_id,
1468+
overseer_handle.clone(),
1469+
)
1470+
.await;
1471+
while let Some(request) = request_stream.next().await {
1472+
let pvd = request.persisted_validation_data().clone();
1473+
let last_head_hash =
1474+
match <Block as BlockT>::Header::decode(&mut &pvd.parent_head.0[..]) {
1475+
Ok(header) => header.hash(),
1476+
Err(e) => {
1477+
log::error!("Could not decode the head data: {e}");
1478+
request.complete(None);
1479+
continue
1480+
},
1481+
};
1482+
1483+
// Check if we have upgraded to an Aura compatible runtime and transition if
1484+
// necessary.
1485+
if client
1486+
.runtime_api()
1487+
.has_api::<dyn AuraApi<Block, AuraId>>(last_head_hash)
1488+
.unwrap_or(false)
1489+
{
1490+
// Respond to this request before transitioning to Aura.
1491+
request.complete(None);
1492+
break
1493+
}
1494+
}
1495+
1496+
// Move to Aura consensus.
1497+
let slot_duration = match cumulus_client_consensus_aura::slot_duration(&*client) {
1498+
Ok(d) => d,
1499+
Err(e) => {
1500+
log::error!("Could not get Aura slot duration: {e}");
1501+
return
1502+
},
1503+
};
1504+
1505+
let proposer = Proposer::new(proposer_factory);
1506+
1507+
let params = BasicAuraParams {
1508+
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
1509+
block_import,
1510+
para_client: client,
1511+
relay_client: relay_chain_interface2,
1512+
sync_oracle,
1513+
keystore,
1514+
collator_key,
1515+
para_id,
1516+
overseer_handle,
1517+
slot_duration,
1518+
relay_chain_slot_duration,
1519+
proposer,
1520+
collator_service,
1521+
// Very limited proposal time.
1522+
authoring_duration: Duration::from_millis(500),
1523+
};
1524+
1525+
basic_aura::run::<Block, <AuraId as AppCrypto>::Pair, _, _, _, _, _, _, _>(params)
1526+
.await
1527+
});
1528+
1529+
let spawner = task_manager.spawn_essential_handle();
1530+
spawner.spawn_essential("cumulus-asset-hub-collator", None, collation_future);
1531+
1532+
Ok(())
1533+
},
1534+
hwbench,
1535+
)
1536+
.await
1537+
}
1538+
13921539
/// Start an aura powered parachain node which uses the lookahead collator to support async backing.
13931540
/// This node is basic in the sense that its runtime api doesn't include common contents such as
13941541
/// transaction payment. Used for aura glutton.

0 commit comments

Comments
 (0)