Skip to content

Commit 6d109a6

Browse files
fix(getBlobs): return null if node is syncing (#9394)
Signed-off-by: Gabriel-Trintinalia <[email protected]>
1 parent 62d92ea commit 6d109a6

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineGetBlobsV1.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
3737

3838
import java.util.Arrays;
39+
import java.util.Collections;
3940
import java.util.List;
4041
import java.util.Optional;
4142
import javax.annotation.Nullable;
@@ -90,12 +91,6 @@ public String getName() {
9091

9192
@Override
9293
public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) {
93-
long timestamp = protocolContext.getBlockchain().getChainHeadHeader().getTimestamp();
94-
ValidationResult<RpcErrorType> forkValidationResult = validateForkSupported(timestamp);
95-
if (!forkValidationResult.isValid()) {
96-
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), forkValidationResult);
97-
}
98-
9994
final VersionedHash[] versionedHashes;
10095
try {
10196
versionedHashes = requestContext.getRequiredParameter(0, VersionedHash[].class);
@@ -111,6 +106,15 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
111106
requestContext.getRequest().getId(),
112107
RpcErrorType.INVALID_ENGINE_GET_BLOBS_TOO_LARGE_REQUEST);
113108
}
109+
if (mergeContext.get().isSyncing()) {
110+
final List<BlobAndProofV1> emptyResults = Collections.nCopies(versionedHashes.length, null);
111+
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), emptyResults);
112+
}
113+
long timestamp = protocolContext.getBlockchain().getChainHeadHeader().getTimestamp();
114+
ValidationResult<RpcErrorType> forkValidationResult = validateForkSupported(timestamp);
115+
if (!forkValidationResult.isValid()) {
116+
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), forkValidationResult);
117+
}
114118

115119
final List<BlobAndProofV1> result = getBlobV1Result(versionedHashes);
116120

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineGetBlobsV2.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,20 @@ public String getName() {
9696

9797
@Override
9898
public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) {
99-
long timestamp = protocolContext.getBlockchain().getChainHeadHeader().getTimestamp();
100-
ValidationResult<RpcErrorType> forkValidationResult = validateForkSupported(timestamp);
101-
if (!forkValidationResult.isValid()) {
102-
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), forkValidationResult);
103-
}
104-
10599
final VersionedHash[] versionedHashes = extractVersionedHashes(requestContext);
106100
if (versionedHashes.length > REQUEST_MAX_VERSIONED_HASHES) {
107101
return new JsonRpcErrorResponse(
108102
requestContext.getRequest().getId(),
109103
RpcErrorType.INVALID_ENGINE_GET_BLOBS_TOO_LARGE_REQUEST);
110104
}
105+
if (mergeContext.get().isSyncing()) {
106+
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), null);
107+
}
108+
long timestamp = protocolContext.getBlockchain().getChainHeadHeader().getTimestamp();
109+
ValidationResult<RpcErrorType> forkValidationResult = validateForkSupported(timestamp);
110+
if (!forkValidationResult.isValid()) {
111+
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), forkValidationResult);
112+
}
111113
requestedCounter.inc(versionedHashes.length);
112114
List<BlobProofBundle> validBundles = new ArrayList<>(versionedHashes.length);
113115
int missingBlobs = 0;

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineGetBlobsV1Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineTestSupport.fromErrorResp;
19+
import static org.mockito.ArgumentMatchers.any;
1920
import static org.mockito.Mockito.spy;
2021
import static org.mockito.Mockito.when;
2122

