Skip to content

Commit

Permalink
Merge pull request tronprotocol#3313 from tronprotocol/pbft-4.1-dev
Browse files Browse the repository at this point in the history
fix the pbft data sync
  • Loading branch information
lvs007 authored Jul 29, 2020
2 parents 3b259d0 + 43cc312 commit 9e644e6
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public enum MessageTypes {

TRX_INVENTORY(0x13),

PBFT_BLOCK_MSG(0x14),

PBFT_SRL_MSG(0x15),
PBFT_COMMIT_MSG(0x14),

P2P_HELLO(0x20),

Expand Down Expand Up @@ -84,7 +82,7 @@ public static boolean inP2pRange(byte code) {
}

public static boolean inTronRange(byte code) {
return code <= PBFT_SRL_MSG.asByte() && code >= FIRST.asByte();
return code <= PBFT_COMMIT_MSG.asByte() && code >= FIRST.asByte();
}

public byte asByte() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ protected void onMessage(PeerConnection peer, TronMessage msg) {
case TRXS:
transactionsMsgHandler.processMessage(peer, msg);
break;
case PBFT_BLOCK_MSG:
case PBFT_SRL_MSG:
case PBFT_COMMIT_MSG:
pbftDataSyncHandler.processMessage(peer, msg);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public class PbftCommitMessage extends TronMessage {

public PbftCommitMessage(byte[] data) {
super(data);
this.type = MessageTypes.PBFT_BLOCK_MSG.asByte();
this.type = MessageTypes.PBFT_COMMIT_MSG.asByte();
this.pbftSignCapsule = new PbftSignCapsule(data);
}

public PbftCommitMessage(PbftSignCapsule pbftSignCapsule) {
data = pbftSignCapsule.getData();
this.type = MessageTypes.PBFT_BLOCK_MSG.asByte();
this.type = MessageTypes.PBFT_COMMIT_MSG.asByte();
this.pbftSignCapsule = pbftSignCapsule;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ private TronMessage create(byte type, byte[] packed) throws Exception {
return new FetchBlockHeadersMessage(packed);
case TRX_INVENTORY:
return new TransactionInventoryMessage(packed);
case PBFT_BLOCK_MSG:
case PBFT_SRL_MSG:
case PBFT_COMMIT_MSG:
return new PbftCommitMessage(packed);
default:
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep

private void sendPbftCommitMessage(PeerConnection peer, BlockCapsule blockCapsule) {
try {
if (!tronNetDelegate.allowPBFT()) {
if (!tronNetDelegate.allowPBFT() || peer.isSyncFinish()) {
return;
}
long epoch = 0;
Expand All @@ -114,16 +114,16 @@ private void sendPbftCommitMessage(PeerConnection peer, BlockCapsule blockCapsul
.getMaintenanceTimeInterval();
if (pbftSignCapsule != null) {
Raw raw = Raw.parseFrom(pbftSignCapsule.getPbftCommitResult().getData());
epoch = raw.getEpoch() + maintenanceTimeInterval;
epoch = raw.getEpoch();
peer.sendMessage(new PbftCommitMessage(pbftSignCapsule));
} else {
epoch =
(blockCapsule.getTimeStamp() / maintenanceTimeInterval + 1) * maintenanceTimeInterval;
}
if (epochCache.getIfPresent(epoch) == null) {
epochCache.put(epoch, true);
PbftSignCapsule srl = tronNetDelegate.getSRLPbftCommitData(epoch);
if (srl != null) {
epochCache.put(epoch, true);
peer.sendMessage(new PbftCommitMessage(srl));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,22 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
}
}

public void processPBFTCommitMessage(long blockNum) {
public void processPBFTCommitData(BlockCapsule block) {
try {
if (!chainBaseManager.getDynamicPropertiesStore().allowPBFT()) {
return;
}
long epoch = 0;
PbftCommitMessage pbftCommitMessage = pbftCommitMessageCache.remove(blockNum);
PbftCommitMessage pbftCommitMessage = pbftCommitMessageCache.remove(block.getNum());
long maintenanceTimeInterval = chainBaseManager.getDynamicPropertiesStore()
.getMaintenanceTimeInterval();
if (pbftCommitMessage == null) {
BlockCapsule blockCapsule = chainBaseManager.getBlockByNum(blockNum);
long round = blockCapsule.getTimeStamp() / maintenanceTimeInterval;
long round = block.getTimeStamp() / maintenanceTimeInterval;
epoch = (round + 1) * maintenanceTimeInterval;
} else {
processPBFTCommitMessage(pbftCommitMessage);
Raw raw = Raw.parseFrom(pbftCommitMessage.getPBFTCommitResult().getData());
epoch = raw.getEpoch() + maintenanceTimeInterval;
epoch = raw.getEpoch();
}
pbftCommitMessage = pbftCommitMessageCache.remove(epoch);
if (pbftCommitMessage != null) {
Expand All @@ -93,16 +92,18 @@ private void processPBFTCommitMessage(PbftCommitMessage pbftCommitMessage) {
if (raw.getDataType() == DataType.BLOCK
&& pbftSignDataStore.getBlockSignData(raw.getViewN()) == null) {
pbftSignDataStore.putBlockSignData(raw.getViewN(), pbftCommitMessage.getPbftSignCapsule());
logger.info("save the block {} pbft commit data", raw.getViewN());
} else if (raw.getDataType() == DataType.SRL
&& pbftSignDataStore.getSrSignData(raw.getEpoch()) == null) {
pbftSignDataStore.putSrSignData(raw.getEpoch(), pbftCommitMessage.getPbftSignCapsule());
logger.info("save the srl {} pbft commit data", raw.getEpoch());
}
} catch (InvalidProtocolBufferException e) {
logger.error("", e);
}
}

public boolean validPbftSign(Raw raw, List<ByteString> srSignList,
private boolean validPbftSign(Raw raw, List<ByteString> srSignList,
List<ByteString> currentSrList) {
//valid sr list
if (srSignList.size() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@ public String log() {
+ nodeStatistics.toString() + "\n";
}

public boolean isSyncFinish() {
return !(needSyncFromPeer || needSyncFromUs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ private void processSyncBlock(BlockCapsule block) {
boolean flag = true;
BlockId blockId = block.getBlockId();
try {
pbftDataSyncHandler.processPBFTCommitMessage(block.getNum());
tronNetDelegate.processBlock(block, true);
pbftDataSyncHandler.processPBFTCommitData(block);
} catch (Exception e) {
logger.error("Process sync block {} failed.", blockId.getString(), e);
flag = false;
Expand Down

0 comments on commit 9e644e6

Please sign in to comment.