when receive a ForwardSmallBlockMessage , wait down the SmallblockMessage , ensure block synchronization
@@ -30,6 +30,8 @@ import io.nuls.kernel.model.Result;
import io.nuls.message.bus.handler.AbstractMessageHandler;
import io.nuls.message.bus.service.MessageBusService;
import io.nuls.network.model.Node;
+import io.nuls.protocol.cache.TemporaryCacheManager;
+import io.nuls.protocol.model.SmallBlock;
import io.nuls.protocol.utils.SmallBlockDuplicateRemoval;
import io.nuls.protocol.message.ForwardSmallBlockMessage;
import io.nuls.protocol.message.GetSmallBlockMessage;
@@ -40,6 +42,7 @@ import io.nuls.protocol.message.GetSmallBlockMessage;
public class ForwardSmallBlockHandler extends AbstractMessageHandler<ForwardSmallBlockMessage> {
private MessageBusService messageBusService = NulsContext.getServiceBean(MessageBusService.class);
+ private TemporaryCacheManager temporaryCacheManager = TemporaryCacheManager.getInstance();
@Override
public void onMessage(ForwardSmallBlockMessage message, Node fromNode) {
@@ -53,10 +56,33 @@ public class ForwardSmallBlockHandler extends AbstractMessageHandler<ForwardSmal
GetSmallBlockMessage getSmallBlockMessage = new GetSmallBlockMessage();
getSmallBlockMessage.setMsgBody(hash);
Result result = messageBusService.sendToNode(getSmallBlockMessage, fromNode, true);
+
if (result.isFailed()) {
SmallBlockDuplicateRemoval.removeForward(hash);
return;
}
+
+ waitForReceiveSmallBlockMessage(hash);
+ }
+
+ private void waitForReceiveSmallBlockMessage(NulsDigestData hash) {
+ try {
+ long beginTime = System.currentTimeMillis();
+ while(true) {
+ SmallBlock smallBlock = temporaryCacheManager.getSmallBlockByHash(hash);
+ if(smallBlock == null) {
+ Thread.sleep(100L);
+ } else {
+ break;
+ }
+ if(System.currentTimeMillis() - beginTime > 1000L) {
+ SmallBlockDuplicateRemoval.removeForward(hash);
+ break;
+ }
+ }
+ } catch (Exception e) {
+ SmallBlockDuplicateRemoval.removeForward(hash);
+ }
}
}
