{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE NamedFieldPuns      #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators       #-}

module Ouroboros.Network.Protocol.LocalStateQuery.Codec
  ( codecLocalStateQuery
  , codecLocalStateQueryId
  , Some (..)
  ) where

import           Control.Monad.Class.MonadST

import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Read as CBOR
import           Data.ByteString.Lazy (ByteString)
import           Data.Kind (Type)
import           Data.Type.Equality ((:~:) (..))
import           Text.Printf

import           Network.TypedProtocol.Codec.CBOR

import           Ouroboros.Network.Protocol.LocalStateQuery.Type


data Some (f :: k -> Type) where
    Some :: f a -> Some f

codecLocalStateQuery
  :: forall block point query m.
     ( MonadST m
     , ShowQuery query
     )
  => (point -> CBOR.Encoding)
  -> (forall s . CBOR.Decoder s point)
  -> (forall result . query result -> CBOR.Encoding)
  -> (forall s . CBOR.Decoder s (Some query))
  -> (forall result . query result -> result -> CBOR.Encoding)
  -> (forall result . query result -> forall s . CBOR.Decoder s result)
  -> Codec (LocalStateQuery block point query) CBOR.DeserialiseFailure m ByteString
codecLocalStateQuery :: (point -> Encoding)
-> (forall s. Decoder s point)
-> (forall result. query result -> Encoding)
-> (forall s. Decoder s (Some query))
-> (forall result. query result -> result -> Encoding)
-> (forall result. query result -> forall s. Decoder s result)
-> Codec
     (LocalStateQuery block point query) DeserialiseFailure m ByteString
codecLocalStateQuery point -> Encoding
encodePoint  forall s. Decoder s point
decodePoint
                     forall result. query result -> Encoding
encodeQuery  forall s. Decoder s (Some query)
decodeQuery
                     forall result. query result -> result -> Encoding
encodeResult forall result. query result -> forall s. Decoder s result
decodeResult =
    (forall (pr :: PeerRole) (st :: LocalStateQuery block point query)
        (st' :: LocalStateQuery block point query).
 PeerHasAgency pr st
 -> Message (LocalStateQuery block point query) st st' -> Encoding)
-> (forall (pr :: PeerRole)
           (st :: LocalStateQuery block point query) s.
    PeerHasAgency pr st -> Decoder s (SomeMessage st))
-> Codec
     (LocalStateQuery block point query) DeserialiseFailure m ByteString
forall ps (m :: * -> *).
MonadST m =>
(forall (pr :: PeerRole) (st :: ps) (st' :: ps).
 PeerHasAgency pr st -> Message ps st st' -> Encoding)
-> (forall (pr :: PeerRole) (st :: ps) s.
    PeerHasAgency pr st -> Decoder s (SomeMessage st))
-> Codec ps DeserialiseFailure m ByteString
mkCodecCborLazyBS forall (pr :: PeerRole) (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query).
PeerHasAgency pr st
-> Message (LocalStateQuery block point query) st st' -> Encoding
encode forall (pr :: PeerRole) s
       (st :: LocalStateQuery block point query).
PeerHasAgency pr st -> Decoder s (SomeMessage st)
forall (pr :: PeerRole) (st :: LocalStateQuery block point query)
       s.
PeerHasAgency pr st -> Decoder s (SomeMessage st)
decode
  where
    encodeFailure :: AcquireFailure -> CBOR.Encoding
    encodeFailure :: AcquireFailure -> Encoding
encodeFailure AcquireFailure
AcquireFailurePointTooOld     = Word8 -> Encoding
CBOR.encodeWord8 Word8
0
    encodeFailure AcquireFailure
AcquireFailurePointNotOnChain = Word8 -> Encoding
CBOR.encodeWord8 Word8
1

    decodeFailure :: forall s. CBOR.Decoder s AcquireFailure
    decodeFailure :: Decoder s AcquireFailure
decodeFailure = do
      Word8
tag <- Decoder s Word8
forall s. Decoder s Word8
CBOR.decodeWord8
      case Word8
tag of
        Word8
0 -> AcquireFailure -> Decoder s AcquireFailure
forall (m :: * -> *) a. Monad m => a -> m a
return AcquireFailure
AcquireFailurePointTooOld
        Word8
1 -> AcquireFailure -> Decoder s AcquireFailure
forall (m :: * -> *) a. Monad m => a -> m a
return AcquireFailure
AcquireFailurePointNotOnChain
        Word8
_ -> String -> Decoder s AcquireFailure
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s AcquireFailure)
-> String -> Decoder s AcquireFailure
forall a b. (a -> b) -> a -> b
$ String
"decodeFailure: invalid tag " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Word8 -> String
forall a. Show a => a -> String
show Word8
tag

    encode :: forall (pr  :: PeerRole)
                     (st  :: LocalStateQuery block point query)
                     (st' :: LocalStateQuery block point query).
              PeerHasAgency pr st
           -> Message (LocalStateQuery block point query) st st'
           -> CBOR.Encoding
    encode :: PeerHasAgency pr st
-> Message (LocalStateQuery block point query) st st' -> Encoding
encode (ClientAgency ClientHasAgency st
TokIdle) (MsgAcquire (Just pt)) =
        Word -> Encoding
CBOR.encodeListLen Word
2
     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
point
pt

    encode (ClientAgency ClientHasAgency st
TokIdle) (MsgAcquire Nothing) =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
8

    encode (ServerAgency ServerHasAgency st
TokAcquiring) Message (LocalStateQuery block point query) st st'
MsgAcquired =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
1

    encode (ServerAgency ServerHasAgency st
TokAcquiring) (MsgFailure failure) =
        Word -> Encoding
CBOR.encodeListLen Word
2
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
2
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> AcquireFailure -> Encoding
encodeFailure AcquireFailure
failure

    encode (ClientAgency ClientHasAgency st
TokAcquired) (MsgQuery query) =
        Word -> Encoding
CBOR.encodeListLen Word
2
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
3
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> query result -> Encoding
forall result. query result -> Encoding
encodeQuery query result
query

    encode (ServerAgency (TokQuerying _query)) (MsgResult query result) =
        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
<> query result -> result -> Encoding
forall result. query result -> result -> Encoding
encodeResult query result
query result
result

    encode (ClientAgency ClientHasAgency st
TokAcquired) Message (LocalStateQuery block point query) st st'
MsgRelease =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
5

    encode (ClientAgency ClientHasAgency st
TokAcquired) (MsgReAcquire (Just pt)) =
        Word -> Encoding
CBOR.encodeListLen Word
2
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
6
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> point -> Encoding
encodePoint point
point
pt

    encode (ClientAgency ClientHasAgency st
TokAcquired) (MsgReAcquire Nothing) =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
9

    encode (ClientAgency ClientHasAgency st
TokIdle) Message (LocalStateQuery block point query) st st'
MsgDone =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
7

    decode :: forall (pr :: PeerRole) s (st :: LocalStateQuery block point query).
              PeerHasAgency pr st
           -> CBOR.Decoder s (SomeMessage st)
    decode :: PeerHasAgency pr st -> Decoder s (SomeMessage st)
decode PeerHasAgency pr st
stok = do
      Int
len <- Decoder s Int
forall s. Decoder s Int
CBOR.decodeListLen
      Word
key <- Decoder s Word
forall s. Decoder s Word
CBOR.decodeWord
      case (PeerHasAgency pr st
stok, Int
len, Word
key) of
        (ClientAgency ClientHasAgency st
TokIdle, Int
2, Word
0) -> do
          point
pt <- Decoder s point
forall s. Decoder s point
decodePoint
          SomeMessage 'StIdle -> Decoder s (SomeMessage 'StIdle)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
-> SomeMessage 'StIdle
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (Maybe point
-> Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
forall k point (block :: k) (query :: * -> *).
Maybe point
-> Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
MsgAcquire (point -> Maybe point
forall a. a -> Maybe a
Just point
pt)))

        (ClientAgency ClientHasAgency st
TokIdle, Int
1, Word
8) -> do
          SomeMessage 'StIdle -> Decoder s (SomeMessage 'StIdle)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
-> SomeMessage 'StIdle
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (Maybe point
-> Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
forall k point (block :: k) (query :: * -> *).
Maybe point
-> Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
MsgAcquire Maybe point
forall a. Maybe a
Nothing))

        (ServerAgency ServerHasAgency st
TokAcquiring, Int
1, Word
1) ->
          SomeMessage 'StAcquiring -> Decoder s (SomeMessage 'StAcquiring)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message
  (LocalStateQuery block point query) 'StAcquiring 'StAcquired
