Skip to content

Commit b1ac699

Browse files
Merge pull request #6173 from halibobo1205/feat/sr_sort_opt
feat(witness): sort witness from address hashcode to address
2 parents 409c420 + 4e4881f commit b1ac699

File tree

10 files changed

+63
-24
lines changed

10 files changed

+63
-24
lines changed

chainbase/src/main/java/org/tron/core/service/MortgageService.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.tron.core.service;
22

3-
import com.google.protobuf.ByteString;
43
import java.math.BigInteger;
5-
import java.util.Comparator;
64
import java.util.List;
75
import java.util.stream.Collectors;
86
import lombok.Getter;
@@ -51,7 +49,8 @@ public void initStore(WitnessStore witnessStore, DelegationStore delegationStore
5149
}
5250

5351
public void payStandbyWitness() {
54-
List<WitnessCapsule> witnessStandbys = witnessStore.getWitnessStandby();
52+
List<WitnessCapsule> witnessStandbys = witnessStore.getWitnessStandby(
53+
dynamicPropertiesStore.allowWitnessSortOptimization());
5554
long voteSum = witnessStandbys.stream().mapToLong(WitnessCapsule::getVoteCount).sum();
5655
if (voteSum < 1) {
5756
return;
@@ -227,10 +226,6 @@ private long computeReward(long beginCycle, long endCycle, AccountCapsule accoun
227226
return reward;
228227
}
229228

230-
public WitnessCapsule getWitnessByAddress(ByteString address) {
231-
return witnessStore.get(address.toByteArray());
232-
}
233-
234229
public void adjustAllowance(byte[] address, long amount) {
235230
try {
236231
if (amount <= 0) {
@@ -259,11 +254,6 @@ public void adjustAllowance(AccountStore accountStore, byte[] accountAddress, lo
259254
accountStore.put(account.createDbKey(), account);
260255
}
261256

262-
private void sortWitness(List<ByteString> list) {
263-
list.sort(Comparator.comparingLong((ByteString b) -> getWitnessByAddress(b).getVoteCount())
264-
.reversed().thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));
265-
}
266-
267257
private long getOldReward(long begin, long end, List<Pair<byte[], Long>> votes) {
268258
if (dynamicPropertiesStore.allowOldRewardOpt()) {
269259
return rewardViCalService.getNewRewardAlgorithmReward(begin, end, votes);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,10 @@ public boolean allowConsensusLogicOptimization() {
29102910
return getConsensusLogicOptimization() == 1L;
29112911
}
29122912

2913+
public boolean allowWitnessSortOptimization() {
2914+
return this.allowConsensusLogicOptimization();
2915+
}
2916+
29132917
private static class DynamicResourceProperties {
29142918

29152919
private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes();

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.store;
22

33
import com.google.common.collect.Streams;
4+
import com.google.protobuf.ByteString;
45
import java.util.ArrayList;
56
import java.util.Comparator;
67
import java.util.List;
@@ -11,6 +12,7 @@
1112
import org.springframework.beans.factory.annotation.Autowired;
1213
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.stereotype.Component;
15+
import org.tron.common.utils.ByteArray;
1416
import org.tron.core.capsule.WitnessCapsule;
1517
import org.tron.core.config.Parameter;
1618
import org.tron.core.db.TronStoreWithRevoking;
@@ -39,12 +41,10 @@ public WitnessCapsule get(byte[] key) {
3941
return ArrayUtils.isEmpty(value) ? null : new WitnessCapsule(value);
4042
}
4143

42-
public List<WitnessCapsule> getWitnessStandby() {
44+
public List<WitnessCapsule> getWitnessStandby(boolean isSortOpt) {
4345
List<WitnessCapsule> ret;
4446
List<WitnessCapsule> all = getAllWitnesses();
45-
all.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount)
46-
.reversed().thenComparing(Comparator.comparingInt(
47-
(WitnessCapsule w) -> w.getAddress().hashCode()).reversed()));
47+
sortWitnesses(all, isSortOpt);
4848
if (all.size() > Parameter.ChainConstant.WITNESS_STANDBY_LENGTH) {
4949
ret = new ArrayList<>(all.subList(0, Parameter.ChainConstant.WITNESS_STANDBY_LENGTH));
5050
} else {
@@ -55,4 +55,18 @@ public List<WitnessCapsule> getWitnessStandby() {
5555
return ret;
5656
}
5757

58+
public void sortWitnesses(List<WitnessCapsule> witnesses, boolean isSortOpt) {
59+
witnesses.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount).reversed()
60+
.thenComparing(isSortOpt
61+
? Comparator.comparing(WitnessCapsule::createReadableString).reversed()
62+
: Comparator.comparingInt((WitnessCapsule w) -> w.getAddress().hashCode()).reversed()));
63+
}
64+
65+
public void sortWitness(List<ByteString> list, boolean isSortOpt) {
66+
list.sort(Comparator.comparingLong((ByteString b) -> get(b.toByteArray()).getVoteCount())
67+
.reversed().thenComparing(isSortOpt
68+
? Comparator.comparing(
69+
(ByteString b) -> ByteArray.toHexString(b.toByteArray())).reversed()
70+
: Comparator.comparingInt(ByteString::hashCode).reversed()));
71+
}
5872
}

consensus/src/main/java/org/tron/consensus/ConsensusDelegate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,8 @@ public void applyBlock(boolean flag) {
135135
public boolean allowChangeDelegation() {
136136
return dynamicPropertiesStore.allowChangeDelegation();
137137
}
138+
139+
public void sortWitness(List<ByteString> list) {
140+
witnessStore.sortWitness(list, dynamicPropertiesStore.allowWitnessSortOptimization());
141+
}
138142
}

consensus/src/main/java/org/tron/consensus/dpos/DposService.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ private void updateSolidBlock() {
163163
}
164164

165165
public void updateWitness(List<ByteString> list) {
166-
list.sort(Comparator.comparingLong((ByteString b) ->
167-
consensusDelegate.getWitness(b.toByteArray()).getVoteCount())
168-
.reversed()
169-
.thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));
170-
166+
consensusDelegate.sortWitness(list);
171167
if (list.size() > MAX_ACTIVE_WITNESS_NUM) {
172168
consensusDelegate
173169
.saveActiveWitnesses(list.subList(0, MAX_ACTIVE_WITNESS_NUM));

framework/src/test/java/org/tron/core/actuator/VoteWitnessActuatorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class VoteWitnessActuatorTest extends BaseTest {
5252

5353
static {
5454
Args.setParam(new String[]{"--output-directory", dbPath()}, Constant.TEST_CONF);
55+
Args.getInstance().setConsensusLogicOptimization(1);
5556
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
5657
WITNESS_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
5758
WITNESS_ADDRESS_NOACCOUNT =

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ public void pushSwitchFork()
618618
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
619619
chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
620620
chainManager.addWitness(ByteString.copyFrom(address));
621-
List<WitnessCapsule> witnessStandby1 = chainManager.getWitnessStore().getWitnessStandby();
621+
List<WitnessCapsule> witnessStandby1 = chainManager.getWitnessStore().getWitnessStandby(
622+
chainManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
622623
Block block = getSignedBlock(witnessCapsule.getAddress(), 1533529947843L, privateKey);
623624
dbManager.pushBlock(new BlockCapsule(block));
624625

@@ -656,7 +657,8 @@ public void pushSwitchFork()
656657
Assert.assertTrue(e instanceof Exception);
657658
}
658659
chainManager.getWitnessStore().put(address, sr2);
659-
List<WitnessCapsule> witnessStandby2 = chainManager.getWitnessStore().getWitnessStandby();
660+
List<WitnessCapsule> witnessStandby2 = chainManager.getWitnessStore().getWitnessStandby(
661+
chainManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
660662
Assert.assertNotEquals(witnessStandby1, witnessStandby2);
661663
}
662664

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.tron.core.db;
22

33
import com.google.protobuf.ByteString;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
47
import javax.annotation.Resource;
58
import lombok.extern.slf4j.Slf4j;
69
import org.junit.Assert;
@@ -46,5 +49,27 @@ public void putAndGetWitness() {
4649
Assert.assertEquals(100L, witnessSource.getVoteCount());
4750
}
4851

49-
52+
@Test
53+
public void testSortWitness() {
54+
this.witnessStore.reset();
55+
WitnessCapsule s1 = new WitnessCapsule(
56+
ByteString.copyFrom(new byte[]{1, 2, 3}), 100L, "URL-1");
57+
this.witnessStore.put(s1.getAddress().toByteArray(), s1);
58+
WitnessCapsule s2 = new WitnessCapsule(
59+
ByteString.copyFrom(new byte[]{1, 1, 34}), 100L, "URL-2");
60+
this.witnessStore.put(s2.getAddress().toByteArray(), s2);
61+
List<WitnessCapsule> allWitnesses = this.witnessStore.getAllWitnesses();
62+
List<ByteString> witnessAddress = allWitnesses.stream().map(WitnessCapsule::getAddress)
63+
.collect(Collectors.toList());
64+
this.witnessStore.sortWitness(witnessAddress, false);
65+
this.witnessStore.sortWitnesses(allWitnesses, false);
66+
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
67+
.collect(Collectors.toList()));
68+
List<ByteString> pre = new ArrayList<>(witnessAddress);
69+
this.witnessStore.sortWitness(witnessAddress, true);
70+
this.witnessStore.sortWitnesses(allWitnesses, true);
71+
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
72+
.collect(Collectors.toList()));
73+
Assert.assertNotEquals(pre, witnessAddress);
74+
}
5075
}

framework/src/test/java/org/tron/core/services/DelegationServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private void testWithdraw() {
107107

108108
public void test() {
109109
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
110+
manager.getDynamicPropertiesStore().saveConsensusLogicOptimization(1);
110111
byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD");
111112
manager.getDelegationStore().setBrokerage(0, sr27, 10);
112113
manager.getDelegationStore().setBrokerage(1, sr27, 20);

framework/src/test/java/org/tron/core/services/ProposalServiceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void testUpdateConsensusLogicOptimization() {
112112
long v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization();
113113
Assert.assertEquals(v, 0);
114114
Assert.assertTrue(!dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization());
115+
Assert.assertFalse(dbManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
115116

116117
long value = 1;
117118
Proposal proposal =
@@ -125,6 +126,7 @@ public void testUpdateConsensusLogicOptimization() {
125126
Assert.assertEquals(v, value);
126127

127128
Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization());
129+
Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
128130
}
129131

130132
}

0 commit comments

Comments
 (0)