|
1 | 1 | package org.tron.core.net.service.sync; |
2 | 2 |
|
3 | 3 | import static org.tron.core.config.Parameter.NetConstants.MAX_BLOCK_FETCH_PER_PEER; |
| 4 | +import static org.tron.core.exception.P2pException.TypeEnum.CALC_MERKLE_ROOT_FAILED; |
| 5 | +import static org.tron.core.exception.P2pException.TypeEnum.SIGN_ERROR; |
4 | 6 |
|
5 | 7 | import com.google.common.cache.Cache; |
6 | 8 | import com.google.common.cache.CacheBuilder; |
|
23 | 25 | import org.tron.core.capsule.BlockCapsule.BlockId; |
24 | 26 | import org.tron.core.config.Parameter.NetConstants; |
25 | 27 | import org.tron.core.config.args.Args; |
| 28 | +import org.tron.core.exception.BadBlockException; |
26 | 29 | import org.tron.core.exception.P2pException; |
27 | 30 | import org.tron.core.exception.P2pException.TypeEnum; |
28 | 31 | import org.tron.core.net.TronNetDelegate; |
@@ -263,33 +266,46 @@ private synchronized void handleSyncBlock() { |
263 | 266 | tronNetDelegate.getActivePeer().stream() |
264 | 267 | .filter(peer -> msg.getBlockId().equals(peer.getSyncBlockToFetch().peek())) |
265 | 268 | .forEach(peer -> { |
266 | | - peer.getSyncBlockToFetch().pop(); |
267 | | - peer.getSyncBlockInProcess().add(msg.getBlockId()); |
268 | 269 | isFound[0] = true; |
269 | 270 | }); |
270 | 271 | if (isFound[0]) { |
271 | 272 | blockWaitToProcess.remove(msg); |
272 | 273 | isProcessed[0] = true; |
273 | | - processSyncBlock(msg.getBlockCapsule()); |
| 274 | + processSyncBlock(msg.getBlockCapsule(), peerConnection); |
274 | 275 | } |
275 | 276 | } |
276 | 277 | }); |
277 | 278 | } |
278 | 279 | } |
279 | 280 |
|
280 | | - private void processSyncBlock(BlockCapsule block) { |
| 281 | + private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection) { |
281 | 282 | boolean flag = true; |
| 283 | + boolean attackFlag = false; |
282 | 284 | BlockId blockId = block.getBlockId(); |
283 | 285 | try { |
284 | 286 | tronNetDelegate.validSignature(block); |
285 | 287 | tronNetDelegate.processBlock(block, true); |
286 | 288 | pbftDataSyncHandler.processPBFTCommitData(block); |
| 289 | + } catch (P2pException p2pException) { |
| 290 | + logger.error("Process sync block {} failed, type: {}", |
| 291 | + blockId.getString(), p2pException.getType()); |
| 292 | + attackFlag = p2pException.getType().equals(SIGN_ERROR) |
| 293 | + || p2pException.getType().equals(CALC_MERKLE_ROOT_FAILED); |
| 294 | + flag = false; |
287 | 295 | } catch (Exception e) { |
288 | 296 | logger.error("Process sync block {} failed", blockId.getString(), e); |
289 | 297 | flag = false; |
290 | 298 | } |
| 299 | + |
| 300 | + if (attackFlag) { |
| 301 | + invalid(blockId, peerConnection); |
| 302 | + peerConnection.disconnect(ReasonCode.BAD_BLOCK); |
| 303 | + return; |
| 304 | + } |
| 305 | + |
291 | 306 | for (PeerConnection peer : tronNetDelegate.getActivePeer()) { |
292 | | - if (peer.getSyncBlockInProcess().remove(blockId)) { |
| 307 | + if (blockId.equals(peer.getSyncBlockToFetch().peek())) { |
| 308 | + peer.getSyncBlockToFetch().pop(); |
293 | 309 | if (flag) { |
294 | 310 | peer.setBlockBothHave(blockId); |
295 | 311 | if (peer.getSyncBlockToFetch().isEmpty() && peer.isFetchAble()) { |
|
0 commit comments