-> SomeMessage 'StAcquiring
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message
  (LocalStateQuery block point query) 'StAcquiring 'StAcquired
forall k k (block :: k) (point :: k) (query :: * -> *).
Message
  (LocalStateQuery block point query) 'StAcquiring 'StAcquired
MsgAcquired)

        (ServerAgency ServerHasAgency st
TokAcquiring, Int
2, Word
2) -> do
          AcquireFailure
failure <- Decoder s AcquireFailure
forall s. Decoder s AcquireFailure
decodeFailure
          SomeMessage 'StAcquiring -> Decoder s (SomeMessage 'StAcquiring)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (LocalStateQuery block point query) 'StAcquiring 'StIdle
-> SomeMessage 'StAcquiring
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (AcquireFailure
-> Message (LocalStateQuery block point query) 'StAcquiring 'StIdle
forall k k (block :: k) (point :: k) (query :: * -> *).
AcquireFailure
-> Message (LocalStateQuery block point query) 'StAcquiring 'StIdle
MsgFailure AcquireFailure
failure))

        (ClientAgency ClientHasAgency st
TokAcquired, Int
2, Word
3) -> do
          Some query a
query <- Decoder s (Some query)
forall s. Decoder s (Some query)
decodeQuery
          SomeMessage 'StAcquired -> Decoder s (SomeMessage 'StAcquired)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message
  (LocalStateQuery block point query) 'StAcquired ('StQuerying a)
