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

Add prop_balanceTransactionBalanced #2996

Merged
merged 9 commits into from
Dec 14, 2021
Merged

Conversation

Anviking
Copy link
Member

@Anviking Anviking commented Nov 1, 2021

Overview

  • Add preconditions to balanceTransaction such that we fail instead of producing imbalanced results...
    • when there are withdrawals with different network tags
    • when there are deposits or refunds (pool reg, stake key reg/dereg) (apparently not pool retirement)
    • when collateral is pre-selected
    • when we underestimate the fee (likely related to variable length coin encoding and the padding)
  • Tweak generators in Cardano.Api.Gen
  • Add (pending) prop_balanceTransactionBalanced
    • Decent shrinking and failure-presentation
    • Used to discover the preconditions above
    • Generator coverages of some corner cases are missing
      • empty txIns
      • empty txOuts
      • gens never cause balanceTransaction to select collateral for some reason (!)
      • PParams are currently static
    • In-the-end marked pending because of a new variable-length-coin-encoding corner cases

Comments

The problems we theorised during the coarse first implementation are indeed caught by the property (collateral, fee encoding corner cases, deposits), which is nice. Discovering additional problems was nice too.

While the coverage is still not ideal, I believe it's good enough to give re-writing the implementation a go.

Issue Number

ADP-1214

@Anviking Anviking self-assigned this Nov 1, 2021
@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch from 2b6baeb to 0bb51d6 Compare November 4, 2021 16:19
lib/core/src/Cardano/Wallet/Primitive/Types/Coin/Gen.hs Outdated Show resolved Hide resolved
{ getFeePolicy = mockFeePolicy
, getTxMaxSize = Quantity 16384
, getTokenBundleMaxSize = TokenBundleMaxSize $ TxSize 4000
, getMaxExecutionUnits = ExecutionUnits 10000000 10000000000
}
, minimumUTxOvalue = MinimumUTxOValue $ Coin 1000000
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: use NumericUnderscores for large values such as these.

@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch 3 times, most recently from 87bc8ff to 12e2320 Compare November 16, 2021 19:54
@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch 3 times, most recently from 8564d8e to 8abd7a1 Compare November 30, 2021 20:14
@@ -433,7 +432,7 @@ instance Arbitrary SlotNo where
arbitrary = genSlotNo

genLovelaceCoverage :: Lovelace -> Property
genLovelaceCoverage = unsignedCoverage (maxBound @Word16) "lovelace"
genLovelaceCoverage = unsignedCoverage (maxBound @Word32) "lovelace"
Copy link
Member Author

@Anviking Anviking Dec 1, 2021

Choose a reason for hiding this comment

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

Lovelace is unbounded — is having to pass in a Word32 bound weird?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is just bad UX on my part. You don't need to pass in the maxBound of the unsigned you are testing the coverage of, but you do need to pass in some value that the test can use to delineate between "large" and "small" values (with things above the value being considered large).

I'm not really happy with this code.

$ classify (txFee sealedTx > Cardano.Lovelace 1_000_000)
"fee above 1 ada"
(txBalance sealedTx combinedUTxO === 0)
.&&. (abs (txFee sealedTx) .<= 4_000_000)
Copy link
Member Author

@Anviking Anviking Dec 1, 2021

Choose a reason for hiding this comment

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

TODO: Test fee >= minFee but also not too much of an over-estimate. Perhaps another property (and definitely another PR/ticket)

@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch from 9ee89ad to 71865b0 Compare December 2, 2021 16:49
@Anviking
Copy link
Member Author

Anviking commented Dec 3, 2021

bors try

iohk-bors bot added a commit that referenced this pull request Dec 3, 2021
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 3, 2021

try

Build failed:

@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch 2 times, most recently from 47ef500 to de9b123 Compare December 6, 2021 13:14
@Anviking
Copy link
Member Author

Anviking commented Dec 6, 2021

bors try

iohk-bors bot added a commit that referenced this pull request Dec 6, 2021
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 6, 2021

try

Build failed:

@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch 2 times, most recently from dcd74d5 to f154313 Compare December 8, 2021 13:13
@Anviking
Copy link
Member Author

Anviking commented Dec 8, 2021

bors try

