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 c where
- pattern UMElem ∷ StrictMaybe RDPair → Set Ptr → StrictMaybe (KeyHash 'StakePool c) → StrictMaybe (DRep c) → UMElem c
- umElemRDPair ∷ UMElem c → Maybe RDPair
- umElemRDActive ∷ UMElem c → Maybe RDPair
- data RewardDelegation c
- = RewardDelegationSPO !(KeyHash 'StakePool c) !(CompactForm Coin)
- | RewardDelegationDRep !(DRep c) !(CompactForm Coin)
- | RewardDelegationBoth !(KeyHash 'StakePool c) !(DRep c) !(CompactForm Coin)
- umElemDRepDelegatedReward ∷ UMElem c → Maybe (CompactForm Coin, DRep c)
- umElemDelegations ∷ UMElem c → Maybe (RewardDelegation c)
- umElemPtrs ∷ UMElem c → Maybe (Set Ptr)
- umElemSPool ∷ UMElem c → Maybe (KeyHash 'StakePool c)
- umElemDRep ∷ UMElem c → Maybe (DRep c)
- umElemAsTuple ∷ UMElem c → (StrictMaybe RDPair, Set Ptr, StrictMaybe (KeyHash 'StakePool c), StrictMaybe (DRep c))
- nullUMElem ∷ UMElem c → Bool
- nullUMElemMaybe ∷ UMElem c → Maybe (UMElem c)
- data UMap c = UMap {
- umElems ∷ !(Map (Credential 'Staking c) (UMElem c))
- umPtrs ∷ !(Map Ptr (Credential 'Staking c))
- umElemsL ∷ Lens' (UMap c) (Map (Credential 'Staking c) (UMElem c))
- empty ∷ UMap c
- umInvariant ∷ Credential 'Staking c → Ptr → UMap c → Bool
- data StakeCredentials c = StakeCredentials {
- scRewards ∷ Map (Credential 'Staking c) Coin
- scDeposits ∷ Map (Credential 'Staking c) Coin
- scSPools ∷ Map (Credential 'Staking c) (KeyHash 'StakePool c)
- scDReps ∷ Map (Credential 'Staking c) (DRep c)
- scPtrs ∷ Map Ptr (Credential 'Staking c)
- scPtrsInverse ∷ Map (Credential 'Staking c) (Set Ptr)
- toStakeCredentials ∷ UMap c → StakeCredentials c
- domRestrictedStakeCredentials ∷ Set (Credential 'Staking c) → UMap c → StakeCredentials c
- data UView c k v where
- RewDepUView ∷ !(UMap c) → UView c (Credential 'Staking c) RDPair
- PtrUView ∷ !(UMap c) → UView c Ptr (Credential 'Staking c)
- SPoolUView ∷ !(UMap c) → UView c (Credential 'Staking c) (KeyHash 'StakePool c)
- DRepUView ∷ !(UMap c) → UView c (Credential 'Staking c) (DRep c)
- rewDepUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) RDPair
- ptrUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c Ptr (Credential 'Staking c)
- sPoolUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) (KeyHash 'StakePool c)
- dRepUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) (DRep c)
- unUView ∷ UView c k v → UMap c
- unUnifyToVMap ∷ UView c k v → VMap VB VB k v
- rdPairMap ∷ UMap c → Map (Credential 'Staking c) RDPair
- rewardMap ∷ UMap c → Map (Credential 'Staking c) Coin
- compactRewardMap ∷ UMap c → Map (Credential 'Staking c) (CompactForm Coin)
- depositMap ∷ UMap c → Map (Credential 'Staking c) Coin
- ptrMap ∷ UMap c → Map Ptr (Credential 'Staking c)
- invPtrMap ∷ UMap c → Map (Credential 'Staking c) (Set Ptr)
- sPoolMap ∷ UMap c → Map (Credential 'Staking c) (KeyHash 'StakePool c)
- dRepMap ∷ UMap c → Map (Credential 'Staking c) (DRep c)
- domRestrictedMap ∷ Set k → UView c 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 c k RDPair → CompactForm Coin
- sumDepositUView ∷ UView c k RDPair → CompactForm Coin
- compactCoinOrError ∷ HasCallStack ⇒ Coin → CompactForm Coin
- unify ∷ Map (Credential 'Staking c) RDPair → Map Ptr (Credential 'Staking c) → Map (Credential 'Staking c) (KeyHash 'StakePool c) → Map (Credential 'Staking c) (DRep c) → UMap c
- unUnify ∷ UView c k v → Map k v
- nullUView ∷ UView c k v → Bool
- member ∷ k → UView c k v → Bool
- member' ∷ Credential 'Staking c → UMap c → Bool
- notMember ∷ k → UView c k v → Bool
- delete ∷ k → UView c k v → UMap c
- delete' ∷ k → UView c k v → UView c k v
- insertWith ∷ (v → v → v) → k → v → UView c k v → UMap c
- insertWith' ∷ (v → v → v) → k → v → UView c k v → UView c k v
- insert ∷ k → v → UView c k v → UMap c
- insert' ∷ k → v → UView c k v → UView c k v
- adjust ∷ (RDPair → RDPair) → k → UView c k RDPair → UMap c
- lookup ∷ k → UView c k v → Maybe v
- domain ∷ UView c k v → Set k
- range ∷ UView c k v → Set v
- (∪) ∷ UView c k v → (k, v) → UMap c
- unionL ∷ UView c k v → (k, v) → UMap c
- (⨃) ∷ UView c k v → Map k v → UMap c
- unionR ∷ UView c k v → Map k v → UMap c
- (∪+) ∷ UView c (Credential 'Staking c) RDPair → Map (Credential 'Staking c) (CompactForm Coin) → UMap c
- unionRewAgg ∷ UView c (Credential 'Staking c) RDPair → Map (Credential 'Staking c) (CompactForm Coin) → UMap c
- unionKeyDeposits ∷ UView c k RDPair → Map k (CompactForm Coin) → UMap c
- (⋪) ∷ Set k → UView c k v → UMap c
- domDelete ∷ Set k → UView c k v → UMap c
- (⋫) ∷ UView c k v → Set v → UMap c
- rngDelete ∷ UView c k v → Set v → UMap c
- (◁) ∷ UView c k v → Map k u → Map k u
- domRestrict ∷ UView c k v → Map k u → Map k u
- findWithDefault ∷ v → k → UView c k v → v
- size ∷ UView c k v → Int
- domDeleteAll ∷ Set (Credential 'Staking c) → UMap c → UMap c
- deleteStakingCredential ∷ Credential 'Staking c → UMap c → UMap c
- extractStakingCredential ∷ Credential 'Staking c → UMap c → (Maybe (UMElem c), UMap c)
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.16.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 c) → StrictMaybe (DRep c) → UMElem c | A |
Instances
umElemRDPair ∷ UMElem c → 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 c → 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 c Source #
RewardDelegationSPO !(KeyHash 'StakePool c) !(CompactForm Coin) | |
RewardDelegationDRep !(DRep c) !(CompactForm Coin) | |
RewardDelegationBoth !(KeyHash 'StakePool c) !(DRep c) !(CompactForm Coin) |
umElemDRepDelegatedReward ∷ UMElem c → Maybe (CompactForm Coin, DRep c) 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 c → Maybe (RewardDelegation c) 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 c → 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 c → Maybe (KeyHash 'StakePool c) 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 c → Maybe (DRep c) 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 c → (StrictMaybe RDPair, Set Ptr, StrictMaybe (KeyHash 'StakePool c), StrictMaybe (DRep c)) 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.
UMap | |
|
Instances
Crypto c ⇒ ToJSON (UMap c) Source # | |
Generic (UMap c) Source # | |
Show (UMap c) Source # | |
Crypto c ⇒ DecShareCBOR (UMap c) Source # | |
Crypto c ⇒ EncCBOR (UMap c) Source # | |
NFData (UMap c) Source # | |
Defined in Cardano.Ledger.UMap | |
Eq (UMap c) Source # | |
NoThunks (UMap c) Source # | |
type Rep (UMap c) Source # | |
Defined in Cardano.Ledger.UMap type Rep (UMap c) = D1 ('MetaData "UMap" "Cardano.Ledger.UMap" "cardano-ledger-core-1.16.0.0-inplace" 'False) (C1 ('MetaCons "UMap" 'PrefixI 'True) (S1 ('MetaSel ('Just "umElems") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map (Credential 'Staking c) (UMElem c))) :*: S1 ('MetaSel ('Just "umPtrs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map Ptr (Credential 'Staking c))))) | |
type Share (UMap c) Source # | |
umInvariant ∷ Credential 'Staking c → Ptr → UMap c → Bool Source #
StakeCredentials
data StakeCredentials c 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
toStakeCredentials ∷ UMap c → StakeCredentials c Source #
domRestrictedStakeCredentials ∷ Set (Credential 'Staking c) → UMap c → StakeCredentials c Source #
UView
and its components
data UView c k v where Source #
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 c) → UView c (Credential 'Staking c) RDPair | |
PtrUView ∷ !(UMap c) → UView c Ptr (Credential 'Staking c) | |
SPoolUView ∷ !(UMap c) → UView c (Credential 'Staking c) (KeyHash 'StakePool c) | |
DRepUView ∷ !(UMap c) → UView c (Credential 'Staking c) (DRep c) |
Instances
Foldable (UView c k) Source # | All |
Defined in Cardano.Ledger.UMap fold ∷ Monoid m ⇒ UView c k m → m Source # foldMap ∷ Monoid m ⇒ (a → m) → UView c k a → m Source # foldMap' ∷ Monoid m ⇒ (a → m) → UView c k a → m Source # foldr ∷ (a → b → b) → b → UView c k a → b Source # foldr' ∷ (a → b → b) → b → UView c k a → b Source # foldl ∷ (b → a → b) → b → UView c k a → b Source # foldl' ∷ (b → a → b) → b → UView c k a → b Source # foldr1 ∷ (a → a → a) → UView c k a → a Source # foldl1 ∷ (a → a → a) → UView c k a → a Source # toList ∷ UView c k a → [a] Source # null ∷ UView c k a → Bool Source # length ∷ UView c k a → Int Source # elem ∷ Eq a ⇒ a → UView c k a → Bool Source # maximum ∷ Ord a ⇒ UView c k a → a Source # minimum ∷ Ord a ⇒ UView c k a → a Source # |
rewDepUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) RDPair Source #
Construct a RewDepUView
from the two maps that make up a UMap
ptrUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c Ptr (Credential 'Staking c) Source #
sPoolUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) (KeyHash 'StakePool c) Source #
Construct a SPoolUView
from the two maps that make up a UMap
dRepUView ∷ Map (Credential 'Staking c) (UMElem c) → Map Ptr (Credential 'Staking c) → UView c (Credential 'Staking c) (DRep c) Source #
unUnifyToVMap ∷ UView c 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 c → Map (Credential 'Staking c) (CompactForm Coin) Source #
depositMap ∷ UMap c → Map (Credential 'Staking c) 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 c k RDPair → CompactForm Coin Source #
sumDepositUView ∷ UView c k RDPair → CompactForm Coin Source #
unify ∷ Map (Credential 'Staking c) RDPair → Map Ptr (Credential 'Staking c) → Map (Credential 'Staking c) (KeyHash 'StakePool c) → Map (Credential 'Staking c) (DRep c) → UMap c Source #
Create a UMap from 4 separate maps. NOTE: For use in tests only.
unUnify ∷ UView c 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 c k v → UMap c Source #
insertWith' ∷ (v → v → v) → k → v → UView c k v → UView c 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 c k v → (k, v) → UMap c 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 c k v → (k, v) → UMap c 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 c k v → Map k v → UMap c 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 c k v → Map k v → UMap c 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 c (Credential 'Staking c) RDPair → Map (Credential 'Staking c) (CompactForm Coin) → UMap c 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 c (Credential 'Staking c) RDPair → Map (Credential 'Staking c) (CompactForm Coin) → UMap c 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 c k RDPair → Map k (CompactForm Coin) → UMap c 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 c 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 c 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 c 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 c) → UMap c → UMap c 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 c → UMap c → UMap c Source #
Completely remove the staking credential from the UMap, including all associated pointers.
extractStakingCredential ∷ Credential 'Staking c → UMap c → (Maybe (UMElem c), UMap c) Source #
Just like deleteStakingCredential
, but also returned the removed element.