ouroboros-network-mock
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Network.Mock.Chain

Description

Reference implementation of a representation of a block chain

Synopsis

Chain type and fundamental operations

data Chain block Source #

Constructors

Genesis 
(Chain block) :> block infixl 5 

Instances

Instances details
Functor Chain Source # 
Instance details

Defined in Ouroboros.Network.Mock.Chain

Methods

fmap :: (a -> b) -> Chain a -> Chain b #

(<$) :: a -> Chain b -> Chain a #

Show block => Show (Chain block) Source # 
Instance details

Defined in Ouroboros.Network.Mock.Chain

Methods

showsPrec :: Int -> Chain block -> ShowS #

show :: Chain block -> String #

showList :: [Chain block] -> ShowS #

Eq block => Eq (Chain block) Source # 
Instance details

Defined in Ouroboros.Network.Mock.Chain

Methods

(==) :: Chain block -> Chain block -> Bool #

(/=) :: Chain block -> Chain block -> Bool #

Ord block => Ord (Chain block) Source # 
Instance details

Defined in Ouroboros.Network.Mock.Chain

Methods

compare :: Chain block -> Chain block -> Ordering #

(<) :: Chain block -> Chain block -> Bool #

(<=) :: Chain block -> Chain block -> Bool #

(>) :: Chain block -> Chain block -> Bool #

(>=) :: Chain block -> Chain block -> Bool #

max :: Chain block -> Chain block -> Chain block #

min :: Chain block -> Chain block -> Chain block #

Serialise block => Serialise (Chain block) Source # 
Instance details

Defined in Ouroboros.Network.Mock.Chain

Methods

encode :: Chain block -> Encoding #

decode :: Decoder s (Chain block) #

encodeList :: [Chain block] -> Encoding #

decodeList :: Decoder s [Chain block] #

valid :: HasFullHeader block => Chain block -> Bool Source #

validExtension :: (HasCallStack, HasFullHeader block) => Chain block -> block -> Bool Source #

foldChain :: (a -> b -> a) -> a -> Chain b -> a Source #

chainToList :: Chain block -> [block] Source #

Make a list from a Chain, in newest-to-oldest order.

Block re-exports

class (StandardHash b, Typeable b) => HasHeader b where #

Abstract over the shape of blocks (or indeed just block headers)

Methods

getHeaderFields :: b -> HeaderFields b #

type family HeaderHash (b :: k) #

Header hash

Instances

Instances details
type HeaderHash Block Source # 
Instance details

Defined in Ouroboros.Network.Mock.ConcreteBlock

type HeaderHash BlockHeader Source # 
Instance details

Defined in Ouroboros.Network.Mock.ConcreteBlock

type HeaderHash (HeaderFields b :: Type) 
Instance details

Defined in Ouroboros.Network.Block

type HeaderHash (Serialised block :: Type) 
Instance details

Defined in Ouroboros.Network.Block

type HeaderHash (Serialised block :: Type) = HeaderHash block

Point type

newtype Point (block :: k) #

A point on the chain is identified by its Slot and HeaderHash.

The Slot tells us where to look and the HeaderHash either simply serves as a check, or in some contexts it disambiguates blocks from different forks that were in the same slot.

It's a newtype rather than a type synonym, because using a type synonym would lead to ambiguity, since HeaderHash is a non-injective type family.

Constructors

Point 

Instances

Instances details
ShowProxy block => ShowProxy (Point block :: Type) 
Instance details

Defined in Ouroboros.Network.Block

Methods

showProxy :: Proxy (Point block) -> String #

