Search in sources :

Example 1 with JsonRpcResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult in project besu by hyperledger.

the class SyncingSubscriptionServiceTest method shouldSendSyncStatusWhenReceiveSyncStatus.

@Test
public void shouldSendSyncStatusWhenReceiveSyncStatus() {
    final SyncingSubscription subscription = new SyncingSubscription(9L, "conn", SubscriptionType.SYNCING);
    final List<SyncingSubscription> subscriptions = Collections.singletonList(subscription);
    final Optional<SyncStatus> syncStatus = Optional.of(new DefaultSyncStatus(0L, 1L, 3L, Optional.empty(), Optional.empty()));
    final JsonRpcResult expectedSyncingResult = new SyncingResult(syncStatus.get());
    doAnswer(invocation -> {
        Consumer<List<SyncingSubscription>> consumer = invocation.getArgument(2);
        consumer.accept(subscriptions);
        return null;
    }).when(subscriptionManager).notifySubscribersOnWorkerThread(any(), any(), any());
    syncStatusListener.onSyncStatusChanged(syncStatus);
    verify(subscriptionManager).sendMessage(ArgumentMatchers.eq(subscription.getSubscriptionId()), eq(expectedSyncingResult));
}
Also used : SyncingResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.SyncingResult) DefaultSyncStatus(org.hyperledger.besu.ethereum.core.DefaultSyncStatus) SyncStatus(org.hyperledger.besu.plugin.data.SyncStatus) DefaultSyncStatus(org.hyperledger.besu.ethereum.core.DefaultSyncStatus) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) List(java.util.List) Test(org.junit.Test)

Example 2 with JsonRpcResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult in project besu by hyperledger.

the class SyncingSubscriptionServiceTest method shouldSendNotSyncingResultWhenReceiveNonSyncingStatus.

@Test
public void shouldSendNotSyncingResultWhenReceiveNonSyncingStatus() {
    final SyncingSubscription subscription = new SyncingSubscription(9L, "conn", SubscriptionType.SYNCING);
    final List<SyncingSubscription> subscriptions = Collections.singletonList(subscription);
    final Optional<SyncStatus> syncStatus = Optional.empty();
    final JsonRpcResult expectedSyncingResult = new NotSynchronisingResult();
    doAnswer(invocation -> {
        Consumer<List<SyncingSubscription>> consumer = invocation.getArgument(2);
        consumer.accept(subscriptions);
        return null;
    }).when(subscriptionManager).notifySubscribersOnWorkerThread(any(), any(), any());
    syncStatusListener.onSyncStatusChanged(syncStatus);
    verify(subscriptionManager).sendMessage(ArgumentMatchers.eq(subscription.getSubscriptionId()), eq(expectedSyncingResult));
}
Also used : SyncStatus(org.hyperledger.besu.plugin.data.SyncStatus) DefaultSyncStatus(org.hyperledger.besu.ethereum.core.DefaultSyncStatus) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) List(java.util.List) Test(org.junit.Test)

Example 3 with JsonRpcResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult in project besu by hyperledger.

the class SubscriptionManagerSendMessageTest method shouldSendMessageOnTheConnectionIdEventBusAddressForExistingSubscription.

@Test
@Ignore
public void shouldSendMessageOnTheConnectionIdEventBusAddressForExistingSubscription(final TestContext context) {
    final String connectionId = UUID.randomUUID().toString();
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, connectionId);
    final JsonRpcResult expectedResult = mock(JsonRpcResult.class);
    final Subscription subscription = new Subscription(1L, connectionId, SubscriptionType.SYNCING, false);
    final SubscriptionResponse expectedResponse = new SubscriptionResponse(subscription, expectedResult);
    final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest);
    final Async async = context.async();
    vertx.eventBus().consumer(connectionId).handler(msg -> {
        context.assertEquals(Json.encode(expectedResponse), msg.body());
        async.complete();
    }).completionHandler(v -> subscriptionManager.sendMessage(subscriptionId, expectedResult));
    async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) SubscriptionType(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscriptionType) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) TestCase.fail(junit.framework.TestCase.fail) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) SubscriptionResponse(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.response.SubscriptionResponse) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Ignore(org.junit.Ignore) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Async(io.vertx.ext.unit.Async) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) SubscriptionResponse(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.response.SubscriptionResponse) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with JsonRpcResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult in project besu by hyperledger.

the class NewBlockHeadersSubscriptionServiceTest method shouldNotSendMessageWhenBlockAddedIsNotOnCanonicalChain.

