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

Commit

Permalink
Merge pull request #57 from Cerebellum-Network/feature/bucket-ownership
Browse files Browse the repository at this point in the history
Feature/bucket ownership
  • Loading branch information
Raid5594 authored Dec 1, 2022
2 parents 80be5b8 + 1babe29 commit b38bd49
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bucket/ddc_bucket/account/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl Account {
self.deposit.increase(cash);
}

pub fn bond(&mut self, time_ms: u64, conv: &CurrencyConverter, payable: Payable) -> Result<()> {
pub fn bond(&mut self, time_ms: u64, conv: &CurrencyConverter, bond_amount: Balance) -> Result<()> {
let payable = Payable(bond_amount);
if self.get_withdrawable(time_ms, conv) >= payable.peek() {
let parsed_payable: u128;
if self.negative.peek() > 0 && payable.peek() >= self.negative.peek() {
Expand Down
9 changes: 7 additions & 2 deletions bucket/ddc_bucket/account/messages.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! The public interface of Accounts and deposits.

use ink_prelude::vec::Vec;
use ink_lang::{EmitEvent, StaticEnv};

use crate::ddc_bucket::{AccountId, Balance, Cash, contract_fee::calculate_contract_fee, DdcBucket, Deposit, Payable, Result, TOKEN};
use crate::ddc_bucket::Error::InsufficientBalance;
use crate::ddc_bucket::perm::entity::Permission;

impl DdcBucket {
pub fn message_get_accounts(&self) -> Vec<AccountId> {
self.accounts.0.keys().cloned().collect()
}

pub fn message_account_deposit(&mut self) -> Result<()> {
// Receive the payable value, minus the contract fee.
let mut cash = Self::receive_cash();
Expand All @@ -24,15 +29,15 @@ impl DdcBucket {
Ok(())
}

pub fn message_account_bond(&mut self, payable: Payable) -> Result<()> {
pub fn message_account_bond(&mut self, bond_amount: Balance) -> Result<()> {
let time_ms = Self::env().block_timestamp();
let account_id = Self::env().caller();
let account = self.accounts.0.get_mut(&account_id)
.ok_or(InsufficientBalance)?;

let conv = &self.accounts.1;

account.bond(time_ms, conv, payable)?;
account.bond(time_ms, conv, bond_amount)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions bucket/ddc_bucket/bucket/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct BucketStatus {
pub bucket: BucketInStatus,
pub params: BucketParams,
pub writer_ids: Vec<AccountId>,
pub reader_ids: Vec<AccountId>,
pub rent_covered_until_ms: u64,
}

Expand Down
16 changes: 13 additions & 3 deletions bucket/ddc_bucket/bucket/messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The public interface to manage Buckets.

use ink_lang::{EmitEvent, StaticEnv};
use ink_prelude::{vec, vec::Vec};
use ink_prelude::vec::Vec;

use crate::ddc_bucket::{AccountId, BucketAllocated, BucketCreated, BucketSettlePayment, DdcBucket, Result};
use crate::ddc_bucket::cluster::entity::{Cluster, ClusterId};
Expand Down Expand Up @@ -96,11 +96,21 @@ impl DdcBucket {
(bucket_statuses, self.buckets.0.len())
}

pub fn message_bucket_list_for_account(&self, owner_id: AccountId) -> Vec<Bucket> {
self.buckets.0
.iter()
.filter(|bucket| bucket.owner_id == owner_id)
.cloned()
.collect()
}

pub fn bucket_calculate_status(&self, bucket_id: BucketId, bucket: Bucket) -> Result<BucketStatus> {
let writer_ids = vec![bucket.owner_id];
let mut writer_ids = self.buckets_perms.get_bucket_readers(bucket_id);
writer_ids.push(bucket.owner_id);
let rent_covered_until_ms = self.accounts.flow_covered_until(&bucket.flow)?;
let params = self.bucket_params.get(bucket_id)?.clone();
Ok(BucketStatus { bucket_id, bucket: bucket.into(), params, writer_ids, rent_covered_until_ms })
let reader_ids = self.buckets_perms.get_bucket_readers(bucket_id);
Ok(BucketStatus { bucket_id, bucket: bucket.into(), params, writer_ids, reader_ids, rent_covered_until_ms })
}

pub fn message_bucket_set_resource_cap(&mut self, bucket_id: BucketId, new_resource_cap: Resource) -> Result<()> {
Expand Down
22 changes: 20 additions & 2 deletions bucket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ pub mod ddc_bucket {
self.message_bucket_list(offset, limit, filter_owner_id)
}

/// Iterate through all buckets and return only those owned by owner
///
/// This method returns bucket struct, not the status
#[ink(message)]
pub fn bucket_list_for_account(&self, owner_id: AccountId) -> Vec<Bucket> {
self.message_bucket_list_for_account(owner_id)
}

/// Set availiablity of the bucket
#[ink(message)]
pub fn bucket_set_availability(&mut self, bucket_id: BucketId, public_availability: bool) -> () {
Expand Down Expand Up @@ -620,8 +628,8 @@ pub mod ddc_bucket {
/// As user, bond some amount of tokens from the withdrawable balance. These funds will be used to
/// pay for CDN nodes
#[ink(message, payable)]
pub fn account_bond(&mut self, payable: Payable) -> () {
self.message_account_bond(payable).unwrap()
pub fn account_bond(&mut self, bond_amount: Balance) -> () {
self.message_account_bond(bond_amount).unwrap()
}

/// As user, unbond a specified amount of tokens
Expand Down Expand Up @@ -719,6 +727,16 @@ pub mod ddc_bucket {
}
// ---- End Admin ----

// ---- Accounts ----
impl DdcBucket {
/// Get all Account IDs stored in the SC
#[ink(message, payable)]
pub fn get_accounts(&self) -> Vec<AccountId> {
self.message_get_accounts()
}
}
// ---- End Accounts ----


// ---- Utils ----
/// One token with 10 decimals.
Expand Down

0 comments on commit b38bd49

Please sign in to comment.