Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import org.tron.common.cron.CronExpression;
import org.tron.common.args.GenesisBlock;
import org.tron.common.config.DbBackupConfig;
import org.tron.common.cron.CronExpression;
import org.tron.common.logsfilter.EventPluginConfig;
import org.tron.common.logsfilter.FilterQuery;
import org.tron.common.setting.RocksDbSettings;
Expand Down Expand Up @@ -495,6 +495,13 @@ public class CommonParameter {
@Getter
@Setter
public boolean jsonRpcHttpPBFTNodeEnable = false;
@Getter
@Setter
public int jsonRpcMaxBlockRange = 5000;
@Getter
@Setter
public int jsonRpcMaxSubTopics = 1000;

@Getter
@Setter
public int maxTransactionPendingSize;
Expand Down
47 changes: 24 additions & 23 deletions common/src/main/java/org/tron/common/utils/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,40 +479,41 @@ public static byte[] setBit(byte[] data, int pos, int val) {

public static byte[] compress(byte[] data) throws EventBloomException {
Deflater deflater = new Deflater();
deflater.setInput(data);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length)) {
deflater.setInput(data);
deflater.finish();
byte[] buffer = new byte[1024];

deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer); // returns the generated code... index
outputStream.write(buffer, 0, count);
}
try {
outputStream.close();
while (!deflater.finished()) {
int count = deflater.deflate(buffer); // returns the generated code... index
outputStream.write(buffer, 0, count);
}

return outputStream.toByteArray();
} catch (IOException e) {
throw new EventBloomException("compress data failed");
} finally {
deflater.end();
}
byte[] output = outputStream.toByteArray();

return output;
}

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
Inflater inflater = new Inflater();
inflater.setInput(data);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length)) {
inflater.setInput(data);
byte[] buffer = new byte[1024];

while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}

