Skip to content

Commit

Permalink
rebased on master, adressed nicks comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSheard committed Dec 6, 2021
1 parent 981b103 commit 3e387a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ utxoTransition = do
pure
Shelley.UTxOState
{ Shelley._utxo = eval ((txins @era txb utxo) utxoAdd),
-- Domain exclusion (a ⋪ b) deletes 'a' from the domain of 'b'
Shelley._deposited = deposits' <> depositChange,
Shelley._fees = fees <> getField @"txfee" txb,
Shelley._ppups = ppup',
Expand Down
52 changes: 30 additions & 22 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ module Cardano.Ledger.Shelley.LedgerState
stakeDistr,
incrementalStakeDistr,
updateStakeDistribution,
resolveIncrementalPtrs,
aggregateUtxoCoinByCredential,
applyRUpd,
applyRUpd',
Expand Down Expand Up @@ -147,7 +146,7 @@ import Cardano.Ledger.Keys
)
import Cardano.Ledger.PoolDistr (PoolDistr (..))
import Cardano.Ledger.SafeHash (HashAnnotated, extractHash, hashAnnotated)
import Cardano.Ledger.Serialization (decodeRecordNamedT, mapToCBOR)
import Cardano.Ledger.Serialization (decodeRecordNamedT, mapFromCBOR, mapToCBOR)
import Cardano.Ledger.Shelley.Address.Bootstrap
( BootstrapWitness (..),
bootstrapWitKeyHash,
Expand Down Expand Up @@ -576,7 +575,15 @@ pvCanFollow (ProtVer m n) (SJust (ProtVer m' n')) =

-- =============================

-- | Incremental Stake, Stake along with possible missed coins from danging Ptrs
-- | Incremental Stake, Stake along with possible missed coins from danging Ptrs.
-- Transactions can use Ptrs to refer to a stake credential in a TxOut. The Ptr
-- does not have to point to anything until the epoch boundary, when we compute
-- rewards and aggregate staking information for ranking. This is unusual but legal.
-- In a non incremental system, we use what ever 'legal' Ptrs exist at the epoch
-- boundary. Here we are computing things incrementally, so we need to remember Ptrs
-- that might point to something by the time the epoch boundary is reached. When
-- the epoch boundary is reached we 'resolve' these pointers, to see if any have
-- become non-dangling since the time they came into the incremental computation.
data IncrementalStake crypto = IStake
{ credMap :: !(Map (Credential 'Staking crypto) Coin),
ptrMap :: !(Map Ptr Coin)
Expand All @@ -587,10 +594,11 @@ instance CC.Crypto crypto => ToCBOR (IncrementalStake crypto) where
toCBOR (IStake st dangle) =
encodeListLen 2 <> mapToCBOR st <> mapToCBOR dangle

instance CC.Crypto crypto => FromSharedCBOR (IncrementalStake crypto) where
instance CC.Crypto crypto => FromSharedCBOR (IncrementalStake crypto) where
type Share (IncrementalStake crypto) = Interns (Credential 'Staking crypto)
fromSharedCBOR credInterns = do
decodeRecordNamed "Stake" (const 2) $ do
stake <- fromSharedCBOR credInterns
stake <- fromSharedCBOR (credInterns, mempty)
dangle <- mapFromCBOR
pure $ IStake stake dangle

Expand Down Expand Up @@ -674,7 +682,6 @@ instance
_stakeDistro <- fromSharedCBOR credInterns
pure UTxOState {_utxo, _deposited, _fees, _ppups, _stakeDistro}


-- | New Epoch state and environment
data NewEpochState era = NewEpochState
{ -- | Last epoch
Expand Down Expand Up @@ -1112,7 +1119,13 @@ stakeDistr u ds ps =
activeDelegs :: Map (Credential 'Staking (Crypto era)) (KeyHash 'StakePool (Crypto era))
activeDelegs = eval ((dom rewards' delegs) dom poolParams)

-- A TxOut has 4 different shapes, depending on the shape of it's embedded Addr.
compactCoinOrError :: Coin -> CompactForm Coin
compactCoinOrError c =
case toCompact c of
Nothing -> error $ "Invalid ADA value in staking: " <> show c
Just compactCoin -> compactCoin

-- A TxOut has 4 different shapes, depending on the shape of its embedded Addr.
-- Credentials are stored in only 2 of the 4 cases.
-- 1) TxOut (Addr _ _ (StakeRefBase cred)) coin -> HERE
-- 2) TxOut (Addr _ _ (StakeRefPtr ptr)) coin -> HERE
Expand Down Expand Up @@ -1188,27 +1201,22 @@ incrementalAggregateUtxoCoinByCredential mode (UTxO u) initial =
Addr _ _ (StakeRefBase hk) -> IStake (Map.alter (keepOrDelete c) hk stake) ptrs
_other -> ans

-- | Resolve inserts and deletes which were indexed by Ptrs, by looking them up in 'ptrs'
-- and combining them with the ordinary stake. Zero out the Ptr indexed stake in the result.
-- This function is meant to only be called at the Epoch boundary in the Snap Rule. It is not meant
-- to be used to transform the IncrementalStake.
resolveIncrementalPtrs :: Map Ptr (Credential 'Staking (Crypto era)) -> IncrementalStake (Crypto era) -> IncrementalStake (Crypto era)
resolveIncrementalPtrs ptrs (IStake stake byPtr) = IStake (Map.foldlWithKey' accum stake byPtr) Map.empty
-- | Resolve inserts and deletes which were indexed by Ptrs, by looking them
-- up in 'ptrs' and combining the result of the lookup with the ordinary stake.
-- Return just this resolved map with type: Map (Credential 'Staking crypto) Coin
resolveIncrementalPtrs ::
Map Ptr (Credential 'Staking (Crypto era)) ->
IncrementalStake (Crypto era) ->
Map (Credential 'Staking (Crypto era)) Coin
resolveIncrementalPtrs ptrs (IStake stake byPtr) = Map.foldlWithKey' accum stake byPtr
where
accum ans ptr coin =
case Map.lookup ptr ptrs of
Nothing -> ans
Just hash -> Map.insertWith (<>) hash coin ans

compactCoinOrError :: Coin -> CompactForm Coin
compactCoinOrError c =
case toCompact c of
Nothing -> error $ "Invalid ADA value in staking: " <> show c
Just compactCoin -> compactCoin

-- | Compute the current state distribution by using the IncrementalStake, which is an aggregate
-- of the current UTxO. This function has a non-incremental analog 'takeDistr'
-- analog 'aggregateUtxoCoinByCredential'
-- of the current UTxO. This function has a non-incremental analog 'stakeDistr'
incrementalStakeDistr ::
forall era.
IncrementalStake (Crypto era) ->
Expand All @@ -1224,7 +1232,7 @@ incrementalStakeDistr incstake ds ps =
DState rewards' delegs bimap _ _ _ = ds
PState poolParams _ _ = ps
stake0, stake1 :: Map (Credential 'Staking (Crypto era)) Coin
(IStake stake0 _) = resolveIncrementalPtrs @era (forwards bimap) incstake
stake0 = resolveIncrementalPtrs @era (forwards bimap) incstake
stake1 = Map.unionWith (<>) stake0 rewards'
activeDelegs :: Map (Credential 'Staking (Crypto era)) (KeyHash 'StakePool (Crypto era))
activeDelegs = eval ((dom rewards' delegs) dom poolParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ utxoState0 =
_deposited = Coin 0,
_fees = Coin 0,
_ppups = PPUPState (ProposedPPUpdates mempty) (ProposedPPUpdates mempty),
_stakeDistro = IStake mempty mempty
_stakeDistro = mempty
}

tx :: Tx C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import Cardano.Ledger.Shelley.Delegation.Certificates (DelegCert (..))
import Cardano.Ledger.Shelley.LedgerState
( AccountState (..),
DPState,
IncrementalStake (..),
UTxOState (..),
)
import Cardano.Ledger.Shelley.PParams (PParams, PParams' (..), emptyPParams)
Expand Down Expand Up @@ -155,7 +154,7 @@ initUTxO n =
(Coin 0)
(Coin 0)
def
(IStake mempty mempty)
mempty

-- Protocal Parameters used for the benchmarknig tests.
-- Note that the fees and deposits are set to zero for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ exampleNewEpochState value ppp pp =
_deposited = Coin 1000,
_fees = Coin 1,
_ppups = def,
_stakeDistro = IStake mempty mempty -- Maybe this needs to agree with _utxo?
_stakeDistro = mempty
},
_delegationState = def
},
Expand Down

0 comments on commit 3e387a3

Please sign in to comment.