Skip to content

Commit

Permalink
Feat: Node's Submit Task is based on pallet storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
luckychacha committed Dec 31, 2023
1 parent 92a4ed7 commit c5ad152
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
57 changes: 20 additions & 37 deletions node/src/avail_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ where
<<B as BlockT>::Header as HeaderT>::Number: Into<u32>,
{
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:
Expand All @@ -145,18 +144,19 @@ 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 = {
// let avail_record_local = avail_record.lock().await;
// 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
Expand All @@ -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(),
Expand All @@ -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",
Expand All @@ -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;
Expand Down Expand Up @@ -256,29 +251,17 @@ 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();
let last_submit_block = {
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<sp_runtime::generic::SignedBlock<B>> =
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
31 changes: 24 additions & 7 deletions node/src/service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions pallets/avail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub mod pallet {
#[pallet::getter(fn last_submit_block_confirm)]
pub(super) type LastSubmitBlockConfirm<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn last_submit_block)]
pub(super) type LastSubmitBlock<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn last_avail_scan_block)]
pub(super) type LastAvailScanBlock<T: Config> = StorageValue<_, u32, ValueQuery>;
Expand Down Expand Up @@ -84,6 +88,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
LastSubmitBlockConfirm::<T>::set(data.last_submit_block_confirm);
LastSubmitBlock::<T>::set(data.last_submit_block);
Ok(().into())
}
}
Expand Down
13 changes: 9 additions & 4 deletions primitives/avail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit c5ad152

Please sign in to comment.