Skip to content

Commit

Permalink
Merge branch 'dev' into feature/extend-clusters-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 authored Dec 1, 2023
2 parents e48f1ad + b4c80f7 commit 1c3b586
Show file tree
Hide file tree
Showing 20 changed files with 1,306 additions and 365 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- 'dev'
- 'deployment-0.9.30'
workflow_dispatch:

env:
Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
tags: |
${{ steps.login-ecr.outputs.registry }}/pos-network-node:${{ env.GITHUB_SHA }}
${{ steps.login-ecr.outputs.registry }}/pos-network-node:dev-latest
${{ steps.login-ecr.outputs.registry }}/pos-network-node:deployment-0.9.30-latest
- name: Copy wasm artifacts from the image
run: |
Expand Down
330 changes: 166 additions & 164 deletions Cargo.lock

Large diffs are not rendered by default.

65 changes: 27 additions & 38 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use codec::{Decode, Encode, HasCompact};
use ddc_primitives::{BucketId, ClusterId};
use ddc_traits::{
cluster::{ClusterCreator, ClusterVisitor},
customer::{CustomerCharger, CustomerChargerError},
customer::CustomerCharger,
};
use frame_support::{
parameter_types,
Expand Down Expand Up @@ -139,17 +139,13 @@ impl<
.ok_or(Error::<T>::ArithmeticOverflow)?;
if temp <= value {
unlocking_balance = temp;
self.active =
self.active.checked_sub(&last.value).ok_or(Error::<T>::ArithmeticUnderflow)?;
self.unlocking.pop();
} else {
let diff =
value.checked_sub(&unlocking_balance).ok_or(Error::<T>::ArithmeticUnderflow)?;

unlocking_balance =
unlocking_balance.checked_add(&diff).ok_or(Error::<T>::ArithmeticOverflow)?;
self.active =
self.active.checked_sub(&diff).ok_or(Error::<T>::ArithmeticUnderflow)?;
last.value =
last.value.checked_sub(&diff).ok_or(Error::<T>::ArithmeticUnderflow)?;
}
Expand Down Expand Up @@ -442,7 +438,7 @@ pub mod pallet {
.map_err(|_| Error::<T>::NoMoreChunks)?;
};

Self::update_ledger(&owner, &ledger);
<Ledger<T>>::insert(&owner, &ledger);

Self::deposit_event(Event::<T>::InitialDepositUnlock(ledger.owner, value));
}
Expand Down Expand Up @@ -481,7 +477,8 @@ pub mod pallet {
log::debug!("Updating ledger");
// This was the consequence of a partial deposit unlock. just update the ledger and
// move on.
Self::update_ledger(&owner, &ledger);
<Ledger<T>>::insert(&owner, &ledger);

// This is only an update, so we use less overall weight.
Some(<T as pallet::Config>::WeightInfo::withdraw_unlocked_deposit_update())
};
Expand Down Expand Up @@ -523,11 +520,9 @@ pub mod pallet {
owner: &T::AccountId,
ledger: &AccountsLedger<T::AccountId, BalanceOf<T>, T>,
) -> DispatchResult {
let account_id = Self::account_id();

<T as pallet::Config>::Currency::transfer(
owner,
&account_id,
&Self::account_id(),
ledger.total,
ExistenceRequirement::KeepAlive,
)?;
Expand All @@ -536,14 +531,6 @@ pub mod pallet {
Ok(())
}

/// Update the ledger for a owner.
fn update_ledger(
owner: &T::AccountId,
ledger: &AccountsLedger<T::AccountId, BalanceOf<T>, T>,
) {
<Ledger<T>>::insert(owner, ledger);
}

/// Remove all associated data of a owner account from the accounts system.
///
/// Assumes storage is upgraded before calling.
Expand All @@ -564,47 +551,49 @@ pub mod pallet {
content_owner: T::AccountId,
billing_vault: T::AccountId,
amount: u128,
) -> Result<u128, CustomerChargerError> {
) -> Result<u128, DispatchError> {
let actually_charged: BalanceOf<T>;
let mut ledger = Self::ledger(&content_owner).ok_or(CustomerChargerError::NotOwner)?;
let mut amount_to_deduct = amount.saturated_into::<BalanceOf<T>>();
let mut ledger = Self::ledger(&content_owner).ok_or(Error::<T>::NotOwner)?;
let amount_to_deduct = amount.saturated_into::<BalanceOf<T>>();

ensure!(ledger.total >= ledger.active, CustomerChargerError::ArithmeticUnderflow);
if ledger.active >= amount_to_deduct {
actually_charged = amount_to_deduct;
ledger.active = ledger
.active
.checked_sub(&amount_to_deduct)
.ok_or(CustomerChargerError::ArithmeticUnderflow)?;
.ok_or(Error::<T>::ArithmeticUnderflow)?;
ledger.total = ledger
.total
.checked_sub(&amount_to_deduct)
.ok_or(CustomerChargerError::ArithmeticUnderflow)?;
Self::update_ledger(&content_owner, &ledger);
.ok_or(Error::<T>::ArithmeticUnderflow)?;

<Ledger<T>>::insert(&content_owner, &ledger);
} else {
let diff = amount_to_deduct
.checked_sub(&ledger.active)
.ok_or(CustomerChargerError::ArithmeticUnderflow)?;
actually_charged = diff;
.ok_or(Error::<T>::ArithmeticUnderflow)?;

actually_charged = ledger.active;
ledger.total = ledger
.total
.checked_sub(&ledger.active)
.ok_or(CustomerChargerError::ArithmeticUnderflow)?;
amount_to_deduct = ledger.active;
.ok_or(Error::<T>::ArithmeticUnderflow)?;
ledger.active = BalanceOf::<T>::zero();
let (ledger, _charged) = ledger
.charge_unlocking(diff)
.map_err(|_| CustomerChargerError::UnlockFailed)?;
Self::update_ledger(&content_owner, &ledger);
};

let (ledger, charged) = ledger.charge_unlocking(diff)?;

actually_charged.checked_add(&charged).ok_or(Error::<T>::ArithmeticUnderflow)?;

<Ledger<T>>::insert(&content_owner, &ledger);
}

<T as pallet::Config>::Currency::transfer(
&Self::account_id(),
&billing_vault,
amount_to_deduct,
ExistenceRequirement::KeepAlive,
)
.map_err(|_| CustomerChargerError::TransferFailed)?;
actually_charged,
ExistenceRequirement::AllowDeath,
)?;

