Skip to content

Commit

Permalink
Introduced IncrementalState, and fixed dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSheard committed Oct 25, 2021
1 parent 79de41f commit 3149960
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ translateToShelleyLedgerState genesisShelley epochNo cvs =
_deposited = Coin 0,
_fees = Coin 0,
_ppups = def,
_stakeDistro = Stake mempty
_stakeDistro = IStake mempty Map.empty
},
_delegationState =
DPState
Expand Down
5 changes: 3 additions & 2 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import Cardano.Ledger.Shelley.API.Types
ShelleyGenesis (sgGenDelegs, sgMaxLovelaceSupply, sgProtocolParams),
StrictMaybe (SNothing),
UTxOState (UTxOState),
IncrementalStake(..),
balance,
genesisUTxO,
word64ToCoin,
)
import Cardano.Ledger.Shelley.EpochBoundary (Stake (..), emptySnapShots)
import Cardano.Ledger.Shelley.EpochBoundary (emptySnapShots)
import Cardano.Ledger.Val (Val ((<->)))
import Control.State.Transition (STS (State))
import Data.Default.Class (Default, def)
Expand Down Expand Up @@ -67,7 +68,7 @@ instance
(Coin 0)
(Coin 0)
def
(Stake mempty)
(IStake mempty Map.empty)
)
(DPState (def {_genDelegs = GenDelegs genDelegs}) def)
)
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import Cardano.Ledger.Shelley.LedgerState as X
PState (..),
RewardUpdate (..),
UTxOState (..),
IncrementalStake(..),
WitHashes (..),
)
import Cardano.Ledger.Shelley.Metadata as X
Expand Down
12 changes: 5 additions & 7 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/EpochBoundary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ import NoThunks.Class (NoThunks (..))
import Numeric.Natural (Natural)

-- | Type of stake as map from hash key to coins associated.

