Skip to content

Commit

Permalink
Move all haskell projects to toplevel
Browse files Browse the repository at this point in the history
The whole project is an experiment so no need to have another
level. It simplifies IDE configuration
  • Loading branch information
abailly-iohk committed Feb 6, 2024
1 parent 7ccdc00 commit 5c06c07
Show file tree
Hide file tree
Showing 27 changed files with 92 additions and 73 deletions.
32 changes: 25 additions & 7 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-- Custom repository for cardano haskell packages, see CONTRIBUTING.md
repository cardano-haskell-packages
url: https://input-output-hk.github.io/cardano-haskell-packages
secure: True
Expand All @@ -10,13 +9,32 @@ repository cardano-haskell-packages
c00aae8461a256275598500ea0e187588c35a5d5d7454fb57eac18d9edb86a56
d4a35cd3121aa00d18544bb0ac01c3e1691d618f462c46129271bccf39f7e8ee

-- See CONTRIBUTING.md for information about when and how to update these.
index-state: 2024-01-05T13:49:07Z
index-state:
, hackage.haskell.org 2023-09-14T06:54:18Z
, cardano-haskell-packages 2023-09-14T16:16:59Z
, hackage.haskell.org 2023-12-15T16:32:49Z
, cardano-haskell-packages 2023-12-15T14:50:31Z

packages: .
packages:
random-forks
quickcheck-model
sketch

-- Always show detailed output for tests
tests: True
test-show-details: direct

package strict-containers
ghc-options: -Wno-noncanonical-monad-instances -Wno-error=noncanonical-monad-instances

package comonad
flags: -test-doctests

package cryptonite
-- Using RDRAND instead of /dev/urandom as an entropy source for key
-- generation is dubious. Set the flag so we use /dev/urandom by default.
flags: -support_rdrand

benchmarks: False
optimization: False

flags: +defer-plugin-errors

write-ghc-environment-files: never
40 changes: 0 additions & 40 deletions experiments/cabal.project

This file was deleted.

1 change: 0 additions & 1 deletion experiments/quickcheck-model/src/Peras/Model.hs

This file was deleted.

Binary file removed experiments/random-forks/chain-100.png
Binary file not shown.
Binary file removed experiments/random-forks/peers.png
Binary file not shown.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ library
-- other-extensions:

-- Other library packages from which modules are imported.
build-depends: base ^>=4.18.1.0
build-depends: base ^>=4.18.1.0,
bytestring,
quickcheck-dynamic

-- Directories containing source files.
hs-source-dirs: src
Expand Down Expand Up @@ -100,12 +102,13 @@ test-suite quickcheck-model-test

-- Test dependencies.
build-depends:
base ^>=4.18.1.0
, quickcheck-model
, hspec
, QuickCheck
, io-sim
, si-timers
base ^>=4.18.1.0
, QuickCheck
, hspec
, io-sim
, quickcheck-dynamic
, quickcheck-model
, si-timers

build-tool-depends:
hspec-discover:hspec-discover
32 changes: 32 additions & 0 deletions quickcheck-model/src/Peras/Model.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
module Peras.Model where

import Test.QuickCheck.StateModel(StateModel(..), Var)
import GHC.Generics (Generic)
import Data.ByteString (ByteString)

-- | We model a network of nodes interconnected through a diffusion layer.
data Network = Network
deriving (Show, Generic)

newtype BlockId = BlockId { unBlockId :: ByteString }
deriving (Eq, Show)

newtype NodeId = NodeId { unNodeId :: ByteString }
deriving (Eq, Show)

newtype Block = Block { blockId :: BlockId }
deriving (Eq, Show)

data Chain = Genesis
| Chain Block Chain
deriving (Eq, Show)

instance StateModel Network where

data Action Network a where
DispatchBlock :: Action Network (Maybe (Var Block ))

ObserveNode :: NodeId -> Action Network Chain
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}

module Peras.RandomForks.Chain (
Block(..)
Expand Down Expand Up @@ -30,30 +34,33 @@ mkBlock
-> Slot
-> IO Block
mkBlock name slot = Block name slot <$> nextRandom
newtype Chain =

data Chain =
Chain
{
blocks :: [Block]
block :: Block,
prev :: Chain
}
deriving (Eq, Ord, Read, Show)

instance Semigroup Chain where
Chain x <> Chain y = Chain $ x <> y
| Genesis
deriving stock (Eq, Ord, Read, Show)

instance Monoid Chain where
mempty = Chain mempty
blocks :: Chain -> [Block]
blocks = \case
Genesis -> []
Chain {block, prev} -> block : blocks prev

chainLength
:: Chain
-> Int
chainLength = length . blocks
chainLength = \case
Genesis -> 0
Chain{prev} -> 1 + chainLength prev

extendChain
:: Chain
-> Block
:: Block
-> Chain
-> Chain
extendChain = (. (Chain . pure)) . (<>)
extendChain block = Chain block

data Message =
Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Peras.RandomForks.Peer (

import Data.List (delete)
import Data.Maybe (fromMaybe)
import Peras.RandomForks.Chain (Chain, Message(..), chainLength, extendChain, mkBlock)
import Peras.RandomForks.Chain (Chain (Genesis), Message(..), chainLength, extendChain, mkBlock)
import Peras.RandomForks.Protocol (Protocol(..), Parameters(..), isCommitteeMember, isFirstSlotInRound, isSlotLeader)
import Peras.RandomForks.Types (Currency, PeerName(..), Slot)
import System.Random (randomRIO)
Expand Down Expand Up @@ -56,13 +56,13 @@ randomPeers Parameters{..} protocol =
upstreams = M.fromListWith (<>) . concatMap (\(name, names) -> (, S.singleton name) <$> S.toList names) $ M.toList downstreams
randomPeer name =
do
currency <- randomRIO (1, maximumCurrency)
currency <- randomRIO (1, maximumCurrency)
vrfOutput <- randomRIO (0, 1)
slotLeader <- isSlotLeader protocol currency
committeeMember <- isCommitteeMember protocol currency
let upstream = fromMaybe mempty $ M.lookup name upstreams
downstream = fromMaybe mempty $ M.lookup name downstreams
preferredChain = mempty
preferredChain = Genesis
pendingMessages = mempty
pure PeerState{..}
Peers . M.fromList <$> mapM (\name -> (name, ) <$> randomPeer name) peerNames
Expand All @@ -83,7 +83,7 @@ nextSlot protocol slot name state@PeerState{..} =
preferredChainBeforeNow <- (longest !!) <$> randomRIO (0, length longest - 1)
preferredChain' <-
if slotLeader'
then extendChain preferredChainBeforeNow <$> mkBlock name slot
then (`extendChain` preferredChainBeforeNow) <$> mkBlock name slot
else pure preferredChainBeforeNow
let
newMessages =
Expand All @@ -105,7 +105,7 @@ nextSlot protocol slot name state@PeerState{..} =
, preferredChain = preferredChain'
, pendingMessages = mempty
}
pure (newState, newMessages)
pure (newState, newMessages)

peerGraph
:: Peers
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5c06c07

Please sign in to comment.