-> SomeMessage 'StAcquired
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (query a
-> Message
     (LocalStateQuery block point query) 'StAcquired ('StQuerying a)
forall k k (query :: * -> *) result (block :: k) (point :: k).
query result
-> Message
     (LocalStateQuery block point query)
     'StAcquired
     ('StQuerying result)
MsgQuery query a
query))

        (ServerAgency (TokQuerying query), Int
2, Word
4) -> do
          result
result <- query result -> forall s. Decoder s result
forall result. query result -> forall s. Decoder s result
decodeResult query result
query
          SomeMessage ('StQuerying result)
-> Decoder s (SomeMessage ('StQuerying result))
forall (m :: * -> *) a. Monad m => a -> m a
return (Message
  (LocalStateQuery block point query)
  ('StQuerying result)
  'StAcquired
-> SomeMessage ('StQuerying result)
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (query result
-> result
-> Message
     (LocalStateQuery block point query)
     ('StQuerying result)
     'StAcquired
forall k k (query :: * -> *) result (block :: k) (point :: k).
query result
-> result
-> Message
     (LocalStateQuery block point query)
     ('StQuerying result)
     'StAcquired
MsgResult query result
query result
result))

        (ClientAgency ClientHasAgency st
TokAcquired, Int
1, Word
5) ->
          SomeMessage 'StAcquired -> Decoder s (SomeMessage 'StAcquired)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (LocalStateQuery block point query) 'StAcquired 'StIdle
-> SomeMessage 'StAcquired
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (LocalStateQuery block point query) 'StAcquired 'StIdle
forall k k (block :: k) (point :: k) (query :: * -> *).
Message (LocalStateQuery block point query) 'StAcquired 'StIdle
MsgRelease)

        (ClientAgency ClientHasAgency st
TokAcquired, Int
2, Word
6) -> do
          point
pt <- Decoder s point
forall s. Decoder s point
decodePoint
          SomeMessage 'StAcquired -> Decoder s (SomeMessage 'StAcquired)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message
  (LocalStateQuery block point query) 'StAcquired 'StAcquiring
-> SomeMessage 'StAcquired
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (Maybe point
-> Message
     (LocalStateQuery block point query) 'StAcquired 'StAcquiring
forall k point (block :: k) (query :: * -> *).
Maybe point
-> Message
     (LocalStateQuery block point query) 'StAcquired 'StAcquiring
MsgReAcquire (point -> Maybe point
forall a. a -> Maybe a
Just point
pt)))

        (ClientAgency ClientHasAgency st
TokAcquired, Int
1, Word
9) -> do
          SomeMessage 'StAcquired -> Decoder s (SomeMessage 'StAcquired)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message
  (LocalStateQuery block point query) 'StAcquired 'StAcquiring
-> SomeMessage 'StAcquired
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (Maybe point
-> Message
     (LocalStateQuery block point query) 'StAcquired 'StAcquiring
forall k point (block :: k) (query :: * -> *).
Maybe point
-> Message
     (LocalStateQuery block point query) 'StAcquired 'StAcquiring
MsgReAcquire Maybe point
forall a. Maybe a
Nothing))

        (ClientAgency ClientHasAgency st
TokIdle, Int
1, Word
7) ->
          SomeMessage 'StIdle -> Decoder s (SomeMessage 'StIdle)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (LocalStateQuery block point query) 'StIdle 'StDone
