Skip to content

Commit 3764148

Browse files
committed
Merge branch 'release_v4.8.1' of https://github.com/barbatos2011/java-tron into release_v4.8.1
2 parents e2e4b75 + 598f621 commit 3764148

File tree

14 files changed

+117
-80
lines changed

14 files changed

+117
-80
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tron.common.crypto.SignInterface;
2626
import org.tron.common.crypto.SignUtils;
2727
import org.tron.core.config.Parameter.ChainConstant;
28+
import org.tron.core.exception.TronError;
2829

2930
@Slf4j(topic = "app")
3031
public class LocalWitnesses {
@@ -78,13 +79,14 @@ private void validate(String privateKey) {
7879

7980
if (StringUtils.isBlank(privateKey)
8081
|| privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
81-
throw new IllegalArgumentException(
82-
String.format("private key must be %d hex string, actual: %d",
83-
ChainConstant.PRIVATE_KEY_LENGTH,
84-
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()));
82+
throw new TronError(String.format("private key must be %d hex string, actual: %d",
83+
ChainConstant.PRIVATE_KEY_LENGTH,
84+
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()),
85+
TronError.ErrCode.WITNESS_INIT);
8586
}
8687
if (!StringUtil.isHexadecimal(privateKey)) {
87-
throw new IllegalArgumentException("private key must be hex string");
88+
throw new TronError("private key must be hex string",
89+
TronError.ErrCode.WITNESS_INIT);
8890
}
8991
}
9092

chainbase/src/main/java/org/tron/core/service/MortgageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private long computeReward(long cycle, List<Pair<byte[], Long>> votes) {
182182
}
183183
long userVote = vote.getValue();
184184
double voteRate = (double) userVote / totalVote;
185-
reward += (long) (voteRate * totalReward);
185+
reward += voteRate * totalReward;
186186
}
187187
return reward;
188188
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Rsv fromSignature(byte[] sign) {
1919
byte[] s = Arrays.copyOfRange(sign, 32, 64);
2020
byte v = sign[64];
2121
if (v < 27) {
22-
v += (byte) 27; //revId -> v
22+
v += 27; //revId -> v
2323
}
2424
return new Rsv(r, s, v);
2525
}

