-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

-- | Owned protocol values and schema codecs. No native calls occur here.
module Cometa.Internal.ProtocolValues
  ( NonNegativeRational (..),
    UnitInterval (..),
    ExUnits (..),
    ProtocolVersion (..),
    ExUnitPrices (..),
    PoolVotingThresholds (..),
    DRepVotingThresholds (..),
    mkNonNegativeRational,
    mkUnitInterval,
    rationalNumerator,
    rationalDenominator,
    rationalValue,
    unitIntervalRational,
    mkExUnits,
    mkProtocolVersion,
    encodeBytes,
    decodeBytes,
    decodeTuple,
    encodeRational,
    decodeRational,
    encodeUnitInterval,
    decodeUnitInterval,
    encodeExUnits,
    decodeExUnits,
    encodeProtocolVersion,
    decodeProtocolVersion,
    encodeExUnitPrices,
    decodeExUnitPrices,
    encodePoolVotingThresholds,
    decodePoolVotingThresholds,
    encodeDRepVotingThresholds,
    decodeDRepVotingThresholds,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import qualified Codec.CBOR.Write as Write
import Cometa.Error (Error (..))
import Control.Monad (when)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as Lazy
import Data.Ratio ((%))
import qualified Data.Text as Text
import Data.Word (Word32, Word64)

-- TYPES ----------------------------------------------------------------------

-- | Exact nonnegative fraction with an unsigned 64-bit numerator and positive
-- unsigned 64-bit denominator. The original pair is retained for encoding;
-- equality and ordering compare numeric values. Show is diagnostic.
data NonNegativeRational = NonNegativeRational !Word64 !Word64 deriving (Int -> NonNegativeRational -> ShowS
[NonNegativeRational] -> ShowS
NonNegativeRational -> String
(Int -> NonNegativeRational -> ShowS)
-> (NonNegativeRational -> String)
-> ([NonNegativeRational] -> ShowS)
-> Show NonNegativeRational
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NonNegativeRational -> ShowS
showsPrec :: Int -> NonNegativeRational -> ShowS
$cshow :: NonNegativeRational -> String
show :: NonNegativeRational -> String
$cshowList :: [NonNegativeRational] -> ShowS
showList :: [NonNegativeRational] -> ShowS
Show)

instance Eq NonNegativeRational where
  NonNegativeRational
a == :: NonNegativeRational -> NonNegativeRational -> Bool
== NonNegativeRational
b = NonNegativeRational -> NonNegativeRational -> Ordering
forall a. Ord a => a -> a -> Ordering
compare NonNegativeRational
a NonNegativeRational
b Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ

instance Ord NonNegativeRational where
  compare :: NonNegativeRational -> NonNegativeRational -> Ordering
compare (NonNegativeRational Word64
n Word64
d) (NonNegativeRational Word64
m Word64
e) = Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
e) (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
d)

