{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE NamedFieldPuns      #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}

module Ouroboros.Network.Protocol.BlockFetch.Codec
  ( codecBlockFetch
  , codecBlockFetchId
  , byteLimitsBlockFetch
  , timeLimitsBlockFetch
  ) where

import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadTime.SI

import Data.ByteString.Lazy qualified as LBS

import Codec.CBOR.Decoding qualified as CBOR
import Codec.CBOR.Encoding qualified as CBOR
import Codec.CBOR.Read qualified as CBOR
import Text.Printf

import Network.TypedProtocol.Codec.CBOR

import Ouroboros.Network.Protocol.BlockFetch.Type
import Ouroboros.Network.Protocol.Limits

-- | Byte Limit.
byteLimitsBlockFetch :: forall bytes block point.
                        (bytes -> Word)
                     -> ProtocolSizeLimits (BlockFetch block point) bytes
byteLimitsBlockFetch :: forall {k} {k1} bytes (block :: k) (point :: k1).
(bytes -> Word)
-> ProtocolSizeLimits (BlockFetch block point) bytes
byteLimitsBlockFetch = (forall (st :: BlockFetch block point).
 ActiveState st =>
 StateToken st -> Word)
-> (bytes -> Word)
-> ProtocolSizeLimits (BlockFetch block point) bytes
forall ps bytes.
(forall (st :: ps). ActiveState st => StateToken st -> Word)
-> (bytes -> Word) -> ProtocolSizeLimits ps bytes
ProtocolSizeLimits StateToken st -> Word
forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Word
stateToLimit
  where
    stateToLimit :: forall (st :: BlockFetch block point).
                    ActiveState st => StateToken st -> Word
    stateToLimit :: forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Word
stateToLimit StateToken st
SingBlockFetch st
SingBFIdle      = Word
smallByteLimit
    stateToLimit StateToken st
SingBlockFetch st
SingBFBusy      = Word
smallByteLimit
    stateToLimit StateToken st
SingBlockFetch st
SingBFStreaming = Word
largeByteLimit
    stateToLimit a :: StateToken st
a@StateToken st
SingBlockFetch st
SingBFDone    = StateToken 'BFDone -> forall a. a
forall ps (st :: ps).
(StateAgency st ~ 'NobodyAgency, ActiveState st) =>
StateToken st -> forall a. a
notActiveState StateToken st
StateToken 'BFDone
a

-- | Time Limits
--
-- `TokIdle' No timeout
-- `TokBusy` `longWait` timeout
-- `TokStreaming` `longWait` timeout
timeLimitsBlockFetch :: forall block point.
                        ProtocolTimeLimits (BlockFetch block point)
timeLimitsBlockFetch :: forall {k} {k1} (block :: k) (point :: k1).
ProtocolTimeLimits (BlockFetch block point)
timeLimitsBlockFetch = (forall (st :: BlockFetch block point).
 ActiveState st =>
 StateToken st -> Maybe DiffTime)
-> ProtocolTimeLimits (BlockFetch block point)
forall ps.
(forall (st :: ps).
 ActiveState st =>
 StateToken st -> Maybe DiffTime)
-> ProtocolTimeLimits ps
ProtocolTimeLimits StateToken st -> Maybe DiffTime
forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Maybe DiffTime
stateToLimit
  where
    stateToLimit :: forall (st :: BlockFetch block point).
                    ActiveState st => StateToken st -> Maybe DiffTime
    stateToLimit :: forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Maybe DiffTime
stateToLimit StateToken st
SingBlockFetch st
SingBFIdle      = Maybe DiffTime
waitForever
    stateToLimit StateToken st
SingBlockFetch st
SingBFBusy      = Maybe DiffTime
longWait
    stateToLimit StateToken st
SingBlockFetch st
SingBFStreaming = Maybe DiffTime
longWait
    stateToLimit a :: StateToken st
a@StateToken st
SingBlockFetch st
SingBFDone    = StateToken 'BFDone -> forall a. a
forall ps (st :: ps).
(StateAgency st ~ 'NobodyAgency, ActiveState st) =>
StateToken st -> forall a. a
notActiveState StateToken st
StateToken 'BFDone
a

-- | Codec for chain sync that encodes/decodes blocks
--
-- NOTE: See 'wrapCBORinCBOR' and 'unwrapCBORinCBOR' if you want to use this
-- with a block type that has annotations.
codecBlockFetch
  :: forall block point m.
     MonadST m
  => (block            -> CBOR.Encoding)
  -> (forall s. CBOR.Decoder s block)
  -> (point -> CBOR.Encoding)
  -> (forall s. CBOR.Decoder s point)
  -> Codec (BlockFetch block point) CBOR.DeserialiseFailure m LBS.ByteString
codecBlockFetch :: forall block point (m :: * -> *).
MonadST m =>
(block -> Encoding)
-> (forall s. Decoder s block)
-> (point -> Encoding)
-> (forall s. Decoder s point)
-> Codec (BlockFetch block point) DeserialiseFailure m ByteString
codecBlockFetch block -> Encoding
encodeBlock forall s. Decoder s block
decodeBlock
                point -> Encoding
encodePoint forall s. Decoder s point
decodePoint =
    (forall (st :: BlockFetch block point)
        (st' :: BlockFetch block point).
 (StateTokenI st, ActiveState st) =>
 Message (BlockFetch block point) st st' -> Encoding)
-> (forall (st :: BlockFetch block point) s.
    ActiveState st =>
    StateToken st -> Decoder s (SomeMessage st))
-> Codec (BlockFetch block point) DeserialiseFailure m ByteString
forall ps (m :: * -> *).
MonadST m =>
(forall (st :: ps) (st' :: ps).
 (StateTokenI st, ActiveState st) =>
 Message ps st st' -> Encoding)
-> (forall (st :: ps) s.
    ActiveState st =>
    StateToken st -> Decoder s (SomeMessage st))
-> Codec ps DeserialiseFailure m ByteString
mkCodecCborLazyBS Message (BlockFetch block point) st st' -> Encoding
forall (st :: BlockFetch block point)
       (st' :: BlockFetch block point).
Message (BlockFetch block point) st st' -> Encoding
forall (st :: BlockFetch block point)
       (st' :: BlockFetch block point).
(StateTokenI st, ActiveState st) =>
Message (BlockFetch block point) st st' -> Encoding
encode StateToken st -> Decoder s (SomeMessage st)
forall s (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Decoder s (SomeMessage st)
forall (st :: BlockFetch block point) s.
ActiveState st =>
StateToken st -> Decoder s (SomeMessage st)
decode
 where
  encode :: forall st st'.
            Message (BlockFetch block point) st st'
         -> CBOR.Encoding
  encode :: forall (st :: BlockFetch block point)
       (st' :: BlockFetch block point).
Message (BlockFetch block point) st st' -> Encoding
encode (MsgRequestRange (ChainRange point1
from point1
to)) =
    Word -> Encoding
CBOR.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> point -> Encoding
encodePoint point
point1
from
                                              Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> point -> Encoding
encodePoint point
point1
to
  encode Message (BlockFetch block point) st st'
R:MessageBlockFetchfromto (*) (*) block point st st'
MsgClientDone =
    Word -> Encoding
CBOR.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
1
  encode Message (BlockFetch block point) st st'
R:MessageBlockFetchfromto (*) (*) block point st st'
MsgStartBatch =
    Word -> Encoding
CBOR.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
2
  encode Message (BlockFetch block point) st st'
R:MessageBlockFetchfromto (*) (*) block point st st'
MsgNoBlocks =
    Word -> Encoding
CBOR.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
3
  encode (MsgBlock block1
block) =
    Word -> Encoding
CBOR.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> block -> Encoding
encodeBlock block
block1
block
  encode Message (BlockFetch block point) st st'
R:MessageBlockFetchfromto (*) (*) block point st st'
MsgBatchDone =
    Word -> Encoding
CBOR.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
5

  decode :: forall s (st :: BlockFetch block point).
            ActiveState st
         => StateToken st
         -> CBOR.Decoder s (SomeMessage st)
  decode :: forall s (st :: BlockFetch block point).
ActiveState st =>
StateToken st -> Decoder s (SomeMessage st)
decode StateToken st
stok = do
    len <- Decoder s Int
forall s. Decoder s Int
CBOR.decodeListLen
    key <- CBOR.decodeWord
    case (stok, key, len) of
      (SingBlockFetch st
SingBFIdle, Word
0, Int
3) -> do
        from <- Decoder s point
forall s. Decoder s point
decodePoint
        to   <- decodePoint
        return $ SomeMessage $ MsgRequestRange (ChainRange from to)
      (SingBlockFetch st
SingBFIdle, Word
1, Int
1) -> SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st -> Decoder s (SomeMessage st))
-> SomeMessage st -> Decoder s (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ Message (BlockFetch block point) st 'BFDone -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st 'BFDone
Message (BlockFetch block point) 'BFIdle 'BFDone
forall {k} {k1} (block :: k) (point :: k1).
Message (BlockFetch block point) 'BFIdle 'BFDone
MsgClientDone
      (SingBlockFetch st
SingBFBusy, Word
2, Int
1) -> SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st -> Decoder s (SomeMessage st))
-> SomeMessage st -> Decoder s (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ Message (BlockFetch block point) st 'BFStreaming -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st 'BFStreaming
Message (BlockFetch block point) 'BFBusy 'BFStreaming
forall {k} {k1} (block :: k) (point :: k1).
Message (BlockFetch block point) 'BFBusy 'BFStreaming
MsgStartBatch
      (SingBlockFetch st
SingBFBusy, Word
3, Int
1) -> SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st -> Decoder s (SomeMessage st))
-> SomeMessage st -> Decoder s (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ Message (BlockFetch block point) st 'BFIdle -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st 'BFIdle
Message (BlockFetch block point) 'BFBusy 'BFIdle
forall {k} {k1} (block :: k) (point :: k1).
Message (BlockFetch block point) 'BFBusy 'BFIdle
MsgNoBlocks
      (SingBlockFetch st
SingBFStreaming, Word
4, Int
2) -> Message (BlockFetch block point) st 'BFStreaming -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage (Message (BlockFetch block point) st 'BFStreaming
 -> SomeMessage st)
-> (block -> Message (BlockFetch block point) st 'BFStreaming)
-> block
-> SomeMessage st
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> Message (BlockFetch block point) st 'BFStreaming
block -> Message (BlockFetch block point) 'BFStreaming 'BFStreaming
forall {k1} block1 (point :: k1).
block1
-> Message (BlockFetch block1 point) 'BFStreaming 'BFStreaming
MsgBlock
                                                 (block -> SomeMessage st)
-> Decoder s block -> Decoder s (SomeMessage st)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s block
forall s. Decoder s block
decodeBlock
      (SingBlockFetch st
SingBFStreaming, Word
5, Int
1) -> SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st -> Decoder s (SomeMessage st))
-> SomeMessage st -> Decoder s (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ Message (BlockFetch block point) st 'BFIdle -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st 'BFIdle
Message (BlockFetch block point) 'BFStreaming 'BFIdle
forall {k} {k1} (block :: k) (point :: k1).
Message (BlockFetch block point) 'BFStreaming 'BFIdle
MsgBatchDone

      --
      -- failures per protocol state
      --

      (SingBlockFetch st
SingBFIdle, Word
_, Int
_) ->
        String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecBlockFetch (%s, %s) unexpected key (%d, %d)"
                     (ActiveAgency' 'BFIdle 'ClientAgency -> String
forall a. Show a => a -> String
show (ActiveAgency' st 'ClientAgency
ActiveAgency' st (StateAgency st)
forall {ps} (st :: ps) (agency :: Agency).
IsActiveState st agency =>
ActiveAgency' st agency
activeAgency :: ActiveAgency st)) (SingBlockFetch 'BFIdle -> String
forall a. Show a => a -> String
show StateToken st
SingBlockFetch 'BFIdle
stok) Word
key Int
len)
      (SingBlockFetch st
SingBFStreaming, Word
_ , Int
_) ->
        String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecBlockFetch (%s, %s) unexpected key (%d, %d)"
                     (ActiveAgency' 'BFStreaming 'ServerAgency -> String
forall a. Show a => a -> String
show (ActiveAgency' st 'ServerAgency
ActiveAgency' st (StateAgency st)
forall {ps} (st :: ps) (agency :: Agency).
IsActiveState st agency =>
ActiveAgency' st agency
activeAgency :: ActiveAgency st)) (SingBlockFetch 'BFStreaming -> String
forall a. Show a => a -> String
show StateToken st
SingBlockFetch 'BFStreaming
stok) Word
key Int
len)
      (SingBlockFetch st
SingBFBusy, Word
_, Int
_) ->
        String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecBlockFetch (%s, %s) unexpected key (%d, %d)"
                     (ActiveAgency' 'BFBusy 'ServerAgency -> String
forall a. Show a => a -> String
show (ActiveAgency' st 'ServerAgency
ActiveAgency' st (StateAgency st)
forall {ps} (st :: ps) (agency :: Agency).
IsActiveState st agency =>
ActiveAgency' st agency
activeAgency :: ActiveAgency st)) (SingBlockFetch 'BFBusy -> String
forall a. Show a => a -> String
show StateToken st
SingBlockFetch 'BFBusy
stok) Word
key Int
len)

      (SingBlockFetch st
SingBFDone, Word
_, Int
_) -> StateToken 'BFDone -> forall a. a
forall ps (st :: ps).
(StateAgency st ~ 'NobodyAgency, ActiveState st) =>
StateToken st -> forall a. a
notActiveState StateToken st
StateToken 'BFDone
stok


codecBlockFetchId
  :: forall block point m.
     Monad m
  => Codec (BlockFetch block point) CodecFailure m
           (AnyMessage (BlockFetch block point))
codecBlockFetchId :: forall {k} {k1} (block :: k) (point :: k1) (m :: * -> *).
Monad m =>
Codec
  (BlockFetch block point)
  CodecFailure
  m
  (AnyMessage (BlockFetch block point))
codecBlockFetchId = (forall (st :: BlockFetch block point)
        (st' :: BlockFetch block point).
 (StateTokenI st, ActiveState st) =>
 Message (BlockFetch block point) st st'
 -> AnyMessage (BlockFetch block point))
-> (forall (st :: BlockFetch block point).
    ActiveState st =>
    StateToken st
    -> m (DecodeStep
            (AnyMessage (BlockFetch block point))
            CodecFailure
            m
            (SomeMessage st)))
-> Codec
     (BlockFetch block point)
     CodecFailure
     m
     (AnyMessage (BlockFetch block point))
forall ps failure (m :: * -> *) bytes.
(forall (st :: ps) (st' :: ps).
 (StateTokenI st, ActiveState st) =>
 Message ps st st' -> bytes)
-> (forall (st :: ps).
    ActiveState st =>
    StateToken st -> m (DecodeStep bytes failure m (SomeMessage st)))
-> Codec ps failure m bytes
Codec Message (BlockFetch block point) st st'
-> AnyMessage (BlockFetch block point)
forall (st :: BlockFetch block point)
       (st' :: BlockFetch block point).
(StateTokenI st, ActiveState st) =>
Message (BlockFetch block point) st st'
-> AnyMessage (BlockFetch block point)
encode StateToken st
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
decode
 where
  encode :: forall st st'.
            StateTokenI st
         => ActiveState st
         => Message (BlockFetch block point) st st'
         -> AnyMessage (BlockFetch block point)
  encode :: forall (st :: BlockFetch block point)
       (st' :: BlockFetch block point).
(StateTokenI st, ActiveState st) =>
Message (BlockFetch block point) st st'
-> AnyMessage (BlockFetch block point)
encode = Message (BlockFetch block point) st st'
-> AnyMessage (BlockFetch block point)
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, ActiveState st) =>
Message ps st st' -> AnyMessage ps
AnyMessage

  decode :: forall (st :: BlockFetch block point).
            ActiveState st
         => StateToken st
         -> m (DecodeStep (AnyMessage (BlockFetch block point))
                          CodecFailure m (SomeMessage st))
  decode :: forall (st :: BlockFetch block point).
ActiveState st =>
StateToken st
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
decode StateToken st
stok = DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (BlockFetch block point))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (BlockFetch block point))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ (Maybe (AnyMessage (BlockFetch block point))
 -> m (DecodeStep
         (AnyMessage (BlockFetch block point))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
(Maybe bytes -> m (DecodeStep bytes failure m a))
-> DecodeStep bytes failure m a
DecodePartial ((Maybe (AnyMessage (BlockFetch block point))
  -> m (DecodeStep
          (AnyMessage (BlockFetch block point))
          CodecFailure
          m
          (SomeMessage st)))
 -> DecodeStep
      (AnyMessage (BlockFetch block point))
      CodecFailure
      m
      (SomeMessage st))
-> (Maybe (AnyMessage (BlockFetch block point))
    -> m (DecodeStep
            (AnyMessage (BlockFetch block point))
            CodecFailure
            m
            (SomeMessage st)))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ \Maybe (AnyMessage (BlockFetch block point))
bytes ->
    case (StateToken st
SingBlockFetch st
stok, Maybe (AnyMessage (BlockFetch block point))
bytes) of
      (SingBlockFetch st
_, Maybe (AnyMessage (BlockFetch block point))
Nothing) -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (BlockFetch block point))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (BlockFetch block point))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ CodecFailure
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail CodecFailure
CodecFailureOutOfInput
      (SingBlockFetch st
SingBFIdle,      Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgRequestRange {}))) -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFIdle,      Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgClientDone {})))   -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFBusy,      Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgStartBatch {})))   -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFBusy,      Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgNoBlocks {})))     -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFStreaming, Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgBlock {})))        -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFStreaming, Just (AnyMessage msg :: Message (BlockFetch block point) st st'
msg@(MsgBatchDone {})))    -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe (AnyMessage (BlockFetch block point))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (BlockFetch block point) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
(StateTokenI st, StateTokenI st', ActiveState st) =>
Message ps st st' -> SomeMessage st
SomeMessage Message (BlockFetch block point) st st'
Message (BlockFetch block point) st st'
msg) Maybe (AnyMessage (BlockFetch block point))
forall a. Maybe a
Nothing)
      (SingBlockFetch st
SingBFDone,      Maybe (AnyMessage (BlockFetch block point))
_)                                          -> StateToken 'BFDone -> forall a. a
forall ps (st :: ps).
(StateAgency st ~ 'NobodyAgency, ActiveState st) =>
StateToken st -> forall a. a
notActiveState StateToken st
StateToken 'BFDone
stok
      (SingBlockFetch st
_, Maybe (AnyMessage (BlockFetch block point))
_) -> DecodeStep
  (AnyMessage (BlockFetch block point))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (BlockFetch block point))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (BlockFetch block point))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (BlockFetch block point))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ CodecFailure
-> DecodeStep
     (AnyMessage (BlockFetch block point))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail (String -> CodecFailure
CodecFailure String
"codecBlockFetchId: no matching message")