Skip to content

Commit

Permalink
fix failing smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Apr 24, 2024
1 parent 99bd716 commit a3bdc7f
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions crates/view/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Planner<R: RngCore + CryptoRng> {
balance: Balance,
vote_intents: BTreeMap<u64, VoteIntent>,
plan: TransactionPlan,
ibc_actions: Vec<IbcRelay>,
gas_prices: GasPrices,
fee_tier: FeeTier,
actions: Vec<ActionPlan>,
Expand Down Expand Up @@ -91,6 +92,7 @@ impl<R: RngCore + CryptoRng> Planner<R> {
balance: Balance::default(),
vote_intents: BTreeMap::default(),
plan: TransactionPlan::default(),
ibc_actions: Vec::new(),
gas_prices: GasPrices::zero(),
fee_tier: FeeTier::default(),
actions: Vec::new(),
Expand Down Expand Up @@ -682,7 +684,9 @@ impl<R: RngCore + CryptoRng> Planner<R> {
// result in change sent back to us). This unlinks nullifiers used for voting on
// multiple non-overlapping proposals, increasing privacy.
if record.height_spent.is_none() {
self.spend(record.note.clone(), record.position);
self.push(
SpendPlan::new(&mut OsRng, record.note.clone(), record.position).into(),
);
}

self.delegator_vote_precise(
Expand All @@ -708,8 +712,33 @@ impl<R: RngCore + CryptoRng> Planner<R> {
}
}

// Check enum for voting-based action
let mut is_voting = false;
for action in self.actions.iter() {
if matches!(action, ActionPlan::Spend(_)) {
is_voting = true;
}
}

let mut notes_by_asset_id = BTreeMap::new();
for required in self.calculate_balance().required() {

// Cache the balance calculations to avoid multiple calls
let balance = self.calculate_balance();
let mut required_iter = balance.required().peekable();
let mut provided_iter = balance.provided().peekable();

// Determine which iterator to use based on the presence of elements
let balance_iter: Box<dyn Iterator<Item = penumbra_asset::Value> + Send> =
if required_iter.peek().is_some() {
Box::new(required_iter)
} else if provided_iter.peek().is_some() {
Box::new(provided_iter)
} else {
// Handle the case where neither iterator has elements
todo!();
};

for required in balance_iter {
// Find all the notes of this asset in the source account.
let records: Vec<SpendableNoteRecord> = view
.notes(NotesRequest {
Expand All @@ -734,7 +763,25 @@ impl<R: RngCore + CryptoRng> Planner<R> {
// collected responses to the requests generated by an immediately preceding call to
// [`Planner::note_requests`].
let mut iterations = 0usize;
while let Some(required) = self.calculate_balance_with_fees(fee).required().next() {

// Cache the balance calculations to avoid multiple calls
let balance = self.calculate_balance_with_fees(fee);
let mut required_iter = balance.required().peekable();
let mut provided_iter = balance.provided().peekable();

// Determine which iterator to use based on the presence of elements
let mut balance_iter: Box<dyn Iterator<Item = penumbra_asset::Value> + Send> =
if required_iter.peek().is_some() {
Box::new(required_iter)
} else if provided_iter.peek().is_some() {
Box::new(provided_iter)
} else {
// Handle the case where neither iterator has elements
todo!();
};

while let Some(required) = balance_iter.next() {
println!("required is: {:?}", required);
// Spend a single note towards the required balance, if possible.
// This adds the required spends to the planner.
let Some(note) = notes_by_asset_id
Expand All @@ -749,9 +796,15 @@ impl<R: RngCore + CryptoRng> Planner<R> {
)
.into());
};
print!("note is: {:?}", note);

// Add the required spends to the planner.
self.push(SpendPlan::new(&mut OsRng, note.clone().note, note.clone().position).into());
// Add the required spends to the planner. If it's a voting action, avoid adding the spend
// to the planner since we already added it, otherwise it will double spend the nullifier.
if !is_voting {
self.push(
SpendPlan::new(&mut OsRng, note.clone().note, note.clone().position).into(),
);
}

// Recompute the change outputs, without accounting for fees.
self.refresh_change(change_address);
Expand Down Expand Up @@ -786,7 +839,7 @@ impl<R: RngCore + CryptoRng> Planner<R> {
.collect(),
transaction_parameters: TransactionParameters {
expiry_height: self.plan.transaction_parameters.expiry_height,
chain_id: chain_id.clone(),
chain_id: chain_id,
fee: fee,
},
detection_data: None,
Expand All @@ -807,7 +860,7 @@ impl<R: RngCore + CryptoRng> Planner<R> {

// All actions have now been added, so check to make sure that you don't build and submit an
// empty transaction.
if self.actions.is_empty() {
if self.plan.actions.is_empty() {
anyhow::bail!("planned transaction would be empty, so should not be submitted");
}

Expand Down

0 comments on commit a3bdc7f

Please sign in to comment.