|
| 1 | +/* |
| 2 | + * Copyright contributors to Hyperledger Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.p2p.network; |
| 16 | + |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 19 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 20 | +import static org.mockito.ArgumentMatchers.anyString; |
| 21 | +import static org.mockito.ArgumentMatchers.eq; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import org.hyperledger.besu.ethereum.p2p.peers.Peer; |
| 28 | +import org.hyperledger.besu.ethereum.p2p.rlpx.MessageCallback; |
| 29 | +import org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection; |
| 30 | +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; |
| 31 | +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage; |
| 32 | +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Message; |
| 33 | +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; |
| 34 | +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol; |
| 35 | +import org.hyperledger.besu.plugin.services.MetricsSystem; |
| 36 | +import org.hyperledger.besu.plugin.services.metrics.Counter; |
| 37 | +import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.function.BiFunction; |
| 41 | + |
| 42 | +import org.junit.jupiter.api.BeforeEach; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | +import org.mockito.ArgumentCaptor; |
| 45 | + |
| 46 | +public class NetworkRunnerTest { |
| 47 | + |
| 48 | + private NetworkRunner networkRunner; |
| 49 | + private P2PNetwork network; |
| 50 | + private LabelledMetric<Counter> inboundMessageCounter; |
| 51 | + private LabelledMetric<Counter> inboundBytesCounter; |
| 52 | + private Counter messageCounter; |
| 53 | + private Counter bytesCounter; |
| 54 | + private SubProtocol subProtocol; |
| 55 | + private ProtocolManager protocolManager; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + public void setUp() { |
| 60 | + network = mock(P2PNetwork.class); |
| 61 | + subProtocol = mock(SubProtocol.class); |
| 62 | + protocolManager = mock(ProtocolManager.class); |
| 63 | + |
| 64 | + inboundMessageCounter = mock(LabelledMetric.class); |
| 65 | + inboundBytesCounter = mock(LabelledMetric.class); |
| 66 | + messageCounter = mock(Counter.class); |
| 67 | + bytesCounter = mock(Counter.class); |
| 68 | + |
| 69 | + MetricsSystem metricsSystem = mock(MetricsSystem.class); |
| 70 | + when(metricsSystem.createLabelledCounter( |
| 71 | + any(), eq("p2p_messages_inbound"), any(), any(), any(), any())) |
| 72 | + .thenReturn(inboundMessageCounter); |
| 73 | + when(metricsSystem.createLabelledCounter( |
| 74 | + any(), eq("p2p_bytes_inbound"), any(), any(), any(), any())) |
| 75 | + .thenReturn(inboundBytesCounter); |
| 76 | + |
| 77 | + when(inboundMessageCounter.labels(anyString(), anyString(), anyString())) |
| 78 | + .thenReturn(messageCounter); |
| 79 | + when(inboundBytesCounter.labels(anyString(), anyString(), anyString())) |
| 80 | + .thenReturn(bytesCounter); |
| 81 | + |
| 82 | + // Setup subProtocol to return "eth" as its name so it can be looked up |
| 83 | + when(subProtocol.getName()).thenReturn("eth"); |
| 84 | + |
| 85 | + // Setup network mocks to allow start() to complete |
| 86 | + when(network.isListening()).thenReturn(true); |
| 87 | + |
| 88 | + BiFunction<Peer, Boolean, Boolean> ethPeersShouldConnect = (peer, incoming) -> true; |
| 89 | + |
| 90 | + NetworkRunner.NetworkBuilder networkBuilder = caps -> network; |
| 91 | + |
| 92 | + networkRunner = |
| 93 | + NetworkRunner.builder() |
| 94 | + .protocolManagers(List.of(protocolManager)) |
| 95 | + .subProtocols(subProtocol) |
| 96 | + .network(networkBuilder) |
| 97 | + .metricsSystem(metricsSystem) |
| 98 | + .ethPeersShouldConnect(ethPeersShouldConnect) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void shouldIncrementInboundBytesCounterWhenProcessingMessage() { |
| 104 | + // Setup |
| 105 | + Capability capability = Capability.create("eth", 68); |
| 106 | + int messageCode = 1; |
| 107 | + int messageSize = 1024; |
| 108 | + |
| 109 | + when(subProtocol.getName()).thenReturn("eth"); |
| 110 | + when(subProtocol.isValidMessageCode(anyInt(), eq(messageCode))).thenReturn(true); |
| 111 | + when(subProtocol.messageName(anyInt(), eq(messageCode))).thenReturn("Status"); |
| 112 | + when(protocolManager.getSupportedCapabilities()).thenReturn(List.of(capability)); |
| 113 | + |
| 114 | + // Start network runner to register handlers |
| 115 | + networkRunner.start(); |
| 116 | + |
| 117 | + // Capture the message handler that was registered |
| 118 | + ArgumentCaptor<MessageCallback> handlerCaptor = ArgumentCaptor.forClass(MessageCallback.class); |
| 119 | + verify(network).subscribe(eq(capability), handlerCaptor.capture()); |
| 120 | + |
| 121 | + MessageCallback handler = handlerCaptor.getValue(); |
| 122 | + |
| 123 | + // Create test message |
| 124 | + PeerConnection peerConnection = mock(PeerConnection.class); |
| 125 | + MessageData messageData = mock(MessageData.class); |
| 126 | + when(messageData.getSize()).thenReturn(messageSize); |
| 127 | + when(messageData.getCode()).thenReturn(messageCode); |
| 128 | + |
| 129 | + Message message = new DefaultMessage(peerConnection, messageData); |
| 130 | + |
| 131 | + // Process message through the handler |
| 132 | + handler.onMessage(capability, message); |
| 133 | + |
| 134 | + // Verify bytes counter was incremented with correct labels and size |
| 135 | + verify(inboundBytesCounter).labels(eq(capability.toString()), eq("Status"), eq("1")); |
| 136 | + verify(bytesCounter).inc(messageSize); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void shouldIncrementInboundBytesCounterForMultipleMessages() { |
| 141 | + // Setup |
| 142 | + Capability capability = Capability.create("eth", 68); |
| 143 | + int messageCode1 = 1; |
| 144 | + int messageCode2 = 2; |
| 145 | + int messageSize1 = 512; |
| 146 | + int messageSize2 = 2048; |
| 147 | + |
| 148 | + when(subProtocol.getName()).thenReturn("eth"); |
| 149 | + when(subProtocol.isValidMessageCode(anyInt(), anyInt())).thenReturn(true); |
| 150 | + when(subProtocol.messageName(anyInt(), eq(messageCode1))).thenReturn("Status"); |
| 151 | + when(subProtocol.messageName(anyInt(), eq(messageCode2))).thenReturn("GetBlockHeaders"); |
| 152 | + when(protocolManager.getSupportedCapabilities()).thenReturn(List.of(capability)); |
| 153 | + |
| 154 | + // Start network runner to register handlers |
| 155 | + networkRunner.start(); |
| 156 | + |
| 157 | + // Capture the message handler |
| 158 | + ArgumentCaptor<MessageCallback> handlerCaptor = ArgumentCaptor.forClass(MessageCallback.class); |
| 159 | + verify(network).subscribe(eq(capability), handlerCaptor.capture()); |
| 160 | + |
| 161 | + MessageCallback handler = handlerCaptor.getValue(); |
| 162 | + |
| 163 | + // Create first test message |
| 164 | + PeerConnection peerConnection = mock(PeerConnection.class); |
| 165 | + MessageData messageData1 = mock(MessageData.class); |
| 166 | + when(messageData1.getSize()).thenReturn(messageSize1); |
| 167 | + when(messageData1.getCode()).thenReturn(messageCode1); |
| 168 | + Message message1 = new DefaultMessage(peerConnection, messageData1); |
| 169 | + |
| 170 | + // Create second test message |
| 171 | + MessageData messageData2 = mock(MessageData.class); |
| 172 | + when(messageData2.getSize()).thenReturn(messageSize2); |
| 173 | + when(messageData2.getCode()).thenReturn(messageCode2); |
| 174 | + Message message2 = new DefaultMessage(peerConnection, messageData2); |
| 175 | + |
| 176 | + // Process both messages |
| 177 | + handler.onMessage(capability, message1); |
| 178 | + handler.onMessage(capability, message2); |
| 179 | + |
| 180 | + // Verify bytes counter was incremented for both messages |
| 181 | + verify(bytesCounter).inc(messageSize1); |
| 182 | + verify(bytesCounter).inc(messageSize2); |
| 183 | + verify(bytesCounter, times(2)).inc(anyLong()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void shouldUseCorrectLabelsForInboundBytesCounter() { |
| 188 | + // Setup |
| 189 | + Capability capability = Capability.create("eth", 68); |
| 190 | + int messageCode = 5; |
| 191 | + int messageSize = 256; |
| 192 | + String messageName = "NewBlock"; |
| 193 | + |
| 194 | + when(subProtocol.getName()).thenReturn("eth"); |
| 195 | + when(subProtocol.isValidMessageCode(anyInt(), eq(messageCode))).thenReturn(true); |
| 196 | + when(subProtocol.messageName(anyInt(), eq(messageCode))).thenReturn(messageName); |
| 197 | + when(protocolManager.getSupportedCapabilities()).thenReturn(List.of(capability)); |
| 198 | + |
| 199 | + // Start network runner to register handlers |
| 200 | + networkRunner.start(); |
| 201 | + |
| 202 | + // Capture the message handler |
| 203 | + ArgumentCaptor<MessageCallback> handlerCaptor = ArgumentCaptor.forClass(MessageCallback.class); |
| 204 | + verify(network).subscribe(eq(capability), handlerCaptor.capture()); |
| 205 | + |
| 206 | + MessageCallback handler = handlerCaptor.getValue(); |
| 207 | + |
| 208 | + // Create test message |
| 209 | + PeerConnection peerConnection = mock(PeerConnection.class); |
| 210 | + MessageData messageData = mock(MessageData.class); |
| 211 | + when(messageData.getSize()).thenReturn(messageSize); |
| 212 | + when(messageData.getCode()).thenReturn(messageCode); |
| 213 | + Message message = new DefaultMessage(peerConnection, messageData); |
| 214 | + |
| 215 | + // Process message |
| 216 | + handler.onMessage(capability, message); |
| 217 | + |
| 218 | + // Verify correct labels were used (protocol, message name, code) |
| 219 | + verify(inboundBytesCounter) |
| 220 | + .labels(eq(capability.toString()), eq(messageName), eq(String.valueOf(messageCode))); |
| 221 | + verify(bytesCounter).inc(messageSize); |
| 222 | + } |
| 223 | +} |
0 commit comments