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

Commit

Permalink
Add BucketAvailabilityUpdated event and test (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisglotov authored May 16, 2023
1 parent dc79c2d commit f7f70f6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
3 changes: 2 additions & 1 deletion bucket/ddc_bucket/bucket/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use ink_lang::codegen::{EmitEvent, StaticEnv};
use ink_prelude::vec::Vec;

use crate::ddc_bucket::{AccountId, BucketAllocated, BucketCreated, BucketSettlePayment, DdcBucket, Result};
use crate::ddc_bucket::{AccountId, BucketAllocated, BucketCreated, BucketSettlePayment, BucketAvailabilityUpdated, DdcBucket, Result};
use crate::ddc_bucket::cluster::entity::{Cluster, ClusterId};
use crate::ddc_bucket::node::entity::Resource;

Expand Down Expand Up @@ -127,6 +127,7 @@ impl DdcBucket {

Self::only_owner_or_cluster_manager(bucket, cluster)?;
bucket.set_availability(public_availability);
Self::env().emit_event(BucketAvailabilityUpdated { bucket_id, public_availability });
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions bucket/ddc_bucket/tests/env_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn print_events(events: &[Event]) {
Event::BucketCreated(ev) => println!("EVENT {:?}", ev),
Event::BucketAllocated(ev) => println!("EVENT {:?}", ev),
Event::BucketSettlePayment(ev) => println!("EVENT {:?}", ev),
Event::BucketAvailabilityUpdated(ev) => println!("EVENT {:?}", ev),
Event::Deposit(ev) => println!("EVENT {:?}", ev),
Event::GrantPermission(ev) => println!("EVENT {:?}", ev),
Event::RevokePermission(ev) => println!("EVENT {:?}", ev),
Expand Down
31 changes: 26 additions & 5 deletions bucket/ddc_bucket/tests/test_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,11 @@ fn cluster_add_node() {

set_caller_value(new_provider_id, CONTRACT_FEE_LIMIT);
let new_node_id = ctx.contract.node_create(
rent_per_month,
node_params.to_string(),
capacity,
node_tag,
ctx.manager,
rent_per_month,
node_params.to_string(),
capacity,
node_tag,
ctx.manager,
);

set_caller_value(new_provider_id, CONTRACT_FEE_LIMIT);
Expand Down Expand Up @@ -1242,6 +1242,27 @@ fn bucket_list_works() {
);
}

#[ink::test]
fn bucket_set_availability_works() {
let ctx = &mut new_cluster();
let test_bucket = &new_bucket(ctx);
let status = ctx.contract.bucket_get(test_bucket.bucket_id)?;
assert_eq!(status.bucket.public_availability, false);

// Set public availability
ctx.contract
.bucket_set_availability(test_bucket.bucket_id, true);

// Check the last event.
let ev = get_events().pop().unwrap();
assert!(matches!(ev, Event::BucketAvailabilityUpdated(ev) if ev ==
BucketAvailabilityUpdated { bucket_id: test_bucket.bucket_id, public_availability: true }));

// Check the changed params.
let status = ctx.contract.bucket_get(test_bucket.bucket_id)?;
assert_eq!(status.bucket.public_availability, true);
}

#[ink::test]
fn node_list_works() {
let mut ddc_bucket = setup();
Expand Down
25 changes: 21 additions & 4 deletions bucket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub mod ddc_bucket {
use cash::*;
use cluster::{entity::*, store::*};
use committer::store::*;
use ink_storage::traits::SpreadAllocate;
use node::{entity::*, store::*};
use params::store::*;
use perm::store::*;
use ink_storage::traits::SpreadAllocate;

use crate::ddc_bucket::account::entity::Account;
use crate::ddc_bucket::cdn_cluster::entity::CdnClusterStatus;
Expand Down Expand Up @@ -85,7 +85,7 @@ pub mod ddc_bucket {
pub fn new() -> Self {
ink_lang::utils::initialize_contract(|contract: &mut Self| {
let operator = Self::env().caller();

contract.committer_store.init(operator);
contract.protocol_store.init(operator, DEFAULT_BASIS_POINTS);

Expand All @@ -94,14 +94,21 @@ pub mod ddc_bucket {
contract
.perms
.grant_permission(admin_id, &Permission::SuperAdmin);

// Reserve IDs 0.
let _ = contract.accounts.create_if_not_exist(AccountId::default());
let _ = contract.cdn_nodes.create(AccountId::default(), 0);
let _ = contract.cdn_node_params.create("".to_string()).unwrap();
let _ = contract
.nodes
.create(AccountId::default(), 0, 0, NodeTag::ACTIVE, AccountId::default()).unwrap();
.create(
AccountId::default(),
0,
0,
NodeTag::ACTIVE,
AccountId::default(),
)
.unwrap();
let _ = contract.node_params.create("".to_string()).unwrap();
let _ = contract
.clusters
Expand Down Expand Up @@ -152,6 +159,16 @@ pub mod ddc_bucket {
cluster_id: ClusterId,
}

/// The availiablity of the bucket was updated.
#[ink(event)]
#[cfg_attr(feature = "std", derive(PartialEq, Debug, scale_info::TypeInfo))]
pub struct BucketAvailabilityUpdated {
#[ink(topic)]
bucket_id: BucketId,
#[ink(topic)]
public_availability: bool,
}

impl DdcBucket {
/// Create a new bucket and return its `bucket_id`.
///
Expand Down

0 comments on commit f7f70f6

Please sign in to comment.