Skip to content

Commit

Permalink
Adapt to latest changes in ledger.
Browse files Browse the repository at this point in the history
1. Abstracted protocol parameters

The set of protocol parameters may now change from era to era (in
particular, in Alonzo new protocol parameters will be introduced.) We
add the set of required parameters to `ShelleyBasedEra` so that they may
be used throughout consensus, and switch to using `HasField` accessors
everywhere.

2. Safe hash

The hash abstraction in the ledger has now changed somewhat. Generally,
this entails working within the `SafeHash` abstraction over hashes. This
is principally a concern in tests - in regular code, this is all hidden
within the ledger.

3. New reward format

The internal structure for storing the reward update has now changed to
track more provenance information. This will be helpful for debugging
issues with rewards in the future.
  • Loading branch information
nc6 committed Feb 22, 2021
1 parent 62fc0e6 commit 9ebef06
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 89 deletions.
Expand Up @@ -37,15 +37,14 @@ import qualified Cardano.Crypto.Signing as Byron
import qualified Cardano.Chain.Common as Byron
import Cardano.Chain.Genesis (GeneratedSecrets (..))

import qualified Cardano.Ledger.SafeHash as SL
import Cardano.Ledger.Val ((<->))
import qualified Shelley.Spec.Ledger.Address as SL (BootstrapAddress (..))
import qualified Shelley.Spec.Ledger.Address.Bootstrap as SL
(makeBootstrapWitness)
import qualified Shelley.Spec.Ledger.API as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL (truncateUnitInterval)
import qualified Shelley.Spec.Ledger.Tx as SL (WitnessSetHKD (..))
import qualified Shelley.Spec.Ledger.TxBody as SL (EraIndependentTxBody,
eraIndTxBodyHash)
import qualified Shelley.Spec.Ledger.UTxO as SL (makeWitnessVKey)

import Ouroboros.Consensus.Shelley.Ledger (GenTx, ShelleyBlock,
Expand Down Expand Up @@ -186,13 +185,13 @@ migrateUTxO migrationInfo curSlot lcfg lst
, SL._wdrls = SL.Wdrl Map.empty
}

bodyHash :: SL.Hash c SL.EraIndependentTxBody
bodyHash = SL.eraIndTxBodyHash body
bodyHash :: SL.SafeHash c SL.EraIndependentTxBody
bodyHash = SL.hashAnnotated body

-- Witness the use of bootstrap address's utxo.
byronWit :: SL.BootstrapWitness c
byronWit =
SL.makeBootstrapWitness bodyHash byronSK $
SL.makeBootstrapWitness (SL.extractHash bodyHash) byronSK $
Byron.addrAttributes byronAddr

