Skip to content

Conversation

mjdietzx
Copy link
Contributor

@mjdietzx mjdietzx commented Jan 8, 2021

On top of #20876 only review this commit 3b9b083

This PR gets rid of any side-effects that methods in MiniWallet might leave if an exception is thrown. ie beforehand, if sendrawtransaction failed we would have had an invalid utxo left in self._utxos. More subtle - beforehand, if send_self_transfer failed self._utxos is always going to be modified bc the first thing send_self_transfer does is sort self._utxos. I think this is confusing, especially if tests are catching exceptions

Maybe I'm being paranoid with generate, and side-effects probably wouldn't cause as much confusion here. But originally, if anything failed in the for-loop https://github.com/bitcoin/bitcoin/compare/master...mjdietzx:test-miniwallet-no-side-effects?expand=1#diff-7932a60a9127fd22d10d367526fd7b987f9647ce017595f8b1af5c32d5db0083L38-L40 then self._utxos would include utxos for all items processed before the failure. Seems like we'd prefer no side-effect to me

@DrahtBot DrahtBot added the Tests label Jan 8, 2021
@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 9, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@stackman27 stackman27 left a comment

Choose a reason for hiding this comment

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

Concept ACK. Reviewed and tested send-self-transfer, still testing generate method. Actually, faced a related issue while implementing tx after an exception in mempool_limit.py, but just went with the intuition that we pop the invalid utxo therefore the rest stays valid 😅

So I believe the invalid utxo stays in the list even after popping and/or exception in send_self_transfer?

@maflcko
Copy link
Member

maflcko commented Jan 10, 2021

The initial design was that none of the calls would be allowed to throw, but no objection to making it more robust and extensible.

@mjdietzx
Copy link
Contributor Author

Concept ACK. Reviewed and tested send-self-transfer, still testing generate method. Actually, faced a related issue while implementing tx after an exception in mempool_limit.py, but just went with the intuition that we pop the invalid utxo therefore the rest stays valid 😅

So I believe the invalid utxo stays in the list even after popping and/or exception in send_self_transfer?

Thanks! So before this PR the invalid utxo would stay in the list (unless you explicitly popped it out yourself when you handle the exception). After this PR, the invalid utxo would never have been appended to the list if send_self_transfer failed. So after this PR it would be more robust, and I think more intuitive

Copy link
Contributor

@stackman27 stackman27 left a comment

Choose a reason for hiding this comment

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

Everything looks good to go, left a comment before giving an ACK :)

Copy link
Contributor

@DariusParvin DariusParvin left a comment

Choose a reason for hiding this comment

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

Concept ACK! The changes make the utxo handling more robust. Just left one suggestion for readability.

cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0]
self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']})
cb_txs = map(lambda b: self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0], blocks)
cb_utxos = map(lambda cb_tx: {'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']}, cb_txs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's just me but I found these lambda functions a little bit harder to parse compared to the original for loop. Perhaps it would be more readable to create a temporary variable and add it to the utxo list, similar to how utxos are handled in self_send_transfer?

Suggested change
cb_utxos = map(lambda cb_tx: {'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']}, cb_txs)
new_utxos = []
for b in blocks:
cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0]
new_utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']})
self._utxos.extend(new_utxos)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer map/filter/reduce combined with lambda functions. I know this kind of diverges from most of the python in the codebase though. So I can definitely be convinced to stick with loops like this, but for now I'll leave it and see if anyone else weighs in

@mjdietzx mjdietzx force-pushed the test-miniwallet-no-side-effects branch from 140e429 to a83cccc Compare April 28, 2021 15:12
@mjdietzx
Copy link
Contributor Author

Closing in favor of #21762

@mjdietzx mjdietzx closed this Apr 28, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants