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

Fixes for balanceTransaction in presence of scripts #2989

Merged
merged 3 commits into from
Nov 1, 2021

Conversation

Anviking
Copy link
Member

@Anviking Anviking commented Oct 22, 2021

  • Don't multiply with numberOfScripts in _maxScriptExecutionCost
  • Fix mismatch of prMem and PrSteps in fromAlonzoPrices

Comments

Behaviour after fix, which I don't quite understand:

With manual w <- fixtureWalletWith @n ctx [15_000_000]

Failures:

  src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs:1341:54:
  1) API Specifications, NEW_SHELLEY_TRANSACTIONS, Plutus scenarios, game state-machine
       uncaught exception: RequestException
       ClientError (Object (fromList [("code",String "insufficient_collateral"),("message",String "I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 2.262900 The largest combination of pure ada UTxOs I could find is: [] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required.")]))

  To rerun use: --match "/API Specifications/NEW_SHELLEY_TRANSACTIONS/Plutus scenarios/game state-machine/"

  src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs:1341:54:
  2) API Specifications, NEW_SHELLEY_TRANSACTIONS, Plutus scenarios, mint-burn
       uncaught exception: RequestException
       ClientError (Object (fromList [("code",String "insufficient_collateral"),("message",String "I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 2.214450 The largest combination of pure ada UTxOs I could find is: [] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required.")]))

However I think it would be better to investigate while creating property tests and/or with simplified implementation.

Issue Number

ADP-1227

@Anviking Anviking self-assigned this Oct 22, 2021
@@ -715,7 +715,7 @@ executionUnitPricesFromPParams pp =
fromAlonzoPrices prices
where
prices = getField @"_prices" pp
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, do you know why it wouldn't be possible to use pp ^. #_prices here?

(Apologies if I'm missing something.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Skärmavbild 2021-11-01 kl  10 08 10

Not sure. The getField + HasField seems more straight-forward than the more magical ^. + OverloadedLabels. I tried some time ago but couldn't get ^. working.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I just saw the same error. 🤷🏻‍♂️

Oh well, I guess it's something we can save for another day!

lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs Outdated Show resolved Hide resolved
executionCost (ExecutionUnitPrices perStep perMem) (W.ExecutionUnits steps mem) =
perStep * (toRational steps) + perMem * (toRational mem)
Coin.unsafeNaturalToCoin . ceiling $
perStep * (toRational steps) + perMem * (toRational mem)
Copy link
Contributor

Choose a reason for hiding this comment

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

I found this code a little hard to understand, since the field names used in this function are quite different from the field names of the original record types. I think it would be good to use consistent terminology everywhere, to help people trying to understand the code in future.

Looking at the types themselves, I think I prefer your names!

Can we change the field names of ExecutionUnitPrices to something like:

data ExecutionUnitPrices = ExecutionUnitPrices
-     { priceExecutionSteps
+     { pricePerStep
          :: Rational
-     , priceExecutionMemory
+     , pricePerMemoryUnit
          :: Rational
      } deriving (Eq, Generic, Show)

I think calling it perMemoryUnit is also clearer from a plain English perspective (since "per memory" is a bit ambiguous).

Secondly, could we rewrite this function to use labels? Attempting to pattern match these records on a single line makes that line rather long. (This approach doesn't really scale if you have more parameters.)

So perhaps:

executionCost :: ExecutionUnitPrices -> ExecutionUnits -> Coin
executionCost ps us = Coin.unsafeNaturalToCoin . ceiling
    $ (ps ^. #pricePerStep)       * toRational (us ^. #steps)
    + (ps ^. #pricePerMemoryUnit) * toRational (us ^. #memoryUnits)

Copy link
Contributor

@jonathanknowles jonathanknowles Nov 1, 2021

Choose a reason for hiding this comment

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

Update: I made a commit with suggested changes, which you could cherry-pick if you prefer:

20273eb

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I think the renaming definitely makes sense.

Secondly, could we rewrite this function to use labels?

You do lose the ability to spot unused fields… but sure, its neater in other ways

Copy link
Contributor

@jonathanknowles jonathanknowles left a comment

Choose a reason for hiding this comment

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

LGTM!

@Anviking
Copy link
Member Author

Anviking commented Nov 1, 2021

bors r+

iohk-bors bot added a commit that referenced this pull request Nov 1, 2021
2989: Fixes for balanceTransaction in presence of scripts r=Anviking a=Anviking

- [x] Don't multiply with numberOfScripts in _maxScriptExecutionCost
- [x] Fix mismatch of prMem and PrSteps in fromAlonzoPrices

### Comments
- Thread: https://input-output-rnd.slack.com/archives/GBT05825V/p1634825136008200
- I believe our the fee minimisation using node functions otherwise hid the price mismatch. This is somewhat alarming, and it will be nice to both property test balanceTransaction as well as simplifying the implementation down the line.

### Behaviour after fix, which I don't quite understand:

With manual `w <- fixtureWalletWith @n ctx [15_000_000]`

```
Failures:

  src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs:1341:54:
  1) API Specifications, NEW_SHELLEY_TRANSACTIONS, Plutus scenarios, game state-machine
       uncaught exception: RequestException
       ClientError (Object (fromList [("code",String "insufficient_collateral"),("message",String "I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 2.262900 The largest combination of pure ada UTxOs I could find is: [] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required.")]))

  To rerun use: --match "/API Specifications/NEW_SHELLEY_TRANSACTIONS/Plutus scenarios/game state-machine/"

  src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs:1341:54:
  2) API Specifications, NEW_SHELLEY_TRANSACTIONS, Plutus scenarios, mint-burn
       uncaught exception: RequestException
       ClientError (Object (fromList [("code",String "insufficient_collateral"),("message",String "I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 2.214450 The largest combination of pure ada UTxOs I could find is: [] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required.")]))
```

However I think it would be better to investigate _while_ creating property tests and/or with simplified implementation.

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1227


Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Jonathan Knowles <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 1, 2021

Build failed:

#expected

Anviking and others added 3 commits November 1, 2021 11:15
The maximum script execution cost is per /tx/, so multiplying with the
number of scripts doesn't make sense. It seems rely on the function
returning `Coin 0` if there are no scripts, however, so keeping that.
Use names that are more consistent with the meaning.
@Anviking Anviking force-pushed the anviking/fixes-for-scripts branch from 91b1d11 to cdff38d Compare November 1, 2021 10:17
@Anviking
Copy link
Member Author

Anviking commented Nov 1, 2021

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 1, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 50dc891 into master Nov 1, 2021
@iohk-bors iohk-bors bot deleted the anviking/fixes-for-scripts branch November 1, 2021 11:01
jonathanknowles added a commit that referenced this pull request Nov 2, 2021
This is to account for recent fixes to the fee calculation logic within
the `balanceTransaction` function, in particular:

#2989
jonathanknowles added a commit that referenced this pull request Nov 8, 2021
This is to account for recent fixes to the fee calculation logic within
the `balanceTransaction` function, in particular:

#2989
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