From 7f0db7e0b9a40da126004a110ab63baf4ff9d3da Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Tue, 24 Oct 2023 17:18:44 +0800 Subject: [PATCH 1/3] fix(pbft): Add block height check when processing PBFT messages --- .../java/org/tron/common/parameter/CommonParameter.java | 3 +++ common/src/main/java/org/tron/core/Constant.java | 1 + .../org/tron/consensus/pbft/message/PbftMessage.java | 4 ++++ .../src/main/java/org/tron/core/config/args/Args.java | 5 +++++ .../org/tron/core/net/messagehandler/PbftMsgHandler.java | 9 +++++++++ 5 files changed, 22 insertions(+) diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 8c4cd23f09a..f8f6e6c5edb 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -518,6 +518,9 @@ public class CommonParameter { public int pBFTHttpPort; @Getter @Setter + public long pBFTExpireNum; + @Getter + @Setter public long oldSolidityBlockNum = -1; @Getter/**/ diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 54ff30d9e65..ba867793b41 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -303,6 +303,7 @@ public class Constant { public static final String SEED_NODE_IP_LIST = "seed.node.ip.list"; public static final String NODE_METRICS_ENABLE = "node.metricsEnable"; public static final String COMMITTEE_ALLOW_PBFT = "committee.allowPBFT"; + public static final String COMMITTEE_PBFT_EXPIRE_NUM = "committee.pBFTExpireNum"; public static final String NODE_AGREE_NODE_COUNT = "node.agreeNodeCount"; public static final String COMMITTEE_ALLOW_TRANSACTION_FEE_POOL = "committee.allowTransactionFeePool"; diff --git a/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java b/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java index b6de49ee878..b56c77f0c53 100644 --- a/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java +++ b/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java @@ -28,6 +28,10 @@ public String getNo() { return pbftMessage.getRawData().getViewN() + "_" + pbftMessage.getRawData().getDataType(); } + public long getViewN() { + return pbftMessage.getRawData().getViewN(); + } + public static PbftMessage prePrepareBlockMsg(BlockCapsule block, long epoch, Miner miner) { return buildCommon(DataType.BLOCK, block.getBlockId().getByteString(), block, epoch, block.getNum(), miner); diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 5c2d76afbb1..75140dd5f54 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -149,6 +149,7 @@ public static void clearParam() { PARAMETER.fullNodeHttpPort = 0; PARAMETER.solidityHttpPort = 0; PARAMETER.pBFTHttpPort = 0; + PARAMETER.pBFTExpireNum = 20; PARAMETER.jsonRpcHttpFullNodePort = 0; PARAMETER.jsonRpcHttpSolidityPort = 0; PARAMETER.jsonRpcHttpPBFTPort = 0; @@ -974,6 +975,10 @@ public static void setParam(final String[] args, final String confFileName) { config.hasPath(Constant.COMMITTEE_ALLOW_PBFT) ? config .getLong(Constant.COMMITTEE_ALLOW_PBFT) : 0; + PARAMETER.pBFTExpireNum = + config.hasPath(Constant.COMMITTEE_PBFT_EXPIRE_NUM) ? config + .getLong(Constant.COMMITTEE_PBFT_EXPIRE_NUM) : 20; + PARAMETER.agreeNodeCount = config.hasPath(Constant.NODE_AGREE_NODE_COUNT) ? config .getInt(Constant.NODE_AGREE_NODE_COUNT) : MAX_ACTIVE_WITNESS_NUM * 2 / 3 + 1; PARAMETER.agreeNodeCount = PARAMETER.agreeNodeCount > MAX_ACTIVE_WITNESS_NUM diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java index 44eed8d1c2f..c746602da5f 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java @@ -11,7 +11,9 @@ import org.tron.consensus.pbft.PbftManager; import org.tron.consensus.pbft.message.PbftBaseMessage; import org.tron.consensus.pbft.message.PbftMessage; +import org.tron.core.config.args.Args; import org.tron.core.exception.P2pException; +import org.tron.core.net.TronNetDelegate; import org.tron.core.net.TronNetService; import org.tron.core.net.peer.PeerConnection; @@ -26,10 +28,17 @@ public class PbftMsgHandler { @Autowired private PbftManager pbftManager; + @Autowired + private TronNetDelegate tronNetDelegate; + public void processMessage(PeerConnection peer, PbftMessage msg) throws Exception { if (Param.getInstance().getPbftInterface().isSyncing()) { return; } + if (tronNetDelegate.getHeadBlockId().getNum() - msg.getViewN() > + Args.getInstance().getPBFTExpireNum()) { + return; + } msg.analyzeSignature(); String key = buildKey(msg); Lock lock = striped.get(key); From 82415a180325d0c5710ad8827eb98400fd2efe47 Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Tue, 24 Oct 2023 17:59:13 +0800 Subject: [PATCH 2/3] fix(pbft): check PBFT message based on data type --- .../org/tron/consensus/pbft/message/PbftMessage.java | 4 ---- .../main/java/org/tron/core/net/TronNetDelegate.java | 4 ++++ .../tron/core/net/messagehandler/PbftMsgHandler.java | 10 ++++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java b/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java index b56c77f0c53..b6de49ee878 100644 --- a/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java +++ b/consensus/src/main/java/org/tron/consensus/pbft/message/PbftMessage.java @@ -28,10 +28,6 @@ public String getNo() { return pbftMessage.getRawData().getViewN() + "_" + pbftMessage.getRawData().getDataType(); } - public long getViewN() { - return pbftMessage.getRawData().getViewN(); - } - public static PbftMessage prePrepareBlockMsg(BlockCapsule block, long epoch, Miner miner) { return buildCommon(DataType.BLOCK, block.getBlockId().getByteString(), block, epoch, block.getNum(), miner); diff --git a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java index 4d22f98d680..a12f00b113c 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java +++ b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java @@ -357,4 +357,8 @@ public Object getForkLock() { return dbManager.getForkLock(); } + public long getNextMaintenanceTime() { + return chainBaseManager.getDynamicPropertiesStore().getNextMaintenanceTime(); + } + } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java index c746602da5f..f747a1af975 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java @@ -16,6 +16,7 @@ import org.tron.core.net.TronNetDelegate; import org.tron.core.net.TronNetService; import org.tron.core.net.peer.PeerConnection; +import org.tron.protos.Protocol.PBFTMessage.DataType; @Component public class PbftMsgHandler { @@ -35,8 +36,13 @@ public void processMessage(PeerConnection peer, PbftMessage msg) throws Exceptio if (Param.getInstance().getPbftInterface().isSyncing()) { return; } - if (tronNetDelegate.getHeadBlockId().getNum() - msg.getViewN() > - Args.getInstance().getPBFTExpireNum()) { + if (msg.getDataType().equals(DataType.BLOCK) && + tronNetDelegate.getHeadBlockId().getNum() - msg.getNumber() + > Args.getInstance().getPBFTExpireNum()) { + return; + } + long currentEpoch = tronNetDelegate.getNextMaintenanceTime(); + if (msg.getDataType().equals(DataType.SRL) && currentEpoch - msg.getEpoch() > 1) { return; } msg.analyzeSignature(); From dff1114e303d17a77673799a6e54f81c4d179b73 Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Tue, 24 Oct 2023 18:28:37 +0800 Subject: [PATCH 3/3] fix(pbft): modify epoch check --- .../main/java/org/tron/core/net/TronNetDelegate.java | 4 ++++ .../tron/core/net/messagehandler/PbftMsgHandler.java | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java index a12f00b113c..5d09e67908d 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java +++ b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java @@ -361,4 +361,8 @@ public long getNextMaintenanceTime() { return chainBaseManager.getDynamicPropertiesStore().getNextMaintenanceTime(); } + public long getMaintenanceTimeInterval() { + return chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); + } + } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java index f747a1af975..4e4cc858898 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java @@ -36,13 +36,15 @@ public void processMessage(PeerConnection peer, PbftMessage msg) throws Exceptio if (Param.getInstance().getPbftInterface().isSyncing()) { return; } - if (msg.getDataType().equals(DataType.BLOCK) && - tronNetDelegate.getHeadBlockId().getNum() - msg.getNumber() - > Args.getInstance().getPBFTExpireNum()) { + if (msg.getDataType().equals(DataType.BLOCK) + && tronNetDelegate.getHeadBlockId().getNum() - msg.getNumber() + > Args.getInstance().getPBFTExpireNum()) { return; } long currentEpoch = tronNetDelegate.getNextMaintenanceTime(); - if (msg.getDataType().equals(DataType.SRL) && currentEpoch - msg.getEpoch() > 1) { + long expireEpoch = 2 * tronNetDelegate.getMaintenanceTimeInterval(); + if (msg.getDataType().equals(DataType.SRL) + && currentEpoch - msg.getEpoch() > expireEpoch) { return; } msg.analyzeSignature();