Skip to content

Commit b605617

Browse files
committed
Merge remote-tracking branch 'origin/release_v4.7.2' into release_v4.7.2
2 parents f9ba0e0 + 54c05e5 commit b605617

10 files changed

Lines changed: 99 additions & 43 deletions

File tree

common/src/main/java/org/tron/core/exception/BadBlockException.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22

33
public class BadBlockException extends TronException {
44

5+
private TypeEnum type = TypeEnum.DEFAULT;
6+
57
public BadBlockException() {
68
super();
79
}
810

911
public BadBlockException(String message) {
1012
super(message);
1113
}
14+
15+
public BadBlockException(TypeEnum type, String message) {
16+
super(message);
17+
this.type = type;
18+
}
19+
20+
public TypeEnum getType() {
21+
return type;
22+
}
23+
24+
public enum TypeEnum {
25+
CALC_MERKLE_ROOT_FAILED(1),
26+
DEFAULT(100);
27+
28+
private Integer value;
29+
30+
TypeEnum(Integer value) {
31+
this.value = value;
32+
}
33+
34+
public Integer getValue() {
35+
return value;
36+
}
37+
}
1238
}

common/src/main/java/org/tron/core/exception/P2pException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public enum TypeEnum {
5050
TRX_EXE_FAILED(12, "trx exe failed"),
5151
DB_ITEM_NOT_FOUND(13, "DB item not found"),
5252
PROTOBUF_ERROR(14, "protobuf inconsistent"),
53+
BLOCK_SIGN_ERROR(15, "block sign error"),
54+
BLOCK_MERKLE_ERROR(16, "block merkle error"),
5355

5456
DEFAULT(100, "default exception");
5557

framework/propPath

Whitespace-only changes.

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.db;
22

33
import static org.tron.common.utils.Commons.adjustBalance;
4+
import static org.tron.core.exception.BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED;
45
import static org.tron.protos.Protocol.Transaction.Contract.ContractType.TransferContract;
56
import static org.tron.protos.Protocol.Transaction.Result.contractResult.SUCCESS;
67

@@ -1213,8 +1214,8 @@ public void pushBlock(final BlockCapsule block)
12131214
if (!block.calcMerkleRoot().equals(block.getMerkleRoot())) {
12141215
logger.warn("Num: {}, the merkle root doesn't match, expect is {} , actual is {}.",
12151216
block.getNum(), block.getMerkleRoot(), block.calcMerkleRoot());
1216-
throw new BadBlockException(String.format("The merkle hash is not validated for %d",
1217-
block.getNum()));
1217+
throw new BadBlockException(CALC_MERKLE_ROOT_FAILED,
1218+
String.format("The merkle hash is not validated for %d", block.getNum()));
12181219
}
12191220
consensus.receiveBlock(block);
12201221
}

framework/src/main/java/org/tron/core/net/TronNetDelegate.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.net;
22

33
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
4+
import static org.tron.core.exception.BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED;
45

56
import com.google.common.cache.Cache;
67
import com.google.common.cache.CacheBuilder;
@@ -282,7 +283,12 @@ public void processBlock(BlockCapsule block, boolean isSync) throws P2pException
282283
| EventBloomException e) {
283284
metricsService.failProcessBlock(block.getNum(), e.getMessage());
284285
logger.error("Process block failed, {}, reason: {}", blockId.getString(), e.getMessage());
285-
throw new P2pException(TypeEnum.BAD_BLOCK, e);
286+
if (e instanceof BadBlockException
287+
&& ((BadBlockException) e).getType().equals(CALC_MERKLE_ROOT_FAILED)) {
288+
throw new P2pException(TypeEnum.BLOCK_MERKLE_ERROR, e);
289+
} else {
290+
throw new P2pException(TypeEnum.BAD_BLOCK, e);
291+
}
286292
}
287293
}
288294
}
@@ -312,10 +318,10 @@ public void validSignature(BlockCapsule block) throws P2pException {
312318
try {
313319
if (!block.validateSignature(dbManager.getDynamicPropertiesStore(),
314320
dbManager.getAccountStore())) {
315-
throw new P2pException(TypeEnum.BAD_BLOCK, "valid signature failed.");
321+
throw new P2pException(TypeEnum.BLOCK_SIGN_ERROR, "valid signature failed.");
316322
}
317323
} catch (ValidateSignatureException e) {
318-
throw new P2pException(TypeEnum.BAD_BLOCK, e);
324+
throw new P2pException(TypeEnum.BLOCK_SIGN_ERROR, e);
319325
}
320326
}
321327

