Search in sources :

Example 1 with NodeDataMessage

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();
    }
}
Also used : TransactionsMessage(org.hyperledger.besu.ethereum.eth.messages.TransactionsMessage) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) ReceiptsMessage(org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) BigInteger(java.math.BigInteger) Block(org.hyperledger.besu.ethereum.core.Block) GetNodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.GetNodeDataMessage) ConditionFactory(org.awaitility.core.ConditionFactory) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) Set(java.util.Set) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) Collectors(java.util.stream.Collectors) BlockBodiesMessage(org.hyperledger.besu.ethereum.eth.messages.BlockBodiesMessage) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) List(java.util.List) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) ProtocolScheduleFixture(org.hyperledger.besu.ethereum.core.ProtocolScheduleFixture) Optional(java.util.Optional) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) TransactionPoolFactory(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolFactory) Awaitility(org.awaitility.Awaitility) Mockito.mock(org.mockito.Mockito.mock) Hash(org.hyperledger.besu.datatypes.Hash) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StatusMessage(org.hyperledger.besu.ethereum.eth.messages.StatusMessage) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) BeforeClass(org.junit.BeforeClass) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) ArrayList(java.util.ArrayList) NodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) HashSet(java.util.HashSet) InMemoryKeyValueStorageProvider.createInMemoryBlockchain(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Wei(org.hyperledger.besu.datatypes.Wei) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) ExecutorService(java.util.concurrent.ExecutorService) GetReceiptsMessage(org.hyperledger.besu.ethereum.eth.messages.GetReceiptsMessage) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) NewBlockMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) GetBlockBodiesMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockBodiesMessage) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) EthPV63(org.hyperledger.besu.ethereum.eth.messages.EthPV63) Mockito.reset(org.mockito.Mockito.reset) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) ArrayList(java.util.ArrayList) Hash(org.hyperledger.besu.datatypes.Hash) GetNodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.GetNodeDataMessage) NodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage) Bytes(org.apache.tuweni.bytes.Bytes) CompletableFuture(java.util.concurrent.CompletableFuture) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) List(java.util.List) ArrayList(java.util.ArrayList) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) Test(org.junit.Test)

Example 2 with NodeDataMessage

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);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) NodeDataMessage(org.hyperledger.besu.ethereum.eth.messages.NodeDataMessage)

Aggregations

Bytes (org.apache.tuweni.bytes.Bytes)2 Lists (com.google.common.collect.Lists)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Awaitility (org.awaitility.Awaitility)1 ConditionFactory (org.awaitility.core.ConditionFactory)1