Skip to content

Stop using UMap in favor of Accounts type family #5128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

lehins
Copy link
Collaborator

@lehins lehins commented Jun 18, 2025

Description

Resolves #4694

Checklist

  • Commits in meaningful sequence and with useful messages.
  • Tests added or updated when needed.
  • CHANGELOG.md files updated for packages with externally visible changes.
    NOTE: New section is never added with the code changes. (See RELEASING.md).
  • Versions updated in .cabal and CHANGELOG.md files when necessary, according to the
    versioning process.
  • Version bounds in .cabal files updated when necessary.
    NOTE: If bounds change in a cabal file, that package itself must have a version increase. (See RELEASING.md).
  • Code formatted (use scripts/fourmolize.sh).
  • Cabal files formatted (use scripts/cabal-format.sh).
  • CDDL files are up to date (use scripts/gen-cddl.sh)
  • hie.yaml updated (use scripts/gen-hie.sh).
  • Self-reviewed the diff.

@lehins lehins mentioned this pull request Jun 18, 2025
23 tasks
@lehins lehins force-pushed the lehins/era-specific-umap branch from 0fa485d to ca95503 Compare June 24, 2025 21:11
Copy link
Contributor

@teodanciu teodanciu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a first pass, will have another look later! Looks great, I finally feel I can be friends with DState 😆

-- or not.
addAccountState :: Credential 'Staking -> AccountState era -> Accounts era -> Accounts era

accountsMapL :: Lens' (Accounts era) (Map (Credential 'Staking) (AccountState era))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to follow the naming convention of the other lenses, how about:
accountStateAccountsL ? too verbose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think this will work, because it is important that it returns a Map.
Every operation that happens after usage of this lens expects a Map.

Also, technically it would have to be plural: either accountStatesAccountsL or accountsStatesAccountsL. In any case, it would be much harder to read it, since there are too many "accounts" in the name 😄 Also, I believe it would be harder to understand what the hell this lens does.

Comment on lines +143 to 145
rewardAccounts_ :: Map.Map (KeyHash 'StakePool) (RewardAccount, Coin)
rewardAccounts_ = Map.intersectionWith (,) rewardAccounts retiringDeposits
rewardAccounts' :: Map.Map RewardAccount Coin
Copy link
Contributor

@teodanciu teodanciu Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are terrible, why not something like:

Suggested change
rewardAccounts_ :: Map.Map (KeyHash 'StakePool) (RewardAccount, Coin)
rewardAccounts_ = Map.intersectionWith (,) rewardAccounts retiringDeposits
rewardAccounts' :: Map.Map RewardAccount Coin
rewardAccountsWithCoin :: Map.Map (KeyHash 'StakePool) (RewardAccount, Coin)
rewardAccountsWithCoin = Map.intersectionWith (,) rewardAccounts retiringDeposits
coinsByRewardAccount :: Map.Map RewardAccount Coin

Copy link
Contributor

@aniketd aniketd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor corrections

Copy link
Contributor

@teodanciu teodanciu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few more minor suggestions

rewardAccount
(packageLeaderReward poolRI)
acc
|| account `Map.member` accountsMap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here we could use

Suggested change
|| account `Map.member` accountsMap
| isAccountRegistered account ( ls ^. lsCertStateL . certDStateL . accountsL)

but it's more verbose, so not necessarily worth it, just if we wanted to reinforce the Accounts terminology

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice suggestion, I'll adjust it a tiny bit:

Suggested change
|| account `Map.member` accountsMap
|| isAccountRegistered account accounts

toShelleyAccountsPairs :: Aeson.KeyValue e a => ShelleyAccounts era -> [a]
toShelleyAccountsPairs sas@(ShelleyAccounts _ _) =
let ShelleyAccounts {..} = sas
in [ "credentials" .= saStates
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "credentials" accurate here? Maybe more like "states" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried a bit about breakage downstream, but I 100% agree with you that it is a terrible name, so I am very much leaning towards your suggestion of changing it.

@@ -158,75 +159,33 @@ newLab b cs =
-- 1) do not give this function duplicates in the 'stakes' or 'pools' inputs.
-- 2) do not use this function when crossing the epoch boundary,
-- instead use 'newEpoch'.
feesAndDeposits ::
addPoolDeposits ::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand well what this function was doing before, but it looks like the haddock for it no longer matches the implementation. So maybe we can remove it or change it to something like:

Suggested change
addPoolDeposits ::
Add deposits for new pools and adjust the deposit tables in the UTxOState and CertState accordingly.
addPoolDeposits ::

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the haddock, which hopefully makes it clearer what this function now does:

-- | Add new pools while updating the deposit pot

(scSPools stakeCredentials, scRewards stakeCredentials)
where
stakeCredentials = domRestrictedStakeCredentials creds umap

-- | Uses `filterStakePoolDelegsAndRewards` to get the same information from the `NewEpochState`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment no longer holds

Suggested change
-- | Uses `filterStakePoolDelegsAndRewards` to get the same information from the `NewEpochState`

Copy link
Contributor

@teodanciu teodanciu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That a huge piece of work!! I think it's a great improvement in terms of understanding not only what we store/map but also how to work with it, thanks to the clear interface.

I admit I got lost with the Transition modules, I don't understand at all what they're about, but that's on me, I shall study them.

Looks good to me, great work!

@lehins lehins force-pushed the lehins/era-specific-umap branch 3 times, most recently from 6c37635 to afaa57f Compare June 28, 2025 02:05
@TimSheard TimSheard force-pushed the lehins/era-specific-umap branch 3 times, most recently from f802c1e to 5eeb3a8 Compare July 2, 2025 12:16
lehins and others added 7 commits July 3, 2025 00:01
… AccountStates

Simplify Transition and reuse it in shelley-test

Add mkTestAccountState and addAccountState
Reworked some messages when things fail.
Removed some unused imports that were not necessary.
Changed Test.Cardano.Ledger.Constrained.Conway.LedgerTypes.Specs, by
  changing conwayCertStateSpec to pass one component of PState through reify to DState
  and then pass one component of DState though reify to VState.
  This replaces the use of two exists.
Made some small chages to MiniTrace.
Added Witnessing the rng of a map that was ConwayAccountState.
Added some Winessed instances that deal with AccountState type family and its instances.
Fix mkConwayTestAccountState which was calling era if the DRep was delegated.
@lehins lehins force-pushed the lehins/era-specific-umap branch from 0587e7f to c45599a Compare July 3, 2025 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace UMap with a better tailored representation and name
4 participants