Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
fix(cluster): listing api is fixed; limit guard is added
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Aug 10, 2023
1 parent d6682bb commit 77ab74d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 10 additions & 4 deletions bucket/ddc_bucket/cluster/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,14 @@ impl DdcBucket {
filter_manager_id: Option<AccountId>,
) -> (Vec<ClusterInfo>, u32) {
let mut clusters = Vec::with_capacity(limit as usize);
for cluster_id in offset..offset + limit {
let cluster = match self.clusters.clusters.get(cluster_id) {
for idx in offset..offset + limit {
let cluster_id = match self.clusters.clusters_ids.get(idx as usize) {
None => break, // No more items, stop.
Some(cluster) => cluster,
Some(cluster_id) => *cluster_id,
};

let cluster = self.clusters.clusters.get(cluster_id).unwrap();

// Apply the filter if given.
if let Some(manager_id) = filter_manager_id {
if manager_id != cluster.manager_id {
Expand All @@ -462,7 +465,10 @@ impl DdcBucket {

clusters.push(cluster_info);
}
(clusters, self.clusters.next_cluster_id)
(
clusters,
self.clusters.clusters_ids.len().try_into().unwrap(),
)
}

pub fn message_cluster_distribute_revenues(&mut self, cluster_id: ClusterId) -> Result<()> {
Expand Down
16 changes: 15 additions & 1 deletion bucket/ddc_bucket/cluster/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

use super::entity::{Cluster, ClusterId, ClusterParams};
use crate::ddc_bucket::{AccountId, Error::*, Resource, Result};
use ink_prelude::vec::Vec;
use ink_storage::traits::{SpreadAllocate, SpreadLayout};
use ink_storage::Mapping;

#[derive(SpreadAllocate, SpreadLayout, Default)]
#[cfg_attr(feature = "std", derive(ink_storage::traits::StorageLayout, Debug))]
pub struct ClusterStore {
pub next_cluster_id: u32,
pub next_cluster_id: ClusterId,
pub clusters: Mapping<ClusterId, Cluster>,
pub clusters_ids: Vec<ClusterId>,
}

// https://use.ink/datastructures/storage-layout#packed-vs-non-packed-layout
// There is a buffer with only limited capacity (around 16KB in the default configuration) available.
pub const MAX_CLUSTERS_LEN_IN_VEC: usize = 3900;

impl ClusterStore {
pub fn create(
&mut self,
manager_id: AccountId,
cluster_params: ClusterParams,
resource_per_v_node: Resource,
) -> Result<ClusterId> {
if self.clusters_ids.len() + 1 > MAX_CLUSTERS_LEN_IN_VEC {
return Err(NodesSizeExceedsLimit);
}

let cluster_id = self.next_cluster_id;
self.next_cluster_id = self.next_cluster_id + 1;

let cluster = Cluster::new(manager_id, cluster_params, resource_per_v_node)?;

self.clusters.insert(&cluster_id, &cluster);
self.clusters_ids.push(cluster_id);
Ok(cluster_id)
}

Expand All @@ -43,5 +54,8 @@ impl ClusterStore {

pub fn remove(&mut self, cluster_id: ClusterId) {
self.clusters.remove(cluster_id);
if let Some(pos) = self.clusters_ids.iter().position(|x| *x == cluster_id) {
self.clusters_ids.remove(pos);
};
}
}

0 comments on commit 77ab74d

Please sign in to comment.