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

Commit

Permalink
chore(lib): returning the Result for mutable endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Jun 30, 2023
1 parent 860fac7 commit f0a849a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 112 deletions.
7 changes: 4 additions & 3 deletions bucket/ddc_bucket/account/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ impl DdcBucket {
self.protocol.curr_converter.to_usd(1 * TOKEN)
}

pub fn message_account_set_usd_per_cere(&mut self, usd_per_cere: Balance) {
self.only_with_permission(Permission::SetExchangeRate).unwrap();
self.protocol.curr_converter.set_usd_per_cere(usd_per_cere)
pub fn message_account_set_usd_per_cere(&mut self, usd_per_cere: Balance) -> Result<()> {
self.only_with_permission(Permission::SetExchangeRate)?;
self.protocol.curr_converter.set_usd_per_cere(usd_per_cere);
Ok(())
}

pub fn receive_cash() -> Cash {
Expand Down
12 changes: 5 additions & 7 deletions bucket/ddc_bucket/bucket/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl DdcBucket {
}

pub fn message_bucket_get(&self, bucket_id: BucketId) -> Result<BucketStatus> {
let bucket = self.buckets.get(bucket_id)?.clone();
let bucket = self.buckets.get(bucket_id)?;
self.bucket_calculate_status(bucket_id, bucket)
}

Expand Down Expand Up @@ -149,9 +149,8 @@ impl DdcBucket {
Ok(())
}

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

pub fn message_grant_writer_permission(&mut self, bucket_id: BucketId, writer: AccountId) -> Result<()> {
Expand All @@ -174,9 +173,8 @@ impl DdcBucket {
Ok(())
}

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

pub fn message_grant_reader_permission(&mut self, bucket_id: BucketId, reader: AccountId) -> Result<()> {
Expand Down
6 changes: 2 additions & 4 deletions bucket/ddc_bucket/bucket/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ impl BucketStore {

// get accounts with permission for bucket reading
pub fn get_bucket_readers(&self, key: BucketId) -> Vec<AccountId> {
let readers = self.readers.get(&key)
self.readers.get(&key)
.unwrap_or(Vec::new())
.iter()
.cloned()
.collect();

return readers;
.collect()
}

// grant permission for bucket reading for some account
Expand Down
11 changes: 5 additions & 6 deletions bucket/ddc_bucket/committer/messages.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use ink_lang::codegen::StaticEnv;
use ink_env::Error;
use ink_prelude::vec::Vec;

use crate::ddc_bucket::{AccountId, DdcBucket, CdnNodeKey};
use crate::ddc_bucket::{AccountId, DdcBucket, CdnNodeKey, Result, Error::*};
use super::store::{Commit, EraConfig, EraStatus, EraAndTimestamp};

pub type Result<T> = core::result::Result<T, Error>;

impl DdcBucket {

pub fn message_set_commit(&mut self, cdn_owner: AccountId, cdn_node_key: CdnNodeKey, commit: Commit) {
pub fn message_set_commit(&mut self, cdn_owner: AccountId, cdn_node_key: CdnNodeKey, commit: Commit) -> Result<()> {
self.committer.set_commit(cdn_owner, cdn_node_key, commit);
Ok(())
}

pub fn message_get_commit(&self, cdn_owner: AccountId) -> Vec<(CdnNodeKey, Commit)> {
Expand All @@ -25,7 +24,7 @@ impl DdcBucket {
let caller = Self::env().caller();

match self.committer.set_era(caller, era_config) {
Err(_e) => panic!("Setting era failed"),
Err(_e) => Err(EraSettingFailed),
Ok(_v) => Ok(()),
}
}
Expand All @@ -38,5 +37,5 @@ impl DdcBucket {
pub fn message_get_era_settings(&self) -> EraConfig {
self.committer.get_era_settings()
}

}
8 changes: 4 additions & 4 deletions bucket/ddc_bucket/tests/setup_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn setup_cluster() -> TestCluster {


set_caller(manager_id);
contract.cluster_reserve_resource(cluster_id, reserved_resource);
contract.cluster_reserve_resource(cluster_id, reserved_resource).unwrap();


let nodes_keys = vec![
Expand Down Expand Up @@ -281,16 +281,16 @@ pub fn setup_bucket(ctx: &mut TestCluster) -> TestBucket {
"{}".to_string(),
ctx.cluster_id,
None
);
).unwrap();

// Reserve some resources for the bucket from the cluster.
set_caller_value(owner_id, CONTRACT_FEE_LIMIT);
let resource = 1;
ctx.contract.bucket_alloc_into_cluster(bucket_id, resource);
ctx.contract.bucket_alloc_into_cluster(bucket_id, resource).unwrap();

// Deposit some value to pay for buckets.
set_caller_value(owner_id, 10 * TOKEN);
ctx.contract.account_deposit();
ctx.contract.account_deposit().unwrap();

TestBucket {
bucket_id,
Expand Down
4 changes: 2 additions & 2 deletions bucket/ddc_bucket/tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn account_deposit_ok() {

// Deposit some value.
set_caller_value(account_id, deposit);
contract.account_deposit();
contract.account_deposit()?;

let account = contract.account_get(account_id)?;
assert_eq!(
Expand All @@ -43,7 +43,7 @@ fn account_deposit_ok() {

// Deposit more value.
set_caller_value(account_id, deposit);
contract.account_deposit();
contract.account_deposit()?;

let account = contract.account_get(account_id)?;
assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions bucket/ddc_bucket/tests/test_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn admin_withdraw_ok() {

set_caller(admin_id());

contract.admin_withdraw(9);
contract.admin_withdraw(9).unwrap();

assert_eq!(balance_of(contract_id()), 1);
}
Expand All @@ -43,7 +43,7 @@ fn admin_withdraw_err_if_not_admin() {

set_caller(not_admin_id());

contract.admin_withdraw(9); // panic.
contract.admin_withdraw(9).unwrap(); // panic.
}


Expand All @@ -67,7 +67,7 @@ fn admin_grant_ok() {

set_caller(new_admin_id);

contract.admin_withdraw(9);
contract.admin_withdraw(9).unwrap();
}


Expand Down Expand Up @@ -107,7 +107,7 @@ fn admin_revoke_ok() {
// Cannot withdraw because no more permission.
set_caller(admin_id());

contract.admin_withdraw(9); // panic.
contract.admin_withdraw(9)?;
}


Expand Down
14 changes: 7 additions & 7 deletions bucket/ddc_bucket/tests/test_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn do_bucket_pays_cluster(
advance_block::<DefaultEnvironment>();
// Pay the due thus far.
set_caller_value(ctx.manager_id, CONTRACT_FEE_LIMIT);
ctx.contract.bucket_settle_payment(test_bucket.bucket_id);
ctx.contract.bucket_settle_payment(test_bucket.bucket_id)?;
let timestamp_after = block_timestamp::<DefaultEnvironment>();

// Check the last event.
Expand Down Expand Up @@ -103,7 +103,7 @@ fn bucket_pays_cluster_at_new_rate_ok() {
// Change the currency exchange rate.
let usd_per_cere = 2;
set_caller(admin_id());
ctx.contract.account_set_usd_per_cere(usd_per_cere * TOKEN);
ctx.contract.account_set_usd_per_cere(usd_per_cere * TOKEN)?;

do_bucket_pays_cluster(ctx, test_bucket, usd_per_cere).unwrap();
}
Expand Down Expand Up @@ -176,7 +176,7 @@ fn bucket_change_params_ok() {
// Change params.
set_caller_value(test_bucket.owner_id, CONTRACT_FEE_LIMIT);
ctx.contract
.bucket_change_params(test_bucket.bucket_id, "new params".to_string());
.bucket_change_params(test_bucket.bucket_id, "new params".to_string())?;

// Check the changed params.
let status = ctx.contract.bucket_get(test_bucket.bucket_id)?;
Expand All @@ -192,7 +192,7 @@ fn bucket_change_params_only_owner() {
// Change params.
set_caller_value(get_accounts().bob, CONTRACT_FEE_LIMIT);
ctx.contract
.bucket_change_params(test_bucket.bucket_id, "new params".to_string());
.bucket_change_params(test_bucket.bucket_id, "new params".to_string())?;
// Panic.
}

Expand All @@ -211,11 +211,11 @@ fn bucket_list_ok() {
let cluster_id = 0;

set_caller_value(owner_id1, CONTRACT_FEE_LIMIT);
let bucket_id1 = ddc_bucket.bucket_create("".to_string(), cluster_id, None);
let bucket_status1 = ddc_bucket.bucket_get(bucket_id1).unwrap();
let bucket_id1 = ddc_bucket.bucket_create("".to_string(), cluster_id, None)?;
let bucket_status1 = ddc_bucket.bucket_get(bucket_id1)?;

set_caller_value(owner_id2, CONTRACT_FEE_LIMIT);
let bucket_id2 = ddc_bucket.bucket_create("".to_string(), cluster_id, None);
let bucket_id2 = ddc_bucket.bucket_create("".to_string(), cluster_id, None)?;
let bucket_status2 = ddc_bucket.bucket_get(bucket_id2)?;

assert_ne!(bucket_id1, bucket_id2);
Expand Down
22 changes: 11 additions & 11 deletions bucket/ddc_bucket/tests/test_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ fn cluster_reserve_resource_ok() {
set_caller(ctx.manager_id);

// Reserve more resources.
ctx.contract.cluster_reserve_resource(ctx.cluster_id, 5);
ctx.contract.cluster_reserve_resource(ctx.cluster_id, 5)?;

// Check the last event.
let ev = get_events().pop().unwrap();
Expand Down Expand Up @@ -1168,7 +1168,7 @@ fn cluster_distribute_revenue_ok() {
advance_block::<DefaultEnvironment>();
// Pay the due thus far.
set_caller_value(ctx.manager_id, CONTRACT_FEE_LIMIT);
ctx.contract.bucket_settle_payment(test_bucket.bucket_id);
ctx.contract.bucket_settle_payment(test_bucket.bucket_id)?;

// Get state before the distribution.
let to_distribute = ctx
Expand All @@ -1193,14 +1193,14 @@ fn cluster_distribute_revenue_ok() {
network_fee_bp,
network_fee_destination: AccountId::default(),
cluster_management_fee_bp,
});
})?;

let burned_fee = to_distribute * network_fee_bp / BASIS_POINTS;
let manager_fee = (to_distribute - burned_fee) * cluster_management_fee_bp / BASIS_POINTS;
let provider_fee: u128 = (to_distribute - burned_fee - manager_fee) / 3;

// Distribute the revenues of the cluster to providers.
ctx.contract.cluster_distribute_revenues(ctx.cluster_id);
ctx.contract.cluster_distribute_revenues(ctx.cluster_id)?;

// Check the last events.
let mut events = get_events();
Expand Down Expand Up @@ -1619,12 +1619,12 @@ fn cluster_distribute_cdn_revenue_ok() {

let usd_per_cere = TOKEN / 100;
set_caller(admin_id());
ctx.contract.account_set_usd_per_cere(usd_per_cere);
ctx.contract.account_set_usd_per_cere(usd_per_cere)?;

let usd_amount = ctx.contract.account_get_usd_per_cere();
println!("Current usd amount is {}", usd_amount);

let rate = ctx.contract.cdn_get_rate(ctx.cluster_id);
let rate = ctx.contract.cdn_get_rate(ctx.cluster_id)?;
println!("The current rate is {}", rate);

let usd_per_kb = rate / KB_PER_GB;
Expand All @@ -1634,13 +1634,13 @@ fn cluster_distribute_cdn_revenue_ok() {
println!("The current cere rate per kb {}", cere_per_kb);

set_caller_value(ctx.provider_id0, 10 * TOKEN);
ctx.contract.account_deposit();
ctx.contract.account_deposit()?;

set_caller_value(ctx.provider_id0, 10 * TOKEN);
ctx.contract.account_bond(5 * TOKEN);
ctx.contract.account_bond(5 * TOKEN)?;

set_caller(admin_id());
ctx.contract.admin_set_protocol_fee_bp(1_000);
ctx.contract.admin_set_protocol_fee_bp(1_000)?;

set_caller(ctx.provider_id0);
let account0_before_putting = ctx.contract.accounts.get(&ctx.provider_id0).unwrap();
Expand All @@ -1652,7 +1652,7 @@ fn cluster_distribute_cdn_revenue_ok() {
vec![(ctx.cdn_node_key0, 1000), (ctx.cdn_node_key1, 541643)],
vec![],
5,
);
)?;
let account0_after_putting = ctx.contract.accounts.get(&ctx.provider_id0).unwrap();
println!("After putting revenue: {:?}", account0_after_putting);

Expand Down Expand Up @@ -1680,7 +1680,7 @@ fn cluster_distribute_cdn_revenue_ok() {

set_caller(ctx.provider_id0);

ctx.contract.cluster_distribute_cdn_revenue(ctx.cluster_id);
ctx.contract.cluster_distribute_cdn_revenue(ctx.cluster_id)?;

let cdn_node0 = ctx.contract.cdn_nodes.get(ctx.cdn_node_key0).unwrap();
let cdn_node1 = ctx.contract.cdn_nodes.get(ctx.cdn_node_key1).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions bucket/ddc_bucket/tests/test_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn currency_conversion_set_rate_ok() {
println!("{}", usd_per_cere);

set_caller(admin_id());
contract.account_set_usd_per_cere(usd_per_cere);
contract.account_set_usd_per_cere(usd_per_cere)?;

assert_eq!(
contract.account_get_usd_per_cere(),
Expand All @@ -42,7 +42,7 @@ fn currency_conversion_set_rate_err_if_not_admin() {
let not_admin = get_accounts().bob;

set_caller(not_admin);
contract.account_set_usd_per_cere(9);
contract.account_set_usd_per_cere(9)?;
}


Expand All @@ -56,13 +56,13 @@ fn converter_ok() {
// The provider stops trusting the manager_id.
println!("Cdn cluster id is {}", ctx.cluster_id);
set_caller(ctx.manager_id);
ctx.contract.cdn_set_rate(ctx.cluster_id, 3_750_000_000);
ctx.contract.cdn_set_rate(ctx.cluster_id, 3_750_000_000)?;
set_caller(ctx.provider_id0);
let rate = ctx.contract.cdn_get_rate(ctx.cluster_id);
let rate = ctx.contract.cdn_get_rate(ctx.cluster_id)?;

let usd_per_cere = TOKEN / 100;
set_caller(admin_id());
ctx.contract.account_set_usd_per_cere(usd_per_cere);
ctx.contract.account_set_usd_per_cere(usd_per_cere)?;

let usd_amount = ctx.contract.account_get_usd_per_cere();
println!("Current usd amount is {}", usd_amount);
Expand Down
Loading

0 comments on commit f0a849a

Please sign in to comment.