framework/src/main/java/org/tron/core/capsule/utils/RLP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static short decodeShort(byte[] data, int index) {
165165
byte pow = (byte) (length - 1);
166166
for (int i = 1; i <= length; ++i) {
167167
// << (8 * pow) == bit shift to 0 (*1), 8 (*256) , 16 (*65..)
168-
value += (short) ((data[index + i] & 0xFF) << (8 * pow));
168+
value += (data[index + i] & 0xFF) << (8 * pow);
169169
pow--;
170170
}
171171
} else {

framework/src/main/java/org/tron/core/services/event/HistoryEventService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class HistoryEventService {
2929
@Autowired
3030
private Manager manager;
3131

32+
private volatile boolean isClosed = false;
33+
3234
private volatile Thread thread;
3335

3436
public void init() {
@@ -44,6 +46,7 @@ public void init() {
4446
}
4547

4648
public void close() {
49+
isClosed = true;
4750
if (thread != null) {
4851
try {
4952
thread.interrupt();
@@ -61,7 +64,7 @@ private void syncEvent() {
6164
long tmp = instance.getStartSyncBlockNum();
6265
long endNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
6366
while (tmp < endNum) {
64-
if (thread.isInterrupted()) {
67+
if (thread.isInterrupted() || isClosed) {
6568
throw new InterruptedException();
6669
}
6770
if (instance.isUseNativeQueue()) {

framework/src/main/java/org/tron/core/zen/address/SpendingKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public DiversifierT defaultDiversifier() throws BadItemException, ZksnarkExcepti
8989
throw new BadItemException(
9090
"librustzcash_check_diversifier does not return valid diversifier");
9191
}
92-
blob[33] += (byte) 1;
92+
blob[33] += 1;
9393
} finally {
9494
JLibsodium.freeState(state);
9595
}

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

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@
1515

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

18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertThrows;
20+
import static org.junit.Assert.assertTrue;
1821
import static org.junit.Assert.fail;
1922

2023
import com.google.common.collect.Lists;
2124
import java.io.IOException;
25+
import java.security.SecureRandom;
2226
import org.junit.Assert;
2327
import org.junit.Before;
2428
import org.junit.Rule;
2529
import org.junit.Test;
2630
import org.junit.rules.TemporaryFolder;
31+
import org.tron.common.utils.ByteArray;
2732
import org.tron.common.utils.LocalWitnesses;
2833
import org.tron.common.utils.PublicMethod;
2934
import org.tron.common.utils.StringUtil;
3035
import org.tron.core.Constant;
36+
import org.tron.core.exception.TronError;
37+
import org.tron.core.exception.TronError.ErrCode;
3138

3239
public class LocalWitnessTest {
3340

@@ -42,7 +49,7 @@ public void setLocalWitness() {
4249
localWitness
4350
.setPrivateKeys(
4451
Lists.newArrayList(
45-
PRIVATE_KEY));
52+
PRIVATE_KEY));
4653
}
4754

4855
@Test
@@ -52,13 +59,13 @@ public void whenSetNullPrivateKey() {
5259
Assert.assertNotNull(localWitness.getPublicKey());
5360
}
5461

55-
@Test(expected = IllegalArgumentException.class)
62+
@Test(expected = TronError.class)
5663
public void whenSetEmptyPrivateKey() {
5764
localWitness.setPrivateKeys(Lists.newArrayList(""));
5865
fail("private key must be 64-bits hex string");
5966
}
6067

61-
@Test(expected = IllegalArgumentException.class)
68+
@Test(expected = TronError.class)
6269
public void whenSetBadFormatPrivateKey() {
6370
localWitness.setPrivateKeys(Lists.newArrayList("a111"));
6471
fail("private key must be 64-bits hex string");
@@ -104,62 +111,27 @@ public void testValidPrivateKeyWithPrefix() {
104111
@Test
105112
public void testInvalidPrivateKey() {
106113
LocalWitnesses localWitnesses = new LocalWitnesses();
114+
String expectedMessage = "private key must be 64 hex string";
115+
assertTronError(localWitnesses, null, expectedMessage);
116+
assertTronError(localWitnesses, "", expectedMessage);
117+
assertTronError(localWitnesses, " ", expectedMessage);
118+
assertTronError(localWitnesses, "11111", expectedMessage);
119+
String expectedMessage2 = "private key must be hex string";
120+
SecureRandom secureRandom = new SecureRandom();
121+
byte[] keyBytes = new byte[31];
122+
secureRandom.nextBytes(keyBytes);
123+
final String privateKey = ByteArray.toHexString(keyBytes) + " ";
124+
assertTronError(localWitnesses, privateKey, expectedMessage2);
125+
final String privateKey2 = "xy" + ByteArray.toHexString(keyBytes);
126+
assertTronError(localWitnesses, privateKey2, expectedMessage2);
127+
}
107128

108-
try {
109-
localWitnesses.addPrivateKeys(null);
110-
fail("should throw IllegalArgumentException");
111-
} catch (IllegalArgumentException e) {
112-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
113-
} catch (Exception e) {
114-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
115-
}
116-
117-
try {
118-
localWitnesses.addPrivateKeys("");
119-
fail("should throw IllegalArgumentException");
120-
} catch (IllegalArgumentException e) {
121-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
122-
} catch (Exception e) {
123-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
124-
}
125-
126-
try {
127-
localWitnesses.addPrivateKeys(" ");
128-
fail("should throw IllegalArgumentException");
129-
} catch (IllegalArgumentException e) {
130-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
131-
} catch (Exception e) {
132-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
133-
}
134-
135-
try {
136-
localWitnesses.addPrivateKeys("11111");
137-
fail("should throw IllegalArgumentException");
138-
} catch (IllegalArgumentException e) {
139-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
140-
} catch (Exception e) {
141-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
142-
}
143-
144-
try {
145-
String privateKey = "11111111111111111111111111111111111111111111111111111111111111 ";
146-
localWitnesses.addPrivateKeys(privateKey);
147-
fail("should throw IllegalArgumentException");
148-
} catch (IllegalArgumentException e) {
149-
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
150-
} catch (Exception e) {
151-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
152-
}
153-
154-
try {
155-
String privateKey = "xy11111111111111111111111111111111111111111111111111111111111111";
156-
localWitnesses.addPrivateKeys(privateKey);
157-
fail("should throw IllegalArgumentException");
158-
} catch (IllegalArgumentException e) {
159-
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
160-
} catch (Exception e) {
161-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
162-
}
129+
private void assertTronError(LocalWitnesses localWitnesses, String privateKey,
130+
String expectedMessage) {
131+
TronError thrown = assertThrows(TronError.class,
132+
() -> localWitnesses.addPrivateKeys(privateKey));
133+
assertEquals(ErrCode.WITNESS_INIT, thrown.getErrCode());
134+
assertTrue(thrown.getMessage().contains(expectedMessage));
163135
}
164136

165137
@Test

framework/src/test/java/org/tron/core/exception/TronErrorTest.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertThrows;
5+
import static org.junit.Assert.fail;
56
import static org.mockito.ArgumentMatchers.any;
67
import static org.mockito.Mockito.mockStatic;
8+
import static org.mockito.Mockito.never;
9+
import static org.mockito.Mockito.times;
710

811
import com.typesafe.config.Config;
912
import com.typesafe.config.ConfigFactory;
@@ -127,25 +130,53 @@ public void shutdownBlockTimeInitTest() {
127130

128131
@Test
129132
public void testThrowIfUnsupportedJavaVersion() {
130-
runArchTest(true, false, true);
131-
runArchTest(true, true, false);
132-
runArchTest(false, false, false);
133+
runArchTest("x86_64", "1.8", false);
134+
runArchTest("x86_64", "11", true);
135+
runArchTest("x86_64", "17", true);
136+
runArchTest("aarch64", "17", false);
137+
runArchTest("aarch64", "1.8", true);
138+
runArchTest("aarch64", "11", true);
133139
}
134140

135-
private void runArchTest(boolean isX86, boolean isJava8, boolean expectThrow) {
141+
private void runArchTest(String osArch, String javaVersion, boolean expectThrow) {
136142
try (MockedStatic<Arch> mocked = mockStatic(Arch.class)) {
143+
boolean isX86 = "x86_64".equals(osArch);
144+
boolean isArm64 = "aarch64".equals(osArch);
145+
146+
boolean isJava8 = "1.8".equals(javaVersion);
147+
boolean isJava17 = "17".equals(javaVersion);
148+
137149
mocked.when(Arch::isX86).thenReturn(isX86);
150+
mocked.when(Arch::isArm64).thenReturn(isArm64);
151+
138152
mocked.when(Arch::isJava8).thenReturn(isJava8);
139-
mocked.when(Arch::getOsArch).thenReturn("x86_64");
140-
mocked.when(Arch::javaSpecificationVersion).thenReturn("17");
141-
mocked.when(Arch::withAll).thenReturn("");
153+
mocked.when(Arch::isJava17).thenReturn(isJava17);
154+
155+
mocked.when(Arch::getOsArch).thenReturn(osArch);
156+
mocked.when(Arch::javaSpecificationVersion).thenReturn(javaVersion);
157+
mocked.when(Arch::withAll).thenReturn(String.format(
158+
"Architecture: %s, Java Version: %s", osArch, javaVersion));
159+
142160
mocked.when(Arch::throwIfUnsupportedJavaVersion).thenCallRealMethod();
143161

144162
if (expectThrow) {
145-
assertEquals(TronError.ErrCode.JDK_VERSION, assertThrows(
146-
TronError.class, () -> Args.setParam(new String[]{}, Constant.TEST_CONF)).getErrCode());
163+
TronError err = assertThrows(
164+
TronError.class, () -> Args.setParam(new String[]{}, Constant.TEST_CONF));
165+
166+
String expectedJavaVersion = isX86 ? "1.8" : "17";
167+
String expectedMessage = String.format(
168+
"Java %s is required for %s architecture. Detected version %s",
169+
expectedJavaVersion, osArch, javaVersion);
170+
assertEquals(expectedMessage, err.getCause().getMessage());
171+
assertEquals(TronError.ErrCode.JDK_VERSION, err.getErrCode());
172+
mocked.verify(Arch::withAll, times(1));
147173
} else {
148-
Arch.throwIfUnsupportedJavaVersion();
174+
try {
175+
Arch.throwIfUnsupportedJavaVersion();
176+
} catch (Exception e) {
177+
fail("Expected no exception, but got: " + e.getMessage());
178+
}
179+
mocked.verify(Arch::withAll, never());
149180
}
150181
}
151182
}

framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import io.grpc.ManagedChannelBuilder;
1212
import java.io.IOException;
1313
import java.util.Objects;
14+
import java.util.concurrent.TimeUnit;
1415
import org.junit.AfterClass;
1516
import org.junit.Assert;
1617
import org.junit.BeforeClass;
1718
import org.junit.ClassRule;
1819
import org.junit.FixMethodOrder;
20+
import org.junit.Rule;
1921
import org.junit.Test;
2022
import org.junit.rules.TemporaryFolder;
23+
import org.junit.rules.Timeout;
2124
import org.junit.runners.MethodSorters;
2225
import org.tron.api.DatabaseGrpc;
2326
import org.tron.api.DatabaseGrpc.DatabaseBlockingStub;
@@ -125,6 +128,8 @@ public class RpcApiServicesTest {
125128
private static WalletSolidityBlockingStub blockingStubPBFT = null;
126129
@ClassRule
127130
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
131+
@Rule
132+
public Timeout timeout = new Timeout(30, TimeUnit.SECONDS);
128133
private static ByteString ownerAddress;
129134
private static ByteString sk;
130135
private static ByteString ask;

framework/src/test/java/org/tron/core/services/WalletApiTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.junit.Assert;
99
import org.junit.BeforeClass;
1010
import org.junit.ClassRule;
11+
import org.junit.Rule;
1112
import org.junit.Test;
1213
import org.junit.rules.TemporaryFolder;
14+
import org.junit.rules.Timeout;
1315
import org.tron.api.GrpcAPI.EmptyMessage;
1416
import org.tron.api.WalletGrpc;
1517
import org.tron.common.application.Application;
@@ -28,6 +30,9 @@ public class WalletApiTest {
2830
@ClassRule
2931
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
3032

33+
@Rule
34+
public Timeout timeout = new Timeout(30, TimeUnit.SECONDS);
35+
3136
private static TronApplicationContext context;
3237
private static Application appT;
3338

0 commit comments

Comments
 (0)