ddc_bucket/ddc_bucket/bucket/
entity.rs

1//! The data structure of Buckets.
2
3use ink_prelude::vec::Vec;
4use ink_storage::traits::{PackedLayout, SpreadLayout};
5use scale::{Decode, Encode};
6
7use crate::ddc_bucket::{
8    AccountId, ClusterId, contract_fee::SIZE_PER_RECORD,
9    Error::*, Result,
10};
11use crate::ddc_bucket::contract_fee::{SIZE_ACCOUNT_ID, SIZE_INDEX, SIZE_RESOURCE};
12use crate::ddc_bucket::flow::Flow;
13use crate::ddc_bucket::node::entity::Resource;
14use crate::ddc_bucket::params::store::Params;
15
16pub type BucketId = u32;
17pub type BucketParams = Params;
18
19#[derive(Clone, PartialEq, Encode, Decode, SpreadLayout, PackedLayout)]
20#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
21pub struct Bucket {
22    pub owner_id: AccountId,
23    pub cluster_id: ClusterId,
24    pub flow: Flow,
25    pub resource_reserved: Resource,
26}
27
28#[derive(Clone, PartialEq, Encode, Decode)]
29#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
30pub struct BucketInStatus {
31    pub owner_id: AccountId,
32    pub cluster_id: ClusterId,
33    // The field "flow" is not included because it triggers a bug in polkadot.js.
34    // TODO: find a fix, then return the entire Bucket structure.
35    pub resource_reserved: Resource,
36}
37
38#[derive(Clone, PartialEq, Encode, Decode)]
39#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
40pub struct BucketStatus {
41    pub bucket_id: BucketId,
42    pub bucket: BucketInStatus,
43    pub params: BucketParams,
44    pub writer_ids: Vec<AccountId>,
45    pub rent_covered_until_ms: u64,
46}
47
48impl Bucket {
49    pub const RECORD_SIZE: usize = SIZE_PER_RECORD
50        + SIZE_ACCOUNT_ID + SIZE_INDEX + Flow::RECORD_SIZE + SIZE_RESOURCE;
51
52    pub fn only_owner(&self, caller: AccountId) -> Result<()> {
53        if self.owner_id == caller { Ok(()) } else { Err(UnauthorizedOwner) }
54    }
55
56    pub fn put_resource(&mut self, amount: Resource) {
57        self.resource_reserved += amount;
58    }
59}
60
61impl From<Bucket> for BucketInStatus {
62    fn from(bucket: Bucket) -> Self {
63        Self {
64            owner_id: bucket.owner_id,
65            cluster_id: bucket.cluster_id,
66            resource_reserved: bucket.resource_reserved,
67        }
68    }
69}