Search in sources :

Example 1 with MessageData

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.

the class FramerTest method shouldThrowExceptionWhenFramingMessageTooLong.

@Test
public void shouldThrowExceptionWhenFramingMessageTooLong() {
    final byte[] aes = { 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2 };
    final byte[] mac = { 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2 };
    final byte[] byteArray = new byte[0xFFFFFF];
    new Random().nextBytes(byteArray);
    final MessageData ethMessage = new RawMessage(0x00, Bytes.wrap(byteArray));
    final HandshakeSecrets secrets = new HandshakeSecrets(aes, mac, mac);
    final Framer framer = new Framer(secrets);
    assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> framer.frame(ethMessage, Unpooled.buffer())).withMessageContaining("Message size in excess of maximum length.");
}
Also used : Random(java.util.Random) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) HandshakeSecrets(org.hyperledger.besu.ethereum.p2p.rlpx.handshake.HandshakeSecrets) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 2 with MessageData

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.

the class FramerTest method frameMessage.

@Test
public void frameMessage() throws IOException {
    // This is a circular test.
    // 
    // This test decrypts all messages in the test vectors; it then impersonates the sending end
    // by swapping the ingress and egress MACs and frames the plaintext messages.
    // We then verify if the resulting ciphertexts are equal to our test vectors.
    // 
    final JsonNode td = MAPPER.readTree(FramerTest.class.getResource("/peer1.json"));
    HandshakeSecrets secrets = secretsFrom(td, false);
    Framer framer = new Framer(secrets);
    final JsonNode messages = td.get("messages");
    final List<MessageData> decrypted = stream(messages.spliterator(), false).map(n -> decodeHexDump(n.get("data").asText())).map(Unpooled::wrappedBuffer).map(framer::deframe).collect(toList());
    secrets = secretsFrom(td, true);
    framer = new Framer(secrets);
    for (int i = 0; i < decrypted.size(); i++) {
        final ByteBuf b = Unpooled.buffer();
        framer.frame(decrypted.get(i), b);
        final byte[] enc = new byte[b.readableBytes()];
        b.readBytes(enc);
        final byte[] expected = decodeHexDump(messages.get(i).get("data").asText());
        assertThat(expected).isEqualTo(enc);
    }
}
Also used : ByteBufUtil.decodeHexDump(io.netty.buffer.ByteBufUtil.decodeHexDump) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DisconnectMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) Unpooled(io.netty.buffer.Unpooled) HandshakeSecrets(org.hyperledger.besu.ethereum.p2p.rlpx.handshake.HandshakeSecrets) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Snappy(org.xerial.snappy.Snappy) StreamSupport.stream(java.util.stream.StreamSupport.stream) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) ByteBuf(io.netty.buffer.ByteBuf) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Unpooled.buffer(io.netty.buffer.Unpooled.buffer) JsonNode(com.fasterxml.jackson.databind.JsonNode) Unpooled.wrappedBuffer(io.netty.buffer.Unpooled.wrappedBuffer) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Unpooled(io.netty.buffer.Unpooled) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) JsonNode(com.fasterxml.jackson.databind.JsonNode) HandshakeSecrets(org.hyperledger.besu.ethereum.p2p.rlpx.handshake.HandshakeSecrets) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 3 with MessageData

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.

the class DisconnectMessageTest method readFromWithReason.

@Test
public void readFromWithReason() {
    MessageData messageData = new RawMessage(WireMessageCodes.DISCONNECT, Bytes.fromHexString("0xC103"));
    DisconnectMessage disconnectMessage = DisconnectMessage.readFrom(messageData);
    DisconnectReason reason = disconnectMessage.getReason();
    assertThat(reason).isEqualTo(DisconnectReason.USELESS_PEER);
}
Also used : MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 4 with MessageData

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.

the class DisconnectMessageTest method readFromWithNoReason.

@Test
public void readFromWithNoReason() {
    MessageData messageData = new RawMessage(WireMessageCodes.DISCONNECT, Bytes.fromHexString("0xC180"));
    DisconnectMessage disconnectMessage = DisconnectMessage.readFrom(messageData);
    DisconnectReason reason = disconnectMessage.getReason();
    assertThat(reason).isEqualTo(DisconnectReason.UNKNOWN);
}
Also used : MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 5 with MessageData

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData 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);
    }
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) 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) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) HashSet(java.util.HashSet) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) Lists(com.google.common.collect.Lists) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) BigInteger(java.math.BigInteger) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Set(java.util.Set) Test(org.junit.Test) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) List(java.util.List) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) CompletableFuture(java.util.concurrent.CompletableFuture) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BigInteger(java.math.BigInteger) List(java.util.List) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) Test(org.junit.Test)

Aggregations

MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)97 Test (org.junit.Test)81 RawMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage)58 ArrayList (java.util.ArrayList)41 Bytes (org.apache.tuweni.bytes.Bytes)38 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)36 List (java.util.List)35 Collections (java.util.Collections)32 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 Hash (org.hyperledger.besu.datatypes.Hash)30 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)30 TestClock (org.hyperledger.besu.testutil.TestClock)30 HashSet (java.util.HashSet)25 Set (java.util.Set)25 Collectors (java.util.stream.Collectors)25 DefaultMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage)25 Optional (java.util.Optional)24 Capability (org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability)24 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)24 Mockito.mock (org.mockito.Mockito.mock)24