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

LocalStateQuery: Use params from chain when building txs #1577

Closed
rvl opened this issue Apr 16, 2020 · 3 comments
Closed

LocalStateQuery: Use params from chain when building txs #1577

rvl opened this issue Apr 16, 2020 · 3 comments
Assignees

Comments

@rvl
Copy link
Contributor

rvl commented Apr 16, 2020

Context

Part of ADP-83.

The wallet server must keep track of certain protocol parameters that may change through soft protocol updates. The current parameters of interest both relate to transaction construction:

  • max tx size
  • fee policy

These two parameters are needed when building a transaction before running the coin selection.

Relevant parameter functions are:

byronGenesisConfig
  :: Ouroboros.Consensus.Byron.Ledger.Config.BlockConfig
  -> Cardano.Chain.Genesis.Config

configGenesisData :: Cardano.Chain.Genesis.Config -> GenesisData

gdProtocolParameters
  :: Cardano.Chain.Genesis.GenesisData
  -> Cardano.Chain.Update.ProtocolParameters

ppMaxTxSize :: ProtocolParameters -> Natural

ppTxFeePolicy :: ProtocolParameters -> TxFeePolicy

The state machine types are in Ouroboros.Network.Protocol.LocalStateQuery.Client.
With cardano-node since revision 0172a96e16f46f1688da6dd8294481bb995fd72d, a new entry is added to NodeToClientProtocols that we can use. In our network layer Cardano.Wallet.Byron.Network we need to make room for this protocol.

Example code exists in Ouroboros.Network.Protocol.LocalStateQuery.Example:

localStateQueryClient
  :: forall block query result m. Applicative m
  => [(Point block, query result)]
  -> LocalStateQueryClient block query m
                           [(Point block, Either AcquireFailure result)]

In the wallet engine are already blockchain parameters stored in the database with every wallet checkpoint.

Note that for transaction construction, only the parameters for the network tip are relevant. A transaction obviously cannot be inserted into the chain under the network tip.

The node only maintains local state for the last k blocks from its tip.

The type Cardano.Chain.Update.ProtocolParameters is the values that the node supports updates for. The intersection of this type and Cardano.Wallet.Primitive.Types.BlockchainParameters is the following three fields:
- ppSlotDuration (getSlotLength)
- ppMaxTxSize
- ppTxFeePolicy

Decision

Option A - parameters from checkpoint

The present code structure has the full bundle of parameters stored in each wallet checkpoint. So it's convenient to use the parameters from the wallet tip (not network tip) when constructing a transaction.

So, for each wallet, after fetching the latest blocks, use LocalStateQuery to get the blockchain parameters for the wallet tip.

Save these dynamic blockchain parameters into the wallet checkpoint after applying the latest blocks.

Option B - request latest parameters when constructing tx

Add a method to NetworkLayer something like this

tipBlockchainParameters
  :: NetworkLayer
  -> ExceptT ErrTipBlockchainParameters m BlockchainParameters

Use this function before running coin selection to get the tx max size and fee policy.

Possible problem

The LocalStateQuery client is not synchronised with the ChainSync client. So there is a race condition between getting the tip point and querying the parameters for this point.

Acceptance Criteria

  1. Transactions are created using parameters dynamically updated from the chain.

Development

  1. Go with something like option B.
  2. Make a specialised chain sync + local state query node client that just keeps the tip point and latest parameters in a mutable variable. This state is contained inside the network layer.
  3. Split BlockchainParameters into the parameters that always stay the same and the parameters that can be updated. The update parameters are not associated with any particular checkpoint, just the latest node tip.
  4. Modify the database so that the split BlockchainParameters can be saved and loaded.
  5. When creating transactions, use the update parameters loaded from the database.

QA

  1. There is a new unit test Cardano.Wallet.Byron.NetworkSpec.spec which tests querying protocol parameters from the node.
  2. Existing integration tests can check that TxParameters from node are being used in transactions -- because initial TxParameters are forced to zero in lib/byron/test/integration/Main.hs:182.
  3. I was thinking about using cardano-cli to submit an update proposal to test TxParameter changes. But this would probably result in a slow flaky test.

DB Migrations

There will be a new tx_parameters table added, but no other fields are modified. So automatic migrations will be ok.

@rvl rvl self-assigned this Apr 16, 2020
@rvl rvl added this to the (ADP-83) Local State Query milestone Apr 16, 2020
@rvl rvl linked a pull request Apr 17, 2020 that will close this issue
7 tasks
iohk-bors bot added a commit that referenced this issue Apr 25, 2020
1593: cardano-node: 1.10.1 -> 1.11.0 r=rvl a=rvl