-- Witness the stake delegation.
Expand Down
Expand Up @@ -73,6 +73,7 @@ import Ouroboros.Consensus.Util.Time
import qualified Cardano.Ledger.AuxiliaryData as SL (AuxiliaryDataHash (..))
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Crypto (ADDRHASH, Crypto, DSIGN, HASH, VRF)
import qualified Cardano.Ledger.SafeHash as SL
import qualified Control.State.Transition.Extended as SL (PredicateFailure)
import Shelley.Spec.Ledger.API (StrictMaybe (..))
import qualified Shelley.Spec.Ledger.API as SL
Expand All @@ -84,11 +85,11 @@ import qualified Shelley.Spec.Ledger.Delegation.Certificates as SL
(IndividualPoolStake (..))
import qualified Shelley.Spec.Ledger.EpochBoundary as SL (BlocksMade (..),
emptySnapShots)
import qualified Shelley.Spec.Ledger.Hashing as SL (hashAnnotated)
import qualified Shelley.Spec.Ledger.Keys as SL (asWitness, hashWithSerialiser,
signedKES)
import qualified Shelley.Spec.Ledger.PParams as SL (emptyPParams,
emptyPParamsUpdate)
import qualified Shelley.Spec.Ledger.Rewards as SL
import qualified Shelley.Spec.Ledger.STS.Delegs as SL
(DelegsPredicateFailure (..))
import qualified Shelley.Spec.Ledger.STS.Ledger as SL
Expand All @@ -104,7 +105,7 @@ import qualified Test.Shelley.Spec.Ledger.Utils as SL hiding (mkKeyPair,
mkKeyPair', mkVRFKeyPair)

import qualified Cardano.Ledger.Mary.Value as MA
import qualified Cardano.Ledger.Shelley.Constraints as SL (makeTxOut)
import qualified Cardano.Ledger.Shelley.Constraints as SL (PParamsDelta, makeTxOut)
import qualified Cardano.Ledger.ShelleyMA as MA
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as MA
import qualified Cardano.Ledger.ShelleyMA.Timelocks as MA
Expand Down Expand Up @@ -155,6 +156,11 @@ codecConfig = ShelleyCodecConfig
mkDummyHash :: forall h a. HashAlgorithm h => Proxy h -> Int -> Hash.Hash h a
mkDummyHash _ = coerce . SL.hashWithSerialiser @h toCBOR

mkDummySafeHash :: forall c a. Crypto c => Proxy c -> Int -> SL.SafeHash c a
mkDummySafeHash _ =
SL.unsafeMakeSafeHash
. mkDummyHash (Proxy @(HASH c))

mkKeyHash :: forall c discriminator. Crypto c => Int -> SL.KeyHash discriminator c
mkKeyHash = SL.KeyHash . mkDummyHash (Proxy @(ADDRHASH c))

Expand Down Expand Up @@ -207,6 +213,8 @@ examples ::
~ SL.LedgerPredicateFailure era
, SL.PredicateFailure (Core.EraRule "DELEGS" era)
~ SL.DelegsPredicateFailure era
, Core.PParams era ~ SL.PParams era
, SL.PParamsDelta era ~ SL.PParams' StrictMaybe era
)
=> Core.Value era
-> Core.TxBody era
Expand Down Expand Up @@ -322,10 +330,13 @@ exampleTxBodyShelley = SL.TxBody
-- Dummy hash to decouple from the auxiliaryData in 'exampleTx'.
auxiliaryDataHash :: SL.AuxiliaryDataHash StandardCrypto
auxiliaryDataHash =
SL.AuxiliaryDataHash $ mkDummyHash (Proxy @(HASH StandardCrypto)) 30
SL.AuxiliaryDataHash $ mkDummySafeHash (Proxy @StandardCrypto) 30

exampleTxBodyMA ::
forall era. ShelleyBasedEra era
forall era.
( ShelleyBasedEra era
, SL.PParamsDelta era ~ SL.PParams' StrictMaybe era
)
=> Core.Value era -> MA.TxBody era
exampleTxBodyMA value = MA.TxBody
exampleTxIns
Expand All @@ -343,7 +354,7 @@ exampleTxBodyMA value = MA.TxBody
-- Dummy hash to decouple from the auxiliary data in 'exampleTx'.
auxiliaryDataHash :: SL.AuxiliaryDataHash (EraCrypto era)
auxiliaryDataHash =
SL.AuxiliaryDataHash $ mkDummyHash (Proxy @(HASH (EraCrypto era))) 30
SL.AuxiliaryDataHash $ mkDummySafeHash (Proxy @(EraCrypto era)) 30

exampleTxBodyAllegra :: Core.TxBody StandardAllegra
exampleTxBodyAllegra = exampleTxBodyMA exampleCoin
Expand Down Expand Up @@ -388,7 +399,7 @@ exampleAuxiliaryDataMA =

exampleTxIns :: Crypto c => Set (SL.TxIn c)
exampleTxIns = Set.fromList [
SL.TxIn (SL.TxId (mkDummyHash Proxy 1)) 0
SL.TxIn (SL.TxId (mkDummySafeHash Proxy 1)) 0
]

exampleCerts :: Crypto c => StrictSeq (SL.DCert c)
Expand All @@ -413,7 +424,10 @@ examplePoolDistr = SL.PoolDistr $ Map.fromList [
(SL.hashVerKeyVRF (snd (SL.vrf (exampleKeys @c)))))
]

