@@ -775,7 +775,6 @@ public WitnessList getPaginatedNowWitnessList(long offset, long limit) {
775775 if (limit <= 0 || offset < 0 ) {
776776 return null ;
777777 }
778- // To control the maximum response size less than 40KB.
779778 if (limit > WITNESS_LIST_LIMIT_MAX ) {
780779 limit = WITNESS_LIST_LIMIT_MAX ;
781780 }
@@ -790,41 +789,46 @@ public WitnessList getPaginatedNowWitnessList(long offset, long limit) {
790789 // Count the vote changes for each witness in the current epoch, it is maybe negative.
791790 Map <ByteString , Long > countWitness = countVote (votesStore );
792791
793- // Iterate through the witness list and apply the vote changes, it will be the realtime vote count.
792+ // Iterate through the witness list to apply vote changes and calculate the real-time vote count
794793 witnessCapsuleList .forEach ((witnessCapsule ) -> {
795794 long voteCount = countWitness .getOrDefault (witnessCapsule .getAddress (), 0L );
796- // since the above chainBaseManager.getWitnessStore().getAllWitnesses()
797- // each time return a copy of the witness list, thus this update will not affect the original database list.
798795 witnessCapsule .setVoteCount (witnessCapsule .getVoteCount () + voteCount );
799796 });
800797
801- // Use the same logic as in the Maintenance period
798+ // Use the same sorting logic as in the Maintenance period
802799 WitnessStore .sortWitnesses (witnessCapsuleList ,
803800 chainBaseManager .getDynamicPropertiesStore ().allowWitnessSortOptimization ());
804801
805- // Return the witness with the highest vote counts at first and skip the offset with limit
806802 List <WitnessCapsule > sortedWitnessList = witnessCapsuleList .stream ()
807803 .skip (offset )
808804 .limit (limit )
809805 .collect (Collectors .toList ());
810806
811- // Pack the sorted WitnessCapsule list into a WitnessList object
812807 WitnessList .Builder builder = WitnessList .newBuilder ();
813808 sortedWitnessList .forEach (witnessCapsule ->
814809 builder .addWitnesses (witnessCapsule .getInstance ()));
815810
816811 return builder .build ();
817812 }
818813
819- /*
820- countVote(VotesStore votesStore): Counts the vote count changes for witnesses in the current epoch,
821- If the voters change their votes in the current epoch,
822- the vote count for last epoch witness will be negative, while the new vote count will be positive.
823- For example:
824- Account A votes for witness W1 in the last epoch with 100 votes,
825- in the current epoch, A change the votes for W2 with 60 votes W3 with 80 votes,
826- then the vote count for W1 will be -100, while the vote count for W2 will be 60, W3 will be 80.
827- */
814+ /**
815+ * Counts vote changes for witnesses in the current epoch.
816+ *
817+ * Vote count changes are tracked as follows:
818+ * - Negative values for votes removed from previous witness in the last epoch
819+ * - Positive values for votes added to new witness in the current epoch
820+ *
821+ * Example:
822+ * an Account X had 100 votes for witness W1 in the previous epoch.
823+ * In the current epoch, X changes votes to:
824+ * - W2: 60 votes
825+ * - W3: 80 votes
826+ *
827+ * Resulting vote changes:
828+ * - W1: -100 (votes removed)
829+ * - W2: +60 (new votes)
830+ * - W3: +80 (new votes)
831+ */
828832 private Map <ByteString , Long > countVote (VotesStore votesStore ) {
829833 // Initialize a result map to store vote changes for each witness
830834 Map <ByteString , Long > countWitness = Maps .newHashMap ();
@@ -836,7 +840,11 @@ private Map<ByteString, Long> countVote(VotesStore votesStore) {
836840 Entry <byte [], VotesCapsule > next = dbIterator .next ();
837841 VotesCapsule votes = next .getValue ();
838842
839- // For each voter, we iterate the old votes and new votes to calculate the vote count changes
843+ /**
844+ * VotesCapsule contains two lists:
845+ * - Old votes: Last votes from the previous epoch, updated in maintenance period
846+ * - New votes: Latest votes in current epoch, updated after each vote transaction
847+ */
840848 votes .getOldVotes ().forEach (vote -> {
841849 ByteString voteAddress = vote .getVoteAddress ();
842850 long voteCount = vote .getVoteCount ();
0 commit comments