newtype Stake crypto = Stake
{ unStake :: Map (Credential 'Staking crypto) Coin
}
{ unStake :: Map (Credential 'Staking crypto) Coin }
deriving (Show, Eq, Ord, NoThunks, NFData)

deriving newtype instance
CC.Crypto crypto => ToCBOR (Stake crypto)
deriving newtype instance CC.Crypto crypto => ToCBOR (Stake crypto)

deriving newtype instance
CC.Crypto crypto => FromCBOR (Stake crypto)
deriving newtype instance CC.Crypto crypto => FromCBOR (Stake crypto)

-- A TxOut has 4 different shapes, depending on the shape its embedded of Addr.
-- Credentials are stored in only 2 of the 4 cases.
Expand Down Expand Up @@ -117,7 +115,7 @@ poolStake ::
Stake crypto ->
Stake crypto
poolStake hk delegs (Stake stake) =
Stake $ eval (dom (delegs setSingleton hk) stake)
Stake (eval (dom (delegs setSingleton hk) stake))

-- | Calculate total possible refunds.
obligation ::
Expand Down
38 changes: 30 additions & 8 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Cardano.Ledger.Shelley.LedgerState
RewardUpdate (..),
RewardSnapShot (..),
UTxOState (..),
IncrementalStake(..),
depositPoolChange,
emptyRewardUpdate,
pvCanFollow,
Expand Down Expand Up @@ -531,12 +532,33 @@ pvCanFollow _ SNothing = True
pvCanFollow (ProtVer m n) (SJust (ProtVer m' n')) =
(m + 1, 0) == (m', n') || (m, n + 1) == (m', n')

-- =============================
-- | Incremental Stake, Take along with possible missed coins from danging Ptrs
data IncrementalStake crypto = IStake
{ getStake :: !(Map (Credential 'Staking crypto) Coin),
dangling :: !(Map Ptr Coin)
}
deriving (Generic, Show, Eq, Ord, NoThunks, NFData)

instance CC.Crypto crypto => ToCBOR (IncrementalStake crypto) where
toCBOR (IStake st dangle) =
encodeListLen 2 <> mapToCBOR st <> mapToCBOR dangle

instance CC.Crypto crypto => FromCBOR (IncrementalStake crypto) where
fromCBOR = do
decodeRecordNamed "Stake" (const 2) $ do
stake <- mapFromCBOR
dangle <- mapFromCBOR
pure $ IStake stake dangle
-- =============================


data UTxOState era = UTxOState
{ _utxo :: !(UTxO era),
_deposited :: !Coin,
_fees :: !Coin,
_ppups :: !(State (Core.EraRule "PPUP" era)),
_stakeDistro :: !(Stake (Crypto era))
_stakeDistro :: !(IncrementalStake (Crypto era))
}
deriving (Generic)

Expand Down Expand Up @@ -713,7 +735,7 @@ genesisState genDelegs0 utxo0 =
(Coin 0)
(Coin 0)
def
(Stake mempty)
(IStake mempty Map.empty)
)
(DPState dState def)
where
Expand Down Expand Up @@ -815,13 +837,13 @@ updateStakeDistribution ::
(Era era,
HasField "address" (Core.TxOut era) (Addr (Crypto era))
) =>
Stake (Crypto era) ->
IncrementalStake (Crypto era) ->
UTxO era ->
UTxO era ->
Map Ptr (Credential 'Staking (Crypto era)) ->
Stake (Crypto era)
updateStakeDistribution (Stake stake) utxoDel utxoAdd ptrs =
Stake $ stake `combine` stakeAdded `combine` stakeDeletedInv
IncrementalStake (Crypto era)
updateStakeDistribution (IStake stake dangle) utxoDel utxoAdd ptrs =
IStake (stake `combine` stakeAdded `combine` stakeDeletedInv) dangle
where
combine = Map.unionWith (<>)
stakeDeleted = aggregateUtxoCoinByCredential ptrs utxoDel mempty
Expand Down Expand Up @@ -1020,7 +1042,7 @@ stakeDistr ::
SnapShot (Crypto era)
stakeDistr u ds ps =
SnapShot
(Stake $ eval (dom activeDelegs stakeRelation))
(Stake (eval (dom activeDelegs stakeRelation)))
delegs
poolParams
where
Expand Down Expand Up @@ -1392,7 +1414,7 @@ instance
Default (State (Core.EraRule "PPUP" era)) =>
Default (UTxOState era)
where
def = UTxOState (UTxO Map.empty) (Coin 0) (Coin 0) def (Stake mempty)
def = UTxOState (UTxO Map.empty) (Coin 0) (Coin 0) def (IStake mempty Map.empty)

instance
(Default (LedgerState era), Default (Core.PParams era)) =>
Expand Down
5 changes: 3 additions & 2 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Snap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Cardano.Ledger.Shelley.LedgerState
PState (..),
LedgerState (..),
UTxOState (..),
IncrementalStake(..),
stakeDistr,
)
import Control.State.Transition
Expand Down Expand Up @@ -63,7 +64,7 @@ snapTransition ::
snapTransition = do
TRC (lstate, s, _) <- judgmentContext

let LedgerState (UTxOState utxo _ fees _ sd) (DPState dstate pstate) = lstate
let LedgerState (UTxOState utxo _ fees _ (IStake sd _)) (DPState dstate pstate) = lstate
stake = stakeDistr utxo dstate pstate
-- ^ The stake distribution calculation done on the epoch boundary, which we
-- would like to replace with an incremental one.
Expand All @@ -76,7 +77,7 @@ snapTransition = do
ds' = Map.filter (\pid -> pid `Map.member` ps) ds

-- add the incremental stake distribution calculation to the existing rewards
sd' = Map.unionWith (<>) (unStake sd) rws
sd' = Map.unionWith (<>) sd rws

-- filter the incremental stake distribution calculation to the credentials which
-- are both registered and delegating to a registered pool
Expand Down

0 comments on commit 3149960

Please sign in to comment.