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

Balance endpoint integration #2906

Merged
merged 12 commits into from
Oct 5, 2021

Conversation

paweljakubas
Copy link
Contributor

@paweljakubas paweljakubas commented Sep 16, 2021

adp-656

We have preliminary impl of balanceTransaction that needs to work:

  1. coin selection with external inputs (@jonathanknowles ) DONE
  2. updateTx impl in shelley (@Anviking ) DONE

running tests:

stack test --ta '-m "TRANS_NEW_BALANCE_"' cardano-wallet:integration

Comments

Issue Number

@paweljakubas paweljakubas self-assigned this Sep 16, 2021
@rvl rvl mentioned this pull request Sep 17, 2021
10 tasks
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from e83a76f to 19e95df Compare September 20, 2021 09:07
iohk-bors bot added a commit that referenced this pull request Sep 23, 2021
2914: Add updateSealedTx and ExtraTxBodyContent r=Anviking a=Anviking

- [x] Refactoring: Replace `to{Shelley, Allegra, Mary, Alonzo}TxOut` with `toCardanoTxOut` for easier re-use.
- [x] Add `updateSealedTx :: SealedTx -> ExtraTxBodyContent -> Either ErrUpdateSealedTx SealedTx`
- [x] Test that `updateSealedTx noExtraBodyContent` works with all PAB goldens
- [x] Test that `updateSealedTx noExtraBodyContent` fails when there are existing key witnesses
- [x] Test that inputs and outputs are combined correctly. 

### Comments

Not tested:
- That it works for all previous eras.

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

### Issue Number

ADP-1140

Relates to / should be needed by #2906

<!-- 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]>
iohk-bors bot added a commit that referenced this pull request Sep 28, 2021
2914: Add updateSealedTx and ExtraTxBodyContent r=Anviking a=Anviking

- [x] Refactoring: Replace `to{Shelley, Allegra, Mary, Alonzo}TxOut` with `toCardanoTxOut` for easier re-use.
- [x] Add `updateSealedTx :: SealedTx -> ExtraTxBodyContent -> Either ErrUpdateSealedTx SealedTx`
- [x] Test that `updateSealedTx noExtraBodyContent` works with all PAB goldens
- [x] Test that `updateSealedTx noExtraBodyContent` fails when there are existing key witnesses
- [x] Test that inputs and outputs are combined correctly. 

### Comments

Not tested:
- That it works for all previous eras.

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

### Issue Number

ADP-1140

Relates to / should be needed by #2906

<!-- 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]>
iohk-bors bot added a commit that referenced this pull request Sep 28, 2021
2914: Add updateSealedTx and ExtraTxBodyContent r=Anviking a=Anviking

- [x] Refactoring: Replace `to{Shelley, Allegra, Mary, Alonzo}TxOut` with `toCardanoTxOut` for easier re-use.
- [x] Add `updateSealedTx :: SealedTx -> ExtraTxBodyContent -> Either ErrUpdateSealedTx SealedTx`
- [x] Test that `updateSealedTx noExtraBodyContent` works with all PAB goldens
- [x] Test that `updateSealedTx noExtraBodyContent` fails when there are existing key witnesses
- [x] Test that inputs and outputs are combined correctly. 

### Comments

Not tested:
- That it works for all previous eras.

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

### Issue Number

ADP-1140

Relates to / should be needed by #2906

<!-- 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]>
iohk-bors bot added a commit that referenced this pull request Sep 28, 2021
2914: Add updateSealedTx and ExtraTxBodyContent r=Anviking a=Anviking

- [x] Refactoring: Replace `to{Shelley, Allegra, Mary, Alonzo}TxOut` with `toCardanoTxOut` for easier re-use.
- [x] Add `updateSealedTx :: SealedTx -> ExtraTxBodyContent -> Either ErrUpdateSealedTx SealedTx`
- [x] Test that `updateSealedTx noExtraBodyContent` works with all PAB goldens
- [x] Test that `updateSealedTx noExtraBodyContent` fails when there are existing key witnesses
- [x] Test that inputs and outputs are combined correctly. 

### Comments

Not tested:
- That it works for all previous eras.

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

### Issue Number

ADP-1140

Relates to / should be needed by #2906

<!-- 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]>
iohk-bors bot added a commit that referenced this pull request Sep 28, 2021
2914: Add updateSealedTx and ExtraTxBodyContent r=Anviking a=Anviking

