use of co.rsk.net.NodeID in project rskj by rsksmart.
the class DownloadingSkeletonSyncState method newSkeleton.
@Override
public void newSkeleton(List<BlockIdentifier> skeleton, MessageChannel peer) {
NodeID peerId = peer.getPeerNodeID();
boolean isSelectedPeer = peerId.equals(syncInformation.getSelectedPeerId());
// defensive programming: this should never happen
if (skeleton.size() < 2) {
syncInformation.reportEvent("Invalid skeleton received from node {}", EventType.INVALID_MESSAGE, peerId, peerId);
// when the selected peer fails automatically all process restarts
if (isSelectedPeer) {
syncEventsHandler.stopSyncing();
return;
}
} else {
skeletons.put(peerId, skeleton);
}
expectedSkeletons--;
selectedPeerAnswered = selectedPeerAnswered || isSelectedPeer;
if (expectedSkeletons <= 0) {
if (skeletons.isEmpty()) {
syncEventsHandler.stopSyncing();
return;
}
syncEventsHandler.startDownloadingHeaders(skeletons, connectionPoint);
}
}
use of co.rsk.net.NodeID in project rskj by rsksmart.
the class ChannelManagerImpl method broadcastBlock.
/**
* broadcastBlock Propagates a block message across active peers with exclusion of
* the peers with an id belonging to the skip set.
*
* @param block new Block to be sent
* @param skip the set of peers to avoid sending the message.
* @return a set containing the ids of the peers that received the block.
*/
@Nonnull
public Set<NodeID> broadcastBlock(@Nonnull final Block block, @Nullable final Set<NodeID> skip) {
Metrics.broadcastBlock(block);
final Set<NodeID> res = new HashSet<>();
final BlockIdentifier bi = new BlockIdentifier(block.getHash().getBytes(), block.getNumber());
final EthMessage newBlock = new RskMessage(config, new BlockMessage(block));
final EthMessage newBlockHashes = new RskMessage(config, new NewBlockHashesMessage(Arrays.asList(bi)));
synchronized (activePeers) {
// Get a randomized list with all the peers that don't have the block yet.
activePeers.values().forEach(c -> logger.trace("RSK activePeers: {}", c));
final Vector<Channel> peers = activePeers.values().stream().filter(p -> skip == null || !skip.contains(p.getNodeId())).collect(Collectors.toCollection(Vector::new));
Collections.shuffle(peers);
int sqrt = (int) Math.floor(Math.sqrt(peers.size()));
for (int i = 0; i < sqrt; i++) {
Channel peer = peers.get(i);
res.add(peer.getNodeId());
logger.trace("RSK propagate: {}", peer);
peer.sendMessage(newBlock);
}
for (int i = sqrt; i < peers.size(); i++) {
Channel peer = peers.get(i);
logger.trace("RSK announce: {}", peer);
peer.sendMessage(newBlockHashes);
}
}
return res;
}
use of co.rsk.net.NodeID in project rskj by rsksmart.
the class ChannelManagerImpl method broadcastBlockHash.
@Nonnull
public Set<NodeID> broadcastBlockHash(@Nonnull final List<BlockIdentifier> identifiers, @Nullable final Set<NodeID> targets) {
final Set<NodeID> res = new HashSet<>();
final EthMessage newBlockHash = new RskMessage(config, new NewBlockHashesMessage(identifiers));
synchronized (activePeers) {
activePeers.values().forEach(c -> logger.trace("RSK activePeers: {}", c));
activePeers.values().stream().filter(p -> targets == null || targets.contains(p.getNodeId())).forEach(peer -> {
logger.trace("RSK announce hash: {}", peer);
peer.sendMessage(newBlockHash);
});
}
return res;
}
use of co.rsk.net.NodeID in project rskj by rsksmart.
the class ChannelManagerImpl method broadcastTransaction.
/**
* broadcastTransaction Propagates a transaction message across active peers with exclusion of
* the peers with an id belonging to the skip set.
*
* @param transaction new Transaction to be sent
* @param skip the set of peers to avoid sending the message.
* @return a set containing the ids of the peers that received the transaction.
*/
@Nonnull
public Set<NodeID> broadcastTransaction(@Nonnull final Transaction transaction, @Nullable final Set<NodeID> skip) {
Metrics.broadcastTransaction(transaction);
List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction);
final Set<NodeID> res = new HashSet<>();
final EthMessage newTransactions = new RskMessage(config, new TransactionsMessage(transactions));
synchronized (activePeers) {
final Vector<Channel> peers = activePeers.values().stream().filter(p -> skip == null || !skip.contains(p.getNodeId())).collect(Collectors.toCollection(Vector::new));
for (Channel peer : peers) {
res.add(peer.getNodeId());
peer.sendMessage(newTransactions);
}
}
return res;
}
use of co.rsk.net.NodeID in project rskj by rsksmart.
the class DownloadingBodiesSyncState method newBody.
@Override
public void newBody(BodyResponseMessage message, MessageChannel peer) {
NodeID peerId = peer.getPeerNodeID();
if (!isExpectedBody(message.getId(), peerId)) {
handleUnexpectedBody(peerId);
return;
}
// we already checked that this message was expected
BlockHeader header = pendingBodyResponses.remove(message.getId()).header;
Block block = Block.fromValidData(header, message.getTransactions(), message.getUncles());
if (!blockUnclesHashValidationRule.isValid(block) || !blockTransactionsValidationRule.isValid(block)) {
handleInvalidMessage(peerId, header);
return;
}
// handle block
if (syncInformation.processBlock(block, peer).isInvalidBlock()) {
handleInvalidBlock(peerId, header);
return;
}
// updates peer downloading information
tryRequestNextBody(peerId);
// check if this was the last block to download
verifyDownloadIsFinished();
}
Aggregations