Skip to content

Commit

Permalink
validation list
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jan 17, 2025
1 parent bc35785 commit 5dfbee0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ To start the development chain with detailed logging, run the following command:
RUST_BACKTRACE=1 ./target/release/node-template -ldebug --dev
```

Code lines
Shivarthu: 23586



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ pub trait PositiveExternalityApi<BlockHash, AccountId> {
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<u64>>>;

#[method(name = "positiveexternality_validationlistlength")]
fn validation_list_length(
&self,
at: Option<BlockHash>,
) -> RpcResult<u64>;

#[method(name = "positiveexternality_validationlist_latest")]
fn validation_list_latest(
&self,
page: u64,
page_size: u64,
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<AccountId>>>;



}

Expand Down Expand Up @@ -295,5 +310,43 @@ where
Ok(res)
}

fn validation_list_length(
&self,
at: Option<Block::Hash>,
) -> RpcResult<u64> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.validation_list_length(at);

fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned {
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string()))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}

fn validation_list_latest(
&self,
page: u64,
page_size: u64,
at: Option<Block::Hash>,
) -> RpcResult<Option<Vec<AccountId>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.validation_list_latest(at, page, page_size);

fn map_err(error: impl ToString, desc: &'static str) -> ErrorObjectOwned {
ErrorObject::owned(Error::RuntimeError.into(), desc, Some(error.to_string()))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sp_api::decl_runtime_apis! {
fn post_by_address_length(user: AccountId) -> u64;
fn paginate_posts_by_address(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;
fn paginate_posts_by_address_latest(user: AccountId, page: u64, page_size: u64) -> Option<Vec<u64>>;

fn validation_list_length() -> u64;
fn validation_list_latest(page: u64, page_size: u64) -> Option<Vec<AccountId>>;
}
}
27 changes: 26 additions & 1 deletion custom-pallets/positive-externality/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<T: Config> Pallet<T> {
) -> Option<Vec<u64>> {
let mut all_posts = PostByAddresss::<T>::get(user);
all_posts.reverse();

let start = (page - 1) * page_size;

if start >= all_posts.len() as u64 {
Expand All @@ -208,4 +208,29 @@ impl<T: Config> Pallet<T> {
let end = (start + page_size).min(all_posts.len() as u64);
Some(all_posts[start as usize..end as usize].to_vec())
}

pub fn validation_list_length() -> u64 {
match <ValidationList<T>>::get() {
Some(value) => value.len().try_into().unwrap(),
None => 0,
}
}

pub fn validation_list_latest(page: u64, page_size: u64) -> Option<Vec<T::AccountId>> {
let mut all_accounts = match <ValidationList<T>>::get() {
Some(value) => value,
None => vec![],
};
all_accounts.reverse();

let start = (page - 1) * page_size;

if start >= all_accounts.len() as u64 {
// If start exceeds available posts, return None (no more pages).
return None;
}

let end = (start + page_size).min(all_accounts.len() as u64);
Some(all_accounts[start as usize..end as usize].to_vec())
}
}
18 changes: 18 additions & 0 deletions custom-pallets/positive-externality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ pub mod pallet {
pub type ValidationBlock<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, BlockNumberOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn validaton_list)]
pub type ValidationList<T: Config> = StorageValue<_, Vec<T::AccountId>>;

#[pallet::storage]
#[pallet::getter(fn got_incentives_positive_externality)]
pub type GotPositiveExternality<T: Config> =
Expand Down Expand Up @@ -175,6 +179,7 @@ pub mod pallet {
/// Errors should have helpful documentation associated with them.
StorageOverflow,
NotAPostOwner,
UserDontMatch,
ValidationPositiveExternalityIsOff,
LessThanMinStake,
CannotStakeNow,
Expand Down Expand Up @@ -240,6 +245,7 @@ pub mod pallet {
user_to_calculate: T::AccountId,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(who == user_to_calculate, Error::<T>::UserDontMatch);

Self::ensure_validation_on_positive_externality(user_to_calculate.clone())?;

Expand Down Expand Up @@ -277,6 +283,18 @@ pub mod pallet {

if storage_main_block > pe_block_number || pe_block_number == zero_block_number {
<ValidationBlock<T>>::insert(user_to_calculate.clone(), storage_main_block);

match <ValidationList<T>>::get() {
Some(mut value) => {
value.push(user_to_calculate.clone());
<ValidationList<T>>::put(value);
}
None => {
let mut value = Vec::new();
value.push(user_to_calculate.clone());
<ValidationList<T>>::put(value);
}
}
// check what if called again
T::SchellingGameSharedSource::set_to_staking_period_pe_link(key.clone(), now)?;
T::SchellingGameSharedSource::create_tree_helper_link(key, 3)?;
Expand Down
8 changes: 8 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,14 @@ impl_runtime_apis! {
PositiveExternality::paginate_posts_by_address_latest(user, page, page_size)
}

fn validation_list_length() -> u64 {
PositiveExternality::validation_list_length()
}

fn validation_list_latest(page: u64, page_size: u64) -> Option<Vec<AccountId>>{
PositiveExternality::validation_list_latest(page, page_size)
}

}

impl project_tips_runtime_api::ProjectTipsApi<Block, AccountId> for Runtime {
Expand Down

0 comments on commit 5dfbee0

Please sign in to comment.