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

Implement NetworkLayer getNext for Jörmungandr #405

Merged
merged 11 commits into from
Jun 15, 2019
Merged

Conversation

Anviking
Copy link
Member

@Anviking Anviking commented Jun 12, 2019

Issue Number

#219

Overview

WIP!

Comments

Fetching blocks with nextBlocks: stack ghci cardano-wallet-jormungandr

import Cardano.Wallet.Jormungandr.Network
import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Network
import Cardano.Wallet
import Data.Generics.Internal.VL.Lens ( view, (^.) )
:m -Cardano.Wallet.Jormungandr.Binary
:set -XOverloadedLabels

let nul = Hash "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL"
let t0 = BlockHeader (SlotId 0 0) nul
nw <- newNetworkLayer (BaseUrl Http "localhost" 8081 "/api")
unsafeRunExceptT $ nextBlocks nw t0

(mapM_ (\b -> print $ b ^. #header ^. #slotId)) =<< (unsafeRunExceptT $ nextBlocks nw t0)

How to do required setup:

:set -XTypeApplications
:set -XDataKinds
import qualified Cardano.Wallet.DB.MVar as MVar
let tl = undefined
db <- MVar.newDBLayer

-- Setup a network layer, but we also need to talk to the underlying client ('j') directly here
mgr <- newManager defaultManagerSettings
let j = mkJormungandrLayer mgr (BaseUrl Http "localhost" 8081 "/api")
let nw = mkNetworkLayer j
waitForConnection nw defaultRetryPolicy

-- since the 'NetworkLayer' is connected, the underlying 'JormungandrLayer' is too
let absentParent = Hash "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL"
[block0Id] <- unsafeRunExceptT $ getDescendantIds j absentParent 1
block0 <- unsafeRunExceptT $ getBlock j block0Id

-- TODO: setup initial state in a custom way

wallet <- newWalletLayer @_ @(Jormungandr 'Mainnet) block0 db nw tl

@Anviking Anviking self-assigned this Jun 12, 2019
ErrGetDescendantsNetworkUnreachable e ->
ErrGetBlockNetworkUnreachable e
ErrGetDescendantsParentNotFound e ->
ErrGetBlockNotFound . T.pack . show $ e
Copy link
Member

Choose a reason for hiding this comment

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

Hmm... not fond of this raw Text here. We use Haskell! Let's use type 😶 ..especially when we have a nice Hash "BlockHeader" we can pass around. Even more context would be nice:

  • What's the starting point?
  • How many descendant are we trying to fetch?
  • What's the parent hash?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm… good point. I suppose we manage with Hash "BlockHeader". ErrGetBlock is defined in Cardano.Wallet.Network, and thus shared, but in practice it will work out since it is not used in http-bridge.

Do we want to go make ErrGetBlock completely tailored to Jormungandr?

Copy link
Member

Choose a reason for hiding this comment

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

Hmmm. No. These were just a few suggestions. We can at least have the Hash "BlockHeader" of block. Any other detail that isn't jormungandr-specific is welcome.

@Anviking Anviking force-pushed the anviking/219/next-blocks branch 3 times, most recently from 35cdb4a to ebe0405 Compare June 13, 2019 21:06
@Anviking Anviking marked this pull request as ready for review June 13, 2019 21:13
let tmpTip = BlockHeader
(SlotId maxBound maxBound)
(Hash "<Hash of the block before genesis>")
let tmpCp = initWallet tmpTip s
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm... what?
We should change initWallet to not take a block header but the actual first block. This will make the akward call to applyBlocks superfluous; initializing a wallet does the genesis block application (for what it's worth) and return an initial checkpoint.

lib/jormungandr/src/Cardano/Wallet/Jormungandr/Binary.hs Outdated Show resolved Hide resolved
lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs Outdated Show resolved Hide resolved
@Anviking Anviking force-pushed the anviking/219/next-blocks branch 2 times, most recently from 3e18f49 to d3f08d2 Compare June 14, 2019 12:51
@KtorZ KtorZ force-pushed the anviking/219/next-blocks branch 2 times, most recently from 853c723 to 7049c56 Compare June 14, 2019 15:48
@Anviking Anviking force-pushed the anviking/219/next-blocks branch from 7049c56 to b606f51 Compare June 14, 2019 17:47
@Anviking
Copy link
Member Author

@KtorZ I reworked this commit be7931d to use prefilterBlock inside initWallet block0

@Anviking Anviking force-pushed the anviking/219/next-blocks branch 3 times, most recently from 8d5af8d to 8975467 Compare June 14, 2019 19:10
initWallet = Wallet mempty mempty
initWallet block s =
let ((txs, utxo'), s') = prefilterBlock (Proxy @t) block mempty s
in Wallet utxo' (Set.fromList txs) (header block) s'
Copy link
Member

Choose a reason for hiding this comment

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

Hmmmm. What about applyBlock block (Wallet mempty mempty (header block) s ?
Feels a bit weird to copy paste partial stuff from applyBlock, especially because, there could be some transactions that are ours in the genesis block (and there will be actually).

Copy link
Member Author

@Anviking Anviking Jun 14, 2019

Choose a reason for hiding this comment

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

We will then transition from
(Wallet mempty mempty (header block) s
to
(Wallet x y (header block) s'

i.e, the header inside the first applyBlock will be incorrect. I was deliberately trying to avoid this.

I'd be fine with this anyway, if it indeed compiles though.

Copy link
Member

Choose a reason for hiding this comment

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

Hmmm. It's probably not important for now since there will be no transactions yet. And maybe it's best to leave it for later actually until we have confirmation of how the current mainnet is going to be included in the genesis block. So, leave it be 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

could be some transactions that are ours in the genesis block

I thought (Set.fromList txs) would add them though 🤔 ?

{ slotId = SlotId 0 0
, prevBlockHash = Hash "genesis"
}
, transactions = []
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@KtorZ KtorZ left a comment

Choose a reason for hiding this comment

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

👌

@Anviking Anviking force-pushed the anviking/219/next-blocks branch from 8345fc8 to 1d1e4fa Compare June 14, 2019 21:34
@KtorZ KtorZ merged commit 688aa40 into master Jun 15, 2019
@KtorZ KtorZ deleted the anviking/219/next-blocks branch June 15, 2019 03:46
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