Skip to content

Commit c33f995

Browse files
committed
optimiz the witnessAddress initialization
1 parent 041e47e commit c33f995

File tree

14 files changed

+57
-56
lines changed

14 files changed

+57
-56
lines changed

chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.tron.common.utils;
1717

1818
import com.google.common.collect.Lists;
19+
import java.util.Arrays;
1920
import java.util.List;
2021
import lombok.Getter;
2122
import lombok.extern.slf4j.Slf4j;
@@ -45,21 +46,18 @@ public LocalWitnesses(List<String> privateKeys) {
4546
setPrivateKeys(privateKeys);
4647
}
4748

48-
public byte[] getWitnessAccountAddress(boolean isECKeyCryptoEngine) {
49-
if (witnessAccountAddress == null && !CollectionUtils.isEmpty(privateKeys)) {
50-
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());
51-
final SignInterface cryptoEngine = SignUtils.fromPrivate(privateKey, isECKeyCryptoEngine);
52-
this.witnessAccountAddress = cryptoEngine.getAddress();
49+
public byte[] getWitnessAccountAddress() {
50+
if (witnessAccountAddress == null) {
51+
return null;
5352
}
54-
return witnessAccountAddress;
55-
}
56-
57-
public void setWitnessAccountAddress(final byte[] localWitnessAccountAddress) {
58-
this.witnessAccountAddress = localWitnessAccountAddress;
53+
return Arrays.copyOf(witnessAccountAddress, witnessAccountAddress.length);
5954
}
6055

61-
public void initWitnessAccountAddress(boolean isECKeyCryptoEngine) {
62-
if (witnessAccountAddress == null && !CollectionUtils.isEmpty(privateKeys)) {
56+
public void initWitnessAccountAddress(final byte[] witnessAddress,
57+
boolean isECKeyCryptoEngine) {
58+
if (witnessAddress != null) {
59+
this.witnessAccountAddress = witnessAddress;
60+
} else if (!CollectionUtils.isEmpty(privateKeys)) {
6361
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());
6462
final SignInterface ecKey = SignUtils.fromPrivate(privateKey,
6563
isECKeyCryptoEngine);

common/src/main/java/org/tron/common/utils/StringUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static ByteString hexString2ByteString(String hexString) {
4646
}
4747

4848
public static boolean isHexadecimal(String str) {
49-
if (str == null || str.length() == 0) {
49+
if (str == null || str.isEmpty()) {
5050
return false;
5151
}
5252
if (str.length() % 2 != 0) {

crypto/src/main/java/org/tron/common/crypto/ECKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.tron.common.crypto.jce.ECKeyPairGenerator;
5656
import org.tron.common.crypto.jce.TronCastleProvider;
5757
import org.tron.common.utils.BIUtil;
58+
import org.tron.common.utils.ByteArray;
5859
import org.tron.common.utils.ByteUtil;
5960

6061
@Slf4j(topic = "crypto")
@@ -285,7 +286,7 @@ public static ECKey fromPrivate(BigInteger privKey) {
285286
* @return -
286287
*/
287288
public static ECKey fromPrivate(byte[] privKeyBytes) {
288-
if (ByteUtil.isNullOrZeroArray(privKeyBytes)) {
289+
if (ByteArray.isEmpty(privKeyBytes)) {
289290
return null;
290291
}
291292
return fromPrivate(new BigInteger(1, privKeyBytes));

crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.tron.common.crypto.SignatureInterface;
4242
import org.tron.common.crypto.jce.ECKeyFactory;
4343
import org.tron.common.crypto.jce.TronCastleProvider;
44+
import org.tron.common.utils.ByteArray;
4445
import org.tron.common.utils.ByteUtil;
4546

4647
/**
@@ -247,7 +248,7 @@ public static SM2 fromPrivate(BigInteger privKey) {
247248
* @return -
248249
*/
249250
public static SM2 fromPrivate(byte[] privKeyBytes) {
250-
if (ByteUtil.isNullOrZeroArray(privKeyBytes)) {
251+
if (ByteArray.isEmpty(privKeyBytes)) {
251252
return null;
252253
}
253254
return fromPrivate(new BigInteger(1, privKeyBytes));

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,33 +405,33 @@ public static void setParam(final Config config) {
405405

406406
if (StringUtils.isNoneBlank(PARAMETER.privateKey)) {
407407
localWitnesses = (new LocalWitnesses(PARAMETER.privateKey));
408+
byte[] witnessAddress = null;
408409
if (StringUtils.isNoneBlank(PARAMETER.witnessAddress)) {
409-
byte[] bytes = Commons.decodeFromBase58Check(PARAMETER.witnessAddress);
410-
if (bytes != null) {
411-
localWitnesses.setWitnessAccountAddress(bytes);
410+
witnessAddress = Commons.decodeFromBase58Check(PARAMETER.witnessAddress);
411+
if (witnessAddress != null) {
412412
logger.debug("Got localWitnessAccountAddress from cmd");
413413
} else {
414414
PARAMETER.witnessAddress = "";
415415
logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT);
416416
}
417417
}
418-
localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine());
418+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
419419
logger.debug("Got privateKey from cmd");
420420
} else if (config.hasPath(Constant.LOCAL_WITNESS)) {
421421
localWitnesses = new LocalWitnesses();
422+
byte[] witnessAddress = getWitnessAddress(config);
422423
List<String> localwitness = config.getStringList(Constant.LOCAL_WITNESS);
423-
if (localwitness.size() > 0) {
424+
if (!localwitness.isEmpty()) {
424425
localWitnesses.setPrivateKeys(localwitness);
425-
witnessAddressCheck(config);
426-
localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine());
427426
logger.debug("Got privateKey from config.conf");
428427
}
428+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
429429
} else if (config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)) {
430430
localWitnesses = new LocalWitnesses();
431431
List<String> privateKeys = new ArrayList<String>();
432432
if (PARAMETER.isWitness()) {
433433
List<String> localwitness = config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE);
434-
if (localwitness.size() > 0) {
434+
if (!localwitness.isEmpty()) {
435435
String fileName = System.getProperty("user.dir") + "/" + localwitness.get(0);
436436
String password;
437437
if (StringUtils.isEmpty(PARAMETER.password)) {
@@ -455,8 +455,8 @@ public static void setParam(final Config config) {
455455
}
456456
}
457457
localWitnesses.setPrivateKeys(privateKeys);
458-
witnessAddressCheck(config);
459-
localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine());
458+
byte[] witnessAddress = getWitnessAddress(config);
459+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
460460
logger.debug("Got privateKey from keystore");
461461
}
462462

@@ -1775,17 +1775,18 @@ public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShie
17751775
PARAMETER.fullNodeAllowShieldedTransactionArgs = fullNodeAllowShieldedTransaction;
17761776
}
17771777

1778-
private static void witnessAddressCheck(Config config) {
1778+
private static byte[] getWitnessAddress(Config config) {
1779+
byte[] witnessAddress = null;
17791780
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
1780-
byte[] bytes = Commons
1781+
witnessAddress = Commons
17811782
.decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS));
1782-
if (bytes != null) {
1783-
localWitnesses.setWitnessAccountAddress(bytes);
1783+
if (witnessAddress != null) {
17841784
logger.debug("Got localWitnessAccountAddress from config.conf");
17851785
} else {
17861786
logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT);
17871787
}
17881788
}
1789+
return witnessAddress;
17891790
}
17901791

17911792
/**

framework/src/main/java/org/tron/core/consensus/ConsensusService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void start() {
6666
fromHexString(Args.getLocalWitnesses().getPrivateKey());
6767
byte[] privateKeyAddress = SignUtils.fromPrivate(privateKey,
6868
Args.getInstance().isECKeyCryptoEngine()).getAddress();
69-
byte[] witnessAddress = Args.getLocalWitnesses().getWitnessAccountAddress(
70-
Args.getInstance().isECKeyCryptoEngine());
69+
byte[] witnessAddress = Args.getLocalWitnesses().getWitnessAccountAddress();
7170
WitnessCapsule witnessCapsule = witnessStore.get(witnessAddress);
7271
if (null == witnessCapsule) {
7372
logger.warn("Witness {} is not in witnessStore.", Hex.toHexString(witnessAddress));

framework/src/main/java/org/tron/core/metrics/node/NodeMetricManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ private void setNodeInfo(NodeInfo nodeInfo) {
3737

3838
nodeInfo.setIp(Args.getInstance().getNodeExternalIp());
3939

40-
byte[] witnessAccountAddress = Args.getLocalWitnesses()
41-
.getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine());
40+
byte[] witnessAccountAddress = Args.getLocalWitnesses().getWitnessAccountAddress();
4241
ByteString witnessAddress = !ByteUtil.isNullOrZeroArray(witnessAccountAddress) ? ByteString
4342
.copyFrom(witnessAccountAddress) : null;
4443
if (chainBaseManager.getWitnessScheduleStore().getActiveWitnesses().contains(witnessAddress)) {

framework/src/main/java/org/tron/core/net/service/relay/RelayService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public class RelayService {
6666

6767
private List<InetSocketAddress> fastForwardNodes = parameter.getFastForwardNodes();
6868

69-
private int keySize = Args.getLocalWitnesses().getPrivateKeys().size();
69+
private final int keySize = Args.getLocalWitnesses().getPrivateKeys().size();
7070

71-
private ByteString witnessAddress = keySize > 0 ? ByteString
72-
.copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress(CommonParameter.getInstance()
73-
.isECKeyCryptoEngine())) : null;
71+
private final ByteString witnessAddress =
72+
Args.getLocalWitnesses().getWitnessAccountAddress() != null ? ByteString
73+
.copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress()) : null;
7474

7575
private int maxFastForwardNum = Args.getInstance().getMaxFastForwardNum();
7676

framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testHasWitnessSignature() {
134134

135135
localWitnesses = new LocalWitnesses();
136136
localWitnesses.setPrivateKeys(Arrays.asList(privateKey));
137-
localWitnesses.initWitnessAccountAddress(true);
137+
localWitnesses.initWitnessAccountAddress(null, true);
138138
Args.setLocalWitnesses(localWitnesses);
139139

140140
Assert.assertFalse(blockCapsule0.hasWitnessSignature());

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public void get() {
6565

6666
localWitnesses = new LocalWitnesses();
6767
localWitnesses.setPrivateKeys(Arrays.asList(privateKey));
68-
localWitnesses.initWitnessAccountAddress(true);
68+
localWitnesses.initWitnessAccountAddress(null, true);
6969
Args.setLocalWitnesses(localWitnesses);
7070
address = ByteArray.toHexString(Args.getLocalWitnesses()
71-
.getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()));
71+
.getWitnessAccountAddress());
7272
Assert.assertEquals(Constant.ADD_PRE_FIX_STRING_TESTNET, DecodeUtil.addressPreFixString);
7373
Assert.assertEquals(0, parameter.getBackupPriority());
7474

@@ -126,7 +126,7 @@ public void get() {
126126

127127
Assert.assertEquals(address,
128128
ByteArray.toHexString(Args.getLocalWitnesses()
129-
.getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())));
129+
.getWitnessAccountAddress()));
130130
}
131131

132132
@Test

0 commit comments

Comments
 (0)