-
Notifications
You must be signed in to change notification settings - Fork 303
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
fees: add unit tests #4316
Closed
Closed
fees: add unit tests #4316
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -843,3 +843,178 @@ impl<R: RngCore + CryptoRng> Planner<R> { | |
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use penumbra_asset::STAKING_TOKEN_ASSET_ID; | ||
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey}; | ||
use rand_core::OsRng; | ||
|
||
#[test] | ||
fn test_sufficient_funds_for_non_zero_fees_in_transaction() { | ||
let rng = OsRng; | ||
let seed_phrase = SeedPhrase::generate(rng); | ||
let sk = SpendKey::from_seed_phrase_bip44(seed_phrase, &Bip44Path::new(0)); | ||
let fvk = sk.full_viewing_key(); | ||
let (sender, _dtk) = fvk.incoming().payment_address(0u32.into()); | ||
|
||
let seed_phrase_2 = SeedPhrase::generate(rng); | ||
let sk_2 = SpendKey::from_seed_phrase_bip44(seed_phrase_2, &Bip44Path::new(0)); | ||
let fvk_2 = sk_2.full_viewing_key(); | ||
let (reciever, _dtk_2) = fvk_2.incoming().payment_address(0u32.into()); | ||
|
||
let note0 = Note::generate( | ||
&mut OsRng, | ||
&sender, | ||
Value { | ||
amount: 3u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}, | ||
); | ||
let note1 = Note::generate( | ||
&mut OsRng, | ||
&sender, | ||
Value { | ||
amount: 5u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}, | ||
); | ||
|
||
let mut spendable_notes: Vec<Note> = Vec::new(); | ||
spendable_notes.push(note0); | ||
spendable_notes.push(note1); | ||
|
||
let mut planner = Planner::new(OsRng); | ||
|
||
// Set non-zero gas price. | ||
let mut gas_price = GasPrices::default(); | ||
gas_price.block_space_price = 1u64; | ||
let fee_tier = FeeTier::Low; | ||
|
||
planner.set_gas_prices(gas_price).set_fee_tier(fee_tier); | ||
|
||
let amount = Value { | ||
amount: 3u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}; | ||
|
||
planner.output(amount, reciever); | ||
planner.actions = planner.plan.actions.clone(); | ||
|
||
let mut iterations = 0usize; | ||
while let Some(_required) = planner | ||
.balance_with_fee_estimate(&planner.gas_prices, &planner.fee_tier) | ||
.required() | ||
.next() | ||
{ | ||
// Spend a single note towards the required balance, if possible. | ||
planner.push( | ||
SpendPlan::new(&mut OsRng, spendable_notes[iterations].clone(), 0u64.into()).into(), | ||
); | ||
|
||
// Recompute the change outputs, without accounting for fees. | ||
planner.refresh_change(sender.clone()); | ||
|
||
// Now re-estimate the fee of the updated transaction and adjust the change if possible. | ||
let fee = planner.fee_estimate(&planner.gas_prices, &planner.fee_tier); | ||
planner.adjust_change_for_fee(fee); | ||
|
||
if planner.balance().is_zero() { | ||
break; | ||
} | ||
|
||
iterations += 1; | ||
} | ||
|
||
assert!(planner.balance().is_zero()); | ||
} | ||
|
||
#[test] | ||
fn test_insufficient_funds_for_non_zero_fees_in_transaction() { | ||
let rng = OsRng; | ||
let seed_phrase = SeedPhrase::generate(rng); | ||
let sk = SpendKey::from_seed_phrase_bip44(seed_phrase, &Bip44Path::new(0)); | ||
let fvk = sk.full_viewing_key(); | ||
let (sender, _dtk) = fvk.incoming().payment_address(0u32.into()); | ||
|
||
let seed_phrase_2 = SeedPhrase::generate(rng); | ||
let sk_2 = SpendKey::from_seed_phrase_bip44(seed_phrase_2, &Bip44Path::new(0)); | ||
let fvk_2 = sk_2.full_viewing_key(); | ||
let (reciever, _dtk_2) = fvk_2.incoming().payment_address(0u32.into()); | ||
|
||
let note0 = Note::generate( | ||
&mut OsRng, | ||
&sender, | ||
Value { | ||
amount: 4u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}, | ||
); | ||
let note1 = Note::generate( | ||
&mut OsRng, | ||
&sender, | ||
Value { | ||
amount: 3u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}, | ||
); | ||
|
||
let mut spendable_notes: Vec<Note> = Vec::new(); | ||
spendable_notes.push(note0); | ||
spendable_notes.push(note1); | ||
|
||
let mut planner = Planner::new(OsRng); | ||
|
||
// Set non-zero gas price. | ||
let mut gas_price = GasPrices::default(); | ||
gas_price.block_space_price = 2u64; | ||
let fee_tier = FeeTier::Low; | ||
|
||
planner.set_gas_prices(gas_price).set_fee_tier(fee_tier); | ||
|
||
let amount = Value { | ||
amount: 5u64.into(), | ||
asset_id: *STAKING_TOKEN_ASSET_ID, | ||
}; | ||
|
||
planner.output(amount, reciever); | ||
planner.actions = planner.plan.actions.clone(); | ||
|
||
let mut iterations = 0usize; | ||
let mut has_insufficient_funds = false; | ||
while let Some(_required) = planner | ||
.balance_with_fee_estimate(&planner.gas_prices, &planner.fee_tier) | ||
.required() | ||
.next() | ||
{ | ||
if iterations >= spendable_notes.len() { | ||
has_insufficient_funds = true; | ||
break; | ||
} | ||
|
||
// Spend a single note towards the required balance, if possible. | ||
planner.push( | ||
SpendPlan::new(&mut OsRng, spendable_notes[iterations].clone(), 0u64.into()).into(), | ||
); | ||
|
||
// Recompute the change outputs, without accounting for fees. | ||
planner.refresh_change(sender.clone()); | ||
|
||
// Now re-estimate the fee of the updated transaction and adjust the change if possible. | ||
let fee = planner.fee_estimate(&planner.gas_prices, &planner.fee_tier); | ||
planner.adjust_change_for_fee(fee); | ||
|
||
if planner.balance().is_zero() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this cheating? The plan should balance once we run out of required notes if we have correctly refreshed the change notes |
||
break; | ||
} | ||
|
||
iterations += 1; | ||
} | ||
|
||
assert!( | ||
has_insufficient_funds && !planner.balance().is_zero(), | ||
"The planner should identify insufficient funds and have a non-zero balance." | ||
); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding tests is a great idea, but my sense is that rather than replicating the logic (which has changed since this PR was opened) we should either:
Planner::plan
directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intuition is that it's probably best to wait a little longer until we try to test this, we'll definitely have less turbulence in the planner in a couple weeks time or less