# Issue Number

Relates to #1577 / ADP-83.

# Overview

- Updates cardano-node version in nix.
- Updates the cardano-haskell stack snapshot.
- Adds `cardano-cli` to the shell. I am hoping to use this in the integration tests.
- Build fixes

# Comments

WIP until input-output-hk/cardano-haskell#13 is merged.


Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue Apr 28, 2020
1587: Cabal: Set -Werror and -Ox only with release flag r=rvl a=rvl

Makes Cabal builds faster, like the `stack build --fast` flag.

Nothing is changed when the development flag is unset.

Some components are missing `-O`, and most others are at `-O2`. However, from what I've read, `-O2` often doesn't help, so you only use it if your benchmarks say so.

1593: cardano-node: 1.10.1 -> 1.11.0 r=rvl a=rvl

# Issue Number

Relates to #1577 / ADP-83.

# Overview

- Updates cardano-node version in nix.
- Updates the cardano-haskell stack snapshot.
- Adds `cardano-cli` to the shell. I am hoping to use this in the integration tests.
- Build fixes

# Comments

WIP until input-output-hk/cardano-haskell#13 is merged.


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: IOHK <[email protected]>
iohk-bors bot added a commit that referenced this issue May 1, 2020
1580: Add LocalStateQuery to byron network layer r=rvl a=rvl

### Issue Number

Relates to #1577 / [ADP-83](https://jira.iohk.io/projects/ADP/issues/ADP-83).

### Overview

- [x] Updates ouroboros-network and cardano-node to latest master versions
- [x] Initial try at LocalStateQuery client in network layer.
- [x] Get latest tx parameters when fetching the block tip.
- [x] Store tx parameters in database while following the chain.
- [x] Use tx parameters from database when creating transactions.

### Comments

The TRANS_CREATE integration tests which check the fee should cover this. The only way the wallet can get TxParameters is through the node protocol parameters state.

### Next PRs
- [ ] Marcin says to use `defaultCodecs` or `clientCodecs` rather than constructing our own codecs. These functions require a `BlockConfig ByronBlock`. Which means that we would need to modify parseGenesisData, etc, etc. So this can be another PR ⇒ #1606.
- [ ] Integration test where new protocol parameters are proposed and accepted. It should test that the new tx max size and fee policy apply. This will require cardano-cli to update the node's parameters. However the update proposal function of cardano-cli is not yet complete.


Co-authored-by: Rodney Lorrimar <[email protected]>
@rvl rvl removed a link to a pull request May 1, 2020
7 tasks
iohk-bors bot added a commit that referenced this issue May 5, 2020
1618: Unit test for LocalStateQuery r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

Adds a test of NetworkLayer.getTxParameters against the self-node

Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 5, 2020
1618: Unit test for LocalStateQuery r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

Adds a test of NetworkLayer.getTxParameters against the self-node

Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 5, 2020
1606: Don't deserialise blocks in the chain sync node client r=KtorZ a=rvl

# Issue Number

Relates to #1577 / ADP-83.

# Overview

The chain tip following node client doesn't need to serialise blocks. So use bytestring representation for blocks in this part. 

# Comments

I attempted to change it to use `defaultCodecs` (serialised) / `clientCodecs` (deserialised) to help when upgrading the node version, but it is quite difficult to set up the arguments `mkByronConfig`. Not to mention there are two different `NodeToClientVersion` types that need to be imported. So I gave up on this.


Co-authored-by: iohk-bors[bot] <43231472+iohk-bors[bot]@users.noreply.github.com>
Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 6, 2020
1610: Buildkite all-tests-pass branch r=rvl a=rvl

### Overview

- Some buildkite fixes.
- Trigger buildkite nightly pipeline from github actions, after the windows tests pass.
- Push the `all-tests-pass` branch after the nightly job successfully completes.
- Add a github tag filter to the docker image push script. Otherwise, if the buildkite ref filter were removed, and someone pushed any tag, then the "latest" docker tags would be updated to that tag.
- Bump haskell.nix revision while we're at it.

1624: Fix migration error on checkpoint table r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83 and  #1621.

### Overview

Fix migration error on checkpoint table found in nightly tests.

SQLite doesn't support removing columns, so we put them back as "unused" fields (https://www.sqlite.org/lang_altertable.html#altertableishard).


1627: Bump version from 2020.4.28 to 2020.5.6 r=rvl a=rvl