@Test
public void shouldNotSendMessageWhenBlockAddedIsNotOnCanonicalChain() {
    final NewBlockHeadersSubscription subscription = createSubscription(false);
    mockSubscriptionManagerNotifyMethod(subscription);
    final Block canonicalBlock = appendBlockWithParent(blockchain, genesisBlock);
    final BlockOptions options = new BlockOptions().setBlockNumber(genesisBlock.getHeader().getNumber() + 1).setParentHash(genesisBlock.getHash()).setDifficulty(genesisBlock.getHeader().getDifficulty().divide(100L));
    appendBlockWithParent(blockchain, options);
    final BlockResult expectedNewBlock = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(canonicalBlock.getHash()).orElse(null));
    verify(subscriptionManagerSpy, times(1)).notifySubscribersOnWorkerThread(any(), any(), any());
    verify(subscriptionManagerSpy, times(1)).sendMessage(subscriptionIdCaptor.capture(), responseCaptor.capture());
    assertThat(subscriptionIdCaptor.getValue()).isEqualTo(subscription.getSubscriptionId());
    List<JsonRpcResult> capturedNewBlocks = responseCaptor.getAllValues();
    assertThat(capturedNewBlocks.size()).isEqualTo(1);
    assertThat(capturedNewBlocks.get(0)).usingRecursiveComparison().isEqualTo(expectedNewBlock);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Block(org.hyperledger.besu.ethereum.core.Block) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Test(org.junit.Test)

Example 5 with JsonRpcResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult in project besu by hyperledger.

the class NewBlockHeadersSubscriptionServiceTest method shouldSendMessagesWhenReorgBlockAdded.

@Test
public void shouldSendMessagesWhenReorgBlockAdded() {
    final NewBlockHeadersSubscription subscription = createSubscription(false);
    mockSubscriptionManagerNotifyMethod(subscription);
    final Block canonicalBlock = appendBlockWithParent(blockchain, genesisBlock);
    final BlockOptions options = new BlockOptions().setBlockNumber(genesisBlock.getHeader().getNumber() + 1).setParentHash(genesisBlock.getHash()).setDifficulty(genesisBlock.getHeader().getDifficulty().divide(100L));
    final Block forkBlock = appendBlockWithParent(blockchain, options);
    options.setDifficulty(forkBlock.getHeader().getDifficulty().divide(100L));
    appendBlockWithParent(blockchain, options);
    options.setDifficulty(blockchain.getChainHeadBlock().getHeader().getDifficulty().multiply(2L));
    final Block forkBlock2 = appendBlockWithParent(blockchain, options);
    final BlockResult expectedNewBlock = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(canonicalBlock.getHash()).orElse(null));
    final BlockResult expectedNewBlock1 = blockResultFactory.transactionHash(blockchainQueriesSpy.blockByHashWithTxHashes(forkBlock2.getHash()).orElse(null));
    verify(subscriptionManagerSpy, times(2)).notifySubscribersOnWorkerThread(any(), any(), any());
    verify(subscriptionManagerSpy, times(2)).sendMessage(subscriptionIdCaptor.capture(), responseCaptor.capture());
    assertThat(subscriptionIdCaptor.getValue()).isEqualTo(subscription.getSubscriptionId());
    List<JsonRpcResult> capturedNewBlocks = responseCaptor.getAllValues();
    assertThat(capturedNewBlocks.size()).isEqualTo(2);
    assertThat(capturedNewBlocks.get(0)).usingRecursiveComparison().isEqualTo(expectedNewBlock);
    assertThat(capturedNewBlocks.get(1)).usingRecursiveComparison().isEqualTo(expectedNewBlock1);
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Block(org.hyperledger.besu.ethereum.core.Block) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Test(org.junit.Test)

Aggregations

JsonRpcResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult)6 Test (org.junit.Test)5 List (java.util.List)2 BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)2 SyncingResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.SyncingResult)2 Block (org.hyperledger.besu.ethereum.core.Block)2 BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)2 DefaultSyncStatus (org.hyperledger.besu.ethereum.core.DefaultSyncStatus)2 SyncStatus (org.hyperledger.besu.plugin.data.SyncStatus)2 Vertx (io.vertx.core.Vertx)1 Json (io.vertx.core.json.Json)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 UUID (java.util.UUID)1 TestCase.fail (junit.framework.TestCase.fail)1 SubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest)1 SubscriptionType (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscriptionType)1 SubscriptionResponse (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.response.SubscriptionResponse)1 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)1