-> SomeMessage 'StIdle
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (LocalStateQuery block point query) 'StIdle 'StDone
forall k k (block :: k) (point :: k) (query :: * -> *).
Message (LocalStateQuery block point query) 'StIdle 'StDone
MsgDone)

        --
        -- failures per protocol state
        --

        (ClientAgency ClientHasAgency st
TokIdle, Int
_, Word
_) ->
          String -> Decoder s (SomeMessage st)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecLocalStateQuery (%s) unexpected key (%d, %d)" (PeerHasAgency pr st -> String
forall a. Show a => a -> String
show PeerHasAgency pr st
stok) Word
key Int
len)
        (ClientAgency ClientHasAgency st
TokAcquired, Int
_, Word
_) ->
          String -> Decoder s (SomeMessage st)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecLocalStateQuery (%s) unexpected key (%d, %d)" (PeerHasAgency pr st -> String
forall a. Show a => a -> String
show PeerHasAgency pr st
stok) Word
key Int
len)
        (ServerAgency ServerHasAgency st
TokAcquiring, Int
_, Word
_) ->
          String -> Decoder s (SomeMessage st)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecLocalStateQuery (%s) unexpected key (%d, %d)" (PeerHasAgency pr st -> String
forall a. Show a => a -> String
show PeerHasAgency pr st
stok) Word
key Int
len)
        (ServerAgency (TokQuerying _), Int
_, Word
_) ->
          String -> Decoder s (SomeMessage st)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecLocalStateQuery (%s) unexpected key (%d, %d)" (PeerHasAgency pr st -> String
forall a. Show a => a -> String
show PeerHasAgency pr st
stok) Word
key Int
len)


-- | An identity 'Codec' for the 'LocalStateQuery' protocol. It does not do
-- any serialisation. It keeps the typed messages, wrapped in 'AnyMessage'.
--
codecLocalStateQueryId
  :: forall block point (query :: Type -> Type) m.
     Monad m
  => (forall result1 result2.
          query result1
       -> query result2
       -> Maybe (result1 :~: result2)
     )
  -> Codec (LocalStateQuery block point query)
           CodecFailure m
           (AnyMessage (LocalStateQuery block point query))
codecLocalStateQueryId :: (forall result1 result2.
 query result1 -> query result2 -> Maybe (result1 :~: result2))
-> Codec
     (LocalStateQuery block point query)
     CodecFailure
     m
     (AnyMessage (LocalStateQuery block point query))
