{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Chain.Update.SoftforkRule (
  SoftforkRule (..),
)
where

import Cardano.Chain.Common (LovelacePortion)
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  encodeListLen,
  enforceSize,
  fromByronCBOR,
  toByronCBOR,
 )
import Cardano.Prelude
import qualified Data.Aeson as Aeson
import Formatting (bprint, build)
import qualified Formatting.Buildable as B
import NoThunks.Class (NoThunks (..))
import Text.JSON.Canonical (FromJSON (..), ToJSON (..), fromJSField, mkObject)

-- | Values defining softfork resolution rule
--
--   If a proposal is confirmed at the 's'-th epoch, softfork resolution
--   threshold at the 't'-th epoch will be 'max spMinThd (spInitThd - (t - s) *
--   spThdDecrement)'.
--
--   Softfork resolution threshold is the portion of total stake such that if
--   total stake of issuers of blocks with some block version is greater than
--   this portion, this block version becomes adopted.
data SoftforkRule = SoftforkRule
  { SoftforkRule -> LovelacePortion
srInitThd :: !LovelacePortion
  -- ^ Initial threshold (right after proposal is confirmed).
  , SoftforkRule -> LovelacePortion
srMinThd :: !LovelacePortion
  -- ^ Minimal threshold (i. e. threshold can't become less than this one).
  , SoftforkRule -> LovelacePortion
srThdDecrement :: !LovelacePortion
  -- ^ Theshold will be decreased by this value after each epoch.
  }
  deriving (Int -> SoftforkRule -> ShowS
[SoftforkRule] -> ShowS
SoftforkRule -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SoftforkRule] -> ShowS
$cshowList :: [SoftforkRule] -> ShowS
show :: SoftforkRule -> String
$cshow :: SoftforkRule -> String
showsPrec :: Int -> SoftforkRule -> ShowS
$cshowsPrec :: Int -> SoftforkRule -> ShowS
Show, SoftforkRule -> SoftforkRule -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SoftforkRule -> SoftforkRule -> Bool
$c/= :: SoftforkRule -> SoftforkRule -> Bool
== :: SoftforkRule -> SoftforkRule -> Bool
$c== :: SoftforkRule -> SoftforkRule -> Bool
Eq, Eq SoftforkRule
SoftforkRule -> SoftforkRule -> Bool
SoftforkRule -> SoftforkRule -> Ordering
SoftforkRule -> SoftforkRule -> SoftforkRule
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
min :: SoftforkRule -> SoftforkRule -> SoftforkRule
$cmin :: SoftforkRule -> SoftforkRule -> SoftforkRule
max :: SoftforkRule -> SoftforkRule -> SoftforkRule
$cmax :: SoftforkRule -> SoftforkRule -> SoftforkRule
>= :: SoftforkRule -> SoftforkRule -> Bool
$c>= :: SoftforkRule -> SoftforkRule -> Bool
> :: SoftforkRule -> SoftforkRule -> Bool
$c> :: SoftforkRule -> SoftforkRule -> Bool
<= :: SoftforkRule -> SoftforkRule -> Bool
$c<= :: SoftforkRule -> SoftforkRule -> Bool
< :: SoftforkRule -> SoftforkRule -> Bool
$c< :: SoftforkRule -> SoftforkRule -> Bool
compare :: SoftforkRule -> SoftforkRule -> Ordering
$ccompare :: SoftforkRule -> SoftforkRule -> Ordering
Ord, forall x. Rep SoftforkRule x -> SoftforkRule
forall x. SoftforkRule -> Rep SoftforkRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SoftforkRule x -> SoftforkRule
$cfrom :: forall x. SoftforkRule -> Rep SoftforkRule x
Generic)
  deriving anyclass (SoftforkRule -> ()
forall a. (a -> ()) -> NFData a
rnf :: SoftforkRule -> ()
$crnf :: SoftforkRule -> ()
NFData, Context -> SoftforkRule -> IO (Maybe ThunkInfo)
Proxy SoftforkRule -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy SoftforkRule -> String
$cshowTypeOf :: Proxy SoftforkRule -> String
wNoThunks :: Context -> SoftforkRule -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SoftforkRule -> IO (Maybe ThunkInfo)
noThunks :: Context -> SoftforkRule -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SoftforkRule -> IO (Maybe ThunkInfo)
NoThunks)

