Skip to content

Commit

Permalink
Add fourmolu code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
kderme committed Jan 17, 2023
1 parent ee88b07 commit 2353364
Show file tree
Hide file tree
Showing 157 changed files with 14,968 additions and 13,000 deletions.
124 changes: 65 additions & 59 deletions cardano-chain-gen/src/Cardano/Mock/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Mock.Chain
( Chain' (..)
, State
, Chain
, getTipState
, successorBlock
, pointOnChain
, rollback
, findFirstPointChain
, pointIsAfter
, findFirstPointByBlockNo
, currentTipBlockNo
) where

import Ouroboros.Consensus.Block
module Cardano.Mock.Chain (
Chain' (..),
State,
Chain,
getTipState,
successorBlock,
pointOnChain,
rollback,
findFirstPointChain,
pointIsAfter,
findFirstPointByBlockNo,
currentTipBlockNo,
) where

import Ouroboros.Consensus.Block
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus

import qualified Ouroboros.Network.AnchoredFragment as AF
import Ouroboros.Network.Block
import Ouroboros.Network.Block

-- | This looks a lot like the 'Chain' defined in Ouroboros.Network.MockChain.Chain
-- but this version includes also the ledger states.
data Chain' block st =
Genesis st
data Chain' block st
= Genesis st
| Chain' block st :> (block, st)
deriving (Eq, Ord, Show, Functor)

Expand All @@ -39,44 +38,47 @@ getTipState :: Chain' blk st -> st
getTipState (Genesis st) = st
getTipState (_ :> (_, st)) = st

successorBlock :: forall block . HasHeader block => Point block -> Chain block -> Maybe block
successorBlock :: forall block. HasHeader block => Point block -> Chain block -> Maybe block
successorBlock p c0 | headPoint c0 == p = Nothing
successorBlock p c0 =
go c0
go c0
where
go :: Chain block -> Maybe block
go (c :> (b',st') :> (b, _)) | blockPoint b' == p = Just b
| otherwise = go (c :> (b',st'))
go (Genesis _ :> (b, _)) | p == genesisPoint = Just b
go (c :> (b', st') :> (b, _))
| blockPoint b' == p = Just b
| otherwise = go (c :> (b', st'))
go (Genesis _ :> (b, _)) | p == genesisPoint = Just b
go _ = Nothing

pointOnChain :: HasHeader block => Point block -> Chain block -> Bool
pointOnChain GenesisPoint _ = True
pointOnChain (BlockPoint _ _) (Genesis _) = False
pointOnChain GenesisPoint _ = True
pointOnChain (BlockPoint _ _) (Genesis _) = False
pointOnChain p@(BlockPoint pslot phash) (c :> (b, _))
| pslot > blockSlot b = False
| pslot > blockSlot b = False
| phash == blockHash b = True
| otherwise = pointOnChain p c
| otherwise = pointOnChain p c

headPoint :: HasHeader block => Chain block -> Point block
headPoint (Genesis _) = genesisPoint
headPoint (_ :> (b, _)) = blockPoint b

findFirstPointChain
:: HasHeader block
=> [Point block]
-> Chain block
-> Maybe (Point block)
findFirstPointChain [] _ = Nothing
findFirstPointChain (p:ps) c
| pointOnChain p c = Just p
| otherwise = findFirstPointChain ps c
headPoint (_ :> (b, _)) = blockPoint b

findFirstPointChain ::
HasHeader block =>
[Point block] ->
Chain block ->
Maybe (Point block)
findFirstPointChain [] _ = Nothing
findFirstPointChain (p : ps) c
| pointOnChain p c = Just p
| otherwise = findFirstPointChain ps c

rollback :: HasHeader block => Chain block -> Point block -> Maybe (Chain block)
rollback (c :> (b, st)) p | blockPoint b == p = Just (c :> (b, st))
| otherwise = rollback c p
rollback (Genesis st) p | p == genesisPoint = Just (Genesis st)
| otherwise = Nothing
rollback (c :> (b, st)) p
| blockPoint b == p = Just (c :> (b, st))
| otherwise = rollback c p
rollback (Genesis st) p
| p == genesisPoint = Just (Genesis st)
| otherwise = Nothing

