diff --git a/cabal.project b/cabal.project index a956fa34ee5..f18bdf174a3 100644 --- a/cabal.project +++ b/cabal.project @@ -16,7 +16,7 @@ source-repository-package subdir: hs -- !WARNING!: -- MAKE SURE THIS POINTS TO A COMMIT IN `*-artifacts` BEFORE MERGE! - tag: 7af1d3dbe03721048265591b12b857705f5c2577 + tag: d84538c1ad6d8dc3cff4e59ead574daf84c88117 source-repository-package type: git @@ -96,3 +96,21 @@ if impl(ghc >=9.12) allow-newer: -- Unique: https://github.com/kapralVV/Unique/issues/11 , Unique:hashable + +source-repository-package + type: git + location: https://github.com/tweag/cardano-canonical-ledger.git + subdir: scls-cbor + tag: 13672a64a59d3e8f4b3d7549472ae3cccbdaa3ba + +source-repository-package + type: git + location: https://github.com/tweag/cardano-canonical-ledger.git + subdir: scls-format + tag: 13672a64a59d3e8f4b3d7549472ae3cccbdaa3ba + +source-repository-package + type: git + location: https://github.com/tweag/cardano-canonical-ledger.git + subdir: merkle-tree-incremental + tag: 13672a64a59d3e8f4b3d7549472ae3cccbdaa3ba \ No newline at end of file diff --git a/eras/alonzo/impl/cardano-ledger-alonzo.cabal b/eras/alonzo/impl/cardano-ledger-alonzo.cabal index 07eacce227c..dce313359cd 100644 --- a/eras/alonzo/impl/cardano-ledger-alonzo.cabal +++ b/eras/alonzo/impl/cardano-ledger-alonzo.cabal @@ -83,7 +83,7 @@ library base64-bytestring, bytestring, cardano-crypto-class, - cardano-data ^>=1.2.1, + cardano-data ^>=1.3, cardano-ledger-allegra ^>=1.9, cardano-ledger-binary ^>=1.8, cardano-ledger-core:{cardano-ledger-core, internal} ^>=1.19, diff --git a/eras/conway/impl/cardano-ledger-conway.cabal b/eras/conway/impl/cardano-ledger-conway.cabal index 78f6905b73a..c2d4c217195 100644 --- a/eras/conway/impl/cardano-ledger-conway.cabal +++ b/eras/conway/impl/cardano-ledger-conway.cabal @@ -95,7 +95,7 @@ library aeson >=2.2, base >=4.18 && <5, cardano-crypto-class, - cardano-data >=1.2.3, + cardano-data >=1.3, cardano-ledger-allegra ^>=1.9, cardano-ledger-alonzo ^>=1.15, cardano-ledger-babbage ^>=1.13, @@ -208,6 +208,54 @@ library testlib time, tree-diff, +library scls-export + exposed-modules: + Cardano.Ledger.Export.Namespace.UTxO + visibility: public + hs-source-dirs: scls-export/src + default-language: Haskell2010 + ghc-options: + -Wall + -Wcompat + -Wincomplete-record-updates + -Wincomplete-uni-patterns + -Wpartial-fields + -Wredundant-constraints + -Wunused-packages + + build-depends: + -- cardano-data:{cardano-data}, + cardano-ledger-allegra, + cardano-ledger-alonzo:{cardano-ledger-alonzo}, + cardano-ledger-babbage:{cardano-ledger-babbage}, + cardano-ledger-binary:{cardano-ledger-binary}, + -- cardano-ledger-byron:{cardano-ledger-byron}, + cardano-ledger-conway:{cardano-ledger-conway}, + cardano-ledger-core:{cardano-ledger-core}, + cardano-ledger-mary:{cardano-ledger-mary}, + cardano-ledger-shelley:{cardano-ledger-shelley}, + -- cardano-slotting:{cardano-slotting}, + -- cardano-strict-containers, + cborg, + -- containers, + -- cuddle >=0.4, + -- data-default, + -- deepseq, + -- generic-random, + -- heredoc, + -- kmicrolens, + mempack, + -- microlens-mtl, + -- mtl, + -- plutus-ledger-api, + -- prettyprinter, + -- small-steps >=1.1, + -- text, + base, + scls-cbor, + scls-format, + -- time + executable huddle-cddl main-is: Main.hs hs-source-dirs: huddle-cddl diff --git a/eras/conway/impl/scls-export/src/Cardano/Ledger/Export/Namespace/UTxO.hs b/eras/conway/impl/scls-export/src/Cardano/Ledger/Export/Namespace/UTxO.hs new file mode 100644 index 00000000000..daf7a830387 --- /dev/null +++ b/eras/conway/impl/scls-export/src/Cardano/Ledger/Export/Namespace/UTxO.hs @@ -0,0 +1,236 @@ +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# OPTIONS_GHC -Wno-orphans #-} +-- | UTxO namespace export. +module Cardano.Ledger.Export.Namespace.UTxO + ( UtxoKey(..) + , UtxoOut(..) + , Version(..) + , ToCanonicalCBOR(..) + , FromCanonicalCBOR(..) + ) where + +import Cardano.SCLS.CBOR.Canonical.Encoder +import Cardano.Ledger.Binary (decodeMemPack, encodeMemPack, EncCBOR(..), DecCBOR(..), toPlainEncoding, shelleyProtVer, toPlainDecoder) +import Cardano.SCLS.CBOR.Canonical.Decoder +import qualified Codec.CBOR.Encoding as E +import qualified Codec.CBOR.Decoding as D +import Cardano.Ledger.Conway (ConwayEra) +import Cardano.Ledger.TxIn (TxIn(..), TxId(..)) +import Cardano.Ledger.Core (TxOut(..)) +import Cardano.Ledger.Compactible +import Cardano.Ledger.Address +import Cardano.Ledger.Credential +import Cardano.Ledger.Keys +import Cardano.Ledger.Hashes +import Cardano.Ledger.Plutus.Data (Datum(..)) +import Cardano.Ledger.Plutus.Data (BinaryData) +import Cardano.Ledger.Mary (MaryValue) +import Cardano.Ledger.Coin (Coin) +import qualified Cardano.Ledger.Coin as Coin +import Cardano.SCLS.Internal.Entry +import Cardano.SCLS.Internal.Version +import Data.Typeable (Typeable) +import qualified Cardano.Ledger.Shelley.TxOut as Shelley +import qualified Cardano.Ledger.Babbage.TxOut as Babbage +import Cardano.Ledger.Allegra.Scripts (Timelock(..)) +import Data.MemPack +import Data.Word (Word8) +import Cardano.Ledger.Alonzo.TxOut (DataHash32, Addr28Extra, decodeAddress28) +import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose, AsItem, AlonzoScript(..)) + +-- | Helper that allows us to deriving instances via internal CBOR representation +newtype LedgerCBOR (v::Version) a = LedgerCBOR { unLedgerCBOR :: a } + deriving (Eq, Show) + +instance EncCBOR a => ToCanonicalCBOR v (LedgerCBOR v a) where + toCanonicalCBOR _v (LedgerCBOR a) = toPlainEncoding shelleyProtVer (encCBOR a) + +instance DecCBOR a => FromCanonicalCBOR v (LedgerCBOR v a) where + fromCanonicalCBOR = Versioned . LedgerCBOR <$> toPlainDecoder Nothing shelleyProtVer decCBOR + +newtype MemPackCBOR a = MemPackCBOR { unMemPackCBOR :: a } + deriving (Eq, Show) + +instance (MemPack a) => ToCanonicalCBOR V1 (MemPackCBOR a) where + toCanonicalCBOR _v (MemPackCBOR a) = toPlainEncoding shelleyProtVer (encodeMemPack a) + +instance (MemPack a) => FromCanonicalCBOR V1 (MemPackCBOR a) where + fromCanonicalCBOR = Versioned . MemPackCBOR <$> toPlainDecoder Nothing shelleyProtVer decodeMemPack + +-- | Input wrapper for the keys that are used in utxo namespace +data UtxoKey + = UtxoKeyIn TxIn + deriving (Show) + +instance Eq UtxoKey where + (UtxoKeyIn txIn1) == (UtxoKeyIn txIn2) = txIn1 == txIn2 + +instance Ord UtxoKey where + compare (UtxoKeyIn txIn1) (UtxoKeyIn txIn2) = compare txIn1 txIn2 + +instance IsKey UtxoKey where + keySize = 34 + packKeyM (UtxoKeyIn (TxIn (TxId a) b)) = do + packByteStringM (originalBytes a) + packM b + unpackKeyM = do + a <- unpackM -- FIXME read bytestirng and create unsafe hash + b <- unpackM + return $ UtxoKeyIn (TxIn a b) + +newtype Out = Out (TxOut ConwayEra) + deriving newtype (ToCanonicalCBOR V1, FromCanonicalCBOR V1) + +-- | Output key that is used in utxo namespace +-- +-- Here we follow the current spec, but after benchmarks we can decide that this representation +-- is not efficient and we can replace it with the implementation based on the compact values +data UtxoOut + = UtxoOutShelley (Shelley.ShelleyTxOut ConwayEra) + | UtxoOutBabbage (Babbage.BabbageTxOut ConwayEra) + | UtxoValue MaryValue + +instance ToCanonicalCBOR V1 UtxoKey where + toCanonicalCBOR v (UtxoKeyIn txIn) = E.encodeInt 0 <> toCanonicalCBOR v txIn + +instance FromCanonicalCBOR V1 UtxoKey where + fromCanonicalCBOR = do + tag <- fromCanonicalCBOR + case unVer tag :: Word8 of + 0 -> fmap UtxoKeyIn <$> fromCanonicalCBOR + _ -> fail "Unknown UtxoKey tag" + +instance ToCanonicalCBOR V1 UtxoOut where + toCanonicalCBOR v (UtxoOutShelley shelleyOut) = toCanonicalCBOR v (E.encodeInt 0, toCanonicalCBOR v shelleyOut) + toCanonicalCBOR v (UtxoOutBabbage babbageOut) = toCanonicalCBOR v (E.encodeInt 1, toCanonicalCBOR v babbageOut) + toCanonicalCBOR v (UtxoValue value) = toCanonicalCBOR v (E.encodeInt 2, toCanonicalCBOR v value) + +instance FromCanonicalCBOR V1 UtxoOut where + fromCanonicalCBOR = do + tag <- fromCanonicalCBOR + case unVer tag :: Word8 of + 1 -> fmap UtxoOutShelley <$> fromCanonicalCBOR + 2 -> fmap UtxoOutBabbage <$> fromCanonicalCBOR + 3 -> fmap UtxoValue <$> fromCanonicalCBOR + t -> fail $ "Unknown UtxoOut tag: " <> show t + +instance ToCanonicalCBOR V1 (Babbage.BabbageTxOut ConwayEra) where + toCanonicalCBOR v (Babbage.TxOutCompact' cAddr form) = + E.encodeMapLen 2 + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v form + toCanonicalCBOR v (Babbage.TxOutCompactDH' cAddr form datum) = + E.encodeMapLen 3 + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v form + <> E.encodeInt 2 + <> case datum of + hash_ -> toCanonicalCBOR v (0::Int, originalBytes hash_) + toCanonicalCBOR v (Babbage.TxOutCompactDatum cAddr form inlineDatum) = + E.encodeMapLen 3 + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v form + <> E.encodeInt 2 + <> case inlineDatum of + binaryData -> toCanonicalCBOR v (1::Int, toCanonicalCBOR v (LedgerCBOR @V1 binaryData)) + toCanonicalCBOR v (Babbage.TxOutCompactRefScript cAddr form datum script) = + let datumEncoding = case datum of + NoDatum -> (Nothing) + DatumHash dh -> Just (toCanonicalCBOR v (0::Int, originalBytes dh)) + Datum binaryData -> Just (toCanonicalCBOR v (1:: Int, toCanonicalCBOR v (LedgerCBOR @V1 binaryData))) + in E.encodeMapLen (3 + (case datumEncoding of Just{} -> 1 ; Nothing -> 0)) + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v form + <> case datumEncoding of + Nothing -> mempty + Just enc -> E.encodeInt 2 <> enc + <> E.encodeInt 3 <> toCanonicalCBOR v (LedgerCBOR @V1 script) + toCanonicalCBOR v (Babbage.TxOut_AddrHash28_AdaOnly staking hash28 compactForm) = + let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28)) + in E.encodeMapLen 2 + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v compactForm + toCanonicalCBOR v (Babbage.TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = + let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28)) + in E.encodeMapLen 3 + <> E.encodeInt 0 <> toCanonicalCBOR v cAddr + <> E.encodeInt 1 <> toCanonicalCBOR v compact + <> E.encodeInt 2 <> toCanonicalCBOR v (0::Int, dataHash) + +instance FromCanonicalCBOR V1 (Babbage.BabbageTxOut ConwayEra) where + fromCanonicalCBOR = do + D.decodeTag >>= \case + 0 -> fmap (\(c, f) -> Babbage.TxOutCompact' c f) <$> fromCanonicalCBOR + 1 -> fmap (\(a,b,c) -> Babbage.TxOutCompactDH' a b c) <$> fromCanonicalCBOR + 2 -> fmap (\(a,b,c) -> Babbage.TxOutCompactDatum a b c) <$> fromCanonicalCBOR + 3 -> fmap (\(a,b,c,d) -> Babbage.TxOutCompactRefScript a b c d) <$> fromCanonicalCBOR + 4 -> fmap (\(a,b,c) -> Babbage.TxOut_AddrHash28_AdaOnly a b c) <$> fromCanonicalCBOR + 5 -> fmap (\(a,b,c,d) -> Babbage.TxOut_AddrHash28_AdaOnly_DataHash32 a b c d) <$> fromCanonicalCBOR + t -> fail $ "Unknown BabbageTxOut tag: " <> show t + +instance ToCanonicalCBOR V1 (Credential kr) where + toCanonicalCBOR v (ScriptHashObj sh) = toCanonicalCBOR v (0::Word8, sh) + toCanonicalCBOR v (KeyHashObj kh) = toCanonicalCBOR v (1::Word8, kh) + +instance Typeable kr => FromCanonicalCBOR V1 (Credential kr) where + fromCanonicalCBOR = do + tag <- fromCanonicalCBOR + case unVer tag :: Word8 of + 0 -> fmap ScriptHashObj <$> fromCanonicalCBOR + 1 -> fmap KeyHashObj <$> fromCanonicalCBOR + x -> fail $ "Unknown Credential tag: " <> show x + +deriving via (LedgerCBOR v (Shelley.ShelleyTxOut ConwayEra)) instance ToCanonicalCBOR v (Shelley.ShelleyTxOut ConwayEra) +deriving via (LedgerCBOR v (Shelley.ShelleyTxOut ConwayEra)) instance FromCanonicalCBOR v (Shelley.ShelleyTxOut ConwayEra) +deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra)) instance ToCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra) +deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra)) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra) +deriving via (MemPackCBOR (AlonzoScript ConwayEra)) instance ToCanonicalCBOR V1 (AlonzoScript ConwayEra) +deriving via (MemPackCBOR (AlonzoScript ConwayEra)) instance FromCanonicalCBOR V1 (AlonzoScript ConwayEra) +-- deriving via (MemPackCBOR (CompactForm a)) instance {-# OVERLAPPABLE #-} (MemPack (CompactForm a)) => ToCanonicalCBOR V1 (CompactForm a) + +deriving via (LedgerCBOR v MaryValue) instance ToCanonicalCBOR v MaryValue +deriving via (LedgerCBOR v MaryValue) instance FromCanonicalCBOR v MaryValue +instance {-# OVERLAPPING #-} ToCanonicalCBOR version (CompactForm MaryValue) where + toCanonicalCBOR version v = toCanonicalCBOR version (fromCompact v) + +instance {-# OVERLAPPING #-} ToCanonicalCBOR v (CompactForm Coin) where + toCanonicalCBOR v (Coin.CompactCoin ci) = toCanonicalCBOR v ci + +deriving via (MemPackCBOR (CompactForm a)) instance (MemPack (CompactForm a)) => FromCanonicalCBOR V1 (CompactForm a) +deriving via (MemPackCBOR CompactAddr) instance FromCanonicalCBOR V1 CompactAddr +deriving via (MemPackCBOR CompactAddr) instance ToCanonicalCBOR V1 CompactAddr +deriving via (MemPackCBOR Addr28Extra) instance FromCanonicalCBOR V1 Addr28Extra +deriving via (MemPackCBOR Addr28Extra) instance ToCanonicalCBOR V1 Addr28Extra +deriving via (LedgerCBOR v TxIn) instance FromCanonicalCBOR v TxIn +deriving via (LedgerCBOR v TxIn) instance ToCanonicalCBOR v TxIn +deriving via (MemPackCBOR DataHash32) instance FromCanonicalCBOR V1 DataHash32 +deriving via (MemPackCBOR DataHash32) instance ToCanonicalCBOR V1 DataHash32 +deriving via (MemPackCBOR (Timelock ConwayEra)) instance ToCanonicalCBOR V1 (Timelock ConwayEra) +deriving via (MemPackCBOR (Timelock ConwayEra)) instance FromCanonicalCBOR V1 (Timelock ConwayEra) +-- deriving via (LedgerCBOR v MaryValue) instance ToCanonicalCBOR v MaryValue +-- deriving via (LedgerCBOR v MaryValue) instance FromCanonicalCBOR v MaryValue + + +deriving via (LedgerCBOR v (KeyHash kr)) instance ToCanonicalCBOR v (KeyHash kr) +deriving via (LedgerCBOR v (KeyHash kr)) instance Typeable kr => FromCanonicalCBOR v (KeyHash kr) +deriving via (LedgerCBOR v (ScriptHash)) instance FromCanonicalCBOR v ScriptHash +deriving via (LedgerCBOR v (ScriptHash)) instance ToCanonicalCBOR v ScriptHash +deriving via (LedgerCBOR v (Datum ConwayEra)) instance ToCanonicalCBOR v (Datum ConwayEra) +deriving via (LedgerCBOR v (Datum ConwayEra)) instance FromCanonicalCBOR v (Datum ConwayEra) +deriving via (LedgerCBOR v (BinaryData ConwayEra)) instance ToCanonicalCBOR v (BinaryData ConwayEra) +deriving via (LedgerCBOR v (BinaryData ConwayEra)) instance FromCanonicalCBOR v (BinaryData ConwayEra) +deriving via (LedgerCBOR v (SafeHash EraIndependentData)) instance ToCanonicalCBOR v ((SafeHash EraIndependentData)) +deriving via (LedgerCBOR v (SafeHash EraIndependentData)) instance FromCanonicalCBOR v ((SafeHash EraIndependentData)) + + + diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Procedures.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Procedures.hs index ed4df370511..e32c5e1fe02 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Procedures.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Procedures.hs @@ -327,7 +327,7 @@ instance EraPParams era => EncCBOR (GovActionState era) where !> To gasExpiresAfter instance OMap.HasOKey GovActionId (GovActionState era) where - okeyL = lens gasId $ \gas gi -> gas {gasId = gi} + toOKey = gasId data Voter = CommitteeVoter !(Credential 'HotCommitteeRole) diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs index 05a22388435..44d90caf7e2 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs @@ -75,7 +75,6 @@ import Control.State.Transition ( ) import Data.Map (Map) import qualified Data.Map.Strict as Map -import Data.Maybe (isJust) import Data.Set as Set import Data.Void (Void) import GHC.Generics (Generic) @@ -261,11 +260,19 @@ conwayDelegTransition = do & certVStateL %~ unDelegReDelegDRep stakeCred accountState Nothing & certPStateL %~ unDelegReDelegStakePool stakeCred accountState Nothing ConwayDelegCert stakeCred delegatee -> do - let mAccountState = lookupAccountState stakeCred accounts - isJust mAccountState ?! StakeKeyNotRegisteredDELEG stakeCred checkStakeDelegateeRegistered delegatee - pure $ - processDelegationInternal (pvMajor pv < natVersion @10) stakeCred mAccountState delegatee certState + case lookupAccountStateIntern stakeCred accounts of + Nothing -> do + failBecause $ StakeKeyNotRegisteredDELEG stakeCred + pure certState + Just (internedCred, accountState) -> do + pure $ + processDelegationInternal + (pvMajor pv < natVersion @10) + internedCred + (Just accountState) + delegatee + certState ConwayRegDelegCert stakeCred delegatee deposit -> do checkDepositAgainstPParams deposit checkStakeKeyNotRegistered stakeCred diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/HardFork.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/HardFork.hs index 5d7b7569fbb..06ce976509a 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/HardFork.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/HardFork.hs @@ -108,15 +108,16 @@ populateVRFKeyHashes :: PState era -> PState era populateVRFKeyHashes pState = pState & psVRFKeyHashesL - %~ accumulateVRFKeyHashes (pState ^. psStakePoolsL) - . accumulateVRFKeyHashes (pState ^. psFutureStakePoolsL) + %~ accumulateVRFKeyHashes (pState ^. psStakePoolsL) (^. spsVrfL) + . accumulateVRFKeyHashes (pState ^. psFutureStakePoolParamsL) (^. sppVrfL) where accumulateVRFKeyHashes :: - Map (KeyHash 'StakePool) StakePoolState -> + Map (KeyHash 'StakePool) a -> + (a -> VRFVerKeyHash 'StakePoolVRF) -> Map (VRFVerKeyHash 'StakePoolVRF) (NonZero Word64) -> Map (VRFVerKeyHash 'StakePoolVRF) (NonZero Word64) - accumulateVRFKeyHashes spsMap acc = - Map.foldr' (\sps -> addVRFKeyHashOccurrence (sps ^. spsVrfL)) acc spsMap + accumulateVRFKeyHashes spMap getVrf acc = + Map.foldr' (addVRFKeyHashOccurrence . getVrf) acc spMap addVRFKeyHashOccurrence vrfKeyHash = Map.insertWith combine vrfKeyHash (knownNonZeroBounded @1) where diff --git a/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/RatifySpec.hs b/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/RatifySpec.hs index 80a4a435a7a..a548049010a 100644 --- a/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/RatifySpec.hs +++ b/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/RatifySpec.hs @@ -198,9 +198,7 @@ spoAndCCVotingSpec = do else do getLastEnactedParameterChange `shouldReturn` SNothing newRefScriptBaseFee `shouldBe` initialRefScriptBaseFee - -- https://github.com/IntersectMBO/cardano-ledger/issues/5170 - -- TODO: Re-enable after issue is resolved, by removing this override - disableInConformanceIt "Constitution cannot be changed if active committee size is below min size" + it "Constitution cannot be changed if active committee size is below min size" . whenPostBootstrap $ do modifyPParams $ \pp -> diff --git a/eras/dijkstra/impl/CHANGELOG.md b/eras/dijkstra/impl/CHANGELOG.md index 00acd428314..73e9121b088 100644 --- a/eras/dijkstra/impl/CHANGELOG.md +++ b/eras/dijkstra/impl/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.2.0.0 +* Add `dtbSubTransactions` to `TxBody` +* Add `subTransactionsTxBodyL` method to `DijkstraEraTxBody` class * Add `DijkstraTx` type with `DijkstraTx` and `DijkstraSubTx` constructors * Add `DijkstraSubTxBody` constructor to `DijkstraTxBodyRaw` * Add `TxLevel` argument to `Tx` and `TxBody` diff --git a/eras/dijkstra/impl/cardano-ledger-dijkstra.cabal b/eras/dijkstra/impl/cardano-ledger-dijkstra.cabal index 16c8b7f2f24..2cbea10576d 100644 --- a/eras/dijkstra/impl/cardano-ledger-dijkstra.cabal +++ b/eras/dijkstra/impl/cardano-ledger-dijkstra.cabal @@ -83,7 +83,7 @@ library base >=4.14 && <5, bytestring, cardano-crypto-class, - cardano-data, + cardano-data ^>=1.3, cardano-ledger-allegra, cardano-ledger-alonzo ^>=1.15, cardano-ledger-babbage, diff --git a/eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/TxBody.hs b/eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/TxBody.hs index f93b2ba6682..28be293b2a2 100644 --- a/eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/TxBody.hs +++ b/eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/TxBody.hs @@ -15,6 +15,8 @@ {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableSuperClasses #-} {-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -Wno-orphans #-} @@ -44,6 +46,7 @@ module Cardano.Ledger.Dijkstra.TxBody ( dtbCurrentTreasuryValue, dtbTreasuryDonation, dtbGuards, + dtbSubTransactions, dstbSpendInputs, dstbReferenceInputs, dstbOutputs, @@ -130,10 +133,12 @@ import Cardano.Ledger.MemoBytes ( memoRawTypeL, mkMemoizedEra, ) -import Cardano.Ledger.TxIn (TxIn) +import Cardano.Ledger.TxIn (TxId, TxIn) import Cardano.Ledger.Val (Val (..)) import Control.DeepSeq (NFData (..), deepseq) import Data.Coerce (coerce) +import Data.OMap.Strict (OMap) +import qualified Data.OMap.Strict as OMap import Data.OSet.Strict (OSet, decodeOSet) import qualified Data.OSet.Strict as OSet import Data.STRef (newSTRef, readSTRef, writeSTRef) @@ -166,6 +171,7 @@ data DijkstraTxBodyRaw l era where , dtbrProposalProcedures :: !(OSet.OSet (ProposalProcedure era)) , dtbrCurrentTreasuryValue :: !(StrictMaybe Coin) , dtbrTreasuryDonation :: !Coin + , dtbrSubTransactions :: !(OMap TxId (Tx SubTx era)) } -> DijkstraTxBodyRaw TopTx era DijkstraSubTxBodyRaw :: @@ -187,17 +193,23 @@ data DijkstraTxBodyRaw l era where } -> DijkstraTxBodyRaw SubTx era -deriving instance EraTxBody era => Eq (DijkstraTxBodyRaw l era) +deriving instance (EraTxBody era, Eq (Tx SubTx era)) => Eq (DijkstraTxBodyRaw l era) -instance EqRaw (TxBody l DijkstraEra) +instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + EqRaw (TxBody l DijkstraEra) deriving via InspectHeap (DijkstraTxBodyRaw l era) instance (Typeable l, EraTxBody era) => NoThunks (DijkstraTxBodyRaw l era) -instance EraTxBody era => NFData (DijkstraTxBodyRaw l era) where - rnf txBodyRaw@(DijkstraTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) = +instance (EraTxBody era, NFData (Tx SubTx era)) => NFData (DijkstraTxBodyRaw l era) where + rnf txBodyRaw@(DijkstraTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) = let DijkstraTxBodyRaw {..} = txBodyRaw in dtbrSpendInputs `deepseq` dtbrCollateralInputs `deepseq` @@ -217,7 +229,8 @@ instance EraTxBody era => NFData (DijkstraTxBodyRaw l era) where dtbrVotingProcedures `deepseq` dtbrProposalProcedures `deepseq` dtbrCurrentTreasuryValue `deepseq` - rnf dtbrTreasuryDonation + dtbrTreasuryDonation `deepseq` + rnf dtbrSubTransactions rnf txBodyRaw@(DijkstraSubTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) = let DijkstraSubTxBodyRaw {..} = txBodyRaw in dstbrSpendInputs `deepseq` @@ -236,7 +249,7 @@ instance EraTxBody era => NFData (DijkstraTxBodyRaw l era) where dstbrCurrentTreasuryValue `deepseq` rnf dstbrTreasuryDonation -deriving instance EraTxBody era => Show (DijkstraTxBodyRaw l era) +deriving instance (EraTxBody era, Show (Tx SubTx era)) => Show (DijkstraTxBodyRaw l era) basicDijkstraTxBodyRaw :: EraTxBody era => STxBothLevels l era -> DijkstraTxBodyRaw l era basicDijkstraTxBodyRaw STopTx = @@ -260,6 +273,7 @@ basicDijkstraTxBodyRaw STopTx = OSet.empty SNothing mempty + OMap.Empty basicDijkstraTxBodyRaw SSubTx = DijkstraSubTxBodyRaw mempty @@ -366,9 +380,8 @@ instance (Typeable l, EraTxBody era) => DecCBOR (DijkstraTxBodyRaw l era) where "TxBody: '" <> fieldName <> "' must be " <> requirement <> " when supplied" encodeTxBodyRaw :: - EraTxBody era => - DijkstraTxBodyRaw l era -> - Encode ('Closed 'Sparse) (DijkstraTxBodyRaw l era) + (EraTxBody era, EncCBOR (Tx SubTx era)) => + DijkstraTxBodyRaw l era -> Encode ('Closed 'Sparse) (DijkstraTxBodyRaw l era) encodeTxBodyRaw DijkstraTxBodyRaw {..} = let ValidityInterval bot top = dtbrVldt in Keyed @@ -395,6 +408,7 @@ encodeTxBodyRaw DijkstraTxBodyRaw {..} = !> Omit OSet.null (Key 20 (To dtbrProposalProcedures)) !> encodeKeyedStrictMaybe 21 dtbrCurrentTreasuryValue !> Omit (== mempty) (Key 22 $ To dtbrTreasuryDonation) + !> Omit null (Key 23 $ To dtbrSubTransactions) encodeTxBodyRaw DijkstraSubTxBodyRaw {..} = let ValidityInterval bot top = dstbrVldt in Keyed @@ -418,18 +432,53 @@ encodeTxBodyRaw DijkstraSubTxBodyRaw {..} = !> encodeKeyedStrictMaybe 21 dstbrCurrentTreasuryValue !> Omit (== mempty) (Key 22 $ To dstbrTreasuryDonation) -instance EraTxBody era => EncCBOR (DijkstraTxBodyRaw l era) where +instance + ( EraTxBody era + , EncCBOR (Tx SubTx era) + ) => + EncCBOR (DijkstraTxBodyRaw l era) + where encCBOR = encode . encodeTxBodyRaw -deriving instance Typeable l => NoThunks (TxBody l DijkstraEra) - -deriving instance Eq (TxBody l DijkstraEra) - -deriving newtype instance NFData (TxBody l DijkstraEra) - -deriving instance Show (TxBody l DijkstraEra) +deriving instance + ( Typeable l + , Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + NoThunks (TxBody l DijkstraEra) + +deriving instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + Eq (TxBody l DijkstraEra) + +deriving newtype instance + ( NFData (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + NFData (TxBody l DijkstraEra) + +deriving instance + ( Show (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + Show (TxBody l DijkstraEra) pattern DijkstraTxBody :: + ( EncCBOR (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + ) => Set TxIn -> Set TxIn -> Set TxIn -> @@ -449,6 +498,7 @@ pattern DijkstraTxBody :: OSet.OSet (ProposalProcedure DijkstraEra) -> StrictMaybe Coin -> Coin -> + OMap TxId (Tx SubTx DijkstraEra) -> TxBody TopTx DijkstraEra pattern DijkstraTxBody { dtbSpendInputs @@ -470,6 +520,7 @@ pattern DijkstraTxBody , dtbProposalProcedures , dtbCurrentTreasuryValue , dtbTreasuryDonation + , dtbSubTransactions } <- ( getMemoRawType -> DijkstraTxBodyRaw @@ -492,6 +543,7 @@ pattern DijkstraTxBody , dtbrProposalProcedures = dtbProposalProcedures , dtbrCurrentTreasuryValue = dtbCurrentTreasuryValue , dtbrTreasuryDonation = dtbTreasuryDonation + , dtbrSubTransactions = dtbSubTransactions } ) where @@ -514,7 +566,8 @@ pattern DijkstraTxBody votingProcedures proposalProcedures currentTreasuryValue - treasuryDonation = + treasuryDonation + subTransactions = mkMemoizedEra @DijkstraEra $ DijkstraTxBodyRaw inputsX @@ -536,8 +589,14 @@ pattern DijkstraTxBody proposalProcedures currentTreasuryValue treasuryDonation + subTransactions pattern DijkstraSubTxBody :: + ( EncCBOR (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + ) => Set TxIn -> Set TxIn -> StrictSeq (Sized (TxOut DijkstraEra)) -> @@ -643,7 +702,13 @@ instance (Typeable l, EraTxBody era) => DecCBOR (Annotator (DijkstraTxBodyRaw l deriving via Mem (DijkstraTxBodyRaw l DijkstraEra) instance - Typeable l => DecCBOR (Annotator (TxBody l DijkstraEra)) + ( Typeable l + , Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + DecCBOR (Annotator (TxBody l DijkstraEra)) instance HasEraTxLevel DijkstraTxBodyRaw DijkstraEra where toSTxLevel DijkstraTxBodyRaw {} = STopTx @@ -718,7 +783,14 @@ withdrawalsDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrWithdrawals = y} ) -instance EraTxBody DijkstraEra where +instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + EraTxBody DijkstraEra + where newtype TxBody l DijkstraEra = MkDijkstraTxBody (MemoBytes (DijkstraTxBodyRaw l DijkstraEra)) deriving (Generic, SafeToHash, ToCBOR) @@ -856,7 +928,14 @@ vldtDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrVldt = y} ) -instance AllegraEraTxBody DijkstraEra where +instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + AllegraEraTxBody DijkstraEra + where vldtTxBodyL = memoRawTypeL @DijkstraEra . vldtDijkstraTxBodyRawL {-# INLINE vldtTxBodyL #-} @@ -872,7 +951,14 @@ mintDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrMint = y} ) -instance MaryEraTxBody DijkstraEra where +instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + MaryEraTxBody DijkstraEra + where mintTxBodyL = memoRawTypeL @DijkstraEra . mintDijkstraTxBodyRawL {-# INLINE mintTxBodyL #-} @@ -905,7 +991,14 @@ networkIdDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrNetworkId = y} ) -instance AlonzoEraTxBody DijkstraEra where +instance + ( Eq (Tx SubTx DijkstraEra) + , NFData (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + AlonzoEraTxBody DijkstraEra + where collateralInputsTxBodyL = memoRawTypeL @DijkstraEra . collateralInputsDijkstraTxBodyRawL {-# INLINE collateralInputsTxBodyL #-} @@ -953,7 +1046,14 @@ referenceInputsDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrReferenceInputs = y} ) -instance BabbageEraTxBody DijkstraEra where +instance + ( NFData (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + BabbageEraTxBody DijkstraEra + where sizedOutputsTxBodyL = lensMemoRawType @DijkstraEra ( \case @@ -1033,7 +1133,14 @@ currentTreasuryValueDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrCurrentTreasuryValue = y} ) -instance ConwayEraTxBody DijkstraEra where +instance + ( NFData (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + ConwayEraTxBody DijkstraEra + where votingProceduresTxBodyL = memoRawTypeL @DijkstraEra . votingProceduresDijkstraTxBodyRawL {-# INLINE votingProceduresTxBodyL #-} proposalProceduresTxBodyL = memoRawTypeL @DijkstraEra . proposalProceduresDijkstraTxBodyRawL @@ -1046,6 +1153,8 @@ instance ConwayEraTxBody DijkstraEra where class ConwayEraTxBody era => DijkstraEraTxBody era where guardsTxBodyL :: Lens' (TxBody l era) (OSet (Credential Guard)) + subTransactionsTxBodyL :: Lens' (TxBody TopTx era) (OMap TxId (Tx SubTx era)) + guardsDijkstraTxBodyRawL :: Lens' (DijkstraTxBodyRaw l era) (OSet (Credential Guard)) guardsDijkstraTxBodyRawL = lens @@ -1058,10 +1167,23 @@ guardsDijkstraTxBodyRawL = x@DijkstraSubTxBodyRaw {} -> \y -> x {dstbrGuards = y} ) -instance DijkstraEraTxBody DijkstraEra where +subTransactionsDijkstraTxBodyL :: Lens' (DijkstraTxBodyRaw TopTx era) (OMap TxId (Tx SubTx era)) +subTransactionsDijkstraTxBodyL = lens dtbrSubTransactions (\x y -> x {dtbrSubTransactions = y}) + +instance + ( NFData (Tx SubTx DijkstraEra) + , Eq (Tx SubTx DijkstraEra) + , Show (Tx SubTx DijkstraEra) + , EncCBOR (Tx SubTx DijkstraEra) + ) => + DijkstraEraTxBody DijkstraEra + where {-# INLINE guardsTxBodyL #-} guardsTxBodyL = memoRawTypeL @DijkstraEra . guardsDijkstraTxBodyRawL + {-# INLINE subTransactionsTxBodyL #-} + subTransactionsTxBodyL = memoRawTypeL @DijkstraEra . subTransactionsDijkstraTxBodyL + -- | Decoder for decoding guards in a backwards-compatible manner. It peeks at -- the first element and if it's a credential, it decodes the rest of the -- elements as credentials. If the first element is a plain keyhash, it will diff --git a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Arbitrary.hs b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Arbitrary.hs index 0be7178a430..b0c5290878b 100644 --- a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Arbitrary.hs +++ b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Arbitrary.hs @@ -46,6 +46,25 @@ instance Arbitrary (DijkstraPParams Identity DijkstraEra) where instance Arbitrary (DijkstraPParams StrictMaybe DijkstraEra) where arbitrary = genericArbitraryU +instance Arbitrary (TxBody SubTx DijkstraEra) where + arbitrary = + DijkstraSubTxBody + <$> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> scale (`div` 15) arbitrary + <*> arbitrary + <*> scale (`div` 15) arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + instance Arbitrary (TxBody TopTx DijkstraEra) where arbitrary = DijkstraTxBody @@ -68,6 +87,7 @@ instance Arbitrary (TxBody TopTx DijkstraEra) where <*> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary instance Arbitrary (UpgradeDijkstraPParams Identity DijkstraEra) where arbitrary = genericArbitraryU diff --git a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Examples.hs b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Examples.hs index 2136a140fd3..526284205bd 100644 --- a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Examples.hs +++ b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Examples.hs @@ -114,6 +114,7 @@ exampleTxBodyDijkstra = mempty (SJust $ Coin 867530900000) -- current treasury value mempty + mempty where MaryValue _ exampleMultiAsset = exampleMultiAssetValue 3 diff --git a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/TreeDiff.hs b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/TreeDiff.hs index adc4de57cfd..b586841b82f 100644 --- a/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/TreeDiff.hs +++ b/eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/TreeDiff.hs @@ -47,7 +47,7 @@ instance ToExpr (DijkstraPParams StrictMaybe DijkstraEra) instance ToExpr (DijkstraTxBodyRaw l DijkstraEra) where toExpr = \case - txBody@(DijkstraTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> + txBody@(DijkstraTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> let DijkstraTxBodyRaw {..} = txBody in Rec "DijkstraTxBodyRaw" $ OMap.fromList @@ -70,6 +70,7 @@ instance ToExpr (DijkstraTxBodyRaw l DijkstraEra) where , ("dtbrProposalProcedures", toExpr dtbrProposalProcedures) , ("dtbrCurrentTreasuryValue", toExpr dtbrCurrentTreasuryValue) , ("dtbrTreasuryDonation", toExpr dtbrTreasuryDonation) + , ("dtbrSubTransactions", toExpr dtbrSubTransactions) ] txBody@(DijkstraSubTxBodyRaw _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> let DijkstraSubTxBodyRaw {..} = txBody diff --git a/eras/mary/impl/cardano-ledger-mary.cabal b/eras/mary/impl/cardano-ledger-mary.cabal index 3fc0df2a8be..dd68a2047d9 100644 --- a/eras/mary/impl/cardano-ledger-mary.cabal +++ b/eras/mary/impl/cardano-ledger-mary.cabal @@ -77,7 +77,7 @@ library base16-bytestring, bytestring, cardano-crypto-class, - cardano-data ^>=1.2, + cardano-data ^>=1.3, cardano-ledger-allegra ^>=1.9, cardano-ledger-binary >=1.4, cardano-ledger-core:{cardano-ledger-core, internal} >=1.19, diff --git a/eras/shelley/impl/CHANGELOG.md b/eras/shelley/impl/CHANGELOG.md index 69a822e677a..f04da2b4cc8 100644 --- a/eras/shelley/impl/CHANGELOG.md +++ b/eras/shelley/impl/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.18.0.0 +* Replace `StakePoolState` values in `psFutureStakePoolParams` with `StakePoolParams` +* Remove `psFutureStakePoolsL` +* Add `psFutureStakePoolParamsL` * Remove deprecated function `getPoolParameters` * Remove deprecated function `toShelleyGenesisPairs` * Remove deprecated type `RewardAccounts` diff --git a/eras/shelley/impl/cardano-ledger-shelley.cabal b/eras/shelley/impl/cardano-ledger-shelley.cabal index cfa9cf64260..b3cc87577e5 100644 --- a/eras/shelley/impl/cardano-ledger-shelley.cabal +++ b/eras/shelley/impl/cardano-ledger-shelley.cabal @@ -107,7 +107,7 @@ library bytestring, cardano-crypto-class, cardano-crypto-wrapper, - cardano-data ^>=1.2.2, + cardano-data ^>=1.3, cardano-ledger-binary ^>=1.8, cardano-ledger-byron, cardano-ledger-core:{cardano-ledger-core, internal} ^>=1.19, diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs index 8b2214d9e80..871aa74d781 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs @@ -104,7 +104,7 @@ module Cardano.Ledger.Shelley.LedgerState ( dsIRewardsL, dsFutureGenDelegsL, psStakePoolsL, - psFutureStakePoolsL, + psFutureStakePoolParamsL, psRetiringL, psVRFKeyHashesL, diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs index 6e177e8ee7e..54c631430df 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Deleg.hs @@ -286,15 +286,15 @@ delegationTransition = do DelegStakeTxCert cred stakePool -> do -- note that pattern match is used instead of cwitness and dpool, as in the spec -- (hk ∈ dom (rewards ds)) - case lookupAccountState cred (ds ^. accountsL) of + case lookupAccountStateIntern cred (ds ^. accountsL) of Nothing -> do failBecause $ StakeDelegationImpossibleDELEG cred pure certState - Just accountState -> + Just (internedCred, accountState) -> pure $ certState & certDStateL . accountsL %~ adjustAccountState (stakePoolDelegationAccountStateL ?~ stakePool) cred - & certPStateL %~ unDelegReDelegStakePool cred accountState (Just stakePool) + & certPStateL %~ unDelegReDelegStakePool internedCred accountState (Just stakePool) GenesisDelegTxCert gkh vkh vrf -> do sp <- liftSTS $ asks stabilityWindow -- note that pattern match is used instead of genesisDeleg, as in the spec diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Pool.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Pool.hs index 66fb6bad295..031b3e80e6e 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Pool.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Pool.hs @@ -215,7 +215,7 @@ poolDelegationTransition :: poolDelegationTransition = do TRC ( PoolEnv cEpoch pp - , ps@PState {psStakePools, psFutureStakePools, psVRFKeyHashes} + , ps@PState {psStakePools, psFutureStakePoolParams, psVRFKeyHashes} , poolCert ) <- judgmentContext @@ -273,38 +273,23 @@ poolDelegationTransition = do -- If a pool re-registers with a fresh VRF, we have to record it in the map, -- but also remove the previous VRFHashKey potentially stored in previous re-registration within the same epoch, -- which we retrieve from futureStakePools. - case Map.lookup sppId psFutureStakePools of + case Map.lookup sppId psFutureStakePoolParams of Nothing -> Map.insert sppVrf (knownNonZeroBounded @1) - Just futureStakePoolState - | futureStakePoolState ^. spsVrfL /= sppVrf -> + Just futureStakePoolParams + | futureStakePoolParams ^. sppVrfL /= sppVrf -> Map.insert sppVrf (knownNonZeroBounded @1) - . Map.delete (futureStakePoolState ^. spsVrfL) + . Map.delete (futureStakePoolParams ^. sppVrfL) | otherwise -> id | otherwise = id tellEvent $ ReregisterPool sppId - -- NOTE: The `ppId` is already registered, so we want to reregister - -- it. That means adding it to the Future Stake Pools (if it is not - -- there already), and overriding its range with the new 'poolParams', - -- if it is. - -- + -- This `sppId` is already registered, so we want to reregister it. + -- That means adding it to the futureStakePoolParams or overriding it with the new 'poolParams'. -- We must also unretire it, if it has been scheduled for retirement. - -- - -- The deposit does not change. One pays the deposit just once. Only - -- if it is fully retired (i.e. it's deposit has been refunded, and it - -- has been removed from the registered pools). does it need to pay a - -- new deposit (at the current deposit amount). But of course, if that - -- has happened, we cannot be in this branch of the case statement. - let futureStakePoolState = - mkStakePoolState - (stakePoolState ^. spsDepositL) - -- delegators are set in PoolReap, - -- in order to capture delegations that happened after re-registration but before the end of the epoch - mempty - stakePoolParams + -- The deposit does not change. pure $ ps - & psFutureStakePoolsL - %~ Map.insert sppId futureStakePoolState + & psFutureStakePoolParamsL + %~ Map.insert sppId stakePoolParams & psRetiringL %~ Map.delete sppId & psVRFKeyHashesL %~ updateFutureVRFKeyHash RetirePool sppId e -> do diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/PoolReap.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/PoolReap.hs index 493ca2f7cbf..e65a4afb6bc 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/PoolReap.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/PoolReap.hs @@ -143,21 +143,28 @@ poolReapTransition = do Map.merge Map.dropMissing Map.dropMissing - ( Map.zipWithMaybeMatched $ \_ sps spsF -> - if sps ^. spsVrfL /= spsF ^. spsVrfL then Just (sps ^. spsVrfL) else Nothing + ( Map.zipWithMaybeMatched $ \_ sps sppF -> + if sps ^. spsVrfL /= sppF ^. sppVrfL then Just (sps ^. spsVrfL) else Nothing ) (ps0 ^. psStakePoolsL) - (ps0 ^. psFutureStakePoolsL) + (ps0 ^. psFutureStakePoolParamsL) -- activate future stakePools ps = ps0 { psStakePools = - Map.unionWith - (\newPoolState oldPoolState -> newPoolState {spsDelegators = spsDelegators oldPoolState}) - (ps0 ^. psFutureStakePoolsL) + Map.merge + Map.dropMissing + Map.preserveMissing + ( Map.zipWithMatched $ \_ futureParams currentState -> + mkStakePoolState + (currentState ^. spsDepositL) + (currentState ^. spsDelegatorsL) + futureParams + ) + (ps0 ^. psFutureStakePoolParamsL) (ps0 ^. psStakePoolsL) - , psFutureStakePools = Map.empty + , psFutureStakePoolParams = Map.empty } cs = cs0 & certPStateL .~ ps diff --git a/eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/PoolSpec.hs b/eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/PoolSpec.hs index c28a7a10e9d..c6cc0bfe73f 100644 --- a/eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/PoolSpec.hs +++ b/eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/PoolSpec.hs @@ -384,8 +384,8 @@ spec = describe "POOL" $ do pps <- psStakePools <$> getPState spsVrf <$> Map.lookup poolKh pps `shouldBe` mbVrf expectFuturePool poolKh mbVrf = do - fps <- psFutureStakePools <$> getPState - spsVrf <$> Map.lookup poolKh fps `shouldBe` mbVrf + fps <- psFutureStakePoolParams <$> getPState + sppVrf <$> Map.lookup poolKh fps `shouldBe` mbVrf expectPoolDelegs poolKh delegs = do pps <- psStakePools <$> getPState spsDelegators <$> Map.lookup poolKh pps `shouldBe` delegs diff --git a/eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Pool.hs b/eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Pool.hs index 920c4655a67..e1afce37695 100644 --- a/eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Pool.hs +++ b/eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Pool.hs @@ -24,7 +24,6 @@ import Cardano.Protocol.TPraos.BHeader (bhbody, bheaderSlotNo) import Control.SetAlgebra (dom, eval, (∈), (∉)) import qualified Data.Map.Strict as Map import qualified Data.Set as Set -import Lens.Micro import Lens.Micro.Extras (view) import Test.Cardano.Ledger.Shelley.ConcreteCryptoTypes (MockCrypto) import Test.Cardano.Ledger.Shelley.Constants (defaultConstants) @@ -126,15 +125,15 @@ poolRegistrationProp } = let hk = sppId stakePoolParams in case Map.lookup hk $ psStakePools sourceSt of - Just sps -> + Just _ -> conjoin [ counterexample "Pre-existing StakePoolParams must still be registered in pParams" (eval (hk ∈ dom (psStakePools targetSt)) :: Bool) , counterexample "New StakePoolParams are registered in future Params map" - ( Map.lookup hk (psFutureStakePools targetSt) - === Just (mkStakePoolState (sps ^. spsDepositL) mempty stakePoolParams) + ( Map.lookup hk (psFutureStakePoolParams targetSt) + === Just stakePoolParams ) , counterexample "StakePoolParams are removed in 'retiring'" @@ -150,7 +149,7 @@ poolRegistrationProp ) , counterexample "StakePoolParams are not present in 'future pool params'" - (eval (hk ∉ dom (psFutureStakePools targetSt)) :: Bool) + (eval (hk ∉ dom (psFutureStakePoolParams targetSt)) :: Bool) , counterexample "StakePoolParams are removed in 'retiring'" (eval (hk ∉ dom (psRetiring targetSt)) :: Bool) diff --git a/eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs b/eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs index 6745ceef89a..01100138efd 100644 --- a/eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs +++ b/eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Examples/Combinators.hs @@ -272,13 +272,13 @@ regPool pool cs = cs {chainNes = nes'} (mkStakePoolState poolDeposit mempty pool) (psStakePools ps) } - Just sps -> + Just _ -> ps - { psFutureStakePools = + { psFutureStakePoolParams = Map.insert (sppId pool) - (mkStakePoolState (spsDeposit sps) mempty pool) - (psFutureStakePools ps) + pool + (psFutureStakePoolParams ps) } dps' = dps & certPStateL .~ ps' ls' = ls {lsCertState = dps'} @@ -312,7 +312,14 @@ updatePoolParams pool cs = cs {chainNes = nes'} (sppId pool) (mkStakePoolState (es ^. curPParamsEpochStateL . ppPoolDepositCompactL) mempty pool) (psStakePools ps) - , psFutureStakePools = Map.delete (sppId pool) (psStakePools ps) + , psFutureStakePoolParams = + Map.mapMaybeWithKey + ( \k sps -> + if k == sppId pool + then Nothing + else Just $ stakePoolStateToStakePoolParams k sps + ) + (psStakePools ps) } dps' = dps & certPStateL .~ ps' ls' = ls {lsCertState = dps'} diff --git a/flake.lock b/flake.lock index 2d791dfe154..83adb05a69b 100644 --- a/flake.lock +++ b/flake.lock @@ -204,11 +204,11 @@ "formal-ledger-specifications": { "flake": false, "locked": { - "lastModified": 1761306650, - "narHash": "sha256-rfVUKGUOYYiHnzLn6KcxaauLWR1o8Qf2MWlrWzMrlsA=", + "lastModified": 1762857598, + "narHash": "sha256-pWnnnpixhcn2iBZzEcQgYlzb0ae6YS/3JgDQxGKoC4I=", "owner": "IntersectMBO", "repo": "formal-ledger-specifications", - "rev": "7af1d3dbe03721048265591b12b857705f5c2577", + "rev": "d84538c1ad6d8dc3cff4e59ead574daf84c88117", "type": "github" }, "original": { diff --git a/libs/cardano-data/CHANGELOG.md b/libs/cardano-data/CHANGELOG.md index b6da01879f0..b45304ea530 100644 --- a/libs/cardano-data/CHANGELOG.md +++ b/libs/cardano-data/CHANGELOG.md @@ -1,8 +1,9 @@ # Version history for `cardano-data` -## 1.2.4.2 +## 1.3.0.0 -* +* Add `lookupInternMap` +* Replace `okeyL` with `toOKey` ## 1.2.4.1 diff --git a/libs/cardano-data/cardano-data.cabal b/libs/cardano-data/cardano-data.cabal index a761ec8ea22..d49e8b83308 100644 --- a/libs/cardano-data/cardano-data.cabal +++ b/libs/cardano-data/cardano-data.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: cardano-data -version: 1.2.4.2 +version: 1.3.0.0 license: Apache-2.0 maintainer: operations@iohk.io author: IOHK @@ -44,7 +44,6 @@ library containers, data-default, deepseq, - microlens, mtl, nothunks, vector, @@ -73,7 +72,6 @@ library testlib cardano-ledger-binary:testlib, containers, hspec, - microlens, test-suite cardano-data-tests type: exitcode-stdio-1.0 @@ -103,6 +101,5 @@ test-suite cardano-data-tests cardano-strict-containers, containers, hspec, - microlens, quickcheck-classes, testlib, diff --git a/libs/cardano-data/src/Data/MapExtras.hs b/libs/cardano-data/src/Data/MapExtras.hs index fd9263532a3..b5f0529b6b2 100644 --- a/libs/cardano-data/src/Data/MapExtras.hs +++ b/libs/cardano-data/src/Data/MapExtras.hs @@ -28,6 +28,7 @@ module Data.MapExtras ( extractKeysSmallSet, fromKeys, fromElems, + lookupInternMap, ) where import Data.Foldable (toList) @@ -270,3 +271,16 @@ fromElems f vs = -- a nice optimization for already sorted keys and with list fusion there should be no overhead Map.fromList [(f v, v) | v <- toList vs] {-# INLINE fromElems #-} + +-- | Look up a key in a map and return the interned key together with its value, if present. +-- The returned key is exactly the one stored in the map. +-- Useful for maximizing sharing by avoiding duplicate-but-equal keys. +lookupInternMap :: Ord k => k -> Map k v -> Maybe (k, v) +lookupInternMap k = go + where + go Tip = Nothing + go (Bin _ kx v l r) = + case compare k kx of + LT -> go l + GT -> go r + EQ -> Just (kx, v) diff --git a/libs/cardano-data/src/Data/OMap/Strict.hs b/libs/cardano-data/src/Data/OMap/Strict.hs index 08aca69f791..212a2ae3e24 100644 --- a/libs/cardano-data/src/Data/OMap/Strict.hs +++ b/libs/cardano-data/src/Data/OMap/Strict.hs @@ -14,7 +14,7 @@ {-# LANGUAGE ViewPatterns #-} module Data.OMap.Strict ( - HasOKey (okeyL), + HasOKey (toOKey), OMap (Empty, (:<|:), (:|>:)), null, size, @@ -61,6 +61,7 @@ import Control.DeepSeq (NFData (..)) import Data.Aeson (ToJSON (..)) import Data.Default (Default (..)) import Data.Foldable qualified as F +import Data.Functor ((<&>)) import Data.Map.Strict qualified as Map import Data.MapExtras qualified as MapE import Data.Maybe (isJust) @@ -69,7 +70,6 @@ import Data.Set qualified as Set import Data.Typeable (Typeable) import GHC.Exts qualified as Exts import GHC.Generics (Generic) -import Lens.Micro import NoThunks.Class (NoThunks (..)) import Prelude hiding (elem, filter, lookup, null, seq) @@ -78,7 +78,7 @@ import Prelude hiding (elem, filter, lookup, null, seq) -- -- For a type @V@, defines a lens from @V@ to and Ord type @K@. class Ord k => HasOKey k v | v -> k where - okeyL :: Lens' v k + toOKey :: v -> k -- | A general-purpose finite, insert-ordered, map that is strict in its -- keys and values. @@ -130,7 +130,7 @@ size (OMap sseq _) = SSeq.length sseq -- | \(O(1)\). Strict in its arguments. singleton :: HasOKey k v => v -> OMap k v singleton !v = - let k = v ^. okeyL + let k = toOKey v in OMap (SSeq.singleton k) (Map.singleton k v) -- | \(O(\log n)\). If the key is not present 'lookup' returns @@ -148,7 +148,7 @@ cons v omap@(OMap sseq kv) | Map.member k kv = omap | otherwise = OMap (k SSeq.<| sseq) (Map.insert k v kv) where - k = v ^. okeyL + k = toOKey v -- | \(O(\log n)\). Checks membership before cons'ing. (<|) :: HasOKey k v => v -> OMap k v -> OMap k v @@ -163,7 +163,7 @@ cons' v (OMap sseq kv) | Map.member k kv = OMap sseq kv' | otherwise = OMap (k SSeq.<| sseq) kv' where - k = v ^. okeyL + k = toOKey v kv' = Map.insert k v kv -- | \(O(\log n)\). Checks membership before cons'ing. Overwrites a @@ -181,7 +181,7 @@ snoc omap@(OMap sseq kv) v | Map.member k kv = omap | otherwise = OMap (sseq SSeq.|> k) (Map.insert k v kv) where - k = v ^. okeyL + k = toOKey v -- | \(O(\log n)\). Checks membership before snoc'ing. (|>) :: HasOKey k v => OMap k v -> v -> OMap k v @@ -196,7 +196,7 @@ snoc' (OMap sseq kv) v | Map.member k kv = OMap sseq kv' | otherwise = OMap (sseq SSeq.|> k) kv' where - k = v ^. okeyL + k = toOKey v kv' = Map.insert k v kv -- | \(O(\log n)\). Checks membership before snoc'ing. Overwrites a @@ -238,7 +238,7 @@ fromFoldableDuplicates = F.foldl' snoc_ (Set.empty, empty) where snoc_ :: (HasOKey k v, Ord v) => (Set.Set v, OMap k v) -> v -> (Set.Set v, OMap k v) snoc_ (duplicates, omap@(OMap sseq kv)) v = - let k = v ^. okeyL + let k = toOKey v in if Map.member k kv then (Set.insert v duplicates, omap) else (duplicates, OMap (sseq SSeq.|> k) (Map.insert k v kv)) @@ -269,7 +269,7 @@ member k (OMap _sseq kv) = Map.member k kv -- | \(O(\log n)\). Value membership check. elem :: (HasOKey k v, Eq v) => v -> OMap k v -> Bool -elem v = (Just v ==) . lookup (v ^. okeyL) +elem v = (Just v ==) . lookup (toOKey v) -- | \(O(n)\). Given a `Set` of @k@s, and an `OMap` @k@ @v@ return -- a pair of `Map` and `OMap` where the @k@s in the `Set` have been @@ -304,13 +304,12 @@ extractKeys ks (OMap sseq kv) = -- -- >>> :set -XFlexibleInstances -XMultiParamTypeClasses -- >>> import Data.OMap.Strict --- >>> import Lens.Micro --- >>> instance HasOKey Int (Int, Char) where okeyL = _1 +-- >>> instance HasOKey Int (Int, Char) where toOKey = fst -- >>> let m = fromFoldable $ zip [1,2] ['a','b'] :: OMap Int (Int, Char) -- >>> m -- StrictSeq {fromStrict = fromList [(1,(1,'a')),(2,(2,'b'))]} -- >>> let adjustingFn (k, v) = (k, succ v) -- Changes the value --- >>> let overwritingAdjustingFn (k,v) = (succ k, v) -- Changes the `okeyL`. +-- >>> let overwritingAdjustingFn (k,v) = (succ k, v) -- Modify the key. -- >>> adjust adjustingFn 1 m -- StrictSeq {fromStrict = fromList [(1,(1,'b')),(2,(2,'b'))]} -- >>> adjust overwritingAdjustingFn 1 m @@ -321,7 +320,7 @@ adjust f k omap@(OMap sseq kv) = Nothing -> omap Just v -> let v' = f v - k' = v' ^. okeyL + k' = toOKey v' in if k' == k then OMap sseq (Map.insert k v' kv) else diff --git a/libs/cardano-data/test/Test/Cardano/Data/OMap/StrictSpec.hs b/libs/cardano-data/test/Test/Cardano/Data/OMap/StrictSpec.hs index e9cd9ca85ec..10eeed5cdeb 100644 --- a/libs/cardano-data/test/Test/Cardano/Data/OMap/StrictSpec.hs +++ b/libs/cardano-data/test/Test/Cardano/Data/OMap/StrictSpec.hs @@ -12,7 +12,6 @@ import Data.OMap.Strict import Data.Proxy (Proxy (Proxy)) import Data.Sequence.Strict qualified as SSeq import Data.Set qualified as Set -import Lens.Micro hiding (set) import Test.Cardano.Data.Arbitrary () import Test.Cardano.Ledger.Binary.RoundTrip (roundTripCborSpec) import Test.Hspec @@ -29,11 +28,11 @@ spec = prop "unconsed" $ \(m :: OMap Int Int) -> case m of Empty -> pure () - v :<|: _kv -> v ^. okeyL `shouldSatisfy` (`member` m) + v :<|: _kv -> toOKey v `shouldSatisfy` (`member` m) prop "unsnoced" $ \(m :: OMap Int Int) -> case m of Empty -> pure () - _kv :|>: v -> v ^. okeyL `shouldSatisfy` (`member` m) + _kv :|>: v -> toOKey v `shouldSatisfy` (`member` m) context "when cons-ing" $ do prop "adding a duplicate results in a no-op" $ \(m :: OMap Int Int) -> do @@ -108,16 +107,16 @@ spec = prop "cons' - (<||)" $ \((omap, i) :: (OMap Int OMapTest, OMapTest)) -> do let consed = i <|| omap - k = i ^. okeyL + k = toOKey i if k `member` omap - then consed `shouldBe` adjust (const i) (i ^. okeyL) omap + then consed `shouldBe` adjust (const i) (toOKey i) omap else consed `shouldBe` i <| omap prop "snoc' - (||>)" $ \((omap, i) :: (OMap Int OMapTest, OMapTest)) -> do let snoced = omap ||> i - k = i ^. okeyL + k = toOKey i if k `member` omap - then snoced `shouldBe` adjust (const i) (i ^. okeyL) omap + then snoced `shouldBe` adjust (const i) (toOKey i) omap else snoced `shouldBe` omap |> i prop "fromFoldable preserves order" $ \(set :: Set.Set Int) -> do @@ -156,13 +155,13 @@ spec = ] instance HasOKey Int Int where - okeyL = lens id const + toOKey = id data OMapTest = OMapTest {omFst :: Int, omSnd :: Int} deriving (Eq, Show, Ord) instance HasOKey Int OMapTest where - okeyL = lens omFst $ \om u -> om {omFst = u} + toOKey = omFst instance Arbitrary OMapTest where arbitrary = OMapTest <$> arbitrary <*> arbitrary diff --git a/libs/cardano-data/testlib/Test/Cardano/Data/Arbitrary.hs b/libs/cardano-data/testlib/Test/Cardano/Data/Arbitrary.hs index c9398d2569e..263abbd30a3 100644 --- a/libs/cardano-data/testlib/Test/Cardano/Data/Arbitrary.hs +++ b/libs/cardano-data/testlib/Test/Cardano/Data/Arbitrary.hs @@ -1,13 +1,12 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ImportQualifiedPost #-} +{-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -Wno-orphans #-} module Test.Cardano.Data.Arbitrary (genOSet) where -import Data.Map.Strict qualified as Map import Data.OMap.Strict qualified as OMap import Data.OSet.Strict qualified as OSet -import Lens.Micro (set) import Test.Cardano.Ledger.Binary.Arbitrary () import Test.QuickCheck @@ -17,6 +16,5 @@ instance (Arbitrary a, Ord a) => Arbitrary (OSet.OSet a) where genOSet :: Ord a => Gen a -> Gen (OSet.OSet a) genOSet = fmap OSet.fromFoldable . listOf -instance (Ord v, Arbitrary v, OMap.HasOKey k v, Arbitrary k) => Arbitrary (OMap.OMap k v) where - arbitrary = - fmap OMap.fromFoldable . shuffle . Map.elems . Map.mapWithKey (flip (set OMap.okeyL)) =<< arbitrary +instance (Arbitrary v, OMap.HasOKey k v, Arbitrary k) => Arbitrary (OMap.OMap k v) where + arbitrary = OMap.fromFoldable @[] <$> arbitrary diff --git a/libs/cardano-ledger-api/src/Cardano/Ledger/Api/Era.hs b/libs/cardano-ledger-api/src/Cardano/Ledger/Api/Era.hs index e28e81f6b2b..05006e3bc94 100644 --- a/libs/cardano-ledger-api/src/Cardano/Ledger/Api/Era.hs +++ b/libs/cardano-ledger-api/src/Cardano/Ledger/Api/Era.hs @@ -639,6 +639,7 @@ instance EraApi DijkstraEra where , dtbProposalProcedures = OSet.mapL upgradeProposals ctbrProposalProcedures , dtbVotingProcedures = coerce ctbrVotingProcedures , dtbTreasuryDonation = ctbrTreasuryDonation + , dtbSubTransactions = mempty } upgradeTxWits atw = diff --git a/libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs b/libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs index baf69547c7a..1d237e51776 100644 --- a/libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs +++ b/libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs @@ -461,8 +461,7 @@ mkQueryPoolStateResult :: mkQueryPoolStateResult f ps = QueryPoolStateResult { qpsrStakePoolParams = Map.mapWithKey stakePoolStateToStakePoolParams restrictedStakePools - , qpsrFutureStakePoolParams = - Map.mapWithKey stakePoolStateToStakePoolParams (f $ psFutureStakePools ps) + , qpsrFutureStakePoolParams = f $ psFutureStakePoolParams ps , qpsrRetiring = f $ psRetiring ps , qpsrDeposits = Map.map (fromCompact . spsDeposit) restrictedStakePools } diff --git a/libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Pool.hs b/libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Pool.hs index bbe6482ebc1..47fa4e4d779 100644 --- a/libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Pool.hs +++ b/libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Pool.hs @@ -33,7 +33,7 @@ instance SpecTranslate ctx (PState era) where toSpecRep PState {..} = Agda.MkPState <$> toSpecRep (Map.mapWithKey stakePoolStateToStakePoolParams psStakePools) - <*> toSpecRep (Map.mapWithKey stakePoolStateToStakePoolParams psFutureStakePools) + <*> toSpecRep psFutureStakePoolParams <*> toSpecRep psRetiring instance SpecTranslate ctx PoolCert where diff --git a/libs/cardano-ledger-core/CHANGELOG.md b/libs/cardano-ledger-core/CHANGELOG.md index 491480e33bc..1e710f2e935 100644 --- a/libs/cardano-ledger-core/CHANGELOG.md +++ b/libs/cardano-ledger-core/CHANGELOG.md @@ -2,6 +2,8 @@ ## 1.19.0.0 +* Add `lookupAccountStateIntern` to `State.Account` module +* Add `HasOKey` instance for `TxId (Tx l era)` * Remove `Generic` instance from `BoundedRatio` type * Remove deprecated function `addrPtrNormalize` * Remove deprecated functions `mkTxIx`, `mkCertIx`, `hashAnchorData` diff --git a/libs/cardano-ledger-core/cardano-ledger-core.cabal b/libs/cardano-ledger-core/cardano-ledger-core.cabal index 93a9a4cbcad..1a7e1af8fc7 100644 --- a/libs/cardano-ledger-core/cardano-ledger-core.cabal +++ b/libs/cardano-ledger-core/cardano-ledger-core.cabal @@ -110,6 +110,7 @@ library cardano-crypto, cardano-crypto-class ^>=2.2, cardano-crypto-wrapper, + cardano-data >=1.3, cardano-ledger-binary ^>=1.8, cardano-ledger-byron, cardano-ledger-core:internal, diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs index b3af188b342..8fd102d550a 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs @@ -185,6 +185,7 @@ import NoThunks.Class (NoThunks (..)) import Numeric.Natural (Natural) import Quiet (Quiet (Quiet)) import System.Random.Stateful (Random, Uniform (..), UniformRange (..)) +import Type.Reflection (typeRep) #if MIN_VERSION_random(1,3,0) import System.Random.Stateful (isInRangeOrd) #endif @@ -343,11 +344,21 @@ fromRatioBoundedRatio ratio lowerBound = minBound :: BoundedRatio b a upperBound = maxBound :: BoundedRatio b a -instance (ToCBOR a, Integral a, Bounded a, Typeable b) => ToCBOR (BoundedRatio b a) where +instance + (ToCBOR a, Integral a, Bounded a, Typeable b, Typeable (BoundedRatio b a)) => + ToCBOR (BoundedRatio b a) + where toCBOR (BoundedRatio u) = Plain.encodeRatioWithTag toCBOR u instance - (FromCBOR a, Bounded (BoundedRatio b a), Bounded a, Integral a, Typeable b, Show a) => + ( FromCBOR a + , Bounded (BoundedRatio b a) + , Bounded a + , Integral a + , Typeable b + , Show a + , Typeable (BoundedRatio b a) + ) => FromCBOR (BoundedRatio b a) where fromCBOR = do @@ -359,7 +370,9 @@ instance instance (ToCBOR (BoundedRatio b a), Typeable b, Typeable a) => EncCBOR (BoundedRatio b a) -instance (FromCBOR (BoundedRatio b a), Typeable b, Typeable a) => DecCBOR (BoundedRatio b a) +instance + (FromCBOR (BoundedRatio b a), Typeable b, Typeable a, Typeable (BoundedRatio b a)) => + DecCBOR (BoundedRatio b a) instance Bounded (BoundedRatio b Word64) => ToJSON (BoundedRatio b Word64) where toJSON :: BoundedRatio b Word64 -> Value @@ -747,7 +760,14 @@ data Mismatch (r :: Relation) a = Mismatch { mismatchSupplied :: !a , mismatchExpected :: !a } - deriving (Eq, Ord, Show, Generic, NFData, ToJSON, FromJSON, NoThunks) + deriving (Eq, Ord, Generic, NFData, ToJSON, FromJSON, NoThunks) + +instance (Typeable r, Show a) => Show (Mismatch (r :: Relation) a) where + show (Mismatch {mismatchSupplied, mismatchExpected}) = + let headerLine = "Mismatch (" <> show (typeRep @r) <> ")" + suppliedLine = "supplied: " <> show mismatchSupplied + expectedLine = "expected: " <> show mismatchExpected + in headerLine <> " {" <> suppliedLine <> ", " <> expectedLine <> "}" -- | Convert a `Mismatch` to a tuple that has "supplied" and "expected" swapped places swapMismatch :: Mismatch r a -> (a, a) diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs index 187cecea3e4..5737dbf2ba5 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs @@ -130,6 +130,7 @@ import qualified Data.Map.Strict as Map import Data.Maybe (fromMaybe, isJust) import Data.Maybe.Strict (StrictMaybe, maybeToStrictMaybe, strictMaybe, strictMaybeToMaybe) import Data.MemPack +import Data.OMap.Strict (HasOKey (..)) import Data.Sequence.Strict (StrictSeq) import Data.Set (Set) import qualified Data.Set as Set @@ -679,3 +680,6 @@ toStrictMaybeL = lens maybeToStrictMaybe (const strictMaybeToMaybe) fromStrictMaybeL :: Lens' (StrictMaybe a) (Maybe a) fromStrictMaybeL = lens strictMaybeToMaybe (const maybeToStrictMaybe) + +instance EraTx era => HasOKey TxId (Tx l era) where + toOKey = txIdTx diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/State/Account.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/State/Account.hs index 5824cbdb6c8..4dcebdacd6d 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/State/Account.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/State/Account.hs @@ -15,6 +15,7 @@ module Cardano.Ledger.State.Account ( CanSetAccounts (..), EraAccounts (..), lookupAccountState, + lookupAccountStateIntern, updateLookupAccountState, isAccountRegistered, adjustAccountState, @@ -44,6 +45,7 @@ import Data.Kind (Type) import qualified Data.Map.Merge.Strict as Map import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map +import Data.MapExtras (lookupInternMap) import Data.Set (Set) import Lens.Micro import NoThunks.Class (NoThunks) @@ -145,6 +147,12 @@ lookupAccountState :: EraAccounts era => Credential 'Staking -> Accounts era -> Maybe (AccountState era) lookupAccountState cred accounts = Map.lookup cred (accounts ^. accountsMapL) +lookupAccountStateIntern :: + EraAccounts era => + Credential 'Staking -> Accounts era -> Maybe (Credential 'Staking, AccountState era) +lookupAccountStateIntern cred accounts = + lookupInternMap cred (accounts ^. accountsMapL) + -- | Update account state. Returns Nothing if the value is not present and modified value otherwise updateLookupAccountState :: EraAccounts era => diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/State/CertState.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/State/CertState.hs index 2bb6eea80b9..bce51d4b0c0 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/State/CertState.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/State/CertState.hs @@ -44,7 +44,7 @@ module Cardano.Ledger.State.CertState ( iRDeltaTreasuryL, dsFutureGenDelegsL, psStakePoolsL, - psFutureStakePoolsL, + psFutureStakePoolParamsL, psRetiringL, psVRFKeyHashesL, ) where @@ -81,7 +81,7 @@ import Cardano.Ledger.DRep (DRep (..), DRepState (..)) import Cardano.Ledger.Hashes (GenDelegPair (..), GenDelegs (..)) import Cardano.Ledger.Slot (EpochNo (..), SlotNo (..)) import Cardano.Ledger.State.Account -import Cardano.Ledger.State.StakePool (StakePoolState (..), spsDelegatorsL) +import Cardano.Ledger.State.StakePool (StakePoolParams, StakePoolState (..), spsDelegatorsL) import Control.DeepSeq (NFData (..)) import Control.Monad.Trans import Data.Aeson (ToJSON (..), object, (.=)) @@ -234,8 +234,8 @@ data PState era = PState -- ^ VRF key hashes that have been registered via PoolParams , psStakePools :: !(Map (KeyHash 'StakePool) StakePoolState) -- ^ The state of current stake pools. - , psFutureStakePools :: !(Map (KeyHash 'StakePool) StakePoolState) - -- ^ The state of future stake pools. + , psFutureStakePoolParams :: !(Map (KeyHash 'StakePool) StakePoolParams) + -- ^ Future pool params -- Changes to existing stake pool parameters are staged in order -- to give delegators time to react to changes. -- See section 11.2, "Example Illustration of the Reward Cycle", @@ -260,9 +260,9 @@ instance DecShareCBOR (PState era) where decSharePlusCBOR = decodeRecordNamedT "PState" (const 4) $ do psVRFKeyHashes <- decSharePlusLensCBOR (toMemptyLens _1 _1) psStakePools <- decSharePlusLensCBOR (toMemptyLens _1 _2) - psFutureStakePools <- decSharePlusLensCBOR (toMemptyLens _1 _2) + psFutureStakePoolParams <- decSharePlusLensCBOR (toMemptyLens _1 _2) psRetiring <- decSharePlusLensCBOR (toMemptyLens _1 _2) - pure PState {psVRFKeyHashes, psStakePools, psFutureStakePools, psRetiring} + pure PState {psVRFKeyHashes, psStakePools, psFutureStakePoolParams, psRetiring} instance (Era era, DecShareCBOR (PState era)) => DecCBOR (PState era) where decCBOR = decNoShareCBOR @@ -271,7 +271,7 @@ instance ToKeyValuePairs (PState era) where toKeyValuePairs PState {..} = [ "vrfKeyHashes" .= psVRFKeyHashes , "stakePools" .= psStakePools - , "futureStakePools" .= psFutureStakePools + , "futureStakePoolParams" .= psFutureStakePoolParams , "retiring" .= psRetiring ] @@ -483,8 +483,8 @@ dsFutureGenDelegsL = lens dsFutureGenDelegs (\ds u -> ds {dsFutureGenDelegs = u} psStakePoolsL :: Lens' (PState era) (Map (KeyHash 'StakePool) StakePoolState) psStakePoolsL = lens psStakePools (\ps u -> ps {psStakePools = u}) -psFutureStakePoolsL :: Lens' (PState era) (Map (KeyHash 'StakePool) StakePoolState) -psFutureStakePoolsL = lens psFutureStakePools (\ps u -> ps {psFutureStakePools = u}) +psFutureStakePoolParamsL :: Lens' (PState era) (Map (KeyHash 'StakePool) StakePoolParams) +psFutureStakePoolParamsL = lens psFutureStakePoolParams (\ps u -> ps {psFutureStakePoolParams = u}) psRetiringL :: Lens' (PState era) (Map (KeyHash 'StakePool) EpochNo) psRetiringL = lens psRetiring (\ps u -> ps {psRetiring = u}) diff --git a/libs/cardano-ledger-test/bench/Bench/Cardano/Ledger/StakeDistr.hs b/libs/cardano-ledger-test/bench/Bench/Cardano/Ledger/StakeDistr.hs index f9a6153c2a9..54b875c33f2 100644 --- a/libs/cardano-ledger-test/bench/Bench/Cardano/Ledger/StakeDistr.hs +++ b/libs/cardano-ledger-test/bench/Bench/Cardano/Ledger/StakeDistr.hs @@ -108,6 +108,9 @@ readNewEpochState = do case Plain.decodeFullDecoder lbl fromCBOR lazyBytes of Left err -> error (show err) Right (nes :: NewEpochState CurrentEra) -> pure nes + -- case Aeson.eitherDecode lazyBytes of + -- Left err -> error (show err) + -- Right (nes :: NewEpochState CurrentEra) -> pure nes Nothing -> bogusNewEpochState <$ do putStrLn $ diff --git a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/ModelState.hs b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/ModelState.hs index c7d0afe95ed..c4e9be7c3f6 100644 --- a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/ModelState.hs +++ b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/ModelState.hs @@ -109,7 +109,7 @@ data ModelNewEpochState era = ModelNewEpochState , mCount :: !Int , mIndex :: !(Map Int TxId) , -- below here NO EFFECT until we model EpochBoundary - mFStakePools :: !(Map (KeyHash 'StakePool) StakePoolState) + mFStakePools :: !(Map (KeyHash 'StakePool) StakePoolParams) , mRetiring :: !(Map (KeyHash 'StakePool) EpochNo) , mSnapshots :: !SnapShots , mEL :: !EpochNo -- The current epoch, @@ -161,7 +161,7 @@ pStateZero = PState { psVRFKeyHashes = Map.empty , psStakePools = Map.empty - , psFutureStakePools = Map.empty + , psFutureStakePoolParams = Map.empty , psRetiring = Map.empty } @@ -340,7 +340,7 @@ abstract x = , mIndex = Map.empty , -- below here NO EFFECT until we model EpochBoundary mFStakePools = - ( psFutureStakePools + ( psFutureStakePoolParams . certPState . lsCertState . esLState diff --git a/libs/ledger-state/app-canonical/Main.hs b/libs/ledger-state/app-canonical/Main.hs new file mode 100644 index 00000000000..0ebb6519c58 --- /dev/null +++ b/libs/ledger-state/app-canonical/Main.hs @@ -0,0 +1,188 @@ +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE PackageImports #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -fno-warn-unused-imports #-} +{-# OPTIONS_GHC -fno-warn-deprecations #-} +module Main where + +import Cardano.Ledger.Shelley.LedgerState +-- import Cardano.Ledger.State.Query +import Cardano.Ledger.State.UTxO + +import Control.Exception (throwIO) +import qualified Data.Aeson as Aeson +import qualified Cardano.Ledger.Shelley.LedgerState as Shelley +import Data.Aeson.Types (Value) +import Data.Bifunctor (first) +import Control.Monad +import Data.Function ((&)) +import Cardano.SCLS.Internal.Serializer.Dump.Plan +import qualified Data.ByteString.Lazy as LBS +import qualified Data.Text as T +import Cardano.Ledger.Mary (MaryEra) +-- import Data.Text as T (pack) +import qualified Data.ByteString.Base16.Lazy as Base16 +import Cardano.Ledger.Binary.Plain as Plain +import Options.Applicative +import Cardano.Ledger.Api.Era +import Cardano.Ledger.Export.Namespace.UTxO +import Cardano.Ledger.State -- Core (UTxO (..)) +-- import Cardano.Ledger.UTxO +-- import Cardano.Ledger.State.UTxO (CurrentEra) -- , readHexUTxO, readNewEpochState) +import Cardano.Chain.UTxO (TxIn, TxId) -- , TxOut) +import qualified Data.Map as Map +import Data.Proxy (Proxy(..)) +import Data.Foldable (for_) +import qualified Streaming as S +import qualified Streaming.Prelude as S +import Data.Word (Word16) +-- import qualified GHC.Exts as GHC +import qualified GHC.Generics as GHC + +import Cardano.Ledger.Export.Namespace.UTxO +-- import Cardano.Ledger.Conway.Era + +import qualified Cardano.SCLS.Internal.Serializer.External.Impl as External (serialize) +import Cardano.SCLS.Internal.Entry +import Cardano.SCLS.Internal.Serializer.MemPack +import Cardano.Types.Network (NetworkId (..)) +import Cardano.Types.SlotNo (SlotNo (..)) +import qualified Cardano.Ledger.Babbage.TxOut as Babbage +import qualified Cardano.Ledger.Shelley.TxOut as Shelley () + +-- import Cardano.Ledger.State +-- import Cardano.Ledger.State.UTxO (readHexUTxO) +import System.IO + +import Debug.Trace (traceM) + +-- | Insight into options: +-- +-- * `optsNewEpochStateBinaryFile` is for reading a previously serialized +-- * `NewEpochState` produced by cardano-cli` and is used to populate sqlite +-- * database +-- +-- * `optsEpochStateBinaryFile` is used for grabbing data from sqlite, +-- * constructing `EpochState` (in a new format) and writing it into the cbor +-- * serialized file +data Opts = Opts + { optsNewEpochStateBinaryFile :: Maybe FilePath + -- ^ Path to the CBOR encoded NewEpochState data type, which will be used to + -- load into sqlite database + , optsEpochStateBinaryFile :: Maybe FilePath + -- ^ Path to the CBOR encoded EpochState data type, which will have data + -- from sqlite database written into it. + , optsSqliteDbFile :: Maybe FilePath + -- ^ Path to Sqlite database file. + } + deriving (Show) + +data Cmd + = CmdCreateFile FilePath FilePath + | CmdCreateStateFile FilePath FilePath + deriving (Show) + +optsParser :: Parser Cmd +optsParser = hsubparser + (command "create" (info createCommand (progDesc "Create canonical file for utxo")) + <> command "create-state" (info createStateCommand (progDesc "Create canonical file for ledger state")) + ) + where + createCommand = CmdCreateFile + <$> argument str (metavar "UTXO_HEX_FILE") + <*> argument str (metavar "SCLS_FILE") + createStateCommand = CmdCreateStateFile + <$> argument str (metavar "STATE_JSON_FILE") + <*> argument str (metavar "SCLS_FILE") + +main :: IO () +main = do + hSetBuffering stdout LineBuffering + cmd <- + execParser $ + info + ( optsParser + <* abortOption + (ShowHelpText Nothing) + (long "help" <> short 'h' <> help "Display this message.") + ) + (header "canonical-state - Tool for working with canonical ledger state representation") + case cmd of + -- cabal run canonical-ledger -- create ~/iohk/chain/mainnet/utxo.hex 1.scls + CmdCreateFile utxoFilePath fileName -> do + putStrLn "Creating file..." + putStrLn $ "Reading UTxO from " ++ utxoFilePath + UTxO utxo <- localReadDecCBORHex utxoFilePath + + External.serialize + fileName + Mainnet + (SlotNo 1) +<<<<<<< Updated upstream +======= +<<<<<<< Updated upstream + $ S.each + [ "utxo" S.:> + S.each + [ ChunkEntry + (UtxoKeyIn txin) + (RawBytes $ toStrictByteString $ toCanonicalCBOR (Proxy :: Proxy V1) $ txout + ) + | (txin, txout) <- Map.toList utxo + ] + ] +======= +>>>>>>> Stashed changes + (defaultSerializationPlan & addChunks + (S.each + [ "utxo/v0" S.:> + (S.each + [ ChunkEntry + (UtxoKeyIn txin) + (RawBytes $ toStrictByteString $ toCanonicalCBOR (Proxy :: Proxy V1) $ UtxoOutBabbage txout) + | (txin, txout) <- Map.toList utxo + ]) + ])) +<<<<<<< Updated upstream +======= + CmdCreateStateFile stateFilePath _fileName -> do + putStrLn "Creating state file..." + putStrLn $ "Reading State from " ++ stateFilePath + _nes <- readNewEpochState stateFilePath + putStrLn "hohohoho" + -- val <- Aeson.decodeFileStrict stateFilePath + -- print (val :: Maybe (Serialised (Shelley.NewEpochState ConwayEra))) + + -- External.serialize + -- fileName + -- Mainnet + -- (SlotNo 1) + -- (defaultSerializationPlan & addChunks + -- (S.each + -- [ "utxo-state/v0" S.:> + -- (S.each + -- [ ChunkEntry + -- (UtxoKeyIn txin) + -- (RawBytes $ toStrictByteString $ toCanonicalCBOR (Proxy :: Proxy V1) $ UtxoOutBabbage txout) + -- | (txin, txout) <- Map.toList utxo + -- ]) + -- ])) +>>>>>>> Stashed changes +>>>>>>> Stashed changes + +data TxIn' = TxIn' TxId Word16 + +instance FromCBOR TxIn' where + fromCBOR = decodeRecordNamed "TxIn" + (const 2) + (TxIn' <$> fromCBOR <*> fromCBOR) + +localReadDecCBORHex :: FilePath -> IO (UTxO ConwayEra) +localReadDecCBORHex = either throwIO pure . decodeFullHex <=< LBS.readFile + where + decodeFullHex = + Plain.decodeFull + <=< first (DecoderErrorCustom "Invalid Hex encoding:" . T.pack) . Base16.decode \ No newline at end of file diff --git a/libs/ledger-state/app/Main.hs b/libs/ledger-state/app/Main.hs index 758f98c5b1e..93d64572ac2 100644 --- a/libs/ledger-state/app/Main.hs +++ b/libs/ledger-state/app/Main.hs @@ -79,6 +79,7 @@ main = do ) (header "ledger-state - Tool for analyzing ledger state") forM_ (optsNewEpochStateBinaryFile opts) $ \binFp -> do + putStrLn $ "Reading NewEpochState from " ++ binFp nes <- readNewEpochState binFp case optsSqliteDbFile opts of Nothing -> printNewEpochStateStats $ countNewEpochStateStats nes diff --git a/libs/ledger-state/ledger-state.cabal b/libs/ledger-state/ledger-state.cabal index de13969b529..97f0141b206 100644 --- a/libs/ledger-state/ledger-state.cabal +++ b/libs/ledger-state/ledger-state.cabal @@ -82,6 +82,41 @@ executable ledger-state optparse-applicative, text, +executable canonical-ledger + main-is: Main.hs + hs-source-dirs: app-canonical + default-language: Haskell2010 + ghc-options: + -Wall + -Wcompat + -Wincomplete-record-updates + -Wincomplete-uni-patterns + -Wredundant-constraints + -O2 + -threaded + -rtsopts + + build-depends: + base, + aeson, + bytestring, + cardano-ledger-shelley:{cardano-ledger-shelley}, + cardano-ledger-babbage:{cardano-ledger-babbage}, + cardano-ledger-byron:{cardano-ledger-byron}, + cardano-ledger-conway:{scls-export,cardano-ledger-conway}, + cardano-ledger-mary:{cardano-ledger-mary}, + cardano-ledger-api, + cardano-ledger-binary, + cardano-ledger-core, + -- cardano-ledger-core, + ledger-state, + base16-bytestring, + containers, + optparse-applicative, + streaming, + scls-format, + text, + benchmark memory type: exitcode-stdio-1.0 main-is: Memory.hs diff --git a/libs/ledger-state/src/Cardano/Ledger/State/UTxO.hs b/libs/ledger-state/src/Cardano/Ledger/State/UTxO.hs index 9e02e1dad3c..36570214745 100644 --- a/libs/ledger-state/src/Cardano/Ledger/State/UTxO.hs +++ b/libs/ledger-state/src/Cardano/Ledger/State/UTxO.hs @@ -486,11 +486,11 @@ countPStateStats PState {..} = PStateStats { pssKeyHashStakePool = statMapKeys psStakePools - <> statMapKeys psFutureStakePools + <> statMapKeys psFutureStakePoolParams <> statMapKeys psRetiring , pssPoolParamsStats = foldMap countPoolParamsStats (Map.mapWithKey stakePoolStateToStakePoolParams psStakePools) - <> foldMap countPoolParamsStats (Map.mapWithKey stakePoolStateToStakePoolParams psFutureStakePools) + <> foldMap countPoolParamsStats psFutureStakePoolParams } data LedgerStateStats = LedgerStateStats