Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsonrpc): ethGetBlockByNumber supports finalized #6007

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
30 changes: 25 additions & 5 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import static org.tron.core.config.Parameter.DatabaseConstants.PROPOSAL_COUNT_LIMIT_MAX;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseEnergyFee;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.FINALIZED_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.LATEST_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.PENDING_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR;
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;
import static org.tron.protos.contract.Common.ResourceCode;
Expand Down Expand Up @@ -681,6 +685,20 @@ public Block getBlockByNum(long blockNum) {
}
}

public Block getSolidBlock() {
try {
long blockNum = getSolidBlockNum();
return chainBaseManager.getBlockByNum(blockNum).getInstance();
} catch (StoreException e) {
logger.info(e.getMessage());
return null;
}
}

public long getSolidBlockNum() {
return chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
}

public BlockCapsule getBlockCapsuleByNum(long blockNum) {
try {
return chainBaseManager.getBlockByNum(blockNum);
Expand All @@ -706,10 +724,12 @@ public long getTransactionCountByBlockNum(long blockNum) {
public Block getByJsonBlockId(String id) throws JsonRpcInvalidParamsException {
if (EARLIEST_STR.equalsIgnoreCase(id)) {
return getBlockByNum(0);
} else if ("latest".equalsIgnoreCase(id)) {
} else if (LATEST_STR.equalsIgnoreCase(id)) {
return getNowBlock();
} else if ("pending".equalsIgnoreCase(id)) {
throw new JsonRpcInvalidParamsException("TAG pending not supported");
} else if (FINALIZED_STR.equalsIgnoreCase(id)) {
return getSolidBlock();
} else if (PENDING_STR.equalsIgnoreCase(id)) {
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
} else {
long blockNumber;
try {
Expand All @@ -724,8 +744,8 @@ public Block getByJsonBlockId(String id) throws JsonRpcInvalidParamsException {

public List<Transaction> getTransactionsByJsonBlockId(String id)
throws JsonRpcInvalidParamsException {
if ("pending".equalsIgnoreCase(id)) {
throw new JsonRpcInvalidParamsException("TAG pending not supported");
if (PENDING_STR.equalsIgnoreCase(id)) {
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
} else {
Block block = getByJsonBlockId(id);
return block != null ? block.getTransactionsList() : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.tron.core.services.jsonrpc;

import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.FINALIZED_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.LATEST_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.PENDING_STR;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR;

import com.google.common.base.Throwables;
import com.google.common.primitives.Longs;
Expand Down Expand Up @@ -513,14 +515,17 @@ public static long parseEnergyFee(long timestamp, String energyPriceHistory) {
return -1;
}

public static long getByJsonBlockId(String blockNumOrTag) throws JsonRpcInvalidParamsException {
public static long getByJsonBlockId(String blockNumOrTag, Wallet wallet)
throws JsonRpcInvalidParamsException {
if (PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException("TAG pending not supported");
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
}
if (StringUtils.isEmpty(blockNumOrTag) || LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
return -1;
} else if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)) {
return 0;
} else if (FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
return wallet.getSolidBlockNum();
} else {
return ByteArray.jsonHexToLong(blockNumOrTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ public enum RequestSource {
public static final String EARLIEST_STR = "earliest";
public static final String PENDING_STR = "pending";
public static final String LATEST_STR = "latest";
public static final String FINALIZED_STR = "finalized";
public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported";

private static final String JSON_ERROR = "invalid json request";
private static final String BLOCK_NUM_ERROR = "invalid block number";
private static final String TAG_NOT_SUPPORT_ERROR = "TAG [earliest | pending] not supported";
private static final String TAG_NOT_SUPPORT_ERROR =
"TAG [earliest | pending | finalized] not supported";
private static final String QUANTITY_NOT_SUPPORT_ERROR =
"QUANTITY not supported, just support TAG as latest";
private static final String NO_BLOCK_HEADER = "header not found";
Expand Down Expand Up @@ -351,7 +354,8 @@ public String getLatestBlockNum() {
public String getTrxBalance(String address, String blockNumOrTag)
throws JsonRpcInvalidParamsException {
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException(TAG_NOT_SUPPORT_ERROR);
} else if (LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
byte[] addressData = addressCompatibleToByteArray(address);
Expand Down Expand Up @@ -488,7 +492,8 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va
public String getStorageAt(String address, String storageIdx, String blockNumOrTag)
throws JsonRpcInvalidParamsException {
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException(TAG_NOT_SUPPORT_ERROR);
} else if (LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
byte[] addressByte = addressCompatibleToByteArray(address);
Expand Down Expand Up @@ -523,7 +528,8 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT
public String getABIOfSmartContract(String contractAddress, String blockNumOrTag)
throws JsonRpcInvalidParamsException {
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException(TAG_NOT_SUPPORT_ERROR);
} else if (LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
byte[] addressData = addressCompatibleToByteArray(contractAddress);
Expand Down Expand Up @@ -823,7 +829,8 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
}

if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException(TAG_NOT_SUPPORT_ERROR);
} else if (LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
byte[] addressData = addressCompatibleToByteArray(transactionCall.getFrom());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet
// then if toBlock < maxBlockNum, set fromBlock = toBlock
// then if toBlock >= maxBlockNum, set fromBlock = maxBlockNum
if (StringUtils.isEmpty(fr.getFromBlock()) && StringUtils.isNotEmpty(fr.getToBlock())) {
toBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getToBlock());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method public static long getByJsonBlockId(String blockNumOrTag) is only used in LogFilterWrapper, and it should not support finalized. So it should throw new JsonRpcInvalidParamsException("TAG finalized not supported");

toBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getToBlock(), wallet);
if (toBlockSrc == -1) {
toBlockSrc = Long.MAX_VALUE;
}
fromBlockSrc = Math.min(toBlockSrc, currentMaxBlockNum);

} else if (StringUtils.isNotEmpty(fr.getFromBlock())
&& StringUtils.isEmpty(fr.getToBlock())) {
fromBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getFromBlock());
fromBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getFromBlock(), wallet);
if (fromBlockSrc == -1) {
fromBlockSrc = currentMaxBlockNum;
}
Expand All @@ -70,8 +70,8 @@ public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet
toBlockSrc = Long.MAX_VALUE;

} else {
fromBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getFromBlock());
toBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getToBlock());
fromBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getFromBlock(), wallet);
toBlockSrc = JsonRpcApiUtil.getByJsonBlockId(fr.getToBlock(), wallet);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show in my waalet

if (fromBlockSrc == -1 && toBlockSrc == -1) {
fromBlockSrc = currentMaxBlockNum;
toBlockSrc = Long.MAX_VALUE;
Expand Down
Loading