Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
A UMap
(for Unified map) represents
- 4 Maps with the same domain in one direction, as a single
Map
and - 1 other
Map
which is an inverse of one of the other 4 Maps.
The advantage of using UMap
is that it stores all the information
compactly, by exploiting the large amount of sharing in Map #1.
As for the other Map #2, we don't expect it to have much volume.
Synopsis
- data RDPair = RDPair {
- rdReward ∷ !(CompactForm Coin)
- rdDeposit ∷ !(CompactForm Coin)
- rdRewardCoin ∷ RDPair → Coin
- rdDepositCoin ∷ RDPair → Coin
- data UMElem where
- pattern UMElem ∷ StrictMaybe RDPair → Set Ptr → StrictMaybe (KeyHash 'StakePool) → StrictMaybe DRep → UMElem
- umElemRDPair ∷ UMElem → Maybe RDPair
- umElemRDActive ∷ UMElem → Maybe RDPair
- data RewardDelegation
- = RewardDelegationSPO !(KeyHash 'StakePool) !(CompactForm Coin)
- | RewardDelegationDRep !DRep !(CompactForm Coin)
- | RewardDelegationBoth !(KeyHash 'StakePool) !DRep !(CompactForm Coin)
- umElemDRepDelegatedReward ∷ UMElem → Maybe (CompactForm Coin, DRep)
- umElemDelegations ∷ UMElem → Maybe RewardDelegation
- umElemPtrs ∷ UMElem → Maybe (Set Ptr)
- umElemSPool ∷ UMElem → Maybe (KeyHash 'StakePool)
- umElemDRep ∷ UMElem → Maybe DRep
- umElemAsTuple ∷ UMElem → (StrictMaybe RDPair, Set Ptr, StrictMaybe (KeyHash 'StakePool), StrictMaybe DRep)
- nullUMElem ∷ UMElem → Bool
- nullUMElemMaybe ∷ UMElem → Maybe UMElem
- data UMap = UMap {
- umElems ∷ !(Map (Credential 'Staking) UMElem)
- umPtrs ∷ !(Map Ptr (Credential 'Staking))
- umElemsL ∷ Lens' UMap (Map (Credential 'Staking) UMElem)
- empty ∷ UMap
- umInvariant ∷ Credential 'Staking → Ptr → UMap → Bool
- data StakeCredentials = StakeCredentials {
- scRewards ∷ Map (Credential 'Staking) Coin
- scDeposits ∷ Map (Credential 'Staking) Coin
- scSPools ∷ Map (Credential 'Staking) (KeyHash 'StakePool)
- scDReps ∷ Map (Credential 'Staking) DRep
- scPtrs ∷ Map Ptr (Credential 'Staking)
- scPtrsInverse ∷ Map (Credential 'Staking) (Set Ptr)
- toStakeCredentials ∷ UMap → StakeCredentials
- domRestrictedStakeCredentials ∷ Set (Credential 'Staking) → UMap → StakeCredentials
- data UView k v where
- RewDepUView ∷ !UMap → UView (Credential 'Staking) RDPair
- PtrUView ∷ !UMap → UView Ptr (Credential 'Staking)
- SPoolUView ∷ !UMap → UView (Credential 'Staking) (KeyHash 'StakePool)
- DRepUView ∷ !UMap → UView (Credential 'Staking) DRep
- rewDepUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) RDPair
- ptrUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView Ptr (Credential 'Staking)
- sPoolUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) (KeyHash 'StakePool)
- dRepUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) DRep
- unUView ∷ UView k v → UMap
- unUnifyToVMap ∷ UView k v → VMap VB VB k v
- rdPairMap ∷ UMap → Map (Credential 'Staking) RDPair
- rewardMap ∷ UMap → Map (Credential 'Staking) Coin
- compactRewardMap ∷ UMap → Map (Credential 'Staking) (CompactForm Coin)
- depositMap ∷ UMap → Map (Credential 'Staking) Coin
- ptrMap ∷ UMap → Map Ptr (Credential 'Staking)
- invPtrMap ∷ UMap → Map (Credential 'Staking) (Set Ptr)
- sPoolMap ∷ UMap → Map (Credential 'Staking) (KeyHash 'StakePool)
- dRepMap ∷ UMap → Map (Credential 'Staking) DRep
- domRestrictedMap ∷ Set k → UView k v → Map k v
- data family CompactForm a ∷ Type
- toCompact ∷ Compactible a ⇒ a → Maybe (CompactForm a)
- fromCompact ∷ Compactible a ⇒ CompactForm a → a
- addCompact ∷ CompactForm Coin → CompactForm Coin → CompactForm Coin
- sumCompactCoin ∷ Foldable t ⇒ t (CompactForm Coin) → CompactForm Coin
- sumRewardsUView ∷ UView k RDPair → CompactForm Coin
- sumDepositUView ∷ UView k RDPair → CompactForm Coin
- compactCoinOrError ∷ HasCallStack ⇒ Coin → CompactForm Coin
- unify ∷ Map (Credential 'Staking) RDPair → Map Ptr (Credential 'Staking) → Map (Credential 'Staking) (KeyHash 'StakePool) → Map (Credential 'Staking) DRep → UMap
- unUnify ∷ UView k v → Map k v
- nullUView ∷ UView k v → Bool
- member ∷ k → UView k v → Bool
- member' ∷ Credential 'Staking → UMap → Bool
- notMember ∷ k → UView k v → Bool
- delete ∷ k → UView k v → UMap
- delete' ∷ k → UView k v → UView k v
- insertWith ∷ (v → v → v) → k → v → UView k v → UMap
- insertWith' ∷ (v → v → v) → k → v → UView k v → UView k v
- insert ∷ k → v → UView k v → UMap
- insert' ∷ k → v → UView k v → UView k v
- adjust ∷ (RDPair → RDPair) → k → UView k RDPair → UMap
- lookup ∷ k → UView k v → Maybe v
- domain ∷ UView k v → Set k
- range ∷ UView k v → Set v
- (∪) ∷ UView k v → (k, v) → UMap
- unionL ∷ UView k v → (k, v) → UMap
- (⨃) ∷ UView k v → Map k v → UMap
- unionR ∷ UView k v → Map k v → UMap
- (∪+) ∷ UView (Credential 'Staking) RDPair → Map (Credential 'Staking) (CompactForm Coin) → UMap
- unionRewAgg ∷ UView (Credential 'Staking) RDPair → Map (Credential 'Staking) (CompactForm Coin) → UMap
- unionKeyDeposits ∷ UView k RDPair → Map k (CompactForm Coin) → UMap
- (⋪) ∷ Set k → UView k v → UMap
- domDelete ∷ Set k → UView k v → UMap
- (⋫) ∷ UView k v → Set v → UMap
- rngDelete ∷ UView k v → Set v → UMap
- (◁) ∷ UView k v → Map k u → Map k u
- domRestrict ∷ UView k v → Map k u → Map k u
- findWithDefault ∷ v → k → UView k v → v
- size ∷ UView k v → Int
- domDeleteAll ∷ Set (Credential 'Staking) → UMap → UMap
- deleteStakingCredential ∷ Credential 'Staking → UMap → UMap
- extractStakingCredential ∷ Credential 'Staking → UMap → (Maybe UMElem, UMap)
Constructing a UMap
A Reward-Deposit Pair Used to represent the reward and the deposit for a given (Credential 'Staking c)
RDPair | |
|
Instances
Generic RDPair Source # | |
Show RDPair Source # | |
DecCBOR RDPair Source # | |
EncCBOR RDPair Source # | |
NFData RDPair Source # | |
Defined in Cardano.Ledger.UMap | |
Eq RDPair Source # | |
Ord RDPair Source # | |
Defined in Cardano.Ledger.UMap | |
NoThunks RDPair Source # | |
type Rep RDPair Source # | |
Defined in Cardano.Ledger.UMap type Rep RDPair = D1 ('MetaData "RDPair" "Cardano.Ledger.UMap" "cardano-ledger-core-1.17.0.0-inplace" 'False) (C1 ('MetaCons "RDPair" 'PrefixI 'True) (S1 ('MetaSel ('Just "rdReward") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 (CompactForm Coin)) :*: S1 ('MetaSel ('Just "rdDeposit") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 (CompactForm Coin)))) |
rdRewardCoin ∷ RDPair → Coin Source #
rdDepositCoin ∷ RDPair → Coin Source #
A UMElem
compactly represents the range of 4 Map
s with the same domain
as a single n-tuple.
This space-compacting datatype, and the pattern UMElem
are equivalent to:
data Elem c = Elem
{ rdPairT :: !(StrictMaybe RDPair),
ptrT :: !(Set Ptr),
sPoolT :: !(StrictMaybe (KeyHash 'StakePool c)), -- the stake pool identity
dRepT :: !(StrictMaybe (DRep c)),
}
deriving (Show, Eq, Generic, NoThunks, NFData)
To name the constructors of UMElem
we use the notation Txxx
where
each x
is
either F
for full, i.e. the component is present,
or E
for empty, i.e. the component is not present.
There are four components:
1) the reward-deposit pair as an RDPair
(CompactForm Coin) (CompactForm Coin) as a pair of Word64s, the first x
,
2) the set of pointers, the second x
,
3) the stake pool id (KeyHash 'StakePool c), the third x
, and
4) the voting delegatee id (DRep c), the fourth x
.
So, TEEEE means none of the components are present, TFEEE means only the reward-deposit pair is present, TEFEE means only the set of pointers is present, TEEFE means only the stake pool id is present. etc. TEEEF means only the voting delegatee id is present, and
The pattern UMElem
will correctly use the optimal constructor.
pattern UMElem ∷ StrictMaybe RDPair → Set Ptr → StrictMaybe (KeyHash 'StakePool) → StrictMaybe DRep → UMElem | A |
Instances
umElemRDPair ∷ UMElem → Maybe RDPair Source #
Extract the reward-deposit pair if it is present. We can tell that the reward is present when Txxxx has an F in the first position
This is equivalent to the pattern (UMElem (SJust r) _ _ _) -> Just r
umElemRDActive ∷ UMElem → Maybe RDPair Source #
Extract a delegated reward-deposit pair if it is present. We can tell that the pair is present and active when Txxxx has an F in the 1st position (present) and 3rd position (delegated).
This is equivalent to the pattern (UMElem (SJust r) _ (SJust _) _) -> Just r
data RewardDelegation Source #
umElemDRepDelegatedReward ∷ UMElem → Maybe (CompactForm Coin, DRep) Source #
Extract a DRep delegated reward if it is present. We can tell that the pair is present and active when Txxxx has an F in the 1st position (present) and 4rd position (DRep delegated).
This is equivalent to the pattern (UMElem (SJust r) _ _ (SJust d)) -> Just (r, d)
umElemDelegations ∷ UMElem → Maybe RewardDelegation Source #
Extract rewards that are either delegated to a DRep or an SPO (or both). We can tell that the pair is present and active when Txxxx has F's in the 1st and either 3rd or 4th or both positions. If there are no rewards or deposits but the delegations still exist, then we return zero coin as reward.
umElemPtrs ∷ UMElem → Maybe (Set Ptr) Source #
Extract the set of pointers if it is non-empty. We can tell that the reward is present when Txxxx has an F in the second position
This is equivalent to the pattern (UMElem _ p _ _) -> Just p
umElemSPool ∷ UMElem → Maybe (KeyHash 'StakePool) Source #
Extract the stake delegatee pool id, if present. We can tell that the pool id is present when Txxxx has an F in the third position
This is equivalent to the pattern (UMElem _ _ (SJust s) _) -> Just s
umElemDRep ∷ UMElem → Maybe DRep Source #
Extract the voting delegatee id, if present. We can tell that the delegatee is present when Txxxx has an F in the fourth position
This is equivalent to the pattern (UMElem _ _ _ (SJust d)) -> Just d
umElemAsTuple ∷ UMElem → (StrictMaybe RDPair, Set Ptr, StrictMaybe (KeyHash 'StakePool), StrictMaybe DRep) Source #
A unified map represents 4 Maps with domain (Credential 'Staking c)
1) Map (Credential 'Staking c) RDPair -- (RDPair rewardCoin depositCoin)
2) Map (Credential 'Staking c) (Set Ptr)
3) Map (Credential 'Staking c) (StrictMaybe (KeyHash 'StakePool c))
4) Map (Credential 'Staking c) (StrictMaybe (DRep c))
and one more map in the inverse direction with Ptr
for keys and (Credential 'Staking c)
for values.
Instances
ToJSON UMap Source # | |
Generic UMap Source # | |
Show UMap Source # | |
DecShareCBOR UMap Source # | |
EncCBOR UMap Source # | |
NFData UMap Source # | |
Defined in Cardano.Ledger.UMap | |
Eq UMap Source # | |
NoThunks UMap Source # | |
type Rep UMap Source # | |
Defined in Cardano.Ledger.UMap type Rep UMap = D1 ('MetaData "UMap" "Cardano.Ledger.UMap" "cardano-ledger-core-1.17.0.0-inplace" 'False) (C1 ('MetaCons "UMap" 'PrefixI 'True) (S1 ('MetaSel ('Just "umElems") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map (Credential 'Staking) UMElem)) :*: S1 ('MetaSel ('Just "umPtrs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map Ptr (Credential 'Staking))))) | |
type Share UMap Source # | |
umInvariant ∷ Credential 'Staking → Ptr → UMap → Bool Source #
StakeCredentials
data StakeCredentials Source #
All maps unrolled. It is important to note that all fields are lazy, because conversion from UMap can be expensive, thus only fields that are forced will incur that conversion overhead.
StakeCredentials | |
|
Instances
UView
and its components
A UView
lets one view a UMap
in n
different ways,
one for each of the elements in a Unified Element UMElem
(4)
A (UView c key value)
can be used like a (Map key value)
.
It acts like a map, supporting efficient insert, delete, and lookup operations.
RewDepUView ∷ !UMap → UView (Credential 'Staking) RDPair | |
PtrUView ∷ !UMap → UView Ptr (Credential 'Staking) | |
SPoolUView ∷ !UMap → UView (Credential 'Staking) (KeyHash 'StakePool) | |
DRepUView ∷ !UMap → UView (Credential 'Staking) DRep |
Instances
Foldable (UView k) Source # | All |
Defined in Cardano.Ledger.UMap fold ∷ Monoid m ⇒ UView k m → m Source # foldMap ∷ Monoid m ⇒ (a → m) → UView k a → m Source # foldMap' ∷ Monoid m ⇒ (a → m) → UView k a → m Source # foldr ∷ (a → b → b) → b → UView k a → b Source # foldr' ∷ (a → b → b) → b → UView k a → b Source # foldl ∷ (b → a → b) → b → UView k a → b Source # foldl' ∷ (b → a → b) → b → UView k a → b Source # foldr1 ∷ (a → a → a) → UView k a → a Source # foldl1 ∷ (a → a → a) → UView k a → a Source # toList ∷ UView k a → [a] Source # null ∷ UView k a → Bool Source # length ∷ UView k a → Int Source # elem ∷ Eq a ⇒ a → UView k a → Bool Source # maximum ∷ Ord a ⇒ UView k a → a Source # minimum ∷ Ord a ⇒ UView k a → a Source # |
rewDepUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) RDPair Source #
Construct a RewDepUView
from the two maps that make up a UMap
ptrUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView Ptr (Credential 'Staking) Source #
sPoolUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) (KeyHash 'StakePool) Source #
Construct a SPoolUView
from the two maps that make up a UMap
dRepUView ∷ Map (Credential 'Staking) UMElem → Map Ptr (Credential 'Staking) → UView (Credential 'Staking) DRep Source #
unUnifyToVMap ∷ UView k v → VMap VB VB k v Source #
Materialize a real VMap
(Vector Map) from a UView
This is expensive, use it wisely (like maybe once per epoch boundary to make a SnapShot
)
compactRewardMap ∷ UMap → Map (Credential 'Staking) (CompactForm Coin) Source #
depositMap ∷ UMap → Map (Credential 'Staking) Coin Source #
data family CompactForm a ∷ Type Source #
Instances
toCompact ∷ Compactible a ⇒ a → Maybe (CompactForm a) Source #
fromCompact ∷ Compactible a ⇒ CompactForm a → a Source #
sumCompactCoin ∷ Foldable t ⇒ t (CompactForm Coin) → CompactForm Coin Source #
sumRewardsUView ∷ UView k RDPair → CompactForm Coin Source #
sumDepositUView ∷ UView k RDPair → CompactForm Coin Source #
unify ∷ Map (Credential 'Staking) RDPair → Map Ptr (Credential 'Staking) → Map (Credential 'Staking) (KeyHash 'StakePool) → Map (Credential 'Staking) DRep → UMap Source #
Create a UMap from 4 separate maps. NOTE: For use in tests only.
unUnify ∷ UView k v → Map k v Source #
Materialize a real Map
from a View
This is expensive, use it wisely (like maybe once per epoch boundary to make a SnapShot
)
See also domRestrictedMap, which domain-restricts before computing a view.
Set and Map operations on UView
s
insertWith ∷ (v → v → v) → k → v → UView k v → UMap Source #
insertWith' ∷ (v → v → v) → k → v → UView k v → UView k v Source #
Insert with combination
If k
exists as a key in the (map-like) UView
:
- to keep the old value > insertWith' ( old new -> old) k v view
- to replace the old value with the new value > insertWith' ( old new -> new) k v view
- to combine the old and new values with summation > insertWith' ( old new -> old + new) k v view
If k
does not exist as a key in the UView
,
the combining function is ignored, and
the key k
and the value v
are inserted into the map-like UView
> insertWith' ignoredCombiningFunction k v view
(∪) ∷ UView k v → (k, v) → UMap Source #
Union with left preference. So if k, already exists, do nothing, if it doesn't exist insert it.
Spec: evalUnified (RewDepUView u1 ∪ singleton hk mempty) evalUnified (Ptrs u2 ∪ singleton ptr hk)
unionL ∷ UView k v → (k, v) → UMap Source #
Union with left preference. So if k, already exists, do nothing, if it doesn't exist insert it.
Spec: evalUnified (RewDepUView u1 ∪ singleton hk mempty) evalUnified (Ptrs u2 ∪ singleton ptr hk)
(⨃) ∷ UView k v → Map k v → UMap Source #
Union with right preference. So if k, already exists, then old v is overwritten with the new v.
Special rules apply for the RewDepUView
, where only the rdReward
field of the RDPair
is overwritten, and the old rdDeposit
value persists.
Note: In this case it is an invariant that the domain of the Map
on the right side
is a subset of the domain of the RewDepUView. See the single case in
module Cardano.Ledger.Shelley.Rules.Delegs, in the dealing with Withdrawals's where
it is used at this type.
Spec: evalUnified (delegations ds ⨃ singleton hk dpool) evalUnified (rewards' ⨃ wdrls_')
unionR ∷ UView k v → Map k v → UMap Source #
Union with right preference. So if k, already exists, then old v is overwritten with the new v.
Special rules apply for the RewDepUView
, where only the rdReward
field of the RDPair
is overwritten, and the old rdDeposit
value persists.
Note: In this case it is an invariant that the domain of the Map
on the right side
is a subset of the domain of the RewDepUView. See the single case in
module Cardano.Ledger.Shelley.Rules.Delegs, in the dealing with Withdrawals's where
it is used at this type.
Spec: evalUnified (delegations ds ⨃ singleton hk dpool) evalUnified (rewards' ⨃ wdrls_')
(∪+) ∷ UView (Credential 'Staking) RDPair → Map (Credential 'Staking) (CompactForm Coin) → UMap Source #
Add the reward from the Map
on the right side to the reward in the UView
on the left.
This is only implemented and is applicable to RewDepUView
s.
We presume that the domain of the Map
on the right, is a subset of the domain of the UView
on the left.
Spec: evalUnified (rewards dState ∪+ registeredAggregated) evalUnified (rewards' ∪+ update) evalUnified (RewDepUView u0 ∪+ refunds)
unionRewAgg ∷ UView (Credential 'Staking) RDPair → Map (Credential 'Staking) (CompactForm Coin) → UMap Source #
Add the reward from the Map
on the right side to the reward in the UView
on the left.
This is only implemented and is applicable to RewDepUView
s.
We presume that the domain of the Map
on the right, is a subset of the domain of the UView
on the left.
Spec: evalUnified (rewards dState ∪+ registeredAggregated) evalUnified (rewards' ∪+ update) evalUnified (RewDepUView u0 ∪+ refunds)
unionKeyDeposits ∷ UView k RDPair → Map k (CompactForm Coin) → UMap Source #
Add the deposit from the Map
on the right side to the deposit in the UView
on the left.
This is only implemented and is applicable to RewDepUView
s.
(◁) ∷ UView k v → Map k u → Map k u Source #
Domain restriction.
Spec: eval (dom rewards' ◁ iRReserves (_irwd ds) :: RewardAccounts (Crypto era)) eval (dom rewards' ◁ iRTreasury (_irwd ds) :: RewardAccounts (Crypto era))
domRestrict ∷ UView k v → Map k u → Map k u Source #
Domain restriction.
Spec: eval (dom rewards' ◁ iRReserves (_irwd ds) :: RewardAccounts (Crypto era)) eval (dom rewards' ◁ iRTreasury (_irwd ds) :: RewardAccounts (Crypto era))
Derived functions
findWithDefault ∷ v → k → UView k v → v Source #
Find the value associated with a key from a UView
, return the default if the key is not there.
domDeleteAll ∷ Set (Credential 'Staking) → UMap → UMap Source #
Delete the stake credentials in the domain and all associated ranges from the UMap
This can be expensive when there are many pointers associated with the credential.
deleteStakingCredential ∷ Credential 'Staking → UMap → UMap Source #
Completely remove the staking credential from the UMap, including all associated pointers.
extractStakingCredential ∷ Credential 'Staking → UMap → (Maybe UMElem, UMap) Source #
Just like deleteStakingCredential
, but also returned the removed element.