instance B.Buildable SoftforkRule where
  build :: SoftforkRule -> Builder
build SoftforkRule
sr =
    forall a. Format Builder a -> a
bprint
      (Format
  (LovelacePortion -> LovelacePortion -> LovelacePortion -> Builder)
  (LovelacePortion -> LovelacePortion -> LovelacePortion -> Builder)
"(init = " forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Buildable a => Format r (a -> r)
build forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (LovelacePortion -> LovelacePortion -> Builder)
  (LovelacePortion -> LovelacePortion -> Builder)
", min = " forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Buildable a => Format r (a -> r)
build forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format (LovelacePortion -> Builder) (LovelacePortion -> Builder)
", decrement = " forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Buildable a => Format r (a -> r)
build forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder Builder
")")
      (SoftforkRule -> LovelacePortion
srInitThd SoftforkRule
sr)
      (SoftforkRule -> LovelacePortion
srMinThd SoftforkRule
sr)
      (SoftforkRule -> LovelacePortion
srThdDecrement SoftforkRule
sr)

-- Used for debugging purposes only
instance Aeson.ToJSON SoftforkRule

instance ToCBOR SoftforkRule where
  toCBOR :: SoftforkRule -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR

instance FromCBOR SoftforkRule where
  fromCBOR :: forall s. Decoder s SoftforkRule
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR SoftforkRule where
  encCBOR :: SoftforkRule -> Encoding
encCBOR SoftforkRule
sr =
    Word -> Encoding
encodeListLen Word
3
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (SoftforkRule -> LovelacePortion
srInitThd SoftforkRule
sr)
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (SoftforkRule -> LovelacePortion
srMinThd SoftforkRule
sr)
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR
        (SoftforkRule -> LovelacePortion
srThdDecrement SoftforkRule
sr)

instance DecCBOR SoftforkRule where
  decCBOR :: forall s. Decoder s SoftforkRule
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"SoftforkRule" Int
3
    LovelacePortion
-> LovelacePortion -> LovelacePortion -> SoftforkRule
SoftforkRule forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR

instance Monad m => ToJSON m SoftforkRule where
  toJSON :: SoftforkRule -> m JSValue
toJSON SoftforkRule
sr =
    forall (m :: * -> *).
Monad m =>
[(JSString, m JSValue)] -> m JSValue
mkObject
      [ (JSString
"initThd", forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON forall a b. (a -> b) -> a -> b
$ SoftforkRule -> LovelacePortion
srInitThd SoftforkRule
sr)
      , (JSString
"minThd", forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON forall a b. (a -> b) -> a -> b
$ SoftforkRule -> LovelacePortion
srMinThd SoftforkRule
sr)
      , (JSString
"thdDecrement", forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON forall a b. (a -> b) -> a -> b
$ SoftforkRule -> LovelacePortion
srThdDecrement SoftforkRule
sr)
      ]

instance MonadError SchemaError m => FromJSON m SoftforkRule where
  fromJSON :: JSValue -> m SoftforkRule
fromJSON JSValue
obj =
    LovelacePortion
-> LovelacePortion -> LovelacePortion -> SoftforkRule
SoftforkRule
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(ReportSchemaErrors m, FromJSON m a) =>
JSValue -> JSString -> m a
fromJSField JSValue
obj JSString
"initThd"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *) a.
(ReportSchemaErrors m, FromJSON m a) =>
JSValue -> JSString -> m a
fromJSField JSValue
obj JSString
"minThd"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *) a.
(ReportSchemaErrors m, FromJSON m a) =>
JSValue -> JSString -> m a
fromJSField JSValue
obj JSString
"thdDecrement"