Skip to content

Commit

Permalink
#2127 - instead of having a separate counter for batch ids, take the…
Browse files Browse the repository at this point in the history
… one used for requests

Signed-off-by: Dmitry Shohov <[email protected]>
  • Loading branch information
Dmitry Shohov authored and Shohou committed Dec 17, 2024
1 parent 6e553a5 commit a0dcc69
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/org/web3j/protocol/core/DefaultIdProvider.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 1 addition & 4 deletions core/src/main/java/org/web3j/protocol/core/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
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;

import org.web3j.protocol.Web3jService;

public class Request<S, T extends Response> {
private static AtomicLong nextId = new AtomicLong(0);

private String jsonrpc = "2.0";
private String method;
private List<S> params;
Expand All @@ -41,7 +38,7 @@ public Request() {}
public Request(String method, List<S> params, Web3jService web3jService, Class<T> type) {
this.method = method;
this.params = params;
this.id = nextId.getAndIncrement();
this.id = DefaultIdProvider.getNextId();
this.web3jService = web3jService;
this.responseType = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -223,7 +221,7 @@ public CompletableFuture<BatchResponse> sendBatchAsync(BatchRequest requests) {
CompletableFuture<BatchResponse> result = new CompletableFuture<>();

// replace first batch elements's id to handle response
long requestId = nextBatchId.getAndIncrement();
long requestId = DefaultIdProvider.getNextId();
Request<?, ? extends Response<?>> firstRequest = requests.getRequests().get(0);
long originId = firstRequest.getId();
requests.getRequests().get(0).setId(requestId);
Expand Down

0 comments on commit a0dcc69

Please sign in to comment.