- Updates the cabal package versions from 2020.4.28 to 2020.5.6.
- A few shell script tweaks to the release script.


Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 6, 2020
1624: Fix migration error on checkpoint table r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83 and  #1621.

### Overview

Fix migration error on checkpoint table found in nightly tests.

SQLite doesn't support removing columns, so we put them back as "unused" fields (https://www.sqlite.org/lang_altertable.html#altertableishard).


1627: Bump version from 2020.4.28 to 2020.5.6 r=rvl a=rvl

- Updates the cabal package versions from 2020.4.28 to 2020.5.6.
- A few shell script tweaks to the release script.


Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 7, 2020
1631: Don't deserialise blocks in the chain sync node client r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

This is #1606, except with `master` as the PR base branch.


Co-authored-by: Rodney Lorrimar <[email protected]>
iohk-bors bot added a commit that referenced this issue May 7, 2020
1630: Reduce logging from network layer r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

- Set network tip log level to debug
- Improve formatting of text log message
- Only log the tip when it changes, because the node can seem to send updates when nothing changes

### Comments

This is what it looks like now:

```
[cardano-wallet.network:Debug:18] [2020-05-06 07:49:54.10 UTC] Network node tip block height is 4125310 at hash c5cb3bcd
```


1633: review RELEASE_TEMPLATE to constuct changelog from PR + labels r=rvl a=KtorZ

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->

N/A

# Overview

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

We used to organize the CHANGELOG by milestones, but we are now organizing it by 'type of change' (improvement, bug fix, addition). Therefore, the current thing called CHANGELOG is redundant with the other. I've tweaked the 'make_changelog' script to build a changelog according to these categories, provided that PR were labelled accordingly.



# Comments

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

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Assign the PR to a corresponding milestone
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: Matthias Benkort <[email protected]>
Co-authored-by: KtorZ <[email protected]>
iohk-bors bot added a commit that referenced this issue May 7, 2020
1630: Reduce logging from network layer r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

- Set network tip log level to debug
- Improve formatting of text log message
- Only log the tip when it changes, because the node can seem to send updates when nothing changes

### Comments

This is what it looks like now:

```
[cardano-wallet.network:Debug:18] [2020-05-06 07:49:54.10 UTC] Network node tip block height is 4125310 at hash c5cb3bcd
```


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: Matthias Benkort <[email protected]>
iohk-bors bot added a commit that referenced this issue May 7, 2020
1630: Reduce logging from network layer r=rvl a=rvl

### Issue Number

Relates to #1577 / ADP-83.

### Overview

- Set network tip log level to debug
- Improve formatting of text log message
- Only log the tip when it changes, because the node can seem to send updates when nothing changes

### Comments

This is what it looks like now:

```
[cardano-wallet.network:Debug:18] [2020-05-06 07:49:54.10 UTC] Network node tip block height is 4125310 at hash c5cb3bcd
```


Co-authored-by: Rodney Lorrimar <[email protected]>
@piotr-iohk
Copy link
Contributor

@rvl looks good, I just have few questions:

There will be a new tx_parameters table added, but no other fields are modified. So automatic migrations will be ok.

Not sure if it is possibly related but I raised DB migration issue -> #1649

Existing integration tests can check that TxParameters from node are being used in transactions -- because initial TxParameters are forced to zero in lib/byron/test/integration/Main.hs:182

Does that mean we could/should update existing integration tests for transactions to verify this (or add some new integration test checking this)

  1. Should it affect maybe GET /network/parameters (in terms of returned data perhaps?)

@rvl
Copy link
Contributor Author

rvl commented May 13, 2020

Thanks @piotr-iohk

  1. Not sure if it is possibly related but I raised DB migration issue -> DB migration tests failing intermittently #1649

I do not think it's related. The two causes are port conflicts and jormungandr changes. There was one migration test failure because I removed some table columns, but they started passing again once I put the columns back.

  1. Does that mean we could/should update existing integration tests for transactions to verify this (or add some new integration test checking this)

I decided that a parameter change integration test will be slow and flaky.

Something I could possibly do for the integration tests is set the initial TxParameters to dummy values. Then the transaction tests will show that the parameters did successfully update once. But I did not do that because it adds a bit of "wtf?" code to test/integration/Main.hs.

  1. Should it affect maybe GET /network/parameters (in terms of returned data perhaps?)

The two tx parameters were never included in this endpoint, so it stays exactly the same.

@piotr-iohk
Copy link
Contributor

ok, closing. 👍

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

No branches or pull requests

2 participants