Skip to content

Commit

Permalink
Merge pull request #5925 from 317787106/hotfix/set_block_both_have2
Browse files Browse the repository at this point in the history
feat(net): set block_both_have after handling broadcast block
  • Loading branch information
lvs007 committed Jul 25, 2024
2 parents 729a99b + 34eb577 commit d6bbbe4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ private void processBlock(PeerConnection peer, BlockCapsule block) throws P2pExc
try {
tronNetDelegate.processBlock(block, false);
witnessProductBlockService.validWitnessProductTwoBlock(block);

Item item = new Item(blockId, InventoryType.BLOCK);
tronNetDelegate.getActivePeer().forEach(p -> {
if (p.getAdvInvReceive().getIfPresent(item) != null) {
p.setBlockBothHave(blockId);
}
});
} catch (Exception e) {
logger.warn("Process adv block {} from peer {} failed. reason: {}",
blockId, peer.getInetAddress(), e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ public String log() {
channel.getInetSocketAddress(),
(now - channel.getStartTime()) / Constant.ONE_THOUSAND,
channel.getAvgLatency(),
fastForwardBlock != null ? fastForwardBlock.getNum() : blockBothHave.getNum(),
fastForwardBlock != null ? fastForwardBlock.getNum() : String.format("%d [%ds]",
blockBothHave.getNum(), (now - blockBothHaveUpdateTime) / Constant.ONE_THOUSAND),
isNeedSyncFromPeer(),
isNeedSyncFromUs(),
syncBlockToFetch.size(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package org.tron.core.net.messagehandler;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.tron.common.BaseTest;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.core.config.Parameter;
import org.tron.core.config.args.Args;
import org.tron.core.exception.P2pException;
import org.tron.core.net.TronNetDelegate;
import org.tron.core.net.message.adv.BlockMessage;
import org.tron.core.net.peer.Item;
import org.tron.core.net.peer.PeerConnection;
Expand All @@ -41,9 +48,8 @@ public class BlockMsgHandlerTest extends BaseTest {
*/
@BeforeClass
public static void init() {
Args.setParam(new String[]{"--output-directory", dbPath(), "--debug"},
Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"},
Constant.TEST_CONF);

}

@Before
Expand Down Expand Up @@ -123,4 +129,42 @@ public void testProcessMessage() {
logger.error("error", e);
}
}

@Test
public void testProcessBlock() {
TronNetDelegate tronNetDelegate = Mockito.mock(TronNetDelegate.class);

try {
Field field = handler.getClass().getDeclaredField("tronNetDelegate");
field.setAccessible(true);
field.set(handler, tronNetDelegate);

BlockCapsule blockCapsule0 = new BlockCapsule(1,
Sha256Hash.wrap(ByteString
.copyFrom(ByteArray
.fromHexString(
"9938a342238077182498b464ac0292229938a342238077182498b464ac029222"))),
1234,
ByteString.copyFrom("1234567".getBytes()));

peer.getAdvInvReceive()
.put(new Item(blockCapsule0.getBlockId(), InventoryType.BLOCK),
System.currentTimeMillis());

Mockito.doReturn(true).when(tronNetDelegate).validBlock(any(BlockCapsule.class));
Mockito.doReturn(true).when(tronNetDelegate).containBlock(any(BlockId.class));
Mockito.doReturn(blockCapsule0.getBlockId()).when(tronNetDelegate).getHeadBlockId();
Mockito.doNothing().when(tronNetDelegate).processBlock(any(BlockCapsule.class), anyBoolean());
List<PeerConnection> peers = new ArrayList<>();
peers.add(peer);
Mockito.doReturn(peers).when(tronNetDelegate).getActivePeer();

Method method = handler.getClass()
.getDeclaredMethod("processBlock", PeerConnection.class, BlockCapsule.class);
method.setAccessible(true);
method.invoke(handler, peer, blockCapsule0);
} catch (Exception e) {
Assert.fail();
}
}
}

0 comments on commit d6bbbe4

Please sign in to comment.