Skip to content
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

Translate tables if needed when pushing diffs in V2 #1360

Open
wants to merge 2 commits into
base: utxo-hd-main
Choose a base branch
from

Conversation

jasagredo
Copy link
Contributor

The new function upgradeTables (local to the V2 implementation) will be used when pushing diffs, giving it the current tip and the new tip with diffs (as returned by the ledger rules). If can then choose to eject all txouts to the new tip index (performing translations) then reinjecting them. This should only be called when we are crossing an era boundary.

atomically
$ modifyTVar tv
(\r -> guardClosed r (LedgerTablesHandleOpen . flip (ltliftA2 (\(ValuesMK vals) (DiffMK d) -> ValuesMK (Diff.applyDiff vals d))) diffs))
(\r -> guardClosed r (LedgerTablesHandleOpen . flip (ltliftA2 (\(ValuesMK vals) (DiffMK d) -> ValuesMK (Diff.applyDiff vals d))) (projectLedgerTables diffs) . upgradeTables st0 diffs))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the place where upgradeTables is used.

@@ -1110,6 +1112,44 @@ class ( Show (HardForkTxOut xs)
default txOutEjections :: CanHardFork xs => NP (K (NS WrapTxOut xs) -.-> WrapTxOut) xs
txOutEjections = composeTxOutTranslations $ ipTranslateTxOut hardForkEraTranslation

instance (CanHardFork xs, HasHardForkTxOut xs) => CanUpgradeLedgerTables (LedgerState (HardForkBlock xs)) where
upgradeTables
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is where we define it for the HardForkBlock (so for Cardano)

@@ -527,3 +528,6 @@ decodeByronResult :: BlockQuery ByronBlock fp result
-> forall s. Decoder s result
decodeByronResult query = case query of
GetUpdateInterfaceState -> fromByronCBOR

instance V2.CanUpgradeLedgerTables (LedgerState ByronBlock) where
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Byron instance is trivial

@@ -754,3 +755,6 @@ decodeShelleyLedgerState = decodeVersion [
, shelleyLedgerTransition
, shelleyLedgerTables = emptyLedgerTables
}

instance V2.CanUpgradeLedgerTables (LedgerState (ShelleyBlock proto era)) where
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shelley instance is trivial

@jasagredo jasagredo force-pushed the utxo-hd-txout-fwd-3 branch from 5f97360 to 88b224f Compare January 17, 2025 12:01
pushDiffs newtbs diffs
newst = forgetLedgerTables st'

pushDiffs newtbs st' st'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is wrong. It should be st st'

Copy link
Contributor

@jorisdral jorisdral left a comment

Choose a reason for hiding this comment

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

Looks okay!

@@ -178,6 +178,7 @@ load ::
( LedgerDbSerialiseConstraints blk
, CanStowLedgerTables (LedgerState blk)
, LedgerSupportsProtocol blk
, V2.CanUpgradeLedgerTables (LedgerState blk)
Copy link
Contributor

Choose a reason for hiding this comment

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

LedgerSupportsV2LedgerDB

@@ -226,6 +227,7 @@ store ::
( LedgerDbSerialiseConstraints blk
, CanStowLedgerTables (LedgerState blk)
, LedgerSupportsProtocol blk
, V2.CanUpgradeLedgerTables (LedgerState blk)
Copy link
Contributor

Choose a reason for hiding this comment

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

LedgerSupportsV2LedgerDB

LedgerTables
$ ValuesMK
$ (
if (nsToIndex $ Telescope.tip hs0) /= (nsToIndex $ Telescope.tip hs1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can write something more direct by recursing on both telescopes. Or maybe something like this already exists, like matchTelescope.

It's maybe not a super important optimisation to apply, what do you think? We'll be calling this function every time we push diffs

Comment on lines +1123 to +1126
if (nsToIndex $ Telescope.tip hs0) /= (nsToIndex $ Telescope.tip hs1)
then extendTables (hmap (const (K ())) $ Telescope.tip hs1)
else id)
$ vs
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling Telescope.tip on hs1 twice

@@ -106,6 +107,7 @@ class ( LedgerSupportsProtocol blk
, ShowProxy (BlockQuery blk)
, ShowProxy (TxId (GenTx blk))
, (forall fp. ShowQuery (BlockQuery blk fp))
, V2.LedgerSupportsV2LedgerDB blk
Copy link
Contributor

Choose a reason for hiding this comment

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

The V2. prefix might be dropped, since the constraint name already includes V2. This also applies to the same qualified imports in the other modules in this PR

Comment on lines 23 to +24
-- * Snapshots
, CanUpgradeLedgerTables (..)
Copy link
Contributor

Choose a reason for hiding this comment

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

CanUpgradeLedgerTables is not part of snapshots

@@ -104,10 +116,10 @@ newInMemoryLedgerTablesHandle someFS@(SomeHasFS hasFS) l = do
, readAll = do
hs <- readTVarIO tv
guardClosed hs pure
, pushDiffs = \(!diffs) ->
, pushDiffs = \st0 (!diffs) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
, pushDiffs = \st0 (!diffs) ->
, pushDiffs = \st0 !diffs ->

pushDiffs newtbs diffs
newst = forgetLedgerTables st'

pushDiffs newtbs st' st'
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks wrong, passing in the same state twice?

@@ -81,7 +81,7 @@ data LedgerTablesHandle m l = LedgerTablesHandle {
-- | Costly read all operation, not to be used in Consensus but only in
-- snapshot-converter executable.
, readAll :: !(m (LedgerTables l ValuesMK))
, pushDiffs :: !(LedgerTables l DiffMK -> m ())
, pushDiffs :: !(forall mk. l mk -> l DiffMK -> m ())
Copy link
Contributor

Choose a reason for hiding this comment

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

This would benefit from some docs explaining about how the two ledger state should be related

@@ -1110,6 +1112,44 @@ class ( Show (HardForkTxOut xs)
default txOutEjections :: CanHardFork xs => NP (K (NS WrapTxOut xs) -.-> WrapTxOut) xs
txOutEjections = composeTxOutTranslations $ ipTranslateTxOut hardForkEraTranslation

instance (CanHardFork xs, HasHardForkTxOut xs) => CanUpgradeLedgerTables (LedgerState (HardForkBlock xs)) where
Copy link
Contributor

Choose a reason for hiding this comment

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

Slightly long line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants