From 21d719bc3de4d9e548f9cf66c12b60463e40ab13 Mon Sep 17 00:00:00 2001 From: Dmitry Shohov Date: Mon, 16 Dec 2024 14:00:11 +0200 Subject: [PATCH] #2127 - instead of having a separate counter for batch ids, take the one used for requests --- CHANGELOG.md | 1 + .../protocol/core/DefaultIdProvider.java | 26 +++++++++++++++++++ .../java/org/web3j/protocol/core/Request.java | 5 +--- .../protocol/websocket/WebSocketService.java | 6 ++--- 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 core/src/main/java/org/web3j/protocol/core/DefaultIdProvider.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 48a9672834..27ec55bb25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline * fixed several code issues found by sonar [#2113](https://github.com/hyperledger/web3j/pull/2113) * update GitHub actions versions [#2114](https://github.com/hyperledger/web3j/pull/2114) +* fixed subscription id conflict [#2127](https://github.com/hyperledger/web3j/pull/2127) ### Features diff --git a/core/src/main/java/org/web3j/protocol/core/DefaultIdProvider.java b/core/src/main/java/org/web3j/protocol/core/DefaultIdProvider.java new file mode 100644 index 0000000000..ebdd926cd9 --- /dev/null +++ b/core/src/main/java/org/web3j/protocol/core/DefaultIdProvider.java @@ -0,0 +1,26 @@ +/* + * Copyright 2020 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.protocol.core; + +import java.util.concurrent.atomic.AtomicLong; + +public class DefaultIdProvider { + private static final AtomicLong nextId = new AtomicLong(0); + + protected DefaultIdProvider() { + } + + public static long getNextId() { + return nextId.getAndIncrement(); + } +} diff --git a/core/src/main/java/org/web3j/protocol/core/Request.java b/core/src/main/java/org/web3j/protocol/core/Request.java index bd003df3da..644a3a62da 100644 --- a/core/src/main/java/org/web3j/protocol/core/Request.java +++ b/core/src/main/java/org/web3j/protocol/core/Request.java @@ -15,7 +15,6 @@ import java.io.IOException; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicLong; import com.fasterxml.jackson.annotation.JsonIgnore; import io.reactivex.Flowable; @@ -23,8 +22,6 @@ import org.web3j.protocol.Web3jService; public class Request { - private static AtomicLong nextId = new AtomicLong(0); - private String jsonrpc = "2.0"; private String method; private List params; @@ -41,7 +38,7 @@ public Request() {} public Request(String method, List params, Web3jService web3jService, Class type) { this.method = method; this.params = params; - this.id = nextId.getAndIncrement(); + this.id = DefaultIdProvider.getNextId(); this.web3jService = web3jService; this.responseType = type; } diff --git a/core/src/main/java/org/web3j/protocol/websocket/WebSocketService.java b/core/src/main/java/org/web3j/protocol/websocket/WebSocketService.java index 8012cca728..1d4a8b609a 100644 --- a/core/src/main/java/org/web3j/protocol/websocket/WebSocketService.java +++ b/core/src/main/java/org/web3j/protocol/websocket/WebSocketService.java @@ -26,7 +26,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; import com.fasterxml.jackson.core.JsonProcessingException; @@ -44,6 +43,7 @@ import org.web3j.protocol.Web3jService; import org.web3j.protocol.core.BatchRequest; import org.web3j.protocol.core.BatchResponse; +import org.web3j.protocol.core.DefaultIdProvider; import org.web3j.protocol.core.Request; import org.web3j.protocol.core.Response; import org.web3j.protocol.core.methods.response.EthSubscribe; @@ -65,8 +65,6 @@ public class WebSocketService implements Web3jService { // Timeout for JSON-RPC requests static final long REQUEST_TIMEOUT = 60; - // replaced batch's next id - static final AtomicLong nextBatchId = new AtomicLong(0); // WebSocket client private final WebSocketClient webSocketClient; @@ -223,7 +221,7 @@ public CompletableFuture sendBatchAsync(BatchRequest requests) { CompletableFuture result = new CompletableFuture<>(); // replace first batch elements's id to handle response - long requestId = nextBatchId.getAndIncrement(); + long requestId = DefaultIdProvider.getNextId(); Request> firstRequest = requests.getRequests().get(0); long originId = firstRequest.getId(); requests.getRequests().get(0).setId(requestId);