Generic (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Associated Types

type Rep (Point block) 
Instance details

Defined in Ouroboros.Network.Block

type Rep (Point block) = D1 ('MetaData "Point" "Ouroboros.Network.Block" "ouroboros-network-api-0.11.0.0-inplace" 'True) (C1 ('MetaCons "Point" 'PrefixI 'True) (S1 ('MetaSel ('Just "getPoint") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (WithOrigin (Block SlotNo (HeaderHash block))))))

Methods

from :: Point block -> Rep (Point block) x #

to :: Rep (Point block) x -> Point block #

StandardHash block => Show (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

showsPrec :: Int -> Point block -> ShowS #

show :: Point block -> String #

showList :: [Point block] -> ShowS #

StandardHash block => Eq (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

(==) :: Point block -> Point block -> Bool #

(/=) :: Point block -> Point block -> Bool #

StandardHash block => Ord (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

compare :: Point block -> Point block -> Ordering #

(<) :: Point block -> Point block -> Bool #

(<=) :: Point block -> Point block -> Bool #

(>) :: Point block -> Point block -> Bool #

(>=) :: Point block -> Point block -> Bool #

max :: Point block -> Point block -> Point block #

min :: Point block -> Point block -> Point block #

StandardHash block => NoThunks (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

noThunks :: Context -> Point block -> IO (Maybe ThunkInfo) #

wNoThunks :: Context -> Point block -> IO (Maybe ThunkInfo) #

showTypeOf :: Proxy (Point block) -> String #

Serialise (HeaderHash block) => Serialise (Point block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

encode :: Point block -> Encoding #

decode :: Decoder s (Point block) #

encodeList :: [Point block] -> Encoding #

decodeList :: Decoder s [Point block] #

type Rep (Point block) 
Instance details

Defined in Ouroboros.Network.Block

type Rep (Point block) = D1 ('MetaData "Point" "Ouroboros.Network.Block" "ouroboros-network-api-0.11.0.0-inplace" 'True) (C1 ('MetaCons "Point" 'PrefixI 'True) (S1 ('MetaSel ('Just "getPoint") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (WithOrigin (Block SlotNo (HeaderHash block))))))

blockPoint :: HasHeader block => block -> Point block #

Chain construction and inspection

Genesis

Head inspection

headPoint :: HasHeader block => Chain block -> Point block Source #

headHash :: HasHeader block => Chain block -> ChainHash block Source #

headTip :: HasHeader block => Chain block -> Tip block Source #

headAnchor :: HasHeader block => Chain block -> Anchor block Source #

Basic operations

head :: Chain b -> Maybe b Source #

toNewestFirst :: Chain block -> [block] Source #

Produce the list of blocks, from most recent back to genesis

toOldestFirst :: Chain block -> [block] Source #

Produce the list of blocks, from genesis to the most recent

fromNewestFirst :: HasHeader block => [block] -> Chain block Source #

Make a chain from a list of blocks. The head of the list is the head of the chain.

fromOldestFirst :: HasHeader block => [block] -> Chain block Source #

Construct chain from list of blocks from oldest to newest

drop :: Int -> Chain block -> Chain block Source #

length :: Chain block -> Int Source #

null :: Chain block -> Bool Source #

takeWhile :: (blk -> Bool) -> Chain blk -> Chain blk Source #

Update type and operations

data ChainUpdate (block :: k) a #

A representation of two actions to update a chain: add a block or roll back to a previous point.

The type parameter a is there to allow a Functor instance. Typically, it will be instantiated with block itself.

Constructors

AddBlock a 
RollBack (Point block) 

Instances

Instances details
Functor (ChainUpdate block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

fmap :: (a -> b) -> ChainUpdate block a -> ChainUpdate block b #

(<$) :: a -> ChainUpdate block b -> ChainUpdate block a #

Foldable (ChainUpdate block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

fold :: Monoid m => ChainUpdate block m -> m #

foldMap :: Monoid m => (a -> m) -> ChainUpdate block a -> m #

foldMap' :: Monoid m => (a -> m) -> ChainUpdate block a -> m #

foldr :: (a -> b -> b) -> b -> ChainUpdate block a -> b #

foldr' :: (a -> b -> b) -> b -> ChainUpdate block a -> b #

foldl :: (b -> a -> b) -> b -> ChainUpdate block a -> b #

foldl' :: (b -> a -> b) -> b -> ChainUpdate block a -> b #

foldr1 :: (a -> a -> a) -> ChainUpdate block a -> a #

foldl1 :: (a -> a -> a) -> ChainUpdate block a -> a #

toList :: ChainUpdate block a -> [a] #

null :: ChainUpdate block a -> Bool #

length :: ChainUpdate block a -> Int #

elem :: Eq a => a -> ChainUpdate block a -> Bool #

maximum :: Ord a => ChainUpdate block a -> a #

minimum :: Ord a => ChainUpdate block a -> a #

sum :: Num a => ChainUpdate block a -> a #

product :: Num a => ChainUpdate block a -> a #

Traversable (ChainUpdate block) 
Instance details

Defined in Ouroboros.Network.Block

Methods

traverse :: Applicative f => (a -> f b) -> ChainUpdate block a -> f (ChainUpdate block b) #

sequenceA :: Applicative f => ChainUpdate block (f a) -> f (ChainUpdate block a) #

mapM :: Monad m => (a -> m b) -> ChainUpdate block a -> m (ChainUpdate block b) #

sequence :: Monad m => ChainUpdate block (m a) -> m (ChainUpdate block a) #

(StandardHash block, Show a) => Show (ChainUpdate block a) 
Instance details

Defined in Ouroboros.Network.Block

Methods

showsPrec :: Int -> ChainUpdate block a -> ShowS #

show :: ChainUpdate block a -> String #

showList :: [ChainUpdate block a] -> ShowS #

(StandardHash block, Eq a) => Eq (ChainUpdate block a) 
Instance details

Defined in Ouroboros.Network.Block

Methods

(==) :: ChainUpdate block a -> ChainUpdate block a -> Bool #

(/=) :: ChainUpdate block a -> ChainUpdate block a -> Bool #

addBlock :: HasHeader block => block -> Chain block -> Chain block Source #

rollback :: HasHeader block => Point block -> Chain block -> Maybe (Chain block) Source #

applyChainUpdate :: HasHeader block => ChainUpdate block block -> Chain block -> Maybe (Chain block) Source #

applyChainUpdates :: HasHeader block => [ChainUpdate block block] -> Chain block -> Maybe (Chain block) Source #

Special operations

pointOnChain :: HasHeader block => Point block -> Chain block -> Bool Source #

pointIsAfter :: HasHeader block => Point block -> Point block -> Chain block -> Bool Source #

Check whether the first point is after the second point on the chain. Usually, this can simply be checked using the SlotNos, but some blocks may have the same SlotNo.

When the first point equals the second point, the answer will be False.

PRECONDITION: both points are on the chain.

successorBlock :: HasHeader block => Point block -> Chain block -> Maybe block Source #

A Just result holds the provided point's successor block. A Nothing result means the provided point was the tip.

The function will error if the point is not on the chain - callers can use pointOnChain to check point membership on a chain in advance of calling this function.

selectChain :: HasHeader block => Chain block -> Chain block -> Chain block Source #

selectPoints :: HasHeader block => [Int] -> Chain block -> [Point block] Source #

Select a bunch of Points based on offsets from the head of the chain. This is used in the chain consumer protocol as part of finding the intersection between a local and remote chain.

The typical pattern is to use a selection of offsets covering the last K blocks, biased towards more recent blocks. For example:

selectPoints (0 : [ fib n | n <- [1 .. 17] ])

findBlock :: (block -> Bool) -> Chain block -> Maybe block Source #

selectBlockRange :: HasHeader block => Chain block -> Point block -> Point block -> Maybe [block] Source #

findFirstPoint :: HasHeader block => [Point block] -> Chain block -> Maybe (Point block) Source #

intersectChains :: HasHeader block => Chain block -> Chain block -> Maybe (Point block) Source #

isPrefixOf :: Eq block => Chain block -> Chain block -> Bool Source #

Conversion to/from AnchoredFragment

fromAnchoredFragment :: HasHeader block => AnchoredFragment block -> Maybe (Chain block) Source #

Convert an AnchoredFragment to a Chain.

The anchor of the fragment must be genesisPoint, otherwise Nothing is returned.

toAnchoredFragment :: HasHeader block => Chain block -> AnchoredFragment block Source #

Convert a Chain to an AnchoredFragment.

The anchor of the fragment will be genesisPoint.

Helper functions

prettyPrintChain :: String -> (block -> String) -> Chain block -> String Source #