{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | Tools for reporting things in readable manner. Used in Rules to implement
--   STS 'renderAssertionViolation' methods, and in Tests.
module Cardano.Ledger.Shelley.Rules.Reports (
  showCred,
  showIR,
  showKeyHash,
  showListy,
  showMap,
  showWithdrawal,
  showSafeHash,
  synopsisCoinMap,
  showTxCerts,
  produceEqualsConsumed,
)
where

import Cardano.Ledger.CertState (
  CertState (..),
  InstantaneousRewards (..),
 )
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.Keys (KeyHash (..))
import Cardano.Ledger.SafeHash (SafeHash, extractHash)
import Cardano.Ledger.Shelley.AdaPots (consumedTxBody, producedTxBody)
import Cardano.Ledger.Shelley.TxBody (RewardAccount (..), Withdrawals (..))
import Cardano.Ledger.UTxO (UTxO (..))
import Data.Foldable (fold, toList)
import qualified Data.Map.Strict as Map
import Lens.Micro ((^.))

-- ===============================================
-- Reporting Certificates

showCred :: Credential x c -> String
showCred :: forall (x :: KeyRole) c. Credential x c -> String
showCred (ScriptHashObj (ScriptHash Hash (ADDRHASH c) EraIndependentScript
x)) = forall a. Show a => a -> String
show Hash (ADDRHASH c) EraIndependentScript
x
showCred (KeyHashObj (KeyHash Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c))
x)) = forall a. Show a => a -> String
show Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c))
x

showKeyHash :: KeyHash c x -> String
showKeyHash :: forall (c :: KeyRole) x. KeyHash c x -> String
showKeyHash (KeyHash Hash (ADDRHASH x) (VerKeyDSIGN (DSIGN x))
hash) = forall a. Int -> [a] -> [a]
take Int
10 (forall a. Show a => a -> String
show Hash (ADDRHASH x) (VerKeyDSIGN (DSIGN x))
hash)

showCerts :: Show (TxCert era) => [TxCert era] -> String
showCerts :: forall era. Show (TxCert era) => [TxCert era] -> String
showCerts [TxCert era]
certs = [String] -> String
unlines (forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) [TxCert era]
certs)

showTxCerts :: EraTxBody era => TxBody era -> String
showTxCerts :: forall era. EraTxBody era => TxBody era -> String
showTxCerts TxBody era
txb = forall era. Show (TxCert era) => [TxCert era] -> String
showCerts (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (TxBody era
txb forall s a. s -> Getting a s a -> a
^. forall era.
EraTxBody era =>
Lens' (TxBody era) (StrictSeq (TxCert era))
certsTxBodyL))

-- | Display a synopsis of a map to Coin
synopsisCoinMap :: Maybe (Map.Map k Coin) -> String
synopsisCoinMap :: forall k. Maybe (Map k Coin) -> String
synopsisCoinMap (Just Map k Coin
m) = String
"Count = " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k a. Map k a -> Int
Map.size Map k Coin
m) forall a. [a] -> [a] -> [a]
++ String
",  total = " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Map k Coin
m)
synopsisCoinMap Maybe (Map k Coin)
Nothing = String
"SYNOPSIS NOTHING"

-- ===============================================
-- Printing Produced == Consumed

produceEqualsConsumed ::
  EraTxBody era =>
  PParams era ->
  CertState era ->
  UTxO era ->
  TxBody era ->
  String
produceEqualsConsumed :: forall era.
EraTxBody era =>
PParams era -> CertState era -> UTxO era -> TxBody era -> String
produceEqualsConsumed PParams era
pp CertState era
dpstate UTxO era
utxo TxBody era
txb =
  let consumedValue :: Consumed
consumedValue = forall era.
EraTxBody era =>
TxBody era -> PParams era -> CertState era -> UTxO era -> Consumed
consumedTxBody TxBody era
txb PParams era
pp CertState era
dpstate UTxO era
utxo
      producedValue :: Produced
producedValue = forall era.
EraTxBody era =>
TxBody era -> PParams era -> CertState era -> Produced
producedTxBody TxBody era
txb PParams era
pp CertState era
dpstate
   in ( String
"\n (Produce = Consumed) Report\n  certificates\n"
          forall a. [a] -> [a] -> [a]
++ forall era. EraTxBody era => TxBody era -> String
showTxCerts TxBody era
txb
          forall a. [a] -> [a] -> [a]
++ String
"\n  withdrawals "
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Withdrawals c -> Map (RewardAccount c) Coin
unWithdrawals forall a b. (a -> b) -> a -> b
$ TxBody era
txb forall s a. s -> Getting a s a -> a
^. forall era.
EraTxBody era =>
Lens' (TxBody era) (Withdrawals (EraCrypto era))
withdrawalsTxBodyL)
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Produced
producedValue
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Consumed
consumedValue
      )

-- ========================

showMap :: (t1 -> [Char]) -> (t2 -> [Char]) -> Map.Map t1 t2 -> String
showMap :: forall t1 t2.
(t1 -> String) -> (t2 -> String) -> Map t1 t2 -> String
showMap t1 -> String
showKey t2 -> String
showVal Map t1 t2
m = [String] -> String
unlines (forall a b. (a -> b) -> [a] -> [b]
map (t1, t2) -> String
showpair (forall k a. Map k a -> [(k, a)]
Map.toList Map t1 t2
m))
  where
    showpair :: (t1, t2) -> String
showpair (t1
key, t2
val) = t1 -> String
showKey t1
key forall a. [a] -> [a] -> [a]
++ String
" -> " forall a. [a] -> [a] -> [a]
++ t2 -> String
showVal t2
val

showListy :: Foldable t => (a -> String) -> t a -> String
showListy :: forall (t :: * -> *) a.
Foldable t =>
(a -> String) -> t a -> String
showListy a -> String
showElem t a
list = [String] -> String
unlines (forall a b. (a -> b) -> [a] -> [b]
map a -> String
showElem (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList t a
list))

showRewardAcct :: RewardAccount c -> [Char]
showRewardAcct :: forall c. RewardAccount c -> String
showRewardAcct (RewardAccount {raNetwork :: forall c. RewardAccount c -> Network
raNetwork = Network
network, raCredential :: forall c. RewardAccount c -> Credential 'Staking c
raCredential = Credential 'Staking c
cred}) =
  forall a. Show a => a -> String
show Network
network forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ forall (x :: KeyRole) c. Credential x c -> String
showCred Credential 'Staking c
cred

showWithdrawal :: Withdrawals c -> String
showWithdrawal :: forall c. Withdrawals c -> String
showWithdrawal (Withdrawals Map (RewardAccount c) Coin
m) = forall t1 t2.
(t1 -> String) -> (t2 -> String) -> Map t1 t2 -> String
showMap ((String
"   " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. RewardAccount c -> String
showRewardAcct) forall a. Show a => a -> String
show Map (RewardAccount c) Coin
m

showIR :: InstantaneousRewards c -> String
showIR :: forall c. InstantaneousRewards c -> String
showIR (InstantaneousRewards Map (Credential 'Staking c) Coin
m Map (Credential 'Staking c) Coin
n DeltaCoin
x DeltaCoin
y) =
  [String] -> String
unlines
    [ String
"IRReseves " forall a. [a] -> [a] -> [a]
++ forall t1 t2.
(t1 -> String) -> (t2 -> String) -> Map t1 t2 -> String
showMap ((String
"   " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
10 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: KeyRole) c. Credential x c -> String
showCred) forall a. Show a => a -> String
show Map (Credential 'Staking c) Coin
m
    , String
"IRTreasury " forall a. [a] -> [a] -> [a]
++ forall t1 t2.
(t1 -> String) -> (t2 -> String) -> Map t1 t2 -> String
showMap ((String
"   " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
10 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (x :: KeyRole) c. Credential x c -> String
showCred) forall a. Show a => a -> String
show Map (Credential 'Staking c) Coin
n
    , String
"DeltaReserves " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show DeltaCoin
x
    , String
"DeltaTreasury " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show DeltaCoin
y
    ]

showSafeHash :: SafeHash c i -> String
showSafeHash :: forall c i. SafeHash c i -> String
showSafeHash SafeHash c i
x = forall a. Int -> [a] -> [a]
take Int
12 (forall a. Show a => a -> String
show (forall c i. SafeHash c i -> Hash (HASH c) i
extractHash SafeHash c i
x))