use of org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler in project besu by hyperledger.
the class Istanbul99ProtocolManagerTest method respondToEth65GetHeadersUsingIstanbul99.
@Test
public void respondToEth65GetHeadersUsingIstanbul99() throws ExecutionException, InterruptedException, TimeoutException {
final CompletableFuture<Void> done = new CompletableFuture<>();
final EthScheduler ethScheduler = new DeterministicEthScheduler(() -> false);
EthPeers peers = new EthPeers(Istanbul99Protocol.NAME, TestClock.fixed(), new NoOpMetricsSystem(), 25);
EthMessages messages = new EthMessages();
final BigInteger networkId = BigInteger.ONE;
try (final EthProtocolManager ethManager = new Istanbul99ProtocolManager(blockchain, networkId, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig(), peers, messages, new EthContext(peers, messages, ethScheduler), Collections.emptyList(), false, ethScheduler)) {
final long startBlock = blockchain.getChainHeadBlockNumber() + 1;
final int blockCount = 5;
final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
assertThat(headers.size()).isEqualTo(0);
done.complete(null);
};
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(Istanbul99Protocol.ISTANBUL99, new DefaultMessage(peer, messageData));
done.get(10, TimeUnit.SECONDS);
}
}
use of org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetHeaders.
@Test
public void respondToGetHeaders() throws ExecutionException, InterruptedException {
final CompletableFuture<Void> done = new CompletableFuture<>();
try (final EthProtocolManager ethManager = EthProtocolManagerTestUtil.create(blockchain, () -> false, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig())) {
final long startBlock = 5L;
final int blockCount = 5;
final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
assertThat(headers).hasSize(blockCount);
for (int i = 0; i < blockCount; i++) {
assertThat(headers.get(i).getNumber()).isEqualTo(startBlock + i);
}
done.complete(null);
};
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData));
done.get();
}
}
use of org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetHeadersWithinLimits.
@Test
public void respondToGetHeadersWithinLimits() 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))) {
final long startBlock = 5L;
final int blockCount = 10;
final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
assertThat(headers).hasSize(limit);
for (int i = 0; i < limit; i++) {
assertThat(headers.get(i).getNumber()).isEqualTo(startBlock + i);
}
done.complete(null);
};
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData));
done.get();
}
}
use of org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler in project besu by hyperledger.
the class EthProtocolManagerTest method respondToGetBodiesPartial.
@Test
public void respondToGetBodiesPartial() 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 expectedBlockNumber = blockchain.getChainHeadBlockNumber() - 1;
final BlockHeader header = blockchain.getBlockHeader(expectedBlockNumber).get();
final BlockBody body = blockchain.getBlockBody(header.getHash()).get();
final Block expectedBlock = new Block(header, body);
final List<Hash> hashes = Arrays.asList(gen.hash(), expectedBlock.getHash(), gen.hash());
final MessageData messageData = GetBlockBodiesMessage.create(hashes);
// Define handler to validate response
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_BODIES);
final BlockBodiesMessage blocksMessage = BlockBodiesMessage.readFrom(message);
final List<BlockBody> bodies = Lists.newArrayList(blocksMessage.bodies(protocolSchedule));
assertThat(bodies).hasSize(1);
assertThat(expectedBlock.getBody()).isEqualTo(bodies.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.manager.MockPeerConnection.PeerSendHandler 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();
}
}
Aggregations