use of org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetNodeData.
@Test
public void respondToGetNodeData() throws Exception {
final CompletableFuture<Void> done = new CompletableFuture<>();
final WorldStateArchive worldStateArchive = protocolContext.getWorldStateArchive();
try (final EthProtocolManager ethManager = EthProtocolManagerTestUtil.create(blockchain, () -> false, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig())) {
// Setup node data query
final List<Bytes> expectedResults = new ArrayList<>();
final List<Hash> requestedHashes = new ArrayList<>();
final long startBlock = blockchain.getChainHeadBlockNumber() - 5;
final int blockCount = 2;
for (int i = 0; i < blockCount; i++) {
final BlockHeader header = blockchain.getBlockHeader(startBlock + i).get();
requestedHashes.add(header.getStateRoot());
expectedResults.add(worldStateArchive.getNodeData(header.getStateRoot()).get());
}
final MessageData messageData = GetNodeDataMessage.create(requestedHashes);
// Define handler to validate response
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV63.NODE_DATA);
final NodeDataMessage receiptsMessage = NodeDataMessage.readFrom(message);
final List<Bytes> nodeData = receiptsMessage.nodeData();
assertThat(nodeData).hasSize(blockCount);
for (int i = 0; i < blockCount; i++) {
assertThat(expectedResults.get(i)).isEqualTo(nodeData.get(i));
}
done.complete(null);
};
// Run test
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData));
done.get();
}
}
use of org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage in project besu by hyperledger.
the class GetNodeDataFromPeerTask method processResponse.
@Override
protected Optional<Map<Hash, Bytes>> processResponse(final boolean streamClosed, final MessageData message, final EthPeer peer) {
if (streamClosed) {
// the data we're requesting.
return Optional.of(emptyMap());
}
final NodeDataMessage nodeDataMessage = NodeDataMessage.readFrom(message);
final List<Bytes> nodeData = nodeDataMessage.nodeData();
if (nodeData.size() > hashes.size()) {
// Can't be the response to our request
return Optional.empty();
}
return mapNodeDataByHash(nodeData);
}
Aggregations