Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions chainbase/src/main/java/org/tron/core/service/MortgageService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.tron.core.service;

import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
Expand Down Expand Up @@ -51,7 +49,8 @@ public void initStore(WitnessStore witnessStore, DelegationStore delegationStore
}

public void payStandbyWitness() {
List<WitnessCapsule> witnessStandbys = witnessStore.getWitnessStandby();
List<WitnessCapsule> witnessStandbys = witnessStore.getWitnessStandby(
dynamicPropertiesStore.allowWitnessSortOptimization());
long voteSum = witnessStandbys.stream().mapToLong(WitnessCapsule::getVoteCount).sum();
if (voteSum < 1) {
return;
Expand Down Expand Up @@ -227,10 +226,6 @@ private long computeReward(long beginCycle, long endCycle, AccountCapsule accoun
return reward;
}

public WitnessCapsule getWitnessByAddress(ByteString address) {
return witnessStore.get(address.toByteArray());
}

public void adjustAllowance(byte[] address, long amount) {
try {
if (amount <= 0) {
Expand Down Expand Up @@ -259,11 +254,6 @@ public void adjustAllowance(AccountStore accountStore, byte[] accountAddress, lo
accountStore.put(account.createDbKey(), account);
}

private void sortWitness(List<ByteString> list) {
list.sort(Comparator.comparingLong((ByteString b) -> getWitnessByAddress(b).getVoteCount())
.reversed().thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));
}

private long getOldReward(long begin, long end, List<Pair<byte[], Long>> votes) {
if (dynamicPropertiesStore.allowOldRewardOpt()) {
return rewardViCalService.getNewRewardAlgorithmReward(begin, end, votes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,10 @@ public boolean allowConsensusLogicOptimization() {
return getConsensusLogicOptimization() == 1L;
}

public boolean allowWitnessSortOptimization() {
return this.allowConsensusLogicOptimization();
}

private static class DynamicResourceProperties {

private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes();
Expand Down
22 changes: 18 additions & 4 deletions chainbase/src/main/java/org/tron/core/store/WitnessStore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.core.store;

import com.google.common.collect.Streams;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -11,6 +12,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.utils.ByteArray;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.Parameter;
import org.tron.core.db.TronStoreWithRevoking;
Expand Down Expand Up @@ -39,12 +41,10 @@ public WitnessCapsule get(byte[] key) {
return ArrayUtils.isEmpty(value) ? null : new WitnessCapsule(value);
}

public List<WitnessCapsule> getWitnessStandby() {
public List<WitnessCapsule> getWitnessStandby(boolean isSortOpt) {
List<WitnessCapsule> ret;
List<WitnessCapsule> all = getAllWitnesses();
all.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount)
.reversed().thenComparing(Comparator.comparingInt(
(WitnessCapsule w) -> w.getAddress().hashCode()).reversed()));
sortWitnesses(all, isSortOpt);
if (all.size() > Parameter.ChainConstant.WITNESS_STANDBY_LENGTH) {
ret = new ArrayList<>(all.subList(0, Parameter.ChainConstant.WITNESS_STANDBY_LENGTH));
} else {
Expand All @@ -55,4 +55,18 @@ public List<WitnessCapsule> getWitnessStandby() {
return ret;
}

public void sortWitnesses(List<WitnessCapsule> witnesses, boolean isSortOpt) {
witnesses.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount).reversed()
.thenComparing(isSortOpt
? Comparator.comparing(WitnessCapsule::createReadableString).reversed()
: Comparator.comparingInt((WitnessCapsule w) -> w.getAddress().hashCode()).reversed()));
}

public void sortWitness(List<ByteString> list, boolean isSortOpt) {
list.sort(Comparator.comparingLong((ByteString b) -> get(b.toByteArray()).getVoteCount())
.reversed().thenComparing(isSortOpt
? Comparator.comparing(
(ByteString b) -> ByteArray.toHexString(b.toByteArray())).reversed()
: Comparator.comparingInt(ByteString::hashCode).reversed()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,8 @@ public void applyBlock(boolean flag) {
public boolean allowChangeDelegation() {
return dynamicPropertiesStore.allowChangeDelegation();
}

public void sortWitness(List<ByteString> list) {
witnessStore.sortWitness(list, dynamicPropertiesStore.allowWitnessSortOptimization());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ private void updateSolidBlock() {
}

public void updateWitness(List<ByteString> list) {
list.sort(Comparator.comparingLong((ByteString b) ->
consensusDelegate.getWitness(b.toByteArray()).getVoteCount())
.reversed()
.thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));

consensusDelegate.sortWitness(list);
if (list.size() > MAX_ACTIVE_WITNESS_NUM) {
consensusDelegate
.saveActiveWitnesses(list.subList(0, MAX_ACTIVE_WITNESS_NUM));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class VoteWitnessActuatorTest extends BaseTest {

static {
Args.setParam(new String[]{"--output-directory", dbPath()}, Constant.TEST_CONF);
Args.getInstance().setConsensusLogicOptimization(1);
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
WITNESS_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
WITNESS_ADDRESS_NOACCOUNT =
Expand Down
6 changes: 4 additions & 2 deletions framework/src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ public void pushSwitchFork()
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
chainManager.addWitness(ByteString.copyFrom(address));
List<WitnessCapsule> witnessStandby1 = chainManager.getWitnessStore().getWitnessStandby();
List<WitnessCapsule> witnessStandby1 = chainManager.getWitnessStore().getWitnessStandby(
chainManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
Block block = getSignedBlock(witnessCapsule.getAddress(), 1533529947843L, privateKey);
dbManager.pushBlock(new BlockCapsule(block));

Expand Down Expand Up @@ -656,7 +657,8 @@ public void pushSwitchFork()
Assert.assertTrue(e instanceof Exception);
}
chainManager.getWitnessStore().put(address, sr2);
List<WitnessCapsule> witnessStandby2 = chainManager.getWitnessStore().getWitnessStandby();
List<WitnessCapsule> witnessStandby2 = chainManager.getWitnessStore().getWitnessStandby(
chainManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
Assert.assertNotEquals(witnessStandby1, witnessStandby2);
}

Expand Down
27 changes: 26 additions & 1 deletion framework/src/test/java/org/tron/core/db/WitnessStoreTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.tron.core.db;

import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
Expand Down Expand Up @@ -46,5 +49,27 @@ public void putAndGetWitness() {
Assert.assertEquals(100L, witnessSource.getVoteCount());
}


@Test
public void testSortWitness() {
this.witnessStore.reset();
WitnessCapsule s1 = new WitnessCapsule(
ByteString.copyFrom(new byte[]{1, 2, 3}), 100L, "URL-1");
this.witnessStore.put(s1.getAddress().toByteArray(), s1);
WitnessCapsule s2 = new WitnessCapsule(
ByteString.copyFrom(new byte[]{1, 1, 34}), 100L, "URL-2");
this.witnessStore.put(s2.getAddress().toByteArray(), s2);
List<WitnessCapsule> allWitnesses = this.witnessStore.getAllWitnesses();
List<ByteString> witnessAddress = allWitnesses.stream().map(WitnessCapsule::getAddress)
.collect(Collectors.toList());
this.witnessStore.sortWitness(witnessAddress, false);
this.witnessStore.sortWitnesses(allWitnesses, false);
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
.collect(Collectors.toList()));
List<ByteString> pre = new ArrayList<>(witnessAddress);
this.witnessStore.sortWitness(witnessAddress, true);
this.witnessStore.sortWitnesses(allWitnesses, true);
Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress)
.collect(Collectors.toList()));
Assert.assertNotEquals(pre, witnessAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private void testWithdraw() {

public void test() {
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
manager.getDynamicPropertiesStore().saveConsensusLogicOptimization(1);
byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD");
manager.getDelegationStore().setBrokerage(0, sr27, 10);
manager.getDelegationStore().setBrokerage(1, sr27, 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void testUpdateConsensusLogicOptimization() {
long v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization();
Assert.assertEquals(v, 0);
Assert.assertTrue(!dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization());
Assert.assertFalse(dbManager.getDynamicPropertiesStore().allowWitnessSortOptimization());

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

Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization());
Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowWitnessSortOptimization());
}

}