-- | Exact proportion between zero and one, inclusive. Equality and ordering
-- compare numeric values; the original pair is retained for encoding.
newtype UnitInterval = UnitInterval NonNegativeRational deriving (UnitInterval -> UnitInterval -> Bool
(UnitInterval -> UnitInterval -> Bool)
-> (UnitInterval -> UnitInterval -> Bool) -> Eq UnitInterval
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnitInterval -> UnitInterval -> Bool
== :: UnitInterval -> UnitInterval -> Bool
$c/= :: UnitInterval -> UnitInterval -> Bool
/= :: UnitInterval -> UnitInterval -> Bool
Eq, Eq UnitInterval
Eq UnitInterval =>
(UnitInterval -> UnitInterval -> Ordering)
-> (UnitInterval -> UnitInterval -> Bool)
-> (UnitInterval -> UnitInterval -> Bool)
-> (UnitInterval -> UnitInterval -> Bool)
-> (UnitInterval -> UnitInterval -> Bool)
-> (UnitInterval -> UnitInterval -> UnitInterval)
-> (UnitInterval -> UnitInterval -> UnitInterval)
-> Ord UnitInterval
UnitInterval -> UnitInterval -> Bool
UnitInterval -> UnitInterval -> Ordering
UnitInterval -> UnitInterval -> UnitInterval
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: UnitInterval -> UnitInterval -> Ordering
compare :: UnitInterval -> UnitInterval -> Ordering
$c< :: UnitInterval -> UnitInterval -> Bool
< :: UnitInterval -> UnitInterval -> Bool
$c<= :: UnitInterval -> UnitInterval -> Bool
<= :: UnitInterval -> UnitInterval -> Bool
$c> :: UnitInterval -> UnitInterval -> Bool
> :: UnitInterval -> UnitInterval -> Bool
$c>= :: UnitInterval -> UnitInterval -> Bool
>= :: UnitInterval -> UnitInterval -> Bool
$cmax :: UnitInterval -> UnitInterval -> UnitInterval
max :: UnitInterval -> UnitInterval -> UnitInterval
$cmin :: UnitInterval -> UnitInterval -> UnitInterval
min :: UnitInterval -> UnitInterval -> UnitInterval
Ord, Int -> UnitInterval -> ShowS
[UnitInterval] -> ShowS
UnitInterval -> String
(Int -> UnitInterval -> ShowS)
-> (UnitInterval -> String)
-> ([UnitInterval] -> ShowS)
-> Show UnitInterval
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnitInterval -> ShowS
showsPrec :: Int -> UnitInterval -> ShowS
$cshow :: UnitInterval -> String
show :: UnitInterval -> String
$cshowList :: [UnitInterval] -> ShowS
showList :: [UnitInterval] -> ShowS
Show)

-- | Memory and CPU execution budget. Each unsigned component is bounded by 2^64-1.
data ExUnits
  = -- | Assemble validated components.
    ExUnits
    { -- | Memory execution units.
      ExUnits -> Word64
memory :: !Word64,
      -- | CPU execution steps.
      ExUnits -> Word64
steps :: !Word64
    }
  deriving (ExUnits -> ExUnits -> Bool
(ExUnits -> ExUnits -> Bool)
-> (ExUnits -> ExUnits -> Bool) -> Eq ExUnits
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExUnits -> ExUnits -> Bool
== :: ExUnits -> ExUnits -> Bool
$c/= :: ExUnits -> ExUnits -> Bool
/= :: ExUnits -> ExUnits -> Bool
Eq, Int -> ExUnits -> ShowS
[ExUnits] -> ShowS
ExUnits -> String
(Int -> ExUnits -> ShowS)
-> (ExUnits -> String) -> ([ExUnits] -> ShowS) -> Show ExUnits
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExUnits -> ShowS
showsPrec :: Int -> ExUnits -> ShowS
$cshow :: ExUnits -> String
show :: ExUnits -> String
$cshowList :: [ExUnits] -> ShowS
showList :: [ExUnits] -> ShowS
Show)

-- | Protocol major/minor version; ordering compares major then minor.
data ProtocolVersion
  = -- | Assemble validated components.
    ProtocolVersion
    { -- | Unsigned 64-bit major version.
      ProtocolVersion -> Word64
major :: !Word64,
      -- | Unsigned 32-bit minor version.
      ProtocolVersion -> Word32
minor :: !Word32
    }
  deriving (ProtocolVersion -> ProtocolVersion -> Bool
(ProtocolVersion -> ProtocolVersion -> Bool)
-> (ProtocolVersion -> ProtocolVersion -> Bool)
-> Eq ProtocolVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProtocolVersion -> ProtocolVersion -> Bool
== :: ProtocolVersion -> ProtocolVersion -> Bool
$c/= :: ProtocolVersion -> ProtocolVersion -> Bool
/= :: ProtocolVersion -> ProtocolVersion -> Bool
Eq, Eq ProtocolVersion
Eq ProtocolVersion =>
(ProtocolVersion -> ProtocolVersion -> Ordering)
-> (ProtocolVersion -> ProtocolVersion -> Bool)
-> (ProtocolVersion -> ProtocolVersion -> Bool)
-> (ProtocolVersion -> ProtocolVersion -> Bool)
-> (ProtocolVersion -> ProtocolVersion -> Bool)
-> (ProtocolVersion -> ProtocolVersion -> ProtocolVersion)
-> (ProtocolVersion -> ProtocolVersion -> ProtocolVersion)
-> Ord ProtocolVersion
ProtocolVersion -> ProtocolVersion -> Bool
ProtocolVersion -> ProtocolVersion -> Ordering
ProtocolVersion -> ProtocolVersion -> ProtocolVersion
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ProtocolVersion -> ProtocolVersion -> Ordering
compare :: ProtocolVersion -> ProtocolVersion -> Ordering
$c< :: ProtocolVersion -> ProtocolVersion -> Bool
< :: ProtocolVersion -> ProtocolVersion -> Bool
$c<= :: ProtocolVersion -> ProtocolVersion -> Bool
<= :: ProtocolVersion -> ProtocolVersion -> Bool
$c> :: ProtocolVersion -> ProtocolVersion -> Bool
> :: ProtocolVersion -> ProtocolVersion -> Bool
$c>= :: ProtocolVersion -> ProtocolVersion -> Bool
>= :: ProtocolVersion -> ProtocolVersion -> Bool
$cmax :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
max :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
$cmin :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
min :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
Ord, Int -> ProtocolVersion -> ShowS
[ProtocolVersion] -> ShowS
ProtocolVersion -> String
(Int -> ProtocolVersion -> ShowS)
-> (ProtocolVersion -> String)
-> ([ProtocolVersion] -> ShowS)
-> Show ProtocolVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProtocolVersion -> ShowS
showsPrec :: Int -> ProtocolVersion -> ShowS
$cshow :: ProtocolVersion -> String
show :: ProtocolVersion -> String
$cshowList :: [ProtocolVersion] -> ShowS
showList :: [ProtocolVersion] -> ShowS
Show)

-- | Exact nonnegative lovelace costs per memory unit and CPU step.
data ExUnitPrices
  = -- | Assemble validated components.
    ExUnitPrices
    { -- | Lovelace per memory unit.
      ExUnitPrices -> NonNegativeRational
memoryPrice :: !NonNegativeRational,
      -- | Lovelace per CPU step.
      ExUnitPrices -> NonNegativeRational
stepPrice :: !NonNegativeRational
    }
  deriving (ExUnitPrices -> ExUnitPrices -> Bool
(ExUnitPrices -> ExUnitPrices -> Bool)
-> (ExUnitPrices -> ExUnitPrices -> Bool) -> Eq ExUnitPrices
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExUnitPrices -> ExUnitPrices -> Bool
== :: ExUnitPrices -> ExUnitPrices -> Bool
$c/= :: ExUnitPrices -> ExUnitPrices -> Bool
/= :: ExUnitPrices -> ExUnitPrices -> Bool
Eq, Int -> ExUnitPrices -> ShowS
[ExUnitPrices] -> ShowS
ExUnitPrices -> String
(Int -> ExUnitPrices -> ShowS)
-> (ExUnitPrices -> String)
-> ([ExUnitPrices] -> ShowS)
-> Show ExUnitPrices
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExUnitPrices -> ShowS
showsPrec :: Int -> ExUnitPrices -> ShowS
$cshow :: ExUnitPrices -> String
show :: ExUnitPrices -> String
$cshowList :: [ExUnitPrices] -> ShowS
showList :: [ExUnitPrices] -> ShowS
Show)

