@@ -65,17 +65,26 @@ pub fn compare_ballots(
6565 ) )
6666}
6767
68+ /// Result of ballot validation with detailed context for error reporting
69+ #[ derive( Debug ) ]
70+ pub struct BallotValidationResult {
71+ pub is_valid : bool ,
72+ pub incoming_ballot : protocol:: Ballot ,
73+ pub stored_ballot : protocol:: Ballot ,
74+ pub comparison : std:: cmp:: Ordering ,
75+ }
76+
6877/// Validate that a ballot is the highest seen for the given instance & updates the highest stored
6978/// ballot if needed.
7079///
71- /// Returns true if the ballot is valid (higher than previously seen) .
80+ /// Returns detailed validation result including comparison information .
7281#[ tracing:: instrument( skip_all) ]
7382pub async fn validate_and_update_ballot_for_instance (
7483 tx : & Transaction ,
7584 replica_id : protocol:: ReplicaId ,
7685 ballot : & protocol:: Ballot ,
7786 instance : & protocol:: Instance ,
78- ) -> Result < bool > {
87+ ) -> Result < BallotValidationResult > {
7988 let instance_ballot_key =
8089 keys:: replica:: InstanceBallotKey :: new ( instance. replica_id , instance. slot_id ) ;
8190 let subspace = keys:: subspace ( replica_id) ;
@@ -98,18 +107,24 @@ pub async fn validate_and_update_ballot_for_instance(
98107 } ;
99108
100109 // Compare incoming ballot with highest seen - only accept if strictly greater
101- let is_valid = match compare_ballots ( ballot, & highest_ballot) {
110+ let comparison = compare_ballots ( ballot, & highest_ballot) ;
111+ let is_valid = match comparison {
102112 std:: cmp:: Ordering :: Greater => true ,
103113 std:: cmp:: Ordering :: Equal | std:: cmp:: Ordering :: Less => false ,
104114 } ;
105115
106116 // If the incoming ballot is higher, update our stored highest
107- if compare_ballots ( ballot , & highest_ballot ) == std:: cmp:: Ordering :: Greater {
117+ if comparison == std:: cmp:: Ordering :: Greater {
108118 let serialized = instance_ballot_key. serialize ( ballot. clone ( ) ) ?;
109119 tx. set ( & packed_key, & serialized) ;
110120
111121 tracing:: debug!( ?ballot, ?instance, "updated highest ballot for instance" ) ;
112122 }
113123
114- Ok ( is_valid)
124+ Ok ( BallotValidationResult {
125+ is_valid,
126+ incoming_ballot : ballot. clone ( ) ,
127+ stored_ballot : highest_ballot,
128+ comparison,
129+ } )
115130}
0 commit comments