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();
}
}
}
}
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);
}
}
}
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);
}
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();
}
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));
}
Aggregations