codecLocalStateQueryId forall result1 result2.
query result1 -> query result2 -> Maybe (result1 :~: result2)
eqQuery =
  (forall (pr :: PeerRole) (st :: LocalStateQuery block point query)
        (st' :: LocalStateQuery block point query).
 PeerHasAgency pr st
 -> Message (LocalStateQuery block point query) st st'
 -> AnyMessage (LocalStateQuery block point query))
-> (forall (pr :: PeerRole)
           (st :: LocalStateQuery block point query).
    PeerHasAgency pr st
    -> m (DecodeStep
            (AnyMessage (LocalStateQuery block point query))
            CodecFailure
            m
            (SomeMessage st)))
-> Codec
     (LocalStateQuery block point query)
     CodecFailure
     m
     (AnyMessage (LocalStateQuery block point query))
forall ps failure (m :: * -> *) bytes.
(forall (pr :: PeerRole) (st :: ps) (st' :: ps).
 PeerHasAgency pr st -> Message ps st st' -> bytes)
-> (forall (pr :: PeerRole) (st :: ps).
    PeerHasAgency pr st
    -> m (DecodeStep bytes failure m (SomeMessage st)))
-> Codec ps failure m bytes
Codec forall (pr :: PeerRole) (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query).
PeerHasAgency pr st
-> Message (LocalStateQuery block point query) st st'
-> AnyMessage (LocalStateQuery block point query)
encode forall (pr :: PeerRole) (st :: LocalStateQuery block point query).
PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
decode
 where
  encode :: forall (pr :: PeerRole) st st'.
            PeerHasAgency pr st
         -> Message (LocalStateQuery block point query) st st'
         -> AnyMessage (LocalStateQuery block point query)
  encode :: PeerHasAgency pr st
-> Message (LocalStateQuery block point query) st st'
-> AnyMessage (LocalStateQuery block point query)
encode PeerHasAgency pr st
_ = Message (LocalStateQuery block point query) st st'
-> AnyMessage (LocalStateQuery block point query)
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> AnyMessage ps
AnyMessage

  decode :: forall (pr :: PeerRole) (st :: LocalStateQuery block point query).
            PeerHasAgency pr st
         -> m (DecodeStep (AnyMessage (LocalStateQuery block point query))
                          CodecFailure m (SomeMessage st))
  decode :: PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
decode PeerHasAgency pr st
stok = DecodeStep
  (AnyMessage (LocalStateQuery block point query))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (LocalStateQuery block point query))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (LocalStateQuery block point query))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (LocalStateQuery block point query))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ (Maybe (AnyMessage (LocalStateQuery block point query))
 -> m (DecodeStep
         (AnyMessage (LocalStateQuery block point query))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (LocalStateQuery block point query))
     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 (LocalStateQuery block point query))
  -> m (DecodeStep
          (AnyMessage (LocalStateQuery block point query))
          CodecFailure
          m
          (SomeMessage st)))
 -> DecodeStep
      (AnyMessage (LocalStateQuery block point query))
      CodecFailure
      m
      (SomeMessage st))
-> (Maybe (AnyMessage (LocalStateQuery block point query))
    -> m (DecodeStep
            (AnyMessage (LocalStateQuery block point query))
            CodecFailure
            m
            (SomeMessage st)))
-> DecodeStep
     (AnyMessage (LocalStateQuery block point query))
     CodecFailure
     m
     (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ \Maybe (AnyMessage (LocalStateQuery block point query))
bytes -> case (PeerHasAgency pr st
stok, Maybe (AnyMessage (LocalStateQuery block point query))
bytes) of
    (ClientAgency ClientHasAgency st
TokIdle,         Just (AnyMessage msg@(MsgAcquire{})))   -> Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StIdle))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message (LocalStateQuery block point query) 'StIdle 'StAcquiring
msg
    (ClientAgency ClientHasAgency st
TokIdle,         Just (AnyMessage msg@(MsgDone{})))      -> Message (LocalStateQuery block point query) 'StIdle 'StDone
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StIdle))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message (LocalStateQuery block point query) 'StIdle 'StDone
msg
    (ClientAgency ClientHasAgency st
TokAcquired,     Just (AnyMessage msg@(MsgQuery{})))     -> Message
  (LocalStateQuery block point query)
  'StAcquired
  ('StQuerying result)
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StAcquired))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message
  (LocalStateQuery block point query)
  'StAcquired
  ('StQuerying result)
msg
    (ClientAgency ClientHasAgency st
TokAcquired,     Just (AnyMessage msg@(MsgReAcquire{}))) -> Message
  (LocalStateQuery block point query) 'StAcquired 'StAcquiring
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StAcquired))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message
  (LocalStateQuery block point query) 'StAcquired 'StAcquiring
msg
    (ClientAgency ClientHasAgency st
TokAcquired,     Just (AnyMessage msg@(MsgRelease{})))   -> Message (LocalStateQuery block point query) 'StAcquired 'StIdle
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StAcquired))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message (LocalStateQuery block point query) 'StAcquired 'StIdle
msg
    (ServerAgency ServerHasAgency st
TokAcquiring,    Just (AnyMessage msg@(MsgAcquired{})))  -> Message
  (LocalStateQuery block point query) 'StAcquiring 'StAcquired
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StAcquiring))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message
  (LocalStateQuery block point query) 'StAcquiring 'StAcquired
msg
    (ServerAgency ServerHasAgency st
TokAcquiring,    Just (AnyMessage msg@(MsgFailure{})))   -> Message (LocalStateQuery block point query) 'StAcquiring 'StIdle
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage 'StAcquiring))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message (LocalStateQuery block point query) 'StAcquiring 'StIdle
msg
    (ServerAgency (TokQuerying q), Just (AnyMessage msg@(MsgResult query _)))
       | Just result :~: result
Refl <- query result -> query result -> Maybe (result :~: result)
forall result1 result2.
query result1 -> query result2 -> Maybe (result1 :~: result2)
eqQuery query result
q query result
query
       -> Message
  (LocalStateQuery block point query)
  ('StQuerying result)
  'StAcquired
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage ('StQuerying result)))
forall (st :: LocalStateQuery block point query)
       (st' :: LocalStateQuery block point query) bytes failure.
Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
Message
  (LocalStateQuery block point query)
  ('StQuerying result)
  'StAcquired
msg
    (PeerHasAgency pr st
_, Maybe (AnyMessage (LocalStateQuery block point query))
Nothing) -> DecodeStep
  (AnyMessage (LocalStateQuery block point query))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
forall (m :: * -> *) a. Monad m => a -> m a
return (CodecFailure
-> DecodeStep
     (AnyMessage (LocalStateQuery block point query))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail CodecFailure
CodecFailureOutOfInput)
    (PeerHasAgency pr st
_, Maybe (AnyMessage (LocalStateQuery block point query))
_)       -> DecodeStep
  (AnyMessage (LocalStateQuery block point query))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (LocalStateQuery block point query))
        CodecFailure
        m
        (SomeMessage st))
forall (m :: * -> *) a. Monad m => a -> m a
return (CodecFailure
-> DecodeStep
     (AnyMessage (LocalStateQuery block point query))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail (String -> CodecFailure
CodecFailure String
failmsg))

  res :: Message (LocalStateQuery block point query) st st'
      -> m (DecodeStep bytes failure m (SomeMessage st))
  res :: Message (LocalStateQuery block point query) st st'
-> m (DecodeStep bytes failure m (SomeMessage st))
res Message (LocalStateQuery block point query) st st'
msg = DecodeStep bytes failure m (SomeMessage st)
-> m (DecodeStep bytes failure m (SomeMessage st))
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeMessage st
-> Maybe bytes -> DecodeStep bytes failure m (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (LocalStateQuery block point query) st st'
-> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (LocalStateQuery block point query) st st'
msg) Maybe bytes
forall a. Maybe a
Nothing)
  failmsg :: String
failmsg = String
"codecLocalStateQueryId: no matching message"