Skip to content

Commit

Permalink
#798 [Bug] planner throws an error in cases where we need to spend m…
Browse files Browse the repository at this point in the history
…ore than one note (#807)

* ignore ephemeral address index when grouping balances by account

* rename index to account

* planner should ignore randomizer for source

* rm logs

* review fixes
  • Loading branch information
Valentine1898 authored Mar 21, 2024
1 parent 86ea7e3 commit 4ed02a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/minifront/src/components/dashboard/assets-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function AssetsTable() {
<div className='flex items-center justify-center gap-2'>
<AddressIcon address={a.address} size={20} />
<h2 className='whitespace-nowrap font-bold md:text-base xl:text-xl'>
Account #{a.index.account}
Account #{a.account}
</h2>
</div>

Expand Down
11 changes: 4 additions & 7 deletions apps/minifront/src/fetchers/balances/by-account.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import {
Address,
AddressIndex,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { Address } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { getBalances } from '.';
import { BalancesResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb';
import { getAddress, getAddressIndex } from '@penumbra-zone/getters/src/address-view';

export interface BalancesByAccount {
index: AddressIndex;
account: number;
address: Address;
balances: BalancesResponse[];
}

const groupByAccount = (acc: BalancesByAccount[], curr: BalancesResponse): BalancesByAccount[] => {
const index = getAddressIndex(curr.accountAddress);
const grouping = acc.find(a => a.index.equals(index));
const grouping = acc.find(a => a.account === index.account);

if (grouping) {
grouping.balances.push(curr);
} else {
acc.push({
index,
account: index.account,
address: getAddress(curr.accountAddress),
balances: [curr],
});
Expand Down
7 changes: 4 additions & 3 deletions apps/minifront/src/state/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,9 @@ const toUnstakedTokensByAccount = (
({ balanceView }) => getDisplayDenomFromView(balanceView) === STAKING_TOKEN,
);

if (stakingTokenBalance?.balanceView)
unstakedTokensByAccount.set(curr.index.account, stakingTokenBalance.balanceView);
if (stakingTokenBalance?.balanceView) {
unstakedTokensByAccount.set(curr.account, stakingTokenBalance.balanceView);
}

return unstakedTokensByAccount;
};
Expand Down Expand Up @@ -446,7 +447,7 @@ const toUnbondingTokensByAccount = (
},
});

unbondingTokensByAccount.set(curr.index.account, {
unbondingTokensByAccount.set(curr.account, {
tokens: unbondingTokens,
total: unbondingTotal,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/wasm/crate/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ pub async fn plan_transaction(
.unwrap_or_default();

let fvk = FullViewingKey::from_str(bech32_full_viewing_key)?;
let (change_address, _) = fvk.incoming().payment_address(source_address_index);

// should ignore the randomizer for change_address, there is no point using ephemeral address
let (change_address, _) = fvk
.incoming()
.payment_address(source_address_index.account.into());

let storage = IndexedDBStorage::new(serde_wasm_bindgen::from_value(idb_constants)?).await?;

Expand Down
7 changes: 5 additions & 2 deletions packages/wasm/crate/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ impl IndexedDBStorage {
continue;
}

if Some(record.address_index) != address_index {
continue;
// Planner should omit the address index randomizer and compare only the account index
if let Some(ai) = address_index {
if record.address_index.account != ai.account {
continue;
}
}

total += record.note.amount();
Expand Down

0 comments on commit 4ed02a1

Please sign in to comment.