- [x] Refactoring: Replace `to{Shelley, Allegra, Mary, Alonzo}TxOut` with `toCardanoTxOut` for easier re-use.
- [x] Add `updateSealedTx :: SealedTx -> ExtraTxBodyContent -> Either ErrUpdateSealedTx SealedTx`
- [x] Test that `updateSealedTx noExtraBodyContent` works with all PAB goldens
- [x] Test that `updateSealedTx noExtraBodyContent` fails when there are existing key witnesses
- [x] Test that inputs and outputs are combined correctly. 

### Comments

Not tested:
- That it works for all previous eras.

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

### Issue Number

ADP-1140

Relates to / should be needed by #2906

<!-- 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]>
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch 6 times, most recently from c0165d1 to 386791f Compare October 1, 2021 12:09
UTxOIndex.fromSequence externalInputs

let utxoAvailable = UTxOSelection.fromIndexPair
(internalUtxoAvailable, externalSelectedUtxo)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good. 👍🏻


let UnsignedTx colls inps _outs change _wdrl = unsignedtx
let extraBody = ExtraTxBodyContent
{ extraInputs = (\(txin, _, _) -> txin) <$> F.toList inps
Copy link
Member

Choose a reason for hiding this comment

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

Won't inps already include the pre-selected inputs from sealedTxIncoming?

updateTx would then try to append the whole inps to the pre-selected inputs.

Although as the ledger represents the inputs as a Set, it might not actually matter in the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we may filter out preselected inputs. although ledger stores inputs as set so it should not affect txbody or fee.

@Anviking Anviking force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from 19cd20c to 5d0a3ea Compare October 1, 2021 17:03
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from 9a35f0e to 37bdb06 Compare October 1, 2021 18:16
Comment on lines 2225 to 2229
(wdrl, _) <-
if Map.member acct wdrlMap then
mkRewardAccountBuilder @_ @s @_ @n ctx wid (Just SelfWithdrawal)
else
mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(wdrl, _) <-
if Map.member acct wdrlMap then
mkRewardAccountBuilder @_ @s @_ @n ctx wid (Just SelfWithdrawal)
else
mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
(wdrl, _) <- mkRewardAccountBuilder @_ @s @_ @n ctx wid $
if Map.member acct wdrlMap
then Just SelfWithdrawal
else Nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 435 to 436
-- *However* when used to respresent the inputs known by the wallet, in
-- contrast to all inputs, it can be empty.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
-- *However* when used to respresent the inputs known by the wallet, in
-- contrast to all inputs, it can be empty.
-- *However* when used to represent the inputs known by the wallet, in
-- contrast to all inputs, it can be empty.

let transform s sel' =
W.assignChangeAddresses genChange sel' s
& uncurry (W.selectionToUnsignedTx (txWithdrawal txCtx))
unsignedtx <- liftHandler $ W.selectAssets @_ @s @k wrk W.SelectAssetsParams
Copy link
Member

Choose a reason for hiding this comment

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

Because selectAssets only knows about the things we tell it, and not the entire sealedTxIncoming, I think exotic things like submitting update proposals will result in imbalanced txs.

In a re-thought approach, related to ADP-891, and I think which you've mentioned, we could calculate minfee sealedTxIncoming instead and go from there (either by overestimating for the TxExtraBodyContent, or trying to find some fix point)

But I guess the similar and more pressing issue is datums, which everyone was aware of on today's call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree here, it is reasonable to start with minFee, and then increment knowing the cost per inp/out/coll. But we need to remember that evaluation of min fee requires number of witnesses, so it would influence selecting inputs/collaterals in feedback loop. It is not staightforward for me how, in details, it should work

@@ -1346,28 +1346,24 @@ selectionToUnsignedTx wdrl sel s =
fullyQualifiedWithdrawal wdrl
}
where
-- NOTE: External addresses, not known to the wallet, will be filtered out.
Copy link
Member

Choose a reason for hiding this comment

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

When talking briefly with Nikola and hearing / being reminded that they will present the result of balanceTransaction in full detail to the user, this seems wrong.

