Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) thr
throw new P2pException(TypeEnum.BAD_MESSAGE,
"minBlockNum: " + minBlockNum + ", blockNum: " + blockNum);
}
if (blockNum > peer.getLastSyncBlockId().getNum()) {
throw new P2pException(TypeEnum.BAD_MESSAGE,
"maxBlockNum: " + peer.getLastSyncBlockId().getNum() + ", blockNum: " + blockNum);
}
if (peer.getSyncBlockIdCache().getIfPresent(hash) != null) {
throw new P2pException(TypeEnum.BAD_MESSAGE,
new BlockId(hash).getString() + " is exist");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.tron.core.net.service.adv.AdvService;
import org.tron.protos.Protocol;



public class FetchInvDataMsgHandlerTest {

@Test
Expand Down Expand Up @@ -62,4 +60,37 @@ public void testProcessMessage() throws Exception {
new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK));
Assert.assertNotNull(syncBlockIdCache.getIfPresent(blockId));
}

@Test
public void testSyncFetchCheck() {
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L);
List<Sha256Hash> blockIds = new LinkedList<>();
blockIds.add(blockId);
FetchInvDataMessage msg =
new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK);

PeerConnection peer = Mockito.mock(PeerConnection.class);
Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true);
Cache<Item, Long> advInvSpread = CacheBuilder.newBuilder().maximumSize(100)
.expireAfterWrite(1, TimeUnit.HOURS).recordStats().build();
Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread);

FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler();

try {
Mockito.when(peer.getLastSyncBlockId())
.thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1000L));
fetchInvDataMsgHandler.processMessage(peer, msg);
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "maxBlockNum: 1000, blockNum: 10000");
}

try {
Mockito.when(peer.getLastSyncBlockId())
.thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 20000L));
fetchInvDataMsgHandler.processMessage(peer, msg);
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000");
}
}
}