Skip to content
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

fix unconfirmed change note tracking in transactions #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

borngraced
Copy link
Member

This fixes the issue where change outputs weren't being tracked properly in the unconfirmed state, which could cause incorrect balance calculations and note availability issues KomodoPlatform/komodo-defi-framework#2273

@borngraced borngraced self-assigned this Nov 19, 2024
@@ -634,7 +634,6 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
// Change output
//

/*
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see, the change output is added in KDF code. I hope this won't affect that code.
Maybe some test is needed in KDF to ensure that the change is calculated correctly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks a lot..

// Automatically decrypt and store any outputs sent to our wallet, including change.
// This uses our viewing keys to find any outputs we can decrypt, creates decrypted
// note data for spendability, and saves them to the wallet database.
decrypt_and_store_transaction(params, wallet_db, &tx).await?;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this code we would need to wait until the block is scanned, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the case above, we need this fix in kdf directly after building txs..

// This uses our viewing keys to find any outputs we can decrypt, creates decrypted
// note data for spendability, and saves them to the wallet database.
decrypt_and_store_transaction(params, wallet_db, &tx).await?;

wallet_db
.store_sent_tx(&SentTransaction {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this store_sent_tx call stores the created transaction output in the walletdb.
Wouldn't the decrypt_and_store_transaction call do a similar thing?

Copy link
Member Author

@borngraced borngraced Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store_sent_tx - stores notes we created to send value to others, while decrypt_and_store_transaction calls store_received_tx(stores notes we can spend) internally

/// Scans a [`Transaction`] for any information that can be decrypted by the accounts in
/// the wallet, and saves it to the wallet.
pub async fn decrypt_and_store_transaction<N, E, P, D>(
params: &P,
data: &mut D,
tx: &Transaction,
) -> Result<(), E>
where
E: From<Error<N>>,
P: consensus::Parameters,
D: WalletWrite<Error = E>,
{
// Fetch the ExtendedFullViewingKeys we are tracking
let extfvks = data.get_extended_full_viewing_keys().await?;
let max_height = data.block_height_extrema().await?.map(|(_, max)| max + 1);
let height = data
.get_tx_height(tx.txid())
.await?
.or(max_height)
.or_else(|| params.activation_height(NetworkUpgrade::Sapling))
.ok_or(Error::SaplingNotActive)?;
let outputs = decrypt_transaction(params, height, tx, &extfvks);
if outputs.is_empty() {
Ok(())
} else {
data.store_received_tx(&ReceivedTransaction {
tx,
outputs: &outputs,
})
.await?;
Ok(())
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, we need to add the change as a received output, to fix the balance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants