{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.Shelley.ByronTranslation (testGroupByronTranslation) where

import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Chain.UTxO as Byron
import Cardano.Ledger.Address
import Cardano.Ledger.Coin
import qualified Cardano.Ledger.Crypto as CryptoClass
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API.ByronTranslation
import Cardano.Ledger.Shelley.Core
import Test.Cardano.Ledger.Shelley.Arbitrary ()
import Test.Cardano.Ledger.Shelley.ConcreteCryptoTypes (C_Crypto)
import Test.Tasty
import Test.Tasty.QuickCheck

{------------------------------------------------------------------------------
  Top-level tests
------------------------------------------------------------------------------}

testGroupByronTranslation :: TestTree
testGroupByronTranslation :: TestTree
testGroupByronTranslation =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"Translation from Byron to Shelley"
    [ forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"translateTxOut correctness" CompactTxOut -> Property
prop_translateTxOut_correctness
    ]

{------------------------------------------------------------------------------
  Properties
------------------------------------------------------------------------------}

prop_translateTxOut_correctness :: Byron.CompactTxOut -> Property
prop_translateTxOut_correctness :: CompactTxOut -> Property
prop_translateTxOut_correctness CompactTxOut
compactTxOut =
  forall c. Crypto c => TxOut -> TxOut (ShelleyEra c)
translateTxOutByronToShelley
    @C_Crypto
    (CompactTxOut -> TxOut
Byron.fromCompactTxOut CompactTxOut
compactTxOut)
    forall a. (Eq a, Show a) => a -> a -> Property
=== forall c. CompactTxOut -> ShelleyTxOut (ShelleyEra c)
translateCompactTxOutByronToShelley CompactTxOut
compactTxOut

{------------------------------------------------------------------------------
  Reference implementation
------------------------------------------------------------------------------}

translateTxOutByronToShelley ::
  forall c.
  CryptoClass.Crypto c =>
  Byron.TxOut ->
  TxOut (ShelleyEra c)
translateTxOutByronToShelley :: forall c. Crypto c => TxOut -> TxOut (ShelleyEra c)
translateTxOutByronToShelley (Byron.TxOut Address
addr Lovelace
amount) =
  forall era.
(EraTxOut era, HasCallStack) =>
Addr (EraCrypto era) -> Value era -> TxOut era
mkBasicTxOut (Address -> Addr c
translateAddr Address
addr) (Lovelace -> Coin
translateAmount Lovelace
amount)
  where
    translateAmount :: Byron.Lovelace -> Coin
    translateAmount :: Lovelace -> Coin
translateAmount = Integer -> Coin
Coin forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lovelace -> Integer
Byron.lovelaceToInteger

    translateAddr :: Byron.Address -> Addr c
    translateAddr :: Address -> Addr c
translateAddr = forall c. BootstrapAddress c -> Addr c
AddrBootstrap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. Address -> BootstrapAddress c
BootstrapAddress