1+ {-# LANGUAGE TypeApplications #-}
12{-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE DataKinds #-}
34{-# LANGUAGE StandaloneDeriving #-}
1314module Cardano.Ledger.Export.Namespace.UTxO
1415 ( UtxoKey (.. )
1516 , UtxoOut (.. )
16- , AddrUtxoIn (.. )
17- , ScriptUtxoIn (.. )
1817 , Version (.. )
1918 , ToCanonicalCBOR (.. )
2019 , FromCanonicalCBOR (.. )
@@ -26,27 +25,28 @@ import Cardano.SCLS.CBOR.Canonical.Decoder
2625import qualified Codec.CBOR.Encoding as E
2726import qualified Codec.CBOR.Decoding as D
2827import Cardano.Ledger.Conway (ConwayEra )
29- import Cardano.Ledger.TxIn (TxIn (.. ))
28+ import Cardano.Ledger.TxIn (TxIn (.. ), TxId ( .. ) )
3029import Cardano.Ledger.Core (TxOut (.. ))
31- -- import Cardano.Ledger.TxOut (TxOut(..))
3230import Cardano.Ledger.Compactible
3331import Cardano.Ledger.Address
3432import Cardano.Ledger.Credential
3533import Cardano.Ledger.Keys
3634import Cardano.Ledger.Hashes
3735import Cardano.Ledger.Plutus.Data (Datum (.. ))
3836import Cardano.Ledger.Plutus.Data (BinaryData )
39- import Cardano.Ledger.Mary (MaryEra , MaryValue )
37+ import Cardano.Ledger.Mary (MaryValue )
38+ import Cardano.Ledger.Coin (Coin )
39+ import qualified Cardano.Ledger.Coin as Coin
4040import Cardano.SCLS.Internal.Entry
4141import Cardano.SCLS.Internal.Version
4242import Data.Typeable (Typeable )
4343import qualified Cardano.Ledger.Shelley.TxOut as Shelley
4444import qualified Cardano.Ledger.Babbage.TxOut as Babbage
4545import Cardano.Ledger.Allegra.Scripts (Timelock (.. ))
4646import Data.MemPack
47- import Data.Word (Word8 , Word16 )
48- import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra )
49- import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem , AlonzoScript )
47+ import Data.Word (Word8 )
48+ import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra , decodeAddress28 )
49+ import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem , AlonzoScript ( .. ) )
5050
5151-- | Helper that allows us to deriving instances via internal CBOR representation
5252newtype LedgerCBOR (v :: Version ) a = LedgerCBOR { unLedgerCBOR :: a }
@@ -70,76 +70,50 @@ instance (MemPack a) => FromCanonicalCBOR V1 (MemPackCBOR a) where
7070-- | Input wrapper for the keys that are used in utxo namespace
7171data UtxoKey
7272 = UtxoKeyIn TxIn
73- -- | UtxoKeyScript ScriptUtxoIn
73+ deriving ( Show )
7474
7575instance Eq UtxoKey where
7676 (UtxoKeyIn txIn1) == (UtxoKeyIn txIn2) = txIn1 == txIn2
77- -- (UtxoKeyScript script1) == (UtxoKeyScript script2) = undefined script1 script2
78- -- _ == _ = False
7977
8078instance Ord UtxoKey where
8179 compare (UtxoKeyIn txIn1) (UtxoKeyIn txIn2) = compare txIn1 txIn2
82- -- compare (UtxoKeyScript script1) (UtxoKeyScript script2) = undefined script1 script2
83- -- compare (UtxoKeyIn _) (UtxoKeyScript _) = LT
84- -- compare (UtxoKeyScript _) (UtxoKeyIn _) = GT
8580
8681instance IsKey UtxoKey where
8782 keySize = 34
88- packKeyM (UtxoKeyIn (TxIn a b)) = do
89- packM a
83+ packKeyM (UtxoKeyIn (TxIn ( TxId a) b)) = do
84+ packByteStringM (originalBytes a)
9085 packM b
91- -- packKeyM (UtxoKeyScript (ScriptUtxoIn purpose hash)) = do
92- -- undefined purpose hash
9386 unpackKeyM = do
94- a <- unpackM
87+ a <- unpackM -- FIXME read bytestirng and create unsafe hash
9588 b <- unpackM
9689 return $ UtxoKeyIn (TxIn a b)
97- -- toKeyBytes v key = toStrictByteString $ toCanonicalCBOR v key
9890
9991newtype Out = Out (TxOut ConwayEra )
10092 deriving newtype (ToCanonicalCBOR V1 , FromCanonicalCBOR V1 )
10193
102- data AddrUtxoIn = AddrUtxoIn { addrUtxoInAddress :: DataHash32 , addrUtxoInIndex :: Word16 }
103- data ScriptUtxoIn = ScriptUtxoIn { scriptUtxoInPurpose :: AlonzoPlutusPurpose AsItem MaryEra , scriptUtxoInHash :: ScriptHash }
104-
10594-- | Output key that is used in utxo namespace
10695--
10796-- Here we follow the current spec, but after benchmarks we can decide that this representation
10897-- is not efficient and we can replace it with the implementation based on the compact values
10998data UtxoOut
110- = UtxoOutShelley (Shelley. ShelleyTxOut MaryEra )
111- | UtxoOutBabbage (Babbage. BabbageTxOut MaryEra )
99+ = UtxoOutShelley (Shelley. ShelleyTxOut ConwayEra )
100+ | UtxoOutBabbage (Babbage. BabbageTxOut ConwayEra )
112101 | UtxoValue MaryValue
113102
114103instance ToCanonicalCBOR V1 UtxoKey where
115- toCanonicalCBOR v (UtxoKeyIn txIn) = E. encodeTag 0 <> toCanonicalCBOR v txIn
116- -- toCanonicalCBOR v (UtxoKeyScript script) = E.encodeTag 1 <> toCanonicalCBOR v script
117-
118-
119- instance ToCanonicalCBOR V1 AddrUtxoIn where
120- toCanonicalCBOR v (AddrUtxoIn addr idx) = toCanonicalCBOR v (addr, idx)
121-
122- instance FromCanonicalCBOR V1 AddrUtxoIn where
123- fromCanonicalCBOR = fmap (uncurry AddrUtxoIn ) <$> fromCanonicalCBOR
124-
125- instance ToCanonicalCBOR V1 ScriptUtxoIn where
126- toCanonicalCBOR v (ScriptUtxoIn purpose hash) = toCanonicalCBOR v (purpose, hash)
127-
128- instance FromCanonicalCBOR V1 ScriptUtxoIn where
129- fromCanonicalCBOR = fmap (uncurry ScriptUtxoIn ) <$> fromCanonicalCBOR
104+ toCanonicalCBOR v (UtxoKeyIn txIn) = E. encodeInt 0 <> toCanonicalCBOR v txIn
130105
131106instance FromCanonicalCBOR V1 UtxoKey where
132107 fromCanonicalCBOR = do
133108 tag <- fromCanonicalCBOR
134109 case unVer tag :: Word8 of
135110 0 -> fmap UtxoKeyIn <$> fromCanonicalCBOR
136- -- 1 -> fmap UtxoKeyScript <$> fromCanonicalCBOR
137111 _ -> fail " Unknown UtxoKey tag"
138112
139113instance ToCanonicalCBOR V1 UtxoOut where
140- toCanonicalCBOR v (UtxoOutShelley shelleyOut) = E. encodeTag 1 <> toCanonicalCBOR v shelleyOut
141- toCanonicalCBOR v (UtxoOutBabbage babbageOut) = E. encodeTag 2 <> toCanonicalCBOR v babbageOut
142- toCanonicalCBOR v (UtxoValue value) = E. encodeTag 3 <> toCanonicalCBOR v value
114+ toCanonicalCBOR v (UtxoOutShelley shelleyOut) = toCanonicalCBOR v ( E. encodeInt 0 , toCanonicalCBOR v shelleyOut)
115+ toCanonicalCBOR v (UtxoOutBabbage babbageOut) = toCanonicalCBOR v ( E. encodeInt 1 , toCanonicalCBOR v babbageOut)
116+ toCanonicalCBOR v (UtxoValue value) = toCanonicalCBOR v ( E. encodeInt 2 , toCanonicalCBOR v value)
143117
144118instance FromCanonicalCBOR V1 UtxoOut where
145119 fromCanonicalCBOR = do
@@ -151,12 +125,47 @@ instance FromCanonicalCBOR V1 UtxoOut where
151125 t -> fail $ " Unknown UtxoOut tag: " <> show t
152126
153127instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut ConwayEra ) where
154- toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeTag 0 <> toCanonicalCBOR v (cAddr, form)
155- toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = E. encodeTag 1 <> toCanonicalCBOR v (cAddr, form, dataHash)
156- toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = E. encodeTag 2 <> toCanonicalCBOR v (cAddr, form, inlineDatum)
157- toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = E. encodeTag 3 <> toCanonicalCBOR v (cAddr, form, datum, script)
158- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = E. encodeTag 4 <> toCanonicalCBOR v (staking, hash28, compact)
159- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = E. encodeTag 5 <> toCanonicalCBOR v (staking, hash28, compact, dataHash)
128+ toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) =
129+ E. encodeMapLen 2
130+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
131+ <> E. encodeInt 1 <> toCanonicalCBOR v form
132+ toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form datum) =
133+ E. encodeMapLen 3
134+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
135+ <> E. encodeInt 1 <> toCanonicalCBOR v form
136+ <> E. encodeInt 2
137+ <> case datum of
138+ hash_ -> toCanonicalCBOR v (0 :: Int , originalBytes hash_ )
139+ toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) =
140+ E. encodeMapLen 3
141+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
142+ <> E. encodeInt 1 <> toCanonicalCBOR v form
143+ <> E. encodeInt 2
144+ <> case inlineDatum of
145+ binaryData -> toCanonicalCBOR v (1 :: Int , toCanonicalCBOR v (LedgerCBOR @ V1 binaryData ))
146+ toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) =
147+ let datumEncoding = case datum of
148+ NoDatum -> (Nothing )
149+ DatumHash dh -> Just (toCanonicalCBOR v (0 :: Int , originalBytes dh ))
150+ Datum binaryData -> Just (toCanonicalCBOR v (1 :: Int , toCanonicalCBOR v (LedgerCBOR @ V1 binaryData )))
151+ in E. encodeMapLen (3 + (case datumEncoding of Just {} -> 1 ; Nothing -> 0 ))
152+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
153+ <> E. encodeInt 1 <> toCanonicalCBOR v form
154+ <> case datumEncoding of
155+ Nothing -> mempty
156+ Just enc -> E. encodeInt 2 <> enc
157+ <> E. encodeInt 3 <> toCanonicalCBOR v (LedgerCBOR @ V1 script)
158+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compactForm) =
159+ let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28))
160+ in E. encodeMapLen 2
161+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
162+ <> E. encodeInt 1 <> toCanonicalCBOR v compactForm
163+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) =
164+ let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28))
165+ in E. encodeMapLen 3
166+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
167+ <> E. encodeInt 1 <> toCanonicalCBOR v compact
168+ <> E. encodeInt 2 <> toCanonicalCBOR v (0 :: Int , dataHash )
160169
161170instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut ConwayEra ) where
162171 fromCanonicalCBOR = do
@@ -169,27 +178,7 @@ instance FromCanonicalCBOR V1 (Babbage.BabbageTxOut ConwayEra) where
169178 5 -> fmap (\ (a,b,c,d) -> Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 a b c d) <$> fromCanonicalCBOR
170179 t -> fail $ " Unknown BabbageTxOut tag: " <> show t
171180
172- instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
173- toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeTag 0 <> toCanonicalCBOR v (cAddr, form)
174- toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = E. encodeTag 1 <> toCanonicalCBOR v (cAddr, form, dataHash)
175- toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = E. encodeTag 2 <> toCanonicalCBOR v (cAddr, form, inlineDatum)
176- toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = E. encodeTag 3 <> toCanonicalCBOR v (cAddr, form, datum, script)
177- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = E. encodeTag 4 <> toCanonicalCBOR v (staking, hash28, compact)
178- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = E. encodeTag 5 <> toCanonicalCBOR v (staking, hash28, compact, dataHash)
179-
180- instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
181- fromCanonicalCBOR = do
182- D. decodeTag >>= \ case
183- 0 -> fmap (\ (c, f) -> Babbage. TxOutCompact' c f) <$> fromCanonicalCBOR
184- 1 -> fmap (\ (a,b,c) -> Babbage. TxOutCompactDH' a b c) <$> fromCanonicalCBOR
185- 2 -> fmap (\ (a,b,c) -> Babbage. TxOutCompactDatum a b c) <$> fromCanonicalCBOR
186- 3 -> fmap (\ (a,b,c,d) -> Babbage. TxOutCompactRefScript a b c d) <$> fromCanonicalCBOR
187- 4 -> fmap (\ (a,b,c) -> Babbage. TxOut_AddrHash28_AdaOnly a b c) <$> fromCanonicalCBOR
188- 5 -> fmap (\ (a,b,c,d) -> Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 a b c d) <$> fromCanonicalCBOR
189- t -> fail $ " Unknown BabbageTxOut tag: " <> show t
190-
191-
192- instance Typeable kr => ToCanonicalCBOR V1 (Credential kr ) where
181+ instance ToCanonicalCBOR V1 (Credential kr ) where
193182 toCanonicalCBOR v (ScriptHashObj sh) = toCanonicalCBOR v (0 :: Word8 , sh )
194183 toCanonicalCBOR v (KeyHashObj kh) = toCanonicalCBOR v (1 :: Word8 , kh )
195184
@@ -201,15 +190,22 @@ instance Typeable kr => FromCanonicalCBOR V1 (Credential kr) where
201190 1 -> fmap KeyHashObj <$> fromCanonicalCBOR
202191 x -> fail $ " Unknown Credential tag: " <> show x
203192
204- deriving via (LedgerCBOR v (Shelley. ShelleyTxOut MaryEra )) instance ToCanonicalCBOR v (Shelley. ShelleyTxOut MaryEra )
205- deriving via (LedgerCBOR v (Shelley. ShelleyTxOut MaryEra )) instance FromCanonicalCBOR v (Shelley. ShelleyTxOut MaryEra )
206- deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem MaryEra )) instance ToCanonicalCBOR v (AlonzoPlutusPurpose AsItem MaryEra )
207- deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem MaryEra )) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem MaryEra )
193+ deriving via (LedgerCBOR v (Shelley. ShelleyTxOut ConwayEra )) instance ToCanonicalCBOR v (Shelley. ShelleyTxOut ConwayEra )
194+ deriving via (LedgerCBOR v (Shelley. ShelleyTxOut ConwayEra )) instance FromCanonicalCBOR v (Shelley. ShelleyTxOut ConwayEra )
208195deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )) instance ToCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )
209196deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )
210197deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance ToCanonicalCBOR V1 (AlonzoScript ConwayEra )
211198deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance FromCanonicalCBOR V1 (AlonzoScript ConwayEra )
212- deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => ToCanonicalCBOR V1 (CompactForm a )
199+ -- deriving via (MemPackCBOR (CompactForm a)) instance {-# OVERLAPPABLE #-} (MemPack (CompactForm a)) => ToCanonicalCBOR V1 (CompactForm a)
200+
201+ deriving via (LedgerCBOR v MaryValue ) instance ToCanonicalCBOR v MaryValue
202+ deriving via (LedgerCBOR v MaryValue ) instance FromCanonicalCBOR v MaryValue
203+ instance {-# OVERLAPPING #-} ToCanonicalCBOR version (CompactForm MaryValue ) where
204+ toCanonicalCBOR version v = toCanonicalCBOR version (fromCompact v)
205+
206+ instance {-# OVERLAPPING #-} ToCanonicalCBOR v (CompactForm Coin ) where
207+ toCanonicalCBOR v (Coin. CompactCoin ci) = toCanonicalCBOR v ci
208+
213209deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => FromCanonicalCBOR V1 (CompactForm a )
214210deriving via (MemPackCBOR CompactAddr ) instance FromCanonicalCBOR V1 CompactAddr
215211deriving via (MemPackCBOR CompactAddr ) instance ToCanonicalCBOR V1 CompactAddr
@@ -219,22 +215,18 @@ deriving via (LedgerCBOR v TxIn) instance FromCanonicalCBOR v TxIn
219215deriving via (LedgerCBOR v TxIn ) instance ToCanonicalCBOR v TxIn
220216deriving via (MemPackCBOR DataHash32 ) instance FromCanonicalCBOR V1 DataHash32
221217deriving via (MemPackCBOR DataHash32 ) instance ToCanonicalCBOR V1 DataHash32
222- deriving via (MemPackCBOR (Timelock MaryEra )) instance ToCanonicalCBOR V1 (Timelock MaryEra )
223- deriving via (MemPackCBOR (Timelock MaryEra )) instance FromCanonicalCBOR V1 (Timelock MaryEra )
224- deriving via (LedgerCBOR v MaryValue ) instance ToCanonicalCBOR v MaryValue
225- deriving via (LedgerCBOR v MaryValue ) instance FromCanonicalCBOR v MaryValue
218+ deriving via (MemPackCBOR (Timelock ConwayEra )) instance ToCanonicalCBOR V1 (Timelock ConwayEra )
219+ deriving via (MemPackCBOR (Timelock ConwayEra )) instance FromCanonicalCBOR V1 (Timelock ConwayEra )
220+ -- deriving via (LedgerCBOR v MaryValue) instance ToCanonicalCBOR v MaryValue
221+ -- deriving via (LedgerCBOR v MaryValue) instance FromCanonicalCBOR v MaryValue
226222
227223
228- deriving via (LedgerCBOR v (KeyHash kr )) instance Typeable kr => ToCanonicalCBOR v (KeyHash kr )
224+ deriving via (LedgerCBOR v (KeyHash kr )) instance ToCanonicalCBOR v (KeyHash kr )
229225deriving via (LedgerCBOR v (KeyHash kr )) instance Typeable kr => FromCanonicalCBOR v (KeyHash kr )
230226deriving via (LedgerCBOR v (ScriptHash )) instance FromCanonicalCBOR v ScriptHash
231227deriving via (LedgerCBOR v (ScriptHash )) instance ToCanonicalCBOR v ScriptHash
232- deriving via (LedgerCBOR v (Datum MaryEra )) instance ToCanonicalCBOR v (Datum MaryEra )
233- deriving via (LedgerCBOR v (Datum MaryEra )) instance FromCanonicalCBOR v (Datum MaryEra )
234228deriving via (LedgerCBOR v (Datum ConwayEra )) instance ToCanonicalCBOR v (Datum ConwayEra )
235229deriving via (LedgerCBOR v (Datum ConwayEra )) instance FromCanonicalCBOR v (Datum ConwayEra )
236- deriving via (LedgerCBOR v (BinaryData MaryEra )) instance ToCanonicalCBOR v (BinaryData MaryEra )
237- deriving via (LedgerCBOR v (BinaryData MaryEra )) instance FromCanonicalCBOR v (BinaryData MaryEra )
238230deriving via (LedgerCBOR v (BinaryData ConwayEra )) instance ToCanonicalCBOR v (BinaryData ConwayEra )
239231deriving via (LedgerCBOR v (BinaryData ConwayEra )) instance FromCanonicalCBOR v (BinaryData ConwayEra )
240232deriving via (LedgerCBOR v (SafeHash EraIndependentData )) instance ToCanonicalCBOR v ((SafeHash EraIndependentData ))
0 commit comments