Skip to content

Commit 7bc1f4b

Browse files
committed
process invalid witness address
1 parent 28aaa52 commit 7bc1f4b

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed

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

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -412,67 +412,69 @@ public static void setParam(final Config config) {
412412
PARAMETER.cryptoEngine = config.hasPath(Constant.CRYPTO_ENGINE) ? config
413413
.getString(Constant.CRYPTO_ENGINE) : Constant.ECKey_ENGINE;
414414

415-
if (StringUtils.isNoneBlank(PARAMETER.privateKey)) {
416-
localWitnesses = (new LocalWitnesses(PARAMETER.privateKey));
415+
localWitnesses = new LocalWitnesses();
416+
if (PARAMETER.isWitness()) {
417417
byte[] witnessAddress = null;
418-
if (StringUtils.isNoneBlank(PARAMETER.witnessAddress)) {
419-
witnessAddress = Commons.decodeFromBase58Check(PARAMETER.witnessAddress);
420-
if (witnessAddress != null) {
421-
logger.debug("Got localWitnessAccountAddress from cmd");
422-
} else {
423-
PARAMETER.witnessAddress = "";
424-
logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT);
425-
}
426-
}
427-
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
428-
logger.debug("Got privateKey from cmd");
429-
} else if (config.hasPath(Constant.LOCAL_WITNESS)) {
430-
localWitnesses = new LocalWitnesses();
431-
byte[] witnessAddress = getWitnessAddress(config);
432-
List<String> localwitness = config.getStringList(Constant.LOCAL_WITNESS);
433-
if (!localwitness.isEmpty()) {
434-
localWitnesses.setPrivateKeys(localwitness);
435-
logger.debug("Got privateKey from config.conf");
436-
}
437-
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
438-
} else if (config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)) {
439-
localWitnesses = new LocalWitnesses();
440-
List<String> privateKeys = new ArrayList<String>();
441-
if (PARAMETER.isWitness()) {
442-
List<String> localwitness = config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE);
443-
if (!localwitness.isEmpty()) {
444-
String fileName = System.getProperty("user.dir") + "/" + localwitness.get(0);
445-
String password;
446-
if (StringUtils.isEmpty(PARAMETER.password)) {
447-
System.out.println("Please input your password.");
448-
password = WalletUtils.inputPassword();
418+
if (StringUtils.isNoneBlank(PARAMETER.privateKey)) {
419+
localWitnesses = new LocalWitnesses(PARAMETER.privateKey);
420+
if (StringUtils.isNoneBlank(PARAMETER.witnessAddress)) {
421+
witnessAddress = Commons.decodeFromBase58Check(PARAMETER.witnessAddress);
422+
if (witnessAddress != null) {
423+
logger.debug("Got localWitnessAccountAddress from cmd");
449424
} else {
450-
password = PARAMETER.password;
451-
PARAMETER.password = null;
425+
throw new TronError("LocalWitnessAccountAddress format from cmd is incorrect",
426+
TronError.ErrCode.WITNESS_INIT);
452427
}
453-
454-
try {
455-
Credentials credentials = WalletUtils
456-
.loadCredentials(password, new File(fileName));
457-
SignInterface sign = credentials.getSignInterface();
458-
String prikey = ByteArray.toHexString(sign.getPrivateKey());
459-
privateKeys.add(prikey);
460-
} catch (IOException | CipherException e) {
461-
logger.error("Witness node start failed!");
462-
throw new TronError(e, TronError.ErrCode.WITNESS_KEYSTORE_LOAD);
428+
}
429+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
430+
logger.debug("Got privateKey from cmd");
431+
} else if (config.hasPath(Constant.LOCAL_WITNESS) && !config.getStringList(
432+
Constant.LOCAL_WITNESS).isEmpty()) {
433+
List<String> localWitness = config.getStringList(Constant.LOCAL_WITNESS);
434+
localWitnesses.setPrivateKeys(localWitness);
435+
logger.debug("Got privateKey from config.conf");
436+
witnessAddress = getWitnessAddress(config);
437+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
438+
} else if (config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)) {
439+
List<String> privateKeys = new ArrayList<String>();
440+
List<String> localWitness = config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE);
441+
if (!localWitness.isEmpty()) {
442+
int i = 1;
443+
System.out.printf("There are %d keystore in the config.%n", localWitness.size());
444+
for (String keystore : localWitness) {
445+
String fileName = System.getProperty("user.dir") + "/" + keystore;
446+
String password;
447+
if (StringUtils.isEmpty(PARAMETER.password)) {
448+
System.out.printf("Please input your %d-th keystore password: %n", i);
449+
password = WalletUtils.inputPassword();
450+
} else {
451+
password = PARAMETER.password;
452+
PARAMETER.password = null;
453+
}
454+
455+
try {
456+
Credentials credentials = WalletUtils
457+
.loadCredentials(password, new File(fileName));
458+
SignInterface sign = credentials.getSignInterface();
459+
String prikey = ByteArray.toHexString(sign.getPrivateKey());
460+
privateKeys.add(prikey);
461+
} catch (IOException | CipherException e) {
462+
logger.error("Witness node start failed!");
463+
throw new TronError(e, TronError.ErrCode.WITNESS_KEYSTORE_LOAD);
464+
}
465+
i++;
463466
}
467+
localWitnesses.setPrivateKeys(privateKeys);
468+
witnessAddress = getWitnessAddress(config);
469+
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
470+
logger.debug("Got privateKey from keystore");
464471
}
465472
}
466-
localWitnesses.setPrivateKeys(privateKeys);
467-
byte[] witnessAddress = getWitnessAddress(config);
468-
localWitnesses.initWitnessAccountAddress(witnessAddress, PARAMETER.isECKeyCryptoEngine());
469-
logger.debug("Got privateKey from keystore");
470-
}
471473

472-
if (PARAMETER.isWitness()
473-
&& CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) {
474-
throw new TronError("This is a witness node, but localWitnesses is null",
475-
TronError.ErrCode.WITNESS_INIT);
474+
if (CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) {
475+
throw new TronError("This is a witness node, but localWitnesses is null",
476+
TronError.ErrCode.WITNESS_INIT);
477+
}
476478
}
477479

478480
if (config.hasPath(Constant.VM_SUPPORT_CONSTANT)) {
@@ -1822,12 +1824,18 @@ public static void logConfig() {
18221824
private static byte[] getWitnessAddress(Config config) {
18231825
byte[] witnessAddress = null;
18241826
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
1827+
if (localWitnesses.getPrivateKeys().size() != 1) {
1828+
throw new TronError(
1829+
"LocalWitnessAccountAddress can only be set when there is only one private key",
1830+
TronError.ErrCode.WITNESS_INIT);
1831+
}
18251832
witnessAddress = Commons
18261833
.decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS));
18271834
if (witnessAddress != null) {
18281835
logger.debug("Got localWitnessAccountAddress from config.conf");
18291836
} else {
1830-
logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT);
1837+
throw new TronError("LocalWitnessAccountAddress format from config is incorrect",
1838+
TronError.ErrCode.WITNESS_INIT);
18311839
}
18321840
}
18331841
return witnessAddress;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.stereotype.Component;
66
import org.tron.common.backup.BackupManager;
7-
import org.tron.common.parameter.CommonParameter;
8-
import org.tron.common.utils.ByteUtil;
7+
import org.tron.common.utils.ByteArray;
98
import org.tron.core.ChainBaseManager;
109
import org.tron.core.config.args.Args;
1110
import org.tron.program.Version;
@@ -38,7 +37,7 @@ private void setNodeInfo(NodeInfo nodeInfo) {
3837
nodeInfo.setIp(Args.getInstance().getNodeExternalIp());
3938

4039
byte[] witnessAccountAddress = Args.getLocalWitnesses().getWitnessAccountAddress();
41-
ByteString witnessAddress = !ByteUtil.isNullOrZeroArray(witnessAccountAddress) ? ByteString
40+
ByteString witnessAddress = !ByteArray.isEmpty(witnessAccountAddress) ? ByteString
4241
.copyFrom(witnessAccountAddress) : null;
4342
if (chainBaseManager.getWitnessScheduleStore().getActiveWitnesses().contains(witnessAddress)) {
4443
nodeInfo.setNodeType(1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void testConstructor() {
198198
@Test
199199
public void testLocalWitnessConfig() throws IOException {
200200
Args.setParam(
201-
new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "--debug"},
201+
new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "-w", "--debug"},
202202
"config-localtest.conf");
203203
LocalWitnesses witness = Args.getLocalWitnesses();
204204
Assert.assertNotNull(witness.getPrivateKey());

framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public class ShieldedReceiveTest extends BaseTest {
128128
private static boolean init;
129129

130130
static {
131-
Args.setParam(new String[]{"--output-directory", dbPath()}, "config-localtest.conf");
131+
Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, "config-localtest.conf");
132132
ADDRESS_ONE_PRIVATE_KEY = getRandomPrivateKey();
133133
FROM_ADDRESS = getHexAddressByPrivateKey(ADDRESS_ONE_PRIVATE_KEY);
134134
}

0 commit comments

Comments
 (0)