Skip to content

Commit

Permalink
Account for change in strategy with respect to Enc/DecCBOR for ledger…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
lehins committed Feb 7, 2023
1 parent a7aef63 commit cc31cfc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 57 deletions.
8 changes: 4 additions & 4 deletions cabal.project
Expand Up @@ -122,8 +122,8 @@ allow-newer:
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: aff148d8c4677341a82b38da3e2b2f18e52dafe9
--sha256: 1qyd2skr25znrd8dd8ykqrfanbhks54bm2jig77cm9qm5j70g8cz
tag: f58f00155427657147069d7919d3c3365bc5fea5
--sha256: 0424hi45krfsjinb5gnasp1afwf3fxdcfb5lwz2jdzy4m9fcgv5s
subdir:
base-deriving-via
binary
Expand All @@ -140,8 +140,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-ledger
tag: 43662c17ef81d9f00bc104d73a05536e3eee2bf0
--sha256: 1iyv3byfnqw5sjg5jxay4b9pb1y2y4l3c5jdh4dnamg6mqg4cqm7
tag: a4e72113f0326abad853dbcec58784960292f993
--sha256: 0jsxwbk241xfrykpdzxvhk0ni4lwdv7gmarm7zyrl0pdb1kdsswi
subdir:
eras/alonzo/impl
eras/alonzo/test-suite
Expand Down
Expand Up @@ -54,7 +54,7 @@ import Cardano.Ledger.Babbage (BabbageEra)
import qualified Cardano.Ledger.Babbage.Rules as Babbage
import qualified Cardano.Ledger.Babbage.Translation as Babbage
import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Binary (DecCBOR, EncCBOR, FromCBOR, ToCBOR)
import Cardano.Ledger.Binary (FromCBOR, ToCBOR)
import Cardano.Ledger.Conway (ConwayEra)
import qualified Cardano.Ledger.Conway.Translation as Conway
import Cardano.Ledger.Core as Core
Expand Down Expand Up @@ -130,17 +130,17 @@ class ( Core.EraSegWits era
, Default (State (Core.EraRule "PPUP" era))

, NoThunks (SL.StashedAVVMAddresses era)
, EncCBOR (SL.StashedAVVMAddresses era)
, DecCBOR (SL.StashedAVVMAddresses era)
, ToCBOR (SL.StashedAVVMAddresses era)
, FromCBOR (SL.StashedAVVMAddresses era)
, Show (SL.StashedAVVMAddresses era)
, Eq (SL.StashedAVVMAddresses era)

-- TODO: this constraint will have to be removed in Conway and
-- GetProposedPParamsUpdates query will need adjustment
, SL.PPUPState era ~ SL.ShelleyPPUPState era
, NoThunks (SL.PPUPState era)
, EncCBOR (SL.PPUPState era)
, DecCBOR (SL.PPUPState era)
, ToCBOR (SL.PPUPState era)
, FromCBOR (SL.PPUPState era)
, Show (SL.PPUPState era)
, Eq (SL.PPUPState era)

Expand Down
Expand Up @@ -50,9 +50,9 @@ import Ouroboros.Consensus.Util.Condense

import Cardano.Ledger.Binary (Annotator (..), FromCBOR (..),
FullByteString (..), ToCBOR (..), serialize,
toPlainDecoder, toPlainEncoding)
toPlainDecoder)
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Core as SL (eraProtVerLow)
import Cardano.Ledger.Core as SL (eraProtVerLow, encEraToCBOR)
import Cardano.Ledger.Crypto (HASH)
import qualified Cardano.Ledger.Era as SL (hashTxSeq)
import qualified Cardano.Ledger.Shelley.API as SL
Expand Down Expand Up @@ -252,7 +252,7 @@ instance ShelleyCompatible proto era => FromCBOR (Annotator (Header (ShelleyBloc
encodeShelleyBlock ::
forall proto era. ShelleyCompatible proto era
=> ShelleyBlock proto era -> Plain.Encoding
encodeShelleyBlock = toPlainEncoding (eraProtVerLow @era) . toCBOR
encodeShelleyBlock = encEraToCBOR @era

decodeShelleyBlock ::
forall proto era. ShelleyCompatible proto era
Expand All @@ -271,7 +271,7 @@ shelleyBinaryBlockInfo blk = BinaryBlockInfo {
encodeShelleyHeader ::
forall proto era. ShelleyCompatible proto era
=> Header (ShelleyBlock proto era) -> Plain.Encoding
encodeShelleyHeader = toPlainEncoding (eraProtVerLow @era) . toCBOR
encodeShelleyHeader = encEraToCBOR @era

decodeShelleyHeader ::
forall proto era. ShelleyCompatible proto era
Expand Down
Expand Up @@ -33,7 +33,7 @@ import Codec.CBOR.Decoding (Decoder)
import qualified Codec.CBOR.Decoding as CBOR
import Codec.CBOR.Encoding (Encoding)
import qualified Codec.CBOR.Encoding as CBOR
import Codec.Serialise (Serialise, decode, encode)
import Codec.Serialise (decode, encode)
import Control.DeepSeq (NFData)
import Data.Kind (Type)
import Data.Map.Strict (Map)
Expand All @@ -46,8 +46,7 @@ import GHC.Generics (Generic)

import Cardano.Binary (DecCBOR (..), EncCBOR (..), encodeListLen,
enforceSize)
import Cardano.Ledger.Binary (FromCBOR (..), ToCBOR (..),
toPlainDecoder, toPlainEncoding)
import Cardano.Ledger.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Ledger.UMapCompact (View (..), domRestrictedView,
rewView)

Expand Down Expand Up @@ -91,16 +90,12 @@ newtype NonMyopicMemberRewards c = NonMyopicMemberRewards {
(Map (SL.KeyHash 'SL.StakePool c) SL.Coin)
}
deriving stock (Show)
deriving newtype (Eq)
deriving newtype (Eq, ToCBOR, FromCBOR)

type Delegations c =
Map (SL.Credential 'SL.Staking c)
(SL.KeyHash 'SL.StakePool c)

instance Crypto c => Serialise (NonMyopicMemberRewards c) where
encode = encCBOR . unNonMyopicMemberRewards
decode = NonMyopicMemberRewards <$> decCBOR

data instance BlockQuery (ShelleyBlock proto era) :: Type -> Type where
GetLedgerTip :: BlockQuery (ShelleyBlock proto era) (Point (ShelleyBlock proto era))
GetEpochNo :: BlockQuery (ShelleyBlock proto era) EpochNo
Expand Down Expand Up @@ -581,24 +576,23 @@ encodeShelleyQuery query = case query of
GetEpochNo ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 1
GetNonMyopicMemberRewards creds ->
CBOR.encodeListLen 2 <> CBOR.encodeWord8 2 <> encCBOR creds
CBOR.encodeListLen 2 <> CBOR.encodeWord8 2 <> LC.encEraToCBOR @era creds
GetCurrentPParams ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 3
GetProposedPParamsUpdates ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 4
GetStakeDistribution ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 5
GetUTxOByAddress addrs ->
CBOR.encodeListLen 2 <> CBOR.encodeWord8 6 <>
toPlainEncoding (LC.eraProtVerLow @era) (toCBOR addrs)
CBOR.encodeListLen 2 <> CBOR.encodeWord8 6 <> LC.encEraToCBOR @era addrs
GetUTxOWhole ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 7
DebugEpochState ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 8
GetCBOR query' ->
CBOR.encodeListLen 2 <> CBOR.encodeWord8 9 <> encodeShelleyQuery query'
GetFilteredDelegationsAndRewardAccounts creds ->
CBOR.encodeListLen 2 <> CBOR.encodeWord8 10 <> encCBOR creds
CBOR.encodeListLen 2 <> CBOR.encodeWord8 10 <> LC.encEraToCBOR @era creds
GetGenesisConfig ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 11
DebugNewEpochState ->
Expand All @@ -608,7 +602,7 @@ encodeShelleyQuery query = case query of
GetRewardProvenance ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 14
GetUTxOByTxIn txins ->
CBOR.encodeListLen 2 <> CBOR.encodeWord8 15 <> encCBOR txins
CBOR.encodeListLen 2 <> CBOR.encodeWord8 15 <> LC.encEraToCBOR @era txins
GetStakePools ->
CBOR.encodeListLen 1 <> CBOR.encodeWord8 16
GetStakePoolParams poolids ->
Expand All @@ -631,20 +625,20 @@ decodeShelleyQuery = do
case (len, tag) of
(1, 0) -> return $ SomeSecond GetLedgerTip
(1, 1) -> return $ SomeSecond GetEpochNo
(2, 2) -> SomeSecond . GetNonMyopicMemberRewards <$> decCBOR
(2, 2) -> SomeSecond . GetNonMyopicMemberRewards <$> LC.decEraFromCBOR @era
(1, 3) -> return $ SomeSecond GetCurrentPParams
(1, 4) -> return $ SomeSecond GetProposedPParamsUpdates
(1, 5) -> return $ SomeSecond GetStakeDistribution
(2, 6) -> SomeSecond . GetUTxOByAddress <$> toPlainDecoder (LC.eraProtVerLow @era) fromCBOR
(2, 6) -> SomeSecond . GetUTxOByAddress <$> LC.decEraFromCBOR @era
(1, 7) -> return $ SomeSecond GetUTxOWhole
(1, 8) -> return $ SomeSecond DebugEpochState
(2, 9) -> (\(SomeSecond q) -> SomeSecond (GetCBOR q)) <$> decodeShelleyQuery
(2, 10) -> SomeSecond . GetFilteredDelegationsAndRewardAccounts <$> decCBOR
(2, 10) -> SomeSecond . GetFilteredDelegationsAndRewardAccounts <$> LC.decEraFromCBOR @era
(1, 11) -> return $ SomeSecond GetGenesisConfig
(1, 12) -> return $ SomeSecond DebugNewEpochState
(1, 13) -> return $ SomeSecond DebugChainDepState
(1, 14) -> return $ SomeSecond GetRewardProvenance
(2, 15) -> SomeSecond . GetUTxOByTxIn <$> decCBOR
(2, 15) -> SomeSecond . GetUTxOByTxIn <$> LC.decEraFromCBOR @era
(1, 16) -> return $ SomeSecond GetStakePools
(2, 17) -> SomeSecond . GetStakePoolParams <$> decCBOR
(1, 18) -> return $ SomeSecond GetRewardInfoPools
Expand All @@ -660,55 +654,55 @@ encodeShelleyResult ::
=> BlockQuery (ShelleyBlock proto era) result -> result -> Encoding
encodeShelleyResult query = case query of
GetLedgerTip -> encodePoint encode
GetEpochNo -> encode
GetNonMyopicMemberRewards {} -> encode
GetEpochNo -> encCBOR
GetNonMyopicMemberRewards {} -> LC.encEraToCBOR @era
GetCurrentPParams -> encCBOR
GetProposedPParamsUpdates -> encCBOR
GetStakeDistribution -> encCBOR
GetStakeDistribution -> LC.encEraToCBOR @era
GetUTxOByAddress {} -> encCBOR
GetUTxOWhole -> encCBOR
DebugEpochState -> encCBOR
GetCBOR {} -> encode
GetFilteredDelegationsAndRewardAccounts {} -> encCBOR
GetFilteredDelegationsAndRewardAccounts {} -> LC.encEraToCBOR @era
GetGenesisConfig -> encCBOR
DebugNewEpochState -> encCBOR
DebugChainDepState -> encode
GetRewardProvenance -> toPlainEncoding (LC.eraProtVerLow @era) . toCBOR
GetRewardProvenance -> LC.encEraToCBOR @era
GetUTxOByTxIn {} -> encCBOR
GetStakePools -> encCBOR
GetStakePoolParams {} -> encCBOR
GetRewardInfoPools -> toPlainEncoding (LC.eraProtVerLow @era) . toCBOR
GetPoolState {} -> encCBOR
GetStakePoolParams {} -> LC.encEraToCBOR @era
GetRewardInfoPools -> LC.encEraToCBOR @era
GetPoolState {} -> LC.encEraToCBOR @era
GetStakeSnapshots {} -> encCBOR
GetPoolDistr {} -> encCBOR
GetPoolDistr {} -> LC.encEraToCBOR @era

decodeShelleyResult ::
forall proto era result. ShelleyCompatible proto era
=> BlockQuery (ShelleyBlock proto era) result
-> forall s. Decoder s result
decodeShelleyResult query = case query of
GetLedgerTip -> decodePoint decode
GetEpochNo -> decode
GetNonMyopicMemberRewards {} -> decode
GetEpochNo -> decCBOR
GetNonMyopicMemberRewards {} -> LC.decEraFromCBOR @era
GetCurrentPParams -> decCBOR
GetProposedPParamsUpdates -> decCBOR
GetStakeDistribution -> decCBOR
GetStakeDistribution -> LC.decEraFromCBOR @era
GetUTxOByAddress {} -> decCBOR
GetUTxOWhole -> decCBOR
DebugEpochState -> decCBOR
GetCBOR {} -> decode
GetFilteredDelegationsAndRewardAccounts {} -> decCBOR
GetFilteredDelegationsAndRewardAccounts {} -> LC.decEraFromCBOR @era
GetGenesisConfig -> decCBOR
DebugNewEpochState -> decCBOR
DebugChainDepState -> decode
GetRewardProvenance -> toPlainDecoder (LC.eraProtVerLow @era) fromCBOR
GetRewardProvenance -> LC.decEraFromCBOR @era
GetUTxOByTxIn {} -> decCBOR
GetStakePools -> decCBOR
GetStakePoolParams {} -> decCBOR
GetRewardInfoPools -> toPlainDecoder (LC.eraProtVerLow @era) fromCBOR
GetPoolState {} -> decCBOR
GetStakePoolParams {} -> LC.decEraFromCBOR @era
GetRewardInfoPools -> LC.decEraFromCBOR @era
GetPoolState {} -> LC.decEraFromCBOR @era
GetStakeSnapshots {} -> decCBOR
GetPoolDistr {} -> decCBOR
GetPoolDistr {} -> LC.decEraFromCBOR @era

-- | The stake snapshot returns information about the mark, set, go ledger snapshots for a pool,
-- plus the total active stake for each snapshot that can be used in a 'sigma' calculation.
Expand Down
Expand Up @@ -11,9 +11,8 @@ import Control.Exception (Exception, throw)
import qualified Data.ByteString.Lazy as Lazy
import Data.Typeable (Typeable)

import Cardano.Ledger.Binary (decCBOR, encCBOR, fromCBOR, toCBOR,
toPlainDecoder, toPlainEncoding)
import Cardano.Ledger.Core (eraProtVerLow)
import Cardano.Ledger.Binary (decCBOR, encCBOR)
import Cardano.Ledger.Core (decEraFromCBOR, encEraToCBOR)
import Codec.Serialise (decode, encode)

import Ouroboros.Network.Block (Serialised, unwrapCBORinCBOR,
Expand Down Expand Up @@ -131,8 +130,8 @@ instance ShelleyCompatible proto era

instance ShelleyCompatible proto era
=> SerialiseNodeToNode (ShelleyBlock proto era) (GenTxId (ShelleyBlock proto era)) where
encodeNodeToNode _ _ = toPlainEncoding (eraProtVerLow @era) . toCBOR
decodeNodeToNode _ _ = toPlainDecoder (eraProtVerLow @era) $ fromCBOR
encodeNodeToNode _ _ = encEraToCBOR @era
decodeNodeToNode _ _ = decEraFromCBOR @era

{-------------------------------------------------------------------------------
SerialiseNodeToClient
Expand Down Expand Up @@ -172,13 +171,13 @@ instance ShelleyCompatible proto era

instance ShelleyCompatible proto era
=> SerialiseNodeToClient (ShelleyBlock proto era) (GenTxId (ShelleyBlock proto era)) where
encodeNodeToClient _ _ = toPlainEncoding (eraProtVerLow @era) . toCBOR
decodeNodeToClient _ _ = toPlainDecoder (eraProtVerLow @era) $ fromCBOR
encodeNodeToClient _ _ = encEraToCBOR @era
decodeNodeToClient _ _ = decEraFromCBOR @era

-- | @'ApplyTxErr' '(ShelleyBlock era)'@
instance ShelleyBasedEra era => SerialiseNodeToClient (ShelleyBlock proto era) (SL.ApplyTxError era) where
encodeNodeToClient _ _ = toPlainEncoding (eraProtVerLow @era) . toCBOR
decodeNodeToClient _ _ = toPlainDecoder (eraProtVerLow @era) $ fromCBOR
encodeNodeToClient _ _ = encEraToCBOR @era
decodeNodeToClient _ _ = decEraFromCBOR @era

instance ShelleyCompatible proto era
=> SerialiseNodeToClient (ShelleyBlock proto era) (SomeSecond BlockQuery (ShelleyBlock proto era)) where
Expand Down

0 comments on commit cc31cfc

Please sign in to comment.