Skip to content

Commit 34b2d1f

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into feat/opt-api
# Conflicts: # framework/src/main/java/org/tron/core/Wallet.java # framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java # framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java
2 parents 9fcf3c5 + 22e0aa3 commit 34b2d1f

21 files changed

Lines changed: 1075 additions & 246 deletions

File tree

common/src/main/java/org/tron/common/prometheus/MetricKeys.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public static class Counter {
1414
public static final String TXS = "tron:txs";
1515
public static final String MINER = "tron:miner";
1616
public static final String BLOCK_FORK = "tron:block_fork";
17+
// witness label: bounded cardinality -- SR candidate pool is finite, rotation is
18+
// infrequent (at most once per maintenance interval); kept for at-a-glance SR
19+
// identification in dashboards rather than requiring log cross-referencing.
20+
public static final String SR_SET_CHANGE = "tron:sr_set_change";
1721
public static final String P2P_ERROR = "tron:p2p_error";
1822
public static final String P2P_DISCONNECT = "tron:p2p_disconnect";
1923
public static final String INTERNAL_SERVICE_FAIL = "tron:internal_service_fail";
@@ -62,6 +66,7 @@ public static class Histogram {
6266
public static final String MESSAGE_PROCESS_LATENCY = "tron:message_process_latency_seconds";
6367
public static final String BLOCK_FETCH_LATENCY = "tron:block_fetch_latency_seconds";
6468
public static final String BLOCK_RECEIVE_DELAY = "tron:block_receive_delay_seconds";
69+
public static final String BLOCK_TRANSACTION_COUNT = "tron:block_transaction_count";
6570

6671
private Histogram() {
6772
throw new IllegalStateException("Histogram");

common/src/main/java/org/tron/common/prometheus/MetricLabels.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public static class Counter {
3131
public static final String TXS_FAIL_SIG = "sig";
3232
public static final String TXS_FAIL_TAPOS = "tapos";
3333
public static final String TXS_FAIL_DUP = "dup";
34+
public static final String SR_ADD = "add";
35+
public static final String SR_REMOVE = "remove";
3436

3537
private Counter() {
3638
throw new IllegalStateException("Counter");
@@ -66,6 +68,7 @@ private Gauge() {
6668

6769
// Histogram
6870
public static class Histogram {
71+
public static final String MINER = "miner";
6972
public static final String TRAFFIC_IN = "in";
7073
public static final String TRAFFIC_OUT = "out";
7174

common/src/main/java/org/tron/common/prometheus/MetricsCounter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class MetricsCounter {
1414
init(MetricKeys.Counter.TXS, "tron txs info .", "type", "detail");
1515
init(MetricKeys.Counter.MINER, "tron miner info .", "miner", "type");
1616
init(MetricKeys.Counter.BLOCK_FORK, "tron block fork info .", "type");
17+
init(MetricKeys.Counter.SR_SET_CHANGE, "tron sr set change .", "action", "witness");
1718
init(MetricKeys.Counter.P2P_ERROR, "tron p2p error info .", "type");
1819
init(MetricKeys.Counter.P2P_DISCONNECT, "tron p2p disconnect .", "type");
1920
init(MetricKeys.Counter.INTERNAL_SERVICE_FAIL, "internal Service fail.",

common/src/main/java/org/tron/common/prometheus/MetricsHistogram.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class MetricsHistogram {
2020
init(MetricKeys.Histogram.JSONRPC_SERVICE_LATENCY, "JsonRpc Service latency.",
2121
"method");
2222
init(MetricKeys.Histogram.MINER_LATENCY, "miner latency.",
23-
"miner");
23+
MetricLabels.Histogram.MINER);
2424
init(MetricKeys.Histogram.PING_PONG_LATENCY, "node ping pong latency.");
2525
init(MetricKeys.Histogram.VERIFY_SIGN_LATENCY, "verify sign latency for trx , block.",
2626
"type");
@@ -36,7 +36,7 @@ public class MetricsHistogram {
3636
init(MetricKeys.Histogram.PROCESS_TRANSACTION_LATENCY, "process transaction latency.",
3737
"type", "contract");
3838
init(MetricKeys.Histogram.MINER_DELAY, "miner delay time, actualTime - planTime.",
39-
"miner");
39+
MetricLabels.Histogram.MINER);
4040
init(MetricKeys.Histogram.UDP_BYTES, "udp_bytes traffic.",
4141
"type");
4242
init(MetricKeys.Histogram.TCP_BYTES, "tcp_bytes traffic.",
@@ -48,6 +48,11 @@ public class MetricsHistogram {
4848
init(MetricKeys.Histogram.BLOCK_FETCH_LATENCY, "fetch block latency.");
4949
init(MetricKeys.Histogram.BLOCK_RECEIVE_DELAY,
5050
"receive block delay time, receiveTime - blockTime.");
51+
52+
init(MetricKeys.Histogram.BLOCK_TRANSACTION_COUNT,
53+
"Distribution of transaction counts per block.",
54+
new double[]{0, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000},
55+
MetricLabels.Histogram.MINER);
5156
}
5257

5358
private MetricsHistogram() {
@@ -62,6 +67,17 @@ private static void init(String name, String help, String... labels) {
6267
.register());
6368
}
6469

70+
private static void init(String name, String help, double[] buckets, String... labels) {
71+
Histogram.Builder builder = Histogram.build()
72+
.name(name)
73+
.help(help)
74+
.labelNames(labels);
75+
if (buckets != null && buckets.length > 0) {
76+
builder.buckets(buckets);
77+
}
78+
container.put(name, builder.register());
79+
}
80+
6581
static Histogram.Timer startTimer(String key, String... labels) {
6682
if (Metrics.enabled()) {
6783
Histogram histogram = container.get(key);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.tron.common.prometheus;
2+
3+
import com.google.protobuf.ByteString;
4+
import java.util.List;
5+
import org.tron.common.utils.StringUtil;
6+
7+
public class SRMetrics {
8+
9+
private SRMetrics() {
10+
throw new IllegalStateException("SRMetrics");
11+
}
12+
13+
public static void recordSrSetChange(List<ByteString> currentWits, List<ByteString> newWits) {
14+
if (!Metrics.enabled()) {
15+
return;
16+
}
17+
newWits.stream()
18+
.filter(w -> !currentWits.contains(w))
19+
.forEach(w -> Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1,
20+
MetricLabels.Counter.SR_ADD, StringUtil.encode58Check(w.toByteArray())));
21+
currentWits.stream()
22+
.filter(w -> !newWits.contains(w))
23+
.forEach(w -> Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1,
24+
MetricLabels.Counter.SR_REMOVE, StringUtil.encode58Check(w.toByteArray())));
25+
}
26+
}

consensus/src/main/java/org/tron/consensus/dpos/MaintenanceManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.bouncycastle.util.encoders.Hex;
1717
import org.springframework.beans.factory.annotation.Autowired;
1818
import org.springframework.stereotype.Component;
19+
import org.tron.common.prometheus.SRMetrics;
1920
import org.tron.consensus.ConsensusDelegate;
2021
import org.tron.consensus.pbft.PbftManager;
2122
import org.tron.core.capsule.AccountCapsule;
@@ -141,6 +142,8 @@ public void doMaintenance() {
141142
witnessCapsule.setIsJobs(true);
142143
consensusDelegate.saveWitness(witnessCapsule);
143144
});
145+
146+
SRMetrics.recordSrSetChange(currentWits, newWits);
144147
}
145148

146149
logger.info("Update witness success. \nbefore: {} \nafter: {}",

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@
3131
import static org.tron.core.config.Parameter.DatabaseConstants.MARKET_COUNT_LIMIT_MAX;
3232
import static org.tron.core.config.Parameter.DatabaseConstants.PROPOSAL_COUNT_LIMIT_MAX;
3333
import static org.tron.core.config.Parameter.DatabaseConstants.WITNESS_COUNT_LIMIT_MAX;
34-
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseBlockNumber;
3534
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseEnergyFee;
36-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
37-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.FINALIZED_STR;
38-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.LATEST_STR;
39-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.PENDING_STR;
40-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR;
4135
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
4236
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;
4337
import static org.tron.protos.contract.Common.ResourceCode;
@@ -194,7 +188,6 @@
194188
import org.tron.core.exception.VMIllegalException;
195189
import org.tron.core.exception.ValidateSignatureException;
196190
import org.tron.core.exception.ZksnarkException;
197-
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
198191
import org.tron.core.net.TronNetDelegate;
199192
import org.tron.core.net.TronNetService;
200193
import org.tron.core.net.message.adv.TransactionMessage;
@@ -711,6 +704,10 @@ public long getSolidBlockNum() {
711704
return chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
712705
}
713706

707+
public long getHeadBlockNum() {
708+
return chainBaseManager.getHeadBlockNum();
709+
}
710+
714711
public BlockCapsule getBlockCapsuleByNum(long blockNum) {
715712
try {
716713
return chainBaseManager.getBlockByNum(blockNum);
@@ -733,31 +730,6 @@ public long getTransactionCountByBlockNum(long blockNum) {
733730
return count;
734731
}
735732

736-
public Block getByJsonBlockId(String id) throws JsonRpcInvalidParamsException {
737-
if (EARLIEST_STR.equalsIgnoreCase(id)) {
738-
return getBlockByNum(0);
739-
} else if (LATEST_STR.equalsIgnoreCase(id)) {
740-
return getNowBlock();
741-
} else if (FINALIZED_STR.equalsIgnoreCase(id)) {
742-
return getSolidBlock();
743-
} else if (PENDING_STR.equalsIgnoreCase(id)) {
744-
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
745-
} else {
746-
long blockNumber = parseBlockNumber(id);
747-
return getBlockByNum(blockNumber);
748-
}
749-
}
750-
751-
public List<Transaction> getTransactionsByJsonBlockId(String id)
752-
throws JsonRpcInvalidParamsException {
753-
if (PENDING_STR.equalsIgnoreCase(id)) {
754-
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
755-
} else {
756-
Block block = getByJsonBlockId(id);
757-
return block != null ? block.getTransactionsList() : null;
758-
}
759-
}
760-
761733
public WitnessList getWitnessList() {
762734
WitnessList.Builder builder = WitnessList.newBuilder();
763735
List<WitnessCapsule> witnessCapsuleList = chainBaseManager.getWitnessStore().getAllWitnesses();

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,11 @@ public void pushBlock(final BlockCapsule block)
12701270
synchronized (this) {
12711271
Metrics.histogramObserve(blockedTimer.get());
12721272
blockedTimer.remove();
1273+
if (Metrics.enabled()) {
1274+
Metrics.histogramObserve(MetricKeys.Histogram.BLOCK_TRANSACTION_COUNT,
1275+
block.getTransactions().size(),
1276+
StringUtil.encode58Check(block.getWitnessAddress().toByteArray()));
1277+
}
12731278
long headerNumber = getDynamicPropertiesStore().getLatestBlockHeaderNumber();
12741279
if (block.getNum() <= headerNumber && khaosDb.containBlockInMiniStore(block.getBlockId())) {
12751280
logger.info("Block {} is already exist.", block.getBlockId().getString());

framework/src/main/java/org/tron/core/metrics/blockchain/BlockChainMetricManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ public void applyBlock(BlockCapsule block) {
164164
}
165165

166166
//TPS
167-
if (block.getTransactions().size() > 0) {
168-
MetricsUtil.meterMark(MetricsKey.BLOCKCHAIN_TPS, block.getTransactions().size());
169-
Metrics.counterInc(MetricKeys.Counter.TXS, block.getTransactions().size(),
167+
int txCount = block.getTransactions().size();
168+
if (txCount > 0) {
169+
MetricsUtil.meterMark(MetricsKey.BLOCKCHAIN_TPS, txCount);
170+
Metrics.counterInc(MetricKeys.Counter.TXS, txCount,
170171
MetricLabels.Counter.TXS_SUCCESS, MetricLabels.Counter.TXS_SUCCESS);
171172
}
172173
}

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

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package org.tron.core.services.jsonrpc;
22

3-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
4-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.FINALIZED_STR;
5-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.LATEST_STR;
6-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.PENDING_STR;
7-
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR;
8-
93
import com.google.common.base.Throwables;
104
import com.google.common.primitives.Longs;
115
import com.google.protobuf.Any;
@@ -58,6 +52,15 @@
5852
@Slf4j(topic = "API")
5953
public class JsonRpcApiUtil {
6054

55+
public static final String EARLIEST_STR = "earliest";
56+
public static final String PENDING_STR = "pending";
57+
public static final String LATEST_STR = "latest";
58+
public static final String FINALIZED_STR = "finalized";
59+
public static final String SAFE_STR = "safe";
60+
public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported";
61+
public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported";
62+
public static final String BLOCK_NUM_ERROR = "invalid block number";
63+
6164
public static byte[] convertToTronAddress(byte[] address) {
6265
byte[] newAddress = new byte[21];
6366
byte[] temp = new byte[] {Wallet.getAddressPreFixByte()};
@@ -524,22 +527,20 @@ public static long parseEnergyFee(long timestamp, String energyPriceHistory) {
524527
*/
525528
private static final int MAX_BLOCK_NUM_HEX_LEN = 100;
526529

527-
private static final String BLOCK_NUM_ERROR = "invalid block number";
528-
529530
/**
530531
* Parse a JSON-RPC block number (hex "0x..." or decimal) into a long,
531532
* enforcing the {@link #MAX_BLOCK_NUM_HEX_LEN} length limit, rejecting
532533
* negative values, and rejecting values that overflow a signed 64-bit
533534
* block number.
534535
*/
535-
public static long parseBlockNumber(String blockNumOrTag)
536+
public static long parseBlockNumber(String blockNum)
536537
throws JsonRpcInvalidParamsException {
537-
if (blockNumOrTag == null || blockNumOrTag.length() > MAX_BLOCK_NUM_HEX_LEN) {
538+
if (blockNum == null || blockNum.length() > MAX_BLOCK_NUM_HEX_LEN) {
538539
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
539540
}
540541
BigInteger value;
541542
try {
542-
value = ByteArray.hexToBigInteger(blockNumOrTag);
543+
value = ByteArray.hexToBigInteger(blockNum);
543544
} catch (Exception e) {
544545
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
545546
}
@@ -553,20 +554,54 @@ public static long parseBlockNumber(String blockNumOrTag)
553554
}
554555
}
555556

556-
public static long getByJsonBlockId(String blockNumOrTag, Wallet wallet)
557+
/**
558+
* Parse a block tag or hex block number. Tags resolve via parseBlockTag;
559+
* numeric inputs go through ByteArray.jsonHexToLong (strict 0x prefix).
560+
* Pathologically long inputs are rejected up front to avoid worst-case
561+
* parsing cost (DoS guard mirroring single-arg parseBlockNumber).
562+
*/
563+
public static long parseBlockNumber(String blockNumOrTag, Wallet wallet)
557564
throws JsonRpcInvalidParamsException {
558-
if (PENDING_STR.equalsIgnoreCase(blockNumOrTag)) {
559-
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
565+
if (isBlockTag(blockNumOrTag)) {
566+
return parseBlockTag(blockNumOrTag, wallet);
567+
}
568+
569+
return parseBlockNumber(blockNumOrTag);
570+
}
571+
572+
public static boolean isBlockTag(String tag) {
573+
return LATEST_STR.equalsIgnoreCase(tag)
574+
|| EARLIEST_STR.equalsIgnoreCase(tag)
575+
|| FINALIZED_STR.equalsIgnoreCase(tag)
576+
|| PENDING_STR.equalsIgnoreCase(tag)
577+
|| SAFE_STR.equalsIgnoreCase(tag);
578+
}
579+
580+
/**
581+
* Parse a block tag (latest, earliest, finalized) to block number.
582+
*
583+
* <p>Note: for "latest", the returned block number may not yet be available in
584+
* blockStore or blockIndexStore due to write ordering. Callers that need the
585+
* actual block must handle the not-found case.</p>
586+
*/
587+
public static long parseBlockTag(String tag, Wallet wallet)
588+
throws JsonRpcInvalidParamsException {
589+
if (LATEST_STR.equalsIgnoreCase(tag)) {
590+
return wallet.getHeadBlockNum();
560591
}
561-
if (StringUtils.isEmpty(blockNumOrTag) || LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
562-
return -1;
563-
} else if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)) {
592+
if (EARLIEST_STR.equalsIgnoreCase(tag)) {
564593
return 0;
565-
} else if (FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
594+
}
595+
if (FINALIZED_STR.equalsIgnoreCase(tag)) {
566596
return wallet.getSolidBlockNum();
567-
} else {
568-
return ByteArray.jsonHexToLong(blockNumOrTag);
569597
}
598+
if (PENDING_STR.equalsIgnoreCase(tag)) {
599+
throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR);
600+
}
601+
if (SAFE_STR.equalsIgnoreCase(tag)) {
602+
throw new JsonRpcInvalidParamsException(TAG_SAFE_SUPPORT_ERROR);
603+
}
604+
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
570605
}
571606

572607
public static String generateFilterId() {

0 commit comments

Comments
 (0)