Skip to content

Commit 9c03624

Browse files
committed
Merge remote-tracking branch 'upstream/release_v4.8.0' into feature/consensus_optimize_tx
2 parents b57411f + 31016d3 commit 9c03624

File tree

58 files changed

+1313
-1364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1313
-1364
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
}

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,30 @@ public class CommonParameter {
456456
@Getter
457457
@Setter
458458
public String cryptoEngine = Constant.ECKey_ENGINE;
459+
460+
@Getter
461+
@Setter
462+
public boolean rpcEnable = true;
463+
464+
@Getter
465+
@Setter
466+
public boolean rpcSolidityEnable = true;
467+
468+
@Getter
469+
@Setter
470+
public boolean rpcPBFTEnable = true;
471+
459472
@Getter
460473
@Setter
461474
public boolean fullNodeHttpEnable = true;
462475
@Getter
463476
@Setter
464477
public boolean solidityNodeHttpEnable = true;
478+
479+
@Getter
480+
@Setter
481+
public boolean pBFTHttpEnable = true;
482+
465483
@Getter
466484
@Setter
467485
public boolean jsonRpcHttpFullNodeEnable = false;

common/src/main/java/org/tron/core/Constant.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,21 @@ public class Constant {
124124
public static final String NODE_DNS_AWS_REGION = "node.dns.awsRegion";
125125
public static final String NODE_DNS_AWS_HOST_ZONE_ID = "node.dns.awsHostZoneId";
126126

127+
// config for rpc
127128
public static final String NODE_RPC_PORT = "node.rpc.port";
128129
public static final String NODE_RPC_SOLIDITY_PORT = "node.rpc.solidityPort";
129130
public static final String NODE_RPC_PBFT_PORT = "node.rpc.PBFTPort";
131+
public static final String NODE_RPC_ENABLE = "node.rpc.enable";
132+
public static final String NODE_RPC_SOLIDITY_ENABLE = "node.rpc.solidityEnable";
133+
public static final String NODE_RPC_PBFT_ENABLE = "node.rpc.PBFTEnable";
134+
// config for http
130135
public static final String NODE_HTTP_FULLNODE_PORT = "node.http.fullNodePort";
131136
public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort";
132137
public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable";
133138
public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable";
139+
public static final String NODE_HTTP_PBFT_ENABLE = "node.http.PBFTEnable";
134140
public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort";
135-
141+
// config for jsonrpc
136142
public static final String NODE_JSONRPC_HTTP_FULLNODE_ENABLE = "node.jsonrpc.httpFullNodeEnable";
137143
public static final String NODE_JSONRPC_HTTP_FULLNODE_PORT = "node.jsonrpc.httpFullNodePort";
138144
public static final String NODE_JSONRPC_HTTP_SOLIDITY_ENABLE = "node.jsonrpc.httpSolidityEnable";
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.tron.core.exception;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* If a {@link TronError} is thrown, the service will trigger {@link System#exit(int)} by
7+
* {@link Thread#setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)}.
8+
* NOTE: Do not attempt to catch {@link TronError}.
9+
*/
10+
@Getter
11+
public class TronError extends Error {
12+
13+
private final ErrCode errCode;
14+
15+
public TronError(String message, ErrCode exitCode) {
16+
super(message);
17+
this.errCode = exitCode;
18+
}
19+
20+
public TronError(String message, Throwable cause, ErrCode exitCode) {
21+
super(message, cause);
22+
this.errCode = exitCode;
23+
}
24+
25+
public TronError(Throwable cause, ErrCode exitCode) {
26+
super(cause);
27+
this.errCode = exitCode;
28+
}
29+
30+
@Getter
31+
public enum ErrCode {
32+
WITNESS_KEYSTORE_LOAD(-1),
33+
CHECKPOINT_VERSION(-1),
34+
LEVELDB_INIT(1),
35+
ROCKSDB_INIT(1),
36+
DB_FLUSH(1),
37+
REWARD_VI_CALCULATOR(1),
38+
KHAOS_DB_INIT(1),
39+
GENESIS_BLOCK_INIT(1),
40+
EVENT_SUBSCRIBE_ERROR(1),
41+
AUTO_STOP_PARAMS(1),
42+
API_SERVER_INIT(1),
43+
SOLID_NODE_INIT(0);
44+
45+
private final int code;
46+
47+
ErrCode(int code) {
48+
this.code = code;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return name() + "(" + code + ")";
54+
}
55+
}
56+
}

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));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.tron.common.application;
2+
3+
import com.google.common.base.Objects;
4+
import java.util.concurrent.CompletableFuture;
5+
import lombok.Getter;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.tron.core.config.args.Args;
8+
9+
10+
@Slf4j(topic = "service")
11+
public abstract class AbstractService implements Service {
12+
13+
protected int port;
14+
@Getter
15+
protected boolean enable;
16+
@Getter
17+
protected final String name = this.getClass().getSimpleName();
18+
19+
20+
@Override
21+
public CompletableFuture<Boolean> start() {
22+
logger.info("{} starting on {}", name, port);
23+
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
24+
try {
25+
innerStart();
26+
resultFuture.complete(true);
27+
logger.info("{} started, listening on {}", name, port);
28+
} catch (Exception e) {
29+
resultFuture.completeExceptionally(e);
30+
}
31+
return resultFuture;
32+
}
33+
34+
@Override
35+
public CompletableFuture<Boolean> stop() {
36+
logger.info("{} shutdown...", name);
37+
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
38+
try {
39+
innerStop();
40+
resultFuture.complete(true);
41+
logger.info("{} shutdown complete", name);
42+
} catch (Exception e) {
43+
resultFuture.completeExceptionally(e);
44+
}
45+
return resultFuture;
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) {
51+
return true;
52+
}
53+
if (o == null || getClass() != o.getClass()) {
54+
return false;
55+
}
56+
AbstractService that = (AbstractService) o;
57+
return port == that.port;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hashCode(name, port);
63+
}
64+
65+
public abstract void innerStart() throws Exception;
66+
67+
public abstract void innerStop() throws Exception;
68+
69+
protected boolean isFullNode() {
70+
return !Args.getInstance().isSolidityNode();
71+
}
72+
73+
}

framework/src/main/java/org/tron/common/application/Application.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,11 @@
1515

1616
package org.tron.common.application;
1717

18-
import org.tron.common.parameter.CommonParameter;
1918
import org.tron.core.ChainBaseManager;
20-
import org.tron.core.config.args.Args;
2119
import org.tron.core.db.Manager;
2220

2321
public interface Application {
2422

25-
void setOptions(Args args);
26-
27-
void init(CommonParameter parameter);
28-
29-
void initServices(CommonParameter parameter);
30-
3123
void startup();
3224

3325
void shutdown();
@@ -40,8 +32,6 @@ default void blockUntilShutdown() {
4032

4133
void shutdownServices();
4234

43-
void addService(Service service);
44-
4535
Manager getDbManager();
4636

4737
ChainBaseManager getChainBaseManager();

0 commit comments

Comments
 (0)