-- | Check whether the first point is after the second point on the chain.
-- Usually, this can simply be checked using the 'SlotNo's, but some blocks
Expand All @@ -85,16 +87,21 @@ rollback (Genesis st) p | p == genesisPoint = Just (Genesis st)
-- When the first point equals the second point, the answer will be 'False'.
--
-- PRECONDITION: both points are on the chain.
pointIsAfter :: HasHeader block
=> Point block -> Point block -> Chain block -> Bool
pointIsAfter ::
HasHeader block =>
Point block ->
Point block ->
Chain block ->
Bool
pointIsAfter pt1 pt2 c =
case pointSlot pt1 `compare` pointSlot pt2 of
LT -> False
GT -> True
EQ | Just (_, afterPt2) <- AF.splitAfterPoint (toAnchoredFragment c) pt2
-> AF.pointOnFragment pt1 afterPt2
| otherwise
-> False
case pointSlot pt1 `compare` pointSlot pt2 of
LT -> False
GT -> True
EQ
| Just (_, afterPt2) <- AF.splitAfterPoint (toAnchoredFragment c) pt2 ->
AF.pointOnFragment pt1 afterPt2
| otherwise ->
False

-- * Conversions to/from 'AnchoredFragment'

Expand All @@ -109,19 +116,18 @@ toOldestFirst :: Chain block -> [block]
toOldestFirst = reverse . toNewestFirst

-- | Produce the list of blocks, from most recent back to genesis
--
toNewestFirst :: Chain block -> [block]
toNewestFirst = foldChain (flip (:)) []

foldChain :: (a -> b -> a) -> a -> Chain b -> a
foldChain _blk gen (Genesis _st) = gen
foldChain blk gen (c :> (b, _)) = blk (foldChain blk gen c) b
foldChain blk gen (c :> (b, _)) = blk (foldChain blk gen c) b

findFirstPointByBlockNo
:: HasHeader block
=> Chain block
-> BlockNo
-> Maybe (Point block)
findFirstPointByBlockNo ::
HasHeader block =>
Chain block ->
BlockNo ->
Maybe (Point block)
findFirstPointByBlockNo c blkNo = case c of
Genesis _ -> Nothing
(_ :> (b, _)) | blockNo b == blkNo -> Just $ blockPoint b
Expand Down
42 changes: 20 additions & 22 deletions cardano-chain-gen/src/Cardano/Mock/ChainDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Mock.ChainDB
( ChainDB (..)
, initChainDB
, headTip
, currentState
, replaceGenesisDB
, extendChainDB
, findFirstPoint
, rollbackChainDB
, findPointByBlockNo
, currentBlockNo
) where

import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Config
import Ouroboros.Consensus.Ledger.Abstract
module Cardano.Mock.ChainDB (
ChainDB (..),
initChainDB,
headTip,
currentState,
replaceGenesisDB,
extendChainDB,
findFirstPoint,
rollbackChainDB,
findPointByBlockNo,
currentBlockNo,
) where

import Cardano.Mock.Chain
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Config
import Ouroboros.Consensus.Ledger.Abstract
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
import Ouroboros.Consensus.Ledger.SupportsProtocol

import Ouroboros.Network.Block (Tip (..))

import Cardano.Mock.Chain
import Ouroboros.Consensus.Ledger.SupportsProtocol
import Ouroboros.Network.Block (Tip (..))

-- | Thin layer around 'Chain' that knows how to apply blocks and maintain
-- new and old states. The state here, which is the 'Chain', is not a MVar,
Expand Down Expand Up @@ -73,7 +71,7 @@ findFirstPoint points chainDB = findFirstPointChain points (cchain chainDB)
rollbackChainDB :: HasHeader block => ChainDB block -> Point block -> Maybe (ChainDB block)
rollbackChainDB chainDB p = do
chain <- rollback (cchain chainDB) p
Just $ chainDB { cchain = chain}
Just $ chainDB {cchain = chain}

findPointByBlockNo :: HasHeader block => ChainDB block -> BlockNo -> Maybe (Point block)
findPointByBlockNo chainDB =
Expand Down
Loading

0 comments on commit 2353364

Please sign in to comment.