use of org.ethereum.core.BlockIdentifier in project rskj by rsksmart.
the class NodeMessageHandler method relayBlock.
private void relayBlock(@Nonnull MessageChannel sender, Block block) {
final BlockNodeInformation nodeInformation = this.blockProcessor.getNodeInformation();
final Set<NodeID> nodesWithBlock = nodeInformation.getNodesByBlock(block.getHash().getBytes());
final Set<NodeID> newNodes = this.syncProcessor.getKnownPeersNodeIDs().stream().filter(p -> !nodesWithBlock.contains(p)).collect(Collectors.toSet());
List<BlockIdentifier> identifiers = new ArrayList<>();
identifiers.add(new BlockIdentifier(block.getHash().getBytes(), block.getNumber()));
channelManager.broadcastBlockHash(identifiers, newNodes);
Metrics.processBlockMessage("blockRelayed", block, sender.getPeerNodeID());
}
use of org.ethereum.core.BlockIdentifier in project rskj by rsksmart.
the class NewBlockHashesMessage method parse.
private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
blockIdentifiers = new ArrayList<>();
for (int i = 0; i < paramsList.size(); ++i) {
RLPList rlpData = ((RLPList) paramsList.get(i));
blockIdentifiers.add(new BlockIdentifier(rlpData));
}
parsed = true;
}
use of org.ethereum.core.BlockIdentifier in project rskj by rsksmart.
the class NewBlockHashesMessage method encode.
private void encode() {
List<byte[]> encodedElements = new ArrayList<>();
for (BlockIdentifier identifier : blockIdentifiers) {
encodedElements.add(identifier.getEncoded());
}
byte[][] encodedElementArray = encodedElements.toArray(new byte[encodedElements.size()][]);
this.encoded = RLP.encodeList(encodedElementArray);
}
use of org.ethereum.core.BlockIdentifier 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 org.ethereum.core.BlockIdentifier 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;
}
Aggregations