iohk-bors bot added a commit that referenced this pull request Dec 8, 2021
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 8, 2021

try

Build failed:

@Anviking Anviking changed the title WIP: Stub prop_balanceTx Add prop_balanceTransactionBalanced Dec 8, 2021
@Anviking Anviking marked this pull request as ready for review December 8, 2021 13:32
@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch from f154313 to 7854206 Compare December 8, 2021 14:55
@Anviking
Copy link
Member Author

Anviking commented Dec 8, 2021

bors try

@sevanspowell
Copy link
Contributor

Thanks for your work on this Johannes 🎉 I think I'm ok with this, as long as my understanding of it is correct: This is a first step in fixing bugs with the current balanceTransaction implementation.

I'm not sure how I feel about the coverage of the generators (which is my fault too!), but that's unimportant I think. The important thing is that the generators and the test have found issues, and we should use these going forward to fix the implementation of balance transaction.

I have one unimportant suggestion here: #3052, but otherwise, looks good!

@sevanspowell sevanspowell self-requested a review December 10, 2021 09:14
Anviking and others added 6 commits December 10, 2021 19:36
Using a not-yet-committed prop_balanceTransactionBalanced
it was discovered that balanceTransaction would produce
imbalanced transactions under several scenarios.

The guards/preconditions added here ensure the limitations
are clear to users who run into them. It also makes the
limitations clearly known and documented, allowing us to
come up with an appropriate strategy to fix.
@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch from 34c3640 to 32bc654 Compare December 10, 2021 18:37
Anviking and others added 3 commits December 10, 2021 19:38
- Tests that balanceTransaction produces balanced results
- Decent shrinking and failure-presentation
- Used to discover the preconditions added in a prev commit
- Generator coverages of some corner cases are missing
    - empty txIns
    - empty txOuts
    - gens never cause balanceTransaction to select collateral for some reason (!)
    - PParams are currently static
- In-the-end marked pending because of a new variable-length-coin-encoding corner cases
- Make the semantics of "Maybe Coin" more obvious by using a descriptive data
type.
Co-authored-by: Samuel Evans-Powell <[email protected]>
@Anviking Anviking force-pushed the anviking/ADP-1214/prop_balanceTx branch from 32bc654 to f5bf2a1 Compare December 10, 2021 18:40
@Anviking
Copy link
Member Author

Thanks! Yes, this is a first step!

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 10, 2021
2996: Add prop_balanceTransactionBalanced r=Anviking a=Anviking

## Overview

- Add preconditions to `balanceTransaction` such that we fail instead of producing imbalanced results...
    - when there are withdrawals with different network tags
    - when there are deposits or refunds (pool reg, stake key reg/dereg) (apparently not pool retirement)
    - when collateral is pre-selected
    - when we underestimate the fee (likely related to variable length coin encoding and the padding)
- Tweak generators in `Cardano.Api.Gen`
- Add (pending) `prop_balanceTransactionBalanced`
    - Decent shrinking and failure-presentation
    - Used to discover the preconditions above
    - Generator coverages of some corner cases are missing
        - empty txIns
        - empty txOuts
        - gens never cause balanceTransaction to select collateral for some reason (!)
        - PParams are currently static
    - In-the-end marked pending because of a new variable-length-coin-encoding corner cases

## Comments

The problems we theorised during the coarse first implementation are indeed caught by the property (collateral, fee encoding corner cases, deposits), which is nice. Discovering additional problems was nice too.

While the coverage is still not ideal, I believe it's good enough to give re-writing the implementation a go. 

### Issue Number


ADP-1214

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Samuel Evans-Powell <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 10, 2021

Build failed:

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 10, 2021
2996: Add prop_balanceTransactionBalanced r=Anviking a=Anviking

## Overview

- Add preconditions to `balanceTransaction` such that we fail instead of producing imbalanced results...
    - when there are withdrawals with different network tags
    - when there are deposits or refunds (pool reg, stake key reg/dereg) (apparently not pool retirement)
    - when collateral is pre-selected
    - when we underestimate the fee (likely related to variable length coin encoding and the padding)