We probably should:

  1. Make the DerivationPath optional on ApiCoinSelection
  2. Don't filter out pre-selected inputs/outputs
  3. Ensure all other possible tx data is exposed. (We're probably missing glue code for showing datums in api here)

Copy link
Member

Choose a reason for hiding this comment

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

(Not critical for merging this PR and I could have a look)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Daedalus will invoke balance tx, so it will know details of external inputs already - see "inputs" in https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/balanceTransaction And in ApiCoinSelection we will present ADDED inps/outs/colls (always being the resource of the wallet). So the client will know everything having "inputs" and response.
Some argument in favor of what is now is that it is in line what constructTx responds, and as both construct and balance are first step before sign+submit. It makes sense to be compatible....

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from 37bdb06 to f93e437 Compare October 4, 2021 13:12
Copy link
Member

@Anviking Anviking left a comment

Choose a reason for hiding this comment

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

It is great to see this start coming together concretely!

My take is that remaining problems are:

  1. Critical: Ensure fees are not underestimated because of datums
  2. Critical: Ensure e.g. ProtocolParameter Hash, or other stuff won't lead to underestimated fees
  3. Moderate: Is the API response for CoinSelection appropriate for Daedalus?
  4. Low/Moderate: Certificates, update proposals leading to underestimated fees

and that we shouldn't rush to merge this before seeing how the critical things will pan out.

I suspect there are 3 potential solutions for (1):
a. Tweak the coin selection in a minimal / hacky way to account for datums
b. Call ledger/node's minfee in a loop - first on the partial input tx, and then after adding inputs and outputs until you converge.
c. Call ledger/node's minfee once, and then use the existing TxConstraints overestimations to add additional inputs and outputs.

Edit

I must have gotten confused in the original message. Got things a bit wrong. Now edited. We are adjusting the original binary. Ensuring tx body content is preserved is not the problem. The problem is ensuring the fee estimation also is aware of all that content.

(ApiTxOut (ApiT addr, _) _ (Quantity amt) (ApiT assets))) =
(TxIn hashTx ix, TxOut addr (TokenBundle (Coin $ fromIntegral amt) assets))
externalInputs = toTxInTxOut <$> apiExternalInps
sealedTxIncoming = body ^. #transaction . #getApiT
Copy link
Member

@Anviking Anviking Oct 4, 2021

Choose a reason for hiding this comment

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

Just some stylistic thoughts:
I think I have more of a taste for either:

  1. Defining important values as let bindings at the top
  2. Making the where definitions all functions rather than values

such that you can read it from top to bottom and easily see where txIncoming is coming from.

balanceTransaction ctx genChange (ApiT wid) body = do
pp <- liftIO $ NW.currentProtocolParameters nl

--TODO deal with coll and validity and delegations
Copy link
Member

@Anviking Anviking Oct 4, 2021

Choose a reason for hiding this comment

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

👍 Yes.

Collateral we could simply forbid as discussed.

Delegations and other stuff could be present in the sealedTxIncoming, but are probably not crucial. (unused by PAB)

Validity, however, does sound crucial to support. (used by PAB)

Related to #2906 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

🤔 might have gotten this slightly wrong. Will revisit tomorrow.

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from fc3ea41 to 18be8f9 Compare October 5, 2021 12:19
@paweljakubas
Copy link
Contributor Author

bors r+

iohk-bors bot added a commit that referenced this pull request Oct 5, 2021
2906: Balance endpoint integration r=paweljakubas a=paweljakubas

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->
adp-656

We have preliminary impl of balanceTransaction that needs to work:
1. coin selection with external inputs (@jonathanknowles ) DONE
2. updateTx impl in shelley (@Anviking ) DONE

running tests:
```
stack test --ta '-m "TRANS_NEW_BALANCE_"' cardano-wallet:integration
```

### Comments

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

### Issue Number

<!-- 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: Pawel Jakubas <[email protected]>
Co-authored-by: Johannes Lund <[email protected]>
paweljakubas and others added 12 commits October 5, 2021 15:46
For pre-selected external addresses, we cannot find derivation paths. So
rather than erroring:

```
"selectionToUnsignedTx: unable to find derivation path of a known \
         \input or change address. This is impossible.")
        (traverse withDerivationPath hasAddresses)
```
This commit filters them out.

This has the effect of:
1. Making `stack test --ta '-m "TRANS_NEW_BALANCE_"'
   cardano-wallet:integration pass`
2. Removing the requirement that all ApiCoinSelections have a non-empty
   list of inputs.
some cleaning
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-656/balance-endpoint-integration branch from 18be8f9 to ac30014 Compare October 5, 2021 13:47
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 5, 2021

Canceled.

@paweljakubas
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 5, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 234687d into master Oct 5, 2021
@iohk-bors iohk-bors bot deleted the paweljakubas/adp-656/balance-endpoint-integration branch October 5, 2021 15:35
WilliamKingNoel-Bot pushed a commit that referenced this pull request Oct 5, 2021
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