return output;
return outputStream.toByteArray();
} finally {
inflater.end();
}
}

}
2 changes: 2 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public class Constant {
public static final String NODE_JSONRPC_HTTP_SOLIDITY_PORT = "node.jsonrpc.httpSolidityPort";
public static final String NODE_JSONRPC_HTTP_PBFT_ENABLE = "node.jsonrpc.httpPBFTEnable";
public static final String NODE_JSONRPC_HTTP_PBFT_PORT = "node.jsonrpc.httpPBFTPort";
public static final String NODE_JSONRPC_MAX_BLOCK_RANGE = "node.jsonrpc.maxBlockRange";
public static final String NODE_JSONRPC_MAX_SUB_TOPICS = "node.jsonrpc.maxSubTopics";

public static final String NODE_DISABLED_API_LIST = "node.disabledApi";

Expand Down
12 changes: 12 additions & 0 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public static void clearParam() {
PARAMETER.jsonRpcHttpFullNodeEnable = false;
PARAMETER.jsonRpcHttpSolidityNodeEnable = false;
PARAMETER.jsonRpcHttpPBFTNodeEnable = false;
PARAMETER.jsonRpcMaxBlockRange = 5000;
PARAMETER.jsonRpcMaxSubTopics = 1000;
PARAMETER.nodeMetricsEnable = false;
PARAMETER.metricsStorageEnable = false;
PARAMETER.metricsPrometheusEnable = false;
Expand Down Expand Up @@ -507,6 +509,16 @@ public static void setParam(final Config config) {
config.getBoolean(Constant.NODE_JSONRPC_HTTP_PBFT_ENABLE);
}

if (config.hasPath(Constant.NODE_JSONRPC_MAX_BLOCK_RANGE)) {
PARAMETER.jsonRpcMaxBlockRange =
config.getInt(Constant.NODE_JSONRPC_MAX_BLOCK_RANGE);
}

if (config.hasPath(Constant.NODE_JSONRPC_MAX_SUB_TOPICS)) {
PARAMETER.jsonRpcMaxSubTopics =
config.getInt(Constant.NODE_JSONRPC_MAX_SUB_TOPICS);
}

if (config.hasPath(Constant.VM_MIN_TIME_RATIO)) {
PARAMETER.minTimeRatio = config.getDouble(Constant.VM_MIN_TIME_RATIO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ public LogFilterElement[] getLogs(FilterRequest fr) throws JsonRpcInvalidParamsE

long currentMaxBlockNum = wallet.getNowBlock().getBlockHeader().getRawData().getNumber();
//convert FilterRequest to LogFilterWrapper
LogFilterWrapper logFilterWrapper = new LogFilterWrapper(fr, currentMaxBlockNum, wallet);
LogFilterWrapper logFilterWrapper = new LogFilterWrapper(fr, currentMaxBlockNum, wallet, true);

return getLogsByLogFilterWrapper(logFilterWrapper, currentMaxBlockNum);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@ public LogBlockQuery(LogFilterWrapper logFilterWrapper, SectionBloomStore sectio
this.currentMaxBlockNum = currentMaxBlockNum;

if (logFilterWrapper.getFromBlock() == Long.MAX_VALUE) {
minSection = (int) (currentMaxBlockNum / Bloom.BLOOM_BIT_SIZE);
minBlock = currentMaxBlockNum;
} else {
minSection = (int) (logFilterWrapper.getFromBlock() / Bloom.BLOOM_BIT_SIZE);
minBlock = logFilterWrapper.getFromBlock();
}
minSection = (int) (minBlock / Bloom.BLOOM_BIT_SIZE);

if (logFilterWrapper.getToBlock() == Long.MAX_VALUE) {
maxSection = (int) (currentMaxBlockNum / Bloom.BLOOM_BIT_SIZE);
maxBlock = currentMaxBlockNum;
} else {
maxSection = (int) (logFilterWrapper.getToBlock() / Bloom.BLOOM_BIT_SIZE);
maxBlock = logFilterWrapper.getToBlock();
if (maxBlock > currentMaxBlockNum) {
maxBlock = currentMaxBlockNum;
}
}
maxSection = (int) (maxBlock / Bloom.BLOOM_BIT_SIZE);
}

public List<Long> getPossibleBlock() throws ExecutionException, InterruptedException,
Expand All @@ -71,7 +69,7 @@ public List<Long> getPossibleBlock() throws ExecutionException, InterruptedExcep
BitSet blockNumBitSet = new BitSet(capacity);
blockNumBitSet.set(0, capacity);

//works serial
// works serial
for (int[][] conditionsIndex : allConditionsIndex) {
BitSet bitSet = subMatch(conditionsIndex);
blockNumBitSet.and(bitSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.tron.common.bloom.Bloom;
import org.tron.common.crypto.Hash;
import org.tron.common.runtime.vm.DataWord;
import org.tron.core.config.args.Args;
import org.tron.core.exception.JsonRpcInvalidParamsException;
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
import org.tron.protos.Protocol.TransactionInfo.Log;
Expand All @@ -35,6 +36,8 @@ public class LogFilter {
@Setter
private Bloom[][] filterBlooms;

// The maximum number of topic criteria allowed, vm.LOG4 - vm.LOG0
private final int maxTopics = 4;

public LogFilter() {
}
Expand Down Expand Up @@ -66,8 +69,8 @@ public LogFilter(FilterRequest fr) throws JsonRpcInvalidParamsException {

if (fr.getTopics() != null) {
//restrict depth of topics, because event has a signature and most 3 indexed parameters
if (fr.getTopics().length > 4) {
throw new JsonRpcInvalidParamsException("topics size should be <= 4");
if (fr.getTopics().length > maxTopics) {
throw new JsonRpcInvalidParamsException("topics size should be <= " + maxTopics);
}
for (Object topic : fr.getTopics()) {
if (topic == null) {
Expand All @@ -79,6 +82,10 @@ public LogFilter(FilterRequest fr) throws JsonRpcInvalidParamsException {
throw new JsonRpcInvalidParamsException("invalid topic(s): " + topic);
}
} else if (topic instanceof ArrayList) {
int maxSubTopics = Args.getInstance().getJsonRpcMaxSubTopics();
if (maxSubTopics > 0 && ((ArrayList<?>) topic).size() > maxSubTopics) {
throw new JsonRpcInvalidParamsException("exceed max topics: " + maxSubTopics);
}

List<byte[]> t = new ArrayList<>();
for (Object s : ((ArrayList) topic)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class LogFilterAndResult extends FilterResult<LogFilterElement> {

public LogFilterAndResult(FilterRequest fr, long currentMaxBlockNum, Wallet wallet)
throws JsonRpcInvalidParamsException {
this.logFilterWrapper = new LogFilterWrapper(fr, currentMaxBlockNum, wallet);
// eth_newFilter, no need to check block range
this.logFilterWrapper = new LogFilterWrapper(fr, currentMaxBlockNum, wallet, false);
result = new LinkedBlockingQueue<>();
this.updateExpireTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.StringUtils;
import org.tron.common.utils.ByteArray;
import org.tron.core.Wallet;
import org.tron.core.config.args.Args;
import org.tron.core.exception.JsonRpcInvalidParamsException;
import org.tron.core.services.jsonrpc.JsonRpcApiUtil;
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
Expand All @@ -23,8 +24,8 @@ public class LogFilterWrapper {
@Getter
private final long toBlock;

public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet)
throws JsonRpcInvalidParamsException {
public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet,
boolean checkBlockRange) throws JsonRpcInvalidParamsException {

// 1.convert FilterRequest to LogFilter
this.logFilter = new LogFilter(fr);
Expand Down Expand Up @@ -86,6 +87,12 @@ public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet
throw new JsonRpcInvalidParamsException("please verify: fromBlock <= toBlock");
}
}

// till now, it needs to check block range for eth_getLogs
int maxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange();
if (checkBlockRange && maxBlockRange > 0 && (toBlockSrc - fromBlockSrc) > maxBlockRange) {
throw new JsonRpcInvalidParamsException("exceed max block range: " + maxBlockRange);
}
}

this.fromBlock = fromBlockSrc;
Expand Down
2 changes: 2 additions & 0 deletions framework/src/main/resources/config-localtest.conf
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ node {
# httpSolidityPort = 8555
# httpPBFTEnable = true
# httpPBFTPort = 8565
# maxBlockRange = 5000
# maxSubTopics = 1000
}

}
Expand Down
8 changes: 8 additions & 0 deletions framework/src/main/resources/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ node {
# httpSolidityPort = 8555
# httpPBFTEnable = true
# httpPBFTPort = 8565

# The maximum blocks range to retrieve logs for eth_getLogs, default value is 5000,
# should be > 0, otherwise means no limit.
# maxBlockRange = 5000

# The maximum number of allowed topics within a topic criteria, default value is 1000,
# should be > 0, otherwise means no limit.
# maxSubTopics = 1000
}

# Disabled api list, it will work for http, rpc and pbft, both fullnode and soliditynode,
Expand Down
14 changes: 14 additions & 0 deletions framework/src/test/java/org/tron/core/config/args/ArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public void testInitService() {
Assert.assertFalse(Args.getInstance().isJsonRpcHttpFullNodeEnable());
Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable());
Assert.assertFalse(Args.getInstance().isJsonRpcHttpPBFTNodeEnable());
Assert.assertEquals(5000, Args.getInstance().getJsonRpcMaxBlockRange());
Assert.assertEquals(1000, Args.getInstance().getJsonRpcMaxSubTopics());
Args.clearParam();
// test set all true value
storage.put("node.rpc.enable", "true");
Expand All @@ -184,6 +186,8 @@ public void testInitService() {
storage.put("node.jsonrpc.httpFullNodeEnable", "true");
storage.put("node.jsonrpc.httpSolidityEnable", "true");
storage.put("node.jsonrpc.httpPBFTEnable", "true");
storage.put("node.jsonrpc.maxBlockRange", "10");
storage.put("node.jsonrpc.maxSubTopics", "20");
config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage));
// test value
Args.setParam(config);
Expand All @@ -196,6 +200,8 @@ public void testInitService() {
Assert.assertTrue(Args.getInstance().isJsonRpcHttpFullNodeEnable());
Assert.assertTrue(Args.getInstance().isJsonRpcHttpSolidityNodeEnable());
Assert.assertTrue(Args.getInstance().isJsonRpcHttpPBFTNodeEnable());
Assert.assertEquals(10, Args.getInstance().getJsonRpcMaxBlockRange());
Assert.assertEquals(20, Args.getInstance().getJsonRpcMaxSubTopics());
Args.clearParam();
// test set all false value
storage.put("node.rpc.enable", "false");
Expand All @@ -207,6 +213,8 @@ public void testInitService() {
storage.put("node.jsonrpc.httpFullNodeEnable", "false");
storage.put("node.jsonrpc.httpSolidityEnable", "false");
storage.put("node.jsonrpc.httpPBFTEnable", "false");
storage.put("node.jsonrpc.maxBlockRange", "5000");
storage.put("node.jsonrpc.maxSubTopics", "1000");
config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage));
// test value
Args.setParam(config);
Expand All @@ -219,6 +227,8 @@ public void testInitService() {
Assert.assertFalse(Args.getInstance().isJsonRpcHttpFullNodeEnable());
Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable());
Assert.assertFalse(Args.getInstance().isJsonRpcHttpPBFTNodeEnable());
Assert.assertEquals(5000, Args.getInstance().getJsonRpcMaxBlockRange());
Assert.assertEquals(1000, Args.getInstance().getJsonRpcMaxSubTopics());
Args.clearParam();
// test set random value
storage.put("node.rpc.enable", "false");
Expand All @@ -230,6 +240,8 @@ public void testInitService() {
storage.put("node.jsonrpc.httpFullNodeEnable", "true");
storage.put("node.jsonrpc.httpSolidityEnable", "false");
storage.put("node.jsonrpc.httpPBFTEnable", "true");
storage.put("node.jsonrpc.maxBlockRange", "30");
storage.put("node.jsonrpc.maxSubTopics", "40");
config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage));
// test value
Args.setParam(config);
Expand All @@ -242,6 +254,8 @@ public void testInitService() {
Assert.assertTrue(Args.getInstance().isJsonRpcHttpFullNodeEnable());
Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable());
Assert.assertTrue(Args.getInstance().isJsonRpcHttpPBFTNodeEnable());
Assert.assertEquals(30, Args.getInstance().getJsonRpcMaxBlockRange());
Assert.assertEquals(40, Args.getInstance().getJsonRpcMaxSubTopics());
Args.clearParam();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public void testGetConditions() {
topics,
null),
100,
null);
null,
false);

LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, null, 100, null);
int[][][] conditions = logBlockQuery.getConditions();
Expand Down Expand Up @@ -331,7 +332,8 @@ public void testGetConditionWithHashCollision() {
topics,
null),
100,
null);
null,
false);

LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, null, 100, null);
int[][][] conditions = logBlockQuery.getConditions();
Expand Down
Loading