Skip to content

Commit

Permalink
refactor: ♻️ change the price updater to return the price per gigabyte (
Browse files Browse the repository at this point in the history
#266)

* refactor: ♻️ change all data units to bytes and all prices per unit per tick to prices per gigaunit per tick

This should provide more granularity when calculating how much to charge users of payment streams while keeping the precision of keeping track of data units in bytes. Keep in mind that after this change the lowest amount possible to charge a user is 0.1 DOT since DOT has 10 decimal places

* test: ✅ fix all tests + run typegen

* fix: 🩹 fix tests + update benchmarks

* chore: 🏷️ run typegen

* fix: 🩹 amend review

* chore: 🏷️ run typegen

* style: 🚨 run cargo fmt

* chore: 🏷️ run typegen
  • Loading branch information
TDemeco authored Nov 26, 2024
1 parent a2950d8 commit 37627ad
Show file tree
Hide file tree
Showing 49 changed files with 866 additions and 541 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions api-augment/dist/interfaces/lookup.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api-augment/dist/interfaces/lookup.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions api-augment/dist/types/interfaces/augment-api-consts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ declare module "@polkadot/api-base/types/consts" {
[key: string]: Codec;
};
paymentStreams: {
/**
* The base deposit for a new payment stream. The actual deposit will be this constant + the deposit calculated using the `NewStreamDeposit` constant.
**/
baseDeposit: u128 & AugmentedConst<ApiType>;
/**
* The maximum amount of Users that a Provider can charge in a single extrinsic execution.
* This is used to prevent a Provider from charging too many Users in a single block, which could lead to a DoS attack.
Expand All @@ -249,9 +253,9 @@ declare module "@polkadot/api-base/types/consts" {
/**
* The number of ticks that correspond to the deposit that a User has to pay to open a payment stream.
* This means that, from the balance of the User for which the payment stream is being created, the amount
* `NewStreamDeposit * rate` will be held as a deposit.
* In the case of dynamic-rate payment streams, `rate` will be `amount_provided * current_service_price`, where `current_service_price` has
* to be provided by the pallet using the `PaymentStreamsInterface` interface.
* `NewStreamDeposit * rate + BaseDeposit` will be held as a deposit.
* In the case of dynamic-rate payment streams, `rate` will be `amount_provided_in_giga_units * price_per_giga_unit_per_tick`, where `price_per_giga_unit_per_tick` is
* obtained from the `CurrentPricePerGigaUnitPerTick` storage.
**/
newStreamDeposit: u32 & AugmentedConst<ApiType>;
/**
Expand Down
9 changes: 4 additions & 5 deletions api-augment/dist/types/interfaces/augment-api-query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,14 +996,13 @@ declare module "@polkadot/api-base/types/storage" {
accumulatedPriceIndex: AugmentedQuery<ApiType, () => Observable<u128>, []> &
QueryableStorageEntry<ApiType, []>;
/**
* The current price per unit per tick of the provided service, used to calculate the amount to charge for dynamic-rate payment streams.
* The current price per gigaunit per tick of the provided service, used to calculate the amount to charge for dynamic-rate payment streams.
*
* This is updated each tick using the formula that considers current system capacity (total storage of the system) and system availability (total storage available).
* This can be updated each tick by the system manager.
*
* This storage is updated in:
* - [do_update_current_price_per_unit_per_tick](crate::utils::do_update_current_price_per_unit_per_tick), which updates the current price per unit per tick.
* It is in giga-units to allow for a more granular price per unit considering the limitations in decimal places that the Balance type might have.
**/
currentPricePerUnitPerTick: AugmentedQuery<ApiType, () => Observable<u128>, []> &
currentPricePerGigaUnitPerTick: AugmentedQuery<ApiType, () => Observable<u128>, []> &
QueryableStorageEntry<ApiType, []>;
/**
* The double mapping from a Provider, to its provided Users, to their dynamic-rate payment streams.
Expand Down
6 changes: 3 additions & 3 deletions api-augment/dist/types/interfaces/augment-api-tx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3393,7 +3393,7 @@ declare module "@polkadot/api-base/types/submittable" {
**/
addValueProp: AugmentedSubmittable<
(
pricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
pricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
bucketDataLimit: u64 | AnyNumber | Uint8Array
) => SubmittableExtrinsic<ApiType>,
Expand Down Expand Up @@ -3555,7 +3555,7 @@ declare module "@polkadot/api-base/types/submittable" {
mspId: H256 | string | Uint8Array,
capacity: u64 | AnyNumber | Uint8Array,
multiaddresses: Vec<Bytes> | (Bytes | string | Uint8Array)[],
valuePropPricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
valuePropPricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
valuePropMaxDataLimit: u64 | AnyNumber | Uint8Array,
paymentAccount: AccountId32 | string | Uint8Array
Expand Down Expand Up @@ -3675,7 +3675,7 @@ declare module "@polkadot/api-base/types/submittable" {
(
capacity: u64 | AnyNumber | Uint8Array,
multiaddresses: Vec<Bytes> | (Bytes | string | Uint8Array)[],
valuePropPricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
valuePropPricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
valuePropMaxDataLimit: u64 | AnyNumber | Uint8Array,
paymentAccount: AccountId32 | string | Uint8Array
Expand Down
8 changes: 4 additions & 4 deletions api-augment/dist/types/interfaces/lookup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ declare const _default: {
* Lookup129: pallet_storage_providers::types::ValueProposition<T>
**/
PalletStorageProvidersValueProposition: {
pricePerUnitOfDataPerBlock: string;
pricePerGigaUnitOfDataPerBlock: string;
commitment: string;
bucketDataLimit: string;
available: string;
Expand Down Expand Up @@ -3285,7 +3285,7 @@ declare const _default: {
request_msp_sign_up: {
capacity: string;
multiaddresses: string;
valuePropPricePerUnitOfDataPerBlock: string;
valuePropPricePerGigaUnitOfDataPerBlock: string;
commitment: string;
valuePropMaxDataLimit: string;
paymentAccount: string;
Expand All @@ -3305,7 +3305,7 @@ declare const _default: {
newCapacity: string;
};
add_value_prop: {
pricePerUnitOfDataPerBlock: string;
pricePerGigaUnitOfDataPerBlock: string;
commitment: string;
bucketDataLimit: string;
};
Expand All @@ -3323,7 +3323,7 @@ declare const _default: {
mspId: string;
capacity: string;
multiaddresses: string;
valuePropPricePerUnitOfDataPerBlock: string;
valuePropPricePerGigaUnitOfDataPerBlock: string;
commitment: string;
valuePropMaxDataLimit: string;
paymentAccount: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ export interface GetUsersWithDebtOverThresholdError extends Enum {
readonly isProviderNotRegistered: boolean;
readonly isProviderWithoutPaymentStreams: boolean;
readonly isAmountToChargeOverflow: boolean;
readonly isAmountToChargeUnderflow: boolean;
readonly isDebtOverflow: boolean;
readonly isInternalApiError: boolean;
readonly type:
| "ProviderNotRegistered"
| "ProviderWithoutPaymentStreams"
| "AmountToChargeOverflow"
| "AmountToChargeUnderflow"
| "DebtOverflow"
| "InternalApiError";
}
Expand Down Expand Up @@ -212,7 +214,7 @@ export interface TrieRemoveMutation extends Null {}
export interface ValuePropId extends H256 {}
/** @name ValueProposition */
export interface ValueProposition extends Struct {
readonly price_per_unit_of_data_per_block: u64;
readonly price_per_giga_unit_of_data_per_block: u64;
readonly bucket_data_limit: StorageDataUnit;
}
/** @name ValuePropositionWithId */
Expand Down
8 changes: 4 additions & 4 deletions api-augment/dist/types/interfaces/types-lookup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ declare module "@polkadot/types/lookup" {
}
/** @name PalletStorageProvidersValueProposition (129) */
interface PalletStorageProvidersValueProposition extends Struct {
readonly pricePerUnitOfDataPerBlock: u128;
readonly pricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly bucketDataLimit: u64;
readonly available: bool;
Expand Down Expand Up @@ -4015,7 +4015,7 @@ declare module "@polkadot/types/lookup" {
readonly asRequestMspSignUp: {
readonly capacity: u64;
readonly multiaddresses: Vec<Bytes>;
readonly valuePropPricePerUnitOfDataPerBlock: u128;
readonly valuePropPricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly valuePropMaxDataLimit: u64;
readonly paymentAccount: AccountId32;
Expand All @@ -4039,7 +4039,7 @@ declare module "@polkadot/types/lookup" {
} & Struct;
readonly isAddValueProp: boolean;
readonly asAddValueProp: {
readonly pricePerUnitOfDataPerBlock: u128;
readonly pricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly bucketDataLimit: u64;
} & Struct;
Expand All @@ -4061,7 +4061,7 @@ declare module "@polkadot/types/lookup" {
readonly mspId: H256;
readonly capacity: u64;
readonly multiaddresses: Vec<Bytes>;
readonly valuePropPricePerUnitOfDataPerBlock: u128;
readonly valuePropPricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly valuePropMaxDataLimit: u64;
readonly paymentAccount: AccountId32;
Expand Down
10 changes: 7 additions & 3 deletions api-augment/src/interfaces/augment-api-consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ declare module "@polkadot/api-base/types/consts" {
[key: string]: Codec;
};
paymentStreams: {
/**
* The base deposit for a new payment stream. The actual deposit will be this constant + the deposit calculated using the `NewStreamDeposit` constant.
**/
baseDeposit: u128 & AugmentedConst<ApiType>;
/**
* The maximum amount of Users that a Provider can charge in a single extrinsic execution.
* This is used to prevent a Provider from charging too many Users in a single block, which could lead to a DoS attack.
Expand All @@ -257,9 +261,9 @@ declare module "@polkadot/api-base/types/consts" {
/**
* The number of ticks that correspond to the deposit that a User has to pay to open a payment stream.
* This means that, from the balance of the User for which the payment stream is being created, the amount
* `NewStreamDeposit * rate` will be held as a deposit.
* In the case of dynamic-rate payment streams, `rate` will be `amount_provided * current_service_price`, where `current_service_price` has
* to be provided by the pallet using the `PaymentStreamsInterface` interface.
* `NewStreamDeposit * rate + BaseDeposit` will be held as a deposit.
* In the case of dynamic-rate payment streams, `rate` will be `amount_provided_in_giga_units * price_per_giga_unit_per_tick`, where `price_per_giga_unit_per_tick` is
* obtained from the `CurrentPricePerGigaUnitPerTick` storage.
**/
newStreamDeposit: u32 & AugmentedConst<ApiType>;
/**
Expand Down
9 changes: 4 additions & 5 deletions api-augment/src/interfaces/augment-api-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,13 @@ declare module "@polkadot/api-base/types/storage" {
accumulatedPriceIndex: AugmentedQuery<ApiType, () => Observable<u128>, []> &
QueryableStorageEntry<ApiType, []>;
/**
* The current price per unit per tick of the provided service, used to calculate the amount to charge for dynamic-rate payment streams.
* The current price per gigaunit per tick of the provided service, used to calculate the amount to charge for dynamic-rate payment streams.
*
* This is updated each tick using the formula that considers current system capacity (total storage of the system) and system availability (total storage available).
* This can be updated each tick by the system manager.
*
* This storage is updated in:
* - [do_update_current_price_per_unit_per_tick](crate::utils::do_update_current_price_per_unit_per_tick), which updates the current price per unit per tick.
* It is in giga-units to allow for a more granular price per unit considering the limitations in decimal places that the Balance type might have.
**/
currentPricePerUnitPerTick: AugmentedQuery<ApiType, () => Observable<u128>, []> &
currentPricePerGigaUnitPerTick: AugmentedQuery<ApiType, () => Observable<u128>, []> &
QueryableStorageEntry<ApiType, []>;
/**
* The double mapping from a Provider, to its provided Users, to their dynamic-rate payment streams.
Expand Down
6 changes: 3 additions & 3 deletions api-augment/src/interfaces/augment-api-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,7 @@ declare module "@polkadot/api-base/types/submittable" {
**/
addValueProp: AugmentedSubmittable<
(
pricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
pricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
bucketDataLimit: u64 | AnyNumber | Uint8Array
) => SubmittableExtrinsic<ApiType>,
Expand Down Expand Up @@ -2997,7 +2997,7 @@ declare module "@polkadot/api-base/types/submittable" {
mspId: H256 | string | Uint8Array,
capacity: u64 | AnyNumber | Uint8Array,
multiaddresses: Vec<Bytes> | (Bytes | string | Uint8Array)[],
valuePropPricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
valuePropPricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
valuePropMaxDataLimit: u64 | AnyNumber | Uint8Array,
paymentAccount: AccountId32 | string | Uint8Array
Expand Down Expand Up @@ -3117,7 +3117,7 @@ declare module "@polkadot/api-base/types/submittable" {
(
capacity: u64 | AnyNumber | Uint8Array,
multiaddresses: Vec<Bytes> | (Bytes | string | Uint8Array)[],
valuePropPricePerUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
valuePropPricePerGigaUnitOfDataPerBlock: u128 | AnyNumber | Uint8Array,
commitment: Bytes | string | Uint8Array,
valuePropMaxDataLimit: u64 | AnyNumber | Uint8Array,
paymentAccount: AccountId32 | string | Uint8Array
Expand Down
8 changes: 4 additions & 4 deletions api-augment/src/interfaces/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ export default {
* Lookup129: pallet_storage_providers::types::ValueProposition<T>
**/
PalletStorageProvidersValueProposition: {
pricePerUnitOfDataPerBlock: "u128",
pricePerGigaUnitOfDataPerBlock: "u128",
commitment: "Bytes",
bucketDataLimit: "u64",
available: "bool"
Expand Down Expand Up @@ -3361,7 +3361,7 @@ export default {
request_msp_sign_up: {
capacity: "u64",
multiaddresses: "Vec<Bytes>",
valuePropPricePerUnitOfDataPerBlock: "u128",
valuePropPricePerGigaUnitOfDataPerBlock: "u128",
commitment: "Bytes",
valuePropMaxDataLimit: "u64",
paymentAccount: "AccountId32"
Expand All @@ -3381,7 +3381,7 @@ export default {
newCapacity: "u64"
},
add_value_prop: {
pricePerUnitOfDataPerBlock: "u128",
pricePerGigaUnitOfDataPerBlock: "u128",
commitment: "Bytes",
bucketDataLimit: "u64"
},
Expand All @@ -3399,7 +3399,7 @@ export default {
mspId: "H256",
capacity: "u64",
multiaddresses: "Vec<Bytes>",
valuePropPricePerUnitOfDataPerBlock: "u128",
valuePropPricePerGigaUnitOfDataPerBlock: "u128",
commitment: "Bytes",
valuePropMaxDataLimit: "u64",
paymentAccount: "AccountId32"
Expand Down
4 changes: 3 additions & 1 deletion api-augment/src/interfaces/storagehubclient/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ export interface GetUsersWithDebtOverThresholdError extends Enum {
readonly isProviderNotRegistered: boolean;
readonly isProviderWithoutPaymentStreams: boolean;
readonly isAmountToChargeOverflow: boolean;
readonly isAmountToChargeUnderflow: boolean;
readonly isDebtOverflow: boolean;
readonly isInternalApiError: boolean;
readonly type:
| "ProviderNotRegistered"
| "ProviderWithoutPaymentStreams"
| "AmountToChargeOverflow"
| "AmountToChargeUnderflow"
| "DebtOverflow"
| "InternalApiError";
}
Expand Down Expand Up @@ -252,7 +254,7 @@ export interface ValuePropId extends H256 {}

/** @name ValueProposition */
export interface ValueProposition extends Struct {
readonly price_per_unit_of_data_per_block: u64;
readonly price_per_giga_unit_of_data_per_block: u64;
readonly bucket_data_limit: StorageDataUnit;
}

Expand Down
8 changes: 4 additions & 4 deletions api-augment/src/interfaces/types-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ declare module "@polkadot/types/lookup" {

/** @name PalletStorageProvidersValueProposition (129) */
interface PalletStorageProvidersValueProposition extends Struct {
readonly pricePerUnitOfDataPerBlock: u128;
readonly pricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly bucketDataLimit: u64;
readonly available: bool;
Expand Down Expand Up @@ -4220,7 +4220,7 @@ declare module "@polkadot/types/lookup" {
readonly asRequestMspSignUp: {
readonly capacity: u64;
readonly multiaddresses: Vec<Bytes>;
readonly valuePropPricePerUnitOfDataPerBlock: u128;
readonly valuePropPricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly valuePropMaxDataLimit: u64;
readonly paymentAccount: AccountId32;
Expand All @@ -4244,7 +4244,7 @@ declare module "@polkadot/types/lookup" {
} & Struct;
readonly isAddValueProp: boolean;
readonly asAddValueProp: {
readonly pricePerUnitOfDataPerBlock: u128;
readonly pricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly bucketDataLimit: u64;
} & Struct;
Expand All @@ -4266,7 +4266,7 @@ declare module "@polkadot/types/lookup" {
readonly mspId: H256;
readonly capacity: u64;
readonly multiaddresses: Vec<Bytes>;
readonly valuePropPricePerUnitOfDataPerBlock: u128;
readonly valuePropPricePerGigaUnitOfDataPerBlock: u128;
readonly commitment: Bytes;
readonly valuePropMaxDataLimit: u64;
readonly paymentAccount: AccountId32;
Expand Down
2 changes: 1 addition & 1 deletion api-augment/storagehub.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pallets/bucket-nfts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ impl pallet_payment_streams::Config for Test {
type TreasuryCutCalculator = NoCutTreasuryCutCalculator<Balance, Self::Units>;
type TreasuryAccount = TreasuryAccount;
type MaxUsersToCharge = ConstU32<10>;
type BaseDeposit = ConstU128<10>;
}
// Converter from the BlockNumber type to the Balance type for math
pub struct BlockNumberToBalance;
Expand Down
2 changes: 1 addition & 1 deletion pallets/file-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub mod pallet {
ProviderId = <Self::Providers as shp_traits::ReadProvidersInterface>::ProviderId,
Units = <Self::Providers as shp_traits::ReadStorageProvidersInterface>::StorageDataUnit,
>
+ shp_traits::MutatePricePerUnitPerTickInterface<PricePerUnitPerTick = BalanceOf<Self>>;
+ shp_traits::MutatePricePerGigaUnitPerTickInterface<PricePerGigaUnitPerTick = BalanceOf<Self>>;

type UpdateStoragePrice: shp_traits::UpdateStoragePrice<
Price = BalanceOf<Self>,
Expand Down
Loading

0 comments on commit 37627ad

Please sign in to comment.