Skip to content

Conversation

@agustinmista
Copy link
Contributor

@agustinmista agustinmista requested a review from amesgen October 21, 2025 10:55
@agustinmista agustinmista self-assigned this Oct 21, 2025
Comment on lines 13 to 18
, perasVotingRule1A
, perasVotingRule1B
, perasVotingRule2A
, perasVotingRule2B
, perasVotingRule1
, perasVotingRule2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be a good idea not to export these unless we want to test them individually.

Comment on lines 89 to 100
withPerasVotingView ::
PerasParams ->
Set (AnchoredFragment blk) ->
WithArrivalTime (ValidatedPerasCert blk) ->
WithArrivalTime (ValidatedPerasCert blk) ->
(SlotNo -> PerasRoundNo) ->
(SlotNo -> SlotNo) ->
(SlotNo -> Point blk) ->
(Point blk -> WithArrivalTime (ValidatedPerasCert blk) -> Set (AnchoredFragment blk) -> Bool) ->
(WithArrivalTime (ValidatedPerasCert blk) -> SlotNo) ->
(PerasVotingView blk -> STM m a) ->
STM m a
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 a sketch for now. The idea is to derive most of these inputs from the context (e.g. a ChainDB instance)

Comment on lines 131 to 138
perasVotingRule1A :: StandardHash blk => SlotNo -> PerasVotingView blk -> Bool
perasVotingRule1A s t =
and
[ pvvRound t s == getPerasCertRound (pvvLatestCertSeen t) <> coerce @Word64 1
, pvvArrivalSlot t (pvvLatestCertSeen t) <= pvvRoundStart t s + _X
]
where
_X = coerce (perasCertArrivalThreshold (pvvPerasParams t))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the extended definition from tweag/cardano-peras#99, this should be the only rule that doesn't follow verbatim the CIP-0140

Copy link
Contributor Author

@agustinmista agustinmista left a comment

Choose a reason for hiding this comment

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

Some implementation details are yet to be defined, especially around PerasVotingView

@agustinmista agustinmista force-pushed the peras/pure-voting-rules branch from abde225 to 8f63b94 Compare October 21, 2025 11:15
Copy link
Member

@amesgen amesgen left a comment

Choose a reason for hiding this comment

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

Looks good, some first thoughts

-- necessary inpure machinery to retrieve their inputs. These rules are translated
-- as verbatim as possible from:
--
-- https://github.com/cardano-foundation/CIPs/blob/master/CIP-0140/README.md#rules-for-voting-in-a-round
Copy link
Member

@amesgen amesgen Oct 21, 2025

Choose a reason for hiding this comment

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

Probably makes sense to mention that VR-1A is a bit different because it now also takes the arrival time of the certificate into account.

EDIT: Ah, didn't see #1723 (comment) in time

Comment on lines 190 to 196
-- | If either VR-1A and VR-1B hold, or VR-2A and VR-2B hold, then voting is allowed.
isPerasVotingAllowed :: StandardHash blk => SlotNo -> PerasVotingView blk -> Bool
isPerasVotingAllowed s t =
or
[ perasVotingRule1 s t,
perasVotingRule2 (pvvRound t s) t
]
Copy link
Member

Choose a reason for hiding this comment

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

I think we want a somewhat richer return type, eg Either NoVoteReason VoteReason. This can then be used for tracing to signal why we did (not) vote, eg

  • We did not vote because the previous round didn't give rise to a quorum.
  • We did not vote because the cert from the previous round arrived too late.
  • We voted for the first time after a cooldown period.

(More abstract perspective: We essentially want to explain why a predicate succeeded/failed, so there is some overlap with general certificates for the truthiness of logical terms, as well as libraries like explainable-predicates, but we should probably use sth ad-hoc; maybe there is room for a mini-DSL here)

Later, we could set up conformance testing against the Agda implementation.

Comment on lines 49 to 89
data PerasVotingView blk = PerasVotingView
{ pvvPerasParams ::
PerasParams
-- ^ Peras protocol parameters
, pvvAllChains ::
Set (AnchoredFragment blk)
-- ^ All known chains fragments
, pvvLatestCertSeen ::
WithArrivalTime (ValidatedPerasCert blk)
-- ^ The most recent certificate seen by the voter.
, pvvLatestCertOnChain ::
WithArrivalTime (ValidatedPerasCert blk)
-- ^ The most recent certificate present in some chain.
, pvvRound ::
SlotNo ->
PerasRoundNo
-- ^ Get the Peras round number corresponding to a slot.
, pvvRoundStart ::
SlotNo ->
SlotNo
-- ^ Get the slot number corresponding to the start of the Peras round for a slot.
, pvvBlockSelection ::
SlotNo ->
Point blk
-- ^ The most recent block on the preferred chain that is at least L slots old.
, pvvExtends ::
Point blk ->
WithArrivalTime (ValidatedPerasCert blk) ->
Set (AnchoredFragment blk) ->
Bool
-- ^ Does this block extend the one boosted by a certificate and is present in some chain?
, pvvArrivalSlot ::
WithArrivalTime (ValidatedPerasCert blk) ->
SlotNo
-- ^ Transform the arrival time of a certificate into its corresponding slot number
}
Copy link
Member

Choose a reason for hiding this comment

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

Some of these things are fallible in practice for boring reasons (PastHorizonException for time translations due to the HFC). We should think about this a bit; I think we can add appropriate preconditions (eg pvvRoundStart may only be invoked for slots older than the current slot) to keep things simple.

@agustinmista agustinmista force-pushed the peras/pure-voting-rules branch from 5ceeebd to 3c642f5 Compare October 22, 2025 14:23
@agustinmista agustinmista force-pushed the peras/pure-voting-rules branch from 3c642f5 to 7b568d4 Compare October 23, 2025 14:45
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