Skip to content

Commit 1a285fd

Browse files
committed
fix(crypto): optimize the null localwitness
1 parent 58b0571 commit 1a285fd

File tree

7 files changed

+23
-19
lines changed

7 files changed

+23
-19
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public LocalWitnesses(List<String> privateKeys) {
4646
}
4747

4848
public byte[] getWitnessAccountAddress(boolean isECKeyCryptoEngine) {
49-
if (witnessAccountAddress == null) {
49+
if (witnessAccountAddress == null && !this.privateKeys.isEmpty()) {
5050
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());
5151
final SignInterface cryptoEngine = SignUtils.fromPrivate(privateKey, isECKeyCryptoEngine);
5252
this.witnessAccountAddress = cryptoEngine.getAddress();
@@ -59,7 +59,7 @@ public void setWitnessAccountAddress(final byte[] localWitnessAccountAddress) {
5959
}
6060

6161
public void initWitnessAccountAddress(boolean isECKeyCryptoEngine) {
62-
if (witnessAccountAddress == null) {
62+
if (witnessAccountAddress == null && !this.privateKeys.isEmpty()) {
6363
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());
6464
final SignInterface ecKey = SignUtils.fromPrivate(privateKey,
6565
isECKeyCryptoEngine);
@@ -85,8 +85,8 @@ private void validate(String privateKey) {
8585
privateKey = privateKey.substring(2);
8686
}
8787

88-
if (StringUtils.isNotBlank(privateKey)
89-
&& privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
88+
if (StringUtils.isBlank(privateKey) || (StringUtils.isNotBlank(privateKey)
89+
&& privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH)) {
9090
throw new IllegalArgumentException(
9191
String.format("private key must be %d-bits hex string, actual: %d",
9292
ChainConstant.PRIVATE_KEY_LENGTH, privateKey.length()));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public static ECKey fromPrivate(BigInteger privKey) {
285285
* @return -
286286
*/
287287
public static ECKey fromPrivate(byte[] privKeyBytes) {
288-
if (Objects.isNull(privKeyBytes)) {
288+
if (Objects.isNull(privKeyBytes) || privKeyBytes.length == 0) {
289289
return null;
290290
}
291291
return fromPrivate(new BigInteger(1, privKeyBytes));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static SM2 fromPrivate(BigInteger privKey) {
247247
* @return -
248248
*/
249249
public static SM2 fromPrivate(byte[] privKeyBytes) {
250-
if (Objects.isNull(privKeyBytes)) {
250+
if (Objects.isNull(privKeyBytes) || privKeyBytes.length == 0) {
251251
return null;
252252
}
253253
return fromPrivate(new BigInteger(1, privKeyBytes));

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,12 @@ public static void setParam(final Config config) {
420420
} else if (config.hasPath(Constant.LOCAL_WITNESS)) {
421421
localWitnesses = new LocalWitnesses();
422422
List<String> localwitness = config.getStringList(Constant.LOCAL_WITNESS);
423-
localWitnesses.setPrivateKeys(localwitness);
424-
witnessAddressCheck(config);
425-
localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine());
426-
logger.debug("Got privateKey from config.conf");
423+
if (localwitness.size() > 0) {
424+
localWitnesses.setPrivateKeys(localwitness);
425+
witnessAddressCheck(config);
426+
localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine());
427+
logger.debug("Got privateKey from config.conf");
428+
}
427429
} else if (config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)) {
428430
localWitnesses = new LocalWitnesses();
429431
List<String> privateKeys = new ArrayList<String>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void start() {
6161
logger.info("Add witness: {}, size: {}",
6262
Hex.toHexString(privateKeyAddress), miners.size());
6363
}
64-
} else {
64+
} else if (privateKeys.size() == 1) {
6565
byte[] privateKey =
6666
fromHexString(Args.getLocalWitnesses().getPrivateKey());
6767
byte[] privateKeyAddress = SignUtils.fromPrivate(privateKey,

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,12 +66,12 @@ public class RelayService {
6666

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

69-
private ByteString witnessAddress = ByteString
70-
.copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress(CommonParameter.getInstance()
71-
.isECKeyCryptoEngine()));
72-
7369
private int keySize = Args.getLocalWitnesses().getPrivateKeys().size();
7470

71+
private ByteString witnessAddress = keySize > 0 ? ByteString
72+
.copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress(CommonParameter.getInstance()
73+
.isECKeyCryptoEngine())) : null;
74+
7575
private int maxFastForwardNum = Args.getInstance().getMaxFastForwardNum();
7676

7777
public void init() {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.tron.core.config.args;
1717

18+
import static org.junit.Assert.fail;
19+
1820
import com.google.common.collect.Lists;
1921
import org.junit.Assert;
2022
import org.junit.Before;
@@ -42,16 +44,16 @@ public void whenSetNullPrivateKey() {
4244
Assert.assertNotNull(localWitness.getPublicKey());
4345
}
4446

45-
@Test
47+
@Test(expected = IllegalArgumentException.class)
4648
public void whenSetEmptyPrivateKey() {
4749
localWitness.setPrivateKeys(Lists.newArrayList(""));
48-
Assert.assertNotNull(localWitness.getPrivateKey());
49-
Assert.assertNotNull(localWitness.getPublicKey());
50+
fail("private key must be 64-bits hex string");
5051
}
5152

5253
@Test(expected = IllegalArgumentException.class)
5354
public void whenSetBadFormatPrivateKey() {
5455
localWitness.setPrivateKeys(Lists.newArrayList("a111"));
56+
fail("private key must be 64-bits hex string");
5557
}
5658

5759
@Test
@@ -85,6 +87,6 @@ public void testConstructor() {
8587
Assert.assertNull(localWitnesses2.getPublicKey());
8688
localWitnesses2.initWitnessAccountAddress(true);
8789
LocalWitnesses localWitnesses3 = new LocalWitnesses();
88-
Assert.assertNotNull(localWitnesses3.getWitnessAccountAddress(true));
90+
Assert.assertNull(localWitnesses3.getWitnessAccountAddress(true));
8991
}
9092
}

0 commit comments

Comments
 (0)