framework/src/main/java/org/tron/core/net/service/sync/SyncService.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.tron.core.capsule.BlockCapsule.BlockId;
2424
import org.tron.core.config.Parameter.NetConstants;
2525
import org.tron.core.config.args.Args;
26+
import org.tron.core.exception.BadBlockException;
2627
import org.tron.core.exception.P2pException;
2728
import org.tron.core.exception.P2pException.TypeEnum;
2829
import org.tron.core.net.TronNetDelegate;
@@ -263,33 +264,46 @@ private synchronized void handleSyncBlock() {
263264
tronNetDelegate.getActivePeer().stream()
264265
.filter(peer -> msg.getBlockId().equals(peer.getSyncBlockToFetch().peek()))
265266
.forEach(peer -> {
266-
peer.getSyncBlockToFetch().pop();
267-
peer.getSyncBlockInProcess().add(msg.getBlockId());
268267
isFound[0] = true;
269268
});
270269
if (isFound[0]) {
271270
blockWaitToProcess.remove(msg);
272271
isProcessed[0] = true;
273-
processSyncBlock(msg.getBlockCapsule());
272+
processSyncBlock(msg.getBlockCapsule(), peerConnection);
274273
}
275274
}
276275
});
277276
}
278277
}
279278

280-
private void processSyncBlock(BlockCapsule block) {
279+
private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection) {
281280
boolean flag = true;
281+
boolean attackFlag = false;
282282
BlockId blockId = block.getBlockId();
283283
try {
284284
tronNetDelegate.validSignature(block);
285285
tronNetDelegate.processBlock(block, true);
286286
pbftDataSyncHandler.processPBFTCommitData(block);
287+
} catch (P2pException p2pException) {
288+
logger.error("Process sync block {} failed, type: {}",
289+
blockId.getString(), p2pException.getType());
290+
attackFlag = p2pException.getType().equals(TypeEnum.BLOCK_SIGN_ERROR)
291+
|| p2pException.getType().equals(TypeEnum.BLOCK_MERKLE_ERROR);
292+
flag = false;
287293
} catch (Exception e) {
288294
logger.error("Process sync block {} failed", blockId.getString(), e);
289295
flag = false;
290296
}
297+
298+
if (attackFlag) {
299+
invalid(blockId, peerConnection);
300+
peerConnection.disconnect(ReasonCode.BAD_BLOCK);
301+
return;
302+
}
303+
291304
for (PeerConnection peer : tronNetDelegate.getActivePeer()) {
292-
if (peer.getSyncBlockInProcess().remove(blockId)) {
305+
if (blockId.equals(peer.getSyncBlockToFetch().peek())) {
306+
peer.getSyncBlockToFetch().pop();
293307
if (flag) {
294308
peer.setBlockBothHave(blockId);
295309
if (peer.getSyncBlockToFetch().isEmpty() && peer.isFetchAble()) {

framework/src/test/java/org/tron/core/db/ManagerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.tron.common.utils.Commons.adjustBalance;
55
import static org.tron.common.utils.Commons.adjustTotalShieldedPoolValue;
66
import static org.tron.common.utils.Commons.getExchangeStoreFinal;
7+
import static org.tron.core.exception.BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED;
78

89
import com.google.common.collect.Maps;
910
import com.google.protobuf.ByteString;
@@ -392,6 +393,7 @@ public void pushBlockInvalidMerkelRoot() {
392393
Assert.assertTrue(false);
393394
} catch (BadBlockException e) {
394395
Assert.assertTrue(e instanceof BadBlockException);
396+
Assert.assertTrue(e.getType().equals(CALC_MERKLE_ROOT_FAILED));
395397
Assert.assertEquals("The merkle hash is not validated for "
396398
+ blockCapsule2.getNum(), e.getMessage());
397399
} catch (Exception e) {

framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
package org.tron.core.services.http;
22

33
import com.google.gson.JsonObject;
4-
import java.io.File;
4+
import javax.annotation.Resource;
55
import lombok.extern.slf4j.Slf4j;
66
import org.apache.http.HttpResponse;
77
import org.bouncycastle.util.encoders.Hex;
8-
import org.junit.AfterClass;
8+
import org.junit.Before;
99
import org.junit.BeforeClass;
1010
import org.junit.Test;
1111
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
12+
import org.tron.common.BaseTest;
1213
import org.tron.common.application.Application;
13-
import org.tron.common.application.ApplicationFactory;
14-
import org.tron.common.application.TronApplicationContext;
1514
import org.tron.common.utils.ByteArray;
16-
import org.tron.common.utils.FileUtil;
1715
import org.tron.core.Constant;
1816
import org.tron.core.capsule.ContractCapsule;
19-
import org.tron.core.config.DefaultConfig;
2017
import org.tron.core.config.args.Args;
2118
import org.tron.core.store.StoreFactory;
2219
import org.tron.core.vm.repository.Repository;
2320
import org.tron.core.vm.repository.RepositoryImpl;
2421
import org.tron.protos.Protocol;
2522
import org.tron.protos.contract.SmartContractOuterClass;
23+
import stest.tron.wallet.common.client.Configuration;
2624
import stest.tron.wallet.common.client.utils.HttpMethed;
2725

2826
@Slf4j
29-
public class TriggerSmartContractServletTest {
27+
public class TriggerSmartContractServletTest extends BaseTest {
28+
private static final String httpNode = Configuration.getByPath("testng.conf")
29+
.getStringList("httpnode.ip.list")
30+
.get(0);
31+
private static final byte[] ownerAddr = Hex.decode("410000000000000000000000000000000000000000");
32+
private static final byte[] contractAddr = Hex.decode(
33+
"41000000000000000000000000000000000000dEaD");
3034

31-
private static String dbPath;
32-
private static byte[] ownerAddr = Hex.decode("410000000000000000000000000000000000000000");
33-
private static byte[] contractAddr = Hex.decode("41000000000000000000000000000000000000dEaD");
34-
private static TronApplicationContext context;
35+
@Resource
36+
private FullNodeHttpApiService httpApiService;
37+
@Resource
38+
private Application appT;
3539

3640
@BeforeClass
3741
public static void init() throws Exception {
@@ -43,13 +47,10 @@ public static void init() throws Exception {
4347
// build app context
4448
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
4549
beanFactory.setAllowCircularReferences(false);
46-
context = new TronApplicationContext(beanFactory);
47-
context.register(DefaultConfig.class);
48-
context.refresh();
49-
Application appT = ApplicationFactory.create(context);
50+
}
5051

51-
// register http service
52-
FullNodeHttpApiService httpApiService = context.getBean(FullNodeHttpApiService.class);
52+
@Before
53+
public void before() {
5354
appT.addService(httpApiService);
5455

5556
// start services
@@ -71,17 +72,10 @@ public static void init() throws Exception {
7172
rootRepository.commit();
7273
}
7374

74-
@AfterClass
75-
public static void destroy() {
76-
Args.clearParam();
77-
context.destroy();
78-
FileUtil.deleteDir(new File(dbPath));
79-
}
80-
8175

8276
@Test
83-
public void testNormalCall() throws Exception {
84-
HttpMethed.waitToProduceOneBlock("127.0.0.1:8090");
77+
public void testNormalCall() {
78+
HttpMethed.waitToProduceOneBlock(httpNode);
8579
JsonObject parameter = new JsonObject();
8680
parameter.addProperty("owner_address", ByteArray.toHexString(ownerAddr));
8781
parameter.addProperty("contract_address", ByteArray.toHexString(contractAddr));
@@ -94,7 +88,7 @@ public void testNormalCall() throws Exception {
9488
public static HttpResponse invokeToLocal(
9589
String method, JsonObject parameter) {
9690
try {
97-
final String requestUrl = "http://127.0.0.1:8090/wallet/" + method;
91+
final String requestUrl = "http://" + httpNode + "/wallet/" + method;
9892
return HttpMethed.createConnect(requestUrl, parameter);
9993
} catch (Exception e) {
10094
e.printStackTrace();

framework/src/test/java/stest/tron/wallet/common/client/utils/HttpMethed.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.HashMap;
1414
import java.util.List;
1515
import java.util.Map;
16+
import java.util.Objects;
1617
import java.util.Optional;
1718
import lombok.extern.slf4j.Slf4j;
1819
import org.apache.http.HttpResponse;
@@ -2223,8 +2224,13 @@ public static HttpResponse getNowBlockFromPbft(String httpSolidityNode) {
22232224
public static void waitToProduceOneBlock(String httpNode) {
22242225
response = HttpMethed.getNowBlock(httpNode);
22252226
responseContent = HttpMethed.parseResponseContent(response);
2226-
responseContent = HttpMethed.parseStringContent(responseContent.get("block_header").toString());
2227-
responseContent = HttpMethed.parseStringContent(responseContent.get("raw_data").toString());
2227+
if (responseContent.containsKey("block_header")) {
2228+
responseContent = HttpMethed.parseStringContent(
2229+
responseContent.get("block_header").toString());
2230+
}
2231+
if (responseContent.containsKey("raw_data")) {
2232+
responseContent = HttpMethed.parseStringContent(responseContent.get("raw_data").toString());
2233+
}
22282234
Integer currentBlockNum = 0;
22292235
if (responseContent.containsKey("number")) {
22302236
currentBlockNum = Integer.parseInt(responseContent.get("number").toString());
@@ -2234,9 +2240,14 @@ public static void waitToProduceOneBlock(String httpNode) {
22342240
while (nextBlockNum <= currentBlockNum + 1 && times++ <= 10) {
22352241
response = HttpMethed.getNowBlock(httpNode);
22362242
responseContent = HttpMethed.parseResponseContent(response);
2237-
responseContent =
2238-
HttpMethed.parseStringContent(responseContent.get("block_header").toString());
2239-
responseContent = HttpMethed.parseStringContent(responseContent.get("raw_data").toString());
2243+
if (responseContent.containsKey("block_header")) {
2244+
responseContent = HttpMethed.parseStringContent(
2245+
responseContent.get("block_header").toString());
2246+
}
2247+
if (responseContent.containsKey("raw_data")) {
2248+
responseContent = HttpMethed.parseStringContent(
2249+
responseContent.get("raw_data").toString());
2250+
}
22402251
if (responseContent.containsKey("number")) {
22412252
nextBlockNum = Integer.parseInt(responseContent.get("number").toString());
22422253
}

framework/src/test/java/stest/tron/wallet/dailybuild/http/HttpTestClearAbiContract001.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void test1DeployContract() {
7777
response = HttpMethed.getTransactionById(httpnode, txid);
7878
responseContent = HttpMethed.parseResponseContent(response);
7979
HttpMethed.printJsonContent(responseContent);
80-
Assert.assertTrue(!responseContent.getString("contract_address").isEmpty());
80+
Assert.assertFalse(responseContent.getString("contract_address").isEmpty());
8181
contractAddress = responseContent.getString("contract_address");
8282

8383
response = HttpMethed.getTransactionInfoById(httpnode, txid);

0 commit comments

Comments
 (0)