-
Notifications
You must be signed in to change notification settings - Fork 58
feat(drive)!: make the daily withdrawal limit 15% of the total credits held a day ago #4457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,422
−81
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa6892c
feat(drive)!: make the daily withdrawal limit 15% of the total credit…
QuantumExplorer cb860a2
perf(drive): record the total credits history only when the total cha…
QuantumExplorer 6780d2a
fix(drive): floor the relative withdrawal limit, lag it at activation…
QuantumExplorer 5db4b3e
fix(drive): cap the relative withdrawal limit at Core's daily unlock …
QuantumExplorer cfe5147
test(dpp): exercise the relative withdrawal limit's truncation above …
QuantumExplorer f91070d
fix(dpp): reject a daily withdrawal cap below one maximal withdrawal
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
packages/rs-dpp/src/withdrawal/daily_withdrawal_limit/v1/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| use crate::fee::Credits; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| /// Flat daily withdrawal limit, read from the protocol version's system limits | ||
| /// (`SystemLimits::daily_withdrawal_limit`): 2000 Dash up to protocol version 13 | ||
| /// (the limit in Core v22), 4000 Dash from protocol version 14 (Core v24). | ||
| pub fn daily_withdrawal_limit_v1(platform_version: &PlatformVersion) -> Credits { | ||
| platform_version.system_limits.daily_withdrawal_limit | ||
| /// Flat daily withdrawal limit of 2000 Dash, matching the limit in Core v22 | ||
| /// (`LimitAmountV22`). In force from protocol version 8 to protocol version 13; | ||
| /// superseded by the relative limit of version 2. | ||
| pub const fn daily_withdrawal_limit_v1() -> Credits { | ||
| // 2000 Dash | ||
| 200_000_000_000_000 | ||
| } |
194 changes: 194 additions & 0 deletions
194
packages/rs-dpp/src/withdrawal/daily_withdrawal_limit/v2/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| use crate::fee::Credits; | ||
| use crate::withdrawal::daily_withdrawal_limit::v1::daily_withdrawal_limit_v1; | ||
| use crate::ProtocolError; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| /// Relative daily withdrawal limit: `daily_withdrawal_limit_percent` (from the | ||
| /// protocol version's system limits) of the total credits Platform held a day ago. | ||
| /// Using a day-old base means a sudden jump in the total credits does not raise | ||
| /// the limit for a day. | ||
| /// | ||
| /// Three guards keep it usable: | ||
| /// * it is never below `max_withdrawal_amount`, so every withdrawal Platform | ||
| /// accepts eventually fits the daily maximum and cannot block the pooling | ||
| /// queue behind it; | ||
| /// * it is never above `max_daily_withdrawal_amount`, Core's credit-pool unlock | ||
| /// capacity per day: pooling more than Core will mine only cycles those | ||
| /// unlocks through expiry and re-signing; | ||
| /// * while the total credits a day ago are not known (`None`: the history is | ||
| /// younger than a day, i.e. right after this rule activates), the flat limit | ||
| /// of version 1 applies, so the lag cannot be skipped by inflating the total | ||
| /// before or at activation. | ||
| pub fn daily_withdrawal_limit_v2( | ||
| total_credits_in_platform_a_day_ago: Option<Credits>, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<Credits, ProtocolError> { | ||
| let Some(total_credits_a_day_ago) = total_credits_in_platform_a_day_ago else { | ||
| return Ok(daily_withdrawal_limit_v1()); | ||
| }; | ||
|
|
||
| let percent = platform_version | ||
| .system_limits | ||
| .daily_withdrawal_limit_percent | ||
| .ok_or_else(|| { | ||
| ProtocolError::CorruptedCodeExecution( | ||
| "daily_withdrawal_limit v2 requires system_limits.daily_withdrawal_limit_percent" | ||
| .to_string(), | ||
| ) | ||
| })?; | ||
|
|
||
| let max_daily_withdrawal_amount = platform_version | ||
| .system_limits | ||
| .max_daily_withdrawal_amount | ||
| .ok_or_else(|| { | ||
| ProtocolError::CorruptedCodeExecution( | ||
| "daily_withdrawal_limit v2 requires system_limits.max_daily_withdrawal_amount" | ||
| .to_string(), | ||
| ) | ||
| })?; | ||
|
|
||
| let max_withdrawal_amount = platform_version.system_limits.max_withdrawal_amount; | ||
| if max_daily_withdrawal_amount < max_withdrawal_amount { | ||
| // A cap below one maximal withdrawal would let an accepted withdrawal never fit the | ||
| // daily maximum; that is a contradictory configuration, not a limit to apply. | ||
| return Err(ProtocolError::CorruptedCodeExecution(format!( | ||
| "daily_withdrawal_limit v2 requires system_limits.max_daily_withdrawal_amount ({max_daily_withdrawal_amount}) to be at least max_withdrawal_amount ({max_withdrawal_amount})" | ||
| ))); | ||
| } | ||
|
|
||
| // u128 keeps `total * percent` from overflowing for any u64 total. | ||
| let relative_limit = (total_credits_a_day_ago as u128) * (percent as u128) / 100; | ||
| let relative_limit = Credits::try_from(relative_limit) | ||
| .map_err(|_| ProtocolError::Overflow("daily withdrawal limit overflow"))?; | ||
|
|
||
| Ok(relative_limit | ||
| .max(max_withdrawal_amount) | ||
| .min(max_daily_withdrawal_amount)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::dash_to_credits; | ||
|
|
||
| fn platform_version_with(percent: Option<u8>) -> PlatformVersion { | ||
| let mut platform_version = PlatformVersion::latest().clone(); | ||
| platform_version | ||
| .system_limits | ||
| .daily_withdrawal_limit_percent = percent; | ||
| platform_version.system_limits.max_withdrawal_amount = dash_to_credits!(500); | ||
| platform_version.system_limits.max_daily_withdrawal_amount = Some(dash_to_credits!(4000)); | ||
| platform_version | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_return_the_configured_percent_of_the_lagged_total() { | ||
| let platform_version = platform_version_with(Some(15)); | ||
|
|
||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(20000)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(3000) | ||
| ); | ||
| // Rounds down to whole credits: 15% of 4000 Dash + 7 credits is 600 Dash + 1.05 credits. | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(4000) + 7), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(600) + 1 | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_never_go_below_one_maximal_withdrawal() { | ||
| let platform_version = platform_version_with(Some(15)); | ||
|
|
||
| // 15% of 2000 Dash is 300 Dash, below the 500 Dash a single withdrawal may carry. | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(2000)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(500) | ||
| ); | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(0), &platform_version).expect("expected limit"), | ||
| dash_to_credits!(500) | ||
| ); | ||
| // Exactly at the boundary the percent takes over. | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(4000)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(600) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_never_exceed_cores_unlock_capacity_per_day() { | ||
| let platform_version = platform_version_with(Some(15)); | ||
|
|
||
| // 15% of 30000 Dash is 4500 Dash, above what Core mines per day. | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(30000)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(4000) | ||
| ); | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(Credits::MAX), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(4000) | ||
| ); | ||
| // Just under the boundary the percent still applies. | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(26666)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(3999.9) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_keep_the_flat_limit_until_the_lagged_total_is_known() { | ||
| let platform_version = platform_version_with(Some(15)); | ||
|
|
||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(None, &platform_version).expect("expected limit"), | ||
| dash_to_credits!(2000) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_fail_when_the_percent_or_the_cap_is_not_configured() { | ||
| let platform_version = platform_version_with(None); | ||
| assert!(matches!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(100)), &platform_version), | ||
| Err(ProtocolError::CorruptedCodeExecution(_)) | ||
| )); | ||
|
|
||
| let mut platform_version = platform_version_with(Some(15)); | ||
| platform_version.system_limits.max_daily_withdrawal_amount = None; | ||
| assert!(matches!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(100)), &platform_version), | ||
| Err(ProtocolError::CorruptedCodeExecution(_)) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_fail_when_the_cap_is_below_one_maximal_withdrawal() { | ||
| let mut platform_version = platform_version_with(Some(15)); | ||
| platform_version.system_limits.max_daily_withdrawal_amount = | ||
| Some(dash_to_credits!(500) - 1); | ||
|
|
||
| // Whatever the total, a cap below the floor is a contradictory configuration. | ||
| for total in [0, dash_to_credits!(100), dash_to_credits!(30000)] { | ||
| assert!(matches!( | ||
| daily_withdrawal_limit_v2(Some(total), &platform_version), | ||
| Err(ProtocolError::CorruptedCodeExecution(_)) | ||
| )); | ||
| } | ||
|
|
||
| // Exactly the floor is allowed and the limit is that floor. | ||
| platform_version.system_limits.max_daily_withdrawal_amount = Some(dash_to_credits!(500)); | ||
| assert_eq!( | ||
| daily_withdrawal_limit_v2(Some(dash_to_credits!(30000)), &platform_version) | ||
| .expect("expected limit"), | ||
| dash_to_credits!(500) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.