use of org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage 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.ReceiptsMessage in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetReceiptsWithinLimits.
@Test
public void respondToGetReceiptsWithinLimits() throws ExecutionException, InterruptedException {
final CompletableFuture<Void> done = new CompletableFuture<>();
final int limit = 5;
try (final EthProtocolManager ethManager = EthProtocolManagerTestUtil.create(blockchain, () -> false, protocolContext.getWorldStateArchive(), transactionPool, new EthProtocolConfiguration(limit, limit, limit, limit, limit, false))) {
// Setup blocks query
final int blockCount = 10;
final long startBlock = blockchain.getChainHeadBlockNumber() - blockCount;
final List<List<TransactionReceipt>> expectedReceipts = new ArrayList<>(blockCount);
final List<Hash> blockHashes = new ArrayList<>(blockCount);
for (int i = 0; i < blockCount; i++) {
final BlockHeader header = blockchain.getBlockHeader(startBlock + i).get();
expectedReceipts.add(blockchain.getTxReceipts(header.getHash()).get());
blockHashes.add(header.getHash());
}
final MessageData messageData = GetReceiptsMessage.create(blockHashes);
// 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.RECEIPTS);
final ReceiptsMessage receiptsMessage = ReceiptsMessage.readFrom(message);
final List<List<TransactionReceipt>> receipts = Lists.newArrayList(receiptsMessage.receipts());
assertThat(receipts).hasSize(limit);
for (int i = 0; i < limit; i++) {
assertThat(expectedReceipts.get(i)).isEqualTo(receipts.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.ReceiptsMessage in project besu by hyperledger.
the class GetReceiptsFromPeerTask method processResponse.
@Override
protected Optional<Map<BlockHeader, List<TransactionReceipt>>> processResponse(final boolean streamClosed, final MessageData message, final EthPeer peer) {
if (streamClosed) {
// All outstanding requests have been responded to and we still haven't found the response
// we wanted. It must have been empty or contain data that didn't match.
peer.recordUselessResponse("receipts");
return Optional.of(emptyMap());
}
final ReceiptsMessage receiptsMessage = ReceiptsMessage.readFrom(message);
final List<List<TransactionReceipt>> receiptsByBlock = receiptsMessage.receipts();
if (receiptsByBlock.isEmpty()) {
return Optional.empty();
} else if (receiptsByBlock.size() > blockHeaders.size()) {
return Optional.empty();
}
final Map<BlockHeader, List<TransactionReceipt>> receiptsByHeader = new HashMap<>();
for (final List<TransactionReceipt> receiptsInBlock : receiptsByBlock) {
final List<BlockHeader> blockHeaders = headersByReceiptsRoot.get(receiptsRoot(receiptsInBlock));
if (blockHeaders == null) {
// Contains receipts that we didn't request, so mustn't be the response we're looking for.
return Optional.empty();
}
blockHeaders.forEach(header -> receiptsByHeader.put(header, receiptsInBlock));
}
return Optional.of(receiptsByHeader);
}
use of org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetReceiptsPartial.
@Test
public void respondToGetReceiptsPartial() throws ExecutionException, InterruptedException {
final CompletableFuture<Void> done = new CompletableFuture<>();
try (final EthProtocolManager ethManager = EthProtocolManagerTestUtil.create(blockchain, () -> false, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig())) {
// Setup blocks query
final long blockNumber = blockchain.getChainHeadBlockNumber() - 5;
final BlockHeader header = blockchain.getBlockHeader(blockNumber).get();
final List<TransactionReceipt> expectedReceipts = blockchain.getTxReceipts(header.getHash()).get();
final Hash blockHash = header.getHash();
final MessageData messageData = GetReceiptsMessage.create(Arrays.asList(gen.hash(), blockHash, gen.hash()));
// 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.RECEIPTS);
final ReceiptsMessage receiptsMessage = ReceiptsMessage.readFrom(message);
final List<List<TransactionReceipt>> receipts = Lists.newArrayList(receiptsMessage.receipts());
assertThat(receipts).hasSize(1);
assertThat(expectedReceipts).isEqualTo(receipts.get(0));
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.ReceiptsMessage in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetReceipts.
@Test
public void respondToGetReceipts() throws ExecutionException, InterruptedException {
final CompletableFuture<Void> done = new CompletableFuture<>();
try (final EthProtocolManager ethManager = EthProtocolManagerTestUtil.create(blockchain, () -> false, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig())) {
// Setup blocks query
final long startBlock = blockchain.getChainHeadBlockNumber() - 5;
final int blockCount = 2;
final List<List<TransactionReceipt>> expectedReceipts = new ArrayList<>(blockCount);
final List<Hash> blockHashes = new ArrayList<>(blockCount);
for (int i = 0; i < blockCount; i++) {
final BlockHeader header = blockchain.getBlockHeader(startBlock + i).get();
expectedReceipts.add(blockchain.getTxReceipts(header.getHash()).get());
blockHashes.add(header.getHash());
}
final MessageData messageData = GetReceiptsMessage.create(blockHashes);
// 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.RECEIPTS);
final ReceiptsMessage receiptsMessage = ReceiptsMessage.readFrom(message);
final List<List<TransactionReceipt>> receipts = Lists.newArrayList(receiptsMessage.receipts());
assertThat(receipts).hasSize(blockCount);
for (int i = 0; i < blockCount; i++) {
assertThat(expectedReceipts.get(i)).isEqualTo(receipts.get(i));
}
done.complete(null);
};
// Run test
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData));
done.get();
}
}
Aggregations