Skip to content

Commit f28beb3

Browse files
committed
remove reduant maxBlockFilterNum; optimize commitToResponse
1 parent 3908770 commit f28beb3

5 files changed

Lines changed: 41 additions & 19 deletions

File tree

common/src/main/resources/reference.conf

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,27 +380,20 @@ node {
380380
httpPBFTEnable = false
381381
httpPBFTPort = 8565
382382

383-
# Maximum blocks range for eth_getLogs, >0 otherwise no limit
383+
# The maximum blocks range to retrieve logs for eth_getLogs, default: 5000, <=0 means no limit
384384
maxBlockRange = 5000
385-
386-
# Maximum topics within a topic criteria, >0 otherwise no limit
385+
# Allowed max address count in filter request, default: 1000, <=0 means no limit
386+
maxAddressSize = 1000
387+
# The maximum number of allowed topics within a topic criteria, default: 1000, <=0 means no limit
387388
maxSubTopics = 1000
388-
389-
# Maximum number for blockFilter. >0 otherwise no limit
389+
# Allowed maximum number for blockFilter, default: 50000, <=0 means no limit
390390
maxBlockFilterNum = 50000
391-
392-
# Maximum number of requests in a JSON-RPC batch, >0 otherwise no limit
391+
# Allowed batch size, default: 100, <=0 means no limit
393392
maxBatchSize = 100
394-
395-
# Maximum response body size in bytes for JSON-RPC (default 25MB), >0 otherwise no limit
393+
# Allowed max response byte size, default: 26214400 (25 MB), <=0 means no limit
396394
maxResponseSize = 26214400
397-
398-
# Maximum number of addresses in a single JSON-RPC request, >0 otherwise no limit
399-
maxAddressSize = 1000
400-
401-
# Maximum number of concurrent eth_newFilter registrations, >0 otherwise no limit
395+
# Allowed maximum number for newFilter, <=0 means no limit
402396
maxLogFilterNum = 20000
403-
404397
# Maximum JSON-RPC request body size, default 4MB. Independent from rpc.maxMessageSize.
405398
maxMessageSize = 4M
406399
}

framework/src/main/java/org/tron/core/services/filter/BufferedResponseWrapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ public void commitToResponse() throws IOException {
160160
throw new IllegalStateException("commitToResponse() already called");
161161
}
162162
committed = true;
163+
// Flush the PrintWriter's OutputStreamWriter encoder into our ByteArrayOutputStream.
164+
// PrintWriter(autoFlush=true) only auto-flushes on println/printf/format, not print/write,
165+
// so bytes can remain buffered in the encoder until an explicit flush.
166+
writer.flush();
167+
if (overflow) {
168+
return;
169+
}
163170
if (contentType != null) {
164171
actual.setContentType(contentType);
165172
}

framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,12 @@ private void handleSingle(HttpServletRequest req, HttpServletResponse resp,
140140
return;
141141
}
142142

143+
bufferedResp.commitToResponse();
143144
if (bufferedResp.isOverflow()) {
144145
writeJsonRpcError(resp, JsonRpcError.RESPONSE_TOO_LARGE,
145146
"Response exceeds the limit of " + maxResponseSize + " bytes",
146147
rootNode.get("id"), false);
147-
return;
148148
}
149-
bufferedResp.commitToResponse();
150149
}
151150

152151
private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxResponseSize)

framework/src/main/resources/config.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ node {
400400
maxBatchSize = 100
401401
# Allowed max response byte size, default: 26214400 (25 MB), <=0 means no limit
402402
maxResponseSize = 26214400
403-
# Allowed maximum number for blockFilter, <=0 means no limit
404-
maxBlockFilterNum = 50000
405403
# Allowed maximum number for newFilter, <=0 means no limit
406404
maxLogFilterNum = 20000
405+
# Maximum JSON-RPC request body size, default 4MB. Independent from rpc.maxMessageSize.
406+
maxMessageSize = 4M
407407
}
408408

409409
# Disabled api list, it will work for http, rpc and pbft, both FullNode and SolidityNode,

framework/src/test/java/org/tron/core/services/filter/BufferedResponseWrapperTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,27 @@ public void writeViaWriter_commitToResponse_flushesBody() throws IOException {
261261
w.commitToResponse();
262262
assertEquals("hello", mockResp.getContentAsString());
263263
}
264+
265+
@Test
266+
public void writeViaWriter_noExplicitFlush_commitToResponse_flushesBody() throws IOException {
267+
// Regression: PrintWriter(autoFlush=true) does NOT flush on plain print(); bytes can sit
268+
// in the OutputStreamWriter encoder until commitToResponse() flushes the writer internally.
269+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
270+
w.getWriter().print("hello");
271+
w.commitToResponse();
272+
assertEquals("hello", mockResp.getContentAsString());
273+
assertEquals(5, mockResp.getContentLength());
274+
}
275+
276+
@Test
277+
public void writeViaWriter_noExplicitFlush_flushTripsOverflow() throws IOException {
278+
// Regression: bytes buffered in the encoder may push the total past maxBytes when
279+
// commitToResponse() flushes — overflow must be detected and nothing written to actual.
280+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 3);
281+
w.getWriter().print("hello"); // 5 bytes, not yet in ByteArrayOutputStream
282+
assertFalse("overflow must not trigger before flush", w.isOverflow());
283+
w.commitToResponse();
284+
assertTrue("flush inside commitToResponse must trip overflow", w.isOverflow());
285+
assertEquals(0, mockResp.getContentAsByteArray().length);
286+
}
264287
}

0 commit comments

Comments
 (0)