Skip to content

Commit 63c268d

Browse files
authored
Merge pull request #4642 from tronprotocol/feature/graph_jsonrpc
feat: optimize default values for jsonrpc api results
2 parents b6295a5 + 25094fe commit 63c268d

5 files changed

Lines changed: 112 additions & 31 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, Stri
152152
@JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"),
153153
@JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"),
154154
})
155-
String getCall(CallArguments transactionCall, String blockNumOrTag)
155+
String getCall(CallArguments transactionCall, Object blockNumOrTag)
156156
throws JsonRpcInvalidParamsException, JsonRpcInvalidRequestException,
157157
JsonRpcInternalException;
158158

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.protobuf.ByteString;
1515
import com.google.protobuf.GeneratedMessageV3;
1616
import java.util.Arrays;
17+
import java.util.HashMap;
1718
import java.util.Iterator;
1819
import java.util.List;
1920
import java.util.Map;
@@ -134,6 +135,8 @@ public enum RequestSource {
134135
private static final String TAG_NOT_SUPPORT_ERROR = "TAG [earliest | pending] not supported";
135136
private static final String QUANTITY_NOT_SUPPORT_ERROR =
136137
"QUANTITY not supported, just support TAG as latest";
138+
private static final String NO_BLOCK_HEADER = "header not found";
139+
private static final String NO_BLOCK_HEADER_BY_HASH = "header for hash not found";
137140

138141
private static final String ERROR_SELECTOR = "08c379a0"; // Function selector for Error(string)
139142
/**
@@ -716,9 +719,54 @@ public TransactionReceipt getTransactionReceipt(String txId)
716719
}
717720

718721
@Override
719-
public String getCall(CallArguments transactionCall, String blockNumOrTag)
722+
public String getCall(CallArguments transactionCall, Object blockParamObj)
720723
throws JsonRpcInvalidParamsException, JsonRpcInvalidRequestException,
721724
JsonRpcInternalException {
725+
726+
String blockNumOrTag;
727+
if (blockParamObj instanceof HashMap) {
728+
HashMap<String, String> paramMap;
729+
paramMap = (HashMap<String, String>) blockParamObj;
730+
731+
if (paramMap.containsKey("blockNumber")) {
732+
try {
733+
blockNumOrTag = paramMap.get("blockNumber");
734+
} catch (ClassCastException e) {
735+
throw new JsonRpcInvalidRequestException(JSON_ERROR);
736+
}
737+
738+
long blockNumber;
739+
try {
740+
blockNumber = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
741+
} catch (Exception e) {
742+
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
743+
}
744+
745+
if (wallet.getBlockByNum(blockNumber) == null) {
746+
throw new JsonRpcInternalException(NO_BLOCK_HEADER);
747+
}
748+
749+
} else if (paramMap.containsKey("blockHash")) {
750+
try {
751+
blockNumOrTag = paramMap.get("blockHash");
752+
} catch (ClassCastException e) {
753+
throw new JsonRpcInvalidRequestException(JSON_ERROR);
754+
}
755+
756+
if (getBlockByJsonHash(blockNumOrTag) == null) {
757+
throw new JsonRpcInternalException(NO_BLOCK_HEADER_BY_HASH);
758+
}
759+
} else {
760+
throw new JsonRpcInvalidRequestException(JSON_ERROR);
761+
}
762+
763+
blockNumOrTag = LATEST_STR;
764+
} else if (blockParamObj instanceof String) {
765+
blockNumOrTag = (String) blockParamObj;
766+
} else {
767+
throw new JsonRpcInvalidRequestException(JSON_ERROR);
768+
}
769+
722770
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
723771
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
724772
throw new JsonRpcInvalidParamsException(TAG_NOT_SUPPORT_ERROR);

framework/src/main/java/org/tron/core/services/jsonrpc/types/BlockResult.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public class BlockResult {
7979

8080
@Getter
8181
@Setter
82-
private String baseFeePerGas = null;
82+
private String baseFeePerGas = "0x0";
8383
@Getter
8484
@Setter
85-
private String mixHash = null;
85+
private String mixHash = ByteArray.toJsonHex(new byte[32]);
8686

8787
public BlockResult(Block block, boolean fullTx, Wallet wallet) {
8888
BlockCapsule blockCapsule = new BlockCapsule(block);
@@ -91,20 +91,25 @@ public BlockResult(Block block, boolean fullTx, Wallet wallet) {
9191
hash = ByteArray.toJsonHex(blockCapsule.getBlockId().getBytes());
9292
parentHash =
9393
ByteArray.toJsonHex(block.getBlockHeader().getRawData().getParentHash().toByteArray());
94-
nonce = null; // no value
95-
sha3Uncles = null; // no value
94+
nonce = ByteArray.toJsonHex(new byte[8]); // no value
95+
sha3Uncles = ByteArray.toJsonHex(new byte[32]); // no value
9696
logsBloom = ByteArray.toJsonHex(new byte[256]); // no value
9797
transactionsRoot = ByteArray
9898
.toJsonHex(block.getBlockHeader().getRawData().getTxTrieRoot().toByteArray());
99-
stateRoot = ByteArray
100-
.toJsonHex(block.getBlockHeader().getRawData().getAccountStateRoot().toByteArray());
101-
receiptsRoot = null; // no value
102-
miner = ByteArray.toJsonHexAddress(blockCapsule.getWitnessAddress().toByteArray());
103-
difficulty = null; // no value
104-
totalDifficulty = null; // no value
105-
extraData = null; // no value
99+
stateRoot = ByteArray.toJsonHex(new byte[32]);
100+
receiptsRoot = ByteArray.toJsonHex(new byte[32]); // no value
101+
102+
if (blockCapsule.getNum() == 0) {
103+
miner = ByteArray.toJsonHex(new byte[20]);
104+
} else {
105+
miner = ByteArray.toJsonHexAddress(blockCapsule.getWitnessAddress().toByteArray());
106+
}
107+
108+
difficulty = "0x0"; // no value
109+
totalDifficulty = "0x0"; // no value
110+
extraData = "0x"; // no value
106111
size = ByteArray.toJsonHex(block.getSerializedSize());
107-
timestamp = ByteArray.toJsonHex(blockCapsule.getTimeStamp());
112+
timestamp = ByteArray.toJsonHex(blockCapsule.getTimeStamp() / 1000);
108113

109114
long gasUsedInBlock = 0;
110115
long gasLimitInBlock = 0;

framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.tron.protos.Protocol;
1616
import org.tron.protos.Protocol.Transaction;
1717
import org.tron.protos.Protocol.Transaction.Contract;
18+
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
19+
import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract;
1820

1921
@JsonPropertyOrder(alphabetic = true)
2022
@ToString
@@ -56,9 +58,9 @@ public class TransactionResult {
5658
private void parseSignature(Transaction tx) {
5759

5860
if (tx.getSignatureCount() == 0) {
59-
v = null;
60-
r = null;
61-
s = null;
61+
v = ByteArray.toJsonHex(new byte[1]);
62+
r = ByteArray.toJsonHex(new byte[32]);
63+
s = ByteArray.toJsonHex(new byte[32]);
6264
return;
6365
}
6466

@@ -75,12 +77,34 @@ private void parseSignature(Transaction tx) {
7577
s = ByteArray.toJsonHex(sByte);
7678
}
7779

80+
private String parseInput(Transaction tx) {
81+
String data;
82+
if (tx.getRawData().getContractCount() == 0) {
83+
data = "0x";
84+
} else {
85+
Contract contract = tx.getRawData().getContract(0);
86+
if (contract.getType() == ContractType.TriggerSmartContract) {
87+
try {
88+
TriggerSmartContract triggerSmartContract = contract.getParameter()
89+
.unpack(TriggerSmartContract.class);
90+
data = ByteArray.toJsonHex(triggerSmartContract.getData().toByteArray());
91+
} catch (Exception e) {
92+
data = "0x";
93+
}
94+
} else {
95+
data = "0x";
96+
}
97+
}
98+
99+
return data;
100+
}
101+
78102
public TransactionResult(BlockCapsule blockCapsule, int index, Protocol.Transaction tx,
79103
long energyUsageTotal, long energyFee, Wallet wallet) {
80104
TransactionCapsule capsule = new TransactionCapsule(tx);
81105
byte[] txId = capsule.getTransactionId().getBytes();
82106
hash = ByteArray.toJsonHex(txId);
83-
nonce = null; // no value
107+
nonce = ByteArray.toJsonHex(new byte[8]); // no value
84108
blockHash = ByteArray.toJsonHex(blockCapsule.getBlockId().getBytes());
85109
blockNumber = ByteArray.toJsonHex(blockCapsule.getNum());
86110
transactionIndex = ByteArray.toJsonHex(index);
@@ -89,18 +113,24 @@ public TransactionResult(BlockCapsule blockCapsule, int index, Protocol.Transact
89113
Contract contract = tx.getRawData().getContract(0);
90114
byte[] fromByte = capsule.getOwnerAddress();
91115
byte[] toByte = getToAddress(tx);
92-
from = ByteArray.toJsonHexAddress(fromByte);
116+
117+
if (blockCapsule.getNum() == 0) {
118+
from = ByteArray.toJsonHex(new byte[20]);
119+
} else {
120+
from = ByteArray.toJsonHexAddress(fromByte);
121+
}
122+
93123
to = ByteArray.toJsonHexAddress(toByte);
94124
value = ByteArray.toJsonHex(getTransactionAmount(contract, hash, wallet));
95125
} else {
96-
from = null;
97-
to = null;
98-
value = null;
126+
from = ByteArray.toJsonHex(new byte[20]);
127+
to = ByteArray.toJsonHex(new byte[20]);
128+
value = "0x0";
99129
}
100130

101131
gas = ByteArray.toJsonHex(energyUsageTotal);
102132
gasPrice = ByteArray.toJsonHex(energyFee);
103-
input = ByteArray.toJsonHex(tx.getRawData().getData().toByteArray());
133+
input = parseInput(tx);
104134

105135
parseSignature(tx);
106136
}
@@ -109,7 +139,7 @@ public TransactionResult(Transaction tx, Wallet wallet) {
109139
TransactionCapsule capsule = new TransactionCapsule(tx);
110140
byte[] txid = capsule.getTransactionId().getBytes();
111141
hash = ByteArray.toJsonHex(txid);
112-
nonce = null; // no value
142+
nonce = ByteArray.toJsonHex(new byte[8]); // no value
113143
blockHash = "0x";
114144
blockNumber = "0x";
115145
transactionIndex = "0x";
@@ -122,14 +152,14 @@ public TransactionResult(Transaction tx, Wallet wallet) {
122152
to = ByteArray.toJsonHexAddress(toByte);
123153
value = ByteArray.toJsonHex(getTransactionAmount(contract, hash, wallet));
124154
} else {
125-
from = null;
126-
to = null;
127-
value = null;
155+
from = ByteArray.toJsonHex(new byte[20]);
156+
to = ByteArray.toJsonHex(new byte[20]);
157+
value = "0x0";
128158
}
129159

130160
gas = "0x0";
131161
gasPrice = "0x";
132-
input = ByteArray.toJsonHex(tx.getRawData().getData().toByteArray());
162+
input = parseInput(tx);
133163

134164
parseSignature(tx);
135165
}

framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.tron.core.jsonrpc;
22

3-
import com.google.protobuf.Any;
43
import com.google.protobuf.ByteString;
54
import java.io.File;
65
import lombok.extern.slf4j.Slf4j;
@@ -230,8 +229,7 @@ public void testGetBlockByNumber() {
230229
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule.getNum()), blockResult.getNumber());
231230
Assert
232231
.assertEquals(blockCapsule.getTransactions().size(), blockResult.getTransactions().length);
233-
Assert.assertNull(blockResult.getNonce());
234-
232+
Assert.assertEquals("0x0000000000000000", blockResult.getNonce());
235233
}
236234

237235

0 commit comments

Comments
 (0)