use of org.tron.core.net.message.FetchInvDataMessage in project java-tron by tronprotocol.
the class NodeImpl method onHandleBlockInventoryMessage.
private void onHandleBlockInventoryMessage(PeerConnection peer, BlockInventoryMessage msg) {
logger.info("on handle advertise blocks inventory message");
peer.cleanInvGarbage();
// todo: check this peer's advertise history and the history of our request to this peer.
// simple implement here first
List<Sha256Hash> fetchList = new ArrayList<>();
msg.getBlockIds().forEach(hash -> {
// TODO: Check this block whether we need it,Use peer.invToUs and peer.invWeAdv.
logger.info("We will fetch " + hash + " from " + peer);
fetchList.add(hash);
});
FetchInvDataMessage fetchMsg = new FetchInvDataMessage(fetchList, InventoryType.BLOCK);
fetchMap.put(fetchMsg.getMessageId(), peer);
loopFetchBlocks.push(fetchMsg);
}
use of org.tron.core.net.message.FetchInvDataMessage in project java-tron by tronprotocol.
the class NodeImpl method onHandleFetchDataMessage.
private void onHandleFetchDataMessage(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) {
logger.info("on handle fetch block message");
MessageTypes type = fetchInvDataMsg.getInvMessageType();
// TODO:maybe can use message cache here
final BlockCapsule[] blocks = { del.getGenesisBlock() };
// get data and send it one by one
fetchInvDataMsg.getHashList().forEach(hash -> {
if (del.contain(hash, type)) {
Message msg = del.getData(hash, type);
if (type.equals(MessageTypes.BLOCK)) {
blocks[0] = ((BlockMessage) msg).getBlockCapsule();
}
peer.sendMessage(msg);
} else {
peer.sendMessage(new ItemNotFound());
}
});
if (blocks[0] != null) {
peer.setHeadBlockWeBothHave(blocks[0].getBlockId());
peer.setHeadBlockTimeWeBothHave(blocks[0].getTimeStamp());
}
}
use of org.tron.core.net.message.FetchInvDataMessage in project java-tron by tronprotocol.
the class NodeImpl method startFetchSyncBlock.
private synchronized void startFetchSyncBlock() {
// TODO: check how many block is processing and decide if fetch more
HashMap<PeerConnection, List<BlockId>> send = new HashMap<>();
HashSet<BlockId> request = new HashSet<>();
getActivePeer().stream().filter(peer -> peer.isNeedSyncFromPeer() && !peer.isBusy()).forEach(peer -> {
if (!send.containsKey(peer)) {
// TODO: Attention multi thread here
send.put(peer, new LinkedList<>());
}
for (BlockId blockId : peer.getSyncBlockToFetch()) {
if (// TODO: clean processing block
!request.contains(blockId) && !syncBlockIdWeRequested.containsKey(blockId)) {
send.get(peer).add(blockId);
request.add(blockId);
// TODO: check max block num to fetch from one peer.
// if (send.get(peer).size() > 200) { //Max Blocks peer get one time
// break;
// }
}
}
});
send.forEach((peer, blockIds) -> {
// TODO: use collector
blockIds.forEach(blockId -> {
syncBlockIdWeRequested.put(blockId, System.currentTimeMillis());
peer.getSyncBlockRequested().put(blockId, System.currentTimeMillis());
});
List<Sha256Hash> ids = new LinkedList<>();
ids.addAll(blockIds);
peer.sendMessage(new FetchInvDataMessage(ids, InventoryType.BLOCK));
});
send.clear();
}
Aggregations