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 wallet state transition functions to properly account for script validation failures. #3037

Conversation

jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented Nov 25, 2021

Issue Number

ADP-1039

Summary

This PR:

  • breaks up wallet state transition functions into smaller functions for easier testing.
  • adds function isOurTx, which returns True if and only if a transaction is relevant to the wallet.
  • adjusts the withdrawal sum computation within applyOurTxToUTxO to correctly account for script validation failure.
  • adds a property test for applyOurTxToUTxO that relates it to isOurTx and applyTxToUTxO.
  • fixes the termination logic of applyOurTxToUTxO so that it returns a result only when appropriate.

@jonathanknowles jonathanknowles self-assigned this Nov 25, 2021
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch 3 times, most recently from d987b7c to 91bb49f Compare November 29, 2021 09:23
The `Coin` type has its own `Semigroup` and `Monoid` instances that
correspond to addition and summation. So there's no need to unwrap
`Coin` values in order to sum them.
It's a singular `Coin` sum value, rather than a plurality of withdrawals.
This will make it possible to test the function in isolation.
This will make it possible to test the function in isolation.
This will make it possible to test the function in isolation.
This commit splits `applyTx` into the following two functions:

- `applyOurTxToUTxO`:
   conditionally applies a single transaction to a UTxO set, if the
   transaction is relevant to the wallet.

- `applyOurTx`:
   conditionally applies a single transaction to a UTxO set in the
   context of an accumulating list of already applied transactions.

The `applyOurTxToUTxO` function will be easier to test in isolation,
since there's no need to supply an accumulating list argument.
This commit restricts the scope of `mkTxMeta` so that it is an inner
function of `applyOurTxToUTxO`.

This will mean that `mkTxMeta` can reuse any terms that are brought into
scope by `applyOurTxToUTxO`.
This will make it easier to turn `applyOurTxToUTxO` into a top-level
function, as it no longer has implict arguments.
This will make it possible to test the function in isolation.
The name `applyBlockToUTxO` describes the purpose of this function more
accurately, as it really does compute the result of applying a block to
a UTxO set.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch from 62feea3 to e3381d2 Compare November 30, 2021 10:10
@jonathanknowles jonathanknowles changed the title Increase testability of wallet state transition functions. Improve testability of wallet state transition functions. Nov 30, 2021
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch from e3381d2 to 8a6e5b7 Compare November 30, 2021 11:02
This function is never called in a context where an extra monadic
context is necessary. Therefore, there's no need to impose the extra
burden of providing such a context on callers.
This property demonstrates the equivalence between `applyTxToUTxO` and
`applyOurTxToUTxO` in contexts where all transaction inputs and outputs
are marked as belonging to the wallet.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch 2 times, most recently from ccb4ebd to 631fbf1 Compare December 1, 2021 04:29
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch from 631fbf1 to 5764d28 Compare December 1, 2021 04:51
@jonathanknowles jonathanknowles changed the title Improve testability of wallet state transition functions. Fix wallet state transition functions to properly account for script validation failures. Dec 1, 2021
We now use:

- `result`
- `haveResult`
- `shouldHaveResult`

In the case where we have a result, we may or may not have an updated
UTxO.
Copy link
Contributor

@sevanspowell sevanspowell left a comment

Choose a reason for hiding this comment

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

Great! I'm glad to have more coverage of this. I hope we can have more still.

@jonathanknowles
Copy link
Contributor Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 1, 2021
3037: Fix wallet state transition functions to properly account for script validation failures. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1039

## Summary

This PR:

- breaks up wallet state transition functions into smaller functions for easier testing.
- adds function `isOurTx`, which returns `True` if and only if a transaction is relevant to the wallet.
- adjusts the withdrawal sum computation within `applyOurTxToUTxO` to correctly account for script validation failure.
- adds a property test for `applyOurTxToUTxO` that relates it to `isOurTx` and `applyTxToUTxO`.
- fixes the termination logic of `applyOurTxToUTxO` so that it returns a result only when appropriate.

Co-authored-by: Jonathan Knowles <[email protected]>
@jonathanknowles jonathanknowles marked this pull request as ready for review December 1, 2021 07:27
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 1, 2021

Build failed:

Failures:

  src/Test/Integration/Scenario/CLI/Shelley/Transactions.hs:244:59: 
  1) CLI Specifications, SHELLEY_CLI_TRANSACTIONS, TRANS_CREATE_06 - Invalid amount, string with diacritics
       uncaught exception: IOException of type ResourceVanished
       fd:138: hFlush: resource vanished (Broken pipe)
 
  To rerun use: --match "/CLI Specifications/SHELLEY_CLI_TRANSACTIONS/TRANS_CREATE_06 - Invalid amount/string with diacritics/"

#2980

@jonathanknowles
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 1, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit a112dc1 into master Dec 1, 2021
@iohk-bors iohk-bors bot deleted the jonathanknowles/adp-1039/increase-testability-of-wallet-state-transition-functions branch December 1, 2021 08:20
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