Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ogabrielides committed Jul 12, 2024
1 parent 3a9e940 commit 4c383bc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 33 deletions.
25 changes: 11 additions & 14 deletions packages/rs-dpp/src/fee/default_costs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub enum KnownCostItem {
FetchIdentityBalanceProcessingCost,
/// The cost for fetching an identity key
FetchSingleIdentityKeyProcessingCost,
/// The cost for a Single SHA256 operation
SingleSHA256,
/// The cost for a Blake3 operation
Blake3,
/// The cost for a Single SHA256 operation, with a specific size
SingleSHA256(usize),
/// The cost for a Blake3 operation, with a specific size
Blake3(usize),
/// The cost for a EcdsaSecp256k1 signature verification
VerifySignatureEcdsaSecp256k1,
/// The cost for a BLS12_381 signature verification
Expand All @@ -78,7 +78,7 @@ pub enum KnownCostItem {

impl KnownCostItem {
#[inline]
pub fn lookup_cost(&self, fee_version: &FeeVersion, size: Option<usize>) -> Credits {
pub fn lookup_cost(&self, fee_version: &FeeVersion) -> Credits {
match self {
KnownCostItem::StorageDiskUsageCreditPerByte => {
fee_version.storage.storage_disk_usage_credit_per_byte
Expand All @@ -103,13 +103,13 @@ impl KnownCostItem {
.processing
.fetch_single_identity_key_processing_cost
}
KnownCostItem::Blake3 => {
KnownCostItem::Blake3(size) => {
fee_version.hashing.blake3_base
+ fee_version.hashing.blake3_per_block * size.unwrap_or(0) as u64
+ fee_version.hashing.blake3_per_block * *size as u64
}
KnownCostItem::SingleSHA256 => {
KnownCostItem::SingleSHA256(size) => {
fee_version.hashing.single_sha256_base
+ fee_version.hashing.sha256_per_block * size.unwrap_or(0) as u64
+ fee_version.hashing.sha256_per_block * *size as u64
}
KnownCostItem::VerifySignatureEcdsaSecp256k1 => {
fee_version.signature.verify_signature_ecdsa_secp256k1
Expand All @@ -133,10 +133,9 @@ impl KnownCostItem {
&self,
epoch: &T,
cached_fee_version: &CachedEpochIndexFeeVersions,
size: Option<usize>,
) -> Credits {
let version = epoch.active_fee_version(cached_fee_version);
self.lookup_cost(&version, size)
self.lookup_cost(&version)
}
}

Expand All @@ -150,7 +149,6 @@ pub trait EpochCosts {
&self,
cached_fee_version: &CachedEpochIndexFeeVersions,
cost_item: KnownCostItem,
size: Option<usize>,
) -> Credits;
}

Expand All @@ -175,8 +173,7 @@ impl EpochCosts for Epoch {
&self,
cached_fee_version: &CachedEpochIndexFeeVersions,
cost_item: KnownCostItem,
size: Option<usize>,
) -> Credits {
cost_item.lookup_cost_on_epoch(self, cached_fee_version, size)
cost_item.lookup_cost_on_epoch(self, cached_fee_version)
}
}
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/fee/fee_result/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl FeeRefunds {
// TODO We should use multipliers

let credits: Credits = (bytes as Credits)
.checked_mul(Epoch::new(current_epoch_index)?.cost_for_known_cost_item(previous_fee_versions, StorageDiskUsageCreditPerByte, None))
.checked_mul(Epoch::new(current_epoch_index)?.cost_for_known_cost_item(previous_fee_versions, StorageDiskUsageCreditPerByte))
.ok_or(ProtocolError::Overflow("storage written bytes cost overflow"))?;

let (amount, _) = calculate_storage_fee_refund_amount_and_leftovers(
Expand Down
3 changes: 0 additions & 3 deletions packages/rs-drive/src/drive/document/delete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
// We added 1559 bytes
assert_eq!(added_bytes, 1559);
Expand Down Expand Up @@ -867,7 +866,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert!(added_bytes > refund_equivalent_bytes);
Expand Down Expand Up @@ -931,7 +929,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
// We added 1558 bytes
assert_eq!(added_bytes, 1559);
Expand Down
4 changes: 0 additions & 4 deletions packages/rs-drive/src/drive/document/insert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ mod tests {
* Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None
),
processing_fee: 2356200, // TODO: Readjust this test when FeeHashingVersion blake3_base, sha256_ripe_md160_base, blake3_per_block values are finalised
..Default::default()
Expand Down Expand Up @@ -407,7 +406,6 @@ mod tests {
* Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None
),
processing_fee: 1500200, // TODO: Readjust this test when FeeHashingVersion blake3_base, sha256_ripe_md160_base, blake3_per_block values are finalised
..Default::default()
Expand Down Expand Up @@ -475,7 +473,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
assert_eq!(1305, added_bytes);
assert_eq!(144859600, processing_fee); // TODO: Readjust this test when FeeHashingVersion blake3_base, sha256_ripe_md160_base, blake3_per_block values are finalised
Expand Down Expand Up @@ -707,7 +704,6 @@ mod tests {
* Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None
),
processing_fee: 2095400, // TODO: Readjust this test when FeeHashingVersion blake3_base, sha256_ripe_md160_base, blake3_per_block values are finalised
..Default::default()
Expand Down
11 changes: 0 additions & 11 deletions packages/rs-drive/src/drive/document/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
let expected_added_bytes = if using_history {
//Explanation for 1237
Expand Down Expand Up @@ -1110,7 +1109,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert!(expected_added_bytes > refund_equivalent_bytes);
Expand All @@ -1131,7 +1129,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert_eq!(original_bytes, expected_added_bytes);
Expand All @@ -1153,7 +1150,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

let expected_added_bytes = if using_history { 313 } else { 1 };
Expand Down Expand Up @@ -1226,7 +1222,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
let expected_added_bytes = if using_history { 1238 } else { 962 };
assert_eq!(original_bytes, expected_added_bytes);
Expand All @@ -1253,7 +1248,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert!(expected_added_bytes > refund_equivalent_bytes);
Expand All @@ -1274,7 +1268,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert_eq!(original_bytes, expected_added_bytes);
Expand All @@ -1296,7 +1289,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

let removed_credits = update_fees
Expand All @@ -1316,7 +1308,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

assert!(expected_added_bytes > refund_equivalent_bytes);
Expand Down Expand Up @@ -1430,7 +1421,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);
let expected_added_bytes = if using_history {
//Explanation for 1237
Expand Down Expand Up @@ -1591,7 +1581,6 @@ mod tests {
/ Epoch::new(0).unwrap().cost_for_known_cost_item(
&EPOCH_CHANGE_FEE_VERSION_TEST,
StorageDiskUsageCreditPerByte,
None,
);

let expected_added_bytes = if using_history { 1239 } else { 963 };
Expand Down

0 comments on commit 4c383bc

Please sign in to comment.