Skip to content

Commit

Permalink
feat: implement current_deadline runtime api
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder committed Nov 25, 2024
1 parent abd6eb8 commit c8e197b
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 20 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
5 changes: 2 additions & 3 deletions cli/polka-storage-provider/client/src/commands/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum ProofsCommand {
/// Directory where the PoSt proof will be stored. Defaults to the current directory.
output_path: Option<PathBuf>,
#[arg(short, long)]
sector_number: u64,
sector_number: u32,
},
}

Expand Down Expand Up @@ -364,7 +364,6 @@ impl ProofsCommand {
comm_r,
output_path,
sector_number,
randomness,
} => {
let Some(signer) = Option::<MultiPairSigner>::from(signer_key) else {
return Err(UtilsCommandError::NoSigner)?;
Expand All @@ -388,7 +387,7 @@ impl ProofsCommand {
cid::Cid::from_str(&comm_r).map_err(|_| UtilsCommandError::CommRError)?;

let replicas = vec![ReplicaInfo {
sector_id: sector_number,
sector_id: sector_number.try_into().unwrap(),
comm_r: comm_r
.hash()
.digest()
Expand Down
6 changes: 5 additions & 1 deletion cli/polka-storage-provider/server/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ async fn precommit(
})
};
let sealing_output = sealing_handle.await??;
tracing::info!("Created sector's replica, CommD: {}, CommR: {}", sealing_output.comm_d.cid(), sealing_output.comm_r.cid());
tracing::info!(
"Created sector's replica, CommD: {}, CommR: {}",
sealing_output.comm_d.cid(),
sealing_output.comm_r.cid()
);

let sealing_output_commr = Commitment::<CommR>::from(sealing_output.comm_r);
let sealing_output_commd = Commitment::<CommD>::from(sealing_output.comm_d);
Expand Down
2 changes: 1 addition & 1 deletion cli/polka-storage/storagext-cli/src/cmd/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ProofsCommand {
wait_for_finalization,
)
.await?
},
}
ProofsCommand::SetPostVerifyingKey { verifying_key } => {
let Some(account_keypair) = account_keypair else {
return Err(missing_keypair_error::<Self>().into());
Expand Down
30 changes: 22 additions & 8 deletions cli/polka-storage/storagext/src/clients/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use crate::{
self,
bounded_vec::IntoBoundedByteVec,
client::SubmissionResult,
runtime_types::pallet_storage_provider::{
proofs::SubmitWindowedPoStParams, storage_provider::StorageProviderState,
runtime_types::{
pallet_storage_provider::{
proofs::SubmitWindowedPoStParams, storage_provider::StorageProviderState,
},
primitives_proofs::CurrentDeadline,
},
storage_provider::calls::types::register_storage_provider::PeerId,
},
Expand Down Expand Up @@ -99,16 +102,27 @@ pub trait StorageProviderClientExt {
&self,
) -> impl Future<Output = Result<Vec<String>, subxt::Error>>;


fn current_deadline(&self, account_id: &AccountId32) -> impl Future<Output = Result<u64, subxt::Error>>;
fn current_deadline(
&self,
account_id: &AccountId32,
) -> impl Future<Output = Result<Option<CurrentDeadline<BlockNumber>>, subxt::Error>>;
}

impl StorageProviderClientExt for crate::runtime::client::Client {
async fn current_deadline(&self, account_id: &AccountId32) -> Result<u64, subxt::Error> {
// let _ = runtime::apis().storage_provider_api().current_deadline(account_id);
// let _ = runtime::apis().account_nonce_api().account_nonce(account_id);
async fn current_deadline(
&self,
account_id: &AccountId32,
) -> Result<Option<CurrentDeadline<BlockNumber>>, subxt::Error> {
let payload = runtime::apis()
.storage_provider_api()
.current_deadline(account_id.clone());

Ok(0)
self.client
.runtime_api()
.at_latest()
.await?
.call(payload)
.await
}

#[tracing::instrument(
Expand Down
1 change: 1 addition & 0 deletions pallets/storage-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ std = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"primitives-proofs/std",
]
try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"]
28 changes: 27 additions & 1 deletion pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,8 @@ pub mod pallet {
PublicReplicaInfo {
comm_r: comm_r.raw(),
},
).map_err(|_| Error::<T>::TooManyReplicas);
)
.map_err(|_| Error::<T>::TooManyReplicas);
}
}

Expand Down Expand Up @@ -1062,6 +1063,31 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub fn current_deadline(
storage_provider: &T::AccountId,
) -> core::option::Option<primitives_proofs::CurrentDeadline<BlockNumberFor<T>>> {
let sp = StorageProviders::<T>::try_get(storage_provider).ok()?;
let current_block = <frame_system::Pallet<T>>::block_number();

let deadline = sp
.deadline_info(
current_block,
T::WPoStPeriodDeadlines::get(),
T::WPoStProvingPeriod::get(),
T::WPoStChallengeWindow::get(),
T::WPoStChallengeLookBack::get(),
T::FaultDeclarationCutoff::get(),
)
.ok()?;

Some(primitives_proofs::CurrentDeadline {
deadline_index: deadline.idx,
open: deadline.is_open(),
challenge_block: deadline.challenge,
start: deadline.open_at,
})
}

fn validate_expiration(
curr_block: BlockNumberFor<T>,
activation: BlockNumberFor<T>,
Expand Down
19 changes: 16 additions & 3 deletions primitives/proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub mod randomness;
mod traits;
mod types;

use codec::{Codec, Encode};
use codec::{Codec, Decode, Encode};
use scale_info::TypeInfo;
pub use traits::*;
pub use types::*;

Expand All @@ -24,9 +25,21 @@ where
encoded
}

#[derive(Encode, Decode, TypeInfo)]
pub struct CurrentDeadline<BlockNumber> {
pub deadline_index: u64,
pub open: bool,
pub challenge_block: BlockNumber,
pub start: BlockNumber,
}

sp_api::decl_runtime_apis! {
pub trait StorageProviderApi<AccountId> where AccountId: Codec
{
fn current_deadline(storage_provider: AccountId) -> u64;
fn current_deadline(storage_provider: AccountId) -> core::option::Option<
CurrentDeadline<
<<Block as sp_runtime::traits::Block>::Header as sp_runtime::traits::Header>::Number
>
>;
}
}
}
2 changes: 1 addition & 1 deletion primitives/proofs/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ pub struct ActiveDeal<AccountId> {
pub piece_cid: Cid,
/// Real size of the data
pub piece_size: u64,
}
}
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ impl Runtime {

impl_runtime_apis! {
impl primitives_proofs::StorageProviderApi<Block, AccountId> for Runtime {
fn current_deadline(storage_provider: AccountId) -> u64 {
0
fn current_deadline(storage_provider: AccountId) -> Option<primitives_proofs::CurrentDeadline<BlockNumber>> {
StorageProvider::current_deadline(&storage_provider)
}
}

Expand Down

0 comments on commit c8e197b

Please sign in to comment.