23+
import org.hyperledger.besu.consensus.merge.MergeContext;
2224
import org.hyperledger.besu.crypto.KeyPair;
2325
import org.hyperledger.besu.crypto.SECPPrivateKey;
2426
import org.hyperledger.besu.crypto.SignatureAlgorithm;
@@ -85,13 +87,17 @@ public class EngineGetBlobsV1Test extends AbstractScheduledApiTest {
8587
@Mock private MutableBlockchain blockchain;
8688
@Mock private TransactionPool transactionPool;
8789
@Mock private BlockHeader blockHeader;
90+
@Mock private MergeContext mergeContext;
8891

8992
private EngineGetBlobsV1 method;
9093

9194
private static final Vertx vertx = Vertx.vertx();
9295

9396
@BeforeEach
9497
public void beforeEach() {
98+
when(mergeContext.isSyncing()).thenReturn(false);
99+
when(protocolContext.safeConsensusContext(any())).thenReturn(Optional.ofNullable(mergeContext));
100+
95101
when(protocolContext.getBlockchain()).thenReturn(blockchain);
96102
when(blockHeader.getTimestamp()).thenReturn(cancunHardfork.milestone());
97103
when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
@@ -255,6 +261,16 @@ void shouldFailWhenOsakaActive() {
255261
.isEqualTo(RpcErrorType.UNSUPPORTED_FORK.getCode());
256262
}
257263

264+
@Test
265+
public void shouldReturnNullWhenSyncing() {
266+
when(mergeContext.isSyncing()).thenReturn(true);
267+
var response = resp(new VersionedHash[] {VERSIONED_HASH_ZERO});
268+
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
269+
final List<BlobAndProofV1> blobAndProofV1s = fromSuccessResp(response);
270+
assertThat(blobAndProofV1s).hasSize(1);
271+
assertThat(blobAndProofV1s.getFirst()).isNull();
272+
}
273+
258274
Transaction createBlobTransaction() {
259275
BlobTestFixture blobTestFixture = new BlobTestFixture();
260276
BlobsWithCommitments bwc = blobTestFixture.createBlobsWithCommitments(6);

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineGetBlobsV2Test.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import static org.hyperledger.besu.datatypes.BlobType.KZG_CELL_PROOFS;
1919
import static org.hyperledger.besu.datatypes.BlobType.KZG_PROOF;
2020
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineTestSupport.fromErrorResp;
21+
import static org.mockito.ArgumentMatchers.any;
2122
import static org.mockito.ArgumentMatchers.anyString;
2223
import static org.mockito.ArgumentMatchers.eq;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.verify;
2526
import static org.mockito.Mockito.verifyNoInteractions;
2627
import static org.mockito.Mockito.when;
2728

29+
import org.hyperledger.besu.consensus.merge.MergeContext;
2830
import org.hyperledger.besu.datatypes.Hash;
2931
import org.hyperledger.besu.datatypes.VersionedHash;
3032
import org.hyperledger.besu.ethereum.ProtocolContext;
@@ -49,6 +51,7 @@
4951

5052
import java.util.Arrays;
5153
import java.util.List;
54+
import java.util.Optional;
5255

5356
import io.vertx.core.Vertx;
5457
import org.junit.jupiter.api.BeforeEach;
@@ -73,11 +76,14 @@ public class EngineGetBlobsV2Test extends AbstractScheduledApiTest {
7376
@Mock Counter hitCounter;
7477
@Mock Counter missCounter;
7578
@Mock ObservableMetricsSystem metricsSystem;
79+
@Mock MergeContext mergeContext;
7680

7781
@BeforeEach
7882
public void setup() {
7983
transactionPool = mock(TransactionPool.class);
8084
ProtocolContext protocolContext = mock(ProtocolContext.class);
85+
when(mergeContext.isSyncing()).thenReturn(false);
86+
when(protocolContext.safeConsensusContext(any())).thenReturn(Optional.ofNullable(mergeContext));
8187
when(protocolContext.getBlockchain()).thenReturn(blockchain);
8288
when(blockHeader.getTimestamp()).thenReturn(osakaHardfork.milestone());
8389
when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
@@ -210,6 +216,19 @@ void shouldSucceedWhenOsakaActive() {
210216
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
211217
}
212218

219+
@Test
220+
public void shouldReturnNullWhenSyncing() {
221+
when(mergeContext.isSyncing()).thenReturn(true);
222+
BlobProofBundle bundle = createBundleAndRegisterToPool();
223+
JsonRpcSuccessResponse response =
224+
getSuccessResponse(buildRequestContext(bundle.getVersionedHash()));
225+
assertThat(response.getResult()).isNull();
226+
verifyNoInteractions(requestedCounter);
227+
verifyNoInteractions(availableCounter);
228+
verifyNoInteractions(missCounter);
229+
verifyNoInteractions(hitCounter);
230+
}
231+
213232
private BlobProofBundle createBundleAndRegisterToPool() {
214233
BlobTestFixture blobFixture = new BlobTestFixture();
215234
BlobProofBundle bundle = blobFixture.createBlobProofBundle(KZG_CELL_PROOFS);

0 commit comments

Comments
 (0)