Self::deposit_event(Event::<T>::Charged(content_owner, amount_to_deduct));

Ok(actually_charged.saturated_into::<u128>())
Expand Down
6 changes: 4 additions & 2 deletions pallets/ddc-customers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ impl ExtBuilder {
sp_tracing::try_init_simple();
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

let _ = pallet_balances::GenesisConfig::<Test> { balances: vec![(1, 100), (2, 100)] }
.assimilate_storage(&mut storage);
let _ = pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 100), (2, 100), (3, 1000)],
}
.assimilate_storage(&mut storage);

TestExternalities::new(storage)
}
Expand Down
112 changes: 101 additions & 11 deletions pallets/ddc-customers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,27 @@ fn deposit_and_deposit_extra_works() {
BalancesError::<Test, _>::KeepAlive
);

let amount1 = 10_u128;
// Deposited
assert_ok!(DdcCustomers::deposit(RuntimeOrigin::signed(account_1), 10_u128));
assert_ok!(DdcCustomers::deposit(RuntimeOrigin::signed(account_1), amount1));

// Check storage
assert_eq!(
DdcCustomers::ledger(&account_1),
Some(AccountsLedger {
owner: 1,
total: 10_u128,
active: 10_u128,
owner: account_1,
total: amount1,
active: amount1,
unlocking: Default::default(),
})
);

// Checking that event was emitted
System::assert_last_event(Event::Deposited(account_1, 10).into());
System::assert_last_event(Event::Deposited(account_1, amount1).into());

// Deposit should fail when called the second time
assert_noop!(
DdcCustomers::deposit(RuntimeOrigin::signed(account_1), 10_u128),
DdcCustomers::deposit(RuntimeOrigin::signed(account_1), amount1),
Error::<Test>::AlreadyPaired
);

Expand All @@ -110,21 +111,110 @@ fn deposit_and_deposit_extra_works() {
);

// Deposited extra
assert_ok!(DdcCustomers::deposit_extra(RuntimeOrigin::signed(account_1), 20_u128));
let amount2 = 20_u128;
assert_ok!(DdcCustomers::deposit_extra(RuntimeOrigin::signed(account_1), amount2));

