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

[WIP] Annotate headers with time #1288

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

dnadales
Copy link
Member

Introduce a new HeaderWithTime data type, which associates the relative slot time the chain sync client mini-protocol received the header.

Introduce a new `HeaderWithTime` data type which associates the
relative slot time the header was received by the chain sync client
mini-protocol.
From

```
BlockFetchConsensusInterface (ConnectionId addrNTN) (Header blk) blk m
```

to

```
BlockFetchConsensusInterface (ConnectionId addrNTN) (HeaderWithTime blk) blk m
```

The next commits will start implementing the `undefined` in the code.
dnadales and others added 5 commits November 14, 2024 12:13
This is semantically identical (as the LoE fragment is anchored in a recent
immutable tip), but it is nice to use a more familiar function, and it is also
potentially useful for a planned change.
... from `AnchoredFragment (Header blk)` to
`AnchoredFragment (HeaderWithTime blk)`.
... which contains an `AddBogusTime` type class, which allows to add
bogus times to headers. This type class and its instances are meant to
be used in test code.
@dnadales dnadales force-pushed the dnadales/annotate-headers-with-time branch from b76d77c to 2b11f09 Compare November 19, 2024 13:11
... from `AnchoredFragment (Header TestBlock))` to `AnchoredFragment (HeaderWithTime TestBlock))`.
... from `AnchoredFragment (Header TestBlock))` to `AnchoredFragment (HeaderWithTime TestBlock))`.
... in the density disconnect tests.
... to `AnchoredFragment (HeaderWithTime blk)`.
Projecting a header
-------------------------------------------------------------------------------}

-- REVIEW: we could consider placing this somewhere else.
Copy link
Member

Choose a reason for hiding this comment

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

What about Ouroboros.Consensus.Block.Abstract? This module defines GetHeader which contains getHeader :: blk -> Header blk which is quite similar to projectHeader.

One could even move getHeader to ProjectHeader and remove projectHeader like this:

class (HasHeader blk, HasHeader a) => ProjectHeader a blk | a -> blk where
  getHeader :: a -> Header blk


class (HasHeader (Header blk), ProjectHeader blk blk) => GetHeader blk where
  ...

(This will probably require small changes in various places, so could also be done separately.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that'd make sense. If the change is a sensible generalization of Consensus, it could be merged before this PR.

Copy link
Member Author

@dnadales dnadales Nov 21, 2024

Choose a reason for hiding this comment

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

The preferred way to move this forward would be to rename GetHeader to something like BlockSupportsHeader, and rename ProjectHeader to GetHeader. The method getHeader will have to be removed from what is now the GetHeader class. (thus ending in the new GetHeader class).

-> Ordering
compareCandidateChains = compareAnchoredFragments bcfg

headerForgeUTCTime = slotForgeTime . headerRealPoint . unFromConsensus
headerForgeUTCTime = slotForgeTime . headerRealPoint . hwtHeader . unFromConsensus
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment here that this is the place to adapt once IntersectMBO/ouroboros-network#5009 is integrated into Consensus?

Copy link
Member Author

Choose a reason for hiding this comment

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

The blockForgeUTCTime definition you mean?

Copy link
Member

Choose a reason for hiding this comment

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

Both blockForgeUTCTime and headerForgeUTCTime. The former will just be gone, and the latter can stop relying on slotForgeTime (which can be removed) and instead be replaced with projecting out the time from HeaderWithTime. (Technically, that can already happen now, but it would be a bit weird as blockForgeUTCTime still has to use slotForgeTime.)

Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: change the implementation of headerForgeUTCTime to use the HeaderWithTime annotation, and add the comment Alex mentioned above blockForgeUTCTime.

AnchoredFragment (Header blk)
-> HeaderStateHistory blk
-> AnchoredFragment (HeaderWithTime blk)
withTime fragment history = undefined fragment history
Copy link
Member

Choose a reason for hiding this comment

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

Reifying this undefined as a review comment

Comment on lines +91 to +92
-> AnchoredFragment (t blk)
-> AnchoredFragment (t blk)
Copy link
Member

Choose a reason for hiding this comment

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

The payloads could also be different, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good one. I think so, let me try.

Comment on lines 30 to 33
-- REVIEW: I even wonder if this instance should be defined here and
-- at all: there might be use cases in which the 'TestBlock's are used
-- in tests that depend on the header having a meaningful time
-- associated with them.
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, one idea: All tests where you are currently adding bogus times are using non-HFC blocks. For these, time translation is very easy as there just is a constant slot length, so this wouldn't even be a "bogus" time!

attachSlotTime ::
     NoHardForks blk
  => TopLevelConfig blk
  -> Header blk
  -> HeaderWithTime blk
attachSlotTime cfg hdr = HeaderWithTime {
      hwtHeader           = hdr
    , hwtSlotRelativeTime =
        runIdentity $ epochInfoSlotToRelativeTime ei (blockSlot hdr)
    }
  where
    ei = noHardForksEpochInfo cfg

The important thing here is that we can get a single EraParams from the TopLevelConfig. NoHardForks provides that via getEraParams, but it has SingleEraBlock as a superclass constraint, which our TestBlocks don't implement (they could, but it requires implementing lots of "boring" stuff so nobody has done that yet). To sidestep that, we could move getEraParams to a separate class that doesn't demand SingleEraBlock instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

An alternative is to extract the logic of noHardForksEpochInfo and use it in attachSlotTime directly. For this, we need to rely on a function that extracts the era-params from a top-level config (which is always true for our tests).

... and place it in a new class called `HasEraParams`.
... in favour of `attachSlotTimeToFragment` and `attahcSlotTime`,
respectively. These functions rely on the `HasEraParams` type class
which allows us to extract the `EpochInfo` needed for time
conversions.

For those tests for which we do not have a top level configuration
available, we rely on `singleNodeTestConfig`.
This class generalizes this function in the sense that it can extract
headers of any values that support this operation, not only blocks.

Required by #1301
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.

2 participants