Skip to content

Commit

Permalink
feat: generating a vault address for each billing report
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Nov 1, 2023
1 parent ce750e3 commit b489d54
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
46 changes: 43 additions & 3 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ parameter_types! {
pub mod pallet {

use super::*;
use frame_support::PalletId;
use sp_io::hashing::blake2_128;
use sp_runtime::traits::{AccountIdConversion, Zero};

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand All @@ -40,6 +43,8 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::constant]
type PalletId: Get<PalletId>;
}

#[pallet::event]
Expand Down Expand Up @@ -73,13 +78,15 @@ pub mod pallet {
ClusterId,
Blake2_128Concat,
DdcEra,
BillingReport,
BillingReport<T>,
ValueQuery,
>;

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Default)]
pub struct BillingReport {
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
#[scale_info(skip_type_params(T))]
pub struct BillingReport<T: Config> {
state: State,
vault: T::AccountId,
total_balance: u128,
distributed_balance: u128,
// stage 1
Expand All @@ -90,6 +97,21 @@ pub mod pallet {
rewarding_processed_batches: BoundedVec<BatchIndex, MaxBatchesCount>,
}

impl<T: pallet::Config> Default for BillingReport<T> {
fn default() -> Self {
Self {
state: State::default(),
vault: T::PalletId::get().into_account_truncating(),
total_balance: Zero::zero(),
distributed_balance: Zero::zero(),
charging_max_batch_index: Zero::zero(),
charging_processed_batches: BoundedVec::default(),
rewarding_max_batch_index: Zero::zero(),
rewarding_processed_batches: BoundedVec::default(),
}
}
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Default)]
pub enum State {
#[default]
Expand All @@ -113,6 +135,7 @@ pub mod pallet {
ensure_signed(origin)?; // todo: check that the caller is DAC account

let mut billing_report = BillingReport::default();
billing_report.vault = Self::sub_account_id(cluster_id.clone(), era);
billing_report.state = State::Initialized;
ActiveBillingReports::<T>::insert(cluster_id.clone(), era, billing_report);

Expand Down Expand Up @@ -342,4 +365,21 @@ pub mod pallet {
Ok(())
}
}

impl<T: Config> Pallet<T> {
fn account_id() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}

fn sub_account_id(cluster_id: ClusterId, era: DdcEra) -> T::AccountId {
let mut bytes = Vec::new();
bytes.extend_from_slice(&cluster_id[..]);
bytes.extend_from_slice(&era.encode());
let hash = blake2_128(&bytes);
// "modl" + "payouts_" + hash is 28 bytes, the T::AccountId is 32 bytes, so we should be
// safe from the truncation and possible collisions caused by it. The rest 4 bytes will
// be fulfilled with trailing zeros.
T::PalletId::get().into_sub_account_truncating(hash)
}
}
}
2 changes: 1 addition & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sp_core::hash::H160;
use sp_runtime::{AccountId32, RuntimeDebug};

pub type ClusterId = H160;
pub type DdcEra = u64;
pub type DdcEra = u32;

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
Expand Down
5 changes: 5 additions & 0 deletions runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,13 @@ impl pallet_ddc_clusters::Config for Runtime {
type NodeRepository = pallet_ddc_nodes::Pallet<Runtime>;
}

parameter_types! {
pub const PayoutsPalletId: PalletId = PalletId(*b"payouts_");
}

impl pallet_ddc_payouts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = PayoutsPalletId;
}

construct_runtime!(
Expand Down

0 comments on commit b489d54

Please sign in to comment.