// Check storage
assert_eq!(
DdcCustomers::ledger(&account_1),
Some(AccountsLedger {
owner: 1,
total: 30_u128,
active: 30_u128,
owner: account_1,
total: amount1 + amount2,
active: amount1 + amount2,
unlocking: Default::default(),
})
);

// Checking that event was emitted
System::assert_last_event(Event::Deposited(account_1, 20).into());
System::assert_last_event(Event::Deposited(account_1, amount2).into());
})
}

#[test]
fn charge_content_owner_works() {
ExtBuilder.build_and_execute(|| {
System::set_block_number(1);

let account_3 = 3;
let vault = 4;
let deposit = 100_u128;

let balance_before_deposit = Balances::free_balance(account_3);
// Deposited
assert_ok!(DdcCustomers::deposit(RuntimeOrigin::signed(account_3), deposit));
let balance_after_deposit = Balances::free_balance(account_3);
assert_eq!(balance_before_deposit - deposit, balance_after_deposit);

let pallet_balance = Balances::free_balance(DdcCustomers::account_id());
assert_eq!(deposit, pallet_balance);

// Check storage
assert_eq!(
DdcCustomers::ledger(&account_3),
Some(AccountsLedger {
owner: account_3,
total: deposit,
active: deposit,
unlocking: Default::default(),
})
);

// Checking that event was emitted
System::assert_last_event(Event::Deposited(account_3, deposit).into());

// successful transfer
let charge1 = 10;
let charged = DdcCustomers::charge_content_owner(account_3, vault, charge1).unwrap();
assert_eq!(charge1, charged);

let vault_balance = Balances::free_balance(vault);
assert_eq!(charged, vault_balance);

let account_balance = Balances::free_balance(account_3);
assert_eq!(balance_after_deposit, account_balance);

let pallet_balance_after_charge = Balances::free_balance(DdcCustomers::account_id());
assert_eq!(pallet_balance - charged, pallet_balance_after_charge);

// Check storage
assert_eq!(
DdcCustomers::ledger(&account_3),
Some(AccountsLedger {
owner: account_3,
total: deposit - charge1,
active: deposit - charge1,
unlocking: Default::default(),
})
);

// failed transfer
let charge2 = 100u128;
let charge_result = DdcCustomers::charge_content_owner(account_3, vault, charge2).unwrap();
assert_eq!(
DdcCustomers::ledger(&account_3),
Some(AccountsLedger {
owner: account_3,
total: 0,
active: 0,
unlocking: Default::default(),
})
);

assert_eq!(0, Balances::free_balance(DdcCustomers::account_id()));
assert_eq!(charge_result, deposit - charge1);

assert_ok!(DdcCustomers::deposit_extra(RuntimeOrigin::signed(account_3), deposit));
assert_eq!(
DdcCustomers::ledger(&account_3),
Some(AccountsLedger {
owner: account_3,
total: deposit,
active: deposit,
unlocking: Default::default(),
})
);

assert_eq!(deposit, Balances::free_balance(DdcCustomers::account_id()));
})
}

Expand Down
6 changes: 6 additions & 0 deletions pallets/ddc-nodes/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ benchmarks! {
p2p_port: 65000u16,
});
}

impl_benchmark_test_suite!(
DdcNodes,
crate::mock::ExtBuilder.build(),
crate::mock::Test,
);
}
1 change: 1 addition & 0 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub mod pallet {
fn delete(node_pub_key: NodePubKey) -> Result<(), NodeRepositoryError>;
}

#[derive(Debug, PartialEq)]
pub enum NodeRepositoryError {
StorageNodeAlreadyExists,
CDNNodeAlreadyExists,
Expand Down
2 changes: 1 addition & 1 deletion pallets/ddc-nodes/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) type TestRuntimeCall = <Test as frame_system::Config>::RuntimeCall;
pub struct ExtBuilder;

impl ExtBuilder {
fn build(self) -> TestExternalities {
pub fn build(self) -> TestExternalities {
sp_tracing::try_init_simple();
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

Expand Down
1 change: 1 addition & 0 deletions pallets/ddc-nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl<T: frame_system::Config> NodeTrait<T> for Node<T> {
}
}

#[derive(Debug, PartialEq)]
pub enum NodeError {
InvalidStorageNodePubKey,
InvalidCDNNodePubKey,
Expand Down
Loading

0 comments on commit 1c3b586

Please sign in to comment.