Skip to content
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
41 changes: 4 additions & 37 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
import static org.tron.core.config.Parameter.DatabaseConstants.PROPOSAL_COUNT_LIMIT_MAX;
import static org.tron.core.config.Parameter.DatabaseConstants.WITNESS_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 @@ -193,7 +188,6 @@
import org.tron.core.exception.VMIllegalException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.exception.ZksnarkException;
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
import org.tron.core.net.TronNetDelegate;
import org.tron.core.net.TronNetService;
import org.tron.core.net.message.adv.TransactionMessage;
Expand Down Expand Up @@ -711,6 +705,10 @@ public long getSolidBlockNum() {
return chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
}

public long getHeadBlockNum() {
return chainBaseManager.getHeadBlockNum();
}

public BlockCapsule getBlockCapsuleByNum(long blockNum) {
try {
return chainBaseManager.getBlockByNum(blockNum);
Expand All @@ -733,37 +731,6 @@ public long getTransactionCountByBlockNum(long blockNum) {
return count;
}

public Block getByJsonBlockId(String id) throws JsonRpcInvalidParamsException {
if (EARLIEST_STR.equalsIgnoreCase(id)) {
return getBlockByNum(0);
} else if (LATEST_STR.equalsIgnoreCase(id)) {
return getNowBlock();
} 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 {
blockNumber = ByteArray.hexToBigInteger(id).longValue();
} catch (Exception e) {
throw new JsonRpcInvalidParamsException("invalid block number");
}

return getBlockByNum(blockNumber);
}
}

public List<Transaction> getTransactionsByJsonBlockId(String id)
throws JsonRpcInvalidParamsException {
if (PENDING_STR.equalsIgnoreCase(id)) {
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
} else {
Block block = getByJsonBlockId(id);
return block != null ? block.getTransactionsList() : null;
}
}

public WitnessList getWitnessList() {
WitnessList.Builder builder = WitnessList.newBuilder();
List<WitnessCapsule> witnessCapsuleList = chainBaseManager.getWitnessStore().getAllWitnesses();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
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;
import com.google.protobuf.Any;
Expand Down Expand Up @@ -57,6 +51,15 @@
@Slf4j(topic = "API")
public class JsonRpcApiUtil {

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 SAFE_STR = "safe";
public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported";
public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported";
public static final String BLOCK_NUM_ERROR = "invalid block number";

public static byte[] convertToTronAddress(byte[] address) {
byte[] newAddress = new byte[21];
byte[] temp = new byte[] {Wallet.getAddressPreFixByte()};
Expand Down Expand Up @@ -515,20 +518,52 @@ public static long parseEnergyFee(long timestamp, String energyPriceHistory) {
return -1;
}

public static long getByJsonBlockId(String blockNumOrTag, Wallet wallet)
public static boolean isBlockTag(String tag) {
return LATEST_STR.equalsIgnoreCase(tag)
|| EARLIEST_STR.equalsIgnoreCase(tag)
|| FINALIZED_STR.equalsIgnoreCase(tag)
|| PENDING_STR.equalsIgnoreCase(tag)
|| SAFE_STR.equalsIgnoreCase(tag);
}

public static long parseBlockTag(String tag, Wallet wallet)
throws JsonRpcInvalidParamsException {
if (PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
if (LATEST_STR.equalsIgnoreCase(tag)) {
// Use getNowBlock() to fetch the latest persisted block directly from blockStore,
// avoiding a race with updateDynamicProperties that updates latestBlockHeaderNumber
// before the block is written to blockStore/blockIndexStore.
Block block = wallet.getNowBlock();
if (block == null) {
return 0;
}
return block.getBlockHeader().getRawData().getNumber();
}
if (StringUtils.isEmpty(blockNumOrTag) || LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
return -1;
} else if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)) {
if (EARLIEST_STR.equalsIgnoreCase(tag)) {
return 0;
} else if (FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
}
if (FINALIZED_STR.equalsIgnoreCase(tag)) {
return wallet.getSolidBlockNum();
} else {
return ByteArray.jsonHexToLong(blockNumOrTag);
}
if (PENDING_STR.equalsIgnoreCase(tag)) {
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
}
if (SAFE_STR.equalsIgnoreCase(tag)) {
throw new JsonRpcInvalidParamsException(TAG_SAFE_SUPPORT_ERROR);
}
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
}

/**
* Parse a block tag or hex number. Uses strict jsonHexToLong (requires 0x prefix) for hex.
* Callers needing flexible hex parsing (0x -> hex, bare number -> decimal) should use
* isBlockTag/parseBlockTag and handle hex separately with hexToBigInteger.
*/
public static long parseBlockNumber(String blockNumOrTag, Wallet wallet)
throws JsonRpcInvalidParamsException {
if (isBlockTag(blockNumOrTag)) {
return parseBlockTag(blockNumOrTag, wallet);
}
return ByteArray.jsonHexToLong(blockNumOrTag);
}

public static String generateFilterId() {
Expand Down
Loading
Loading