Using stack GHCI to explorer cardano-sl core on Debian Gnu/Linux Sid and Plant UML to visualize

in utopian-io •  7 years ago  (edited)

TLDR;

curl -sSL https://get.haskellstack.org/ | sh
export PATH=/home/mdupont/.local/bin:$PATH
stack upgrade
git clone [email protected]:h4ck3rm1k3/cardano-sl.git
pushd cardano-sl
git checkout use-universum
stack ghci --no-load

and finally

:l Pos.Core

Use
:help for help
:browse to see current definitions
:info <name> to get one
:t <expr> to see the type of a definition

Here are some starting points:
:info Blockchain

*Pos.Core> :info Blockchain
class Blockchain p where
  data family BodyProof p
  data family ConsensusData p
  type family ExtraHeaderData p :: *
    Default: ()
  type family BBlockHeader p :: *
    Default: GenericBlockHeader p
  type family BHeaderHash p :: *
    Default: HeaderHash
  data family Body p
  type family ExtraBodyData p :: *
    Default: ()
  type family BBlock p :: *
    Default: GenericBlock p
  mkBodyProof :: Body p -> BodyProof p
  checkBodyProof :: Control.Monad.Error.Class.MonadError
                      Data.Text.Internal.Text m =>
                    Body p -> BodyProof p -> m ()
  default checkBodyProof :: (Control.Monad.Error.Class.MonadError
                               Data.Text.Internal.Text m,
                             Data.Text.Buildable.Buildable (BodyProof p),
                             ghc-prim-0.5.0.0:GHC.Classes.Eq (BodyProof p)) =>
                            Body p -> BodyProof p -> m ()
  {-# MINIMAL mkBodyProof #-}
        -- Defined at /home/mdupont/experiments/cardano-sl/core/Pos/Core/Block/Blockchain.hs:51:1
instance (HasConfiguration, Pos.Binary.Class.Core.Bi BlockHeader,
          Pos.Binary.Class.Core.Bi (BodyProof MainBlockchain),
          IsMainHeader (GenericBlockHeader MainBlockchain)) =>
         Blockchain MainBlockchain
  -- Defined at /home/mdupont/experiments/cardano-sl/core/Pos/Core/Block/Main/Chain.hs:36:10
instance Blockchain GenesisBlockchain
  -- Defined at /home/mdupont/experiments/cardano-sl/core/Pos/Core/Block/Genesis/Chain.hs:24:10

Now for the boring details

Audience

Warning : This is an advanced tutorial that does not cover all the groundwork you will need to learn all these topics in detail. You should as a beginner be able to jump into the system and get learning but you might have to research yourself on some of the steps to begin with.

GHI

GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
https://wiki.haskell.org/GHC/GHCi

Prelude and Universum

Learning the cardano-sl codebase can be very daunting, there is the haskell programming language and all the math behind it. We can take this step by step and start exploring the system from the inside using the GHCI.

The first lesson you need to learn about haskell is that everything is a function or defined in a library, even the things that you would never expect to be such things like 'return' and 'IO', they are defined in a library called Prelude which the cardano project does not use. If you try and use ghci on the codebase you will encounter the error https://github.com/input-output-hk/cardano-sl/issues/2214 and you will want to get my branch h4ck3rm1k3:use-universum

Stack

What is stack? Stack is a nice way to use haskell. I would prefer to use the debian package manager but it is basically broken and useless for haskell except for basic modules. In haskell you have many different modules at different versions and you need to maintain those in different states in parallel. Debian can do that with more overhead. Cardano uses both stack and nix and seems to translate stack to nix.

https://docs.haskellstack.org/en/stable/install_and_upgrade/

curl -sSL https://get.haskellstack.org/ | sh

and stack upgrade to avoid some strange json errors.

Invoking

When you invoke ghc you need to tell it not to load all modules because the cardano system has multiple conflicting modules.
That is why you use :
stack ghci --no-load

Loading

You can load any module, I am starting with the core code, the Pos.Core ProofOfStake Core
you load that with
:l Pos.Core
After you load one module all the other modules are discarded.

Exploring

If you pick a starting point, say the Blockchain class, you can drill down into the parts.
here are some of the ones I looked at in order

  1. Blockchain
  2. GenericBlock
  3. UnsafeGenericBlock
  4. GenericBlockHeader

Visualizing

I started to translate this information into plantuml syntax

@startuml
Blockchain -- ConsensusData
Blockchain -- BodyProof
Blockchain -- ExtraHeaderData
Blockchain -- BBlockHeader
Blockchain -- BHeaderHash
Blockchain -- Body
Blockchain -- ExtraBodyData
Blockchain -- BBlock
BBlockHeader -- GenericBlockHeader
BHeaderHash -- HeaderHash
BBlock-- GenericBlock
GenericBlock -- UnsafeGenericBlock
UnsafeGenericBlock --  GenericBlockHeader : _gbHeader
UnsafeGenericBlock --  Body:                _gbBody
UnsafeGenericBlock --  ExtraBodyData :      _gbExtra
@enduml

http://www.plantuml.com/plantuml/uml/TP513i8W44NtSmekq0kmQgh6BjsuDYDCRME516YYjxTGeu30RlxybpypjDQXSSjZXcxMuYv6d1Hh6hRJofAoYzsZmwpNQVaw6Qs7J3yydS6UK98fKQ7SsdbhKtkqOo6gbEBrQWR4KLu-aY8pYMI0NwHtn0C4MqO1G5nwvAmi3fIO_YLlBCGppYwtwsUK2kLNv2nxAnLEKc6Isp3-PO8Eq9AIwrU_0G00
image



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @h4ck3rm1k3st33m I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Hey @h4ck3rm1k3st33m I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x