Skip to content

Commit 6ef1bf9

Browse files
authored
Merge pull request #5552 from IntersectMBO/qnikst/canonical-pots-v0
Introduce CanonicalCoin type
1 parent 28604ab commit 6ef1bf9

File tree

2 files changed

+23
-0
lines changed
  • libs/cardano-ledger-canonical-state

2 files changed

+23
-0
lines changed

libs/cardano-ledger-canonical-state/src/Cardano/Ledger/CanonicalState/BasicTypes.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
77
{-# LANGUAGE KindSignatures #-}
88
{-# LANGUAGE MultiParamTypeClasses #-}
9+
{-# LANGUAGE RecordWildCards #-}
910
{-# LANGUAGE ScopedTypeVariables #-}
1011
{-# LANGUAGE TypeApplications #-}
1112

1213
module Cardano.Ledger.CanonicalState.BasicTypes (
1314
OnChain (..),
1415
DecodeOnChain (..),
16+
CanonicalCoin (..),
1517
) where
1618

19+
import Cardano.Ledger.Coin (Coin (..), CompactForm (CompactCoin))
1720
import Cardano.SCLS.CBOR.Canonical (CanonicalDecoder)
1821
import Cardano.SCLS.CBOR.Canonical.Decoder (FromCanonicalCBOR (..))
1922
import Cardano.SCLS.CBOR.Canonical.Encoder (ToCanonicalCBOR (..))
@@ -55,3 +58,18 @@ instance DecodeOnChain v a => FromCanonicalCBOR v (OnChain a) where
5558
-- `toPlainDecoder`.
5659
class DecodeOnChain (v :: Symbol) (a :: Type) where
5760
decodeOnChain :: BS.ByteString -> CanonicalDecoder s a
61+
62+
-- | Wrapper for the coin type.
63+
--
64+
-- Despite the fact that Coin is on-chain type, we do not want to use
65+
-- 'OnChain' wrapper for it. Because it's expected that if we keep chain
66+
-- structure like transaction in canonical state, then we should keep entire
67+
-- structure there and keep that as a whole, like 'UTxOut'.
68+
newtype CanonicalCoin = CanonicalCoin {unCoin :: CompactForm Coin}
69+
deriving (Eq, Ord, Show, Generic)
70+
71+
instance FromCanonicalCBOR v CanonicalCoin where
72+
fromCanonicalCBOR = fmap (CanonicalCoin . CompactCoin) <$> fromCanonicalCBOR
73+
74+
instance ToCanonicalCBOR v CanonicalCoin where
75+
toCanonicalCBOR v (CanonicalCoin (CompactCoin c)) = toCanonicalCBOR v c

libs/cardano-ledger-canonical-state/testlib/Test/Cardano/Ledger/CanonicalState/Arbitrary.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
module Test.Cardano.Ledger.CanonicalState.Arbitrary () where
99

10+
import Cardano.Ledger.CanonicalState.BasicTypes (CanonicalCoin (..))
1011
import Cardano.Ledger.CanonicalState.Conway ()
1112
import qualified Cardano.Ledger.CanonicalState.Namespace.Blocks.V0 as Blocks.V0
1213
import qualified Cardano.Ledger.CanonicalState.Namespace.UTxO.V0 as UtxoOut.V0
14+
import Cardano.Ledger.Coin (CompactForm (CompactCoin))
1315
import Cardano.Ledger.Core (Era, EraTxOut, TxOut)
1416
import Test.Cardano.Ledger.Conway.Arbitrary ()
1517
import Test.QuickCheck (Arbitrary (..), Positive (..))
@@ -21,3 +23,6 @@ instance Arbitrary Blocks.V0.BlockOut where
2123

2224
instance (EraTxOut era, Arbitrary (TxOut era), Era era) => Arbitrary (UtxoOut.V0.UtxoOut era) where
2325
arbitrary = UtxoOut.V0.mkUtxo <$> arbitrary
26+
27+
instance Arbitrary CanonicalCoin where
28+
arbitrary = CanonicalCoin . CompactCoin <$> arbitrary

0 commit comments

Comments
 (0)