|
23 | 23 | import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor; |
24 | 24 | import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; |
25 | 25 | import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; |
| 26 | +import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketMethodsFactory; |
| 27 | +import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionManager; |
| 28 | +import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; |
26 | 29 |
|
27 | 30 | import java.nio.file.Path; |
28 | 31 | import java.util.Arrays; |
29 | 32 | import java.util.Collections; |
| 33 | +import java.util.HashMap; |
30 | 34 | import java.util.Map; |
| 35 | +import java.util.Optional; |
31 | 36 | import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
32 | 38 |
|
33 | 39 | import io.vertx.core.Vertx; |
34 | 40 | import io.vertx.core.VertxOptions; |
@@ -259,4 +265,156 @@ private void assertSocketCall( |
259 | 265 | })) |
260 | 266 | .write(request))))); |
261 | 267 | } |
| 268 | + |
| 269 | + @Test |
| 270 | + void subscriptionRequestSuccessful() { |
| 271 | + final Path socketPath = tempDir.resolve("besu-test.ipc"); |
| 272 | + final SubscriptionManager subscriptionManager = |
| 273 | + new SubscriptionManager(new NoOpMetricsSystem()); |
| 274 | + vertx.deployVerticle(subscriptionManager); |
| 275 | + |
| 276 | + final Map<String, JsonRpcMethod> methods = |
| 277 | + new WebSocketMethodsFactory(subscriptionManager, new HashMap<>()).methods(); |
| 278 | + |
| 279 | + final JsonRpcIpcService service = |
| 280 | + new JsonRpcIpcService( |
| 281 | + vertx, |
| 282 | + socketPath, |
| 283 | + new JsonRpcExecutor(new BaseJsonRpcProcessor(), methods), |
| 284 | + Optional.of(subscriptionManager)); |
| 285 | + |
| 286 | + final String request = "{\"id\":1,\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"]}\n"; |
| 287 | + final String expectedResponse = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x1\"}"; |
| 288 | + |
| 289 | + service |
| 290 | + .start() |
| 291 | + .onComplete( |
| 292 | + testContext.succeeding( |
| 293 | + server -> |
| 294 | + vertx |
| 295 | + .createNetClient() |
| 296 | + .connect(SocketAddress.domainSocketAddress(socketPath.toString())) |
| 297 | + .onComplete( |
| 298 | + testContext.succeeding( |
| 299 | + socket -> |
| 300 | + socket |
| 301 | + .handler( |
| 302 | + buffer -> |
| 303 | + testContext.verify( |
| 304 | + () -> { |
| 305 | + assertThat(buffer.toString().trim()) |
| 306 | + .isEqualTo(expectedResponse); |
| 307 | + service |
| 308 | + .stop() |
| 309 | + .onComplete( |
| 310 | + testContext.succeedingThenComplete()); |
| 311 | + })) |
| 312 | + .write(Buffer.buffer(request)))))); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + void unsubscribeRequestSuccessful() { |
| 317 | + final Path socketPath = tempDir.resolve("besu-test.ipc"); |
| 318 | + final SubscriptionManager subscriptionManager = |
| 319 | + new SubscriptionManager(new NoOpMetricsSystem()); |
| 320 | + vertx.deployVerticle(subscriptionManager); |
| 321 | + |
| 322 | + final Map<String, JsonRpcMethod> methods = |
| 323 | + new WebSocketMethodsFactory(subscriptionManager, new HashMap<>()).methods(); |
| 324 | + |
| 325 | + final JsonRpcIpcService service = |
| 326 | + new JsonRpcIpcService( |
| 327 | + vertx, |
| 328 | + socketPath, |
| 329 | + new JsonRpcExecutor(new BaseJsonRpcProcessor(), methods), |
| 330 | + Optional.of(subscriptionManager)); |
| 331 | + |
| 332 | + final String subscribeRequest = |
| 333 | + "{\"id\":1,\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"]}\n"; |
| 334 | + final String unsubscribeRequest = |
| 335 | + "{\"id\":2,\"method\":\"eth_unsubscribe\",\"params\":[\"0x1\"]}\n"; |
| 336 | + final AtomicInteger messageCount = new AtomicInteger(0); |
| 337 | + |
| 338 | + service |
| 339 | + .start() |
| 340 | + .onComplete( |
| 341 | + testContext.succeeding( |
| 342 | + server -> |
| 343 | + vertx |
| 344 | + .createNetClient() |
| 345 | + .connect(SocketAddress.domainSocketAddress(socketPath.toString())) |
| 346 | + .onComplete( |
| 347 | + testContext.succeeding( |
| 348 | + socket -> { |
| 349 | + socket.handler( |
| 350 | + buffer -> { |
| 351 | + final int count = messageCount.incrementAndGet(); |
| 352 | + if (count == 1) { |
| 353 | + // First response is subscribe |
| 354 | + socket.write(Buffer.buffer(unsubscribeRequest)); |
| 355 | + } else if (count == 2) { |
| 356 | + // Second response is unsubscribe |
| 357 | + testContext.verify( |
| 358 | + () -> { |
| 359 | + assertThat(buffer.toString().trim()) |
| 360 | + .contains("\"result\":true"); |
| 361 | + service |
| 362 | + .stop() |
| 363 | + .onComplete( |
| 364 | + testContext.succeedingThenComplete()); |
| 365 | + }); |
| 366 | + } |
| 367 | + }); |
| 368 | + socket.write(Buffer.buffer(subscribeRequest)); |
| 369 | + })))); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + void batchRequestDoesNotSupportSubscriptions() { |
| 374 | + final Path socketPath = tempDir.resolve("besu-test.ipc"); |
| 375 | + final SubscriptionManager subscriptionManager = |
| 376 | + new SubscriptionManager(new NoOpMetricsSystem()); |
| 377 | + vertx.deployVerticle(subscriptionManager); |
| 378 | + |
| 379 | + final Map<String, JsonRpcMethod> methods = |
| 380 | + new WebSocketMethodsFactory(subscriptionManager, new HashMap<>()).methods(); |
| 381 | + |
| 382 | + final JsonRpcIpcService service = |
| 383 | + new JsonRpcIpcService( |
| 384 | + vertx, |
| 385 | + socketPath, |
| 386 | + new JsonRpcExecutor(new BaseJsonRpcProcessor(), methods), |
| 387 | + Optional.of(subscriptionManager)); |
| 388 | + |
| 389 | + final String batchRequest = |
| 390 | + "[{\"id\":1,\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"]}," |
| 391 | + + "{\"id\":2,\"method\":\"eth_subscribe\",\"params\":[\"logs\"]}]"; |
| 392 | + |
| 393 | + service |
| 394 | + .start() |
| 395 | + .onComplete( |
| 396 | + testContext.succeeding( |
| 397 | + server -> |
| 398 | + vertx |
| 399 | + .createNetClient() |
| 400 | + .connect(SocketAddress.domainSocketAddress(socketPath.toString())) |
| 401 | + .onComplete( |
| 402 | + testContext.succeeding( |
| 403 | + socket -> |
| 404 | + socket |
| 405 | + .handler( |
| 406 | + buffer -> |
| 407 | + testContext.verify( |
| 408 | + () -> { |
| 409 | + // Batch requests with subscriptions should |
| 410 | + // fail |
| 411 | + assertThat(buffer.toString()) |
| 412 | + .contains("\"error\""); |
| 413 | + service |
| 414 | + .stop() |
| 415 | + .onComplete( |
| 416 | + testContext.succeedingThenComplete()); |
| 417 | + })) |
| 418 | + .write(Buffer.buffer(batchRequest)))))); |
| 419 | + } |
262 | 420 | } |
0 commit comments