exampleProposedPPUpdates :: ShelleyBasedEra era => SL.ProposedPPUpdates era
exampleProposedPPUpdates ::
( SL.PParamsDelta era ~ SL.PParams' StrictMaybe era
, ShelleyBasedEra era
) => SL.ProposedPPUpdates era
exampleProposedPPUpdates = SL.ProposedPPUpdates $
Map.singleton
(mkKeyHash 1)
Expand Down Expand Up @@ -540,7 +554,10 @@ exampleChainDepState = TPraosState (NotOrigin 1) (mkPrtclState 1)
-- | This is probably not a valid ledger. We don't care, we are only
-- interested in serialisation, not validation.
exampleNewEpochState ::
forall era. ShelleyBasedEra era
forall era.
( ShelleyBasedEra era
, Core.PParams era ~ SL.PParams era
)
=> Core.Value era
-> SL.NewEpochState era
exampleNewEpochState value = SL.NewEpochState {
Expand All @@ -562,7 +579,7 @@ exampleNewEpochState value = SL.NewEpochState {
, esLState = SL.LedgerState {
_utxoState = SL.UTxOState {
_utxo = SL.UTxO $ Map.fromList [
(SL.TxIn (SL.TxId (mkDummyHash Proxy 1)) 0,
(SL.TxIn (SL.TxId (mkDummySafeHash Proxy 1)) 0,
SL.makeTxOut (Proxy @era) addr value)
]
, _deposited = SL.Coin 1000
Expand All @@ -587,7 +604,13 @@ exampleNewEpochState value = SL.NewEpochState {
rewardUpdate = SL.RewardUpdate {
deltaT = SL.DeltaCoin 10
, deltaR = SL.DeltaCoin (- 100)
, rs = Map.singleton (keyToCredential exampleStakeKey) (SL.Coin 10)
, rs = Map.singleton
(keyToCredential exampleStakeKey) $
Set.singleton $ SL.Reward {
SL.rewardType = SL.MemberReward
, SL.rewardPool = (SL._poolId examplePoolParams)
, SL.rewardAmount = SL.Coin 10
}
, deltaF = SL.DeltaCoin (- 3)
, nonMyopic = nonMyopic
}
Expand All @@ -596,7 +619,10 @@ exampleNewEpochState value = SL.NewEpochState {
nonMyopic = def

exampleLedgerState ::
forall era. ShelleyBasedEra era
forall era.
( ShelleyBasedEra era
, Core.PParams era ~ SL.PParams era
)
=> Core.Value era
-> LedgerState (ShelleyBlock era)
exampleLedgerState value = ShelleyLedgerState {
Expand All @@ -613,7 +639,9 @@ exampleHeaderState :: ShelleyBasedEra era => HeaderState (ShelleyBlock era)
exampleHeaderState = genesisHeaderState exampleChainDepState

exampleExtLedgerState ::
ShelleyBasedEra era
( ShelleyBasedEra era
, Core.PParams era ~ SL.PParams era
)
=> Core.Value era
-> ExtLedgerState (ShelleyBlock era)
exampleExtLedgerState value = ExtLedgerState {
Expand Down
Expand Up @@ -42,8 +42,7 @@ import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators ()
import Test.Consensus.Shelley.MockCrypto (CanMock)
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes as SL
import Test.Shelley.Spec.Ledger.Generator.ShelleyEraGen ()
import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators
(genPParams)
import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators ()
import Test.Shelley.Spec.Ledger.Serialisation.Generators ()

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -88,7 +87,7 @@ instance CanMock era => Arbitrary (SomeResult (ShelleyBlock era)) where
[ SomeResult GetLedgerTip <$> arbitrary
, SomeResult GetEpochNo <$> arbitrary
, SomeResult <$> (GetNonMyopicMemberRewards <$> arbitrary) <*> arbitrary
, SomeResult GetCurrentPParams <$> genPParams (Proxy @era)
, SomeResult GetCurrentPParams <$> arbitrary
, SomeResult GetProposedPParamsUpdates <$> arbitrary
, SomeResult GetStakeDistribution <$> arbitrary
, SomeResult DebugEpochState <$> arbitrary
Expand Down
Expand Up @@ -61,6 +61,7 @@ type CanMock era =
, SL.Mock (EraCrypto era)
, SL.ValidateScript era
, Arbitrary (Core.AuxiliaryData era)
, Arbitrary (Core.PParams era)
, Arbitrary (Core.Script era)
, Arbitrary (Core.TxBody era)
, Arbitrary (Core.TxOut era)
Expand Down
Expand Up @@ -70,20 +70,20 @@ import Test.Util.Slots (NumSlots (..))
import Test.Util.Time (dawnOfTime)

import qualified Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto, DSIGN, KES, VRF)
import Cardano.Ledger.Crypto (Crypto, DSIGN, HASH, KES, VRF)
import qualified Cardano.Ledger.Era
import Cardano.Ledger.SafeHash (EraIndependentTxBody,
HashAnnotated (..), SafeHash, hashAnnotated)
import qualified Cardano.Ledger.Shelley.Constraints as SL
import qualified Cardano.Ledger.ShelleyMA.TxBody as MA
import qualified Shelley.Spec.Ledger.API as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL (truncateUnitInterval,
unitIntervalFromRational)
import Shelley.Spec.Ledger.Hashing (EraIndependentTxBody,
HashAnnotated (..))
import qualified Shelley.Spec.Ledger.Keys
import qualified Shelley.Spec.Ledger.OCert as SL (OCertSignable (..))
import qualified Shelley.Spec.Ledger.PParams as SL (emptyPParams,
emptyPParamsUpdate)
import qualified Shelley.Spec.Ledger.Tx as SL (WitnessSetHKD (..))
import qualified Shelley.Spec.Ledger.TxBody as SL (eraIndTxBodyHash)
import qualified Shelley.Spec.Ledger.UTxO as SL (makeWitnessesVKey)

import Ouroboros.Consensus.Shelley.Eras (EraCrypto, ShelleyEra)
Expand Down Expand Up @@ -453,7 +453,7 @@ mkSetDecentralizationParamTxs coreNodes pVer ttl dNew =
signatures :: Set (SL.WitVKey 'SL.Witness c)
signatures =
SL.makeWitnessesVKey
(SL.eraIndTxBodyHash body)
(hashAnnotated body)
[ SL.KeyPair (SL.VKey vk) sk
| cn <- coreNodes
, let sk = cnDelegateKey cn
Expand Down Expand Up @@ -550,6 +550,8 @@ mkAllegraSetDecentralizationParamTxs ::
( ShelleyBasedEra era
, Cardano.Ledger.Core.TxBody era ~ MA.TxBody era
, Cardano.Ledger.Core.Value era ~ SL.Coin
, Cardano.Ledger.Core.PParams era ~ SL.PParams era
, SL.PParamsDelta era ~ SL.PParams' SL.StrictMaybe era
)
=> [CoreNode (Cardano.Ledger.Era.Crypto era)]
-> ProtVer -- ^ The proposed protocol version
Expand Down Expand Up @@ -649,12 +651,12 @@ mkAllegraSetDecentralizationParamTxs coreNodes pVer ttl dNew =
]

eraIndTxBodyHash' ::
forall era body.
( HashAnnotated body era
, HashIndex body ~ EraIndependentTxBody
forall crypto body.
( HashAlgorithm (Cardano.Ledger.Crypto.HASH crypto)
, HashAnnotated body EraIndependentTxBody crypto
)
=> body
-> Shelley.Spec.Ledger.Keys.Hash
(Cardano.Ledger.Era.Crypto era)
-> SafeHash
crypto
EraIndependentTxBody
eraIndTxBodyHash' = coerce . hashAnnotated
Expand Up @@ -23,6 +23,8 @@ module Ouroboros.Consensus.Shelley.Eras (

import Data.Default.Class (Default)
import Data.Text (Text)
import GHC.Records
import Numeric.Natural (Natural)

import Cardano.Ledger.Allegra (AllegraEra)
import qualified Cardano.Ledger.Core as LC
Expand All @@ -31,8 +33,10 @@ import Cardano.Ledger.Mary (MaryEra)
import Cardano.Ledger.Shelley (ShelleyEra)
import Control.State.Transition (State)

import qualified Cardano.Ledger.Shelley.Constraints as SL
import Ouroboros.Consensus.Shelley.Protocol.Crypto (StandardCrypto)
import qualified Shelley.Spec.Ledger.API as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL

{-------------------------------------------------------------------------------
Eras instantiated with standard crypto
Expand Down Expand Up @@ -79,9 +83,21 @@ type EraCrypto era = Crypto era
-- needed to determine the hard fork point. In the future this should be
-- replaced with an appropriate API - see
-- https://github.com/input-output-hk/ouroboros-network/issues/2890
--
-- TODO Currently we include the constraint @SL.AdditionalGenesisConfig era ~
-- ()@. When we fork to Alonzo we will need additional genesis config
-- information.
class ( SL.ShelleyBasedEra era
, State (LC.EraRule "PPUP" era) ~ SL.PPUPState era
, Default (State (LC.EraRule "PPUP" era))
, HasField "_maxBHSize" (LC.PParams era) Natural
, HasField "_maxTxSize" (LC.PParams era) Natural
, HasField "_a0" (LC.PParams era) Rational
, HasField "_nOpt" (LC.PParams era) Natural
, HasField "_rho" (LC.PParams era) SL.UnitInterval
, HasField "_tau" (LC.PParams era) SL.UnitInterval
, HasField "_protocolVersion" (SL.PParamsDelta era) (SL.StrictMaybe SL.ProtVer)
, SL.AdditionalGenesisConfig era ~ ()
) => ShelleyBasedEra era where
-- | Return the name of the Shelley-based era, e.g., @"Shelley"@, @"Allegra"@,
-- etc.
Expand Down

0 comments on commit 9ebef06

Please sign in to comment.