diff --git a/node/src/avail_task.rs b/node/src/avail_task.rs index 2c4bb61..d5cedb7 100644 --- a/node/src/avail_task.rs +++ b/node/src/avail_task.rs @@ -126,7 +126,6 @@ where <::Header as HeaderT>::Number: Into, { task_manager.spawn_essential_handle().spawn("spawn_query_block", "magport", { - // let bak_last_avail_scan_block = // client.runtime_api().last_avail_scan_block(lastest_hash)?; let avail_record = // Arc::new(Mutex::new(AvailRecord { // last_submit_block_confirm: @@ -145,10 +144,10 @@ where if notification.origin != BlockOrigin::Own || block_number % 5 != 0 { continue; } - let latest_final_heght = client.info().finalized_number.into(); + let latest_final_height = client.info().finalized_number.into(); log::info!( - "================query block task working: latest_final_heght:{:?}", - latest_final_heght + "================query block task working: latest_final_height:{:?}", + latest_final_height ); // let last_submit_block_confirm = { @@ -156,7 +155,8 @@ where // avail_record_local.last_submit_block_confirm // }; let lastest_hash = client.info().best_hash; - let last_submit_block_confirm = client.runtime_api().last_submit_block_confirm(lastest_hash).unwrap_or(0); + let last_submit_block_confirm = + client.runtime_api().last_submit_block_confirm(lastest_hash).unwrap_or(0); log::info!( "================query task-last_submit_block_confirm:{:?}", last_submit_block_confirm @@ -170,21 +170,16 @@ where let avail_latest_block_height = get_avail_latest_height(&avail_client).await.unwrap(); log::info!( - "================last_avail_scan_block_confirm:{:?}", - last_avail_scan_block_confirm - ); - log::info!( - "================avail_latest_block_height:{:?}", + "================ Avail Block Query Range: {:?} to {:?} ================", + last_avail_scan_block_confirm, avail_latest_block_height ); - // log::info!("last_submit_block_confirm:{:?}", last_submit_block_confirm); - // log::info!("latest_final_heght:{:?}", latest_final_heght); let mut confirm_block_number = last_submit_block_confirm; let mut last_avail_scan_block = last_avail_scan_block_confirm; - for block_number in last_avail_scan_block..=avail_latest_block_height { + for block_number in last_avail_scan_block_confirm..=avail_latest_block_height { log::info!("================search avail block:{:?}========", block_number); - for block_number_solo in last_submit_block_confirm + 1..=latest_final_heght { + for block_number_solo in last_submit_block_confirm + 1..=latest_final_height { if let Ok(find_result) = query_block_exist( &avail_client, client.clone(), @@ -193,16 +188,16 @@ where ) .await { + log::info!( + "================solo block:{:?}, find result:{:?}========", + block_number_solo, + find_result + ); if !find_result { break; } confirm_block_number = block_number_solo; last_avail_scan_block = block_number; - log::info!( - "================find solo block:{:?}, result:{:?}========", - block_number_solo, - find_result - ); } else { log::info!( "Query task DA Layer error block_number: {:?} not found in DA Layer", @@ -214,8 +209,8 @@ where { let mut avail_record_local = avail_record.lock().await; log::info!( - "================query latest_final_heght:{:?}========", - latest_final_heght + "================query latest_final_height:{:?}========", + latest_final_height ); avail_record_local.last_submit_block_confirm = confirm_block_number; avail_record_local.last_avail_scan_block_confirm = last_avail_scan_block; @@ -256,7 +251,7 @@ where continue; } log::info!("================submit block task working: {:?}", block_number); - let latest_final_heght = client.info().finalized_number.into(); + let latest_final_height = client.info().finalized_number.into(); // let lastest_hash = client.info().best_hash; // let bak_last_submit_block_confirm = // client.runtime_api().last_submit_block_confirm(lastest_hash).unwrap(); @@ -264,21 +259,9 @@ where let avail_record_local = avail_record.lock().await; avail_record_local.last_submit_block }; - // let last_submit_block_confirm = { - // let avail_record_local = avail_record.lock().await; - // avail_record_local.last_submit_block_confirm - // }; - // log::info!( - // "================last_submit_block_confirm:{:?}", - // last_submit_block_confirm - // ); log::info!("================last_submit_block:{:?}", last_submit_block); - log::info!("================submit latest_final_heght:{:?}", latest_final_heght); - // if last_submit_block_confirm < last_submit_block { - // Delay::new(std::time::Duration::from_secs(6)).await; - // continue; - // } - for block_number_solo in last_submit_block + 1..=latest_final_heght { + log::info!("================submit latest_final_height:{:?}", latest_final_height); + for block_number_solo in last_submit_block + 1..=latest_final_height { let rollup_block_hash = client.block_hash(block_number_solo.into()).unwrap().unwrap(); let rollup_block: Option> = @@ -317,7 +300,7 @@ where } { let mut avail_record_local = avail_record.lock().await; - avail_record_local.last_submit_block = latest_final_heght; + avail_record_local.last_submit_block = latest_final_height; } } } diff --git a/node/src/service.rs b/node/src/service.rs index 873d083..9a466e3 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -1,6 +1,5 @@ //! Service and ServiceFactory implementation. Specialized wrapper over substrate service. -use std::sync::Arc; use primitives_avail::{AvailRecord, AvailRuntimeApi}; use sc_client_api::BlockBackend; use sc_consensus_babe::SlotProportion; @@ -13,9 +12,10 @@ use sc_rpc_api::DenyUnsafe; use sc_service::{error::Error as ServiceError, Configuration, RpcHandlers, TaskManager}; use sc_telemetry::{Telemetry, TelemetryWorker}; use sc_transaction_pool_api::OffchainTransactionPoolFactory; -use sp_runtime::traits::Block as BlockT; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; +use sp_runtime::traits::Block as BlockT; +use std::sync::Arc; use frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE; @@ -243,13 +243,30 @@ pub fn new_full_base( let mut avail_record_local = avail_record_clone.lock().await; let last_submit_block_confirm = avail_record_local.last_submit_block_confirm; let lastest_hash = client_clone.info().best_hash; - let storage_last_submit_block_confirm = client_clone.runtime_api().last_submit_block_confirm(lastest_hash).unwrap_or(0); + let storage_last_submit_block_confirm = client_clone + .runtime_api() + .last_submit_block_confirm(lastest_hash) + .unwrap_or(0); if last_submit_block_confirm < storage_last_submit_block_confirm { - avail_record_local.last_submit_block_confirm = storage_last_submit_block_confirm; + log::info!("local struct last_submit_block_confirm: {:?} is smaller than storage_last_submit_block_confirm: {:?}, update local struct last_submit_block_confirm", last_submit_block_confirm, storage_last_submit_block_confirm); + avail_record_local.last_submit_block_confirm = + storage_last_submit_block_confirm; } - log::info!("================create_inherent_data_providers: last_submit_block_confirm:{:?}", last_submit_block_confirm); - let avail = - primitives_avail::AvailInherentDataProvider::new(avail_record_local.last_submit_block_confirm); + + // If Pallet Storage last_submit_block is larger than last_submit_block in AvailRecord struct. + // It means that other nodes have submitted blocks, and the last_submit_block in AvailRecord struct needs to be updated which can avoid submitting the same block. + let last_submit_block = avail_record_local.last_submit_block; + let storage_last_submit_block = + client_clone.runtime_api().last_submit_block(lastest_hash).unwrap_or(0); + if last_submit_block < storage_last_submit_block { + log::info!("local struct last_submit_block: {:?} is smaller than storage_last_submit_block: {:?}, update local struct last_submit_block", last_submit_block, storage_last_submit_block); + avail_record_local.last_submit_block = storage_last_submit_block; + } + log::info!("================create_inherent_data_providers: last_submit_block_confirm: {:?} last_submit_block: {:?}", avail_record_local.last_submit_block_confirm, avail_record_local.last_submit_block); + let avail = primitives_avail::AvailInherentDataProvider::new( + avail_record_local.last_submit_block_confirm, + avail_record_local.last_submit_block, + ); Ok((slot, timestamp, storage_proof, avail)) } diff --git a/pallets/avail/src/lib.rs b/pallets/avail/src/lib.rs index f23e902..0b5e297 100644 --- a/pallets/avail/src/lib.rs +++ b/pallets/avail/src/lib.rs @@ -34,6 +34,10 @@ pub mod pallet { #[pallet::getter(fn last_submit_block_confirm)] pub(super) type LastSubmitBlockConfirm = StorageValue<_, u32, ValueQuery>; + #[pallet::storage] + #[pallet::getter(fn last_submit_block)] + pub(super) type LastSubmitBlock = StorageValue<_, u32, ValueQuery>; + #[pallet::storage] #[pallet::getter(fn last_avail_scan_block)] pub(super) type LastAvailScanBlock = StorageValue<_, u32, ValueQuery>; @@ -84,6 +88,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { ensure_none(origin)?; LastSubmitBlockConfirm::::set(data.last_submit_block_confirm); + LastSubmitBlock::::set(data.last_submit_block); Ok(().into()) } } diff --git a/primitives/avail/src/lib.rs b/primitives/avail/src/lib.rs index 2813334..c3e766e 100644 --- a/primitives/avail/src/lib.rs +++ b/primitives/avail/src/lib.rs @@ -6,13 +6,17 @@ use sp_inherents::{Error, InherentData, InherentIdentifier}; #[derive(Encode, Decode, sp_core::RuntimeDebug, Clone, PartialEq, TypeInfo)] pub struct AvailInherentDataProvider { pub last_submit_block_confirm: u32, + pub last_submit_block: u32, } pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"availiht"; #[cfg(feature = "std")] impl AvailInherentDataProvider { - pub fn new(block_number: u32) -> AvailInherentDataProvider { - AvailInherentDataProvider { last_submit_block_confirm: block_number } + pub fn new( + last_submit_block_confirm: u32, + last_submit_block: u32, + ) -> AvailInherentDataProvider { + AvailInherentDataProvider { last_submit_block_confirm, last_submit_block } } } @@ -44,7 +48,8 @@ sp_api::decl_runtime_apis! { #[api_version(2)] pub trait AvailRuntimeApi { - fn last_submit_block_confirm()-> u32; - fn last_avail_scan_block()->u32; + fn last_submit_block_confirm() -> u32; + fn last_submit_block() -> u32; + fn last_avail_scan_block() ->u32; } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9178855..274e836 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -934,6 +934,9 @@ impl_runtime_apis! { fn last_submit_block_confirm()-> u32{ AvailPallet::last_submit_block_confirm() } + fn last_submit_block()-> u32{ + AvailPallet::last_submit_block() + } fn last_avail_scan_block()->u32 { AvailPallet::last_avail_scan_block() }