- Tweak generators in `Cardano.Api.Gen`
- Add (pending) `prop_balanceTransactionBalanced`
    - Decent shrinking and failure-presentation
    - Used to discover the preconditions above
    - Generator coverages of some corner cases are missing
        - empty txIns
        - empty txOuts
        - gens never cause balanceTransaction to select collateral for some reason (!)
        - PParams are currently static
    - In-the-end marked pending because of a new variable-length-coin-encoding corner cases

## Comments

The problems we theorised during the coarse first implementation are indeed caught by the property (collateral, fee encoding corner cases, deposits), which is nice. Discovering additional problems was nice too.

While the coverage is still not ideal, I believe it's good enough to give re-writing the implementation a go. 

### Issue Number


ADP-1214

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Samuel Evans-Powell <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 10, 2021

Build failed:

Due to renaming of cardano-ledger-specs repo

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 14, 2021
2996: Add prop_balanceTransactionBalanced r=Anviking a=Anviking

## Overview

- Add preconditions to `balanceTransaction` such that we fail instead of producing imbalanced results...
    - when there are withdrawals with different network tags
    - when there are deposits or refunds (pool reg, stake key reg/dereg) (apparently not pool retirement)
    - when collateral is pre-selected
    - when we underestimate the fee (likely related to variable length coin encoding and the padding)
- Tweak generators in `Cardano.Api.Gen`
- Add (pending) `prop_balanceTransactionBalanced`
    - Decent shrinking and failure-presentation
    - Used to discover the preconditions above
    - Generator coverages of some corner cases are missing
        - empty txIns
        - empty txOuts
        - gens never cause balanceTransaction to select collateral for some reason (!)
        - PParams are currently static
    - In-the-end marked pending because of a new variable-length-coin-encoding corner cases

## Comments

The problems we theorised during the coarse first implementation are indeed caught by the property (collateral, fee encoding corner cases, deposits), which is nice. Discovering additional problems was nice too.

While the coverage is still not ideal, I believe it's good enough to give re-writing the implementation a go. 

### Issue Number


ADP-1214

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Samuel Evans-Powell <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 14, 2021

Build failed:

[ 77 of 106] Compiling Data.Time.Text   ( src/Data/Time/Text.hs, dist/build/Data/Time/Text.o, dist/build/Data/Time/Text.dyn_o )
[ 78 of 106] Compiling Cardano.Wallet.Api.Types ( src/Cardano/Wallet/Api/Types.hs, dist/build/Cardano/Wallet/Api/Types.o, dist/build/Cardano/Wallet/Api/Types.dyn_o )
/nix/store/7gdji02y6rbnzv5mcxcf3mwksk785y8j-stdenv-linux/setup: line 1311:   119 Killed                  $SETUP_HS build lib:cardano-wallet-core -j$(($NIX_BUILD_CORES > 4 ? 4 : $NIX_BUILD_CORES))

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 14, 2021
2996: Add prop_balanceTransactionBalanced r=Anviking a=Anviking

## Overview

- Add preconditions to `balanceTransaction` such that we fail instead of producing imbalanced results...
    - when there are withdrawals with different network tags
    - when there are deposits or refunds (pool reg, stake key reg/dereg) (apparently not pool retirement)
    - when collateral is pre-selected
    - when we underestimate the fee (likely related to variable length coin encoding and the padding)
- Tweak generators in `Cardano.Api.Gen`
- Add (pending) `prop_balanceTransactionBalanced`
    - Decent shrinking and failure-presentation
    - Used to discover the preconditions above
    - Generator coverages of some corner cases are missing
        - empty txIns
        - empty txOuts
        - gens never cause balanceTransaction to select collateral for some reason (!)
        - PParams are currently static
    - In-the-end marked pending because of a new variable-length-coin-encoding corner cases

## Comments

The problems we theorised during the coarse first implementation are indeed caught by the property (collateral, fee encoding corner cases, deposits), which is nice. Discovering additional problems was nice too.

While the coverage is still not ideal, I believe it's good enough to give re-writing the implementation a go. 

### Issue Number


ADP-1214

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Samuel Evans-Powell <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 14, 2021

Build failed:

@Anviking
Copy link
Member Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 14, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit bc866ee into master Dec 14, 2021
@iohk-bors iohk-bors bot deleted the anviking/ADP-1214/prop_balanceTx branch December 14, 2021 13:53
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.

3 participants