ddc_bucket/ddc_bucket/node/
store.rs

1//! The store where to create and access Nodes.
2
3use ink_storage::{
4    collections::Vec as InkVec,
5    traits,
6};
7
8use crate::ddc_bucket::{AccountId, Balance, Error::*, Result};
9use crate::ddc_bucket::node::entity::Resource;
10
11use super::entity::{Node, NodeId};
12
13#[derive(traits::SpreadLayout, Default)]
14#[cfg_attr(feature = "std", derive(traits::StorageLayout, Debug))]
15pub struct NodeStore(pub InkVec<Node>);
16
17impl NodeStore {
18    pub fn create(&mut self,
19                  provider_id: AccountId,
20                  rent_per_month: Balance,
21                  capacity: Resource,
22    ) -> NodeId {
23        let node_id = self.0.len();
24        let node = Node { provider_id, rent_per_month, free_resource: capacity };
25
26        self.0.push(node);
27        node_id
28    }
29
30    pub fn get(&self, node_id: NodeId) -> Result<&Node> {
31        self.0.get(node_id).ok_or(NodeDoesNotExist)
32    }
33
34    pub fn get_mut(&mut self, node_id: NodeId) -> Result<&mut Node> {
35        self.0.get_mut(node_id).ok_or(NodeDoesNotExist)
36    }
37}