-- | Stake-pool approval thresholds, each from zero through one inclusive.
data PoolVotingThresholds
  = -- | Assemble validated components.
    PoolVotingThresholds
    { -- | Approval threshold for a motion of no confidence in the committee.
      PoolVotingThresholds -> UnitInterval
poolMotionNoConfidence :: !UnitInterval,
      -- | Approval threshold for committee changes while the committee has confidence.
      PoolVotingThresholds -> UnitInterval
poolCommitteeNormal :: !UnitInterval,
      -- | Approval threshold for committee changes after a loss of confidence.
      PoolVotingThresholds -> UnitInterval
poolCommitteeNoConfidence :: !UnitInterval,
      -- | Approval threshold for initiating a protocol hard fork.
      PoolVotingThresholds -> UnitInterval
poolHardForkInitiation :: !UnitInterval,
      -- | Approval threshold for security-relevant protocol parameter changes.
      PoolVotingThresholds -> UnitInterval
poolSecurityRelevantParameter :: !UnitInterval
    }
  deriving (PoolVotingThresholds -> PoolVotingThresholds -> Bool
(PoolVotingThresholds -> PoolVotingThresholds -> Bool)
-> (PoolVotingThresholds -> PoolVotingThresholds -> Bool)
-> Eq PoolVotingThresholds
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PoolVotingThresholds -> PoolVotingThresholds -> Bool
== :: PoolVotingThresholds -> PoolVotingThresholds -> Bool
$c/= :: PoolVotingThresholds -> PoolVotingThresholds -> Bool
/= :: PoolVotingThresholds -> PoolVotingThresholds -> Bool
Eq, Int -> PoolVotingThresholds -> ShowS
[PoolVotingThresholds] -> ShowS
PoolVotingThresholds -> String
(Int -> PoolVotingThresholds -> ShowS)
-> (PoolVotingThresholds -> String)
-> ([PoolVotingThresholds] -> ShowS)
-> Show PoolVotingThresholds
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PoolVotingThresholds -> ShowS
showsPrec :: Int -> PoolVotingThresholds -> ShowS
$cshow :: PoolVotingThresholds -> String
show :: PoolVotingThresholds -> String
$cshowList :: [PoolVotingThresholds] -> ShowS
showList :: [PoolVotingThresholds] -> ShowS
Show)

-- | DRep approval thresholds, each from zero through one inclusive.
data DRepVotingThresholds
  = -- | Assemble validated components.
    DRepVotingThresholds
    { -- | Approval threshold for a motion of no confidence in the committee.
      DRepVotingThresholds -> UnitInterval
drepMotionNoConfidence :: !UnitInterval,
      -- | Approval threshold for committee changes while the committee has confidence.
      DRepVotingThresholds -> UnitInterval
drepCommitteeNormal :: !UnitInterval,
      -- | Approval threshold for committee changes after a loss of confidence.
      DRepVotingThresholds -> UnitInterval
drepCommitteeNoConfidence :: !UnitInterval,
      -- | Approval threshold for adopting a new constitution.
      DRepVotingThresholds -> UnitInterval
drepUpdateConstitution :: !UnitInterval,
      -- | Approval threshold for initiating a protocol hard fork.
      DRepVotingThresholds -> UnitInterval
drepHardForkInitiation :: !UnitInterval,
      -- | Approval threshold for network protocol parameter changes.
      DRepVotingThresholds -> UnitInterval
drepNetworkParameterGroup :: !UnitInterval,
      -- | Approval threshold for economic protocol parameter changes.
      DRepVotingThresholds -> UnitInterval
drepEconomicParameterGroup :: !UnitInterval,
      -- | Approval threshold for technical protocol parameter changes.
      DRepVotingThresholds -> UnitInterval
drepTechnicalParameterGroup :: !UnitInterval,
      -- | Approval threshold for governance protocol parameter changes.
      DRepVotingThresholds -> UnitInterval
drepGovernanceParameterGroup :: !UnitInterval,
      -- | Approval threshold for treasury withdrawal proposals.
      DRepVotingThresholds -> UnitInterval
drepTreasuryWithdrawal :: !UnitInterval
    }
  deriving (DRepVotingThresholds -> DRepVotingThresholds -> Bool
(DRepVotingThresholds -> DRepVotingThresholds -> Bool)
-> (DRepVotingThresholds -> DRepVotingThresholds -> Bool)
-> Eq DRepVotingThresholds
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DRepVotingThresholds -> DRepVotingThresholds -> Bool
== :: DRepVotingThresholds -> DRepVotingThresholds -> Bool
$c/= :: DRepVotingThresholds -> DRepVotingThresholds -> Bool
/= :: DRepVotingThresholds -> DRepVotingThresholds -> Bool
Eq, Int -> DRepVotingThresholds -> ShowS
[DRepVotingThresholds] -> ShowS
DRepVotingThresholds -> String
(Int -> DRepVotingThresholds -> ShowS)
-> (DRepVotingThresholds -> String)
-> ([DRepVotingThresholds] -> ShowS)
-> Show DRepVotingThresholds
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DRepVotingThresholds -> ShowS
showsPrec :: Int -> DRepVotingThresholds -> ShowS
$cshow :: DRepVotingThresholds -> String
show :: DRepVotingThresholds -> String
$cshowList :: [DRepVotingThresholds] -> ShowS
showList :: [DRepVotingThresholds] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Validate an unsigned 64-bit numerator and positive unsigned 64-bit
-- denominator. Retain the exact pair. Invalid values return
-- 'InvalidProtocolRational'.
mkNonNegativeRational :: Integer -> Integer -> Either Error NonNegativeRational
mkNonNegativeRational :: Integer -> Integer -> Either Error NonNegativeRational
mkNonNegativeRational Integer
n Integer
d
  | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) Bool -> Bool -> Bool
|| Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
0 Bool -> Bool -> Bool
|| Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) = Error -> Either Error NonNegativeRational
forall a b. a -> Either a b
Left (Integer -> Integer -> Error
InvalidProtocolRational Integer
n Integer
d)
  | Bool
otherwise = NonNegativeRational -> Either Error NonNegativeRational
forall a b. b -> Either a b
Right (Word64 -> Word64 -> NonNegativeRational
NonNegativeRational (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
n) (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
d))

-- | Validate a proportion numerator and denominator. Invalid unsigned bounds
-- or a zero denominator return 'InvalidProtocolRational'; a numerator larger
-- than its denominator returns 'InvalidUnitInterval'.
mkUnitInterval :: Integer -> Integer -> Either Error UnitInterval
mkUnitInterval :: Integer -> Integer -> Either Error UnitInterval
mkUnitInterval Integer
n Integer
d = do
  NonNegativeRational
value <- Integer -> Integer -> Either Error NonNegativeRational
mkNonNegativeRational Integer
n Integer
d
  if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
d then Error -> Either Error UnitInterval
forall a b. a -> Either a b
Left (Integer -> Integer -> Error
InvalidUnitInterval Integer
n Integer
d) else UnitInterval -> Either Error UnitInterval
forall a b. b -> Either a b
Right (NonNegativeRational -> UnitInterval
UnitInterval NonNegativeRational
value)

rationalNumerator :: NonNegativeRational -> Word64
rationalNumerator :: NonNegativeRational -> Word64
rationalNumerator (NonNegativeRational Word64
n Word64
_) = Word64
n

rationalDenominator :: NonNegativeRational -> Word64
rationalDenominator :: NonNegativeRational -> Word64
rationalDenominator (NonNegativeRational Word64
_ Word64
d) = Word64
d

rationalValue :: NonNegativeRational -> Rational
rationalValue :: NonNegativeRational -> Rational
rationalValue (NonNegativeRational Word64
n Word64
d) = Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
n Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
d

unitIntervalRational :: UnitInterval -> NonNegativeRational
unitIntervalRational :: UnitInterval -> NonNegativeRational
unitIntervalRational (UnitInterval NonNegativeRational
r) = NonNegativeRational
r

-- | Validate external memory and step counts in 0..2^64-1. Invalid input
-- returns 'InvalidExUnits'.
mkExUnits :: Integer -> Integer -> Either Error ExUnits
mkExUnits :: Integer -> Integer -> Either Error ExUnits
mkExUnits Integer
m Integer
s
  | Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) Bool -> Bool -> Bool
|| Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) = Error -> Either Error ExUnits
forall a b. a -> Either a b
Left (Integer -> Integer -> Error
InvalidExUnits Integer
m Integer
s)
  | Bool
otherwise = ExUnits -> Either Error ExUnits
forall a b. b -> Either a b
Right (Word64 -> Word64 -> ExUnits
ExUnits (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
m) (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
s))

-- | Validate an external major in 0..2^64-1 and minor in 0..2^32-1.
-- Invalid input returns 'InvalidProtocolVersion'.
mkProtocolVersion :: Integer -> Integer -> Either Error ProtocolVersion
mkProtocolVersion :: Integer -> Integer -> Either Error ProtocolVersion
mkProtocolVersion Integer
a Integer
b
  | Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
a Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) Bool -> Bool -> Bool
|| Integer
b Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
b Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word32
forall a. Bounded a => a
maxBound :: Word32) = Error -> Either Error ProtocolVersion
forall a b. a -> Either a b
Left (Integer -> Integer -> Error
InvalidProtocolVersion Integer
a Integer
b)
  | Bool
otherwise = ProtocolVersion -> Either Error ProtocolVersion
forall a b. b -> Either a b
Right (Word64 -> Word32 -> ProtocolVersion
ProtocolVersion (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
a) (Integer -> Word32
forall a. Num a => Integer -> a
fromInteger Integer
b))

encodeRational :: NonNegativeRational -> Encode.Encoding
encodeRational :: NonNegativeRational -> Encoding
encodeRational (NonNegativeRational Word64
n Word64
d) = Word -> Encoding
Encode.encodeTag Word
30 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
n Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
d

decodeRational :: Decode.Decoder s NonNegativeRational
decodeRational :: forall s. Decoder s NonNegativeRational
decodeRational = do
  Word
tag <- Decoder s Word
forall s. Decoder s Word
Decode.decodeTag
  Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word
tag Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
30) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected rational tag 30")
  Int
-> Decoder s NonNegativeRational -> Decoder s NonNegativeRational
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
2 (Decoder s NonNegativeRational -> Decoder s NonNegativeRational)
-> Decoder s NonNegativeRational -> Decoder s NonNegativeRational
forall a b. (a -> b) -> a -> b
$ do
    Word64
n <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
    Word64
d <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
    (Error -> Decoder s NonNegativeRational)
-> (NonNegativeRational -> Decoder s NonNegativeRational)
-> Either Error NonNegativeRational
-> Decoder s NonNegativeRational
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s NonNegativeRational
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s NonNegativeRational)
-> (Error -> String) -> Error -> Decoder s NonNegativeRational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) NonNegativeRational -> Decoder s NonNegativeRational
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Integer -> Either Error NonNegativeRational
mkNonNegativeRational (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
n) (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
d))

encodeUnitInterval :: UnitInterval -> Encode.Encoding
encodeUnitInterval :: UnitInterval -> Encoding
encodeUnitInterval = NonNegativeRational -> Encoding
encodeRational (NonNegativeRational -> Encoding)
-> (UnitInterval -> NonNegativeRational)
-> UnitInterval
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnitInterval -> NonNegativeRational
unitIntervalRational

decodeUnitInterval :: Decode.Decoder s UnitInterval
decodeUnitInterval :: forall s. Decoder s UnitInterval
decodeUnitInterval = do
  r :: NonNegativeRational
r@(NonNegativeRational Word64
n Word64
d) <- Decoder s NonNegativeRational
forall s. Decoder s NonNegativeRational
decodeRational
  if Word64
n Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
d then String -> Decoder s UnitInterval
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unit interval exceeds one" else UnitInterval -> Decoder s UnitInterval
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NonNegativeRational -> UnitInterval
UnitInterval NonNegativeRational
r)

