Safe Haskell | None |
---|---|
Language | Haskell2010 |
Reference implementation of a representation of a block chain
Synopsis
- data Chain block
- valid :: HasFullHeader block => Chain block -> Bool
- validExtension :: (HasCallStack, HasFullHeader block) => Chain block -> block -> Bool
- foldChain :: (a -> b -> a) -> a -> Chain b -> a
- chainToList :: Chain block -> [block]
- class (StandardHash b, Typeable b) => HasHeader b where
- getHeaderFields :: b -> HeaderFields b
- type family HeaderHash (b :: k)
- newtype Point (block :: k) = Point {
- getPoint :: WithOrigin (Block SlotNo (HeaderHash block))
- blockPoint :: HasHeader block => block -> Point block
- genesis :: Chain b
- headPoint :: HasHeader block => Chain block -> Point block
- headSlot :: HasHeader block => Chain block -> WithOrigin SlotNo
- headHash :: HasHeader block => Chain block -> ChainHash block
- headTip :: HasHeader block => Chain block -> Tip block
- headBlockNo :: HasHeader block => Chain block -> WithOrigin BlockNo
- headAnchor :: HasHeader block => Chain block -> Anchor block
- head :: Chain b -> Maybe b
- toNewestFirst :: Chain block -> [block]
- toOldestFirst :: Chain block -> [block]
- fromNewestFirst :: HasHeader block => [block] -> Chain block
- fromOldestFirst :: HasHeader block => [block] -> Chain block
- drop :: Int -> Chain block -> Chain block
- length :: Chain block -> Int
- null :: Chain block -> Bool
- takeWhile :: (blk -> Bool) -> Chain blk -> Chain blk
- data ChainUpdate (block :: k) a
- addBlock :: HasHeader block => block -> Chain block -> Chain block
- rollback :: HasHeader block => Point block -> Chain block -> Maybe (Chain block)
- applyChainUpdate :: HasHeader block => ChainUpdate block block -> Chain block -> Maybe (Chain block)
- applyChainUpdates :: HasHeader block => [ChainUpdate block block] -> Chain block -> Maybe (Chain block)
- pointOnChain :: HasHeader block => Point block -> Chain block -> Bool
- pointIsAfter :: HasHeader block => Point block -> Point block -> Chain block -> Bool
- successorBlock :: HasHeader block => Point block -> Chain block -> Maybe block
- selectChain :: HasHeader block => Chain block -> Chain block -> Chain block
- selectPoints :: HasHeader block => [Int] -> Chain block -> [Point block]
- findBlock :: (block -> Bool) -> Chain block -> Maybe block
- selectBlockRange :: HasHeader block => Chain block -> Point block -> Point block -> Maybe [block]
- findFirstPoint :: HasHeader block => [Point block] -> Chain block -> Maybe (Point block)
- intersectChains :: HasHeader block => Chain block -> Chain block -> Maybe (Point block)
- isPrefixOf :: Eq block => Chain block -> Chain block -> Bool
- fromAnchoredFragment :: HasHeader block => AnchoredFragment block -> Maybe (Chain block)
- toAnchoredFragment :: HasHeader block => Chain block -> AnchoredFragment block
- prettyPrintChain :: String -> (block -> String) -> Chain block -> String
Chain type and fundamental operations
Instances
Functor Chain Source # | |
Show block => Show (Chain block) Source # | |
Eq block => Eq (Chain block) Source # | |
Ord block => Ord (Chain block) Source # | |
Defined in Ouroboros.Network.Mock.Chain | |
Serialise block => Serialise (Chain block) Source # | |
validExtension :: (HasCallStack, HasFullHeader block) => Chain block -> block -> Bool 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)
getHeaderFields :: b -> HeaderFields b #
Instances
HasHeader Block Source # | |
Defined in Ouroboros.Network.Mock.ConcreteBlock getHeaderFields :: Block -> HeaderFields Block # | |
HasHeader BlockHeader Source # | |
Defined in Ouroboros.Network.Mock.ConcreteBlock | |
(StandardHash b, Typeable b, Typeable k) => HasHeader (HeaderFields b) | |
Defined in Ouroboros.Network.Block getHeaderFields :: HeaderFields b -> HeaderFields (HeaderFields b) # |
type family HeaderHash (b :: k) #
Header hash
Instances
type HeaderHash Block Source # | |
Defined in Ouroboros.Network.Mock.ConcreteBlock | |
type HeaderHash BlockHeader Source # | |
Defined in Ouroboros.Network.Mock.ConcreteBlock | |
type HeaderHash (HeaderFields b :: Type) | |
Defined in Ouroboros.Network.Block | |
type HeaderHash (Serialised block :: Type) | |
Defined in Ouroboros.Network.Block |
Point type
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.
Point | |
|
Instances
blockPoint :: HasHeader block => block -> Point block #
Chain construction and inspection
Genesis
Head inspection
headBlockNo :: HasHeader block => Chain block -> WithOrigin BlockNo Source #
Basic operations
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
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.
Instances
Functor (ChainUpdate block) | |
Defined in Ouroboros.Network.Block fmap :: (a -> b) -> ChainUpdate block a -> ChainUpdate block b # (<$) :: a -> ChainUpdate block b -> ChainUpdate block a # | |
Foldable (ChainUpdate block) | |
Defined in Ouroboros.Network.Block 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) | |
Defined in Ouroboros.Network.Block 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) | |
Defined in Ouroboros.Network.Block showsPrec :: Int -> ChainUpdate block a -> ShowS # show :: ChainUpdate block a -> String # showList :: [ChainUpdate block a] -> ShowS # | |
(StandardHash block, Eq a) => Eq (ChainUpdate block a) | |
Defined in Ouroboros.Network.Block (==) :: ChainUpdate block a -> ChainUpdate block a -> Bool # (/=) :: ChainUpdate block a -> ChainUpdate block a -> Bool # |
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
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.
selectPoints :: HasHeader block => [Int] -> Chain block -> [Point block] Source #
Select a bunch of Point
s 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] ])
selectBlockRange :: HasHeader block => Chain block -> Point block -> Point block -> Maybe [block] 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
.