Search in sources :

Example 1 with FetchInvDataMessage

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);
}
Also used : FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) Sha256Hash(org.tron.common.utils.Sha256Hash) ArrayList(java.util.ArrayList)

Example 2 with FetchInvDataMessage

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());
    }
}
Also used : TransactionMessage(org.tron.core.net.message.TransactionMessage) InventoryMessage(org.tron.core.net.message.InventoryMessage) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TronMessage(org.tron.core.net.message.TronMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) MessageTypes(org.tron.core.net.message.MessageTypes) BlockCapsule(org.tron.core.capsule.BlockCapsule)

Example 3 with FetchInvDataMessage

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();
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) BlockCapsule(org.tron.core.capsule.BlockCapsule) ConcurrentSet(io.netty.util.internal.ConcurrentSet) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) NodeHandler(org.tron.common.overlay.discover.NodeHandler) TransactionMessage(org.tron.core.net.message.TransactionMessage) NodeConstant(org.tron.core.config.Parameter.NodeConstant) Pair(javafx.util.Pair) InventoryMessage(org.tron.core.net.message.InventoryMessage) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MessageTypes(org.tron.core.net.message.MessageTypes) UnReachBlockException(org.tron.core.exception.UnReachBlockException) ExecutorLoop(org.tron.common.utils.ExecutorLoop) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Entry(java.util.Map.Entry) Queue(java.util.Queue) TronException(org.tron.core.exception.TronException) BlockConstant(org.tron.core.config.Parameter.BlockConstant) BadTransactionException(org.tron.core.exception.BadTransactionException) Iterables(com.google.common.collect.Iterables) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TronState(org.tron.common.overlay.server.Channel.TronState) PeerConnectionDelegate(org.tron.core.net.peer.PeerConnectionDelegate) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BadBlockException(org.tron.core.exception.BadBlockException) LinkedList(java.util.LinkedList) Sha256Hash(org.tron.common.utils.Sha256Hash) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) InventoryType(org.tron.protos.Protocol.Inventory.InventoryType) NetConstants(org.tron.core.config.Parameter.NetConstants) SyncPool(org.tron.common.overlay.server.SyncPool) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) TraitorPeerException(org.tron.core.exception.TraitorPeerException) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TimeUnit(java.util.concurrent.TimeUnit) TronMessage(org.tron.core.net.message.TronMessage) PeerConnection(org.tron.core.net.peer.PeerConnection) Component(org.springframework.stereotype.Component) ReasonCode(org.tron.common.overlay.message.ReasonCode) Time(org.tron.common.utils.Time) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) PeerConnection(org.tron.core.net.peer.PeerConnection) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Sha256Hash(org.tron.common.utils.Sha256Hash) LinkedList(java.util.LinkedList) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Aggregations

FetchInvDataMessage (org.tron.core.net.message.FetchInvDataMessage)3 ArrayList (java.util.ArrayList)2 Message (org.tron.common.overlay.message.Message)2 Sha256Hash (org.tron.common.utils.Sha256Hash)2 BlockCapsule (org.tron.core.capsule.BlockCapsule)2 BlockInventoryMessage (org.tron.core.net.message.BlockInventoryMessage)2 BlockMessage (org.tron.core.net.message.BlockMessage)2 ChainInventoryMessage (org.tron.core.net.message.ChainInventoryMessage)2 InventoryMessage (org.tron.core.net.message.InventoryMessage)2 ItemNotFound (org.tron.core.net.message.ItemNotFound)2 MessageTypes (org.tron.core.net.message.MessageTypes)2 SyncBlockChainMessage (org.tron.core.net.message.SyncBlockChainMessage)2 TransactionMessage (org.tron.core.net.message.TransactionMessage)2 TronMessage (org.tron.core.net.message.TronMessage)2 Iterables (com.google.common.collect.Iterables)1 ConcurrentSet (io.netty.util.internal.ConcurrentSet)1 Collection (java.util.Collection)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1