Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

[CDEC-464] Switch uses of pipes to conduit #3305

Merged
merged 1 commit into from
Jul 31, 2018
Merged

Conversation

mhuesch
Copy link
Contributor

@mhuesch mhuesch commented Jul 25, 2018

Description

The Cardano SL codebase currently depends on the pipes package in addition to depending on conduit.

Since used of conduit is far more prevalent we should replace all usage of pipes with conduit.

Linked issue

https://iohk.myjetbrains.com/youtrack/issue/CDEC-464

Type of change

  • [~] 🐞 Bug fix (non-breaking change which fixes an issue)
  • [~] 🛠 New feature (non-breaking change which adds functionality)
  • [~] ⚠️ Breaking change (fix or feature that would cause existing functionality to change)
  • 🏭 Refactoring that does not change existing functionality but does improve things like code readability, structure etc
  • [~] 🔨 New or improved tests for existing code
  • [~] ⛑ git-flow chore (backport, hotfix, etc)

Developer checklist

  • I have read the style guide document, and my code follows the code style of this project.
  • If my code deals with exceptions, it follows the guidelines.
  • I have updated any documentation accordingly, if needed. Documentation changes can be reflected in opening a PR on cardanodocs.com, amending the inline Haddock comments, any relevant README file or one of the document listed in the docs directory.

Testing checklist

  • [~] I have added tests to cover my changes.
  • All new and existing tests passed.
    ^ no new tests added - existing ones should cover these changes.

@mhuesch mhuesch requested a review from erikd as a code owner July 25, 2018 23:03
Copy link
Contributor Author

@mhuesch mhuesch left a comment

Choose a reason for hiding this comment

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

I marked two areas that need attention.

I did not replace uses of pipes in the post-mortem executable from cardano-sl-tools, since it does not build at present.

Once this PR is up I will look at bumping dependencies to get post-mortem buildable, and then will change uses of pipes there.

lift $ sendRaw conv $ serializeMsgStreamBlock $ MsgSerializedBlock b
loop nodeId conv (window - 1)
mB <- await
case mB of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

❗️ The await from conduit returns a (Maybe a). The await from pipes returns an a.

The conduit docs say here:

Wait for a single input value from upstream. If no data is available, returns Nothing. Once await returns Nothing, subsequent calls will also return Nothing.

It seems like if/when we hit the Nothing case, we will loop forever. However in the previous pipes code, I believe the Nothing values would just be dropped, so I think the behavior should be the same.

Copy link
Member

@erikd erikd Jul 25, 2018

Choose a reason for hiding this comment

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

With conduit, await returning a Nothing means there is no more data to be got from the source.

I therefore think the we should not loop there. Instead, the Nothing case should just do pure () or pure someReturnValue depending on what is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This chunk is in IO (), so pure () seems appropriate.

hoistLogic :: Monad m => (forall x . m x -> n x) -> Logic m -> Logic n
hoistLogic nat logic = logic
{ getSerializedBlock = nat . getSerializedBlock logic
, streamBlocks = unsafeHoist nat . streamBlocks logic
, streamBlocks = transPipe nat . streamBlocks logic
Copy link
Contributor Author

@mhuesch mhuesch Jul 25, 2018

Choose a reason for hiding this comment

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

❗️transPipe comes with a warning in the docs:

Note that the monad transforming function will be run multiple times, resulting in unintuitive behavior in some cases. For a fuller treatment, please see:

https://github.com/snoyberg/conduit/wiki/Dealing-with-monad-transformers

From reading the github post I think we're ok, because the only use of hoistLogic calls elimRealMode to unwrap a ReaderT, which shouldn't involve the state-carry-over effects mentioned as potential problems.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that seems legit!

Copy link
Contributor

@k0001 k0001 Jul 27, 2018

Choose a reason for hiding this comment

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

Another way of saying this is that the passed in function foo :: (forall x. m x -> n x) must be a monad morphism and not just any natural transformation. What this means, more or less, is that foo a >> foo b should behave the same as foo (a >> b). If we set foo = flip evalState 1, it's easy to see how these two examples will behave differently. I would recommend improving the documentation here, saying that the given function is expected to be a monad morphism.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@k0001 Thanks, that is a good recommendation. I've updated the comment here.

@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch from 74ffa66 to 8c4e8ce Compare July 25, 2018 23:13
erikd
erikd previously requested changes Jul 25, 2018
Copy link
Member

@erikd erikd left a comment

Choose a reason for hiding this comment

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

The handling of the Nothing case from the return of Counduit.await is not correct and needs fixing.

@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch from 8c4e8ce to 221c1bf Compare July 26, 2018 00:14
@erikd erikd dismissed their stale review July 26, 2018 01:07

Conduit.await issue addressed

@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch 2 times, most recently from d52d77c to 2c5c07c Compare July 27, 2018 01:24
@mhuesch mhuesch changed the title [CDEC-471] Switch uses of pipes to conduit [CDEC-464] Switch uses of pipes to conduit Jul 27, 2018
@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch from 2c5c07c to 172bcc0 Compare July 27, 2018 15:51
@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch from 172bcc0 to 3e5d6e9 Compare July 27, 2018 15:52
I did not replace uses of `pipes` in the `post-mortem` executable from
`cardano-sl-tools`, since it does not build at present.
@mhuesch mhuesch force-pushed the mhuesch/CDEC-464 branch from 3e5d6e9 to d1f9688 Compare July 30, 2018 14:31
@mhuesch
Copy link
Contributor Author

mhuesch commented Jul 31, 2018

@erikd I believe that I've confirmed correct behavior between the latest revision on this branch, the release/1.3.0 revision, and the latest develop revision.

As such, this should be safe to merge.

@mhuesch mhuesch merged commit 99cc588 into develop Jul 31, 2018
@mhuesch mhuesch deleted the mhuesch/CDEC-464 branch July 31, 2018 18:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants