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

module Ouroboros.Network.Protocol.TxSubmission2.Codec
  ( codecTxSubmission2
  , codecTxSubmission2Id
  , encodeTxSubmission2
  , decodeTxSubmission2
  , byteLimitsTxSubmission2
  , timeLimitsTxSubmission2
  ) where

import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadTime.SI
import Data.List.NonEmpty qualified as NonEmpty

import Codec.CBOR.Decoding qualified as CBOR
import Codec.CBOR.Encoding qualified as CBOR
import Codec.CBOR.Read qualified as CBOR
import Data.ByteString.Lazy (ByteString)
import Text.Printf

import Network.TypedProtocol.Codec.CBOR

import Ouroboros.Network.Protocol.Limits
import Ouroboros.Network.Protocol.TxSubmission2.Type

-- | Byte Limits.
byteLimitsTxSubmission2 :: forall bytes txid tx.
                           (bytes -> Word)
                        -> ProtocolSizeLimits (TxSubmission2 txid tx) bytes
byteLimitsTxSubmission2 :: forall {k} {k1} bytes (txid :: k) (tx :: k1).
(bytes -> Word) -> ProtocolSizeLimits (TxSubmission2 txid tx) bytes
byteLimitsTxSubmission2 = (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
 PeerHasAgency pr st -> Word)
-> (bytes -> Word)
-> ProtocolSizeLimits (TxSubmission2 txid tx) bytes
forall ps bytes.
(forall (pr :: PeerRole) (st :: ps). PeerHasAgency pr st -> Word)
-> (bytes -> Word) -> ProtocolSizeLimits ps bytes
ProtocolSizeLimits PeerHasAgency pr st -> Word
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Word
stateToLimit
  where
    stateToLimit :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
                    PeerHasAgency pr st -> Word
    stateToLimit :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Word
stateToLimit (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokInit)                   = Word
smallByteLimit
    stateToLimit (ClientAgency (TokTxIds TokBlockingStyle b
TokBlocking))    = Word
largeByteLimit
    stateToLimit (ClientAgency (TokTxIds TokBlockingStyle b
TokNonBlocking)) = Word
largeByteLimit
    stateToLimit (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokTxs)                    = Word
largeByteLimit
    stateToLimit (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st k k1 txid tx st
TokIdle)                   = Word
smallByteLimit


-- | Time Limits.
--
-- `TokTxIds TokBlocking` No timeout
-- `TokTxIds TokNonBlocking` `shortWait` timeout
-- `TokTxs` `shortWait` timeout
-- `TokIdle` `shortWait` timeout
timeLimitsTxSubmission2 :: forall txid tx. ProtocolTimeLimits (TxSubmission2 txid tx)
timeLimitsTxSubmission2 :: forall {k} {k1} (txid :: k) (tx :: k1).
ProtocolTimeLimits (TxSubmission2 txid tx)
timeLimitsTxSubmission2 = (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
 PeerHasAgency pr st -> Maybe DiffTime)
-> ProtocolTimeLimits (TxSubmission2 txid tx)
forall ps.
(forall (pr :: PeerRole) (st :: ps).
 PeerHasAgency pr st -> Maybe DiffTime)
-> ProtocolTimeLimits ps
ProtocolTimeLimits PeerHasAgency pr st -> Maybe DiffTime
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Maybe DiffTime
stateToLimit
  where
    stateToLimit :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
                    PeerHasAgency pr st -> Maybe DiffTime
    stateToLimit :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Maybe DiffTime
stateToLimit (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokInit)                   = Maybe DiffTime
waitForever
    stateToLimit (ClientAgency (TokTxIds TokBlockingStyle b
TokBlocking))    = Maybe DiffTime
waitForever
    stateToLimit (ClientAgency (TokTxIds TokBlockingStyle b
TokNonBlocking)) = Maybe DiffTime
shortWait
    stateToLimit (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokTxs)                    = Maybe DiffTime
shortWait
    stateToLimit (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st k k1 txid tx st
TokIdle)                   = Maybe DiffTime
waitForever


codecTxSubmission2
  :: forall txid tx m.
     MonadST m
  => (txid -> CBOR.Encoding)
  -> (forall s . CBOR.Decoder s txid)
  -> (tx -> CBOR.Encoding)
  -> (forall s . CBOR.Decoder s tx)
  -> Codec (TxSubmission2 txid tx) CBOR.DeserialiseFailure m ByteString
codecTxSubmission2 :: forall txid tx (m :: * -> *).
MonadST m =>
(txid -> Encoding)
-> (forall s. Decoder s txid)
-> (tx -> Encoding)
-> (forall s. Decoder s tx)
-> Codec (TxSubmission2 txid tx) DeserialiseFailure m ByteString
codecTxSubmission2 txid -> Encoding
encodeTxId forall s. Decoder s txid
decodeTxId
                   tx -> Encoding
encodeTx   forall s. Decoder s tx
decodeTx =
    (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
        (st' :: TxSubmission2 txid tx).
 PeerHasAgency pr st
 -> Message (TxSubmission2 txid tx) st st' -> Encoding)
-> (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx) s.
    PeerHasAgency pr st -> Decoder s (SomeMessage st))
-> Codec (TxSubmission2 txid tx) 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
      ((txid -> Encoding)
-> (tx -> Encoding)
-> forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
          (st' :: TxSubmission2 txid tx).
   PeerHasAgency pr st
   -> Message (TxSubmission2 txid tx) st st' -> Encoding
forall txid tx.
(txid -> Encoding)
-> (tx -> Encoding)
-> forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
          (st' :: TxSubmission2 txid tx).
   PeerHasAgency pr st
   -> Message (TxSubmission2 txid tx) st st' -> Encoding
encodeTxSubmission2 txid -> Encoding
encodeTxId tx -> Encoding
encodeTx)
      PeerHasAgency pr st -> Decoder s (SomeMessage st)
PeerHasAgency pr st -> forall s. Decoder s (SomeMessage st)
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> forall s. Decoder s (SomeMessage st)
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx) s.
PeerHasAgency pr st -> Decoder s (SomeMessage st)
decode
  where
    decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
              PeerHasAgency pr st
           -> forall s. CBOR.Decoder s (SomeMessage st)
    decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> forall s. Decoder s (SomeMessage st)
decode PeerHasAgency pr st
stok = do
      len <- Decoder s Int
forall s. Decoder s Int
CBOR.decodeListLen
      key <- CBOR.decodeWord
      decodeTxSubmission2 decodeTxId decodeTx stok len key

encodeTxSubmission2
    :: forall txid tx.
       (txid -> CBOR.Encoding)
    -> (tx -> CBOR.Encoding)
    -> (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx) (st' :: TxSubmission2 txid tx).
               PeerHasAgency pr st
            -> Message (TxSubmission2 txid tx) st st'
            -> CBOR.Encoding)
encodeTxSubmission2 :: forall txid tx.
(txid -> Encoding)
-> (tx -> Encoding)
-> forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
          (st' :: TxSubmission2 txid tx).
   PeerHasAgency pr st
   -> Message (TxSubmission2 txid tx) st st' -> Encoding
encodeTxSubmission2 txid -> Encoding
encodeTxId tx -> Encoding
encodeTx = PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st' -> Encoding
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st' -> Encoding
encode
  where
    encode :: forall (pr :: PeerRole) st st'.
              PeerHasAgency pr st
           -> Message (TxSubmission2 txid tx) st st'
           -> CBOR.Encoding
    encode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st' -> Encoding
encode (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokInit)  Message (TxSubmission2 txid tx) st st'
R:MessageTxSubmission2fromto (*) (*) txid tx st st'
MsgInit =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
6
    encode (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st (*) (*) txid tx st
TokIdle) (MsgRequestTxIds
                                     TokBlockingStyle blocking
blocking
                                     (NumTxIdsToAck Word16
ackNo)
                                     (NumTxIdsToReq Word16
reqNo)) =
        Word -> Encoding
CBOR.encodeListLen Word
4
     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
<> Bool -> Encoding
CBOR.encodeBool (case TokBlockingStyle blocking
blocking of
                           TokBlockingStyle blocking
TokBlocking    -> Bool
True
                           TokBlockingStyle blocking
TokNonBlocking -> Bool
False)
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word16 -> Encoding
CBOR.encodeWord16 Word16
ackNo
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word16 -> Encoding
CBOR.encodeWord16 Word16
reqNo

    encode (ClientAgency (TokTxIds TokBlockingStyle b
_)) (MsgReplyTxIds BlockingReplyList blocking (txid1, TxSizeInBytes)
txids) =
        Word -> Encoding
CBOR.encodeListLen Word
2
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
CBOR.encodeListLenIndef
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((txid, TxSizeInBytes) -> Encoding -> Encoding)
-> Encoding -> [(txid, TxSizeInBytes)] -> Encoding
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(txid
txid, SizeInBytes Word32
sz) Encoding
r ->
                                Word -> Encoding
CBOR.encodeListLen Word
2
                             Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> txid -> Encoding
encodeTxId txid
txid
                             Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word32 -> Encoding
CBOR.encodeWord32 Word32
sz
                             Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
r)
              Encoding
CBOR.encodeBreak
              [(txid, TxSizeInBytes)]
txids'
      where
        txids' :: [(txid, SizeInBytes)]
        txids' :: [(txid, TxSizeInBytes)]
txids' = case BlockingReplyList blocking (txid1, TxSizeInBytes)
txids of
                   BlockingReply    NonEmpty (txid1, TxSizeInBytes)
xs -> NonEmpty (txid, TxSizeInBytes) -> [(txid, TxSizeInBytes)]
forall a. NonEmpty a -> [a]
NonEmpty.toList NonEmpty (txid, TxSizeInBytes)
NonEmpty (txid1, TxSizeInBytes)
xs
                   NonBlockingReply [(txid1, TxSizeInBytes)]
xs -> [(txid, TxSizeInBytes)]
[(txid1, TxSizeInBytes)]
xs

    encode (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st (*) (*) txid tx st
TokIdle) (MsgRequestTxs [txid1]
txids) =
        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
<> Encoding
CBOR.encodeListLenIndef
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (txid -> Encoding -> Encoding) -> Encoding -> [txid] -> Encoding
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\txid
txid Encoding
r -> txid -> Encoding
encodeTxId txid
txid Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
r) Encoding
CBOR.encodeBreak [txid]
[txid1]
txids

    encode (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokTxs)  (MsgReplyTxs [tx1]
txs) =
        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
<> Encoding
CBOR.encodeListLenIndef
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (tx -> Encoding -> Encoding) -> Encoding -> [tx] -> Encoding
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\tx
txid Encoding
r -> tx -> Encoding
encodeTx tx
txid Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
r) Encoding
CBOR.encodeBreak [tx]
[tx1]
txs

    encode (ClientAgency (TokTxIds TokBlockingStyle b
TokBlocking)) Message (TxSubmission2 txid tx) st st'
R:MessageTxSubmission2fromto (*) (*) txid tx st st'
MsgDone =
        Word -> Encoding
CBOR.encodeListLen Word
1
     Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
CBOR.encodeWord Word
4


decodeTxSubmission2
    :: forall txid tx.
       (forall s . CBOR.Decoder s txid)
    -> (forall s . CBOR.Decoder s tx)
    -> (forall (pr :: PeerRole) (st :: TxSubmission2 txid tx) s.
               PeerHasAgency pr st
            -> Int
            -> Word
            -> CBOR.Decoder s (SomeMessage st))
decodeTxSubmission2 :: forall txid tx.
(forall s. Decoder s txid)
-> (forall s. Decoder s tx)
-> forall (pr :: PeerRole) (st :: TxSubmission2 txid tx) s.
   PeerHasAgency pr st -> Int -> Word -> Decoder s (SomeMessage st)
decodeTxSubmission2 forall s. Decoder s txid
decodeTxId forall s. Decoder s tx
decodeTx = PeerHasAgency pr st -> Int -> Word -> Decoder s (SomeMessage st)
forall (pr :: PeerRole) s (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Int -> Word -> Decoder s (SomeMessage st)
decode
  where
    decode :: forall (pr :: PeerRole) s (st :: TxSubmission2 txid tx).
              PeerHasAgency pr st
           -> Int
           -> Word
           -> CBOR.Decoder s (SomeMessage st)
    decode :: forall (pr :: PeerRole) s (st :: TxSubmission2 txid tx).
PeerHasAgency pr st -> Int -> Word -> Decoder s (SomeMessage st)
decode PeerHasAgency pr st
stok Int
len Word
key = do
      case (PeerHasAgency pr st
stok, Int
len, Word
key) of
        (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokInit,       Int
1, Word
6) ->
          SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (TxSubmission2 txid tx) st 'StIdle -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st 'StIdle
Message (TxSubmission2 txid tx) 'StInit 'StIdle
forall {k} {k1} (txid :: k) (tx :: k1).
Message (TxSubmission2 txid tx) 'StInit 'StIdle
MsgInit)
        (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st (*) (*) txid tx st
TokIdle,       Int
4, Word
0) -> do
          blocking <- Decoder s Bool
forall s. Decoder s Bool
CBOR.decodeBool
          ackNo    <- NumTxIdsToAck <$> CBOR.decodeWord16
          reqNo    <- NumTxIdsToReq <$> CBOR.decodeWord16
          return $!
            if blocking
            then SomeMessage (MsgRequestTxIds TokBlocking    ackNo reqNo)
            else SomeMessage (MsgRequestTxIds TokNonBlocking ackNo reqNo)

        (ClientAgency (TokTxIds TokBlockingStyle b
b),  Int
2, Word
1) -> do
          Decoder s ()
forall s. Decoder s ()
CBOR.decodeListLenIndef
          txids <- ([(txid, TxSizeInBytes)]
 -> (txid, TxSizeInBytes) -> [(txid, TxSizeInBytes)])
-> [(txid, TxSizeInBytes)]
-> ([(txid, TxSizeInBytes)] -> [(txid, TxSizeInBytes)])
-> Decoder s (txid, TxSizeInBytes)
-> Decoder s [(txid, TxSizeInBytes)]
forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
CBOR.decodeSequenceLenIndef
                     (((txid, TxSizeInBytes)
 -> [(txid, TxSizeInBytes)] -> [(txid, TxSizeInBytes)])
-> [(txid, TxSizeInBytes)]
-> (txid, TxSizeInBytes)
-> [(txid, TxSizeInBytes)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] [(txid, TxSizeInBytes)] -> [(txid, TxSizeInBytes)]
forall a. [a] -> [a]
reverse
                     (do Int -> Decoder s ()
forall s. Int -> Decoder s ()
CBOR.decodeListLenOf Int
2
                         txid <- Decoder s txid
forall s. Decoder s txid
decodeTxId
                         sz   <- CBOR.decodeWord32
                         return (txid, SizeInBytes sz))
          case (b, txids) of
            (TokBlockingStyle b
TokBlocking, (txid, TxSizeInBytes)
t:[(txid, TxSizeInBytes)]
ts) ->
              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 (TxSubmission2 txid tx) st 'StIdle -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (BlockingReplyList 'StBlocking (txid, TxSizeInBytes)
-> Message (TxSubmission2 txid tx) ('StTxIds 'StBlocking) 'StIdle
forall {k1} (blocking :: StBlockingStyle) txid1 (tx :: k1).
BlockingReplyList blocking (txid1, TxSizeInBytes)
-> Message (TxSubmission2 txid1 tx) ('StTxIds blocking) 'StIdle
MsgReplyTxIds (NonEmpty (txid, TxSizeInBytes)
-> BlockingReplyList 'StBlocking (txid, TxSizeInBytes)
forall a. NonEmpty a -> BlockingReplyList 'StBlocking a
BlockingReply ((txid, TxSizeInBytes)
t (txid, TxSizeInBytes)
-> [(txid, TxSizeInBytes)] -> NonEmpty (txid, TxSizeInBytes)
forall a. a -> [a] -> NonEmpty a
NonEmpty.:| [(txid, TxSizeInBytes)]
ts)))

            (TokBlockingStyle b
TokNonBlocking, [(txid, TxSizeInBytes)]
ts) ->
              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 (TxSubmission2 txid tx) st 'StIdle -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage (BlockingReplyList 'StNonBlocking (txid, TxSizeInBytes)
-> Message
     (TxSubmission2 txid tx) ('StTxIds 'StNonBlocking) 'StIdle
forall {k1} (blocking :: StBlockingStyle) txid1 (tx :: k1).
BlockingReplyList blocking (txid1, TxSizeInBytes)
-> Message (TxSubmission2 txid1 tx) ('StTxIds blocking) 'StIdle
MsgReplyTxIds ([(txid, TxSizeInBytes)]
-> BlockingReplyList 'StNonBlocking (txid, TxSizeInBytes)
forall a. [a] -> BlockingReplyList 'StNonBlocking a
NonBlockingReply [(txid, TxSizeInBytes)]
ts))

            (TokBlockingStyle b
TokBlocking, []) ->
              String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"codecTxSubmission: MsgReplyTxIds: empty list not permitted"


        (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st (*) (*) txid tx st
TokIdle,       Int
2, Word
2) -> do
          Decoder s ()
forall s. Decoder s ()
CBOR.decodeListLenIndef
          txids <- ([txid] -> txid -> [txid])
-> [txid]
-> ([txid] -> [txid])
-> Decoder s txid
-> Decoder s [txid]
forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
CBOR.decodeSequenceLenIndef ((txid -> [txid] -> [txid]) -> [txid] -> txid -> [txid]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] [txid] -> [txid]
forall a. [a] -> [a]
reverse Decoder s txid
forall s. Decoder s txid
decodeTxId
          return (SomeMessage (MsgRequestTxs txids))

        (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokTxs,     Int
2, Word
3) -> do
          Decoder s ()
forall s. Decoder s ()
CBOR.decodeListLenIndef
          txids <- ([tx] -> tx -> [tx])
-> [tx] -> ([tx] -> [tx]) -> Decoder s tx -> Decoder s [tx]
forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
CBOR.decodeSequenceLenIndef ((tx -> [tx] -> [tx]) -> [tx] -> tx -> [tx]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] [tx] -> [tx]
forall a. [a] -> [a]
reverse Decoder s tx
forall s. Decoder s tx
decodeTx
          return (SomeMessage (MsgReplyTxs txids))

        (ClientAgency (TokTxIds TokBlockingStyle b
TokBlocking), Int
1, Word
4) ->
          SomeMessage st -> Decoder s (SomeMessage st)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Message (TxSubmission2 txid tx) st 'StDone -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st 'StDone
Message (TxSubmission2 txid tx) ('StTxIds 'StBlocking) 'StDone
forall {k} {k1} (txid :: k) (tx :: k1).
Message (TxSubmission2 txid tx) ('StTxIds 'StBlocking) 'StDone
MsgDone)

        --
        -- failures per protocol state
        --

        (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokInit, Int
_, Word
_) ->
            String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecTxSubmission (%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 (TokTxIds TokBlockingStyle b
TokBlocking), Int
_, Word
_) ->
            String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecTxSubmission (%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 (TokTxIds TokBlockingStyle b
TokNonBlocking), Int
_, Word
_) ->
            String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecTxSubmission (%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
R:ClientHasAgencyTxSubmission2st (*) (*) txid tx st
TokTxs, Int
_, Word
_) ->
            String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecTxSubmission (%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
R:ServerHasAgencyTxSubmission2st (*) (*) txid tx st
TokIdle, Int
_, Word
_) ->
            String -> Decoder s (SomeMessage st)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> String -> Word -> Int -> String
forall r. PrintfType r => String -> r
printf String
"codecTxSubmission (%s) unexpected key (%d, %d)" (PeerHasAgency pr st -> String
forall a. Show a => a -> String
show PeerHasAgency pr st
stok) Word
key Int
len)

codecTxSubmission2Id
  :: forall txid tx m. Monad m
  => Codec (TxSubmission2 txid tx) CodecFailure m (AnyMessage (TxSubmission2 txid tx))
codecTxSubmission2Id :: forall {k} {k1} (txid :: k) (tx :: k1) (m :: * -> *).
Monad m =>
Codec
  (TxSubmission2 txid tx)
  CodecFailure
  m
  (AnyMessage (TxSubmission2 txid tx))
codecTxSubmission2Id = Codec { PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
encode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
encode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
encode, PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
decode }
 where
  encode :: forall (pr :: PeerRole) st st'.
            PeerHasAgency pr st
         -> Message (TxSubmission2 txid tx) st st'
         -> AnyMessage (TxSubmission2 txid tx)
  encode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx)
       (st' :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
encode PeerHasAgency pr st
_ = Message (TxSubmission2 txid tx) st st'
-> AnyMessage (TxSubmission2 txid tx)
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> AnyMessage ps
AnyMessage

  decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
            PeerHasAgency pr st
         -> m (DecodeStep (AnyMessage (TxSubmission2 txid tx))
                          CodecFailure m (SomeMessage st))
  decode :: forall (pr :: PeerRole) (st :: TxSubmission2 txid tx).
PeerHasAgency pr st
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
decode PeerHasAgency pr st
stok = DecodeStep
  (AnyMessage (TxSubmission2 txid tx))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (TxSubmission2 txid tx))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (TxSubmission2 txid tx))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ (Maybe (AnyMessage (TxSubmission2 txid tx))
 -> m (DecodeStep
         (AnyMessage (TxSubmission2 txid tx))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     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 (TxSubmission2 txid tx))
  -> m (DecodeStep
          (AnyMessage (TxSubmission2 txid tx))
          CodecFailure
          m
          (SomeMessage st)))
 -> DecodeStep
      (AnyMessage (TxSubmission2 txid tx))
      CodecFailure
      m
      (SomeMessage st))
-> (Maybe (AnyMessage (TxSubmission2 txid tx))
    -> m (DecodeStep
            (AnyMessage (TxSubmission2 txid tx))
            CodecFailure
            m
            (SomeMessage st)))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall a b. (a -> b) -> a -> b
$ \Maybe (AnyMessage (TxSubmission2 txid tx))
bytes -> DecodeStep
  (AnyMessage (TxSubmission2 txid tx))
  CodecFailure
  m
  (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DecodeStep
   (AnyMessage (TxSubmission2 txid tx))
   CodecFailure
   m
   (SomeMessage st)
 -> m (DecodeStep
         (AnyMessage (TxSubmission2 txid tx))
         CodecFailure
         m
         (SomeMessage st)))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
-> m (DecodeStep
        (AnyMessage (TxSubmission2 txid tx))
        CodecFailure
        m
        (SomeMessage st))
forall a b. (a -> b) -> a -> b
$ case (PeerHasAgency pr st
stok, Maybe (AnyMessage (TxSubmission2 txid tx))
bytes) of
    (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokInit,      Just (AnyMessage msg :: Message (TxSubmission2 txid tx) st st'
msg@Message (TxSubmission2 txid tx) st st'
R:MessageTxSubmission2fromto k k1 txid tx st st'
MsgInit))              -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
    (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st k k1 txid tx st
TokIdle,      Just (AnyMessage msg :: Message (TxSubmission2 txid tx) st st'
msg@(MsgRequestTxIds {}))) -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
    (ServerAgency ServerHasAgency st
R:ServerHasAgencyTxSubmission2st k k1 txid tx st
TokIdle,      Just (AnyMessage msg :: Message (TxSubmission2 txid tx) st st'
msg@(MsgRequestTxs {})))   -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
    (ClientAgency ClientHasAgency st
R:ClientHasAgencyTxSubmission2st k k1 txid tx st
TokTxs,       Just (AnyMessage msg :: Message (TxSubmission2 txid tx) st st'
msg@(MsgReplyTxs {})))     -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
    (ClientAgency (TokTxIds TokBlockingStyle b
b), Just (AnyMessage Message (TxSubmission2 txid tx) st st'
msg)) -> case (TokBlockingStyle b
b, Message (TxSubmission2 txid tx) st st'
msg) of
      (TokBlockingStyle b
TokBlocking,    MsgReplyTxIds (BlockingReply {}))    -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
      (TokBlockingStyle b
TokNonBlocking, MsgReplyTxIds (NonBlockingReply {})) -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
      (TokBlockingStyle b
TokBlocking,    MsgDone {})                          -> SomeMessage st
-> Maybe (AnyMessage (TxSubmission2 txid tx))
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
a -> Maybe bytes -> DecodeStep bytes failure m a
DecodeDone (Message (TxSubmission2 txid tx) st st' -> SomeMessage st
forall ps (st :: ps) (st' :: ps).
Message ps st st' -> SomeMessage st
SomeMessage Message (TxSubmission2 txid tx) st st'
Message (TxSubmission2 txid tx) st st'
msg) Maybe (AnyMessage (TxSubmission2 txid tx))
forall a. Maybe a
Nothing
      (TokBlockingStyle b
_, Message (TxSubmission2 txid tx) st st'
_) -> CodecFailure
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail (String -> CodecFailure
CodecFailure String
"codecTxSubmissionId: no matching message")
    (PeerHasAgency pr st
_, Maybe (AnyMessage (TxSubmission2 txid tx))
_) -> CodecFailure
-> DecodeStep
     (AnyMessage (TxSubmission2 txid tx))
     CodecFailure
     m
     (SomeMessage st)
forall bytes failure (m :: * -> *) a.
failure -> DecodeStep bytes failure m a
DecodeFail (String -> CodecFailure
CodecFailure String
"codecTxSubmissionId: no matching message")