Search in sources :

Example 1 with Sha256Hash

use of org.tron.common.utils.Sha256Hash in project java-tron by tronprotocol.

the class NodeImpl method onHandleBlockMessage.

private void onHandleBlockMessage(PeerConnection peer, BlockMessage blkMsg) {
    // logger.info("on handle block message");
    // peer.setLastBlockPeerKnow((BlockId) blkMsg.getMessageId());
    HashMap<Sha256Hash, Long> advObjWeRequested = peer.getAdvObjWeRequested();
    HashMap<BlockId, Long> syncBlockRequested = peer.getSyncBlockRequested();
    BlockId blockId = blkMsg.getBlockId();
    if (advObjWeRequested.containsKey(blockId)) {
        // broadcast mode
        advObjWeRequested.remove(blockId);
        processAdvBlock(peer, blkMsg.getBlockCapsule());
        startFetchItem();
    } else if (syncBlockRequested.containsKey(blockId)) {
        // sync mode
        syncBlockRequested.remove(blockId);
        // peer.getSyncBlockToFetch().remove(blockId);
        syncBlockIdWeRequested.remove(blockId);
        // TODO: maybe use consume pipe here better
        blockWaitToProcBak.add(blkMsg);
        // processSyncBlock(blkMsg.getBlockCapsule());
        if (!peer.isBusy()) {
            if (peer.getUnfetchSyncNum() > 0 && peer.getSyncBlockToFetch().size() < NodeConstant.SYNC_FETCH_BATCH_NUM) {
                syncNextBatchChainIds(peer);
            } else {
            // TODO: here should be a loop do this thing
            // startFetchSyncBlock();
            }
        }
    }
}
Also used : Sha256Hash(org.tron.common.utils.Sha256Hash) BlockId(org.tron.core.capsule.BlockCapsule.BlockId)

Example 2 with Sha256Hash

use of org.tron.common.utils.Sha256Hash in project java-tron by tronprotocol.

the class NodeImpl method processAdvBlock.

private void processAdvBlock(PeerConnection peer, BlockCapsule block) {
    // TODO: lack the complete flow.
    if (!freshBlockId.contains(block.getBlockId())) {
        try {
            LinkedList<Sha256Hash> trxIds = del.handleBlock(block, false);
            freshBlockId.offer(block.getBlockId());
            trxIds.forEach(trxId -> advObjToFetch.remove(trxId));
            // TODO:save message cache again.
            getActivePeer().stream().filter(p -> p.getAdvObjSpreadToUs().containsKey(block.getBlockId())).forEach(p -> {
                p.setHeadBlockWeBothHave(block.getBlockId());
                p.setHeadBlockTimeWeBothHave(block.getTimeStamp());
            });
            getActivePeer().forEach(p -> p.cleanInvGarbage());
            // rebroadcast
            broadcast(new BlockMessage(block));
        } catch (BadBlockException e) {
            badAdvObj.put(block.getBlockId(), System.currentTimeMillis());
        } catch (UnLinkedBlockException e) {
            // reSync
            startSyncWithPeer(peer);
        }
    }
}
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) BadBlockException(org.tron.core.exception.BadBlockException) BlockMessage(org.tron.core.net.message.BlockMessage) Sha256Hash(org.tron.common.utils.Sha256Hash) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException)

Example 3 with Sha256Hash

use of org.tron.common.utils.Sha256Hash 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 4 with Sha256Hash

use of org.tron.common.utils.Sha256Hash 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)

Example 5 with Sha256Hash

use of org.tron.common.utils.Sha256Hash in project java-tron by tronprotocol.

the class BlockUtilTest method testBlockUtil.

@Test
public void testBlockUtil() {
    // test create GenesisBlockCapsule
    BlockCapsule blockCapsule1 = BlockUtil.newGenesisBlockCapsule();
    Sha256Hash sha256Hash = Sha256Hash.wrap(ByteArray.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000000"));
    Assert.assertEquals(0, blockCapsule1.getTimeStamp());
    Assert.assertEquals(sha256Hash, blockCapsule1.getParentHash());
    Assert.assertEquals(0, blockCapsule1.getNum());
    // test isParentOf method: create blockCapsule2 and blockCapsule3
    // blockCapsule3.setParentHash() equals blockCapsule2.getBlockId
    BlockCapsule blockCapsule2 = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))))).build());
    BlockCapsule blockCapsule3 = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString(blockCapsule2.getBlockId().toString()))))).build());
    Assert.assertEquals(false, BlockUtil.isParentOf(blockCapsule1, blockCapsule2));
    Assert.assertFalse(BlockUtil.isParentOf(blockCapsule1, blockCapsule2));
    Assert.assertEquals(true, BlockUtil.isParentOf(blockCapsule2, blockCapsule3));
    Assert.assertTrue(BlockUtil.isParentOf(blockCapsule2, blockCapsule3));
}
Also used : Sha256Hash(org.tron.common.utils.Sha256Hash) BlockCapsule(org.tron.core.capsule.BlockCapsule) Test(org.junit.Test)

Aggregations

Sha256Hash (org.tron.common.utils.Sha256Hash)6 ArrayList (java.util.ArrayList)3 BlockCapsule (org.tron.core.capsule.BlockCapsule)3 BlockId (org.tron.core.capsule.BlockCapsule.BlockId)3 FetchInvDataMessage (org.tron.core.net.message.FetchInvDataMessage)3 Iterables (com.google.common.collect.Iterables)2 ConcurrentSet (io.netty.util.internal.ConcurrentSet)2 Collection (java.util.Collection)2 Deque (java.util.Deque)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Queue (java.util.Queue)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Executors (java.util.concurrent.Executors)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2