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 #54 from Cerebellum-Network/feature/introduce-cdn-…
Browse files Browse the repository at this point in the history
…node

Feature/introduce cdn node
  • Loading branch information
Raid5594 authored Nov 10, 2022
2 parents 65abbdd + 96da2d6 commit 80be5b8
Show file tree
Hide file tree
Showing 21 changed files with 1,171 additions and 10 deletions.
1 change: 1 addition & 0 deletions bucket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ink_lang_macro = { version = "=3.0.0-rc4", default-features = false, optional =

serde = { version = "1.0.136", optional = true }
serde_json = { version = "1.0.79", optional = true }
more-asserts = { version = "0.3.0" }

[lib]
name = "ddc_bucket"
Expand Down
70 changes: 69 additions & 1 deletion bucket/ddc_bucket/account/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use crate::ddc_bucket::currency::{USD, CurrencyConverter};
#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
pub struct Account {
pub deposit: Cash,
pub bonded: Cash,
pub negative: Cash,
pub unbonded_amount: Cash,
pub unbonded_timestamp: u64,
pub payable_schedule: Schedule,
}

Expand All @@ -26,6 +30,10 @@ impl Account {
pub fn new() -> Account {
Account {
deposit: Cash(0),
bonded: Cash(0),
negative: Cash(0),
unbonded_amount: Cash(0),
unbonded_timestamp: 0,
payable_schedule: Schedule::empty(),
}
}
Expand All @@ -34,6 +42,39 @@ impl Account {
self.deposit.increase(cash);
}

pub fn bond(&mut self, time_ms: u64, conv: &CurrencyConverter, payable: Payable) -> Result<()> {
if self.get_withdrawable(time_ms, conv) >= payable.peek() {
let parsed_payable: u128;
if self.negative.peek() > 0 && payable.peek() >= self.negative.peek() {
parsed_payable = payable.peek() - self.negative.peek();
self.deposit.pay_unchecked(payable);
self.bonded.increase(Cash(parsed_payable));
Ok(())
} else if self.negative.peek() > 0 && payable.peek() < self.negative.peek(){
Err(InsufficientBalance)
} else {
let bonded_amount = Cash(payable.peek());
self.deposit.pay_unchecked(payable);
self.bonded.increase(bonded_amount);
Ok(())
}
} else {
Err(InsufficientBalance)
}
}

pub fn unbond(&mut self, amount_to_unbond: Cash, timestamp: u64) -> Result<()> {
let remaining_bonded = self.bonded.peek() - self.unbonded_amount.peek();
if remaining_bonded >= amount_to_unbond.peek() {
self.bonded.pay_unchecked(Payable(amount_to_unbond.peek()));
self.unbonded_amount.increase(amount_to_unbond);
self.unbonded_timestamp = timestamp + MS_PER_WEEK;
Ok(())
} else {
Err(InsufficientBalance)
}
}

pub fn withdraw(&mut self, time_ms: u64, conv: &CurrencyConverter, payable: Payable) -> Result<()> {
if self.get_withdrawable(time_ms, conv) >= payable.peek() {
self.deposit.pay_unchecked(payable);
Expand All @@ -43,6 +84,31 @@ impl Account {
}
}

// Add logics when balance is below requested
pub fn withdraw_bonded(&mut self, payable: Payable) -> Result<()> {
let remaining_bonded = self.bonded.peek() - self.unbonded_amount.peek();
if remaining_bonded >= payable.peek() {
self.bonded.pay_unchecked(payable);
Ok(())
} else {
let negative_balance = payable.peek() - remaining_bonded;
self.bonded.pay_unchecked(payable);
self.negative.increase(Cash(negative_balance));
Ok(())
}
}

pub fn withdraw_unbonded(&mut self, timestamp: u64) -> Result<()> {
if timestamp >= self.unbonded_timestamp {
self.deposit.increase(self.unbonded_amount);
self.unbonded_amount = Cash(0);
self.unbonded_timestamp = 0;
Ok(())
} else {
Err(BondingPeriodNotFinished)
}
}

pub fn get_withdrawable(&self, time_ms: u64, conv: &CurrencyConverter) -> Balance {
let deposit = self.deposit.peek();
let consumed_usd = self.payable_schedule.value_at_time(time_ms);
Expand Down Expand Up @@ -79,4 +145,6 @@ impl Account {
fn unlock_scheduled_amount(&mut self, unlocked: Balance) {
self.payable_schedule.take_value(unlocked);
}
}
}

pub const MS_PER_WEEK: u64 = 7 * 24 * 3600 * 1000;
44 changes: 42 additions & 2 deletions bucket/ddc_bucket/account/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use ink_lang::{EmitEvent, StaticEnv};

use crate::ddc_bucket::{AccountId, Balance, Cash, contract_fee::calculate_contract_fee, DdcBucket, Deposit, InsufficientBalance, Payable, Result, TOKEN};
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 {
Expand All @@ -23,6 +24,38 @@ impl DdcBucket {
Ok(())
}

pub fn message_account_bond(&mut self, payable: Payable) -> 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)?;
Ok(())
}

pub fn message_account_unbond(&mut self, amount_to_unbond: Cash) -> Result<()> {
let time_ms = Self::env().block_timestamp();
let account_id = Self::env().caller();

self.accounts
.get_mut(&account_id)?
.unbond(amount_to_unbond, time_ms).unwrap();
Ok(())
}

pub fn message_account_withdraw_unbonded(&mut self) -> Result<()> {
let time_ms = Self::env().block_timestamp();
let account_id = Self::env().caller();

self.accounts
.get_mut(&account_id)?
.withdraw_unbonded(time_ms).unwrap();
Ok(())
}

pub fn message_account_get_usd_per_cere(&self) -> Balance {
self.accounts.1.to_usd(1 * TOKEN)
}
Expand All @@ -44,7 +77,6 @@ impl DdcBucket {
}
}


fn _account_withdraw(&mut self, from: AccountId, payable: Payable) -> Result<()> {
let account = self.accounts.0.get_mut(&from)
.ok_or(InsufficientBalance)?;
Expand All @@ -55,6 +87,14 @@ impl DdcBucket {
Ok(())
}

fn _account_withdraw_bonded(&mut self, account_id: AccountId, payable: Payable) -> Result<()> {
let account = self.accounts.0.get_mut(&account_id)
.ok_or(InsufficientBalance)?;

account.withdraw_bonded(payable)?;
Ok(())
}

fn _account_get_net(&self, from: AccountId) -> Balance {
match self.accounts.0.get(&from) {
None => 0,
Expand Down
19 changes: 19 additions & 0 deletions bucket/ddc_bucket/bucket/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ pub struct Bucket {
pub cluster_id: ClusterId,
pub flow: Flow,
pub resource_reserved: Resource,
pub public_availability: bool,
pub resource_consumption_cap: Resource,
}

// Add to status field bucket availability
#[derive(Clone, PartialEq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
pub struct BucketInStatus {
Expand All @@ -33,6 +36,8 @@ pub struct BucketInStatus {
// The field "flow" is not included because it triggers a bug in polkadot.js.
// TODO: find a fix, then return the entire Bucket structure.
pub resource_reserved: Resource,
pub public_availability: bool,
pub resource_consumption_cap: Resource,
}

#[derive(Clone, PartialEq, Encode, Decode)]
Expand All @@ -56,6 +61,18 @@ impl Bucket {
pub fn put_resource(&mut self, amount: Resource) {
self.resource_reserved += amount;
}

pub fn set_cap(&mut self, amount: Resource) {
self.resource_consumption_cap = amount;
}

pub fn set_availability(&mut self, availability: bool) {
self.public_availability = availability;
}

pub fn change_owner(&mut self, owner_id: AccountId) {
self.owner_id = owner_id;
}
}

impl From<Bucket> for BucketInStatus {
Expand All @@ -64,6 +81,8 @@ impl From<Bucket> for BucketInStatus {
owner_id: bucket.owner_id,
cluster_id: bucket.cluster_id,
resource_reserved: bucket.resource_reserved,
public_availability: bucket.public_availability,
resource_consumption_cap: bucket.resource_consumption_cap,
}
}
}
76 changes: 74 additions & 2 deletions bucket/ddc_bucket/bucket/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::ddc_bucket::node::entity::Resource;
use super::entity::{Bucket, BucketId, BucketParams, BucketStatus};

impl DdcBucket {
pub fn message_bucket_create(&mut self, bucket_params: BucketParams, cluster_id: ClusterId) -> Result<BucketId> {
let owner_id = Self::env().caller();
pub fn message_bucket_create(&mut self, bucket_params: BucketParams, cluster_id: ClusterId, owner_id: Option<AccountId>) -> Result<BucketId> {
let owner_id = owner_id.unwrap_or(Self::env().caller());
let record_size0 = self.accounts.create_if_not_exist(owner_id);
let bucket_id = self.buckets.create(owner_id, cluster_id);
let (params_id, record_size2) = self.bucket_params.create(bucket_params)?;
Expand All @@ -21,6 +21,14 @@ impl DdcBucket {
Ok(bucket_id)
}

pub fn message_bucket_change_owner(&mut self, bucket_id: BucketId, owner_id: AccountId) -> Result<()> {
let caller = Self::env().caller();
let bucket = self.buckets.get_mut(bucket_id)?;
bucket.only_owner(caller)?;
bucket.change_owner(owner_id);
Ok(())
}

pub fn message_bucket_alloc_into_cluster(&mut self, bucket_id: BucketId, resource: Resource) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;
Expand Down Expand Up @@ -95,6 +103,70 @@ impl DdcBucket {
Ok(BucketStatus { bucket_id, bucket: bucket.into(), params, writer_ids, rent_covered_until_ms })
}

pub fn message_bucket_set_resource_cap(&mut self, bucket_id: BucketId, new_resource_cap: Resource) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
bucket.set_cap(new_resource_cap);
Ok(())
}

pub fn message_bucket_set_availability(&mut self, bucket_id: BucketId, public_availability: bool) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
bucket.set_availability(public_availability);
Ok(())
}

pub fn message_get_bucket_writers(&mut self, bucket_id: BucketId) -> Result<Vec<AccountId>> {
let writers = self.buckets_perms.get_bucket_writers(bucket_id);
Ok(writers)
}

pub fn message_grant_writer_permission(&mut self, bucket_id: BucketId, writer: AccountId) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
self.buckets_perms.grant_writer_permission(bucket_id, writer).unwrap();
Ok(())
}

pub fn message_revoke_writer_permission(&mut self, bucket_id: BucketId, writer: AccountId) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
self.buckets_perms.revoke_writer_permission(bucket_id, writer).unwrap();
Ok(())
}

pub fn message_get_bucket_readers(&mut self, bucket_id: BucketId) -> Result<Vec<AccountId>> {
let readers = self.buckets_perms.get_bucket_readers(bucket_id);
Ok(readers)
}

pub fn message_grant_reader_permission(&mut self, bucket_id: BucketId, reader: AccountId) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
self.buckets_perms.grant_reader_permission(bucket_id, reader).unwrap();
Ok(())
}

pub fn message_revoke_reader_permission(&mut self, bucket_id: BucketId, reader: AccountId) -> Result<()> {
let bucket = self.buckets.get_mut(bucket_id)?;
let cluster = self.clusters.get_mut(bucket.cluster_id)?;

Self::only_owner_or_cluster_manager(bucket, cluster)?;
self.buckets_perms.revoke_reader_permission(bucket_id, reader).unwrap();
Ok(())
}

fn only_owner_or_cluster_manager(bucket: &Bucket, cluster: &Cluster) -> Result<()> {
let caller = Self::env().caller();
cluster.only_manager(caller)
Expand Down
2 changes: 2 additions & 0 deletions bucket/ddc_bucket/bucket/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl BucketStore {
cluster_id,
flow: Flow { from: owner_id, schedule: Schedule::empty() },
resource_reserved: 0,
resource_consumption_cap: 0,
public_availability: false,
};
let bucket_id = self.0.len();
self.0.push(bucket);
Expand Down
3 changes: 3 additions & 0 deletions bucket/ddc_bucket/buckets_perms/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Bucket perms management.

pub mod store;
Loading

0 comments on commit 80be5b8

Please sign in to comment.