Skip to content

Commit

Permalink
wip: introducing 'commit_billing_report_fingerprint' extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Nov 26, 2024
1 parent 97d08d4 commit ca60dde
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
81 changes: 78 additions & 3 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ddc_primitives::{
pallet::PalletVisitor as PalletVisitorType, payout::PayoutProcessor,
},
BatchIndex, BillingReportParams, BucketId, BucketUsage, ClusterId, CustomerCharge, DdcEra,
MMRProof, NodePubKey, NodeUsage, PayoutError, PayoutState, ProviderReward,
MMRProof, NodePubKey, NodeUsage, PayableUsageHash, PayoutError, PayoutState, ProviderReward,
MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, MILLICENTS,
};
use frame_election_provider_support::SortedListProvider;
Expand All @@ -42,8 +42,12 @@ use frame_support::{
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::prelude::string::String;
use sp_runtime::{traits::Convert, AccountId32, PerThing, Perquintill};
use sp_std::prelude::*;
use sp_core::H256;
use sp_runtime::{
traits::{Convert, Hash},
AccountId32, PerThing, Perquintill,
};
use sp_std::{collections::btree_set::BTreeSet, prelude::*};

#[derive(PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Clone)]
pub struct BillingReportDebt {
Expand All @@ -62,6 +66,8 @@ pub type VoteScoreOf<T> =
<T as frame_system::Config>::AccountId,
>>::Score;

pub type Fingerprint = H256;

parameter_types! {
pub MaxBatchesCount: u16 = MAX_PAYOUT_BATCH_COUNT;
pub MaxDust: u128 = MILLICENTS;
Expand Down Expand Up @@ -101,6 +107,7 @@ pub mod pallet {
type VoteScoreToU64: Convert<VoteScoreOf<Self>, u64>;
type ValidatorVisitor: ValidatorVisitor<Self>;
type AccountIdConverter: From<Self::AccountId> + Into<AccountId32>;
type FingerprintHasher: Hash<Output = Fingerprint>;
}

#[pallet::event]
Expand Down Expand Up @@ -231,6 +238,7 @@ pub mod pallet {
IncorrectClusterId,
ClusterProtocolParamsNotSet,
TotalStoredBytesLessThanZero,
FingerprintIsAlreadyCommited,
}

#[pallet::storage]
Expand Down Expand Up @@ -304,6 +312,34 @@ pub mod pallet {
Finalized = 7,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct BillingReportFingerprint<AccountId> {
pub era_id: DdcEra,
pub start_era: i64,
pub end_era: i64,
pub payers_merkle_root: PayableUsageHash,
pub payees_merkle_root: PayableUsageHash,
pub validators: BTreeSet<AccountId>,
}

impl<AccountId> BillingReportFingerprint<AccountId> {
fn selective_hash<T: Config>(&self) -> Fingerprint {
let mut data = self.era_id.encode();
data.extend_from_slice(&self.start_era.encode());
data.extend_from_slice(&self.end_era.encode());
data.extend_from_slice(&self.payers_merkle_root.encode());
data.extend_from_slice(&self.payees_merkle_root.encode());
// we truncate the `validators` field on purpose as it's appendable collection that is
// used for reaching the quorum on the fingerprint
T::FingerprintHasher::hash(&data).into()
}
}

#[pallet::storage]
#[pallet::getter(fn fingerprints)]
pub type Fingerprints<T: Config> =
StorageMap<_, Blake2_128Concat, Fingerprint, BillingReportFingerprint<T::AccountId>>;

#[pallet::call]
impl<T: Config> Pallet<T> {}

Expand Down Expand Up @@ -555,6 +591,45 @@ pub mod pallet {
}

impl<T: Config> PayoutProcessor<T> for Pallet<T> {
fn commit_billing_report_fingerprint(
validator: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
start_era: i64,
end_era: i64,
payers_merkle_root: PayableUsageHash,
payees_merkle_root: PayableUsageHash,
) -> DispatchResult {
ensure!(end_era > start_era, Error::<T>::BadRequest);
ensure!(payers_merkle_root != Default::default(), Error::<T>::BadRequest);
ensure!(payees_merkle_root != Default::default(), Error::<T>::BadRequest);

let inited_fingerprint = BillingReportFingerprint::<T::AccountId> {
era_id,
start_era,
end_era,
payers_merkle_root,
payees_merkle_root,
validators: Default::default(),
};
let hash = inited_fingerprint.selective_hash::<T>();

let mut fingerprint = if let Some(commited_fingerprint) = Fingerprints::<T>::get(hash) {
commited_fingerprint
} else {
inited_fingerprint
};

ensure!(
fingerprint.validators.insert(validator),
Error::<T>::FingerprintIsAlreadyCommited
);

Fingerprints::<T>::insert(hash, fingerprint);

Ok(())
}

fn begin_billing_report(
cluster_id: ClusterId,
era: DdcEra,
Expand Down
6 changes: 3 additions & 3 deletions pallets/ddc-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use ddc_primitives::{
ValidatorVisitor,
},
ActivityHash, BatchIndex, BillingReportParams, BucketUsage, ClusterId, ClusterStatus, DdcEra,
EraValidation, EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage, PayoutState,
StorageNodeParams,
EraValidation, EraValidationStatus, MMRProof, NodeParams, NodePubKey, NodeUsage,
PayableUsageHash, PayoutState, StorageNodeParams,
};
use frame_support::{
pallet_prelude::*,
Expand Down Expand Up @@ -3441,7 +3441,7 @@ pub mod pallet {
start_era: i64,
end_era: i64,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let sender = ensure_signed(origin.clone())?;
ensure!(Self::is_ocw_validator(sender.clone()), Error::<T>::Unauthorized);

T::PayoutProcessor::begin_billing_report(cluster_id, era_id, start_era, end_era)?;
Expand Down
6 changes: 4 additions & 2 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use scale_info::{
TypeInfo,
};
use serde::{Deserialize, Serialize};
use sp_core::{crypto::KeyTypeId, hash::H160};
use sp_core::{crypto::KeyTypeId, hash::H160, H256};
use sp_runtime::{AccountId32, Perquintill, RuntimeDebug};

pub mod traits;
Expand All @@ -30,7 +30,9 @@ pub type DdcEra = u32;
pub type BucketId = u64;
pub type ClusterNodesCount = u16;
pub type StorageNodePubKey = AccountId32;
pub type ActivityHash = [u8; 32];
pub type ActivityHash = [u8; 32]; // todo: rename to `DeltaUsageHash`
pub type PayableUsageHash = H256;

pub type BatchIndex = u16;
pub const AVG_SECONDS_MONTH: i64 = 2630016; // 30.44 * 24.0 * 3600.0;

Expand Down
12 changes: 11 additions & 1 deletion primitives/src/traits/payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ use sp_runtime::DispatchResult;

use crate::{
BatchIndex, BillingReportParams, BucketId, BucketUsage, ClusterId, DdcEra, MMRProof,
NodePubKey, NodeUsage, PayoutError, PayoutState,
NodePubKey, NodeUsage, PayableUsageHash, PayoutError, PayoutState,
};

pub trait PayoutProcessor<T: frame_system::Config> {
fn commit_billing_report_fingerprint(
validator: T::AccountId,
cluster_id: ClusterId,
era_id: DdcEra,
start_era: i64,
end_era: i64,
payers_merkle_root: PayableUsageHash,
payees_merkle_root: PayableUsageHash,
) -> DispatchResult;

fn begin_billing_report(
cluster_id: ClusterId,
era_id: DdcEra,
Expand Down
1 change: 1 addition & 0 deletions runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ impl pallet_ddc_payouts::Config for Runtime {
type ValidatorVisitor = pallet_ddc_verification::Pallet<Runtime>;
type NodeManager = pallet_ddc_nodes::Pallet<Runtime>;
type AccountIdConverter = AccountId32;
type FingerprintHasher = BlakeTwo256;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions runtime/cere/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ impl pallet_ddc_payouts::Config for Runtime {
type ValidatorVisitor = pallet_ddc_verification::Pallet<Runtime>;
type NodeManager = pallet_ddc_nodes::Pallet<Runtime>;
type AccountIdConverter = AccountId32;
type FingerprintHasher = BlakeTwo256;
}

parameter_types! {
Expand Down

0 comments on commit ca60dde

Please sign in to comment.