Skip to content

Commit 2e4f13e

Browse files
authored
Merge pull request tronprotocol#1531 from tronprotocol/mdfNPE2BadBlockException
Mdf npe2 bad block exception
2 parents 9ccb44c + e8d6ce8 commit 2e4f13e

3 files changed

Lines changed: 25 additions & 30 deletions

File tree

‎src/main/java/org/tron/core/net/node/NodeDelegate.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ LinkedList<Sha256Hash> handleBlock(BlockCapsule block, boolean syncMode)
2828
Deque<BlockId> getBlockChainSummary(BlockId beginBLockId, Deque<BlockId> blockIds)
2929
throws TronException;
3030

31-
Message getData(Sha256Hash msgId, MessageTypes type);
31+
Message getData(Sha256Hash msgId, MessageTypes type) throws StoreException;
3232

3333
void syncToCli(long unSyncNum);
3434

‎src/main/java/org/tron/core/net/node/NodeDelegateImpl.java‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -279,31 +279,19 @@ public Deque<BlockId> getBlockChainSummary(BlockId beginBlockId, Deque<BlockId>
279279
}
280280

281281
@Override
282-
public Message getData(Sha256Hash hash, MessageTypes type) {
282+
public Message getData(Sha256Hash hash, MessageTypes type)
283+
throws StoreException {
283284
switch (type) {
284285
case BLOCK:
285-
try {
286-
return new BlockMessage(dbManager.getBlockById(hash));
287-
} catch (BadItemException e) {
288-
logger.debug(e.getMessage());
289-
} catch (ItemNotFoundException e) {
290-
291-
logger.debug(e.getMessage());
292-
} catch (Exception e) {
293-
logger.error("new BlockMessage fail", e);
294-
}
295-
return null;
286+
return new BlockMessage(dbManager.getBlockById(hash));
296287
case TRX:
297-
try {
298-
return new TransactionMessage(
299-
dbManager.getTransactionStore().get(hash.getBytes()).getData());
300-
} catch (Exception e) {
301-
logger.error("new TransactionMessage fail", e);
288+
TransactionCapsule tx = dbManager.getTransactionStore().get(hash.getBytes());
289+
if (tx != null) {
290+
return new TransactionMessage(tx.getData());
302291
}
303-
return null;
292+
throw new ItemNotFoundException("transaction is not found");
304293
default:
305-
logger.info("message type not block or trx.");
306-
return null;
294+
throw new BadItemException("message type not block or trx.");
307295
}
308296
}
309297

‎src/main/java/org/tron/core/net/node/NodeImpl.java‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,14 +1001,14 @@ private void onHandleFetchDataMessage(PeerConnection peer, FetchInvDataMessage f
10011001
}
10021002

10031003
if (msg == null) {
1004-
msg = del.getData(hash, type);
1005-
}
1006-
1007-
if (msg == null) {
1008-
logger.error("fetch message {} {} failed.", type, hash);
1009-
peer.sendMessage(new ItemNotFound());
1010-
//todo,should be disconnect?
1011-
return;
1004+
try {
1005+
msg = del.getData(hash, type);
1006+
} catch (StoreException e) {
1007+
logger.error("fetch message {} {} failed.", type, hash);
1008+
peer.sendMessage(new ItemNotFound());
1009+
//todo,should be disconnect?
1010+
return;
1011+
}
10121012
}
10131013

10141014
if (type.equals(MessageTypes.BLOCK)) {
@@ -1174,6 +1174,12 @@ private void onHandleChainInventoryMessage(PeerConnection peer, ChainInventoryMe
11741174
} catch (TraitorPeerException e) {
11751175
logger.error(e.getMessage());
11761176
banTraitorPeer(peer, ReasonCode.BAD_PROTOCOL);
1177+
} catch (StoreException e) {
1178+
//we try update our both known block but this block is null
1179+
//because when we try to switch to this block's fork then fail.
1180+
//The reason only is we get a bad block which peer broadcast to us.
1181+
logger.error(e.getMessage());
1182+
banTraitorPeer(peer, ReasonCode.BAD_BLOCK);
11771183
}
11781184
}
11791185

@@ -1236,7 +1242,8 @@ private void updateBlockWeBothHave(PeerConnection peer, BlockCapsule block) {
12361242
peer.setHeadBlockTimeWeBothHave(block.getTimeStamp());
12371243
}
12381244

1239-
private void updateBlockWeBothHave(PeerConnection peer, BlockId blockId) {
1245+
private void updateBlockWeBothHave(PeerConnection peer, BlockId blockId)
1246+
throws StoreException {
12401247
logger.info("update peer {} block both we have, {}", peer.getNode().getHost(),
12411248
blockId.getString());
12421249
peer.setHeadBlockWeBothHave(blockId);

0 commit comments

Comments
 (0)