Skip to content

Commit 5f91628

Browse files
author
Sunny Jiao
committed
sort witness using the same logic as in maintence
1 parent 55bbebe commit 5f91628

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

chainbase/src/main/java/org/tron/core/store/WitnessStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public List<WitnessCapsule> getWitnessStandby(boolean isSortOpt) {
5555
return ret;
5656
}
5757

58-
public void sortWitnesses(List<WitnessCapsule> witnesses, boolean isSortOpt) {
58+
public static void sortWitnesses(List<WitnessCapsule> witnesses, boolean isSortOpt) {
5959
witnesses.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount).reversed()
6060
.thenComparing(isSortOpt
6161
? Comparator.comparing(WitnessCapsule::createReadableString).reversed()

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
import org.tron.core.store.MarketPairToPriceStore;
208208
import org.tron.core.store.StoreFactory;
209209
import org.tron.core.store.VotesStore;
210+
import org.tron.core.store.WitnessStore;
210211
import org.tron.core.utils.TransactionUtil;
211212
import org.tron.core.vm.program.Program;
212213
import org.tron.core.zen.ShieldedTRC20ParametersBuilder;
@@ -797,9 +798,12 @@ public WitnessList getPaginatedNowWitnessList(long offset, long limit) {
797798
witnessCapsule.setVoteCount(witnessCapsule.getVoteCount() + voteCount);
798799
});
799800

801+
// Use the same logic as in the Maintenance period
802+
WitnessStore.sortWitnesses(witnessCapsuleList,
803+
chainBaseManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
804+
800805
// Return the witness with the highest vote counts at first and skip the offset with limit
801806
List<WitnessCapsule> sortedWitnessList = witnessCapsuleList.stream()
802-
.sorted(Comparator.comparingLong(WitnessCapsule::getVoteCount).reversed())
803807
.skip(offset)
804808
.limit(limit)
805809
.collect(Collectors.toList());
@@ -837,13 +841,13 @@ private Map<ByteString, Long> countVote(VotesStore votesStore) {
837841
ByteString voteAddress = vote.getVoteAddress();
838842
long voteCount = vote.getVoteCount();
839843
countWitness.put(voteAddress,
840-
countWitness.containsKey(voteAddress) ? countWitness.get(voteAddress) : 0 - voteCount);
844+
countWitness.getOrDefault(voteAddress, 0L) - voteCount);
841845
});
842846
votes.getNewVotes().forEach(vote -> {
843847
ByteString voteAddress = vote.getVoteAddress();
844848
long voteCount = vote.getVoteCount();
845849
countWitness.put(voteAddress,
846-
countWitness.containsKey(voteAddress) ? countWitness.get(voteAddress) : 0 + voteCount);
850+
countWitness.getOrDefault(voteAddress, 0L) + voteCount);
847851
});
848852
}
849853
return countWitness;

framework/src/test/java/org/tron/core/db/WitnessStoreTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public void testSortWitness() {
6262
List<ByteString> witnessAddress = allWitnesses.stream().map(WitnessCapsule::getAddress)
6363
.collect(Collectors.toList());
6464
this.witnessStore.sortWitness(witnessAddress, false);
65-
this.witnessStore.sortWitnesses(allWitnesses, false);
65+
WitnessStore.sortWitnesses(allWitnesses, false);
6666
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
6767
.collect(Collectors.toList()));
6868
List<ByteString> pre = new ArrayList<>(witnessAddress);
6969
this.witnessStore.sortWitness(witnessAddress, true);
70-
this.witnessStore.sortWitnesses(allWitnesses, true);
70+
WitnessStore.sortWitnesses(allWitnesses, true);
7171
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
7272
.collect(Collectors.toList()));
7373
Assert.assertNotEquals(pre, witnessAddress);

0 commit comments

Comments
 (0)