encodeExUnits :: ExUnits -> Encode.Encoding
encodeExUnits :: ExUnits -> Encoding
encodeExUnits (ExUnits Word64
m Word64
s) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
m Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
s

decodeExUnits :: Decode.Decoder s ExUnits
decodeExUnits :: forall s. Decoder s ExUnits
decodeExUnits = Int -> Decoder s ExUnits -> Decoder s ExUnits
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
2 (Word64 -> Word64 -> ExUnits
ExUnits (Word64 -> Word64 -> ExUnits)
-> Decoder s Word64 -> Decoder s (Word64 -> ExUnits)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64 Decoder s (Word64 -> ExUnits)
-> Decoder s Word64 -> Decoder s ExUnits
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)

encodeProtocolVersion :: ProtocolVersion -> Encode.Encoding
encodeProtocolVersion :: ProtocolVersion -> Encoding
encodeProtocolVersion (ProtocolVersion Word64
a Word32
b) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
a Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word32 -> Encoding
Encode.encodeWord32 Word32
b

decodeProtocolVersion :: Decode.Decoder s ProtocolVersion
decodeProtocolVersion :: forall s. Decoder s ProtocolVersion
decodeProtocolVersion = Int -> Decoder s ProtocolVersion -> Decoder s ProtocolVersion
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
2 (Word64 -> Word32 -> ProtocolVersion
ProtocolVersion (Word64 -> Word32 -> ProtocolVersion)
-> Decoder s Word64 -> Decoder s (Word32 -> ProtocolVersion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64 Decoder s (Word32 -> ProtocolVersion)
-> Decoder s Word32 -> Decoder s ProtocolVersion
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word32
forall s. Decoder s Word32
Decode.decodeWord32)

encodeExUnitPrices :: ExUnitPrices -> Encode.Encoding
encodeExUnitPrices :: ExUnitPrices -> Encoding
encodeExUnitPrices (ExUnitPrices NonNegativeRational
m NonNegativeRational
s) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> NonNegativeRational -> Encoding
encodeRational NonNegativeRational
m Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> NonNegativeRational -> Encoding
encodeRational NonNegativeRational
s

decodeExUnitPrices :: Decode.Decoder s ExUnitPrices
decodeExUnitPrices :: forall s. Decoder s ExUnitPrices
decodeExUnitPrices = Int -> Decoder s ExUnitPrices -> Decoder s ExUnitPrices
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
2 (NonNegativeRational -> NonNegativeRational -> ExUnitPrices
ExUnitPrices (NonNegativeRational -> NonNegativeRational -> ExUnitPrices)
-> Decoder s NonNegativeRational
-> Decoder s (NonNegativeRational -> ExUnitPrices)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s NonNegativeRational
forall s. Decoder s NonNegativeRational
decodeRational Decoder s (NonNegativeRational -> ExUnitPrices)
-> Decoder s NonNegativeRational -> Decoder s ExUnitPrices
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s NonNegativeRational
forall s. Decoder s NonNegativeRational
decodeRational)

-- | Fixed tuples accept definite and indefinite arrays and check exact arity.
decodeTuple :: Int -> Decode.Decoder s a -> Decode.Decoder s a
decodeTuple :: forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
expected Decoder s a
fields = do
  Maybe Int
count <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  case Maybe Int
count of
    Just Int
actual | Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
expected -> String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"wrong protocol tuple length"
    Maybe Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  a
result <- Decoder s a
fields
  case Maybe Int
count of
    Maybe Int
Nothing -> Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr Decoder s Bool -> (Bool -> Decoder s ()) -> Decoder s ()
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
end -> if Bool
end then () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure () else String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"extra protocol tuple field"
    Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result

encodeBytes :: (a -> Encode.Encoding) -> a -> ByteString
encodeBytes :: forall a. (a -> Encoding) -> a -> ByteString
encodeBytes a -> Encoding
encode = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString) -> (a -> Encoding) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
encode

decodeBytes :: (forall s. Decode.Decoder s a) -> ByteString -> Either Error a
decodeBytes :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeBytes forall s. Decoder s a
decode ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s a
forall s. Decoder s a
decode (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
  Left DeserialiseFailure
failure -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidProtocolCbor (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
failure)))
  Right (ByteString
remaining, a
value)
    | ByteString -> Bool
Lazy.null ByteString
remaining -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
    | Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidProtocolCbor Text
"trailing CBOR data")

encodePoolVotingThresholds :: PoolVotingThresholds -> Encode.Encoding
encodePoolVotingThresholds :: PoolVotingThresholds -> Encoding
encodePoolVotingThresholds (PoolVotingThresholds UnitInterval
noConfidence UnitInterval
normalCommittee UnitInterval
noConfidenceCommittee UnitInterval
hardFork UnitInterval
securityParameters) = Word -> Encoding
Encode.encodeListLen Word
5 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
noConfidence Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
normalCommittee Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
noConfidenceCommittee Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
hardFork Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
securityParameters

decodePoolVotingThresholds :: Decode.Decoder s PoolVotingThresholds
decodePoolVotingThresholds :: forall s. Decoder s PoolVotingThresholds
decodePoolVotingThresholds = Int
-> Decoder s PoolVotingThresholds -> Decoder s PoolVotingThresholds
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
5 (UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> PoolVotingThresholds
PoolVotingThresholds (UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> PoolVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> PoolVotingThresholds)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> PoolVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval -> UnitInterval -> PoolVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval -> UnitInterval -> PoolVotingThresholds)
-> Decoder s UnitInterval
-> Decoder s (UnitInterval -> UnitInterval -> PoolVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder s (UnitInterval -> UnitInterval -> PoolVotingThresholds)
-> Decoder s UnitInterval
-> Decoder s (UnitInterval -> PoolVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder s (UnitInterval -> PoolVotingThresholds)
-> Decoder s UnitInterval -> Decoder s PoolVotingThresholds
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval)

encodeDRepVotingThresholds :: DRepVotingThresholds -> Encode.Encoding
encodeDRepVotingThresholds :: DRepVotingThresholds -> Encoding
encodeDRepVotingThresholds (DRepVotingThresholds UnitInterval
noConfidence UnitInterval
normalCommittee UnitInterval
noConfidenceCommittee UnitInterval
constitution UnitInterval
hardFork UnitInterval
networkParameters UnitInterval
economicParameters UnitInterval
technicalParameters UnitInterval
governanceParameters UnitInterval
treasury) = Word -> Encoding
Encode.encodeListLen Word
10 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
noConfidence Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
normalCommittee Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
noConfidenceCommittee Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
constitution Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
hardFork Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
networkParameters Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
economicParameters Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
technicalParameters Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
governanceParameters Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
encodeUnitInterval UnitInterval
treasury

decodeDRepVotingThresholds :: Decode.Decoder s DRepVotingThresholds
decodeDRepVotingThresholds :: forall s. Decoder s DRepVotingThresholds
decodeDRepVotingThresholds = Int
-> Decoder s DRepVotingThresholds -> Decoder s DRepVotingThresholds
forall s a. Int -> Decoder s a -> Decoder s a
decodeTuple Int
10 (UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> DRepVotingThresholds
DRepVotingThresholds (UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder
     s
     (UnitInterval
      -> UnitInterval -> UnitInterval -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder
  s
  (UnitInterval
   -> UnitInterval -> UnitInterval -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder s (UnitInterval -> UnitInterval -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder s (UnitInterval -> UnitInterval -> DRepVotingThresholds)
-> Decoder s UnitInterval
-> Decoder s (UnitInterval -> DRepVotingThresholds)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval Decoder s (UnitInterval -> DRepVotingThresholds)
-> Decoder s UnitInterval -> Decoder s DRepVotingThresholds
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s UnitInterval
forall s. Decoder s UnitInterval
decodeUnitInterval)