From 45e3bf88ca73dacee93477572e1775fa499e0639 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Tue, 13 Jan 2026 01:16:39 +0800 Subject: [PATCH 01/57] feat(*): disable exchange transaction (#6507) --- .../java/org/tron/core/config/Parameter.java | 5 +- .../main/java/org/tron/core/db/Manager.java | 30 +++++ .../main/java/org/tron/program/Version.java | 2 +- .../ExchangeTransactionActuatorTest.java | 105 +++++++++++++++++- 4 files changed, 138 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/org/tron/core/config/Parameter.java b/common/src/main/java/org/tron/core/config/Parameter.java index a71dc58e8bd..c0e1bb06470 100644 --- a/common/src/main/java/org/tron/core/config/Parameter.java +++ b/common/src/main/java/org/tron/core/config/Parameter.java @@ -26,7 +26,8 @@ public enum ForkBlockVersionEnum { VERSION_4_7_4(29, 1596780000000L, 80), VERSION_4_7_5(30, 1596780000000L, 80), VERSION_4_7_7(31, 1596780000000L, 80), - VERSION_4_8_0(32, 1596780000000L, 80); + VERSION_4_8_0(32, 1596780000000L, 80), + VERSION_4_8_0_1(33, 1596780000000L, 70); // if add a version, modify BLOCK_VERSION simultaneously @Getter @@ -75,7 +76,7 @@ public class ChainConstant { public static final int SINGLE_REPEAT = 1; public static final int BLOCK_FILLED_SLOTS_NUMBER = 128; public static final int MAX_FROZEN_NUMBER = 1; - public static final int BLOCK_VERSION = 32; + public static final int BLOCK_VERSION = 33; public static final long FROZEN_PERIOD = 86_400_000L; public static final long DELEGATE_PERIOD = 3 * 86_400_000L; public static final long TRX_PRECISION = 1000_000L; diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 1eecc103874..8c14f280077 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -97,6 +97,7 @@ import org.tron.core.capsule.TransactionRetCapsule; import org.tron.core.capsule.WitnessCapsule; import org.tron.core.capsule.utils.TransactionUtil; +import org.tron.core.config.Parameter; import org.tron.core.config.Parameter.ChainConstant; import org.tron.core.config.args.Args; import org.tron.core.consensus.ProposalController; @@ -873,6 +874,11 @@ public boolean pushTransaction(final TransactionCapsule trx) return true; } + if (isExchangeTransaction(trx.getInstance())) { + throw new ContractValidateException("ExchangeTransactionContract is rejected"); + } + + pushTransactionQueue.add(trx); Metrics.gaugeInc(MetricKeys.Gauge.MANAGER_QUEUE, 1, MetricLabels.Gauge.QUEUE_QUEUED); @@ -1676,6 +1682,11 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) { accountSet.add(ownerAddress); } } + + if (isExchangeTransaction(transaction)) { + continue; + } + if (ownerAddressSet.contains(ownerAddress)) { trx.setVerified(false); } @@ -1749,6 +1760,24 @@ private boolean isShieldedTransaction(Transaction transaction) { } } + private boolean isExchangeTransaction(Transaction transaction) { + Contract contract = transaction.getRawData().getContract(0); + switch (contract.getType()) { + case ExchangeTransactionContract: { + return true; + } + default: + return false; + } + } + + private void rejectExchangeTransaction(Transaction transaction) throws ContractValidateException { + if (isExchangeTransaction(transaction) && chainBaseManager.getForkController() + .pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_0_1)) { + throw new ContractValidateException("ExchangeTransactionContract is rejected"); + } + } + public TransactionStore getTransactionStore() { return chainBaseManager.getTransactionStore(); } @@ -1803,6 +1832,7 @@ private void processBlock(BlockCapsule block, List txs) List results = new ArrayList<>(); long num = block.getNum(); for (TransactionCapsule transactionCapsule : block.getTransactions()) { + rejectExchangeTransaction(transactionCapsule.getInstance()); if (chainBaseManager.getDynamicPropertiesStore().allowConsensusLogicOptimization() && transactionCapsule.retCountIsGreatThanContractCount()) { throw new BadBlockException(String.format("The result count %d of this transaction %s is " diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 4e9528ee50e..5bd9217ee09 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -4,7 +4,7 @@ public class Version { public static final String VERSION_NAME = "GreatVoyage-v4.7.7-243-gb3555dd655"; public static final String VERSION_CODE = "18631"; - private static final String VERSION = "4.8.0"; + private static final String VERSION = "4.8.0.1"; public static String getVersion() { return VERSION; diff --git a/framework/src/test/java/org/tron/core/actuator/ExchangeTransactionActuatorTest.java b/framework/src/test/java/org/tron/core/actuator/ExchangeTransactionActuatorTest.java index d39706e0699..fbce246101e 100644 --- a/framework/src/test/java/org/tron/core/actuator/ExchangeTransactionActuatorTest.java +++ b/framework/src/test/java/org/tron/core/actuator/ExchangeTransactionActuatorTest.java @@ -1,10 +1,13 @@ package org.tron.core.actuator; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import static org.tron.core.config.Parameter.ChainSymbol.TRX_SYMBOL_BYTES; import com.google.protobuf.Any; import com.google.protobuf.ByteString; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Map; import junit.framework.TestCase; @@ -13,18 +16,30 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.crypto.ECKey; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.ForkController; +import org.tron.common.utils.PublicMethod; +import org.tron.consensus.base.Param; +import org.tron.consensus.base.Param.Miner; import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.AssetIssueCapsule; +import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.ExchangeCapsule; +import org.tron.core.capsule.TransactionCapsule; import org.tron.core.capsule.TransactionResultCapsule; +import org.tron.core.capsule.WitnessCapsule; +import org.tron.core.config.Parameter; +import org.tron.core.config.Parameter.ForkBlockVersionEnum; import org.tron.core.config.args.Args; +import org.tron.core.db.Manager; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; import org.tron.core.exception.ItemNotFoundException; import org.tron.protos.Protocol.AccountType; +import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.Protocol.Transaction.Result.code; import org.tron.protos.contract.AssetIssueContractOuterClass; import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract; @@ -1674,7 +1689,7 @@ public void noContract() { public void invalidContractType() { ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(); // create AssetIssueContract, not a valid ClearABI contract , which will throw e expectipon - Any invalidContractTypes = Any.pack(AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + Any invalidContractTypes = Any.pack(AssetIssueContract.newBuilder() .build()); actuator.setChainBaseManager(dbManager.getChainBaseManager()) .setAny(invalidContractTypes); @@ -1725,4 +1740,92 @@ private void processAndCheckInvalid(ExchangeTransactionActuator actuator, } } + /** + * isExchangeTransaction + */ + @Test + public void isExchangeTransactionPush() { + try { + TransactionCapsule transactionCap = new TransactionCapsule( + ExchangeTransactionContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) + .setExchangeId(1) + .setTokenId(ByteString.copyFrom("_".getBytes())) + .setQuant(1) + .setExpected(1) + .build(), ContractType.ExchangeTransactionContract); + dbManager.pushTransaction(transactionCap); + + } catch (Exception e) { + Assert.assertTrue(true); + } + } + + @Test + public void isExchangeTransactionGenerate() { + try { + + String key = PublicMethod.getRandomPrivateKey(); + byte[] privateKey = ByteArray.fromHexString(key); + final ECKey ecKey = ECKey.fromPrivate(privateKey); + byte[] address = ecKey.getAddress(); + WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address)); + + String OWNER_ADDRESS_SECOND = + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; + TransactionCapsule transactionCap = new TransactionCapsule( + ExchangeTransactionContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) + .setExchangeId(1) + .setTokenId(ByteString.copyFrom("_".getBytes())) + .setQuant(1) + .setExpected(1) + .build(), ContractType.ExchangeTransactionContract); + dbManager.getPendingTransactions().add(transactionCap); + Param param = Param.getInstance(); + Miner miner = param.new Miner(privateKey, witnessCapsule.getAddress(), + witnessCapsule.getAddress()); + BlockCapsule blockCapsule = dbManager + .generateBlock(miner, 1533529947843L, System.currentTimeMillis() + 1000); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + @Test + public void rejectExchangeTransaction() { + try { + long maintenanceTimeInterval = dbManager.getDynamicPropertiesStore() + .getMaintenanceTimeInterval(); + long hardForkTime = + ((ForkBlockVersionEnum.VERSION_4_0_1.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + dbManager.getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + dbManager.getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0_1.getValue(), stats); + boolean flag = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_0_1); + Assert.assertTrue(flag); + String OWNER_ADDRESS_SECOND = + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; + TransactionCapsule transactionCap = new TransactionCapsule( + ExchangeTransactionContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) + .setExchangeId(1) + .setTokenId(ByteString.copyFrom("_".getBytes())) + .setQuant(1) + .setExpected(1) + .build(), ContractType.ExchangeTransactionContract); + Method rejectExchangeTransaction = Manager.class.getDeclaredMethod( + "rejectExchangeTransaction", org.tron.protos.Protocol.Transaction.class); + rejectExchangeTransaction.setAccessible(true); + Exception ex = assertThrows(InvocationTargetException.class, () -> { + rejectExchangeTransaction.invoke(dbManager, transactionCap.getInstance()); + }); + } catch (Exception e) { + fail(); + } + } } From 25b35f514833c0a7024e42beeb2c03c539580138 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Tue, 13 Jan 2026 01:23:47 +0800 Subject: [PATCH 02/57] update a new version. version name:GreatVoyage-v4.8.0-1-g45e3bf88ca,version code:18634 (#6508) --- framework/src/main/java/org/tron/program/Version.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 5bd9217ee09..6dde9a8e52c 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -2,8 +2,8 @@ public class Version { - public static final String VERSION_NAME = "GreatVoyage-v4.7.7-243-gb3555dd655"; - public static final String VERSION_CODE = "18631"; + public static final String VERSION_NAME = "GreatVoyage-v4.8.0-1-g45e3bf88ca"; + public static final String VERSION_CODE = "18634"; private static final String VERSION = "4.8.0.1"; public static String getVersion() { From 44a4bc826383f089e03351124edebf4190d41eb4 Mon Sep 17 00:00:00 2001 From: YAaron <4241080+kuny0707@users.noreply.github.com> Date: Wed, 4 Feb 2026 11:25:01 +0800 Subject: [PATCH 03/57] Merge release_v4.8.1 to master (#6541) --- README.md | 243 +++-- .../core/actuator/AssetIssueActuator.java | 12 + .../core/actuator/ProposalCreateActuator.java | 3 +- .../actuator/ShieldedTransferActuator.java | 34 +- .../org/tron/core/utils/ProposalUtil.java | 38 +- .../java/org/tron/core/vm/EnergyCost.java | 9 + .../org/tron/core/vm/OperationActions.java | 15 + .../org/tron/core/vm/OperationRegistry.java | 11 + .../tron/core/vm/PrecompiledContracts.java | 56 +- .../src/main/java/org/tron/core/vm/VM.java | 5 +- .../java/org/tron/core/vm/VMConstant.java | 1 + .../org/tron/core/vm/config/ConfigLoader.java | 1 + .../tron/core/vm/program/ContractState.java | 10 + .../java/org/tron/core/vm/program/Memory.java | 2 +- .../org/tron/core/vm/program/Program.java | 114 ++- .../vm/program/invoke/ProgramInvokeImpl.java | 5 +- .../tron/core/vm/repository/Repository.java | 4 + .../core/vm/repository/RepositoryImpl.java | 33 + .../org/tron/core/vm/repository/Type.java | 2 +- .../org/tron/core/vm/repository/Value.java | 2 +- .../vm/repository/WriteOptionsWrapper.java | 24 - build.gradle | 92 +- chainbase/build.gradle | 1 - .../common/storage/WriteOptionsWrapper.java | 29 +- .../leveldb/LevelDbDataSourceImpl.java | 94 +- .../rocksdb/RocksDbDataSourceImpl.java | 476 ++++----- .../org/tron/common/utils/LocalWitnesses.java | 37 +- .../org/tron/common/utils/StorageUtils.java | 16 +- .../tron/common/zksnark/JLibrustzcash.java | 107 +- .../org/tron/common/zksnark/JLibsodium.java | 35 +- .../tron/core/capsule/TransactionCapsule.java | 11 +- .../tron/core/capsule/utils/MarketUtils.java | 79 +- .../java/org/tron/core/db/TronDatabase.java | 26 +- .../tron/core/db/TronStoreWithRevoking.java | 21 +- .../tron/core/db/common/DbSourceInter.java | 55 +- .../db/common/iterator/RockStoreIterator.java | 13 +- .../db/common/iterator/StoreIterator.java | 5 + .../org/tron/core/db2/common/LevelDB.java | 3 +- .../org/tron/core/db2/common/RocksDB.java | 5 +- .../org/tron/core/db2/common/TxCacheDB.java | 11 +- .../tron/core/db2/core/SnapshotManager.java | 13 +- .../org/tron/core/store/AccountStore.java | 10 +- .../tron/core/store/CheckPointV2Store.java | 17 +- .../core/store/DynamicPropertiesStore.java | 34 + .../store/MarketPairPriceToOrderStore.java | 23 +- .../org/tron/core/store/WitnessStore.java | 2 +- common/build.gradle | 39 +- .../org/tron/common/exit/ExitManager.java | 2 +- .../java/org/tron/common/log/LogService.java | 5 +- .../tron/common/logsfilter/FilterQuery.java | 14 +- .../common/parameter/CommonParameter.java | 33 +- .../tron/common/setting/RocksDbSettings.java | 123 ++- .../java/org/tron/common/utils/ByteArray.java | 2 +- .../org/tron/common/utils/StringUtil.java | 17 + .../tron/common/utils/TimeoutInterceptor.java | 30 + .../src/main/java/org/tron/core/Constant.java | 19 + .../org/tron/core/config/CommonConfig.java | 2 - .../java/org/tron/core/config/Parameter.java | 6 +- .../org/tron/core/config/args/Storage.java | 9 + .../MaintenanceUnavailableException.java | 20 + .../org/tron/core/exception/P2pException.java | 1 + .../org/tron/core/exception/TronError.java | 4 +- .../jsonrpc/JsonRpcExceedLimitException.java | 16 + .../exception/jsonrpc/JsonRpcException.java | 32 + .../JsonRpcInternalException.java | 8 +- .../JsonRpcInvalidParamsException.java | 4 +- .../JsonRpcInvalidRequestException.java | 4 +- .../JsonRpcMethodNotFoundException.java | 4 +- .../JsonRpcTooManyResultException.java | 4 +- .../org/tron/core/vm/config/VMConfig.java | 10 + .../java/org/tron/common/crypto/ECKey.java | 3 +- .../main/java/org/tron/common/crypto/Rsv.java | 26 + .../tron/common/crypto/cryptohash/Digest.java | 2 +- .../java/org/tron/common/crypto/sm2/SM2.java | 3 +- .../org/tron/common/crypto/sm2/SM2Signer.java | 2 +- .../org/tron/common/crypto/zksnark/Fp12.java | 2 +- .../org/tron/common/crypto/zksnark/Fp2.java | 2 +- .../org/tron/common/crypto/zksnark/Fp6.java | 2 +- docker/arm64/Dockerfile | 33 + framework/build.gradle | 53 +- .../tron/common/application/RpcService.java | 31 +- .../application/TronApplicationContext.java | 2 + .../org/tron/common/backup/BackupManager.java | 2 +- .../common/logsfilter/EventPluginLoader.java | 10 +- .../src/main/java/org/tron/core/Wallet.java | 234 +++-- .../java/org/tron/core/config/args/Args.java | 189 ++-- .../core/config/args/WitnessInitializer.java | 149 +++ .../tron/core/consensus/ConsensusService.java | 7 +- .../tron/core/consensus/ProposalService.java | 8 + .../main/java/org/tron/core/db/Manager.java | 119 ++- .../core/metrics/node/NodeMetricManager.java | 7 +- .../tron/core/net/P2pEventHandlerImpl.java | 10 +- .../org/tron/core/net/P2pRateLimiter.java | 32 + .../net/message/handshake/HelloMessage.java | 7 +- .../FetchInvDataMsgHandler.java | 97 +- .../SyncBlockChainMsgHandler.java | 8 + .../TransactionsMsgHandler.java | 5 +- .../tron/core/net/peer/PeerConnection.java | 23 +- .../tron/core/net/peer/PeerStatusCheck.java | 4 + .../service/handshake/HandshakeService.java | 20 +- .../core/net/service/relay/RelayService.java | 8 +- .../core/net/service/sync/SyncService.java | 11 +- .../org/tron/core/services/RpcApiService.java | 102 +- .../core/services/event/BlockEventCache.java | 9 +- .../core/services/event/BlockEventGet.java | 53 +- .../services/event/HistoryEventService.java | 19 +- .../services/event/RealtimeEventService.java | 43 +- .../services/event/SolidEventService.java | 37 +- .../services/filter/HttpApiAccessFilter.java | 3 +- .../core/services/filter/HttpInterceptor.java | 3 +- .../filter/LiteFnQueryHttpFilter.java | 3 +- .../services/http/FullNodeHttpApiService.java | 6 + .../GetPaginatedNowWitnessListServlet.java | 52 + .../services/http/GetProposalByIdServlet.java | 2 +- .../services/http/RateLimiterServlet.java | 5 +- .../solidity/SolidityNodeHttpApiService.java | 5 + .../http/PBFT/HttpApiOnPBFTService.java | 2 +- .../RpcApiServiceOnSolidity.java | 7 + ...inatedNowWitnessListOnSolidityServlet.java | 24 + .../solidity/HttpApiOnSolidityService.java | 8 +- .../core/services/jsonrpc/JsonRpcApiUtil.java | 2 +- .../jsonrpc/JsonRpcErrorResolver.java | 81 ++ .../core/services/jsonrpc/JsonRpcServlet.java | 1 + .../core/services/jsonrpc/TronJsonRpc.java | 56 +- .../services/jsonrpc/TronJsonRpcImpl.java | 221 +++- .../jsonrpc/filters/LogBlockQuery.java | 84 +- .../services/jsonrpc/filters/LogFilter.java | 2 +- .../jsonrpc/filters/LogFilterAndResult.java | 2 +- .../jsonrpc/filters/LogFilterWrapper.java | 2 +- .../services/jsonrpc/filters/LogMatch.java | 16 +- .../jsonrpc/types/BuildArguments.java | 4 +- .../services/jsonrpc/types/CallArguments.java | 4 +- .../jsonrpc/types/TransactionReceipt.java | 206 ++-- .../jsonrpc/types/TransactionResult.java | 16 +- .../org/tron/core/zen/ZksnarkInitService.java | 44 +- .../main/java/org/tron/program/DBConvert.java | 413 -------- .../main/java/org/tron/program/FullNode.java | 14 +- .../org/tron/program/KeystoreFactory.java | 12 +- .../java/org/tron/program/SolidityNode.java | 27 +- .../main/java/org/tron/program/Version.java | 2 +- .../src/main/resources/config-localtest.conf | 1 + framework/src/main/resources/config.conf | 462 ++++++--- .../http/InvalidMediaTypeException.java | 61 ++ .../org/springframework/http/MediaType.java | 841 +++++++++++++++ .../test/java/org/tron/common/BaseTest.java | 8 + .../test/java/org/tron/common/EntityTest.java | 39 + .../java/org/tron/common/ParameterTest.java | 6 + .../tron/common/backup/BackupServerTest.java | 3 +- .../org/tron/common/crypto/ECKeyTest.java | 6 + .../org/tron/common/crypto/SM2KeyTest.java | 6 + .../logsfilter/NativeMessageQueueTest.java | 19 +- .../vm/BatchValidateSignContractTest.java | 8 + .../tron/common/runtime/vm/Create2Test.java | 2 +- .../common/runtime/vm/OperationsTest.java | 121 +++ .../vm/ValidateMultiSignContractTest.java | 8 + .../common/storage/CheckOrInitEngineTest.java | 263 +++++ .../leveldb/LevelDbDataSourceImplTest.java | 182 +++- .../RocksDbDataSourceImplTest.java | 186 +++- .../org/tron/common/utils/FileUtilTest.java | 1 + .../org/tron/common/utils/HashCodeTest.java | 23 + .../tron/common/utils/ObjectSizeUtilTest.java | 62 -- .../common/utils/client/Configuration.java | 11 +- .../common/utils/client/utils/HttpMethed.java | 2 +- .../utils/client/utils/TransactionUtils.java | 15 +- .../java/org/tron/core/CoreExceptionTest.java | 10 +- .../core/CreateCommonTransactionTest.java | 47 - .../java/org/tron/core/ShieldWalletTest.java | 20 +- .../tron/core/ShieldedTRC20BuilderTest.java | 14 +- .../java/org/tron/core/WalletMockTest.java | 6 +- .../test/java/org/tron/core/WalletTest.java | 153 +++ .../core/actuator/AssetIssueActuatorTest.java | 50 + .../ShieldedTransferActuatorTest.java | 4 +- .../core/actuator/utils/ProposalUtilTest.java | 104 ++ .../actuator/vm/ProgramTraceListenerTest.java | 28 +- .../core/actuator/vm/SerializersTest.java | 12 + .../tron/core/capsule/BlockCapsuleTest.java | 2 +- .../capsule/utils/ExchangeProcessorTest.java | 65 +- .../org/tron/core/config/args/ArgsTest.java | 11 +- .../core/config/args/LocalWitnessTest.java | 121 ++- .../config/args/WitnessInitializerTest.java | 268 +++++ .../org/tron/core/db/AccountStoreTest.java | 24 + .../java/org/tron/core/db/DBIteratorTest.java | 5 +- .../java/org/tron/core/db/ManagerTest.java | 78 +- .../tron/core/db/TransactionExpireTest.java | 10 +- .../org/tron/core/db/WitnessStoreTest.java | 4 +- .../java/org/tron/core/db2/ChainbaseTest.java | 5 +- .../org/tron/core/db2/CheckpointV2Test.java | 14 +- .../db2/RevokingDbWithCacheNewValueTest.java | 13 +- .../org/tron/core/db2/SnapshotImplTest.java | 15 +- .../tron/core/db2/SnapshotManagerTest.java | 14 +- .../org/tron/core/db2/SnapshotRootTest.java | 27 +- .../tron/core/event/BlockEventCacheTest.java | 24 +- .../tron/core/event/BlockEventGetTest.java | 69 +- .../core/event/HistoryEventServiceTest.java | 3 +- .../tron/core/exception/TronErrorTest.java | 100 +- .../org/tron/core/jsonrpc/ApiUtilTest.java | 13 + .../java/org/tron/core/jsonrpc/BloomTest.java | 2 +- .../core/jsonrpc/ConcurrentHashMapTest.java | 10 +- .../org/tron/core/jsonrpc/JsonRpcTest.java | 2 +- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 135 ++- .../tron/core/jsonrpc/LogBlockQueryTest.java | 108 ++ .../core/jsonrpc/LogMatchExactlyTest.java | 15 +- .../core/jsonrpc/SectionBloomStoreTest.java | 2 + .../prometheus/PrometheusApiServiceTest.java | 2 +- .../test/java/org/tron/core/net/BaseNet.java | 134 --- .../java/org/tron/core/net/BaseNetTest.java | 16 - .../core/net/P2pEventHandlerImplTest.java | 14 +- .../org/tron/core/net/P2pRateLimiterTest.java | 23 + .../FetchInvDataMsgHandlerTest.java | 66 ++ .../messagehandler/MessageHandlerTest.java | 15 +- .../messagehandler/PbftMsgHandlerTest.java | 15 +- .../SyncBlockChainMsgHandlerTest.java | 48 +- .../core/net/peer/PeerConnectionTest.java | 16 + .../tron/core/net/peer/PeerManagerTest.java | 24 + .../nodepersist/NodePersistServiceTest.java | 40 + .../core/net/services/AdvServiceTest.java | 3 + .../services/EffectiveCheckServiceTest.java | 43 +- .../net/services/HandShakeServiceTest.java | 88 +- .../core/net/services/RelayServiceTest.java | 122 ++- .../net/services/ResilienceServiceTest.java | 67 +- .../core/net/services/SyncServiceTest.java | 11 + .../core/services/DelegationServiceTest.java | 81 +- .../core/services/NodeInfoServiceTest.java | 70 +- .../core/services/ProposalServiceTest.java | 20 + .../core/services/RpcApiServicesTest.java | 79 +- .../org/tron/core/services/WalletApiTest.java | 43 +- .../filter/HttpApiAccessFilterTest.java | 2 +- .../LiteFnQueryGrpcInterceptorTest.java | 38 +- .../filter/LiteFnQueryHttpFilterTest.java | 2 +- .../filter/RpcApiAccessInterceptorTest.java | 36 +- .../services/http/GetRewardServletTest.java | 8 +- .../core/services/http/HttpServletTest.java | 4 + .../services/jsonrpc/BuildArgumentsTest.java | 4 +- .../services/jsonrpc/CallArgumentsTest.java | 4 +- .../jsonrpc/JsonRpcErrorResolverTest.java | 75 ++ .../jsonrpc/TransactionReceiptTest.java | 69 +- .../jsonrpc/TransactionResultTest.java | 44 +- .../core/services/stop/BlockTimeStopTest.java | 6 +- .../services/stop/ConditionallyStopTest.java | 113 +-- .../tron/core/zksnark/LibrustzcashTest.java | 13 +- .../tron/core/zksnark/SaplingNoteTest.java | 2 +- .../tron/core/zksnark/SendCoinShieldTest.java | 38 +- .../core/zksnark/ShieldedReceiveTest.java | 60 +- .../org/tron/keystroe/CredentialsTest.java | 23 +- .../java/org/tron/program/DBConvertTest.java | 116 --- .../org/tron/program/SolidityNodeTest.java | 26 +- .../src/test/resources/config-localtest.conf | 3 +- .../src/test/resources/config-test-index.conf | 1 + .../test/resources/config-test-mainnet.conf | 1 + framework/src/test/resources/config-test.conf | 5 +- framework/src/test/resources/logback-test.xml | 4 +- gradle/jdk17/java-tron.vmoptions | 8 + gradle/verification-metadata.xml | 913 +++++++++-------- install_dependencies.sh | 957 ++++++++++++++++++ jitpack.yml | 2 +- platform/build.gradle | 17 + .../arm/org/tron/common/math/MathWrapper.java | 254 +++++ .../MarketOrderPriceComparatorForRocksDB.java | 32 + .../common/org/tron/common/arch/Arch.java | 91 ++ .../tron/common/utils/MarketComparator.java | 114 +-- .../MarketOrderPriceComparatorForLevelDB.java | 4 +- .../org/tron/common/math/MathWrapper.java | 0 .../MarketOrderPriceComparatorForRocksDB.java | 7 +- plugins/README.md | 12 +- plugins/build.gradle | 43 +- .../arm/org/tron/plugins/ArchiveManifest.java | 29 + .../java/arm/org/tron/plugins/DbArchive.java | 50 + .../{ => common}/org/tron/plugins/Db.java | 0 .../org/tron/plugins/DbConvert.java | 59 +- .../{ => common}/org/tron/plugins/DbCopy.java | 0 .../{ => common}/org/tron/plugins/DbLite.java | 11 +- .../{ => common}/org/tron/plugins/DbMove.java | 0 .../{ => common}/org/tron/plugins/DbRoot.java | 24 +- .../org/tron/plugins/Toolkit.java | 0 .../org/tron/plugins/utils/ByteArray.java | 0 .../org/tron/plugins/utils/CryptoUitls.java | 0 .../org/tron/plugins/utils/DBUtils.java | 57 +- .../org/tron/plugins/utils/FileUtils.java | 0 .../org/tron/plugins/utils/MarketUtils.java | 68 ++ .../org/tron/plugins/utils/MerkleRoot.java | 0 .../org/tron/plugins/utils/Sha256Hash.java | 0 .../tron/plugins/utils/db/DBInterface.java | 39 + .../org/tron/plugins/utils/db/DBIterator.java | 0 .../org/tron/plugins/utils/db/DbTool.java | 24 +- .../tron/plugins/utils/db/LevelDBImpl.java | 7 +- .../plugins/utils/db/LevelDBIterator.java | 0 .../tron/plugins/utils/db/RockDBIterator.java | 12 +- .../tron/plugins/utils/db/RocksDBImpl.java | 97 ++ .../MarketOrderPriceComparatorForLevelDB.java | 28 - .../MarketOrderPriceComparatorForRockDB.java | 38 - .../tron/plugins/utils/db/DBInterface.java | 23 - .../tron/plugins/utils/db/RocksDBImpl.java | 69 -- .../org/tron/plugins/ArchiveManifest.java | 3 +- .../{ => x86}/org/tron/plugins/DbArchive.java | 3 +- .../java/org/tron/plugins/DbCopyTest.java | 19 +- .../java/org/tron/plugins/DbLiteTest.java | 32 +- .../java/org/tron/plugins/DbMoveTest.java | 79 +- .../java/org/tron/plugins/DbRootTest.java | 21 +- .../test/java/org/tron/plugins/DbTest.java | 44 +- .../{ => leveldb}/ArchiveManifestTest.java | 46 +- .../plugins/{ => leveldb}/DbArchiveTest.java | 45 +- .../plugins/{ => leveldb}/DbConvertTest.java | 16 +- .../{ => leveldb}/DbLiteLevelDbTest.java | 3 +- .../{ => leveldb}/DbLiteLevelDbV2Test.java | 3 +- .../{ => rocksdb}/DbLiteRocksDbTest.java | 3 +- .../plugins/rocksdb/DbLiteRocksDbV2Test.java | 13 + plugins/src/test/resources/config.conf | 6 - protocol/build.gradle | 7 +- protocol/src/main/protos/api/api.proto | 6 + protocol/src/main/protos/core/Tron.proto | 4 +- .../core/contract/asset_issue_contract.proto | 4 +- settings.gradle | 1 + start.sh.simple | 192 ++++ 313 files changed, 9967 insertions(+), 4302 deletions(-) delete mode 100644 actuator/src/main/java/org/tron/core/vm/repository/WriteOptionsWrapper.java create mode 100644 common/src/main/java/org/tron/common/utils/TimeoutInterceptor.java create mode 100644 common/src/main/java/org/tron/core/exception/MaintenanceUnavailableException.java create mode 100644 common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcExceedLimitException.java create mode 100644 common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInternalException.java (53%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInvalidParamsException.java (68%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInvalidRequestException.java (69%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcMethodNotFoundException.java (68%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcTooManyResultException.java (69%) create mode 100644 crypto/src/main/java/org/tron/common/crypto/Rsv.java create mode 100644 docker/arm64/Dockerfile create mode 100644 framework/src/main/java/org/tron/core/config/args/WitnessInitializer.java create mode 100644 framework/src/main/java/org/tron/core/net/P2pRateLimiter.java create mode 100644 framework/src/main/java/org/tron/core/services/http/GetPaginatedNowWitnessListServlet.java create mode 100644 framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/GetPaginatedNowWitnessListOnSolidityServlet.java create mode 100644 framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java delete mode 100644 framework/src/main/java/org/tron/program/DBConvert.java create mode 100644 framework/src/test/java/org/springframework/http/InvalidMediaTypeException.java create mode 100644 framework/src/test/java/org/springframework/http/MediaType.java create mode 100644 framework/src/test/java/org/tron/common/storage/CheckOrInitEngineTest.java rename framework/src/test/java/org/tron/common/storage/{leveldb => rocksdb}/RocksDbDataSourceImplTest.java (71%) create mode 100644 framework/src/test/java/org/tron/common/utils/HashCodeTest.java delete mode 100644 framework/src/test/java/org/tron/common/utils/ObjectSizeUtilTest.java delete mode 100644 framework/src/test/java/org/tron/core/CreateCommonTransactionTest.java create mode 100644 framework/src/test/java/org/tron/core/actuator/vm/SerializersTest.java create mode 100644 framework/src/test/java/org/tron/core/config/args/WitnessInitializerTest.java create mode 100644 framework/src/test/java/org/tron/core/jsonrpc/LogBlockQueryTest.java delete mode 100644 framework/src/test/java/org/tron/core/net/BaseNet.java delete mode 100644 framework/src/test/java/org/tron/core/net/BaseNetTest.java create mode 100644 framework/src/test/java/org/tron/core/net/P2pRateLimiterTest.java create mode 100644 framework/src/test/java/org/tron/core/net/service/nodepersist/NodePersistServiceTest.java create mode 100644 framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java delete mode 100644 framework/src/test/java/org/tron/program/DBConvertTest.java create mode 100644 gradle/jdk17/java-tron.vmoptions create mode 100755 install_dependencies.sh create mode 100644 platform/build.gradle create mode 100644 platform/src/main/java/arm/org/tron/common/math/MathWrapper.java create mode 100644 platform/src/main/java/arm/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java create mode 100644 platform/src/main/java/common/org/tron/common/arch/Arch.java rename plugins/src/main/java/org/tron/plugins/utils/MarketUtils.java => platform/src/main/java/common/org/tron/common/utils/MarketComparator.java (50%) rename {chainbase/src/main/java => platform/src/main/java/common}/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java (87%) rename {common/src/main/java => platform/src/main/java/x86}/org/tron/common/math/MathWrapper.java (100%) rename chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForRockDB.java => platform/src/main/java/x86/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java (70%) create mode 100644 plugins/src/main/java/arm/org/tron/plugins/ArchiveManifest.java create mode 100644 plugins/src/main/java/arm/org/tron/plugins/DbArchive.java rename plugins/src/main/java/{ => common}/org/tron/plugins/Db.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/DbConvert.java (89%) rename plugins/src/main/java/{ => common}/org/tron/plugins/DbCopy.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/DbLite.java (99%) rename plugins/src/main/java/{ => common}/org/tron/plugins/DbMove.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/DbRoot.java (84%) rename plugins/src/main/java/{ => common}/org/tron/plugins/Toolkit.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/ByteArray.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/CryptoUitls.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/DBUtils.java (72%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/FileUtils.java (100%) create mode 100644 plugins/src/main/java/common/org/tron/plugins/utils/MarketUtils.java rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/MerkleRoot.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/Sha256Hash.java (100%) create mode 100644 plugins/src/main/java/common/org/tron/plugins/utils/db/DBInterface.java rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/db/DBIterator.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/db/DbTool.java (85%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/db/LevelDBImpl.java (83%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/db/LevelDBIterator.java (100%) rename plugins/src/main/java/{ => common}/org/tron/plugins/utils/db/RockDBIterator.java (76%) create mode 100644 plugins/src/main/java/common/org/tron/plugins/utils/db/RocksDBImpl.java delete mode 100644 plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForLevelDB.java delete mode 100644 plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForRockDB.java delete mode 100644 plugins/src/main/java/org/tron/plugins/utils/db/DBInterface.java delete mode 100644 plugins/src/main/java/org/tron/plugins/utils/db/RocksDBImpl.java rename plugins/src/main/java/{ => x86}/org/tron/plugins/ArchiveManifest.java (98%) rename plugins/src/main/java/{ => x86}/org/tron/plugins/DbArchive.java (98%) rename plugins/src/test/java/org/tron/plugins/{ => leveldb}/ArchiveManifestTest.java (69%) rename plugins/src/test/java/org/tron/plugins/{ => leveldb}/DbArchiveTest.java (71%) rename plugins/src/test/java/org/tron/plugins/{ => leveldb}/DbConvertTest.java (76%) rename plugins/src/test/java/org/tron/plugins/{ => leveldb}/DbLiteLevelDbTest.java (76%) rename plugins/src/test/java/org/tron/plugins/{ => leveldb}/DbLiteLevelDbV2Test.java (76%) rename plugins/src/test/java/org/tron/plugins/{ => rocksdb}/DbLiteRocksDbTest.java (76%) create mode 100644 plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbV2Test.java create mode 100644 start.sh.simple diff --git a/README.md b/README.md index 0d0eeb6ef71..0f8b30704bf 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,25 @@


-
- java-tron -

-

- Java implementation of the Tron Protocol + Java implementation of the TRON Protocol

- - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + +

## Table of Contents - [What’s TRON?](#whats-tron) - [Building the Source Code](#building-the-source-code) +- [Executables](#executables) - [Running java-tron](#running-java-tron) - [Community](#community) - [Contribution](#contribution) @@ -53,129 +29,171 @@ # What's TRON? -TRON is a project dedicated to building the infrastructure for a truly decentralized Internet. +TRON is building the foundational infrastructure for the decentralized internet ecosystem with a focus on high-performance, scalability, and security. + +- TRON Protocol: High-throughput(2000+ TPS), scalable blockchain OS (DPoS consensus) powering the TRON ecosystem. +- TRON Virtual Machine (TVM): EVM-compatible smart-contract engine for fast smart-contract execution. -- Tron Protocol, one of the largest blockchain-based operating systems in the world, offers scalable, high-availability and high-throughput support that underlies all the decentralized applications in the TRON ecosystem. +# Building the Source Code +Before building java-tron, make sure you have: +- Hardware with at least 4 CPU cores, 16 GB RAM, 10 GB free disk space for a smooth compilation process. +- Operating system: `Linux` or `macOS` (`Windows` is not supported). +- Git and correct JDK(version `8` or `17`) installed based on your CPU architecture. -- Tron Virtual Machine (TVM) allows anyone to develop decentralized applications (DAPPs) for themselves or their communities with smart contracts thereby making decentralized crowdfunding and token issuance easier than ever. +There are two ways to install the required dependencies: -TRON enables large-scale development and engagement. With over 2000 transactions per second (TPS), high concurrency, low latency, and massive data transmission. It is ideal for building decentralized entertainment applications. Free features and incentive systems allow developers to create premium app experiences for users. +- **Option 1: Automated script (recommended for quick setup)** -# Building the Source Code + Use the provided [`install_dependencies.sh`](install_dependencies.sh) script: -Building java-tron requires `git` package and 64-bit version of `Oracle JDK 1.8` to be installed, other JDK versions are not supported yet. Make sure you operate on `Linux` and `MacOS` operating systems. + ```bash + chmod +x install_dependencies.sh + ./install_dependencies.sh + ``` + > **Note**: For production-grade stability with JDK 8 on x86_64 architecture, Oracle JDK 8 is strongly recommended (the script installs OpenJDK 8). -Clone the repo and switch to the `master` branch +- **Option 2: Manual installation** + Follow the [Prerequisites and Installation Guide](https://tronprotocol.github.io/documentation-en/using_javatron/installing_javatron/#prerequisites-before-compiling-java-tron) for step-by-step instructions. + +Once all dependencies have been installed, download and compile java-tron by executing: ```bash -$ git clone https://github.com/tronprotocol/java-tron.git -$ cd java-tron -$ git checkout -t origin/master +git clone https://github.com/tronprotocol/java-tron.git +cd java-tron +git checkout -t origin/master +./gradlew clean build -x test ``` +* The parameter `-x test` indicates skipping the execution of test cases. +* If you encounter any error please refer to the [Compiling java-tron Source Code](https://tronprotocol.github.io/documentation-en/using_javatron/installing_javatron/#compiling-java-tron-source-code) documentation for troubleshooting steps. -then run the following command to build java-tron, the `FullNode.jar` file can be found in `java-tron/build/libs/` after build successfully. +# Executables -```bash -$ ./gradlew clean build -x test -``` +The java-tron project comes with several runnable artifacts and helper scripts found in the project root and build directories. -# Running java-tron +| Artifact/Script | Description | +| :---------------------- | :---------- | +| **`FullNode.jar`** | Main TRON node executable (generated in `build/libs/` after a successful build following the above guidance). Runs as a full node by default. `java -jar FullNode.jar --help` for command line options| +| **`Toolkit.jar`** | Node management utility (generated in `build/libs/`): partition, prune, copy, convert DBs; shadow-fork tool. [Usage](https://tronprotocol.github.io/documentation-en/using_javatron/toolkit/#toolkit-a-java-tron-node-maintenance-suite) | +| **`start.sh`** | Quick start script (x86_64, JDK 8) to download/build/run `FullNode.jar`. See the tool [guide](./shell.md). | +| **`start.sh.simple`** | Quick start script template (ARM64, JDK 17). See usage notes inside the script. | -Running java-tron requires 64-bit version of `Oracle JDK 1.8` to be installed, other JDK versions are not supported yet. Make sure you operate on `Linux` and `MacOS` operating systems. +# Running java-tron -Get the mainnet configuration file: [main_net_config.conf](https://github.com/tronprotocol/tron-deployment/blob/master/main_net_config.conf), other network configuration files can be found [here](https://github.com/tronprotocol/tron-deployment). +## Hardware Requirements for Mainnet -## Hardware Requirements +| Deployment Tier | CPU Cores | Memory | High-performance SSD Storage | Network Downstream | +|--------------------------|-------|--------|---------------------------|-----------------| +| FullNode (Minimum) | 8 | 16 GB | 200 GB ([Lite](https://tronprotocol.github.io/documentation-en/using_javatron/litefullnode/#lite-fullnode)) | ≥ 5 MBit/sec | +| FullNode (Stable) | 8 | 32 GB | 200 GB (Lite) 3.5 TB (Full) | ≥ 5 MBit/sec | +| FullNode (Recommend) | 16+ | 32 GB+ | 4 TB | ≥ 50 MBit/sec | +| Super Representative | 32+ | 64 GB+ | 4 TB | ≥ 50 MBit/sec | -Minimum: +> **Note**: For test networks, where transaction volume is significantly lower, you may operate with reduced hardware specifications. -- CPU with 8 cores -- 16GB RAM -- 3TB free storage space to sync the Mainnet +## Launching a full node -Recommended: +A full node acts as a gateway to the TRON network, exposing comprehensive interfaces via HTTP and RPC APIs. Through these endpoints, clients may execute asset transfers, deploy smart contracts, and invoke on-chain logic. It must join a TRON network to participate in the network's consensus and transaction processing. -- CPU with 16+ cores(32+ cores for a super representative) -- 32GB+ RAM(64GB+ for a super representative) -- High Performance SSD with at least 4TB free space -- 100+ MB/s download Internet service +### Network Types -## Running a full node for mainnet +The TRON network is mainly divided into: -Full node has full historical data, it is the entry point into the TRON network, it can be used by other processes as a gateway into the TRON network via HTTP and GRPC endpoints. You can interact with the TRON network through full node:transfer assets, deploy contracts, interact with contracts and so on. `-c` parameter specifies a configuration file to run a full node: +- **Main Network (Mainnet)** + The primary public blockchain where real value (TRX, TRC-20 tokens, etc.) is transacted, secured by a massive decentralized network. -```bash -$ nohup java -Xms9G -Xmx9G -XX:ReservedCodeCacheSize=256m \ - -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m \ - -XX:MaxDirectMemorySize=1G -XX:+PrintGCDetails \ - -XX:+PrintGCDateStamps -Xloggc:gc.log \ - -XX:+UseConcMarkSweepGC -XX:NewRatio=2 \ - -XX:+CMSScavengeBeforeRemark -XX:+ParallelRefProcEnabled \ - -XX:+HeapDumpOnOutOfMemoryError \ - -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 \ - -jar FullNode.jar -c main_net_config.conf >> start.log 2>&1 & -``` +- **[Nile Test Network (Testnet)](https://nileex.io/)** + A forward-looking testnet where new features and governance proposals are launched first for developers to experience. Consequently, its codebase is typically ahead of the Mainnet. -## Running a super representative node for mainnet +- **[Shasta Testnet](https://shasta.tronex.io/)** + Closely mirrors the Mainnet’s features and governance proposals. Its network parameters and software versions are kept in sync with the Mainnet, providing developers with a highly realistic environment for final testing. -Adding the `--witness` parameter to the startup command, full node will run as a super representative node. The super representative node supports all the functions of the full node and also supports block production. Before running, make sure you have a super representative account and get votes from others. Once the number of obtained votes ranks in the top 27, your super representative node will participate in block production. +- **Private Networks** + Customized TRON networks set up by private entities for testing, development, or specific use cases. -Fill in the private key of a super representative address into the `localwitness` list in the `main_net_config.conf`. Here is an example: +Network selection is performed by specifying the appropriate configuration file upon full-node startup. Mainnet configuration: [config.conf](framework/src/main/resources/config.conf); Nile testnet configuration: [config-nile.conf](https://github.com/tron-nile-testnet/nile-testnet/blob/master/framework/src/main/resources/config-nile.conf) +### 1. Join the TRON main network +Launch a main-network full node with the built-in default configuration: +```bash +java -jar ./build/libs/FullNode.jar ``` - localwitness = [ - - ] + +> For production deployments or long-running Mainnet nodes, please refer to the [JVM Parameter Optimization for FullNode](https://tronprotocol.github.io/documentation-en/using_javatron/installing_javatron/#jvm-parameter-optimization-for-mainnet-fullnode-deployment) guide for the recommended Java command configuration. + +Using the below command, you can monitor the blocks syncing progress: +```bash +tail -f ./logs/tron.log ``` -then run the following command to start the node: +Use [TronScan](https://tronscan.org/#/), TRON's official block explorer, to view main network transactions, blocks, accounts, witness voting, and governance metrics, etc. + +### 2. Join Nile test network +Utilize the `-c` flag to direct the node to the configuration file corresponding to the desired network. Since Nile TestNet may incorporate features not yet available on the MainNet, it is **strongly advised** to compile the source code following the [Building the Source Code](https://github.com/tron-nile-testnet/nile-testnet/blob/master/README.md#building-the-source-code) instructions for the Nile TestNet. ```bash -$ nohup java -Xms9G -Xmx9G -XX:ReservedCodeCacheSize=256m \ - -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m \ - -XX:MaxDirectMemorySize=1G -XX:+PrintGCDetails \ - -XX:+PrintGCDateStamps -Xloggc:gc.log \ - -XX:+UseConcMarkSweepGC -XX:NewRatio=2 \ - -XX:+CMSScavengeBeforeRemark -XX:+ParallelRefProcEnabled \ - -XX:+HeapDumpOnOutOfMemoryError \ - -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 \ - -jar FullNode.jar --witness -c main_net_config.conf >> start.log 2>&1 & +java -jar ./build/libs/FullNode.jar -c config-nile.conf ``` -## Quick Start Tool +Nile resources: explorer, faucet, wallet, developer docs, and network statistics at [nileex.io](https://nileex.io/). -An easier way to build and run java-tron is to use `start.sh`. `start.sh` is a quick start script written in the Shell language. You can use it to build and run java-tron quickly and easily. +### 3. Access Shasta test network +Shasta does not accept public node peers. Programmatic access is available via TronGrid endpoints; see [TronGrid Service](https://developers.tron.network/docs/trongrid) for details. -Here are some common use cases of the scripting tool +Shasta resources: explorer, faucet, wallet, developer docs, and network statistics at [shastaex.io](https://shasta.tronex.io/). -- Use `start.sh` to start a full node with the downloaded `FullNode.jar` -- Use `start.sh` to download the latest `FullNode.jar` and start a full node. -- Use `start.sh` to download the latest source code and compile a `FullNode.jar` and then start a full node. +### 4. Set up a private network +To set up a private network for testing or development, follow the [Private Network guidance](https://tronprotocol.github.io/documentation-en/using_javatron/private_network/). -For more details, please refer to the tool [guide](./shell.md). +## Running a super representative node -## Run inside Docker container +To operate the node as a Super Representative (SR), append the `--witness` parameter to the standard launch command. An SR node inherits every capability of a FullNode and additionally participates in block production. Refer to the [Super Representative documentation](https://tronprotocol.github.io/documentation-en/mechanism-algorithm/sr/) for eligibility requirements. -One of the quickest ways to get `java-tron` up and running on your machine is by using Docker: +Fill in the private key of your SR account into the `localwitness` list in the configuration file. Here is an example: -```shell -$ docker run -d --name="java-tron" \ - -v /your_path/output-directory:/java-tron/output-directory \ - -v /your_path/logs:/java-tron/logs \ - -p 8090:8090 -p 18888:18888 -p 50051:50051 \ - tronprotocol/java-tron \ - -c /java-tron/config/main_net_config.conf ``` + localwitness = [ + + ] +``` +Check [Starting a Block Production Node](https://tronprotocol.github.io/documentation-en/using_javatron/installing_javatron/#starting-a-block-production-node) for more details. +You could also test the process by connecting to a testnet or setting up a private network. -This will mount the `output-directory` and `logs` directories on the host, the docker.sh tool can also be used to simplify the use of docker, see more [here](docker/docker.md). +## Programmatically interfacing FullNode -# Community +Upon the FullNode startup successfully, interaction with the TRON network is facilitated through a comprehensive suite of programmatic interfaces exposed by java-tron: +- **HTTP API**: See the complete [HTTP API reference and endpoint list](https://tronprotocol.github.io/documentation-en/api/http/). +- **gRPC**: High-performance APIs suitable for service-to-service integration. See the supported [gRPC reference](https://tronprotocol.github.io/documentation-en/api/rpc/). +- **JSON-RPC**: Provides Ethereum-compatible JSON-RPC methods for logs, transactions and contract calls, etc. See the supported [JSON-RPC methods](https://tronprotocol.github.io/documentation-en/api/json-rpc/). -[Tron Developers & SRs](https://discord.gg/hqKvyAM) is Tron's official Discord channel. Feel free to join this channel if you have any questions. +Enable or disable each interface in the configuration file: + +``` +node { + http { + fullNodeEnable = true + fullNodePort = 8090 + } + + jsonrpc { + httpFullNodeEnable = true + httpFullNodePort = 8545 + } + + rpc { + enable = true + port = 9090 + } +} +``` +When exposing any of these APIs to a public interface, ensure the node is protected with appropriate authentication, rate limiting, and network access controls in line with your security requirements. + +Public hosted HTTP endpoints for both mainnet and testnet are provided by TronGrid. Please refer to the [TRON Network HTTP Endpoints](https://developers.tron.network/docs/connect-to-the-tron-network#tron-network-http-endpoints) for the latest list. For supported methods and request formats, see the HTTP API reference above. + +# Community -[Core Devs Community](https://t.me/troncoredevscommunity) is the Telegram channel for java-tron community developers. If you want to contribute to java-tron, please join this channel. +[TRON Developers & SRs](https://discord.gg/hqKvyAM) is TRON's official Discord channel. Feel free to join this channel if you have any questions. -[tronprotocol/allcoredev](https://gitter.im/tronprotocol/allcoredev) is the official Gitter channel for developers. +The [Core Devs Community](https://t.me/troncoredevscommunity) and [TRON Official Developer Group](https://t.me/TronOfficialDevelopersGroupEn) are Telegram channels specifically designed for java-tron community developers to engage in technical discussions. # Contribution @@ -184,9 +202,10 @@ Thank you for considering to help out with the source code! If you'd like to con # Resources - [Medium](https://medium.com/@coredevs) java-tron's official technical articles are published there. -- [Documentation](https://tronprotocol.github.io/documentation-en/introduction/) java-tron's official technical documentation website. -- [Test network](http://nileex.io/) A stable test network of TRON contributed by TRON community. -- [Tronscan](https://tronscan.org/#/) TRON network blockchain browser. +- [Documentation](https://tronprotocol.github.io/documentation-en/) and [TRON Developer Hub](https://developers.tron.network/) serve as java-tron’s primary documentation websites. +- [TronScan](https://tronscan.org/#/) TRON main network blockchain browser. +- [Nile Test network](http://nileex.io/) A stable test network of TRON contributed by TRON community. +- [Shasta Test network](https://shasta.tronex.io/) A stable test network of TRON contributed by TRON community. - [Wallet-cli](https://github.com/tronprotocol/wallet-cli) TRON network wallet using command line. - [TIP](https://github.com/tronprotocol/tips) TRON Improvement Proposal (TIP) describes standards for the TRON network. - [TP](https://github.com/tronprotocol/tips/tree/master/tp) TRON Protocol (TP) describes standards already implemented in TRON network but not published as a TIP. diff --git a/actuator/src/main/java/org/tron/core/actuator/AssetIssueActuator.java b/actuator/src/main/java/org/tron/core/actuator/AssetIssueActuator.java index 331b45f106a..618a9fb191e 100644 --- a/actuator/src/main/java/org/tron/core/actuator/AssetIssueActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/AssetIssueActuator.java @@ -24,10 +24,12 @@ import java.util.List; import java.util.Objects; import lombok.extern.slf4j.Slf4j; +import org.tron.common.math.StrictMathWrapper; import org.tron.common.utils.DecodeUtil; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.AssetIssueCapsule; import org.tron.core.capsule.TransactionResultCapsule; +import org.tron.core.config.Parameter.ForkBlockVersionEnum; import org.tron.core.exception.BalanceInsufficientException; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; @@ -263,6 +265,16 @@ public boolean validate() throws ContractValidateException { "frozenDuration must be less than " + maxFrozenSupplyTime + " days " + "and more than " + minFrozenSupplyTime + " days"); } + // make sure FrozenSupply.expireTime not overflow + if (chainBaseManager.getForkController().pass(ForkBlockVersionEnum.VERSION_4_8_1)) { + long frozenPeriod = next.getFrozenDays() * FROZEN_PERIOD; + try { + StrictMathWrapper.addExact(assetIssueContract.getStartTime(), frozenPeriod); + } catch (ArithmeticException e) { + throw new ContractValidateException( + "Start time and frozen days would cause expire time overflow"); + } + } remainSupply -= next.getFrozenAmount(); } diff --git a/actuator/src/main/java/org/tron/core/actuator/ProposalCreateActuator.java b/actuator/src/main/java/org/tron/core/actuator/ProposalCreateActuator.java index a3639cca07f..e0044c4958d 100755 --- a/actuator/src/main/java/org/tron/core/actuator/ProposalCreateActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/ProposalCreateActuator.java @@ -9,7 +9,6 @@ import java.util.Map; import java.util.Objects; import lombok.extern.slf4j.Slf4j; -import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.DecodeUtil; import org.tron.common.utils.StringUtil; import org.tron.core.capsule.ProposalCapsule; @@ -53,7 +52,7 @@ public boolean execute(Object result) throws ContractExeException { long currentMaintenanceTime = chainBaseManager.getDynamicPropertiesStore().getNextMaintenanceTime(); - long now3 = now + CommonParameter.getInstance().getProposalExpireTime(); + long now3 = now + chainBaseManager.getDynamicPropertiesStore().getProposalExpireTime(); long round = (now3 - currentMaintenanceTime) / maintenanceTimeInterval; long expirationTime = currentMaintenanceTime + (round + 1) * maintenanceTimeInterval; diff --git a/actuator/src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java b/actuator/src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java index 2773dc07d26..338e948f304 100644 --- a/actuator/src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java @@ -170,28 +170,27 @@ private void executeShielded(List spends, List= MAX_PROPOSAL_EXPIRE_TIME) { + throw new ContractValidateException( + "This value[PROPOSAL_EXPIRE_TIME] is only allowed to be greater than " + + MIN_PROPOSAL_EXPIRE_TIME + " and less than " + + MAX_PROPOSAL_EXPIRE_TIME + "!"); + } + break; + } default: break; } @@ -921,7 +953,9 @@ public enum ProposalType { // current value, value range ALLOW_TVM_CANCUN(83), // 0, 1 ALLOW_STRICT_MATH(87), // 0, 1 CONSENSUS_LOGIC_OPTIMIZATION(88), // 0, 1 - ALLOW_TVM_BLOB(89); // 0, 1 + ALLOW_TVM_BLOB(89), // 0, 1 + PROPOSAL_EXPIRE_TIME(92), // (0, 31536003000) + ALLOW_TVM_SELFDESTRUCT_RESTRICTION(94); // 0, 1 private long code; diff --git a/actuator/src/main/java/org/tron/core/vm/EnergyCost.java b/actuator/src/main/java/org/tron/core/vm/EnergyCost.java index b758438c940..d47f716943f 100644 --- a/actuator/src/main/java/org/tron/core/vm/EnergyCost.java +++ b/actuator/src/main/java/org/tron/core/vm/EnergyCost.java @@ -55,6 +55,7 @@ public class EnergyCost { private static final long EXT_CODE_SIZE = 20; private static final long EXT_CODE_HASH = 400; private static final long SUICIDE = 0; + private static final long SUICIDE_V2 = 5000; private static final long STOP = 0; private static final long CREATE_DATA = 200; private static final long TLOAD = 100; @@ -289,6 +290,14 @@ public static long getSuicideCost2(Program program) { return getSuicideCost(program); } + public static long getSuicideCost3(Program program) { + DataWord inheritorAddress = program.getStack().peek(); + if (isDeadAccount(program, inheritorAddress)) { + return SUICIDE_V2 + NEW_ACCT_CALL; + } + return SUICIDE_V2; + } + public static long getBalanceCost(Program ignored) { return BALANCE; } diff --git a/actuator/src/main/java/org/tron/core/vm/OperationActions.java b/actuator/src/main/java/org/tron/core/vm/OperationActions.java index f10fb37dd7e..0d978743a5e 100644 --- a/actuator/src/main/java/org/tron/core/vm/OperationActions.java +++ b/actuator/src/main/java/org/tron/core/vm/OperationActions.java @@ -1072,4 +1072,19 @@ public static void suicideAction(Program program) { program.stop(); } + public static void suicideAction2(Program program) { + if (program.isStaticCall()) { + throw new Program.StaticCallModificationException(); + } + + if (!program.canSuicide2()) { + program.getResult().setRevert(); + } else { + DataWord address = program.stackPop(); + program.suicide2(address); + } + + program.stop(); + } + } diff --git a/actuator/src/main/java/org/tron/core/vm/OperationRegistry.java b/actuator/src/main/java/org/tron/core/vm/OperationRegistry.java index be29238a775..f6140107efb 100644 --- a/actuator/src/main/java/org/tron/core/vm/OperationRegistry.java +++ b/actuator/src/main/java/org/tron/core/vm/OperationRegistry.java @@ -79,6 +79,10 @@ public static JumpTable getTable() { adjustForFairEnergy(table); } + if (VMConfig.allowTvmSelfdestructRestriction()) { + adjustSelfdestruct(table); + } + return table; } @@ -695,4 +699,11 @@ public static void appendCancunOperations(JumpTable table) { OperationActions::blobBaseFeeAction, tvmBlobProposal)); } + + public static void adjustSelfdestruct(JumpTable table) { + table.set(new Operation( + Op.SUICIDE, 1, 0, + EnergyCost::getSuicideCost3, + OperationActions::suicideAction2)); + } } diff --git a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java index 7913d7928fa..654a76db33b 100644 --- a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java +++ b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java @@ -16,6 +16,7 @@ import static org.tron.common.utils.ByteUtil.parseWord; import static org.tron.common.utils.ByteUtil.stripLeadingZeroes; import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION; +import static org.tron.core.vm.VMConstant.SIG_LENGTH; import com.google.protobuf.ByteString; @@ -41,6 +42,7 @@ import org.apache.commons.lang3.tuple.Triple; import org.tron.common.crypto.Blake2bfMessageDigest; import org.tron.common.crypto.Hash; +import org.tron.common.crypto.Rsv; import org.tron.common.crypto.SignUtils; import org.tron.common.crypto.SignatureInterface; import org.tron.common.crypto.zksnark.BN128; @@ -201,7 +203,7 @@ public class PrecompiledContracts { public static PrecompiledContract getOptimizedContractForConstant(PrecompiledContract contract) { try { Constructor constructor = contract.getClass().getDeclaredConstructor(); - return (PrecompiledContracts.PrecompiledContract) constructor.newInstance(); + return (PrecompiledContracts.PrecompiledContract) constructor.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } @@ -352,22 +354,13 @@ private static byte[] encodeMultiRes(byte[]... words) { } private static byte[] recoverAddrBySign(byte[] sign, byte[] hash) { - byte v; - byte[] r; - byte[] s; byte[] out = null; if (ArrayUtils.isEmpty(sign) || sign.length < 65) { return new byte[0]; } try { - r = Arrays.copyOfRange(sign, 0, 32); - s = Arrays.copyOfRange(sign, 32, 64); - v = sign[64]; - if (v < 27) { - v += 27; - } - - SignatureInterface signature = SignUtils.fromComponents(r, s, v, + Rsv rsv = Rsv.fromSignature(sign); + SignatureInterface signature = SignUtils.fromComponents(rsv.getR(), rsv.getS(), rsv.getV(), CommonParameter.getInstance().isECKeyCryptoEngine()); if (signature.validateComponents()) { out = SignUtils.signatureToAddress(hash, signature, @@ -403,6 +396,20 @@ private static byte[][] extractBytesArray(DataWord[] words, int offset, byte[] d return bytesArray; } + private static byte[][] extractSigArray(DataWord[] words, int offset, byte[] data) { + if (offset > words.length - 1) { + return new byte[0][]; + } + int len = words[offset].intValueSafe(); + byte[][] bytesArray = new byte[len][]; + for (int i = 0; i < len; i++) { + int bytesOffset = words[offset + i + 1].intValueSafe() / WORD_SIZE; + bytesArray[i] = extractBytes(data, (bytesOffset + offset + 2) * WORD_SIZE, + SIG_LENGTH); + } + return bytesArray; + } + private static byte[] extractBytes(byte[] data, int offset, int len) { return Arrays.copyOfRange(data, offset, offset + len); } @@ -944,8 +951,15 @@ public Pair execute(byte[] rawData) { byte[] hash = Sha256Hash.hash(CommonParameter .getInstance().isECKeyCryptoEngine(), combine); - byte[][] signatures = extractBytesArray( - words, words[3].intValueSafe() / WORD_SIZE, rawData); + if (VMConfig.allowTvmSelfdestructRestriction()) { + int sigArraySize = words[words[3].intValueSafe() / WORD_SIZE].intValueSafe(); + if (sigArraySize > MAX_SIZE) { + return Pair.of(true, DATA_FALSE); + } + } + byte[][] signatures = VMConfig.allowTvmSelfdestructRestriction() ? + extractSigArray(words, words[3].intValueSafe() / WORD_SIZE, rawData) : + extractBytesArray(words, words[3].intValueSafe() / WORD_SIZE, rawData); if (signatures.length == 0 || signatures.length > MAX_SIZE) { return Pair.of(true, DATA_FALSE); @@ -1029,8 +1043,18 @@ private Pair doExecute(byte[] data) throws InterruptedException, ExecutionException { DataWord[] words = DataWord.parseArray(data); byte[] hash = words[0].getData(); - byte[][] signatures = extractBytesArray( - words, words[1].intValueSafe() / WORD_SIZE, data); + + if (VMConfig.allowTvmSelfdestructRestriction()) { + int sigArraySize = words[words[1].intValueSafe() / WORD_SIZE].intValueSafe(); + int addrArraySize = words[words[2].intValueSafe() / WORD_SIZE].intValueSafe(); + if (sigArraySize > MAX_SIZE || addrArraySize > MAX_SIZE) { + return Pair.of(true, DATA_FALSE); + } + } + + byte[][] signatures = VMConfig.allowTvmSelfdestructRestriction() ? + extractSigArray(words, words[1].intValueSafe() / WORD_SIZE, data) : + extractBytesArray(words, words[1].intValueSafe() / WORD_SIZE, data); byte[][] addresses = extractBytes32Array( words, words[2].intValueSafe() / WORD_SIZE); int cnt = signatures.length; diff --git a/actuator/src/main/java/org/tron/core/vm/VM.java b/actuator/src/main/java/org/tron/core/vm/VM.java index 2150df04c64..b1d7b027601 100644 --- a/actuator/src/main/java/org/tron/core/vm/VM.java +++ b/actuator/src/main/java/org/tron/core/vm/VM.java @@ -108,7 +108,10 @@ public static void play(Program program, JumpTable jumpTable) { } catch (JVMStackOverFlowException | OutOfTimeException e) { throw e; } catch (RuntimeException e) { - if (StringUtils.isEmpty(e.getMessage())) { + // https://openjdk.org/jeps/358 + // https://bugs.openjdk.org/browse/JDK-8220715 + // since jdk 14, the NullPointerExceptions message is not empty + if (e instanceof NullPointerException || StringUtils.isEmpty(e.getMessage())) { logger.warn("Unknown Exception occurred, tx id: {}", Hex.toHexString(program.getRootTransactionId()), e); program.setRuntimeFailure(new RuntimeException("Unknown Exception")); diff --git a/actuator/src/main/java/org/tron/core/vm/VMConstant.java b/actuator/src/main/java/org/tron/core/vm/VMConstant.java index abbb6ae6d38..266224a1502 100644 --- a/actuator/src/main/java/org/tron/core/vm/VMConstant.java +++ b/actuator/src/main/java/org/tron/core/vm/VMConstant.java @@ -4,6 +4,7 @@ public class VMConstant { public static final int CONTRACT_NAME_LENGTH = 32; public static final int MIN_TOKEN_ID = 1_000_000; + public static final int SIG_LENGTH = 65; // Numbers public static final int ONE_HUNDRED = 100; diff --git a/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java b/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java index 1ec8a75d669..e0ebca329cd 100644 --- a/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java +++ b/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java @@ -44,6 +44,7 @@ public static void load(StoreFactory storeFactory) { VMConfig.initAllowTvmCancun(ds.getAllowTvmCancun()); VMConfig.initDisableJavaLangMath(ds.getConsensusLogicOptimization()); VMConfig.initAllowTvmBlob(ds.getAllowTvmBlob()); + VMConfig.initAllowTvmSelfdestructRestriction(ds.getAllowTvmSelfdestructRestriction()); } } } diff --git a/actuator/src/main/java/org/tron/core/vm/program/ContractState.java b/actuator/src/main/java/org/tron/core/vm/program/ContractState.java index e095201e393..c6347b9a072 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/ContractState.java +++ b/actuator/src/main/java/org/tron/core/vm/program/ContractState.java @@ -121,6 +121,16 @@ public void updateContractState(byte[] address, ContractStateCapsule contractSta repository.updateContractState(address, contractStateCapsule); } + @Override + public void putNewContract(byte[] address) { + repository.putNewContract(address); + } + + @Override + public boolean isNewContract(byte[] address) { + return repository.isNewContract(address); + } + @Override public void updateAccount(byte[] address, AccountCapsule accountCapsule) { repository.updateAccount(address, accountCapsule); diff --git a/actuator/src/main/java/org/tron/core/vm/program/Memory.java b/actuator/src/main/java/org/tron/core/vm/program/Memory.java index 4249a7e2634..2c43c02e138 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Memory.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Memory.java @@ -98,7 +98,7 @@ public void write(int address, byte[] data, int dataSize, boolean limited) { public void extendAndWrite(int address, int allocSize, byte[] data) { extend(address, allocSize); - write(address, data, data.length, false); + write(address, data, allocSize, false); } public void extend(int address, int size) { diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 5da0b02ecb7..80d972041dc 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -515,6 +515,72 @@ public void suicide(DataWord obtainerAddress) { getResult().addDeleteAccount(this.getContractAddress()); } + public void suicide2(DataWord obtainerAddress) { + + byte[] owner = getContextAddress(); + boolean isNewContract = getContractState().isNewContract(owner); + if (isNewContract) { + suicide(obtainerAddress); + return; + } + + byte[] obtainer = obtainerAddress.toTronAddress(); + + long balance = getContractState().getBalance(owner); + + if (logger.isDebugEnabled()) { + logger.debug("Transfer to: [{}] heritage: [{}]", + Hex.toHexString(obtainer), + balance); + } + + increaseNonce(); + + InternalTransaction internalTx = addInternalTx(null, owner, obtainer, balance, null, + "suicide", nonce, getContractState().getAccount(owner).getAssetMapV2()); + + if (FastByteComparisons.isEqual(owner, obtainer)) { + return; + } + + if (VMConfig.allowTvmVote()) { + withdrawRewardAndCancelVote(owner, getContractState()); + balance = getContractState().getBalance(owner); + if (internalTx != null && balance != internalTx.getValue()) { + internalTx.setValue(balance); + } + } + + // transfer balance and trc10 + createAccountIfNotExist(getContractState(), obtainer); + try { + MUtil.transfer(getContractState(), owner, obtainer, balance); + if (VMConfig.allowTvmTransferTrc10()) { + MUtil.transferAllToken(getContractState(), owner, obtainer); + } + } catch (ContractValidateException e) { + if (VMConfig.allowTvmConstantinople()) { + throw new TransferException( + "transfer all token or transfer all trx failed in suicide: %s", e.getMessage()); + } + throw new BytecodeExecutionException("transfer failure"); + } + + // transfer freeze + if (VMConfig.allowTvmFreeze()) { + transferDelegatedResourceToInheritor(owner, obtainer, getContractState()); + } + + // transfer freezeV2 + if (VMConfig.allowTvmFreezeV2()) { + long expireUnfrozenBalance = + transferFrozenV2BalanceToInheritor(owner, obtainer, getContractState()); + if (expireUnfrozenBalance > 0 && internalTx != null) { + internalTx.setValue(internalTx.getValue() + expireUnfrozenBalance); + } + } + } + public Repository getContractState() { return this.contractState; } @@ -544,6 +610,11 @@ private void transferDelegatedResourceToInheritor(byte[] ownerAddr, byte[] inher // transfer all kinds of frozen balance to BlackHole repo.addBalance(inheritorAddr, frozenBalanceForBandwidthOfOwner + frozenBalanceForEnergyOfOwner); + + if (VMConfig.allowTvmSelfdestructRestriction()) { + clearOwnerFreeze(ownerCapsule); + repo.updateAccount(ownerAddr, ownerCapsule); + } } private long transferFrozenV2BalanceToInheritor(byte[] ownerAddr, byte[] inheritorAddr, Repository repo) { @@ -609,6 +680,11 @@ private long transferFrozenV2BalanceToInheritor(byte[] ownerAddr, byte[] inherit return expireUnfrozenBalance; } + private void clearOwnerFreeze(AccountCapsule ownerCapsule) { + ownerCapsule.setFrozenForBandwidth(0, 0); + ownerCapsule.setFrozenForEnergy(0, 0); + } + private void clearOwnerFreezeV2(AccountCapsule ownerCapsule) { ownerCapsule.clearFrozenV2(); ownerCapsule.setNetUsage(0); @@ -666,6 +742,38 @@ public boolean canSuicide() { // return freezeCheck && voteCheck; } + public boolean canSuicide2() { + byte[] owner = getContextAddress(); + AccountCapsule accountCapsule = getContractState().getAccount(owner); + + return freezeV1Check(accountCapsule) && freezeV2Check(accountCapsule); + } + + private boolean freezeV1Check(AccountCapsule accountCapsule) { + if (!VMConfig.allowTvmFreeze()) { + return true; + } + + // check freeze + long now = getContractState().getDynamicPropertiesStore().getLatestBlockHeaderTimestamp(); + // bandwidth + if (accountCapsule.getFrozenCount() > 0 + && accountCapsule.getFrozenList().stream() + .anyMatch(frozen -> frozen.getExpireTime() > now)) { + return false; + } + // energy + Protocol.Account.Frozen frozenEnergy = + accountCapsule.getAccountResource().getFrozenBalanceForEnergy(); + if (frozenEnergy.getFrozenBalance() > 0 && frozenEnergy.getExpireTime() > now) { + return false; + } + + // check delegate + return accountCapsule.getDelegatedFrozenBalanceForBandwidth() == 0 + && accountCapsule.getDelegatedFrozenBalanceForEnergy() == 0; + } + private boolean freezeV2Check(AccountCapsule accountCapsule) { if (!VMConfig.allowTvmFreezeV2()) { return true; @@ -1638,7 +1746,11 @@ public void callToPrecompiledAddress(MessageCall msg, } } - this.memorySave(msg.getOutDataOffs().intValue(), out.getRight()); + if (VMConfig.allowTvmSelfdestructRestriction()) { + this.memorySave(msg.getOutDataOffs().intValueSafe(), msg.getOutDataSize().intValueSafe(), out.getRight()); + } else { + this.memorySave(msg.getOutDataOffs().intValue(), out.getRight()); + } } } diff --git a/actuator/src/main/java/org/tron/core/vm/program/invoke/ProgramInvokeImpl.java b/actuator/src/main/java/org/tron/core/vm/program/invoke/ProgramInvokeImpl.java index 7997aaedcd5..ede20103609 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/invoke/ProgramInvokeImpl.java +++ b/actuator/src/main/java/org/tron/core/vm/program/invoke/ProgramInvokeImpl.java @@ -314,7 +314,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return new Integer(Boolean.valueOf(byTestingSuite).hashCode() + return Boolean.valueOf(byTestingSuite).hashCode() + Boolean.valueOf(byTransaction).hashCode() + address.hashCode() + balance.hashCode() @@ -326,8 +326,7 @@ public int hashCode() { + origin.hashCode() + prevHash.hashCode() + deposit.hashCode() - + timestamp.hashCode() - ).hashCode(); + + timestamp.hashCode(); } @Override diff --git a/actuator/src/main/java/org/tron/core/vm/repository/Repository.java b/actuator/src/main/java/org/tron/core/vm/repository/Repository.java index 664ee26ee92..8f91d59d0b8 100644 --- a/actuator/src/main/java/org/tron/core/vm/repository/Repository.java +++ b/actuator/src/main/java/org/tron/core/vm/repository/Repository.java @@ -55,6 +55,10 @@ public interface Repository { void updateContractState(byte[] address, ContractStateCapsule contractStateCapsule); + void putNewContract(byte[] address); + + boolean isNewContract(byte[] address); + void updateAccount(byte[] address, AccountCapsule accountCapsule); void updateDynamicProperty(byte[] word, BytesCapsule bytesCapsule); diff --git a/actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java b/actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java index a064cbf6c8a..9de7c0691ba 100644 --- a/actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java +++ b/actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java @@ -9,6 +9,7 @@ import com.google.common.collect.HashBasedTable; import com.google.protobuf.ByteString; import java.util.HashMap; +import java.util.HashSet; import java.util.Optional; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -135,6 +136,7 @@ public class RepositoryImpl implements Repository { private final HashMap> delegationCache = new HashMap<>(); private final HashMap> delegatedResourceAccountIndexCache = new HashMap<>(); private final HashBasedTable> transientStorage = HashBasedTable.create(); + private final HashSet newContractCache = new HashSet<>(); public static void removeLruCache(byte[] address) { } @@ -479,6 +481,7 @@ public void deleteContract(byte[] address) { public void createContract(byte[] address, ContractCapsule contractCapsule) { contractCache.put(Key.create(address), Value.create(contractCapsule, Type.CREATE)); + putNewContract(address); } @Override @@ -533,6 +536,29 @@ public void updateContractState(byte[] address, ContractStateCapsule contractSta Value.create(contractStateCapsule, Type.DIRTY)); } + @Override + public void putNewContract(byte[] address) { + newContractCache.add(Key.create(address)); + } + + @Override + public boolean isNewContract(byte[] address) { + Key key = Key.create(address); + if (newContractCache.contains(key)) { + return true; + } + + if (parent != null) { + boolean isNew = parent.isNewContract(address); + if (isNew) { + newContractCache.add(key); + } + return isNew; + } else { + return false; + } + } + @Override public void updateAccount(byte[] address, AccountCapsule accountCapsule) { accountCache.put(Key.create(address), @@ -740,6 +766,7 @@ public void commit() { commitDelegationCache(repository); commitDelegatedResourceAccountIndexCache(repository); commitTransientStorage(repository); + commitNewContractCache(repository); } @Override @@ -1060,6 +1087,12 @@ public void commitTransientStorage(Repository deposit) { } } + public void commitNewContractCache(Repository deposit) { + if (deposit != null) { + newContractCache.forEach(key -> deposit.putNewContract(key.getData())); + } + } + /** * Get the block id from the number. */ diff --git a/actuator/src/main/java/org/tron/core/vm/repository/Type.java b/actuator/src/main/java/org/tron/core/vm/repository/Type.java index e0842e6a593..9dfbd69f9ae 100644 --- a/actuator/src/main/java/org/tron/core/vm/repository/Type.java +++ b/actuator/src/main/java/org/tron/core/vm/repository/Type.java @@ -73,7 +73,7 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return new Integer(type).hashCode(); + return type; } @Override diff --git a/actuator/src/main/java/org/tron/core/vm/repository/Value.java b/actuator/src/main/java/org/tron/core/vm/repository/Value.java index bf5d99c9c94..1df758f0b3e 100644 --- a/actuator/src/main/java/org/tron/core/vm/repository/Value.java +++ b/actuator/src/main/java/org/tron/core/vm/repository/Value.java @@ -58,6 +58,6 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return new Integer(type.hashCode() + Objects.hashCode(value)).hashCode(); + return type.hashCode() + Objects.hashCode(value); } } diff --git a/actuator/src/main/java/org/tron/core/vm/repository/WriteOptionsWrapper.java b/actuator/src/main/java/org/tron/core/vm/repository/WriteOptionsWrapper.java deleted file mode 100644 index f9e819f9716..00000000000 --- a/actuator/src/main/java/org/tron/core/vm/repository/WriteOptionsWrapper.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.tron.core.vm.repository; - -import lombok.Getter; - -public class WriteOptionsWrapper { - - @Getter - private org.rocksdb.WriteOptions rocks = null; - @Getter - private org.iq80.leveldb.WriteOptions level = null; - - public static WriteOptionsWrapper getInstance() { - WriteOptionsWrapper wrapper = new WriteOptionsWrapper(); - wrapper.level = new org.iq80.leveldb.WriteOptions(); - wrapper.rocks = new org.rocksdb.WriteOptions(); - return wrapper; - } - - public WriteOptionsWrapper sync(boolean bool) { - this.level.sync(bool); - this.rocks.setSync(bool); - return this; - } -} diff --git a/build.gradle b/build.gradle index 14b095b1795..12a0622db99 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,58 @@ +import org.gradle.nativeplatform.platform.internal.Architectures +import org.gradle.internal.os.OperatingSystem allprojects { version = "1.0.0" apply plugin: "java-library" + ext { + springVersion = "5.3.39" + } +} +def arch = System.getProperty("os.arch").toLowerCase() +def javaVersion = JavaVersion.current() +def isArm64 = Architectures.AARCH64.isAlias(arch) +def archSource = isArm64 ? "arm" : "x86" +def isMac = OperatingSystem.current().isMacOsX() + +ext.archInfo = [ + name : arch, + java : javaVersion, + isArm64 : isArm64, + sourceSets: [ + main: [ + java: [ + srcDirs: ["src/main/java/common", "src/main/java/${archSource}"] + ] + ], + test: [ + java: [ + srcDirs: ["src/test/java"] + ] + ] + ], + requires: [ + JavaVersion: isArm64 ? JavaVersion.VERSION_17 : JavaVersion.VERSION_1_8, + RocksdbVersion: isArm64 ? '9.7.4' : '5.15.10', + // https://github.com/grpc/grpc-java/issues/7690 + // https://github.com/grpc/grpc-java/pull/12319, Add support for macOS aarch64 with universal binary + // https://github.com/grpc/grpc-java/pull/11371 , 1.64.x is not supported CentOS 7. + ProtocGenVersion: isArm64 && isMac ? '1.76.0' : '1.60.0' + ], + VMOptions: isArm64 ? "${rootDir}/gradle/jdk17/java-tron.vmoptions" : "${rootDir}/gradle/java-tron.vmoptions" +] + +if (!archInfo.java.is(archInfo.requires.JavaVersion)) { + throw new GradleException("Java ${archInfo.requires.JavaVersion} is required for ${archInfo.name}. Detected version ${archInfo.java}") } +println "Building for architecture: ${archInfo.name}, Java version: ${archInfo.java}" + + subprojects { apply plugin: "jacoco" apply plugin: "maven-publish" sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.current() [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' jacoco { @@ -41,18 +85,23 @@ subprojects { implementation group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25' implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.13' implementation "com.google.code.findbugs:jsr305:3.0.0" - implementation group: 'org.springframework', name: 'spring-context', version: '5.3.18' - implementation group: 'org.springframework', name: 'spring-tx', version: '5.3.18' + implementation group: 'org.springframework', name: 'spring-context', version: "${springVersion}" implementation "org.apache.commons:commons-lang3:3.4" implementation group: 'org.apache.commons', name: 'commons-math', version: '2.2' implementation "org.apache.commons:commons-collections4:4.1" implementation group: 'joda-time', name: 'joda-time', version: '2.3' - implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69' + implementation group: 'org.bouncycastle', name: 'bcprov-jdk18on', version: '1.79' - compileOnly 'org.projectlombok:lombok:1.18.12' - annotationProcessor 'org.projectlombok:lombok:1.18.12' - testCompileOnly 'org.projectlombok:lombok:1.18.12' - testAnnotationProcessor 'org.projectlombok:lombok:1.18.12' + compileOnly 'org.projectlombok:lombok:1.18.34' + annotationProcessor 'org.projectlombok:lombok:1.18.34' + testCompileOnly 'org.projectlombok:lombok:1.18.34' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.34' + + // https://www.oracle.com/java/technologies/javase/11-relnote-issues.html#JDK-8190378 + implementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2' + // for json-rpc, see https://github.com/briandilley/jsonrpc4j/issues/278 + implementation group: 'javax.jws', name: 'javax.jws-api', version: '1.1' + annotationProcessor group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2' testImplementation group: 'junit', name: 'junit', version: '4.13.2' testImplementation "org.mockito:mockito-core:4.11.0" @@ -71,6 +120,33 @@ subprojects { reproducibleFileOrder = true duplicatesStrategy = DuplicatesStrategy.INCLUDE // allow duplicates } + tasks.withType(Test).configureEach { + // https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:environment + environment 'CI', 'true' + } + + publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + } + configurations.configureEach { + resolutionStrategy { + eachDependency { details -> + if (details.requested.group == 'com.google.guava' && + details.requested.name == 'guava') { + def requestedVersion = details.requested.version + if (requestedVersion.matches(/.*-android$/)) { + def jreVersion = requestedVersion.replaceAll(/-android$/, '-jre') + details.useVersion(jreVersion) + details.because("Automatically replace android guava with jre version: ${requestedVersion} -> ${jreVersion}") + } + } + } + } + } } task copyToParent(type: Copy) { diff --git a/chainbase/build.gradle b/chainbase/build.gradle index bc82d9496c3..1a07ff95fa5 100644 --- a/chainbase/build.gradle +++ b/chainbase/build.gradle @@ -10,7 +10,6 @@ dependencies { api project(":common") api project(":crypto") api "org.fusesource.jansi:jansi:$jansiVersion" - api 'io.github.tronprotocol:zksnark-java-sdk:1.0.0' api 'org.reflections:reflections:0.9.11' } diff --git a/chainbase/src/main/java/org/tron/common/storage/WriteOptionsWrapper.java b/chainbase/src/main/java/org/tron/common/storage/WriteOptionsWrapper.java index 11277eafe75..bd6cacc6481 100644 --- a/chainbase/src/main/java/org/tron/common/storage/WriteOptionsWrapper.java +++ b/chainbase/src/main/java/org/tron/common/storage/WriteOptionsWrapper.java @@ -1,6 +1,8 @@ package org.tron.common.storage; -public class WriteOptionsWrapper { +import java.io.Closeable; + +public class WriteOptionsWrapper implements Closeable { public org.rocksdb.WriteOptions rocks = null; public org.iq80.leveldb.WriteOptions level = null; @@ -9,6 +11,23 @@ private WriteOptionsWrapper() { } + /** + * Returns an WriteOptionsWrapper. + * + *

CRITICAL: The returned WriteOptionsWrapper holds native resources + * and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try ( WriteOptionsWrapper readOptions = WriteOptionsWrapper.getInstance()) {
+   *  // do something
+   * }
+   * }
+ * + * @return a new WriteOptionsWrapper that must be closed. + */ public static WriteOptionsWrapper getInstance() { WriteOptionsWrapper wrapper = new WriteOptionsWrapper(); wrapper.level = new org.iq80.leveldb.WriteOptions(); @@ -23,4 +42,12 @@ public WriteOptionsWrapper sync(boolean bool) { this.rocks.setSync(bool); return this; } + + @Override + public void close() { + if (rocks != null) { + rocks.close(); + } + // leveldb WriteOptions has no close method, and does not need to be closed + } } diff --git a/chainbase/src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java b/chainbase/src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java index 506ecdcb6c7..c48800573e1 100644 --- a/chainbase/src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java +++ b/chainbase/src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java @@ -17,8 +17,9 @@ import static org.fusesource.leveldbjni.JniDBFactory.factory; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Sets; -import java.io.File; +import com.google.common.primitives.Bytes; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -30,26 +31,20 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; - -import com.google.common.primitives.Bytes; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.iq80.leveldb.CompressionType; import org.iq80.leveldb.DB; import org.iq80.leveldb.DBIterator; -import org.iq80.leveldb.Logger; import org.iq80.leveldb.Options; import org.iq80.leveldb.ReadOptions; import org.iq80.leveldb.WriteBatch; import org.iq80.leveldb.WriteOptions; -import org.slf4j.LoggerFactory; import org.tron.common.parameter.CommonParameter; import org.tron.common.storage.WriteOptionsWrapper; import org.tron.common.storage.metric.DbStat; @@ -73,29 +68,11 @@ public class LevelDbDataSourceImpl extends DbStat implements DbSourceInter allKeys() { resetDbLock.readLock().lock(); @@ -243,6 +224,7 @@ public Set allKeys() { } @Deprecated + @VisibleForTesting @Override public Set allValues() { resetDbLock.readLock().lock(); @@ -362,6 +344,8 @@ public Map prefixQuery(byte[] key) { } } + @Deprecated + @VisibleForTesting @Override public long getTotal() throws RuntimeException { resetDbLock.readLock().lock(); @@ -378,13 +362,6 @@ public long getTotal() throws RuntimeException { } } - private void updateByBatchInner(Map rows) throws Exception { - try (WriteBatch batch = database.createWriteBatch()) { - innerBatchUpdate(rows,batch); - database.write(batch, writeOptions); - } - } - private void updateByBatchInner(Map rows, WriteOptions options) throws Exception { try (WriteBatch batch = database.createWriteBatch()) { innerBatchUpdate(rows,batch); @@ -404,30 +381,23 @@ private void innerBatchUpdate(Map rows, WriteBatch batch) { @Override public void updateByBatch(Map rows, WriteOptionsWrapper options) { - resetDbLock.readLock().lock(); - try { - updateByBatchInner(rows, options.level); - } catch (Exception e) { - try { - updateByBatchInner(rows, options.level); - } catch (Exception e1) { - throw new RuntimeException(e); - } - } finally { - resetDbLock.readLock().unlock(); - } + this.updateByBatch(rows, options.level); } @Override public void updateByBatch(Map rows) { + this.updateByBatch(rows, writeOptions); + } + + private void updateByBatch(Map rows, WriteOptions options) { resetDbLock.readLock().lock(); try { - updateByBatchInner(rows); + updateByBatchInner(rows, options); } catch (Exception e) { try { - updateByBatchInner(rows); + updateByBatchInner(rows, options); } catch (Exception e1) { - throw new RuntimeException(e); + throw new RuntimeException(e1); } } finally { resetDbLock.readLock().unlock(); @@ -455,6 +425,24 @@ public void closeDB() { } } + /** + * Returns an iterator over the database. + * + *

CRITICAL: The returned iterator holds native resources and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try (DBIterator iterator = db.iterator()) {
+   *   while (iterator.hasNext()) {
+   *     // ... process entry
+   *   }
+   * }
+   * }
+ * + * @return a new database iterator that must be closed. + */ @Override public org.tron.core.db.common.iterator.DBIterator iterator() { return new StoreIterator(getDBIterator()); @@ -467,7 +455,7 @@ public Stream> stream() { @Override public LevelDbDataSourceImpl newInstance() { return new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dataBaseName), - dataBaseName, options, writeOptions); + dataBaseName); } private DBIterator getDBIterator() { diff --git a/chainbase/src/main/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImpl.java b/chainbase/src/main/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImpl.java index 5c051bdc101..c7ca698cc3d 100644 --- a/chainbase/src/main/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImpl.java +++ b/chainbase/src/main/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImpl.java @@ -1,5 +1,6 @@ package org.tron.common.storage.rocksdb; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Sets; import com.google.common.primitives.Bytes; import java.io.File; @@ -20,27 +21,20 @@ import java.util.stream.Collectors; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.rocksdb.BlockBasedTableConfig; -import org.rocksdb.BloomFilter; import org.rocksdb.Checkpoint; -import org.rocksdb.DirectComparator; -import org.rocksdb.InfoLogLevel; -import org.rocksdb.Logger; import org.rocksdb.Options; import org.rocksdb.ReadOptions; import org.rocksdb.RocksDB; import org.rocksdb.RocksDBException; import org.rocksdb.RocksIterator; -import org.rocksdb.Statistics; import org.rocksdb.Status; import org.rocksdb.WriteBatch; import org.rocksdb.WriteOptions; -import org.slf4j.LoggerFactory; +import org.tron.common.error.TronDBException; import org.tron.common.setting.RocksDbSettings; import org.tron.common.storage.WriteOptionsWrapper; import org.tron.common.storage.metric.DbStat; import org.tron.common.utils.FileUtil; -import org.tron.common.utils.PropUtil; import org.tron.core.db.common.DbSourceInter; import org.tron.core.db.common.iterator.RockStoreIterator; import org.tron.core.db2.common.Instance; @@ -53,38 +47,19 @@ public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter, Iterable>, Instance { - ReadOptions readOpts; private String dataBaseName; private RocksDB database; private volatile boolean alive; private String parentPath; private ReadWriteLock resetDbLock = new ReentrantReadWriteLock(); - private static final String KEY_ENGINE = "ENGINE"; - private static final String ROCKSDB = "ROCKSDB"; - private DirectComparator comparator; - private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB); + private Options options; - public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings, - DirectComparator comparator) { - this.dataBaseName = name; - this.parentPath = parentPath; - this.comparator = comparator; - RocksDbSettings.setRocksDbSettings(settings); - initDB(); - } - - public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings) { + public RocksDbDataSourceImpl(String parentPath, String name) { this.dataBaseName = name; this.parentPath = parentPath; - RocksDbSettings.setRocksDbSettings(settings); initDB(); } - public RocksDbDataSourceImpl(String parentPath, String name) { - this.parentPath = parentPath; - this.dataBaseName = name; - } - public Path getDbPath() { return Paths.get(parentPath, dataBaseName); } @@ -104,6 +79,9 @@ public void closeDB() { if (!isAlive()) { return; } + if (this.options != null) { + this.options.close(); + } database.close(); alive = false; } catch (Exception e) { @@ -125,40 +103,68 @@ public void resetDb() { } } - private boolean quitIfNotAlive() { + private void throwIfNotAlive() { if (!isAlive()) { - logger.warn("DB {} is not alive.", dataBaseName); + throw new TronDBException("DB " + this.getDBName() + " is closed."); } - return !isAlive(); } + /** copy from {@link org.fusesource.leveldbjni.internal#checkArgNotNull} */ + private static void checkArgNotNull(Object value, String name) { + if (value == null) { + throw new IllegalArgumentException("The " + name + " argument cannot be null"); + } + } + + @Deprecated + @VisibleForTesting @Override public Set allKeys() throws RuntimeException { resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return null; - } + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { Set result = Sets.newHashSet(); - try (final RocksIterator iter = getRocksIterator()) { - for (iter.seekToFirst(); iter.isValid(); iter.next()) { - result.add(iter.key()); - } - return result; + for (iter.seekToFirst(); iter.isValid(); iter.next()) { + result.add(iter.key()); } + return result; } finally { resetDbLock.readLock().unlock(); } } + @Deprecated + @VisibleForTesting @Override public Set allValues() throws RuntimeException { - return null; + resetDbLock.readLock().lock(); + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + Set result = Sets.newHashSet(); + for (iter.seekToFirst(); iter.isValid(); iter.next()) { + result.add(iter.value()); + } + return result; + } finally { + resetDbLock.readLock().unlock(); + } } + @Deprecated + @VisibleForTesting @Override public long getTotal() throws RuntimeException { - return 0; + resetDbLock.readLock().lock(); + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + long total = 0; + for (iter.seekToFirst(); iter.isValid(); iter.next()) { + total++; + } + return total; + } finally { + resetDbLock.readLock().unlock(); + } } @Override @@ -168,39 +174,10 @@ public String getDBName() { @Override public void setDBName(String name) { + this.dataBaseName = name; } - public boolean checkOrInitEngine() { - String dir = getDbPath().toString(); - String enginePath = dir + File.separator + "engine.properties"; - - if (FileUtil.createDirIfNotExists(dir)) { - if (!FileUtil.createFileIfNotExists(enginePath)) { - return false; - } - } else { - return false; - } - - // for the first init engine - String engine = PropUtil.readProperty(enginePath, KEY_ENGINE); - if (engine.isEmpty() && !PropUtil.writeProperty(enginePath, KEY_ENGINE, ROCKSDB)) { - return false; - } - engine = PropUtil.readProperty(enginePath, KEY_ENGINE); - - return ROCKSDB.equals(engine); - } - - public void initDB() { - if (!checkOrInitEngine()) { - throw new RuntimeException( - String.format("failed to check database: %s, engine do not match", dataBaseName)); - } - initDB(RocksDbSettings.getSettings()); - } - - public void initDB(RocksDbSettings settings) { + private void initDB() { resetDbLock.writeLock().lock(); try { if (isAlive()) { @@ -210,80 +187,40 @@ public void initDB(RocksDbSettings settings) { throw new IllegalArgumentException("No name set to the dbStore"); } - try (Options options = new Options()) { - - // most of these options are suggested by https://github.com/facebook/rocksdb/wiki/Set-Up-Options + try { + logger.debug("Opening database {}.", dataBaseName); + final Path dbPath = getDbPath(); - // general options - if (settings.isEnableStatistics()) { - options.setStatistics(new Statistics()); - options.setStatsDumpPeriodSec(60); + if (!Files.isSymbolicLink(dbPath.getParent())) { + Files.createDirectories(dbPath.getParent()); } - options.setCreateIfMissing(true); - options.setIncreaseParallelism(1); - options.setLevelCompactionDynamicLevelBytes(true); - options.setMaxOpenFiles(settings.getMaxOpenFiles()); - - // general options supported user config - options.setNumLevels(settings.getLevelNumber()); - options.setMaxBytesForLevelMultiplier(settings.getMaxBytesForLevelMultiplier()); - options.setMaxBytesForLevelBase(settings.getMaxBytesForLevelBase()); - options.setMaxBackgroundCompactions(settings.getCompactThreads()); - options.setLevel0FileNumCompactionTrigger(settings.getLevel0FileNumCompactionTrigger()); - options.setTargetFileSizeMultiplier(settings.getTargetFileSizeMultiplier()); - options.setTargetFileSizeBase(settings.getTargetFileSizeBase()); - if (comparator != null) { - options.setComparator(comparator); - } - options.setLogger(new Logger(options) { - @Override - protected void log(InfoLogLevel infoLogLevel, String logMsg) { - rocksDbLogger.info("{} {}", dataBaseName, logMsg); - } - }); - - // table options - final BlockBasedTableConfig tableCfg; - options.setTableFormatConfig(tableCfg = new BlockBasedTableConfig()); - tableCfg.setBlockSize(settings.getBlockSize()); - tableCfg.setBlockCache(RocksDbSettings.getCache()); - tableCfg.setCacheIndexAndFilterBlocks(true); - tableCfg.setPinL0FilterAndIndexBlocksInCache(true); - tableCfg.setFilter(new BloomFilter(10, false)); - - // read options - readOpts = new ReadOptions(); - readOpts = readOpts.setPrefixSameAsStart(true) - .setVerifyChecksums(false); try { - logger.debug("Opening database {}.", dataBaseName); - final Path dbPath = getDbPath(); - - if (!Files.isSymbolicLink(dbPath.getParent())) { - Files.createDirectories(dbPath.getParent()); + DbSourceInter.checkOrInitEngine(getEngine(), dbPath.toString(), + TronError.ErrCode.ROCKSDB_INIT); + this.options = RocksDbSettings.getOptionsByDbName(dataBaseName); + database = RocksDB.open(this.options, dbPath.toString()); + } catch (RocksDBException e) { + if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) { + logger.error("Database {} corrupted, please delete database directory({}) " + + "and restart.", dataBaseName, parentPath, e); + } else { + logger.error("Open Database {} failed", dataBaseName, e); } - try { - database = RocksDB.open(options, dbPath.toString()); - } catch (RocksDBException e) { - if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) { - logger.error("Database {} corrupted, please delete database directory({}) " + - "and restart.", dataBaseName, parentPath, e); - } else { - logger.error("Open Database {} failed", dataBaseName, e); - } - throw new TronError(e, TronError.ErrCode.ROCKSDB_INIT); + if (this.options != null) { + this.options.close(); } - - alive = true; - } catch (IOException ioe) { - throw new RuntimeException( - String.format("failed to init database: %s", dataBaseName), ioe); + throw new TronError(e, TronError.ErrCode.ROCKSDB_INIT); } - logger.debug("Init DB {} done.", dataBaseName); + alive = true; + } catch (IOException ioe) { + throw new RuntimeException( + String.format("failed to init database: %s", dataBaseName), ioe); } + + logger.debug("Init DB {} done.", dataBaseName); } finally { resetDbLock.writeLock().unlock(); } @@ -293,9 +230,9 @@ protected void log(InfoLogLevel infoLogLevel, String logMsg) { public void putData(byte[] key, byte[] value) { resetDbLock.readLock().lock(); try { - if (quitIfNotAlive()) { - return; - } + throwIfNotAlive(); + checkArgNotNull(key, "key"); + checkArgNotNull(value, "value"); database.put(key, value); } catch (RocksDBException e) { throw new RuntimeException(dataBaseName, e); @@ -308,9 +245,8 @@ public void putData(byte[] key, byte[] value) { public byte[] getData(byte[] key) { resetDbLock.readLock().lock(); try { - if (quitIfNotAlive()) { - return null; - } + throwIfNotAlive(); + checkArgNotNull(key, "key"); return database.get(key); } catch (RocksDBException e) { throw new RuntimeException(dataBaseName, e); @@ -323,9 +259,8 @@ public byte[] getData(byte[] key) { public void deleteData(byte[] key) { resetDbLock.readLock().lock(); try { - if (quitIfNotAlive()) { - return; - } + throwIfNotAlive(); + checkArgNotNull(key, "key"); database.delete(key); } catch (RocksDBException e) { throw new RuntimeException(dataBaseName, e); @@ -339,74 +274,65 @@ public boolean flush() { return false; } + /** + * Returns an iterator over the database. + * + *

CRITICAL: The returned iterator holds native resources and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try (DBIterator iterator = db.iterator()) {
+   *   while (iterator.hasNext()) {
+   *     // ... process entry
+   *   }
+   * }
+   * }
+ * + * @return a new database iterator that must be closed. + */ @Override public org.tron.core.db.common.iterator.DBIterator iterator() { - return new RockStoreIterator(getRocksIterator()); - } - - private void updateByBatchInner(Map rows) throws Exception { - if (quitIfNotAlive()) { - return; - } - try (WriteBatch batch = new WriteBatch()) { - for (Map.Entry entry : rows.entrySet()) { - if (entry.getValue() == null) { - batch.delete(entry.getKey()); - } else { - batch.put(entry.getKey(), entry.getValue()); - } - } - database.write(new WriteOptions(), batch); - } + ReadOptions readOptions = getReadOptions(); + return new RockStoreIterator(getRocksIterator(readOptions), readOptions); } private void updateByBatchInner(Map rows, WriteOptions options) throws Exception { - if (quitIfNotAlive()) { - return; - } try (WriteBatch batch = new WriteBatch()) { for (Map.Entry entry : rows.entrySet()) { + checkArgNotNull(entry.getKey(), "key"); if (entry.getValue() == null) { batch.delete(entry.getKey()); } else { batch.put(entry.getKey(), entry.getValue()); } } + throwIfNotAlive(); database.write(options, batch); } } @Override public void updateByBatch(Map rows, WriteOptionsWrapper optionsWrapper) { - resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return; - } - updateByBatchInner(rows, optionsWrapper.rocks); - } catch (Exception e) { - try { - updateByBatchInner(rows); - } catch (Exception e1) { - throw new RuntimeException(dataBaseName, e1); - } - } finally { - resetDbLock.readLock().unlock(); - } + this.updateByBatch(rows, optionsWrapper.rocks); } @Override public void updateByBatch(Map rows) { + try (WriteOptions writeOptions = new WriteOptions()) { + this.updateByBatch(rows, writeOptions); + } + } + + private void updateByBatch(Map rows, WriteOptions options) { resetDbLock.readLock().lock(); try { - if (quitIfNotAlive()) { - return; - } - updateByBatchInner(rows); + updateByBatchInner(rows, options); } catch (Exception e) { try { - updateByBatchInner(rows); + updateByBatchInner(rows, options); } catch (Exception e1) { throw new RuntimeException(dataBaseName, e1); } @@ -416,45 +342,36 @@ public void updateByBatch(Map rows) { } public List getKeysNext(byte[] key, long limit) { + if (limit <= 0) { + return new ArrayList<>(); + } resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return new ArrayList<>(); - } - if (limit <= 0) { - return new ArrayList<>(); - } - - try (RocksIterator iter = getRocksIterator()) { - List result = new ArrayList<>(); - long i = 0; - for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { - result.add(iter.key()); - } - return result; + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + List result = new ArrayList<>(); + long i = 0; + for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { + result.add(iter.key()); } + return result; } finally { resetDbLock.readLock().unlock(); } } public Map getNext(byte[] key, long limit) { + if (limit <= 0) { + return Collections.emptyMap(); + } resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return null; - } - if (limit <= 0) { - return Collections.emptyMap(); - } - try (RocksIterator iter = getRocksIterator()) { - Map result = new HashMap<>(); - long i = 0; - for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { - result.put(iter.key(), iter.value()); - } - return result; + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + Map result = new HashMap<>(); + long i = 0; + for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { + result.put(iter.key(), iter.value()); } + return result; } finally { resetDbLock.readLock().unlock(); } @@ -463,80 +380,109 @@ public Map getNext(byte[] key, long limit) { @Override public Map prefixQuery(byte[] key) { resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return null; - } - try (RocksIterator iterator = getRocksIterator()) { - Map result = new HashMap<>(); - for (iterator.seek(key); iterator.isValid(); iterator.next()) { - if (Bytes.indexOf(iterator.key(), key) == 0) { - result.put(WrappedByteArray.of(iterator.key()), iterator.value()); - } else { - return result; - } + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iterator = getRocksIterator(readOptions)) { + Map result = new HashMap<>(); + for (iterator.seek(key); iterator.isValid(); iterator.next()) { + if (Bytes.indexOf(iterator.key(), key) == 0) { + result.put(WrappedByteArray.of(iterator.key()), iterator.value()); + } else { + return result; } - return result; } + return result; } finally { resetDbLock.readLock().unlock(); } } public Set getlatestValues(long limit) { + if (limit <= 0) { + return Sets.newHashSet(); + } resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return null; - } - if (limit <= 0) { - return Sets.newHashSet(); - } - try (RocksIterator iter = getRocksIterator()) { - Set result = Sets.newHashSet(); - long i = 0; - for (iter.seekToLast(); iter.isValid() && i < limit; iter.prev(), i++) { - result.add(iter.value()); - } - return result; + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + Set result = Sets.newHashSet(); + long i = 0; + for (iter.seekToLast(); iter.isValid() && i < limit; iter.prev(), i++) { + result.add(iter.value()); } + return result; } finally { resetDbLock.readLock().unlock(); } } - public Set getValuesNext(byte[] key, long limit) { + if (limit <= 0) { + return Sets.newHashSet(); + } resetDbLock.readLock().lock(); - try { - if (quitIfNotAlive()) { - return null; - } - if (limit <= 0) { - return Sets.newHashSet(); - } - try (RocksIterator iter = getRocksIterator()) { - Set result = Sets.newHashSet(); - long i = 0; - for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { - result.add(iter.value()); - } - return result; + try (final ReadOptions readOptions = getReadOptions(); + final RocksIterator iter = getRocksIterator(readOptions)) { + Set result = Sets.newHashSet(); + long i = 0; + for (iter.seek(key); iter.isValid() && i < limit; iter.next(), i++) { + result.add(iter.value()); } + return result; } finally { resetDbLock.readLock().unlock(); } } public void backup(String dir) throws RocksDBException { - Checkpoint cp = Checkpoint.create(database); - cp.createCheckpoint(dir + this.getDBName()); + throwIfNotAlive(); + try (Checkpoint cp = Checkpoint.create(database)) { + cp.createCheckpoint(dir + this.getDBName()); + } } - private RocksIterator getRocksIterator() { - try ( ReadOptions readOptions = new ReadOptions().setFillCache(false)) { - return database.newIterator(readOptions); - } + /** + * Returns an iterator over the database. + * + *

CRITICAL: The returned iterator holds native resources and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try ( ReadOptions readOptions = new ReadOptions().setFillCache(false);
+   *      RocksIterator iterator = getRocksIterator(readOptions)) {
+   *      iterator.seekToFirst();
+   *  // do something
+   * }
+   * }
+ * + * @return a new database iterator that must be closed. + */ + private RocksIterator getRocksIterator(ReadOptions readOptions) { + throwIfNotAlive(); + return database.newIterator(readOptions); + } + + /** + * Returns an ReadOptions. + * + *

CRITICAL: The returned ReadOptions holds native resources and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try (ReadOptions readOptions = getReadOptions();
+   *      RocksIterator iterator = getRocksIterator(readOptions)) {
+   *      iterator.seekToFirst();
+   *  // do something
+   * }
+   * }
+ * + * @return a new database iterator that must be closed. + */ + private ReadOptions getReadOptions() { + throwIfNotAlive(); + return new ReadOptions().setFillCache(false); } public boolean deleteDbBakPath(String dir) { @@ -545,7 +491,7 @@ public boolean deleteDbBakPath(String dir) { @Override public RocksDbDataSourceImpl newInstance() { - return new RocksDbDataSourceImpl(parentPath, dataBaseName, RocksDbSettings.getSettings()); + return new RocksDbDataSourceImpl(parentPath, dataBaseName); } diff --git a/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java b/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java index 940a107a2ac..7179045ea7e 100644 --- a/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java +++ b/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java @@ -25,6 +25,7 @@ import org.tron.common.crypto.SignInterface; import org.tron.common.crypto.SignUtils; import org.tron.core.config.Parameter.ChainConstant; +import org.tron.core.exception.TronError; @Slf4j(topic = "app") public class LocalWitnesses { @@ -32,6 +33,7 @@ public class LocalWitnesses { @Getter private List privateKeys = Lists.newArrayList(); + @Getter private byte[] witnessAccountAddress; public LocalWitnesses() { @@ -45,21 +47,11 @@ public LocalWitnesses(List privateKeys) { setPrivateKeys(privateKeys); } - public byte[] getWitnessAccountAddress(boolean isECKeyCryptoEngine) { - if (witnessAccountAddress == null) { - byte[] privateKey = ByteArray.fromHexString(getPrivateKey()); - final SignInterface cryptoEngine = SignUtils.fromPrivate(privateKey, isECKeyCryptoEngine); - this.witnessAccountAddress = cryptoEngine.getAddress(); - } - return witnessAccountAddress; - } - - public void setWitnessAccountAddress(final byte[] localWitnessAccountAddress) { - this.witnessAccountAddress = localWitnessAccountAddress; - } - - public void initWitnessAccountAddress(boolean isECKeyCryptoEngine) { - if (witnessAccountAddress == null) { + public void initWitnessAccountAddress(final byte[] witnessAddress, + boolean isECKeyCryptoEngine) { + if (witnessAddress != null) { + this.witnessAccountAddress = witnessAddress; + } else if (!CollectionUtils.isEmpty(privateKeys)) { byte[] privateKey = ByteArray.fromHexString(getPrivateKey()); final SignInterface ecKey = SignUtils.fromPrivate(privateKey, isECKeyCryptoEngine); @@ -85,11 +77,16 @@ private void validate(String privateKey) { privateKey = privateKey.substring(2); } - if (StringUtils.isNotBlank(privateKey) - && privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) { - throw new IllegalArgumentException( - String.format("private key must be %d-bits hex string, actual: %d", - ChainConstant.PRIVATE_KEY_LENGTH, privateKey.length())); + if (StringUtils.isBlank(privateKey) + || privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) { + throw new TronError(String.format("private key must be %d hex string, actual: %d", + ChainConstant.PRIVATE_KEY_LENGTH, + StringUtils.isBlank(privateKey) ? 0 : privateKey.length()), + TronError.ErrCode.WITNESS_INIT); + } + if (!StringUtil.isHexadecimal(privateKey)) { + throw new TronError("private key must be hex string", + TronError.ErrCode.WITNESS_INIT); } } diff --git a/chainbase/src/main/java/org/tron/common/utils/StorageUtils.java b/chainbase/src/main/java/org/tron/common/utils/StorageUtils.java index 16df43f1534..0c7c77bd23f 100644 --- a/chainbase/src/main/java/org/tron/common/utils/StorageUtils.java +++ b/chainbase/src/main/java/org/tron/common/utils/StorageUtils.java @@ -1,15 +1,20 @@ package org.tron.common.utils; import static org.tron.common.parameter.CommonParameter.ENERGY_LIMIT_HARD_FORK; +import static org.tron.core.db.common.DbSourceInter.LEVELDB; import java.io.File; import org.apache.commons.lang3.StringUtils; import org.iq80.leveldb.Options; +import org.slf4j.LoggerFactory; import org.tron.common.parameter.CommonParameter; +import org.tron.core.Constant; public class StorageUtils { + private static final org.slf4j.Logger levelDbLogger = LoggerFactory.getLogger(LEVELDB); + public static boolean getEnergyLimitHardFork() { return ENERGY_LIMIT_HARD_FORK; } @@ -52,9 +57,16 @@ public static String getOutputDirectory() { } public static Options getOptionsByDbName(String dbName) { + Options options; if (hasProperty(dbName)) { - return getProperty(dbName).getDbOptions(); + options = getProperty(dbName).getDbOptions(); + } else { + options = CommonParameter.getInstance().getStorage().newDefaultDbOptions(dbName); + } + if (Constant.MARKET_PAIR_PRICE_TO_ORDER.equals(dbName)) { + options.comparator(new MarketOrderPriceComparatorForLevelDB()); } - return CommonParameter.getInstance().getStorage().newDefaultDbOptions(dbName); + options.logger(message -> levelDbLogger.info("{} {}", dbName, message)); + return options; } } diff --git a/chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java b/chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java index f7b8c17b57f..3700d300411 100644 --- a/chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java +++ b/chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java @@ -29,65 +29,42 @@ @Slf4j public class JLibrustzcash { - private static Librustzcash INSTANCE; + private static Librustzcash INSTANCE = LibrustzcashWrapper.getInstance(); public static void librustzcashZip32XskMaster(Zip32XskMasterParams params) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashZip32XskMaster(params.getData(), params.getSize(), params.getM_bytes()); } public static void librustzcashInitZksnarkParams(InitZksnarkParams params) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashInitZksnarkParams(params.getSpend_path(), params.getSpend_hash(), params.getOutput_path(), params.getOutput_hash()); } public static void librustzcashZip32XskDerive(Zip32XskDeriveParams params) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashZip32XskDerive(params.getData(), params.getSize(), params.getM_bytes()); } public static boolean librustzcashZip32XfvkAddress(Zip32XfvkAddressParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashZip32XfvkAddress(params.getXfvk(), params.getJ(), params.getJ_ret(), params.getAddr_ret()); } public static void librustzcashCrhIvk(CrhIvkParams params) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashCrhIvk(params.getAk(), params.getNk(), params.getIvk()); } public static boolean librustzcashKaAgree(KaAgreeParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingKaAgree(params.getP(), params.getSk(), params.getResult()); } public static boolean librustzcashComputeCm(ComputeCmParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingComputeCm(params.getD(), params.getPkD(), params.getValue(), params.getR(), params.getCm()); } public static boolean librustzcashComputeNf(ComputeNfParams params) { - if (isOpenZen()) { - INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(), - params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult()); - } + INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(), + params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult()); return true; } @@ -96,9 +73,6 @@ public static boolean librustzcashComputeNf(ComputeNfParams params) { * @return ak 32 bytes */ public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException { - if (!isOpenZen()) { - return ByteUtil.EMPTY_BYTE_ARRAY; - } LibrustzcashParam.valid32Params(ask); byte[] ak = new byte[32]; INSTANCE.librustzcashAskToAk(ask, ak); @@ -110,9 +84,6 @@ public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException { * @return 32 bytes */ public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException { - if (!isOpenZen()) { - return ByteUtil.EMPTY_BYTE_ARRAY; - } LibrustzcashParam.valid32Params(nsk); byte[] nk = new byte[32]; INSTANCE.librustzcashNskToNk(nsk, nk); @@ -125,26 +96,17 @@ public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException { * @return r: random number, less than r_J, 32 bytes */ public static byte[] librustzcashSaplingGenerateR(byte[] r) throws ZksnarkException { - if (!isOpenZen()) { - return ByteUtil.EMPTY_BYTE_ARRAY; - } LibrustzcashParam.valid32Params(r); INSTANCE.librustzcashSaplingGenerateR(r); return r; } public static boolean librustzcashSaplingKaDerivepublic(KaDerivepublicParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingKaDerivepublic(params.getDiversifier(), params.getEsk(), params.getResult()); } public static long librustzcashSaplingProvingCtxInit() { - if (!isOpenZen()) { - return 0; - } return INSTANCE.librustzcashSaplingProvingCtxInit(); } @@ -154,17 +116,11 @@ public static long librustzcashSaplingProvingCtxInit() { * @param d 11 bytes */ public static boolean librustzcashCheckDiversifier(byte[] d) throws ZksnarkException { - if (!isOpenZen()) { - return true; - } LibrustzcashParam.valid11Params(d); return INSTANCE.librustzcashCheckDiversifier(d); } public static boolean librustzcashSaplingSpendProof(SpendProofParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingSpendProof(params.getCtx(), params.getAk(), params.getNsk(), params.getD(), params.getR(), params.getAlpha(), params.getValue(), params.getAnchor(), params.getVoucherPath(), params.getCv(), params.getRk(), @@ -172,26 +128,17 @@ public static boolean librustzcashSaplingSpendProof(SpendProofParams params) { } public static boolean librustzcashSaplingOutputProof(OutputProofParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingOutputProof(params.getCtx(), params.getEsk(), params.getD(), params.getPkD(), params.getR(), params.getValue(), params.getCv(), params.getZkproof()); } public static boolean librustzcashSaplingSpendSig(SpendSigParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingSpendSig(params.getAsk(), params.getAlpha(), params.getSigHash(), params.getResult()); } public static boolean librustzcashSaplingBindingSig(BindingSigParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingBindingSig(params.getCtx(), params.getValueBalance(), params.getSighash(), params.getResult()); } @@ -203,74 +150,47 @@ public static boolean librustzcashSaplingBindingSig(BindingSigParams params) { * @param data 32 bytes */ public static void librustzcashToScalar(byte[] value, byte[] data) throws ZksnarkException { - if (!isOpenZen()) { - return; - } LibrustzcashParam.validParamLength(value, 64); LibrustzcashParam.valid32Params(data); INSTANCE.librustzcashToScalar(value, data); } public static void librustzcashSaplingProvingCtxFree(long ctx) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashSaplingProvingCtxFree(ctx); } public static long librustzcashSaplingVerificationCtxInit() { - if (!isOpenZen()) { - return 0; - } return INSTANCE.librustzcashSaplingVerificationCtxInit(); } public static boolean librustzcashSaplingCheckSpend(CheckSpendParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingCheckSpend(params.getCtx(), params.getCv(), params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(), params.getSpendAuthSig(), params.getSighashValue()); } public static boolean librustzcashSaplingCheckOutput(CheckOutputParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingCheckOutput(params.getCtx(), params.getCv(), params.getCm(), params.getEphemeralKey(), params.getZkproof()); } public static boolean librustzcashSaplingFinalCheck(FinalCheckParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingFinalCheck(params.getCtx(), params.getValueBalance(), params.getBindingSig(), params.getSighashValue()); } public static boolean librustzcashSaplingCheckSpendNew(CheckSpendNewParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingCheckSpendNew(params.getCv(), params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(), params.getSpendAuthSig(), params.getSighashValue()); } public static boolean librustzcashSaplingCheckOutputNew(CheckOutputNewParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashSaplingCheckOutputNew(params.getCv(), params.getCm(), params.getEphemeralKey(), params.getZkproof()); } public static boolean librustzcashSaplingFinalCheckNew(FinalCheckNewParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE .librustzcashSaplingFinalCheckNew(params.getValueBalance(), params.getBindingSig(), params.getSighashValue(), params.getSpendCv(), params.getSpendCvLen(), @@ -278,23 +198,14 @@ public static boolean librustzcashSaplingFinalCheckNew(FinalCheckNewParams param } public static void librustzcashSaplingVerificationCtxFree(long ctx) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashSaplingVerificationCtxFree(ctx); } public static boolean librustzcashIvkToPkd(IvkToPkdParams params) { - if (!isOpenZen()) { - return true; - } return INSTANCE.librustzcashIvkToPkd(params.getIvk(), params.getD(), params.getPkD()); } public static void librustzcashMerkleHash(MerkleHashParams params) { - if (!isOpenZen()) { - return; - } INSTANCE.librustzcashMerkleHash(params.getDepth(), params.getA(), params.getB(), params.getResult()); } @@ -303,19 +214,7 @@ public static void librustzcashMerkleHash(MerkleHashParams params) { * @param result uncommitted value, 32 bytes */ public static void librustzcashTreeUncommitted(byte[] result) throws ZksnarkException { - if (!isOpenZen()) { - return; - } LibrustzcashParam.valid32Params(result); INSTANCE.librustzcashTreeUncommitted(result); } - - public static boolean isOpenZen() { - boolean res = CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs(); - if (res) { - INSTANCE = LibrustzcashWrapper.getInstance(); - } - return res; - } - } diff --git a/chainbase/src/main/java/org/tron/common/zksnark/JLibsodium.java b/chainbase/src/main/java/org/tron/common/zksnark/JLibsodium.java index 0159ba0bf6b..0713d74b7bd 100644 --- a/chainbase/src/main/java/org/tron/common/zksnark/JLibsodium.java +++ b/chainbase/src/main/java/org/tron/common/zksnark/JLibsodium.java @@ -12,37 +12,25 @@ public class JLibsodium { public static final int CRYPTO_GENERICHASH_BLAKE2B_PERSONALBYTES = 16; public static final int CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12; - private static Libsodium INSTANCE; + private static Libsodium INSTANCE = LibsodiumWrapper.getInstance(); public static int cryptoGenerichashBlake2bInitSaltPersonal(Blake2bInitSaltPersonalParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE .cryptoGenerichashBlake2BInitSaltPersonal(params.getState(), params.getKey(), params.getKeyLen(), params.getOutLen(), params.getSalt(), params.getPersonal()); } public static int cryptoGenerichashBlake2bUpdate(Blake2bUpdateParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE .cryptoGenerichashBlake2BUpdate(params.getState(), params.getIn(), params.getInLen()); } public static int cryptoGenerichashBlake2bFinal(Blake2bFinalParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE.cryptoGenerichashBlake2BFinal(params.getState(), params.getOut(), params.getOutLen()); } public static int cryptoGenerichashBlack2bSaltPersonal(Black2bSaltPersonalParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE.cryptoGenerichashBlake2BSaltPersonal(params.getOut(), params.getOutLen(), params.getIn(), params.getInLen(), params.getKey(), params.getKeyLen(), params.getSalt(), @@ -51,9 +39,6 @@ public static int cryptoGenerichashBlack2bSaltPersonal(Black2bSaltPersonalParams public static int cryptoAeadChacha20poly1305IetfDecrypt( Chacha20poly1305IetfDecryptParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE .cryptoAeadChacha20Poly1305IetfDecrypt(params.getM(), params.getMLenP(), params.getNSec(), @@ -63,9 +48,6 @@ public static int cryptoAeadChacha20poly1305IetfDecrypt( public static int cryptoAeadChacha20Poly1305IetfEncrypt( Chacha20Poly1305IetfEncryptParams params) { - if (!isOpenZen()) { - return 0; - } return INSTANCE .cryptoAeadChacha20Poly1305IetfEncrypt(params.getC(), params.getCLenP(), params.getM(), params.getMLen(), params.getAd(), params.getAdLen(), @@ -73,25 +55,10 @@ public static int cryptoAeadChacha20Poly1305IetfEncrypt( } public static long initState() { - if (!isOpenZen()) { - return 0; - } return INSTANCE.cryptoGenerichashBlake2BStateInit(); } public static void freeState(long state) { - if (!isOpenZen()) { - return; - } INSTANCE.cryptoGenerichashBlake2BStateFree(state); } - - private static boolean isOpenZen() { - boolean res = CommonParameter.getInstance() - .isFullNodeAllowShieldedTransactionArgs(); - if (res) { - INSTANCE = LibsodiumWrapper.getInstance(); - } - return res; - } } diff --git a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java index 95f436b19f0..b11c6b1e0a4 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java @@ -41,6 +41,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; import org.tron.common.crypto.ECKey.ECDSASignature; +import org.tron.common.crypto.Rsv; import org.tron.common.crypto.SignInterface; import org.tron.common.crypto.SignUtils; import org.tron.common.es.ExecutorServiceManager; @@ -456,14 +457,8 @@ public static long getCallValue(Transaction.Contract contract) { } public static String getBase64FromByteString(ByteString sign) { - byte[] r = sign.substring(0, 32).toByteArray(); - byte[] s = sign.substring(32, 64).toByteArray(); - byte v = sign.byteAt(64); - if (v < 27) { - v += 27; //revId -> v - } - ECDSASignature signature = ECDSASignature.fromComponents(r, s, v); - return signature.toBase64(); + Rsv rsv = Rsv.fromSignature(sign.toByteArray()); + return ECDSASignature.fromComponents(rsv.getR(), rsv.getS(), rsv.getV()).toBase64(); } public static boolean validateSignature(Transaction transaction, diff --git a/chainbase/src/main/java/org/tron/core/capsule/utils/MarketUtils.java b/chainbase/src/main/java/org/tron/core/capsule/utils/MarketUtils.java index d711ac0d63b..e98bba9db08 100644 --- a/chainbase/src/main/java/org/tron/core/capsule/utils/MarketUtils.java +++ b/chainbase/src/main/java/org/tron/core/capsule/utils/MarketUtils.java @@ -25,6 +25,7 @@ import org.tron.common.crypto.Hash; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; +import org.tron.common.utils.MarketComparator; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.MarketAccountOrderCapsule; import org.tron.core.capsule.MarketOrderCapsule; @@ -227,29 +228,6 @@ public static byte[] createPairKey(byte[] sellTokenId, byte[] buyTokenId) { return result; } - /** - * Note: the params should be the same token pair, or you should change the order. - * All the quantity should be bigger than 0. - * */ - public static int comparePrice(long price1SellQuantity, long price1BuyQuantity, - long price2SellQuantity, long price2BuyQuantity) { - try { - return Long.compare(multiplyExact(price1BuyQuantity, price2SellQuantity, true), - multiplyExact(price2BuyQuantity, price1SellQuantity, true)); - - } catch (ArithmeticException ex) { - // do nothing here, because we will use BigInteger to compute again - } - - BigInteger price1BuyQuantityBI = BigInteger.valueOf(price1BuyQuantity); - BigInteger price1SellQuantityBI = BigInteger.valueOf(price1SellQuantity); - BigInteger price2BuyQuantityBI = BigInteger.valueOf(price2BuyQuantity); - BigInteger price2SellQuantityBI = BigInteger.valueOf(price2SellQuantity); - - return price1BuyQuantityBI.multiply(price2SellQuantityBI) - .compareTo(price2BuyQuantityBI.multiply(price1SellQuantityBI)); - } - /** * if takerPrice >= makerPrice, return True * note: here are two different token pairs @@ -265,7 +243,8 @@ public static boolean priceMatch(MarketPrice takerPrice, MarketPrice makerPrice) // ==> Price_TRX * sellQuantity_taker/buyQuantity_taker >= Price_TRX * buyQuantity_maker/sellQuantity_maker // ==> sellQuantity_taker * sellQuantity_maker > buyQuantity_taker * buyQuantity_maker - return comparePrice(takerPrice.getBuyTokenQuantity(), takerPrice.getSellTokenQuantity(), + return MarketComparator.comparePrice(takerPrice.getBuyTokenQuantity(), + takerPrice.getSellTokenQuantity(), makerPrice.getSellTokenQuantity(), makerPrice.getBuyTokenQuantity()) >= 0; } @@ -316,57 +295,7 @@ public static void returnSellTokenRemain(MarketOrderCapsule orderCapsule, } public static int comparePriceKey(byte[] o1, byte[] o2) { - //compare pair - byte[] pair1 = new byte[TOKEN_ID_LENGTH * 2]; - byte[] pair2 = new byte[TOKEN_ID_LENGTH * 2]; - - System.arraycopy(o1, 0, pair1, 0, TOKEN_ID_LENGTH * 2); - System.arraycopy(o2, 0, pair2, 0, TOKEN_ID_LENGTH * 2); - - int pairResult = org.bouncycastle.util.Arrays.compareUnsigned(pair1, pair2); - if (pairResult != 0) { - return pairResult; - } - - //compare price - byte[] getSellTokenQuantity1 = new byte[8]; - byte[] getBuyTokenQuantity1 = new byte[8]; - - byte[] getSellTokenQuantity2 = new byte[8]; - byte[] getBuyTokenQuantity2 = new byte[8]; - - int longByteNum = 8; - - System.arraycopy(o1, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH, - getSellTokenQuantity1, 0, longByteNum); - System.arraycopy(o1, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum, - getBuyTokenQuantity1, 0, longByteNum); - - System.arraycopy(o2, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH, - getSellTokenQuantity2, 0, longByteNum); - System.arraycopy(o2, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum, - getBuyTokenQuantity2, 0, longByteNum); - - long sellTokenQuantity1 = ByteArray.toLong(getSellTokenQuantity1); - long buyTokenQuantity1 = ByteArray.toLong(getBuyTokenQuantity1); - long sellTokenQuantity2 = ByteArray.toLong(getSellTokenQuantity2); - long buyTokenQuantity2 = ByteArray.toLong(getBuyTokenQuantity2); - - if ((sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0) - && (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0)) { - return 0; - } - - if (sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0) { - return -1; - } - - if (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0) { - return 1; - } - - return comparePrice(sellTokenQuantity1, buyTokenQuantity1, - sellTokenQuantity2, buyTokenQuantity2); + return MarketComparator.comparePriceKey(o1, o2); } diff --git a/chainbase/src/main/java/org/tron/core/db/TronDatabase.java b/chainbase/src/main/java/org/tron/core/db/TronDatabase.java index d791e189eb4..40762568c82 100644 --- a/chainbase/src/main/java/org/tron/core/db/TronDatabase.java +++ b/chainbase/src/main/java/org/tron/core/db/TronDatabase.java @@ -8,8 +8,6 @@ import javax.annotation.PostConstruct; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.iq80.leveldb.WriteOptions; -import org.rocksdb.DirectComparator; import org.springframework.beans.factory.annotation.Autowired; import org.tron.common.parameter.CommonParameter; import org.tron.common.storage.WriteOptionsWrapper; @@ -29,7 +27,7 @@ public abstract class TronDatabase implements ITronChainBase { protected DbSourceInter dbSource; @Getter private String dbName; - private WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() + private final WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() .sync(CommonParameter.getInstance().getStorage().isDbSync()); @Autowired @@ -40,22 +38,13 @@ protected TronDatabase(String dbName) { if ("LEVELDB".equals(CommonParameter.getInstance().getStorage() .getDbEngine().toUpperCase())) { - dbSource = - new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), - dbName, - getOptionsByDbNameForLevelDB(dbName), - new WriteOptions().sync(CommonParameter.getInstance() - .getStorage().isDbSync())); + dbSource = new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), dbName); } else if ("ROCKSDB".equals(CommonParameter.getInstance() .getStorage().getDbEngine().toUpperCase())) { String parentName = Paths.get(StorageUtils.getOutputDirectoryByDbName(dbName), CommonParameter.getInstance().getStorage().getDbDirectory()).toString(); - dbSource = - new RocksDbDataSourceImpl(parentName, dbName, CommonParameter.getInstance() - .getRocksDBCustomSettings(), getDirectComparator()); + dbSource = new RocksDbDataSourceImpl(parentName, dbName); } - - dbSource.initDB(); } @PostConstruct @@ -66,14 +55,6 @@ protected void init() { protected TronDatabase() { } - protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) { - return StorageUtils.getOptionsByDbName(dbName); - } - - protected DirectComparator getDirectComparator() { - return null; - } - public DbSourceInter getDbSource() { return dbSource; } @@ -96,6 +77,7 @@ public void reset() { public void close() { logger.info("******** Begin to close {}. ********", getName()); try { + writeOptions.close(); dbSource.closeDB(); } catch (Exception e) { logger.warn("Failed to close {}.", getName(), e); diff --git a/chainbase/src/main/java/org/tron/core/db/TronStoreWithRevoking.java b/chainbase/src/main/java/org/tron/core/db/TronStoreWithRevoking.java index 4b75ddee3a4..73b1b103d76 100644 --- a/chainbase/src/main/java/org/tron/core/db/TronStoreWithRevoking.java +++ b/chainbase/src/main/java/org/tron/core/db/TronStoreWithRevoking.java @@ -15,8 +15,6 @@ import javax.annotation.PostConstruct; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.iq80.leveldb.WriteOptions; -import org.rocksdb.DirectComparator; import org.springframework.beans.factory.annotation.Autowired; import org.tron.common.parameter.CommonParameter; import org.tron.common.storage.leveldb.LevelDbDataSourceImpl; @@ -58,33 +56,18 @@ protected TronStoreWithRevoking(String dbName) { String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine(); if ("LEVELDB".equals(dbEngine.toUpperCase())) { this.db = new LevelDB( - new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), - dbName, - getOptionsByDbNameForLevelDB(dbName), - new WriteOptions().sync(CommonParameter.getInstance() - .getStorage().isDbSync()))); + new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), dbName)); } else if ("ROCKSDB".equals(dbEngine.toUpperCase())) { String parentPath = Paths .get(StorageUtils.getOutputDirectoryByDbName(dbName), CommonParameter .getInstance().getStorage().getDbDirectory()).toString(); - this.db = new RocksDB( - new RocksDbDataSourceImpl(parentPath, - dbName, CommonParameter.getInstance() - .getRocksDBCustomSettings(), getDirectComparator())); + this.db = new RocksDB(new RocksDbDataSourceImpl(parentPath, dbName)); } else { throw new RuntimeException(String.format("db engine %s is error", dbEngine)); } this.revokingDB = new Chainbase(new SnapshotRoot(this.db)); } - protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) { - return StorageUtils.getOptionsByDbName(dbName); - } - - protected DirectComparator getDirectComparator() { - return null; - } - protected TronStoreWithRevoking(DB db) { this.db = db; this.revokingDB = new Chainbase(new SnapshotRoot(db)); diff --git a/chainbase/src/main/java/org/tron/core/db/common/DbSourceInter.java b/chainbase/src/main/java/org/tron/core/db/common/DbSourceInter.java index 0823b7a7cf4..21c0a0dff2a 100755 --- a/chainbase/src/main/java/org/tron/core/db/common/DbSourceInter.java +++ b/chainbase/src/main/java/org/tron/core/db/common/DbSourceInter.java @@ -15,37 +15,82 @@ * You should have received a copy of the GNU Lesser General Public License * along with the ethereumJ library. If not, see . */ -package org.tron.core.db.common; -import org.tron.core.db2.common.WrappedByteArray; +package org.tron.core.db.common; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; +import java.io.File; +import java.nio.file.Paths; import java.util.Map; import java.util.Set; - +import org.tron.common.utils.FileUtil; +import org.tron.common.utils.PropUtil; +import org.tron.core.db2.common.WrappedByteArray; +import org.tron.core.exception.TronError; public interface DbSourceInter extends BatchSourceInter, Iterable> { + String ENGINE_KEY = "ENGINE"; + String ENGINE_FILE = "engine.properties"; + String ROCKSDB = "ROCKSDB"; + String LEVELDB = "LEVELDB"; + String getDBName(); void setDBName(String name); - void initDB(); - boolean isAlive(); void closeDB(); void resetDb(); + @VisibleForTesting + @Deprecated Set allKeys() throws RuntimeException; + @VisibleForTesting + @Deprecated Set allValues() throws RuntimeException; + @VisibleForTesting + @Deprecated long getTotal() throws RuntimeException; void stat(); Map prefixQuery(byte[] key); + static void checkOrInitEngine(String expectedEngine, String dir, TronError.ErrCode errCode) { + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + File currentFile = new File(dir, "CURRENT"); + if (ROCKSDB.equals(expectedEngine) && currentFile.exists() + && !Paths.get(engineFile).toFile().exists()) { + // if the CURRENT file exists, but the engine.properties file does not exist, it is LevelDB + // 000003.log CURRENT LOCK MANIFEST-000002 + throw new TronError( + String.format("Cannot open %s database with %s engine.", LEVELDB, ROCKSDB), errCode); + } + if (FileUtil.createDirIfNotExists(dir)) { + if (!FileUtil.createFileIfNotExists(engineFile)) { + throw new TronError(String.format("Cannot create file: %s.", engineFile), errCode); + } + } else { + throw new TronError(String.format("Cannot create dir: %s.", dir), errCode); + } + String actualEngine = PropUtil.readProperty(engineFile, ENGINE_KEY); + // engine init + if (Strings.isNullOrEmpty(actualEngine) + && !PropUtil.writeProperty(engineFile, ENGINE_KEY, expectedEngine)) { + throw new TronError(String.format("Cannot write file: %s.", engineFile), errCode); + } + actualEngine = PropUtil.readProperty(engineFile, ENGINE_KEY); + if (!expectedEngine.equals(actualEngine)) { + throw new TronError(String.format( + "Cannot open %s database with %s engine.", + actualEngine, expectedEngine), errCode); + } + } } diff --git a/chainbase/src/main/java/org/tron/core/db/common/iterator/RockStoreIterator.java b/chainbase/src/main/java/org/tron/core/db/common/iterator/RockStoreIterator.java index 541f71348af..cf9a5ff1e22 100644 --- a/chainbase/src/main/java/org/tron/core/db/common/iterator/RockStoreIterator.java +++ b/chainbase/src/main/java/org/tron/core/db/common/iterator/RockStoreIterator.java @@ -5,6 +5,7 @@ import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicBoolean; import lombok.extern.slf4j.Slf4j; +import org.rocksdb.ReadOptions; import org.rocksdb.RocksIterator; @@ -15,14 +16,17 @@ public final class RockStoreIterator implements DBIterator { private boolean first = true; private final AtomicBoolean close = new AtomicBoolean(false); + private final ReadOptions readOptions; - public RockStoreIterator(RocksIterator dbIterator) { + public RockStoreIterator(RocksIterator dbIterator, ReadOptions readOptions) { + this.readOptions = readOptions; this.dbIterator = dbIterator; } @Override public void close() throws IOException { if (close.compareAndSet(false, true)) { + readOptions.close(); dbIterator.close(); } } @@ -47,7 +51,7 @@ public boolean hasNext() { try { close(); } catch (Exception e1) { - logger.error(e.getMessage(), e); + logger.error(e1.getMessage(), e1); } } return hasNext; @@ -79,6 +83,11 @@ public byte[] setValue(byte[] value) { }; } + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + @Override public void seek(byte[] key) { checkState(); diff --git a/chainbase/src/main/java/org/tron/core/db/common/iterator/StoreIterator.java b/chainbase/src/main/java/org/tron/core/db/common/iterator/StoreIterator.java index d771716a7e8..c2803f99637 100755 --- a/chainbase/src/main/java/org/tron/core/db/common/iterator/StoreIterator.java +++ b/chainbase/src/main/java/org/tron/core/db/common/iterator/StoreIterator.java @@ -46,6 +46,11 @@ public boolean hasNext() { } } catch (Exception e) { logger.error(e.getMessage(), e); + try { + close(); + } catch (Exception e1) { + logger.error(e1.getMessage(), e1); + } } return hasNext; diff --git a/chainbase/src/main/java/org/tron/core/db2/common/LevelDB.java b/chainbase/src/main/java/org/tron/core/db2/common/LevelDB.java index b5616f87b8a..5942bb7444c 100644 --- a/chainbase/src/main/java/org/tron/core/db2/common/LevelDB.java +++ b/chainbase/src/main/java/org/tron/core/db2/common/LevelDB.java @@ -13,7 +13,7 @@ public class LevelDB implements DB, Flusher { @Getter private LevelDbDataSourceImpl db; - private WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() + private final WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() .sync(CommonParameter.getInstance().getStorage().isDbSync()); public LevelDB(LevelDbDataSourceImpl db) { @@ -65,6 +65,7 @@ public void flush(Map batch) { @Override public void close() { + this.writeOptions.close(); db.closeDB(); } diff --git a/chainbase/src/main/java/org/tron/core/db2/common/RocksDB.java b/chainbase/src/main/java/org/tron/core/db2/common/RocksDB.java index 31970b185cc..1d67438eceb 100644 --- a/chainbase/src/main/java/org/tron/core/db2/common/RocksDB.java +++ b/chainbase/src/main/java/org/tron/core/db2/common/RocksDB.java @@ -14,7 +14,7 @@ public class RocksDB implements DB, Flusher { @Getter private RocksDbDataSourceImpl db; - private WriteOptionsWrapper optionsWrapper = WriteOptionsWrapper.getInstance() + private final WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() .sync(CommonParameter.getInstance().getStorage().isDbSync()); public RocksDB(RocksDbDataSourceImpl db) { @@ -61,11 +61,12 @@ public void flush(Map batch) { Map rows = batch.entrySet().stream() .map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes())) .collect(HashMap::new, (m, k) -> m.put(k.getKey(), k.getValue()), HashMap::putAll); - db.updateByBatch(rows, optionsWrapper); + db.updateByBatch(rows, writeOptions); } @Override public void close() { + writeOptions.close(); db.closeDB(); } diff --git a/chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java b/chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java index 9d2409685c9..31131de0866 100644 --- a/chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java +++ b/chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java @@ -31,7 +31,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; import org.bouncycastle.util.encoders.Hex; -import org.iq80.leveldb.WriteOptions; import org.tron.common.parameter.CommonParameter; import org.tron.common.prometheus.MetricKeys; import org.tron.common.prometheus.Metrics; @@ -105,19 +104,13 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore, String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine(); if ("LEVELDB".equals(dbEngine.toUpperCase())) { this.persistentStore = new LevelDB( - new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name), - name, StorageUtils.getOptionsByDbName(name), - new WriteOptions().sync(CommonParameter.getInstance() - .getStorage().isDbSync()))); + new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name), name)); } else if ("ROCKSDB".equals(dbEngine.toUpperCase())) { String parentPath = Paths .get(StorageUtils.getOutputDirectoryByDbName(name), CommonParameter .getInstance().getStorage().getDbDirectory()).toString(); - this.persistentStore = new RocksDB( - new RocksDbDataSourceImpl(parentPath, - name, CommonParameter.getInstance() - .getRocksDBCustomSettings())); + this.persistentStore = new RocksDB(new RocksDbDataSourceImpl(parentPath, name)); } else { throw new RuntimeException(String.format("db type: %s is not supported", dbEngine)); } diff --git a/chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java b/chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java index eb27141a82c..e20490d93c0 100644 --- a/chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java +++ b/chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java @@ -29,7 +29,6 @@ import org.tron.common.error.TronDBException; import org.tron.common.es.ExecutorServiceManager; import org.tron.common.parameter.CommonParameter; -import org.tron.common.storage.WriteOptionsWrapper; import org.tron.common.utils.FileUtil; import org.tron.common.utils.StorageUtils; import org.tron.core.db.RevokingDatabase; @@ -357,7 +356,6 @@ public void flush() { public void createCheckpoint() { TronDatabase checkPointStore = null; - boolean syncFlag; try { Map batch = new HashMap<>(); for (Chainbase db : dbs) { @@ -389,16 +387,13 @@ public void createCheckpoint() { if (isV2Open()) { String dbName = String.valueOf(System.currentTimeMillis()); checkPointStore = getCheckpointDB(dbName); - syncFlag = CommonParameter.getInstance().getStorage().isCheckpointSync(); } else { checkPointStore = checkTmpStore; - syncFlag = CommonParameter.getInstance().getStorage().isDbSync(); } - checkPointStore.getDbSource().updateByBatch(batch.entrySet().stream() + checkPointStore.updateByBatch(batch.entrySet().stream() .map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes())) - .collect(HashMap::new, (m, k) -> m.put(k.getKey(), k.getValue()), HashMap::putAll), - WriteOptionsWrapper.getInstance().sync(syncFlag)); + .collect(HashMap::new, (m, k) -> m.put(k.getKey(), k.getValue()), HashMap::putAll)); } catch (Exception e) { throw new TronDBException(e); @@ -427,6 +422,10 @@ public List getCheckpointList() { } private void deleteCheckpoint() { + if(checkTmpStore == null) { + // only occurs in mock test. TODO fix test + return; + } try { Map hmap = new HashMap<>(); for (Map.Entry e : checkTmpStore.getDbSource()) { diff --git a/chainbase/src/main/java/org/tron/core/store/AccountStore.java b/chainbase/src/main/java/org/tron/core/store/AccountStore.java index 4d39049ee79..5aec5958729 100644 --- a/chainbase/src/main/java/org/tron/core/store/AccountStore.java +++ b/chainbase/src/main/java/org/tron/core/store/AccountStore.java @@ -12,6 +12,7 @@ import org.tron.core.capsule.BlockCapsule; import org.tron.core.db.TronStoreWithRevoking; import org.tron.core.db.accountstate.AccountStateCallBackUtils; +import org.tron.core.exception.TronError; import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace; import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace.Operation; @@ -23,6 +24,8 @@ @Component public class AccountStore extends TronStoreWithRevoking { + private static String ACCOUNT_BLACKHOLE = "Blackhole"; + private static Map assertsAddress = new HashMap<>(); // key = name , value = address @Autowired @@ -50,6 +53,9 @@ public static void setAccount(com.typesafe.config.Config config) { byte[] address = Commons.decodeFromBase58Check(obj.get("address").unwrapped().toString()); assertsAddress.put(accountName, address); } + if (assertsAddress.get(ACCOUNT_BLACKHOLE) == null) { + throw new TronError("Account[Blackhole] is not configured.", TronError.ErrCode.GENESIS_BLOCK_INIT); + } } @Override @@ -109,12 +115,12 @@ public AccountCapsule getSun() { * Min TRX account. */ public AccountCapsule getBlackhole() { - return getUnchecked(assertsAddress.get("Blackhole")); + return getUnchecked(assertsAddress.get(ACCOUNT_BLACKHOLE)); } public byte[] getBlackholeAddress() { - return assertsAddress.get("Blackhole"); + return assertsAddress.get(ACCOUNT_BLACKHOLE); } /** diff --git a/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java b/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java index 2f952e6b82a..f027bd02664 100644 --- a/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java +++ b/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java @@ -1,18 +1,23 @@ package org.tron.core.store; import com.google.protobuf.InvalidProtocolBufferException; +import java.util.Map; +import java.util.Spliterator; +import java.util.function.Consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.storage.WriteOptionsWrapper; import org.tron.core.db.TronDatabase; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ItemNotFoundException; -import java.util.Spliterator; -import java.util.function.Consumer; - @Slf4j(topic = "DB") public class CheckPointV2Store extends TronDatabase { + private final WriteOptionsWrapper writeOptions = WriteOptionsWrapper.getInstance() + .sync(CommonParameter.getInstance().getStorage().isCheckpointSync()); + @Autowired public CheckPointV2Store(String dbPath) { super(dbPath); @@ -52,6 +57,11 @@ public Spliterator spliterator() { protected void init() { } + @Override + public void updateByBatch(Map rows) { + this.dbSource.updateByBatch(rows, writeOptions); + } + /** * close the database. */ @@ -59,6 +69,7 @@ protected void init() { public void close() { logger.debug("******** Begin to close {}. ********", getName()); try { + writeOptions.close(); dbSource.closeDB(); } catch (Exception e) { logger.warn("Failed to close {}.", getName(), e); diff --git a/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java b/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java index 91b0cff68a0..89c5ba18e59 100644 --- a/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java +++ b/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java @@ -1,6 +1,8 @@ package org.tron.core.store; import static org.tron.common.math.Maths.max; +import static org.tron.core.Constant.MAX_PROPOSAL_EXPIRE_TIME; +import static org.tron.core.Constant.MIN_PROPOSAL_EXPIRE_TIME; import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL; import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD; @@ -231,6 +233,10 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking private static final byte[] ALLOW_TVM_CANCUN = "ALLOW_TVM_CANCUN".getBytes(); private static final byte[] ALLOW_TVM_BLOB = "ALLOW_TVM_BLOB".getBytes(); + private static final byte[] PROPOSAL_EXPIRE_TIME = "PROPOSAL_EXPIRE_TIME".getBytes(); + + private static final byte[] ALLOW_TVM_SELFDESTRUCT_RESTRICTION = + "ALLOW_TVM_SELFDESTRUCT_RESTRICTION".getBytes(); @Autowired private DynamicPropertiesStore(@Value("properties") String dbName) { @@ -2946,6 +2952,34 @@ public long getAllowTvmBlob() { .orElse(CommonParameter.getInstance().getAllowTvmBlob()); } + + public long getAllowTvmSelfdestructRestriction() { + return Optional.ofNullable(getUnchecked(ALLOW_TVM_SELFDESTRUCT_RESTRICTION)) + .map(BytesCapsule::getData) + .map(ByteArray::toLong) + .orElse(0L); + } + + public void saveAllowTvmSelfdestructRestriction(long value) { + this.put(ALLOW_TVM_SELFDESTRUCT_RESTRICTION, new BytesCapsule(ByteArray.fromLong(value))); + } + + public boolean allowTvmSelfdestructRestriction() { + return getAllowTvmSelfdestructRestriction() == 1L; + } + + public void saveProposalExpireTime(long proposalExpireTime) { + this.put(PROPOSAL_EXPIRE_TIME, new BytesCapsule(ByteArray.fromLong(proposalExpireTime))); + } + + public long getProposalExpireTime() { + return Optional.ofNullable(getUnchecked(PROPOSAL_EXPIRE_TIME)) + .map(BytesCapsule::getData) + .map(ByteArray::toLong) + .filter(time -> time > MIN_PROPOSAL_EXPIRE_TIME && time < MAX_PROPOSAL_EXPIRE_TIME) + .orElse(CommonParameter.getInstance().getProposalExpireTime()); + } + private static class DynamicResourceProperties { private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes(); diff --git a/chainbase/src/main/java/org/tron/core/store/MarketPairPriceToOrderStore.java b/chainbase/src/main/java/org/tron/core/store/MarketPairPriceToOrderStore.java index 605952328ed..391fb4249c8 100644 --- a/chainbase/src/main/java/org/tron/core/store/MarketPairPriceToOrderStore.java +++ b/chainbase/src/main/java/org/tron/core/store/MarketPairPriceToOrderStore.java @@ -3,16 +3,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.iq80.leveldb.Options; -import org.rocksdb.ComparatorOptions; -import org.rocksdb.DirectComparator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.tron.common.utils.ByteUtil; -import org.tron.common.utils.MarketOrderPriceComparatorForLevelDB; -import org.tron.common.utils.MarketOrderPriceComparatorForRockDB; -import org.tron.common.utils.StorageUtils; +import org.tron.core.Constant; import org.tron.core.capsule.MarketOrderIdListCapsule; import org.tron.core.capsule.utils.MarketUtils; import org.tron.core.db.TronStoreWithRevoking; @@ -22,24 +17,10 @@ public class MarketPairPriceToOrderStore extends TronStoreWithRevoking { @Autowired - protected MarketPairPriceToOrderStore(@Value("market_pair_price_to_order") String dbName) { + protected MarketPairPriceToOrderStore(@Value(Constant.MARKET_PAIR_PRICE_TO_ORDER) String dbName) { super(dbName); } - @Override - protected Options getOptionsByDbNameForLevelDB(String dbName) { - Options options = StorageUtils.getOptionsByDbName(dbName); - options.comparator(new MarketOrderPriceComparatorForLevelDB()); - return options; - } - - //todo: to test later - @Override - protected DirectComparator getDirectComparator() { - ComparatorOptions comparatorOptions = new ComparatorOptions(); - return new MarketOrderPriceComparatorForRockDB(comparatorOptions); - } - @Override public MarketOrderIdListCapsule get(byte[] key) throws ItemNotFoundException { byte[] value = revokingDB.get(key); diff --git a/chainbase/src/main/java/org/tron/core/store/WitnessStore.java b/chainbase/src/main/java/org/tron/core/store/WitnessStore.java index 9f444d3333d..9c30df195af 100644 --- a/chainbase/src/main/java/org/tron/core/store/WitnessStore.java +++ b/chainbase/src/main/java/org/tron/core/store/WitnessStore.java @@ -55,7 +55,7 @@ public List getWitnessStandby(boolean isSortOpt) { return ret; } - public void sortWitnesses(List witnesses, boolean isSortOpt) { + public static void sortWitnesses(List witnesses, boolean isSortOpt) { witnesses.sort(Comparator.comparingLong(WitnessCapsule::getVoteCount).reversed() .thenComparing(isSortOpt ? Comparator.comparing(WitnessCapsule::createReadableString).reversed() diff --git a/common/build.gradle b/common/build.gradle index c6ce8cf44f9..8ea91ecd5b1 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -6,45 +6,23 @@ version '1.0.0' sourceCompatibility = 1.8 -// Dependency versions -// --------------------------------------- -def leveldbVersion = "1.8" -// -------------------------------------- - -static def isWindows() { - return org.gradle.internal.os.OperatingSystem.current().isWindows() -} - -if (isWindows()) { - ext { - leveldbGroup = "org.ethereum" - leveldbName = "leveldbjni-all" - leveldbVersion = "1.18.3" - } -} else { - ext { - leveldbGroup = "org.fusesource.leveldbjni" - leveldbName = "leveldbjni-all" - leveldbVersion = "1.8" - } -} dependencies { - api group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4.2' // https://github.com/FasterXML/jackson-databind/issues/3627 - api "com.cedarsoftware:java-util:1.8.0" + api group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.18.3' // https://github.com/FasterXML/jackson-databind/issues/3627 + api "com.cedarsoftware:java-util:3.2.0" api group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.1' api group: 'commons-codec', name: 'commons-codec', version: '1.11' api group: 'com.beust', name: 'jcommander', version: '1.78' api group: 'com.typesafe', name: 'config', version: '1.3.2' - api group: leveldbGroup, name: leveldbName, version: leveldbVersion - api group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10' api group: 'io.prometheus', name: 'simpleclient', version: '0.15.0' api group: 'io.prometheus', name: 'simpleclient_httpserver', version: '0.15.0' api group: 'io.prometheus', name: 'simpleclient_hotspot', version: '0.15.0' - api 'org.aspectj:aspectjrt:1.8.13' - api 'org.aspectj:aspectjweaver:1.8.13' - api 'org.aspectj:aspectjtools:1.8.13' - api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.5',{ + // https://openjdk.org/jeps/396, JEP 396: Strongly Encapsulate JDK Internals by Default + // https://eclipse.dev/aspectj/doc/latest/release/JavaVersionCompatibility.html + api 'org.aspectj:aspectjrt:1.9.8' + api 'org.aspectj:aspectjweaver:1.9.8' + api 'org.aspectj:aspectjtools:1.9.8' + api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.7',{ exclude group: 'io.grpc', module: 'grpc-context' exclude group: 'io.grpc', module: 'grpc-core' exclude group: 'io.grpc', module: 'grpc-netty' @@ -62,6 +40,7 @@ dependencies { exclude group: 'org.bouncycastle', module: 'bcutil-jdk18on' } api project(":protocol") + api project(":platform") } jacocoTestReport { diff --git a/common/src/main/java/org/tron/common/exit/ExitManager.java b/common/src/main/java/org/tron/common/exit/ExitManager.java index d80b7838c08..1a1cc9ed3fc 100644 --- a/common/src/main/java/org/tron/common/exit/ExitManager.java +++ b/common/src/main/java/org/tron/common/exit/ExitManager.java @@ -46,7 +46,7 @@ public static Optional findTronError(Throwable e) { public static void logAndExit(TronError exit) { final int code = exit.getErrCode().getCode(); - logger.error("Shutting down with code: {}.", exit.getErrCode(), exit); + logger.error("Shutting down with code: {}, reason: {}", exit.getErrCode(), exit.getMessage()); Thread exitThread = exitThreadFactory.newThread(() -> System.exit(code)); exitThread.start(); } diff --git a/common/src/main/java/org/tron/common/log/LogService.java b/common/src/main/java/org/tron/common/log/LogService.java index 801e13f54f7..bce52001e92 100644 --- a/common/src/main/java/org/tron/common/log/LogService.java +++ b/common/src/main/java/org/tron/common/log/LogService.java @@ -2,6 +2,7 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.core.util.StatusPrinter; import java.io.File; import org.slf4j.LoggerFactory; import org.tron.core.exception.TronError; @@ -9,18 +10,20 @@ public class LogService { public static void load(String path) { + LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { File file = new File(path); if (!file.exists() || !file.isFile() || !file.canRead()) { return; } - LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure(file); } catch (Exception e) { throw new TronError(e, TronError.ErrCode.LOG_LOAD); + } finally { + StatusPrinter.printInCaseOfErrorsOrWarnings(lc); } } } diff --git a/common/src/main/java/org/tron/common/logsfilter/FilterQuery.java b/common/src/main/java/org/tron/common/logsfilter/FilterQuery.java index b2d0fc428e4..2ae50370129 100644 --- a/common/src/main/java/org/tron/common/logsfilter/FilterQuery.java +++ b/common/src/main/java/org/tron/common/logsfilter/FilterQuery.java @@ -31,12 +31,7 @@ public static long parseFromBlockNumber(String blockNum) { if (StringUtils.isEmpty(blockNum) || FilterQuery.EARLIEST.equalsIgnoreCase(blockNum)) { number = FilterQuery.EARLIEST_BLOCK_NUM; } else { - try { - number = Long.parseLong(blockNum); - } catch (Exception e) { - logger.error("invalid filter: fromBlockNumber: {}", blockNum); - throw e; - } + number = Long.parseLong(blockNum); } return number; } @@ -46,12 +41,7 @@ public static long parseToBlockNumber(String blockNum) { if (StringUtils.isEmpty(blockNum) || FilterQuery.LATEST.equalsIgnoreCase(blockNum)) { number = FilterQuery.LATEST_BLOCK_NUM; } else { - try { - number = Long.parseLong(blockNum); - } catch (Exception e) { - logger.error("invalid filter: toBlockNumber: {}", blockNum); - throw e; - } + number = Long.parseLong(blockNum); } return number; } diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 45893970fb0..0583962f266 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -67,7 +67,7 @@ public class CommonParameter { public boolean debug = false; @Getter @Setter - @Parameter(names = {"--min-time-ratio"}, description = "Maximum CPU tolerance when executing " + @Parameter(names = {"--min-time-ratio"}, description = "Minimum CPU tolerance when executing " + "timeout transactions while synchronizing blocks. (default: 0.0)") public double minTimeRatio = 0.0; @Getter @@ -205,7 +205,15 @@ public class CommonParameter { //If you are running a solidity node for java tron, this flag is set to true @Getter @Setter + @Parameter(names = {"--solidity"}, description = "running a solidity node for java tron") public boolean solidityNode = false; + + //If you are running KeystoreFactory, this flag is set to true + @Getter + @Setter + @Parameter(names = {"--keystore-factory"}, description = "running KeystoreFactory") + public boolean keystoreFactory = false; + @Getter @Setter public int rpcPort; @@ -241,6 +249,15 @@ public class CommonParameter { @Getter @Setter public int flowControlWindow; + // the positive limit of RST_STREAM frames per connection per period for grpc, + // 0 or Integer.MAX_VALUE for unlimited, by default there is no limit. + @Getter + @Setter + public int rpcMaxRstStream; + // the positive number of seconds per period for grpc + @Getter + @Setter + public int rpcSecondsPerWindow; @Getter @Setter public long maxConnectionIdleInMillis; @@ -383,7 +400,7 @@ public class CommonParameter { // full node used this parameter to close shielded transaction @Getter @Setter - public boolean fullNodeAllowShieldedTransactionArgs; + public boolean allowShieldedTransactionApi; @Getter @Setter public long blockNumForEnergyLimit; @@ -429,6 +446,15 @@ public class CommonParameter { @Getter public int rateLimiterGlobalApiQps; @Getter + @Setter + public double rateLimiterSyncBlockChain; + @Getter + @Setter + public double rateLimiterFetchInvData; + @Getter + @Setter + public double rateLimiterDisconnect; + @Getter public DbBackupConfig dbBackupConfig; @Getter public RocksDbSettings rocksDBCustomSettings; @@ -501,6 +527,9 @@ public class CommonParameter { @Getter @Setter public int jsonRpcMaxSubTopics = 1000; + @Getter + @Setter + public int jsonRpcMaxBlockFilterNum = 50000; @Getter @Setter diff --git a/common/src/main/java/org/tron/common/setting/RocksDbSettings.java b/common/src/main/java/org/tron/common/setting/RocksDbSettings.java index 7436150cae2..d5df5e261b5 100644 --- a/common/src/main/java/org/tron/common/setting/RocksDbSettings.java +++ b/common/src/main/java/org/tron/common/setting/RocksDbSettings.java @@ -1,16 +1,26 @@ package org.tron.common.setting; +import static org.tron.core.Constant.ROCKSDB; + +import java.util.Arrays; import lombok.Getter; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.rocksdb.BlockBasedTableConfig; +import org.rocksdb.BloomFilter; +import org.rocksdb.ComparatorOptions; +import org.rocksdb.InfoLogLevel; import org.rocksdb.LRUCache; +import org.rocksdb.Logger; +import org.rocksdb.Options; import org.rocksdb.RocksDB; +import org.rocksdb.Statistics; +import org.slf4j.LoggerFactory; +import org.tron.common.utils.MarketOrderPriceComparatorForRocksDB; +import org.tron.core.Constant; @Slf4j public class RocksDbSettings { - @Setter - @Getter private static RocksDbSettings rocksDbSettings; @Getter @@ -40,6 +50,17 @@ public class RocksDbSettings { private static final LRUCache cache = new LRUCache(1 * 1024 * 1024 * 1024L); + private static final String[] CI_ENVIRONMENT_VARIABLES = { + "CI", + "JENKINS_URL", + "TRAVIS", + "CIRCLECI", + "GITHUB_ACTIONS", + "GITLAB_CI" + }; + + private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB); + private RocksDbSettings() { } @@ -60,9 +81,9 @@ public static RocksDbSettings initCustomSettings(int levelNumber, int compactThr int blockSize, long maxBytesForLevelBase, double maxBytesForLevelMultiplier, int level0FileNumCompactionTrigger, long targetFileSizeBase, - int targetFileSizeMultiplier) { + int targetFileSizeMultiplier, int maxOpenFiles) { rocksDbSettings = new RocksDbSettings() - .withMaxOpenFiles(5000) + .withMaxOpenFiles(maxOpenFiles) .withEnableStatistics(false) .withLevelNumber(levelNumber) .withCompactThreads(compactThreads) @@ -76,16 +97,17 @@ public static RocksDbSettings initCustomSettings(int levelNumber, int compactThr } public static void loggingSettings() { - logger.info(String.format( - "level number: %d, CompactThreads: %d, Blocksize: %d, maxBytesForLevelBase: %d," - + " withMaxBytesForLevelMultiplier: %f, level0FileNumCompactionTrigger: %d, " - + "withTargetFileSizeBase: %d, withTargetFileSizeMultiplier: %d", + logger.info( + "level number: {}, CompactThreads: {}, Blocksize:{}, maxBytesForLevelBase: {}," + + " withMaxBytesForLevelMultiplier: {}, level0FileNumCompactionTrigger: {}, " + + "withTargetFileSizeBase: {}, withTargetFileSizeMultiplier: {}, maxOpenFiles: {}", rocksDbSettings.getLevelNumber(), rocksDbSettings.getCompactThreads(), rocksDbSettings.getBlockSize(), rocksDbSettings.getMaxBytesForLevelBase(), rocksDbSettings.getMaxBytesForLevelMultiplier(), rocksDbSettings.getLevel0FileNumCompactionTrigger(), - rocksDbSettings.getTargetFileSizeBase(), rocksDbSettings.getTargetFileSizeMultiplier())); + rocksDbSettings.getTargetFileSizeBase(), rocksDbSettings.getTargetFileSizeMultiplier(), + rocksDbSettings.getMaxOpenFiles()); } public RocksDbSettings withMaxOpenFiles(int maxOpenFiles) { @@ -140,4 +162,85 @@ public RocksDbSettings withTargetFileSizeMultiplier(int targetFileSizeMultiplier public static LRUCache getCache() { return cache; } + + /** + * Creates a new RocksDB Options. + * + *

CRITICAL: Must be closed after use to prevent native memory leaks. + * Use try-with-resources. + * + *

{@code
+   * try (Options options = getOptionsByDbName(dbName)) {
+   *     // do something
+   * }
+   * }
+ * + * @param dbName db name + * @return a new Options instance that must be closed + */ + public static Options getOptionsByDbName(String dbName) { + RocksDbSettings settings = getSettings(); + + Options options = new Options(); + + options.setLogger(new Logger(options) { + @Override + protected void log(InfoLogLevel infoLogLevel, String logMsg) { + rocksDbLogger.info("{} {}", dbName, logMsg); + } + }); + // most of these options are suggested by https://github.com/facebook/rocksdb/wiki/Set-Up-Options + + // general options + if (settings.isEnableStatistics()) { + options.setStatistics(new Statistics()); + options.setStatsDumpPeriodSec(60); + } + options.setCreateIfMissing(true); + options.setIncreaseParallelism(1); + options.setLevelCompactionDynamicLevelBytes(true); + options.setMaxOpenFiles(settings.getMaxOpenFiles()); + + // general options supported user config + options.setNumLevels(settings.getLevelNumber()); + options.setMaxBytesForLevelMultiplier(settings.getMaxBytesForLevelMultiplier()); + options.setMaxBytesForLevelBase(settings.getMaxBytesForLevelBase()); + options.setMaxBackgroundCompactions(settings.getCompactThreads()); + options.setLevel0FileNumCompactionTrigger(settings.getLevel0FileNumCompactionTrigger()); + options.setTargetFileSizeMultiplier(settings.getTargetFileSizeMultiplier()); + options.setTargetFileSizeBase(settings.getTargetFileSizeBase()); + + // table options + final BlockBasedTableConfig tableCfg; + options.setTableFormatConfig(tableCfg = new BlockBasedTableConfig()); + tableCfg.setBlockSize(settings.getBlockSize()); + tableCfg.setBlockCache(RocksDbSettings.getCache()); + tableCfg.setCacheIndexAndFilterBlocks(true); + tableCfg.setPinL0FilterAndIndexBlocksInCache(true); + tableCfg.setFilter(new BloomFilter(10, false)); + if (Constant.MARKET_PAIR_PRICE_TO_ORDER.equals(dbName)) { + ComparatorOptions comparatorOptions = new ComparatorOptions(); + options.setComparator(new MarketOrderPriceComparatorForRocksDB(comparatorOptions)); + } + + if (isRunningInCI()) { + options.optimizeForSmallDb(); + // Disable fallocate calls to avoid issues with disk space + options.setAllowFAllocate(false); + // Set WAL size limits to avoid excessive disk + options.setMaxTotalWalSize(2 * 1024 * 1024); + // Set recycle log file + options.setRecycleLogFileNum(1); + // Enable creation of missing column families + options.setCreateMissingColumnFamilies(true); + // Set max background flushes to 1 to reduce resource usage + options.setMaxBackgroundFlushes(1); + } + + return options; + } + + private static boolean isRunningInCI() { + return Arrays.stream(CI_ENVIRONMENT_VARIABLES).anyMatch(System.getenv()::containsKey); + } } diff --git a/common/src/main/java/org/tron/common/utils/ByteArray.java b/common/src/main/java/org/tron/common/utils/ByteArray.java index b77dd310380..d0ac4cbaddf 100644 --- a/common/src/main/java/org/tron/common/utils/ByteArray.java +++ b/common/src/main/java/org/tron/common/utils/ByteArray.java @@ -14,7 +14,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; /* * Copyright (c) [2016] [ ] diff --git a/common/src/main/java/org/tron/common/utils/StringUtil.java b/common/src/main/java/org/tron/common/utils/StringUtil.java index ce3e95af46e..412a70d7f9c 100644 --- a/common/src/main/java/org/tron/common/utils/StringUtil.java +++ b/common/src/main/java/org/tron/common/utils/StringUtil.java @@ -44,4 +44,21 @@ public static String createReadableString(ByteString string) { public static ByteString hexString2ByteString(String hexString) { return ByteString.copyFrom(ByteArray.fromHexString(hexString)); } + + public static boolean isHexadecimal(String str) { + if (str == null || str.isEmpty()) { + return false; + } + if (str.length() % 2 != 0) { + return false; + } + + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + if (Character.digit(c, 16) == -1) { + return false; + } + } + return true; + } } diff --git a/common/src/main/java/org/tron/common/utils/TimeoutInterceptor.java b/common/src/main/java/org/tron/common/utils/TimeoutInterceptor.java new file mode 100644 index 00000000000..07b00e861e8 --- /dev/null +++ b/common/src/main/java/org/tron/common/utils/TimeoutInterceptor.java @@ -0,0 +1,30 @@ +package org.tron.common.utils; + +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.MethodDescriptor; +import java.util.concurrent.TimeUnit; + + +public class TimeoutInterceptor implements ClientInterceptor { + + private final long timeout; + + /** + * @param timeout ms + */ + public TimeoutInterceptor(long timeout) { + this.timeout = timeout; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, + CallOptions callOptions, + Channel next) { + return next.newCall(method, + callOptions.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS)); + } +} diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index c5a8a02fb4e..47331808a5b 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -24,6 +24,10 @@ public class Constant { public static final int NODE_TYPE_FULL_NODE = 0; public static final int NODE_TYPE_LIGHT_NODE = 1; + // DB NAME + public static final String MARKET_PAIR_PRICE_TO_ORDER = "market_pair_price_to_order"; + // DB NAME + // config for transaction public static final long TRANSACTION_MAX_BYTE_SIZE = 500 * 1_024L; public static final int CREATE_ACCOUNT_TRANSACTION_MIN_BYTE_SIZE = 500; @@ -39,6 +43,9 @@ public class Constant { public static final long MAX_CONTRACT_RESULT_SIZE = 2L; public static final long PB_DEFAULT_ENERGY_LIMIT = 0L; public static final long CREATOR_DEFAULT_ENERGY_LIMIT = 1000 * 10_000L; + public static final long MIN_PROPOSAL_EXPIRE_TIME = 0L; // 0 ms + public static final long MAX_PROPOSAL_EXPIRE_TIME = 31536003000L; // ms of 365 days + 3000 ms + public static final long DEFAULT_PROPOSAL_EXPIRE_TIME = 259200000L; // ms of 3 days // Numbers @@ -147,6 +154,7 @@ public class Constant { 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_JSONRPC_MAX_BLOCK_FILTER_NUM = "node.jsonrpc.maxBlockFilterNum"; public static final String NODE_DISABLED_API_LIST = "node.disabledApi"; @@ -156,6 +164,8 @@ public class Constant { public static final String NODE_RPC_MAX_CONCURRENT_CALLS_PER_CONNECTION = "node.rpc.maxConcurrentCallsPerConnection"; public static final String NODE_RPC_FLOW_CONTROL_WINDOW = "node.rpc.flowControlWindow"; public static final String NODE_RPC_MAX_CONNECTION_IDLE_IN_MILLIS = "node.rpc.maxConnectionIdleInMillis"; + public static final String NODE_RPC_MAX_RST_STREAM = "node.rpc.maxRstStream"; + public static final String NODE_RPC_SECONDS_PER_WINDOW = "node.rpc.secondsPerWindow"; public static final String NODE_PRODUCED_TIMEOUT = "node.blockProducedTimeOut"; public static final String NODE_MAX_HTTP_CONNECT_NUMBER = "node.maxHttpConnectNumber"; @@ -251,6 +261,9 @@ public class Constant { public static final String NODE_FULLNODE_ALLOW_SHIELDED_TRANSACTION = "node" + ".fullNodeAllowShieldedTransaction"; + public static final String ALLOW_SHIELDED_TRANSACTION_API = "node" + + ".allowShieldedTransactionApi"; + public static final String NODE_ZEN_TOKENID = "node.zenTokenId"; public static final String COMMITTEE_ALLOW_PROTO_FILTER_NUM = "committee.allowProtoFilterNum"; @@ -318,6 +331,9 @@ public class Constant { public static final String RATE_LIMITER_HTTP = "rate.limiter.http"; public static final String RATE_LIMITER_RPC = "rate.limiter.rpc"; + public static final String RATE_LIMITER_P2P_SYNC_BLOCK_CHAIN = "rate.limiter.p2p.syncBlockChain"; + public static final String RATE_LIMITER_P2P_FETCH_INV_DATA = "rate.limiter.p2p.fetchInvData"; + public static final String RATE_LIMITER_P2P_DISCONNECT = "rate.limiter.p2p.disconnect"; public static final String SEED_NODE_IP_LIST = "seed.node.ip.list"; public static final String NODE_METRICS_ENABLE = "node.metricsEnable"; @@ -405,4 +421,7 @@ public class Constant { public static final String COMMITTEE_ALLOW_TVM_CANCUN = "committee.allowTvmCancun"; public static final String COMMITTEE_ALLOW_TVM_BLOB = "committee.allowTvmBlob"; + + public static final String COMMITTEE_PROPOSAL_EXPIRE_TIME = "committee.proposalExpireTime"; + } diff --git a/common/src/main/java/org/tron/core/config/CommonConfig.java b/common/src/main/java/org/tron/core/config/CommonConfig.java index 4d17a0faedd..b258a4cf3f5 100644 --- a/common/src/main/java/org/tron/core/config/CommonConfig.java +++ b/common/src/main/java/org/tron/core/config/CommonConfig.java @@ -21,10 +21,8 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; -import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableTransactionManagement @EnableAspectJAutoProxy @ComponentScan(basePackages = "org.tron") public class CommonConfig { diff --git a/common/src/main/java/org/tron/core/config/Parameter.java b/common/src/main/java/org/tron/core/config/Parameter.java index c0e1bb06470..8ec27ed3eb5 100644 --- a/common/src/main/java/org/tron/core/config/Parameter.java +++ b/common/src/main/java/org/tron/core/config/Parameter.java @@ -27,7 +27,8 @@ public enum ForkBlockVersionEnum { VERSION_4_7_5(30, 1596780000000L, 80), VERSION_4_7_7(31, 1596780000000L, 80), VERSION_4_8_0(32, 1596780000000L, 80), - VERSION_4_8_0_1(33, 1596780000000L, 70); + VERSION_4_8_0_1(33, 1596780000000L, 70), + VERSION_4_8_1(34, 1596780000000L, 80); // if add a version, modify BLOCK_VERSION simultaneously @Getter @@ -76,7 +77,7 @@ public class ChainConstant { public static final int SINGLE_REPEAT = 1; public static final int BLOCK_FILLED_SLOTS_NUMBER = 128; public static final int MAX_FROZEN_NUMBER = 1; - public static final int BLOCK_VERSION = 33; + public static final int BLOCK_VERSION = 34; public static final long FROZEN_PERIOD = 86_400_000L; public static final long DELEGATE_PERIOD = 3 * 86_400_000L; public static final long TRX_PRECISION = 1000_000L; @@ -108,6 +109,7 @@ public class DatabaseConstants { public static final int PROPOSAL_COUNT_LIMIT_MAX = 1000; public static final int EXCHANGE_COUNT_LIMIT_MAX = 1000; public static final int MARKET_COUNT_LIMIT_MAX = 1000; + public static final int WITNESS_COUNT_LIMIT_MAX = 1000; } public class AdaptiveResourceLimitConstants { diff --git a/common/src/main/java/org/tron/core/config/args/Storage.java b/common/src/main/java/org/tron/core/config/args/Storage.java index 9cf6eb6bab1..655b6b779fe 100644 --- a/common/src/main/java/org/tron/core/config/args/Storage.java +++ b/common/src/main/java/org/tron/core/config/args/Storage.java @@ -25,9 +25,11 @@ import java.util.stream.Collectors; import lombok.Getter; import lombok.Setter; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.iq80.leveldb.CompressionType; import org.iq80.leveldb.Options; +import org.tron.common.arch.Arch; import org.tron.common.cache.CacheStrategies; import org.tron.common.cache.CacheType; import org.tron.common.utils.DbOptionalsUtils; @@ -42,6 +44,7 @@ * @version 1.0 * @since 2018/5/25 */ +@Slf4j(topic = "db") public class Storage { /** @@ -87,6 +90,7 @@ public class Storage { * Default values of directory */ private static final String DEFAULT_DB_ENGINE = "LEVELDB"; + private static final String ROCKS_DB_ENGINE = "ROCKSDB"; private static final boolean DEFAULT_DB_SYNC = false; private static final boolean DEFAULT_EVENT_SUBSCRIBE_CONTRACT_PARSE = true; private static final String DEFAULT_DB_DIRECTORY = "database"; @@ -171,6 +175,11 @@ public class Storage { private final Map dbRoots = Maps.newConcurrentMap(); public static String getDbEngineFromConfig(final Config config) { + if (Arch.isArm64()) { + // if is arm64 but config is leveldb, should throw exception? + logger.warn("Arm64 architecture detected, using RocksDB as db engine, ignore config."); + return ROCKS_DB_ENGINE; + } return config.hasPath(DB_ENGINE_CONFIG_KEY) ? config.getString(DB_ENGINE_CONFIG_KEY) : DEFAULT_DB_ENGINE; } diff --git a/common/src/main/java/org/tron/core/exception/MaintenanceUnavailableException.java b/common/src/main/java/org/tron/core/exception/MaintenanceUnavailableException.java new file mode 100644 index 00000000000..1911f84a616 --- /dev/null +++ b/common/src/main/java/org/tron/core/exception/MaintenanceUnavailableException.java @@ -0,0 +1,20 @@ +package org.tron.core.exception; + +/** + * Maintenance unavailable exception - thrown when service is in maintenance state + * Please try again later + */ +public class MaintenanceUnavailableException extends TronException { + + public MaintenanceUnavailableException() { + super(); + } + + public MaintenanceUnavailableException(String message) { + super(message); + } + + public MaintenanceUnavailableException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/common/src/main/java/org/tron/core/exception/P2pException.java b/common/src/main/java/org/tron/core/exception/P2pException.java index 00d82e9fbf7..eae830627c2 100644 --- a/common/src/main/java/org/tron/core/exception/P2pException.java +++ b/common/src/main/java/org/tron/core/exception/P2pException.java @@ -52,6 +52,7 @@ public enum TypeEnum { PROTOBUF_ERROR(14, "protobuf inconsistent"), BLOCK_SIGN_ERROR(15, "block sign error"), BLOCK_MERKLE_ERROR(16, "block merkle error"), + RATE_LIMIT_EXCEEDED(17, "rate limit exceeded"), DEFAULT(100, "default exception"); diff --git a/common/src/main/java/org/tron/core/exception/TronError.java b/common/src/main/java/org/tron/core/exception/TronError.java index 9d11d249476..f407c6dfe3c 100644 --- a/common/src/main/java/org/tron/core/exception/TronError.java +++ b/common/src/main/java/org/tron/core/exception/TronError.java @@ -47,7 +47,9 @@ public enum ErrCode { LOG_LOAD(1), WITNESS_INIT(1), RATE_LIMITER_INIT(1), - SOLID_NODE_INIT(0); + SOLID_NODE_INIT(0), + PARAMETER_INIT(1), + JDK_VERSION(1); private final int code; diff --git a/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcExceedLimitException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcExceedLimitException.java new file mode 100644 index 00000000000..9e6f59d4636 --- /dev/null +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcExceedLimitException.java @@ -0,0 +1,16 @@ +package org.tron.core.exception.jsonrpc; + +public class JsonRpcExceedLimitException extends JsonRpcException { + + public JsonRpcExceedLimitException() { + super(); + } + + public JsonRpcExceedLimitException(String message) { + super(message); + } + + public JsonRpcExceedLimitException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java new file mode 100644 index 00000000000..7ed42d101d5 --- /dev/null +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java @@ -0,0 +1,32 @@ +package org.tron.core.exception.jsonrpc; + +import lombok.Getter; +import org.tron.core.exception.TronException; + +@Getter +public class JsonRpcException extends TronException { + private Object data = null; + + public JsonRpcException() { + super(); + report(); + } + + public JsonRpcException(String message, Object data) { + super(message); + this.data = data; + report(); + } + + public JsonRpcException(String message) { + super(message); + report(); + } + + public JsonRpcException(String message, Throwable cause) { + super(message, cause); + report(); + } + + +} diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java similarity index 53% rename from common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java index 12310e67747..904449866ae 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInternalException extends TronException { +public class JsonRpcInternalException extends JsonRpcException { public JsonRpcInternalException() { super(); @@ -13,4 +13,8 @@ public JsonRpcInternalException(String message) { public JsonRpcInternalException(String message, Throwable cause) { super(message, cause); } + + public JsonRpcInternalException(String message, Object data) { + super(message, data); + } } \ No newline at end of file diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java similarity index 68% rename from common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java index adf205a309a..55ee15521e1 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInvalidParamsException extends TronException { +public class JsonRpcInvalidParamsException extends JsonRpcException { public JsonRpcInvalidParamsException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java similarity index 69% rename from common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java index 2689bff0cd2..32bd11a3ed9 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInvalidRequestException extends TronException { +public class JsonRpcInvalidRequestException extends JsonRpcException { public JsonRpcInvalidRequestException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java similarity index 68% rename from common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java index d8e18168d9d..406a51f45bd 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcMethodNotFoundException extends TronException { +public class JsonRpcMethodNotFoundException extends JsonRpcException { public JsonRpcMethodNotFoundException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java similarity index 69% rename from common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java index e8e183d49c1..03bc089b1c7 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcTooManyResultException extends TronException { +public class JsonRpcTooManyResultException extends JsonRpcException { public JsonRpcTooManyResultException() { super(); diff --git a/common/src/main/java/org/tron/core/vm/config/VMConfig.java b/common/src/main/java/org/tron/core/vm/config/VMConfig.java index 2bdbeb78b92..578827b2f8c 100644 --- a/common/src/main/java/org/tron/core/vm/config/VMConfig.java +++ b/common/src/main/java/org/tron/core/vm/config/VMConfig.java @@ -59,6 +59,8 @@ public class VMConfig { private static boolean ALLOW_TVM_BLOB = false; + private static boolean ALLOW_TVM_SELFDESTRUCT_RESTRICTION = false; + private VMConfig() { } @@ -166,6 +168,10 @@ public static void initAllowTvmBlob(long allow) { ALLOW_TVM_BLOB = allow == 1; } + public static void initAllowTvmSelfdestructRestriction(long allow) { + ALLOW_TVM_SELFDESTRUCT_RESTRICTION = allow == 1; + } + public static boolean getEnergyLimitHardFork() { return CommonParameter.ENERGY_LIMIT_HARD_FORK; } @@ -261,4 +267,8 @@ public static boolean disableJavaLangMath() { public static boolean allowTvmBlob() { return ALLOW_TVM_BLOB; } + + public static boolean allowTvmSelfdestructRestriction() { + return ALLOW_TVM_SELFDESTRUCT_RESTRICTION; + } } diff --git a/crypto/src/main/java/org/tron/common/crypto/ECKey.java b/crypto/src/main/java/org/tron/common/crypto/ECKey.java index 5fe8b9ef359..d0a6048aca1 100644 --- a/crypto/src/main/java/org/tron/common/crypto/ECKey.java +++ b/crypto/src/main/java/org/tron/common/crypto/ECKey.java @@ -55,6 +55,7 @@ import org.tron.common.crypto.jce.ECKeyPairGenerator; import org.tron.common.crypto.jce.TronCastleProvider; import org.tron.common.utils.BIUtil; +import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; @Slf4j(topic = "crypto") @@ -285,7 +286,7 @@ public static ECKey fromPrivate(BigInteger privKey) { * @return - */ public static ECKey fromPrivate(byte[] privKeyBytes) { - if (Objects.isNull(privKeyBytes)) { + if (ByteArray.isEmpty(privKeyBytes)) { return null; } return fromPrivate(new BigInteger(1, privKeyBytes)); diff --git a/crypto/src/main/java/org/tron/common/crypto/Rsv.java b/crypto/src/main/java/org/tron/common/crypto/Rsv.java new file mode 100644 index 00000000000..15c8498e836 --- /dev/null +++ b/crypto/src/main/java/org/tron/common/crypto/Rsv.java @@ -0,0 +1,26 @@ +package org.tron.common.crypto; + + +import java.util.Arrays; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class Rsv { + + private final byte[] r; + private final byte[] s; + private final byte v; + + + public static Rsv fromSignature(byte[] sign) { + byte[] r = Arrays.copyOfRange(sign, 0, 32); + byte[] s = Arrays.copyOfRange(sign, 32, 64); + byte v = sign[64]; + if (v < 27) { + v += 27; //revId -> v + } + return new Rsv(r, s, v); + } +} diff --git a/crypto/src/main/java/org/tron/common/crypto/cryptohash/Digest.java b/crypto/src/main/java/org/tron/common/crypto/cryptohash/Digest.java index cb1ca99944f..62362d1d62d 100644 --- a/crypto/src/main/java/org/tron/common/crypto/cryptohash/Digest.java +++ b/crypto/src/main/java/org/tron/common/crypto/cryptohash/Digest.java @@ -19,7 +19,7 @@ package org.tron.common.crypto.cryptohash; /** - * Copyright (c) 2007-2010 Projet RNRT SAPHIR + * Copyright (c) 2007-2010 Project RNRT SAPHIR * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, diff --git a/crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java b/crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java index c6aebba385a..b1d349efad3 100644 --- a/crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java +++ b/crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java @@ -41,6 +41,7 @@ import org.tron.common.crypto.SignatureInterface; import org.tron.common.crypto.jce.ECKeyFactory; import org.tron.common.crypto.jce.TronCastleProvider; +import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; /** @@ -247,7 +248,7 @@ public static SM2 fromPrivate(BigInteger privKey) { * @return - */ public static SM2 fromPrivate(byte[] privKeyBytes) { - if (Objects.isNull(privKeyBytes)) { + if (ByteArray.isEmpty(privKeyBytes)) { return null; } return fromPrivate(new BigInteger(1, privKeyBytes)); diff --git a/crypto/src/main/java/org/tron/common/crypto/sm2/SM2Signer.java b/crypto/src/main/java/org/tron/common/crypto/sm2/SM2Signer.java index 01a4fc0aef6..817b909de58 100644 --- a/crypto/src/main/java/org/tron/common/crypto/sm2/SM2Signer.java +++ b/crypto/src/main/java/org/tron/common/crypto/sm2/SM2Signer.java @@ -185,7 +185,7 @@ public boolean verifySignature(byte[] message, BigInteger r, BigInteger s, } /** - * verfify the hash signature + * verify the hash signature */ public boolean verifyHashSignature(byte[] hash, BigInteger r, BigInteger s) { BigInteger n = ecParams.getN(); diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java index 26ea708fbe4..f144656b0ba 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java @@ -356,7 +356,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return new Integer(a.hashCode() + b.hashCode()).hashCode(); + return a.hashCode() + b.hashCode(); } @Override diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java index ba2a1ceb477..162a5b13b30 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java @@ -164,7 +164,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return new Integer(a.hashCode() + b.hashCode()).hashCode(); + return a.hashCode() + b.hashCode(); } Fp2 frobeniusMap(int power) { diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java index 0636cc334f1..fb863bb6b34 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java @@ -246,6 +246,6 @@ public boolean equals(Object o) { @Override public int hashCode() { - return new Integer(a.hashCode() + b.hashCode() + c.hashCode()).hashCode(); + return a.hashCode() + b.hashCode() + c.hashCode(); } } diff --git a/docker/arm64/Dockerfile b/docker/arm64/Dockerfile new file mode 100644 index 00000000000..6435faf7ead --- /dev/null +++ b/docker/arm64/Dockerfile @@ -0,0 +1,33 @@ +FROM arm64v8/eclipse-temurin:17 + +ENV TMP_DIR="/tron-build" +ENV BASE_DIR="/java-tron" + +RUN set -o errexit -o nounset \ + && apt-get update \ + && apt-get -y install git p7zip-full wget libtcmalloc-minimal4 \ + && echo "git clone" \ + && mkdir -p $TMP_DIR \ + && cd $TMP_DIR \ + && git clone https://github.com/tronprotocol/java-tron.git \ + && cd java-tron \ + && git checkout master \ + && ./gradlew clean build -x test -x check --no-daemon \ + && cd build/distributions \ + && 7za x -y java-tron-1.0.0.zip \ + && mv java-tron-1.0.0 $BASE_DIR \ + && rm -rf $TMP_DIR \ + && rm -rf ~/.gradle \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +ENV LD_PRELOAD="/usr/lib/aarch64-linux-gnu/libtcmalloc_minimal.so.4" +ENV TCMALLOC_RELEASE_RATE=10 + +RUN wget -P $BASE_DIR/config https://raw.githubusercontent.com/tronprotocol/tron-deployment/master/main_net_config.conf + +COPY docker-entrypoint.sh $BASE_DIR/bin + +WORKDIR $BASE_DIR + +ENTRYPOINT ["./bin/docker-entrypoint.sh"] \ No newline at end of file diff --git a/framework/build.gradle b/framework/build.gradle index 0f04685f2d8..59d070e066d 100644 --- a/framework/build.gradle +++ b/framework/build.gradle @@ -38,15 +38,11 @@ dependencies { //local libraries implementation fileTree(dir: 'libs', include: '*.jar') // end local libraries - testImplementation group: 'org.hamcrest', name: 'hamcrest-junit', version: '1.0.0.1' - - implementation group: 'com.google.inject', name: 'guice', version: '4.1.0' implementation group: 'io.dropwizard.metrics', name: 'metrics-core', version: '3.1.2' implementation group: 'com.github.davidb', name: 'metrics-influxdb', version: '0.8.2' - implementation group: 'com.carrotsearch', name: 'java-sizeof', version: '0.0.5' // http - implementation 'org.eclipse.jetty:jetty-server:9.4.53.v20231009' - implementation 'org.eclipse.jetty:jetty-servlet:9.4.53.v20231009' + implementation 'org.eclipse.jetty:jetty-server:9.4.57.v20241219' + implementation 'org.eclipse.jetty:jetty-servlet:9.4.57.v20241219' implementation 'com.alibaba:fastjson:1.2.83' // end http @@ -56,14 +52,11 @@ dependencies { // https://mvnrepository.com/artifact/javax.portlet/portlet-api compileOnly group: 'javax.portlet', name: 'portlet-api', version: '3.0.1' - implementation "io.vavr:vavr:0.9.2" implementation (group: 'org.pf4j', name: 'pf4j', version: '3.10.0') { exclude group: "org.slf4j", module: "slf4j-api" } - testImplementation group: 'org.springframework', name: 'spring-test', version: '5.2.0.RELEASE' - testImplementation group: 'org.springframework', name: 'spring-web', version: '5.2.0.RELEASE' - + testImplementation group: 'org.springframework', name: 'spring-test', version: "${springVersion}" implementation group: 'org.zeromq', name: 'jeromq', version: '0.5.3' api project(":chainbase") api project(":protocol") @@ -123,6 +116,17 @@ test { destinationFile = file("$buildDir/jacoco/jacocoTest.exec") classDumpDir = file("$buildDir/jacoco/classpathdumps") } + if (rootProject.archInfo.isArm64) { + exclude { element -> + element.file.name.toLowerCase().contains('leveldb') + } + filter { + excludeTestsMatching '*.*leveldb*' + excludeTestsMatching '*.*Leveldb*' + excludeTestsMatching '*.*LevelDB*' + excludeTestsMatching '*.*LevelDb*' + } + } if (isWindows()) { exclude '**/ShieldedTransferActuatorTest.class' exclude '**/BackupDbUtilTest.class' @@ -134,6 +138,7 @@ test { } maxHeapSize = "1024m" doFirst { + // Restart the JVM after every 100 tests to avoid memory leaks and ensure test isolation forkEvery = 100 jvmArgs "-XX:MetaspaceSize=128m","-XX:MaxMetaspaceSize=256m", "-XX:+UseG1GC" } @@ -158,7 +163,7 @@ def binaryRelease(taskName, jarName, mainClass) { // explicit_dependency dependsOn (project(':actuator').jar, project(':consensus').jar, project(':chainbase').jar, - project(':crypto').jar, project(':common').jar, project(':protocol').jar) + project(':crypto').jar, project(':common').jar, project(':protocol').jar, project(':platform').jar) from { configurations.runtimeClasspath.collect { @@ -204,8 +209,7 @@ def createScript(project, mainClass, name) { } } } - -applicationDistribution.from("../gradle/java-tron.vmoptions") { +applicationDistribution.from(rootProject.archInfo.VMOptions) { into "bin" } //distZip { @@ -219,35 +223,12 @@ startScripts.enabled = false run.enabled = false tasks.distTar.enabled = false -createScript(project, 'org.tron.program.SolidityNode', 'SolidityNode') createScript(project, 'org.tron.program.FullNode', 'FullNode') -createScript(project, 'org.tron.program.KeystoreFactory', 'KeystoreFactory') -createScript(project, 'org.tron.program.DBConvert', 'DBConvert') - def releaseBinary = hasProperty('binaryRelease') ? getProperty('binaryRelease') : 'true' -def skipSolidity = hasProperty('skipSolidity') ? true : false -def skipKeystore = hasProperty('skipKeystore') ? true : false -def skipConvert = hasProperty('skipConvert') ? true : false -def skipAll = hasProperty('skipAll') ? true : false if (releaseBinary == 'true') { artifacts { archives(binaryRelease('buildFullNodeJar', 'FullNode', 'org.tron.program.FullNode')) } - if (!skipAll) { - if (!skipSolidity) { - artifacts { - archives(binaryRelease('buildSolidityNodeJar', 'SolidityNode', 'org.tron.program.SolidityNode'))} - } - if (!skipKeystore) { - artifacts { - archives(binaryRelease('buildKeystoreFactoryJar', 'KeystoreFactory', 'org.tron.program.KeystoreFactory'))} - } - if (!skipConvert) { - artifacts { - archives(binaryRelease('buildDBConvertJar', 'DBConvert', 'org.tron.program.DBConvert'))} - } - } - } task copyToParent(type: Copy) { diff --git a/framework/src/main/java/org/tron/common/application/RpcService.java b/framework/src/main/java/org/tron/common/application/RpcService.java index 2d118806e2c..c398b71ae41 100644 --- a/framework/src/main/java/org/tron/common/application/RpcService.java +++ b/framework/src/main/java/org/tron/common/application/RpcService.java @@ -19,6 +19,7 @@ import io.grpc.netty.NettyServerBuilder; import io.grpc.protobuf.services.ProtoReflectionService; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -34,6 +35,7 @@ public abstract class RpcService extends AbstractService { private Server apiServer; + private ExecutorService executorService; protected String executorName; @Autowired @@ -58,7 +60,24 @@ public void innerStart() throws Exception { @Override public void innerStop() throws Exception { if (this.apiServer != null) { - this.apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS); + this.apiServer.shutdown(); + try { + if (!this.apiServer.awaitTermination(5, TimeUnit.SECONDS)) { + logger.warn("gRPC server did not shutdown gracefully, forcing shutdown"); + this.apiServer.shutdownNow(); + } + } catch (InterruptedException e) { + logger.warn("Interrupted while waiting for gRPC server shutdown", e); + this.apiServer.shutdownNow(); + Thread.currentThread().interrupt(); + } + } + + // Close executor + if (this.executorService != null) { + ExecutorServiceManager.shutdownAndAwaitTermination( + this.executorService, this.executorName); + this.executorService = null; } } @@ -76,9 +95,9 @@ protected NettyServerBuilder initServerBuilder() { NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(this.port); CommonParameter parameter = Args.getInstance(); if (parameter.getRpcThreadNum() > 0) { - serverBuilder = serverBuilder - .executor(ExecutorServiceManager.newFixedThreadPool( - this.executorName, parameter.getRpcThreadNum())); + this.executorService = ExecutorServiceManager.newFixedThreadPool( + this.executorName, parameter.getRpcThreadNum()); + serverBuilder = serverBuilder.executor(this.executorService); } // Set configs from config.conf or default value serverBuilder @@ -88,6 +107,10 @@ protected NettyServerBuilder initServerBuilder() { .maxConnectionAge(parameter.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS) .maxInboundMessageSize(parameter.getMaxMessageSize()) .maxHeaderListSize(parameter.getMaxHeaderListSize()); + if (parameter.getRpcMaxRstStream() > 0 && parameter.getRpcSecondsPerWindow() > 0) { + serverBuilder.maxRstFramesPerWindow( + parameter.getRpcMaxRstStream(), parameter.getRpcSecondsPerWindow()); + } if (parameter.isRpcReflectionServiceEnable()) { serverBuilder.addService(ProtoReflectionService.newInstance()); diff --git a/framework/src/main/java/org/tron/common/application/TronApplicationContext.java b/framework/src/main/java/org/tron/common/application/TronApplicationContext.java index 64edec77c9c..6c934528f4f 100644 --- a/framework/src/main/java/org/tron/common/application/TronApplicationContext.java +++ b/framework/src/main/java/org/tron/common/application/TronApplicationContext.java @@ -13,8 +13,10 @@ public TronApplicationContext(DefaultListableBeanFactory beanFactory) { super(beanFactory); } + //only used for testcase public TronApplicationContext(Class... annotatedClasses) { super(annotatedClasses); + this.registerShutdownHook(); } public TronApplicationContext(String... basePackages) { diff --git a/framework/src/main/java/org/tron/common/backup/BackupManager.java b/framework/src/main/java/org/tron/common/backup/BackupManager.java index 0c4a3e60dfd..a8812a62bb4 100644 --- a/framework/src/main/java/org/tron/common/backup/BackupManager.java +++ b/framework/src/main/java/org/tron/common/backup/BackupManager.java @@ -126,7 +126,7 @@ public void handleEvent(UdpEvent udpEvent) { logger.warn("Receive keep alive message from {} is not my member", sender.getHostString()); return; } - + logger.info("Receive keep alive message from {}", sender); lastKeepAliveTime = System.currentTimeMillis(); KeepAliveMessage keepAliveMessage = (KeepAliveMessage) msg; diff --git a/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java b/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java index 0a7a5ac3a76..7061b2e9d57 100644 --- a/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java +++ b/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java @@ -9,7 +9,6 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; - import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.bouncycastle.util.encoders.Hex; @@ -26,7 +25,6 @@ import org.tron.common.logsfilter.trigger.SolidityTrigger; import org.tron.common.logsfilter.trigger.TransactionLogTrigger; import org.tron.common.logsfilter.trigger.Trigger; -import org.tron.common.utils.JsonUtil; @Slf4j public class EventPluginLoader { @@ -140,7 +138,7 @@ public static boolean matchFilter(ContractTrigger trigger) { private static boolean filterContractAddress(ContractTrigger trigger, List addressList) { addressList = addressList.stream().filter(item -> - org.apache.commons.lang3.StringUtils.isNotEmpty(item)) + org.apache.commons.lang3.StringUtils.isNotEmpty(item)) .collect(Collectors.toList()); if (Objects.isNull(addressList) || addressList.isEmpty()) { return true; @@ -173,7 +171,7 @@ private static boolean filterContractTopicList(ContractTrigger trigger, List(((ContractEventTrigger) trigger).getTopicMap().values()); } else if (trigger != null) { hset = trigger.getLogInfo().getClonedTopics() - .stream().map(Hex::toHexString).collect(Collectors.toSet()); + .stream().map(Hex::toHexString).collect(Collectors.toSet()); } for (String top : topList) { @@ -547,6 +545,10 @@ public boolean isBusy() { return false; } int queueSize = 0; + if (eventListeners == null || eventListeners.isEmpty()) { + // only occurs in mock test. TODO fix test + return false; + } for (IPluginEventListener listener : eventListeners) { try { queueSize += listener.getPendingSize(); diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 8dfb18331ff..8c86f2f66ac 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -30,6 +30,7 @@ import static org.tron.core.config.Parameter.DatabaseConstants.EXCHANGE_COUNT_LIMIT_MAX; import static org.tron.core.config.Parameter.DatabaseConstants.MARKET_COUNT_LIMIT_MAX; 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; @@ -44,6 +45,7 @@ import com.google.common.collect.DiscreteDomain; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Range; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; @@ -59,6 +61,7 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; @@ -161,6 +164,7 @@ import org.tron.core.capsule.TransactionInfoCapsule; import org.tron.core.capsule.TransactionResultCapsule; import org.tron.core.capsule.TransactionRetCapsule; +import org.tron.core.capsule.VotesCapsule; import org.tron.core.capsule.WitnessCapsule; import org.tron.core.capsule.utils.MarketUtils; import org.tron.core.config.args.Args; @@ -170,6 +174,7 @@ import org.tron.core.db.Manager; import org.tron.core.db.TransactionContext; import org.tron.core.db2.core.Chainbase; +import org.tron.core.db2.core.Chainbase.Cursor; import org.tron.core.exception.AccountResourceInsufficientException; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ContractExeException; @@ -177,7 +182,7 @@ import org.tron.core.exception.DupTransactionException; import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.MaintenanceUnavailableException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.exception.PermissionException; import org.tron.core.exception.SignatureFormatException; @@ -188,6 +193,7 @@ 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; @@ -201,6 +207,8 @@ import org.tron.core.store.MarketPairPriceToOrderStore; import org.tron.core.store.MarketPairToPriceStore; import org.tron.core.store.StoreFactory; +import org.tron.core.store.VotesStore; +import org.tron.core.store.WitnessStore; import org.tron.core.utils.TransactionUtil; import org.tron.core.vm.program.Program; import org.tron.core.zen.ShieldedTRC20ParametersBuilder; @@ -764,6 +772,110 @@ public WitnessList getWitnessList() { return builder.build(); } + public WitnessList getPaginatedNowWitnessList(long offset, long limit) throws + MaintenanceUnavailableException { + if (limit <= 0 || offset < 0) { + return null; + } + if (limit > WITNESS_COUNT_LIMIT_MAX) { + limit = WITNESS_COUNT_LIMIT_MAX; + } + + /* + In the maintenance period, the VoteStores will be cleared. + To avoid the race condition of VoteStores deleted but Witness vote counts not updated, + return retry error. + Only apply to requests that rely on the latest block, + which means the normal fullnode requests with HEAD cursor. + */ + boolean isMaintenance = chainBaseManager.getDynamicPropertiesStore().getStateFlag() == 1; + if (isMaintenance && !Args.getInstance().isSolidityNode() && getCursor() == Cursor.HEAD) { + String message = + "Service temporarily unavailable during maintenance period. Please try again later."; + throw new MaintenanceUnavailableException(message); + } + // It contains the final vote count at the end of the last epoch. + List witnessCapsuleList = chainBaseManager.getWitnessStore().getAllWitnesses(); + if (offset >= witnessCapsuleList.size()) { + return null; + } + + VotesStore votesStore = chainBaseManager.getVotesStore(); + // Count the vote changes for each witness in the current epoch, it is maybe negative. + Map countWitness = countVote(votesStore); + + // Iterate through the witness list to apply vote changes and calculate the real-time vote count + witnessCapsuleList.forEach(witnessCapsule -> { + long voteCount = countWitness.getOrDefault(witnessCapsule.getAddress(), 0L); + witnessCapsule.setVoteCount(witnessCapsule.getVoteCount() + voteCount); + }); + + // Use the same sorting logic as in the Maintenance period + WitnessStore.sortWitnesses(witnessCapsuleList, + chainBaseManager.getDynamicPropertiesStore().allowWitnessSortOptimization()); + + List sortedWitnessList = witnessCapsuleList.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + + WitnessList.Builder builder = WitnessList.newBuilder(); + sortedWitnessList.forEach(witnessCapsule -> + builder.addWitnesses(witnessCapsule.getInstance())); + + return builder.build(); + } + + /** + * Counts vote changes for witnesses in the current epoch. + * + * Vote count changes are tracked as follows: + * - Negative values for votes removed from previous witness in the last epoch + * - Positive values for votes added to new witness in the current epoch + * + * Example: + * an Account X had 100 votes for witness W1 in the previous epoch. + * In the current epoch, X changes votes to: + * - W2: 60 votes + * - W3: 80 votes + * + * Resulting vote changes: + * - W1: -100 (votes removed) + * - W2: +60 (new votes) + * - W3: +80 (new votes) + */ + private Map countVote(VotesStore votesStore) { + // Initialize a result map to store vote changes for each witness + Map countWitness = Maps.newHashMap(); + + // VotesStore is a key-value store, where the key is the address of the voter + Iterator> dbIterator = votesStore.iterator(); + + while (dbIterator.hasNext()) { + Entry next = dbIterator.next(); + VotesCapsule votes = next.getValue(); + + /** + * VotesCapsule contains two lists: + * - Old votes: Last votes from the previous epoch, updated in maintenance period + * - New votes: Latest votes in current epoch, updated after each vote transaction + */ + votes.getOldVotes().forEach(vote -> { + ByteString voteAddress = vote.getVoteAddress(); + long voteCount = vote.getVoteCount(); + countWitness.put(voteAddress, + countWitness.getOrDefault(voteAddress, 0L) - voteCount); + }); + votes.getNewVotes().forEach(vote -> { + ByteString voteAddress = vote.getVoteAddress(); + long voteCount = vote.getVoteCount(); + countWitness.put(voteAddress, + countWitness.getOrDefault(voteAddress, 0L) + voteCount); + }); + } + return countWitness; + } + public ProposalList getProposalList() { ProposalList.Builder builder = ProposalList.newBuilder(); List proposalCapsuleList = @@ -1387,6 +1499,16 @@ public Protocol.ChainParameters getChainParameters() { .setValue(dbManager.getDynamicPropertiesStore().getAllowTvmBlob()) .build()); + builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder() + .setKey("getAllowTvmSelfdestructRestriction") + .setValue(dbManager.getDynamicPropertiesStore().getAllowTvmSelfdestructRestriction()) + .build()); + + builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder() + .setKey("getProposalExpireTime") + .setValue(dbManager.getDynamicPropertiesStore().getProposalExpireTime()) + .build()); + return builder.build(); } @@ -1806,12 +1928,8 @@ public Exchange getExchangeById(ByteString exchangeId) { return null; } - private boolean getFullNodeAllowShieldedTransaction() { - return Args.getInstance().isFullNodeAllowShieldedTransactionArgs(); - } - - private void checkFullNodeAllowShieldedTransaction() throws ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { + private void checkAllowShieldedTransactionApi() throws ZksnarkException { + if (!Args.getInstance().isAllowShieldedTransactionApi()) { throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); } } @@ -1830,10 +1948,7 @@ public BytesMessage getNullifier(ByteString id) { } private long getBlockNumber(OutputPoint outPoint) - throws BadItemException, ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { - throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); - } + throws BadItemException { ByteString txId = outPoint.getHash(); long blockNum = chainBaseManager.getTransactionStore().getBlockNumber(txId.toByteArray()); @@ -1848,9 +1963,6 @@ private long getBlockNumber(OutputPoint outPoint) private IncrementalMerkleVoucherContainer createWitness(OutputPoint outPoint, Long blockNumber) throws ItemNotFoundException, BadItemException, InvalidProtocolBufferException, ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { - throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); - } ByteString txId = outPoint.getHash(); //Get the tree in blockNum-1 position @@ -1946,9 +2058,6 @@ private IncrementalMerkleVoucherContainer createWitness(OutputPoint outPoint, Lo private void updateWitnesses(List witnessList, long large, int synBlockNum) throws ItemNotFoundException, BadItemException, InvalidProtocolBufferException, ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { - throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); - } long start = large; long end = large + synBlockNum - 1; @@ -2022,10 +2131,7 @@ private void updateLowWitness(IncrementalMerkleVoucherContainer witness, long bl } } - private void validateInput(OutputPointInfo request) throws BadItemException, ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { - throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); - } + private void validateInput(OutputPointInfo request) throws BadItemException { if (request.getBlockNum() < 0 || request.getBlockNum() > 1000) { throw new BadItemException("request.BlockNum must be specified with range in [0, 1000]"); } @@ -2051,7 +2157,7 @@ private void validateInput(OutputPointInfo request) throws BadItemException, Zks public IncrementalMerkleVoucherInfo getMerkleTreeVoucherInfo(OutputPointInfo request) throws ItemNotFoundException, BadItemException, InvalidProtocolBufferException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); validateInput(request); IncrementalMerkleVoucherInfo.Builder result = IncrementalMerkleVoucherInfo.newBuilder(); @@ -2097,9 +2203,7 @@ public IncrementalMerkleVoucherInfo getMerkleTreeVoucherInfo(OutputPointInfo req } public IncrementalMerkleTree getMerkleTreeOfBlock(long blockNum) throws ZksnarkException { - if (!getFullNodeAllowShieldedTransaction()) { - throw new ZksnarkException(SHIELDED_ID_NOT_ALLOWED); - } + checkAllowShieldedTransactionApi(); if (blockNum < 0) { return null; } @@ -2166,7 +2270,7 @@ public ReceiveNote createReceiveNoteRandom(long value) throws ZksnarkException, public TransactionCapsule createShieldedTransaction(PrivateParameters request) throws ContractValidateException, RuntimeException, ZksnarkException, BadItemException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ZenTransactionBuilder builder = new ZenTransactionBuilder(this); @@ -2268,7 +2372,7 @@ public TransactionCapsule createShieldedTransaction(PrivateParameters request) public TransactionCapsule createShieldedTransactionWithoutSpendAuthSig( PrivateParametersWithoutAsk request) throws ContractValidateException, ZksnarkException, BadItemException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ZenTransactionBuilder builder = new ZenTransactionBuilder(this); @@ -2385,7 +2489,7 @@ private void shieldedOutput(List shieldedReceives, public ShieldedAddressInfo getNewShieldedAddress() throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ShieldedAddressInfo.Builder addressInfo = ShieldedAddressInfo.newBuilder(); @@ -2418,7 +2522,7 @@ public ShieldedAddressInfo getNewShieldedAddress() throws BadItemException, Zksn } public BytesMessage getSpendingKey() throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] sk = SpendingKey.random().getValue(); return BytesMessage.newBuilder().setValue(ByteString.copyFrom(sk)).build(); @@ -2426,7 +2530,7 @@ public BytesMessage getSpendingKey() throws ZksnarkException { public ExpandedSpendingKeyMessage getExpandedSpendingKey(ByteString spendingKey) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); if (Objects.isNull(spendingKey)) { throw new BadItemException("spendingKey is null"); @@ -2452,7 +2556,7 @@ public ExpandedSpendingKeyMessage getExpandedSpendingKey(ByteString spendingKey) public BytesMessage getAkFromAsk(ByteString ask) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); if (Objects.isNull(ask)) { throw new BadItemException("ask is null"); @@ -2468,7 +2572,7 @@ public BytesMessage getAkFromAsk(ByteString ask) throws public BytesMessage getNkFromNsk(ByteString nsk) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); if (Objects.isNull(nsk)) { throw new BadItemException("nsk is null"); @@ -2484,7 +2588,7 @@ public BytesMessage getNkFromNsk(ByteString nsk) throws public IncomingViewingKeyMessage getIncomingViewingKey(byte[] ak, byte[] nk) throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] ivk = new byte[32]; // the incoming viewing key JLibrustzcash.librustzcashCrhIvk(new CrhIvkParams(ak, nk, ivk)); @@ -2495,7 +2599,7 @@ public IncomingViewingKeyMessage getIncomingViewingKey(byte[] ak, byte[] nk) } public DiversifierMessage getDiversifier() throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] d; while (true) { @@ -2511,7 +2615,7 @@ public DiversifierMessage getDiversifier() throws ZksnarkException { } public BytesMessage getRcm() throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] rcm = Note.generateR(); return BytesMessage.newBuilder().setValue(ByteString.copyFrom(rcm)).build(); @@ -2519,7 +2623,7 @@ public BytesMessage getRcm() throws ZksnarkException { public PaymentAddressMessage getPaymentAddress(IncomingViewingKey ivk, DiversifierT d) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); if (!JLibrustzcash.librustzcashCheckDiversifier(d.getData())) { throw new BadItemException("d is not valid"); @@ -2543,7 +2647,7 @@ public PaymentAddressMessage getPaymentAddress(IncomingViewingKey ivk, public SpendResult isSpend(NoteParameters noteParameters) throws ZksnarkException, InvalidProtocolBufferException, BadItemException, ItemNotFoundException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); GrpcAPI.Note note = noteParameters.getNote(); byte[] ak = noteParameters.getAk().toByteArray(); @@ -2599,7 +2703,7 @@ public SpendResult isSpend(NoteParameters noteParameters) throws public BytesMessage createSpendAuthSig(SpendAuthSigParameters spendAuthSigParameters) throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] result = new byte[64]; SpendSigParams spendSigParams = new SpendSigParams( @@ -2613,7 +2717,7 @@ public BytesMessage createSpendAuthSig(SpendAuthSigParameters spendAuthSigParame } public BytesMessage createShieldNullifier(NfParameters nfParameters) throws ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); byte[] ak = nfParameters.getAk().toByteArray(); byte[] nk = nfParameters.getNk().toByteArray(); @@ -2649,7 +2753,7 @@ public BytesMessage createShieldNullifier(NfParameters nfParameters) throws Zksn public BytesMessage getShieldTransactionHash(Transaction transaction) throws ContractValidateException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); List contract = transaction.getRawData().getContractList(); if (contract == null || contract.isEmpty()) { @@ -2671,37 +2775,7 @@ public BytesMessage getShieldTransactionHash(Transaction transaction) } public TransactionInfoList getTransactionInfoByBlockNum(long blockNum) { - TransactionInfoList.Builder transactionInfoList = TransactionInfoList.newBuilder(); - - try { - TransactionRetCapsule result = dbManager.getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(blockNum)); - - if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { - result.getInstance().getTransactioninfoList().forEach( - transactionInfo -> transactionInfoList.addTransactionInfo(transactionInfo) - ); - } else { - Block block = chainBaseManager.getBlockByNum(blockNum).getInstance(); - - if (block != null) { - List listTransaction = block.getTransactionsList(); - for (Transaction transaction : listTransaction) { - TransactionInfoCapsule transactionInfoCapsule = dbManager.getTransactionHistoryStore() - .get(Sha256Hash.hash(CommonParameter.getInstance() - .isECKeyCryptoEngine(), transaction.getRawData().toByteArray())); - - if (transactionInfoCapsule != null) { - transactionInfoList.addTransactionInfo(transactionInfoCapsule.getInstance()); - } - } - } - } - } catch (BadItemException | ItemNotFoundException e) { - logger.warn(e.getMessage()); - } - - return transactionInfoList.build(); + return dbManager.getTransactionInfoByBlockNum(blockNum); } public NodeList listNodes() { @@ -3354,7 +3428,7 @@ private GrpcAPI.DecryptNotes queryNoteByIvk(long startNum, long endNum, byte[] i */ public GrpcAPI.DecryptNotes scanNoteByIvk(long startNum, long endNum, byte[] ivk) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); return queryNoteByIvk(startNum, endNum, ivk); } @@ -3365,7 +3439,7 @@ public GrpcAPI.DecryptNotes scanNoteByIvk(long startNum, long endNum, public GrpcAPI.DecryptNotesMarked scanAndMarkNoteByIvk(long startNum, long endNum, byte[] ivk, byte[] ak, byte[] nk) throws BadItemException, ZksnarkException, InvalidProtocolBufferException, ItemNotFoundException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); GrpcAPI.DecryptNotes srcNotes = queryNoteByIvk(startNum, endNum, ivk); GrpcAPI.DecryptNotesMarked.Builder builder = GrpcAPI.DecryptNotesMarked.newBuilder(); @@ -3398,7 +3472,7 @@ public GrpcAPI.DecryptNotesMarked scanAndMarkNoteByIvk(long startNum, long endNu */ public GrpcAPI.DecryptNotes scanNoteByOvk(long startNum, long endNum, byte[] ovk) throws BadItemException, ZksnarkException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); GrpcAPI.DecryptNotes.Builder builder = GrpcAPI.DecryptNotes.newBuilder(); if (!(startNum >= 0 && endNum > startNum && endNum - startNum <= 1000)) { @@ -3538,7 +3612,7 @@ private void buildShieldedTRC20Output(ShieldedTRC20ParametersBuilder builder, public ShieldedTRC20Parameters createShieldedContractParameters( PrivateShieldedTRC20Parameters request) throws ContractValidateException, ZksnarkException, ContractExeException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder(); @@ -3675,7 +3749,7 @@ private void buildShieldedTRC20InputWithAK( public ShieldedTRC20Parameters createShieldedContractParametersWithoutAsk( PrivateShieldedTRC20ParametersWithoutAsk request) throws ZksnarkException, ContractValidateException, ContractExeException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder(); byte[] shieldedTRC20ContractAddress = request.getShieldedTRC20ContractAddress().toByteArray(); @@ -3971,7 +4045,7 @@ public DecryptNotesTRC20 scanShieldedTRC20NotesByIvk( long startNum, long endNum, byte[] shieldedTRC20ContractAddress, byte[] ivk, byte[] ak, byte[] nk, ProtocolStringList topicsList) throws BadItemException, ZksnarkException, ContractExeException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); return queryTRC20NoteByIvk(startNum, endNum, shieldedTRC20ContractAddress, ivk, ak, nk, topicsList); @@ -4050,7 +4124,7 @@ private Optional getNoteTxFromLogListByOvk( public DecryptNotesTRC20 scanShieldedTRC20NotesByOvk(long startNum, long endNum, byte[] ovk, byte[] shieldedTRC20ContractAddress, ProtocolStringList topicsList) throws ZksnarkException, BadItemException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); DecryptNotesTRC20.Builder builder = DecryptNotesTRC20.newBuilder(); if (!(startNum >= 0 && endNum > startNum && endNum - startNum <= 1000)) { @@ -4113,7 +4187,7 @@ private byte[] getShieldedTRC20Nullifier(GrpcAPI.Note note, long pos, byte[] ak, public GrpcAPI.NullifierResult isShieldedTRC20ContractNoteSpent(NfTRC20Parameters request) throws ZksnarkException, ContractExeException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); return GrpcAPI.NullifierResult.newBuilder() .setIsSpent(isShieldedTRC20NoteSpent(request.getNote(), @@ -4236,7 +4310,7 @@ public byte[] getShieldedContractScalingFactor(byte[] contractAddress) public BytesMessage getTriggerInputForShieldedTRC20Contract( ShieldedTRC20TriggerContractParameters request) throws ZksnarkException, ContractValidateException { - checkFullNodeAllowShieldedTransaction(); + checkAllowShieldedTransactionApi(); ShieldedTRC20Parameters shieldedTRC20Parameters = request.getShieldedTRC20Parameters(); List spendAuthoritySignature = request.getSpendAuthoritySignatureList(); diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 3162360bbb9..46695986c1f 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -1,13 +1,18 @@ package org.tron.core.config.args; import static java.lang.System.exit; +import static org.fusesource.jansi.Ansi.ansi; import static org.tron.common.math.Maths.max; import static org.tron.common.math.Maths.min; import static org.tron.core.Constant.ADD_PRE_FIX_BYTE_MAINNET; +import static org.tron.core.Constant.DEFAULT_PROPOSAL_EXPIRE_TIME; import static org.tron.core.Constant.DYNAMIC_ENERGY_INCREASE_FACTOR_RANGE; import static org.tron.core.Constant.DYNAMIC_ENERGY_MAX_FACTOR_RANGE; +import static org.tron.core.Constant.MAX_PROPOSAL_EXPIRE_TIME; +import static org.tron.core.Constant.MIN_PROPOSAL_EXPIRE_TIME; import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCE_TIMEOUT_PERCENT; import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM; +import static org.tron.core.exception.TronError.ErrCode.PARAMETER_INIT; import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterDescription; @@ -42,14 +47,15 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.fusesource.jansi.AnsiConsole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.tron.common.arch.Arch; import org.tron.common.args.Account; import org.tron.common.args.GenesisBlock; import org.tron.common.args.Witness; import org.tron.common.config.DbBackupConfig; import org.tron.common.cron.CronExpression; -import org.tron.common.crypto.SignInterface; import org.tron.common.logsfilter.EventPluginConfig; import org.tron.common.logsfilter.FilterQuery; import org.tron.common.logsfilter.TriggerConfig; @@ -58,7 +64,6 @@ import org.tron.common.parameter.CommonParameter; import org.tron.common.parameter.RateLimiterInitialization; import org.tron.common.setting.RocksDbSettings; -import org.tron.common.utils.ByteArray; import org.tron.common.utils.Commons; import org.tron.common.utils.LocalWitnesses; import org.tron.core.Constant; @@ -66,11 +71,8 @@ import org.tron.core.config.Configuration; import org.tron.core.config.Parameter.NetConstants; import org.tron.core.config.Parameter.NodeConstant; -import org.tron.core.exception.CipherException; import org.tron.core.exception.TronError; import org.tron.core.store.AccountStore; -import org.tron.keystore.Credentials; -import org.tron.keystore.WalletUtils; import org.tron.p2p.P2pConfig; import org.tron.p2p.dns.update.DnsType; import org.tron.p2p.dns.update.PublishConfig; @@ -169,6 +171,7 @@ public static void clearParam() { PARAMETER.tcpNettyWorkThreadNum = 0; PARAMETER.udpNettyWorkThreadNum = 0; PARAMETER.solidityNode = false; + PARAMETER.keystoreFactory = false; PARAMETER.trustNodeAddr = ""; PARAMETER.walletExtensionApi = false; PARAMETER.estimateEnergy = false; @@ -186,7 +189,7 @@ public static void clearParam() { PARAMETER.maxHttpConnectNumber = 50; PARAMETER.allowMultiSign = 0; PARAMETER.trxExpirationTimeInMilliseconds = 0; - PARAMETER.fullNodeAllowShieldedTransactionArgs = true; + PARAMETER.allowShieldedTransactionApi = true; PARAMETER.zenTokenId = "000000"; PARAMETER.allowProtoFilterNum = 0; PARAMETER.allowAccountStateRoot = 0; @@ -204,6 +207,7 @@ public static void clearParam() { PARAMETER.jsonRpcHttpPBFTNodeEnable = false; PARAMETER.jsonRpcMaxBlockRange = 5000; PARAMETER.jsonRpcMaxSubTopics = 1000; + PARAMETER.jsonRpcMaxBlockFilterNum = 50000; PARAMETER.nodeMetricsEnable = false; PARAMETER.metricsStorageEnable = false; PARAMETER.metricsPrometheusEnable = false; @@ -235,6 +239,9 @@ public static void clearParam() { PARAMETER.rateLimiterGlobalQps = 50000; PARAMETER.rateLimiterGlobalIpQps = 10000; PARAMETER.rateLimiterGlobalApiQps = 1000; + PARAMETER.rateLimiterSyncBlockChain = 3.0; + PARAMETER.rateLimiterFetchInvData = 3.0; + PARAMETER.rateLimiterDisconnect = 1.0; PARAMETER.p2pDisable = false; PARAMETER.dynamicConfigEnable = false; PARAMETER.dynamicConfigCheckInterval = 600; @@ -247,6 +254,8 @@ public static void clearParam() { PARAMETER.consensusLogicOptimization = 0; PARAMETER.allowTvmCancun = 0; PARAMETER.allowTvmBlob = 0; + PARAMETER.rpcMaxRstStream = 0; + PARAMETER.rpcSecondsPerWindow = 0; } /** @@ -342,7 +351,7 @@ private static String getCommitIdAbbrev() { private static Map getOptionGroup() { String[] tronOption = new String[] {"version", "help", "shellConfFileName", "logbackPath", - "eventSubscribe"}; + "eventSubscribe", "solidityNode", "keystoreFactory"}; String[] dbOption = new String[] {"outputDirectory"}; String[] witnessOption = new String[] {"witness", "privateKey"}; String[] vmOption = new String[] {"debug"}; @@ -369,12 +378,30 @@ private static Map getOptionGroup() { * set parameters. */ public static void setParam(final String[] args, final String confFileName) { + try { + Arch.throwIfUnsupportedJavaVersion(); + } catch (UnsupportedOperationException e) { + AnsiConsole.systemInstall(); + // To avoid confusion caused by silent execution when using -h or -v flags, + // errors are explicitly logged to the console in this context. + // Console output is not required for errors in other scenarios. + System.out.println(ansi().fgRed().a(e.getMessage()).reset()); + AnsiConsole.systemUninstall(); + throw new TronError(e, TronError.ErrCode.JDK_VERSION); + } JCommander.newBuilder().addObject(PARAMETER).build().parse(args); if (PARAMETER.version) { printVersion(); exit(0); } + if (PARAMETER.isHelp()) { + JCommander jCommander = JCommander.newBuilder().addObject(Args.PARAMETER).build(); + jCommander.parse(args); + Args.printHelp(jCommander); + exit(0); + } + Config config = Configuration.getByFileName(PARAMETER.shellConfFileName, confFileName); setParam(config); } @@ -396,61 +423,7 @@ public static void setParam(final Config config) { PARAMETER.cryptoEngine = config.hasPath(Constant.CRYPTO_ENGINE) ? config .getString(Constant.CRYPTO_ENGINE) : Constant.ECKey_ENGINE; - if (StringUtils.isNoneBlank(PARAMETER.privateKey)) { - localWitnesses = (new LocalWitnesses(PARAMETER.privateKey)); - if (StringUtils.isNoneBlank(PARAMETER.witnessAddress)) { - byte[] bytes = Commons.decodeFromBase58Check(PARAMETER.witnessAddress); - if (bytes != null) { - localWitnesses.setWitnessAccountAddress(bytes); - logger.debug("Got localWitnessAccountAddress from cmd"); - } else { - PARAMETER.witnessAddress = ""; - logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT); - } - } - localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine()); - logger.debug("Got privateKey from cmd"); - } else if (config.hasPath(Constant.LOCAL_WITNESS)) { - localWitnesses = new LocalWitnesses(); - List localwitness = config.getStringList(Constant.LOCAL_WITNESS); - localWitnesses.setPrivateKeys(localwitness); - witnessAddressCheck(config); - localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine()); - logger.debug("Got privateKey from config.conf"); - } else if (config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)) { - localWitnesses = new LocalWitnesses(); - List privateKeys = new ArrayList(); - if (PARAMETER.isWitness()) { - List localwitness = config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE); - if (localwitness.size() > 0) { - String fileName = System.getProperty("user.dir") + "/" + localwitness.get(0); - String password; - if (StringUtils.isEmpty(PARAMETER.password)) { - System.out.println("Please input your password."); - password = WalletUtils.inputPassword(); - } else { - password = PARAMETER.password; - PARAMETER.password = null; - } - - try { - Credentials credentials = WalletUtils - .loadCredentials(password, new File(fileName)); - SignInterface sign = credentials.getSignInterface(); - String prikey = ByteArray.toHexString(sign.getPrivateKey()); - privateKeys.add(prikey); - } catch (IOException | CipherException e) { - logger.error("Witness node start failed!"); - throw new TronError(e, TronError.ErrCode.WITNESS_KEYSTORE_LOAD); - } - } - } - localWitnesses.setPrivateKeys(privateKeys); - witnessAddressCheck(config); - localWitnesses.initWitnessAccountAddress(PARAMETER.isECKeyCryptoEngine()); - logger.debug("Got privateKey from keystore"); - } - + localWitnesses = new WitnessInitializer(config).initLocalWitnesses(); if (PARAMETER.isWitness() && CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) { throw new TronError("This is a witness node, but localWitnesses is null", @@ -519,6 +492,11 @@ public static void setParam(final Config config) { config.getInt(Constant.NODE_JSONRPC_MAX_SUB_TOPICS); } + if (config.hasPath(Constant.NODE_JSONRPC_MAX_BLOCK_FILTER_NUM)) { + PARAMETER.jsonRpcMaxBlockFilterNum = + config.getInt(Constant.NODE_JSONRPC_MAX_BLOCK_FILTER_NUM); + } + if (config.hasPath(Constant.VM_MIN_TIME_RATIO)) { PARAMETER.minTimeRatio = config.getDouble(Constant.VM_MIN_TIME_RATIO); } @@ -764,6 +742,12 @@ public static void setParam(final Config config) { PARAMETER.flowControlWindow = config.hasPath(Constant.NODE_RPC_FLOW_CONTROL_WINDOW) ? config.getInt(Constant.NODE_RPC_FLOW_CONTROL_WINDOW) : NettyServerBuilder.DEFAULT_FLOW_CONTROL_WINDOW; + if (config.hasPath(Constant.NODE_RPC_MAX_RST_STREAM)) { + PARAMETER.rpcMaxRstStream = config.getInt(Constant.NODE_RPC_MAX_RST_STREAM); + } + if (config.hasPath(Constant.NODE_RPC_SECONDS_PER_WINDOW)) { + PARAMETER.rpcSecondsPerWindow = config.getInt(Constant.NODE_RPC_SECONDS_PER_WINDOW); + } PARAMETER.maxConnectionIdleInMillis = config.hasPath(Constant.NODE_RPC_MAX_CONNECTION_IDLE_IN_MILLIS) @@ -808,9 +792,7 @@ public static void setParam(final Config config) { config.hasPath(Constant.BLOCK_MAINTENANCE_TIME_INTERVAL) ? config .getInt(Constant.BLOCK_MAINTENANCE_TIME_INTERVAL) : 21600000L; - PARAMETER.proposalExpireTime = - config.hasPath(Constant.BLOCK_PROPOSAL_EXPIRE_TIME) ? config - .getInt(Constant.BLOCK_PROPOSAL_EXPIRE_TIME) : 259200000L; + PARAMETER.proposalExpireTime = getProposalExpirationTime(config); PARAMETER.checkFrozenTime = config.hasPath(Constant.BLOCK_CHECK_FROZEN_TIME) ? config @@ -984,9 +966,18 @@ public static void setParam(final Config config) { PARAMETER.eventFilter = config.hasPath(Constant.EVENT_SUBSCRIBE_FILTER) ? getEventFilter(config) : null; - PARAMETER.fullNodeAllowShieldedTransactionArgs = - !config.hasPath(Constant.NODE_FULLNODE_ALLOW_SHIELDED_TRANSACTION) - || config.getBoolean(Constant.NODE_FULLNODE_ALLOW_SHIELDED_TRANSACTION); + if (config.hasPath(Constant.ALLOW_SHIELDED_TRANSACTION_API)) { + PARAMETER.allowShieldedTransactionApi = + config.getBoolean(Constant.ALLOW_SHIELDED_TRANSACTION_API); + } else if (config.hasPath(Constant.NODE_FULLNODE_ALLOW_SHIELDED_TRANSACTION)) { + // for compatibility with previous configuration + PARAMETER.allowShieldedTransactionApi = + config.getBoolean(Constant.NODE_FULLNODE_ALLOW_SHIELDED_TRANSACTION); + logger.warn("Configuring [node.fullNodeAllowShieldedTransaction] will be deprecated. " + + "Please use [node.allowShieldedTransactionApi] instead."); + } else { + PARAMETER.allowShieldedTransactionApi = true; + } PARAMETER.zenTokenId = config.hasPath(Constant.NODE_ZEN_TOKENID) ? config.getString(Constant.NODE_ZEN_TOKENID) : "000000"; @@ -1023,10 +1014,6 @@ public static void setParam(final Config config) { config.hasPath(Constant.NODE_SHIELDED_TRANS_IN_PENDING_MAX_COUNTS) ? config .getInt(Constant.NODE_SHIELDED_TRANS_IN_PENDING_MAX_COUNTS) : 10; - if (PARAMETER.isWitness()) { - PARAMETER.fullNodeAllowShieldedTransactionArgs = true; - } - PARAMETER.rateLimiterGlobalQps = config.hasPath(Constant.RATE_LIMITER_GLOBAL_QPS) ? config .getInt(Constant.RATE_LIMITER_GLOBAL_QPS) : 50000; @@ -1041,6 +1028,18 @@ public static void setParam(final Config config) { PARAMETER.rateLimiterInitialization = getRateLimiterFromConfig(config); + PARAMETER.rateLimiterSyncBlockChain = + config.hasPath(Constant.RATE_LIMITER_P2P_SYNC_BLOCK_CHAIN) ? config + .getDouble(Constant.RATE_LIMITER_P2P_SYNC_BLOCK_CHAIN) : 3.0; + + PARAMETER.rateLimiterFetchInvData = + config.hasPath(Constant.RATE_LIMITER_P2P_FETCH_INV_DATA) ? config + .getDouble(Constant.RATE_LIMITER_P2P_FETCH_INV_DATA) : 3.0; + + PARAMETER.rateLimiterDisconnect = + config.hasPath(Constant.RATE_LIMITER_P2P_DISCONNECT) ? config + .getDouble(Constant.RATE_LIMITER_P2P_DISCONNECT) : 1.0; + PARAMETER.changedDelegation = config.hasPath(Constant.COMMITTEE_CHANGED_DELEGATION) ? config .getInt(Constant.COMMITTEE_CHANGED_DELEGATION) : 0; @@ -1293,6 +1292,25 @@ public static void setParam(final Config config) { logConfig(); } + private static long getProposalExpirationTime(final Config config) { + if (config.hasPath(Constant.COMMITTEE_PROPOSAL_EXPIRE_TIME)) { + throw new TronError("It is not allowed to configure committee.proposalExpireTime in " + + "config.conf, please set the value in block.proposalExpireTime.", PARAMETER_INIT); + } + if (config.hasPath(Constant.BLOCK_PROPOSAL_EXPIRE_TIME)) { + long proposalExpireTime = config.getLong(Constant.BLOCK_PROPOSAL_EXPIRE_TIME); + if (proposalExpireTime <= MIN_PROPOSAL_EXPIRE_TIME + || proposalExpireTime >= MAX_PROPOSAL_EXPIRE_TIME) { + throw new TronError("The value[block.proposalExpireTime] is only allowed to " + + "be greater than " + MIN_PROPOSAL_EXPIRE_TIME + " and less than " + + MAX_PROPOSAL_EXPIRE_TIME + "!", PARAMETER_INIT); + } + return proposalExpireTime; + } else { + return DEFAULT_PROPOSAL_EXPIRE_TIME; + } + } + private static List getWitnessesFromConfig(final com.typesafe.config.Config config) { return config.getObjectList(Constant.GENESIS_BLOCK_WITNESSES).stream() .map(Args::createWitness) @@ -1615,7 +1633,7 @@ private static FilterQuery getEventFilter(final com.typesafe.config.Config confi try { fromBlockLong = FilterQuery.parseFromBlockNumber(fromBlock); } catch (Exception e) { - logger.error("{}", e); + logger.error("invalid filter: fromBlockNumber: {}", fromBlock, e); return null; } filter.setFromBlock(fromBlockLong); @@ -1624,7 +1642,7 @@ private static FilterQuery getEventFilter(final com.typesafe.config.Config confi try { toBlockLong = FilterQuery.parseToBlockNumber(toBlock); } catch (Exception e) { - logger.error("{}", e); + logger.error("invalid filter: toBlockNumber: {}", toBlock, e); return null; } filter.setToBlock(toBlockLong); @@ -1677,11 +1695,13 @@ private static void initRocksDbSettings(Config config) { .getLong(prefix + "targetFileSizeBase") : 64; int targetFileSizeMultiplier = config.hasPath(prefix + "targetFileSizeMultiplier") ? config .getInt(prefix + "targetFileSizeMultiplier") : 1; + int maxOpenFiles = config.hasPath(prefix + "maxOpenFiles") + ? config.getInt(prefix + "maxOpenFiles") : 5000; PARAMETER.rocksDBCustomSettings = RocksDbSettings .initCustomSettings(levelNumber, compactThreads, blocksize, maxBytesForLevelBase, maxBytesForLevelMultiplier, level0FileNumCompactionTrigger, - targetFileSizeBase, targetFileSizeMultiplier); + targetFileSizeBase, targetFileSizeMultiplier, maxOpenFiles); RocksDbSettings.loggingSettings(); } @@ -1718,6 +1738,8 @@ private static void initBackupProperty(Config config) { public static void logConfig() { CommonParameter parameter = CommonParameter.getInstance(); logger.info("\n"); + logger.info("************************ System info ************************"); + logger.info("{}", Arch.withAll()); logger.info("************************ Net config ************************"); logger.info("P2P version: {}", parameter.getNodeP2pVersion()); logger.info("LAN IP: {}", parameter.getNodeLanIp()); @@ -1762,23 +1784,6 @@ public static void logConfig() { logger.info("\n"); } - public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShieldedTransaction) { - PARAMETER.fullNodeAllowShieldedTransactionArgs = fullNodeAllowShieldedTransaction; - } - - private static void witnessAddressCheck(Config config) { - if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) { - byte[] bytes = Commons - .decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)); - if (bytes != null) { - localWitnesses.setWitnessAccountAddress(bytes); - logger.debug("Got localWitnessAccountAddress from config.conf"); - } else { - logger.warn(IGNORE_WRONG_WITNESS_ADDRESS_FORMAT); - } - } - } - /** * get output directory. */ diff --git a/framework/src/main/java/org/tron/core/config/args/WitnessInitializer.java b/framework/src/main/java/org/tron/core/config/args/WitnessInitializer.java new file mode 100644 index 00000000000..2ea3a449ef4 --- /dev/null +++ b/framework/src/main/java/org/tron/core/config/args/WitnessInitializer.java @@ -0,0 +1,149 @@ +package org.tron.core.config.args; + +import com.typesafe.config.Config; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.common.crypto.SignInterface; +import org.tron.common.utils.ByteArray; +import org.tron.common.utils.Commons; +import org.tron.common.utils.LocalWitnesses; +import org.tron.core.Constant; +import org.tron.core.exception.CipherException; +import org.tron.core.exception.TronError; +import org.tron.keystore.Credentials; +import org.tron.keystore.WalletUtils; + +@Slf4j +public class WitnessInitializer { + + private final Config config; + + private LocalWitnesses localWitnesses; + + public WitnessInitializer(Config config) { + this.config = config; + this.localWitnesses = new LocalWitnesses(); + } + + public LocalWitnesses initLocalWitnesses() { + if (!Args.PARAMETER.isWitness()) { + return localWitnesses; + } + + if (tryInitFromCommandLine()) { + return localWitnesses; + } + + if (tryInitFromConfig()) { + return localWitnesses; + } + + tryInitFromKeystore(); + + return localWitnesses; + } + + private boolean tryInitFromCommandLine() { + if (StringUtils.isBlank(Args.PARAMETER.privateKey)) { + return false; + } + + byte[] witnessAddress = null; + this.localWitnesses = new LocalWitnesses(Args.PARAMETER.privateKey); + if (StringUtils.isNotEmpty(Args.PARAMETER.witnessAddress)) { + witnessAddress = Commons.decodeFromBase58Check(Args.PARAMETER.witnessAddress); + if (witnessAddress == null) { + throw new TronError("LocalWitnessAccountAddress format from cmd is incorrect", + TronError.ErrCode.WITNESS_INIT); + } + logger.debug("Got localWitnessAccountAddress from cmd"); + } + + this.localWitnesses.initWitnessAccountAddress(witnessAddress, + Args.PARAMETER.isECKeyCryptoEngine()); + logger.debug("Got privateKey from cmd"); + return true; + } + + private boolean tryInitFromConfig() { + if (!config.hasPath(Constant.LOCAL_WITNESS) || config.getStringList(Constant.LOCAL_WITNESS) + .isEmpty()) { + return false; + } + + List localWitness = config.getStringList(Constant.LOCAL_WITNESS); + this.localWitnesses.setPrivateKeys(localWitness); + logger.debug("Got privateKey from config.conf"); + byte[] witnessAddress = getWitnessAddress(); + this.localWitnesses.initWitnessAccountAddress(witnessAddress, + Args.PARAMETER.isECKeyCryptoEngine()); + return true; + } + + private void tryInitFromKeystore() { + if (!config.hasPath(Constant.LOCAL_WITNESS_KEYSTORE) + || config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE).isEmpty()) { + return; + } + + List localWitness = config.getStringList(Constant.LOCAL_WITNESS_KEYSTORE); + if (localWitness.size() > 1) { + logger.warn( + "Multiple keystores detected. Only the first keystore will be used as witness, all " + + "others will be ignored."); + } + + List privateKeys = new ArrayList<>(); + String fileName = System.getProperty("user.dir") + "/" + localWitness.get(0); + String password; + if (StringUtils.isEmpty(Args.PARAMETER.password)) { + System.out.println("Please input your password."); + password = WalletUtils.inputPassword(); + } else { + password = Args.PARAMETER.password; + Args.PARAMETER.password = null; + } + + try { + Credentials credentials = WalletUtils + .loadCredentials(password, new File(fileName)); + SignInterface sign = credentials.getSignInterface(); + String prikey = ByteArray.toHexString(sign.getPrivateKey()); + privateKeys.add(prikey); + } catch (IOException | CipherException e) { + logger.error("Witness node start failed!"); + throw new TronError(e, TronError.ErrCode.WITNESS_KEYSTORE_LOAD); + } + + this.localWitnesses.setPrivateKeys(privateKeys); + byte[] witnessAddress = getWitnessAddress(); + this.localWitnesses.initWitnessAccountAddress(witnessAddress, + Args.PARAMETER.isECKeyCryptoEngine()); + logger.debug("Got privateKey from keystore"); + } + + private byte[] getWitnessAddress() { + if (!config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) { + return null; + } + + if (localWitnesses.getPrivateKeys().size() != 1) { + throw new TronError( + "LocalWitnessAccountAddress can only be set when there is only one private key", + TronError.ErrCode.WITNESS_INIT); + } + byte[] witnessAddress = Commons + .decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)); + if (witnessAddress != null) { + logger.debug("Got localWitnessAccountAddress from config.conf"); + } else { + throw new TronError("LocalWitnessAccountAddress format from config is incorrect", + TronError.ErrCode.WITNESS_INIT); + } + return witnessAddress; + } +} diff --git a/framework/src/main/java/org/tron/core/consensus/ConsensusService.java b/framework/src/main/java/org/tron/core/consensus/ConsensusService.java index ce1f1f1cf08..ef8f30ef498 100644 --- a/framework/src/main/java/org/tron/core/consensus/ConsensusService.java +++ b/framework/src/main/java/org/tron/core/consensus/ConsensusService.java @@ -61,17 +61,18 @@ public void start() { logger.info("Add witness: {}, size: {}", Hex.toHexString(privateKeyAddress), miners.size()); } - } else { + } else if (privateKeys.size() == 1) { byte[] privateKey = fromHexString(Args.getLocalWitnesses().getPrivateKey()); byte[] privateKeyAddress = SignUtils.fromPrivate(privateKey, Args.getInstance().isECKeyCryptoEngine()).getAddress(); - byte[] witnessAddress = Args.getLocalWitnesses().getWitnessAccountAddress( - Args.getInstance().isECKeyCryptoEngine()); + byte[] witnessAddress = Args.getLocalWitnesses().getWitnessAccountAddress(); WitnessCapsule witnessCapsule = witnessStore.get(witnessAddress); if (null == witnessCapsule) { logger.warn("Witness {} is not in witnessStore.", Hex.toHexString(witnessAddress)); } + // In multi-signature mode, the address derived from the private key may differ from + // witnessAddress. Miner miner = param.new Miner(privateKey, ByteString.copyFrom(privateKeyAddress), ByteString.copyFrom(witnessAddress)); miners.add(miner); diff --git a/framework/src/main/java/org/tron/core/consensus/ProposalService.java b/framework/src/main/java/org/tron/core/consensus/ProposalService.java index b25f0d6fa8d..51d53f6a59e 100644 --- a/framework/src/main/java/org/tron/core/consensus/ProposalService.java +++ b/framework/src/main/java/org/tron/core/consensus/ProposalService.java @@ -384,6 +384,14 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule) manager.getDynamicPropertiesStore().saveAllowTvmBlob(entry.getValue()); break; } + case ALLOW_TVM_SELFDESTRUCT_RESTRICTION: { + manager.getDynamicPropertiesStore().saveAllowTvmSelfdestructRestriction(entry.getValue()); + break; + } + case PROPOSAL_EXPIRE_TIME: { + manager.getDynamicPropertiesStore().saveProposalExpireTime(entry.getValue()); + break; + } default: find = false; break; diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 8c14f280077..cd1a61c01fe 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -48,6 +49,7 @@ import org.bouncycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.tron.api.GrpcAPI; import org.tron.api.GrpcAPI.TransactionInfoList; import org.tron.common.args.GenesisBlock; import org.tron.common.bloom.Bloom; @@ -164,6 +166,7 @@ import org.tron.core.store.WitnessScheduleStore; import org.tron.core.store.WitnessStore; import org.tron.core.utils.TransactionRegister; +import org.tron.protos.Protocol; import org.tron.protos.Protocol.AccountType; import org.tron.protos.Protocol.Permission; import org.tron.protos.Protocol.Transaction; @@ -869,16 +872,15 @@ public boolean pushTransaction(final TransactionCapsule trx) TooBigTransactionException, TransactionExpirationException, ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException { - if (isShieldedTransaction(trx.getInstance()) && !Args.getInstance() - .isFullNodeAllowShieldedTransactionArgs()) { - return true; + if (isShieldedTransaction(trx.getInstance()) && !chainBaseManager.getDynamicPropertiesStore() + .supportShieldedTransaction()) { + throw new ContractValidateException("ShieldedTransferContract is not supported."); } if (isExchangeTransaction(trx.getInstance())) { throw new ContractValidateException("ExchangeTransactionContract is rejected"); } - pushTransactionQueue.add(trx); Metrics.gaugeInc(MetricKeys.Gauge.MANAGER_QUEUE, 1, MetricLabels.Gauge.QUEUE_QUEUED); @@ -1198,6 +1200,28 @@ private void switchFork(BlockCapsule newHead) } + private boolean isSameSig(TransactionCapsule tx1, TransactionCapsule tx2) { + if (tx1 == null || tx2 == null) { + return false; + } + + if (tx1.getInstance().getSignatureCount() != tx2.getInstance().getSignatureCount()) { + return false; + } + + boolean flag = true; + for (int i = 0; i < tx1.getInstance().getSignatureCount(); i++) { + ByteString sig1 = tx1.getInstance().getSignature(i); + ByteString sig2 = tx2.getInstance().getSignature(i); + if (!sig1.equals(sig2)) { + flag = false; + break; + } + } + + return flag; + } + public List getVerifyTxs(BlockCapsule block) { if (pendingTransactions.size() == 0) { @@ -1205,7 +1229,7 @@ public List getVerifyTxs(BlockCapsule block) { } List txs = new ArrayList<>(); - Set txIds = new HashSet<>(); + Map txMap = new HashMap<>(); Set multiAddresses = new HashSet<>(); pendingTransactions.forEach(capsule -> { @@ -1214,14 +1238,14 @@ public List getVerifyTxs(BlockCapsule block) { String address = Hex.toHexString(capsule.getOwnerAddress()); multiAddresses.add(address); } else { - txIds.add(txId); + txMap.put(txId, capsule); } }); block.getTransactions().forEach(capsule -> { String address = Hex.toHexString(capsule.getOwnerAddress()); String txId = Hex.toHexString(capsule.getTransactionId().getBytes()); - if (multiAddresses.contains(address) || !txIds.contains(txId)) { + if (multiAddresses.contains(address) || !isSameSig(capsule, txMap.get(txId))) { txs.add(capsule); } else { capsule.setVerified(true); @@ -1887,12 +1911,10 @@ private void processBlock(BlockCapsule block, List txs) chainBaseManager.getBalanceTraceStore().resetCurrentBlockTrace(); - if (CommonParameter.getInstance().isJsonRpcFilterEnabled()) { - Bloom blockBloom = chainBaseManager.getSectionBloomStore() - .initBlockSection(transactionRetCapsule); - chainBaseManager.getSectionBloomStore().write(block.getNum()); - block.setBloom(blockBloom); - } + Bloom blockBloom = chainBaseManager.getSectionBloomStore() + .initBlockSection(transactionRetCapsule); + chainBaseManager.getSectionBloomStore().write(block.getNum()); + block.setBloom(blockBloom); } private void payReward(BlockCapsule block) { @@ -2189,25 +2211,7 @@ private void processTransactionTrigger(BlockCapsule newBlock) { // need to set eth compatible data from transactionInfoList if (EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible() && newBlock.getNum() != 0) { - TransactionInfoList transactionInfoList = TransactionInfoList.newBuilder().build(); - TransactionInfoList.Builder transactionInfoListBuilder = TransactionInfoList.newBuilder(); - - try { - TransactionRetCapsule result = chainBaseManager.getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(newBlock.getNum())); - - if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { - result.getInstance().getTransactioninfoList().forEach( - transactionInfoListBuilder::addTransactionInfo - ); - - transactionInfoList = transactionInfoListBuilder.build(); - } - } catch (BadItemException e) { - logger.error("PostBlockTrigger getTransactionInfoList blockNum = {}, error is {}.", - newBlock.getNum(), e.getMessage()); - } - + TransactionInfoList transactionInfoList = getTransactionInfoByBlockNum(newBlock.getNum()); if (transactionCapsuleList.size() == transactionInfoList.getTransactionInfoCount()) { long cumulativeEnergyUsed = 0; long cumulativeLogCount = 0; @@ -2265,21 +2269,8 @@ private void postLogsFilter(final BlockCapsule blockCapsule, boolean solidified, boolean removed) { if (!blockCapsule.getTransactions().isEmpty()) { long blockNumber = blockCapsule.getNum(); - List transactionInfoList = new ArrayList<>(); - - try { - TransactionRetCapsule result = chainBaseManager.getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(blockNumber)); - - if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { - transactionInfoList.addAll(result.getInstance().getTransactioninfoList()); - } - } catch (BadItemException e) { - logger.error("ProcessLogsFilter getTransactionInfoList blockNum = {}, error is {}.", - blockNumber, e.getMessage()); - return; - } - + List transactionInfoList + = getTransactionInfoByBlockNum(blockNumber).getTransactionInfoList(); LogsFilterCapsule logsFilterCapsule = new LogsFilterCapsule(blockNumber, blockCapsule.getBlockId().toString(), blockCapsule.getBloom(), transactionInfoList, solidified, removed); @@ -2520,6 +2511,40 @@ private boolean isBlockWaitingLock() { return blockWaitLock.get() > NO_BLOCK_WAITING_LOCK; } + public TransactionInfoList getTransactionInfoByBlockNum(long blockNum) { + TransactionInfoList.Builder transactionInfoList = TransactionInfoList.newBuilder(); + + try { + TransactionRetCapsule result = getTransactionRetStore() + .getTransactionInfoByBlockNum(ByteArray.fromLong(blockNum)); + + if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { + result.getInstance().getTransactioninfoList().forEach( + transactionInfo -> transactionInfoList.addTransactionInfo(transactionInfo) + ); + } else { + Protocol.Block block = chainBaseManager.getBlockByNum(blockNum).getInstance(); + + if (block != null) { + List listTransaction = block.getTransactionsList(); + for (Transaction transaction : listTransaction) { + TransactionInfoCapsule transactionInfoCapsule = getTransactionHistoryStore() + .get(Sha256Hash.hash(CommonParameter.getInstance() + .isECKeyCryptoEngine(), transaction.getRawData().toByteArray())); + + if (transactionInfoCapsule != null) { + transactionInfoList.addTransactionInfo(transactionInfoCapsule.getInstance()); + } + } + } + } + } catch (BadItemException | ItemNotFoundException e) { + logger.warn(e.getMessage()); + } + + return transactionInfoList.build(); + } + public void close() { stopRePushThread(); stopRePushTriggerThread(); diff --git a/framework/src/main/java/org/tron/core/metrics/node/NodeMetricManager.java b/framework/src/main/java/org/tron/core/metrics/node/NodeMetricManager.java index a0aba129648..9a5ecb33213 100644 --- a/framework/src/main/java/org/tron/core/metrics/node/NodeMetricManager.java +++ b/framework/src/main/java/org/tron/core/metrics/node/NodeMetricManager.java @@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.backup.BackupManager; -import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.ByteArray; import org.tron.core.ChainBaseManager; import org.tron.core.config.args.Args; import org.tron.program.Version; @@ -36,8 +36,9 @@ private void setNodeInfo(NodeInfo nodeInfo) { nodeInfo.setIp(Args.getInstance().getNodeExternalIp()); - ByteString witnessAddress = ByteString.copyFrom(Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())); + byte[] witnessAccountAddress = Args.getLocalWitnesses().getWitnessAccountAddress(); + ByteString witnessAddress = !ByteArray.isEmpty(witnessAccountAddress) ? ByteString + .copyFrom(witnessAccountAddress) : null; if (chainBaseManager.getWitnessScheduleStore().getActiveWitnesses().contains(witnessAddress)) { nodeInfo.setNodeType(1); } else { diff --git a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java index 795c90b4edd..9cfa5058e8c 100644 --- a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java +++ b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java @@ -178,9 +178,11 @@ private void processMessage(PeerConnection peer, byte[] data) { handshakeService.processHelloMessage(peer, (HelloMessage) msg); break; case P2P_DISCONNECT: - peer.getChannel().close(); - peer.getNodeStatistics() - .nodeDisconnectedRemote(((DisconnectMessage)msg).getReason()); + if (peer.getP2pRateLimiter().tryAcquire(type.asByte())) { + peer.getChannel().close(); + peer.getNodeStatistics() + .nodeDisconnectedRemote(((DisconnectMessage)msg).getReason()); + } break; case SYNC_BLOCK_CHAIN: syncBlockChainMsgHandler.processMessage(peer, msg); @@ -253,12 +255,14 @@ private void processException(PeerConnection peer, TronMessage msg, Exception ex code = Protocol.ReasonCode.BAD_TX; break; case BAD_BLOCK: + case BLOCK_SIGN_ERROR: code = Protocol.ReasonCode.BAD_BLOCK; break; case NO_SUCH_MESSAGE: code = Protocol.ReasonCode.NO_SUCH_MESSAGE; break; case BAD_MESSAGE: + case RATE_LIMIT_EXCEEDED: code = Protocol.ReasonCode.BAD_PROTOCOL; break; case SYNC_FAILED: diff --git a/framework/src/main/java/org/tron/core/net/P2pRateLimiter.java b/framework/src/main/java/org/tron/core/net/P2pRateLimiter.java new file mode 100644 index 00000000000..9b36e1e5df3 --- /dev/null +++ b/framework/src/main/java/org/tron/core/net/P2pRateLimiter.java @@ -0,0 +1,32 @@ +package org.tron.core.net; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.util.concurrent.RateLimiter; + +public class P2pRateLimiter { + private final Cache rateLimiters = CacheBuilder.newBuilder() + .maximumSize(32).build(); + + public void register(Byte type, double rate) { + RateLimiter rateLimiter = RateLimiter.create(Double.POSITIVE_INFINITY); + rateLimiter.setRate(rate); + rateLimiters.put(type, rateLimiter); + } + + public void acquire(Byte type) { + RateLimiter rateLimiter = rateLimiters.getIfPresent(type); + if (rateLimiter == null) { + return; + } + rateLimiter.acquire(); + } + + public boolean tryAcquire(Byte type) { + RateLimiter rateLimiter = rateLimiters.getIfPresent(type); + if (rateLimiter == null) { + return true; + } + return rateLimiter.tryAcquire(); + } +} diff --git a/framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java b/framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java index 867ced5dbff..68123c93db6 100755 --- a/framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java +++ b/framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java @@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils; import org.tron.common.utils.ByteArray; import org.tron.common.utils.DecodeUtil; +import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.StringUtil; import org.tron.core.ChainBaseManager; import org.tron.core.capsule.BlockCapsule; @@ -156,17 +157,17 @@ public Protocol.HelloMessage getInstance() { public boolean valid() { byte[] genesisBlockByte = this.helloMessage.getGenesisBlockId().getHash().toByteArray(); - if (genesisBlockByte.length == 0) { + if (genesisBlockByte.length != Sha256Hash.LENGTH) { return false; } byte[] solidBlockId = this.helloMessage.getSolidBlockId().getHash().toByteArray(); - if (solidBlockId.length == 0) { + if (solidBlockId.length != Sha256Hash.LENGTH) { return false; } byte[] headBlockId = this.helloMessage.getHeadBlockId().getHash().toByteArray(); - if (headBlockId.length == 0) { + if (headBlockId.length != Sha256Hash.LENGTH) { return false; } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java index 5415ea435e3..ecb7853ce6f 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java @@ -38,7 +38,7 @@ public class FetchInvDataMsgHandler implements TronMsgHandler { private volatile Cache epochCache = CacheBuilder.newBuilder().initialCapacity(100) - .maximumSize(1000).expireAfterWrite(1, TimeUnit.HOURS).build(); + .maximumSize(1000).expireAfterWrite(1, TimeUnit.HOURS).build(); private static final int MAX_SIZE = 1_000_000; @Autowired @@ -55,7 +55,9 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep FetchInvDataMessage fetchInvDataMsg = (FetchInvDataMessage) msg; - check(peer, fetchInvDataMsg); + boolean isAdv = isAdvInv(peer, fetchInvDataMsg); + + check(peer, fetchInvDataMsg, isAdv); InventoryType type = fetchInvDataMsg.getInventoryType(); List transactions = Lists.newArrayList(); @@ -64,6 +66,15 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep for (Sha256Hash hash : fetchInvDataMsg.getHashList()) { Item item = new Item(hash, type); + /* Cache the Inventory sent to the peer. + Once a FetchInvData message is received from the peer, remove this Inventory from the cache. + If the same FetchInvData request is received from the peer again and it is + no longer in the cache, then reject the request. + * */ + if (isAdv) { + peer.getAdvInvSpread().invalidate(item); + } + Message message = advService.getMessage(item); if (message == null) { try { @@ -84,7 +95,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep } else { transactions.add(((TransactionMessage) message).getTransactionCapsule().getInstance()); size += ((TransactionMessage) message).getTransactionCapsule().getInstance() - .getSerializedSize(); + .getSerializedSize(); if (size > MAX_SIZE) { peer.sendMessage(new TransactionsMessage(transactions)); transactions = Lists.newArrayList(); @@ -104,16 +115,16 @@ private void sendPbftCommitMessage(PeerConnection peer, BlockCapsule blockCapsul } long epoch = 0; PbftSignCapsule pbftSignCapsule = tronNetDelegate - .getBlockPbftCommitData(blockCapsule.getNum()); + .getBlockPbftCommitData(blockCapsule.getNum()); long maintenanceTimeInterval = consensusDelegate.getDynamicPropertiesStore() - .getMaintenanceTimeInterval(); + .getMaintenanceTimeInterval(); if (pbftSignCapsule != null) { Raw raw = Raw.parseFrom(pbftSignCapsule.getPbftCommitResult().getData()); epoch = raw.getEpoch(); peer.sendMessage(new PbftCommitMessage(pbftSignCapsule)); } else { - epoch = - (blockCapsule.getTimeStamp() / maintenanceTimeInterval + 1) * maintenanceTimeInterval; + epoch = (blockCapsule.getTimeStamp() / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; } if (epochCache.getIfPresent(epoch) == null) { PbftSignCapsule srl = tronNetDelegate.getSRLPbftCommitData(epoch); @@ -127,7 +138,21 @@ private void sendPbftCommitMessage(PeerConnection peer, BlockCapsule blockCapsul } } - private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) throws P2pException { + public boolean isAdvInv(PeerConnection peer, FetchInvDataMessage msg) { + MessageTypes type = msg.getInvMessageType(); + if (type == MessageTypes.TRX) { + return true; + } + for (Sha256Hash hash : msg.getHashList()) { + if (peer.getAdvInvSpread().getIfPresent(new Item(hash, InventoryType.BLOCK)) == null) { + return false; + } + } + return true; + } + + private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg, + boolean isAdv) throws P2pException { MessageTypes type = fetchInvDataMsg.getInvMessageType(); if (type == MessageTypes.TRX) { @@ -144,38 +169,38 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) thr + "maxCount: {}, fetchCount: {}, peer: {}", maxCount, fetchCount, peer.getInetAddress()); } - } else { - boolean isAdv = true; + } + + if (!isAdv) { + if (!peer.isNeedSyncFromUs()) { + throw new P2pException(TypeEnum.BAD_MESSAGE, "no need sync"); + } + if (!peer.getP2pRateLimiter().tryAcquire(fetchInvDataMsg.getType().asByte())) { + throw new P2pException(TypeEnum.RATE_LIMIT_EXCEEDED, fetchInvDataMsg.getType() + + " message exceeds the rate limit"); + } + if (fetchInvDataMsg.getHashList().size() > NetConstants.MAX_BLOCK_FETCH_PER_PEER) { + throw new P2pException(TypeEnum.BAD_MESSAGE, "fetch too many blocks, size:" + + fetchInvDataMsg.getHashList().size()); + } for (Sha256Hash hash : fetchInvDataMsg.getHashList()) { - if (peer.getAdvInvSpread().getIfPresent(new Item(hash, InventoryType.BLOCK)) == null) { - isAdv = false; - break; + long blockNum = new BlockId(hash).getNum(); + long minBlockNum = + peer.getLastSyncBlockId().getNum() - 2 * NetConstants.SYNC_FETCH_BATCH_NUM; + if (blockNum < minBlockNum) { + throw new P2pException(TypeEnum.BAD_MESSAGE, + "minBlockNum: " + minBlockNum + ", blockNum: " + blockNum); } - } - if (!isAdv) { - if (!peer.isNeedSyncFromUs()) { - throw new P2pException(TypeEnum.BAD_MESSAGE, "no need sync"); + if (blockNum > peer.getLastSyncBlockId().getNum()) { + throw new P2pException(TypeEnum.BAD_MESSAGE, + "maxBlockNum: " + peer.getLastSyncBlockId().getNum() + ", blockNum: " + blockNum); } - for (Sha256Hash hash : fetchInvDataMsg.getHashList()) { - long blockNum = new BlockId(hash).getNum(); - long minBlockNum = - peer.getLastSyncBlockId().getNum() - 2 * NetConstants.SYNC_FETCH_BATCH_NUM; - if (blockNum < minBlockNum) { - throw new P2pException(TypeEnum.BAD_MESSAGE, - "minBlockNum: " + minBlockNum + ", blockNum: " + blockNum); - } - if (blockNum > peer.getLastSyncBlockId().getNum()) { - throw new P2pException(TypeEnum.BAD_MESSAGE, - "maxBlockNum: " + peer.getLastSyncBlockId().getNum() + ", blockNum: " + blockNum); - } - if (peer.getSyncBlockIdCache().getIfPresent(hash) != null) { - throw new P2pException(TypeEnum.BAD_MESSAGE, - new BlockId(hash).getString() + " is exist"); - } - peer.getSyncBlockIdCache().put(hash, System.currentTimeMillis()); + if (peer.getSyncBlockIdCache().getIfPresent(hash) != null) { + throw new P2pException(TypeEnum.BAD_MESSAGE, + new BlockId(hash).getString() + " is exist"); } + peer.getSyncBlockIdCache().put(hash, System.currentTimeMillis()); } } } - -} +} \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java index 55446593bd0..71d268b22bc 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java @@ -58,6 +58,14 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep } private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2pException { + if (peer.getRemainNum() > 0 + && !peer.getP2pRateLimiter().tryAcquire(msg.getType().asByte())) { + // Discard messages that exceed the rate limit + logger.warn("{} message from peer {} exceeds the rate limit", + msg.getType(), peer.getInetSocketAddress()); + return false; + } + List blockIds = msg.getBlockIds(); if (CollectionUtils.isEmpty(blockIds)) { throw new P2pException(TypeEnum.BAD_MESSAGE, "SyncBlockChain blockIds is empty"); diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java index 5fab8bc6f33..0436b48d374 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java @@ -70,6 +70,10 @@ public boolean isBusy() { public void processMessage(PeerConnection peer, TronMessage msg) throws P2pException { TransactionsMessage transactionsMessage = (TransactionsMessage) msg; check(peer, transactionsMessage); + for (Transaction trx : transactionsMessage.getTransactions().getTransactionsList()) { + Item item = new Item(new TransactionMessage(trx).getMessageId(), InventoryType.TRX); + peer.getAdvInvRequest().remove(item); + } int smartContractQueueSize = 0; int trxHandlePoolQueueSize = 0; int dropSmartContractCount = 0; @@ -101,7 +105,6 @@ private void check(PeerConnection peer, TransactionsMessage msg) throws P2pExcep throw new P2pException(TypeEnum.BAD_MESSAGE, "trx: " + msg.getMessageId() + " without request."); } - peer.getAdvInvRequest().remove(item); if (trx.getRawData().getContractCount() < 1) { throw new P2pException(TypeEnum.BAD_TRX, "tx " + item.getHash() + " contract size should be greater than 0"); diff --git a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java index 2e08e105bed..253502bc3a1 100644 --- a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java +++ b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java @@ -1,5 +1,9 @@ package org.tron.core.net.peer; +import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA; +import static org.tron.core.net.message.MessageTypes.P2P_DISCONNECT; +import static org.tron.core.net.message.MessageTypes.SYNC_BLOCK_CHAIN; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.protobuf.ByteString; @@ -32,6 +36,7 @@ import org.tron.core.config.args.Args; import org.tron.core.metrics.MetricsKey; import org.tron.core.metrics.MetricsUtil; +import org.tron.core.net.P2pRateLimiter; import org.tron.core.net.TronNetDelegate; import org.tron.core.net.message.adv.InventoryMessage; import org.tron.core.net.message.adv.TransactionsMessage; @@ -85,7 +90,7 @@ public class PeerConnection { @Getter @Setter - private TronState tronState = TronState.INIT; + private volatile TronState tronState = TronState.INIT; @Autowired private TronNetDelegate tronNetDelegate; @@ -123,15 +128,15 @@ public class PeerConnection { private Map advInvRequest = new ConcurrentHashMap<>(); @Setter - private BlockId fastForwardBlock; + private volatile BlockId fastForwardBlock; @Getter - private BlockId blockBothHave = new BlockId(); + private volatile BlockId blockBothHave = new BlockId(); @Getter private volatile long blockBothHaveUpdateTime = System.currentTimeMillis(); @Setter @Getter - private BlockId lastSyncBlockId; + private volatile BlockId lastSyncBlockId; @Setter @Getter private volatile long remainNum; @@ -146,7 +151,7 @@ public class PeerConnection { private Map syncBlockRequested = new ConcurrentHashMap<>(); @Setter @Getter - private Pair, Long> syncChainRequested = null; + private volatile Pair, Long> syncChainRequested = null; @Setter @Getter private Set syncBlockInProcess = new HashSet<>(); @@ -156,6 +161,8 @@ public class PeerConnection { @Setter @Getter private volatile boolean needSyncFromUs = true; + @Getter + private P2pRateLimiter p2pRateLimiter = new P2pRateLimiter(); public void setChannel(Channel channel) { this.channel = channel; @@ -164,6 +171,12 @@ public void setChannel(Channel channel) { } this.nodeStatistics = TronStatsManager.getNodeStatistics(channel.getInetAddress()); lastInteractiveTime = System.currentTimeMillis(); + p2pRateLimiter.register(SYNC_BLOCK_CHAIN.asByte(), + Args.getInstance().getRateLimiterSyncBlockChain()); + p2pRateLimiter.register(FETCH_INV_DATA.asByte(), + Args.getInstance().getRateLimiterFetchInvData()); + p2pRateLimiter.register(P2P_DISCONNECT.asByte(), + Args.getInstance().getRateLimiterDisconnect()); } public void setBlockBothHave(BlockId blockId) { diff --git a/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java b/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java index 6ccbf6427a7..04eac202484 100644 --- a/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java +++ b/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java @@ -42,6 +42,10 @@ public void statusCheck() { long now = System.currentTimeMillis(); + if (tronNetDelegate == null) { + // only occurs in mock test. TODO fix test + return; + } tronNetDelegate.getActivePeer().forEach(peer -> { boolean isDisconnected = false; diff --git a/framework/src/main/java/org/tron/core/net/service/handshake/HandshakeService.java b/framework/src/main/java/org/tron/core/net/service/handshake/HandshakeService.java index 6cd117c83dd..070a9f56406 100644 --- a/framework/src/main/java/org/tron/core/net/service/handshake/HandshakeService.java +++ b/framework/src/main/java/org/tron/core/net/service/handshake/HandshakeService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; import org.tron.common.utils.ByteArray; import org.tron.core.ChainBaseManager; +import org.tron.core.ChainBaseManager.NodeType; import org.tron.core.config.args.Args; import org.tron.core.net.TronNetService; import org.tron.core.net.message.handshake.HelloMessage; @@ -57,7 +58,7 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) { msg.getInstance().getAddress().toByteArray().length, msg.getInstance().getSignature().toByteArray().length, msg.getInstance().getCodeVersion().toByteArray().length); - peer.disconnect(ReasonCode.UNEXPECTED_IDENTITY); + peer.disconnect(ReasonCode.INCOMPATIBLE_PROTOCOL); return; } @@ -96,12 +97,17 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) { } if (chainBaseManager.getSolidBlockId().getNum() >= msg.getSolidBlockId().getNum() - && !chainBaseManager.containBlockInMainChain(msg.getSolidBlockId())) { - logger.info("Peer {} different solid block, peer->{}, me->{}", - peer.getInetSocketAddress(), - msg.getSolidBlockId().getString(), - chainBaseManager.getSolidBlockId().getString()); - peer.disconnect(ReasonCode.FORKED); + && !chainBaseManager.containBlockInMainChain(msg.getSolidBlockId())) { + if (chainBaseManager.getLowestBlockNum() <= msg.getSolidBlockId().getNum()) { + logger.info("Peer {} different solid block, fork with me, peer->{}, me->{}", + peer.getInetSocketAddress(), + msg.getSolidBlockId().getString(), + chainBaseManager.getSolidBlockId().getString()); + peer.disconnect(ReasonCode.FORKED); + } else { + logger.info("Peer {} solid block is below than my lowest", peer.getInetSocketAddress()); + peer.disconnect(ReasonCode.LIGHT_NODE_SYNC_FAIL); + } return; } diff --git a/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java b/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java index 161e918336b..61ae6326e9f 100644 --- a/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java +++ b/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java @@ -66,11 +66,11 @@ public class RelayService { private List fastForwardNodes = parameter.getFastForwardNodes(); - private ByteString witnessAddress = ByteString - .copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress(CommonParameter.getInstance() - .isECKeyCryptoEngine())); + private final int keySize = Args.getLocalWitnesses().getPrivateKeys().size(); - private int keySize = Args.getLocalWitnesses().getPrivateKeys().size(); + private final ByteString witnessAddress = + Args.getLocalWitnesses().getWitnessAccountAddress() != null ? ByteString + .copyFrom(Args.getLocalWitnesses().getWitnessAccountAddress()) : null; private int maxFastForwardNum = Args.getInstance().getMaxFastForwardNum(); diff --git a/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java b/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java index e387329c467..75349bd4c19 100644 --- a/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java +++ b/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java @@ -124,7 +124,11 @@ public void syncNext(PeerConnection peer) { peer.setSyncChainRequested(new Pair<>(chainSummary, System.currentTimeMillis())); peer.sendMessage(new SyncBlockChainMessage(chainSummary)); } catch (Exception e) { - logger.error("Peer {} sync failed, reason: {}", peer.getInetAddress(), e); + if (e instanceof P2pException) { + logger.warn("Peer {} sync failed, reason: {}", peer.getInetAddress(), e.getMessage()); + } else { + logger.error("Peer {} sync failed.", peer.getInetAddress(), e); + } peer.disconnect(ReasonCode.SYNC_FAIL); } } @@ -159,9 +163,8 @@ private void invalid(BlockId blockId, PeerConnection peerConnection) { } private LinkedList getBlockChainSummary(PeerConnection peer) throws P2pException { - - BlockId beginBlockId = peer.getBlockBothHave(); List blockIds = new ArrayList<>(peer.getSyncBlockToFetch()); + BlockId beginBlockId = peer.getBlockBothHave(); List forkList = new LinkedList<>(); LinkedList summary = new LinkedList<>(); long syncBeginNumber = tronNetDelegate.getSyncBeginNumber(); @@ -323,9 +326,9 @@ private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection) for (PeerConnection peer : tronNetDelegate.getActivePeer()) { BlockId bid = peer.getSyncBlockToFetch().peek(); if (blockId.equals(bid)) { + peer.setBlockBothHave(blockId); peer.getSyncBlockToFetch().remove(bid); if (flag) { - peer.setBlockBothHave(blockId); if (peer.getSyncBlockToFetch().isEmpty() && peer.isFetchAble()) { syncNext(peer); } diff --git a/framework/src/main/java/org/tron/core/services/RpcApiService.java b/framework/src/main/java/org/tron/core/services/RpcApiService.java index 8f9c6b15bb7..63e7ba03fc7 100755 --- a/framework/src/main/java/org/tron/core/services/RpcApiService.java +++ b/framework/src/main/java/org/tron/core/services/RpcApiService.java @@ -89,6 +89,7 @@ import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; import org.tron.core.exception.ItemNotFoundException; +import org.tron.core.exception.MaintenanceUnavailableException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.exception.StoreException; import org.tron.core.exception.VMIllegalException; @@ -291,20 +292,6 @@ private StatusRuntimeException getRunTimeException(Exception e) { } } - private void checkSupportShieldedTransaction() throws ZksnarkException { - String msg = "Not support Shielded Transaction, need to be opened by the committee"; - if (!dbManager.getDynamicPropertiesStore().supportShieldedTransaction()) { - throw new ZksnarkException(msg); - } - } - - private void checkSupportShieldedTRC20Transaction() throws ZksnarkException { - String msg = "Not support Shielded TRC20 Transaction, need to be opened by the committee"; - if (!dbManager.getDynamicPropertiesStore().supportShieldedTRC20Transaction()) { - throw new ZksnarkException(msg); - } - } - /** * DatabaseApi. */ @@ -396,6 +383,18 @@ public void listWitnesses(EmptyMessage request, StreamObserver resp responseObserver.onCompleted(); } + @Override + public void getPaginatedNowWitnessList(PaginatedMessage request, + StreamObserver responseObserver) { + try { + responseObserver.onNext( + wallet.getPaginatedNowWitnessList(request.getOffset(), request.getLimit())); + } catch (MaintenanceUnavailableException e) { + responseObserver.onError(getRunTimeException(e)); + } + responseObserver.onCompleted(); + } + @Override public void getAssetIssueList(EmptyMessage request, StreamObserver responseObserver) { @@ -651,8 +650,6 @@ public void getMerkleTreeVoucherInfo(OutputPointInfo request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - IncrementalMerkleVoucherInfo witnessInfo = wallet .getMerkleTreeVoucherInfo(request); responseObserver.onNext(witnessInfo); @@ -669,8 +666,6 @@ public void scanNoteByIvk(GrpcAPI.IvkDecryptParameters request, long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotes decryptNotes = wallet .scanNoteByIvk(startNum, endNum, request.getIvk().toByteArray()); responseObserver.onNext(decryptNotes); @@ -687,8 +682,6 @@ public void scanAndMarkNoteByIvk(GrpcAPI.IvkDecryptAndMarkParameters request, long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotesMarked decryptNotes = wallet.scanAndMarkNoteByIvk(startNum, endNum, request.getIvk().toByteArray(), request.getAk().toByteArray(), @@ -707,8 +700,6 @@ public void scanNoteByOvk(GrpcAPI.OvkDecryptParameters request, long startNum = request.getStartBlockIndex(); long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotes decryptNotes = wallet .scanNoteByOvk(startNum, endNum, request.getOvk().toByteArray()); responseObserver.onNext(decryptNotes); @@ -721,8 +712,6 @@ public void scanNoteByOvk(GrpcAPI.OvkDecryptParameters request, @Override public void isSpend(NoteParameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - responseObserver.onNext(wallet.isSpend(request)); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -742,7 +731,6 @@ public void scanShieldedTRC20NotesByIvk(IvkDecryptTRC20Parameters request, ProtocolStringList topicsList = request.getEventsList(); try { - checkSupportShieldedTRC20Transaction(); responseObserver.onNext( wallet.scanShieldedTRC20NotesByIvk(startNum, endNum, contractAddress, ivk, ak, nk, topicsList)); @@ -762,7 +750,6 @@ public void scanShieldedTRC20NotesByOvk(OvkDecryptTRC20Parameters request, byte[] ovk = request.getOvk().toByteArray(); ProtocolStringList topicList = request.getEventsList(); try { - checkSupportShieldedTRC20Transaction(); responseObserver .onNext(wallet .scanShieldedTRC20NotesByOvk(startNum, endNum, ovk, contractAddress, topicList)); @@ -776,7 +763,6 @@ public void scanShieldedTRC20NotesByOvk(OvkDecryptTRC20Parameters request, public void isShieldedTRC20ContractNoteSpent(NfTRC20Parameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); responseObserver.onNext(wallet.isShieldedTRC20ContractNoteSpent(request)); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -1872,6 +1858,18 @@ public void listWitnesses(EmptyMessage request, responseObserver.onCompleted(); } + @Override + public void getPaginatedNowWitnessList(PaginatedMessage request, + StreamObserver responseObserver) { + try { + responseObserver.onNext( + wallet.getPaginatedNowWitnessList(request.getOffset(), request.getLimit())); + } catch (MaintenanceUnavailableException e) { + responseObserver.onError(getRunTimeException(e)); + } + responseObserver.onCompleted(); + } + @Override public void listProposals(EmptyMessage request, StreamObserver responseObserver) { @@ -2066,8 +2064,6 @@ public void getMerkleTreeVoucherInfo(OutputPointInfo request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - IncrementalMerkleVoucherInfo witnessInfo = wallet .getMerkleTreeVoucherInfo(request); responseObserver.onNext(witnessInfo); @@ -2087,8 +2083,6 @@ public void createShieldedTransaction(PrivateParameters request, Return.Builder retBuilder = Return.newBuilder(); try { - checkSupportShieldedTransaction(); - TransactionCapsule trx = wallet.createShieldedTransaction(request); trxExtBuilder.setTransaction(trx.getInstance()); trxExtBuilder.setTxid(trx.getTransactionId().getByteString()); @@ -2118,8 +2112,6 @@ public void createShieldedTransactionWithoutSpendAuthSig(PrivateParametersWithou Return.Builder retBuilder = Return.newBuilder(); try { - checkSupportShieldedTransaction(); - TransactionCapsule trx = wallet.createShieldedTransactionWithoutSpendAuthSig(request); trxExtBuilder.setTransaction(trx.getInstance()); trxExtBuilder.setTxid(trx.getTransactionId().getByteString()); @@ -2147,8 +2139,6 @@ public void getNewShieldedAddress(EmptyMessage request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getNewShieldedAddress()); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -2161,8 +2151,6 @@ public void getNewShieldedAddress(EmptyMessage request, public void getSpendingKey(EmptyMessage request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getSpendingKey()); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -2175,8 +2163,6 @@ public void getSpendingKey(EmptyMessage request, public void getRcm(EmptyMessage request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getRcm()); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -2191,8 +2177,6 @@ public void getExpandedSpendingKey(BytesMessage request, ByteString spendingKey = request.getValue(); try { - checkSupportShieldedTRC20Transaction(); - ExpandedSpendingKeyMessage response = wallet.getExpandedSpendingKey(spendingKey); responseObserver.onNext(response); } catch (BadItemException | ZksnarkException e) { @@ -2208,8 +2192,6 @@ public void getAkFromAsk(BytesMessage request, StreamObserver resp ByteString ak = request.getValue(); try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getAkFromAsk(ak)); } catch (BadItemException | ZksnarkException e) { responseObserver.onError(getRunTimeException(e)); @@ -2224,8 +2206,6 @@ public void getNkFromNsk(BytesMessage request, StreamObserver resp ByteString nk = request.getValue(); try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getNkFromNsk(nk)); } catch (BadItemException | ZksnarkException e) { responseObserver.onError(getRunTimeException(e)); @@ -2242,8 +2222,6 @@ public void getIncomingViewingKey(ViewingKeyMessage request, ByteString nk = request.getNk(); try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getIncomingViewingKey(ak.toByteArray(), nk.toByteArray())); } catch (ZksnarkException e) { responseObserver.onError(getRunTimeException(e)); @@ -2257,8 +2235,6 @@ public void getIncomingViewingKey(ViewingKeyMessage request, public void getDiversifier(EmptyMessage request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - DiversifierMessage d = wallet.getDiversifier(); responseObserver.onNext(d); } catch (ZksnarkException e) { @@ -2276,8 +2252,6 @@ public void getZenPaymentAddress(IncomingViewingKeyDiversifierMessage request, DiversifierMessage d = request.getD(); try { - checkSupportShieldedTRC20Transaction(); - PaymentAddressMessage saplingPaymentAddressMessage = wallet.getPaymentAddress(new IncomingViewingKey(ivk.getIvk().toByteArray()), new DiversifierT(d.getD().toByteArray())); @@ -2298,8 +2272,6 @@ public void scanNoteByIvk(GrpcAPI.IvkDecryptParameters request, long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotes decryptNotes = wallet .scanNoteByIvk(startNum, endNum, request.getIvk().toByteArray()); responseObserver.onNext(decryptNotes); @@ -2318,8 +2290,6 @@ public void scanAndMarkNoteByIvk(GrpcAPI.IvkDecryptAndMarkParameters request, long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotesMarked decryptNotes = wallet.scanAndMarkNoteByIvk(startNum, endNum, request.getIvk().toByteArray(), request.getAk().toByteArray(), @@ -2340,8 +2310,6 @@ public void scanNoteByOvk(GrpcAPI.OvkDecryptParameters request, long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTransaction(); - DecryptNotes decryptNotes = wallet .scanNoteByOvk(startNum, endNum, request.getOvk().toByteArray()); responseObserver.onNext(decryptNotes); @@ -2355,8 +2323,6 @@ public void scanNoteByOvk(GrpcAPI.OvkDecryptParameters request, @Override public void isSpend(NoteParameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - responseObserver.onNext(wallet.isSpend(request)); } catch (Exception e) { responseObserver.onError(getRunTimeException(e)); @@ -2369,8 +2335,6 @@ public void isSpend(NoteParameters request, StreamObserver response public void createShieldNullifier(GrpcAPI.NfParameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - BytesMessage nf = wallet .createShieldNullifier(request); responseObserver.onNext(nf); @@ -2385,8 +2349,6 @@ public void createShieldNullifier(GrpcAPI.NfParameters request, public void createSpendAuthSig(SpendAuthSigParameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - BytesMessage spendAuthSig = wallet.createSpendAuthSig(request); responseObserver.onNext(spendAuthSig); } catch (Exception e) { @@ -2400,8 +2362,6 @@ public void createSpendAuthSig(SpendAuthSigParameters request, public void getShieldTransactionHash(Transaction request, StreamObserver responseObserver) { try { - checkSupportShieldedTransaction(); - BytesMessage transactionHash = wallet.getShieldTransactionHash(request); responseObserver.onNext(transactionHash); } catch (Exception e) { @@ -2416,8 +2376,6 @@ public void createShieldedContractParameters( PrivateShieldedTRC20Parameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - ShieldedTRC20Parameters shieldedTRC20Parameters = wallet .createShieldedContractParameters(request); responseObserver.onNext(shieldedTRC20Parameters); @@ -2438,8 +2396,6 @@ public void createShieldedContractParametersWithoutAsk( PrivateShieldedTRC20ParametersWithoutAsk request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - ShieldedTRC20Parameters shieldedTRC20Parameters = wallet .createShieldedContractParametersWithoutAsk(request); responseObserver.onNext(shieldedTRC20Parameters); @@ -2459,8 +2415,6 @@ public void scanShieldedTRC20NotesByIvk( long startNum = request.getStartBlockIndex(); long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTRC20Transaction(); - DecryptNotesTRC20 decryptNotes = wallet.scanShieldedTRC20NotesByIvk(startNum, endNum, request.getShieldedTRC20ContractAddress().toByteArray(), request.getIvk().toByteArray(), @@ -2487,8 +2441,6 @@ public void scanShieldedTRC20NotesByOvk( long startNum = request.getStartBlockIndex(); long endNum = request.getEndBlockIndex(); try { - checkSupportShieldedTRC20Transaction(); - DecryptNotesTRC20 decryptNotes = wallet.scanShieldedTRC20NotesByOvk(startNum, endNum, request.getOvk().toByteArray(), request.getShieldedTRC20ContractAddress().toByteArray(), @@ -2506,8 +2458,6 @@ public void scanShieldedTRC20NotesByOvk( public void isShieldedTRC20ContractNoteSpent(NfTRC20Parameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - GrpcAPI.NullifierResult nf = wallet .isShieldedTRC20ContractNoteSpent(request); responseObserver.onNext(nf); @@ -2523,8 +2473,6 @@ public void getTriggerInputForShieldedTRC20Contract( ShieldedTRC20TriggerContractParameters request, StreamObserver responseObserver) { try { - checkSupportShieldedTRC20Transaction(); - responseObserver.onNext(wallet.getTriggerInputForShieldedTRC20Contract(request)); } catch (Exception e) { responseObserver.onError(e); diff --git a/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java b/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java index 3548859262e..e92bf6c8f1a 100644 --- a/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java +++ b/framework/src/main/java/org/tron/core/services/event/BlockEventCache.java @@ -66,7 +66,14 @@ public static void add(BlockEvent blockEvent) throws EventException { } if (blockEvent.getSolidId().getNum() > solidId.getNum()) { - solidId = blockEvent.getSolidId(); + BlockCapsule.BlockId headBlockId = head.getBlockId(); + if (blockEvent.getSolidId().getNum() <= headBlockId.getNum()) { + solidId = blockEvent.getSolidId(); + } else if (blockEvent.getBlockId().equals(headBlockId)) { + // Fork chains needs to be considered to ensure that the head is on the main chain. + logger.info("Set solidId to head {}", headBlockId.getString()); + solidId = headBlockId; + } } } diff --git a/framework/src/main/java/org/tron/core/services/event/BlockEventGet.java b/framework/src/main/java/org/tron/core/services/event/BlockEventGet.java index bf668a3e0b6..122a61222c3 100644 --- a/framework/src/main/java/org/tron/core/services/event/BlockEventGet.java +++ b/framework/src/main/java/org/tron/core/services/event/BlockEventGet.java @@ -57,10 +57,16 @@ public BlockEvent getBlockEvent(long blockNum) throws Exception { BlockCapsule block = manager.getChainBaseManager().getBlockByNum(blockNum); block.getTransactions().forEach(t -> t.setBlockNum(block.getNum())); long solidNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum(); + long headNum = manager.getHeadBlockNum(); + // solve the single SR concurrency problem + if (solidNum >= headNum && headNum > 0) { + solidNum = headNum - 1; + } BlockEvent blockEvent = new BlockEvent(); blockEvent.setBlockId(block.getBlockId()); blockEvent.setParentId(block.getParentBlockId()); blockEvent.setSolidId(manager.getChainBaseManager().getBlockIdByNum(solidNum)); + if (instance.isBlockLogTriggerEnable()) { blockEvent.setBlockLogTriggerCapsule(getBlockLogTrigger(block, solidNum)); } @@ -88,18 +94,13 @@ public BlockEvent getBlockEvent(long blockNum) throws Exception { } public SmartContractTrigger getContractTrigger(BlockCapsule block, long solidNum) { - TransactionRetCapsule result; - try { - result = manager.getChainBaseManager().getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(block.getNum())); - } catch (BadItemException e) { - throw new RuntimeException(e); - } + + GrpcAPI.TransactionInfoList list = manager.getTransactionInfoByBlockNum(block.getNum()); SmartContractTrigger contractTrigger = new SmartContractTrigger(); for (int i = 0; i < block.getTransactions().size(); i++) { Protocol.Transaction tx = block.getInstance().getTransactions(i); - Protocol.TransactionInfo txInfo = result.getInstance().getTransactioninfo(i); + Protocol.TransactionInfo txInfo = list.getTransactionInfo(i); List triggers = parseLogs(tx, txInfo); for (ContractTrigger trigger : triggers) { @@ -328,22 +329,10 @@ public List getTransactionLogTrigger(BlockCapsule if (!EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible()) { return getTransactionTriggers(block, solidNum); } + + GrpcAPI.TransactionInfoList transactionInfoList + = manager.getTransactionInfoByBlockNum(block.getNum()); List transactionCapsuleList = block.getTransactions(); - GrpcAPI.TransactionInfoList transactionInfoList = GrpcAPI - .TransactionInfoList.newBuilder().build(); - GrpcAPI.TransactionInfoList.Builder transactionInfoListBuilder = GrpcAPI - .TransactionInfoList.newBuilder(); - try { - TransactionRetCapsule result = manager.getChainBaseManager().getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(block.getNum())); - if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { - result.getInstance().getTransactioninfoList() - .forEach(transactionInfoListBuilder::addTransactionInfo); - transactionInfoList = transactionInfoListBuilder.build(); - } - } catch (BadItemException e) { - logger.error("Get TransactionInfo failed, blockNum {}, {}.", block.getNum(), e.getMessage()); - } if (transactionCapsuleList.size() != transactionInfoList.getTransactionInfoCount()) { logger.error("Get TransactionInfo size not eq, blockNum {}, {}, {}", block.getNum(), transactionCapsuleList.size(), @@ -384,22 +373,8 @@ public List getTransactionTriggers(BlockCapsule bl return list; } - GrpcAPI.TransactionInfoList transactionInfoList = GrpcAPI - .TransactionInfoList.newBuilder().build(); - GrpcAPI.TransactionInfoList.Builder transactionInfoListBuilder = GrpcAPI - .TransactionInfoList.newBuilder(); - try { - TransactionRetCapsule result = manager.getChainBaseManager().getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(block.getNum())); - if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) { - result.getInstance().getTransactioninfoList() - .forEach(transactionInfoListBuilder::addTransactionInfo); - transactionInfoList = transactionInfoListBuilder.build(); - } - } catch (Exception e) { - logger.warn("Get TransactionInfo failed, blockNum {}, {}.", block.getNum(), e.getMessage()); - } - + GrpcAPI.TransactionInfoList transactionInfoList + = manager.getTransactionInfoByBlockNum(block.getNum()); if (block.getTransactions().size() != transactionInfoList.getTransactionInfoCount()) { for (TransactionCapsule t : block.getTransactions()) { TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(t, block); diff --git a/framework/src/main/java/org/tron/core/services/event/HistoryEventService.java b/framework/src/main/java/org/tron/core/services/event/HistoryEventService.java index 8f79ee47a3c..2eccb9fa2a9 100644 --- a/framework/src/main/java/org/tron/core/services/event/HistoryEventService.java +++ b/framework/src/main/java/org/tron/core/services/event/HistoryEventService.java @@ -29,6 +29,8 @@ public class HistoryEventService { @Autowired private Manager manager; + private volatile boolean isClosed = false; + private volatile Thread thread; public void init() { @@ -44,8 +46,15 @@ public void init() { } public void close() { + isClosed = true; if (thread != null) { - thread.interrupt(); + try { + thread.interrupt(); + thread.join(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.warn("Wait close timeout, {}", e.getMessage()); + } } logger.info("History event service close."); } @@ -54,7 +63,10 @@ private void syncEvent() { try { long tmp = instance.getStartSyncBlockNum(); long endNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum(); - while (tmp <= endNum) { + while (tmp < endNum) { + if (thread.isInterrupted() || isClosed) { + throw new InterruptedException(); + } if (instance.isUseNativeQueue()) { Thread.sleep(20); } else if (instance.isBusy()) { @@ -67,7 +79,8 @@ private void syncEvent() { tmp++; endNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum(); } - initEventService(manager.getChainBaseManager().getBlockIdByNum(endNum)); + long startNum = endNum == 0 ? 0 : endNum - 1; + initEventService(manager.getChainBaseManager().getBlockIdByNum(startNum)); } catch (InterruptedException e1) { logger.warn("History event service interrupted."); Thread.currentThread().interrupt(); diff --git a/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java b/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java index 093594f1c95..5aee55b1c13 100644 --- a/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java +++ b/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java @@ -4,6 +4,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; + +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -20,6 +22,9 @@ public class RealtimeEventService { private EventPluginLoader instance = EventPluginLoader.getInstance(); + @Getter + private static Object contractLock = new Object(); + @Autowired private Manager manager; @@ -94,30 +99,32 @@ public void flush(BlockEvent blockEvent, boolean isRemove) { } } - if (instance.isContractEventTriggerEnable()) { - if (blockEvent.getSmartContractTrigger() == null) { - logger.warn("SmartContractTrigger is null. {}", blockEvent.getBlockId().getString()); - } else { - blockEvent.getSmartContractTrigger().getContractEventTriggers().forEach(v -> { - v.setTriggerName(Trigger.CONTRACTEVENT_TRIGGER_NAME); - v.setRemoved(isRemove); - EventPluginLoader.getInstance().postContractEventTrigger(v); - }); + synchronized (contractLock) { + if (instance.isContractEventTriggerEnable()) { + if (blockEvent.getSmartContractTrigger() == null) { + logger.warn("SmartContractTrigger is null. {}", blockEvent.getBlockId().getString()); + } else { + blockEvent.getSmartContractTrigger().getContractEventTriggers().forEach(v -> { + v.setTriggerName(Trigger.CONTRACTEVENT_TRIGGER_NAME); + v.setRemoved(isRemove); + EventPluginLoader.getInstance().postContractEventTrigger(v); + }); + } } - } - if (instance.isContractLogTriggerEnable() && blockEvent.getSmartContractTrigger() != null) { - blockEvent.getSmartContractTrigger().getContractLogTriggers().forEach(v -> { - v.setTriggerName(Trigger.CONTRACTLOG_TRIGGER_NAME); - v.setRemoved(isRemove); - EventPluginLoader.getInstance().postContractLogTrigger(v); - }); - if (instance.isContractLogTriggerRedundancy()) { - blockEvent.getSmartContractTrigger().getRedundancies().forEach(v -> { + if (instance.isContractLogTriggerEnable() && blockEvent.getSmartContractTrigger() != null) { + blockEvent.getSmartContractTrigger().getContractLogTriggers().forEach(v -> { v.setTriggerName(Trigger.CONTRACTLOG_TRIGGER_NAME); v.setRemoved(isRemove); EventPluginLoader.getInstance().postContractLogTrigger(v); }); + if (instance.isContractLogTriggerRedundancy()) { + blockEvent.getSmartContractTrigger().getRedundancies().forEach(v -> { + v.setTriggerName(Trigger.CONTRACTLOG_TRIGGER_NAME); + v.setRemoved(isRemove); + EventPluginLoader.getInstance().postContractLogTrigger(v); + }); + } } } } diff --git a/framework/src/main/java/org/tron/core/services/event/SolidEventService.java b/framework/src/main/java/org/tron/core/services/event/SolidEventService.java index 6102a87f892..0614541f16f 100644 --- a/framework/src/main/java/org/tron/core/services/event/SolidEventService.java +++ b/framework/src/main/java/org/tron/core/services/event/SolidEventService.java @@ -86,27 +86,32 @@ public void flush(BlockEvent blockEvent) { } } - if (instance.isSolidityEventTriggerEnable()) { - if (blockEvent.getSmartContractTrigger() == null) { - logger.warn("SmartContractTrigger is null. {}", blockEvent.getBlockId()); - } else { - blockEvent.getSmartContractTrigger().getContractEventTriggers().forEach(v -> { - v.setTriggerName(Trigger.SOLIDITYEVENT_TRIGGER_NAME); - EventPluginLoader.getInstance().postSolidityEventTrigger(v); - }); + synchronized (RealtimeEventService.getContractLock()) { + if (instance.isSolidityEventTriggerEnable()) { + if (blockEvent.getSmartContractTrigger() == null) { + logger.warn("SmartContractTrigger is null. {}", blockEvent.getBlockId()); + } else { + blockEvent.getSmartContractTrigger().getContractEventTriggers().forEach(v -> { + v.setTriggerName(Trigger.SOLIDITYEVENT_TRIGGER_NAME); + v.setRemoved(false); + EventPluginLoader.getInstance().postSolidityEventTrigger(v); + }); + } } - } - if (instance.isSolidityLogTriggerEnable() && blockEvent.getSmartContractTrigger() != null) { - blockEvent.getSmartContractTrigger().getContractLogTriggers().forEach(v -> { - v.setTriggerName(Trigger.SOLIDITYLOG_TRIGGER_NAME); - EventPluginLoader.getInstance().postSolidityLogTrigger(v); - }); - if (instance.isSolidityLogTriggerRedundancy()) { - blockEvent.getSmartContractTrigger().getRedundancies().forEach(v -> { + if (instance.isSolidityLogTriggerEnable() && blockEvent.getSmartContractTrigger() != null) { + blockEvent.getSmartContractTrigger().getContractLogTriggers().forEach(v -> { v.setTriggerName(Trigger.SOLIDITYLOG_TRIGGER_NAME); + v.setRemoved(false); EventPluginLoader.getInstance().postSolidityLogTrigger(v); }); + if (instance.isSolidityLogTriggerRedundancy()) { + blockEvent.getSmartContractTrigger().getRedundancies().forEach(v -> { + v.setTriggerName(Trigger.SOLIDITYLOG_TRIGGER_NAME); + v.setRemoved(false); + EventPluginLoader.getInstance().postSolidityLogTrigger(v); + }); + } } } diff --git a/framework/src/main/java/org/tron/core/services/filter/HttpApiAccessFilter.java b/framework/src/main/java/org/tron/core/services/filter/HttpApiAccessFilter.java index 0405165ff99..59b9b15582b 100644 --- a/framework/src/main/java/org/tron/core/services/filter/HttpApiAccessFilter.java +++ b/framework/src/main/java/org/tron/core/services/filter/HttpApiAccessFilter.java @@ -26,7 +26,8 @@ public void init(FilterConfig filterConfig) { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { if (request instanceof HttpServletRequest) { - String endpoint = ((HttpServletRequest) request).getRequestURI(); + String contextPath = ((HttpServletRequest) request).getContextPath(); + String endpoint = contextPath + ((HttpServletRequest) request).getServletPath(); HttpServletResponse resp = (HttpServletResponse) response; if (isDisabled(endpoint)) { diff --git a/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java b/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java index 8b43cfef642..2ff8a5ad321 100644 --- a/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java +++ b/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java @@ -34,7 +34,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha chain.doFilter(request, response); return; } - endpoint = ((HttpServletRequest) request).getRequestURI(); + String contextPath = ((HttpServletRequest) request).getContextPath(); + endpoint = contextPath + ((HttpServletRequest) request).getServletPath(); CharResponseWrapper responseWrapper = new CharResponseWrapper( (HttpServletResponse) response); chain.doFilter(request, responseWrapper); diff --git a/framework/src/main/java/org/tron/core/services/filter/LiteFnQueryHttpFilter.java b/framework/src/main/java/org/tron/core/services/filter/LiteFnQueryHttpFilter.java index a8ab947066c..07025996677 100644 --- a/framework/src/main/java/org/tron/core/services/filter/LiteFnQueryHttpFilter.java +++ b/framework/src/main/java/org/tron/core/services/filter/LiteFnQueryHttpFilter.java @@ -110,7 +110,8 @@ public void init(FilterConfig filterConfig) throws ServletException { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - String requestPath = ((HttpServletRequest) servletRequest).getRequestURI(); + String contextPath = ((HttpServletRequest) servletRequest).getContextPath(); + String requestPath = contextPath + ((HttpServletRequest) servletRequest).getServletPath(); if (chainBaseManager.isLiteNode() && !CommonParameter.getInstance().openHistoryQueryWhenLiteFN && filterPaths.contains(requestPath)) { diff --git a/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java b/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java index 76785218096..3ad4ace62fc 100644 --- a/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java +++ b/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java @@ -86,6 +86,8 @@ public class FullNodeHttpApiService extends HttpService { @Autowired private ListWitnessesServlet listWitnessesServlet; @Autowired + private GetPaginatedNowWitnessListServlet getPaginatedNowWitnessListServlet; + @Autowired private GetAssetIssueListServlet getAssetIssueListServlet; @Autowired private GetPaginatedAssetIssueListServlet getPaginatedAssetIssueListServlet; @@ -342,7 +344,11 @@ protected void addServlet(ServletContextHandler context) { context.addServlet( new ServletHolder(getTransactionCountByBlockNumServlet), "/wallet/gettransactioncountbyblocknum"); + // Get the list of witnesses info with contains vote counts for last epoch/maintenance context.addServlet(new ServletHolder(listWitnessesServlet), "/wallet/listwitnesses"); + // Get the paged list of witnesses info with realtime vote counts + context.addServlet(new ServletHolder(getPaginatedNowWitnessListServlet), + "/wallet/getpaginatednowwitnesslist"); context.addServlet(new ServletHolder(getAssetIssueListServlet), "/wallet/getassetissuelist"); context.addServlet( new ServletHolder(getPaginatedAssetIssueListServlet), diff --git a/framework/src/main/java/org/tron/core/services/http/GetPaginatedNowWitnessListServlet.java b/framework/src/main/java/org/tron/core/services/http/GetPaginatedNowWitnessListServlet.java new file mode 100644 index 00000000000..e53ab6610ec --- /dev/null +++ b/framework/src/main/java/org/tron/core/services/http/GetPaginatedNowWitnessListServlet.java @@ -0,0 +1,52 @@ +package org.tron.core.services.http; + +import java.io.IOException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.tron.api.GrpcAPI; +import org.tron.core.Wallet; +import org.tron.core.exception.MaintenanceUnavailableException; + +// Get the paged list of witnesses info with realtime vote counts +@Component +@Slf4j(topic = "API") +public class GetPaginatedNowWitnessListServlet extends RateLimiterServlet { + + @Autowired + private Wallet wallet; + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + try { + boolean visible = Util.getVisible(request); + long offset = Long.parseLong(request.getParameter("offset")); + long limit = Long.parseLong(request.getParameter("limit")); + fillResponse(offset, limit, visible, response); + } catch (Exception e) { + Util.processError(e, response); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) { + try { + PostParams params = PostParams.getPostParams(request); + GrpcAPI.PaginatedMessage.Builder build = GrpcAPI.PaginatedMessage.newBuilder(); + JsonFormat.merge(params.getParams(), build, params.isVisible()); + fillResponse(build.getOffset(), build.getLimit(), params.isVisible(), response); + } catch (Exception e) { + Util.processError(e, response); + } + } + + private void fillResponse(long offset, long limit, boolean visible, HttpServletResponse response) + throws IOException, MaintenanceUnavailableException { + GrpcAPI.WitnessList reply = wallet.getPaginatedNowWitnessList(offset, limit); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } + } +} diff --git a/framework/src/main/java/org/tron/core/services/http/GetProposalByIdServlet.java b/framework/src/main/java/org/tron/core/services/http/GetProposalByIdServlet.java index a903a5b4920..9d7805d4f98 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetProposalByIdServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetProposalByIdServlet.java @@ -26,7 +26,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); String input = request.getParameter("id"); - long id = new Long(input); + long id = Long.parseLong(input); fillResponse(ByteString.copyFrom(ByteArray.fromLong(id)), visible, response); } catch (Exception e) { Util.processError(e, response); diff --git a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java index fa59a72303d..7a66aed34f6 100644 --- a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java @@ -99,8 +99,9 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) if (rateLimiter != null) { acquireResource = rateLimiter.acquire(runtimeData); } - String url = Strings.isNullOrEmpty(req.getRequestURI()) - ? MetricLabels.UNDEFINED : req.getRequestURI(); + String contextPath = req.getContextPath(); + String url = Strings.isNullOrEmpty(req.getServletPath()) + ? MetricLabels.UNDEFINED : contextPath + req.getServletPath(); try { resp.setContentType("application/json; charset=utf-8"); diff --git a/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java b/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java index ea08d2d42cf..359adfc2b39 100644 --- a/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java +++ b/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java @@ -44,6 +44,7 @@ import org.tron.core.services.http.GetNodeInfoServlet; import org.tron.core.services.http.GetNowBlockServlet; import org.tron.core.services.http.GetPaginatedAssetIssueListServlet; +import org.tron.core.services.http.GetPaginatedNowWitnessListServlet; import org.tron.core.services.http.GetRewardServlet; import org.tron.core.services.http.GetTransactionCountByBlockNumServlet; import org.tron.core.services.http.GetTransactionInfoByBlockNumServlet; @@ -92,6 +93,8 @@ public class SolidityNodeHttpApiService extends HttpService { @Autowired private ListWitnessesServlet listWitnessesServlet; @Autowired + private GetPaginatedNowWitnessListServlet getPaginatedNowWitnessListServlet; + @Autowired private GetAssetIssueListServlet getAssetIssueListServlet; @Autowired private GetPaginatedAssetIssueListServlet getPaginatedAssetIssueListServlet; @@ -174,6 +177,8 @@ protected void addServlet(ServletContextHandler context) { // same as FullNode context.addServlet(new ServletHolder(getAccountServlet), "/walletsolidity/getaccount"); context.addServlet(new ServletHolder(listWitnessesServlet), "/walletsolidity/listwitnesses"); + context.addServlet(new ServletHolder(getPaginatedNowWitnessListServlet), + "/walletsolidity/getpaginatednowwitnesslist"); context.addServlet(new ServletHolder(getAssetIssueListServlet), "/walletsolidity/getassetissuelist"); context.addServlet(new ServletHolder(getPaginatedAssetIssueListServlet), diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java index 828d36e664f..a77b45353c9 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java @@ -172,7 +172,7 @@ public class HttpApiOnPBFTService extends HttpService { public HttpApiOnPBFTService() { port = Args.getInstance().getPBFTHttpPort(); enable = isFullNode() && Args.getInstance().isPBFTHttpEnable(); - contextPath = "/walletpbft/"; + contextPath = "/walletpbft"; } @Override diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java index aa566f56042..315d70df8d6 100755 --- a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java @@ -162,6 +162,13 @@ public void listWitnesses(EmptyMessage request, StreamObserver resp () -> rpcApiService.getWalletSolidityApi().listWitnesses(request, responseObserver)); } + public void getPaginatedNowWitnessList(PaginatedMessage request, + StreamObserver responseObserver) { + walletOnSolidity.futureGet( + () -> rpcApiService.getWalletSolidityApi() + .getPaginatedNowWitnessList(request, responseObserver)); + } + @Override public void getAssetIssueById(BytesMessage request, StreamObserver responseObserver) { diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/GetPaginatedNowWitnessListOnSolidityServlet.java b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/GetPaginatedNowWitnessListOnSolidityServlet.java new file mode 100644 index 00000000000..4578393ec76 --- /dev/null +++ b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/GetPaginatedNowWitnessListOnSolidityServlet.java @@ -0,0 +1,24 @@ +package org.tron.core.services.interfaceOnSolidity.http; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.tron.core.services.http.GetPaginatedNowWitnessListServlet; +import org.tron.core.services.interfaceOnSolidity.WalletOnSolidity; + +@Component +@Slf4j(topic = "API") +public class GetPaginatedNowWitnessListOnSolidityServlet extends GetPaginatedNowWitnessListServlet { + @Autowired + private WalletOnSolidity walletOnSolidity; + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + walletOnSolidity.futureGet(() -> super.doGet(request, response)); + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) { + walletOnSolidity.futureGet(() -> super.doPost(request, response)); + } +} diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java index b1d940ce2cd..f69597959f8 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java @@ -3,8 +3,6 @@ import java.util.EnumSet; import javax.servlet.DispatcherType; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -46,6 +44,7 @@ import org.tron.core.services.interfaceOnSolidity.http.GetNodeInfoOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.GetNowBlockOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.GetPaginatedAssetIssueListOnSolidityServlet; +import org.tron.core.services.interfaceOnSolidity.http.GetPaginatedNowWitnessListOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.GetRewardOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.GetTransactionCountByBlockNumOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.GetTransactionInfoByBlockNumOnSolidityServlet; @@ -60,7 +59,6 @@ import org.tron.core.services.interfaceOnSolidity.http.ScanShieldedTRC20NotesByOvkOnSolidityServlet; import org.tron.core.services.interfaceOnSolidity.http.TriggerConstantContractOnSolidityServlet; - @Slf4j(topic = "API") public class HttpApiOnSolidityService extends HttpService { @@ -74,6 +72,8 @@ public class HttpApiOnSolidityService extends HttpService { @Autowired private ListWitnessesOnSolidityServlet listWitnessesOnSolidityServlet; @Autowired + private GetPaginatedNowWitnessListOnSolidityServlet getPaginatedNowWitnessListOnSolidityServlet; + @Autowired private GetAssetIssueListOnSolidityServlet getAssetIssueListOnSolidityServlet; @Autowired private GetPaginatedAssetIssueListOnSolidityServlet getPaginatedAssetIssueListOnSolidityServlet; @@ -189,6 +189,8 @@ protected void addServlet(ServletContextHandler context) { context.addServlet(new ServletHolder(accountOnSolidityServlet), "/walletsolidity/getaccount"); context.addServlet(new ServletHolder(listWitnessesOnSolidityServlet), "/walletsolidity/listwitnesses"); + context.addServlet(new ServletHolder(getPaginatedNowWitnessListOnSolidityServlet), + "/walletsolidity/getpaginatednowwitnesslist"); context.addServlet(new ServletHolder(getAssetIssueListOnSolidityServlet), "/walletsolidity/getassetissuelist"); context.addServlet(new ServletHolder(getPaginatedAssetIssueListOnSolidityServlet), diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java index 955ba55060f..4a60f14b534 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java @@ -26,7 +26,7 @@ import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.StringUtil; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java new file mode 100644 index 00000000000..b92b3cf1af6 --- /dev/null +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java @@ -0,0 +1,81 @@ +package org.tron.core.services.jsonrpc; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorData; +import com.googlecode.jsonrpc4j.ErrorResolver; +import com.googlecode.jsonrpc4j.JsonRpcError; +import com.googlecode.jsonrpc4j.JsonRpcErrors; +import com.googlecode.jsonrpc4j.ReflectionUtil; +import java.lang.reflect.Method; +import java.util.List; +import org.tron.core.exception.jsonrpc.JsonRpcException; + +/** + * {@link ErrorResolver} that uses annotations. + */ +public enum JsonRpcErrorResolver implements ErrorResolver { + INSTANCE; + + /** + * {@inheritDoc} + */ + @Override + public JsonError resolveError( + Throwable thrownException, Method method, List arguments) { + JsonRpcError resolver = getResolverForException(thrownException, method); + if (notFoundResolver(resolver)) { + return null; + } + + String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); + + // data priority: exception > annotation > default ErrorData + Object data = null; + if (thrownException instanceof JsonRpcException) { + JsonRpcException jsonRpcException = (JsonRpcException) thrownException; + data = jsonRpcException.getData(); + } + + if (data == null) { + data = hasErrorData(resolver) + ? resolver.data() + : new ErrorData(resolver.exception().getName(), message); + } + + return new JsonError(resolver.code(), message, data); + } + + private JsonRpcError getResolverForException(Throwable thrownException, Method method) { + JsonRpcErrors errors = ReflectionUtil.getAnnotation(method, JsonRpcErrors.class); + if (hasAnnotations(errors)) { + for (JsonRpcError errorDefined : errors.value()) { + if (isExceptionInstanceOfError(thrownException, errorDefined)) { + return errorDefined; + } + } + } + return null; + } + + private boolean notFoundResolver(JsonRpcError resolver) { + return resolver == null; + } + + private boolean hasErrorMessage(JsonRpcError em) { + // noinspection ConstantConditions + return em.message() != null && !em.message().trim().isEmpty(); + } + + private boolean hasErrorData(JsonRpcError em) { + // noinspection ConstantConditions + return em.data() != null && !em.data().trim().isEmpty(); + } + + private boolean hasAnnotations(JsonRpcErrors errors) { + return errors != null; + } + + private boolean isExceptionInstanceOfError(Throwable target, JsonRpcError em) { + return em.exception().isInstance(target); + } +} \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index 878b71d86b5..104a0e9e470 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -43,6 +43,7 @@ public void init(ServletConfig config) throws ServletException { true); rpcServer = new JsonRpcServer(compositeService); + rpcServer.setErrorResolver(JsonRpcErrorResolver.INSTANCE); HttpStatusCodeProvider httpStatusCodeProvider = new HttpStatusCodeProvider() { @Override diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java index 52a3a2380d1..115df6ef9da 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java @@ -7,6 +7,7 @@ import com.googlecode.jsonrpc4j.JsonRpcMethod; import java.io.IOException; import java.util.List; +import java.util.Objects; import java.util.concurrent.ExecutionException; import lombok.AllArgsConstructor; import lombok.Getter; @@ -18,17 +19,21 @@ import org.tron.common.utils.ByteArray; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; +import org.tron.core.exception.jsonrpc.JsonRpcExceedLimitException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.jsonrpc.types.BlockResult; import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.core.services.jsonrpc.types.TransactionReceipt; import org.tron.core.services.jsonrpc.types.TransactionResult; +/** + * Error code refers to https://www.quicknode.com/docs/ethereum/error-references + */ @Component public interface TronJsonRpc { @@ -146,6 +151,14 @@ TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, Stri }) TransactionReceipt getTransactionReceipt(String txid) throws JsonRpcInvalidParamsException; + @JsonRpcMethod("eth_getBlockReceipts") + @JsonRpcErrors({ + @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), + @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}") + }) + List getBlockReceipts(String blockNumOrHashOrTag) + throws JsonRpcInvalidParamsException, JsonRpcInternalException; + @JsonRpcMethod("eth_call") @JsonRpcErrors({ @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), @@ -284,9 +297,10 @@ String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException, @JsonRpcMethod("eth_newBlockFilter") @JsonRpcErrors({ + @JsonRpcError(exception = JsonRpcExceedLimitException.class, code = -32005, data = "{}"), @JsonRpcError(exception = JsonRpcMethodNotFoundException.class, code = -32601, data = "{}"), }) - String newBlockFilter() throws JsonRpcMethodNotFoundException; + String newBlockFilter() throws JsonRpcExceedLimitException, JsonRpcMethodNotFoundException; @JsonRpcMethod("eth_uninstallFilter") @JsonRpcErrors({ @@ -464,5 +478,35 @@ public LogFilterElement(String blockHash, Long blockNum, String txId, Integer tx } this.removed = removed; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || this.getClass() != o.getClass()) { + return false; + } + LogFilterElement item = (LogFilterElement) o; + if (!Objects.equals(blockHash, item.blockHash)) { + return false; + } + if (!Objects.equals(transactionHash, item.transactionHash)) { + return false; + } + if (!Objects.equals(transactionIndex, item.transactionIndex)) { + return false; + } + if (!Objects.equals(logIndex, item.logIndex)) { + return false; + } + return removed == item.removed; + } + + @Override + public int hashCode() { + return Objects.hash(blockHash, transactionHash, transactionIndex, logIndex, removed); + } + } } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index eb432432a1c..de939bdfff4 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -11,10 +11,13 @@ import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract; import com.alibaba.fastjson.JSON; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.protobuf.ByteString; import com.google.protobuf.GeneratedMessageV3; import java.io.Closeable; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; @@ -24,11 +27,10 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.regex.Matcher; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; import org.springframework.beans.factory.annotation.Autowired; @@ -38,6 +40,7 @@ import org.tron.api.GrpcAPI.Return; import org.tron.api.GrpcAPI.Return.response_code; import org.tron.api.GrpcAPI.TransactionExtention; +import org.tron.api.GrpcAPI.TransactionInfoList; import org.tron.common.crypto.Hash; import org.tron.common.es.ExecutorServiceManager; import org.tron.common.logsfilter.ContractEventParser; @@ -50,6 +53,7 @@ import org.tron.core.Wallet; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.config.args.Args; import org.tron.core.db.Manager; import org.tron.core.db2.core.Chainbase; import org.tron.core.exception.BadItemException; @@ -57,12 +61,13 @@ import org.tron.core.exception.ContractValidateException; import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; import org.tron.core.exception.VMIllegalException; +import org.tron.core.exception.jsonrpc.JsonRpcExceedLimitException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.http.JsonFormat; import org.tron.core.services.http.Util; @@ -76,12 +81,14 @@ import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.core.services.jsonrpc.types.TransactionReceipt; +import org.tron.core.services.jsonrpc.types.TransactionReceipt.TransactionContext; import org.tron.core.services.jsonrpc.types.TransactionResult; import org.tron.core.store.StorageRowStore; import org.tron.core.vm.program.Storage; import org.tron.program.Version; import org.tron.protos.Protocol.Account; import org.tron.protos.Protocol.Block; +import org.tron.protos.Protocol.ResourceReceipt; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.Protocol.Transaction.Result.code; @@ -94,6 +101,7 @@ import org.tron.protos.contract.SmartContractOuterClass.SmartContractDataWrapper; import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract; + @Slf4j(topic = "API") @Component public class TronJsonRpcImpl implements TronJsonRpc, Closeable { @@ -106,6 +114,17 @@ public enum RequestSource { private static final String FILTER_NOT_FOUND = "filter not found"; public static final int EXPIRE_SECONDS = 5 * 60; + private static final int maxBlockFilterNum = Args.getInstance().getJsonRpcMaxBlockFilterNum(); + private static final Cache logElementCache = + CacheBuilder.newBuilder() + .maximumSize(300_000L) // 300s * tps(1000) * 1 log/tx ≈ 300_000 + .expireAfterWrite(EXPIRE_SECONDS, TimeUnit.SECONDS) + .recordStats().build(); //LRU cache + private static final Cache blockHashCache = + CacheBuilder.newBuilder() + .maximumSize(60_000L) // 300s * 200 block/s when syncing + .expireAfterWrite(EXPIRE_SECONDS, TimeUnit.SECONDS) + .recordStats().build(); //LRU cache /** * for log filter in Full Json-RPC */ @@ -177,16 +196,31 @@ public static void handleBLockFilter(BlockFilterCapsule blockFilterCapsule) { it = getBlockFilter2ResultFull().entrySet().iterator(); } + if (!it.hasNext()) { + return; + } + final String originalBlockHash = ByteArray.toJsonHex(blockFilterCapsule.getBlockHash()); + String cachedBlockHash; + try { + // compare with hashcode() first, then with equals(). If not exist, put it. + cachedBlockHash = blockHashCache.get(originalBlockHash, () -> originalBlockHash); + } catch (ExecutionException e) { + logger.error("Getting/loading blockHash from cache failed", e); // never happen + cachedBlockHash = originalBlockHash; + } while (it.hasNext()) { Entry entry = it.next(); if (entry.getValue().isExpire()) { it.remove(); continue; } - entry.getValue().getResult().add(ByteArray.toJsonHex(blockFilterCapsule.getBlockHash())); + entry.getValue().getResult().add(cachedBlockHash); } } + /** + * append LogsFilterCapsule's LogFilterElement list to each filter if matched + */ public static void handleLogsFilter(LogsFilterCapsule logsFilterCapsule) { Iterator> it; @@ -222,23 +256,28 @@ public static void handleLogsFilter(LogsFilterCapsule logsFilterCapsule) { LogMatch.matchBlock(logFilter, logsFilterCapsule.getBlockNumber(), logsFilterCapsule.getBlockHash(), logsFilterCapsule.getTxInfoList(), logsFilterCapsule.isRemoved()); - if (CollectionUtils.isNotEmpty(elements)) { - logFilterAndResult.getResult().addAll(elements); + + for (LogFilterElement element : elements) { + LogFilterElement cachedElement; + try { + // compare with hashcode() first, then with equals(). If not exist, put it. + cachedElement = logElementCache.get(element, () -> element); + } catch (ExecutionException e) { + logger.error("Getting/loading LogFilterElement from cache fails", e); // never happen + cachedElement = element; + } + logFilterAndResult.getResult().add(cachedElement); } } } @Override public String web3ClientVersion() { - Pattern shortVersion = Pattern.compile("(\\d\\.\\d).*"); - Matcher matcher = shortVersion.matcher(System.getProperty("java.version")); - matcher.matches(); - return String.join("/", Arrays.asList( "TRON", "v" + Version.getVersion(), System.getProperty("os.name"), - "Java" + matcher.group(1))); + "Java" + System.getProperty("java.specification.version"))); } @Override @@ -473,7 +512,6 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va } result = ByteArray.toJsonHex(listBytes); } else { - logger.error("trigger contract failed."); String errMsg = retBuilder.getMessage().toStringUtf8(); byte[] resData = trxExtBuilder.getConstantResult(0).toByteArray(); if (resData.length > 4 && Hex.toHexString(resData).startsWith(ERROR_SELECTOR)) { @@ -483,7 +521,12 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va errMsg += ": " + msg; } - throw new JsonRpcInternalException(errMsg); + if (resData.length > 0) { + throw new JsonRpcInternalException(errMsg, ByteArray.toJsonHex(resData)); + } else { + throw new JsonRpcInternalException(errMsg); + } + } return result; @@ -644,7 +687,12 @@ public String estimateGas(CallArguments args) throws JsonRpcInvalidRequestExcept errMsg += ": " + msg; } - throw new JsonRpcInternalException(errMsg); + if (data.length > 0) { + throw new JsonRpcInternalException(errMsg, ByteArray.toJsonHex(data)); + } else { + throw new JsonRpcInternalException(errMsg); + } + } else { if (supportEstimateEnergy) { @@ -763,6 +811,13 @@ public TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTa return getTransactionByBlockAndIndex(block, index); } + /** + * Get a transaction receipt by transaction hash + * + * @param txId the transaction hash in hex format (with or without 0x prefix) + * @return TransactionReceipt object for the specified transaction, or null if not found + * @throws JsonRpcInvalidParamsException if the transaction hash format is invalid + */ @Override public TransactionReceipt getTransactionReceipt(String txId) throws JsonRpcInvalidParamsException { @@ -777,7 +832,126 @@ public TransactionReceipt getTransactionReceipt(String txId) return null; } - return new TransactionReceipt(block, transactionInfo, wallet); + BlockCapsule blockCapsule = new BlockCapsule(block); + long blockNum = blockCapsule.getNum(); + TransactionInfoList transactionInfoList = wallet.getTransactionInfoByBlockNum(blockNum); + long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp()); + + // Find transaction context + TransactionReceipt.TransactionContext context + = findTransactionContext(transactionInfoList, + transactionInfo.getId()); + + if (context == null) { + return null; // Transaction not found in block + } + + return new TransactionReceipt(blockCapsule, transactionInfo, context, energyFee); + } + + /** + * Finds transaction context for a specific transaction ID within the block + * Calculates cumulative gas and log count up to the target transaction + * @param infoList the transactionInfo list for the block + * @param txId the transaction ID + * @return TransactionContext containing index and cumulative values, or null if not found + */ + private TransactionContext findTransactionContext(TransactionInfoList infoList, + ByteString txId) { + + long cumulativeGas = 0; + long cumulativeLogCount = 0; + + for (int index = 0; index < infoList.getTransactionInfoCount(); index++) { + TransactionInfo info = infoList.getTransactionInfo(index); + ResourceReceipt resourceReceipt = info.getReceipt(); + + if (info.getId().equals(txId)) { + return new TransactionContext(index, cumulativeGas, cumulativeLogCount); + } else { + cumulativeGas += resourceReceipt.getEnergyUsageTotal(); + cumulativeLogCount += info.getLogCount(); + } + } + return null; + } + + /** + * Get all transaction receipts for a specific block + * @param blockNumOrHashOrTag blockNumber or blockHash or tag, + * tag includes: latest, earliest, pending, finalized + * @return List of TransactionReceipt objects for all transactions in the block, + * null if block not found + * @throws JsonRpcInvalidParamsException if the parameter format is invalid + * @throws JsonRpcInternalException if there's an internal error + */ + @Override + public List getBlockReceipts(String blockNumOrHashOrTag) + throws JsonRpcInvalidParamsException, JsonRpcInternalException { + + Block block = null; + + if (Pattern.matches(HASH_REGEX, blockNumOrHashOrTag)) { + block = getBlockByJsonHash(blockNumOrHashOrTag); + } else { + block = wallet.getByJsonBlockId(blockNumOrHashOrTag); + } + + // block receipts not available: block is genesis, not produced yet, or pruned in light node + if (block == null || block.getBlockHeader().getRawData().getNumber() == 0) { + return null; + } + + BlockCapsule blockCapsule = new BlockCapsule(block); + long blockNum = blockCapsule.getNum(); + TransactionInfoList transactionInfoList = wallet.getTransactionInfoByBlockNum(blockNum); + + // energy price at the block timestamp + long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp()); + + // Validate transaction list size consistency + int transactionSizeInBlock = blockCapsule.getTransactions().size(); + if (transactionSizeInBlock != transactionInfoList.getTransactionInfoCount()) { + throw new JsonRpcInternalException( + String.format("TransactionList size mismatch: " + + "block has %d transactions, but transactionInfoList has %d", + transactionSizeInBlock, transactionInfoList.getTransactionInfoCount())); + } + + return getTransactionReceiptsFromBlock(blockCapsule, transactionInfoList, energyFee); + } + + /** + * Get all TransactionReceipts from a block + * This method processes all transactions in the block + * and creates receipts with cumulative gas calculations + * @param blockCapsule the block containing transactions + * @param transactionInfoList the transaction info list for the block + * @param energyFee the energy price at the block timestamp + * @return List of TransactionReceipt objects for all transactions in the block + */ + private List getTransactionReceiptsFromBlock(BlockCapsule blockCapsule, + TransactionInfoList transactionInfoList, long energyFee) { + + List receipts = new ArrayList<>(); + long cumulativeGas = 0; + long cumulativeLogCount = 0; + + for (int index = 0; index < transactionInfoList.getTransactionInfoCount(); index++) { + TransactionInfo info = transactionInfoList.getTransactionInfo(index); + ResourceReceipt resourceReceipt = info.getReceipt(); + + TransactionReceipt.TransactionContext context = new TransactionContext( + index, cumulativeGas, cumulativeLogCount); + + // Use the constructor with pre-calculated context + TransactionReceipt receipt = new TransactionReceipt(blockCapsule, info, context, energyFee); + receipts.add(receipt); + + cumulativeGas += resourceReceipt.getEnergyUsageTotal(); + cumulativeLogCount += info.getLogCount(); + } + return receipts; } @Override @@ -1256,7 +1430,8 @@ public String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException, } @Override - public String newBlockFilter() throws JsonRpcMethodNotFoundException { + public String newBlockFilter() throws JsonRpcMethodNotFoundException, + JsonRpcExceedLimitException { disableInPBFT("eth_newBlockFilter"); Map blockFilter2Result; @@ -1265,6 +1440,10 @@ public String newBlockFilter() throws JsonRpcMethodNotFoundException { } else { blockFilter2Result = blockFilter2ResultSolidity; } + if (blockFilter2Result.size() >= maxBlockFilterNum) { + throw new JsonRpcExceedLimitException( + "exceed max block filters: " + maxBlockFilterNum + ", try again later"); + } BlockFilterAndResult filterAndResult = new BlockFilterAndResult(); String filterID = generateFilterId(); @@ -1394,6 +1573,8 @@ public static Object[] getFilterResult(String filterId, Map>> bitSetList = new ArrayList<>(); - + // 1. Collect all unique bitIndexes + Set uniqueBitIndexes = new HashSet<>(); for (int[] index : bitIndexes) { - List> futureList = new ArrayList<>(); - for (final int bitIndex : index) { //must be 3 - Future bitSetFuture = - sectionExecutor.submit(() -> sectionBloomStore.get(section, bitIndex)); - futureList.add(bitSetFuture); + for (int bitIndex : index) { //normally 3, but could be less due to hash collisions + uniqueBitIndexes.add(bitIndex); + } + } + + // 2. Submit concurrent requests for all unique bitIndexes + Map> bitIndexResults = new HashMap<>(); + for (int bitIndex : uniqueBitIndexes) { + Future future + = sectionExecutor.submit(() -> sectionBloomStore.get(section, bitIndex)); + bitIndexResults.put(bitIndex, future); + } + + // 3. Wait for all results and cache them + Map resultCache = new HashMap<>(); + for (Map.Entry> entry : bitIndexResults.entrySet()) { + BitSet result = entry.getValue().get(); + if (result != null) { + resultCache.put(entry.getKey(), result); } - bitSetList.add(futureList); } - BitSet bitSet = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); - for (List> futureList : bitSetList) { - // initial a BitSet with all 1 - BitSet subBitSet = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); - subBitSet.set(0, SectionBloomStore.BLOCK_PER_SECTION); + // 4. Process valid groups with reused BitSet objects + BitSet finalResult = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); + BitSet tempBitSet = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); + + for (int[] index : bitIndexes) { + + // init tempBitSet with all 1 + tempBitSet.set(0, SectionBloomStore.BLOCK_PER_SECTION); + // and condition in second dimension - for (Future future : futureList) { - BitSet one = future.get(); - if (one == null) { //match nothing - subBitSet.clear(); + for (int bitIndex : index) { + BitSet cached = resultCache.get(bitIndex); + if (cached == null) { //match nothing + tempBitSet.clear(); break; } // "and" condition in second dimension - subBitSet.and(one); + tempBitSet.and(cached); + if (tempBitSet.isEmpty()) { + break; + } } + // "or" condition in first dimension - bitSet.or(subBitSet); + if (!tempBitSet.isEmpty()) { + finalResult.or(tempBitSet); + } } - return bitSet; + + return finalResult; } /** diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java index ce315e506d2..42bc123d4bc 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java @@ -14,7 +14,7 @@ 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.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.protos.Protocol.TransactionInfo.Log; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java index 3b893aec4cf..57739819d1e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java @@ -5,7 +5,7 @@ import java.util.concurrent.LinkedBlockingQueue; import lombok.Getter; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java index c0cd1ff12df..97a012b7f9a 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java @@ -8,7 +8,7 @@ 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.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.JsonRpcApiUtil; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.protos.Protocol.Block; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java index d96aa07f9a4..cf958d1e2cb 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java @@ -10,7 +10,7 @@ import org.tron.core.db.Manager; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; import org.tron.protos.Protocol.TransactionInfo; import org.tron.protos.Protocol.TransactionInfo.Log; @@ -83,17 +83,13 @@ public LogFilterElement[] matchBlockOneByOne() List logFilterElementList = new ArrayList<>(); for (long blockNum : blockNumList) { - TransactionRetCapsule transactionRetCapsule = - manager.getTransactionRetStore() - .getTransactionInfoByBlockNum(ByteArray.fromLong(blockNum)); - if (transactionRetCapsule == null) { - //if query condition (address and topics) is empty, we will traversal every block, - //include empty block + List transactionInfoList = + manager.getTransactionInfoByBlockNum(blockNum).getTransactionInfoList(); + //if query condition (address and topics) is empty, we will traversal every block, + //include empty block + if (transactionInfoList.isEmpty()) { continue; } - TransactionRet transactionRet = transactionRetCapsule.getInstance(); - List transactionInfoList = transactionRet.getTransactioninfoList(); - String blockHash = manager.getChainBaseManager().getBlockIdByNum(blockNum).toString(); List matchedLog = matchBlock(logFilterWrapper.getLogFilter(), blockNum, blockHash, transactionInfoList, false); diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java index 223e807e622..490219a13d9 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java @@ -14,8 +14,8 @@ import org.apache.commons.lang3.StringUtils; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.SmartContractOuterClass.SmartContract; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java index 1485448c4b6..70edd1ad94f 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java @@ -13,8 +13,8 @@ import org.apache.commons.lang3.StringUtils; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.SmartContractOuterClass.SmartContract; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionReceipt.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionReceipt.java index 81b7c763cca..fd57ec0d9ad 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionReceipt.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionReceipt.java @@ -9,179 +9,151 @@ import java.util.List; import lombok.Getter; import lombok.Setter; -import org.tron.api.GrpcAPI.TransactionInfoList; import org.tron.common.utils.ByteArray; -import org.tron.core.Wallet; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.TransactionCapsule; import org.tron.protos.Protocol; -import org.tron.protos.Protocol.ResourceReceipt; import org.tron.protos.Protocol.Transaction.Contract; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.Protocol.TransactionInfo; +@Getter +@Setter @JsonPropertyOrder(alphabetic = true) public class TransactionReceipt { - @JsonPropertyOrder(alphabetic = true) + @Getter + @Setter public static class TransactionLog { - @Getter - @Setter private String logIndex; - @Getter - @Setter private String blockHash; - @Getter - @Setter private String blockNumber; - @Getter - @Setter private String transactionIndex; - @Getter - @Setter private String transactionHash; - @Getter - @Setter private String address; - @Getter - @Setter private String data; - @Getter - @Setter private String[] topics; - @Getter - @Setter private boolean removed = false; - public TransactionLog() { - } + public TransactionLog() {} } - @Getter - @Setter private String blockHash; - @Getter - @Setter private String blockNumber; - @Getter - @Setter private String transactionIndex; - @Getter - @Setter private String transactionHash; - @Getter - @Setter private String from; - @Getter - @Setter private String to; - @Getter - @Setter private String cumulativeGasUsed; - @Getter - @Setter private String effectiveGasPrice; - @Getter - @Setter private String gasUsed; - @Getter - @Setter private String contractAddress; - @Getter - @Setter private TransactionLog[] logs; - @Getter - @Setter - private String logsBloom; - @JsonInclude(JsonInclude.Include.NON_NULL) - public String root; // 32 bytes of post-transaction stateroot (pre Byzantium) - @JsonInclude(JsonInclude.Include.NON_NULL) - public String status; // either 1 (success) or 0 (failure) (post Byzantium) + private String logsBloom = ByteArray.toJsonHex(new byte[256]); // default no value; - @Getter - @Setter - private String type = "0x0"; - - public TransactionReceipt(Protocol.Block block, TransactionInfo txInfo, Wallet wallet) { - BlockCapsule blockCapsule = new BlockCapsule(block); - String txid = ByteArray.toHexString(txInfo.getId().toByteArray()); - long blockNum = blockCapsule.getNum(); - - Protocol.Transaction transaction = null; - long cumulativeGas = 0; - long cumulativeLogCount = 0; - - TransactionInfoList infoList = wallet.getTransactionInfoByBlockNum(blockNum); - for (int index = 0; index < infoList.getTransactionInfoCount(); index++) { - TransactionInfo info = infoList.getTransactionInfo(index); - ResourceReceipt resourceReceipt = info.getReceipt(); - - long energyUsage = resourceReceipt.getEnergyUsageTotal(); - cumulativeGas += energyUsage; - - if (ByteArray.toHexString(info.getId().toByteArray()).equals(txid)) { - transactionIndex = ByteArray.toJsonHex(index); - cumulativeGasUsed = ByteArray.toJsonHex(cumulativeGas); - gasUsed = ByteArray.toJsonHex(energyUsage); - status = resourceReceipt.getResultValue() <= 1 ? "0x1" : "0x0"; - - transaction = block.getTransactions(index); - break; - } else { - cumulativeLogCount += info.getLogCount(); - } - } - - blockHash = ByteArray.toJsonHex(blockCapsule.getBlockId().getBytes()); - blockNumber = ByteArray.toJsonHex(blockCapsule.getNum()); - transactionHash = ByteArray.toJsonHex(txInfo.getId().toByteArray()); - effectiveGasPrice = ByteArray.toJsonHex(wallet.getEnergyFee(blockCapsule.getTimeStamp())); - - from = null; - to = null; - contractAddress = null; + @JsonInclude(JsonInclude.Include.NON_NULL) + private String root = null; // 32 bytes of post-transaction stateroot (pre Byzantium) - if (transaction != null && !transaction.getRawData().getContractList().isEmpty()) { + @JsonInclude(JsonInclude.Include.NON_NULL) + private String status; // either 1 (success) or 0 (failure) (post Byzantium) + + private String type = "0x0"; // legacy transaction, set 0 in java-tron + + /** + * Constructor for creating a TransactionReceipt + * + * @param blockCapsule the block containing the transaction + * @param txInfo the transaction info containing execution details + * @param context the pre-calculated transaction context + * @param energyFee the energy price at the block timestamp + */ + public TransactionReceipt( + BlockCapsule blockCapsule, + TransactionInfo txInfo, + TransactionContext context, + long energyFee) { + // Set basic fields + this.blockHash = ByteArray.toJsonHex(blockCapsule.getBlockId().getBytes()); + this.blockNumber = ByteArray.toJsonHex(blockCapsule.getNum()); + this.transactionHash = ByteArray.toJsonHex(txInfo.getId().toByteArray()); + this.transactionIndex = ByteArray.toJsonHex(context.index); + // Compute cumulative gas until this transaction + this.cumulativeGasUsed = + ByteArray.toJsonHex(context.cumulativeGas + txInfo.getReceipt().getEnergyUsageTotal()); + this.gasUsed = ByteArray.toJsonHex(txInfo.getReceipt().getEnergyUsageTotal()); + this.status = txInfo.getReceipt().getResultValue() <= 1 ? "0x1" : "0x0"; + this.effectiveGasPrice = ByteArray.toJsonHex(energyFee); + + // Set contract fields + this.from = null; + this.to = null; + this.contractAddress = null; + + TransactionCapsule txCapsule = blockCapsule.getTransactions().get(context.index); + Protocol.Transaction transaction = txCapsule.getInstance(); + if (!transaction.getRawData().getContractList().isEmpty()) { Contract contract = transaction.getRawData().getContract(0); byte[] fromByte = TransactionCapsule.getOwner(contract); byte[] toByte = getToAddress(transaction); - from = ByteArray.toJsonHexAddress(fromByte); - to = ByteArray.toJsonHexAddress(toByte); + this.from = ByteArray.toJsonHexAddress(fromByte); + this.to = ByteArray.toJsonHexAddress(toByte); if (contract.getType() == ContractType.CreateSmartContract) { - contractAddress = ByteArray.toJsonHexAddress(txInfo.getContractAddress().toByteArray()); + this.contractAddress = + ByteArray.toJsonHexAddress(txInfo.getContractAddress().toByteArray()); } } - // logs + // Set logs List logList = new ArrayList<>(); - for (int index = 0; index < txInfo.getLogCount(); index++) { - TransactionInfo.Log log = txInfo.getLogList().get(index); - - TransactionReceipt.TransactionLog transactionLog = new TransactionReceipt.TransactionLog(); - // index is the index in the block - transactionLog.logIndex = ByteArray.toJsonHex(index + cumulativeLogCount); - transactionLog.transactionHash = transactionHash; - transactionLog.transactionIndex = transactionIndex; - transactionLog.blockHash = blockHash; - transactionLog.blockNumber = blockNumber; + for (int logIndex = 0; logIndex < txInfo.getLogCount(); logIndex++) { + TransactionInfo.Log log = txInfo.getLogList().get(logIndex); + TransactionLog transactionLog = new TransactionLog(); + transactionLog.setLogIndex(ByteArray.toJsonHex(logIndex + context.cumulativeLogCount)); + transactionLog.setTransactionHash(this.transactionHash); + transactionLog.setTransactionIndex(this.transactionIndex); + transactionLog.setBlockHash(this.blockHash); + transactionLog.setBlockNumber(this.blockNumber); + byte[] addressByte = convertToTronAddress(log.getAddress().toByteArray()); - transactionLog.address = ByteArray.toJsonHexAddress(addressByte); - transactionLog.data = ByteArray.toJsonHex(log.getData().toByteArray()); + transactionLog.setAddress(ByteArray.toJsonHexAddress(addressByte)); + transactionLog.setData(ByteArray.toJsonHex(log.getData().toByteArray())); String[] topics = new String[log.getTopicsCount()]; for (int i = 0; i < log.getTopicsCount(); i++) { topics[i] = ByteArray.toJsonHex(log.getTopics(i).toByteArray()); } - transactionLog.topics = topics; + transactionLog.setTopics(topics); logList.add(transactionLog); } + this.logs = logList.toArray(new TransactionLog[0]); + + } - logs = logList.toArray(new TransactionReceipt.TransactionLog[logList.size()]); - logsBloom = ByteArray.toJsonHex(new byte[256]); // no value - root = null; + /** + * Context class to hold transaction creation parameters Contains index and cumulative values + * needed for receipt creation + */ + @Getter + public static class TransactionContext { + private final int index; + private final long cumulativeGas; + private final long cumulativeLogCount; + + /** + * Creates a transaction context with the given parameters + * + * @param index the transaction index within the block + * @param cumulativeGas the cumulative gas used up to this transaction + * @param cumulativeLogCount the cumulative log count up to this transaction + */ + public TransactionContext(int index, long cumulativeGas, long cumulativeLogCount) { + this.index = index; + this.cumulativeGas = cumulativeGas; + this.cumulativeLogCount = cumulativeLogCount; + } } -} \ No newline at end of file +} diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java index 389c58505cd..57650355d46 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java @@ -5,9 +5,9 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.google.protobuf.ByteString; -import java.util.Arrays; import lombok.Getter; import lombok.ToString; +import org.tron.common.crypto.Rsv; import org.tron.common.utils.ByteArray; import org.tron.core.Wallet; import org.tron.core.capsule.BlockCapsule; @@ -65,16 +65,10 @@ private void parseSignature(Transaction tx) { } ByteString signature = tx.getSignature(0); // r[32] + s[32] + v[1] - byte[] signData = signature.toByteArray(); - byte[] rByte = Arrays.copyOfRange(signData, 0, 32); - byte[] sByte = Arrays.copyOfRange(signData, 32, 64); - byte vByte = signData[64]; - if (vByte < 27) { - vByte += 27; - } - v = ByteArray.toJsonHex(vByte); - r = ByteArray.toJsonHex(rByte); - s = ByteArray.toJsonHex(sByte); + Rsv rsv = Rsv.fromSignature(signature.toByteArray()); + r = ByteArray.toJsonHex(rsv.getR()); + s = ByteArray.toJsonHex(rsv.getS()); + v = ByteArray.toJsonHex(rsv.getV()); } private String parseInput(Transaction tx) { diff --git a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java index a1f812426f7..dfc4b428836 100644 --- a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java +++ b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java @@ -3,6 +3,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; @@ -16,34 +17,43 @@ @Component public class ZksnarkInitService { + private static final AtomicBoolean initialized = new AtomicBoolean(false); + @PostConstruct private void init() { librustzcashInitZksnarkParams(); } public static void librustzcashInitZksnarkParams() { - logger.info("init zk param begin"); - - if (!JLibrustzcash.isOpenZen()) { - logger.info("zen switch is off, zen will not start."); + if (initialized.get()) { + logger.info("zk param already initialized"); return; } - String spendPath = getParamsFile("sapling-spend.params"); - String spendHash = "25fd9a0d1c1be0526c14662947ae95b758fe9f3d7fb7f55e9b4437830dcc6215a7ce3ea465" - + "914b157715b7a4d681389ea4aa84438190e185d5e4c93574d3a19a"; + synchronized (ZksnarkInitService.class) { + if (initialized.get()) { + logger.info("zk param already initialized"); + return; + } + logger.info("init zk param begin"); - String outputPath = getParamsFile("sapling-output.params"); - String outputHash = "a1cb23b93256adce5bce2cb09cefbc96a1d16572675ceb691e9a3626ec15b5b546926ff1c" - + "536cfe3a9df07d796b32fdfc3e5d99d65567257bf286cd2858d71a6"; + String spendPath = getParamsFile("sapling-spend.params"); + String spendHash = "25fd9a0d1c1be0526c14662947ae95b758fe9f3d7fb7f55e9b4437830dcc6215a7ce3ea" + + "465914b157715b7a4d681389ea4aa84438190e185d5e4c93574d3a19a"; - try { - JLibrustzcash.librustzcashInitZksnarkParams( - new LibrustzcashParam.InitZksnarkParams(spendPath, spendHash, outputPath, outputHash)); - } catch (ZksnarkException e) { - throw new TronError(e, TronError.ErrCode.ZCASH_INIT); + String outputPath = getParamsFile("sapling-output.params"); + String outputHash = "a1cb23b93256adce5bce2cb09cefbc96a1d16572675ceb691e9a3626ec15b5b546926f" + + "f1c536cfe3a9df07d796b32fdfc3e5d99d65567257bf286cd2858d71a6"; + + try { + JLibrustzcash.librustzcashInitZksnarkParams( + new LibrustzcashParam.InitZksnarkParams(spendPath, spendHash, outputPath, outputHash)); + } catch (ZksnarkException e) { + throw new TronError(e, TronError.ErrCode.ZCASH_INIT); + } + initialized.set(true); + logger.info("init zk param done"); } - logger.info("init zk param done"); } private static String getParamsFile(String fileName) { @@ -61,4 +71,4 @@ private static String getParamsFile(String fileName) { } return fileOut.getAbsolutePath(); } -} \ No newline at end of file +} diff --git a/framework/src/main/java/org/tron/program/DBConvert.java b/framework/src/main/java/org/tron/program/DBConvert.java deleted file mode 100644 index 7b9d63544dc..00000000000 --- a/framework/src/main/java/org/tron/program/DBConvert.java +++ /dev/null @@ -1,413 +0,0 @@ -package org.tron.program; - -import static org.fusesource.leveldbjni.JniDBFactory.factory; -import static org.tron.common.math.Maths.max; - -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import lombok.extern.slf4j.Slf4j; -import org.fusesource.leveldbjni.JniDBFactory; -import org.iq80.leveldb.CompressionType; -import org.iq80.leveldb.DB; -import org.iq80.leveldb.DBIterator; -import org.rocksdb.BlockBasedTableConfig; -import org.rocksdb.BloomFilter; -import org.rocksdb.ComparatorOptions; -import org.rocksdb.Options; -import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; -import org.rocksdb.RocksIterator; -import org.rocksdb.Status; -import org.tron.common.utils.FileUtil; -import org.tron.common.utils.MarketOrderPriceComparatorForLevelDB; -import org.tron.common.utils.MarketOrderPriceComparatorForRockDB; -import org.tron.common.utils.PropUtil; - -@Slf4j -public class DBConvert implements Callable { - - static { - RocksDB.loadLibrary(); - } - - private final String srcDir; - private final String dstDir; - private final String dbName; - private final Path srcDbPath; - private final Path dstDbPath; - - private long srcDbKeyCount = 0L; - private long dstDbKeyCount = 0L; - private long srcDbKeySum = 0L; - private long dstDbKeySum = 0L; - private long srcDbValueSum = 0L; - private long dstDbValueSum = 0L; - private final long startTime; - private static final int CPUS = Runtime.getRuntime().availableProcessors(); - private static final int BATCH = 256; - private static final String CHECKPOINT_V2_DIR_NAME = "checkpoint"; - - - @Override - public Boolean call() throws Exception { - return doConvert(); - } - - public DBConvert(String src, String dst, String name) { - this.srcDir = src; - this.dstDir = dst; - this.dbName = name; - this.srcDbPath = Paths.get(this.srcDir, name); - this.dstDbPath = Paths.get(this.dstDir, name); - this.startTime = System.currentTimeMillis(); - } - - public static org.iq80.leveldb.Options newDefaultLevelDbOptions() { - org.iq80.leveldb.Options dbOptions = new org.iq80.leveldb.Options(); - dbOptions.createIfMissing(true); - dbOptions.paranoidChecks(true); - dbOptions.verifyChecksums(true); - dbOptions.compressionType(CompressionType.SNAPPY); - dbOptions.blockSize(4 * 1024); - dbOptions.writeBufferSize(10 * 1024 * 1024); - dbOptions.cacheSize(10 * 1024 * 1024L); - dbOptions.maxOpenFiles(1000); - return dbOptions; - } - - public static void main(String[] args) { - int code = run(args); - logger.info("exit code {}.", code); - System.out.printf("exit code %d.\n", code); - System.exit(code); - } - - public static int run(String[] args) { - String dbSrc; - String dbDst; - if (args.length < 2) { - dbSrc = "output-directory/database"; - dbDst = "output-directory-dst/database"; - } else { - dbSrc = args[0]; - dbDst = args[1]; - } - File dbDirectory = new File(dbSrc); - if (!dbDirectory.exists()) { - logger.info(" {} does not exist.", dbSrc); - return 404; - } - List files = Arrays.stream(Objects.requireNonNull(dbDirectory.listFiles())) - .filter(File::isDirectory) - .filter(e -> !CHECKPOINT_V2_DIR_NAME.equals(e.getName())) - .collect(Collectors.toList()); - - // add checkpoint v2 convert - File cpV2Dir = new File(Paths.get(dbSrc, CHECKPOINT_V2_DIR_NAME).toString()); - List cpList = null; - if (cpV2Dir.exists()) { - cpList = Arrays.stream(Objects.requireNonNull(cpV2Dir.listFiles())) - .filter(File::isDirectory) - .collect(Collectors.toList()); - } - - if (files.isEmpty()) { - logger.info("{} does not contain any database.", dbSrc); - return 0; - } - final long time = System.currentTimeMillis(); - final List> res = new ArrayList<>(); - - final ThreadPoolExecutor esDb = new ThreadPoolExecutor( - CPUS, 16 * CPUS, 1, TimeUnit.MINUTES, - new ArrayBlockingQueue<>(CPUS, true), Executors.defaultThreadFactory(), - new ThreadPoolExecutor.CallerRunsPolicy()); - - esDb.allowCoreThreadTimeOut(true); - - files.forEach(f -> res.add(esDb.submit(new DBConvert(dbSrc, dbDst, f.getName())))); - // convert v2 - if (cpList != null) { - cpList.forEach(f -> res.add(esDb.submit( - new DBConvert(dbSrc + "/" + CHECKPOINT_V2_DIR_NAME, - dbDst + "/" + CHECKPOINT_V2_DIR_NAME, f.getName())))); - } - - int fails = res.size(); - - for (Future re : res) { - try { - if (re.get()) { - fails--; - } - } catch (InterruptedException e) { - logger.error("{}", e); - Thread.currentThread().interrupt(); - } catch (ExecutionException e) { - logger.error("{}", e); - } - } - - esDb.shutdown(); - logger.info("database convert use {} seconds total.", - (System.currentTimeMillis() - time) / 1000); - if (fails > 0) { - logger.error("failed!!!!!!!!!!!!!!!!!!!!!!!! size:{}", fails); - } - return fails; - } - - public DB newLevelDb(Path db) throws Exception { - DB database; - File file = db.toFile(); - org.iq80.leveldb.Options dbOptions = newDefaultLevelDbOptions(); - if ("market_pair_price_to_order".equalsIgnoreCase(this.dbName)) { - dbOptions.comparator(new MarketOrderPriceComparatorForLevelDB()); - } - database = factory.open(file, dbOptions); - return database; - } - - private Options newDefaultRocksDbOptions() { - Options options = new Options(); - options.setCreateIfMissing(true); - options.setIncreaseParallelism(1); - options.setNumLevels(7); - options.setMaxOpenFiles(5000); - options.setTargetFileSizeBase(64 * 1024 * 1024); - options.setTargetFileSizeMultiplier(1); - options.setMaxBytesForLevelBase(512 * 1024 * 1024); - options.setMaxBackgroundCompactions(max(1, Runtime.getRuntime().availableProcessors(), true)); - options.setLevel0FileNumCompactionTrigger(4); - options.setLevelCompactionDynamicLevelBytes(true); - if ("market_pair_price_to_order".equalsIgnoreCase(this.dbName)) { - options.setComparator(new MarketOrderPriceComparatorForRockDB(new ComparatorOptions())); - } - final BlockBasedTableConfig tableCfg; - options.setTableFormatConfig(tableCfg = new BlockBasedTableConfig()); - tableCfg.setBlockSize(64 * 1024); - tableCfg.setBlockCacheSize(32 * 1024 * 1024); - tableCfg.setCacheIndexAndFilterBlocks(true); - tableCfg.setPinL0FilterAndIndexBlocksInCache(true); - tableCfg.setFilter(new BloomFilter(10, false)); - options.prepareForBulkLoad(); - return options; - } - - public RocksDB newRocksDb(Path db) { - RocksDB database = null; - try (Options options = newDefaultRocksDbOptions()) { - database = RocksDB.open(options, db.toString()); - } catch (Exception e) { - logger.error("{}", e); - } - return database; - } - - private void batchInsert(RocksDB rocks, List keys, List values) - throws Exception { - try (org.rocksdb.WriteBatch batch = new org.rocksdb.WriteBatch()) { - for (int i = 0; i < keys.size(); i++) { - byte[] k = keys.get(i); - byte[] v = values.get(i); - batch.put(k, v); - } - write(rocks, batch); - } - keys.clear(); - values.clear(); - } - - /** - * https://github.com/facebook/rocksdb/issues/6625 - * @param rocks db - * @param batch write batch - * @throws Exception RocksDBException - */ - private void write(RocksDB rocks, org.rocksdb.WriteBatch batch) throws Exception { - try { - rocks.write(new org.rocksdb.WriteOptions(), batch); - } catch (RocksDBException e) { - // retry - if (maybeRetry(e)) { - TimeUnit.MILLISECONDS.sleep(1); - write(rocks, batch); - } else { - throw e; - } - } - } - - private boolean maybeRetry(RocksDBException e) { - boolean retry = false; - if (e.getStatus() != null) { - retry = e.getStatus().getCode() == Status.Code.TryAgain - || e.getStatus().getCode() == Status.Code.Busy - || e.getStatus().getCode() == Status.Code.Incomplete; - } - return retry || (e.getMessage() != null && ("Write stall".equalsIgnoreCase(e.getMessage()) - || ("Incomplete").equalsIgnoreCase(e.getMessage()))); - } - - /** - * https://github.com/facebook/rocksdb/wiki/RocksDB-FAQ . - * What's the fastest way to load data into RocksDB? - * @param level leveldb - * @param rocks rocksdb - * @return if ok - */ - public boolean convertLevelToRocksBatchIterator(DB level, RocksDB rocks) { - // convert - List keys = new ArrayList<>(BATCH); - List values = new ArrayList<>(BATCH); - try (DBIterator levelIterator = level.iterator( - new org.iq80.leveldb.ReadOptions().fillCache(false))) { - - JniDBFactory.pushMemoryPool(1024 * 1024); - levelIterator.seekToFirst(); - - while (levelIterator.hasNext()) { - Map.Entry entry = levelIterator.next(); - byte[] key = entry.getKey(); - byte[] value = entry.getValue(); - srcDbKeyCount++; - srcDbKeySum = byteArrayToIntWithOne(srcDbKeySum, key); - srcDbValueSum = byteArrayToIntWithOne(srcDbValueSum, value); - keys.add(key); - values.add(value); - if (keys.size() >= BATCH) { - try { - batchInsert(rocks, keys, values); - } catch (Exception e) { - logger.error("{}", e); - return false; - } - } - } - - if (!keys.isEmpty()) { - try { - batchInsert(rocks, keys, values); - } catch (Exception e) { - logger.error("{}", e); - return false; - } - } - // check - check(rocks); - } catch (Exception e) { - logger.error("{}", e); - return false; - } finally { - try { - level.close(); - rocks.close(); - JniDBFactory.popMemoryPool(); - } catch (Exception e1) { - logger.error("{}", e1); - } - } - return dstDbKeyCount == srcDbKeyCount && dstDbKeySum == srcDbKeySum - && dstDbValueSum == srcDbValueSum; - } - - private void check(RocksDB rocks) throws RocksDBException { - logger.info("check database {} start", this.dbName); - // manually call CompactRange() - logger.info("compact database {} start", this.dbName); - rocks.compactRange(); - logger.info("compact database {} end", this.dbName); - // check - try (org.rocksdb.ReadOptions r = new org.rocksdb.ReadOptions().setFillCache(false); - RocksIterator rocksIterator = rocks.newIterator(r)) { - for (rocksIterator.seekToFirst(); rocksIterator.isValid(); rocksIterator.next()) { - byte[] key = rocksIterator.key(); - byte[] value = rocksIterator.value(); - dstDbKeyCount++; - dstDbKeySum = byteArrayToIntWithOne(dstDbKeySum, key); - dstDbValueSum = byteArrayToIntWithOne(dstDbValueSum, value); - } - } - logger.info("check database {} end", this.dbName); - } - - public boolean createEngine(String dir) { - String enginePath = dir + File.separator + "engine.properties"; - - if (!FileUtil.createFileIfNotExists(enginePath)) { - return false; - } - - return PropUtil.writeProperty(enginePath, "ENGINE", "ROCKSDB"); - } - - public boolean checkDone(String dir) { - String enginePath = dir + File.separator + "engine.properties"; - return FileUtil.isExists(enginePath); - - } - - public boolean doConvert() throws Exception { - - if (checkDone(this.dstDbPath.toString())) { - logger.info(" {} is done, skip it.", this.dbName); - return true; - } - - File levelDbFile = srcDbPath.toFile(); - if (!levelDbFile.exists()) { - logger.info(" {} does not exist.", srcDbPath.toString()); - return false; - } - - DB level = newLevelDb(srcDbPath); - - if (this.dstDbPath.toFile().exists()) { - logger.info(" {} begin to clear exist database directory", this.dbName); - FileUtil.deleteDir(this.dstDbPath.toFile()); - logger.info(" {} clear exist database directory done.", this.dbName); - } - - FileUtil.createDirIfNotExists(dstDir); - RocksDB rocks = newRocksDb(dstDbPath); - - logger.info("Convert database {} start", this.dbName); - boolean result = convertLevelToRocksBatchIterator(level, rocks) - && createEngine(dstDbPath.toString()); - long etime = System.currentTimeMillis(); - - if (result) { - logger.info("Convert database {} successful end with {} key-value {} minutes", - this.dbName, this.srcDbKeyCount, (etime - this.startTime) / 1000.0 / 60); - } else { - logger.info("Convert database {} failure", this.dbName); - if (this.dstDbPath.toFile().exists()) { - logger.info(" {} begin to clear exist database directory", this.dbName); - FileUtil.deleteDir(this.dstDbPath.toFile()); - logger.info(" {} clear exist database directory done.", this.dbName); - } - } - return result; - } - - public long byteArrayToIntWithOne(long sum, byte[] b) { - for (byte oneByte : b) { - sum += oneByte; - } - return sum; - } -} \ No newline at end of file diff --git a/framework/src/main/java/org/tron/program/FullNode.java b/framework/src/main/java/org/tron/program/FullNode.java index ef23d20ce52..9f2f497a579 100644 --- a/framework/src/main/java/org/tron/program/FullNode.java +++ b/framework/src/main/java/org/tron/program/FullNode.java @@ -1,6 +1,5 @@ package org.tron.program; -import com.beust.jcommander.JCommander; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.tron.common.application.Application; @@ -22,19 +21,20 @@ public class FullNode { */ public static void main(String[] args) { ExitManager.initExceptionHandler(); - logger.info("Full node running."); Args.setParam(args, Constant.TESTNET_CONF); CommonParameter parameter = Args.getInstance(); LogService.load(parameter.getLogbackPath()); - if (parameter.isHelp()) { - JCommander jCommander = JCommander.newBuilder().addObject(Args.PARAMETER).build(); - jCommander.parse(args); - Args.printHelp(jCommander); + if (parameter.isSolidityNode()) { + SolidityNode.start(); return; } - + if (parameter.isKeystoreFactory()) { + KeystoreFactory.start(); + return; + } + logger.info("Full node running."); if (Args.getInstance().isDebug()) { logger.info("in debug mode, it won't check energy time"); } else { diff --git a/framework/src/main/java/org/tron/program/KeystoreFactory.java b/framework/src/main/java/org/tron/program/KeystoreFactory.java index bfd2df22856..8199d7e9076 100755 --- a/framework/src/main/java/org/tron/program/KeystoreFactory.java +++ b/framework/src/main/java/org/tron/program/KeystoreFactory.java @@ -1,6 +1,5 @@ package org.tron.program; -import com.beust.jcommander.JCommander; import java.io.File; import java.io.IOException; import java.util.Scanner; @@ -11,8 +10,6 @@ import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; import org.tron.common.utils.Utils; -import org.tron.core.Constant; -import org.tron.core.config.args.Args; import org.tron.core.exception.CipherException; import org.tron.keystore.Credentials; import org.tron.keystore.WalletUtils; @@ -22,15 +19,8 @@ public class KeystoreFactory { private static final String FilePath = "Wallet"; - public static void main(String[] args) { - Args.setParam(args, Constant.TESTNET_CONF); + public static void start() { KeystoreFactory cli = new KeystoreFactory(); - - JCommander.newBuilder() - .addObject(cli) - .build() - .parse(args); - cli.run(); } diff --git a/framework/src/main/java/org/tron/program/SolidityNode.java b/framework/src/main/java/org/tron/program/SolidityNode.java index b774ab03aaa..3367141e2a5 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -5,21 +5,17 @@ import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.atomic.AtomicLong; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.BooleanUtils; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.util.ObjectUtils; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; import org.tron.common.client.DatabaseGrpcClient; -import org.tron.common.exit.ExitManager; import org.tron.common.parameter.CommonParameter; import org.tron.common.prometheus.Metrics; import org.tron.core.ChainBaseManager; -import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; import org.tron.core.config.DefaultConfig; -import org.tron.core.config.args.Args; import org.tron.core.db.Manager; import org.tron.core.exception.TronError; import org.tron.protos.Protocol.Block; @@ -55,25 +51,12 @@ public SolidityNode(Manager dbManager) { /** * Start the SolidityNode. */ - public static void main(String[] args) { - ExitManager.initExceptionHandler(); + public static void start() { logger.info("Solidity node is running."); - Args.setParam(args, Constant.TESTNET_CONF); CommonParameter parameter = CommonParameter.getInstance(); - - logger.info("index switch is {}", - BooleanUtils.toStringOnOff(BooleanUtils - .toBoolean(parameter.getStorage().getIndexSwitch()))); - if (ObjectUtils.isEmpty(parameter.getTrustNodeAddr())) { - logger.error("Trust node is not set."); - return; - } - parameter.setSolidityNode(true); - - if (parameter.isHelp()) { - logger.info("Here is the help message."); - return; + throw new TronError(new IllegalArgumentException("Trust node is not set."), + TronError.ErrCode.SOLID_NODE_INIT); } // init metrics first Metrics.init(); @@ -88,11 +71,11 @@ public static void main(String[] args) { context.registerShutdownHook(); appT.startup(); SolidityNode node = new SolidityNode(appT.getDbManager()); - node.start(); + node.run(); appT.blockUntilShutdown(); } - private void start() { + private void run() { try { new Thread(this::getBlock).start(); new Thread(this::processBlock).start(); diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 6dde9a8e52c..af3bcd8e185 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -4,7 +4,7 @@ public class Version { public static final String VERSION_NAME = "GreatVoyage-v4.8.0-1-g45e3bf88ca"; public static final String VERSION_CODE = "18634"; - private static final String VERSION = "4.8.0.1"; + private static final String VERSION = "4.8.1"; public static String getVersion() { return VERSION; diff --git a/framework/src/main/resources/config-localtest.conf b/framework/src/main/resources/config-localtest.conf index 405a0f92b2d..bdd5ea14d3e 100644 --- a/framework/src/main/resources/config-localtest.conf +++ b/framework/src/main/resources/config-localtest.conf @@ -163,6 +163,7 @@ node { # httpPBFTPort = 8565 # maxBlockRange = 5000 # maxSubTopics = 1000 + # maxBlockFilterNum = 30000 } } diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index d434d9c7203..54f229e4e25 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -1,90 +1,80 @@ net { + # Type can be 'mainnet' or 'testnet', refers to address type. + # Hex address of 'mainnet' begin with 0x41, and 'testnet' begin with 0xa0. + # Note: 'testnet' is not related to TRON network Nile, Shasta or private net type = mainnet - # type = testnet } storage { # Directory for storing persistent data - db.engine = "LEVELDB", + db.engine = "LEVELDB", // deprecated for arm, because arm only support "ROCKSDB". db.sync = false, db.directory = "database", - index.directory = "index", - transHistory.switch = "on", - # You can custom these 14 databases' configs: - - # account, account-index, asset-issue, block, block-index, - # block_KDB, peers, properties, recent-block, trans, - # utxo, votes, witness, witness_schedule. - # Otherwise, db configs will remain default and data will be stored in - # the path of "output-directory" or which is set by "-d" ("--output-directory"). + # Whether to write transaction result in transactionRetStore + transHistory.switch = "on", - # setting can impove leveldb performance .... start + # setting can improve leveldb performance .... start, deprecated for arm # node: if this will increase process fds,you may be check your ulimit if 'too many open files' error occurs # see https://github.com/tronprotocol/tips/blob/master/tip-343.md for detail - # if you find block sync has lower performance,you can try this settings - #default = { + # if you find block sync has lower performance, you can try this settings + # default = { # maxOpenFiles = 100 - #} - #defaultM = { + # } + # defaultM = { # maxOpenFiles = 500 - #} - #defaultL = { + # } + # defaultL = { # maxOpenFiles = 1000 - #} - # setting can impove leveldb performance .... end + # } + # setting can improve leveldb performance .... end, deprecated for arm - # Attention: name is a required field that must be set !!! + # You can customize the configuration for each database. Otherwise, the database settings will use + # their defaults, and data will be stored in the "output-directory" or in the directory specified + # by the "-d" or "--output-directory" option. Attention: name is a required field that must be set! + # In this configuration, the name and path properties take effect for both LevelDB and RocksDB storage engines, + # while the additional properties (such as createIfMissing, paranoidChecks, compressionType, etc.) only take effect when using LevelDB. properties = [ - // { - // name = "account", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - // { - // name = "account-index", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, + # { + # name = "account", + # path = "storage_directory_test", + # createIfMissing = true, // deprecated for arm start + # paranoidChecks = true, + # verifyChecksums = true, + # compressionType = 1, // compressed with snappy + # blockSize = 4096, // 4 KB = 4 * 1024 B + # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # maxOpenFiles = 100 // deprecated for arm end + # }, + # { + # name = "account-index", + # path = "storage_directory_test", + # createIfMissing = true, + # paranoidChecks = true, + # verifyChecksums = true, + # compressionType = 1, // compressed with snappy + # blockSize = 4096, // 4 KB = 4 * 1024 B + # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # maxOpenFiles = 100 + # }, ] needToUpdateAsset = true - //dbsettings is needed when using rocksdb as the storage implement (db.engine="ROCKSDB"). - //we'd strongly recommend that do not modify it unless you know every item's meaning clearly. + # dbsettings is needed when using rocksdb as the storage implement (db.engine="ROCKSDB"). + # we'd strongly recommend that do not modify it unless you know every item's meaning clearly. dbSettings = { levelNumber = 7 - //compactThreads = 32 + # compactThreads = 32 blocksize = 64 // n * KB maxBytesForLevelBase = 256 // n * MB maxBytesForLevelMultiplier = 10 level0FileNumCompactionTrigger = 4 targetFileSizeBase = 256 // n * MB targetFileSizeMultiplier = 1 - } - - //backup settings when using rocks db as the storage implement (db.engine="ROCKSDB"). - //if you want to use the backup plugin, please confirm set the db.engine="ROCKSDB" above. - backup = { - enable = false // indicate whether enable the backup plugin - propPath = "prop.properties" // record which bak directory is valid - bak1path = "bak1/database" // you must set two backup directories to prevent application halt unexpected(e.g. kill -9). - bak2path = "bak2/database" - frequency = 10000 // indicate backup db once every 10000 blocks processed. + maxOpenFiles = 5000 } balance.history.lookup = false @@ -95,13 +85,16 @@ storage { # the estimated number of block transactions (default 1000, min 100, max 10000). # so the total number of cached transactions is 65536 * txCache.estimatedTransactions # txCache.estimatedTransactions = 1000 - # if true, transaction cache initialization will be faster. default false - # txCache.initOptimization = true - # data root setting, for check data, currently, only reward-vi is used. + # if true, transaction cache initialization will be faster. Default: false + txCache.initOptimization = true + + # The number of blocks flushed to db in each batch during node syncing. Default: 1 + # snapshot.maxFlushCount = 1 + # data root setting, for check data, currently, only reward-vi is used. # merkleRoot = { - # reward-vi = 9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8 // main-net, Sha256Hash, hexString + # reward-vi = 9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8 // main-net, Sha256Hash, hexString # } } @@ -119,13 +112,13 @@ node.discovery = { #} node.backup { + # udp listen port, each member should have the same configuration + port = 10001 + # my priority, each member should use different priority priority = 8 - # udp listen port, each member should have the save configuration - port = 10001 - - # time interval to send keepAlive message, each member should have the save configuration + # time interval to send keepAlive message, each member should have the same configuration keepAliveInterval = 3000 # peer's ip list, can't contain mine @@ -135,18 +128,27 @@ node.backup { ] } +# Specify the algorithm for generating a public key from private key. To avoid forks, please do not modify it crypto { engine = "eckey" } -# prometheus metrics start -# node.metrics = { -# prometheus{ -# enable=true -# port="9527" -# } -# } -# prometheus metrics end +node.metrics = { + # prometheus metrics + prometheus { + enable = false + port = 9527 + } + + # influxdb metrics + storageEnable = false # Whether write metrics data into InfluxDb. Default: false. + influxdb { + ip = "" + port = 8086 + database = "" + metricsReportInterval = 10 + } +} node { # trust node for solidity node @@ -161,17 +163,16 @@ node { connection.timeout = 2 fetchBlock.timeout = 200 + # syncFetchBatchNum = 2000 - tcpNettyWorkThreadNum = 0 - - udpNettyWorkThreadNum = 1 + # Number of validate sign thread, default availableProcessors + # validateSignThreadNum = 16 maxConnections = 30 + minConnections = 8 - minActiveConnections = 3 - # Number of validate sign thread, default availableProcessors / 2 - # validateSignThreadNum = 16 + minActiveConnections = 3 maxConnectionsWithSameIp = 2 @@ -179,11 +180,23 @@ node { minParticipationRate = 15 + # allowShieldedTransactionApi = true + + # openPrintLog = true + + # If set to true, SR packs transactions into a block in descending order of fee; otherwise, it packs + # them based on their receive timestamp. Default: false + # openTransactionSort = false + + # The threshold for the number of broadcast transactions received from each peer every second, + # transactions exceeding this threshold will be discarded + # maxTps = 1000 + isOpenFullTcpDisconnect = false inactiveThreshold = 600 //seconds p2p { - version = 11111 # 11111: mainnet; 20180622: testnet + version = 11111 # Mainnet:11111; Nile:201910292; Shasta:1 } active = [ @@ -214,19 +227,6 @@ node { PBFTPort = 8092 } - # use your ipv6 address for node discovery and tcp connection, default false - enableIpv6 = false - - # if your node's highest block num is below than all your pees', try to acquire new connection. default false - effectiveCheckEnable = false - - dns { - # dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty - treeUrls = [ - #"tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@main.trondisco.net", - ] - } - rpc { enable = true port = 50051 @@ -234,6 +234,7 @@ node { solidityPort = 50061 PBFTEnable = true PBFTPort = 50071 + # Number of gRPC thread, default availableProcessors / 2 # thread = 16 @@ -255,17 +256,23 @@ node { # The maximum size of header list allowed to be received, default 8192 # maxHeaderListSize = + # The number of RST_STREAM frames allowed to be sent per connection per period for grpc, by default there is no limit. + # maxRstStream = + + # The number of seconds per period for grpc + # secondsPerWindow = + # Transactions can only be broadcast if the number of effective connections is reached. minEffectiveConnection = 1 - # The switch of the reflection service, effective for all gRPC services - # reflectionService = true + # The switch of the reflection service, effective for all gRPC services, used for grpcurl tool. Default: false + reflectionService = false } # number of solidity thread in the FullNode. # If accessing solidity rpc and http interface timeout, could increase the number of threads, # The default value is the number of cpu cores of the machine. - #solidity.threads = 8 + # solidity.threads = 8 # Limits the maximum percentage (default 75%) of producing block interval # to provide sufficient time to perform other operations e.g. broadcast block @@ -274,52 +281,120 @@ node { # Limits the maximum number (default 700) of transaction from network layer # netMaxTrxPerSecond = 700 - # open the history query APIs(http&GRPC) when node is a lite fullNode, - # like {getBlockByNum, getBlockByID, getTransactionByID...}. - # default: false. + # Whether to enable the node detection function. Default: false + # nodeDetectEnable = false + + # use your ipv6 address for node discovery and tcp connection. Default: false + # enableIpv6 = false + + # if your node's highest block num is below than all your pees', try to acquire new connection. Default: false + # effectiveCheckEnable = false + + # Dynamic loading configuration function, disabled by default + dynamicConfig = { + # enable = false + # checkInterval = 600 // Check interval of Configuration file's change, default is 600 seconds + } + + # Whether to continue broadcast transactions after at least maxUnsolidifiedBlocks are not solidified. Default: false + # unsolidifiedBlockCheck = false + # maxUnsolidifiedBlocks = 54 + + dns { + # dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty + treeUrls = [ + #"tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@main.trondisco.net", + ] + + # enable or disable dns publish. Default: false + # publish = false + + # dns domain to publish nodes, required if publish is true + # dnsDomain = "nodes1.example.org" + + # dns private key used to publish, required if publish is true, hex string of length 64 + # dnsPrivate = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" + + # known dns urls to publish if publish is true, url format tree://{pubkey}@{domain}, default empty + # knownUrls = [ + #"tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes2.example.org", + # ] + + # staticNodes = [ + # static nodes to published on dns + # Sample entries: + # "ip:port", + # "ip:port" + # ] + + # merge several nodes into a leaf of tree, should be 1~5 + # maxMergeSize = 5 + + # only nodes change percent is bigger then the threshold, we update data on dns + # changeThreshold = 0.1 + + # dns server to publish, required if publish is true, only aws or aliyun is support + # serverType = "aws" + + # access key id of aws or aliyun api, required if publish is true, string + # accessKeyId = "your-key-id" + + # access key secret of aws or aliyun api, required if publish is true, string + # accessKeySecret = "your-key-secret" + + # if publish is true and serverType is aliyun, it's endpoint of aws dns server, string + # aliyunDnsEndpoint = "alidns.aliyuncs.com" + + # if publish is true and serverType is aws, it's region of aws api, such as "eu-south-1", string + # awsRegion = "us-east-1" + + # if publish is true and server-type is aws, it's host zone id of aws's domain, string + # awsHostZoneId = "your-host-zone-id" + } + + # open the history query APIs(http&GRPC) when node is a lite FullNode, + # like {getBlockByNum, getBlockByID, getTransactionByID...}. Default: false. # note: above APIs may return null even if blocks and transactions actually are on the blockchain - # when opening on a lite fullnode. only open it if the consequences being clearly known + # when opening on a lite FullNode. only open it if the consequences being clearly known # openHistoryQueryWhenLiteFN = false jsonrpc { - # Note: If you turn on jsonrpc and run it for a while and then turn it off, you will not - # be able to get the data from eth_getLogs for that period of time. - - # httpFullNodeEnable = true + # Note: Before release_4.8.1, if you turn on jsonrpc and run it for a while and then turn it off, + # you will not be able to get the data from eth_getLogs for that period of time. Default: false + # httpFullNodeEnable = false # httpFullNodePort = 8545 - # httpSolidityEnable = true + # httpSolidityEnable = false # httpSolidityPort = 8555 - # httpPBFTEnable = true + # httpPBFTEnable = false # 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 + 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 + maxSubTopics = 1000 + # Allowed maximum number for blockFilter + maxBlockFilterNum = 50000 } - # Disabled api list, it will work for http, rpc and pbft, both fullnode and soliditynode, - # but not jsonrpc. - # Sample: The setting is case insensitive, GetNowBlock2 is equal to getnowblock2 - # - # disabledApi = [ - # "getaccount", - # "getnowblock2" - # ] + # Disabled api list, it will work for http, rpc and pbft, both FullNode and SolidityNode, + # but not jsonrpc. The setting is case insensitive, GetNowBlock2 is equal to getnowblock2 + disabledApi = [ + # "getaccount", + # "getnowblock2" + ] } ## rate limiter config rate.limiter = { - # Every api could be set a specific rate limit strategy. Three strategy are supported:GlobalPreemptibleAdapter、IPQPSRateLimiterAdapte、QpsRateLimiterAdapter - # GlobalPreemptibleAdapter: permit is the number of preemptible resource, every client must apply one resourse - # before do the request and release the resource after got the reponse automaticlly. permit should be a Integer. + # Every api could only set a specific rate limit strategy. Three blocking strategy are supported: + # GlobalPreemptibleAdapter: The number of preemptible resource or maximum concurrent requests globally. # QpsRateLimiterAdapter: qps is the average request count in one second supported by the server, it could be a Double or a Integer. # IPQPSRateLimiterAdapter: similar to the QpsRateLimiterAdapter, qps could be a Double or a Integer. - # If do not set, the "default strategy" is set.The "default startegy" is based on QpsRateLimiterAdapter, the qps is set as 10000. + # If not set, QpsRateLimiterAdapter with qps=1000 is the default strategy. # # Sample entries: # @@ -363,9 +438,20 @@ rate.limiter = { # }, ] + p2p = { + # syncBlockChain = 3.0 + # fetchInvData = 3.0 + # disconnect = 1.0 + } + + # global qps, default 50000 + global.qps = 50000 + # IP-based global qps, default 10000 + global.ip.qps = 10000 } + seed.node = { # List of the seed nodes # Seed nodes are stable full nodes @@ -375,18 +461,18 @@ seed.node = { # "ip:port" # ] ip.list = [ - "54.236.37.243:18888", - "52.53.189.99:18888", - "18.196.99.16:18888", - "34.253.187.192:18888", - "52.56.56.149:18888", - "35.180.51.163:18888", - "54.252.224.209:18888", - "18.228.15.36:18888", - "52.15.93.92:18888", - "34.220.77.106:18888", - "13.127.47.162:18888", - "13.124.62.58:18888", + "3.225.171.164:18888", + "52.8.46.215:18888", + "3.79.71.167:18888", + "108.128.110.16:18888", + "18.133.82.227:18888", + "35.180.81.133:18888", + "13.210.151.5:18888", + "18.231.27.82:18888", + "3.12.212.122:18888", + "52.24.128.7:18888", + "15.207.144.3:18888", + "3.39.38.55:18888", "54.151.226.240:18888", "35.174.93.198:18888", "18.210.241.149:18888", @@ -404,7 +490,9 @@ seed.node = { "54.82.161.39:18888", "54.179.207.68:18888", "18.142.82.44:18888", - "18.163.230.203:18888" + "18.163.230.203:18888", + # "[2a05:d014:1f2f:2600:1b15:921:d60b:4c60]:18888", // use this if support ipv6 + # "[2600:1f18:7260:f400:8947:ebf3:78a0:282b]:18888", // use this if support ipv6 ] } @@ -569,34 +657,33 @@ genesis.block = { } ] - timestamp = "0" #2017-8-26 12:00:00 + timestamp = "0" # Genesis block timestamp, milli seconds parentHash = "0xe58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f" } -// Optional.The default is empty. -// It is used when the witness account has set the witnessPermission. -// When it is not empty, the localWitnessAccountAddress represents the address of the witness account, -// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. -// When it is empty,the localwitness is configured with the private key of the witness account. - -//localWitnessAccountAddress = +# Optional. The default is empty. It is used when the witness account has set the witnessPermission. +# When it is not empty, the localWitnessAccountAddress represents the address of the witness account, +# and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. +# When it is empty,the localwitness is configured with the private key of the witness account. +# localWitnessAccountAddress = localwitness = [ ] -#localwitnesskeystore = [ +# localwitnesskeystore = [ # "localwitnesskeystore.json" -#] +# ] block = { needSyncCheck = true - maintenanceTimeInterval = 21600000 - proposalExpireTime = 259200000 // 3 day: 259200000(ms) + maintenanceTimeInterval = 21600000 // 6 hours: 21600000(ms) + proposalExpireTime = 259200000 // default value: 3 days: 259200000(ms), Note: this value is controlled by committee proposal + # checkFrozenTime = 1 // for test only } -# Transaction reference block, default is "solid", configure to "head" may accur TaPos error -# trx.reference.block = "solid" // head;solid; +# Transaction reference block, default is "solid", configure to "head" may cause TaPos error +trx.reference.block = "solid" // "head" or "solid" # This property sets the number of milliseconds after the creation of the transaction that is expired, default value is 60000. # trx.expiration.timeInMilliseconds = 60000 @@ -607,26 +694,73 @@ vm = { minTimeRatio = 0.0 maxTimeRatio = 5.0 saveInternalTx = false + # lruCacheSize = 500 + # vmTrace = false - # Indicates whether the node stores featured internal transactions, such as freeze, vote and so on + # Indicates whether the node stores featured internal transactions, such as freeze, vote and so on. Default: false. # saveFeaturedInternalTx = false - # Indicates whether the node stores the details of the internal transactions generated by the CANCELALLUNFREEZEV2 opcode, such as bandwidth/energy/tronpower cancel amount. + # Indicates whether the node stores the details of the internal transactions generated by the CANCELALLUNFREEZEV2 opcode, + # such as bandwidth/energy/tronpower cancel amount. Default: false. # saveCancelAllUnfreezeV2Details = false # In rare cases, transactions that will be within the specified maximum execution time (default 10(ms)) are re-executed and packaged # longRunningTime = 10 - # Indicates whether the node support estimate energy API. + # Indicates whether the node support estimate energy API. Default: false. # estimateEnergy = false - # Indicates the max retry time for executing transaction in estimating energy. + # Indicates the max retry time for executing transaction in estimating energy. Default 3. # estimateEnergyMaxRetry = 3 } +# These parameters are designed for private chain testing only and cannot be freely switched on or off in production systems. committee = { allowCreationOfContracts = 0 //mainnet:0 (reset by committee),test:1 allowAdaptiveEnergy = 0 //mainnet:0 (reset by committee),test:1 + # allowCreationOfContracts = 0 + # allowMultiSign = 0 + # allowAdaptiveEnergy = 0 + # allowDelegateResource = 0 + # allowSameTokenName = 0 + # allowTvmTransferTrc10 = 0 + # allowTvmConstantinople = 0 + # allowTvmSolidity059 = 0 + # forbidTransferToContract = 0 + # allowShieldedTRC20Transaction = 0 + # allowTvmIstanbul = 0 + # allowMarketTransaction = 0 + # allowProtoFilterNum = 0 + # allowAccountStateRoot = 0 + # changedDelegation = 0 + # allowPBFT = 0 + # pBFTExpireNum = 0 + # allowTransactionFeePool = 0 + # allowBlackHoleOptimization = 0 + # allowNewResourceModel = 0 + # allowReceiptsMerkleRoot = 0 + # allowTvmFreeze = 0 + # allowTvmVote = 0 + # unfreezeDelayDays = 0 + # allowTvmLondon = 0 + # allowTvmCompatibleEvm = 0 + # allowNewRewardAlgorithm = 0 + # allowAccountAssetOptimization = 0 + # allowAssetOptimization = 0 + # allowNewReward = 0 + # memoFee = 0 + # allowDelegateOptimization = 0 + # allowDynamicEnergy = 0 + # dynamicEnergyThreshold = 0 + # dynamicEnergyMaxFactor = 0 + # allowTvmShangHai = 0 + # allowOldRewardOpt = 0 + # allowEnergyAdjustment = 0 + # allowStrictMath = 0 + # allowTvmCancun = 0 + # allowTvmBlob = 0 + # consensusLogicOptimization = 0 + # allowOptimizedReturnValueOfChainId = 0 } event.subscribe = { @@ -635,24 +769,35 @@ event.subscribe = { bindport = 5555 // bind port sendqueuelength = 1000 //max length of send queue } + version = 0 + # Specify the starting block number to sync historical events. This is only applicable when version = 1. + # After performing a full event sync, set this value to 0 or a negative number. + # startSyncBlockNum = 1 path = "" // absolute path of plugin server = "" // target server address to receive event triggers - dbconfig = "" // dbname|username|password - contractParse = true, + # dbname|username|password, if you want to create indexes for collections when the collections + # are not exist, you can add version and set it to 2, as dbname|username|password|version + # if you use version 2 and one collection not exists, it will create index automaticaly; + # if you use version 2 and one collection exists, it will not create index, you must create index manually; + dbconfig = "" + contractParse = true topics = [ { triggerName = "block" // block trigger, the value can't be modified enable = false topic = "block" // plugin topic, the value could be modified + solidified = false // if set true, just need solidified block. Default: false }, { triggerName = "transaction" enable = false topic = "transaction" + solidified = false + ethCompatible = false // if set true, add transactionIndex, cumulativeEnergyUsed, preCumulativeLogCount, logList, energyUnitPrice. Default: false }, { - triggerName = "contractevent" + triggerName = "contractevent" // contractevent represents contractlog data decoded by the ABI. enable = false topic = "contractevent" }, @@ -660,10 +805,11 @@ event.subscribe = { triggerName = "contractlog" enable = false topic = "contractlog" + redundancy = false // if set true, contractevent will also be regarded as contractlog }, { - triggerName = "solidity" // solidity block event trigger, the value can't be modified - enable = true // the default value is true + triggerName = "solidity" // solidity block trigger(just include solidity block number and timestamp), the value can't be modified + enable = true // Default: true topic = "solidity" }, { @@ -675,6 +821,7 @@ event.subscribe = { triggerName = "soliditylog" enable = false topic = "soliditylog" + redundancy = false // if set true, solidityevent will also be regarded as soliditylog } ] @@ -689,5 +836,4 @@ event.subscribe = { "" // contract topic you want to subscribe, if it's set to "", you will receive contract logs/events with any contract topic. ] } - } diff --git a/framework/src/test/java/org/springframework/http/InvalidMediaTypeException.java b/framework/src/test/java/org/springframework/http/InvalidMediaTypeException.java new file mode 100644 index 00000000000..3b1e41f1756 --- /dev/null +++ b/framework/src/test/java/org/springframework/http/InvalidMediaTypeException.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.http; + +import org.springframework.util.InvalidMimeTypeException; + +/** + * Exception thrown from {@link MediaType#parseMediaType(String)} in case of + * encountering an invalid media type specification String. + * + * @author Juergen Hoeller + * @since 3.2.2 + */ +@SuppressWarnings("serial") +public class InvalidMediaTypeException extends IllegalArgumentException { + + private final String mediaType; + + + /** + * Create a new InvalidMediaTypeException for the given media type. + * + * @param mediaType the offending media type + * @param message a detail message indicating the invalid part + */ + public InvalidMediaTypeException(String mediaType, String message) { + super("Invalid media type \"" + mediaType + "\": " + message); + this.mediaType = mediaType; + } + + /** + * Constructor that allows wrapping {@link InvalidMimeTypeException}. + */ + InvalidMediaTypeException(InvalidMimeTypeException ex) { + super(ex.getMessage(), ex); + this.mediaType = ex.getMimeType(); + } + + + /** + * Return the offending media type. + */ + public String getMediaType() { + return this.mediaType; + } + +} \ No newline at end of file diff --git a/framework/src/test/java/org/springframework/http/MediaType.java b/framework/src/test/java/org/springframework/http/MediaType.java new file mode 100644 index 00000000000..84962970235 --- /dev/null +++ b/framework/src/test/java/org/springframework/http/MediaType.java @@ -0,0 +1,841 @@ +/* + * Copyright 2002-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.http; + +import java.io.Serializable; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; +import org.springframework.util.InvalidMimeTypeException; +import org.springframework.util.MimeType; +import org.springframework.util.MimeTypeUtils; +import org.springframework.util.StringUtils; + +/** + * A subclass of {@link MimeType} that adds support for quality parameters + * as defined in the HTTP specification. + * + * @author Arjen Poutsma + * @author Juergen Hoeller + * @author Rossen Stoyanchev + * @author Sebastien Deleuze + * @author Kazuki Shimizu + * @author Sam Brannen + * @see + * HTTP 1.1: Semantics and Content, section 3.1.1.1 + * @since 3.0 + */ +public class MediaType extends MimeType implements Serializable { + + /** + * Public constant media type that includes all media ranges (i.e. "*/*"). + */ + public static final MediaType ALL; + /** + * A String equivalent of {@link MediaType#ALL}. + */ + public static final String ALL_VALUE = "*/*"; + /** + * Public constant media type for {@code application/atom+xml}. + */ + public static final MediaType APPLICATION_ATOM_XML; + /** + * A String equivalent of {@link MediaType#APPLICATION_ATOM_XML}. + */ + public static final String APPLICATION_ATOM_XML_VALUE = "application/atom+xml"; + /** + * Public constant media type for {@code application/cbor}. + * + * @since 5.2 + */ + public static final MediaType APPLICATION_CBOR; + /** + * A String equivalent of {@link MediaType#APPLICATION_CBOR}. + * + * @since 5.2 + */ + public static final String APPLICATION_CBOR_VALUE = "application/cbor"; + /** + * Public constant media type for {@code application/x-www-form-urlencoded}. + */ + public static final MediaType APPLICATION_FORM_URLENCODED; + /** + * A String equivalent of {@link MediaType#APPLICATION_FORM_URLENCODED}. + */ + public static final String APPLICATION_FORM_URLENCODED_VALUE = + "application/x-www-form-urlencoded"; + /** + * Public constant media type for {@code application/graphql+json}. + * + * @see GraphQL over HTTP spec + * @since 5.3.19 + */ + public static final MediaType APPLICATION_GRAPHQL; + /** + * A String equivalent of {@link MediaType#APPLICATION_GRAPHQL}. + * + * @since 5.3.19 + */ + public static final String APPLICATION_GRAPHQL_VALUE = "application/graphql+json"; + /** + * Public constant media type for {@code application/json}. + */ + public static final MediaType APPLICATION_JSON; + /** + * A String equivalent of {@link MediaType#APPLICATION_JSON}. + * + * @see #APPLICATION_JSON_UTF8_VALUE + */ + public static final String APPLICATION_JSON_VALUE = "application/json"; + /** + * Public constant media type for {@code application/json;charset=UTF-8}. + * + * @deprecated as of 5.2 in favor of {@link #APPLICATION_JSON} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. + */ + @Deprecated + public static final MediaType APPLICATION_JSON_UTF8; + /** + * A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}. + * + * @deprecated as of 5.2 in favor of {@link #APPLICATION_JSON_VALUE} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. + */ + @Deprecated + public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8"; + /** + * Public constant media type for {@code application/octet-stream}. + */ + public static final MediaType APPLICATION_OCTET_STREAM; + /** + * A String equivalent of {@link MediaType#APPLICATION_OCTET_STREAM}. + */ + public static final String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream"; + /** + * Public constant media type for {@code application/pdf}. + * + * @since 4.3 + */ + public static final MediaType APPLICATION_PDF; + /** + * A String equivalent of {@link MediaType#APPLICATION_PDF}. + * + * @since 4.3 + */ + public static final String APPLICATION_PDF_VALUE = "application/pdf"; + /** + * Public constant media type for {@code application/problem+json}. + * + * @see + * Problem Details for HTTP APIs, 6.1. application/problem+json + * @since 5.0 + */ + public static final MediaType APPLICATION_PROBLEM_JSON; + /** + * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_JSON}. + * + * @since 5.0 + */ + public static final String APPLICATION_PROBLEM_JSON_VALUE = "application/problem+json"; + /** + * Public constant media type for {@code application/problem+json}. + * + * @see + * Problem Details for HTTP APIs, 6.1. application/problem+json + * @since 5.0 + * @deprecated as of 5.2 in favor of {@link #APPLICATION_PROBLEM_JSON} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. + */ + @Deprecated + public static final MediaType APPLICATION_PROBLEM_JSON_UTF8; + /** + * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_JSON_UTF8}. + * + * @since 5.0 + * @deprecated as of 5.2 in favor of {@link #APPLICATION_PROBLEM_JSON_VALUE} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. + */ + @Deprecated + public static final String APPLICATION_PROBLEM_JSON_UTF8_VALUE = + "application/problem+json;charset=UTF-8"; + /** + * Public constant media type for {@code application/problem+xml}. + * + * @see + * Problem Details for HTTP APIs, 6.2. application/problem+xml + * @since 5.0 + */ + public static final MediaType APPLICATION_PROBLEM_XML; + /** + * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_XML}. + * + * @since 5.0 + */ + public static final String APPLICATION_PROBLEM_XML_VALUE = "application/problem+xml"; + /** + * Public constant media type for {@code application/rss+xml}. + * + * @since 4.3.6 + */ + public static final MediaType APPLICATION_RSS_XML; + /** + * A String equivalent of {@link MediaType#APPLICATION_RSS_XML}. + * + * @since 4.3.6 + */ + public static final String APPLICATION_RSS_XML_VALUE = "application/rss+xml"; + /** + * Public constant media type for {@code application/x-ndjson}. + * + * @since 5.3 + */ + public static final MediaType APPLICATION_NDJSON; + /** + * A String equivalent of {@link MediaType#APPLICATION_NDJSON}. + * + * @since 5.3 + */ + public static final String APPLICATION_NDJSON_VALUE = "application/x-ndjson"; + /** + * Public constant media type for {@code application/stream+json}. + * + * @since 5.0 + * @deprecated as of 5.3, see notice on {@link #APPLICATION_STREAM_JSON_VALUE}. + */ + @Deprecated + public static final MediaType APPLICATION_STREAM_JSON; + /** + * A String equivalent of {@link MediaType#APPLICATION_STREAM_JSON}. + * + * @since 5.0 + * @deprecated as of 5.3 since it originates from the W3C Activity Streams + * specification which has a more specific purpose and has been since + * replaced with a different mime type. Use {@link #APPLICATION_NDJSON} as + * a replacement or any other line-delimited JSON format (e.g. JSON Lines, + * JSON Text Sequences). + */ + @Deprecated + public static final String APPLICATION_STREAM_JSON_VALUE = "application/stream+json"; + /** + * Public constant media type for {@code application/xhtml+xml}. + */ + public static final MediaType APPLICATION_XHTML_XML; + /** + * A String equivalent of {@link MediaType#APPLICATION_XHTML_XML}. + */ + public static final String APPLICATION_XHTML_XML_VALUE = "application/xhtml+xml"; + /** + * Public constant media type for {@code application/xml}. + */ + public static final MediaType APPLICATION_XML; + /** + * A String equivalent of {@link MediaType#APPLICATION_XML}. + */ + public static final String APPLICATION_XML_VALUE = "application/xml"; + /** + * Public constant media type for {@code image/gif}. + */ + public static final MediaType IMAGE_GIF; + /** + * A String equivalent of {@link MediaType#IMAGE_GIF}. + */ + public static final String IMAGE_GIF_VALUE = "image/gif"; + /** + * Public constant media type for {@code image/jpeg}. + */ + public static final MediaType IMAGE_JPEG; + /** + * A String equivalent of {@link MediaType#IMAGE_JPEG}. + */ + public static final String IMAGE_JPEG_VALUE = "image/jpeg"; + /** + * Public constant media type for {@code image/png}. + */ + public static final MediaType IMAGE_PNG; + /** + * A String equivalent of {@link MediaType#IMAGE_PNG}. + */ + public static final String IMAGE_PNG_VALUE = "image/png"; + /** + * Public constant media type for {@code multipart/form-data}. + */ + public static final MediaType MULTIPART_FORM_DATA; + /** + * A String equivalent of {@link MediaType#MULTIPART_FORM_DATA}. + */ + public static final String MULTIPART_FORM_DATA_VALUE = "multipart/form-data"; + /** + * Public constant media type for {@code multipart/mixed}. + * + * @since 5.2 + */ + public static final MediaType MULTIPART_MIXED; + /** + * A String equivalent of {@link MediaType#MULTIPART_MIXED}. + * + * @since 5.2 + */ + public static final String MULTIPART_MIXED_VALUE = "multipart/mixed"; + /** + * Public constant media type for {@code multipart/related}. + * + * @since 5.2.5 + */ + public static final MediaType MULTIPART_RELATED; + /** + * A String equivalent of {@link MediaType#MULTIPART_RELATED}. + * + * @since 5.2.5 + */ + public static final String MULTIPART_RELATED_VALUE = "multipart/related"; + /** + * Public constant media type for {@code text/event-stream}. + * + * @see Server-Sent Events W3C recommendation + * @since 4.3.6 + */ + public static final MediaType TEXT_EVENT_STREAM; + /** + * A String equivalent of {@link MediaType#TEXT_EVENT_STREAM}. + * + * @since 4.3.6 + */ + public static final String TEXT_EVENT_STREAM_VALUE = "text/event-stream"; + /** + * Public constant media type for {@code text/html}. + */ + public static final MediaType TEXT_HTML; + /** + * A String equivalent of {@link MediaType#TEXT_HTML}. + */ + public static final String TEXT_HTML_VALUE = "text/html"; + /** + * Public constant media type for {@code text/markdown}. + * + * @since 4.3 + */ + public static final MediaType TEXT_MARKDOWN; + /** + * A String equivalent of {@link MediaType#TEXT_MARKDOWN}. + * + * @since 4.3 + */ + public static final String TEXT_MARKDOWN_VALUE = "text/markdown"; + /** + * Public constant media type for {@code text/plain}. + */ + public static final MediaType TEXT_PLAIN; + /** + * A String equivalent of {@link MediaType#TEXT_PLAIN}. + */ + public static final String TEXT_PLAIN_VALUE = "text/plain"; + /** + * Public constant media type for {@code text/xml}. + */ + public static final MediaType TEXT_XML; + /** + * A String equivalent of {@link MediaType#TEXT_XML}. + */ + public static final String TEXT_XML_VALUE = "text/xml"; + private static final long serialVersionUID = 2069937152339670231L; + private static final String PARAM_QUALITY_FACTOR = "q"; + /** + * Comparator used by {@link #sortByQualityValue(List)}. + */ + public static final Comparator QUALITY_VALUE_COMPARATOR = (mediaType1, mediaType2) -> { + double quality1 = mediaType1.getQualityValue(); + double quality2 = mediaType2.getQualityValue(); + int qualityComparison = Double.compare(quality2, quality1); + if (qualityComparison != 0) { + return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3 + } else if (mediaType1.isWildcardType() && !mediaType2.isWildcardType()) { // */* < audio/* + return 1; + } else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */* + return -1; + } else if (!mediaType1.getType().equals(mediaType2.getType())) { // audio/basic == text/html + return 0; + } else { // mediaType1.getType().equals(mediaType2.getType()) + if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { + // audio/* < audio/basic + return 1; + } else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { + // audio/basic > audio/* + return -1; + } else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { + // audio/basic == audio/wave + return 0; + } else { + int paramsSize1 = mediaType1.getParameters().size(); + int paramsSize2 = mediaType2.getParameters().size(); + return Integer.compare(paramsSize2, paramsSize1); // audio/basic;level=1 < audio/basic + } + } + }; + /** + * Comparator used by {@link #sortBySpecificity(List)}. + */ + public static final Comparator SPECIFICITY_COMPARATOR = + new SpecificityComparator() { + + @Override + protected int compareParameters(MediaType mediaType1, MediaType mediaType2) { + double quality1 = mediaType1.getQualityValue(); + double quality2 = mediaType2.getQualityValue(); + int qualityComparison = Double.compare(quality2, quality1); + if (qualityComparison != 0) { + return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3 + } + return super.compareParameters(mediaType1, mediaType2); + } + }; + + static { + // Not using "valueOf' to avoid static init cost + ALL = new MediaType("*", "*"); + APPLICATION_ATOM_XML = new MediaType("application", "atom+xml"); + APPLICATION_CBOR = new MediaType("application", "cbor"); + APPLICATION_FORM_URLENCODED = new MediaType("application", "x-www-form-urlencoded"); + APPLICATION_GRAPHQL = new MediaType("application", "graphql+json"); + APPLICATION_JSON = new MediaType("application", "json"); + APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8); + APPLICATION_NDJSON = new MediaType("application", "x-ndjson"); + APPLICATION_OCTET_STREAM = new MediaType("application", "octet-stream"); + APPLICATION_PDF = new MediaType("application", "pdf"); + APPLICATION_PROBLEM_JSON = new MediaType("application", "problem+json"); + APPLICATION_PROBLEM_JSON_UTF8 = new MediaType("application", "problem+json", + StandardCharsets.UTF_8); + APPLICATION_PROBLEM_XML = new MediaType("application", "problem+xml"); + APPLICATION_RSS_XML = new MediaType("application", "rss+xml"); + APPLICATION_STREAM_JSON = new MediaType("application", "stream+json"); + APPLICATION_XHTML_XML = new MediaType("application", "xhtml+xml"); + APPLICATION_XML = new MediaType("application", "xml"); + IMAGE_GIF = new MediaType("image", "gif"); + IMAGE_JPEG = new MediaType("image", "jpeg"); + IMAGE_PNG = new MediaType("image", "png"); + MULTIPART_FORM_DATA = new MediaType("multipart", "form-data"); + MULTIPART_MIXED = new MediaType("multipart", "mixed"); + MULTIPART_RELATED = new MediaType("multipart", "related"); + TEXT_EVENT_STREAM = new MediaType("text", "event-stream"); + TEXT_HTML = new MediaType("text", "html"); + TEXT_MARKDOWN = new MediaType("text", "markdown"); + TEXT_PLAIN = new MediaType("text", "plain"); + TEXT_XML = new MediaType("text", "xml"); + } + + /** + * Create a new {@code MediaType} for the given primary type. + *

The {@linkplain #getSubtype() subtype} is set to "*", parameters empty. + * + * @param type the primary type + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(String type) { + super(type); + } + + /** + * Create a new {@code MediaType} for the given primary type and subtype. + *

The parameters are empty. + * + * @param type the primary type + * @param subtype the subtype + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(String type, String subtype) { + super(type, subtype, Collections.emptyMap()); + } + + /** + * Create a new {@code MediaType} for the given type, subtype, and character set. + * + * @param type the primary type + * @param subtype the subtype + * @param charset the character set + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(String type, String subtype, Charset charset) { + super(type, subtype, charset); + } + + /** + * Create a new {@code MediaType} for the given type, subtype, and quality value. + * + * @param type the primary type + * @param subtype the subtype + * @param qualityValue the quality value + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(String type, String subtype, double qualityValue) { + this(type, subtype, Collections.singletonMap(PARAM_QUALITY_FACTOR, + Double.toString(qualityValue))); + } + + /** + * Copy-constructor that copies the type, subtype and parameters of the given + * {@code MediaType}, and allows to set the specified character set. + * + * @param other the other media type + * @param charset the character set + * @throws IllegalArgumentException if any of the parameters contain illegal characters + * @since 4.3 + */ + public MediaType(MediaType other, Charset charset) { + super(other, charset); + } + + /** + * Copy-constructor that copies the type and subtype of the given {@code MediaType}, + * and allows for different parameters. + * + * @param other the other media type + * @param parameters the parameters, may be {@code null} + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(MediaType other, @Nullable Map parameters) { + super(other.getType(), other.getSubtype(), parameters); + } + + + /** + * Create a new {@code MediaType} for the given type, subtype, and parameters. + * + * @param type the primary type + * @param subtype the subtype + * @param parameters the parameters, may be {@code null} + * @throws IllegalArgumentException if any of the parameters contain illegal characters + */ + public MediaType(String type, String subtype, @Nullable Map parameters) { + super(type, subtype, parameters); + } + + /** + * Create a new {@code MediaType} for the given {@link MimeType}. + * The type, subtype and parameters information is copied and {@code MediaType}-specific + * checks on parameters are performed. + * + * @param mimeType the MIME type + * @throws IllegalArgumentException if any of the parameters contain illegal characters + * @since 5.3 + */ + public MediaType(MimeType mimeType) { + super(mimeType); + getParameters().forEach(this::checkParameters); + } + + /** + * Parse the given String value into a {@code MediaType} object, + * with this method name following the 'valueOf' naming convention + * (as supported by {@link org.springframework.core.convert.ConversionService}. + * + * @param value the string to parse + * @throws InvalidMediaTypeException if the media type value cannot be parsed + * @see #parseMediaType(String) + */ + public static MediaType valueOf(String value) { + return parseMediaType(value); + } + + /** + * Parse the given String into a single {@code MediaType}. + * + * @param mediaType the string to parse + * @return the media type + * @throws InvalidMediaTypeException if the media type value cannot be parsed + */ + public static MediaType parseMediaType(String mediaType) { + MimeType type; + try { + type = MimeTypeUtils.parseMimeType(mediaType); + } catch (InvalidMimeTypeException ex) { + throw new InvalidMediaTypeException(ex); + } + try { + return new MediaType(type); + } catch (IllegalArgumentException ex) { + throw new InvalidMediaTypeException(mediaType, ex.getMessage()); + } + } + + /** + * Parse the comma-separated string into a list of {@code MediaType} objects. + *

This method can be used to parse an Accept or Content-Type header. + * + * @param mediaTypes the string to parse + * @return the list of media types + * @throws InvalidMediaTypeException if the media type value cannot be parsed + */ + public static List parseMediaTypes(@Nullable String mediaTypes) { + if (!StringUtils.hasLength(mediaTypes)) { + return Collections.emptyList(); + } + // Avoid using java.util.stream.Stream in hot paths + List tokenizedTypes = MimeTypeUtils.tokenize(mediaTypes); + List result = new ArrayList<>(tokenizedTypes.size()); + for (String type : tokenizedTypes) { + if (StringUtils.hasText(type)) { + result.add(parseMediaType(type)); + } + } + return result; + } + + /** + * Parse the given list of (potentially) comma-separated strings into a + * list of {@code MediaType} objects. + *

This method can be used to parse an Accept or Content-Type header. + * + * @param mediaTypes the string to parse + * @return the list of media types + * @throws InvalidMediaTypeException if the media type value cannot be parsed + * @since 4.3.2 + */ + public static List parseMediaTypes(@Nullable List mediaTypes) { + if (CollectionUtils.isEmpty(mediaTypes)) { + return Collections.emptyList(); + } else if (mediaTypes.size() == 1) { + return parseMediaTypes(mediaTypes.get(0)); + } else { + List result = new ArrayList<>(8); + for (String mediaType : mediaTypes) { + result.addAll(parseMediaTypes(mediaType)); + } + return result; + } + } + + /** + * Re-create the given mime types as media types. + * + * @since 5.0 + */ + public static List asMediaTypes(List mimeTypes) { + List mediaTypes = new ArrayList<>(mimeTypes.size()); + for (MimeType mimeType : mimeTypes) { + mediaTypes.add(MediaType.asMediaType(mimeType)); + } + return mediaTypes; + } + + /** + * Re-create the given mime type as a media type. + * + * @since 5.0 + */ + public static MediaType asMediaType(MimeType mimeType) { + if (mimeType instanceof MediaType) { + return (MediaType) mimeType; + } + return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); + } + + /** + * Return a string representation of the given list of {@code MediaType} objects. + *

This method can be used to for an {@code Accept} or {@code Content-Type} header. + * + * @param mediaTypes the media types to create a string representation for + * @return the string representation + */ + public static String toString(Collection mediaTypes) { + return MimeTypeUtils.toString(mediaTypes); + } + + /** + * Sorts the given list of {@code MediaType} objects by specificity. + *

Given two media types: + *

    + *
  1. if either media type has a {@linkplain #isWildcardType() wildcard type}, + * then the media type without the wildcard is ordered before the other.
  2. + *
  3. if the two media types have different {@linkplain #getType() types}, + * then they are considered equal and remain their current order.
  4. + *
  5. if either media type has a {@linkplain #isWildcardSubtype() wildcard subtype}, + * then the media type without the wildcard is sorted before the other.
  6. + *
  7. if the two media types have different {@linkplain #getSubtype() subtypes}, + * then they are considered equal and remain their current order.
  8. + *
  9. if the two media types have different {@linkplain #getQualityValue() quality value}, + * then the media type with the highest quality value is ordered before the other.
  10. + *
  11. if the two media types have a different amount of + * {@linkplain #getParameter(String) parameters}, then the + * media type with the most parameters is ordered before the other.
  12. + *
+ *

For example: + *

audio/basic < audio/* < */*
+ *
audio/* < audio/*;q=0.7; audio/*;q=0.3
+ *
audio/basic;level=1 < audio/basic
+ *
audio/basic == text/html
+ *
audio/basic == audio/wave
+ * + * @param mediaTypes the list of media types to be sorted + * @see HTTP 1.1: Semantics + * and Content, section 5.3.2 + */ + public static void sortBySpecificity(List mediaTypes) { + Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); + if (mediaTypes.size() > 1) { + mediaTypes.sort(SPECIFICITY_COMPARATOR); + } + } + + /** + * Sorts the given list of {@code MediaType} objects by quality value. + *

Given two media types: + *

    + *
  1. if the two media types have different {@linkplain #getQualityValue() quality value}, + * then the media type with the highest quality value is ordered before the other.
  2. + *
  3. if either media type has a {@linkplain #isWildcardType() wildcard type}, + * then the media type without the wildcard is ordered before the other.
  4. + *
  5. if the two media types have different {@linkplain #getType() types}, + * then they are considered equal and remain their current order.
  6. + *
  7. if either media type has a {@linkplain #isWildcardSubtype() wildcard subtype}, + * then the media type without the wildcard is sorted before the other.
  8. + *
  9. if the two media types have different {@linkplain #getSubtype() subtypes}, + * then they are considered equal and remain their current order.
  10. + *
  11. if the two media types have a different amount of + * {@linkplain #getParameter(String) parameters}, then the + * media type with the most parameters is ordered before the other.
  12. + *
+ * + * @param mediaTypes the list of media types to be sorted + * @see #getQualityValue() + */ + public static void sortByQualityValue(List mediaTypes) { + Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); + if (mediaTypes.size() > 1) { + mediaTypes.sort(QUALITY_VALUE_COMPARATOR); + } + } + + /** + * Sorts the given list of {@code MediaType} objects by specificity as the + * primary criteria and quality value the secondary. + * + * @see MediaType#sortBySpecificity(List) + * @see MediaType#sortByQualityValue(List) + */ + public static void sortBySpecificityAndQuality(List mediaTypes) { + Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); + if (mediaTypes.size() > 1) { + mediaTypes.sort( + MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR)); + } + } + + @Override + protected void checkParameters(String parameter, String value) { + super.checkParameters(parameter, value); + if (PARAM_QUALITY_FACTOR.equals(parameter)) { + String unquotedValue = unquote(value); + double d = Double.parseDouble(unquotedValue); + Assert.isTrue(d >= 0D && d <= 1D, + () -> "Invalid quality value \"" + unquotedValue + "\": should be between 0.0 and 1.0"); + } + } + + /** + * Return the quality factor, as indicated by a {@code q} parameter, if any. + * Defaults to {@code 1.0}. + * + * @return the quality factor as double value + */ + public double getQualityValue() { + String qualityFactor = getParameter(PARAM_QUALITY_FACTOR); + return (qualityFactor != null ? Double.parseDouble(unquote(qualityFactor)) : 1D); + } + + /** + * Indicate whether this {@code MediaType} includes the given media type. + *

For instance, {@code text/*} includes {@code text/plain} and {@code text/html}, + * and {@code application/*+xml} includes {@code application/soap+xml}, etc. + * This method is not symmetric. + *

Simply calls {@link MimeType#includes(MimeType)} but declared with a + * {@code MediaType} parameter for binary backwards compatibility. + * + * @param other the reference media type with which to compare + * @return {@code true} if this media type includes the given media type; + * {@code false} otherwise + */ + public boolean includes(@Nullable MediaType other) { + return super.includes(other); + } + + /** + * Indicate whether this {@code MediaType} is compatible with the given media type. + *

For instance, {@code text/*} is compatible with {@code text/plain}, + * {@code text/html}, and vice versa. In effect, this method is similar to + * {@link #includes}, except that it is symmetric. + *

Simply calls {@link MimeType#isCompatibleWith(MimeType)} but declared with a + * {@code MediaType} parameter for binary backwards compatibility. + * + * @param other the reference media type with which to compare + * @return {@code true} if this media type is compatible with the given media type; + * {@code false} otherwise + */ + public boolean isCompatibleWith(@Nullable MediaType other) { + return super.isCompatibleWith(other); + } + + /** + * Return a replica of this instance with the quality value of the given {@code MediaType}. + * + * @return the same instance if the given MediaType doesn't have a quality value, + * or a new one otherwise + */ + public MediaType copyQualityValue(MediaType mediaType) { + if (!mediaType.getParameters().containsKey(PARAM_QUALITY_FACTOR)) { + return this; + } + Map params = new LinkedHashMap<>(getParameters()); + params.put(PARAM_QUALITY_FACTOR, mediaType.getParameters().get(PARAM_QUALITY_FACTOR)); + return new MediaType(this, params); + } + + /** + * Return a replica of this instance with its quality value removed. + * + * @return the same instance if the media type doesn't contain a quality value, + * or a new one otherwise + */ + public MediaType removeQualityValue() { + if (!getParameters().containsKey(PARAM_QUALITY_FACTOR)) { + return this; + } + Map params = new LinkedHashMap<>(getParameters()); + params.remove(PARAM_QUALITY_FACTOR); + return new MediaType(this, params); + } + +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/common/BaseTest.java b/framework/src/test/java/org/tron/common/BaseTest.java index 552808b842c..dd4400e10f2 100644 --- a/framework/src/test/java/org/tron/common/BaseTest.java +++ b/framework/src/test/java/org/tron/common/BaseTest.java @@ -25,6 +25,8 @@ import org.tron.core.config.args.Args; import org.tron.core.db.Manager; import org.tron.core.exception.BalanceInsufficientException; +import org.tron.core.net.peer.PeerConnection; +import org.tron.core.net.peer.PeerManager; import org.tron.core.store.AccountStore; import org.tron.protos.Protocol; @@ -68,6 +70,12 @@ public static void destroy() { Args.clearParam(); } + public void closePeer() { + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); + } + } + public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] privateKey) { long blockTime = System.currentTimeMillis() / 3000 * 3000; if (time != 0) { diff --git a/framework/src/test/java/org/tron/common/EntityTest.java b/framework/src/test/java/org/tron/common/EntityTest.java index 483475a453b..bbdc8631225 100644 --- a/framework/src/test/java/org/tron/common/EntityTest.java +++ b/framework/src/test/java/org/tron/common/EntityTest.java @@ -5,13 +5,16 @@ import static org.junit.Assert.assertTrue; import com.google.common.collect.Lists; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.junit.Before; import org.junit.Test; import org.tron.common.entity.NodeInfo; import org.tron.common.entity.NodeInfo.MachineInfo; import org.tron.common.entity.NodeInfo.MachineInfo.DeadLockThreadInfo; +import org.tron.common.entity.PeerInfo; public class EntityTest { @@ -54,6 +57,9 @@ public void testDeadLockThreadInfo() { @Test public void testNodeInfo() { + List peerInfoList = new ArrayList<>(); + peerInfoList.add(getDefaultPeerInfo()); + NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setTotalFlow(1L); nodeInfo.setCheatWitnessInfoMap(new HashMap<>()); @@ -62,6 +68,39 @@ public void testNodeInfo() { nodeInfo.setMachineInfo(machineInfo); nodeInfo.setBlock("block"); nodeInfo.setSolidityBlock("solidityBlock"); + nodeInfo.setPeerList(peerInfoList); nodeInfo.transferToProtoEntity(); } + + private PeerInfo getDefaultPeerInfo() { + PeerInfo peerInfo = new PeerInfo(); + peerInfo.setAvgLatency(peerInfo.getAvgLatency()); + peerInfo.setBlockInPorcSize(peerInfo.getBlockInPorcSize()); + peerInfo.setConnectTime(peerInfo.getConnectTime()); + peerInfo.setDisconnectTimes(peerInfo.getDisconnectTimes()); + peerInfo.setHeadBlockTimeWeBothHave(peerInfo.getHeadBlockTimeWeBothHave()); + peerInfo.setHeadBlockWeBothHave(peerInfo.getHeadBlockWeBothHave()); + peerInfo.setHost("host"); + peerInfo.setInFlow(peerInfo.getInFlow()); + peerInfo.setLastBlockUpdateTime(peerInfo.getLastBlockUpdateTime()); + peerInfo.setLastSyncBlock("last"); + peerInfo.setLocalDisconnectReason("localDisconnectReason"); + peerInfo.setNodeCount(peerInfo.getNodeCount()); + peerInfo.setNodeId("nodeId"); + peerInfo.setHeadBlockWeBothHave("headBlockWeBothHave"); + peerInfo.setRemainNum(peerInfo.getRemainNum()); + peerInfo.setRemoteDisconnectReason("remoteDisconnectReason"); + peerInfo.setScore(peerInfo.getScore()); + peerInfo.setPort(peerInfo.getPort()); + peerInfo.setSyncFlag(peerInfo.isSyncFlag()); + peerInfo.setNeedSyncFromPeer(peerInfo.isNeedSyncFromPeer()); + peerInfo.setNeedSyncFromUs(peerInfo.isNeedSyncFromUs()); + peerInfo.setSyncToFetchSize(peerInfo.getSyncToFetchSize()); + peerInfo.setSyncToFetchSizePeekNum(peerInfo.getSyncToFetchSizePeekNum()); + peerInfo.setSyncBlockRequestedSize(peerInfo.getSyncBlockRequestedSize()); + peerInfo.setUnFetchSynNum(peerInfo.getUnFetchSynNum()); + peerInfo.setActive(peerInfo.isActive()); + + return peerInfo; + } } diff --git a/framework/src/test/java/org/tron/common/ParameterTest.java b/framework/src/test/java/org/tron/common/ParameterTest.java index b16be405f61..2f65189ac1c 100644 --- a/framework/src/test/java/org/tron/common/ParameterTest.java +++ b/framework/src/test/java/org/tron/common/ParameterTest.java @@ -129,6 +129,12 @@ public void testCommonParameter() { assertEquals(10, parameter.getMaxConcurrentCallsPerConnection()); parameter.setFlowControlWindow(20); assertEquals(20, parameter.getFlowControlWindow()); + assertEquals(0, parameter.getRpcMaxRstStream()); + parameter.setRpcMaxRstStream(10); + assertEquals(10, parameter.getRpcMaxRstStream()); + assertEquals(0, parameter.getRpcSecondsPerWindow()); + parameter.setRpcSecondsPerWindow(5); + assertEquals(5, parameter.getRpcSecondsPerWindow()); parameter.setMaxConnectionIdleInMillis(1000); assertEquals(1000, parameter.getMaxConnectionIdleInMillis()); parameter.setBlockProducedTimeOut(500); diff --git a/framework/src/test/java/org/tron/common/backup/BackupServerTest.java b/framework/src/test/java/org/tron/common/backup/BackupServerTest.java index c40aca7e17a..18e264eead2 100644 --- a/framework/src/test/java/org/tron/common/backup/BackupServerTest.java +++ b/framework/src/test/java/org/tron/common/backup/BackupServerTest.java @@ -10,6 +10,7 @@ import org.junit.rules.Timeout; import org.tron.common.backup.socket.BackupServer; import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.args.Args; @@ -26,7 +27,7 @@ public class BackupServerTest { @Before public void setUp() throws Exception { Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); - CommonParameter.getInstance().setBackupPort(80); + CommonParameter.getInstance().setBackupPort(PublicMethod.chooseRandomPort()); List members = new ArrayList<>(); members.add("127.0.0.2"); CommonParameter.getInstance().setBackupMembers(members); diff --git a/framework/src/test/java/org/tron/common/crypto/ECKeyTest.java b/framework/src/test/java/org/tron/common/crypto/ECKeyTest.java index 4e7d45ee8d7..273672e8342 100644 --- a/framework/src/test/java/org/tron/common/crypto/ECKeyTest.java +++ b/framework/src/test/java/org/tron/common/crypto/ECKeyTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.tron.common.utils.client.utils.AbiUtil.generateOccupationConstantPrivateKey; @@ -67,6 +68,11 @@ public void testFromPrivateKey() { assertTrue(key.isPubKeyCanonical()); assertTrue(key.hasPrivKey()); assertArrayEquals(pubKey, key.getPubKey()); + + key = ECKey.fromPrivate((byte[]) null); + assertNull(key); + key = ECKey.fromPrivate(new byte[0]); + assertNull(key); } @Test(expected = IllegalArgumentException.class) diff --git a/framework/src/test/java/org/tron/common/crypto/SM2KeyTest.java b/framework/src/test/java/org/tron/common/crypto/SM2KeyTest.java index b84026d2085..87e4e14698c 100644 --- a/framework/src/test/java/org/tron/common/crypto/SM2KeyTest.java +++ b/framework/src/test/java/org/tron/common/crypto/SM2KeyTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.tron.common.utils.client.utils.AbiUtil.generateOccupationConstantPrivateKey; @@ -64,6 +65,11 @@ public void testFromPrivateKey() { assertTrue(key.isPubKeyCanonical()); assertTrue(key.hasPrivKey()); assertArrayEquals(pubKey, key.getPubKey()); + + key = SM2.fromPrivate((byte[]) null); + assertNull(key); + key = SM2.fromPrivate(new byte[0]); + assertNull(key); } @Test(expected = IllegalArgumentException.class) diff --git a/framework/src/test/java/org/tron/common/logsfilter/NativeMessageQueueTest.java b/framework/src/test/java/org/tron/common/logsfilter/NativeMessageQueueTest.java index e6bb407bb53..d356e43d66c 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/NativeMessageQueueTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/NativeMessageQueueTest.java @@ -55,18 +55,19 @@ public void publishTrigger() { public void startSubscribeThread() { Thread thread = new Thread(() -> { - ZContext context = new ZContext(); - ZMQ.Socket subscriber = context.createSocket(SocketType.SUB); + try (ZContext context = new ZContext()) { + ZMQ.Socket subscriber = context.createSocket(SocketType.SUB); - Assert.assertEquals(true, subscriber.connect(String.format("tcp://localhost:%d", bindPort))); - Assert.assertEquals(true, subscriber.subscribe(topic)); + Assert.assertTrue(subscriber.connect(String.format("tcp://localhost:%d", bindPort))); + Assert.assertTrue(subscriber.subscribe(topic)); - while (!Thread.currentThread().isInterrupted()) { - byte[] message = subscriber.recv(); - String triggerMsg = new String(message); - - Assert.assertEquals(true, triggerMsg.contains(dataToSend) || triggerMsg.contains(topic)); + while (!Thread.currentThread().isInterrupted()) { + byte[] message = subscriber.recv(); + String triggerMsg = new String(message); + Assert.assertTrue(triggerMsg.contains(dataToSend) || triggerMsg.contains(topic)); + } + // ZMQ.Socket will be automatically closed when ZContext is closed } }); thread.start(); diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BatchValidateSignContractTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BatchValidateSignContractTest.java index fc60d8c0648..c18eb396546 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BatchValidateSignContractTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BatchValidateSignContractTest.java @@ -15,6 +15,7 @@ import org.tron.core.db.TransactionTrace; import org.tron.core.vm.PrecompiledContracts; import org.tron.core.vm.PrecompiledContracts.BatchValidateSign; +import org.tron.core.vm.config.VMConfig; @Slf4j @@ -74,6 +75,13 @@ public void staticCallTest() { ret = validateMultiSign(hash, signatures, addresses); Assert.assertEquals(ret.getValue().length, 32); Assert.assertArrayEquals(ret.getValue(), new byte[32]); + + //after optimized + VMConfig.initAllowTvmSelfdestructRestriction(1); + ret = validateMultiSign(hash, signatures, addresses); + Assert.assertEquals(ret.getValue().length, 32); + Assert.assertArrayEquals(ret.getValue(), new byte[32]); + VMConfig.initAllowTvmSelfdestructRestriction(0); System.gc(); // force triggering full gc to avoid timeout for next test } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java b/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java index f400b3215ee..6fa2801c51f 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java @@ -19,9 +19,9 @@ import org.tron.core.Wallet; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; -import org.tron.core.exception.JsonRpcInvalidParamsException; import org.tron.core.exception.ReceiptCheckErrException; import org.tron.core.exception.VMIllegalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.jsonrpc.TronJsonRpcImpl; import org.tron.core.vm.config.ConfigLoader; diff --git a/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java b/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java index 3315005b7d2..d6bbdddc854 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java @@ -1,6 +1,8 @@ package org.tron.common.runtime.vm; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.tron.core.config.Parameter.ChainConstant.FROZEN_PERIOD; import java.util.List; import java.util.Random; @@ -13,25 +15,33 @@ import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.tron.common.BaseTest; import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.InternalTransaction; import org.tron.common.utils.DecodeUtil; import org.tron.core.Constant; +import org.tron.core.Wallet; +import org.tron.core.capsule.AccountCapsule; import org.tron.core.config.args.Args; import org.tron.core.exception.ContractValidateException; import org.tron.core.store.StoreFactory; import org.tron.core.vm.EnergyCost; import org.tron.core.vm.JumpTable; +import org.tron.core.vm.MessageCall; import org.tron.core.vm.Op; import org.tron.core.vm.Operation; +import org.tron.core.vm.OperationActions; import org.tron.core.vm.OperationRegistry; +import org.tron.core.vm.PrecompiledContracts; import org.tron.core.vm.VM; import org.tron.core.vm.config.ConfigLoader; import org.tron.core.vm.config.VMConfig; import org.tron.core.vm.program.Program; import org.tron.core.vm.program.invoke.ProgramInvokeMockImpl; +import org.tron.core.vm.repository.Repository; import org.tron.protos.Protocol; @Slf4j @@ -40,6 +50,8 @@ public class OperationsTest extends BaseTest { private ProgramInvokeMockImpl invoke; private Program program; private final JumpTable jumpTable = OperationRegistry.getTable(); + @Autowired + private Wallet wallet; @BeforeClass public static void init() { @@ -749,6 +761,30 @@ public void testPushDupSwapAndLogOperations() throws ContractValidateException { Assert.assertEquals(2158, program.getResult().getEnergyUsed()); } + @Test + public void testCallOperations() throws ContractValidateException { + invoke = new ProgramInvokeMockImpl(); + Protocol.Transaction trx = Protocol.Transaction.getDefaultInstance(); + InternalTransaction interTrx = + new InternalTransaction(trx, InternalTransaction.TrxType.TRX_UNKNOWN_TYPE); + + byte prePrefixByte = DecodeUtil.addressPreFixByte; + DecodeUtil.addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_MAINNET; + VMConfig.initAllowTvmSelfdestructRestriction(1); + + program = new Program(new byte[0], new byte[0], invoke, interTrx); + MessageCall messageCall = new MessageCall( + Op.CALL, new DataWord(10000), + DataWord.ZERO(), DataWord.ZERO(), + DataWord.ZERO(), DataWord.ZERO(), + DataWord.ZERO(), DataWord.ZERO(), + DataWord.ZERO(), false); + program.callToPrecompiledAddress(messageCall, new PrecompiledContracts.ECRecover()); + + DecodeUtil.addressPreFixByte = prePrefixByte; + VMConfig.initAllowTvmSelfdestructRestriction(0); + } + @Test public void testOtherOperations() throws ContractValidateException { invoke = new ProgramInvokeMockImpl(); @@ -886,6 +922,12 @@ public void testSuicideCost() throws ContractValidateException { Assert.assertEquals(25000, EnergyCost.getSuicideCost2(program)); invoke.getDeposit().createAccount(receiver2, Protocol.AccountType.Normal); Assert.assertEquals(0, EnergyCost.getSuicideCost2(program)); + + byte[] receiver3 = generateRandomAddress(); + program.stackPush(new DataWord(receiver3)); + Assert.assertEquals(30000, EnergyCost.getSuicideCost3(program)); + invoke.getDeposit().createAccount(receiver3, Protocol.AccountType.Normal); + Assert.assertEquals(5000, EnergyCost.getSuicideCost3(program)); } @Test @@ -911,6 +953,85 @@ public void testSuicideAction() throws ContractValidateException { VMConfig.initAllowEnergyAdjustment(0); } + @Test + public void testCanSuicide2() throws ContractValidateException { + VMConfig.initAllowTvmFreeze(1); + VMConfig.initAllowTvmFreezeV2(1); + + byte[] contractAddr = Hex.decode("41471fd3ad3e9eeadeec4608b92d16ce6b500704cc"); + invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], contractAddr); + + program = new Program(null, null, invoke, + new InternalTransaction( + Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + program.getContractState().createAccount( + program.getContextAddress(), Protocol.AccountType.Contract); + Assert.assertTrue(program.canSuicide2()); + + long nowInMs = + program.getContractState().getDynamicPropertiesStore().getLatestBlockHeaderTimestamp(); + long expireTime = nowInMs + FROZEN_PERIOD; + AccountCapsule owner = program.getContractState().getAccount(program.getContextAddress()); + owner.setFrozenForEnergy(1000000, expireTime); + program.getContractState().updateAccount(program.getContextAddress(), owner); + Assert.assertFalse(program.canSuicide2()); + + VMConfig.initAllowTvmFreeze(0); + VMConfig.initAllowTvmFreezeV2(0); + } + + @Test + public void testSuicideAction2() throws ContractValidateException { + byte[] contractAddr = Hex.decode("41471fd3ad3e9eeadeec4608b92d16ce6b500704cc"); + invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], contractAddr); + Assert.assertTrue(invoke.getDeposit().isNewContract(contractAddr)); + + program = new Program(null, null, invoke, + new InternalTransaction( + Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + + VMConfig.initAllowEnergyAdjustment(1); + VMConfig.initAllowTvmSelfdestructRestriction(1); + VMConfig.initAllowTvmFreeze(1); + VMConfig.initAllowTvmFreezeV2(1); + VMConfig.initAllowTvmCompatibleEvm(1); + VMConfig.initAllowTvmVote(1); + byte prePrefixByte = DecodeUtil.addressPreFixByte; + DecodeUtil.addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_MAINNET; + + program.stackPush(new DataWord( + dbManager.getAccountStore().getBlackhole().getAddress().toByteArray())); + OperationActions.suicideAction2(program); + + Assert.assertEquals(1, program.getResult().getDeleteAccounts().size()); + + + invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], contractAddr); + program = new Program(null, null, invoke, + new InternalTransaction( + Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + Program spyProgram = Mockito.spy(program); + Repository realContractState = program.getContractState(); + Repository spyContractState = Mockito.spy(realContractState); + Mockito.when(spyContractState.isNewContract(any(byte[].class))).thenReturn(false); + Mockito.when(spyProgram.getContractState()).thenReturn(spyContractState); + spyProgram.suicide2(new DataWord( + dbManager.getAccountStore().getBlackhole().getAddress().toByteArray())); + + Assert.assertEquals(0, spyProgram.getResult().getDeleteAccounts().size()); + + DecodeUtil.addressPreFixByte = prePrefixByte; + VMConfig.initAllowEnergyAdjustment(0); + VMConfig.initAllowTvmSelfdestructRestriction(0); + VMConfig.initAllowTvmFreeze(0); + VMConfig.initAllowTvmFreezeV2(0); + VMConfig.initAllowTvmCompatibleEvm(0); + VMConfig.initAllowTvmVote(0); + } + @Test public void testVoteWitnessCost() throws ContractValidateException { // Build stack environment, the stack from top to bottom is 0x00, 0x80, 0x00, 0x80 diff --git a/framework/src/test/java/org/tron/common/runtime/vm/ValidateMultiSignContractTest.java b/framework/src/test/java/org/tron/common/runtime/vm/ValidateMultiSignContractTest.java index a688f5f9a29..894022fcea1 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/ValidateMultiSignContractTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/ValidateMultiSignContractTest.java @@ -25,6 +25,7 @@ import org.tron.core.config.args.Args; import org.tron.core.store.StoreFactory; import org.tron.core.vm.PrecompiledContracts.ValidateMultiSign; +import org.tron.core.vm.config.VMConfig; import org.tron.core.vm.repository.Repository; import org.tron.core.vm.repository.RepositoryImpl; import org.tron.protos.Protocol; @@ -123,6 +124,13 @@ public void testDifferentCase() { validateMultiSign(StringUtil.encode58Check(key.getAddress()), permissionId, data, signs) .getValue(), DataWord.ONE().getData()); + //after optimized + VMConfig.initAllowTvmSelfdestructRestriction(1); + Assert.assertArrayEquals( + validateMultiSign(StringUtil.encode58Check(key.getAddress()), permissionId, data, signs) + .getValue(), DataWord.ONE().getData()); + VMConfig.initAllowTvmSelfdestructRestriction(0); + //weight not enough signs = new ArrayList<>(); signs.add(Hex.toHexString(key1.sign(toSign).toByteArray())); diff --git a/framework/src/test/java/org/tron/common/storage/CheckOrInitEngineTest.java b/framework/src/test/java/org/tron/common/storage/CheckOrInitEngineTest.java new file mode 100644 index 00000000000..90aac10c0b6 --- /dev/null +++ b/framework/src/test/java/org/tron/common/storage/CheckOrInitEngineTest.java @@ -0,0 +1,263 @@ +package org.tron.common.storage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mockStatic; +import static org.tron.core.db.common.DbSourceInter.ENGINE_FILE; +import static org.tron.core.db.common.DbSourceInter.ENGINE_KEY; +import static org.tron.core.db.common.DbSourceInter.LEVELDB; +import static org.tron.core.db.common.DbSourceInter.ROCKSDB; +import static org.tron.core.db.common.DbSourceInter.checkOrInitEngine; + +import com.google.common.base.Strings; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.tron.common.utils.FileUtil; +import org.tron.common.utils.PropUtil; +import org.tron.core.exception.TronError; + + +public class CheckOrInitEngineTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static final String ACCOUNT = "account"; + + @After + public void clearMocks() { + Mockito.clearAllCaches(); + } + + @Test + public void testLevelDbDetectedWhenExpectingRocksDb() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + File currentFile = new File(dir, "CURRENT"); + assertTrue(currentFile.createNewFile()); + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + TronError exception = assertThrows(TronError.class, () -> + checkOrInitEngine(ROCKSDB, dir, errCode)); + assertEquals("Cannot open LEVELDB database with ROCKSDB engine.", + exception.getMessage()); + assertEquals(errCode, exception.getErrCode()); + } + } + + @Test + public void testCannotCreateDir() { + try (MockedStatic fileUtil = mockStatic(FileUtil.class)) { + String dir = "/invalid/path/that/cannot/be/created"; + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(false); + TronError.ErrCode errCode = TronError.ErrCode.LEVELDB_INIT; + TronError exception = assertThrows(TronError.class, () -> + checkOrInitEngine(LEVELDB, dir, errCode)); + assertEquals("Cannot create dir: " + dir + ".", exception.getMessage()); + assertEquals(errCode, exception.getErrCode()); + } + } + + @Test + public void testCannotCreateEngineFile() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class)) { + String dir = temporaryFolder.newFolder().toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(false); + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + TronError exception = assertThrows(TronError.class, () -> + checkOrInitEngine(ROCKSDB, dir, errCode)); + + assertEquals("Cannot create file: " + engineFile + ".", exception.getMessage()); + assertEquals(errCode, exception.getErrCode()); + } + } + + @Test + public void testCannotWritePropertyFile() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder().toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(null); + strings.when(() -> Strings.isNullOrEmpty(null)).thenReturn(true); + + propUtil.when(() -> PropUtil.writeProperty(engineFile, ENGINE_KEY, ROCKSDB)) + .thenReturn(false); + + TronError.ErrCode errCode = TronError.ErrCode.LEVELDB_INIT; + + TronError exception = assertThrows(TronError.class, () -> + checkOrInitEngine(ROCKSDB, dir, errCode)); + + assertEquals("Cannot write file: " + engineFile + ".", exception.getMessage()); + assertEquals(errCode, exception.getErrCode()); + } + + } + + @Test + public void testEngineMismatch() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(LEVELDB); + strings.when(() -> Strings.isNullOrEmpty(LEVELDB)).thenReturn(false); + + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + + TronError exception = assertThrows(TronError.class, () -> + checkOrInitEngine(ROCKSDB, dir, errCode)); + + assertEquals("Cannot open LEVELDB database with ROCKSDB engine.", + exception.getMessage()); + assertEquals(errCode, exception.getErrCode()); + } + } + + @Test + public void testSuccessfulFirstTimeInit() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)) + .thenReturn(null) + .thenReturn(LEVELDB); + strings.when(() -> Strings.isNullOrEmpty(null)).thenReturn(true); + + propUtil.when(() -> PropUtil.writeProperty(engineFile, ENGINE_KEY, LEVELDB)) + .thenReturn(true); + + TronError.ErrCode errCode = TronError.ErrCode.LEVELDB_INIT; + checkOrInitEngine(LEVELDB, dir, errCode); + } + } + + @Test + public void testSuccessfulExistingEngine() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(ROCKSDB); + strings.when(() -> Strings.isNullOrEmpty(ROCKSDB)).thenReturn(false); + + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + checkOrInitEngine(ROCKSDB, dir, errCode); + } + } + + @Test + /** + * 000003.log CURRENT LOCK MANIFEST-000002 + */ + public void testCurrentFileExistsWithNoEngineFile() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + File currentFile = new File(dir, "CURRENT"); + assertTrue(currentFile.createNewFile()); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(LEVELDB); + strings.when(() -> Strings.isNullOrEmpty(LEVELDB)).thenReturn(false); + + TronError.ErrCode errCode = TronError.ErrCode.LEVELDB_INIT; + + checkOrInitEngine(LEVELDB, dir, errCode); + } + } + + @Test + /** + * 000003.log CURRENT LOCK MANIFEST-000002 engine.properties(RocksDB) + */ + public void testCurrentFileExistsEngineFileExists() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder(ACCOUNT).toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + File currentFile = new File(dir, "CURRENT"); + File engineFileObj = new File(engineFile); + assertTrue(currentFile.createNewFile()); + assertTrue(engineFileObj.createNewFile()); + + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(ROCKSDB); + strings.when(() -> Strings.isNullOrEmpty(ROCKSDB)).thenReturn(false); + + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + checkOrInitEngine(ROCKSDB, dir, errCode); + } + } + + @Test + public void testEmptyStringEngine() throws IOException { + try (MockedStatic fileUtil = mockStatic(FileUtil.class); + MockedStatic propUtil = mockStatic(PropUtil.class); + MockedStatic strings = mockStatic(Strings.class)) { + + String dir = temporaryFolder.newFolder("account").toString(); + String engineFile = Paths.get(dir, ENGINE_FILE).toString(); + + fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true); + fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true); + + propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)) + .thenReturn("").thenReturn(ROCKSDB); + strings.when(() -> Strings.isNullOrEmpty("")).thenReturn(true); + + propUtil.when(() -> PropUtil.writeProperty(engineFile, ENGINE_KEY, ROCKSDB)) + .thenReturn(true); + TronError.ErrCode errCode = TronError.ErrCode.ROCKSDB_INIT; + checkOrInitEngine(ROCKSDB, dir, errCode); + } + } +} diff --git a/framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java b/framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java index bf18b988f19..78cbba3d079 100644 --- a/framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java +++ b/framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java @@ -28,6 +28,7 @@ import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -38,15 +39,24 @@ import java.util.Set; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.iq80.leveldb.DBException; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.rocksdb.RocksDB; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.storage.WriteOptionsWrapper; +import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl; import org.tron.common.utils.ByteArray; import org.tron.common.utils.FileUtil; +import org.tron.common.utils.PropUtil; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.StorageUtils; import org.tron.core.Constant; import org.tron.core.config.args.Args; import org.tron.core.db2.common.WrappedByteArray; @@ -73,6 +83,14 @@ public class LevelDbDataSourceImplTest { private byte[] key5 = "00000005aa".getBytes(); private byte[] key6 = "00000006aa".getBytes(); + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + static { + RocksDB.loadLibrary(); + } + /** * Release resources. */ @@ -94,7 +112,6 @@ public void testPutGet() { dataSourceTest.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); - dataSourceTest.initDB(); String value1 = "50000"; byte[] value = value1.getBytes(); @@ -102,8 +119,19 @@ public void testPutGet() { assertNotNull(dataSourceTest.getData(key)); assertEquals(1, dataSourceTest.allKeys().size()); + assertEquals(1, dataSourceTest.getTotal()); + assertEquals(1, dataSourceTest.allValues().size()); assertEquals("50000", ByteArray.toStr(dataSourceTest.getData(key1.getBytes()))); + dataSourceTest.deleteData(key); + assertNull(dataSourceTest.getData(key)); + assertEquals(0, dataSourceTest.getTotal()); + dataSourceTest.iterator().forEachRemaining(entry -> Assert.fail("iterator should be empty")); + dataSourceTest.stream().forEach(entry -> Assert.fail("stream should be empty")); + dataSourceTest.stat(); dataSourceTest.closeDB(); + dataSourceTest.stat(); // stat again + exception.expect(DBException.class); + dataSourceTest.deleteData(key); } @Test @@ -126,8 +154,6 @@ public void testReset() { public void testupdateByBatchInner() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_updateByBatch"); - dataSource.initDB(); - dataSource.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); String value1 = "50000"; String key2 = PublicMethod.getRandomPrivateKey(); @@ -142,6 +168,25 @@ public void testupdateByBatchInner() { assertEquals("50000", ByteArray.toStr(dataSource.getData(key1.getBytes()))); assertEquals("10000", ByteArray.toStr(dataSource.getData(key2.getBytes()))); assertEquals(2, dataSource.allKeys().size()); + + rows.clear(); + rows.put(key1.getBytes(), null); + rows.put(key2.getBytes(), null); + try (WriteOptionsWrapper options = WriteOptionsWrapper.getInstance()) { + dataSource.updateByBatch(rows, options); + } + assertEquals(0, dataSource.allKeys().size()); + + rows.clear(); + rows.put(key1.getBytes(), value1.getBytes()); + rows.put(key2.getBytes(), null); + dataSource.updateByBatch(rows); + assertEquals("50000", ByteArray.toStr(dataSource.getData(key1.getBytes()))); + assertEquals(1, dataSource.allKeys().size()); + rows.clear(); + rows.put(null, null); + exception.expect(RuntimeException.class); + dataSource.updateByBatch(rows); dataSource.closeDB(); } @@ -149,7 +194,6 @@ public void testupdateByBatchInner() { public void testdeleteData() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_delete"); - dataSource.initDB(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); dataSource.deleteData(key); @@ -163,8 +207,6 @@ public void testdeleteData() { public void testallKeys() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_find_key"); - dataSource.initDB(); - dataSource.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); @@ -187,7 +229,6 @@ public void testallKeys() { @Test(timeout = 1000) public void testLockReleased() { - dataSourceTest.initDB(); // normal close dataSourceTest.closeDB(); // closing already closed db. @@ -202,8 +243,6 @@ public void testLockReleased() { public void allKeysTest() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_allKeysTest_key"); - dataSource.initDB(); - dataSource.resetDb(); byte[] key = "0000000987b10fbb7f17110757321".getBytes(); byte[] value = "50000".getBytes(); @@ -216,7 +255,6 @@ public void allKeysTest() { logger.info(ByteArray.toStr(keyOne)); }); assertEquals(2, dataSource.allKeys().size()); - dataSource.resetDb(); dataSource.closeDB(); } @@ -242,26 +280,10 @@ private void putSomeKeyValue(LevelDbDataSourceImpl dataSource) { dataSource.putData(key4, value4); } - @Test - public void seekTest() { - LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( - Args.getInstance().getOutputDirectory(), "test_seek_key"); - dataSource.initDB(); - dataSource.resetDb(); - - putSomeKeyValue(dataSource); - Assert.assertTrue(true); - dataSource.resetDb(); - dataSource.closeDB(); - } - @Test public void getValuesNext() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getValuesNext_key"); - dataSource.initDB(); - dataSource.resetDb(); - putSomeKeyValue(dataSource); Set seekKeyLimitNext = dataSource.getValuesNext("0000000300".getBytes(), 2); HashSet hashSet = Sets.newHashSet(ByteArray.toStr(value3), ByteArray.toStr(value4)); @@ -276,7 +298,6 @@ public void getValuesNext() { public void testGetTotal() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getTotal_key"); - dataSource.initDB(); dataSource.resetDb(); Map dataMapset = Maps.newHashMap(); @@ -293,8 +314,6 @@ public void testGetTotal() { public void getKeysNext() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getKeysNext_key"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); int limit = 2; @@ -304,8 +323,6 @@ public void getKeysNext() { for (int i = 0; i < limit; i++) { Assert.assertArrayEquals(list.get(i), seekKeyLimitNext.get(i)); } - - dataSource.resetDb(); dataSource.closeDB(); } @@ -313,9 +330,6 @@ public void getKeysNext() { public void prefixQueryTest() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_prefixQuery"); - dataSource.initDB(); - dataSource.resetDb(); - putSomeKeyValue(dataSource); // put a kv that will not be queried. byte[] key7 = "0000001".getBytes(); @@ -341,23 +355,113 @@ public void prefixQueryTest() { Assert.assertEquals(list.size(), result.size()); list.forEach(entry -> Assert.assertTrue(result.contains(entry))); - dataSource.resetDb(); dataSource.closeDB(); } @Test public void initDbTest() { makeExceptionDb("test_initDb"); - LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( - Args.getInstance().getOutputDirectory(), "test_initDb"); - TronError thrown = assertThrows(TronError.class, dataSource::initDB); + TronError thrown = assertThrows(TronError.class, () -> new LevelDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "test_initDb")); assertEquals(TronError.ErrCode.LEVELDB_INIT, thrown.getErrCode()); } + @Test + public void testCheckOrInitEngine() { + String dir = + Args.getInstance().getOutputDirectory() + Args.getInstance().getStorage().getDbDirectory(); + String enginePath = dir + File.separator + "test_engine" + File.separator + "engine.properties"; + FileUtil.createDirIfNotExists(dir + File.separator + "test_engine"); + FileUtil.createFileIfNotExists(enginePath); + PropUtil.writeProperty(enginePath, "ENGINE", "LEVELDB"); + Assert.assertEquals("LEVELDB", PropUtil.readProperty(enginePath, "ENGINE")); + + LevelDbDataSourceImpl dataSource; + dataSource = new LevelDbDataSourceImpl(dir, "test_engine"); + dataSource.closeDB(); + + PropUtil.writeProperty(enginePath, "ENGINE", "ROCKSDB"); + Assert.assertEquals("ROCKSDB", PropUtil.readProperty(enginePath, "ENGINE")); + try { + new LevelDbDataSourceImpl(dir, "test_engine"); + } catch (TronError e) { + Assert.assertEquals("Cannot open ROCKSDB database with LEVELDB engine.", e.getMessage()); + } + } + + @Test + public void testLevelDbOpenRocksDb() { + String name = "test_openRocksDb"; + String output = Paths + .get(StorageUtils.getOutputDirectoryByDbName(name), CommonParameter + .getInstance().getStorage().getDbDirectory()).toString(); + RocksDbDataSourceImpl rocksDb = new RocksDbDataSourceImpl(output, name); + rocksDb.putData(key1, value1); + rocksDb.closeDB(); + exception.expectMessage("Cannot open ROCKSDB database with LEVELDB engine."); + new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name), name); + } + + @Test + public void testNewInstance() { + dataSourceTest.closeDB(); + LevelDbDataSourceImpl newInst = dataSourceTest.newInstance(); + assertFalse(newInst.flush()); + newInst.closeDB(); + LevelDbDataSourceImpl empty = new LevelDbDataSourceImpl(); + empty.setDBName("empty"); + assertEquals("empty", empty.getDBName()); + String name = "newInst2"; + LevelDbDataSourceImpl newInst2 = new LevelDbDataSourceImpl( + StorageUtils.getOutputDirectoryByDbName(name), + name); + newInst2.closeDB(); + } + + @Test + public void testGetNext() { + LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "test_getNext_key"); + putSomeKeyValue(dataSource); + // case: normal + Map seekKvLimitNext = dataSource.getNext("0000000300".getBytes(), 2); + Map hashMap = Maps.newHashMap(); + hashMap.put(ByteArray.toStr(key3), ByteArray.toStr(value3)); + hashMap.put(ByteArray.toStr(key4), ByteArray.toStr(value4)); + seekKvLimitNext.forEach((key, value) -> { + String keyStr = ByteArray.toStr(key); + Assert.assertTrue("getNext", hashMap.containsKey(keyStr)); + Assert.assertEquals(ByteArray.toStr(value), hashMap.get(keyStr)); + }); + // case: targetKey greater than all existed keys + seekKvLimitNext = dataSource.getNext("0000000700".getBytes(), 2); + Assert.assertEquals(0, seekKvLimitNext.size()); + // case: limit<=0 + seekKvLimitNext = dataSource.getNext("0000000300".getBytes(), 0); + Assert.assertEquals(0, seekKvLimitNext.size()); + dataSource.closeDB(); + } + + @Test + public void testGetlatestValues() { + LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "test_getlatestValues_key"); + putSomeKeyValue(dataSource); + // case: normal + Set seekKeyLimitNext = dataSource.getlatestValues(2); + Set hashSet = Sets.newHashSet(ByteArray.toStr(value5), ByteArray.toStr(value6)); + seekKeyLimitNext.forEach(value -> { + Assert.assertTrue(hashSet.contains(ByteArray.toStr(value))); + }); + // case: limit<=0 + seekKeyLimitNext = dataSource.getlatestValues(0); + assertEquals(0, seekKeyLimitNext.size()); + dataSource.closeDB(); + } + private void makeExceptionDb(String dbName) { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_initDb"); - dataSource.initDB(); dataSource.closeDB(); FileUtil.saveData(dataSource.getDbPath().toString() + "/CURRENT", "...", Boolean.FALSE); diff --git a/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java b/framework/src/test/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImplTest.java similarity index 71% rename from framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java rename to framework/src/test/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImplTest.java index c6fce30e3af..86543db19fb 100644 --- a/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java +++ b/framework/src/test/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImplTest.java @@ -1,4 +1,4 @@ -package org.tron.common.storage.leveldb; +package org.tron.common.storage.rocksdb; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -10,6 +10,8 @@ import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -24,13 +26,20 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl; +import org.rocksdb.RocksDBException; +import org.tron.common.error.TronDBException; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.storage.WriteOptionsWrapper; +import org.tron.common.storage.leveldb.LevelDbDataSourceImpl; import org.tron.common.utils.ByteArray; import org.tron.common.utils.FileUtil; import org.tron.common.utils.PropUtil; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.StorageUtils; import org.tron.core.config.args.Args; import org.tron.core.db2.common.WrappedByteArray; import org.tron.core.exception.TronError; @@ -55,6 +64,9 @@ public class RocksDbDataSourceImplTest { private byte[] key5 = "00000005aa".getBytes(); private byte[] key6 = "00000006aa".getBytes(); + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + /** * Release resources. */ @@ -76,7 +88,6 @@ public void testPutGet() { dataSourceTest.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); - dataSourceTest.initDB(); String value1 = "50000"; byte[] value = value1.getBytes(); @@ -84,8 +95,18 @@ public void testPutGet() { assertNotNull(dataSourceTest.getData(key)); assertEquals(1, dataSourceTest.allKeys().size()); + assertEquals(1, dataSourceTest.getTotal()); + assertEquals(1, dataSourceTest.allValues().size()); assertEquals("50000", ByteArray.toStr(dataSourceTest.getData(key1.getBytes()))); + dataSourceTest.deleteData(key); + assertNull(dataSourceTest.getData(key)); + assertEquals(0, dataSourceTest.getTotal()); + dataSourceTest.iterator().forEachRemaining(entry -> Assert.fail("iterator should be empty")); + dataSourceTest.stat(); dataSourceTest.closeDB(); + dataSourceTest.stat(); // stat again + expectedException.expect(TronDBException.class); + dataSourceTest.deleteData(key); } @Test @@ -108,8 +129,6 @@ public void testReset() { public void testupdateByBatchInner() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_updateByBatch"); - dataSource.initDB(); - dataSource.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); String value1 = "50000"; String key2 = PublicMethod.getRandomPrivateKey(); @@ -124,6 +143,25 @@ public void testupdateByBatchInner() { assertEquals("50000", ByteArray.toStr(dataSource.getData(key1.getBytes()))); assertEquals("10000", ByteArray.toStr(dataSource.getData(key2.getBytes()))); assertEquals(2, dataSource.allKeys().size()); + + rows.clear(); + rows.put(key1.getBytes(), null); + rows.put(key2.getBytes(), null); + try (WriteOptionsWrapper options = WriteOptionsWrapper.getInstance()) { + dataSource.updateByBatch(rows, options); + } + assertEquals(0, dataSource.allKeys().size()); + + rows.clear(); + rows.put(key1.getBytes(), value1.getBytes()); + rows.put(key2.getBytes(), null); + dataSource.updateByBatch(rows); + assertEquals("50000", ByteArray.toStr(dataSource.getData(key1.getBytes()))); + assertEquals(1, dataSource.allKeys().size()); + rows.clear(); + rows.put(null, null); + expectedException.expect(RuntimeException.class); + dataSource.updateByBatch(rows); dataSource.closeDB(); } @@ -131,7 +169,6 @@ public void testupdateByBatchInner() { public void testdeleteData() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_delete"); - dataSource.initDB(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); dataSource.deleteData(key); @@ -145,8 +182,6 @@ public void testdeleteData() { public void testallKeys() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_find_key"); - dataSource.initDB(); - dataSource.resetDb(); String key1 = PublicMethod.getRandomPrivateKey(); byte[] key = key1.getBytes(); @@ -163,13 +198,11 @@ public void testallKeys() { dataSource.putData(key2, value2); assertEquals(2, dataSource.allKeys().size()); - dataSource.resetDb(); dataSource.closeDB(); } @Test(timeout = 1000) public void testLockReleased() { - dataSourceTest.initDB(); // normal close dataSourceTest.closeDB(); // closing already closed db. @@ -184,8 +217,6 @@ public void testLockReleased() { public void allKeysTest() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_allKeysTest_key"); - dataSource.initDB(); - dataSource.resetDb(); byte[] key = "0000000987b10fbb7f17110757321".getBytes(); byte[] value = "50000".getBytes(); @@ -198,7 +229,6 @@ public void allKeysTest() { logger.info(ByteArray.toStr(keyOne)); }); assertEquals(2, dataSource.allKeys().size()); - dataSource.resetDb(); dataSource.closeDB(); } @@ -224,31 +254,15 @@ private void putSomeKeyValue(RocksDbDataSourceImpl dataSource) { dataSource.putData(key4, value4); } - @Test - public void seekTest() { - RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( - Args.getInstance().getOutputDirectory(), "test_seek_key"); - dataSource.initDB(); - dataSource.resetDb(); - - putSomeKeyValue(dataSource); - Assert.assertTrue(true); - dataSource.resetDb(); - dataSource.closeDB(); - } - @Test public void getValuesNext() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getValuesNext_key"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); Set seekKeyLimitNext = dataSource.getValuesNext("0000000300".getBytes(), 2); HashSet hashSet = Sets.newHashSet(ByteArray.toStr(value3), ByteArray.toStr(value4)); seekKeyLimitNext.forEach( value -> Assert.assertTrue("getValuesNext", hashSet.contains(ByteArray.toStr(value)))); - dataSource.resetDb(); dataSource.closeDB(); } @@ -264,23 +278,17 @@ public void testCheckOrInitEngine() { RocksDbDataSourceImpl dataSource; dataSource = new RocksDbDataSourceImpl(dir, "test_engine"); - dataSource.initDB(); Assert.assertNotNull(dataSource.getDatabase()); dataSource.closeDB(); - dataSource = null; - System.gc(); PropUtil.writeProperty(enginePath, "ENGINE", "LEVELDB"); Assert.assertEquals("LEVELDB", PropUtil.readProperty(enginePath, "ENGINE")); - dataSource = new RocksDbDataSourceImpl(dir, "test_engine"); + try { - dataSource.initDB(); - } catch (Exception e) { - Assert.assertEquals(String.format("failed to check database: %s, engine do not match", - "test_engine"), - e.getMessage()); + new RocksDbDataSourceImpl(dir, "test_engine"); + } catch (TronError e) { + Assert.assertEquals("Cannot open LEVELDB database with ROCKSDB engine.", e.getMessage()); } - Assert.assertNull(dataSource.getDatabase()); PropUtil.writeProperty(enginePath, "ENGINE", "ROCKSDB"); } @@ -288,8 +296,6 @@ public void testCheckOrInitEngine() { public void testGetNext() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getNext_key"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); // case: normal Map seekKvLimitNext = dataSource.getNext("0000000300".getBytes(), 2); @@ -307,7 +313,6 @@ public void testGetNext() { // case: limit<=0 seekKvLimitNext = dataSource.getNext("0000000300".getBytes(), 0); Assert.assertEquals(0, seekKvLimitNext.size()); - dataSource.resetDb(); dataSource.closeDB(); } @@ -315,8 +320,6 @@ public void testGetNext() { public void testGetlatestValues() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getlatestValues_key"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); // case: normal Set seekKeyLimitNext = dataSource.getlatestValues(2); @@ -327,7 +330,6 @@ public void testGetlatestValues() { // case: limit<=0 seekKeyLimitNext = dataSource.getlatestValues(0); assertEquals(0, seekKeyLimitNext.size()); - dataSource.resetDb(); dataSource.closeDB(); } @@ -335,8 +337,6 @@ public void testGetlatestValues() { public void getKeysNext() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_getKeysNext_key"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); int limit = 2; @@ -346,8 +346,6 @@ public void getKeysNext() { for (int i = 0; i < limit; i++) { Assert.assertArrayEquals(list.get(i), seekKeyLimitNext.get(i)); } - - dataSource.resetDb(); dataSource.closeDB(); } @@ -355,8 +353,6 @@ public void getKeysNext() { public void prefixQueryTest() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_prefixQuery"); - dataSource.initDB(); - dataSource.resetDb(); putSomeKeyValue(dataSource); // put a kv that will not be queried. @@ -383,23 +379,101 @@ public void prefixQueryTest() { Assert.assertEquals(list.size(), result.size()); list.forEach(entry -> Assert.assertTrue(result.contains(entry))); - dataSource.resetDb(); dataSource.closeDB(); } @Test public void initDbTest() { makeExceptionDb("test_initDb"); - RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( - Args.getInstance().getOutputDirectory(), "test_initDb"); - TronError thrown = assertThrows(TronError.class, dataSource::initDB); + TronError thrown = assertThrows(TronError.class, () -> new RocksDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "test_initDb")); assertEquals(TronError.ErrCode.ROCKSDB_INIT, thrown.getErrCode()); } + @Test + public void testRocksDbOpenLevelDb() { + String name = "test_openLevelDb"; + String output = Paths + .get(StorageUtils.getOutputDirectoryByDbName(name), CommonParameter + .getInstance().getStorage().getDbDirectory()).toString(); + LevelDbDataSourceImpl levelDb = new LevelDbDataSourceImpl( + StorageUtils.getOutputDirectoryByDbName(name), name); + levelDb.putData(key1, value1); + levelDb.closeDB(); + expectedException.expectMessage("Cannot open LEVELDB database with ROCKSDB engine."); + new RocksDbDataSourceImpl(output, name); + } + + @Test + public void testRocksDbOpenLevelDb2() { + String name = "test_openLevelDb2"; + String output = Paths + .get(StorageUtils.getOutputDirectoryByDbName(name), CommonParameter + .getInstance().getStorage().getDbDirectory()).toString(); + LevelDbDataSourceImpl levelDb = new LevelDbDataSourceImpl( + StorageUtils.getOutputDirectoryByDbName(name), name); + levelDb.putData(key1, value1); + levelDb.closeDB(); + // delete engine.properties file to simulate the case that db.engine is not set. + File engineFile = Paths.get(output, name, "engine.properties").toFile(); + if (engineFile.exists()) { + engineFile.delete(); + } + Assert.assertFalse(engineFile.exists()); + + expectedException.expectMessage("Cannot open LEVELDB database with ROCKSDB engine."); + new RocksDbDataSourceImpl(output, name); + } + + @Test + public void testNewInstance() { + dataSourceTest.closeDB(); + RocksDbDataSourceImpl newInst = dataSourceTest.newInstance(); + assertFalse(newInst.flush()); + newInst.closeDB(); + RocksDbDataSourceImpl empty = new RocksDbDataSourceImpl(); + empty.setDBName("empty"); + assertEquals("empty", empty.getDBName()); + String output = Paths + .get(StorageUtils.getOutputDirectoryByDbName("newInst2"), CommonParameter + .getInstance().getStorage().getDbDirectory()).toString(); + RocksDbDataSourceImpl newInst2 = new RocksDbDataSourceImpl(output, "newInst2"); + newInst2.closeDB(); + } + + @Test + public void backupAndDelete() throws RocksDBException { + RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "backupAndDelete"); + putSomeKeyValue(dataSource); + Path dir = Paths.get(Args.getInstance().getOutputDirectory(), "backup"); + String path = dir + File.separator; + FileUtil.createDirIfNotExists(path); + dataSource.backup(path); + File backDB = Paths.get(dir.toString(),dataSource.getDBName()).toFile(); + Assert.assertTrue(backDB.exists()); + dataSource.deleteDbBakPath(path); + Assert.assertFalse(backDB.exists()); + dataSource.closeDB(); + } + + @Test + public void testGetTotal() { + RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( + Args.getInstance().getOutputDirectory(), "test_getTotal_key"); + + Map dataMapset = Maps.newHashMap(); + dataMapset.put(key1, value1); + dataMapset.put(key2, value2); + dataMapset.put(key3, value3); + dataMapset.forEach(dataSource::putData); + Assert.assertEquals(dataMapset.size(), dataSource.getTotal()); + dataSource.closeDB(); + } + private void makeExceptionDb(String dbName) { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "test_initDb"); - dataSource.initDB(); dataSource.closeDB(); FileUtil.saveData(dataSource.getDbPath().toString() + "/CURRENT", "...", Boolean.FALSE); diff --git a/framework/src/test/java/org/tron/common/utils/FileUtilTest.java b/framework/src/test/java/org/tron/common/utils/FileUtilTest.java index 126e0918520..c22e83760a1 100644 --- a/framework/src/test/java/org/tron/common/utils/FileUtilTest.java +++ b/framework/src/test/java/org/tron/common/utils/FileUtilTest.java @@ -67,6 +67,7 @@ public void testReadData_NormalFile() throws IOException { try (FileWriter writer = new FileWriter(tempFile.toFile())) { writer.write("Hello, World!"); } + tempFile.toFile().deleteOnExit(); char[] buffer = new char[1024]; int len = readData(tempFile.toString(), buffer); diff --git a/framework/src/test/java/org/tron/common/utils/HashCodeTest.java b/framework/src/test/java/org/tron/common/utils/HashCodeTest.java new file mode 100644 index 00000000000..36f9435c1aa --- /dev/null +++ b/framework/src/test/java/org/tron/common/utils/HashCodeTest.java @@ -0,0 +1,23 @@ +package org.tron.common.utils; + +import java.util.Objects; +import org.junit.Assert; +import org.junit.Test; +import org.tron.core.capsule.AccountCapsule; +import org.tron.core.vm.repository.Type; +import org.tron.core.vm.repository.Value; +import org.tron.protos.Protocol; + +public class HashCodeTest { + + @Test + public void test() { + Type type = new Type(); + type.setType(Type.NORMAL); + Assert.assertEquals(Integer.valueOf(Type.NORMAL).hashCode(), type.hashCode()); + Protocol.Account account = Protocol.Account.newBuilder().setBalance(100).build(); + Value value = Value.create(new AccountCapsule(account.toByteArray())); + Assert.assertEquals(Integer.valueOf( + type.hashCode() + Objects.hashCode(account)).hashCode(), value.hashCode()); + } +} diff --git a/framework/src/test/java/org/tron/common/utils/ObjectSizeUtilTest.java b/framework/src/test/java/org/tron/common/utils/ObjectSizeUtilTest.java deleted file mode 100644 index c4c72991979..00000000000 --- a/framework/src/test/java/org/tron/common/utils/ObjectSizeUtilTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * java-tron is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * java-tron is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.tron.common.utils; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class ObjectSizeUtilTest { - - @Test - public void testGetObjectSize() { - - Person person = new Person(); - assertEquals(48, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person)); - Person person1 = new Person(1, "tom", new int[]{}); - assertEquals(112, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person1)); - - Person person2 = new Person(1, "tom", new int[]{100}); - assertEquals(120, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person2)); - - Person person3 = new Person(1, "tom", new int[]{100, 100}); - assertEquals(120, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person3)); - Person person4 = new Person(1, "tom", new int[]{100, 100, 100}); - assertEquals(128, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person4)); - Person person5 = new Person(1, "tom", new int[]{100, 100, 100, 100}); - assertEquals(128, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person5)); - Person person6 = new Person(1, "tom", new int[]{100, 100, 100, 100, 100}); - assertEquals(136, com.carrotsearch.sizeof.RamUsageEstimator.sizeOf(person6)); - - } - - class Person { - - int age; - String name; - int[] scores; - - public Person() { - } - - public Person(int age, String name, int[] scores) { - this.age = age; - this.name = name; - this.scores = scores; - } - } - -} diff --git a/framework/src/test/java/org/tron/common/utils/client/Configuration.java b/framework/src/test/java/org/tron/common/utils/client/Configuration.java index 79dded303aa..fb253b9605b 100644 --- a/framework/src/test/java/org/tron/common/utils/client/Configuration.java +++ b/framework/src/test/java/org/tron/common/utils/client/Configuration.java @@ -4,7 +4,6 @@ import com.typesafe.config.ConfigFactory; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.InputStreamReader; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -28,12 +27,12 @@ public static Config getByPath(final String configurationPath) { if (config == null) { File configFile = new File(System.getProperty("user.dir") + '/' + configurationPath); if (configFile.exists()) { - try { - config = ConfigFactory - .parseReader(new InputStreamReader(new FileInputStream(configurationPath))); + try (FileInputStream fis = new FileInputStream(configurationPath); + InputStreamReader isr = new InputStreamReader(fis)) { + config = ConfigFactory.parseReader(isr); logger.info("use user defined config file in current dir"); - } catch (FileNotFoundException e) { - logger.error("load user defined config file exception: " + e.getMessage()); + } catch (Exception e) { + logger.error("load user defined config file exception: {}", e.getMessage()); } } else { config = ConfigFactory.load(configurationPath); diff --git a/framework/src/test/java/org/tron/common/utils/client/utils/HttpMethed.java b/framework/src/test/java/org/tron/common/utils/client/utils/HttpMethed.java index a68bb616f11..030fbd80dea 100644 --- a/framework/src/test/java/org/tron/common/utils/client/utils/HttpMethed.java +++ b/framework/src/test/java/org/tron/common/utils/client/utils/HttpMethed.java @@ -2231,7 +2231,7 @@ public static void waitToProduceOneBlock(String httpNode) { } Integer nextBlockNum = 0; Integer times = 0; - while (nextBlockNum <= currentBlockNum + 1 && times++ <= 10) { + while (nextBlockNum < currentBlockNum + 1 && times++ <= 6) { response = HttpMethed.getNowBlock(httpNode); responseContent = HttpMethed.parseResponseContent(response); if (responseContent.containsKey("block_header")) { diff --git a/framework/src/test/java/org/tron/common/utils/client/utils/TransactionUtils.java b/framework/src/test/java/org/tron/common/utils/client/utils/TransactionUtils.java index b6226d01aae..63ffe1b58ff 100644 --- a/framework/src/test/java/org/tron/common/utils/client/utils/TransactionUtils.java +++ b/framework/src/test/java/org/tron/common/utils/client/utils/TransactionUtils.java @@ -14,6 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +import static org.tron.core.capsule.TransactionCapsule.getBase64FromByteString; import com.google.protobuf.ByteString; import java.security.SignatureException; @@ -116,20 +117,6 @@ public static byte[] getOwner(Transaction.Contract contract) { } } - /** - * constructor. - */ - - public static String getBase64FromByteString(ByteString sign) { - byte[] r = sign.substring(0, 32).toByteArray(); - byte[] s = sign.substring(32, 64).toByteArray(); - byte v = sign.byteAt(64); - if (v < 27) { - v += 27; //revId -> v - } - ECDSASignature signature = ECDSASignature.fromComponents(r, s, v); - return signature.toBase64(); - } /* * 1. check hash diff --git a/framework/src/test/java/org/tron/core/CoreExceptionTest.java b/framework/src/test/java/org/tron/core/CoreExceptionTest.java index 89feaba338c..f82b0efe326 100644 --- a/framework/src/test/java/org/tron/core/CoreExceptionTest.java +++ b/framework/src/test/java/org/tron/core/CoreExceptionTest.java @@ -29,11 +29,6 @@ import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.HighFreqException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; import org.tron.core.exception.NonCommonBlockException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.exception.P2pException; @@ -56,6 +51,11 @@ import org.tron.core.exception.ValidateSignatureException; import org.tron.core.exception.ZkProofValidateException; import org.tron.core.exception.ZksnarkException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; public class CoreExceptionTest { diff --git a/framework/src/test/java/org/tron/core/CreateCommonTransactionTest.java b/framework/src/test/java/org/tron/core/CreateCommonTransactionTest.java deleted file mode 100644 index 4bcef1e148c..00000000000 --- a/framework/src/test/java/org/tron/core/CreateCommonTransactionTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.tron.core; - -import static org.tron.common.utils.client.WalletClient.decodeFromBase58Check; - -import com.google.protobuf.Any; -import com.google.protobuf.ByteString; -import io.grpc.ManagedChannelBuilder; -import org.tron.api.GrpcAPI.TransactionExtention; -import org.tron.api.WalletGrpc; -import org.tron.api.WalletGrpc.WalletBlockingStub; -import org.tron.protos.Protocol.Transaction; -import org.tron.protos.Protocol.Transaction.Contract; -import org.tron.protos.Protocol.Transaction.Contract.ContractType; -import org.tron.protos.Protocol.Transaction.raw; -import org.tron.protos.contract.StorageContract.UpdateBrokerageContract; - -public class CreateCommonTransactionTest { - - private static final String FULL_NODE = "127.0.0.1:50051"; - - /** - * for example create UpdateBrokerageContract - */ - public static void testCreateUpdateBrokerageContract() { - WalletBlockingStub walletStub = WalletGrpc - .newBlockingStub(ManagedChannelBuilder.forTarget(FULL_NODE).usePlaintext().build()); - UpdateBrokerageContract.Builder updateBrokerageContract = UpdateBrokerageContract.newBuilder(); - updateBrokerageContract.setOwnerAddress( - ByteString.copyFrom(decodeFromBase58Check("TN3zfjYUmMFK3ZsHSsrdJoNRtGkQmZLBLz"))) - .setBrokerage(10); - Transaction.Builder transaction = Transaction.newBuilder(); - raw.Builder raw = Transaction.raw.newBuilder(); - Contract.Builder contract = Contract.newBuilder(); - contract.setType(ContractType.UpdateBrokerageContract) - .setParameter(Any.pack(updateBrokerageContract.build())); - raw.addContract(contract.build()); - transaction.setRawData(raw.build()); - TransactionExtention transactionExtention = walletStub - .createCommonTransaction(transaction.build()); - System.out.println("Common UpdateBrokerage: " + transactionExtention); - } - - public static void main(String[] args) { - testCreateUpdateBrokerageContract(); - } - -} diff --git a/framework/src/test/java/org/tron/core/ShieldWalletTest.java b/framework/src/test/java/org/tron/core/ShieldWalletTest.java index f8d5db1a44c..6e35d600ce7 100644 --- a/framework/src/test/java/org/tron/core/ShieldWalletTest.java +++ b/framework/src/test/java/org/tron/core/ShieldWalletTest.java @@ -7,6 +7,7 @@ import java.math.BigInteger; import javax.annotation.Resource; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.tron.api.GrpcAPI.PrivateParameters; import org.tron.api.GrpcAPI.PrivateParametersWithoutAsk; @@ -29,13 +30,14 @@ public class ShieldWalletTest extends BaseTest { @Resource private Wallet wallet; - static { - Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF); + @BeforeClass + public static void init() { + Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); + librustzcashInitZksnarkParams(); } @Test public void testCreateShieldedTransaction1() { - librustzcashInitZksnarkParams(); String transactionStr1 = new String(ByteArray.fromHexString( "0x7b0a20202020227472616e73706172656e745f66726f6d5f61646472657373223a202234433930413" + "73241433344414546324536383932343545463430303839443634314345414337373433323433414233" @@ -68,7 +70,6 @@ public void testCreateShieldedTransaction1() { @Test public void testCreateShieldedTransaction2() { - librustzcashInitZksnarkParams(); String transactionStr2 = new String(ByteArray.fromHexString( "7b0a202020202261736b223a20223938666430333136376632333437623534643737323338343137663" + "6373038643537323939643938376362613838353564653037626532346236316464653064222c0a2020" @@ -176,7 +177,6 @@ public void testCreateShieldedTransaction2() { @Test public void testCreateShieldedTransactionWithoutSpendAuthSig() { - librustzcashInitZksnarkParams(); String transactionStr3 = new String(ByteArray.fromHexString( "7b0a2020202022616b223a2022373161643638633466353035373464356164333735343863626538363" + "63031663732393662393161306362303535353733313462373830383437323730326465222c0a202020" @@ -286,7 +286,6 @@ public void testCreateShieldedTransactionWithoutSpendAuthSig() { @Test public void testGetNewShieldedAddress() { - librustzcashInitZksnarkParams(); try { ShieldedAddressInfo shieldedAddressInfo = wallet.getNewShieldedAddress(); Assert.assertNotNull(shieldedAddressInfo); @@ -297,8 +296,7 @@ public void testGetNewShieldedAddress() { @Test public void testCreateShieldedContractParameters() throws ContractExeException { - librustzcashInitZksnarkParams(); - Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true); + Args.getInstance().setAllowShieldedTransactionApi(true); Wallet wallet1 = spy(new Wallet()); doReturn(BigInteger.valueOf(1).toByteArray()) @@ -340,8 +338,7 @@ public void testCreateShieldedContractParameters() throws ContractExeException { @Test public void testCreateShieldedContractParameters2() throws ContractExeException { - librustzcashInitZksnarkParams(); - Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true); + Args.getInstance().setAllowShieldedTransactionApi(true); Wallet wallet1 = spy(new Wallet()); doReturn(BigInteger.valueOf(1).toByteArray()) @@ -416,8 +413,7 @@ public void testCreateShieldedContractParameters2() throws ContractExeException @Test public void testCreateShieldedContractParametersWithoutAsk() throws ContractExeException { - librustzcashInitZksnarkParams(); - Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true); + Args.getInstance().setAllowShieldedTransactionApi(true); Wallet wallet1 = spy(new Wallet()); doReturn(BigInteger.valueOf(1).toByteArray()) diff --git a/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java b/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java index 2c97473b6c3..c2c4bfe3006 100644 --- a/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java +++ b/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java @@ -1,7 +1,5 @@ package org.tron.core; -import static org.tron.core.zksnark.LibrustzcashTest.librustzcashInitZksnarkParams; - import com.google.protobuf.ByteString; import java.math.BigInteger; import java.util.Arrays; @@ -12,7 +10,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.bouncycastle.util.encoders.Hex; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.tron.api.GrpcAPI; @@ -68,7 +66,6 @@ public class ShieldedTRC20BuilderTest extends BaseTest { SHIELDED_CONTRACT_ADDRESS = WalletClient.decodeFromBase58Check(SHIELDED_CONTRACT_ADDRESS_STR); DEFAULT_OVK = ByteArray .fromHexString("030c8c2bc59fb3eb8afb047a8ea4b028743d23e7d38c6fa30908358431e2314d"); - ZksnarkInitService.librustzcashInitZksnarkParams(); PUBLIC_TO_ADDRESS = WalletClient.decodeFromBase58Check(PUBLIC_TO_ADDRESS_STR); } @@ -76,8 +73,9 @@ public class ShieldedTRC20BuilderTest extends BaseTest { VerifyTransferProof transferContract = new VerifyTransferProof(); VerifyBurnProof burnContract = new VerifyBurnProof(); - @Before - public void before() { + @BeforeClass + public static void initZksnarkParams() { + ZksnarkInitService.librustzcashInitZksnarkParams(); } @Ignore @@ -2172,7 +2170,6 @@ public void createShieldedContractParametersWithoutAskForBurn1to2() @Ignore @Test public void getTriggerInputForForMint() throws Exception { - librustzcashInitZksnarkParams(); SpendingKey sk = SpendingKey.random(); ExpandedSpendingKey expsk = sk.expandedSpendingKey(); byte[] ovk = expsk.getOvk(); @@ -2241,7 +2238,6 @@ public void getTriggerInputForForMint() throws Exception { public void testScanShieldedTRC20NotesByIvk() throws Exception { int statNum = 1; int endNum = 100; - librustzcashInitZksnarkParams(); SpendingKey sk = SpendingKey.decode(priKey); FullViewingKey fvk = sk.fullViewingKey(); byte[] ivk = fvk.inViewingKey().value; @@ -2273,7 +2269,6 @@ public void testscanShieldedTRC20NotesByOvk() throws Exception { public void isShieldedTRC20ContractNoteSpent() throws Exception { int statNum = 9200; int endNum = 9240; - librustzcashInitZksnarkParams(); SpendingKey sk = SpendingKey.decode(priKey); FullViewingKey fvk = sk.fullViewingKey(); byte[] ivk = fvk.inViewingKey().value; @@ -2350,7 +2345,6 @@ private byte[] decodePath(byte[] encodedPath) { private GrpcAPI.PrivateShieldedTRC20Parameters mintParams(String privKey, long value, String contractAddr, byte[] rcm) throws ZksnarkException, ContractValidateException { - librustzcashInitZksnarkParams(); long fromAmount = value; SpendingKey sk = SpendingKey.decode(privKey); ExpandedSpendingKey expsk = sk.expandedSpendingKey(); diff --git a/framework/src/test/java/org/tron/core/WalletMockTest.java b/framework/src/test/java/org/tron/core/WalletMockTest.java index 098ba9aee61..ab7ad7ba10c 100644 --- a/framework/src/test/java/org/tron/core/WalletMockTest.java +++ b/framework/src/test/java/org/tron/core/WalletMockTest.java @@ -835,7 +835,7 @@ public void testGetTriggerInputForShieldedTRC20Contract() { CommonParameter commonParameterMock = mock(Args.class); try (MockedStatic mockedStatic = mockStatic(CommonParameter.class)) { when(CommonParameter.getInstance()).thenReturn(commonParameterMock); - when(commonParameterMock.isFullNodeAllowShieldedTransactionArgs()).thenReturn(true); + when(commonParameterMock.isAllowShieldedTransactionApi()).thenReturn(true); assertThrows(ZksnarkException.class, () -> { wallet.getTriggerInputForShieldedTRC20Contract(triggerParam.build()); @@ -866,7 +866,7 @@ public void testGetTriggerInputForShieldedTRC20Contract1() CommonParameter commonParameterMock = mock(Args.class); try (MockedStatic mockedStatic = mockStatic(CommonParameter.class)) { when(CommonParameter.getInstance()).thenReturn(commonParameterMock); - when(commonParameterMock.isFullNodeAllowShieldedTransactionArgs()).thenReturn(true); + when(commonParameterMock.isAllowShieldedTransactionApi()).thenReturn(true); GrpcAPI.BytesMessage reponse = wallet.getTriggerInputForShieldedTRC20Contract(triggerParam.build()); @@ -1319,4 +1319,4 @@ public void testGetContractInfo1() throws Exception { wallet.getContractInfo(bytesMessage); assertNotNull(smartContractDataWrapper); } -} \ No newline at end of file +} diff --git a/framework/src/test/java/org/tron/core/WalletTest.java b/framework/src/test/java/org/tron/core/WalletTest.java index 831490fdca1..e388d3375c4 100644 --- a/framework/src/test/java/org/tron/core/WalletTest.java +++ b/framework/src/test/java/org/tron/core/WalletTest.java @@ -29,6 +29,8 @@ import com.google.protobuf.Any; import com.google.protobuf.ByteString; + +import java.util.ArrayList; import java.util.Arrays; import javax.annotation.Resource; import lombok.SneakyThrows; @@ -66,9 +68,13 @@ import org.tron.core.capsule.TransactionCapsule; import org.tron.core.capsule.TransactionInfoCapsule; import org.tron.core.capsule.TransactionResultCapsule; +import org.tron.core.capsule.VotesCapsule; +import org.tron.core.capsule.WitnessCapsule; import org.tron.core.config.args.Args; +import org.tron.core.db2.core.Chainbase; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; +import org.tron.core.exception.MaintenanceUnavailableException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.store.DynamicPropertiesStore; import org.tron.core.utils.ProposalUtil.ProposalType; @@ -851,6 +857,153 @@ public void testGetDelegatedResourceV2() { } } + @Test + public void testGetPaginatedNowWitnessList_Error() { + try { + // To avoid throw MaintenanceClearingException + dbManager.getChainBaseManager().getDynamicPropertiesStore().saveStateFlag(1); + wallet.getPaginatedNowWitnessList(0, 10); + Assert.fail("Should throw error when in maintenance period"); + } catch (Exception e) { + Assert.assertTrue("Should throw MaintenanceClearingException", + e instanceof MaintenanceUnavailableException); + } + + try { + Args.getInstance().setSolidityNode(true); + wallet.getPaginatedNowWitnessList(0, 10); + Args.getInstance().setSolidityNode(false); + + dbManager.setCursor(Chainbase.Cursor.SOLIDITY); + wallet.getPaginatedNowWitnessList(0, 10); + dbManager.setCursor(Chainbase.Cursor.HEAD); + } catch (Exception e) { + Assert.assertFalse("Should not throw MaintenanceClearingException", + e instanceof MaintenanceUnavailableException); + } + + dbManager.getChainBaseManager().getDynamicPropertiesStore().saveStateFlag(0); + } + + @Test + public void testGetPaginatedNowWitnessList_CornerCase() { + try { + // To avoid throw MaintenanceClearingException + dbManager.getChainBaseManager().getDynamicPropertiesStore().saveStateFlag(0); + + GrpcAPI.WitnessList witnessList = wallet.getPaginatedNowWitnessList(-100, 0); + Assert.assertTrue("Should return an empty witness list when offset is negative", + witnessList == null); + + witnessList = wallet.getPaginatedNowWitnessList(100, 0); + Assert.assertTrue("Should return an empty witness list when limit is 0", witnessList == null); + + String fakeWitnessAddressPrefix = "fake_witness"; + int fakeNumberOfWitnesses = 1000 + 10; + // Mock additional witnesses with vote counts greater than 1000 + for (int i = 0; i < fakeNumberOfWitnesses; i++) { + saveWitnessWith(fakeWitnessAddressPrefix + i, 200); + } + + witnessList = wallet.getPaginatedNowWitnessList(0, 1000000); + // Check the returned witness list should contain 1000 witnesses with descending vote count + Assert.assertTrue("Witness list should contain 1000 witnesses", + witnessList.getWitnessesCount() == 1000); + + // clean up, delete the fake witnesses + for (int i = 0; i < fakeNumberOfWitnesses; i++) { + chainBaseManager.getWitnessStore() + .delete(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + i).toByteArray()); + } + } catch (MaintenanceUnavailableException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void testGetPaginatedNowWitnessList() { + GrpcAPI.WitnessList witnessList = wallet.getWitnessList(); + logger.info(witnessList.toString()); + + // iterate through the witness list and find the existing maximum vote count + long maxVoteCount = 0L; + for (Protocol.Witness witness : witnessList.getWitnessesList()) { + if (witness.getVoteCount() > maxVoteCount) { + maxVoteCount = witness.getVoteCount(); + } + } + String fakeWitnessAddressPrefix = "fake_witness_address_for_paged_now_witness_list"; + int fakeNumberOfWitnesses = 10; + // Mock additional witnesses with vote counts greater than the maximum + for (int i = 0; i < fakeNumberOfWitnesses; i++) { + saveWitnessWith(fakeWitnessAddressPrefix + i, maxVoteCount + 1000000L); + } + + // Create a VotesCapsule to simulate the votes for the fake witnesses + VotesCapsule votesCapsule = new VotesCapsule(ByteString.copyFromUtf8(ACCOUNT_ADDRESS_ONE), + new ArrayList()); + votesCapsule.addOldVotes(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + 0), 100L); + votesCapsule.addOldVotes(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + 1), 50L); + votesCapsule.addNewVotes(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + 2), 200L); + votesCapsule.addNewVotes(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + 3), 300L); + chainBaseManager.getVotesStore().put(votesCapsule.createDbKey(), votesCapsule); + + logger.info("now request paginated witness list with 0 offset and 10 limit:"); + GrpcAPI.WitnessList witnessList2 = null; + try { + // To avoid throw MaintenanceClearingException + dbManager.getChainBaseManager().getDynamicPropertiesStore().saveStateFlag(0); + witnessList2 = wallet.getPaginatedNowWitnessList(0, 10); + } catch (MaintenanceUnavailableException e) { + Assert.fail(e.getMessage()); + } + // Check the returned witness list should contain 10 witnesses with descending vote count + Assert.assertTrue("Witness list should contain 10 witnesses", + witnessList2.getWitnessesCount() == 10); + // Check the first witness should have the maximum vote count + Assert.assertEquals("The first witness should have the maximum vote count", + fakeWitnessAddressPrefix + 3, witnessList2.getWitnesses(0).getAddress().toStringUtf8()); + Assert.assertEquals(maxVoteCount + 1000300L, witnessList2.getWitnesses(0).getVoteCount()); + // Check the second witness should have the second maximum vote count + Assert.assertEquals("The second witness", fakeWitnessAddressPrefix + 2, + witnessList2.getWitnesses(1).getAddress().toStringUtf8()); + Assert.assertEquals(maxVoteCount + 1000200L, witnessList2.getWitnesses(1).getVoteCount()); + // Check the last witness should have the least vote count + Assert.assertEquals("The tenth witness", fakeWitnessAddressPrefix + 0, + witnessList2.getWitnesses(9).getAddress().toStringUtf8()); + Assert.assertEquals(maxVoteCount + 1000000L - 100L, + witnessList2.getWitnesses(9).getVoteCount()); + + + logger.info("after paged"); + GrpcAPI.WitnessList witnessList3 = wallet.getWitnessList(); + // Check the witness list should remain unchanged after paged request + for (Protocol.Witness witness : witnessList3.getWitnessesList()) { + if (witness.getVoteCount() > maxVoteCount) { + Assert.assertTrue("Check the witness list should remain unchanged after paged request", + witness.getVoteCount() == maxVoteCount + 1000000L); + } + } + + // clean up, delete the fake witnesses + for (int i = 0; i < fakeNumberOfWitnesses; i++) { + chainBaseManager.getWitnessStore() + .delete(ByteString.copyFromUtf8(fakeWitnessAddressPrefix + i).toByteArray()); + } + chainBaseManager.getVotesStore().delete(votesCapsule.createDbKey()); + Assert.assertTrue("Clean up the mocked witness data", + wallet.getWitnessList().getWitnessesCount() == witnessList.getWitnessesCount()); + + } + + public void saveWitnessWith(String witnessAddress, long voteCount) { + WitnessCapsule witness = new WitnessCapsule( + Protocol.Witness.newBuilder() + .setAddress(ByteString.copyFromUtf8(witnessAddress)) // Convert String to ByteString + .setVoteCount(voteCount).build()); + chainBaseManager.getWitnessStore().put(witness.getAddress().toByteArray(), witness); + } + @Test public void testGetDelegatedResourceAccountIndexV2() { dbManager.getDynamicPropertiesStore().saveUnfreezeDelayDays(1L); diff --git a/framework/src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java b/framework/src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java index 77c24c28797..7daf139dc0f 100755 --- a/framework/src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java +++ b/framework/src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java @@ -1,6 +1,7 @@ package org.tron.core.actuator; import static org.junit.Assert.fail; +import static org.tron.core.config.Parameter.ChainConstant.FROZEN_PERIOD; import com.google.protobuf.Any; import com.google.protobuf.ByteString; @@ -15,12 +16,14 @@ import org.junit.Test; import org.tron.common.BaseTest; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.ForkController; import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.AssetIssueCapsule; import org.tron.core.capsule.TransactionResultCapsule; import org.tron.core.config.Parameter.ForkBlockVersionConsts; +import org.tron.core.config.Parameter.ForkBlockVersionEnum; import org.tron.core.config.args.Args; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; @@ -1868,6 +1871,53 @@ public void SameTokenNameCloseInvalidAccount() { } + @Test + public void issueStartTimeTooBig() { + long maintenanceTimeInterval = dbManager.getDynamicPropertiesStore() + .getMaintenanceTimeInterval(); + long hardForkTime = + ((ForkBlockVersionEnum.VERSION_4_0_1.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + dbManager.getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); + + // add more check after 4.8.1 + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + dbManager.getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_1.getValue(), stats); + boolean flag = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_1); + Assert.assertTrue(flag); + + TransactionResultCapsule ret = new TransactionResultCapsule(); + + // Start time is too big. If it's to large, the account.frozen_supply.expireTime will overflow + FrozenSupply frozenSupply = FrozenSupply.newBuilder().setFrozenDays(20).setFrozenAmount(100) + .build(); + long startTime = Long.MAX_VALUE - frozenSupply.getFrozenDays() * FROZEN_PERIOD + 1; + Any any = Any.pack( + AssetIssueContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) + .setName(ByteString.copyFromUtf8(NAME)).setTotalSupply(TOTAL_SUPPLY).setTrxNum(TRX_NUM) + .setNum(NUM) + .setStartTime(startTime) + .setEndTime(startTime + 24 * 3600 * 1000) + .setDescription(ByteString.copyFromUtf8(DESCRIPTION)) + .setUrl(ByteString.copyFromUtf8(URL)) + .setPrecision(3) + .addFrozenSupply(frozenSupply) + .build()); + AssetIssueActuator actuator = new AssetIssueActuator(); + actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(any); + + AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS)); + owner.setBalance(10_000_000_000L); + dbManager.getAccountStore().put(owner.createDbKey(), owner); + + processAndCheckInvalid(actuator, ret, + "Start time and frozen days would cause expire time overflow", + "Start time and frozen days would cause expire time overflow"); + } @Test public void commonErrorCheck() { diff --git a/framework/src/test/java/org/tron/core/actuator/ShieldedTransferActuatorTest.java b/framework/src/test/java/org/tron/core/actuator/ShieldedTransferActuatorTest.java index b71ba432018..cb95194f3d3 100755 --- a/framework/src/test/java/org/tron/core/actuator/ShieldedTransferActuatorTest.java +++ b/framework/src/test/java/org/tron/core/actuator/ShieldedTransferActuatorTest.java @@ -85,7 +85,7 @@ public class ShieldedTransferActuatorTest extends BaseTest { */ @BeforeClass public static void init() throws ZksnarkException { - Args.setFullNodeAllowShieldedTransaction(true); + Args.getInstance().setAllowShieldedTransactionApi(true); librustzcashInitZksnarkParams(); } @@ -950,7 +950,7 @@ public void publicAddressAToShieldAddressNoToAddressFailure() { */ @Test public void publicToShieldAddressAndShieldToPublicAddressWithZoreValueSuccess() { - Args.setFullNodeAllowShieldedTransaction(true); + Args.getInstance().setAllowShieldedTransactionApi(true); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); long fee = dbManager.getDynamicPropertiesStore().getShieldedTransactionFee(); diff --git a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java index e8a1e862f54..2ca466fb4da 100644 --- a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java +++ b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java @@ -1,5 +1,8 @@ package org.tron.core.actuator.utils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import com.google.protobuf.ByteString; import java.util.ArrayList; import java.util.Arrays; @@ -11,6 +14,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.function.ThrowingRunnable; import org.tron.common.BaseTest; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ForkController; @@ -439,6 +443,10 @@ public void validateCheck() { testAllowTvmBlobProposal(); + testAllowMarketTransaction(); + + testAllowTvmSelfdestructRestrictionProposal(); + forkUtils.getManager().getDynamicPropertiesStore() .statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats); forkUtils.reset(); @@ -659,6 +667,102 @@ private void testAllowTvmBlobProposal() { } + private void testAllowTvmSelfdestructRestrictionProposal() { + byte[] stats = new byte[27]; + forkUtils.getManager().getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_1.getValue(), stats); + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.ALLOW_TVM_SELFDESTRUCT_RESTRICTION.getCode(), 1); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "Bad chain parameter id [ALLOW_TVM_SELFDESTRUCT_RESTRICTION]", + e.getMessage()); + } + + long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore() + .getMaintenanceTimeInterval(); + + long hardForkTime = + ((ForkBlockVersionEnum.VERSION_4_8_1.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + forkUtils.getManager().getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); + + stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + forkUtils.getManager().getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_1.getValue(), stats); + + // Should fail because the proposal value is invalid + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.ALLOW_TVM_SELFDESTRUCT_RESTRICTION.getCode(), 2); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "This value[ALLOW_TVM_SELFDESTRUCT_RESTRICTION] is only allowed to be 1", + e.getMessage()); + } + + dynamicPropertiesStore.saveAllowTvmSelfdestructRestriction(1); + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.ALLOW_TVM_SELFDESTRUCT_RESTRICTION.getCode(), 1); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "[ALLOW_TVM_SELFDESTRUCT_RESTRICTION] has been valid, no need to propose again", + e.getMessage()); + } + } + + private void testAllowMarketTransaction() { + ThrowingRunnable off = () -> ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.ALLOW_MARKET_TRANSACTION.getCode(), 0); + ThrowingRunnable open = () -> ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.ALLOW_MARKET_TRANSACTION.getCode(), 1); + String err = "Bad chain parameter id [ALLOW_MARKET_TRANSACTION]"; + + ContractValidateException thrown = assertThrows(ContractValidateException.class, open); + assertEquals(err, thrown.getMessage()); + + activateFork(ForkBlockVersionEnum.VERSION_4_1); + + try { + open.run(); + } catch (Throwable e) { + Assert.fail(e.getMessage()); + } + + thrown = assertThrows(ContractValidateException.class, off); + assertEquals("This value[ALLOW_MARKET_TRANSACTION] is only allowed to be 1", + thrown.getMessage()); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1); + + thrown = assertThrows(ContractValidateException.class, open); + assertEquals(err, thrown.getMessage()); + + thrown = assertThrows(ContractValidateException.class, off); + assertEquals(err, thrown.getMessage()); + } + + private void activateFork(ForkBlockVersionEnum forkVersion) { + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + forkUtils.getManager().getDynamicPropertiesStore() + .statsByVersion(forkVersion.getValue(), stats); + + long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore() + .getMaintenanceTimeInterval(); + long hardForkTime = ((forkVersion.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + forkUtils.getManager().getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); + } + @Test public void blockVersionCheck() { for (ForkBlockVersionEnum forkVersion : ForkBlockVersionEnum.values()) { diff --git a/framework/src/test/java/org/tron/core/actuator/vm/ProgramTraceListenerTest.java b/framework/src/test/java/org/tron/core/actuator/vm/ProgramTraceListenerTest.java index 089711219f8..770e2bd0ea5 100644 --- a/framework/src/test/java/org/tron/core/actuator/vm/ProgramTraceListenerTest.java +++ b/framework/src/test/java/org/tron/core/actuator/vm/ProgramTraceListenerTest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; @@ -15,12 +16,15 @@ import org.tron.core.Constant; import org.tron.core.config.args.Args; import org.tron.core.db.TransactionStoreTest; +import org.tron.core.vm.trace.Op; import org.tron.core.vm.trace.OpActions; import org.tron.core.vm.trace.OpActions.Action; +import org.tron.core.vm.trace.ProgramTrace; import org.tron.core.vm.trace.ProgramTraceListener; @Slf4j(topic = "VM") public class ProgramTraceListenerTest { + @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -33,7 +37,7 @@ public class ProgramTraceListenerTest { @BeforeClass public static void init() throws IOException { - Args.setParam(new String[]{"--output-directory", + Args.setParam(new String[] {"--output-directory", temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF); } @@ -130,7 +134,6 @@ private void validateProgramTraceListener() { Assert.assertFalse(e instanceof IllegalAccessException); } - traceListener.resetActions(); try { @@ -179,4 +182,25 @@ public void programTraceListenerTest() { validateDisableTraceListener(); } + @Test + public void testGetSet() { + ProgramTrace programTrace = new ProgramTrace(); + Op op = new Op(); + List ops = new ArrayList<>(); + ops.add(op); + programTrace.setOps(ops); + programTrace.setResult("result"); + programTrace.setContractAddress("contractAddress"); + programTrace.setError("error"); + programTrace.result(new byte[] {}); + programTrace.error(new Exception()); + programTrace.getOps(); + programTrace.getContractAddress(); + programTrace.getError(); + programTrace.getResult(); + programTrace.toString(); + + Assert.assertTrue(true); + } + } diff --git a/framework/src/test/java/org/tron/core/actuator/vm/SerializersTest.java b/framework/src/test/java/org/tron/core/actuator/vm/SerializersTest.java new file mode 100644 index 00000000000..52ee1eeb937 --- /dev/null +++ b/framework/src/test/java/org/tron/core/actuator/vm/SerializersTest.java @@ -0,0 +1,12 @@ +package org.tron.core.actuator.vm; + +import org.junit.Test; +import org.tron.core.vm.trace.Serializers; + +public class SerializersTest { + + @Test + public void testSerializeFieldsOnly() { + Serializers.serializeFieldsOnly("testString", true); + } +} diff --git a/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java b/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java index 3c86d893895..552a014a97b 100644 --- a/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java +++ b/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java @@ -134,7 +134,7 @@ public void testHasWitnessSignature() { localWitnesses = new LocalWitnesses(); localWitnesses.setPrivateKeys(Arrays.asList(privateKey)); - localWitnesses.initWitnessAccountAddress(true); + localWitnesses.initWitnessAccountAddress(null, true); Args.setLocalWitnesses(localWitnesses); Assert.assertFalse(blockCapsule0.hasWitnessSignature()); diff --git a/framework/src/test/java/org/tron/core/capsule/utils/ExchangeProcessorTest.java b/framework/src/test/java/org/tron/core/capsule/utils/ExchangeProcessorTest.java index 717c62b01a8..1f0be4b1f7c 100644 --- a/framework/src/test/java/org/tron/core/capsule/utils/ExchangeProcessorTest.java +++ b/framework/src/test/java/org/tron/core/capsule/utils/ExchangeProcessorTest.java @@ -138,12 +138,63 @@ public void testWithdraw() { @Test public void testStrictMath() { long supply = 1_000_000_000_000_000_000L; - ExchangeProcessor processor = new ExchangeProcessor(supply, false); - long anotherTokenQuant = processor.exchange(4732214, 2202692725330L, 29218); - processor = new ExchangeProcessor(supply, true); - long result = processor.exchange(4732214, 2202692725330L, 29218); - Assert.assertNotEquals(anotherTokenQuant, result); + long[][] testData = { + {4732214L, 2202692725330L, 29218L}, + {5618633L, 556559904655L, 1L}, + {9299554L, 1120271441185L, 7000L}, + {62433133L, 12013267997895L, 100000L}, + {64212664L, 725836766395L, 50000L}, + {64126212L, 2895100109660L, 5000L}, + {56459055L, 3288380567368L, 165000L}, + {21084707L, 1589204008960L, 50000L}, + {24120521L, 1243764649177L, 20000L}, + {836877L, 212532333234L, 5293L}, + {55879741L, 13424854054078L, 250000L}, + {66388882L, 11300012790454L, 300000L}, + {94470955L, 7941038150919L, 2000L}, + {13613746L, 5012660712983L, 122L}, + {71852829L, 5262251868618L, 396L}, + {3857658L, 446109245044L, 20637L}, + {35491863L, 3887393269796L, 100L}, + {295632118L, 1265298439004L, 500000L}, + {49320113L, 1692106302503L, 123267L}, + {10966984L, 6222910652894L, 2018L}, + {41634280L, 2004508994767L, 865L}, + {10087714L, 6765558834714L, 1009L}, + {42270078L, 210360843525L, 200000L}, + {571091915L, 655011397250L, 2032520L}, + {51026781L, 1635726339365L, 37L}, + {61594L, 312318864132L, 500L}, + {11616684L, 5875978057357L, 20L}, + {60584529L, 1377717821301L, 78132L}, + {29818073L, 3033545989651L, 182L}, + {3855280L, 834647482043L, 16L}, + {58310711L, 1431562205655L, 200000L}, + {60226263L, 1386036785882L, 178226L}, + {3537634L, 965771433992L, 225L}, + {3760534L, 908700758784L, 328L}, + {80913L, 301864126445L, 4L}, + {3789271L, 901842209723L, 1L}, + {4051904L, 843419481286L, 1005L}, + {89141L, 282107742510L, 100L}, + {90170L, 282854635378L, 26L}, + {4229852L, 787503315944L, 137L}, + {4259884L, 781975090197L, 295L}, + {3627657L, 918682223700L, 34L}, + {813519L, 457546358759L, 173L}, + {89626L, 327856173057L, 27L}, + {97368L, 306386489550L, 50L}, + {93712L, 305866015731L, 4L}, + {3281260L, 723656594544L, 40L}, + {3442652L, 689908773685L, 18L}, + }; + + for (long[] data : testData) { + ExchangeProcessor processor = new ExchangeProcessor(supply, false); + long anotherTokenQuant = processor.exchange(data[0], data[1], data[2]); + processor = new ExchangeProcessor(supply, true); + long result = processor.exchange(data[0], data[1], data[2]); + Assert.assertNotEquals(anotherTokenQuant, result); + } } - - } diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 4bb8e7e4909..fb19528b626 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -57,7 +57,8 @@ public void destroy() { @Test public void get() { - Args.setParam(new String[] {"-c", Constant.TEST_CONF}, Constant.TESTNET_CONF); + Args.setParam(new String[] {"-c", Constant.TEST_CONF, "--keystore-factory"}, + Constant.TESTNET_CONF); CommonParameter parameter = Args.getInstance(); @@ -65,10 +66,10 @@ public void get() { localWitnesses = new LocalWitnesses(); localWitnesses.setPrivateKeys(Arrays.asList(privateKey)); - localWitnesses.initWitnessAccountAddress(true); + localWitnesses.initWitnessAccountAddress(null, true); Args.setLocalWitnesses(localWitnesses); address = ByteArray.toHexString(Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())); + .getWitnessAccountAddress()); Assert.assertEquals(Constant.ADD_PRE_FIX_STRING_TESTNET, DecodeUtil.addressPreFixString); Assert.assertEquals(0, parameter.getBackupPriority()); @@ -126,7 +127,9 @@ public void get() { Assert.assertEquals(address, ByteArray.toHexString(Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()))); + .getWitnessAccountAddress())); + + Assert.assertTrue(parameter.isKeystoreFactory()); } @Test diff --git a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java index 27d5effd6b1..7f6d5417924 100644 --- a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java +++ b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java @@ -15,24 +15,47 @@ package org.tron.core.config.args; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import com.google.common.collect.Lists; +import java.io.IOException; +import java.security.SecureRandom; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.tron.common.utils.ByteArray; import org.tron.common.utils.LocalWitnesses; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.StringUtil; +import org.tron.core.Constant; +import org.tron.core.exception.TronError; +import org.tron.core.exception.TronError.ErrCode; public class LocalWitnessTest { private final LocalWitnesses localWitness = new LocalWitnesses(); private static final String PRIVATE_KEY = PublicMethod.getRandomPrivateKey(); + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Before public void setLocalWitness() { localWitness .setPrivateKeys( Lists.newArrayList( - PRIVATE_KEY)); + PRIVATE_KEY)); + } + + @AfterClass + public static void clear() { + Args.clearParam(); } @Test @@ -42,16 +65,16 @@ public void whenSetNullPrivateKey() { Assert.assertNotNull(localWitness.getPublicKey()); } - @Test + @Test(expected = TronError.class) public void whenSetEmptyPrivateKey() { localWitness.setPrivateKeys(Lists.newArrayList("")); - Assert.assertNotNull(localWitness.getPrivateKey()); - Assert.assertNotNull(localWitness.getPublicKey()); + fail("private key must be 64-bits hex string"); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = TronError.class) public void whenSetBadFormatPrivateKey() { localWitness.setPrivateKeys(Lists.newArrayList("a111")); + fail("private key must be 64-bits hex string"); } @Test @@ -65,6 +88,68 @@ public void whenSetPrefixPrivateKey() { Assert.assertNotNull(localWitness.getPrivateKey()); } + @Test + public void testValidPrivateKey() { + LocalWitnesses localWitnesses = new LocalWitnesses(); + + try { + localWitnesses.addPrivateKeys(PRIVATE_KEY); + Assert.assertEquals(1, localWitnesses.getPrivateKeys().size()); + Assert.assertEquals(PRIVATE_KEY, localWitnesses.getPrivateKeys().get(0)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void testValidPrivateKeyWithPrefix() { + LocalWitnesses localWitnesses = new LocalWitnesses(); + + try { + localWitnesses.addPrivateKeys("0x" + PRIVATE_KEY); + Assert.assertEquals(1, localWitnesses.getPrivateKeys().size()); + Assert.assertEquals("0x" + PRIVATE_KEY, localWitnesses.getPrivateKeys().get(0)); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void testInvalidPrivateKey() { + LocalWitnesses localWitnesses = new LocalWitnesses(); + String expectedMessage = "private key must be 64 hex string"; + assertTronError(localWitnesses, null, expectedMessage); + assertTronError(localWitnesses, "", expectedMessage); + assertTronError(localWitnesses, " ", expectedMessage); + assertTronError(localWitnesses, "11111", expectedMessage); + String expectedMessage2 = "private key must be hex string"; + SecureRandom secureRandom = new SecureRandom(); + byte[] keyBytes = new byte[31]; + secureRandom.nextBytes(keyBytes); + final String privateKey = ByteArray.toHexString(keyBytes) + " "; + assertTronError(localWitnesses, privateKey, expectedMessage2); + final String privateKey2 = "xy" + ByteArray.toHexString(keyBytes); + assertTronError(localWitnesses, privateKey2, expectedMessage2); + } + + private void assertTronError(LocalWitnesses localWitnesses, String privateKey, + String expectedMessage) { + TronError thrown = assertThrows(TronError.class, + () -> localWitnesses.addPrivateKeys(privateKey)); + assertEquals(ErrCode.WITNESS_INIT, thrown.getErrCode()); + assertTrue(thrown.getMessage().contains(expectedMessage)); + } + + @Test + public void testHexStringFormat() { + Assert.assertTrue(StringUtil.isHexadecimal("0123456789abcdefABCDEF")); + Assert.assertFalse(StringUtil.isHexadecimal(null)); + Assert.assertFalse(StringUtil.isHexadecimal("")); + Assert.assertFalse(StringUtil.isHexadecimal("abc")); + Assert.assertFalse(StringUtil.isHexadecimal(" ")); + Assert.assertFalse(StringUtil.isHexadecimal("123xyz")); + } + @Test public void getPrivateKey() { Assert.assertEquals(Lists @@ -77,14 +162,34 @@ public void testConstructor() { LocalWitnesses localWitnesses = new LocalWitnesses(PublicMethod.getRandomPrivateKey()); LocalWitnesses localWitnesses1 = new LocalWitnesses(Lists.newArrayList(PublicMethod.getRandomPrivateKey())); - localWitnesses.setWitnessAccountAddress(new byte[0]); + localWitnesses.initWitnessAccountAddress(new byte[0], true); Assert.assertNotNull(localWitnesses1.getPublicKey()); LocalWitnesses localWitnesses2 = new LocalWitnesses(); Assert.assertNull(localWitnesses2.getPrivateKey()); Assert.assertNull(localWitnesses2.getPublicKey()); - localWitnesses2.initWitnessAccountAddress(true); + localWitnesses2.initWitnessAccountAddress(null, true); LocalWitnesses localWitnesses3 = new LocalWitnesses(); - Assert.assertNotNull(localWitnesses3.getWitnessAccountAddress(true)); + Assert.assertNull(localWitnesses3.getWitnessAccountAddress()); + } + + @Test + public void testLocalWitnessConfig() throws IOException { + Args.setParam( + new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "-w", "--debug"}, + "config-localtest.conf"); + LocalWitnesses witness = Args.getLocalWitnesses(); + Assert.assertNotNull(witness.getPrivateKey()); + Assert.assertNotNull(witness.getWitnessAccountAddress()); + } + + @Test + public void testNullLocalWitnessConfig() throws IOException { + Args.setParam( + new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "--debug"}, + Constant.TEST_CONF); + LocalWitnesses witness = Args.getLocalWitnesses(); + Assert.assertNull(witness.getPrivateKey()); + Assert.assertNull(witness.getWitnessAccountAddress()); } } diff --git a/framework/src/test/java/org/tron/core/config/args/WitnessInitializerTest.java b/framework/src/test/java/org/tron/core/config/args/WitnessInitializerTest.java new file mode 100644 index 00000000000..7364b1f9b3a --- /dev/null +++ b/framework/src/test/java/org/tron/core/config/args/WitnessInitializerTest.java @@ -0,0 +1,268 @@ +package org.tron.core.config.args; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import org.bouncycastle.util.encoders.Hex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.tron.common.crypto.SignInterface; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.ByteArray; +import org.tron.common.utils.LocalWitnesses; +import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.client.utils.Base58; +import org.tron.core.Constant; +import org.tron.core.exception.TronError; +import org.tron.core.exception.TronError.ErrCode; +import org.tron.keystore.Credentials; +import org.tron.keystore.WalletUtils; + +public class WitnessInitializerTest { + + private Config config; + private WitnessInitializer witnessInitializer; + + private static final String privateKey = PublicMethod.getRandomPrivateKey(); + private static final String address = Base58.encode58Check( + ByteArray.fromHexString(PublicMethod.getHexAddressByPrivateKey(privateKey))); + private static final String invalidAddress = "RJCzdnv88Hvqa2jB1C9dMmMYHr5DFdF2R3"; + + @Before + public void setUp() { + config = ConfigFactory.empty(); + witnessInitializer = new WitnessInitializer(config); + } + + @After + public void clear() { + Args.clearParam(); + } + + @Test + public void testInitLocalWitnessesEmpty() { + Args.PARAMETER.setWitness(false); + + LocalWitnesses result = witnessInitializer.initLocalWitnesses(); + assertNotNull(result); + assertTrue(result.getPrivateKeys().isEmpty()); + + Args.PARAMETER.setWitness(true); + LocalWitnesses localWitnesses = witnessInitializer.initLocalWitnesses(); + assertTrue(localWitnesses.getPrivateKeys().isEmpty()); + + String configString = "localwitness = [] \n localwitnesskeystore = []"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + localWitnesses = witnessInitializer.initLocalWitnesses(); + assertTrue(localWitnesses.getPrivateKeys().isEmpty()); + } + + @Test + public void testTryInitFromCommandLine() + throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException { + Field privateKeyField = CommonParameter.class.getDeclaredField("privateKey"); + privateKeyField.setAccessible(true); + privateKeyField.set(Args.getInstance(), ""); + + witnessInitializer = new WitnessInitializer(config); + Method method = WitnessInitializer.class.getDeclaredMethod( + "tryInitFromCommandLine"); + method.setAccessible(true); + boolean result = (boolean) method.invoke(witnessInitializer); + assertFalse(result); + + privateKeyField.set(Args.getInstance(), privateKey); + method.invoke(witnessInitializer); + result = (boolean) method.invoke(witnessInitializer); + assertTrue(result); + + Field witnessAddress = CommonParameter.class.getDeclaredField("witnessAddress"); + witnessAddress.setAccessible(true); + witnessAddress.set(Args.getInstance(), address); + result = (boolean) method.invoke(witnessInitializer); + assertTrue(result); + + witnessAddress.set(Args.getInstance(), invalidAddress); + InvocationTargetException thrown = assertThrows(InvocationTargetException.class, + () -> method.invoke(witnessInitializer)); + TronError targetException = (TronError) thrown.getTargetException(); + assertEquals(ErrCode.WITNESS_INIT, targetException.getErrCode()); + } + + @Test + public void testTryInitFromConfig() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + witnessInitializer = new WitnessInitializer(config); + Method method = WitnessInitializer.class.getDeclaredMethod( + "tryInitFromConfig"); + method.setAccessible(true); + boolean result = (boolean) method.invoke(witnessInitializer); + assertFalse(result); + + String configString = "localwitness = []"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + result = (boolean) method.invoke(witnessInitializer); + assertFalse(result); + + configString = "localwitness = [" + privateKey + "]"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + result = (boolean) method.invoke(witnessInitializer); + assertTrue(result); + + configString = "localWitnessAccountAddress = " + address + "\n" + + "localwitness = [\n" + privateKey + "]"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + result = (boolean) method.invoke(witnessInitializer); + assertTrue(result); + + configString = "localwitness = [\n" + privateKey + "\n" + privateKey + "]"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + result = (boolean) method.invoke(witnessInitializer); + assertTrue(result); + + configString = "localWitnessAccountAddress = " + invalidAddress + "\n" + + "localwitness = [\n" + privateKey + "]"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + InvocationTargetException thrown = assertThrows(InvocationTargetException.class, + () -> method.invoke(witnessInitializer)); + TronError targetException = (TronError) thrown.getTargetException(); + assertEquals(ErrCode.WITNESS_INIT, targetException.getErrCode()); + + configString = "localWitnessAccountAddress = " + address + "\n" + + "localwitness = [\n" + privateKey + "\n" + privateKey + "]"; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + thrown = assertThrows(InvocationTargetException.class, + () -> method.invoke(witnessInitializer)); + targetException = (TronError) thrown.getTargetException(); + assertEquals(ErrCode.WITNESS_INIT, targetException.getErrCode()); + } + + @Test + public void testTryInitFromKeystore() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, + NoSuchFieldException { + witnessInitializer = new WitnessInitializer(config); + Method method = WitnessInitializer.class.getDeclaredMethod( + "tryInitFromKeystore"); + method.setAccessible(true); + method.invoke(witnessInitializer); + Field localWitnessField = WitnessInitializer.class.getDeclaredField("localWitnesses"); + localWitnessField.setAccessible(true); + LocalWitnesses localWitnesses = (LocalWitnesses) localWitnessField.get(witnessInitializer); + assertTrue(localWitnesses.getPrivateKeys().isEmpty()); + + String configString = "localwitnesskeystore = []"; + Config emptyListConfig = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(emptyListConfig); + method.invoke(witnessInitializer); + localWitnesses = (LocalWitnesses) localWitnessField.get(witnessInitializer); + assertTrue(localWitnesses.getPrivateKeys().isEmpty()); + } + + @Test + public void testTryInitFromKeyStore2() + throws NoSuchFieldException, IllegalAccessException { + Args.PARAMETER.setWitness(true); + Config mockConfig = mock(Config.class); + when(mockConfig.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)).thenReturn(false); + witnessInitializer = new WitnessInitializer(mockConfig); + witnessInitializer.initLocalWitnesses(); + verify(mockConfig, never()).getStringList(anyString()); + + when(mockConfig.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)).thenReturn(true); + when(mockConfig.getStringList(Constant.LOCAL_WITNESS_KEYSTORE)).thenReturn(new ArrayList<>()); + witnessInitializer = new WitnessInitializer(mockConfig); + witnessInitializer.initLocalWitnesses(); + verify(mockConfig, times(1)).getStringList(Constant.LOCAL_WITNESS_KEYSTORE); + + List keystores = new ArrayList<>(); + keystores.add("keystore1.json"); + keystores.add("keystore2.json"); + when(mockConfig.hasPath(Constant.LOCAL_WITNESS_KEYSTORE)).thenReturn(true); + when(mockConfig.getStringList(Constant.LOCAL_WITNESS_KEYSTORE)).thenReturn(keystores); + + Field password = CommonParameter.class.getDeclaredField("password"); + password.setAccessible(true); + password.set(Args.getInstance(), "password"); + + try (MockedStatic mockedWalletUtils = mockStatic(WalletUtils.class); + MockedStatic mockedByteArray = mockStatic(ByteArray.class)) { + // Mock WalletUtils.loadCredentials + Credentials credentials = mock(Credentials.class); + SignInterface signInterface = mock(SignInterface.class); + when(credentials.getSignInterface()).thenReturn(signInterface); + byte[] keyBytes = Hex.decode(privateKey); + when(signInterface.getPrivateKey()).thenReturn(keyBytes); + mockedWalletUtils.when(() -> WalletUtils.loadCredentials(anyString(), any(File.class))) + .thenReturn(credentials); + mockedByteArray.when(() -> ByteArray.toHexString(any())).thenReturn(privateKey); + mockedByteArray.when(() -> ByteArray.fromHexString(anyString())).thenReturn(keyBytes); + + witnessInitializer = new WitnessInitializer(mockConfig); + Field localWitnessField = WitnessInitializer.class.getDeclaredField("localWitnesses"); + localWitnessField.setAccessible(true); + localWitnessField.set(witnessInitializer, new LocalWitnesses(privateKey)); + LocalWitnesses localWitnesses = witnessInitializer.initLocalWitnesses(); + assertFalse(localWitnesses.getPrivateKeys().isEmpty()); + } + } + + @Test + public void testGetWitnessAddress() + throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, + NoSuchFieldException { + witnessInitializer = new WitnessInitializer(config); + Method method = WitnessInitializer.class.getDeclaredMethod( + "getWitnessAddress"); + method.setAccessible(true); + byte[] result = (byte[]) method.invoke(witnessInitializer); + assertNull(result); + + String configString = "localWitnessAccountAddress = " + address; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + Field localWitnessField = WitnessInitializer.class.getDeclaredField("localWitnesses"); + localWitnessField.setAccessible(true); + localWitnessField.set(witnessInitializer, new LocalWitnesses(privateKey)); + result = (byte[]) method.invoke(witnessInitializer); + assertNotNull(result); + + configString = "localWitnessAccountAddress = " + invalidAddress; + config = ConfigFactory.parseString(configString); + witnessInitializer = new WitnessInitializer(config); + InvocationTargetException thrown = assertThrows(InvocationTargetException.class, + () -> method.invoke(witnessInitializer)); + TronError targetException = (TronError) thrown.getTargetException(); + assertEquals(ErrCode.WITNESS_INIT, targetException.getErrCode()); + } +} diff --git a/framework/src/test/java/org/tron/core/db/AccountStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountStoreTest.java index 9249a3358dc..aab64df16c7 100755 --- a/framework/src/test/java/org/tron/core/db/AccountStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AccountStoreTest.java @@ -1,20 +1,29 @@ package org.tron.core.db; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import com.google.protobuf.ByteString; +import com.typesafe.config.Config; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.tron.common.BaseTest; import org.tron.common.utils.ByteArray; import org.tron.core.Constant; import org.tron.core.capsule.AccountCapsule; import org.tron.core.config.args.Args; import org.tron.core.db2.ISession; +import org.tron.core.exception.TronError; +import org.tron.core.net.peer.PeerManager; import org.tron.core.store.AccountStore; import org.tron.core.store.AssetIssueStore; import org.tron.core.store.DynamicPropertiesStore; @@ -61,6 +70,21 @@ public void init() { init = true; } + @Test + public void setAccountTest() throws Exception { + Field field = AccountStore.class.getDeclaredField("assertsAddress"); + field.setAccessible(true); + field.set(AccountStore.class, new HashMap<>()); + Config config = mock(Config.class); + Mockito.when(config.getObjectList("genesis.block.assets")).thenReturn(new ArrayList<>()); + try { + AccountStore.setAccount(config); + Assert.fail(); + } catch (Throwable e) { + Assert.assertTrue(e instanceof TronError); + } + } + @Test public void get() { //test get and has Method diff --git a/framework/src/test/java/org/tron/core/db/DBIteratorTest.java b/framework/src/test/java/org/tron/core/db/DBIteratorTest.java index b4f7ca424c0..100502428d0 100644 --- a/framework/src/test/java/org/tron/core/db/DBIteratorTest.java +++ b/framework/src/test/java/org/tron/core/db/DBIteratorTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.rocksdb.ReadOptions; import org.rocksdb.RocksDB; import org.rocksdb.RocksDBException; import org.tron.core.db.common.iterator.RockStoreIterator; @@ -83,7 +84,7 @@ public void testRocksDb() throws RocksDBException, IOException { RocksDB db = RocksDB.open(options, file.toString())) { db.put("1".getBytes(StandardCharsets.UTF_8), "1".getBytes(StandardCharsets.UTF_8)); db.put("2".getBytes(StandardCharsets.UTF_8), "2".getBytes(StandardCharsets.UTF_8)); - RockStoreIterator iterator = new RockStoreIterator(db.newIterator()); + RockStoreIterator iterator = new RockStoreIterator(db.newIterator(), new ReadOptions()); iterator.seekToFirst(); Assert.assertArrayEquals("1".getBytes(StandardCharsets.UTF_8), iterator.getKey()); Assert.assertArrayEquals("1".getBytes(StandardCharsets.UTF_8), iterator.next().getValue()); @@ -99,7 +100,7 @@ public void testRocksDb() throws RocksDBException, IOException { Assert.assertTrue(e instanceof IllegalStateException); } - iterator = new RockStoreIterator(db.newIterator()); + iterator = new RockStoreIterator(db.newIterator(), new ReadOptions()); iterator.seekToLast(); Assert.assertArrayEquals("2".getBytes(StandardCharsets.UTF_8), iterator.getKey()); Assert.assertArrayEquals("2".getBytes(StandardCharsets.UTF_8), iterator.getValue()); diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index db219377b74..fc3b5c18265 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -33,6 +33,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.tron.api.GrpcAPI; import org.tron.common.application.TronApplicationContext; import org.tron.common.crypto.ECKey; import org.tron.common.runtime.RuntimeImpl; @@ -52,7 +53,10 @@ import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.AssetIssueCapsule; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.BytesCapsule; import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.capsule.TransactionInfoCapsule; +import org.tron.core.capsule.TransactionRetCapsule; import org.tron.core.capsule.WitnessCapsule; import org.tron.core.config.DefaultConfig; import org.tron.core.config.Parameter; @@ -136,7 +140,7 @@ public void init() throws IOException { localWitnesses = new LocalWitnesses(); localWitnesses.setPrivateKeys(Arrays.asList(privateKey)); - localWitnesses.initWitnessAccountAddress(true); + localWitnesses.initWitnessAccountAddress(null, true); Args.setLocalWitnesses(localWitnesses); blockCapsule2 = @@ -316,7 +320,7 @@ public void transactionTest() { dbManager.pushTransaction(trans0); dbManager.pushTransaction(trans); } catch (Exception e) { - Assert.assertTrue(e instanceof TaposException); + Assert.assertTrue(e instanceof ContractValidateException); } dbManager.rePush(trans0); ReflectUtils.invokeMethod(dbManager,"filterOwnerAddress", @@ -858,6 +862,35 @@ public void getVerifyTxsTest() { dbManager.getPendingTransactions().add(t3); txs = dbManager.getVerifyTxs(capsule); Assert.assertEquals(txs.size(), 2); + + dbManager.getPendingTransactions().clear(); + capsule = new BlockCapsule(0, ByteString.EMPTY, 0, list); + dbManager.getPendingTransactions().add(t1); + dbManager.getPendingTransactions().add(t2); + txs = dbManager.getVerifyTxs(capsule); + Assert.assertEquals(txs.size(), 0); + + dbManager.getPendingTransactions().clear(); + Transaction t1Bak = t1.getInstance().toBuilder() + .addSignature(ByteString.copyFrom("a".getBytes())).build(); + dbManager.getPendingTransactions().add(new TransactionCapsule(t1Bak)); + txs = dbManager.getVerifyTxs(capsule); + Assert.assertEquals(t1.getTransactionId(), new TransactionCapsule(t1Bak).getTransactionId()); + Assert.assertEquals(txs.size(), 2); + + dbManager.getPendingTransactions().clear(); + list.clear(); + list.add(t1Bak); + capsule = new BlockCapsule(0, ByteString.EMPTY, 0, list); + + Transaction t2Bak = t1.getInstance().toBuilder() + .addSignature(ByteString.copyFrom("a".getBytes())) + .addSignature(ByteString.copyFrom("b".getBytes())).build(); + Assert.assertEquals(new TransactionCapsule(t1Bak).getTransactionId(), + new TransactionCapsule(t2Bak).getTransactionId()); + dbManager.getPendingTransactions().add(new TransactionCapsule(t2Bak)); + txs = dbManager.getVerifyTxs(capsule); + Assert.assertEquals(txs.size(), 1); } @Test @@ -1233,6 +1266,47 @@ public void testExpiration() { } + @Test + public void testGetTransactionInfoByBlockNum() throws Exception { + + Transaction transaction = Protocol.Transaction.newBuilder() + .addSignature(ByteString.copyFrom(new byte[1])).build(); + TransactionCapsule transactionCapsule = new TransactionCapsule(transaction); + + Protocol.BlockHeader.raw raw = Protocol.BlockHeader.raw.newBuilder().setNumber(1000L).build(); + Protocol.BlockHeader header = Protocol.BlockHeader.newBuilder().setRawData(raw).build(); + Block block = Block.newBuilder().setBlockHeader(header).addTransactions(transaction).build(); + + Protocol.TransactionInfo info = Protocol.TransactionInfo.newBuilder() + .setBlockNumber(1000L).build(); + + BlockCapsule blockCapsule = new BlockCapsule(block); + byte[] blockId = new BlockCapsule(block).getBlockId().getBytes(); + dbManager.getBlockIndexStore().put(ByteArray.fromLong(1000L), new BytesCapsule(blockId)); + dbManager.getBlockStore().put(blockId, blockCapsule); + dbManager.getTransactionHistoryStore().put(transactionCapsule.getTransactionId().getBytes(), + new TransactionInfoCapsule(info)); + + GrpcAPI.TransactionInfoList transactionInfoList = dbManager.getTransactionInfoByBlockNum(1000L); + + Assert.assertEquals(1, transactionInfoList.getTransactionInfoCount()); + Assert.assertEquals(1, transactionInfoList.getTransactionInfoList().size()); + + Protocol.TransactionRet ret = Protocol.TransactionRet.newBuilder() + .addTransactioninfo(info) + .addTransactioninfo(info).build(); + + TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(ret.toByteArray()); + + dbManager.getTransactionRetStore() + .put(ByteArray.fromLong(1000L), transactionRetCapsule); + + transactionInfoList = dbManager.getTransactionInfoByBlockNum(1000L); + + Assert.assertEquals(2, transactionInfoList.getTransactionInfoCount()); + Assert.assertEquals(2, transactionInfoList.getTransactionInfoList().size()); + } + @Test public void blockTrigger() { Manager manager = spy(new Manager()); diff --git a/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java b/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java index 420b54525e4..f563203b71a 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java @@ -57,7 +57,7 @@ private void initLocalWitness() { String randomPrivateKey = PublicMethod.getRandomPrivateKey(); LocalWitnesses localWitnesses = new LocalWitnesses(); localWitnesses.setPrivateKeys(Arrays.asList(randomPrivateKey)); - localWitnesses.initWitnessAccountAddress(true); + localWitnesses.initWitnessAccountAddress(null, true); Args.setLocalWitnesses(localWitnesses); } @@ -85,7 +85,7 @@ public void testExpireTransaction() { TransferContract transferContract = TransferContract.newBuilder() .setAmount(1L) .setOwnerAddress(ByteString.copyFrom(Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()))) + .getWitnessAccountAddress())) .setToAddress(ByteString.copyFrom(ByteArray.fromHexString( (Wallet.getAddressPreFixString() + "A389132D6639FBDA4FBC8B659264E6B7C90DB086")))) .build(); @@ -116,8 +116,7 @@ public void testExpireTransactionNew() { .saveLatestBlockHeaderTimestamp(blockCapsule.getTimeStamp()); dbManager.updateRecentBlock(blockCapsule); initLocalWitness(); - byte[] address = Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()); + byte[] address = Args.getLocalWitnesses().getWitnessAccountAddress(); ByteString addressByte = ByteString.copyFrom(address); AccountCapsule accountCapsule = new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build()); @@ -157,8 +156,7 @@ public void testTransactionApprovedList() { dbManager.updateRecentBlock(blockCapsule); initLocalWitness(); - byte[] address = Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()); + byte[] address = Args.getLocalWitnesses().getWitnessAccountAddress(); TransferContract transferContract = TransferContract.newBuilder() .setAmount(1L) .setOwnerAddress(ByteString.copyFrom(address)) diff --git a/framework/src/test/java/org/tron/core/db/WitnessStoreTest.java b/framework/src/test/java/org/tron/core/db/WitnessStoreTest.java index d141a5fd790..521d048f23e 100755 --- a/framework/src/test/java/org/tron/core/db/WitnessStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/WitnessStoreTest.java @@ -62,12 +62,12 @@ public void testSortWitness() { List witnessAddress = allWitnesses.stream().map(WitnessCapsule::getAddress) .collect(Collectors.toList()); this.witnessStore.sortWitness(witnessAddress, false); - this.witnessStore.sortWitnesses(allWitnesses, false); + WitnessStore.sortWitnesses(allWitnesses, false); Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress) .collect(Collectors.toList())); List pre = new ArrayList<>(witnessAddress); this.witnessStore.sortWitness(witnessAddress, true); - this.witnessStore.sortWitnesses(allWitnesses, true); + WitnessStore.sortWitnesses(allWitnesses, true); Assert.assertEquals(witnessAddress, allWitnesses.stream().map(WitnessCapsule::getAddress) .collect(Collectors.toList())); Assert.assertNotEquals(pre, witnessAddress); diff --git a/framework/src/test/java/org/tron/core/db2/ChainbaseTest.java b/framework/src/test/java/org/tron/core/db2/ChainbaseTest.java index ee9813be633..4ab36ad2c88 100644 --- a/framework/src/test/java/org/tron/core/db2/ChainbaseTest.java +++ b/framework/src/test/java/org/tron/core/db2/ChainbaseTest.java @@ -76,9 +76,7 @@ public void initDb() throws IOException { public void testPrefixQueryForLeveldb() { LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "testPrefixQueryForLeveldb"); - dataSource.initDB(); - this.chainbase = new Chainbase(new SnapshotRoot( - new LevelDB(dataSource))); + this.chainbase = new Chainbase(new SnapshotRoot(new LevelDB(dataSource))); testDb(chainbase); testRoot(dataSource); chainbase.reset(); @@ -89,7 +87,6 @@ public void testPrefixQueryForLeveldb() { public void testPrefixQueryForRocksdb() { RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl( Args.getInstance().getOutputDirectory(), "testPrefixQueryForRocksdb"); - dataSource.initDB(); this.chainbase = new Chainbase(new SnapshotRoot( new org.tron.core.db2.common.RocksDB(dataSource))); testDb(chainbase); diff --git a/framework/src/test/java/org/tron/core/db2/CheckpointV2Test.java b/framework/src/test/java/org/tron/core/db2/CheckpointV2Test.java index dff2d376fd5..fb7f1987e9f 100644 --- a/framework/src/test/java/org/tron/core/db2/CheckpointV2Test.java +++ b/framework/src/test/java/org/tron/core/db2/CheckpointV2Test.java @@ -4,7 +4,7 @@ import com.google.common.primitives.Bytes; import com.google.common.primitives.Longs; import com.google.protobuf.ByteString; -import java.io.File; +import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -13,11 +13,12 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.FileUtil; import org.tron.common.utils.Sha256Hash; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; @@ -34,10 +35,12 @@ public class CheckpointV2Test { private TronApplicationContext context; private Application appT; private TestRevokingTronStore tronDatabase; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); @Before - public void init() { - Args.setParam(new String[]{"-d", "output_SnapshotManager_test"}, + public void init() throws IOException { + Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); Args.getInstance().getStorage().setCheckpointVersion(2); Args.getInstance().getStorage().setCheckpointSync(true); @@ -54,9 +57,6 @@ public void removeDb() { Args.clearParam(); context.destroy(); tronDatabase.close(); - FileUtil.deleteDir(new File("output_SnapshotManager_test")); - revokingDatabase.getCheckTmpStore().close(); - tronDatabase.close(); } @Test diff --git a/framework/src/test/java/org/tron/core/db2/RevokingDbWithCacheNewValueTest.java b/framework/src/test/java/org/tron/core/db2/RevokingDbWithCacheNewValueTest.java index 2290df86978..f371f2348a7 100644 --- a/framework/src/test/java/org/tron/core/db2/RevokingDbWithCacheNewValueTest.java +++ b/framework/src/test/java/org/tron/core/db2/RevokingDbWithCacheNewValueTest.java @@ -1,22 +1,22 @@ package org.tron.core.db2; -import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.RandomStringUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; import org.tron.common.utils.ByteArray; -import org.tron.common.utils.FileUtil; import org.tron.common.utils.SessionOptional; import org.tron.core.Constant; import org.tron.core.capsule.utils.MarketUtils; @@ -34,12 +34,14 @@ public class RevokingDbWithCacheNewValueTest { private TronApplicationContext context; private Application appT; private TestRevokingTronStore tronDatabase; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); private String databasePath = ""; @Before - public void init() { - databasePath = "output_revokingStore_test_" + RandomStringUtils.randomAlphanumeric(10); + public void init() throws IOException { + databasePath = temporaryFolder.newFolder().toString(); Args.setParam(new String[]{"-d", databasePath}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); @@ -51,7 +53,6 @@ public void removeDb() { Args.clearParam(); context.destroy(); tronDatabase.close(); - FileUtil.deleteDir(new File(databasePath)); } @Test diff --git a/framework/src/test/java/org/tron/core/db2/SnapshotImplTest.java b/framework/src/test/java/org/tron/core/db2/SnapshotImplTest.java index aab6f656b1f..649056aa151 100644 --- a/framework/src/test/java/org/tron/core/db2/SnapshotImplTest.java +++ b/framework/src/test/java/org/tron/core/db2/SnapshotImplTest.java @@ -3,16 +3,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import java.io.File; +import java.io.IOException; import java.lang.reflect.Constructor; import org.junit.After; -import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.FileUtil; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; @@ -26,10 +26,12 @@ public class SnapshotImplTest { private TronApplicationContext context; private Application appT; private SnapshotManager revokingDatabase; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); @Before - public void init() { - Args.setParam(new String[]{"-d", "output_revokingStore_test"}, Constant.TEST_CONF); + public void init() throws IOException { + Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); @@ -44,10 +46,7 @@ public void init() { public void removeDb() { Args.clearParam(); context.destroy(); - FileUtil.deleteDir(new File("output_revokingStore_test")); - tronDatabase.close(); - revokingDatabase.shutdown(); } /** diff --git a/framework/src/test/java/org/tron/core/db2/SnapshotManagerTest.java b/framework/src/test/java/org/tron/core/db2/SnapshotManagerTest.java index 134dc99e51c..d6fd319e6a0 100644 --- a/framework/src/test/java/org/tron/core/db2/SnapshotManagerTest.java +++ b/framework/src/test/java/org/tron/core/db2/SnapshotManagerTest.java @@ -6,7 +6,7 @@ import com.google.common.collect.Maps; import com.google.common.primitives.Longs; import com.google.protobuf.ByteString; -import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -15,11 +15,12 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.FileUtil; import org.tron.common.utils.Sha256Hash; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; @@ -40,11 +41,13 @@ public class SnapshotManagerTest { private TronApplicationContext context; private Application appT; private TestRevokingTronStore tronDatabase; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); @Before - public void init() { - Args.setParam(new String[]{"-d", "output_SnapshotManager_test"}, + public void init() throws IOException { + Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); @@ -59,9 +62,6 @@ public void removeDb() { Args.clearParam(); context.destroy(); tronDatabase.close(); - FileUtil.deleteDir(new File("output_SnapshotManager_test")); - revokingDatabase.getCheckTmpStore().close(); - tronDatabase.close(); } @Test diff --git a/framework/src/test/java/org/tron/core/db2/SnapshotRootTest.java b/framework/src/test/java/org/tron/core/db2/SnapshotRootTest.java index 70b4d9eff30..635cc018cc2 100644 --- a/framework/src/test/java/org/tron/core/db2/SnapshotRootTest.java +++ b/framework/src/test/java/org/tron/core/db2/SnapshotRootTest.java @@ -1,10 +1,13 @@ package org.tron.core.db2; import com.google.common.collect.Sets; -import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import lombok.AllArgsConstructor; @@ -13,7 +16,9 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.springframework.util.CollectionUtils; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; @@ -45,11 +50,13 @@ public class SnapshotRootTest { "exchange","market_order","account-trace","contract-state","trans")); private Set allDBNames; private Set allRevokingDBNames; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); @Before - public void init() { - Args.setParam(new String[]{"-d", "output_revokingStore_test"}, Constant.TEST_CONF); + public void init() throws IOException { + Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); } @@ -58,7 +65,6 @@ public void init() { public void removeDb() { Args.clearParam(); context.destroy(); - FileUtil.deleteDir(new File("output_revokingStore_test")); } @Test @@ -133,7 +139,9 @@ public void testSecondCacheCheck() throws ItemNotFoundException { revokingDatabase = context.getBean(SnapshotManager.class); allRevokingDBNames = parseRevokingDBNames(context); - allDBNames = Arrays.stream(new File("output_revokingStore_test/database").list()) + Path path = Paths.get(Args.getInstance().getOutputDirectory(), + Args.getInstance().getStorage().getDbDirectory()); + allDBNames = Arrays.stream(Objects.requireNonNull(path.toFile().list())) .collect(Collectors.toSet()); if (CollectionUtils.isEmpty(allDBNames)) { throw new ItemNotFoundException("No DBs found"); @@ -152,10 +160,13 @@ public void testSecondCacheCheckAddDb() revokingDatabase = context.getBean(SnapshotManager.class); allRevokingDBNames = parseRevokingDBNames(context); allRevokingDBNames.add("secondCheckTestDB"); - FileUtil.createDirIfNotExists("output_revokingStore_test/database/secondCheckTestDB"); - allDBNames = Arrays.stream(new File("output_revokingStore_test/database").list()) + Path path = Paths.get(Args.getInstance().getOutputDirectory(), + Args.getInstance().getStorage().getDbDirectory()); + Path secondCheckTestDB = Paths.get(path.toString(), "secondCheckTestDB"); + FileUtil.createDirIfNotExists(secondCheckTestDB.toString()); + allDBNames = Arrays.stream(Objects.requireNonNull(path.toFile().list())) .collect(Collectors.toSet()); - FileUtil.deleteDir(new File("output_revokingStore_test/database/secondCheckTestDB")); + FileUtil.deleteDir(secondCheckTestDB.toFile()); if (CollectionUtils.isEmpty(allDBNames)) { throw new ItemNotFoundException("No DBs found"); } diff --git a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java index e99433db3c6..82c887fad53 100644 --- a/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java +++ b/framework/src/test/java/org/tron/core/event/BlockEventCacheTest.java @@ -13,6 +13,8 @@ public class BlockEventCacheTest { @Test public void test() throws Exception { + BlockCapsule.BlockId solidID = new BlockCapsule.BlockId(getBlockId(), 100); + BlockEvent be1 = new BlockEvent(); BlockCapsule.BlockId b1 = new BlockCapsule.BlockId(getBlockId(), 1); be1.setBlockId(b1); @@ -36,32 +38,46 @@ public void test() throws Exception { BlockEventCache.init(b1); + BlockEvent event = new BlockEvent(); + BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(getBlockId(), 2); + event.setBlockId(blockId); + event.setParentId(b1); + event.setSolidId(blockId); + BlockEventCache.add(event); + Assert.assertEquals(event, BlockEventCache.getHead()); + Assert.assertEquals(blockId, BlockEventCache.getSolidId()); + Assert.assertEquals(event, BlockEventCache.getBlockEvent(blockId)); + + BlockEventCache.init(b1); + BlockEvent be2 = new BlockEvent(); BlockCapsule.BlockId b2 = new BlockCapsule.BlockId(getBlockId(), 2); be2.setBlockId(b2); be2.setParentId(b1); - be2.setSolidId(b1); + be2.setSolidId(solidID); BlockEventCache.add(be2); Assert.assertEquals(be2, BlockEventCache.getHead()); + Assert.assertEquals(b2, BlockEventCache.getSolidId()); Assert.assertEquals(be2, BlockEventCache.getBlockEvent(b2)); BlockEvent be22 = new BlockEvent(); BlockCapsule.BlockId b22 = new BlockCapsule.BlockId(getBlockId(), 2); be22.setBlockId(b22); be22.setParentId(b1); - be22.setSolidId(b22); + be22.setSolidId(solidID); BlockEventCache.add(be22); Assert.assertEquals(be2, BlockEventCache.getHead()); Assert.assertEquals(be22, BlockEventCache.getBlockEvent(b22)); - Assert.assertEquals(b22, BlockEventCache.getSolidId()); + Assert.assertEquals(b2, BlockEventCache.getSolidId()); BlockEvent be3 = new BlockEvent(); BlockCapsule.BlockId b3 = new BlockCapsule.BlockId(getBlockId(), 3); be3.setBlockId(b3); be3.setParentId(b22); - be3.setSolidId(b22); + be3.setSolidId(solidID); BlockEventCache.add(be3); Assert.assertEquals(be3, BlockEventCache.getHead()); + Assert.assertEquals(b3, BlockEventCache.getSolidId()); List list = BlockEventCache.getSolidBlockEvents(b2); Assert.assertEquals(1, list.size()); diff --git a/framework/src/test/java/org/tron/core/event/BlockEventGetTest.java b/framework/src/test/java/org/tron/core/event/BlockEventGetTest.java index b6835cfcf82..9ee46118c14 100644 --- a/framework/src/test/java/org/tron/core/event/BlockEventGetTest.java +++ b/framework/src/test/java/org/tron/core/event/BlockEventGetTest.java @@ -11,13 +11,15 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import lombok.extern.slf4j.Slf4j; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; +import org.tron.api.GrpcAPI; import org.tron.common.application.TronApplicationContext; import org.tron.common.logsfilter.EventPluginConfig; import org.tron.common.logsfilter.EventPluginLoader; @@ -63,26 +65,33 @@ public class BlockEventGetTest extends BlockGenerate { protected Manager dbManager; long currentHeader = -1; private TronNetDelegate tronNetDelegate; - private TronApplicationContext context; + private static TronApplicationContext context; static LocalDateTime localDateTime = LocalDateTime.now(); private long time = ZonedDateTime.of(localDateTime, ZoneId.systemDefault()).toInstant().toEpochMilli(); - protected void initDbPath() throws IOException { - dbPath = temporaryFolder.newFolder().toString(); + + public static String dbPath() { + try { + return temporaryFolder.newFolder().toString(); + } catch (IOException e) { + Assert.fail("create temp folder failed"); + } + return null; + } + + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath()}, Constant.TEST_CONF); + context = new TronApplicationContext(DefaultConfig.class); } @Before public void before() throws IOException { - initDbPath(); - logger.info("Full node running."); - Args.setParam(new String[] {"-d", dbPath}, Constant.TEST_CONF); Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet()); - context = new TronApplicationContext(DefaultConfig.class); - dbManager = context.getBean(Manager.class); setManager(dbManager); @@ -91,7 +100,7 @@ public void before() throws IOException { tronNetDelegate = context.getBean(TronNetDelegate.class); tronNetDelegate.setExit(false); currentHeader = dbManager.getDynamicPropertiesStore() - .getLatestBlockHeaderNumberFromDB(); + .getLatestBlockHeaderNumberFromDB(); ByteString addressBS = ByteString.copyFrom(address); WitnessCapsule witnessCapsule = new WitnessCapsule(addressBS); @@ -108,8 +117,10 @@ public void before() throws IOException { dps.saveAllowTvmShangHai(1); } - @After - public void after() throws IOException { + @AfterClass + public static void after() throws IOException { + context.destroy(); + Args.clearParam(); } @Test @@ -132,13 +143,13 @@ public void test() throws Exception { + "57c973388f044038eff0e6474425b38037e75e66d6b3047647290605449c7764736f6c63430008140033"; Protocol.Transaction trx = TvmTestUtils.generateDeploySmartContractAndGetTransaction( "TestTRC20", address, "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\"" - + ":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"}" - + ",{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\"," - + "\"type\":\"event\"}]", code, 0, (long) 1e9, 100, null, 1); + + ":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\"" + + ":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\"" + + ":\"Transfer\",\"type\":\"event\"}]", code, 0, (long) 1e9, 100, null, 1); trx = trx.toBuilder().addRet( - Protocol.Transaction.Result.newBuilder() - .setContractRetValue(Protocol.Transaction.Result.contractResult.SUCCESS_VALUE) - .build()).build(); + Protocol.Transaction.Result.newBuilder() + .setContractRetValue(Protocol.Transaction.Result.contractResult.SUCCESS_VALUE) + .build()).build(); Protocol.Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey); BlockCapsule blockCapsule = new BlockCapsule(block.toBuilder().addTransactions(trx).build()); @@ -223,6 +234,11 @@ public void getTransactionTriggers() throws Exception { bc = new BlockCapsule(100, ByteString.empty(), 1, transactionList); + Manager manager = mock(Manager.class); + ReflectUtils.setFieldValue(blockEventGet, "manager", manager); + Mockito.when(manager.getTransactionInfoByBlockNum(1)) + .thenReturn(GrpcAPI.TransactionInfoList.newBuilder().build()); + list = blockEventGet.getTransactionTriggers(bc, 1); Assert.assertEquals(1, list.size()); Assert.assertEquals(100, list.get(0).getTransactionLogTrigger().getTimeStamp()); @@ -243,21 +259,14 @@ public void getTransactionTriggers() throws Exception { .addContractResult(ByteString.copyFrom(ByteArray.fromHexString("112233"))) .setReceipt(resourceBuild.build()); - Manager manager = mock(Manager.class); - ReflectUtils.setFieldValue(blockEventGet, "manager", manager); - ChainBaseManager chainBaseManager = mock(ChainBaseManager.class); Mockito.when(manager.getChainBaseManager()).thenReturn(chainBaseManager); - TransactionRetStore transactionRetStore = mock(TransactionRetStore.class); - Mockito.when(chainBaseManager.getTransactionRetStore()).thenReturn(transactionRetStore); - - Protocol.TransactionRet transactionRet = Protocol.TransactionRet.newBuilder() - .addTransactioninfo(infoBuild.build()).build(); - TransactionRetCapsule result = new TransactionRetCapsule(transactionRet.toByteArray()); + GrpcAPI.TransactionInfoList result = GrpcAPI.TransactionInfoList.newBuilder() + .addTransactionInfo(infoBuild.build()).build(); - Mockito.when(transactionRetStore.getTransactionInfoByBlockNum(ByteArray.fromLong(0))) + Mockito.when(manager.getTransactionInfoByBlockNum(0)) .thenReturn(result); Protocol.Block block = Protocol.Block.newBuilder() @@ -276,8 +285,8 @@ public void getTransactionTriggers() throws Exception { Assert.assertEquals(2, list.get(0).getTransactionLogTrigger().getEnergyUsageTotal()); - Mockito.when(transactionRetStore.getTransactionInfoByBlockNum(ByteArray.fromLong(0))) - .thenReturn(null); + Mockito.when(manager.getTransactionInfoByBlockNum(0)) + .thenReturn(GrpcAPI.TransactionInfoList.newBuilder().build()); list = blockEventGet.getTransactionTriggers(blockCapsule, 1); Assert.assertEquals(1, list.size()); Assert.assertEquals(1, diff --git a/framework/src/test/java/org/tron/core/event/HistoryEventServiceTest.java b/framework/src/test/java/org/tron/core/event/HistoryEventServiceTest.java index 49f77ccf597..1485d726235 100644 --- a/framework/src/test/java/org/tron/core/event/HistoryEventServiceTest.java +++ b/framework/src/test/java/org/tron/core/event/HistoryEventServiceTest.java @@ -23,7 +23,7 @@ public class HistoryEventServiceTest { HistoryEventService historyEventService = new HistoryEventService(); - @Test + @Test(timeout = 60_000) public void test() throws Exception { EventPluginLoader instance = mock(EventPluginLoader.class); Mockito.when(instance.isUseNativeQueue()).thenReturn(true); @@ -43,6 +43,7 @@ public void test() throws Exception { RealtimeEventService realtimeEventService = new RealtimeEventService(); BlockEventLoad blockEventLoad = new BlockEventLoad(); ReflectUtils.setFieldValue(blockEventLoad, "instance", instance); + ReflectUtils.setFieldValue(blockEventLoad, "manager", manager); ReflectUtils.setFieldValue(historyEventService, "solidEventService", solidEventService); ReflectUtils.setFieldValue(historyEventService, "realtimeEventService", realtimeEventService); diff --git a/framework/src/test/java/org/tron/core/exception/TronErrorTest.java b/framework/src/test/java/org/tron/core/exception/TronErrorTest.java index b4c3dc4b07f..39fe1404b18 100644 --- a/framework/src/test/java/org/tron/core/exception/TronErrorTest.java +++ b/framework/src/test/java/org/tron/core/exception/TronErrorTest.java @@ -2,17 +2,25 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.util.ContextInitializer; +import ch.qos.logback.core.joran.spi.JoranException; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigObject; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.After; import org.junit.Assert; import org.junit.Rule; @@ -22,6 +30,8 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.slf4j.LoggerFactory; +import org.tron.common.arch.Arch; import org.tron.common.log.LogService; import org.tron.common.parameter.RateLimiterInitialization; import org.tron.common.utils.ReflectUtils; @@ -32,6 +42,7 @@ import org.tron.core.services.http.RateLimiterServlet; import org.tron.core.zen.ZksnarkInitService; + @RunWith(MockitoJUnitRunner.class) public class TronErrorTest { @@ -39,7 +50,7 @@ public class TronErrorTest { public TemporaryFolder temporaryFolder = new TemporaryFolder(); @After - public void clearMocks() { + public void clearMocks() { Mockito.clearAllCaches(); Args.clearParam(); } @@ -57,9 +68,14 @@ public void testTronError() { } @Test - public void ZksnarkInitTest() { + public void ZksnarkInitTest() throws IllegalAccessException, NoSuchFieldException { + Field field = ZksnarkInitService.class.getDeclaredField("initialized"); + field.setAccessible(true); + AtomicBoolean atomicBoolean = (AtomicBoolean) field.get(null); + boolean originalValue = atomicBoolean.get(); + atomicBoolean.set(false); + try (MockedStatic mock = mockStatic(JLibrustzcash.class)) { - mock.when(JLibrustzcash::isOpenZen).thenReturn(true); mock.when(() -> JLibrustzcash.librustzcashInitZksnarkParams(any())) .thenAnswer(invocation -> { throw new ZksnarkException("Zksnark init failed"); @@ -67,15 +83,29 @@ public void ZksnarkInitTest() { TronError thrown = assertThrows(TronError.class, ZksnarkInitService::librustzcashInitZksnarkParams); assertEquals(TronError.ErrCode.ZCASH_INIT, thrown.getErrCode()); + } finally { + atomicBoolean.set(originalValue); } } @Test public void LogLoadTest() throws IOException { - LogService.load("non-existent.xml"); - Path path = temporaryFolder.newFile("logback.xml").toPath(); - TronError thrown = assertThrows(TronError.class, () -> LogService.load(path.toString())); - assertEquals(TronError.ErrCode.LOG_LOAD, thrown.getErrCode()); + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + + try { + LogService.load("non-existent.xml"); + Path path = temporaryFolder.newFile("logback.xml").toPath(); + TronError thrown = assertThrows(TronError.class, () -> LogService.load(path.toString())); + assertEquals(TronError.ErrCode.LOG_LOAD, thrown.getErrCode()); + } finally { + try { + context.reset(); + ContextInitializer ci = new ContextInitializer(context); + ci.autoConfig(); + } catch (JoranException e) { + Assert.fail(e.getMessage()); + } + } } @Test @@ -106,7 +136,7 @@ public void rateLimiterServletInitTest() { @Test public void shutdownBlockTimeInitTest() { - Map params = new HashMap<>(); + Map params = new HashMap<>(); params.put(Constant.NODE_SHUTDOWN_BLOCK_TIME, "0"); params.put("storage.db.directory", "database"); Config config = ConfigFactory.defaultOverrides().withFallback( @@ -114,4 +144,58 @@ public void shutdownBlockTimeInitTest() { TronError thrown = assertThrows(TronError.class, () -> Args.setParam(config)); assertEquals(TronError.ErrCode.AUTO_STOP_PARAMS, thrown.getErrCode()); } + + @Test + public void testThrowIfUnsupportedJavaVersion() { + runArchTest("x86_64", "1.8", false); + runArchTest("x86_64", "11", true); + runArchTest("x86_64", "17", true); + runArchTest("aarch64", "17", false); + runArchTest("aarch64", "1.8", true); + runArchTest("aarch64", "11", true); + } + + private void runArchTest(String osArch, String javaVersion, boolean expectThrow) { + try (MockedStatic mocked = mockStatic(Arch.class)) { + boolean isX86 = "x86_64".equals(osArch); + boolean isArm64 = "aarch64".equals(osArch); + + boolean isJava8 = "1.8".equals(javaVersion); + boolean isJava17 = "17".equals(javaVersion); + + mocked.when(Arch::isX86).thenReturn(isX86); + mocked.when(Arch::isArm64).thenReturn(isArm64); + + mocked.when(Arch::isJava8).thenReturn(isJava8); + mocked.when(Arch::isJava17).thenReturn(isJava17); + + mocked.when(Arch::getOsArch).thenReturn(osArch); + mocked.when(Arch::javaSpecificationVersion).thenReturn(javaVersion); + mocked.when(Arch::withAll).thenReturn(String.format( + "Architecture: %s, Java Version: %s", osArch, javaVersion)); + + mocked.when(Arch::throwIfUnsupportedJavaVersion).thenCallRealMethod(); + + if (expectThrow) { + TronError err = assertThrows( + TronError.class, () -> Args.setParam(new String[]{}, Constant.TEST_CONF)); + + String expectedJavaVersion = isX86 ? "1.8" : "17"; + String expectedMessage = String.format( + "Java %s is required for %s architecture. Detected version %s", + expectedJavaVersion, osArch, javaVersion); + assertEquals(expectedMessage, err.getCause().getMessage()); + assertEquals(TronError.ErrCode.JDK_VERSION, err.getErrCode()); + mocked.verify(Arch::withAll, times(1)); + } else { + try { + Arch.throwIfUnsupportedJavaVersion(); + } catch (Exception e) { + fail("Expected no exception, but got: " + e.getMessage()); + } + mocked.verify(Arch::withAll, never()); + } + } + } + } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java b/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java index 570e7ed3498..f62d47d5367 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java @@ -4,10 +4,13 @@ import static org.tron.keystore.Wallet.generateRandomBytes; import com.google.protobuf.ByteString; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.tron.common.utils.ByteArray; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.config.args.Args; import org.tron.core.services.jsonrpc.JsonRpcApiUtil; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.BlockHeader; @@ -16,6 +19,16 @@ public class ApiUtilTest { + @BeforeClass + public static void init() { + Args.setParam(new String[]{}, "config-localtest.conf"); + } + + @AfterClass + public static void clear() { + Args.clearParam(); + } + @Test public void testGetBlockID() { byte[] mockedHash = generateRandomBytes(128); diff --git a/framework/src/test/java/org/tron/core/jsonrpc/BloomTest.java b/framework/src/test/java/org/tron/core/jsonrpc/BloomTest.java index 19e232b0f93..c88615d2d2f 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/BloomTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/BloomTest.java @@ -131,7 +131,7 @@ private TransactionInfo createTransactionInfo(byte[] address1, byte[] address2) @Test public void benchmarkCreateByTransaction() { - int times = 10000; + int times = 1000; // small TransactionRetCapsule smallCapsule = new TransactionRetCapsule(); diff --git a/framework/src/test/java/org/tron/core/jsonrpc/ConcurrentHashMapTest.java b/framework/src/test/java/org/tron/core/jsonrpc/ConcurrentHashMapTest.java index 1de106f68e8..abb73671161 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/ConcurrentHashMapTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/ConcurrentHashMapTest.java @@ -31,7 +31,7 @@ private static int randomInt(int minInt, int maxInt) { */ @Test public void testHandleBlockHash() { - int times = 200; + int times = 100; int eachCount = 200; Map conMap = TronJsonRpcImpl.getBlockFilter2ResultFull(); @@ -67,7 +67,7 @@ public void run() { TronJsonRpcImpl.handleBLockFilter(blockFilterCapsule); } try { - Thread.sleep(randomInt(100, 200)); + Thread.sleep(randomInt(50, 100)); } catch (InterruptedException e) { e.printStackTrace(); } @@ -81,7 +81,7 @@ public void run() { for (int t = 1; t <= times * 2; t++) { try { - Thread.sleep(randomInt(100, 200)); + Thread.sleep(randomInt(50, 100)); } catch (InterruptedException e) { e.printStackTrace(); } @@ -111,7 +111,7 @@ public void run() { for (int t = 1; t <= times * 2; t++) { try { - Thread.sleep(randomInt(100, 200)); + Thread.sleep(randomInt(50, 100)); } catch (InterruptedException e) { e.printStackTrace(); } @@ -144,7 +144,7 @@ public void run() { for (int t = 1; t <= times * 2; t++) { try { - Thread.sleep(randomInt(100, 200)); + Thread.sleep(randomInt(50, 100)); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java index bef0b5a1593..bd357101da3 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java @@ -18,7 +18,7 @@ import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.utils.Commons; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.JsonRpcApiUtil; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.filters.LogBlockQuery; diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 0f2214c5c9c..81db38ce286 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -1,6 +1,7 @@ package org.tron.core.jsonrpc; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getByJsonBlockId; +import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR; import com.alibaba.fastjson.JSON; import com.google.gson.JsonArray; @@ -33,9 +34,12 @@ import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.capsule.TransactionInfoCapsule; +import org.tron.core.capsule.TransactionRetCapsule; import org.tron.core.capsule.utils.BlockUtil; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.interfaceJsonRpcOnPBFT.JsonRpcServiceOnPBFT; import org.tron.core.services.interfaceJsonRpcOnSolidity.JsonRpcServiceOnSolidity; @@ -44,6 +48,7 @@ import org.tron.core.services.jsonrpc.TronJsonRpcImpl; import org.tron.core.services.jsonrpc.filters.LogFilterWrapper; import org.tron.core.services.jsonrpc.types.BlockResult; +import org.tron.core.services.jsonrpc.types.TransactionReceipt; import org.tron.core.services.jsonrpc.types.TransactionResult; import org.tron.protos.Protocol; import org.tron.protos.Protocol.Transaction.Contract.ContractType; @@ -127,6 +132,7 @@ public void init() { (Wallet.getAddressPreFixString() + "ED738B3A0FE390EAA71B768B6D02CDBD18FB207B")))) .build(); + transactionCapsule1 = new TransactionCapsule(transferContract1, ContractType.TransferContract); transactionCapsule1.setBlockNum(blockCapsule1.getNum()); TransactionCapsule transactionCapsule2 = new TransactionCapsule(transferContract2, @@ -155,6 +161,42 @@ public void init() { dbManager.getTransactionStore() .put(transactionCapsule3.getTransactionId().getBytes(), transactionCapsule3); + dbManager.getTransactionStore() + .put(transactionCapsule3.getTransactionId().getBytes(), transactionCapsule3); + + List logs = new ArrayList<>(); + logs.add(Protocol.TransactionInfo.Log.newBuilder() + .setAddress(ByteString.copyFrom("address1".getBytes())) + .setData(ByteString.copyFrom("data1".getBytes())) + .addTopics(ByteString.copyFrom("topic1".getBytes())) + .build()); + logs.add(Protocol.TransactionInfo.Log.newBuilder() + .setAddress(ByteString.copyFrom("address2".getBytes())) + .setData(ByteString.copyFrom("data2".getBytes())) + .addTopics(ByteString.copyFrom("topic2".getBytes())) + .build()); + + TransactionRetCapsule transactionRetCapsule1 = new TransactionRetCapsule(); + blockCapsule1.getTransactions().forEach(tx -> { + TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule(); + transactionInfoCapsule.setId(tx.getTransactionId().getBytes()); + transactionInfoCapsule.setBlockNumber(blockCapsule1.getNum()); + transactionInfoCapsule.addAllLog(logs); + transactionRetCapsule1.addTransactionInfo(transactionInfoCapsule.getInstance()); + }); + dbManager.getTransactionRetStore() + .put(ByteArray.fromLong(blockCapsule1.getNum()), transactionRetCapsule1); + + TransactionRetCapsule transactionRetCapsule2 = new TransactionRetCapsule(); + blockCapsule2.getTransactions().forEach(tx -> { + TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule(); + transactionInfoCapsule.setId(tx.getTransactionId().getBytes()); + transactionInfoCapsule.setBlockNumber(blockCapsule2.getNum()); + transactionRetCapsule2.addTransactionInfo(transactionInfoCapsule.getInstance()); + }); + dbManager.getTransactionRetStore() + .put(ByteArray.fromLong(blockCapsule2.getNum()), transactionRetCapsule2); + tronJsonRpc = new TronJsonRpcImpl(nodeInfoService, wallet, dbManager); } @@ -281,6 +323,8 @@ public void testGetBlockByNumber() { } Assert.assertEquals(ByteArray.toJsonHex(0L), blockResult.getNumber()); Assert.assertEquals(ByteArray.toJsonHex(blockCapsule0.getNum()), blockResult.getNumber()); + Assert.assertEquals(blockResult.getTransactions().length, + blockCapsule0.getTransactions().size()); // latest try { @@ -434,7 +478,8 @@ public void testGetByJsonBlockId() { getByJsonBlockId("0xxabc", wallet); Assert.fail("Expected to be thrown"); } catch (Exception e) { - Assert.assertEquals("For input string: \"xabc\"", e.getMessage()); + // https://bugs.openjdk.org/browse/JDK-8176425, from JDK 12, the exception message is changed + Assert.assertTrue(e.getMessage().startsWith("For input string: \"xabc\"")); } } @@ -968,4 +1013,90 @@ public void testNewFilterFinalizedBlock() { Assert.assertEquals("invalid block range params", e.getMessage()); } } + + @Test + public void testGetBlockReceipts() { + + try { + List transactionReceiptList = tronJsonRpc.getBlockReceipts("0x2710"); + Assert.assertFalse(transactionReceiptList.isEmpty()); + for (TransactionReceipt transactionReceipt: transactionReceiptList) { + TransactionReceipt transactionReceipt1 + = tronJsonRpc.getTransactionReceipt(transactionReceipt.getTransactionHash()); + + Assert.assertEquals( + JSON.toJSONString(transactionReceipt), JSON.toJSONString(transactionReceipt1)); + } + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + try { + List transactionReceiptList = tronJsonRpc.getBlockReceipts("earliest"); + Assert.assertNull(transactionReceiptList); + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + try { + List transactionReceiptList = tronJsonRpc.getBlockReceipts("latest"); + Assert.assertFalse(transactionReceiptList.isEmpty()); + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + try { + List transactionReceiptList = tronJsonRpc.getBlockReceipts("finalized"); + Assert.assertFalse(transactionReceiptList.isEmpty()); + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + try { + tronJsonRpc.getBlockReceipts("pending"); + Assert.fail(); + } catch (Exception e) { + Assert.assertEquals(TAG_PENDING_SUPPORT_ERROR, e.getMessage()); + } + + try { + tronJsonRpc.getBlockReceipts("test"); + Assert.fail(); + } catch (Exception e) { + Assert.assertEquals("invalid block number", e.getMessage()); + } + + try { + List transactionReceiptList = tronJsonRpc.getBlockReceipts("0x2"); + Assert.assertNull(transactionReceiptList); + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + try { + String blockHash = blockCapsule1.getBlockId().toString(); + List transactionReceiptList + = tronJsonRpc.getBlockReceipts(blockHash); + List transactionReceiptList2 + = tronJsonRpc.getBlockReceipts("0x" + blockHash); + + Assert.assertFalse(transactionReceiptList.isEmpty()); + Assert.assertEquals(JSON.toJSONString(transactionReceiptList), + JSON.toJSONString(transactionReceiptList2)); + } catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) { + throw new RuntimeException(e); + } + + } + + @Test + public void testWeb3ClientVersion() { + try { + String[] versions = tronJsonRpc.web3ClientVersion().split("/"); + String javaVersion = versions[versions.length - 1]; + Assert.assertTrue("Java1.8".equals(javaVersion) || "Java17".equals(javaVersion)); + } catch (Exception e) { + Assert.fail(); + } + } } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/LogBlockQueryTest.java b/framework/src/test/java/org/tron/core/jsonrpc/LogBlockQueryTest.java new file mode 100644 index 00000000000..7442e92c8f5 --- /dev/null +++ b/framework/src/test/java/org/tron/core/jsonrpc/LogBlockQueryTest.java @@ -0,0 +1,108 @@ +package org.tron.core.jsonrpc; + +import java.lang.reflect.Method; +import java.util.BitSet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import javax.annotation.Resource; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; +import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; +import org.tron.core.services.jsonrpc.filters.LogBlockQuery; +import org.tron.core.services.jsonrpc.filters.LogFilterWrapper; +import org.tron.core.store.SectionBloomStore; + +public class LogBlockQueryTest extends BaseTest { + + @Resource + SectionBloomStore sectionBloomStore; + private ExecutorService sectionExecutor; + private Method partialMatchMethod; + private static final long CURRENT_MAX_BLOCK_NUM = 50000L; + + static { + Args.setParam(new String[] {"--output-directory", dbPath()}, Constant.TEST_CONF); + } + + @Before + public void setup() throws Exception { + sectionExecutor = Executors.newFixedThreadPool(5); + + // Get private method through reflection + partialMatchMethod = LogBlockQuery.class.getDeclaredMethod("partialMatch", + int[][].class, int.class); + partialMatchMethod.setAccessible(true); + + BitSet bitSet = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); + bitSet.set(0, SectionBloomStore.BLOCK_PER_SECTION); + sectionBloomStore.put(0, 1, bitSet); + sectionBloomStore.put(0, 2, bitSet); + sectionBloomStore.put(0, 3, bitSet); + BitSet bitSet2 = new BitSet(SectionBloomStore.BLOCK_PER_SECTION); + bitSet2.set(0); + sectionBloomStore.put(1, 1, bitSet2); + sectionBloomStore.put(1, 2, bitSet2); + sectionBloomStore.put(1, 3, bitSet2); + } + + @After + public void tearDown() { + if (sectionExecutor != null && !sectionExecutor.isShutdown()) { + sectionExecutor.shutdown(); + } + } + + @Test + public void testPartialMatch() throws Exception { + // Create a basic LogFilterWrapper + LogFilterWrapper logFilterWrapper = new LogFilterWrapper( + new FilterRequest("0x0", "0x1", null, null, null), + CURRENT_MAX_BLOCK_NUM, null, false); + + LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, sectionBloomStore, + CURRENT_MAX_BLOCK_NUM, sectionExecutor); + + int section = 0; + + // Create a hit condition + int[][] bitIndexes = new int[][] { + {1, 2, 3}, // topic0 + {4, 5, 6} // topic1 + }; + + // topic0 hit section 0 + BitSet result = (BitSet) partialMatchMethod.invoke(logBlockQuery, bitIndexes, section); + Assert.assertNotNull(result); + Assert.assertEquals(SectionBloomStore.BLOCK_PER_SECTION, result.cardinality()); + + // topic0 hit section 1 + result = (BitSet) partialMatchMethod.invoke(logBlockQuery, bitIndexes, 1); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.cardinality()); + + // not exist section 2 + result = (BitSet) partialMatchMethod.invoke(logBlockQuery, bitIndexes, 2); + Assert.assertNotNull(result); + Assert.assertTrue(result.isEmpty()); + + //not hit + bitIndexes = new int[][] { + {1, 2, 4}, // topic0 + {3, 5, 6} // topic1 + }; + result = (BitSet) partialMatchMethod.invoke(logBlockQuery, bitIndexes, section); + Assert.assertNotNull(result); + Assert.assertTrue(result.isEmpty()); + + // null condition + bitIndexes = new int[0][]; + result = (BitSet) partialMatchMethod.invoke(logBlockQuery, bitIndexes, section); + Assert.assertNotNull(result); + Assert.assertTrue(result.isEmpty()); + } +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java b/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java index 600cc52b58e..0f9f125b74e 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java @@ -4,12 +4,13 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.junit.Assert; import org.junit.Test; import org.tron.common.runtime.vm.DataWord; import org.tron.common.runtime.vm.LogInfo; import org.tron.common.utils.ByteArray; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; import org.tron.core.services.jsonrpc.filters.LogFilter; @@ -220,6 +221,18 @@ public void testMatchBlock() { List elementList = matchBlock(logFilter, 100, null, transactionInfoList, false); Assert.assertEquals(1, elementList.size()); + + //test LogFilterElement + List elementList2 = + matchBlock(logFilter, 100, null, transactionInfoList, false); + Assert.assertEquals(1, elementList2.size()); + + LogFilterElement logFilterElement1 = elementList.get(0); + LogFilterElement logFilterElement2 = elementList2.get(0); + + Assert.assertEquals(logFilterElement1.hashCode(), logFilterElement2.hashCode()); + Assert.assertEquals(logFilterElement1, logFilterElement2); + } catch (JsonRpcInvalidParamsException e) { Assert.fail(); } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/SectionBloomStoreTest.java b/framework/src/test/java/org/tron/core/jsonrpc/SectionBloomStoreTest.java index 111370bfffd..953465b299e 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/SectionBloomStoreTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/SectionBloomStoreTest.java @@ -235,5 +235,7 @@ public void testWriteAndQuery() { } catch (Exception e) { Assert.fail(); } + + sectionExecutor.shutdownNow(); } } diff --git a/framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java b/framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java index 195e4749209..37c376ce9af 100644 --- a/framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java +++ b/framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java @@ -41,7 +41,7 @@ public class PrometheusApiServiceTest extends BaseTest { static LocalDateTime localDateTime = LocalDateTime.now(); @Resource private DposSlot dposSlot; - final int blocks = 512; + final int blocks = 100; private final String key = PublicMethod.getRandomPrivateKey(); private final byte[] privateKey = ByteArray.fromHexString(key); private static final AtomicInteger port = new AtomicInteger(0); diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java deleted file mode 100644 index 65771bae952..00000000000 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ /dev/null @@ -1,134 +0,0 @@ -package org.tron.core.net; - -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.DefaultMessageSizeEstimator; -import io.netty.channel.FixedRecvByteBufAllocator; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; -import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; -import io.netty.handler.timeout.ReadTimeoutHandler; -import io.netty.handler.timeout.WriteTimeoutHandler; -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import lombok.extern.slf4j.Slf4j; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.rules.TemporaryFolder; -import org.tron.common.application.Application; -import org.tron.common.application.ApplicationFactory; -import org.tron.common.application.TronApplicationContext; -import org.tron.common.parameter.CommonParameter; -import org.tron.common.utils.FileUtil; -import org.tron.common.utils.PublicMethod; -import org.tron.common.utils.ReflectUtils; -import org.tron.core.Constant; -import org.tron.core.config.DefaultConfig; -import org.tron.core.config.args.Args; -import org.tron.core.net.peer.PeerConnection; - -@Slf4j -public class BaseNet { - - @ClassRule - public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static String dbDirectory = "net-database"; - private static String indexDirectory = "net-index"; - private static int port = 10000; - - protected static TronApplicationContext context; - - private static Application appT; - private static TronNetDelegate tronNetDelegate; - - private static ExecutorService executorService = Executors.newFixedThreadPool(1); - - public static Channel connect(ByteToMessageDecoder decoder) throws InterruptedException { - NioEventLoopGroup group = new NioEventLoopGroup(1); - Bootstrap b = new Bootstrap(); - b.group(group).channel(NioSocketChannel.class) - .handler(new ChannelInitializer() { - @Override - protected void initChannel(Channel ch) throws Exception { - ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024)); - ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024); - ch.config().setOption(ChannelOption.SO_BACKLOG, 1024); - ch.pipeline() - .addLast("readTimeoutHandler", new ReadTimeoutHandler(600, TimeUnit.SECONDS)) - .addLast("writeTimeoutHandler", new WriteTimeoutHandler(600, TimeUnit.SECONDS)); - ch.pipeline().addLast("protoPender", new ProtobufVarint32LengthFieldPrepender()); - ch.pipeline().addLast("lengthDecode", new ProtobufVarint32FrameDecoder()); - ch.pipeline().addLast("handshakeHandler", decoder); - ch.closeFuture(); - } - }).option(ChannelOption.SO_KEEPALIVE, true) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000) - .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); - return b.connect(Constant.LOCAL_HOST, port).sync().channel(); - } - - @BeforeClass - public static void init() throws Exception { - executorService.execute(() -> { - logger.info("Full node running."); - try { - Args.setParam( - new String[]{ - "--output-directory", temporaryFolder.newFolder().toString(), - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory - }, - "config.conf" - ); - } catch (IOException e) { - Assert.fail("create temp db directory failed"); - } - CommonParameter parameter = Args.getInstance(); - parameter.setNodeListenPort(port); - parameter.getSeedNode().getAddressList().clear(); - parameter.setNodeExternalIp(Constant.LOCAL_HOST); - parameter.setRpcEnable(true); - parameter.setRpcPort(PublicMethod.chooseRandomPort()); - parameter.setRpcSolidityEnable(false); - parameter.setRpcPBFTEnable(false); - parameter.setFullNodeHttpEnable(false); - parameter.setSolidityNodeHttpEnable(false); - parameter.setPBFTHttpEnable(false); - context = new TronApplicationContext(DefaultConfig.class); - appT = ApplicationFactory.create(context); - appT.startup(); - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - //ignore - } - tronNetDelegate = context.getBean(TronNetDelegate.class); - appT.blockUntilShutdown(); - }); - int tryTimes = 0; - do { - Thread.sleep(3000); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init - } while (++tryTimes < 100 && tronNetDelegate == null); - } - - @AfterClass - public static void destroy() { - Collection peerConnections = ReflectUtils - .invokeMethod(tronNetDelegate, "getActivePeer"); - for (PeerConnection peer : peerConnections) { - peer.getChannel().close(); - } - Args.clearParam(); - context.destroy(); - } -} diff --git a/framework/src/test/java/org/tron/core/net/BaseNetTest.java b/framework/src/test/java/org/tron/core/net/BaseNetTest.java deleted file mode 100644 index fd0847c1f08..00000000000 --- a/framework/src/test/java/org/tron/core/net/BaseNetTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.tron.core.net; - -import lombok.extern.slf4j.Slf4j; -import org.junit.Test; -import org.tron.core.services.DelegationServiceTest; -import org.tron.core.services.NodeInfoServiceTest; - -@Slf4j -public class BaseNetTest extends BaseNet { - - @Test - public void test() { - new NodeInfoServiceTest(context).test(); - new DelegationServiceTest(context).test(); - } -} diff --git a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java index e0c816a537a..99f115351ba 100644 --- a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java +++ b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java @@ -6,8 +6,10 @@ import java.util.ArrayList; import java.util.List; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; +import org.tron.common.BaseTest; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Sha256Hash; import org.tron.core.Constant; @@ -20,12 +22,15 @@ import org.tron.protos.Protocol; import org.tron.protos.Protocol.Inventory.InventoryType; -public class P2pEventHandlerImplTest { +public class P2pEventHandlerImplTest extends BaseTest { + + @BeforeClass + public static void init() throws Exception { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF); + } @Test public void testProcessInventoryMessage() throws Exception { - String[] a = new String[0]; - Args.setParam(a, Constant.TESTNET_CONF); CommonParameter parameter = CommonParameter.getInstance(); parameter.setMaxTps(10); @@ -114,9 +119,6 @@ public void testProcessInventoryMessage() throws Exception { @Test public void testUpdateLastInteractiveTime() throws Exception { - String[] a = new String[0]; - Args.setParam(a, Constant.TESTNET_CONF); - PeerConnection peer = new PeerConnection(); P2pEventHandlerImpl p2pEventHandler = new P2pEventHandlerImpl(); diff --git a/framework/src/test/java/org/tron/core/net/P2pRateLimiterTest.java b/framework/src/test/java/org/tron/core/net/P2pRateLimiterTest.java new file mode 100644 index 00000000000..8a1d9c52749 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/P2pRateLimiterTest.java @@ -0,0 +1,23 @@ +package org.tron.core.net; + +import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA; +import static org.tron.core.net.message.MessageTypes.SYNC_BLOCK_CHAIN; + +import org.junit.Assert; +import org.junit.Test; + +public class P2pRateLimiterTest { + @Test + public void test() { + P2pRateLimiter limiter = new P2pRateLimiter(); + limiter.register(SYNC_BLOCK_CHAIN.asByte(), 2); + limiter.acquire(SYNC_BLOCK_CHAIN.asByte()); + boolean ret = limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte()); + Assert.assertTrue(ret); + limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte()); + ret = limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte()); + Assert.assertFalse(ret); + ret = limiter.tryAcquire(FETCH_INV_DATA.asByte()); + Assert.assertTrue(ret); + } +} diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java index 5fd6d6725ba..270002fffba 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java @@ -1,5 +1,7 @@ package org.tron.core.net.messagehandler; +import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.lang.reflect.Field; @@ -13,6 +15,7 @@ import org.tron.common.utils.Sha256Hash; import org.tron.core.capsule.BlockCapsule; import org.tron.core.config.Parameter; +import org.tron.core.net.P2pRateLimiter; import org.tron.core.net.TronNetDelegate; import org.tron.core.net.message.adv.BlockMessage; import org.tron.core.net.message.adv.FetchInvDataMessage; @@ -55,12 +58,40 @@ public void testProcessMessage() throws Exception { Mockito.when(advService.getMessage(new Item(blockId, Protocol.Inventory.InventoryType.BLOCK))) .thenReturn(new BlockMessage(blockCapsule)); ReflectUtils.setFieldValue(fetchInvDataMsgHandler, "advService", advService); + P2pRateLimiter p2pRateLimiter = new P2pRateLimiter(); + p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 2); + Mockito.when(peer.getP2pRateLimiter()).thenReturn(p2pRateLimiter); fetchInvDataMsgHandler.processMessage(peer, new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK)); Assert.assertNotNull(syncBlockIdCache.getIfPresent(blockId)); } + @Test + public void testIsAdvInv() { + FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler(); + + List list = new LinkedList<>(); + list.add(Sha256Hash.ZERO_HASH); + FetchInvDataMessage msg = + new FetchInvDataMessage(list, Protocol.Inventory.InventoryType.TRX); + + boolean isAdv = fetchInvDataMsgHandler.isAdvInv(null, msg); + Assert.assertTrue(isAdv); + + PeerConnection peer = Mockito.mock(PeerConnection.class); + Cache advInvSpread = CacheBuilder.newBuilder().build(); + Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread); + + msg = new FetchInvDataMessage(list, Protocol.Inventory.InventoryType.BLOCK); + isAdv = fetchInvDataMsgHandler.isAdvInv(peer, msg); + Assert.assertTrue(!isAdv); + + advInvSpread.put(new Item(Sha256Hash.ZERO_HASH, Protocol.Inventory.InventoryType.BLOCK), 1L); + isAdv = fetchInvDataMsgHandler.isAdvInv(peer, msg); + Assert.assertTrue(isAdv); + } + @Test public void testSyncFetchCheck() { BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L); @@ -74,6 +105,9 @@ public void testSyncFetchCheck() { Cache advInvSpread = CacheBuilder.newBuilder().maximumSize(100) .expireAfterWrite(1, TimeUnit.HOURS).recordStats().build(); Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread); + P2pRateLimiter p2pRateLimiter = new P2pRateLimiter(); + p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 2); + Mockito.when(peer.getP2pRateLimiter()).thenReturn(p2pRateLimiter); FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler(); @@ -93,4 +127,36 @@ public void testSyncFetchCheck() { Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000"); } } + + @Test + public void testRateLimiter() { + BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L); + List blockIds = new LinkedList<>(); + for (int i = 0; i <= 100; i++) { + blockIds.add(blockId); + } + FetchInvDataMessage msg = + new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK); + PeerConnection peer = Mockito.mock(PeerConnection.class); + Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true); + Cache advInvSpread = CacheBuilder.newBuilder().maximumSize(100) + .expireAfterWrite(1, TimeUnit.HOURS).recordStats().build(); + Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread); + P2pRateLimiter p2pRateLimiter = new P2pRateLimiter(); + p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 1); + p2pRateLimiter.acquire(FETCH_INV_DATA.asByte()); + Mockito.when(peer.getP2pRateLimiter()).thenReturn(p2pRateLimiter); + FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler(); + + try { + fetchInvDataMsgHandler.processMessage(peer, msg); + } catch (Exception e) { + Assert.assertEquals("fetch too many blocks, size:101", e.getMessage()); + } + try { + fetchInvDataMsgHandler.processMessage(peer, msg); + } catch (Exception e) { + Assert.assertTrue(e.getMessage().endsWith("rate limit")); + } + } } diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java index 7ff29b54bb7..b5feb6a765a 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java @@ -3,13 +3,10 @@ import static org.mockito.Mockito.mock; import com.google.protobuf.ByteString; -import java.lang.reflect.Field; import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collections; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; @@ -63,14 +60,10 @@ public static void destroy() { context.destroy(); } - @Before + @After public void clearPeers() { - try { - Field field = PeerManager.class.getDeclaredField("peers"); - field.setAccessible(true); - field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>())); - } catch (NoSuchFieldException | IllegalAccessException e) { - //ignore + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); } } diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java index 8b9d1969cfc..4ae0e4e54b6 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java @@ -4,14 +4,11 @@ import com.google.protobuf.ByteString; import java.io.File; -import java.lang.reflect.Field; import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collections; import org.bouncycastle.util.encoders.Hex; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; @@ -65,14 +62,10 @@ public static void destroy() { FileUtil.deleteDir(new File(dbPath)); } - @Before + @After public void clearPeers() { - try { - Field field = PeerManager.class.getDeclaredField("peers"); - field.setAccessible(true); - field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>())); - } catch (NoSuchFieldException | IllegalAccessException e) { - //ignore + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); } } diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java index e654e1c9cc2..0d66208c1bf 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java @@ -1,15 +1,17 @@ package org.tron.core.net.messagehandler; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; +import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.tron.common.application.TronApplicationContext; @@ -22,21 +24,34 @@ import org.tron.core.net.message.sync.BlockInventoryMessage; import org.tron.core.net.message.sync.SyncBlockChainMessage; import org.tron.core.net.peer.PeerConnection; +import org.tron.core.net.peer.PeerManager; import org.tron.p2p.connection.Channel; public class SyncBlockChainMsgHandlerTest { - private TronApplicationContext context; + private static TronApplicationContext context; private SyncBlockChainMsgHandler handler; private PeerConnection peer; - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @ClassRule + public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + public static String dbPath() { + try { + return temporaryFolder.newFolder().toString(); + } catch (IOException e) { + Assert.fail("create temp folder failed"); + } + return null; + } + + @BeforeClass + public static void before() { + Args.setParam(new String[] {"--output-directory", dbPath()}, Constant.TEST_CONF); + context = new TronApplicationContext(DefaultConfig.class); + } @Before public void init() throws Exception { - Args.setParam(new String[]{"--output-directory", - temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF); - context = new TronApplicationContext(DefaultConfig.class); handler = context.getBean(SyncBlockChainMsgHandler.class); peer = context.getBean(PeerConnection.class); Channel c1 = new Channel(); @@ -55,6 +70,7 @@ public void init() throws Exception { @Test public void testProcessMessage() throws Exception { try { + peer.setRemainNum(1); handler.processMessage(peer, new SyncBlockChainMessage(new ArrayList<>())); } catch (P2pException e) { Assert.assertEquals("SyncBlockChain blockIds is empty", e.getMessage()); @@ -64,13 +80,17 @@ public void testProcessMessage() throws Exception { blockIds.add(new BlockCapsule.BlockId()); SyncBlockChainMessage message = new SyncBlockChainMessage(blockIds); Method method = handler.getClass().getDeclaredMethod( - "check", PeerConnection.class, SyncBlockChainMessage.class); + "check", PeerConnection.class, SyncBlockChainMessage.class); method.setAccessible(true); - boolean f = (boolean)method.invoke(handler, peer, message); + boolean f = (boolean) method.invoke(handler, peer, message); Assert.assertNotNull(message.getAnswerMessage()); Assert.assertNotNull(message.toString()); Assert.assertNotNull(((BlockInventoryMessage) message).getAnswerMessage()); Assert.assertFalse(f); + method.invoke(handler, peer, message); + method.invoke(handler, peer, message); + f = (boolean) method.invoke(handler, peer, message); + Assert.assertFalse(f); Method method1 = handler.getClass().getDeclaredMethod( "getLostBlockIds", List.class, BlockId.class); @@ -88,8 +108,12 @@ public void testProcessMessage() throws Exception { Assert.assertEquals(1, list.size()); } - @After - public void destroy() { + @AfterClass + public static void destroy() { + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); + } + context.destroy(); Args.clearParam(); } diff --git a/framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java b/framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java index 5a67cd8f609..649e5eb0875 100644 --- a/framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java +++ b/framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java @@ -7,14 +7,18 @@ import java.util.LinkedList; import java.util.List; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import org.tron.common.overlay.message.Message; +import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Pair; import org.tron.common.utils.ReflectUtils; import org.tron.common.utils.Sha256Hash; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.config.args.Args; import org.tron.core.net.message.adv.InventoryMessage; import org.tron.core.net.message.handshake.HelloMessage; import org.tron.core.net.message.keepalive.PingMessage; @@ -26,6 +30,18 @@ public class PeerConnectionTest { + @BeforeClass + public static void initArgs() { + CommonParameter.getInstance().setRateLimiterSyncBlockChain(10); + CommonParameter.getInstance().setRateLimiterFetchInvData(10); + CommonParameter.getInstance().setRateLimiterDisconnect(10); + } + + @AfterClass + public static void destroy() { + Args.clearParam(); + } + @Test public void testVariableDefaultValue() { PeerConnection peerConnection = new PeerConnection(); diff --git a/framework/src/test/java/org/tron/core/net/peer/PeerManagerTest.java b/framework/src/test/java/org/tron/core/net/peer/PeerManagerTest.java index b8c03de1029..ee409a8ab04 100644 --- a/framework/src/test/java/org/tron/core/net/peer/PeerManagerTest.java +++ b/framework/src/test/java/org/tron/core/net/peer/PeerManagerTest.java @@ -8,16 +8,40 @@ import java.util.Collections; import java.util.List; +import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import org.springframework.context.ApplicationContext; +import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ReflectUtils; +import org.tron.core.config.args.Args; import org.tron.p2p.connection.Channel; public class PeerManagerTest { List relayNodes = new ArrayList<>(); + @BeforeClass + public static void initArgs() { + CommonParameter.getInstance().setRateLimiterSyncBlockChain(10); + CommonParameter.getInstance().setRateLimiterFetchInvData(10); + CommonParameter.getInstance().setRateLimiterDisconnect(10); + } + + @AfterClass + public static void destroy() { + Args.clearParam(); + } + + @After + public void clearPeers() { + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); + } + } + @Test public void testAdd() throws Exception { Field field = PeerManager.class.getDeclaredField("peers"); diff --git a/framework/src/test/java/org/tron/core/net/service/nodepersist/NodePersistServiceTest.java b/framework/src/test/java/org/tron/core/net/service/nodepersist/NodePersistServiceTest.java new file mode 100644 index 00000000000..cd80a6b78f0 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/service/nodepersist/NodePersistServiceTest.java @@ -0,0 +1,40 @@ +package org.tron.core.net.service.nodepersist; + +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.JsonUtil; +import org.tron.core.Constant; +import org.tron.core.capsule.BytesCapsule; +import org.tron.core.config.args.Args; + + +public class NodePersistServiceTest extends BaseTest { + + @Resource + private NodePersistService nodePersistService; + + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, + Constant.TEST_CONF); + } + + @Test + public void testDbRead() { + byte[] DB_KEY_PEERS = "peers".getBytes(); + DBNode dbNode = new DBNode("localhost", 3306); + List dbNodeList = new ArrayList<>(); + dbNodeList.add(dbNode); + DBNodes dbNodes = new DBNodes(); + dbNodes.setNodes(dbNodeList); + chainBaseManager.getCommonStore().put(DB_KEY_PEERS, new BytesCapsule( + JsonUtil.obj2Json(dbNodes).getBytes())); + + Assert.assertEquals(1, nodePersistService.dbRead().size()); + } +} diff --git a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java index 04a6315f522..cfaa44574e3 100644 --- a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java @@ -53,6 +53,9 @@ public static void init() throws IOException { @AfterClass public static void after() { + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); + } Args.clearParam(); context.destroy(); } diff --git a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java index 9104b087d26..da7f714d096 100644 --- a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java @@ -1,48 +1,35 @@ package org.tron.core.net.services; -import java.io.IOException; import java.lang.reflect.Method; import java.net.InetSocketAddress; -import org.junit.After; +import javax.annotation.Resource; import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; +import org.junit.BeforeClass; import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.tron.common.application.TronApplicationContext; +import org.tron.common.BaseTest; +import org.tron.common.utils.PublicMethod; import org.tron.common.utils.ReflectUtils; import org.tron.core.Constant; -import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.net.TronNetService; import org.tron.core.net.service.effective.EffectiveCheckService; import org.tron.p2p.P2pConfig; -public class EffectiveCheckServiceTest { +public class EffectiveCheckServiceTest extends BaseTest { - protected TronApplicationContext context; + @Resource private EffectiveCheckService service; + @Resource + private TronNetService tronNetService; - @Rule - public final TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Before - public void init() throws IOException { - Args.setParam(new String[] {"--output-directory", - temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF); - context = new TronApplicationContext(DefaultConfig.class); - service = context.getBean(EffectiveCheckService.class); - } - - @After - public void destroy() { - Args.clearParam(); - context.destroy(); + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, + Constant.TEST_CONF); } @Test public void testNoIpv4() throws Exception { - TronNetService tronNetService = context.getBean(TronNetService.class); Method privateMethod = tronNetService.getClass() .getDeclaredMethod("updateConfig", P2pConfig.class); privateMethod.setAccessible(true); @@ -54,10 +41,10 @@ public void testNoIpv4() throws Exception { @Test public void testFind() { - TronNetService tronNetService = context.getBean(TronNetService.class); + int port = PublicMethod.chooseRandomPort(); P2pConfig p2pConfig = new P2pConfig(); p2pConfig.setIp("127.0.0.1"); - p2pConfig.setPort(34567); + p2pConfig.setPort(port); ReflectUtils.setFieldValue(tronNetService, "p2pConfig", p2pConfig); TronNetService.getP2pService().start(p2pConfig); @@ -65,7 +52,7 @@ public void testFind() { Assert.assertNull(service.getCur()); ReflectUtils.invokeMethod(service, "resetCount"); - InetSocketAddress cur = new InetSocketAddress("192.168.0.1", 34567); + InetSocketAddress cur = new InetSocketAddress("192.168.0.1", port); service.setCur(cur); service.onDisconnect(cur); } diff --git a/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java b/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java index f4fabce5d64..0d8a50c8a92 100644 --- a/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java @@ -4,16 +4,13 @@ import static org.tron.core.net.message.handshake.HelloMessage.getEndpointFromNode; import com.google.protobuf.ByteString; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collections; import java.util.Random; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; @@ -21,7 +18,6 @@ import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.ByteArray; import org.tron.common.utils.ReflectUtils; import org.tron.common.utils.Sha256Hash; import org.tron.core.ChainBaseManager; @@ -34,6 +30,7 @@ import org.tron.core.net.message.handshake.HelloMessage; import org.tron.core.net.peer.PeerConnection; import org.tron.core.net.peer.PeerManager; +import org.tron.core.net.service.handshake.HandshakeService; import org.tron.p2p.P2pConfig; import org.tron.p2p.base.Parameter; import org.tron.p2p.connection.Channel; @@ -72,14 +69,10 @@ public static void destroy() { context.destroy(); } - @Before + @After public void clearPeers() { - try { - Field field = PeerManager.class.getDeclaredField("peers"); - field.setAccessible(true); - field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>())); - } catch (NoSuchFieldException | IllegalAccessException e) { - //ignore + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); } } @@ -101,6 +94,7 @@ public void testOkHelloMessage() Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, a1.getPort()); HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(), ChainBaseManager.getChainBaseManager()); + Assert.assertNotNull(helloMessage.toString()); Assert.assertEquals(Version.getVersion(), new String(helloMessage.getHelloMessage().getCodeVersion().toByteArray())); @@ -126,13 +120,27 @@ public void testInvalidHelloMessage() { //block hash is empty try { BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId(); - Protocol.HelloMessage.BlockId hBlockId = Protocol.HelloMessage.BlockId.newBuilder() - .setHash(ByteString.copyFrom(new byte[0])) + Protocol.HelloMessage.BlockId okBlockId = Protocol.HelloMessage.BlockId.newBuilder() + .setHash(ByteString.copyFrom(new byte[32])) + .setNumber(hid.getNum()) + .build(); + Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder() + .setHash(ByteString.copyFrom(new byte[31])) .setNumber(hid.getNum()) .build(); - builder.setHeadBlockId(hBlockId); + builder.setHeadBlockId(invalidBlockId); HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray()); - Assert.assertTrue(!helloMessage.valid()); + Assert.assertFalse(helloMessage.valid()); + + builder.setHeadBlockId(okBlockId); + builder.setGenesisBlockId(invalidBlockId); + HelloMessage helloMessage2 = new HelloMessage(builder.build().toByteArray()); + Assert.assertFalse(helloMessage2.valid()); + + builder.setGenesisBlockId(okBlockId); + builder.setSolidBlockId(invalidBlockId); + HelloMessage helloMessage3 = new HelloMessage(builder.build().toByteArray()); + Assert.assertFalse(helloMessage3.valid()); } catch (Exception e) { Assert.fail(); } @@ -214,7 +222,7 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException { Node node2 = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, 10002); - //lowestBlockNum > headBlockNum + //peer's lowestBlockNum > my headBlockNum => peer is light, LIGHT_NODE_SYNC_FAIL Protocol.HelloMessage.Builder builder = getHelloMessageBuilder(node2, System.currentTimeMillis(), ChainBaseManager.getChainBaseManager()); @@ -226,7 +234,7 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException { Assert.fail(); } - //genesisBlock is not equal + //genesisBlock is not equal => INCOMPATIBLE_CHAIN builder = getHelloMessageBuilder(node2, System.currentTimeMillis(), ChainBaseManager.getChainBaseManager()); BlockCapsule.BlockId gid = ChainBaseManager.getChainBaseManager().getGenesisBlockId(); @@ -242,9 +250,11 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException { Assert.fail(); } - //solidityBlock <= us, but not contained + // peer's solidityBlock <= my solidityBlock, but not contained + // and my lowestBlockNum <= peer's solidityBlock => FORKED builder = getHelloMessageBuilder(node2, System.currentTimeMillis(), ChainBaseManager.getChainBaseManager()); + BlockCapsule.BlockId sid = ChainBaseManager.getChainBaseManager().getSolidBlockId(); Random gen = new Random(); @@ -262,6 +272,46 @@ public void testLowAndGenesisBlockNum() throws NoSuchMethodException { } catch (Exception e) { Assert.fail(); } + + // peer's solidityBlock <= my solidityBlock, but not contained + // and my lowestBlockNum > peer's solidityBlock => i am light, LIGHT_NODE_SYNC_FAIL + ChainBaseManager.getChainBaseManager().setLowestBlockNum(2); + try { + HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray()); + method.invoke(p2pEventHandler, peer, helloMessage.getSendBytes()); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testProcessHelloMessage() { + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); + Channel c1 = mock(Channel.class); + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); + PeerManager.add(ctx, c1); + PeerConnection p = PeerManager.getPeers().get(0); + + try { + Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), + null, a1.getPort()); + Protocol.HelloMessage.Builder builder = + getHelloMessageBuilder(node, System.currentTimeMillis(), + ChainBaseManager.getChainBaseManager()); + BlockCapsule.BlockId hid = ChainBaseManager.getChainBaseManager().getHeadBlockId(); + Protocol.HelloMessage.BlockId invalidBlockId = Protocol.HelloMessage.BlockId.newBuilder() + .setHash(ByteString.copyFrom(new byte[31])) + .setNumber(hid.getNum()) + .build(); + builder.setHeadBlockId(invalidBlockId); + + HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray()); + HandshakeService handshakeService = new HandshakeService(); + handshakeService.processHelloMessage(p, helloMessage); + } catch (Exception e) { + Assert.fail(); + } } private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long timestamp, diff --git a/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java b/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java index 5e22e538e80..63028001249 100644 --- a/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java @@ -1,10 +1,12 @@ package org.tron.core.net.services; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import com.google.common.collect.Lists; import com.google.protobuf.ByteString; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -12,28 +14,36 @@ import java.util.List; import java.util.Set; import javax.annotation.Resource; - import lombok.extern.slf4j.Slf4j; import org.bouncycastle.util.encoders.Hex; +import org.junit.After; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.tron.common.BaseTest; +import org.tron.common.crypto.SignInterface; +import org.tron.common.crypto.SignUtils; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.ByteArray; import org.tron.common.utils.ReflectUtils; +import org.tron.common.utils.Sha256Hash; import org.tron.core.ChainBaseManager; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.WitnessCapsule; import org.tron.core.config.args.Args; import org.tron.core.net.P2pEventHandlerImpl; +import org.tron.core.net.TronNetDelegate; +import org.tron.core.net.TronNetService; import org.tron.core.net.message.adv.BlockMessage; import org.tron.core.net.message.handshake.HelloMessage; import org.tron.core.net.peer.Item; import org.tron.core.net.peer.PeerConnection; import org.tron.core.net.peer.PeerManager; import org.tron.core.net.service.relay.RelayService; +import org.tron.p2p.P2pConfig; import org.tron.p2p.connection.Channel; import org.tron.p2p.discover.Node; import org.tron.p2p.utils.NetUtil; @@ -44,11 +54,13 @@ public class RelayServiceTest extends BaseTest { @Resource private RelayService service; - @Resource - private PeerConnection peer; + @Resource private P2pEventHandlerImpl p2pEventHandler; + @Resource + private TronNetService tronNetService; + /** * init context. */ @@ -58,6 +70,11 @@ public static void init() { Constant.TEST_CONF); } + @After + public void clearPeers() { + closePeer(); + } + @Test public void test() throws Exception { initWitness(); @@ -67,8 +84,12 @@ public void test() throws Exception { } private void initWitness() { - byte[] key = Hex.decode("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"); + // key: 0154435f065a57fec6af1e12eaa2fa600030639448d7809f4c65bdcf8baed7e5 + // Hex: A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01 + // Base58: 27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz + byte[] key = Hex.decode("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01");//exist already WitnessCapsule witnessCapsule = chainBaseManager.getWitnessStore().get(key); + System.out.println(witnessCapsule.getInstance()); witnessCapsule.setVoteCount(1000); chainBaseManager.getWitnessStore().put(key, witnessCapsule); List list = new ArrayList<>(); @@ -83,7 +104,7 @@ public void testGetNextWitnesses() throws Exception { "getNextWitnesses", ByteString.class, Integer.class); method.setAccessible(true); Set s1 = (Set) method.invoke( - service, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"), 3); + service, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"), 3); Assert.assertEquals(3, s1.size()); assertContains(s1, "A0299F3DB80A24B20A254B89CE639D59132F157F13"); assertContains(s1, "A0807337F180B62A77576377C1D0C9C24DF5C0DD62"); @@ -93,35 +114,51 @@ public void testGetNextWitnesses() throws Exception { service, getFromHexString("A0FAB5FBF6AFB681E4E37E9D33BDDB7E923D6132E5"), 3); Assert.assertEquals(3, s2.size()); assertContains(s2, "A014EEBE4D30A6ACB505C8B00B218BDC4733433C68"); - assertContains(s2, "A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"); + assertContains(s2, "A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"); assertContains(s2, "A0299F3DB80A24B20A254B89CE639D59132F157F13"); Set s3 = (Set) method.invoke( - service, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"), 1); + service, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"), 1); Assert.assertEquals(1, s3.size()); assertContains(s3, "A0299F3DB80A24B20A254B89CE639D59132F157F13"); } private void testBroadcast() { try { + PeerConnection peer = new PeerConnection(); + InetSocketAddress a1 = new InetSocketAddress("127.0.0.2", 10001); + Channel c1 = mock(Channel.class); + Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); + Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); + doNothing().when(c1).send((byte[]) any()); + + peer.setChannel(c1); peer.setAddress(getFromHexString("A0299F3DB80A24B20A254B89CE639D59132F157F13")); peer.setNeedSyncFromPeer(false); peer.setNeedSyncFromUs(false); - List peers = Lists.newArrayList(); + List peers = new ArrayList<>(); peers.add(peer); - ReflectUtils.setFieldValue(p2pEventHandler, "activePeers", peers); + + TronNetDelegate tronNetDelegate = Mockito.mock(TronNetDelegate.class); + Mockito.doReturn(peers).when(tronNetDelegate).getActivePeer(); + + Field field = service.getClass().getDeclaredField("tronNetDelegate"); + field.setAccessible(true); + field.set(service, tronNetDelegate); + BlockCapsule blockCapsule = new BlockCapsule(chainBaseManager.getHeadBlockNum() + 1, chainBaseManager.getHeadBlockId(), - 0, getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F")); + 0, getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01")); BlockMessage msg = new BlockMessage(blockCapsule); service.broadcast(msg); Item item = new Item(blockCapsule.getBlockId(), Protocol.Inventory.InventoryType.BLOCK); Assert.assertEquals(1, peer.getAdvInvSpread().size()); Assert.assertNotNull(peer.getAdvInvSpread().getIfPresent(item)); peer.getChannel().close(); - } catch (NullPointerException e) { - System.out.println(e); + } catch (Exception e) { + logger.info("", e); + assert false; } } @@ -135,20 +172,32 @@ private ByteString getFromHexString(String s) { } private void testCheckHelloMessage() { - ByteString address = getFromHexString("A04711BF7AFBDF44557DEFBDF4C4E7AA6138C6331F"); + String key = "0154435f065a57fec6af1e12eaa2fa600030639448d7809f4c65bdcf8baed7e5"; + ByteString address = getFromHexString("A08A8D690BF36806C36A7DAE3AF796643C1AA9CC01"); InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, a1.getPort()); + + SignInterface cryptoEngine = SignUtils.fromPrivate(ByteArray.fromHexString(key), + Args.getInstance().isECKeyCryptoEngine()); HelloMessage helloMessage = new HelloMessage(node, System.currentTimeMillis(), ChainBaseManager.getChainBaseManager()); + ByteString sig = ByteString.copyFrom(cryptoEngine.Base64toBytes(cryptoEngine + .signHash(Sha256Hash.of(CommonParameter.getInstance() + .isECKeyCryptoEngine(), ByteArray.fromLong(helloMessage + .getTimestamp())).getBytes()))); helloMessage.setHelloMessage(helloMessage.getHelloMessage().toBuilder() - .setAddress(address).build()); + .setAddress(address) + .setSignature(sig) + .build()); + Channel c1 = mock(Channel.class); Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); Channel c2 = mock(Channel.class); Mockito.when(c2.getInetSocketAddress()).thenReturn(a1); Mockito.when(c2.getInetAddress()).thenReturn(a1.getAddress()); + Args.getInstance().fastForward = true; ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, "ctx"); @@ -158,14 +207,51 @@ private void testCheckHelloMessage() { PeerConnection peer2 = PeerManager.add(ctx, c2); assert peer2 != null; peer2.setAddress(address); + + ReflectUtils.setFieldValue(tronNetService, "p2pConfig", new P2pConfig()); + try { Field field = service.getClass().getDeclaredField("witnessScheduleStore"); field.setAccessible(true); field.set(service, chainBaseManager.getWitnessScheduleStore()); + + Field field2 = service.getClass().getDeclaredField("manager"); + field2.setAccessible(true); + field2.set(service, dbManager); + boolean res = service.checkHelloMessage(helloMessage, c1); - Assert.assertFalse(res); + Assert.assertTrue(res); } catch (Exception e) { - logger.info("{}", e.getMessage()); + logger.info("", e); + assert false; + } + } + + @Test + public void testNullWitnessAddress() { + try { + Class clazz = service.getClass(); + + Field keySizeField = clazz.getDeclaredField("keySize"); + keySizeField.setAccessible(true); + keySizeField.set(service, 0); + + Field witnessAddressField = clazz.getDeclaredField("witnessAddress"); + witnessAddressField.setAccessible(true); + witnessAddressField.set(service, null); + + Method isActiveWitnessMethod = clazz.getDeclaredMethod("isActiveWitness"); + isActiveWitnessMethod.setAccessible(true); + + Boolean result = (Boolean) isActiveWitnessMethod.invoke(service); + Assert.assertNotEquals(Boolean.TRUE, result); + + witnessAddressField.set(service, ByteString.copyFrom(new byte[21])); + result = (Boolean) isActiveWitnessMethod.invoke(service); + Assert.assertNotEquals(Boolean.TRUE, result); + } catch (NoSuchMethodException | NoSuchFieldException + | IllegalAccessException | InvocationTargetException e) { + Assert.fail("Reflection invocation failed: " + e.getMessage()); } } -} \ No newline at end of file +} diff --git a/framework/src/test/java/org/tron/core/net/services/ResilienceServiceTest.java b/framework/src/test/java/org/tron/core/net/services/ResilienceServiceTest.java index ce723e5c991..bb1a0607796 100644 --- a/framework/src/test/java/org/tron/core/net/services/ResilienceServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/ResilienceServiceTest.java @@ -8,49 +8,50 @@ import java.net.InetSocketAddress; import java.util.HashSet; import java.util.Set; +import javax.annotation.Resource; import org.junit.After; import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; +import org.junit.BeforeClass; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; -import org.tron.common.application.TronApplicationContext; +import org.springframework.context.ApplicationContext; +import org.tron.common.BaseTest; import org.tron.common.utils.ReflectUtils; -import org.tron.core.ChainBaseManager; import org.tron.core.Constant; -import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; +import org.tron.core.net.P2pEventHandlerImpl; import org.tron.core.net.peer.PeerConnection; import org.tron.core.net.peer.PeerManager; import org.tron.core.net.service.effective.ResilienceService; import org.tron.p2p.connection.Channel; -public class ResilienceServiceTest { +public class ResilienceServiceTest extends BaseTest { - protected TronApplicationContext context; + @Resource private ResilienceService service; - private ChainBaseManager chainBaseManager; - - @Rule - public final TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Before - public void init() throws IOException { - Args.setParam(new String[] {"--output-directory", - temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF); - context = new TronApplicationContext(DefaultConfig.class); - chainBaseManager = context.getBean(ChainBaseManager.class); - service = context.getBean(ResilienceService.class); + + @Resource + private P2pEventHandlerImpl p2pEventHandler; + + + @BeforeClass + public static void init() throws IOException { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF); + } + + @After + public void clearPeers() { + closePeer(); } @Test public void testDisconnectRandom() { int maxConnection = 30; Assert.assertEquals(maxConnection, Args.getInstance().getMaxConnections()); - clearPeers(); Assert.assertEquals(0, PeerManager.getPeers().size()); + ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, + "ctx"); for (int i = 0; i < maxConnection + 1; i++) { InetSocketAddress inetSocketAddress = new InetSocketAddress("201.0.0." + i, 10001); Channel c1 = spy(Channel.class); @@ -59,7 +60,7 @@ public void testDisconnectRandom() { ReflectUtils.setFieldValue(c1, "ctx", spy(ChannelHandlerContext.class)); Mockito.doNothing().when(c1).send((byte[]) any()); - PeerManager.add(context, c1); + PeerManager.add(ctx, c1); } for (PeerConnection peer : PeerManager.getPeers() .subList(0, ResilienceService.minBroadcastPeerSize)) { @@ -99,9 +100,10 @@ public void testDisconnectRandom() { public void testDisconnectLan() { int minConnection = 8; Assert.assertEquals(minConnection, Args.getInstance().getMinConnections()); - clearPeers(); Assert.assertEquals(0, PeerManager.getPeers().size()); + ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, + "ctx"); for (int i = 0; i < 9; i++) { InetSocketAddress inetSocketAddress = new InetSocketAddress("201.0.0." + i, 10001); Channel c1 = spy(Channel.class); @@ -111,7 +113,7 @@ public void testDisconnectLan() { ReflectUtils.setFieldValue(c1, "ctx", spy(ChannelHandlerContext.class)); Mockito.doNothing().when(c1).send((byte[]) any()); - PeerManager.add(context, c1); + PeerManager.add(ctx, c1); } for (PeerConnection peer : PeerManager.getPeers()) { peer.setNeedSyncFromPeer(false); @@ -154,10 +156,11 @@ public void testDisconnectLan() { public void testDisconnectIsolated2() { int maxConnection = 30; Assert.assertEquals(maxConnection, Args.getInstance().getMaxConnections()); - clearPeers(); Assert.assertEquals(0, PeerManager.getPeers().size()); int addSize = (int) (maxConnection * ResilienceService.retentionPercent) + 2; //26 + ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, + "ctx"); for (int i = 0; i < addSize; i++) { InetSocketAddress inetSocketAddress = new InetSocketAddress("201.0.0." + i, 10001); Channel c1 = spy(Channel.class); @@ -168,7 +171,7 @@ public void testDisconnectIsolated2() { ReflectUtils.setFieldValue(c1, "ctx", spy(ChannelHandlerContext.class)); Mockito.doNothing().when(c1).send((byte[]) any()); - PeerManager.add(context, c1); + PeerManager.add(ctx, c1); } PeerManager.getPeers().get(10).setNeedSyncFromUs(false); PeerManager.getPeers().get(10).setNeedSyncFromPeer(false); @@ -190,16 +193,4 @@ public void testDisconnectIsolated2() { Assert.assertEquals((int) (maxConnection * ResilienceService.retentionPercent), PeerManager.getPeers().size()); } - - private void clearPeers() { - for (PeerConnection p : PeerManager.getPeers()) { - PeerManager.remove(p.getChannel()); - } - } - - @After - public void destroy() { - Args.clearParam(); - context.destroy(); - } } \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/net/services/SyncServiceTest.java b/framework/src/test/java/org/tron/core/net/services/SyncServiceTest.java index c2883fb349d..785262e3210 100644 --- a/framework/src/test/java/org/tron/core/net/services/SyncServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/SyncServiceTest.java @@ -19,6 +19,7 @@ import org.springframework.context.ApplicationContext; import org.tron.common.application.TronApplicationContext; import org.tron.common.utils.ReflectUtils; +import org.tron.common.utils.Sha256Hash; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; import org.tron.core.config.DefaultConfig; @@ -64,6 +65,9 @@ public void init() throws Exception { */ @After public void destroy() { + for (PeerConnection p : PeerManager.getPeers()) { + PeerManager.remove(p.getChannel()); + } Args.clearParam(); context.destroy(); } @@ -91,6 +95,13 @@ public void testStartSync() { ReflectUtils.setFieldValue(peer, "tronState", TronState.INIT); + try { + peer.setBlockBothHave(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, -1)); + service.syncNext(peer); + } catch (Exception e) { + // no need to deal with + } + service.startSync(peer); } catch (Exception e) { // no need to deal with diff --git a/framework/src/test/java/org/tron/core/services/DelegationServiceTest.java b/framework/src/test/java/org/tron/core/services/DelegationServiceTest.java index 5c898eb42d6..0ce4ba4d67d 100644 --- a/framework/src/test/java/org/tron/core/services/DelegationServiceTest.java +++ b/framework/src/test/java/org/tron/core/services/DelegationServiceTest.java @@ -4,49 +4,28 @@ import static org.tron.common.utils.client.Parameter.CommonConstant.ADD_PRE_FIX_BYTE_MAINNET; import com.google.protobuf.ByteString; -import io.grpc.ManagedChannelBuilder; +import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; -import org.tron.api.GrpcAPI.BytesMessage; -import org.tron.api.GrpcAPI.TransactionExtention; -import org.tron.api.WalletGrpc; -import org.tron.api.WalletGrpc.WalletBlockingStub; -import org.tron.common.application.TronApplicationContext; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.AccountCapsule; -import org.tron.core.db.Manager; +import org.tron.core.config.args.Args; import org.tron.core.service.MortgageService; -import org.tron.protos.contract.StorageContract.UpdateBrokerageContract; @Slf4j -public class DelegationServiceTest { +public class DelegationServiceTest extends BaseTest { - private static String fullnode = "127.0.0.1:50051"; - private MortgageService mortgageService; - private Manager manager; + @Resource + protected MortgageService mortgageService; - public DelegationServiceTest(TronApplicationContext context) { - mortgageService = context.getBean(MortgageService.class); - manager = context.getBean(Manager.class); - } - - public static void testGrpc() { - WalletBlockingStub walletStub = WalletGrpc - .newBlockingStub(ManagedChannelBuilder.forTarget(fullnode) - .usePlaintext() - .build()); - BytesMessage.Builder builder = BytesMessage.newBuilder(); - builder.setValue(ByteString.copyFromUtf8("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD")); - System.out - .println("getBrokerageInfo: " + walletStub.getBrokerageInfo(builder.build()).getNum()); - System.out.println("getRewardInfo: " + walletStub.getRewardInfo(builder.build()).getNum()); - UpdateBrokerageContract.Builder updateBrokerageContract = UpdateBrokerageContract.newBuilder(); - updateBrokerageContract.setOwnerAddress( - ByteString.copyFrom(decodeFromBase58Check("TN3zfjYUmMFK3ZsHSsrdJoNRtGkQmZLBLz"))) - .setBrokerage(10); - TransactionExtention transactionExtention = walletStub - .updateBrokerage(updateBrokerageContract.build()); - System.out.println("UpdateBrokerage: " + transactionExtention); + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, + Constant.TESTNET_CONF); } private void testPay(int cycle) { @@ -59,7 +38,7 @@ private void testPay(int cycle) { mortgageService.payStandbyWitness(); Wallet.setAddressPreFixByte(ADD_PRE_FIX_BYTE_MAINNET); byte[] sr1 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD"); - long value = manager.getDelegationStore().getReward(cycle, sr1); + long value = dbManager.getDelegationStore().getReward(cycle, sr1); long tmp = 0; for (int i = 0; i < 27; i++) { tmp += 100000000 + i; @@ -73,47 +52,47 @@ private void testPay(int cycle) { expect += 32000000; brokerageAmount = (long) (rate * 32000000); expect -= brokerageAmount; - value = manager.getDelegationStore().getReward(cycle, sr1); + value = dbManager.getDelegationStore().getReward(cycle, sr1); Assert.assertEquals(expect, value); } private void testWithdraw() { //init - manager.getDynamicPropertiesStore().saveCurrentCycleNumber(1); + dbManager.getDynamicPropertiesStore().saveCurrentCycleNumber(1); testPay(1); - manager.getDynamicPropertiesStore().saveCurrentCycleNumber(2); + dbManager.getDynamicPropertiesStore().saveCurrentCycleNumber(2); testPay(2); byte[] sr1 = decodeFromBase58Check("THKJYuUmMKKARNf7s2VT51g5uPY6KEqnat"); - AccountCapsule accountCapsule = manager.getAccountStore().get(sr1); + AccountCapsule accountCapsule = dbManager.getAccountStore().get(sr1); byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD"); accountCapsule.addVotes(ByteString.copyFrom(sr27), 10000000); - manager.getAccountStore().put(sr1, accountCapsule); + dbManager.getAccountStore().put(sr1, accountCapsule); // long allowance = accountCapsule.getAllowance(); long value = mortgageService.queryReward(sr1) - allowance; - long reward1 = (long) ((double) manager.getDelegationStore().getReward(0, sr27) / 100000000 + long reward1 = (long) ((double) dbManager.getDelegationStore().getReward(0, sr27) / 100000000 * 10000000); - long reward2 = (long) ((double) manager.getDelegationStore().getReward(1, sr27) / 100000000 + long reward2 = (long) ((double) dbManager.getDelegationStore().getReward(1, sr27) / 100000000 * 10000000); long reward = reward1 + reward2; - System.out.println("testWithdraw:" + value + ", reward:" + reward); Assert.assertEquals(reward, value); mortgageService.withdrawReward(sr1); - accountCapsule = manager.getAccountStore().get(sr1); + accountCapsule = dbManager.getAccountStore().get(sr1); allowance = accountCapsule.getAllowance() - allowance; System.out.println("withdrawReward:" + allowance); Assert.assertEquals(reward, allowance); } + @Test public void test() { - manager.getDynamicPropertiesStore().saveChangeDelegation(1); - manager.getDynamicPropertiesStore().saveConsensusLogicOptimization(1); + dbManager.getDynamicPropertiesStore().saveChangeDelegation(1); + dbManager.getDynamicPropertiesStore().saveConsensusLogicOptimization(1); byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD"); - manager.getDelegationStore().setBrokerage(0, sr27, 10); - manager.getDelegationStore().setBrokerage(1, sr27, 20); - manager.getDelegationStore().setWitnessVote(0, sr27, 100000000); - manager.getDelegationStore().setWitnessVote(1, sr27, 100000000); - manager.getDelegationStore().setWitnessVote(2, sr27, 100000000); + dbManager.getDelegationStore().setBrokerage(0, sr27, 10); + dbManager.getDelegationStore().setBrokerage(1, sr27, 20); + dbManager.getDelegationStore().setWitnessVote(0, sr27, 100000000); + dbManager.getDelegationStore().setWitnessVote(1, sr27, 100000000); + dbManager.getDelegationStore().setWitnessVote(2, sr27, 100000000); testPay(0); testWithdraw(); } diff --git a/framework/src/test/java/org/tron/core/services/NodeInfoServiceTest.java b/framework/src/test/java/org/tron/core/services/NodeInfoServiceTest.java index be0a96f632b..2ea410f93f1 100644 --- a/framework/src/test/java/org/tron/core/services/NodeInfoServiceTest.java +++ b/framework/src/test/java/org/tron/core/services/NodeInfoServiceTest.java @@ -1,35 +1,56 @@ package org.tron.core.services; -import static org.mockito.Mockito.mock; - import com.alibaba.fastjson.JSON; import com.google.protobuf.ByteString; import java.net.InetSocketAddress; +import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.junit.After; import org.junit.Assert; -import org.mockito.Mockito; -import org.tron.common.application.TronApplicationContext; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.tron.common.BaseTest; import org.tron.common.entity.NodeInfo; +import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.ReflectUtils; import org.tron.common.utils.Sha256Hash; +import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.config.args.Args; import org.tron.core.net.P2pEventHandlerImpl; +import org.tron.core.net.TronNetService; +import org.tron.core.net.peer.PeerManager; +import org.tron.p2p.P2pConfig; import org.tron.p2p.connection.Channel; import org.tron.program.Version; @Slf4j -public class NodeInfoServiceTest { +public class NodeInfoServiceTest extends BaseTest { - private NodeInfoService nodeInfoService; - private WitnessProductBlockService witnessProductBlockService; + @Resource + protected NodeInfoService nodeInfoService; + @Resource + protected WitnessProductBlockService witnessProductBlockService; + @Resource private P2pEventHandlerImpl p2pEventHandler; + @Resource + private TronNetService tronNetService; + + + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, + Constant.TEST_CONF); + } - public NodeInfoServiceTest(TronApplicationContext context) { - nodeInfoService = context.getBean("nodeInfoService", NodeInfoService.class); - witnessProductBlockService = context.getBean(WitnessProductBlockService.class); - p2pEventHandler = context.getBean(P2pEventHandlerImpl.class); + @After + public void clearPeers() { + closePeer(); } + @Test public void test() { BlockCapsule blockCapsule1 = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 100, ByteString.EMPTY); @@ -38,18 +59,31 @@ public void test() { witnessProductBlockService.validWitnessProductTwoBlock(blockCapsule1); witnessProductBlockService.validWitnessProductTwoBlock(blockCapsule2); - //add peer - InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); - Channel c1 = mock(Channel.class); - Mockito.when(c1.getInetSocketAddress()).thenReturn(a1); - Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress()); - p2pEventHandler.onConnect(c1); + addPeer(); //test setConnectInfo NodeInfo nodeInfo = nodeInfoService.getNodeInfo(); Assert.assertEquals(nodeInfo.getConfigNodeInfo().getCodeVersion(), Version.getVersion()); - Assert.assertEquals(nodeInfo.getCheatWitnessInfoMap().size(), 1); + Assert.assertEquals(1, nodeInfo.getCheatWitnessInfoMap().size()); logger.info("{}", JSON.toJSONString(nodeInfo)); } + private void addPeer() { + int port = PublicMethod.chooseRandomPort(); + P2pConfig p2pConfig = new P2pConfig(); + p2pConfig.setIp("127.0.0.1"); + p2pConfig.setPort(port); + ReflectUtils.setFieldValue(tronNetService, "p2pConfig", p2pConfig); + TronNetService.getP2pService().start(p2pConfig); + + ApplicationContext ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, + "ctx"); + InetSocketAddress inetSocketAddress1 = + new InetSocketAddress("127.0.0.1", 10001); + Channel c1 = new Channel(); + ReflectUtils.setFieldValue(c1, "inetSocketAddress", inetSocketAddress1); + ReflectUtils.setFieldValue(c1, "inetAddress", inetSocketAddress1.getAddress()); + + PeerManager.add(ctx, c1); + } } diff --git a/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java b/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java index 300a38a0916..e81c75948b7 100644 --- a/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java +++ b/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java @@ -1,7 +1,9 @@ package org.tron.core.services; +import static org.tron.core.Constant.MAX_PROPOSAL_EXPIRE_TIME; import static org.tron.core.utils.ProposalUtil.ProposalType.CONSENSUS_LOGIC_OPTIMIZATION; import static org.tron.core.utils.ProposalUtil.ProposalType.ENERGY_FEE; +import static org.tron.core.utils.ProposalUtil.ProposalType.PROPOSAL_EXPIRE_TIME; import static org.tron.core.utils.ProposalUtil.ProposalType.TRANSACTION_FEE; import static org.tron.core.utils.ProposalUtil.ProposalType.WITNESS_127_PAY_PER_BLOCK; @@ -13,6 +15,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.parameter.CommonParameter; import org.tron.core.Constant; import org.tron.core.capsule.ProposalCapsule; import org.tron.core.config.args.Args; @@ -131,4 +134,21 @@ public void testUpdateConsensusLogicOptimization() { Assert.assertTrue(dbManager.getDynamicPropertiesStore().disableJavaLangMath()); } + @Test + public void testProposalExpireTime() { + long defaultWindow = dbManager.getDynamicPropertiesStore().getProposalExpireTime(); + long proposalExpireTime = CommonParameter.getInstance().getProposalExpireTime(); + Assert.assertEquals(proposalExpireTime, defaultWindow); + + Proposal proposal = Proposal.newBuilder().putParameters(PROPOSAL_EXPIRE_TIME.getCode(), + 31536000000L).build(); + ProposalCapsule proposalCapsule = new ProposalCapsule(proposal); + proposalCapsule.setExpirationTime(1627279200000L); + boolean result = ProposalService.process(dbManager, proposalCapsule); + Assert.assertTrue(result); + + long window = dbManager.getDynamicPropertiesStore().getProposalExpireTime(); + Assert.assertEquals(MAX_PROPOSAL_EXPIRE_TIME - 3000, window); + } + } \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java index 3ae090d3caf..c873613bb91 100644 --- a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java +++ b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java @@ -11,12 +11,17 @@ import io.grpc.ManagedChannelBuilder; import java.io.IOException; import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.FixMethodOrder; +import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.rules.Timeout; import org.junit.runners.MethodSorters; import org.tron.api.DatabaseGrpc; import org.tron.api.DatabaseGrpc.DatabaseBlockingStub; @@ -49,9 +54,11 @@ import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; +import org.tron.common.es.ExecutorServiceManager; import org.tron.common.utils.ByteArray; import org.tron.common.utils.PublicMethod; import org.tron.common.utils.Sha256Hash; +import org.tron.common.utils.TimeoutInterceptor; import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.AccountCapsule; @@ -60,8 +67,6 @@ import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.db.Manager; -import org.tron.core.services.interfaceOnPBFT.RpcApiServiceOnPBFT; -import org.tron.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity; import org.tron.protos.Protocol; import org.tron.protos.Protocol.Account; import org.tron.protos.Protocol.Block; @@ -111,7 +116,12 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class RpcApiServicesTest { + + private static Application appTest; private static TronApplicationContext context; + private static ManagedChannel channelFull = null; + private static ManagedChannel channelPBFT = null; + private static ManagedChannel channelSolidity = null; private static DatabaseBlockingStub databaseBlockingStubFull = null; private static DatabaseBlockingStub databaseBlockingStubSolidity = null; private static DatabaseBlockingStub databaseBlockingStubPBFT = null; @@ -120,6 +130,8 @@ public class RpcApiServicesTest { private static WalletSolidityBlockingStub blockingStubPBFT = null; @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); private static ByteString ownerAddress; private static ByteString sk; private static ByteString ask; @@ -130,9 +142,14 @@ public class RpcApiServicesTest { private static ByteString ivk; private static ByteString d; + private static ExecutorService executorService; + private static final String executorName = "rpc-test-executor"; + @BeforeClass public static void init() throws IOException { - Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + Assert.assertEquals(5, getInstance().getRpcMaxRstStream()); + Assert.assertEquals(10, getInstance().getRpcSecondsPerWindow()); String OWNER_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; getInstance().setRpcEnable(true); @@ -144,21 +161,30 @@ public static void init() throws IOException { getInstance().setMetricsPrometheusPort(PublicMethod.chooseRandomPort()); getInstance().setMetricsPrometheusEnable(true); getInstance().setP2pDisable(true); - String fullNode = String.format("%s:%d", getInstance().getNodeLanIp(), + String fullNode = String.format("%s:%d", Constant.LOCAL_HOST, getInstance().getRpcPort()); - String solidityNode = String.format("%s:%d", getInstance().getNodeLanIp(), + String solidityNode = String.format("%s:%d", Constant.LOCAL_HOST, getInstance().getRpcOnSolidityPort()); - String pBFTNode = String.format("%s:%d", getInstance().getNodeLanIp(), + String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST, getInstance().getRpcOnPBFTPort()); - ManagedChannel channelFull = ManagedChannelBuilder.forTarget(fullNode) + executorService = ExecutorServiceManager.newFixedThreadPool( + executorName, 3); + + channelFull = ManagedChannelBuilder.forTarget(fullNode) .usePlaintext() + .executor(executorService) + .intercept(new TimeoutInterceptor(5000)) .build(); - ManagedChannel channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode) + channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode) .usePlaintext() + .executor(executorService) + .intercept(new TimeoutInterceptor(5000)) .build(); - ManagedChannel channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) + channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) .usePlaintext() + .executor(executorService) + .intercept(new TimeoutInterceptor(5000)) .build(); context = new TronApplicationContext(DefaultConfig.class); databaseBlockingStubFull = DatabaseGrpc.newBlockingStub(channelFull); @@ -176,16 +202,41 @@ public static void init() throws IOException { manager.getAccountStore().put(ownerCapsule.createDbKey(), ownerCapsule); manager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); manager.getDynamicPropertiesStore().saveAllowShieldedTRC20Transaction(1); - Application appTest = ApplicationFactory.create(context); + appTest = ApplicationFactory.create(context); appTest.startup(); } @AfterClass public static void destroy() { + shutdownChannel(channelFull); + shutdownChannel(channelPBFT); + shutdownChannel(channelSolidity); + + if (executorService != null) { + ExecutorServiceManager.shutdownAndAwaitTermination( + executorService, executorName); + executorService = null; + } + context.close(); Args.clearParam(); } + private static void shutdownChannel(ManagedChannel channel) { + if (channel == null) { + return; + } + try { + channel.shutdown(); + if (!channel.awaitTermination(5, TimeUnit.SECONDS)) { + channel.shutdownNow(); + } + } catch (InterruptedException e) { + channel.shutdownNow(); + Thread.currentThread().interrupt(); + } + } + @Test public void testGetBlockByNum() { NumberMessage message = NumberMessage.newBuilder().setNum(0).build(); @@ -233,6 +284,14 @@ public void testListWitnesses() { assertNotNull(blockingStubPBFT.listWitnesses(message)); } + @Test + public void testGetPaginatedNowWitnessList() { + PaginatedMessage paginatedMessage = PaginatedMessage.newBuilder() + .setOffset(0).setLimit(5).build(); + assertNotNull(blockingStubFull.getPaginatedNowWitnessList(paginatedMessage)); + assertNotNull(blockingStubSolidity.getPaginatedNowWitnessList(paginatedMessage)); + } + @Test public void testGetAssetIssueList() { EmptyMessage message = EmptyMessage.newBuilder().build(); diff --git a/framework/src/test/java/org/tron/core/services/WalletApiTest.java b/framework/src/test/java/org/tron/core/services/WalletApiTest.java index 8890d4bfd9e..f9c95f018c4 100644 --- a/framework/src/test/java/org/tron/core/services/WalletApiTest.java +++ b/framework/src/test/java/org/tron/core/services/WalletApiTest.java @@ -2,19 +2,23 @@ import io.grpc.ManagedChannelBuilder; import java.io.IOException; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.rules.Timeout; import org.tron.api.GrpcAPI.EmptyMessage; import org.tron.api.WalletGrpc; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.TimeoutInterceptor; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; @@ -26,13 +30,16 @@ public class WalletApiTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); + private static TronApplicationContext context; private static Application appT; @BeforeClass public static void init() throws IOException { - Args.setParam(new String[]{ "-d", temporaryFolder.newFolder().toString(), + Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString(), "--p2p-disable", "true"}, Constant.TEST_CONF); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcEnable(true); @@ -45,18 +52,32 @@ public static void init() throws IOException { public void listNodesTest() { String fullNode = String.format("%s:%d", "127.0.0.1", Args.getInstance().getRpcPort()); - WalletGrpc.WalletBlockingStub walletStub = WalletGrpc - .newBlockingStub(ManagedChannelBuilder.forTarget(fullNode) - .usePlaintext() - .build()); - Assert.assertTrue(walletStub.listNodes(EmptyMessage.getDefaultInstance()) - .getNodesList().isEmpty()); + io.grpc.ManagedChannel channel = ManagedChannelBuilder.forTarget(fullNode) + .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) + .build(); + try { + WalletGrpc.WalletBlockingStub walletStub = WalletGrpc.newBlockingStub(channel); + Assert.assertTrue(walletStub.listNodes(EmptyMessage.getDefaultInstance()) + .getNodesList().isEmpty()); + } finally { + // Properly shutdown the gRPC channel to prevent resource leaks + channel.shutdown(); + try { + if (!channel.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) { + channel.shutdownNow(); + } + } catch (InterruptedException e) { + channel.shutdownNow(); + Thread.currentThread().interrupt(); + } + } } - @After - public void destroy() { - Args.clearParam(); + @AfterClass + public static void destroy() { context.destroy(); + Args.clearParam(); } } diff --git a/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java b/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java index 420d890aa48..0d2c9c9ae78 100644 --- a/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java @@ -38,7 +38,7 @@ public class HttpApiAccessFilterTest extends BaseTest { static { Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); - Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false); + Args.getInstance().setAllowShieldedTransactionApi(false); Args.getInstance().setFullNodeHttpEnable(true); Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort()); Args.getInstance().setPBFTHttpEnable(true); diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java index 84869ea0750..b3cd5844b8d 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.junit.rules.Timeout; import org.tron.api.GrpcAPI; import org.tron.api.WalletGrpc; import org.tron.api.WalletSolidityGrpc; @@ -21,13 +22,11 @@ import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.TimeoutInterceptor; import org.tron.core.ChainBaseManager; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; -import org.tron.core.services.RpcApiService; -import org.tron.core.services.interfaceOnPBFT.RpcApiServiceOnPBFT; -import org.tron.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity; @Slf4j public class LiteFnQueryGrpcInterceptorTest { @@ -49,12 +48,15 @@ public class LiteFnQueryGrpcInterceptorTest { @ClassRule public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); + /** * init logic. */ @BeforeClass public static void init() throws IOException { - Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); Args.getInstance().setRpcEnable(true); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcSolidityEnable(true); @@ -62,23 +64,25 @@ public static void init() throws IOException { Args.getInstance().setRpcPBFTEnable(true); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); Args.getInstance().setP2pDisable(true); - String fullnode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), - Args.getInstance().getRpcPort()); - String solidityNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), - Args.getInstance().getRpcOnSolidityPort()); - String pBFTNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), + String fullnode = String.format("%s:%d", Constant.LOCAL_HOST, + Args.getInstance().getRpcPort()); + String solidityNode = String.format("%s:%d", Constant.LOCAL_HOST, + Args.getInstance().getRpcOnSolidityPort()); + String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST, Args.getInstance().getRpcOnPBFTPort()); channelFull = ManagedChannelBuilder.forTarget(fullnode) - .usePlaintext() - .build(); + .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) + .build(); channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) .build(); channelpBFT = ManagedChannelBuilder.forTarget(pBFTNode) - .usePlaintext() - .build(); + .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) + .build(); context = new TronApplicationContext(DefaultConfig.class); - context.registerShutdownHook(); blockingStubFull = WalletGrpc.newBlockingStub(channelFull); blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity); blockingStubpBFT = WalletSolidityGrpc.newBlockingStub(channelpBFT); @@ -93,13 +97,13 @@ public static void init() throws IOException { @AfterClass public static void destroy() throws InterruptedException { if (channelFull != null) { - channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); + channelFull.shutdownNow(); } if (channelSolidity != null) { - channelSolidity.shutdown().awaitTermination(5, TimeUnit.SECONDS); + channelSolidity.shutdownNow(); } if (channelpBFT != null) { - channelpBFT.shutdown().awaitTermination(5, TimeUnit.SECONDS); + channelpBFT.shutdownNow(); } context.close(); Args.clearParam(); diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java index 0f0bdf1eb1f..978042a8578 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java @@ -31,7 +31,7 @@ public class LiteFnQueryHttpFilterTest extends BaseTest { static { Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); - Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false); + Args.getInstance().setAllowShieldedTransactionApi(false); Args.getInstance().setRpcEnable(false); Args.getInstance().setRpcSolidityEnable(false); Args.getInstance().setRpcPBFTEnable(false); diff --git a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java index 900ca304e7d..2e02125e014 100644 --- a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java @@ -14,12 +14,15 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.rules.Timeout; import org.tron.api.GrpcAPI.BlockExtention; import org.tron.api.GrpcAPI.BlockReq; import org.tron.api.GrpcAPI.BytesMessage; @@ -32,24 +35,29 @@ import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.TimeoutInterceptor; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.services.RpcApiService; -import org.tron.core.services.interfaceOnPBFT.RpcApiServiceOnPBFT; -import org.tron.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity; import org.tron.protos.Protocol.Transaction; @Slf4j public class RpcApiAccessInterceptorTest { private static TronApplicationContext context; + private static ManagedChannel channelFull = null; + private static ManagedChannel channelPBFT = null; + private static ManagedChannel channelSolidity = null; private static WalletGrpc.WalletBlockingStub blockingStubFull = null; private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubPBFT = null; @ClassRule public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); + /** * init logic. */ @@ -63,21 +71,24 @@ public static void init() throws IOException { Args.getInstance().setRpcPBFTEnable(true); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); Args.getInstance().setP2pDisable(true); - String fullNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), + String fullNode = String.format("%s:%d", Constant.LOCAL_HOST, Args.getInstance().getRpcPort()); - String solidityNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), + String solidityNode = String.format("%s:%d", Constant.LOCAL_HOST, Args.getInstance().getRpcOnSolidityPort()); - String pBFTNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), + String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST, Args.getInstance().getRpcOnPBFTPort()); - ManagedChannel channelFull = ManagedChannelBuilder.forTarget(fullNode) + channelFull = ManagedChannelBuilder.forTarget(fullNode) .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) .build(); - ManagedChannel channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode) + channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode) .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) .build(); - ManagedChannel channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) + channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) .build(); context = new TronApplicationContext(DefaultConfig.class); @@ -95,6 +106,15 @@ public static void init() throws IOException { */ @AfterClass public static void destroy() { + if (channelFull != null) { + channelFull.shutdownNow(); + } + if (channelPBFT != null) { + channelPBFT.shutdownNow(); + } + if (channelSolidity != null) { + channelSolidity.shutdownNow(); + } context.close(); Args.clearParam(); } diff --git a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java index 404e154a4c3..bd367fc3700 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java @@ -58,7 +58,7 @@ public MockHttpServletRequest createRequest(String contentType) { @Before public void init() { manager.getDynamicPropertiesStore().saveChangeDelegation(1); - byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); + byte[] sr = decodeFromBase58Check("27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz"); delegationStore.setBrokerage(0, sr, 10); delegationStore.setWitnessVote(0, sr, 100000000); } @@ -66,7 +66,7 @@ public void init() { @Test public void getRewardValueByJsonTest() { int expect = 138181; - String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; + String jsonParam = "{\"address\": \"27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz\"}"; MockHttpServletRequest request = createRequest("application/json"); MockHttpServletResponse response = new MockHttpServletResponse(); request.setContent(jsonParam.getBytes()); @@ -84,7 +84,7 @@ public void getRewardValueByJsonTest() { @Test public void getRewardByJsonUTF8Test() { int expect = 138181; - String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; + String jsonParam = "{\"address\": \"27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz\"}"; MockHttpServletRequest request = createRequest("application/json; charset=utf-8"); MockHttpServletResponse response = new MockHttpServletResponse(); request.setContent(jsonParam.getBytes()); @@ -105,7 +105,7 @@ public void getRewardValueTest() { MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); MockHttpServletResponse response = new MockHttpServletResponse(); mortgageService.payStandbyWitness(); - request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); + request.addParameter("address", "27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz"); getRewardServlet.doPost(request, response); try { String contentAsString = response.getContentAsString(); diff --git a/framework/src/test/java/org/tron/core/services/http/HttpServletTest.java b/framework/src/test/java/org/tron/core/services/http/HttpServletTest.java index dfd4c569e3e..03cf11f39a1 100644 --- a/framework/src/test/java/org/tron/core/services/http/HttpServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/HttpServletTest.java @@ -107,6 +107,7 @@ public class HttpServletTest { private ListNodesServlet listNodesServlet; private ListProposalsServlet listProposalsServlet; private ListWitnessesServlet listWitnessesServlet; + private GetPaginatedNowWitnessListServlet getPaginatedNowWitnessListServlet; private MarketCancelOrderServlet marketCancelOrderServlet; private MarketSellAssetServlet marketSellAssetServlet; private MetricsServlet metricsServlet; @@ -244,6 +245,7 @@ public void setUp() { listNodesServlet = new ListNodesServlet(); listProposalsServlet = new ListProposalsServlet(); listWitnessesServlet = new ListWitnessesServlet(); + getPaginatedNowWitnessListServlet = new GetPaginatedNowWitnessListServlet(); marketCancelOrderServlet = new MarketCancelOrderServlet(); marketSellAssetServlet = new MarketSellAssetServlet(); metricsServlet = new MetricsServlet(); @@ -367,6 +369,7 @@ public void doGetTest() { listNodesServlet.doGet(request, response); listProposalsServlet.doGet(request, response); listWitnessesServlet.doGet(request, response); + getPaginatedNowWitnessListServlet.doGet(request, response); marketCancelOrderServlet.doGet(request, response); marketSellAssetServlet.doGet(request, response); metricsServlet.doGet(request, response); @@ -499,6 +502,7 @@ public void doPostTest() { listNodesServlet.doPost(request, response); listProposalsServlet.doPost(request, response); listWitnessesServlet.doPost(request, response); + getPaginatedNowWitnessListServlet.doPost(request, response); marketCancelOrderServlet.doPost(request, response); marketSellAssetServlet.doPost(request, response); participateAssetIssueServlet.doPost(request, response); diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java index f9e264c515f..952e9c81467 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java @@ -8,8 +8,8 @@ import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.protos.Protocol; diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java index 19dd76e5e07..1d7f568453b 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java @@ -8,8 +8,8 @@ import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.protos.Protocol; diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java new file mode 100644 index 00000000000..d8e64308ab8 --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java @@ -0,0 +1,75 @@ +package org.tron.core.services.jsonrpc; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorData; +import com.googlecode.jsonrpc4j.ErrorResolver.JsonError; +import com.googlecode.jsonrpc4j.JsonRpcError; +import com.googlecode.jsonrpc4j.JsonRpcErrors; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.tron.core.exception.jsonrpc.JsonRpcException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; + +public class JsonRpcErrorResolverTest { + + private final JsonRpcErrorResolver resolver = JsonRpcErrorResolver.INSTANCE; + + @JsonRpcErrors({ + @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), + @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), + @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"), + @JsonRpcError(exception = JsonRpcException.class, code = -1) + }) + public void dummyMethod() { + } + + @Test + public void testResolveErrorWithTronException() throws Exception { + + String message = "JsonRpcInvalidRequestException"; + + JsonRpcException exception = new JsonRpcInvalidRequestException(message); + Method method = this.getClass().getMethod("dummyMethod"); + List arguments = new ArrayList<>(); + + JsonError error = resolver.resolveError(exception, method, arguments); + Assert.assertNotNull(error); + Assert.assertEquals(-32600, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals("{}", error.data); + + message = "JsonRpcInternalException"; + String data = "JsonRpcInternalException data"; + exception = new JsonRpcInternalException(message, data); + error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(-32000, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals(data, error.data); + + exception = new JsonRpcInternalException(message, null); + error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(-32000, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals("{}", error.data); + + message = "JsonRpcException"; + exception = new JsonRpcException(message, null); + error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(-1, error.code); + Assert.assertEquals(message, error.message); + Assert.assertTrue(error.data instanceof ErrorData); + + } + +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java index f10526e30a4..23bc11e293f 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java @@ -8,31 +8,32 @@ import org.tron.common.utils.ByteArray; import org.tron.core.Constant; import org.tron.core.Wallet; +import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.TransactionRetCapsule; import org.tron.core.config.args.Args; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; import org.tron.core.services.jsonrpc.types.TransactionReceipt; +import org.tron.core.services.jsonrpc.types.TransactionReceipt.TransactionContext; import org.tron.core.store.TransactionRetStore; import org.tron.protos.Protocol; public class TransactionReceiptTest extends BaseTest { - @Resource - private Wallet wallet; + @Resource private Wallet wallet; - @Resource - private TransactionRetStore transactionRetStore; + @Resource private TransactionRetStore transactionRetStore; static { - Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); + Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF); } @Test - public void testTransactionReceipt() { + public void testTransactionReceipt() throws JsonRpcInternalException { Protocol.TransactionInfo transactionInfo = Protocol.TransactionInfo.newBuilder() .setId(ByteString.copyFrom("1".getBytes())) .setContractAddress(ByteString.copyFrom("address1".getBytes())) .setReceipt(Protocol.ResourceReceipt.newBuilder() - .setEnergyUsageTotal(0L) + .setEnergyUsageTotal(100L) .setResult(Protocol.Transaction.Result.contractResult.DEFAULT) .build()) .addLog(Protocol.TransactionInfo.Log.newBuilder() @@ -50,17 +51,49 @@ public void testTransactionReceipt() { raw.addContract(contract.build()); Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); - TransactionReceipt transactionReceipt = new TransactionReceipt( - Protocol.Block.newBuilder().setBlockHeader( - Protocol.BlockHeader.newBuilder().setRawData( - Protocol.BlockHeader.raw.newBuilder().setNumber(1))).addTransactions( - transaction).build(), transactionInfo, wallet); + Protocol.Block block = Protocol.Block.newBuilder().setBlockHeader( + Protocol.BlockHeader.newBuilder().setRawData( + Protocol.BlockHeader.raw.newBuilder().setNumber(1))).addTransactions( + transaction).build(); - Assert.assertEquals(transactionReceipt.getBlockNumber(),"0x1"); - Assert.assertEquals(transactionReceipt.getTransactionIndex(),"0x0"); - Assert.assertEquals(transactionReceipt.getLogs().length,1); - Assert.assertEquals(transactionReceipt.getBlockHash(), - "0x0000000000000001464f071c8a336fd22eb5145dff1b245bda013ec89add8497"); - } + BlockCapsule blockCapsule = new BlockCapsule(block); + long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp()); + TransactionReceipt.TransactionContext context + = new TransactionContext(0, 2, 3); + + TransactionReceipt transactionReceipt = + new TransactionReceipt(blockCapsule, transactionInfo, context, energyFee); + + Assert.assertNotNull(transactionReceipt); + String blockHash = "0x0000000000000001464f071c8a336fd22eb5145dff1b245bda013ec89add8497"; + + // assert basic fields + Assert.assertEquals(transactionReceipt.getBlockHash(), blockHash); + Assert.assertEquals(transactionReceipt.getBlockNumber(), "0x1"); + Assert.assertEquals(transactionReceipt.getTransactionHash(), "0x31"); + Assert.assertEquals(transactionReceipt.getTransactionIndex(), "0x0"); + Assert.assertEquals(transactionReceipt.getCumulativeGasUsed(), ByteArray.toJsonHex(102)); + Assert.assertEquals(transactionReceipt.getGasUsed(), ByteArray.toJsonHex(100)); + Assert.assertEquals(transactionReceipt.getEffectiveGasPrice(), ByteArray.toJsonHex(energyFee)); + Assert.assertEquals(transactionReceipt.getStatus(), "0x1"); + // assert contract fields + Assert.assertEquals(transactionReceipt.getFrom(), ByteArray.toJsonHexAddress(new byte[0])); + Assert.assertEquals(transactionReceipt.getTo(), ByteArray.toJsonHexAddress(new byte[0])); + Assert.assertNull(transactionReceipt.getContractAddress()); + + // assert logs fields + Assert.assertEquals(transactionReceipt.getLogs().length, 1); + Assert.assertEquals(transactionReceipt.getLogs()[0].getLogIndex(), "0x3"); + Assert.assertEquals( + transactionReceipt.getLogs()[0].getBlockHash(), blockHash); + Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockNumber(), "0x1"); + Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionHash(), "0x31"); + Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionIndex(), "0x0"); + + // assert default fields + Assert.assertNull(transactionReceipt.getRoot()); + Assert.assertEquals(transactionReceipt.getType(), "0x0"); + Assert.assertEquals(transactionReceipt.getLogsBloom(), ByteArray.toJsonHex(new byte[256])); + } } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java index a71441c73b4..625981df3bb 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java @@ -9,49 +9,61 @@ import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.TransactionCapsule; import org.tron.core.config.args.Args; import org.tron.core.services.jsonrpc.types.TransactionResult; import org.tron.protos.Protocol; +import org.tron.protos.contract.SmartContractOuterClass; public class TransactionResultTest extends BaseTest { @Resource private Wallet wallet; + private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc"; + private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548"; + static { - Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); + Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF); } @Test public void testBuildTransactionResultWithBlock() { - Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder().addContract( - Protocol.Transaction.Contract.newBuilder().setType( - Protocol.Transaction.Contract.ContractType.TriggerSmartContract)); - Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); + SmartContractOuterClass.TriggerSmartContract.Builder builder2 = + SmartContractOuterClass.TriggerSmartContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) + .setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS))); + TransactionCapsule transactionCapsule = new TransactionCapsule(builder2.build(), + Protocol.Transaction.Contract.ContractType.TriggerSmartContract); + Protocol.Transaction transaction = transactionCapsule.getInstance(); BlockCapsule blockCapsule = new BlockCapsule(Protocol.Block.newBuilder().setBlockHeader( Protocol.BlockHeader.newBuilder().setRawData(Protocol.BlockHeader.raw.newBuilder() .setParentHash(ByteString.copyFrom(ByteArray.fromHexString( "0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b82"))) .setNumber(9))).addTransactions(transaction).build()); - TransactionResult transactionResult = new TransactionResult(blockCapsule,0, transaction, - 100,1, wallet); + TransactionResult transactionResult = new TransactionResult(blockCapsule, 0, transaction, + 100, 1, wallet); Assert.assertEquals(transactionResult.getBlockNumber(), "0x9"); - Assert.assertEquals(transactionResult.getHash(), - "0xdebef90d0a8077620711b1b5af2b702665887ddcbf80868108026e1ab5e0bfb7"); + Assert.assertEquals("0x5691531881bc44adbc722060d85fdf29265823db8e884b0d104fcfbba253cf11", + transactionResult.getHash()); Assert.assertEquals(transactionResult.getGasPrice(), "0x1"); Assert.assertEquals(transactionResult.getGas(), "0x64"); } @Test public void testBuildTransactionResult() { - Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder().addContract( - Protocol.Transaction.Contract.newBuilder().setType( - Protocol.Transaction.Contract.ContractType.TriggerSmartContract)); - Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); - TransactionResult transactionResult = new TransactionResult(transaction, wallet); - Assert.assertEquals(transactionResult.getHash(), - "0xdebef90d0a8077620711b1b5af2b702665887ddcbf80868108026e1ab5e0bfb7"); + SmartContractOuterClass.TriggerSmartContract.Builder builder2 = + SmartContractOuterClass.TriggerSmartContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) + .setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS))); + TransactionCapsule transactionCapsule = new TransactionCapsule(builder2.build(), + Protocol.Transaction.Contract.ContractType.TriggerSmartContract); + + TransactionResult transactionResult = + new TransactionResult(transactionCapsule.getInstance(), wallet); + Assert.assertEquals("0x5691531881bc44adbc722060d85fdf29265823db8e884b0d104fcfbba253cf11", + transactionResult.getHash()); Assert.assertEquals(transactionResult.getGasPrice(), "0x"); Assert.assertEquals(transactionResult.getNonce(), "0x0000000000000000"); } diff --git a/framework/src/test/java/org/tron/core/services/stop/BlockTimeStopTest.java b/framework/src/test/java/org/tron/core/services/stop/BlockTimeStopTest.java index 1e16ad6c3b0..bf7bcf2cd7e 100644 --- a/framework/src/test/java/org/tron/core/services/stop/BlockTimeStopTest.java +++ b/framework/src/test/java/org/tron/core/services/stop/BlockTimeStopTest.java @@ -1,8 +1,10 @@ package org.tron.core.services.stop; import java.text.ParseException; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.TimeZone; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.tron.common.cron.CronExpression; @@ -12,14 +14,14 @@ public class BlockTimeStopTest extends ConditionallyStopTest { private static final DateTimeFormatter pattern = DateTimeFormatter .ofPattern("ss mm HH dd MM ? yyyy"); - private static final String time = localDateTime.plusSeconds(12 * 3).format(pattern); private static CronExpression cronExpression; static { try { - cronExpression = new CronExpression(time); + cronExpression = new CronExpression(localDateTime.plusSeconds(12 * 3).format(pattern)); + cronExpression.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); } catch (ParseException e) { logger.error("{}", e.getMessage()); } diff --git a/framework/src/test/java/org/tron/core/services/stop/ConditionallyStopTest.java b/framework/src/test/java/org/tron/core/services/stop/ConditionallyStopTest.java index b417b27b380..f9795def416 100644 --- a/framework/src/test/java/org/tron/core/services/stop/ConditionallyStopTest.java +++ b/framework/src/test/java/org/tron/core/services/stop/ConditionallyStopTest.java @@ -2,13 +2,11 @@ import com.google.common.collect.Maps; import com.google.protobuf.ByteString; -import java.io.File; import java.io.IOException; +import java.time.Instant; import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -16,7 +14,6 @@ import java.util.stream.IntStream; import lombok.extern.slf4j.Slf4j; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; @@ -25,11 +22,10 @@ import org.tron.common.crypto.ECKey; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; -import org.tron.common.utils.FileUtil; -import org.tron.common.utils.LocalWitnesses; -import org.tron.common.utils.PublicMethod; import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.Utils; +import org.tron.consensus.ConsensusDelegate; +import org.tron.consensus.dpos.DposService; import org.tron.consensus.dpos.DposSlot; import org.tron.core.ChainBaseManager; import org.tron.core.Constant; @@ -39,23 +35,18 @@ import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.consensus.ConsensusService; -import org.tron.core.db.BlockGenerate; import org.tron.core.db.Manager; import org.tron.core.net.TronNetDelegate; import org.tron.protos.Protocol; -@Slf4j -public abstract class ConditionallyStopTest extends BlockGenerate { +@Slf4j(topic = "test") +public abstract class ConditionallyStopTest { @ClassRule public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); static ChainBaseManager chainManager; private static DposSlot dposSlot; - - private final String key = PublicMethod.getRandomPrivateKey(); - private final byte[] privateKey = ByteArray.fromHexString(key); - private final AtomicInteger port = new AtomicInteger(0); protected String dbPath; protected Manager dbManager; @@ -63,10 +54,12 @@ public abstract class ConditionallyStopTest extends BlockGenerate { private TronNetDelegate tronNetDelegate; private TronApplicationContext context; + private DposService dposService; + private ConsensusDelegate consensusDelegate; - static LocalDateTime localDateTime = LocalDateTime.now(); - private long time = ZonedDateTime.of(localDateTime, - ZoneId.systemDefault()).toInstant().toEpochMilli(); + private static final Instant instant = Instant.parse("2025-10-01T00:00:00Z"); + private final long time = instant.toEpochMilli(); + static LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); protected abstract void initParameter(CommonParameter parameter); @@ -76,6 +69,8 @@ protected void initDbPath() throws IOException { dbPath = temporaryFolder.newFolder().toString(); } + private Map witnesses; + @Before public void init() throws Exception { @@ -84,30 +79,37 @@ public void init() throws Exception { logger.info("Full node running."); Args.setParam(new String[] {"-d", dbPath}, Constant.TEST_CONF); Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet()); - + Args.getInstance().genesisBlock.setTimestamp(Long.toString(time)); initParameter(Args.getInstance()); context = new TronApplicationContext(DefaultConfig.class); dbManager = context.getBean(Manager.class); - setManager(dbManager); dposSlot = context.getBean(DposSlot.class); ConsensusService consensusService = context.getBean(ConsensusService.class); consensusService.start(); chainManager = dbManager.getChainBaseManager(); tronNetDelegate = context.getBean(TronNetDelegate.class); + dposService = context.getBean(DposService.class); + consensusDelegate = context.getBean(ConsensusDelegate.class); tronNetDelegate.setExit(false); currentHeader = dbManager.getDynamicPropertiesStore() .getLatestBlockHeaderNumberFromDB(); - byte[] address = PublicMethod.getAddressByteByPrivateKey(key); - ByteString addressByte = ByteString.copyFrom(address); - WitnessCapsule witnessCapsule = new WitnessCapsule(addressByte); - chainManager.getWitnessStore().put(addressByte.toByteArray(), witnessCapsule); - chainManager.addWitness(addressByte); - - AccountCapsule accountCapsule = - new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build()); - chainManager.getAccountStore().put(addressByte.toByteArray(), accountCapsule); + chainManager.getWitnessScheduleStore().reset(); + chainManager.getWitnessStore().reset(); + witnesses = addTestWitnessAndAccount(); + + List allWitnesses = new ArrayList<>(); + consensusDelegate.getAllWitnesses().forEach(witnessCapsule -> + allWitnesses.add(witnessCapsule.getAddress())); + dposService.updateWitness(allWitnesses); + List activeWitnesses = consensusDelegate.getActiveWitnesses(); + activeWitnesses.forEach(address -> { + WitnessCapsule witnessCapsule = consensusDelegate.getWitness(address.toByteArray()); + witnessCapsule.setIsJobs(true); + consensusDelegate.saveWitness(witnessCapsule); + }); + chainManager.getDynamicPropertiesStore().saveNextMaintenanceTime(time); } @After @@ -116,72 +118,57 @@ public void destroy() { context.destroy(); } - private void generateBlock(Map witnessAndAccount) throws Exception { + private void generateBlock() throws Exception { BlockCapsule block = createTestBlockCapsule( - chainManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp() + 3000, + chainManager.getNextBlockSlotTime(), chainManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1, - chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - witnessAndAccount); + chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString()); tronNetDelegate.processBlock(block, false); + + logger.info("headerNum: {} solidityNum: {}, dbNum: {}", + block.getNum(), chainManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum(), + chainManager.getDynamicPropertiesStore().getLatestBlockHeaderNumberFromDB()); } - @Test + @Test(timeout = 30_000) // milliseconds public void testStop() throws Exception { - final ECKey ecKey = ECKey.fromPrivate(privateKey); - Assert.assertNotNull(ecKey); - byte[] address = ecKey.getAddress(); - WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address)); - chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>()); - chainManager.addWitness(ByteString.copyFrom(address)); - - Protocol.Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey); - - tronNetDelegate.processBlock(new BlockCapsule(block), false); - - Map witnessAndAccount = addTestWitnessAndAccount(); - witnessAndAccount.put(ByteString.copyFrom(address), key); while (!tronNetDelegate.isHitDown()) { - generateBlock(witnessAndAccount); + generateBlock(); } - Assert.assertTrue(tronNetDelegate.isHitDown()); check(); } - private Map addTestWitnessAndAccount() { - chainManager.getWitnesses().clear(); - return IntStream.range(0, 2) + private Map addTestWitnessAndAccount() { + return IntStream.range(0, 27) .mapToObj( i -> { ECKey ecKey = new ECKey(Utils.getRandom()); String privateKey = ByteArray.toHexString(ecKey.getPrivKey().toByteArray()); ByteString address = ByteString.copyFrom(ecKey.getAddress()); - WitnessCapsule witnessCapsule = new WitnessCapsule(address); + WitnessCapsule witnessCapsule = new WitnessCapsule(address, 27 - i, "SR" + i); chainManager.getWitnessStore().put(address.toByteArray(), witnessCapsule); - chainManager.addWitness(address); - AccountCapsule accountCapsule = new AccountCapsule(Protocol.Account.newBuilder().setAddress(address).build()); chainManager.getAccountStore().put(address.toByteArray(), accountCapsule); - return Maps.immutableEntry(address, privateKey); + return Maps.immutableEntry(ByteArray.toHexString(ecKey.getAddress()), privateKey); }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } private BlockCapsule createTestBlockCapsule(long time, - long number, ByteString hash, - Map witnessAddressMap) { - ByteString witnessAddress = dposSlot.getScheduledWitness(dposSlot.getSlot(time)); - BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(hash), time, - witnessAddress); + long number, ByteString hash) { + long slot = dposSlot.getSlot(time); + ByteString witness = dposSlot.getScheduledWitness(slot); + BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(hash), time, witness); blockCapsule.generatedByMyself = true; blockCapsule.setMerkleRoot(); - blockCapsule.sign(ByteArray.fromHexString(witnessAddressMap.get(witnessAddress))); + String pri = witnesses.get(ByteArray.toHexString(witness.toByteArray())); + blockCapsule.sign(ByteArray.fromHexString(pri)); return blockCapsule; } - } diff --git a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java index 049fb2528b1..5d403b54f90 100644 --- a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java @@ -84,7 +84,8 @@ public static void init() { }, "config-test-mainnet.conf" ); - Args.setFullNodeAllowShieldedTransaction(true); + Args.getInstance().setAllowShieldedTransactionApi(true); + ZksnarkInitService.librustzcashInitZksnarkParams(); } private static int randomInt(int minInt, int maxInt) { @@ -115,10 +116,6 @@ public static void test(byte[] K, byte[] ovk, byte[] cv, byte[] cm, byte[] epk) null, 0, cipher_nonce, K))); } - public static void librustzcashInitZksnarkParams() { - ZksnarkInitService.librustzcashInitZksnarkParams(); - } - @Test public void testLibsodium() throws ZksnarkException { byte[] K = new byte[32]; @@ -275,7 +272,6 @@ public long benchmarkCreateSpend() throws ZksnarkException { @Ignore @Test public void calBenchmarkSpendConcurrent() throws Exception { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 2; @@ -301,13 +297,13 @@ public void calBenchmarkSpendConcurrent() throws Exception { })); countDownLatch.await(); + generatePool.shutdown(); logger.info("generate cost time:" + (System.currentTimeMillis() - startGenerate)); } @Test public void calBenchmarkSpend() throws ZksnarkException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 2; @@ -374,7 +370,6 @@ public long benchmarkCreateSaplingSpend() throws BadItemException, ZksnarkExcept @Test public void calBenchmarkCreateSaplingSpend() throws BadItemException, ZksnarkException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 2; @@ -451,7 +446,6 @@ public long benchmarkCreateSaplingOutput() throws BadItemException, ZksnarkExcep @Test public void calBenchmarkCreateSaplingOutPut() throws BadItemException, ZksnarkException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 2; @@ -479,7 +473,6 @@ public void calBenchmarkCreateSaplingOutPut() throws BadItemException, ZksnarkEx @Test public void checkVerifyOutErr() throws ZksnarkException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); // expect fail diff --git a/framework/src/test/java/org/tron/core/zksnark/SaplingNoteTest.java b/framework/src/test/java/org/tron/core/zksnark/SaplingNoteTest.java index da4df70d9ac..34913c98ccc 100644 --- a/framework/src/test/java/org/tron/core/zksnark/SaplingNoteTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/SaplingNoteTest.java @@ -22,7 +22,7 @@ public class SaplingNoteTest { @BeforeClass public static void init() { - Args.setFullNodeAllowShieldedTransaction(true); + Args.getInstance().setAllowShieldedTransactionApi(true); // Args.getInstance().setAllowShieldedTransaction(1); ZksnarkInitService.librustzcashInitZksnarkParams(); } diff --git a/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java b/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java index 7746066abfa..e7dfa06d094 100644 --- a/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java @@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.tron.api.GrpcAPI; @@ -118,6 +119,11 @@ public class SendCoinShieldTest extends BaseTest { .fromHexString("030c8c2bc59fb3eb8afb047a8ea4b028743d23e7d38c6fa30908358431e2314d"); } + @BeforeClass + public static void initZksnarkParams() { + ZksnarkInitService.librustzcashInitZksnarkParams(); + } + /** * Init data. */ @@ -219,10 +225,6 @@ private IncrementalMerkleVoucherContainer createSimpleMerkleVoucherContainer(byt return tree.toVoucher(); } - private void librustzcashInitZksnarkParams() { - ZksnarkInitService.librustzcashInitZksnarkParams(); - } - @Test public void testStringRevert() { byte[] bytes = ByteArray @@ -235,7 +237,6 @@ public void testStringRevert() { @Test public void testGenerateSpendProof() throws Exception { - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); SpendingKey sk = SpendingKey .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); @@ -270,7 +271,6 @@ public void testGenerateSpendProof() throws Exception { @Test public void generateOutputProof() throws ZksnarkException { - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); SpendingKey spendingKey = SpendingKey.random(); FullViewingKey fullViewingKey = spendingKey.fullViewingKey(); @@ -289,7 +289,6 @@ public void generateOutputProof() throws ZksnarkException { @Test public void verifyOutputProof() throws ZksnarkException { - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); SpendingKey spendingKey = SpendingKey.random(); FullViewingKey fullViewingKey = spendingKey.fullViewingKey(); @@ -321,7 +320,6 @@ public void verifyOutputProof() throws ZksnarkException { @Test public void testDecryptReceiveWithIvk() throws ZksnarkException { //verify c_enc - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(); SpendingKey spendingKey = SpendingKey.random(); @@ -385,9 +383,6 @@ public String byte2intstring(byte[] input) { @Test public void testDecryptReceiveWithOvk() throws Exception { - //decode c_out with ovk. - librustzcashInitZksnarkParams(); - // construct payment address SpendingKey spendingKey2 = SpendingKey .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); @@ -467,7 +462,6 @@ public void pushShieldedTransactionAndDecryptWithIvk() AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -556,7 +550,6 @@ public void pushShieldedTransactionAndDecryptWithOvk() AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -643,10 +636,8 @@ private byte[] getHash() { @Ignore @Test public void checkZksnark() throws BadItemException, ZksnarkException { - librustzcashInitZksnarkParams(); long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); // generate spend proof - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(4010 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -679,7 +670,6 @@ public void checkZksnark() throws BadItemException, ZksnarkException { @Test public void testVerifySpendProof() throws BadItemException, ZksnarkException { - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); SpendingKey sk = SpendingKey .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); @@ -717,7 +707,6 @@ public void testVerifySpendProof() throws BadItemException, ZksnarkException { public void saplingBindingSig() throws BadItemException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); // generate spend proof - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); SpendingKey sk = SpendingKey .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); @@ -756,7 +745,6 @@ public void pushShieldedTransaction() ContractExeException, AccountResourceInsufficientException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); // generate spend proof - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(4010 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -789,7 +777,6 @@ public void pushShieldedTransaction() @Test public void finalCheck() throws BadItemException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); // generate spend proof SpendingKey sk = SpendingKey @@ -965,7 +952,6 @@ public void getSpendingKey() throws Exception { @Test public void testTwoCMWithDiffSkInOneTx() throws Exception { // generate spend proof - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(110 * 1000000L); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1025,7 +1011,6 @@ private void executeTx(TransactionCapsule transactionCap) throws Exception { @Test public void testValueBalance() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); //case 1, a public input, no input cm, an output cm, no public output { @@ -1246,7 +1231,6 @@ public void testValueBalance() throws Exception { @Test public void TestCreateMultipleTxAtTheSameTime() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); List txList = Lists.newArrayList(); //case 1, a public input, no input cm, an output cm, no public output @@ -1364,7 +1348,6 @@ public void TestCreateMultipleTxAtTheSameTime() throws Exception { @Test public void TestCtxGeneratesTooMuchProof() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); //case 3, no public input, an input cm, no output cm, a public output { @@ -1439,7 +1422,6 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo @Test public void TestGeneratesProofWithDiffCtx() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); //case 3, no public input, an input cm, no output cm, a public output @@ -1498,7 +1480,6 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo @Test public void TestGeneratesProofWithWrongAlpha() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); //case 3, no public input, an input cm, no output cm, a public output { @@ -1536,7 +1517,6 @@ public void TestGeneratesProofWithWrongAlpha() throws Exception { @Test public void TestGeneratesProofWithWrongRcm() throws Exception { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); // generate spend proof SpendingKey sk = SpendingKey.random(); @@ -1557,7 +1537,6 @@ public void TestGeneratesProofWithWrongRcm() throws Exception { @Test public void TestWrongAsk() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); //case 3, no public input, an input cm, no output cm, a public output @@ -1667,7 +1646,6 @@ private TransactionCapsule generateDefaultBuilder(ZenTransactionBuilder builder) @Test public void TestDefaultBuilder() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); @@ -1678,7 +1656,6 @@ public void TestDefaultBuilder() throws Exception { @Test public void TestWrongSpendRk() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); { @@ -1716,7 +1693,6 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo @Test public void TestWrongSpendProof() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); { @@ -1761,7 +1737,6 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo @Test public void TestWrongNf() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); { @@ -1800,7 +1775,6 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo @Test public void TestWrongAnchor() throws Exception { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); { ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet) { diff --git a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java index 94d7f38cd8a..118e0e1f384 100755 --- a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java @@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.api.GrpcAPI.DecryptNotes; @@ -127,9 +128,14 @@ public class ShieldedReceiveTest extends BaseTest { private static boolean init; static { - Args.setParam(new String[]{"--output-directory", dbPath()}, "config-localtest.conf"); + Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, "config-localtest.conf"); ADDRESS_ONE_PRIVATE_KEY = getRandomPrivateKey(); - FROM_ADDRESS = getHexAddressByPrivateKey(ADDRESS_ONE_PRIVATE_KEY);; + FROM_ADDRESS = getHexAddressByPrivateKey(ADDRESS_ONE_PRIVATE_KEY); + } + + @BeforeClass + public static void initZksnarkParams() { + ZksnarkInitService.librustzcashInitZksnarkParams(); } /** @@ -145,10 +151,6 @@ public void init() { init = true; } - private static void librustzcashInitZksnarkParams() { - ZksnarkInitService.librustzcashInitZksnarkParams(); - } - private static byte[] randomUint256() { return org.tron.keystore.Wallet.generateRandomBytes(32); } @@ -226,6 +228,11 @@ private void updateTotalShieldedPoolValue(long valueBalance) { chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(totalShieldedPoolValue); } + @Test + public void testIsMining() { + Assert.assertTrue(wallet.isMining()); + } + /* * test of change ShieldedTransactionFee proposal */ @@ -244,7 +251,6 @@ public void testSetShieldedTransactionFee() { */ @Test public void testCreateBeforeAllowZksnark() throws ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(0); createCapsule(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -284,7 +290,6 @@ public void testCreateBeforeAllowZksnark() throws ZksnarkException { @Test public void testBroadcastBeforeAllowZksnark() throws ZksnarkException, SignatureFormatException, SignatureException, PermissionException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(0);// or 1 createCapsule(); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -310,20 +315,16 @@ public void testBroadcastBeforeAllowZksnark() ADDRESS_ONE_PRIVATE_KEY, chainBaseManager.getAccountStore()); try { dbManager.pushTransaction(transactionCap); - Assert.assertFalse(true); } catch (Exception e) { Assert.assertTrue(e instanceof ContractValidateException); - Assert.assertEquals( - "Not support Shielded Transaction, need to be opened by the committee", - e.getMessage()); } } /* - * generate spendproof, dataToBeSigned, outputproof example dynamicly according to the params file + * generate spendproof, dataToBeSigned, + * outputproof example dynamically according to the params file */ public String[] generateSpendAndOutputParams() throws ZksnarkException, BadItemException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -460,7 +461,6 @@ private long benchmarkVerifyOutput(String outputParams) throws ZksnarkException @Test public void calBenchmarkVerifySpend() throws ZksnarkException, BadItemException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 10; @@ -492,7 +492,6 @@ public void calBenchmarkVerifySpend() throws ZksnarkException, BadItemException @Test public void calBenchmarkVerifyOutput() throws ZksnarkException, BadItemException { - librustzcashInitZksnarkParams(); System.out.println("--- load ok ---"); int count = 2; @@ -588,7 +587,6 @@ private ZenTransactionBuilder generateBuilderWithoutColumnInDescription( @Test public void testReceiveDescriptionWithEmptyCv() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -634,7 +632,6 @@ public void testReceiveDescriptionWithEmptyCv() @Test public void testReceiveDescriptionWithEmptyCm() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -677,7 +674,6 @@ public void testReceiveDescriptionWithEmptyCm() @Test public void testReceiveDescriptionWithEmptyEpk() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -719,7 +715,6 @@ public void testReceiveDescriptionWithEmptyEpk() @Test public void testReceiveDescriptionWithEmptyZkproof() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -762,7 +757,6 @@ public void testReceiveDescriptionWithEmptyZkproof() @Test public void testReceiveDescriptionWithEmptyCenc() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); dbManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -805,7 +799,6 @@ public void testReceiveDescriptionWithEmptyCenc() @Test public void testReceiveDescriptionWithEmptyCout() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1063,7 +1056,6 @@ private ZenTransactionBuilder generateBuilder(ZenTransactionBuilder builder, lon @Test public void testReceiveDescriptionWithWrongCv() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1092,7 +1084,6 @@ public void testReceiveDescriptionWithWrongCv() @Test public void testReceiveDescriptionWithWrongZkproof() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1121,7 +1112,6 @@ public void testReceiveDescriptionWithWrongZkproof() @Test public void testReceiveDescriptionWithWrongD() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1152,7 +1142,6 @@ public void testReceiveDescriptionWithWrongD() @Test public void testReceiveDescriptionWithWrongPkd() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1183,7 +1172,6 @@ public void testReceiveDescriptionWithWrongPkd() @Test public void testReceiveDescriptionWithWrongValue() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1212,7 +1200,6 @@ public void testReceiveDescriptionWithWrongValue() @Test public void testReceiveDescriptionWithWrongRcm() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1241,7 +1228,6 @@ public void testReceiveDescriptionWithWrongRcm() @Test public void testNotMatchAskAndNsk() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1292,7 +1278,6 @@ public void testNotMatchAskAndNsk() @Test public void testRandomOvk() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1336,7 +1321,6 @@ public void testRandomOvk() //@Test not used public void testSameInputCm() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1385,7 +1369,6 @@ public void testSameInputCm() @Test public void testSameOutputCm() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1437,7 +1420,6 @@ public void testSameOutputCm() @Test public void testShieldInputInsufficient() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1482,7 +1464,6 @@ public void testShieldInputInsufficient() */ @Test public void testTransparentInputInsufficient() throws RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); @@ -1731,7 +1712,6 @@ public TransactionCapsule generateTransactionCapsule(ZenTransactionBuilder build public void testSignWithoutFromAddress() throws BadItemException, ContractValidateException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1771,7 +1751,6 @@ public void testSignWithoutFromAddress() public void testSignWithoutFromAmout() throws BadItemException, ContractValidateException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1810,7 +1789,6 @@ public void testSignWithoutFromAmout() @Test public void testSignWithoutSpendDescription() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1856,7 +1834,6 @@ public void testSignWithoutSpendDescription() @Test public void testSignWithoutReceiveDescription() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1903,7 +1880,6 @@ public void testSignWithoutReceiveDescription() public void testSignWithoutToAddress() throws BadItemException, ContractValidateException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1943,7 +1919,6 @@ public void testSignWithoutToAddress() public void testSignWithoutToAmount() throws BadItemException, ContractValidateException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -1982,7 +1957,6 @@ public void testSignWithoutToAmount() @Test public void testSpendSignatureWithWrongColumn() throws BadItemException, RuntimeException, ZksnarkException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -2100,7 +2074,6 @@ public void testSpendSignatureWithWrongColumn() @Test public void testIsolateSignature() throws ZksnarkException, BadItemException, ContractValidateException, ContractExeException { - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -2235,7 +2208,6 @@ public void testMemoTooLong() throws ContractValidateException, TooBigTransactio AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -2318,7 +2290,6 @@ public void testMemoNotEnough() throws ContractValidateException, TooBigTransact AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException { long ctx = JLibrustzcash.librustzcashSaplingProvingCtxInit(); - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(100 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); @@ -2412,7 +2383,6 @@ public void pushSameSkAndScanAndSpend() throws Exception { dbManager.pushBlock(new BlockCapsule(block)); //create transactions - librustzcashInitZksnarkParams(); chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); diff --git a/framework/src/test/java/org/tron/keystroe/CredentialsTest.java b/framework/src/test/java/org/tron/keystroe/CredentialsTest.java index ce992c3443f..2642129e00a 100644 --- a/framework/src/test/java/org/tron/keystroe/CredentialsTest.java +++ b/framework/src/test/java/org/tron/keystroe/CredentialsTest.java @@ -1,6 +1,5 @@ package org.tron.keystroe; -import lombok.var; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -11,24 +10,24 @@ public class CredentialsTest { @Test public void test_equality() { - var aObject = new Object(); - var si = Mockito.mock(SignInterface.class); - var si2 = Mockito.mock(SignInterface.class); - var si3 = Mockito.mock(SignInterface.class); - var address = "TQhZ7W1RudxFdzJMw6FvMnujPxrS6sFfmj".getBytes(); - var address2 = "TNCmcTdyrYKMtmE1KU2itzeCX76jGm5Not".getBytes(); + Object aObject = new Object(); + SignInterface si = Mockito.mock(SignInterface.class); + SignInterface si2 = Mockito.mock(SignInterface.class); + SignInterface si3 = Mockito.mock(SignInterface.class); + byte[] address = "TQhZ7W1RudxFdzJMw6FvMnujPxrS6sFfmj".getBytes(); + byte[] address2 = "TNCmcTdyrYKMtmE1KU2itzeCX76jGm5Not".getBytes(); Mockito.when(si.getAddress()).thenReturn(address); Mockito.when(si2.getAddress()).thenReturn(address); Mockito.when(si3.getAddress()).thenReturn(address2); - var aCredential = Credentials.create(si); + Credentials aCredential = Credentials.create(si); Assert.assertFalse(aObject.equals(aCredential)); Assert.assertFalse(aCredential.equals(aObject)); Assert.assertFalse(aCredential.equals(null)); - var anotherCredential = Credentials.create(si); - Assert.assertTrue(aCredential.equals(anotherCredential)); - var aCredential2 = Credentials.create(si2); + Credentials anotherCredential = Credentials.create(si); Assert.assertTrue(aCredential.equals(anotherCredential)); - var aCredential3 = Credentials.create(si3); + Credentials aCredential2 = Credentials.create(si2); + Assert.assertTrue(aCredential.equals(anotherCredential)); + Credentials aCredential3 = Credentials.create(si3); Assert.assertFalse(aCredential.equals(aCredential3)); } } diff --git a/framework/src/test/java/org/tron/program/DBConvertTest.java b/framework/src/test/java/org/tron/program/DBConvertTest.java deleted file mode 100644 index 7b3f797d627..00000000000 --- a/framework/src/test/java/org/tron/program/DBConvertTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package org.tron.program; - -import static org.fusesource.leveldbjni.JniDBFactory.factory; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.UUID; -import org.iq80.leveldb.DB; -import org.iq80.leveldb.Options; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.tron.common.utils.ByteArray; -import org.tron.common.utils.FileUtil; -import org.tron.common.utils.MarketOrderPriceComparatorForLevelDB; -import org.tron.core.capsule.MarketOrderIdListCapsule; -import org.tron.core.capsule.utils.MarketUtils; - -public class DBConvertTest { - - - private static final String INPUT_DIRECTORY = "output-directory/convert-database/"; - private static final String OUTPUT_DIRECTORY = "output-directory/convert-database-dest/"; - private static final String ACCOUNT = "account"; - private static final String MARKET = "market_pair_price_to_order"; - - - @BeforeClass - public static void init() throws IOException { - if (new File(INPUT_DIRECTORY).mkdirs()) { - initDB(new File(INPUT_DIRECTORY,ACCOUNT)); - initDB(new File(INPUT_DIRECTORY,MARKET)); - } - } - - private static void initDB(File file) throws IOException { - Options dbOptions = DBConvert.newDefaultLevelDbOptions(); - if (file.getName().contains("market_pair_price_to_order")) { - dbOptions.comparator(new MarketOrderPriceComparatorForLevelDB()); - try (DB db = factory.open(file,dbOptions)) { - - byte[] sellTokenID1 = ByteArray.fromString("100"); - byte[] buyTokenID1 = ByteArray.fromString("200"); - byte[] pairPriceKey1 = MarketUtils.createPairPriceKey( - sellTokenID1, - buyTokenID1, - 1000L, - 2001L - ); - byte[] pairPriceKey2 = MarketUtils.createPairPriceKey( - sellTokenID1, - buyTokenID1, - 1000L, - 2002L - ); - byte[] pairPriceKey3 = MarketUtils.createPairPriceKey( - sellTokenID1, - buyTokenID1, - 1000L, - 2003L - ); - - MarketOrderIdListCapsule capsule1 = new MarketOrderIdListCapsule(ByteArray.fromLong(1), - ByteArray.fromLong(1)); - MarketOrderIdListCapsule capsule2 = new MarketOrderIdListCapsule(ByteArray.fromLong(2), - ByteArray.fromLong(2)); - MarketOrderIdListCapsule capsule3 = new MarketOrderIdListCapsule(ByteArray.fromLong(3), - ByteArray.fromLong(3)); - - //Use out-of-order insertion,key in store should be 1,2,3 - db.put(pairPriceKey1, capsule1.getData()); - db.put(pairPriceKey2, capsule2.getData()); - db.put(pairPriceKey3, capsule3.getData()); - } - - } else { - try (DB db = factory.open(file,dbOptions)) { - for (int i = 0; i < 100; i++) { - byte[] bytes = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8); - db.put(bytes, bytes); - } - } - } - - } - - @AfterClass - public static void destroy() { - FileUtil.deleteDir(new File(INPUT_DIRECTORY)); - FileUtil.deleteDir(new File(OUTPUT_DIRECTORY)); - } - - @Test - public void testRun() { - String[] args = new String[] { INPUT_DIRECTORY, OUTPUT_DIRECTORY }; - Assert.assertEquals(0, DBConvert.run(args)); - } - - @Test - public void testNotExist() { - String[] args = new String[] {OUTPUT_DIRECTORY + File.separator + UUID.randomUUID(), - OUTPUT_DIRECTORY}; - Assert.assertEquals(404, DBConvert.run(args)); - } - - @Test - public void testEmpty() { - File file = new File(OUTPUT_DIRECTORY + File.separator + UUID.randomUUID()); - file.mkdirs(); - file.deleteOnExit(); - String[] args = new String[] {file.toString(), OUTPUT_DIRECTORY}; - Assert.assertEquals(0, DBConvert.run(args)); - } -} diff --git a/framework/src/test/java/org/tron/program/SolidityNodeTest.java b/framework/src/test/java/org/tron/program/SolidityNodeTest.java index a95d07c0c11..943c73cb9ed 100755 --- a/framework/src/test/java/org/tron/program/SolidityNodeTest.java +++ b/framework/src/test/java/org/tron/program/SolidityNodeTest.java @@ -1,13 +1,21 @@ package org.tron.program; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.Timeout; import org.tron.common.BaseTest; import org.tron.common.client.DatabaseGrpcClient; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.args.Args; +import org.tron.core.exception.TronError; import org.tron.core.services.RpcApiService; import org.tron.core.services.http.solidity.SolidityNodeHttpApiService; import org.tron.protos.Protocol.Block; @@ -20,23 +28,35 @@ public class SolidityNodeTest extends BaseTest { RpcApiService rpcApiService; @Resource SolidityNodeHttpApiService solidityNodeHttpApiService; + static int rpcPort = PublicMethod.chooseRandomPort(); + static int solidityHttpPort = PublicMethod.chooseRandomPort(); + + @Rule + public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); static { - Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); - Args.getInstance().setSolidityNode(true); + Args.setParam(new String[] {"-d", dbPath(), "--solidity"}, Constant.TEST_CONF); + Args.getInstance().setRpcPort(rpcPort); + Args.getInstance().setSolidityHttpPort(solidityHttpPort); } @Test public void testSolidityArgs() { Assert.assertNotNull(Args.getInstance().getTrustNodeAddr()); Assert.assertTrue(Args.getInstance().isSolidityNode()); + String trustNodeAddr = Args.getInstance().getTrustNodeAddr(); + Args.getInstance().setTrustNodeAddr(null); + TronError thrown = assertThrows(TronError.class, + SolidityNode::start); + assertEquals(TronError.ErrCode.SOLID_NODE_INIT, thrown.getErrCode()); + Args.getInstance().setTrustNodeAddr(trustNodeAddr); } @Test public void testSolidityGrpcCall() { rpcApiService.start(); DatabaseGrpcClient databaseGrpcClient = null; - String address = Args.getInstance().getTrustNodeAddr(); + String address = Args.getInstance().getTrustNodeAddr().split(":")[0] + ":" + rpcPort; try { databaseGrpcClient = new DatabaseGrpcClient(address); } catch (Exception e) { diff --git a/framework/src/test/resources/config-localtest.conf b/framework/src/test/resources/config-localtest.conf index ff31369a915..8049ceb6cda 100644 --- a/framework/src/test/resources/config-localtest.conf +++ b/framework/src/test/resources/config-localtest.conf @@ -90,7 +90,7 @@ node { minParticipationRate = 0 - fullNodeAllowShieldedTransaction = true + allowShieldedTransactionApi = true zenTokenId = 1000001 @@ -167,6 +167,7 @@ node { # httpPBFTPort = 8565 # maxBlockRange = 5000 # maxSubTopics = 1000 + # maxBlockFilterNum = 30000 } } diff --git a/framework/src/test/resources/config-test-index.conf b/framework/src/test/resources/config-test-index.conf index faa2f93dc5e..3ea6b50b20c 100644 --- a/framework/src/test/resources/config-test-index.conf +++ b/framework/src/test/resources/config-test-index.conf @@ -88,6 +88,7 @@ node { httpPBFTEnable = false # maxBlockRange = 5000 # maxSubTopics = 1000 + # maxBlockFilterNum = 30000 } rpc { diff --git a/framework/src/test/resources/config-test-mainnet.conf b/framework/src/test/resources/config-test-mainnet.conf index 12acad64d8d..123c8e5d368 100644 --- a/framework/src/test/resources/config-test-mainnet.conf +++ b/framework/src/test/resources/config-test-mainnet.conf @@ -94,6 +94,7 @@ node { httpPBFTEnable = false # maxBlockRange = 5000 # maxSubTopics = 1000 + # maxBlockFilterNum = 50000 } rpc { diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index eaa6659a8c4..eb4f605ab91 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -118,6 +118,7 @@ node { httpPBFTEnable = false # maxBlockRange = 5000 # maxSubTopics = 1000 + # maxBlockFilterNum = 30000 } # use your ipv6 address for node discovery and tcp connection, default false @@ -209,6 +210,8 @@ node { # The switch of the reflection service, effective for all gRPC services reflectionService = true + maxRstStream = 5 + secondsPerWindow = 10 } } @@ -327,7 +330,7 @@ genesis.block = { voteCount = 96 }, { - address: 27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh + address: 27bi7CD8d94AgXY3XFS9A9vx78Si5MqrECz url = "http://AlphaLyrae.org", voteCount = 95 } diff --git a/framework/src/test/resources/logback-test.xml b/framework/src/test/resources/logback-test.xml index d05c7fca79e..9cf4a04062f 100644 --- a/framework/src/test/resources/logback-test.xml +++ b/framework/src/test/resources/logback-test.xml @@ -34,7 +34,7 @@ - - + + diff --git a/gradle/jdk17/java-tron.vmoptions b/gradle/jdk17/java-tron.vmoptions new file mode 100644 index 00000000000..7af3123d268 --- /dev/null +++ b/gradle/jdk17/java-tron.vmoptions @@ -0,0 +1,8 @@ +-XX:+UseZGC +-Xlog:gc,gc+heap:file=gc.log:time,tags,level:filecount=10,filesize=100M +-XX:ReservedCodeCacheSize=256m +-XX:+UseCodeCacheFlushing +-XX:MetaspaceSize=256m +-XX:MaxMetaspaceSize=512m +-XX:MaxDirectMemorySize=1g +-XX:+HeapDumpOnOutOfMemoryError \ No newline at end of file diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index b3c879f6b40..4d0bf1013d6 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -6,6 +6,7 @@ + @@ -150,28 +151,12 @@ - - - + + + - - - - - - - - - - - - - - - - - - + + @@ -179,9 +164,9 @@ - - - + + + @@ -189,9 +174,9 @@ - - - + + + @@ -199,9 +184,9 @@ - - - + + + @@ -209,9 +194,9 @@ - - - + + + @@ -219,15 +204,15 @@ - - - + + + - - + + - - + + @@ -235,15 +220,15 @@ - - - + + + - - + + - - + + @@ -251,15 +236,15 @@ - - - + + + - - + + - - + + @@ -269,6 +254,9 @@ + + + @@ -304,12 +292,12 @@ - - - + + + - - + + @@ -336,12 +324,12 @@ - - - + + + - - + + @@ -352,9 +340,9 @@ - - - + + + @@ -375,12 +363,12 @@ - - - + + + - - + + @@ -393,9 +381,9 @@ - - - + + + @@ -406,12 +394,12 @@ - - - + + + - - + + @@ -445,6 +433,28 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -475,6 +485,16 @@ + + + + + + + + + + @@ -483,19 +503,6 @@ - - - - - - - - - - - - - @@ -505,16 +512,21 @@ - - - - - - + + + + + + + + + + + @@ -525,36 +537,42 @@ - - - + + + - - + + - - - + + + - - + + - - - + + + - - - + + + + + + - - + + - - + + + + + @@ -728,6 +746,14 @@ + + + + + + + + @@ -820,12 +846,15 @@ - - - + + + + + + - - + + @@ -836,82 +865,88 @@ - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + + + + + + + @@ -919,105 +954,116 @@ - - - + + + + + + + + + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + + + + - - + + @@ -1086,25 +1132,12 @@ - - - + + + - - - - - - - - - - - - - - - + + @@ -1139,6 +1172,14 @@ + + + + + + + + @@ -1298,9 +1339,14 @@ - - - + + + + + + + + @@ -1337,6 +1383,14 @@ + + + + + + + + @@ -1393,16 +1447,21 @@ - - - - - + + + + + + + + + + @@ -1540,28 +1599,28 @@ - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + @@ -1572,12 +1631,12 @@ - - - + + + - - + + @@ -1593,12 +1652,15 @@ - - - + + + + + + - - + + @@ -1614,12 +1676,12 @@ - - - + + + - - + + @@ -1627,9 +1689,9 @@ - - - + + + @@ -1637,9 +1699,9 @@ - - - + + + @@ -1666,65 +1728,65 @@ - - - + + + - - + + - - - + + + - - + + - - - + + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + @@ -1807,14 +1869,6 @@ - - - - - - - - @@ -1823,27 +1877,19 @@ - - - - - - - - - - - + + + - - - + + + @@ -1943,6 +1989,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2080,12 +2158,12 @@ - - - + + + - - + + @@ -2112,6 +2190,14 @@ + + + + + + + + @@ -2207,81 +2293,86 @@ - - - - - - + + + - - - - + + - - + + - - - - - - + + + - - - - + + - - + + - - - + + + - - + + + + + - - - + + + + + + - - + + - - - + + + + + + - - + + - - - + + + - - + + + + + - - - + + + + + + - - + + - - - + + + diff --git a/install_dependencies.sh b/install_dependencies.sh new file mode 100755 index 00000000000..f72ecf2e192 --- /dev/null +++ b/install_dependencies.sh @@ -0,0 +1,957 @@ +#!/usr/bin/env bash + +set -e + +OS="$(uname -s)" +ARCH="$(uname -m)" + +echo "========================================" +echo " TRON Java Dependencies Installer" +echo "========================================" +echo "" +echo ">>> Environment Detection" +if [[ "$OS" == "Darwin" ]]; then + echo " OS: macOS $OS" +else + echo " OS: $OS" +fi +echo " Architecture: $ARCH" + +# Validate OS and architecture support first +if [[ "$OS" != "Darwin" && "$OS" != "Linux" ]]; then + echo "Error: Unsupported OS $OS" + exit 1 +elif [[ "$OS" == "Darwin" ]]; then + if [[ "$ARCH" != "x86_64" && "$ARCH" != "arm64" ]]; then + echo "Error: Unsupported architecture for macOS: $ARCH" + exit 1 + fi +else + if [[ "$ARCH" != "x86_64" && "$ARCH" != "aarch64" && "$ARCH" != "arm64" ]]; then + echo "Error: Unsupported architecture for Linux: $ARCH" + exit 1 + fi +fi + +echo "" +echo ">>> Tested platforms:" +echo " - macOS x86_64 (JDK 8)" +echo " - macOS arm64 (JDK 17)" +echo " - Linux x86_64 (generic, including Ubuntu) (JDK 8)" +echo " - Linux arm64/aarch64 (generic, including Ubuntu) (JDK 17)" +echo " Note: Other platforms may require manual installation if errors occur" +echo "" +echo ">>> This script will install the following components if not already installed:" +if [[ "$OS" == "Darwin" ]]; then + echo " 1. Homebrew to download and install JDK (macOS only)" + echo " 2. Git for cloning Github repository" +else + echo " 1. Git for cloning Github repository" +fi +if [[ "$OS" == "Darwin" ]]; then + if [[ "$ARCH" == "x86_64" ]]; then + echo " 3. OpenJDK 8 (required for x86_64 architecture)" + else + echo " 3. OpenJDK 17 (required for arm64 architecture)" + fi +else + if [[ "$ARCH" == "x86_64" ]]; then + echo " 2. OpenJDK 8 (required for x86_64 architecture)" + else + echo " 2. OpenJDK 17 (required for arm64/aarch64 architecture)" + fi +fi +echo "" + +# Function to ask for user confirmation +ask_confirmation() { + while true; do + read -p "Do you want to continue? (y/N): " yn + case $yn in + [Yy]* ) return 0;; + [Nn]* | "" ) echo "Installation cancelled."; exit 0;; + * ) echo "Please answer yes (y) or no (n).";; + esac + done +} + +# Function to check Java version +check_java_version() { + if command -v java &> /dev/null; then + local java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + echo " Current Java version: $java_version" + + # Check if it's JDK 8 (version starts with 1.8) + if [[ "$java_version" =~ ^1\.8\. ]]; then + echo " JDK 8 is installed." + return 0 + # Check if it's JDK 17 (version starts with 17) + elif [[ "$java_version" =~ ^17\. ]]; then + echo " JDK 17 is installed." + return 1 + else + echo " Different Java version detected: $java_version" + return 2 + fi + else + return 3 + fi +} + +# Function to ask for JDK installation confirmation +ask_jdk_confirmation() { + local current_version="$1" + local required_version="$2" + local arch="$3" + + echo "" + echo "JDK Version Mismatch Detected!" + echo " Current version: $current_version" + echo " Current installation path: $(which java 2>/dev/null || echo 'Not found')" + if command -v java &> /dev/null && [[ -n "$JAVA_HOME" ]]; then + echo " Current JAVA_HOME: $JAVA_HOME" + fi + echo " Required version for $arch: $required_version" + echo " This script will install $required_version alongside your existing installation." + echo " Your current Java installation will not be removed." + echo "" + + while true; do + read -p "Do you want to install $required_version? (y/N): " yn + case $yn in + [Yy]* ) return 0;; + [Nn]* | "" ) echo "JDK installation cancelled. Exiting."; exit 0;; + * ) echo "Please answer yes (y) or no (n).";; + esac + done +} + +# First, check and install Git (needed for cloning repository) +echo ">>> Checking Git installation..." +if ! command -v git &> /dev/null; then + echo " Git is not installed." + while true; do + read -p "Do you want to install Git (required for cloning the java-tron repository)? (y/N): " yn + case $yn in + [Yy]* ) + echo ">>> Installing Git..." + INSTALL_GIT=true + break;; + [Nn]* | "" ) + echo "Git installation cancelled. You'll need Git to clone the java-tron repository." + echo "You can install Git manually later and then clone the repository." + INSTALL_GIT=false + break;; + * ) echo "Please answer yes (y) or no (n).";; + esac + done +else + echo "Git is already installed: $(git --version)" + INSTALL_GIT=false +fi + +echo "" +echo ">>> Checking existing Java installation..." +set +e # Temporarily disable exit on error +check_java_version +java_status=$? +set -e # Re-enable exit on error + +# Determine required JDK version based on architecture +if [[ "$OS" == "Darwin" ]]; then + if [[ "$ARCH" == "x86_64" ]]; then + required_jdk="JDK 8" + required_status=0 + elif [[ "$ARCH" == "arm64" ]]; then + required_jdk="JDK 17" + required_status=1 + fi +elif [[ "$OS" == "Linux" ]]; then + if [[ "$ARCH" == "x86_64" ]]; then + required_jdk="JDK 8" + required_status=0 + elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then + required_jdk="JDK 17" + required_status=1 + fi +fi + +# Check if correct JDK version is already installed +if [[ $java_status -eq $required_status ]]; then + echo " You can skip the Java installation part." + echo "" + if [[ "$INSTALL_GIT" == "false" ]]; then + echo "Both Git and Java JDK are ready for TRON development!" + echo "" + exit 0 + else + echo ">>> Proceeding with Git installation only..." + SKIP_JAVA_INSTALL=true + fi +elif [[ $java_status -eq 0 ]] || [[ $java_status -eq 1 ]] || [[ $java_status -eq 2 ]]; then + # Different JDK version is installed, ask for confirmation + current_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + ask_jdk_confirmation "$current_version" "$required_jdk" "$ARCH" + SKIP_JAVA_INSTALL=false +else + # No Java installation found, ask for general confirmation + echo "" + echo "No Java installation detected!" + echo " This script will install $required_jdk which is required for $ARCH architecture." + echo "" + ask_confirmation + SKIP_JAVA_INSTALL=false +fi + +# Function to show permanent Java configuration instructions +show_permanent_java_config() { + local jdk_version="$1" + local os_type="$2" + local java_home="$3" + local java_bin_path="$4" + + echo "" + echo " To make JDK $jdk_version permanent:" + if [[ "$os_type" == "Darwin" ]]; then + echo " # Add to ~/.zshrc or ~/.bash_profile:" + echo " echo 'export JAVA_HOME=\"$java_home\"' >> ~/.zshrc" + echo " echo 'export PATH=\"\$JAVA_HOME/bin:\$PATH\"' >> ~/.zshrc" + echo " # Then run below command:" + echo " source ~/.zshrc" + echo "" + echo " # Or use jenv for Java version management:" + echo " brew install jenv" + echo " jenv add $java_home" + elif [[ "$os_type" == "Linux" ]]; then + echo " # Method 1: Add to ~/.bashrc:" + echo " echo 'export JAVA_HOME=\"$java_home\"' >> ~/.bashrc" + echo " echo 'export PATH=\"\$JAVA_HOME/bin:\$PATH\"' >> ~/.bashrc" + echo " source ~/.bashrc" + echo "" + echo " # Method 2: Use update-alternatives (recommended):" + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + echo " sudo update-alternatives --install /usr/bin/java java $java_bin_path/java 1" + echo " sudo update-alternatives --install /usr/bin/javac javac $java_bin_path/javac 1" + echo " sudo update-alternatives --config java" + else + echo " sudo alternatives --install /usr/bin/java java $java_bin_path/java 1" + echo " sudo alternatives --install /usr/bin/javac javac $java_bin_path/javac 1" + echo " sudo alternatives --config java" + fi + fi + echo "" +} + +# Function to show Java environment application instructions +show_java_env_instructions() { + local java_home="$1" + local java_bin_path="$2" + + echo "" + echo " ✓ Java environment has been applied to this script session." + echo " To apply Java environment to your current terminal session:" + echo " source ./tron_java_env.sh" + echo "" + echo " Or run these commands directly:" + echo " export JAVA_HOME=\"$java_home\"" + echo " export PATH=\"$java_bin_path:\$PATH\"" + echo "" + echo " Note: You may need to open a new terminal or run 'source ./tron_java_env.sh'" + echo " if 'java -version' doesn't work immediately after this script completes." +} + +# Function to get Java paths based on OS and architecture +get_java_paths() { + local jdk_version="$1" + local os_type="$2" + local arch="$3" + local java_home="" + + if [[ "$os_type" == "Darwin" ]]; then + # macOS paths - try to detect actual Homebrew installation + if [[ "$jdk_version" == "8" ]]; then + # Try multiple possible paths for JDK 8 + for path in "/usr/local/opt/openjdk@8" "/opt/homebrew/opt/openjdk@8"; do + if [[ -d "$path" ]]; then + java_home="$path" + break + fi + done + # If not found, use brew --prefix to get the correct path + if [[ -z "$java_home" ]] && command -v brew &> /dev/null; then + local brew_prefix=$(brew --prefix openjdk@8 2>/dev/null || echo "") + if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then + java_home="$brew_prefix" + fi + fi + elif [[ "$jdk_version" == "17" ]]; then + # Try multiple possible paths for JDK 17 + for path in "/opt/homebrew/opt/openjdk@17" "/usr/local/opt/openjdk@17"; do + if [[ -d "$path" ]]; then + java_home="$path" + break + fi + done + # If not found, use brew --prefix to get the correct path + if [[ -z "$java_home" ]] && command -v brew &> /dev/null; then + local brew_prefix=$(brew --prefix openjdk@17 2>/dev/null || echo "") + if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then + java_home="$brew_prefix" + fi + fi + fi + elif [[ "$os_type" == "Linux" ]]; then + # Linux paths - provide generic path for manual configuration + java_home="/usr/lib/jvm/java-$jdk_version-openjdk" + fi + + echo "$java_home" +} + +# Unified Java environment configuration function +configure_java_environment() { + local jdk_version="$1" + local os_type="$2" + local arch="$3" + local java_home="" + local java_bin_path="" + + echo "" + echo ">>> Configuring Java environment for JDK $jdk_version..." + + # Ask user for confirmation before changing environment + echo "" + echo "This will modify your Java environment settings:" + echo " - Set JAVA_HOME to the new JDK $jdk_version installation" + echo " - Update PATH to include the new Java binaries" + echo " - Create a script (tron_java_env.sh) for easy environment setup" + echo "" + + while true; do + read -p "Do you want to configure the Java environment for JDK $jdk_version? (y/N): " yn + case $yn in + [Yy]* ) + echo ">>> Proceeding with Java environment configuration..." + break;; + [Nn]* | "" ) + echo "Java environment configuration skipped." + echo "You may need to manually set JAVA_HOME and PATH for JDK $jdk_version" + echo "" + echo "Manual configuration commands:" + + # Get the expected Java path + local expected_java_home=$(get_java_paths "$jdk_version" "$os_type" "$arch") + local expected_java_bin_path="$expected_java_home/bin" + echo " export JAVA_HOME=\"$expected_java_home\"" + echo " export PATH=\"\$JAVA_HOME/bin:\$PATH\"" + + if [[ "$os_type" == "Linux" ]]; then + echo "" + echo "Note: Actual path may vary depending on your distribution." + echo "Common paths include:" + echo " /usr/lib/jvm/java-$jdk_version-openjdk-amd64 (Ubuntu/Debian)" + echo " /usr/lib/jvm/java-1.$jdk_version.0-openjdk (RHEL/CentOS)" + fi + + # Create tron_java_env.sh even when user skips configuration + echo "" + echo ">>> Creating tron_java_env.sh for manual use later..." + local env_script="./tron_java_env.sh" + cat > "$env_script" << EOF +#!/bin/bash +# TRON Java Environment Configuration +# Generated by install_dependencies.sh on $(date) + +export JAVA_HOME="$expected_java_home" +export PATH="$expected_java_bin_path:\$PATH" + +echo "Java environment configured:" +echo " JAVA_HOME: \$JAVA_HOME" +echo " Java version: \$(java -version 2>&1 | head -n 1)" +EOF + chmod +x "$env_script" + echo " ✓ Created $env_script" + + # Show the same application instructions as automatic configuration + show_java_env_instructions "$expected_java_home" "$expected_java_bin_path" + + # Show permanent configuration instructions + show_permanent_java_config "$jdk_version" "$os_type" "$expected_java_home" "$expected_java_bin_path" + + echo "" + return 1;; + * ) echo "Please answer yes (y) or no (n).";; + esac + done + + # Determine Java paths based on OS and architecture + if [[ "$os_type" == "Darwin" ]]; then + # Use the helper function for macOS + java_home=$(get_java_paths "$jdk_version" "$os_type" "$arch") + java_bin_path="$java_home/bin" + + # Debug output for macOS + echo " Detected Java path: $java_home" + if [[ ! -d "$java_home" ]]; then + echo " Warning: Java home directory not found at: $java_home" + echo " Attempting to find JDK installation..." + + # Try to find the installation using brew + if command -v brew &> /dev/null; then + local brew_list=$(brew list --formula | grep "openjdk@$jdk_version" || echo "") + if [[ -n "$brew_list" ]]; then + echo " Found Homebrew package: $brew_list" + local brew_prefix=$(brew --prefix openjdk@$jdk_version 2>/dev/null || echo "") + if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then + java_home="$brew_prefix" + java_bin_path="$java_home/bin" + echo " Updated Java path to: $java_home" + fi + else + echo " Error: openjdk@$jdk_version not found in Homebrew packages" + echo " Try running: brew list | grep openjdk" + return 1 + fi + fi + fi + elif [[ "$os_type" == "Linux" ]]; then + # Linux paths - try to find the actual installation + if [[ "$jdk_version" == "8" ]]; then + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + if [[ "$arch" == "aarch64" ]] || [[ "$arch" == "arm64" ]]; then + java_home="/usr/lib/jvm/java-8-openjdk-arm64" + else + java_home="/usr/lib/jvm/java-8-openjdk-amd64" + fi + else + # RHEL/CentOS/Amazon Linux - try multiple possible paths + for path in "/usr/lib/jvm/java-1.8.0-amazon-corretto" "/usr/lib/jvm/java-1.8.0-openjdk"; do + if [[ -d "$path" ]]; then + java_home="$path" + break + fi + done + fi + elif [[ "$jdk_version" == "17" ]]; then + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + if [[ "$arch" == "aarch64" ]] || [[ "$arch" == "arm64" ]]; then + java_home="/usr/lib/jvm/java-17-openjdk-arm64" + else + java_home="/usr/lib/jvm/java-17-openjdk-amd64" + fi + else + # RHEL/CentOS/Amazon Linux - try multiple possible paths + for path in "/usr/lib/jvm/java-17-amazon-corretto" "/usr/lib/jvm/java-17-openjdk"; do + if [[ -d "$path" ]]; then + java_home="$path" + break + fi + done + fi + fi + java_bin_path="$java_home/bin" + fi + + # Set environment variables for current session + if [[ -d "$java_home" ]]; then + export JAVA_HOME="$java_home" + export PATH="$java_bin_path:$PATH" + echo " JAVA_HOME set to: $JAVA_HOME" + echo " PATH updated to include: $java_bin_path" + echo " Environment temporarily configured for JDK $jdk_version" + + # Create a source script for the user's current shell + local env_script="./tron_java_env.sh" + cat > "$env_script" << EOF +#!/bin/bash +# TRON Java Environment Configuration +# Generated by install_dependencies.sh on $(date) + +export JAVA_HOME="$java_home" +export PATH="$java_bin_path:\$PATH" + +echo "Java environment configured:" +echo " JAVA_HOME: \$JAVA_HOME" +echo " Java version: \$(java -version 2>&1 | head -n 1)" +EOF + chmod +x "$env_script" + echo "" + echo " ✓ Created $env_script" + + echo "" + echo " Applying Java environment to current shell session..." + # Source the environment script to apply it immediately + source "$env_script" + + show_java_env_instructions "$java_home" "$java_bin_path" + + else + echo " Could not find Java installation at expected path: $java_home" + echo " You may need to set JAVA_HOME manually" + return 1 + fi + + # Provide OS-specific permanent configuration instructions + show_permanent_java_config "$jdk_version" "$os_type" "$java_home" "$java_bin_path" + + return 0 +} + +echo "----------------------------------------" + +install_macos() { + if ! command -v brew &> /dev/null; then + echo ">>> Homebrew not found." + echo " Homebrew is required to install Java on macOS." + echo "" + while true; do + read -p "Do you want to install Homebrew? (y/N): " yn + case $yn in + [Yy]* ) + echo ">>> Installing Homebrew..." + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + break;; + [Nn]* | "" ) + echo "Homebrew installation cancelled." + echo "Cannot proceed with Java installation without Homebrew on macOS." + echo "Please install Homebrew manually or use alternative Java installation methods." + exit 1;; + * ) echo "Please answer yes (y) or no (n).";; + esac + done + + # Add Homebrew to PATH for the current session (Apple Silicon vs Intel) + if [[ "$ARCH" == "arm64" ]]; then + eval "$(/opt/homebrew/bin/brew shellenv)" + else + eval "$(/usr/local/bin/brew shellenv)" + fi + else + echo ">>> Homebrew is already installed." + fi + + echo ">>> Updating Homebrew..." + brew update + + # Install Git if needed + if [[ "$INSTALL_GIT" == "true" ]]; then + echo ">>> Installing Git..." + brew install git + echo " Git installed successfully: $(git --version)" + fi + + # Skip Java installation if flag is set + if [[ "$SKIP_JAVA_INSTALL" == "true" ]]; then + echo ">>> Skipping Java installation (correct version already detected)." + return 0 + fi + + if [[ "$ARCH" == "x86_64" ]]; then + echo ">>> Architecture is x86_64. Checking for JDK 8..." + set +e # Temporarily disable exit on error + check_java_version + local java_status=$? + set -e # Re-enable exit on error + + if [[ $java_status -eq 0 ]]; then + echo ">>> JDK 8 is already installed. Skipping installation." + else + if [[ $java_status -eq 1 ]]; then + echo ">>> Installing JDK 8 alongside existing JDK 17..." + elif [[ $java_status -eq 2 ]]; then + echo ">>> Installing JDK 8 alongside existing Java installation..." + else + echo ">>> Installing JDK 8..." + fi + if brew install openjdk@8; then + echo ">>> JDK 8 installation completed successfully." + + # Use unified Java environment configuration + if configure_java_environment "8" "Darwin" "$ARCH"; then + echo "Environment has been updated! Java 8 is now configured." + else + echo "Java 8 installed but environment not configured. You may need to set JAVA_HOME manually." + fi + else + echo "Error: Failed to install JDK 8 via Homebrew." + echo "Please try installing manually with: brew install openjdk@8" + exit 1 + fi + fi + + elif [[ "$ARCH" == "arm64" ]]; then + echo ">>> Architecture is arm64. Checking for JDK 17..." + set +e # Temporarily disable exit on error + check_java_version + local java_status=$? + set -e # Re-enable exit on error + + if [[ $java_status -eq 1 ]]; then + echo ">>> JDK 17 is already installed. Skipping installation." + else + if [[ $java_status -eq 0 ]]; then + echo ">>> Installing JDK 17 alongside existing JDK 8..." + elif [[ $java_status -eq 2 ]]; then + echo ">>> Installing JDK 17 alongside existing Java installation..." + else + echo ">>> Installing JDK 17..." + fi + if brew install openjdk@17; then + echo ">>> JDK 17 installation completed successfully." + + # Use unified Java environment configuration + if configure_java_environment "17" "Darwin" "$ARCH"; then + echo "Environment has been updated! Java 17 is now configured." + else + echo "Java 17 installed but environment not configured. You may need to set JAVA_HOME manually." + fi + else + echo "Error: Failed to install JDK 17 via Homebrew." + echo "Please try installing manually with: brew install openjdk@17" + exit 1 + fi + fi + + else + echo "Error: Unsupported architecture for macOS script: $ARCH" + exit 1 + fi +} + +install_linux() { + if command -v dnf &> /dev/null; then + PKG_MANAGER="dnf" + INSTALL_CMD="sudo dnf install -y" + UPDATE_CMD="sudo dnf check-update" + elif command -v yum &> /dev/null; then + PKG_MANAGER="yum" + INSTALL_CMD="sudo yum install -y" + UPDATE_CMD="sudo yum check-update" + elif command -v apt-get &> /dev/null; then + PKG_MANAGER="apt-get" + INSTALL_CMD="sudo apt-get install -y" + UPDATE_CMD="sudo apt-get update" + else + echo "Error: Unsupported package manager. Only apt-get (Debian/Ubuntu) and yum/dnf (RHEL/CentOS/Amazon Linux) are currently supported." + exit 1 + fi + + echo ">>> Updating package index ($PKG_MANAGER)..." + $UPDATE_CMD || true + + # Install Git if needed + if [[ "$INSTALL_GIT" == "true" ]]; then + echo ">>> Installing Git..." + $INSTALL_CMD git + echo " Git installed successfully: $(git --version)" + fi + + # Skip Java installation if flag is set + if [[ "$SKIP_JAVA_INSTALL" == "true" ]]; then + echo ">>> Skipping Java installation (correct version already detected)." + return 0 + fi + + install_first_available() { + local target_version="$1" + shift + local installed_package="" + + for pkg in "$@"; do + echo " Attempting to install: $pkg" + if $INSTALL_CMD "$pkg"; then + installed_package="$pkg" + echo " Successfully installed: $pkg" + break + else + echo " Failed to install: $pkg" + fi + done + + if [[ -n "$installed_package" ]]; then + # Verify what version was actually installed + echo " Verifying installed Java version..." + if command -v java &> /dev/null; then + local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + echo " Installed Java version: $actual_version" + + # Check if the installed version matches what we expected + if [[ "$target_version" == "8" ]]; then + if [[ "$actual_version" =~ ^1\.8\. ]]; then + echo " ✓ JDK 8 installed successfully" + return 0 + else + echo " ✗ Expected JDK 8 but got: $actual_version" + echo " This may happen if JDK 8 is not available in your distribution" + return 2 + fi + elif [[ "$target_version" == "17" ]]; then + if [[ "$actual_version" =~ ^17\. ]]; then + echo " ✓ JDK 17 installed successfully" + return 0 + else + echo " ✗ Expected JDK 17 but got: $actual_version" + return 2 + fi + fi + else + echo " ✗ Java command not found after installation" + return 1 + fi + else + return 1 + fi + } + + if [[ "$ARCH" == "x86_64" ]]; then + echo ">>> Architecture is x86_64. Checking for JDK 8..." + set +e # Temporarily disable exit on error + check_java_version + local java_status=$? + set -e # Re-enable exit on error + + if [[ $java_status -eq 0 ]]; then + echo ">>> JDK 8 is already installed. Skipping installation." + else + if [[ $java_status -eq 1 ]]; then + echo ">>> Installing JDK 8 alongside existing JDK 17..." + elif [[ $java_status -eq 2 ]]; then + echo ">>> Installing JDK 8 alongside existing Java installation..." + else + echo ">>> Installing JDK 8..." + fi + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + if install_first_available "8" openjdk-8-jdk; then + install_result=0 + else + install_result=$? + fi + else + if install_first_available "8" java-1.8.0-amazon-corretto-devel java-1.8.0-openjdk-devel; then + install_result=0 + else + install_result=$? + fi + fi + + if [[ $install_result -eq 0 ]]; then + # Use unified Java environment configuration + if configure_java_environment "8" "Linux" "$ARCH"; then + echo "Environment has been updated! Java 8 is now configured." + else + echo "Java 8 installed but environment not configured. You may need to set JAVA_HOME manually." + fi + elif [[ $install_result -eq 2 ]]; then + # JDK 8 package is installed but default version is different + # Need to switch to JDK 8 using update-alternatives + local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + echo ">>> JDK 8 package is installed, but system default is: $actual_version" + echo ">>> Switching system default to JDK 8 using update-alternatives..." + + # Try to switch to JDK 8 + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + local jdk8_path="/usr/lib/jvm/java-8-openjdk-amd64" + if [[ -d "$jdk8_path" ]]; then + echo " Found JDK 8 at: $jdk8_path" + # Set JDK 8 as default using update-alternatives + sudo update-alternatives --set java "$jdk8_path/jre/bin/java" 2>/dev/null || \ + sudo update-alternatives --set java "$jdk8_path/bin/java" 2>/dev/null || \ + echo " Note: Could not auto-switch. Please run: sudo update-alternatives --config java" + + sudo update-alternatives --set javac "$jdk8_path/bin/javac" 2>/dev/null || \ + echo " Note: Could not auto-switch javac. Please run: sudo update-alternatives --config javac" + + # Verify the switch + local new_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + if [[ "$new_version" =~ ^1\.8\. ]]; then + echo " ✓ Successfully switched to JDK 8: $new_version" + # Now configure environment for JDK 8 + if configure_java_environment "8" "Linux" "$ARCH"; then + echo "Environment has been updated! Java 8 is now configured." + else + echo "Java 8 is active but environment not configured. You may need to set JAVA_HOME manually." + fi + else + echo " ✗ Auto-switch failed. Current version: $new_version" + echo " Please manually switch to JDK 8:" + echo " sudo update-alternatives --config java" + echo " sudo update-alternatives --config javac" + echo " Then configure environment for JDK 8" + fi + else + echo " ✗ JDK 8 directory not found at expected location: $jdk8_path" + echo " Please manually locate and configure JDK 8" + fi + else + # For yum/dnf systems + echo " Please manually switch to JDK 8:" + echo " sudo alternatives --config java" + echo " sudo alternatives --config javac" + echo "" + echo " After switching, you can configure the environment." + + # Still create tron_java_env.sh for manual use + if configure_java_environment "8" "Linux" "$ARCH"; then + echo "Environment configuration completed for JDK 8." + else + echo "tron_java_env.sh has been created for manual use." + echo "After switching to JDK 8, run: source ./tron_java_env.sh" + fi + fi + else + echo "Error: Unable to install any JDK on $PKG_MANAGER" + exit 1 + fi + fi + + elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then + echo ">>> Architecture is arm64/aarch64. Checking for JDK 17..." + set +e # Temporarily disable exit on error + check_java_version + local java_status=$? + set -e # Re-enable exit on error + + if [[ $java_status -eq 1 ]]; then + echo ">>> JDK 17 is already installed. Skipping installation." + else + if [[ $java_status -eq 0 ]]; then + echo ">>> Installing JDK 17 alongside existing JDK 8..." + elif [[ $java_status -eq 2 ]]; then + echo ">>> Installing JDK 17 alongside existing Java installation..." + else + echo ">>> Installing JDK 17..." + fi + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + if install_first_available "17" openjdk-17-jdk; then + install_result=0 + else + install_result=$? + fi + else + if install_first_available "17" java-17-amazon-corretto-devel java-17-openjdk-devel; then + install_result=0 + else + install_result=$? + fi + fi + + if [[ $install_result -eq 0 ]]; then + # Use unified Java environment configuration + if configure_java_environment "17" "Linux" "$ARCH"; then + echo "Environment has been updated! Java 17 is now configured." + else + echo "Java 17 installed but environment not configured. You may need to set JAVA_HOME manually." + fi + elif [[ $install_result -eq 2 ]]; then + # JDK 17 package is installed but default version is different + # Need to switch to JDK 17 using update-alternatives + local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + echo ">>> JDK 17 package is installed, but system default is: $actual_version" + echo ">>> Switching system default to JDK 17 using update-alternatives..." + + # Try to switch to JDK 17 + if [[ "$PKG_MANAGER" == "apt-get" ]]; then + local jdk17_path="" + if [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then + jdk17_path="/usr/lib/jvm/java-17-openjdk-arm64" + else + jdk17_path="/usr/lib/jvm/java-17-openjdk-amd64" + fi + + if [[ -d "$jdk17_path" ]]; then + echo " Found JDK 17 at: $jdk17_path" + # Set JDK 17 as default using update-alternatives + sudo update-alternatives --set java "$jdk17_path/bin/java" 2>/dev/null || \ + echo " Note: Could not auto-switch. Please run: sudo update-alternatives --config java" + + sudo update-alternatives --set javac "$jdk17_path/bin/javac" 2>/dev/null || \ + echo " Note: Could not auto-switch javac. Please run: sudo update-alternatives --config javac" + + # Verify the switch + local new_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2) + if [[ "$new_version" =~ ^17\. ]]; then + echo " ✓ Successfully switched to JDK 17: $new_version" + # Now configure environment for JDK 17 + if configure_java_environment "17" "Linux" "$ARCH"; then + echo "Environment has been updated! Java 17 is now configured." + else + echo "Java 17 is active but environment not configured. You may need to set JAVA_HOME manually." + fi + else + echo " ✗ Auto-switch failed. Current version: $new_version" + echo " Please manually switch to JDK 17:" + echo " sudo update-alternatives --config java" + echo " sudo update-alternatives --config javac" + echo " Then configure environment for JDK 17" + fi + else + echo " ✗ JDK 17 directory not found at expected location: $jdk17_path" + echo " Please manually locate and configure JDK 17" + fi + else + # For yum/dnf systems + echo " Please manually switch to JDK 17:" + echo " sudo alternatives --config java" + echo " sudo alternatives --config javac" + echo "" + echo " After switching, you can configure the environment." + + # Still create tron_java_env.sh for manual use + if configure_java_environment "17" "Linux" "$ARCH"; then + echo "Environment configuration completed for JDK 17." + else + echo "tron_java_env.sh has been created for manual use." + echo "After switching to JDK 17, run: source ./tron_java_env.sh" + fi + fi + else + echo "Error: Unable to install any JDK on $PKG_MANAGER" + exit 1 + fi + fi + + else + echo "Error: Unsupported architecture for Linux script: $ARCH" + exit 1 + fi +} + +if [[ "$OS" == "Darwin" ]]; then + install_macos +elif [[ "$OS" == "Linux" ]]; then + install_linux +else + echo "Error: Unsupported Operating System: $OS" + exit 1 +fi + +echo "----------------------------------------" +echo "Installation completed successfully!" +echo "" +echo ">>> Verification Commands:" +echo " git --version" +echo " java -version" +echo "" + +# Verify that Java is actually working +echo ">>> Verifying Java installation..." +if command -v java &> /dev/null; then + echo " Java command found: $(which java)" + if java -version &> /dev/null; then + echo " Java version: $(java -version 2>&1 | head -n 1)" + else + echo " ✗ Java command exists but cannot run properly" + echo " Please run: source ./tron_java_env.sh" + fi +else + echo " ✗ Java command not found in PATH" + echo " Please run: source ./tron_java_env.sh" + echo " If that doesn't work, check the Java environment configuration above." +fi + +echo "" +echo ">>> Troubleshooting:" +echo " - If 'java -version' shows incorrect version, run: source ./tron_java_env.sh" +echo " - For permanent configuration, follow the instructions shown above." +echo "" +echo "" \ No newline at end of file diff --git a/jitpack.yml b/jitpack.yml index f951e400136..4065d43092c 100644 --- a/jitpack.yml +++ b/jitpack.yml @@ -1,2 +1,2 @@ install: - - ./gradlew clean -xtest -xlint -xcheck -PbinaryRelease=false install \ No newline at end of file + - ./gradlew clean -xtest -xlint -xcheck -PbinaryRelease=false publishToMavenLocal diff --git a/platform/build.gradle b/platform/build.gradle new file mode 100644 index 00000000000..a94aad3cf17 --- /dev/null +++ b/platform/build.gradle @@ -0,0 +1,17 @@ +description = "platform – a distributed consensus arithmetic for blockchain." + +sourceSets { + main { + java.srcDirs = rootProject.archInfo.sourceSets.main.java.srcDirs + } + test { + java.srcDirs = rootProject.archInfo.sourceSets.test.java.srcDirs + } +} + +dependencies { + api group: 'org.fusesource.leveldbjni', name: 'leveldbjni-all', version: '1.8' + api group: 'org.rocksdb', name: 'rocksdbjni', version: "${rootProject.archInfo.requires.RocksdbVersion}" + api group: 'commons-io', name: 'commons-io', version: '2.18.0' + api 'io.github.tronprotocol:zksnark-java-sdk:1.0.0' exclude(group: 'commons-io', module: 'commons-io') +} diff --git a/platform/src/main/java/arm/org/tron/common/math/MathWrapper.java b/platform/src/main/java/arm/org/tron/common/math/MathWrapper.java new file mode 100644 index 00000000000..12395dffcea --- /dev/null +++ b/platform/src/main/java/arm/org/tron/common/math/MathWrapper.java @@ -0,0 +1,254 @@ +package org.tron.common.math; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * This class is deprecated and should not be used in new code, + * for cross-platform consistency, please use {@link StrictMathWrapper} instead, + * especially for floating-point calculations. + */ +@Deprecated +public class MathWrapper { + + private static final Map powData = Collections.synchronizedMap(new HashMap<>()); + private static final String EXPONENT = "3f40624dd2f1a9fc"; // 1/2000 = 0.0005 + + public static double pow(double a, double b) { + double strictResult = StrictMath.pow(a, b); + return powData.getOrDefault(new PowData(a, b), strictResult); + } + + /** + * This static block is used to initialize the data map. + */ + static { + // init main-net pow data start + addPowData("3ff0192278704be3", EXPONENT, "3ff000033518c576"); // 4137160(block) + addPowData("3ff000002fc6a33f", EXPONENT, "3ff0000000061d86"); // 4065476 + addPowData("3ff00314b1e73ecf", EXPONENT, "3ff0000064ea3ef8"); // 4071538 + addPowData("3ff0068cd52978ae", EXPONENT, "3ff00000d676966c"); // 4109544 + addPowData("3ff0032fda05447d", EXPONENT, "3ff0000068636fe0"); // 4123826 + addPowData("3ff00051c09cc796", EXPONENT, "3ff000000a76c20e"); // 4166806 + addPowData("3ff00bef8115b65d", EXPONENT, "3ff0000186893de0"); // 4225778 + addPowData("3ff009b0b2616930", EXPONENT, "3ff000013d27849e"); // 4251796 + addPowData("3ff00364ba163146", EXPONENT, "3ff000006f26a9dc"); // 4257157 + addPowData("3ff019be4095d6ae", EXPONENT, "3ff0000348e9f02a"); // 4260583 + addPowData("3ff0123e52985644", EXPONENT, "3ff0000254797fd0"); // 4367125 + addPowData("3ff0126d052860e2", EXPONENT, "3ff000025a6cde26"); // 4402197 + addPowData("3ff0001632cccf1b", EXPONENT, "3ff0000002d76406"); // 4405788 + addPowData("3ff0000965922b01", EXPONENT, "3ff000000133e966"); // 4490332 + addPowData("3ff00005c7692d61", EXPONENT, "3ff0000000bd5d34"); // 4499056 + addPowData("3ff015cba20ec276", EXPONENT, "3ff00002c84cef0e"); // 4518035 + addPowData("3ff00002f453d343", EXPONENT, "3ff000000060cf4e"); // 4533215 + addPowData("3ff006ea73f88946", EXPONENT, "3ff00000e26d4ea2"); // 4647814 + addPowData("3ff00a3632db72be", EXPONENT, "3ff000014e3382a6"); // 4766695 + addPowData("3ff000c0e8df0274", EXPONENT, "3ff0000018b0aeb2"); // 4771494 + addPowData("3ff00015c8f06afe", EXPONENT, "3ff0000002c9d73e"); // 4793587 + addPowData("3ff00068def18101", EXPONENT, "3ff000000d6c3cac"); // 4801947 + addPowData("3ff01349f3ac164b", EXPONENT, "3ff000027693328a"); // 4916843 + addPowData("3ff00e86a7859088", EXPONENT, "3ff00001db256a52"); // 4924111 + addPowData("3ff00000c2a51ab7", EXPONENT, "3ff000000018ea20"); // 5098864 + addPowData("3ff020fb74e9f170", EXPONENT, "3ff00004346fbfa2"); // 5133963 + addPowData("3ff00001ce277ce7", EXPONENT, "3ff00000003b27dc"); // 5139389 + addPowData("3ff005468a327822", EXPONENT, "3ff00000acc20750"); // 5151258 + addPowData("3ff00006666f30ff", EXPONENT, "3ff0000000d1b80e"); // 5185021 + addPowData("3ff000045a0b2035", EXPONENT, "3ff00000008e98e6"); // 5295829 + addPowData("3ff00e00380e10d7", EXPONENT, "3ff00001c9ff83c8"); // 5380897 + addPowData("3ff00c15de2b0d5e", EXPONENT, "3ff000018b6eaab6"); // 5400886 + addPowData("3ff00042afe6956a", EXPONENT, "3ff0000008892244"); // 5864127 + addPowData("3ff0005b7357c2d4", EXPONENT, "3ff000000bb48572"); // 6167339 + addPowData("3ff00033d5ab51c8", EXPONENT, "3ff0000006a279c8"); // 6240974 + addPowData("3ff0000046d74585", EXPONENT, "3ff0000000091150"); // 6279093 + addPowData("3ff0010403f34767", EXPONENT, "3ff0000021472146"); // 6428736 + addPowData("3ff00496fe59bc98", EXPONENT, "3ff000009650a4ca"); // 6432355,6493373 + addPowData("3ff0012e43815868", EXPONENT, "3ff0000026af266e"); // 6555029 + addPowData("3ff00021f6080e3c", EXPONENT, "3ff000000458d16a"); // 7092933 + addPowData("3ff000489c0f28bd", EXPONENT, "3ff00000094b3072"); // 7112412 + addPowData("3ff00009d3df2e9c", EXPONENT, "3ff00000014207b4"); // 7675535 + addPowData("3ff000def05fa9c8", EXPONENT, "3ff000001c887cdc"); // 7860324 + addPowData("3ff0013bca543227", EXPONENT, "3ff00000286a42d2"); // 8292427 + addPowData("3ff0021a2f14a0ee", EXPONENT, "3ff0000044deb040"); // 8517311 + addPowData("3ff0002cc166be3c", EXPONENT, "3ff0000005ba841e"); // 8763101 + addPowData("3ff0000cc84e613f", EXPONENT, "3ff0000001a2da46"); // 9269124 + addPowData("3ff000057b83c83f", EXPONENT, "3ff0000000b3a640"); // 9631452 + // init main-net pow data end + // add pow data + } + + private static void addPowData(String a, String b, String ret) { + powData.put(new PowData(hexToDouble(a), hexToDouble(b)), hexToDouble(ret)); + } + + private static double hexToDouble(String input) { + // Convert the hex string to a long + long hexAsLong = Long.parseLong(input, 16); + // and then convert the long to a double + return Double.longBitsToDouble(hexAsLong); + } + + private static class PowData { + final double a; + final double b; + + public PowData(double a, double b) { + this.a = a; + this.b = b; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PowData powData = (PowData) o; + return Double.compare(powData.a, a) == 0 && Double.compare(powData.b, b) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(a, b); + } + } + + /** + * *** methods are same as {@link java.lang.Math} methods, guaranteed by the call start *** + */ + + /** + * finally calls {@link java.lang.Math#addExact(long, long)} + */ + + public static long addExact(long x, long y) { + return StrictMath.addExact(x, y); + } + + /** + * finally calls {@link java.lang.Math#addExact(int, int)} + */ + + public static int addExact(int x, int y) { + return StrictMath.addExact(x, y); + } + + /** + * finally calls {@link java.lang.Math#subtractExact(long, long)} + */ + + public static long subtractExact(long x, long y) { + return StrictMath.subtractExact(x, y); + } + + /** + * finally calls {@link java.lang.Math#floorMod(long, long)} + */ + public static long multiplyExact(long x, long y) { + return StrictMath.multiplyExact(x, y); + } + + public static long multiplyExact(long x, int y) { + return multiplyExact(x, (long) y); + } + + public static int multiplyExact(int x, int y) { + return StrictMath.multiplyExact(x, y); + } + + /** + * finally calls {@link java.lang.Math#floorDiv(long, long)} + */ + public static long floorDiv(long x, long y) { + return StrictMath.floorDiv(x, y); + } + + public static long floorDiv(long x, int y) { + return floorDiv(x, (long) y); + } + + /** + * finally calls {@link java.lang.Math#min(int, int)} + */ + public static int min(int a, int b) { + return StrictMath.min(a, b); + } + + /** + * finally calls {@link java.lang.Math#min(long, long)} + */ + public static long min(long a, long b) { + return StrictMath.min(a, b); + } + + /** + * finally calls {@link java.lang.Math#max(int, int)} + */ + public static int max(int a, int b) { + return StrictMath.max(a, b); + } + + /** + * finally calls {@link java.lang.Math#max(long, long)} + */ + public static long max(long a, long b) { + return StrictMath.max(a, b); + } + + /** + * finally calls {@link java.lang.Math#round(float)} + */ + public static int round(float a) { + return StrictMath.round(a); + } + + /** + * finally calls {@link java.lang.Math#round(double)} + */ + public static long round(double a) { + return StrictMath.round(a); + } + + /** + * finally calls {@link java.lang.Math#signum(double)} + */ + public static double signum(double d) { + return StrictMath.signum(d); + } + + /** + * finally calls {@link java.lang.Math#signum(float)} + */ + public static long abs(long a) { + return StrictMath.abs(a); + } + + /** + * *** methods are same as {@link java.lang.Math} methods, guaranteed by the call end *** + */ + + /** + * *** methods are same as {@link java.lang.Math} methods by mathematical algorithms*** + * / + + + /** + * mathematical integer: ceil(i) = floor(i) = i + * @return the smallest (closest to negative infinity) double value that is greater + * than or equal to the argument and is equal to a mathematical integer. + */ + public static double ceil(double a) { + return StrictMath.ceil(a); + } + + /** + * *** methods are no matters *** + */ + public static double random() { + return StrictMath.random(); + } + +} diff --git a/platform/src/main/java/arm/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java b/platform/src/main/java/arm/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java new file mode 100644 index 00000000000..26c246faf0e --- /dev/null +++ b/platform/src/main/java/arm/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java @@ -0,0 +1,32 @@ +package org.tron.common.utils; + +import java.nio.ByteBuffer; +import org.rocksdb.AbstractComparator; +import org.rocksdb.ComparatorOptions; + +public class MarketOrderPriceComparatorForRocksDB extends AbstractComparator { + + public MarketOrderPriceComparatorForRocksDB(final ComparatorOptions copt) { + super(copt); + } + + @Override + public String name() { + return "MarketOrderPriceComparator"; + } + + @Override + public int compare(final ByteBuffer a, final ByteBuffer b) { + return MarketComparator.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b)); + } + + /** + * DirectSlice.data().array will throw UnsupportedOperationException. + * */ + public byte[] convertDataToBytes(ByteBuffer buf) { + byte[] bytes = new byte[buf.remaining()]; + buf.get(bytes); + return bytes; + } + +} diff --git a/platform/src/main/java/common/org/tron/common/arch/Arch.java b/platform/src/main/java/common/org/tron/common/arch/Arch.java new file mode 100644 index 00000000000..f115d1f07c2 --- /dev/null +++ b/platform/src/main/java/common/org/tron/common/arch/Arch.java @@ -0,0 +1,91 @@ +package org.tron.common.arch; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j(topic = "arch") +public final class Arch { + + private Arch() { + } + + public static String withAll() { + final StringBuilder info = new StringBuilder(); + info.append("os.name").append(": ").append(getOsName()).append("\n"); + info.append("os.arch").append(": ").append(getOsArch()).append("\n"); + info.append("bit.model").append(": ").append(getBitModel()).append("\n"); + info.append("java.version").append(": ").append(javaVersion()).append("\n"); + info.append("java.specification.version").append(": ").append(javaSpecificationVersion()) + .append("\n"); + info.append("java.vendor").append(": ").append(javaVendor()).append("\n"); + return info.toString(); + } + + public static String getOsName() { + return System.getProperty("os.name").toLowerCase().trim(); + + } + public static String getOsArch() { + return System.getProperty("os.arch").toLowerCase().trim(); + } + + public static int getBitModel() { + String prop = System.getProperty("sun.arch.data.model"); + if (prop == null) { + prop = System.getProperty("com.ibm.vm.bitmode"); + } + if (prop != null) { + return Integer.parseInt(prop); + } + // GraalVM support, see https://github.com/fusesource/jansi/issues/162 + String arch = System.getProperty("os.arch"); + if (arch.endsWith("64") && "Substrate VM".equals(System.getProperty("java.vm.name"))) { + return 64; + } + return -1; // we don't know... + } + + public static String javaVersion() { + return System.getProperty("java.version").toLowerCase().trim(); + } + + public static String javaSpecificationVersion() { + return System.getProperty("java.specification.version").toLowerCase().trim(); + } + + public static String javaVendor() { + return System.getProperty("java.vendor").toLowerCase().trim(); + } + + public static boolean isArm64() { + String osArch = getOsArch(); + return osArch.contains("arm64") || osArch.contains("aarch64"); + } + + public static boolean isX86() { + return !isArm64(); + } + + public static boolean isJava8() { + return javaSpecificationVersion().equals("1.8"); + } + + public static boolean isJava17() { + return javaSpecificationVersion().equals("17"); + } + + public static void throwIfUnsupportedJavaVersion() { + if ((isX86() && !isJava8()) || (isArm64() && !isJava17())) { + logger.info(withAll()); + throw new UnsupportedOperationException(String.format( + "Java %s is required for %s architecture. Detected version %s", isX86() ? "1.8" : "17", + getOsArch(), javaSpecificationVersion())); + } + } + + public static void throwIfUnsupportedArm64Exception(String message) { + if (isArm64()) { + throw new UnsupportedOperationException( + message + ": unsupported on " + getOsArch() + " architecture"); + } + } +} diff --git a/plugins/src/main/java/org/tron/plugins/utils/MarketUtils.java b/platform/src/main/java/common/org/tron/common/utils/MarketComparator.java similarity index 50% rename from plugins/src/main/java/org/tron/plugins/utils/MarketUtils.java rename to platform/src/main/java/common/org/tron/common/utils/MarketComparator.java index dbd578a59a3..c2742d4d10b 100644 --- a/plugins/src/main/java/org/tron/plugins/utils/MarketUtils.java +++ b/platform/src/main/java/common/org/tron/common/utils/MarketComparator.java @@ -1,71 +1,10 @@ -package org.tron.plugins.utils; +package org.tron.common.utils; import java.math.BigInteger; -import org.tron.plugins.utils.ByteArray; -public class MarketUtils { +public class MarketComparator { - public static final int TOKEN_ID_LENGTH = ByteArray - .fromString(Long.toString(Long.MAX_VALUE)).length; // 19 - - - - /** - * In order to avoid the difference between the data of same key stored and fetched by hashMap and - * levelDB, when creating the price key, we will find the GCD (Greatest Common Divisor) of - * sellTokenQuantity and buyTokenQuantity. - */ - public static byte[] createPairPriceKey(byte[] sellTokenId, byte[] buyTokenId, - long sellTokenQuantity, long buyTokenQuantity) { - - byte[] sellTokenQuantityBytes; - byte[] buyTokenQuantityBytes; - - // cal the GCD - long gcd = findGCD(sellTokenQuantity, buyTokenQuantity); - if (gcd == 0) { - sellTokenQuantityBytes = ByteArray.fromLong(sellTokenQuantity); - buyTokenQuantityBytes = ByteArray.fromLong(buyTokenQuantity); - } else { - sellTokenQuantityBytes = ByteArray.fromLong(sellTokenQuantity / gcd); - buyTokenQuantityBytes = ByteArray.fromLong(buyTokenQuantity / gcd); - } - - return doCreatePairPriceKey(sellTokenId, buyTokenId, - sellTokenQuantityBytes, buyTokenQuantityBytes); - } - - public static long findGCD(long number1, long number2) { - if (number1 == 0 || number2 == 0) { - return 0; - } - return calGCD(number1, number2); - } - - private static long calGCD(long number1, long number2) { - if (number2 == 0) { - return number1; - } - return calGCD(number2, number1 % number2); - } - - - private static byte[] doCreatePairPriceKey(byte[] sellTokenId, byte[] buyTokenId, - byte[] sellTokenQuantity, byte[] buyTokenQuantity) { - byte[] result = new byte[TOKEN_ID_LENGTH + TOKEN_ID_LENGTH - + sellTokenQuantity.length + buyTokenQuantity.length]; - - System.arraycopy(sellTokenId, 0, result, 0, sellTokenId.length); - System.arraycopy(buyTokenId, 0, result, TOKEN_ID_LENGTH, buyTokenId.length); - System.arraycopy(sellTokenQuantity, 0, result, - TOKEN_ID_LENGTH + TOKEN_ID_LENGTH, - sellTokenQuantity.length); - System.arraycopy(buyTokenQuantity, 0, result, - TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + buyTokenQuantity.length, - buyTokenQuantity.length); - - return result; - } + public static final int TOKEN_ID_LENGTH = Long.toString(Long.MAX_VALUE).getBytes().length; // 19 public static int comparePriceKey(byte[] o1, byte[] o2) { @@ -76,7 +15,7 @@ public static int comparePriceKey(byte[] o1, byte[] o2) { System.arraycopy(o1, 0, pair1, 0, TOKEN_ID_LENGTH * 2); System.arraycopy(o2, 0, pair2, 0, TOKEN_ID_LENGTH * 2); - int pairResult = ByteArray.compareUnsigned(pair1, pair2); + int pairResult = compareUnsigned(pair1, pair2); if (pairResult != 0) { return pairResult; } @@ -100,10 +39,10 @@ public static int comparePriceKey(byte[] o1, byte[] o2) { System.arraycopy(o2, TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + longByteNum, getBuyTokenQuantity2, 0, longByteNum); - long sellTokenQuantity1 = ByteArray.toLong(getSellTokenQuantity1); - long buyTokenQuantity1 = ByteArray.toLong(getBuyTokenQuantity1); - long sellTokenQuantity2 = ByteArray.toLong(getSellTokenQuantity2); - long buyTokenQuantity2 = ByteArray.toLong(getBuyTokenQuantity2); + long sellTokenQuantity1 = toLong(getSellTokenQuantity1); + long buyTokenQuantity1 = toLong(getBuyTokenQuantity1); + long sellTokenQuantity2 = toLong(getSellTokenQuantity2); + long buyTokenQuantity2 = toLong(getBuyTokenQuantity2); if ((sellTokenQuantity1 == 0 || buyTokenQuantity1 == 0) && (sellTokenQuantity2 == 0 || buyTokenQuantity2 == 0)) { @@ -145,4 +84,41 @@ public static int comparePrice(long price1SellQuantity, long price1BuyQuantity, return price1BuyQuantityBI.multiply(price2SellQuantityBI) .compareTo(price2BuyQuantityBI.multiply(price1SellQuantityBI)); } + + /** + * copy from org.bouncycastle.util.Arrays.compareUnsigned + */ + private static int compareUnsigned(byte[] a, byte[] b) { + if (a == b) { + return 0; + } + if (a == null) { + return -1; + } + if (b == null) { + return 1; + } + int minLen = StrictMath.min(a.length, b.length); + for (int i = 0; i < minLen; ++i) { + int aVal = a[i] & 0xFF; + int bVal = b[i] & 0xFF; + if (aVal < bVal) { + return -1; + } + if (aVal > bVal) { + return 1; + } + } + if (a.length < b.length) { + return -1; + } + if (a.length > b.length) { + return 1; + } + return 0; + } + + public static long toLong(byte[] b) { + return (b == null || b.length == 0) ? 0 : new BigInteger(1, b).longValue(); + } } diff --git a/chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java b/platform/src/main/java/common/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java similarity index 87% rename from chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java rename to platform/src/main/java/common/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java index c4c519f68f1..efb7219b211 100644 --- a/chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java +++ b/platform/src/main/java/common/org/tron/common/utils/MarketOrderPriceComparatorForLevelDB.java @@ -1,7 +1,5 @@ package org.tron.common.utils; -import org.tron.core.capsule.utils.MarketUtils; - public class MarketOrderPriceComparatorForLevelDB implements org.iq80.leveldb.DBComparator { @Override @@ -26,7 +24,7 @@ public byte[] findShortSuccessor(byte[] key) { */ @Override public int compare(byte[] o1, byte[] o2) { - return MarketUtils.comparePriceKey(o1, o2); + return MarketComparator.comparePriceKey(o1, o2); } } diff --git a/common/src/main/java/org/tron/common/math/MathWrapper.java b/platform/src/main/java/x86/org/tron/common/math/MathWrapper.java similarity index 100% rename from common/src/main/java/org/tron/common/math/MathWrapper.java rename to platform/src/main/java/x86/org/tron/common/math/MathWrapper.java diff --git a/chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForRockDB.java b/platform/src/main/java/x86/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java similarity index 70% rename from chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForRockDB.java rename to platform/src/main/java/x86/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java index d3812f0f6bc..be406ff658d 100644 --- a/chainbase/src/main/java/org/tron/common/utils/MarketOrderPriceComparatorForRockDB.java +++ b/platform/src/main/java/x86/org/tron/common/utils/MarketOrderPriceComparatorForRocksDB.java @@ -3,11 +3,10 @@ import org.rocksdb.ComparatorOptions; import org.rocksdb.DirectSlice; import org.rocksdb.util.DirectBytewiseComparator; -import org.tron.core.capsule.utils.MarketUtils; -public class MarketOrderPriceComparatorForRockDB extends DirectBytewiseComparator { +public class MarketOrderPriceComparatorForRocksDB extends DirectBytewiseComparator { - public MarketOrderPriceComparatorForRockDB(final ComparatorOptions copt) { + public MarketOrderPriceComparatorForRocksDB(final ComparatorOptions copt) { super(copt); } @@ -18,7 +17,7 @@ public String name() { @Override public int compare(final DirectSlice a, final DirectSlice b) { - return MarketUtils.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b)); + return MarketComparator.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b)); } /** diff --git a/plugins/README.md b/plugins/README.md index 0db6f2e6143..db25811882f 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -2,7 +2,7 @@ This package contains a set of tools for TRON, the followings are the documentation for each tool. -## DB Archive +## DB Archive(Requires x86 + LevelDB) DB archive provides the ability to reformat the manifest according to the current `database`, parameters are compatible with the previous `ArchiveManifest`. @@ -26,7 +26,7 @@ DB archive provides the ability to reformat the manifest according to the curren ``` -## DB Convert +## DB Convert(Requires x86 + LevelDB) DB convert provides a helper which can convert LevelDB data to RocksDB data, parameters are compatible with previous `DBConvert`. @@ -34,15 +34,13 @@ DB convert provides a helper which can convert LevelDB data to RocksDB data, par - ``: Input path for leveldb, default: output-directory/database. - ``: Output path for rocksdb, default: output-directory-dst/database. -- `--safe`: In safe mode, read data from leveldb then put into rocksdb, it's a very time-consuming procedure. If not, just change engine.properties from leveldb to rocksdb, rocksdb - is compatible with leveldb for the current version. This may not be the case in the future, default: false. - `-h | --help`: Provide the help info. ### Examples: ```shell script # full command - java -jar Toolkit.jar db convert [-h] [--safe] + java -jar Toolkit.jar db convert [-h] # examples java -jar Toolkit.jar db convert output-directory/database /tmp/database ``` @@ -66,7 +64,7 @@ DB copy provides a helper which can copy LevelDB or RocksDB data quickly on the java -jar Toolkit.jar db cp output-directory/database /tmp/databse ``` -## DB Lite +## DB Lite(LevelDB unavailable on ARM) DB lite provides lite database, parameters are compatible with previous `LiteFullNodeTool`. @@ -134,7 +132,7 @@ Execute move command. java -jar Toolkit.jar db mv -c main_net_config.conf -d /data/tron/output-directory ``` -## DB Root +## DB Root(LevelDB unavailable on ARM) DB root provides a helper which can compute merkle root for tiny db. diff --git a/plugins/build.gradle b/plugins/build.gradle index 01afaa01708..e03e9a7c49a 100644 --- a/plugins/build.gradle +++ b/plugins/build.gradle @@ -20,6 +20,15 @@ configurations.getByName('checkstyleConfig') { transitive = false } +sourceSets { + main { + java.srcDirs = rootProject.archInfo.sourceSets.main.java.srcDirs + } + test { + java.srcDirs = rootProject.archInfo.sourceSets.test.java.srcDirs + } +} + dependencies { //local libraries implementation fileTree(dir: 'libs', include: '*.jar') @@ -28,10 +37,19 @@ dependencies { implementation group: 'info.picocli', name: 'picocli', version: '4.6.3' implementation group: 'com.typesafe', name: 'config', version: '1.3.2' implementation group: 'me.tongfei', name: 'progressbar', version: '0.9.3' - implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69' - implementation group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10' - implementation 'io.github.tronprotocol:leveldbjni-all:1.18.2' - implementation 'io.github.tronprotocol:leveldb:1.18.2' + implementation group: 'org.bouncycastle', name: 'bcprov-jdk18on', version: '1.79' + if (rootProject.archInfo.isArm64) { + testRuntimeOnly group: 'org.fusesource.hawtjni', name: 'hawtjni-runtime', version: '1.18' // for test + implementation project(":platform") + } else { + implementation project(":platform"), { + exclude(group: 'org.fusesource.leveldbjni', module: 'leveldbjni-all') + exclude(group: 'io.github.tronprotocol', module: 'zksnark-java-sdk') + exclude(group: 'commons-io', module: 'commons-io') + } + implementation 'io.github.tronprotocol:leveldbjni-all:1.18.2' + implementation 'io.github.tronprotocol:leveldb:1.18.2' + } implementation project(":protocol") } @@ -76,6 +94,17 @@ test { destinationFile = file("../framework/build/jacoco/jacocoTest1.exec") classDumpDir = file("$buildDir/jacoco/classpathdumps") } + + if (rootProject.archInfo.isArm64) { + exclude 'org/tron/plugins/leveldb/**' + filter { + excludeTestsMatching '*.*leveldb*' + excludeTestsMatching '*.*Leveldb*' + excludeTestsMatching '*.*LevelDB*' + excludeTestsMatching '*.*LevelDb*' + excludeTestsMatching '*.*Archive*' + } + } } jacocoTestReport { @@ -94,7 +123,7 @@ def binaryRelease(taskName, jarName, mainClass) { from(sourceSets.main.output) { include "/**" } - dependsOn project(':protocol').jar // explicit_dependency + dependsOn (project(':protocol').jar, project(':platform').jar) // explicit_dependency from { configurations.runtimeClasspath.collect { // https://docs.gradle.org/current/userguide/upgrading_version_6.html#changes_6.3 it.isDirectory() ? it : zipTree(it) @@ -127,7 +156,7 @@ def createScript(project, mainClass, name) { } } } -applicationDistribution.from("../gradle/java-tron.vmoptions") { +applicationDistribution.from(rootProject.archInfo.VMOptions) { into "bin" } createScript(project, 'org.tron.plugins.ArchiveManifest', 'ArchiveManifest') @@ -157,4 +186,4 @@ task copyToParent(type: Copy) { -build.finalizedBy(copyToParent) \ No newline at end of file +build.finalizedBy(copyToParent) diff --git a/plugins/src/main/java/arm/org/tron/plugins/ArchiveManifest.java b/plugins/src/main/java/arm/org/tron/plugins/ArchiveManifest.java new file mode 100644 index 00000000000..b7848cf4c6f --- /dev/null +++ b/plugins/src/main/java/arm/org/tron/plugins/ArchiveManifest.java @@ -0,0 +1,29 @@ +package org.tron.plugins; + +import lombok.extern.slf4j.Slf4j; +import org.tron.common.arch.Arch; + +/** + * ARM architecture only supports RocksDB, + * which does not require manifest rebuilding (manifest rebuilding is a LevelDB-only feature). + * This command is not supported but retained for compatibility. + **/ +@Slf4j(topic = "archive") +public class ArchiveManifest { + + public static void main(String[] args) { + int exitCode = run(args); + System.exit(exitCode); + } + + public static int run(String[] args) { + String tips = String.format( + "%s architecture only supports RocksDB, which does not require manifest rebuilding " + + "(manifest rebuilding is a LevelDB-only feature).", + Arch.getOsArch()); + System.out.println(tips); + logger.warn(tips); + return 0; + } + +} diff --git a/plugins/src/main/java/arm/org/tron/plugins/DbArchive.java b/plugins/src/main/java/arm/org/tron/plugins/DbArchive.java new file mode 100644 index 00000000000..03c52f33f02 --- /dev/null +++ b/plugins/src/main/java/arm/org/tron/plugins/DbArchive.java @@ -0,0 +1,50 @@ +package org.tron.plugins; + +import java.util.concurrent.Callable; +import lombok.extern.slf4j.Slf4j; +import org.tron.common.arch.Arch; +import picocli.CommandLine; +import picocli.CommandLine.Option; + +/** + * ARM architecture only supports RocksDB, + * which does not require manifest rebuilding (manifest rebuilding is a LevelDB-only feature). + * This command is not supported but retained for compatibility. + **/ +@Slf4j(topic = "archive") +@CommandLine.Command(name = "archive", description = "A helper to rewrite leveldb manifest.") +public class DbArchive implements Callable { + + @CommandLine.Spec + CommandLine.Model.CommandSpec spec; + @Option(names = {"-d", "--database-directory"}, + defaultValue = "output-directory/database", + description = "java-tron database directory. Default: ${DEFAULT-VALUE}") + private String databaseDirectory; + + @Option(names = {"-b", "--batch-size"}, + defaultValue = "80000", + description = "deal manifest batch size. Default: ${DEFAULT-VALUE}") + private int maxBatchSize; + + @Option(names = {"-m", "--manifest-size"}, + defaultValue = "0", + description = "manifest min size(M) to archive. Default: ${DEFAULT-VALUE}") + private int maxManifestSize; + + @Option(names = {"-h", "--help"}) + private boolean help; + + + @Override + public Integer call() throws Exception { + String tips = String.format( + "%s architecture only supports RocksDB, which does not require manifest rebuilding " + + "(manifest rebuilding is a LevelDB-only feature).", + Arch.getOsArch()); + spec.commandLine().getErr().println(spec.commandLine().getColorScheme().errorText(tips)); + logger.warn(tips); + return 0; + } + +} diff --git a/plugins/src/main/java/org/tron/plugins/Db.java b/plugins/src/main/java/common/org/tron/plugins/Db.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/Db.java rename to plugins/src/main/java/common/org/tron/plugins/Db.java diff --git a/plugins/src/main/java/org/tron/plugins/DbConvert.java b/plugins/src/main/java/common/org/tron/plugins/DbConvert.java similarity index 89% rename from plugins/src/main/java/org/tron/plugins/DbConvert.java rename to plugins/src/main/java/common/org/tron/plugins/DbConvert.java index a75b235bbcf..bcf6e1e7afc 100644 --- a/plugins/src/main/java/org/tron/plugins/DbConvert.java +++ b/plugins/src/main/java/common/org/tron/plugins/DbConvert.java @@ -20,6 +20,7 @@ import org.rocksdb.RocksDBException; import org.rocksdb.RocksIterator; import org.rocksdb.Status; +import org.tron.common.arch.Arch; import org.tron.plugins.utils.DBUtils; import org.tron.plugins.utils.FileUtils; import picocli.CommandLine; @@ -49,20 +50,19 @@ public class DbConvert implements Callable { description = "Output path for rocksdb. Default: ${DEFAULT-VALUE}") private File dest; - @CommandLine.Option(names = {"--safe"}, - description = "In safe mode, read data from leveldb then put rocksdb." - + "If not, just change engine.properties from leveldb to rocksdb," - + "rocksdb is compatible with leveldb for current version." - + "This may not be the case in the future." - + "Default: ${DEFAULT-VALUE}") - private boolean safe; - @CommandLine.Option(names = {"-h", "--help"}) private boolean help; @Override public Integer call() throws Exception { + if (Arch.isArm64()) { + String tips = String.format("This command is not supported on %s architecture.", + Arch.getOsArch()); + spec.commandLine().getErr().println(spec.commandLine().getColorScheme().errorText(tips)); + logger.error(tips); + return 0; + } if (help) { spec.commandLine().usage(System.out); return 0; @@ -95,12 +95,12 @@ public Integer call() throws Exception { final long time = System.currentTimeMillis(); List services = new ArrayList<>(); files.forEach(f -> services.add( - new DbConverter(src.getPath(), dest.getPath(), f.getName(), safe))); + new DbConverter(src.getPath(), dest.getPath(), f.getName()))); cpList.forEach(f -> services.add( new DbConverter( Paths.get(src.getPath(), DBUtils.CHECKPOINT_DB_V2).toString(), Paths.get(dest.getPath(), DBUtils.CHECKPOINT_DB_V2).toString(), - f.getName(), safe))); + f.getName()))); List fails = ProgressBar.wrap(services.stream(), "convert task").parallel().map( dbConverter -> { try { @@ -140,15 +140,12 @@ static class DbConverter implements Converter { private long srcDbValueSum = 0L; private long dstDbValueSum = 0L; - private boolean safe; - - public DbConverter(String srcDir, String dstDir, String name, boolean safe) { + public DbConverter(String srcDir, String dstDir, String name) { this.srcDir = srcDir; this.dstDir = dstDir; this.dbName = name; this.srcDbPath = Paths.get(this.srcDir, name); this.dstDbPath = Paths.get(this.dstDir, name); - this.safe = safe; } @Override @@ -178,23 +175,15 @@ public boolean doConvert() throws Exception { FileUtils.createDirIfNotExists(dstDir); logger.info("Convert database {} start", this.dbName); - if (safe) { - convertLevelToRocks(); - compact(); - } else { - FileUtils.copyDir(Paths.get(srcDir), Paths.get(dstDir), dbName); - } + convertLevelToRocks(); + compact(); + boolean result = check() && createEngine(dstDbPath.toString()); long etime = System.currentTimeMillis(); if (result) { - if (safe) { - logger.info("Convert database {} successful end with {} key-value {} minutes", - this.dbName, this.srcDbKeyCount, (etime - startTime) / 1000.0 / 60); - } else { - logger.info("Convert database {} successful end {} minutes", - this.dbName, (etime - startTime) / 1000.0 / 60); - } + logger.info("Convert database {} successful end with {} key-value {} minutes", + this.dbName, this.srcDbKeyCount, (etime - startTime) / 1000.0 / 60); } else { logger.info("Convert database {} failure", this.dbName); @@ -234,8 +223,8 @@ private void batchInsert(RocksDB rocks, List keys, List values) * @throws Exception RocksDBException */ private void write(RocksDB rocks, org.rocksdb.WriteBatch batch) throws Exception { - try { - rocks.write(new org.rocksdb.WriteOptions(), batch); + try (org.rocksdb.WriteOptions writeOptions = new org.rocksdb.WriteOptions()) { + rocks.write(writeOptions, batch); } catch (RocksDBException e) { // retry if (maybeRetry(e)) { @@ -270,7 +259,8 @@ public void convertLevelToRocks() throws Exception { JniDBFactory.pushMemoryPool(1024 * 1024); try ( DB level = DBUtils.newLevelDb(srcDbPath); - RocksDB rocks = DBUtils.newRocksDbForBulkLoad(dstDbPath); + org.rocksdb.Options options = DBUtils.newDefaultRocksDbOptions(true, dbName); + RocksDB rocks = RocksDB.open(options, this.dstDbPath.toString()); DBIterator levelIterator = level.iterator( new org.iq80.leveldb.ReadOptions().fillCache(false))) { @@ -302,7 +292,8 @@ private void compact() throws RocksDBException { if (DBUtils.MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(this.dbName)) { return; } - try (RocksDB rocks = DBUtils.newRocksDb(this.dstDbPath)) { + try (org.rocksdb.Options options = DBUtils.newDefaultRocksDbOptions(false, dbName); + RocksDB rocks = RocksDB.open(options, this.dstDbPath.toString())) { logger.info("compact database {} start", this.dbName); rocks.compactRange(); logger.info("compact database {} end", this.dbName); @@ -310,11 +301,9 @@ private void compact() throws RocksDBException { } private boolean check() throws RocksDBException { - if (!safe) { - return true; - } try ( - RocksDB rocks = DBUtils.newRocksDbReadOnly(this.dstDbPath); + org.rocksdb.Options options = DBUtils.newDefaultRocksDbOptions(false, dbName); + RocksDB rocks = RocksDB.openReadOnly(options, this.dstDbPath.toString()); org.rocksdb.ReadOptions r = new org.rocksdb.ReadOptions().setFillCache(false); RocksIterator rocksIterator = rocks.newIterator(r)) { diff --git a/plugins/src/main/java/org/tron/plugins/DbCopy.java b/plugins/src/main/java/common/org/tron/plugins/DbCopy.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/DbCopy.java rename to plugins/src/main/java/common/org/tron/plugins/DbCopy.java diff --git a/plugins/src/main/java/org/tron/plugins/DbLite.java b/plugins/src/main/java/common/org/tron/plugins/DbLite.java similarity index 99% rename from plugins/src/main/java/org/tron/plugins/DbLite.java rename to plugins/src/main/java/common/org/tron/plugins/DbLite.java index 732d4913021..3f8a6cb58c8 100644 --- a/plugins/src/main/java/org/tron/plugins/DbLite.java +++ b/plugins/src/main/java/common/org/tron/plugins/DbLite.java @@ -656,12 +656,13 @@ private boolean isLite(String databaseDir) throws RocksDBException, IOException private long getSecondBlock(String databaseDir) throws RocksDBException, IOException { long num = 0; DBInterface sourceBlockIndexDb = DbTool.getDB(databaseDir, BLOCK_INDEX_DB_NAME); - DBIterator iterator = sourceBlockIndexDb.iterator(); - iterator.seek(ByteArray.fromLong(1)); - if (iterator.hasNext()) { - num = Longs.fromByteArray(iterator.getKey()); + try (DBIterator iterator = sourceBlockIndexDb.iterator()) { + iterator.seek(ByteArray.fromLong(1)); + if (iterator.hasNext()) { + num = Longs.fromByteArray(iterator.getKey()); + } + return num; } - return num; } private DBInterface getCheckpointDb(String sourceDir) throws IOException, RocksDBException { diff --git a/plugins/src/main/java/org/tron/plugins/DbMove.java b/plugins/src/main/java/common/org/tron/plugins/DbMove.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/DbMove.java rename to plugins/src/main/java/common/org/tron/plugins/DbMove.java diff --git a/plugins/src/main/java/org/tron/plugins/DbRoot.java b/plugins/src/main/java/common/org/tron/plugins/DbRoot.java similarity index 84% rename from plugins/src/main/java/org/tron/plugins/DbRoot.java rename to plugins/src/main/java/common/org/tron/plugins/DbRoot.java index 7c33219e180..45854bbebdc 100644 --- a/plugins/src/main/java/org/tron/plugins/DbRoot.java +++ b/plugins/src/main/java/common/org/tron/plugins/DbRoot.java @@ -67,16 +67,24 @@ public Integer call() throws Exception { .errorText("Specify at least one exit database: --db dbName.")); return 404; } - List task = ProgressBar.wrap(dbs.stream(), "root task").parallel() - .map(this::calcMerkleRoot).collect(Collectors.toList()); - task.forEach(this::printInfo); - int code = (int) task.stream().filter(r -> r.code == 1).count(); - if (code > 0) { + try { + List task = ProgressBar.wrap(dbs.stream(), "root task").parallel() + .map(this::calcMerkleRoot).collect(Collectors.toList()); + task.forEach(this::printInfo); + int code = (int) task.stream().filter(r -> r.code == 1).count(); + if (code > 0) { + spec.commandLine().getErr().println(spec.commandLine().getColorScheme() + .errorText("There are some errors, please check toolkit.log for detail.")); + } + spec.commandLine().getOut().println("root task done."); + return code; + } catch (Exception e) { + logger.error("{}", e); spec.commandLine().getErr().println(spec.commandLine().getColorScheme() - .errorText("There are some errors, please check toolkit.log for detail.")); + .errorText(e.getMessage())); + spec.commandLine().usage(System.out); + return 1; } - spec.commandLine().getOut().println("root task done."); - return code; } private Ret calcMerkleRoot(String name) { diff --git a/plugins/src/main/java/org/tron/plugins/Toolkit.java b/plugins/src/main/java/common/org/tron/plugins/Toolkit.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/Toolkit.java rename to plugins/src/main/java/common/org/tron/plugins/Toolkit.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/ByteArray.java b/plugins/src/main/java/common/org/tron/plugins/utils/ByteArray.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/ByteArray.java rename to plugins/src/main/java/common/org/tron/plugins/utils/ByteArray.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/CryptoUitls.java b/plugins/src/main/java/common/org/tron/plugins/utils/CryptoUitls.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/CryptoUitls.java rename to plugins/src/main/java/common/org/tron/plugins/utils/CryptoUitls.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/DBUtils.java b/plugins/src/main/java/common/org/tron/plugins/utils/DBUtils.java similarity index 72% rename from plugins/src/main/java/org/tron/plugins/utils/DBUtils.java rename to plugins/src/main/java/common/org/tron/plugins/utils/DBUtils.java index f8559d5dba8..6eb097cbec5 100644 --- a/plugins/src/main/java/org/tron/plugins/utils/DBUtils.java +++ b/plugins/src/main/java/common/org/tron/plugins/utils/DBUtils.java @@ -14,10 +14,9 @@ import org.rocksdb.BloomFilter; import org.rocksdb.ComparatorOptions; import org.rocksdb.Options; -import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; -import org.tron.plugins.comparator.MarketOrderPriceComparatorForLevelDB; -import org.tron.plugins.comparator.MarketOrderPriceComparatorForRockDB; +import org.tron.common.arch.Arch; +import org.tron.common.utils.MarketOrderPriceComparatorForLevelDB; +import org.tron.common.utils.MarketOrderPriceComparatorForRocksDB; import org.tron.protos.Protocol; public class DBUtils { @@ -65,6 +64,7 @@ static Operator valueOf(byte b) { public static final String ROCKSDB = "ROCKSDB"; public static DB newLevelDb(Path db) throws IOException { + Arch.throwIfUnsupportedArm64Exception(LEVELDB); File file = db.toFile(); org.iq80.leveldb.Options dbOptions = newDefaultLevelDbOptions(); if (MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(file.getName())) { @@ -86,7 +86,23 @@ public static org.iq80.leveldb.Options newDefaultLevelDbOptions() { return dbOptions; } - private static Options newDefaultRocksDbOptions(boolean forBulkLoad) { + /** + * Creates a new RocksDB Options. + * + *

CRITICAL: Must be closed after use to prevent native memory leaks. + * Use try-with-resources. + * + *

{@code
+   * try (Options options = newDefaultRocksDbOptions(false, name)) {
+   *     // do something
+   * }
+   * }
+ * + * @param forBulkLoad if true, optimizes for bulk loading + * @param name db name + * @return a new Options instance that must be closed + */ + public static Options newDefaultRocksDbOptions(boolean forBulkLoad, String name) { Options options = new Options(); options.setCreateIfMissing(true); options.setIncreaseParallelism(1); @@ -109,35 +125,10 @@ private static Options newDefaultRocksDbOptions(boolean forBulkLoad) { if (forBulkLoad) { options.prepareForBulkLoad(); } - return options; - } - - public static RocksDB newRocksDb(Path db) throws RocksDBException { - try (Options options = newDefaultRocksDbOptions(false)) { - if (MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(db.getFileName().toString())) { - options.setComparator(new MarketOrderPriceComparatorForRockDB(new ComparatorOptions())); - } - return RocksDB.open(options, db.toString()); - } - } - - public static RocksDB newRocksDbForBulkLoad(Path db) throws RocksDBException { - try (Options options = newDefaultRocksDbOptions(true)) { - if (MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(db.getFileName().toString())) { - options.setComparator(new MarketOrderPriceComparatorForRockDB(new ComparatorOptions())); - } - return RocksDB.open(options, db.toString()); - } - } - - - public static RocksDB newRocksDbReadOnly(Path db) throws RocksDBException { - try (Options options = newDefaultRocksDbOptions(false)) { - if (MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(db.getFileName().toString())) { - options.setComparator(new MarketOrderPriceComparatorForRockDB(new ComparatorOptions())); - } - return RocksDB.openReadOnly(options, db.toString()); + if (MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(name)) { + options.setComparator(new MarketOrderPriceComparatorForRocksDB(new ComparatorOptions())); } + return options; } public static String simpleDecode(byte[] bytes) { diff --git a/plugins/src/main/java/org/tron/plugins/utils/FileUtils.java b/plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/FileUtils.java rename to plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java diff --git a/plugins/src/main/java/common/org/tron/plugins/utils/MarketUtils.java b/plugins/src/main/java/common/org/tron/plugins/utils/MarketUtils.java new file mode 100644 index 00000000000..9bcfd5e71ce --- /dev/null +++ b/plugins/src/main/java/common/org/tron/plugins/utils/MarketUtils.java @@ -0,0 +1,68 @@ +package org.tron.plugins.utils; + +public class MarketUtils { + + public static final int TOKEN_ID_LENGTH = ByteArray + .fromString(Long.toString(Long.MAX_VALUE)).length; // 19 + + + + /** + * In order to avoid the difference between the data of same key stored and fetched by hashMap and + * levelDB, when creating the price key, we will find the GCD (Greatest Common Divisor) of + * sellTokenQuantity and buyTokenQuantity. + */ + public static byte[] createPairPriceKey(byte[] sellTokenId, byte[] buyTokenId, + long sellTokenQuantity, long buyTokenQuantity) { + + byte[] sellTokenQuantityBytes; + byte[] buyTokenQuantityBytes; + + // cal the GCD + long gcd = findGCD(sellTokenQuantity, buyTokenQuantity); + if (gcd == 0) { + sellTokenQuantityBytes = ByteArray.fromLong(sellTokenQuantity); + buyTokenQuantityBytes = ByteArray.fromLong(buyTokenQuantity); + } else { + sellTokenQuantityBytes = ByteArray.fromLong(sellTokenQuantity / gcd); + buyTokenQuantityBytes = ByteArray.fromLong(buyTokenQuantity / gcd); + } + + return doCreatePairPriceKey(sellTokenId, buyTokenId, + sellTokenQuantityBytes, buyTokenQuantityBytes); + } + + public static long findGCD(long number1, long number2) { + if (number1 == 0 || number2 == 0) { + return 0; + } + return calGCD(number1, number2); + } + + private static long calGCD(long number1, long number2) { + if (number2 == 0) { + return number1; + } + return calGCD(number2, number1 % number2); + } + + + private static byte[] doCreatePairPriceKey(byte[] sellTokenId, byte[] buyTokenId, + byte[] sellTokenQuantity, byte[] buyTokenQuantity) { + byte[] result = new byte[TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + + sellTokenQuantity.length + buyTokenQuantity.length]; + + System.arraycopy(sellTokenId, 0, result, 0, sellTokenId.length); + System.arraycopy(buyTokenId, 0, result, TOKEN_ID_LENGTH, buyTokenId.length); + System.arraycopy(sellTokenQuantity, 0, result, + TOKEN_ID_LENGTH + TOKEN_ID_LENGTH, + sellTokenQuantity.length); + System.arraycopy(buyTokenQuantity, 0, result, + TOKEN_ID_LENGTH + TOKEN_ID_LENGTH + buyTokenQuantity.length, + buyTokenQuantity.length); + + return result; + } + + +} diff --git a/plugins/src/main/java/org/tron/plugins/utils/MerkleRoot.java b/plugins/src/main/java/common/org/tron/plugins/utils/MerkleRoot.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/MerkleRoot.java rename to plugins/src/main/java/common/org/tron/plugins/utils/MerkleRoot.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/Sha256Hash.java b/plugins/src/main/java/common/org/tron/plugins/utils/Sha256Hash.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/Sha256Hash.java rename to plugins/src/main/java/common/org/tron/plugins/utils/Sha256Hash.java diff --git a/plugins/src/main/java/common/org/tron/plugins/utils/db/DBInterface.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/DBInterface.java new file mode 100644 index 00000000000..13a195f9347 --- /dev/null +++ b/plugins/src/main/java/common/org/tron/plugins/utils/db/DBInterface.java @@ -0,0 +1,39 @@ +package org.tron.plugins.utils.db; + +import java.io.Closeable; +import java.io.IOException; + + +public interface DBInterface extends Closeable { + + byte[] get(byte[] key); + + void put(byte[] key, byte[] value); + + void delete(byte[] key); + + /** + * Returns an iterator over the database. + * + *

CRITICAL: The returned iterator holds native resources and MUST be closed + * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources + * statement. + * + *

Example of correct usage: + *

{@code
+   * try (DBIterator iterator = db.iterator()) {
+   *  // do something
+   * }
+   * }
+ * + * @return a new database iterator that must be closed. + */ + DBIterator iterator(); + + long size() throws IOException; + + void close() throws IOException; + + String getName(); + +} diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/DBIterator.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/DBIterator.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/db/DBIterator.java rename to plugins/src/main/java/common/org/tron/plugins/utils/db/DBIterator.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/DbTool.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/DbTool.java similarity index 85% rename from plugins/src/main/java/org/tron/plugins/utils/db/DbTool.java rename to plugins/src/main/java/common/org/tron/plugins/utils/db/DbTool.java index 429025e8f8b..127b8f97db5 100644 --- a/plugins/src/main/java/org/tron/plugins/utils/db/DbTool.java +++ b/plugins/src/main/java/common/org/tron/plugins/utils/db/DbTool.java @@ -18,8 +18,8 @@ public class DbTool { private static final String KEY_ENGINE = "ENGINE"; private static final String ENGINE_FILE = "engine.properties"; - private static final String FILE_SEPARATOR = File.separator; private static final String ROCKSDB = "ROCKSDB"; + private static final String LEVELDB = "LEVELDB"; private static final Map dbMap = Maps.newConcurrentMap(); @@ -162,8 +162,7 @@ public static void close() { } private static DbType getDbType(String sourceDir, String dbName) { - String engineFile = String.format("%s%s%s%s%s", sourceDir, FILE_SEPARATOR, - dbName, FILE_SEPARATOR, ENGINE_FILE); + String engineFile = Paths.get(sourceDir, dbName, ENGINE_FILE).toString(); if (!new File(engineFile).exists()) { return DbType.LevelDB; } @@ -175,13 +174,22 @@ private static DbType getDbType(String sourceDir, String dbName) { } } - private static LevelDBImpl openLevelDb(Path db, String name) throws IOException { - return new LevelDBImpl(DBUtils.newLevelDb(db), name); + public static LevelDBImpl openLevelDb(Path db, String name) throws IOException { + LevelDBImpl leveldb = new LevelDBImpl(DBUtils.newLevelDb(db), name); + tryInitEngineFile(db, LEVELDB); + return leveldb; } - private static RocksDBImpl openRocksDb(Path db, String name) throws RocksDBException { - return new RocksDBImpl(DBUtils.newRocksDb(db), name); + public static RocksDBImpl openRocksDb(Path db, String name) throws RocksDBException { + RocksDBImpl rocksdb = new RocksDBImpl(db, name); + tryInitEngineFile(db, ROCKSDB); + return rocksdb; } - + private static void tryInitEngineFile(Path db, String engine) { + String engineFile = Paths.get(db.toString(), ENGINE_FILE).toString(); + if (FileUtils.createFileIfNotExists(engineFile)) { + FileUtils.writeProperty(engineFile, KEY_ENGINE, engine); + } + } } diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/LevelDBImpl.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/LevelDBImpl.java similarity index 83% rename from plugins/src/main/java/org/tron/plugins/utils/db/LevelDBImpl.java rename to plugins/src/main/java/common/org/tron/plugins/utils/db/LevelDBImpl.java index 511f4dfd5b4..1c7f22eff1a 100644 --- a/plugins/src/main/java/org/tron/plugins/utils/db/LevelDBImpl.java +++ b/plugins/src/main/java/common/org/tron/plugins/utils/db/LevelDBImpl.java @@ -40,8 +40,11 @@ public DBIterator iterator() { } @Override - public long size() { - return Streams.stream(leveldb.iterator()).count(); + public long size() throws IOException { + try (DBIterator iterator = this.iterator()) { + iterator.seekToFirst(); + return Streams.stream(iterator).count(); + } } @Override diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/LevelDBIterator.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/LevelDBIterator.java similarity index 100% rename from plugins/src/main/java/org/tron/plugins/utils/db/LevelDBIterator.java rename to plugins/src/main/java/common/org/tron/plugins/utils/db/LevelDBIterator.java diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/RockDBIterator.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/RockDBIterator.java similarity index 76% rename from plugins/src/main/java/org/tron/plugins/utils/db/RockDBIterator.java rename to plugins/src/main/java/common/org/tron/plugins/utils/db/RockDBIterator.java index d3e17d9173f..17ecca4a4c1 100644 --- a/plugins/src/main/java/org/tron/plugins/utils/db/RockDBIterator.java +++ b/plugins/src/main/java/common/org/tron/plugins/utils/db/RockDBIterator.java @@ -2,14 +2,19 @@ import java.io.IOException; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import org.rocksdb.ReadOptions; import org.rocksdb.RocksIterator; public class RockDBIterator implements DBIterator { private final RocksIterator iterator; + private final ReadOptions readOptions; + private final AtomicBoolean closed = new AtomicBoolean(false); - public RockDBIterator(RocksIterator iterator) { + public RockDBIterator(RocksIterator iterator, ReadOptions readOptions) { this.iterator = iterator; + this.readOptions = readOptions; } @Override @@ -72,6 +77,9 @@ public byte[] setValue(byte[] value) { @Override public void close() throws IOException { - iterator.close(); + if (closed.compareAndSet(false, true)) { + readOptions.close(); + iterator.close(); + } } } diff --git a/plugins/src/main/java/common/org/tron/plugins/utils/db/RocksDBImpl.java b/plugins/src/main/java/common/org/tron/plugins/utils/db/RocksDBImpl.java new file mode 100644 index 00000000000..236d0a847b3 --- /dev/null +++ b/plugins/src/main/java/common/org/tron/plugins/utils/db/RocksDBImpl.java @@ -0,0 +1,97 @@ +package org.tron.plugins.utils.db; + +import com.google.common.collect.Streams; +import java.io.IOException; +import java.nio.file.Path; +import java.util.concurrent.atomic.AtomicBoolean; +import lombok.Getter; +import org.rocksdb.Options; +import org.rocksdb.ReadOptions; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksDBException; +import org.tron.plugins.utils.DBUtils; + +public class RocksDBImpl implements DBInterface { + + private final RocksDB rocksDB; + + @Getter + private final String name; + private final AtomicBoolean closed = new AtomicBoolean(false); + private Options options = null; + + public RocksDBImpl(Path path, String name) throws RocksDBException { + try { + this.options = DBUtils.newDefaultRocksDbOptions(false, name); + this.name = name; + this.rocksDB = RocksDB.open(options, path.toString()); + } catch (RocksDBException e) { + if (this.options != null) { + this.options.close(); + } + throw e; + } + } + + @Override + public byte[] get(byte[] key) { + throwIfClosed(); + try { + return rocksDB.get(key); + } catch (RocksDBException e) { + throw new RuntimeException(name, e); + } + } + + @Override + public void put(byte[] key, byte[] value) { + throwIfClosed(); + try { + rocksDB.put(key, value); + } catch (RocksDBException e) { + throw new RuntimeException(name, e); + } + } + + @Override + public void delete(byte[] key) { + throwIfClosed(); + try { + rocksDB.delete(key); + } catch (RocksDBException e) { + throw new RuntimeException(name, e); + } + } + + @Override + public DBIterator iterator() { + throwIfClosed(); + ReadOptions readOptions = new ReadOptions().setFillCache(false); + return new RockDBIterator(rocksDB.newIterator(readOptions), readOptions); + } + + @Override + public long size() throws IOException { + throwIfClosed(); + try (DBIterator iterator = this.iterator()) { + iterator.seekToFirst(); + return Streams.stream(iterator).count(); + } + } + + @Override + public void close() throws IOException { + if (closed.compareAndSet(false, true)) { + if (this.options != null) { + this.options.close(); + } + rocksDB.close(); + } + } + + private void throwIfClosed() { + if (closed.get()) { + throw new IllegalStateException("db " + name + " has been closed"); + } + } +} diff --git a/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForLevelDB.java b/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForLevelDB.java deleted file mode 100644 index 0879f770e1f..00000000000 --- a/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForLevelDB.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tron.plugins.comparator; - -import org.iq80.leveldb.DBComparator; -import org.tron.plugins.utils.MarketUtils; - -public class MarketOrderPriceComparatorForLevelDB implements DBComparator { - - @Override - public String name() { - return "MarketOrderPriceComparator"; - } - - @Override - public byte[] findShortestSeparator(byte[] start, byte[] limit) { - return new byte[0]; - } - - @Override - public byte[] findShortSuccessor(byte[] key) { - return new byte[0]; - } - - @Override - public int compare(byte[] o1, byte[] o2) { - return MarketUtils.comparePriceKey(o1, o2); - } - -} \ No newline at end of file diff --git a/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForRockDB.java b/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForRockDB.java deleted file mode 100644 index cd718bdd2d7..00000000000 --- a/plugins/src/main/java/org/tron/plugins/comparator/MarketOrderPriceComparatorForRockDB.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.tron.plugins.comparator; - -import org.rocksdb.ComparatorOptions; -import org.rocksdb.DirectSlice; -import org.rocksdb.util.DirectBytewiseComparator; -import org.tron.plugins.utils.MarketUtils; - -public class MarketOrderPriceComparatorForRockDB extends DirectBytewiseComparator { - - public MarketOrderPriceComparatorForRockDB(final ComparatorOptions copt) { - super(copt); - } - - @Override - public String name() { - return "MarketOrderPriceComparator"; - } - - @Override - public int compare(final DirectSlice a, final DirectSlice b) { - return MarketUtils.comparePriceKey(convertDataToBytes(a), convertDataToBytes(b)); - } - - /** - * DirectSlice.data().array will throw UnsupportedOperationException. - * */ - public byte[] convertDataToBytes(DirectSlice directSlice) { - int capacity = directSlice.data().capacity(); - byte[] bytes = new byte[capacity]; - - for (int i = 0; i < capacity; i++) { - bytes[i] = directSlice.get(i); - } - - return bytes; - } - -} \ No newline at end of file diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/DBInterface.java b/plugins/src/main/java/org/tron/plugins/utils/db/DBInterface.java deleted file mode 100644 index 513e021c83c..00000000000 --- a/plugins/src/main/java/org/tron/plugins/utils/db/DBInterface.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.tron.plugins.utils.db; - -import java.io.Closeable; -import java.io.IOException; - - -public interface DBInterface extends Closeable { - - byte[] get(byte[] key); - - void put(byte[] key, byte[] value); - - void delete(byte[] key); - - DBIterator iterator(); - - long size(); - - void close() throws IOException; - - String getName(); - -} diff --git a/plugins/src/main/java/org/tron/plugins/utils/db/RocksDBImpl.java b/plugins/src/main/java/org/tron/plugins/utils/db/RocksDBImpl.java deleted file mode 100644 index 50957bbe61b..00000000000 --- a/plugins/src/main/java/org/tron/plugins/utils/db/RocksDBImpl.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.tron.plugins.utils.db; - -import java.io.IOException; -import lombok.Getter; -import org.rocksdb.RocksDBException; -import org.rocksdb.RocksIterator; - -public class RocksDBImpl implements DBInterface { - - private org.rocksdb.RocksDB rocksDB; - - @Getter - private final String name; - - public RocksDBImpl(org.rocksdb.RocksDB rocksDB, String name) { - this.rocksDB = rocksDB; - this.name = name; - } - - @Override - public byte[] get(byte[] key) { - try { - return rocksDB.get(key); - } catch (RocksDBException e) { - e.printStackTrace(); - } - return null; - } - - @Override - public void put(byte[] key, byte[] value) { - try { - rocksDB.put(key, value); - } catch (RocksDBException e) { - e.printStackTrace(); - } - } - - @Override - public void delete(byte[] key) { - try { - rocksDB.delete(key); - } catch (RocksDBException e) { - e.printStackTrace(); - } - } - - @Override - public DBIterator iterator() { - return new RockDBIterator(rocksDB.newIterator( - new org.rocksdb.ReadOptions().setFillCache(false))); - } - - @Override - public long size() { - RocksIterator iterator = rocksDB.newIterator(); - long size = 0; - for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { - size++; - } - iterator.close(); - return size; - } - - @Override - public void close() throws IOException { - rocksDB.close(); - } -} diff --git a/plugins/src/main/java/org/tron/plugins/ArchiveManifest.java b/plugins/src/main/java/x86/org/tron/plugins/ArchiveManifest.java similarity index 98% rename from plugins/src/main/java/org/tron/plugins/ArchiveManifest.java rename to plugins/src/main/java/x86/org/tron/plugins/ArchiveManifest.java index 4d54df6d299..1d7a91027bf 100644 --- a/plugins/src/main/java/org/tron/plugins/ArchiveManifest.java +++ b/plugins/src/main/java/x86/org/tron/plugins/ArchiveManifest.java @@ -35,6 +35,7 @@ import org.iq80.leveldb.DB; import org.iq80.leveldb.Options; import org.iq80.leveldb.impl.Filename; +import org.tron.plugins.utils.DBUtils; import picocli.CommandLine; import picocli.CommandLine.Option; @@ -183,7 +184,7 @@ public boolean checkManifest(String dir) throws IOException { return false; } logger.info("CurrentName {}/{},size {} kb.", dir, currentName, current.length() / 1024); - if ("market_pair_price_to_order".equalsIgnoreCase(this.name)) { + if (DBUtils.MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(this.name)) { logger.info("Db {} ignored.", this.name); return false; } diff --git a/plugins/src/main/java/org/tron/plugins/DbArchive.java b/plugins/src/main/java/x86/org/tron/plugins/DbArchive.java similarity index 98% rename from plugins/src/main/java/org/tron/plugins/DbArchive.java rename to plugins/src/main/java/x86/org/tron/plugins/DbArchive.java index e3032731ede..15bb281babf 100644 --- a/plugins/src/main/java/org/tron/plugins/DbArchive.java +++ b/plugins/src/main/java/x86/org/tron/plugins/DbArchive.java @@ -19,6 +19,7 @@ import org.iq80.leveldb.DB; import org.iq80.leveldb.Options; import org.iq80.leveldb.impl.Filename; +import org.tron.plugins.utils.DBUtils; import org.tron.plugins.utils.FileUtils; import picocli.CommandLine; import picocli.CommandLine.Option; @@ -163,7 +164,7 @@ public boolean checkManifest(String dir) throws IOException { return false; } logger.info("CurrentName {}/{},size {} kb.", dir, currentName, current.length() / 1024); - if ("market_pair_price_to_order".equalsIgnoreCase(this.name)) { + if (DBUtils.MARKET_PAIR_PRICE_TO_ORDER.equalsIgnoreCase(this.name)) { logger.info("Db {} ignored.", this.name); return false; } diff --git a/plugins/src/test/java/org/tron/plugins/DbCopyTest.java b/plugins/src/test/java/org/tron/plugins/DbCopyTest.java index 9e488a592aa..571fd8f5aa7 100644 --- a/plugins/src/test/java/org/tron/plugins/DbCopyTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbCopyTest.java @@ -4,14 +4,25 @@ import java.util.UUID; import org.junit.Assert; import org.junit.Test; +import org.rocksdb.RocksDBException; +import org.tron.plugins.utils.db.DbTool; import picocli.CommandLine; public class DbCopyTest extends DbTest { @Test - public void testRun() { + public void testRunForLevelDB() throws RocksDBException, IOException { + init(DbTool.DbType.LevelDB); String[] args = new String[] { "db", "cp", INPUT_DIRECTORY, - genarateTmpDir()}; + generateTmpDir()}; + Assert.assertEquals(0, cli.execute(args)); + } + + @Test + public void testRunForRocksDB() throws RocksDBException, IOException { + init(DbTool.DbType.RocksDB); + String[] args = new String[] { "db", "cp", INPUT_DIRECTORY, + generateTmpDir()}; Assert.assertEquals(0, cli.execute(args)); } @@ -32,7 +43,7 @@ public void testNotExist() { @Test public void testEmpty() throws IOException { String[] args = new String[] {"db", "cp", temporaryFolder.newFolder().toString(), - genarateTmpDir()}; + generateTmpDir()}; Assert.assertEquals(0, cli.execute(args)); } @@ -46,7 +57,7 @@ public void testDestIsExist() throws IOException { @Test public void testSrcIsFile() throws IOException { String[] args = new String[] {"db", "cp", temporaryFolder.newFile().toString(), - genarateTmpDir()}; + generateTmpDir()}; Assert.assertEquals(403, cli.execute(args)); } diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java index b4c66c9820f..80fd90cce81 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java @@ -7,10 +7,8 @@ import java.io.File; import java.io.IOException; import java.nio.file.Paths; -import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.junit.After; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.rules.TemporaryFolder; import org.tron.api.WalletGrpc; @@ -21,6 +19,7 @@ import org.tron.common.crypto.ECKey; import org.tron.common.utils.FileUtil; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.TimeoutInterceptor; import org.tron.common.utils.Utils; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; @@ -53,6 +52,7 @@ public void startApp() { Args.getInstance().getRpcPort()); channelFull = ManagedChannelBuilder.forTarget(fullNode) .usePlaintext() + .intercept(new TimeoutInterceptor(5000)) .build(); blockingStubFull = WalletGrpc.newBlockingStub(channelFull); } @@ -62,14 +62,15 @@ public void startApp() { */ public void shutdown() throws InterruptedException { if (channelFull != null) { - channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); + channelFull.shutdownNow(); } context.close(); } - public void init() throws IOException { + public void init(String dbType) throws IOException { dbPath = folder.newFolder().toString(); - Args.setParam(new String[]{"-d", dbPath, "-w", "--p2p-disable", "true"}, + Args.setParam(new String[] { + "-d", dbPath, "-w", "--p2p-disable", "true", "--storage-db-engine", dbType}, "config-localtest.conf"); // allow account root Args.getInstance().setAllowAccountStateRoot(1); @@ -85,23 +86,22 @@ public void clear() { Args.clearParam(); } - void testTools(String dbType, int checkpointVersion) + public void testTools(String dbType, int checkpointVersion) throws InterruptedException, IOException { logger.info("dbType {}, checkpointVersion {}", dbType, checkpointVersion); dbPath = String.format("%s_%s_%d", dbPath, dbType, System.currentTimeMillis()); - init(); + init(dbType); final String[] argsForSnapshot = - new String[]{"-o", "split", "-t", "snapshot", "--fn-data-path", + new String[] {"-o", "split", "-t", "snapshot", "--fn-data-path", dbPath + File.separator + databaseDir, "--dataset-path", dbPath}; final String[] argsForHistory = - new String[]{"-o", "split", "-t", "history", "--fn-data-path", + new String[] {"-o", "split", "-t", "history", "--fn-data-path", dbPath + File.separator + databaseDir, "--dataset-path", dbPath}; final String[] argsForMerge = - new String[]{"-o", "merge", "--fn-data-path", dbPath + File.separator + databaseDir, + new String[] {"-o", "merge", "--fn-data-path", dbPath + File.separator + databaseDir, "--dataset-path", dbPath + File.separator + "history"}; - Args.getInstance().getStorage().setDbEngine(dbType); Args.getInstance().getStorage().setCheckpointVersion(checkpointVersion); DbLite.setRecentBlks(3); // start fullNode @@ -126,15 +126,15 @@ void testTools(String dbType, int checkpointVersion) File database = new File(Paths.get(dbPath, databaseDir).toString()); if (!database.renameTo(new File(Paths.get(dbPath, databaseDir + "_bak").toString()))) { throw new RuntimeException( - String.format("rename %s to %s failed", database.getPath(), - Paths.get(dbPath, databaseDir))); + String.format("rename %s to %s failed", database.getPath(), + Paths.get(dbPath, databaseDir))); } // change snapshot to the new database File snapshot = new File(Paths.get(dbPath, "snapshot").toString()); if (!snapshot.renameTo(new File(Paths.get(dbPath, databaseDir).toString()))) { throw new RuntimeException( - String.format("rename snapshot to %s failed", - Paths.get(dbPath, databaseDir))); + String.format("rename snapshot to %s failed", + Paths.get(dbPath, databaseDir))); } // start and validate the snapshot startApp(); @@ -161,7 +161,7 @@ private void generateSomeTransactions(int during) { String sunPri = getRandomPrivateKey(); byte[] sunAddress = PublicMethod.getFinalAddress(sunPri); PublicMethod.sendcoin(address, 1L, - sunAddress, sunPri, blockingStubFull); + sunAddress, sunPri, blockingStubFull); try { Thread.sleep(sleepOnce); } catch (InterruptedException e) { diff --git a/plugins/src/test/java/org/tron/plugins/DbMoveTest.java b/plugins/src/test/java/org/tron/plugins/DbMoveTest.java index c1bc6b470fc..5b25739f272 100644 --- a/plugins/src/test/java/org/tron/plugins/DbMoveTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbMoveTest.java @@ -1,52 +1,41 @@ package org.tron.plugins; -import static org.iq80.leveldb.impl.Iq80DBFactory.factory; - import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Paths; -import java.util.UUID; import lombok.extern.slf4j.Slf4j; -import org.junit.AfterClass; +import org.junit.After; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; -import org.tron.plugins.utils.FileUtils; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.RocksDBException; +import org.tron.plugins.utils.DBUtils; +import org.tron.plugins.utils.db.DbTool; import picocli.CommandLine; @Slf4j public class DbMoveTest { private static final String OUTPUT_DIRECTORY = "output-directory-toolkit"; - private static final String OUTPUT_DIRECTORY_DATABASE = - Paths.get(OUTPUT_DIRECTORY,"ori","database").toString(); - private static final String ENGINE = "ENGINE"; - private static final String LEVELDB = "LEVELDB"; - private static final String ACCOUNT = "account"; - private static final String TRANS = "trans"; - private static final String MARKET = "market_pair_price_to_order"; - private static final String ENGINE_FILE = "engine.properties"; + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); - @BeforeClass - public static void init() throws IOException { - File file = new File(OUTPUT_DIRECTORY_DATABASE, ACCOUNT); - factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); - FileUtils.writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); + private static final String ACCOUNT = "account"; + private static final String TRANS = "trans"; - file = new File(OUTPUT_DIRECTORY_DATABASE, MARKET); - factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); - FileUtils.writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); - file = new File(OUTPUT_DIRECTORY_DATABASE, TRANS); - factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); - FileUtils.writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); + private void init(DbTool.DbType dbType, String path) throws IOException, RocksDBException { + DbTool.getDB(path, ACCOUNT, dbType).close(); + DbTool.getDB(path, DBUtils.MARKET_PAIR_PRICE_TO_ORDER, dbType).close(); + DbTool.getDB(path, TRANS, dbType).close(); } - @AfterClass - public static void destroy() { + @After + public void destroy() { deleteDir(new File(OUTPUT_DIRECTORY)); } @@ -74,9 +63,23 @@ private static String getConfig(String config) { } @Test - public void testMv() { + public void testMvForLevelDB() throws RocksDBException, IOException { + File database = temporaryFolder.newFolder("database"); + init(DbTool.DbType.LevelDB, Paths.get(database.getPath()).toString()); + String[] args = new String[] {"db", "mv", "-d", + database.getParent(), "-c", + getConfig("config.conf")}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(0, cli.execute(args)); + Assert.assertEquals(2, cli.execute(args)); + } + + @Test + public void testMvForRocksDB() throws RocksDBException, IOException { + File database = temporaryFolder.newFolder("database"); + init(DbTool.DbType.RocksDB, Paths.get(database.getPath()).toString()); String[] args = new String[] {"db", "mv", "-d", - Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + database.getParent(), "-c", getConfig("config.conf")}; CommandLine cli = new CommandLine(new Toolkit()); Assert.assertEquals(0, cli.execute(args)); @@ -84,9 +87,10 @@ public void testMv() { } @Test - public void testDuplicate() { + public void testDuplicate() throws IOException { + File output = temporaryFolder.newFolder(); String[] args = new String[] {"db", "mv", "-d", - Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + output.getPath(), "-c", getConfig("config-duplicate.conf")}; CommandLine cli = new CommandLine(new Toolkit()); Assert.assertEquals(2, cli.execute(args)); @@ -107,20 +111,19 @@ public void testDicNotExist() { } @Test - public void testConfNotExist() { + public void testConfNotExist() throws IOException { + File output = temporaryFolder.newFolder(); String[] args = new String[] {"db", "mv", "-d", - Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + output.getPath(), "-c", "config.conf"}; CommandLine cli = new CommandLine(new Toolkit()); Assert.assertEquals(2, cli.execute(args)); } @Test - public void testEmpty() { - File file = new File(OUTPUT_DIRECTORY_DATABASE + File.separator + UUID.randomUUID()); - file.mkdirs(); - file.deleteOnExit(); - String[] args = new String[] {"db", "mv", "-d", file.toString(), "-c", + public void testEmpty() throws IOException { + File output = temporaryFolder.newFolder(); + String[] args = new String[] {"db", "mv", "-d", output.getPath(), "-c", getConfig("config.conf")}; CommandLine cli = new CommandLine(new Toolkit()); diff --git a/plugins/src/test/java/org/tron/plugins/DbRootTest.java b/plugins/src/test/java/org/tron/plugins/DbRootTest.java index b86688f77d5..9d032a43103 100644 --- a/plugins/src/test/java/org/tron/plugins/DbRootTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbRootTest.java @@ -9,10 +9,8 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.rocksdb.RocksDBException; -import org.tron.plugins.utils.DBUtils; import org.tron.plugins.utils.db.DBInterface; import org.tron.plugins.utils.db.DbTool; -import org.tron.plugins.utils.db.LevelDBImpl; import picocli.CommandLine; @Slf4j @@ -27,8 +25,18 @@ public class DbRootTest { private static final String EMPTY_DB = "empty"; private static final String ERROR_DB = "error"; + + @Test + public void testRootForLevelDB() throws RocksDBException, IOException { + testRoot(DbTool.DbType.LevelDB); + } + @Test - public void testRoot() throws IOException, RocksDBException { + public void testRootForRocksDB() throws RocksDBException, IOException { + testRoot(DbTool.DbType.RocksDB); + } + + public void testRoot(DbTool.DbType dbType) throws IOException, RocksDBException { File file = folder.newFolder(); @@ -36,8 +44,8 @@ public void testRoot() throws IOException, RocksDBException { Assert.assertTrue(database.mkdirs()); - try (DBInterface normal = DbTool.getDB(database.toString(), NORMAL_DB, DbTool.DbType.LevelDB); - DBInterface empty = DbTool.getDB(database.toString(), EMPTY_DB, DbTool.DbType.RocksDB)) { + try (DBInterface normal = DbTool.getDB(database.toString(), NORMAL_DB, dbType); + DBInterface empty = DbTool.getDB(database.toString(), EMPTY_DB, dbType)) { for (int i = 0; i < 10; i++) { normal.put(("" + i).getBytes(), (NORMAL_DB + "-" + i).getBytes()); } @@ -53,8 +61,7 @@ public void testRoot() throws IOException, RocksDBException { "--db", EMPTY_DB}; Assert.assertEquals(0, cli.execute(args)); - try (DBInterface errorDb = new LevelDBImpl( - DBUtils.newLevelDb(Paths.get(database.toString(), ERROR_DB)), ERROR_DB)) { + try (DBInterface errorDb = DbTool.getDB(database.toString(), ERROR_DB, dbType)) { for (int i = 0; i < 10; i++) { errorDb.put(("" + i).getBytes(), (ERROR_DB + "-" + i).getBytes()); } diff --git a/plugins/src/test/java/org/tron/plugins/DbTest.java b/plugins/src/test/java/org/tron/plugins/DbTest.java index 8605fa18d50..bbcc1a0bbf7 100644 --- a/plugins/src/test/java/org/tron/plugins/DbTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbTest.java @@ -5,42 +5,43 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.UUID; -import org.iq80.leveldb.DB; -import org.junit.Before; +import org.junit.Assert; import org.junit.Rule; import org.junit.rules.TemporaryFolder; +import org.rocksdb.RocksDBException; import org.tron.plugins.utils.ByteArray; import org.tron.plugins.utils.DBUtils; import org.tron.plugins.utils.MarketUtils; +import org.tron.plugins.utils.db.DBInterface; +import org.tron.plugins.utils.db.DbTool; import picocli.CommandLine; public class DbTest { - String INPUT_DIRECTORY; + public String INPUT_DIRECTORY; private static final String ACCOUNT = "account"; private static final String MARKET = DBUtils.MARKET_PAIR_PRICE_TO_ORDER; - CommandLine cli = new CommandLine(new Toolkit()); - String tmpDir = System.getProperty("java.io.tmpdir"); + public CommandLine cli = new CommandLine(new Toolkit()); @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); - @Before - public void init() throws IOException { + public void init(DbTool.DbType dbType) throws IOException, RocksDBException { INPUT_DIRECTORY = temporaryFolder.newFolder().toString(); - initDB(new File(INPUT_DIRECTORY, ACCOUNT)); - initDB(new File(INPUT_DIRECTORY, MARKET)); - initDB(new File(INPUT_DIRECTORY, DBUtils.CHECKPOINT_DB_V2)); + initDB(INPUT_DIRECTORY, ACCOUNT, dbType); + initDB(INPUT_DIRECTORY, MARKET, dbType); + initDB(INPUT_DIRECTORY, DBUtils.CHECKPOINT_DB_V2, dbType); } - private static void initDB(File file) throws IOException { - if (DBUtils.CHECKPOINT_DB_V2.equalsIgnoreCase(file.getName())) { - File dbFile = new File(file, DBUtils.CHECKPOINT_DB_V2); + private static void initDB(String sourceDir, String dbName, DbTool.DbType dbType) + throws IOException, RocksDBException { + if (DBUtils.CHECKPOINT_DB_V2.equalsIgnoreCase(dbName)) { + File dbFile = new File(sourceDir, DBUtils.CHECKPOINT_DB_V2); if (dbFile.mkdirs()) { for (int i = 0; i < 3; i++) { - try (DB db = DBUtils.newLevelDb(Paths.get(dbFile.getPath(), - System.currentTimeMillis() + ""))) { + try (DBInterface db = DbTool.getDB(dbFile.getPath(), + System.currentTimeMillis() + "", dbType)) { for (int j = 0; j < 100; j++) { byte[] bytes = UUID.randomUUID().toString().getBytes(); db.put(bytes, bytes); @@ -50,8 +51,8 @@ private static void initDB(File file) throws IOException { } return; } - try (DB db = DBUtils.newLevelDb(file.toPath())) { - if (MARKET.equalsIgnoreCase(file.getName())) { + try (DBInterface db = DbTool.getDB(sourceDir, dbName, dbType)) { + if (MARKET.equalsIgnoreCase(dbName)) { byte[] sellTokenID1 = ByteArray.fromString("100"); byte[] buyTokenID1 = ByteArray.fromString("200"); byte[] pairPriceKey1 = MarketUtils.createPairPriceKey( @@ -73,26 +74,29 @@ private static void initDB(File file) throws IOException { 2003L ); - //Use out-of-order insertion,key in store should be 1,2,3 db.put(pairPriceKey1, "1".getBytes(StandardCharsets.UTF_8)); db.put(pairPriceKey2, "2".getBytes(StandardCharsets.UTF_8)); db.put(pairPriceKey3, "3".getBytes(StandardCharsets.UTF_8)); + Assert.assertEquals(3, db.size()); } else { for (int i = 0; i < 100; i++) { byte[] bytes = UUID.randomUUID().toString().getBytes(); db.put(bytes, bytes); } + Assert.assertEquals(100, db.size()); } } } /** * Generate a not-exist temporary directory path. + * * @return temporary path */ - public String genarateTmpDir() { - File dir = Paths.get(tmpDir, UUID.randomUUID().toString()).toFile(); + public String generateTmpDir() throws IOException { + File dir = Paths.get(temporaryFolder.newFolder().toString(), UUID.randomUUID().toString()) + .toFile(); dir.deleteOnExit(); return dir.getPath(); } diff --git a/plugins/src/test/java/org/tron/plugins/ArchiveManifestTest.java b/plugins/src/test/java/org/tron/plugins/leveldb/ArchiveManifestTest.java similarity index 69% rename from plugins/src/test/java/org/tron/plugins/ArchiveManifestTest.java rename to plugins/src/test/java/org/tron/plugins/leveldb/ArchiveManifestTest.java index 4fd9e537d05..f5880d82e39 100644 --- a/plugins/src/test/java/org/tron/plugins/ArchiveManifestTest.java +++ b/plugins/src/test/java/org/tron/plugins/leveldb/ArchiveManifestTest.java @@ -1,6 +1,4 @@ -package org.tron.plugins; - -import static org.iq80.leveldb.impl.Iq80DBFactory.factory; +package org.tron.plugins.leveldb; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -14,47 +12,41 @@ import java.nio.charset.StandardCharsets; import java.util.Properties; import java.util.UUID; - import lombok.extern.slf4j.Slf4j; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.RocksDBException; +import org.tron.plugins.ArchiveManifest; +import org.tron.plugins.utils.DBUtils; +import org.tron.plugins.utils.db.DbTool; @Slf4j public class ArchiveManifestTest { - private static final String OUTPUT_DIRECTORY = "output-directory/database/archiveManifest"; + @ClassRule + public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static String OUTPUT_DIRECTORY; - private static final String ENGINE = "ENGINE"; - private static final String LEVELDB = "LEVELDB"; - private static final String ROCKSDB = "ROCKSDB"; private static final String ACCOUNT = "account"; private static final String ACCOUNT_ROCKSDB = "account-rocksdb"; - private static final String MARKET = "market_pair_price_to_order"; - private static final String ENGINE_FILE = "engine.properties"; - @BeforeClass - public static void init() throws IOException { - File file = new File(OUTPUT_DIRECTORY,ACCOUNT); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,LEVELDB); - - file = new File(OUTPUT_DIRECTORY,MARKET); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,LEVELDB); + public static void init() throws IOException, RocksDBException { + OUTPUT_DIRECTORY = temporaryFolder.newFolder("database").toString(); + File file = new File(OUTPUT_DIRECTORY, ACCOUNT); + DbTool.openLevelDb(file.toPath(),ACCOUNT).close(); - file = new File(OUTPUT_DIRECTORY,ACCOUNT_ROCKSDB); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,ROCKSDB); + file = new File(OUTPUT_DIRECTORY, DBUtils.MARKET_PAIR_PRICE_TO_ORDER); + DbTool.openLevelDb(file.toPath(), DBUtils.MARKET_PAIR_PRICE_TO_ORDER).close(); - } + file = new File(OUTPUT_DIRECTORY, ACCOUNT_ROCKSDB); + DbTool.openRocksDb(file.toPath(), ACCOUNT_ROCKSDB).close(); - @AfterClass - public static void destroy() { - deleteDir(new File(OUTPUT_DIRECTORY)); } @Test diff --git a/plugins/src/test/java/org/tron/plugins/DbArchiveTest.java b/plugins/src/test/java/org/tron/plugins/leveldb/DbArchiveTest.java similarity index 71% rename from plugins/src/test/java/org/tron/plugins/DbArchiveTest.java rename to plugins/src/test/java/org/tron/plugins/leveldb/DbArchiveTest.java index 10bed418764..69dca01e4f8 100644 --- a/plugins/src/test/java/org/tron/plugins/DbArchiveTest.java +++ b/plugins/src/test/java/org/tron/plugins/leveldb/DbArchiveTest.java @@ -1,6 +1,4 @@ -package org.tron.plugins; - -import static org.iq80.leveldb.impl.Iq80DBFactory.factory; +package org.tron.plugins.leveldb; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -12,48 +10,43 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; import java.util.Properties; import java.util.UUID; import lombok.extern.slf4j.Slf4j; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.RocksDBException; +import org.tron.plugins.Toolkit; +import org.tron.plugins.utils.DBUtils; +import org.tron.plugins.utils.db.DbTool; import picocli.CommandLine; @Slf4j public class DbArchiveTest { - private static final String OUTPUT_DIRECTORY = "output-directory/database/dbArchive"; + @ClassRule + public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static String OUTPUT_DIRECTORY; - private static final String ENGINE = "ENGINE"; - private static final String LEVELDB = "LEVELDB"; - private static final String ROCKSDB = "ROCKSDB"; private static final String ACCOUNT = "account"; private static final String ACCOUNT_ROCKSDB = "account-rocksdb"; - private static final String MARKET = "market_pair_price_to_order"; - private static final String ENGINE_FILE = "engine.properties"; @BeforeClass - public static void init() throws IOException { - File file = new File(OUTPUT_DIRECTORY,ACCOUNT); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,LEVELDB); + public static void init() throws IOException, RocksDBException { + OUTPUT_DIRECTORY = temporaryFolder.newFolder("database").toString(); + File file = new File(OUTPUT_DIRECTORY, ACCOUNT); + DbTool.openLevelDb(file.toPath(),ACCOUNT).close(); - file = new File(OUTPUT_DIRECTORY,MARKET); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,LEVELDB); + file = new File(OUTPUT_DIRECTORY, DBUtils.MARKET_PAIR_PRICE_TO_ORDER); + DbTool.openLevelDb(file.toPath(), DBUtils.MARKET_PAIR_PRICE_TO_ORDER).close(); - file = new File(OUTPUT_DIRECTORY,ACCOUNT_ROCKSDB); - factory.open(file,ArchiveManifest.newDefaultLevelDbOptions()).close(); - writeProperty(file.toString() + File.separator + ENGINE_FILE,ENGINE,ROCKSDB); - - } + file = new File(OUTPUT_DIRECTORY, ACCOUNT_ROCKSDB); + DbTool.openRocksDb(file.toPath(), ACCOUNT_ROCKSDB).close(); - @AfterClass - public static void destroy() { - deleteDir(new File(OUTPUT_DIRECTORY)); } @Test diff --git a/plugins/src/test/java/org/tron/plugins/DbConvertTest.java b/plugins/src/test/java/org/tron/plugins/leveldb/DbConvertTest.java similarity index 76% rename from plugins/src/test/java/org/tron/plugins/DbConvertTest.java rename to plugins/src/test/java/org/tron/plugins/leveldb/DbConvertTest.java index 150e47c9f65..d24604f0a0b 100644 --- a/plugins/src/test/java/org/tron/plugins/DbConvertTest.java +++ b/plugins/src/test/java/org/tron/plugins/leveldb/DbConvertTest.java @@ -1,27 +1,25 @@ -package org.tron.plugins; +package org.tron.plugins.leveldb; import java.io.IOException; import java.util.UUID; import org.junit.Assert; import org.junit.Test; +import org.rocksdb.RocksDBException; +import org.tron.plugins.DbTest; +import org.tron.plugins.Toolkit; +import org.tron.plugins.utils.db.DbTool; import picocli.CommandLine; public class DbConvertTest extends DbTest { @Test - public void testRun() throws IOException { + public void testRun() throws IOException, RocksDBException { + init(DbTool.DbType.LevelDB); String[] args = new String[] { "db", "convert", INPUT_DIRECTORY, temporaryFolder.newFolder().toString() }; Assert.assertEquals(0, cli.execute(args)); } - @Test - public void testRunWithSafe() throws IOException { - String[] args = new String[] { "db", "convert", INPUT_DIRECTORY, - temporaryFolder.newFolder().toString(),"--safe" }; - Assert.assertEquals(0, cli.execute(args)); - } - @Test public void testHelp() { String[] args = new String[] {"db", "convert", "-h"}; diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteLevelDbTest.java b/plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbTest.java similarity index 76% rename from plugins/src/test/java/org/tron/plugins/DbLiteLevelDbTest.java rename to plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbTest.java index 792f93ad197..7666806e2b5 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteLevelDbTest.java +++ b/plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbTest.java @@ -1,7 +1,8 @@ -package org.tron.plugins; +package org.tron.plugins.leveldb; import java.io.IOException; import org.junit.Test; +import org.tron.plugins.DbLiteTest; public class DbLiteLevelDbTest extends DbLiteTest { diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteLevelDbV2Test.java b/plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbV2Test.java similarity index 76% rename from plugins/src/test/java/org/tron/plugins/DbLiteLevelDbV2Test.java rename to plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbV2Test.java index ae48e1d66e9..de32ae29c7c 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteLevelDbV2Test.java +++ b/plugins/src/test/java/org/tron/plugins/leveldb/DbLiteLevelDbV2Test.java @@ -1,7 +1,8 @@ -package org.tron.plugins; +package org.tron.plugins.leveldb; import java.io.IOException; import org.junit.Test; +import org.tron.plugins.DbLiteTest; public class DbLiteLevelDbV2Test extends DbLiteTest { diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteRocksDbTest.java b/plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbTest.java similarity index 76% rename from plugins/src/test/java/org/tron/plugins/DbLiteRocksDbTest.java rename to plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbTest.java index e6910b1103a..2f9c92f9679 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteRocksDbTest.java +++ b/plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbTest.java @@ -1,7 +1,8 @@ -package org.tron.plugins; +package org.tron.plugins.rocksdb; import java.io.IOException; import org.junit.Test; +import org.tron.plugins.DbLiteTest; public class DbLiteRocksDbTest extends DbLiteTest { diff --git a/plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbV2Test.java b/plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbV2Test.java new file mode 100644 index 00000000000..ab1067fefc3 --- /dev/null +++ b/plugins/src/test/java/org/tron/plugins/rocksdb/DbLiteRocksDbV2Test.java @@ -0,0 +1,13 @@ +package org.tron.plugins.rocksdb; + +import java.io.IOException; +import org.junit.Test; +import org.tron.plugins.DbLiteTest; + +public class DbLiteRocksDbV2Test extends DbLiteTest { + + @Test + public void testToolsWithRocksDB() throws InterruptedException, IOException { + testTools("ROCKSDB", 2); + } +} diff --git a/plugins/src/test/resources/config.conf b/plugins/src/test/resources/config.conf index 2bfca7dbdd7..77d15d521eb 100644 --- a/plugins/src/test/resources/config.conf +++ b/plugins/src/test/resources/config.conf @@ -1,11 +1,5 @@ storage { - # Directory for storing persistent data - db.engine = "LEVELDB", - db.sync = false, - db.directory = "database", - index.directory = "index", - transHistory.switch = "on", properties = [ { name = "account", diff --git a/protocol/build.gradle b/protocol/build.gradle index 535fac43a65..789d27b6360 100644 --- a/protocol/build.gradle +++ b/protocol/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.google.protobuf' -def protobufVersion = '3.25.5' -def grpcVersion = '1.60.0' +def protobufVersion = '3.25.8' +def grpcVersion = '1.75.0' dependencies { api group: 'com.google.protobuf', name: 'protobuf-java', version: protobufVersion @@ -13,6 +13,7 @@ dependencies { api group: 'io.grpc', name: 'grpc-netty', version: grpcVersion api group: 'io.grpc', name: 'grpc-protobuf', version: grpcVersion api group: 'io.grpc', name: 'grpc-stub', version: grpcVersion + api group: 'io.grpc', name: 'grpc-core', version: grpcVersion api group: 'io.grpc', name: 'grpc-services', version: grpcVersion // end google grpc @@ -45,7 +46,7 @@ protobuf { plugins { grpc { - artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" + artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.archInfo.requires.ProtocGenVersion}" } } generateProtoTasks { diff --git a/protocol/src/main/protos/api/api.proto b/protocol/src/main/protos/api/api.proto index 2505fa48d6f..a67113cb606 100644 --- a/protocol/src/main/protos/api/api.proto +++ b/protocol/src/main/protos/api/api.proto @@ -499,6 +499,8 @@ service Wallet { }; }; + rpc GetPaginatedNowWitnessList (PaginatedMessage) returns (WitnessList) { + }; rpc GetDelegatedResource (DelegatedResourceMessage) returns (DelegatedResourceList) { }; @@ -808,6 +810,10 @@ service WalletSolidity { } }; }; + + rpc GetPaginatedNowWitnessList (PaginatedMessage) returns (WitnessList) { + }; + rpc GetAssetIssueList (EmptyMessage) returns (AssetIssueList) { option (google.api.http) = { post: "/walletsolidity/getassetissuelist" diff --git a/protocol/src/main/protos/core/Tron.proto b/protocol/src/main/protos/core/Tron.proto index 2ffefbf9f3e..2b104b86d34 100644 --- a/protocol/src/main/protos/core/Tron.proto +++ b/protocol/src/main/protos/core/Tron.proto @@ -132,7 +132,7 @@ message ChainParameters { message Account { /* frozen balance */ message Frozen { - int64 frozen_balance = 1; // the frozen trx balance + int64 frozen_balance = 1; // the frozen trx or asset balance int64 expire_time = 2; // the expire time } // account nick name @@ -601,7 +601,7 @@ enum ReasonCode { CONNECT_FAIL = 0x21; TOO_MANY_PEERS_WITH_SAME_IP = 0x22; LIGHT_NODE_SYNC_FAIL = 0x23; - BELOW_THAN_ME = 0X24; + BELOW_THAN_ME = 0x24; NOT_WITNESS = 0x25; NO_SUCH_MESSAGE = 0x26; UNKNOWN = 0xFF; diff --git a/protocol/src/main/protos/core/contract/asset_issue_contract.proto b/protocol/src/main/protos/core/contract/asset_issue_contract.proto index eb86b170219..9e8ff463d52 100644 --- a/protocol/src/main/protos/core/contract/asset_issue_contract.proto +++ b/protocol/src/main/protos/core/contract/asset_issue_contract.proto @@ -10,7 +10,7 @@ message AssetIssueContract { string id = 41; message FrozenSupply { - int64 frozen_amount = 1; + int64 frozen_amount = 1; // asset amount int64 frozen_days = 2; } bytes owner_address = 1; @@ -18,7 +18,7 @@ message AssetIssueContract { bytes abbr = 3; int64 total_supply = 4; repeated FrozenSupply frozen_supply = 5; - int32 trx_num = 6; + int32 trx_num = 6; // The fields trx_num and num define the exchange rate: num tokens can be purchased with trx_num TRX. This avoids using decimals. int32 precision = 7; int32 num = 8; int64 start_time = 9; diff --git a/settings.gradle b/settings.gradle index eb304444378..af32bfca702 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,4 +8,5 @@ include 'common' include 'example:actuator-example' include 'crypto' include 'plugins' +include 'platform' diff --git a/start.sh.simple b/start.sh.simple new file mode 100644 index 00000000000..52548dea62b --- /dev/null +++ b/start.sh.simple @@ -0,0 +1,192 @@ +#!/bin/bash +############################################################################# +# +# GNU LESSER GENERAL PUBLIC LICENSE +# Version 3, 29 June 2007 +# +# Copyright (C) [2007] [TRON Foundation], Inc. +# Everyone is permitted to copy and distribute verbatim copies +# of this license document, but changing it is not allowed. +# +# +# This version of the GNU Lesser General Public License incorporates +# the terms and conditions of version 3 of the GNU General Public +# License, supplemented by the additional permissions listed below. +# +# You can find java-tron at https://github.com/tronprotocol/java-tron/ +# +############################################################################## +# TRON Full Node Management Simple Script +# +# NOTE: This is a simple and concise script to start and stop the java-tron full node, +# designed for developers to quickly get started and learn. +# It may not be suitable for production environments. +# +# Usage: +# sh start.sh # Start the java-tron FullNode +# sh start.sh -s # Stop the java-tron FullNode +# sh start.sh [options] # Start with additional java-tron options,such as: -c config.conf -d /path_to_data, etc. +# +############################################################################## + + +# adjust JVM start +# Set the maximum heap size to 9G, adjust as needed +VM_XMX="9G" +# adjust JVM end + +FULL_NODE_JAR="FullNode.jar" +FULL_START_OPT=() +PID="" +MAX_STOP_TIME=60 +JAVACMD="" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +log() { + local level="$1"; shift + local timestamp color="" + timestamp=$(date '+%Y-%m-%d %H:%M:%S') + case "$level" in + INFO) color="$GREEN" ;; + WARN) color="$YELLOW" ;; + ERROR) color="$RED" ;; + esac + printf "%b[%s] [%s]:%b %s\n" "$color" "$timestamp" "$level" "$NC" "$*" | tee -a "${SCRIPT_DIR}/start.log" +} + +info() { log INFO "$@"; } +warn() { log WARN "$@"; } +error() { log ERROR "$@"; } +die() { error "$@"; exit 1; } + +ulimit -n 65535 || warn "Failed to set ulimit -n 65535" + +findJava() { + if [ -n "${JAVA_HOME:-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + [ -x "$JAVACMD" ] || die "JAVA_HOME is invalid: $JAVA_HOME" + else + JAVACMD="java" + which java >/dev/null 2>&1 || die "JAVA_HOME not set and no 'java' in PATH" + fi + "$JAVACMD" -version > /dev/null 2>&1 || die "Java command not working" +} + +checkPid() { + # shellcheck disable=SC2009 + PID=$(ps -ef |grep $FULL_NODE_JAR |grep -v grep |awk '{print $2}') +} + + +stopService() { + checkPid + + if ! kill -0 "$PID" 2>/dev/null; then + info "java-tron is not running." + return 0 + fi + info "Stopping java-tron service (PID: $PID)" + + local count=1 + + while [ -n "$PID" ] && [ $count -le $MAX_STOP_TIME ]; do + kill -TERM "$PID" 2>/dev/null && info "Sent SIGTERM to java-tron (PID: $PID), attempt $count" + sleep 1 + checkPid + count=$((count + 1)) + done + + if [ -n "$PID" ]; then + warn "Forcing kill java-tron (PID: $PID) after $MAX_STOP_TIME seconds" + kill -KILL "$PID" 2>/dev/null + sleep 1 + checkPid + fi + + if [ -n "$PID" ]; then + die "Failed to stop the service (PID: $PID)" + else + info "java-tron stopped" + wait_with_info 2 "Cleaning up..." + fi +} + +startService() { + if [ -n "${FULL_START_OPT[*]}" ]; then + info "Starting java-tron service with options: ${FULL_START_OPT[*]}" + fi + if [ ! -f "$FULL_NODE_JAR" ]; then + die "$FULL_NODE_JAR not found in path $SCRIPT_DIR." + fi + + nohup "$JAVACMD" \ + -Xmx"$VM_XMX" \ + -XX:+UseZGC \ + -Xlog:gc,gc+heap:file=gc.log:time,tags,level:filecount=10,filesize=100M \ + -XX:ReservedCodeCacheSize=256m \ + -XX:+UseCodeCacheFlushing \ + -XX:MetaspaceSize=256m \ + -XX:MaxMetaspaceSize=512m \ + -XX:MaxDirectMemorySize=1g \ + -XX:+HeapDumpOnOutOfMemoryError \ + -jar "$FULL_NODE_JAR" "${FULL_START_OPT[@]}" \ + >> start.log 2>&1 & + + + info "Waiting for the service to start..." + wait_with_info 5 "Starting..." + + checkPid + + if [ -n "$PID" ]; then + info "Started java-tron with PID $PID on $HOSTNAME." + else + die "Failed to start java-tron, see start.log or logs/tron.log for details." + fi +} + +wait_with_info() { + local seconds=$1 + local message=$2 + for i in $(seq "$seconds" -1 1); do + info "$message wait ($i) s" + sleep 1 + done +} + + +start() { + checkPid + if [ -n "$PID" ]; then + info "java-tron is already running (PID: $PID), to stop the service: sh start.sh -s" + return + fi + findJava + startService +} + +while [ -n "$1" ]; do + case "$1" in + -s) + stopService + exit 0 + ;; + *) + FULL_START_OPT+=("$@") + break + ;; + esac +done + +start + +exit 0 \ No newline at end of file From 87baadabca951981c1188abcf548de9dfafb36a3 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 4 Feb 2026 11:33:39 +0800 Subject: [PATCH 04/57] update a new version. version name:GreatVoyage-v4.8.0.1-1-g44a4bc8263,version code:18636 (#6542) --- framework/src/main/java/org/tron/program/Version.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index af3bcd8e185..de3f91f0a5c 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -2,8 +2,8 @@ public class Version { - public static final String VERSION_NAME = "GreatVoyage-v4.8.0-1-g45e3bf88ca"; - public static final String VERSION_CODE = "18634"; + public static final String VERSION_NAME = "GreatVoyage-v4.8.0.1-1-g44a4bc8263"; + public static final String VERSION_CODE = "18636"; private static final String VERSION = "4.8.1"; public static String getVersion() { From 4036f03f87c2d73ae6849138b95bae0825746f2a Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Sun, 10 May 2026 16:09:36 +0800 Subject: [PATCH 05/57] fix(config): fix the issue where --es fails to start (#6757) * fix(config): remove redundant JSON-RPC config fields and consolidate parameter binding Remove maxBlockFilterNum, maxAddressSize, and maxRequestTimeout from NodeConfig/CommonParameter since they were superseded by the jsonRpcMaxBatchSize/jsonRpcMaxResponseSize parameters. Consolidate the config binding path so all JSON-RPC size limits flow through a single reference.conf entry, and clean up stale test-config fixtures. * fix bug of NodeConfigTest * remove allowShieldedTransactionApi from reference.conf * add testcase of external.ip is null * change comment * fix the bug of --es and 7 failed testcases --- .../java/org/tron/core/config/args/Args.java | 40 +++++++++++-------- .../tron/core/net/peer/PeerConnection.java | 3 +- .../org/tron/core/config/args/ArgsTest.java | 18 +++++++++ .../ChainInventoryMsgHandlerTest.java | 14 +++++++ 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 2d6660f9a6a..7cc1c5b870b 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -167,16 +167,19 @@ public static void setParam(final String[] args, final String confFileName) { ? cmd.shellConfFileName : confFileName; Config config = Configuration.getByFileName(configFilePath); - // 2. Config overrides defaults + // 2. Config overrides defaults (event config bean is read here but not yet applied) applyConfigParams(config); - // 3. CLI overrides Config (highest priority) + // 3. CLI overrides Config (highest priority, including --es → eventSubscribe) applyCLIParams(cmd, jc); - // 4. Apply platform constraints (e.g. ARM64 forces RocksDB) + // 4. Apply event config after CLI + applyEventConfig(eventConfig); + + // 5. Apply platform constraints (e.g. ARM64 forces RocksDB) applyPlatformConstraints(); - // 5. Init witness (depends on CLI witness flag) + // 6. Init witness (depends on CLI witness flag) initLocalWitnesses(config, cmd); } @@ -217,7 +220,7 @@ private static void applyStorageConfig(StorageConfig sc) { PARAMETER.storage.setIndexSwitch( org.apache.commons.lang3.StringUtils.isNotEmpty(indexSwitch) ? indexSwitch : "on"); PARAMETER.storage.setTransactionHistorySwitch(sc.getTransHistory().getSwitch()); - // contractParse is set in applyEventConfig — it belongs to event.subscribe domain + // contractParse is set in applyConfigParams alongside event config, not here PARAMETER.storage.setCheckpointVersion(sc.getCheckpoint().getVersion()); PARAMETER.storage.setCheckpointSync(sc.getCheckpoint().isSync()); @@ -343,21 +346,21 @@ private static void applyRateLimiterConfig(RateLimiterConfig rl) { PARAMETER.rateLimiterInitialization = initialization; } + /** + * Package-private entry point only for tests + */ + static void applyEventConfig() { + applyEventConfig(eventConfig); + } + /** * Bridge EventConfig bean values to CommonParameter fields. * Converts EventConfig (raw bean) into EventPluginConfig and FilterQuery (business objects). */ private static void applyEventConfig(EventConfig ec) { - PARAMETER.eventSubscribe = ec.isEnable(); - // contractParse belongs to event.subscribe but Storage object holds it - PARAMETER.storage.setContractParseSwitch(ec.isContractParse()); - - // PARAMETER.eventPluginConfig and PARAMETER.eventFilter are only consumed by - // Manager.startEventSubscribing(), which itself is gated by isEventSubscribe() - // (= ec.isEnable()) at Manager.java:564. When subscribe is disabled, building - // these objects has no observable effect — skip both early so PARAMETER stays - // consistent with the runtime intent. - if (!ec.isEnable()) { + // cmd parameter has higher priority + PARAMETER.eventSubscribe = PARAMETER.eventSubscribe || ec.isEnable(); + if (!PARAMETER.eventSubscribe) { return; } @@ -770,9 +773,12 @@ public static void applyConfigParams( // node.shutdown — handled in applyNodeConfig - // Event config: bind from config.conf "event.subscribe" section + // Event config: read bean here; applyEventConfig() is called once in setParam() + // after applyCLIParams() so that --es is already reflected in eventSubscribe. eventConfig = EventConfig.fromConfig(config); - applyEventConfig(eventConfig); + // contractParse is event-domain but must be set from config before CLI can + // override it with --contract-parse-enable (which runs in applyCLIParams). + PARAMETER.storage.setContractParseSwitch(eventConfig.isContractParse()); logConfig(); } diff --git a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java index 8d7818d1608..7d7457cf2fc 100644 --- a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java +++ b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java @@ -170,7 +170,8 @@ public class PeerConnection { public void setChannel(Channel channel) { this.channel = channel; - if (relayNodes.stream().anyMatch(n -> n.getAddress().equals(channel.getInetAddress()))) { + if (relayNodes != null + && relayNodes.stream().anyMatch(n -> n.getAddress().equals(channel.getInetAddress()))) { this.isRelayPeer = true; } this.nodeStatistics = TronStatsManager.getNodeStatistics(channel.getInetAddress()); diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 4b6b7ad0a7a..3ae5677fbda 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -344,6 +344,21 @@ public void testCliEsOverridesConfig() { Args.clearParam(); } + /** + * Regression: when --es is the sole source of event.subscribe.enable=true + * (config has it disabled), eventPluginConfig must be built. + * Previously applyEventConfig() ran before applyCLIParams() and returned + * early (both flags false), leaving eventPluginConfig=null; Manager then + * called EventPluginLoader.start(null) and threw "Failed to load eventPlugin." + */ + @Test + public void testCliEsBuildsEventPluginConfig() { + Args.setParam(new String[] {"--es"}, TestConstants.TEST_CONF); + Assert.assertTrue(Args.getInstance().isEventSubscribe()); + Assert.assertNotNull(Args.getInstance().getEventPluginConfig()); + Args.clearParam(); + } + /** * Verify that config file storage values are applied when no CLI override is present. * @@ -454,6 +469,7 @@ public void testEventConfigDisabledSkipsEpcAndFilter() { Config config = ConfigFactory.parseMap(override) .withFallback(ConfigFactory.defaultReference()); Args.applyConfigParams(config); + Args.applyEventConfig(); Assert.assertNull(Args.getInstance().getEventPluginConfig()); Assert.assertNull(Args.getInstance().getEventFilter()); Args.clearParam(); @@ -467,6 +483,7 @@ public void testEventConfigEnabledBuildsEpcAndFilter() { Config config = ConfigFactory.parseMap(override) .withFallback(ConfigFactory.defaultReference()); Args.applyConfigParams(config); + Args.applyEventConfig(); Assert.assertNotNull(Args.getInstance().getEventPluginConfig()); Assert.assertNotNull(Args.getInstance().getEventFilter()); Args.clearParam(); @@ -481,6 +498,7 @@ public void testEventConfigEnabledWithInvalidFromBlockLeavesFilterNull() { Config config = ConfigFactory.parseMap(override) .withFallback(ConfigFactory.defaultReference()); Args.applyConfigParams(config); + Args.applyEventConfig(); // epc still built; filter rejected Assert.assertNotNull(Args.getInstance().getEventPluginConfig()); Assert.assertNull(Args.getInstance().getEventFilter()); diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/ChainInventoryMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/ChainInventoryMsgHandlerTest.java index dab76cfcb46..56853c3dbb7 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/ChainInventoryMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/ChainInventoryMsgHandlerTest.java @@ -3,11 +3,15 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; +import org.tron.common.TestConstants; import org.tron.common.utils.Pair; import org.tron.core.capsule.BlockCapsule.BlockId; import org.tron.core.config.Parameter.NetConstants; +import org.tron.core.config.args.Args; import org.tron.core.exception.P2pException; import org.tron.core.net.message.keepalive.PingMessage; import org.tron.core.net.message.sync.ChainInventoryMessage; @@ -15,6 +19,16 @@ public class ChainInventoryMsgHandlerTest { + @BeforeClass + public static void init() { + Args.setParam(new String[]{}, TestConstants.TEST_CONF); + } + + @AfterClass + public static void destroy() { + Args.clearParam(); + } + private ChainInventoryMsgHandler handler = new ChainInventoryMsgHandler(); private PeerConnection peer = new PeerConnection(); private ChainInventoryMessage msg = new ChainInventoryMessage(new ArrayList<>(), 0L); From 0a26d23ef31380f29c20abb36487b2d27674f528 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Sun, 10 May 2026 20:46:03 +0800 Subject: [PATCH 06/57] restore useNativeQueue to false default (#6759) --- common/src/main/java/org/tron/core/config/args/EventConfig.java | 2 +- common/src/main/resources/reference.conf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/tron/core/config/args/EventConfig.java b/common/src/main/java/org/tron/core/config/args/EventConfig.java index ac1731de2dc..43707956845 100644 --- a/common/src/main/java/org/tron/core/config/args/EventConfig.java +++ b/common/src/main/java/org/tron/core/config/args/EventConfig.java @@ -43,7 +43,7 @@ public class EventConfig { @Getter @Setter public static class NativeConfig { - private boolean useNativeQueue = true; + private boolean useNativeQueue = false; private int bindport = 5555; private int sendqueuelength = 1000; } diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 688e1590788..1e68da051e0 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -758,7 +758,7 @@ event.subscribe = { enable = false native = { - useNativeQueue = true + useNativeQueue = false bindport = 5555 sendqueuelength = 1000 } From 9d28fa79ebcd445e22f14b600587a1caba5ace77 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Mon, 11 May 2026 16:33:00 +0800 Subject: [PATCH 07/57] fix(event): reject incompatible event-plugin versions below 3.0.0 (#6760) Pre-3.0.0(The previous event-plugin public release is 2.2.0) event-plugin builds still link against com.alibaba.fastjson, which was removed from java-tron in #6701. When such a plugin is loaded, the NoClassDefFoundError surfaces inside the plugin's own worker thread and is swallowed by its catch(Throwable) handler. The node keeps running while silently dropping every trigger, leaving operators with no signal that the event subscription is broken. Enforce a minimum Plugin-Version at startup in EventPluginLoader.startPlugin using pf4j's VersionManager (semver). When the descriptor version is below 3.0.0, log a clear upgrade hint and return false; the existing call chain in Manager.startEventSubscribing wraps that into TronError(EVENT_SUBSCRIBE_INIT) and aborts node startup instead of silently degrading. --- .../common/logsfilter/EventPluginLoader.java | 32 +++++++++++++++++++ .../common/logsfilter/EventLoaderTest.java | 31 ++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java b/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java index 7061b2e9d57..c0b7afd6779 100644 --- a/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java +++ b/framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java @@ -3,6 +3,7 @@ import com.beust.jcommander.internal.Sets; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Strings; import java.io.File; import java.util.HashSet; import java.util.List; @@ -14,8 +15,10 @@ import org.bouncycastle.util.encoders.Hex; import org.pf4j.CompoundPluginDescriptorFinder; import org.pf4j.DefaultPluginManager; +import org.pf4j.DefaultVersionManager; import org.pf4j.ManifestPluginDescriptorFinder; import org.pf4j.PluginManager; +import org.pf4j.VersionManager; import org.springframework.util.StringUtils; import org.tron.common.logsfilter.nativequeue.NativeMessageQueue; import org.tron.common.logsfilter.trigger.BlockLogTrigger; @@ -29,6 +32,16 @@ @Slf4j public class EventPluginLoader { + /** + * Minimum event-plugin Plugin-Version compatible with this node. Bumped to 3.0.0 to + * reject pre-fastjson-removal builds whose worker threads would fail with + * NoClassDefFoundError on com.alibaba.fastjson at runtime. The previous event-plugin + * release is 2.2.0, so 3.0.0 is the first version that ships the Jackson replacement. + */ + static final String MIN_PLUGIN_VERSION = "3.0.0"; + + private static final VersionManager VERSION_MANAGER = new DefaultVersionManager(); + private static EventPluginLoader instance; private long MAX_PENDING_SIZE = 50000; @@ -457,6 +470,10 @@ protected CompoundPluginDescriptorFinder createPluginDescriptorFinder() { return false; } + if (!isPluginVersionSupported(pluginManager, pluginId)) { + return false; + } + pluginManager.startPlugins(); eventListeners = pluginManager.getExtensions(IPluginEventListener.class); @@ -471,6 +488,21 @@ protected CompoundPluginDescriptorFinder createPluginDescriptorFinder() { return true; } + static boolean isPluginVersionSupported(PluginManager pm, String pluginId) { + String pluginVersion = pm.getPlugin(pluginId).getDescriptor().getVersion(); + if (Strings.isNullOrEmpty(pluginVersion)) { + return false; + } + boolean isSupported = VERSION_MANAGER.compareVersions(pluginVersion, MIN_PLUGIN_VERSION) >= 0; + + if (!isSupported) { + logger.error( + "event-plugin '{}' version {} is older than required {}, please upgrade event-plugin", + pluginId, pluginVersion, MIN_PLUGIN_VERSION); + } + return isSupported; + } + public void stopPlugin() { if (Objects.nonNull(pluginManager)) { pluginManager.stopPlugins(); diff --git a/framework/src/test/java/org/tron/common/logsfilter/EventLoaderTest.java b/framework/src/test/java/org/tron/common/logsfilter/EventLoaderTest.java index 1e5268ddeb6..958af4f7b7b 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/EventLoaderTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/EventLoaderTest.java @@ -3,11 +3,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Test; +import org.pf4j.PluginDescriptor; +import org.pf4j.PluginManager; +import org.pf4j.PluginWrapper; import org.tron.common.logsfilter.trigger.BlockLogTrigger; import org.tron.common.logsfilter.trigger.TransactionLogTrigger; @@ -48,6 +53,32 @@ public void launchNativeQueue() { EventPluginLoader.getInstance().stopPlugin(); } + @Test + public void testIsPluginVersionSupported() { + assertEquals("3.0.0", EventPluginLoader.MIN_PLUGIN_VERSION); + // last releases before fastjson removal — must be rejected + assertFalse(checkVersion("1.0.0")); + assertFalse(checkVersion("2.2.0")); + assertFalse(checkVersion("2.9.9")); + // 3.0.0 onward — must be accepted + assertTrue(checkVersion("3.0.0")); + assertTrue(checkVersion("3.1.5")); + assertTrue(checkVersion("10.0.0")); + // empty/null version — reject + assertFalse(checkVersion("")); + assertFalse(checkVersion(null)); + } + + private static boolean checkVersion(String version) { + PluginManager pm = mock(PluginManager.class); + PluginWrapper wrapper = mock(PluginWrapper.class); + PluginDescriptor desc = mock(PluginDescriptor.class); + when(pm.getPlugin("test")).thenReturn(wrapper); + when(wrapper.getDescriptor()).thenReturn(desc); + when(desc.getVersion()).thenReturn(version); + return EventPluginLoader.isPluginVersionSupported(pm, "test"); + } + @Test public void testBlockLogTrigger() { BlockLogTrigger blt = new BlockLogTrigger(); From 5f7eeca0771536bccdd706fa2612454e8ae5eba8 Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Tue, 12 May 2026 16:38:41 +0800 Subject: [PATCH 08/57] feat(ratelimiter): add configurable blocking mode for API rate limiters (#6761) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add `rate.limiter.apiNonBlocking` switch (default false). When off, callers wait for a permit; when on, they reject immediately and shed load. - Wire the switch through `RateLimiterConfig` → `Args` → `CommonParameter`; update `reference.conf` / `config.conf`. - Add blocking `acquire()` paths on `IRateLimiter`, `GlobalRateLimiter`, and `QpsStrategy` / `IPQpsStrategy` / `GlobalPreemptibleStrategy`. Only the semaphore-based strategy is bounded (2s); the QPS-based paths use unbounded Guava `RateLimiter.acquire()`. - Introduce `acquirePermit()` dispatcher (default method on `IRateLimiter`; static on `GlobalRateLimiter`) that picks blocking vs non-blocking based on the switch. - Swap `tryAcquire` → `acquirePermit` at the `RateLimiterServlet` and `RateLimiterInterceptor` call sites; preserve per-endpoint-before-global ordering to avoid consuming global quota on per-endpoint rejection. - Extract `loadIpLimiter()` in `GlobalRateLimiter` and `loadLimiter()` in `IPQpsStrategy` to share cache+exception handling between `tryAcquire` and `acquire`. - Add unit tests covering dispatcher routing (both directions), acquire IP-before-global ordering, and IP-loader-failure fail-closed behaviour. --- .../common/parameter/CommonParameter.java | 3 + .../core/config/args/RateLimiterConfig.java | 1 + common/src/main/resources/reference.conf | 1 + .../config/args/RateLimiterConfigTest.java | 6 +- .../java/org/tron/core/config/args/Args.java | 1 + .../services/http/RateLimiterServlet.java | 7 +- .../ratelimiter/GlobalRateLimiter.java | 42 +++++-- .../ratelimiter/RateLimiterInterceptor.java | 7 +- .../adapter/DefaultBaseQqsAdapter.java | 5 + .../adapter/GlobalPreemptibleAdapter.java | 4 + .../adapter/IPQPSRateLimiterAdapter.java | 5 + .../ratelimiter/adapter/IRateLimiter.java | 8 ++ .../adapter/QpsRateLimiterAdapter.java | 5 + .../strategy/GlobalPreemptibleStrategy.java | 24 +++- .../ratelimiter/strategy/IPQpsStrategy.java | 20 +++- .../ratelimiter/strategy/QpsStrategy.java | 5 + framework/src/main/resources/config.conf | 5 +- .../services/http/RateLimiterServletTest.java | 22 ++-- .../ratelimiter/GlobalRateLimiterTest.java | 106 ++++++++++++++++++ .../RateLimiterInterceptorTest.java | 28 ++--- .../ratelimiter/adaptor/AdaptorTest.java | 61 ++++++++++ 21 files changed, 314 insertions(+), 52 deletions(-) diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 3fe1e878ffb..8bbc52e6d87 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -411,6 +411,9 @@ public class CommonParameter { @Setter public double rateLimiterDisconnect; // clearParam: 1.0 @Getter + @Setter + public boolean rateLimiterApiNonBlocking = false; + @Getter public RocksDbSettings rocksDBCustomSettings; @Getter public GenesisBlock genesisBlock; diff --git a/common/src/main/java/org/tron/core/config/args/RateLimiterConfig.java b/common/src/main/java/org/tron/core/config/args/RateLimiterConfig.java index eed5ef1898b..5eab6f6d92d 100644 --- a/common/src/main/java/org/tron/core/config/args/RateLimiterConfig.java +++ b/common/src/main/java/org/tron/core/config/args/RateLimiterConfig.java @@ -21,6 +21,7 @@ public class RateLimiterConfig { private P2pRateLimitConfig p2p = new P2pRateLimitConfig(); private List http = new ArrayList<>(); private List rpc = new ArrayList<>(); + private boolean apiNonBlocking = false; @Getter @Setter diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 1e68da051e0..3f41c556f96 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -451,6 +451,7 @@ rate.limiter = { global.qps = 50000 global.ip.qps = 10000 global.api.qps = 1000 + apiNonBlocking = false } seed.node = { diff --git a/common/src/test/java/org/tron/core/config/args/RateLimiterConfigTest.java b/common/src/test/java/org/tron/core/config/args/RateLimiterConfigTest.java index 7b4d8a87d45..c3b827a8ba4 100644 --- a/common/src/test/java/org/tron/core/config/args/RateLimiterConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/RateLimiterConfigTest.java @@ -1,6 +1,7 @@ package org.tron.core.config.args; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.typesafe.config.Config; @@ -29,6 +30,7 @@ public void testDefaults() { assertEquals(1.0, rl.getP2p().getDisconnect(), 0.001); assertTrue(rl.getHttp().isEmpty()); assertTrue(rl.getRpc().isEmpty()); + assertFalse(rl.isApiNonBlocking()); } @Test @@ -40,7 +42,8 @@ public void testFromConfig() { + " http = [{ component = TestServlet, strategy = QpsRateLimiterAdapter," + " paramString = \"qps=10\" }]," + " rpc = [{ component = TestRpc, strategy = GlobalPreemptibleAdapter," - + " paramString = \"permit=1\" }]" + + " paramString = \"permit=1\" }]," + + " apiNonBlocking = true" + "}"); RateLimiterConfig rl = RateLimiterConfig.fromConfig(config); assertEquals(100, rl.getGlobal().getQps()); @@ -50,5 +53,6 @@ public void testFromConfig() { assertEquals("TestServlet", rl.getHttp().get(0).getComponent()); assertEquals(1, rl.getRpc().size()); assertEquals("TestRpc", rl.getRpc().get(0).getComponent()); + assertTrue(rl.isApiNonBlocking()); } } diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 7cc1c5b870b..ec808267c75 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -328,6 +328,7 @@ private static void applyRateLimiterConfig(RateLimiterConfig rl) { PARAMETER.rateLimiterSyncBlockChain = rl.getP2p().getSyncBlockChain(); PARAMETER.rateLimiterFetchInvData = rl.getP2p().getFetchInvData(); PARAMETER.rateLimiterDisconnect = rl.getP2p().getDisconnect(); + PARAMETER.rateLimiterApiNonBlocking = rl.isApiNonBlocking(); // HTTP/RPC rate limiter items: convert bean lists to business objects RateLimiterInitialization initialization = new RateLimiterInitialization(); diff --git a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java index 3086cbb3619..f488c32df4c 100644 --- a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java @@ -107,9 +107,10 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) IRateLimiter rateLimiter = container.get(KEY_PREFIX_HTTP, getClass().getSimpleName()); // Check per-endpoint first to avoid consuming global IP/QPS quota for requests - // that would be rejected by the per-endpoint limiter anyway. - boolean perEndpointAcquired = rateLimiter == null || rateLimiter.tryAcquire(runtimeData); - boolean acquireResource = perEndpointAcquired && GlobalRateLimiter.tryAcquire(runtimeData); + // that would be rejected by the per-endpoint limiter anyway. acquirePermit() + // chooses blocking or non-blocking semantics based on rate.limiter.apiNonBlocking. + boolean perEndpointAcquired = rateLimiter == null || rateLimiter.acquirePermit(runtimeData); + boolean acquireResource = perEndpointAcquired && GlobalRateLimiter.acquirePermit(runtimeData); String contextPath = req.getContextPath(); String url = Strings.isNullOrEmpty(req.getServletPath()) diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/GlobalRateLimiter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/GlobalRateLimiter.java index 4b3043274d2..11c55e3a2c3 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/GlobalRateLimiter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/GlobalRateLimiter.java @@ -23,21 +23,43 @@ public class GlobalRateLimiter { public static boolean tryAcquire(RuntimeData runtimeData) { String ip = runtimeData.getRemoteAddr(); if (!Strings.isNullOrEmpty(ip)) { - RateLimiter r; - try { - // cache.get is atomic: only one loader executes per key under concurrent requests, - // preventing multiple RateLimiter instances from being created for the same IP. - r = cache.get(ip, () -> RateLimiter.create(IP_QPS)); - } catch (Exception e) { - logger.warn("Failed to load IP rate limiter for {}, denying request: {}", - ip, e.getMessage()); + RateLimiter r = loadIpLimiter(ip); + if (r == null || !r.tryAcquire()) { return false; } - if (!r.tryAcquire()) { + } + return rateLimiter.tryAcquire(); + } + + public static boolean acquire(RuntimeData runtimeData) { + String ip = runtimeData.getRemoteAddr(); + if (!Strings.isNullOrEmpty(ip)) { + RateLimiter r = loadIpLimiter(ip); + if (r == null) { return false; } + r.acquire(); + } + rateLimiter.acquire(); + return true; + } + + public static boolean acquirePermit(RuntimeData runtimeData) { + return Args.getInstance().isRateLimiterApiNonBlocking() + ? tryAcquire(runtimeData) + : acquire(runtimeData); + } + + private static RateLimiter loadIpLimiter(String ip) { + try { + // cache.get is atomic: only one loader executes per key under concurrent requests, + // preventing multiple RateLimiter instances from being created for the same IP. + return cache.get(ip, () -> RateLimiter.create(IP_QPS)); + } catch (Exception e) { + logger.warn("Failed to load IP rate limiter for {}, denying request: {}", + ip, e.getMessage()); + return null; } - return rateLimiter.tryAcquire(); } } diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/RateLimiterInterceptor.java b/framework/src/main/java/org/tron/core/services/ratelimiter/RateLimiterInterceptor.java index a07cf955828..85e94f2e768 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/RateLimiterInterceptor.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/RateLimiterInterceptor.java @@ -108,9 +108,10 @@ public Listener interceptCall(ServerCall call, RuntimeData runtimeData = new RuntimeData(call); // Check per-endpoint first to avoid consuming global IP/QPS quota for requests - // that would be rejected by the per-endpoint limiter anyway. - boolean perEndpointAcquired = rateLimiter == null || rateLimiter.tryAcquire(runtimeData); - boolean acquireResource = perEndpointAcquired && GlobalRateLimiter.tryAcquire(runtimeData); + // that would be rejected by the per-endpoint limiter anyway. acquirePermit() + // chooses blocking or non-blocking semantics based on rate.limiter.apiNonBlocking. + boolean perEndpointAcquired = rateLimiter == null || rateLimiter.acquirePermit(runtimeData); + boolean acquireResource = perEndpointAcquired && GlobalRateLimiter.acquirePermit(runtimeData); if (!acquireResource) { // Release the per-endpoint permit when global rejected, to avoid semaphore leak. diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/DefaultBaseQqsAdapter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/DefaultBaseQqsAdapter.java index 8f5b5a487bf..63d4cc77587 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/DefaultBaseQqsAdapter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/DefaultBaseQqsAdapter.java @@ -15,4 +15,9 @@ public DefaultBaseQqsAdapter(String paramString) { public boolean tryAcquire(RuntimeData data) { return strategy.tryAcquire(); } + + @Override + public boolean acquire(RuntimeData data) { + return strategy.acquire(); + } } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/GlobalPreemptibleAdapter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/GlobalPreemptibleAdapter.java index 4adc142ed28..eb85baa8b41 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/GlobalPreemptibleAdapter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/GlobalPreemptibleAdapter.java @@ -21,4 +21,8 @@ public boolean tryAcquire(RuntimeData data) { return strategy.tryAcquire(); } + @Override + public boolean acquire(RuntimeData data) { + return strategy.acquire(); + } } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IPQPSRateLimiterAdapter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IPQPSRateLimiterAdapter.java index c6fb089063a..0ebd21149a7 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IPQPSRateLimiterAdapter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IPQPSRateLimiterAdapter.java @@ -16,4 +16,9 @@ public boolean tryAcquire(RuntimeData data) { return strategy.tryAcquire(data.getRemoteAddr()); } + @Override + public boolean acquire(RuntimeData data) { + return strategy.acquire(data.getRemoteAddr()); + } + } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IRateLimiter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IRateLimiter.java index 46ed8beee92..29f7b61b6a5 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IRateLimiter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/IRateLimiter.java @@ -1,9 +1,17 @@ package org.tron.core.services.ratelimiter.adapter; +import org.tron.core.config.args.Args; import org.tron.core.services.ratelimiter.RuntimeData; public interface IRateLimiter { boolean tryAcquire(RuntimeData data); + boolean acquire(RuntimeData data); + + default boolean acquirePermit(RuntimeData data) { + return Args.getInstance().isRateLimiterApiNonBlocking() + ? tryAcquire(data) + : acquire(data); + } } diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/QpsRateLimiterAdapter.java b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/QpsRateLimiterAdapter.java index 846a5eb1c4e..62074eac885 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/QpsRateLimiterAdapter.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/adapter/QpsRateLimiterAdapter.java @@ -16,4 +16,9 @@ public boolean tryAcquire(RuntimeData data) { return strategy.tryAcquire(); } + @Override + public boolean acquire(RuntimeData data) { + return strategy.acquire(); + } + } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/GlobalPreemptibleStrategy.java b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/GlobalPreemptibleStrategy.java index 0a29183d762..e7b7f560b29 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/GlobalPreemptibleStrategy.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/GlobalPreemptibleStrategy.java @@ -3,11 +3,15 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class GlobalPreemptibleStrategy extends Strategy { public static final String STRATEGY_PARAM_PERMIT = "permit"; public static final int DEFAULT_PERMIT_NUM = 1; + public static final int DEFAULT_ACQUIRE_TIMEOUT = 2; private Semaphore sp; public GlobalPreemptibleStrategy(String paramString) { @@ -23,15 +27,25 @@ protected Map defaultParam() { return map; } - // Non-blocking: immediately rejects if no permit is available. - // Intentional change from the previous tryAcquire(2, TimeUnit.SECONDS) behaviour: - // blocking the caller for up to 2 s ties up Netty IO / gRPC executor threads and - // masks overload rather than shedding it. All rate-limiting in this stack is now - // non-blocking to keep the thread model consistent with GlobalRateLimiter. + // Non-blocking: immediately rejects if no permit is available. Used when the + // apiNonBlocking switch is on, to shed overload instead of tying up Netty IO / + // gRPC executor threads while waiting for a permit. public boolean tryAcquire() { return sp.tryAcquire(); } + public boolean acquire() { + try { + return sp.tryAcquire(DEFAULT_ACQUIRE_TIMEOUT, TimeUnit.SECONDS); + } catch (InterruptedException e) { + // Restore the interrupt flag and reject — caller must not release a permit + // it never acquired. + logger.error("acquire permit with error: {}", e.getMessage()); + Thread.currentThread().interrupt(); + return false; + } + } + public void release() { sp.release(); } diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/IPQpsStrategy.java b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/IPQpsStrategy.java index 6589c90fe1d..7ffd1f04eb7 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/IPQpsStrategy.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/IPQpsStrategy.java @@ -22,17 +22,29 @@ public IPQpsStrategy(String paramString) { } public boolean tryAcquire(String ip) { - RateLimiter limiter; + RateLimiter limiter = loadLimiter(ip); + return limiter != null && limiter.tryAcquire(); + } + + public boolean acquire(String ip) { + RateLimiter limiter = loadLimiter(ip); + if (limiter == null) { + return false; + } + limiter.acquire(); + return true; + } + + private RateLimiter loadLimiter(String ip) { try { // cache.get is atomic: only one loader executes per key under concurrent requests, // preventing multiple RateLimiter instances from being created for the same IP. - limiter = ipLimiter.get(ip, this::newRateLimiter); + return ipLimiter.get(ip, this::newRateLimiter); } catch (Exception e) { logger.warn("Failed to load IP rate limiter for {}, denying request: {}", ip, e.getMessage()); - return false; + return null; } - return limiter.tryAcquire(); } private RateLimiter newRateLimiter() { diff --git a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/QpsStrategy.java b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/QpsStrategy.java index 7e0466448b3..9116af1b7da 100644 --- a/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/QpsStrategy.java +++ b/framework/src/main/java/org/tron/core/services/ratelimiter/strategy/QpsStrategy.java @@ -29,4 +29,9 @@ protected Map defaultParam() { public boolean tryAcquire() { return rateLimiter.tryAcquire(); } + + public boolean acquire() { + rateLimiter.acquire(); + return true; + } } \ No newline at end of file diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index d00f334f4ce..d6d3ab236a6 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -415,7 +415,7 @@ node { ## rate limiter config rate.limiter = { - # Every api could only set a specific rate limit strategy. Three non-blocking strategy are supported: + # Every api could only set a specific rate limit strategy. Three strategy are supported: # GlobalPreemptibleAdapter: The number of preemptible resource or maximum concurrent requests globally. # QpsRateLimiterAdapter: qps is the average request count in one second supported by the server, it could be a Double or a Integer. # IPQPSRateLimiterAdapter: similar to the QpsRateLimiterAdapter, qps could be a Double or a Integer. @@ -473,6 +473,9 @@ rate.limiter = { global.qps = 50000 # IP-based global qps, default 10000 global.ip.qps = 10000 + # If true, API rate limiters reject immediately on overload (non-blocking). + # If false (default), callers wait for a permit (blocking, the legacy behaviour). + apiNonBlocking = false } diff --git a/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java b/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java index 1ae341696eb..8cca558d151 100644 --- a/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java @@ -167,14 +167,14 @@ public void testBuildsEachWhitelistedAdapter() { @Test public void testPerEndpointRejectedDoesNotConsumeGlobalQuota() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(false); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(false); container.add(KEY_HTTP, "TestServlet", perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { servlet.service(request, response); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), never()); - // tryAcquire returned false — no permit was taken, nothing to release + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), never()); + // acquirePermit returned false — no permit was taken, nothing to release verify(perEndpoint, never()).release(); } } @@ -186,13 +186,13 @@ public void testPerEndpointRejectedDoesNotConsumeGlobalQuota() throws Exception @Test public void testNonPreemptiblePerEndpointRejectedDoesNotConsumeGlobal() throws Exception { IRateLimiter perEndpoint = Mockito.mock(IRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(false); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(false); container.add(KEY_HTTP, "TestServlet", perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { servlet.service(request, response); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), never()); + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), never()); } } @@ -203,11 +203,11 @@ public void testNonPreemptiblePerEndpointRejectedDoesNotConsumeGlobal() throws E @Test public void testGlobalRejectedReleasesPreemptiblePermit() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_HTTP, "TestServlet", perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(false); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(false); servlet.service(request, response); @@ -223,11 +223,11 @@ public void testGlobalRejectedReleasesPreemptiblePermit() throws Exception { @Test public void testBothPassPermitReleasedAfterRequest() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_HTTP, "TestServlet", perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); servlet.service(request, response); @@ -243,11 +243,11 @@ public void testBothPassPermitReleasedAfterRequest() throws Exception { public void testNullRateLimiterConsultsOnlyGlobal() throws Exception { // No entry added to container — container.get() returns null try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); servlet.service(request, response); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), times(1)); + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), times(1)); } } } diff --git a/framework/src/test/java/org/tron/core/services/ratelimiter/GlobalRateLimiterTest.java b/framework/src/test/java/org/tron/core/services/ratelimiter/GlobalRateLimiterTest.java index c34d49d9009..8ea0f908899 100644 --- a/framework/src/test/java/org/tron/core/services/ratelimiter/GlobalRateLimiterTest.java +++ b/framework/src/test/java/org/tron/core/services/ratelimiter/GlobalRateLimiterTest.java @@ -1,5 +1,10 @@ package org.tron.core.services.ratelimiter; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.util.concurrent.RateLimiter; @@ -9,6 +14,9 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.tron.common.TestConstants; import org.tron.core.config.args.Args; @@ -135,6 +143,104 @@ public void testPerIpLimitsAreIndependent() throws Exception { Assert.assertFalse(GlobalRateLimiter.tryAcquire(runtimeDataFor("2.2.2.2"))); } + /** + * acquire() must drain the IP limiter before the global limiter, mirroring + * tryAcquire(). A reversed order would let one chatty IP consume global + * quota even when its own per-IP budget is exhausted. + */ + @Test + public void testAcquireOrdersIpBeforeGlobal() throws Exception { + RateLimiter globalMock = Mockito.mock(RateLimiter.class); + RateLimiter ipMock = Mockito.mock(RateLimiter.class); + injectRateLimiter(globalMock); + Cache seeded = CacheBuilder.newBuilder() + .maximumSize(10).expireAfterWrite(1, TimeUnit.HOURS).build(); + seeded.put("10.0.0.1", ipMock); + injectCache(seeded); + + Assert.assertTrue(GlobalRateLimiter.acquire(runtimeDataFor("10.0.0.1"))); + + InOrder inOrder = Mockito.inOrder(ipMock, globalMock); + inOrder.verify(ipMock).acquire(); + inOrder.verify(globalMock).acquire(); + } + + /** + * If the IP limiter cannot be created (cache loader throws), acquire() + * returns false without consuming a global token — same fail-closed + * behaviour as tryAcquire(). + */ + @Test + public void testAcquireDoesNotConsumeGlobalWhenIpLoaderFails() throws Exception { + RateLimiter globalMock = Mockito.mock(RateLimiter.class); + injectRateLimiter(globalMock); + // RateLimiter.create(-1.0) throws IllegalArgumentException, so the + // cache loader fails and loadIpLimiter() returns null. + injectIpQps(-1.0); + injectCache(CacheBuilder.newBuilder() + .maximumSize(10).expireAfterWrite(1, TimeUnit.HOURS).build()); + + Assert.assertFalse(GlobalRateLimiter.acquire(runtimeDataFor("10.0.0.1"))); + + Mockito.verify(globalMock, never()).acquire(); + } + + /** + * acquirePermit dispatches based on rate.limiter.apiNonBlocking: + * switch on → only tryAcquire runs; switch off → only acquire runs. + * These tests pin that contract on the static dispatcher; the matching + * default-method contract for IRateLimiter is covered in AdaptorTest. + */ + @Test + public void testAcquirePermitDispatchesToTryAcquireWhenNonBlocking() throws Exception { + Args.getInstance().setRateLimiterApiNonBlocking(true); + RuntimeData rd = runtimeDataFor("10.0.0.1"); + + try (MockedStatic mock = mockStatic(GlobalRateLimiter.class)) { + mock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenCallRealMethod(); + mock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + + Assert.assertTrue(GlobalRateLimiter.acquirePermit(rd)); + + mock.verify(() -> GlobalRateLimiter.tryAcquire(any()), times(1)); + mock.verify(() -> GlobalRateLimiter.acquire(any()), never()); + } + } + + @Test + public void testAcquirePermitDispatchesToAcquireWhenBlocking() throws Exception { + Args.getInstance().setRateLimiterApiNonBlocking(false); + RuntimeData rd = runtimeDataFor("10.0.0.1"); + + try (MockedStatic mock = mockStatic(GlobalRateLimiter.class)) { + mock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenCallRealMethod(); + mock.when(() -> GlobalRateLimiter.acquire(any())).thenReturn(true); + + Assert.assertTrue(GlobalRateLimiter.acquirePermit(rd)); + + mock.verify(() -> GlobalRateLimiter.acquire(any()), times(1)); + mock.verify(() -> GlobalRateLimiter.tryAcquire(any()), never()); + } + } + + private static void injectRateLimiter(RateLimiter rl) throws Exception { + Field f = GlobalRateLimiter.class.getDeclaredField("rateLimiter"); + f.setAccessible(true); + f.set(null, rl); + } + + private static void injectCache(Cache cache) throws Exception { + Field f = GlobalRateLimiter.class.getDeclaredField("cache"); + f.setAccessible(true); + f.set(null, cache); + } + + private static void injectIpQps(double qps) throws Exception { + Field f = GlobalRateLimiter.class.getDeclaredField("IP_QPS"); + f.setAccessible(true); + f.set(null, qps); + } + @AfterClass public static void destroy() { Args.clearParam(); diff --git a/framework/src/test/java/org/tron/core/services/ratelimiter/RateLimiterInterceptorTest.java b/framework/src/test/java/org/tron/core/services/ratelimiter/RateLimiterInterceptorTest.java index 6cf02a25050..bbc365f3e0b 100644 --- a/framework/src/test/java/org/tron/core/services/ratelimiter/RateLimiterInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/ratelimiter/RateLimiterInterceptorTest.java @@ -95,13 +95,13 @@ public void setUp() throws Exception { @Test public void testPerEndpointRejectedDoesNotConsumeGlobalQuota() { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(false); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(false); container.add(KEY_RPC, METHOD_NAME, perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { interceptor.interceptCall(call, headers, next); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), never()); + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), never()); verify(perEndpoint, never()).release(); } } @@ -112,13 +112,13 @@ public void testPerEndpointRejectedDoesNotConsumeGlobalQuota() { @Test public void testNonPreemptiblePerEndpointRejectedDoesNotConsumeGlobal() { IRateLimiter perEndpoint = Mockito.mock(IRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(false); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(false); container.add(KEY_RPC, METHOD_NAME, perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { interceptor.interceptCall(call, headers, next); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), never()); + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), never()); } } @@ -129,11 +129,11 @@ public void testNonPreemptiblePerEndpointRejectedDoesNotConsumeGlobal() { @Test public void testGlobalRejectedReleasesPreemptiblePermit() { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_RPC, METHOD_NAME, perEndpoint); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(false); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(false); interceptor.interceptCall(call, headers, next); @@ -153,12 +153,12 @@ public void testGlobalRejectedReleasesPreemptiblePermit() { @Test public void testStartCallExceptionReleasesPermitAndClosesCall() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_RPC, METHOD_NAME, perEndpoint); when(next.startCall(any(), any())).thenThrow(new RuntimeException("handler crash")); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); interceptor.interceptCall(call, headers, next); @@ -176,14 +176,14 @@ public void testStartCallExceptionReleasesPermitAndClosesCall() throws Exception @Test public void testListenerReleasesPermitOnComplete() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_RPC, METHOD_NAME, perEndpoint); ServerCall.Listener delegate = Mockito.mock(ServerCall.Listener.class); when(next.startCall(any(), any())).thenReturn(delegate); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); ServerCall.Listener listener = interceptor.interceptCall(call, headers, next); listener.onComplete(); @@ -199,14 +199,14 @@ public void testListenerReleasesPermitOnComplete() throws Exception { @Test public void testListenerReleasesPermitOnCancel() throws Exception { IPreemptibleRateLimiter perEndpoint = Mockito.mock(IPreemptibleRateLimiter.class); - when(perEndpoint.tryAcquire(any(RuntimeData.class))).thenReturn(true); + when(perEndpoint.acquirePermit(any(RuntimeData.class))).thenReturn(true); container.add(KEY_RPC, METHOD_NAME, perEndpoint); ServerCall.Listener delegate = Mockito.mock(ServerCall.Listener.class); when(next.startCall(any(), any())).thenReturn(delegate); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); ServerCall.Listener listener = interceptor.interceptCall(call, headers, next); listener.onCancel(); @@ -225,11 +225,11 @@ public void testNullRateLimiterConsultsOnlyGlobal() throws Exception { when(next.startCall(any(), any())).thenReturn(delegate); try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { - globalMock.when(() -> GlobalRateLimiter.tryAcquire(any())).thenReturn(true); + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); interceptor.interceptCall(call, headers, next); - globalMock.verify(() -> GlobalRateLimiter.tryAcquire(any()), times(1)); + globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), times(1)); } } } diff --git a/framework/src/test/java/org/tron/core/services/ratelimiter/adaptor/AdaptorTest.java b/framework/src/test/java/org/tron/core/services/ratelimiter/adaptor/AdaptorTest.java index 69a6c688200..5ab85a42bbf 100644 --- a/framework/src/test/java/org/tron/core/services/ratelimiter/adaptor/AdaptorTest.java +++ b/framework/src/test/java/org/tron/core/services/ratelimiter/adaptor/AdaptorTest.java @@ -4,12 +4,18 @@ import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.tron.common.TestConstants; import org.tron.common.es.ExecutorServiceManager; import org.tron.common.utils.ReflectUtils; +import org.tron.core.config.args.Args; +import org.tron.core.services.ratelimiter.RuntimeData; import org.tron.core.services.ratelimiter.adapter.GlobalPreemptibleAdapter; import org.tron.core.services.ratelimiter.adapter.IPQPSRateLimiterAdapter; +import org.tron.core.services.ratelimiter.adapter.IRateLimiter; import org.tron.core.services.ratelimiter.adapter.QpsRateLimiterAdapter; import org.tron.core.services.ratelimiter.strategy.GlobalPreemptibleStrategy; import org.tron.core.services.ratelimiter.strategy.IPQpsStrategy; @@ -17,6 +23,61 @@ public class AdaptorTest { + @Before + public void setUp() { + Args.setParam(new String[0], TestConstants.TEST_CONF); + } + + @AfterClass + public static void tearDown() { + Args.clearParam(); + } + + /** + * IRateLimiter.acquirePermit is a default method that dispatches based on + * rate.limiter.apiNonBlocking. The two cases below pin that contract: with + * the switch on, only tryAcquire is invoked; with the switch off, only + * acquire is invoked. Breaking either direction is a behavioural regression. + */ + @Test + public void testAcquirePermitDispatchesToTryAcquireWhenNonBlocking() { + Args.getInstance().setRateLimiterApiNonBlocking(true); + CountingRateLimiter limiter = new CountingRateLimiter(); + + Assert.assertTrue(limiter.acquirePermit(null)); + + Assert.assertEquals(1, limiter.tryAcquireCount); + Assert.assertEquals(0, limiter.acquireCount); + } + + @Test + public void testAcquirePermitDispatchesToAcquireWhenBlocking() { + Args.getInstance().setRateLimiterApiNonBlocking(false); + CountingRateLimiter limiter = new CountingRateLimiter(); + + Assert.assertTrue(limiter.acquirePermit(null)); + + Assert.assertEquals(0, limiter.tryAcquireCount); + Assert.assertEquals(1, limiter.acquireCount); + } + + private static final class CountingRateLimiter implements IRateLimiter { + int tryAcquireCount; + int acquireCount; + + @Override + public boolean tryAcquire(RuntimeData data) { + tryAcquireCount++; + return true; + } + + @Override + public boolean acquire(RuntimeData data) { + acquireCount++; + return true; + } + } + @Test public void testStrategy() { String paramString1 = "qps=5 notExist=6"; From 0dd5139a5f6fe50abf84e6a0392b9dc4581550cc Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Thu, 14 May 2026 16:53:42 +0800 Subject: [PATCH 09/57] refactor(config,protocol,ci): optimize config binding (#6762) --- codecov.yml | 38 --- .../common/parameter/CommonParameter.java | 3 - .../core/config/args/CommitteeConfig.java | 116 ++++----- .../tron/core/config/args/EventConfig.java | 72 ++---- .../org/tron/core/config/args/NodeConfig.java | 228 +++++++----------- .../org/tron/core/config/args/Overlay.java | 22 -- .../org/tron/core/config/args/Storage.java | 4 - .../tron/core/config/args/StorageConfig.java | 15 +- .../org/tron/core/config/args/VmConfig.java | 33 +-- common/src/main/resources/reference.conf | 21 +- .../core/config/args/CommitteeConfigTest.java | 8 +- .../core/config/args/EventConfigTest.java | 7 + .../tron/core/config/args/NodeConfigTest.java | 2 - .../tron/core/config/args/VmConfigTest.java | 43 ++-- .../java/org/tron/core/config/args/Args.java | 9 +- .../main/java/org/tron/program/FullNode.java | 4 +- framework/src/main/resources/config.conf | 2 +- .../java/org/tron/common/ParameterTest.java | 1 - .../tron/core/config/args/OverlayTest.java | 40 --- protocol/src/main/protos/api/api.proto | 1 - 20 files changed, 224 insertions(+), 445 deletions(-) delete mode 100644 codecov.yml delete mode 100644 common/src/main/java/org/tron/core/config/args/Overlay.java delete mode 100644 framework/src/test/java/org/tron/core/config/args/OverlayTest.java diff --git a/codecov.yml b/codecov.yml deleted file mode 100644 index 1b46f3fa8db..00000000000 --- a/codecov.yml +++ /dev/null @@ -1,38 +0,0 @@ -# DEPRECATED: Codecov integration is no longer active. -# Coverage is now handled by JaCoCo + madrapps/jacoco-report in pr-build.yml. -# This file is retained for reference only and can be safely deleted. - -# Post a Codecov comment on pull requests. If don't need comment, use comment: false, else use following -comment: false -#comment: -# # Show coverage diff, flags table, and changed files in the PR comment -# layout: "diff, flags, files" -# # Update existing comment if present, otherwise create a new one -# behavior: default -# # Post a comment even when coverage numbers do not change -# require_changes: false -# # Do not require a base report before posting the comment -# require_base: false -# # Require the PR head commit to have a coverage report -# require_head: true -# # Show both project coverage and patch coverage in the PR comment -# hide_project_coverage: false - -codecov: - # Do not wait for all CI checks to pass before sending notifications - require_ci_to_pass: false - notify: - wait_for_ci: false - -coverage: - status: - project: # PR coverage/project UI - default: - # Compare against the base branch automatically - target: auto - # Allow a small coverage drop tolerance - threshold: 0.02% -# patch: off - patch: # PR coverage/patch UI - default: - target: 60% diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 8bbc52e6d87..7c8c16ed422 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -14,7 +14,6 @@ import org.tron.common.logsfilter.FilterQuery; import org.tron.common.setting.RocksDbSettings; import org.tron.core.Constant; -import org.tron.core.config.args.Overlay; import org.tron.core.config.args.SeedNode; import org.tron.core.config.args.Storage; import org.tron.p2p.P2pConfig; @@ -435,8 +434,6 @@ public class CommonParameter { @Getter public Storage storage; @Getter - public Overlay overlay; - @Getter public SeedNode seedNode; @Getter public EventPluginConfig eventPluginConfig; diff --git a/common/src/main/java/org/tron/core/config/args/CommitteeConfig.java b/common/src/main/java/org/tron/core/config/args/CommitteeConfig.java index 5cd9de842a0..660fa289e3b 100644 --- a/common/src/main/java/org/tron/core/config/args/CommitteeConfig.java +++ b/common/src/main/java/org/tron/core/config/args/CommitteeConfig.java @@ -2,6 +2,7 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigBeanFactory; +import com.typesafe.config.ConfigValue; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -35,29 +36,10 @@ public class CommitteeConfig { private long allowProtoFilterNum = 0; private long allowAccountStateRoot = 0; private long changedDelegation = 0; - // NON-STANDARD NAMING: "allowPBFT" and "pBFTExpireNum" in config.conf contain - // consecutive uppercase letters ("PBFT"), which violates JavaBean naming convention. - // ConfigBeanFactory derives config keys from setter names using JavaBean rules: - // setPBFTExpireNum -> property "PBFTExpireNum" (capital P, per JavaBean spec) - // but config.conf uses "pBFTExpireNum" (lowercase p) -> mismatch -> binding fails. - // - // These two fields are excluded from auto-binding and handled manually in fromConfig(). - // TODO: Rename config keys to standard camelCase (allowPbft, pbftExpireNum) when - // PBFT feature is enabled and a breaking config change is acceptable. - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) - private long allowPBFT = 0; - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) - private long pBFTExpireNum = 20; - - // Only getters are exposed. No public setters — ConfigBeanFactory scans public - // setters via reflection and would derive key "PBFTExpireNum" / "AllowPBFT" - // (JavaBean uppercase rule), which does not match config keys "pBFTExpireNum" - // / "allowPBFT" and would throw. Values are assigned to fields directly in - // fromConfig() below. - public long getAllowPBFT() { return allowPBFT; } - public long getPBFTExpireNum() { return pBFTExpireNum; } + // "allowPBFT" / "pBFTExpireNum" in config.conf use non-standard casing; they are + // remapped to standard camelCase by normalizeNonStandardKeys() before binding. + private long allowPbft = 0; + private long pbftExpireNum = 20; private long allowTvmFreeze = 0; private long allowTvmVote = 0; private long allowTvmLondon = 0; @@ -85,32 +67,30 @@ public class CommitteeConfig { private long dynamicEnergyMaxFactor = 0; // proposalExpireTime is NOT a committee field — it's in block.* and handled by BlockConfig - // Defaults come from reference.conf (loaded globally via Configuration.java) - - /** - * Create CommitteeConfig from the "committee" section of the application config. - * - * Note: allowPBFT and pBFTExpireNum have non-standard JavaBean naming (consecutive - * uppercase letters) which causes ConfigBeanFactory key mismatch. These two fields - * are excluded from automatic binding and handled manually after. - */ - private static final String PBFT_EXPIRE_NUM_KEY = "pBFTExpireNum"; - private static final String ALLOW_PBFT_KEY = "allowPBFT"; - public static CommitteeConfig fromConfig(Config config) { - Config section = config.getConfig("committee"); - + Config section = normalizeNonStandardKeys(config.getConfig("committee")); CommitteeConfig cc = ConfigBeanFactory.create(section, CommitteeConfig.class); - // Ensure the manually-named fields get the right values from the original keys - cc.allowPBFT = section.hasPath(ALLOW_PBFT_KEY) ? section.getLong(ALLOW_PBFT_KEY) : 0; - cc.pBFTExpireNum = section.hasPath(PBFT_EXPIRE_NUM_KEY) - ? section.getLong(PBFT_EXPIRE_NUM_KEY) : 20; - cc.postProcess(); return cc; } + // "allowPBFT" and "pBFTExpireNum" use non-standard casing that JavaBean Introspector + // cannot derive correctly (setPBFTExpireNum -> property "PBFTExpireNum", not "pBFTExpireNum"). + // Remap them to standard camelCase keys so ConfigBeanFactory binds them normally. + // Config is immutable; withValue() returns a new object. + private static Config normalizeNonStandardKeys(Config section) { + if (section.hasPath("allowPBFT")) { + ConfigValue v = section.getValue("allowPBFT"); + section = section.withValue("allowPbft", v); // rename allowPBFT -> allowPbft + } + if (section.hasPath("pBFTExpireNum")) { + ConfigValue v = section.getValue("pBFTExpireNum"); + section = section.withValue("pbftExpireNum", v); // rename pBFTExpireNum -> pbftExpireNum + } + return section; + } + private void postProcess() { // clamp unfreezeDelayDays to 0-365 if (unfreezeDelayDays < 0) { @@ -121,35 +101,61 @@ private void postProcess() { } // clamp allowDelegateOptimization to 0-1 - if (allowDelegateOptimization < 0) { allowDelegateOptimization = 0; } - if (allowDelegateOptimization > 1) { allowDelegateOptimization = 1; } + if (allowDelegateOptimization < 0) { + allowDelegateOptimization = 0; + } + if (allowDelegateOptimization > 1) { + allowDelegateOptimization = 1; + } // clamp allowDynamicEnergy to 0-1 - if (allowDynamicEnergy < 0) { allowDynamicEnergy = 0; } - if (allowDynamicEnergy > 1) { allowDynamicEnergy = 1; } + if (allowDynamicEnergy < 0) { + allowDynamicEnergy = 0; + } + if (allowDynamicEnergy > 1) { + allowDynamicEnergy = 1; + } // clamp dynamicEnergyThreshold to 0-100_000_000_000_000_000 - if (dynamicEnergyThreshold < 0) { dynamicEnergyThreshold = 0; } + if (dynamicEnergyThreshold < 0) { + dynamicEnergyThreshold = 0; + } if (dynamicEnergyThreshold > 100_000_000_000_000_000L) { dynamicEnergyThreshold = 100_000_000_000_000_000L; } // clamp dynamicEnergyIncreaseFactor to 0-10_000 - if (dynamicEnergyIncreaseFactor < 0) { dynamicEnergyIncreaseFactor = 0; } - if (dynamicEnergyIncreaseFactor > 10_000L) { dynamicEnergyIncreaseFactor = 10_000L; } + if (dynamicEnergyIncreaseFactor < 0) { + dynamicEnergyIncreaseFactor = 0; + } + if (dynamicEnergyIncreaseFactor > 10_000L) { + dynamicEnergyIncreaseFactor = 10_000L; + } // clamp dynamicEnergyMaxFactor to 0-100_000 - if (dynamicEnergyMaxFactor < 0) { dynamicEnergyMaxFactor = 0; } - if (dynamicEnergyMaxFactor > 100_000L) { dynamicEnergyMaxFactor = 100_000L; } + if (dynamicEnergyMaxFactor < 0) { + dynamicEnergyMaxFactor = 0; + } + if (dynamicEnergyMaxFactor > 100_000L) { + dynamicEnergyMaxFactor = 100_000L; + } // clamp allowNewReward to 0-1 (must run BEFORE the cross-field check below, // which depends on allowNewReward != 1) - if (allowNewReward < 0) { allowNewReward = 0; } - if (allowNewReward > 1) { allowNewReward = 1; } + if (allowNewReward < 0) { + allowNewReward = 0; + } + if (allowNewReward > 1) { + allowNewReward = 1; + } // clamp memoFee to 0-1_000_000_000 - if (memoFee < 0) { memoFee = 0; } - if (memoFee > 1_000_000_000L) { memoFee = 1_000_000_000L; } + if (memoFee < 0) { + memoFee = 0; + } + if (memoFee > 1_000_000_000L) { + memoFee = 1_000_000_000L; + } // cross-field: allowOldRewardOpt requires at least one reward/vote flag if (allowOldRewardOpt == 1 && allowNewRewardAlgorithm != 1 diff --git a/common/src/main/java/org/tron/core/config/args/EventConfig.java b/common/src/main/java/org/tron/core/config/args/EventConfig.java index 43707956845..f4378682cc3 100644 --- a/common/src/main/java/org/tron/core/config/args/EventConfig.java +++ b/common/src/main/java/org/tron/core/config/args/EventConfig.java @@ -25,19 +25,15 @@ public class EventConfig { private String server = ""; private String dbconfig = ""; private boolean contractParse = true; - @Getter(lombok.AccessLevel.NONE) + // "native" is a Java reserved word; config key cannot match field name directly. + // @Setter(NONE) prevents ConfigBeanFactory from requiring a "nativeQueue" key. @Setter(lombok.AccessLevel.NONE) private NativeConfig nativeQueue = new NativeConfig(); - public NativeConfig getNativeQueue() { return nativeQueue; } - // Topics list has optional fields (ethCompatible, redundancy, solidified) that - // not all items have. ConfigBeanFactory requires all bean fields to exist in config. - // Excluded from auto-binding, read manually in fromConfig(). - @Getter(lombok.AccessLevel.NONE) + // Topics list items have optional fields; excluded from auto-binding. + // @Setter(NONE) prevents ConfigBeanFactory from requiring a "topics" key. @Setter(lombok.AccessLevel.NONE) private List topics = new ArrayList<>(); - - public List getTopics() { return topics; } private FilterConfig filter = new FilterConfig(); @Getter @@ -70,62 +66,40 @@ public static class FilterConfig { // Defaults come from reference.conf (loaded globally via Configuration.java) + // TopicConfig fields are optional per item; this fallback ensures all keys exist + // for ConfigBeanFactory binding. Values must match TopicConfig field defaults. + private static final Config TOPIC_DEFAULTS = ConfigFactory.parseString( + "triggerName=\"\", enable=false, topic=\"\", " + + "solidified=false, ethCompatible=false, redundancy=false"); + /** * Create EventConfig from the "event.subscribe" section of the application config. * - *

Note: HOCON key "native" is a Java reserved word, so the bean field is named - * "nativeQueue" but config key is "native". We handle this manually after binding. + *

"native" is a Java reserved word, so the field is named "nativeQueue" and the + * sub-section is read directly after binding. "topics" items may omit optional fields; + * TOPIC_DEFAULTS provides fallback values so ConfigBeanFactory can bind each item. */ public static EventConfig fromConfig(Config config) { Config section = config.getConfig("event.subscribe"); - // "native" is a Java reserved word, "topics" has optional fields per item — - // strip both before binding, read manually String nativeKey = "native"; String topicsKey = "topics"; - Config bindable = section.withoutPath(nativeKey).withoutPath(topicsKey) - .withoutPath("topicDefaults"); + // remove two keys to construct EventConfig because they cannot be bind automatically, + // we can bind them manually later + Config bindable = section.withoutPath(nativeKey).withoutPath(topicsKey); EventConfig ec = ConfigBeanFactory.create(bindable, EventConfig.class); - // manually bind "native" sub-section - Config nativeSection = section.hasPath(nativeKey) - ? section.getConfig(nativeKey) : ConfigFactory.empty(); - ec.nativeQueue = new NativeConfig(); - if (nativeSection.hasPath("useNativeQueue")) { - ec.nativeQueue.useNativeQueue = nativeSection.getBoolean("useNativeQueue"); - } - if (nativeSection.hasPath("bindport")) { - ec.nativeQueue.bindport = nativeSection.getInt("bindport"); - } - if (nativeSection.hasPath("sendqueuelength")) { - ec.nativeQueue.sendqueuelength = nativeSection.getInt("sendqueuelength"); - } + // "native" sub-section: bind via ConfigBeanFactory when present, use defaults otherwise + ec.nativeQueue = section.hasPath(nativeKey) + ? ConfigBeanFactory.create(section.getConfig(nativeKey), NativeConfig.class) + : new NativeConfig(); - // manually bind topics — each item may have optional fields + // topics: withFallback fills optional fields so ConfigBeanFactory can bind each item if (section.hasPath(topicsKey)) { ec.topics = new ArrayList<>(); for (com.typesafe.config.ConfigObject obj : section.getObjectList(topicsKey)) { - Config tc = obj.toConfig(); - TopicConfig topic = new TopicConfig(); - if (tc.hasPath("triggerName")) { - topic.triggerName = tc.getString("triggerName"); - } - if (tc.hasPath("enable")) { - topic.enable = tc.getBoolean("enable"); - } - if (tc.hasPath("topic")) { - topic.topic = tc.getString("topic"); - } - if (tc.hasPath("solidified")) { - topic.solidified = tc.getBoolean("solidified"); - } - if (tc.hasPath("ethCompatible")) { - topic.ethCompatible = tc.getBoolean("ethCompatible"); - } - if (tc.hasPath("redundancy")) { - topic.redundancy = tc.getBoolean("redundancy"); - } - ec.topics.add(topic); + ec.topics.add(ConfigBeanFactory.create( + obj.toConfig().withFallback(TOPIC_DEFAULTS), TopicConfig.class)); } } diff --git a/common/src/main/java/org/tron/core/config/args/NodeConfig.java b/common/src/main/java/org/tron/core/config/args/NodeConfig.java index ea9f26a06a0..82619726b7e 100644 --- a/common/src/main/java/org/tron/core/config/args/NodeConfig.java +++ b/common/src/main/java/org/tron/core/config/args/NodeConfig.java @@ -29,47 +29,43 @@ public class NodeConfig { private int syncFetchBatchNum = 2000; private int maxPendingBlockSize = 500; private int validateSignThreadNum = 0; // 0 = auto (availableProcessors) - private int maxConnections = 30; + private int maxConnections = 30; // legacy key maxActiveNodes private int minConnections = 8; private int minActiveConnections = 3; - private int maxConnectionsWithSameIp = 2; + private int maxConnectionsWithSameIp = 2; // legacy key maxActiveNodesWithSameIp private int maxHttpConnectNumber = 50; private int minParticipationRate = 0; private boolean openPrintLog = true; private boolean openTransactionSort = false; private int maxTps = 1000; private int maxBlockInvPerSecond = 10; - // Config key "isOpenFullTcpDisconnect" cannot auto-bind — read manually in fromConfig() - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) - private boolean isOpenFullTcpDisconnect = false; - - public boolean isOpenFullTcpDisconnect() { return isOpenFullTcpDisconnect; } + private boolean openFullTcpDisconnect = false; //rename key // node.discovery.* — HOCON merges into node { discovery { ... } }, auto-bound private DiscoveryConfig discovery = new DiscoveryConfig(); - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) - private String externalIP = ""; - // node.shutdown.* uses PascalCase keys (BlockTime, BlockHeight, BlockCount) - // that don't match JavaBean naming. Excluded, read manually. - @Getter(lombok.AccessLevel.NONE) + // node.shutdown.* uses PascalCase nested keys (shutdown.BlockTime, etc.). + // These are optional (not in reference.conf), so @Setter(NONE) prevents ConfigBeanFactory + // from requiring the keys; values are read manually in fromConfig(). @Setter(lombok.AccessLevel.NONE) private String shutdownBlockTime = ""; - @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private long shutdownBlockHeight = -1; - @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private long shutdownBlockCount = -1; - public boolean isDiscoveryEnable() { return discovery.isEnable(); } - public boolean isDiscoveryPersist() { return discovery.isPersist(); } - public String getDiscoveryExternalIp() { return externalIP; } - public String getShutdownBlockTime() { return shutdownBlockTime; } - public long getShutdownBlockHeight() { return shutdownBlockHeight; } - public long getShutdownBlockCount() { return shutdownBlockCount; } + public boolean isDiscoveryEnable() { + return discovery.isEnable(); + } + + public boolean isDiscoveryPersist() { + return discovery.isPersist(); + } + + public String getDiscoveryExternalIp() { + return discovery.getExternal().getIp(); + } + private int inactiveThreshold = 600; private boolean metricsEnable = false; private int blockProducedTimeOut = 50; @@ -90,14 +86,14 @@ public class NodeConfig { private boolean unsolidifiedBlockCheck = false; private int maxUnsolidifiedBlocks = 54; private String zenTokenId = "000000"; - @Getter(lombok.AccessLevel.NONE) + // allowShieldedTransactionApi is optional (commented out in reference.conf) and has a + // legacy key fallback; @Setter(NONE) prevents ConfigBeanFactory from requiring the key. @Setter(lombok.AccessLevel.NONE) private boolean allowShieldedTransactionApi = false; + + //deprecate key private double activeConnectFactor = 0.1; private double connectFactor = 0.6; - // Legacy alias `maxActiveNodesWithSameIp` has no bean field: we only peek at it via - // section.hasPath() below. Keeping it field-less means reference.conf doesn't have to - // ship a default that would otherwise mask the modern `maxConnectionsWithSameIp` key. // ---- Sub-beans matching config's dot-notation nested structure ---- private ListenConfig listen = new ListenConfig(); @@ -105,11 +101,21 @@ public class NodeConfig { private SolidityConfig solidity = new SolidityConfig(); // Convenience getters for backward compatibility with applyNodeConfig - public int getListenPort() { return listen.getPort(); } - public int getFetchBlockTimeout() { return fetchBlock.getTimeout(); } - public int getSolidityThreads() { return solidity.getThreads(); } - public int getValidContractProtoThreads() { return validContractProto.getThreads(); } - public boolean isAllowShieldedTransactionApi() { return allowShieldedTransactionApi; } + public int getListenPort() { + return listen.getPort(); + } + + public int getFetchBlockTimeout() { + return fetchBlock.getTimeout(); + } + + public int getSolidityThreads() { + return solidity.getThreads(); + } + + public int getValidContractProtoThreads() { + return validContractProto.getThreads(); + } // ---- List fields (manually read) ---- private List active = new ArrayList<>(); @@ -136,43 +142,58 @@ public class NodeConfig { @Getter @Setter public static class DiscoveryConfig { + private boolean enable = false; private boolean persist = false; + private ExternalConfig external = new ExternalConfig(); + + @Getter + @Setter + public static class ExternalConfig { + + private String ip = ""; + } } @Getter @Setter public static class ListenConfig { + private int port = 18888; } @Getter @Setter public static class FetchBlockConfig { + private int timeout = 500; } @Getter @Setter public static class SolidityConfig { + private int threads = 0; // 0 = auto (availableProcessors) } @Getter @Setter public static class ValidContractProtoConfig { + private int threads = 0; // 0 = auto (availableProcessors) } @Getter @Setter public static class P2pConfig { + private int version = 11111; } @Getter @Setter public static class HttpConfig { + private boolean fullNodeEnable = true; private int fullNodePort = 8090; private boolean solidityEnable = true; @@ -180,63 +201,21 @@ public static class HttpConfig { private long maxMessageSize = 4194304; private int maxNestingDepth = 100; private int maxTokenCount = 100_000; - // PBFT fields — handled manually (same naming issue as CommitteeConfig) - // Default must match CommonParameter.pBFTHttpEnable = true - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private boolean pBFTEnable = true; - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private int pBFTPort = 8092; - - public boolean isPBFTEnable() { - return pBFTEnable; - } - - public void setPBFTEnable(boolean v) { - this.pBFTEnable = v; - } - - public int getPBFTPort() { - return pBFTPort; - } - - public void setPBFTPort(int v) { - this.pBFTPort = v; - } } @Getter @Setter public static class RpcConfig { + private boolean enable = true; private int port = 50051; private boolean solidityEnable = true; private int solidityPort = 50061; - // PBFT fields — handled manually - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private boolean pBFTEnable = true; - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private int pBFTPort = 50071; - public boolean isPBFTEnable() { - return pBFTEnable; - } - - public void setPBFTEnable(boolean v) { - this.pBFTEnable = v; - } - - public int getPBFTPort() { - return pBFTPort; - } - - public void setPBFTPort(int v) { - this.pBFTPort = v; - } - private int thread = 0; private int maxConcurrentCallsPerConnection = 2147483647; private int flowControlWindow = 1048576; @@ -254,34 +233,14 @@ public void setPBFTPort(int v) { @Getter @Setter public static class JsonRpcConfig { + private boolean httpFullNodeEnable = false; private int httpFullNodePort = 8545; private boolean httpSolidityEnable = false; private int httpSolidityPort = 8555; - // PBFT fields — handled manually - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private boolean httpPBFTEnable = false; - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) private int httpPBFTPort = 8565; - public boolean isHttpPBFTEnable() { - return httpPBFTEnable; - } - - public void setHttpPBFTEnable(boolean v) { - this.httpPBFTEnable = v; - } - - public int getHttpPBFTPort() { - return httpPBFTPort; - } - - public void setHttpPBFTPort(int v) { - this.httpPBFTPort = v; - } - private int maxBlockRange = 5000; private int maxSubTopics = 1000; private int maxBlockFilterNum = 50000; @@ -295,6 +254,7 @@ public void setHttpPBFTPort(int v) { @Getter @Setter public static class NodeBackupConfig { + private int priority = 0; private int port = 10001; private int keepAliveInterval = 3000; @@ -304,6 +264,7 @@ public static class NodeBackupConfig { @Getter @Setter public static class DynamicConfigSection { + private boolean enable = false; private long checkInterval = 600; } @@ -311,6 +272,7 @@ public static class DynamicConfigSection { @Getter @Setter public static class DnsConfig { + private List treeUrls = new ArrayList<>(); private boolean publish = false; private String dnsDomain = ""; @@ -327,40 +289,20 @@ public static class DnsConfig { private String awsHostZoneId = ""; } - // Defaults come from reference.conf (loaded globally via Configuration.java) - - // =========================================================================== - // Factory method - // =========================================================================== - /** * Create NodeConfig from the "node" section of the application config. * - *

Dot-notation keys (listen.port, fetchBlock.timeout, - * solidity.threads) become nested HOCON objects and cannot be auto-bound to flat - * Java fields. They are read manually after ConfigBeanFactory binding. - * - *

PBFT-named fields in http, rpc, and jsonrpc sub-beans have the same JavaBean - * naming issue as CommitteeConfig and are patched manually. - * *

List fields (active, passive, fastForward, disabledApi) are read manually * since ConfigBeanFactory expects typed bean lists, not string lists. */ public static NodeConfig fromConfig(Config config) { - // Normalize human-readable size values (e.g. "4m") to numeric bytes so - // ConfigBeanFactory's primitive int/long binding succeeds; same step - // enforces non-negative and <= Integer.MAX_VALUE before bean creation - // so failures point at the user-facing config path. - Config section = normalizeMaxMessageSizes(config).getConfig("node"); + Config section = normalizeNonStandardKeys( + normalizeMaxMessageSizes(config).getConfig("node")); // Auto-bind all fields and sub-beans. ConfigBeanFactory fails fast with a - // descriptive path on any `= null` value — external configs that use the - // HOCON null keyword should fix their config rather than rely on silent coercion. + // descriptive path on any `= null` value NodeConfig nc = ConfigBeanFactory.create(section, NodeConfig.class); - // isOpenFullTcpDisconnect: boolean "is" prefix breaks JavaBean pairing - nc.isOpenFullTcpDisconnect = getBool(section, "isOpenFullTcpDisconnect", false); - // --- Legacy key fallbacks (backward compatibility) --- // node.maxActiveNodes (old) -> maxConnections (new) if (section.hasPath("maxActiveNodes")) { @@ -377,11 +319,6 @@ public static NodeConfig fromConfig(Config config) { nc.maxConnectionsWithSameIp = section.getInt("maxActiveNodesWithSameIp"); } - nc.externalIP = getString(section, "discovery.external.ip", ""); - if ("null".equalsIgnoreCase(nc.externalIP)) { - nc.externalIP = ""; - } - // Legacy key fallback: node.fullNodeAllowShieldedTransaction -> allowShieldedTransactionApi. if (section.hasPath("allowShieldedTransactionApi")) { nc.allowShieldedTransactionApi = @@ -394,14 +331,13 @@ public static NodeConfig fromConfig(Config config) { + "Please use [node.allowShieldedTransactionApi] instead."); } - // node.shutdown.* — PascalCase keys (BlockTime, BlockHeight), cannot auto-bind - nc.shutdownBlockTime = config.hasPath("node.shutdown.BlockTime") - ? config.getString("node.shutdown.BlockTime") : ""; - nc.shutdownBlockHeight = config.hasPath("node.shutdown.BlockHeight") - ? config.getLong("node.shutdown.BlockHeight") : -1; - nc.shutdownBlockCount = config.hasPath("node.shutdown.BlockCount") - ? config.getLong("node.shutdown.BlockCount") : -1; - + // node.shutdown.* — optional PascalCase nested keys, not in reference.conf by default + nc.shutdownBlockTime = section.hasPath("shutdown.BlockTime") + ? section.getString("shutdown.BlockTime") : ""; + nc.shutdownBlockHeight = section.hasPath("shutdown.BlockHeight") + ? section.getLong("shutdown.BlockHeight") : -1; + nc.shutdownBlockCount = section.hasPath("shutdown.BlockCount") + ? section.getLong("shutdown.BlockCount") : -1; nc.postProcess(); return nc; @@ -513,11 +449,31 @@ private static String getString(Config config, String path, String defaultValue) return config.hasPath(path) ? config.getString(path) : defaultValue; } - // Pre-normalize size paths so ConfigBeanFactory's primitive int/long binding succeeds - // for human-readable values like "4m" / "128MB". For each maxMessageSize key, parse - // via getMemorySize, validate non-negative and <= Integer.MAX_VALUE, and write the - // numeric byte value back into the Config tree. Validation errors propagate before - // bean creation so the failure points at the user-facing config path. + /** + * "isOpenFullTcpDisconnect" config key has an "is" prefix that the JavaBean Introspector + * strips from boolean getter names, so the derived property is "openFullTcpDisconnect". + * "discovery.external.ip" may be HOCON null or the string "null"; both normalize to "". + */ + private static Config normalizeNonStandardKeys(Config section) { + if (section.hasPath("isOpenFullTcpDisconnect")) { + section = section.withValue("openFullTcpDisconnect", + section.getValue("isOpenFullTcpDisconnect")); + } + String externalIpPath = "discovery.external.ip"; + if (section.getIsNull(externalIpPath) + || "null".equalsIgnoreCase(section.getString(externalIpPath))) { + section = section.withValue(externalIpPath, ConfigValueFactory.fromAnyRef("")); + } + return section; + } + + /** + * Pre-normalize size paths so ConfigBeanFactory's primitive int/long binding succeeds + * for human-readable values like "4m" / "128MB". For each maxMessageSize key, parse + * via getMemorySize, validate non-negative and <= Integer.MAX_VALUE, and write the + * numeric byte value back into the Config tree. Validation errors propagate before + * bean creation so the failure points at the user-facing config path. + */ private static Config normalizeMaxMessageSizes(Config config) { String[] paths = { "node.rpc.maxMessageSize", diff --git a/common/src/main/java/org/tron/core/config/args/Overlay.java b/common/src/main/java/org/tron/core/config/args/Overlay.java deleted file mode 100644 index bdaa40724c7..00000000000 --- a/common/src/main/java/org/tron/core/config/args/Overlay.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.tron.core.config.args; - -import lombok.Getter; -import org.apache.commons.lang3.Range; - -public class Overlay { - - @Getter - private int port; - - /** - * Monitor port number. - */ - public void setPort(final int port) { - Range range = Range.between(0, 65535); - if (!range.contains(port)) { - throw new IllegalArgumentException("Port(" + port + ") must in [0, 65535]"); - } - - this.port = port; - } -} diff --git a/common/src/main/java/org/tron/core/config/args/Storage.java b/common/src/main/java/org/tron/core/config/args/Storage.java index 782a0ef07c8..64a9efab7f1 100644 --- a/common/src/main/java/org/tron/core/config/args/Storage.java +++ b/common/src/main/java/org/tron/core/config/args/Storage.java @@ -201,10 +201,6 @@ private static void applyPropertyOptions(StorageConfig.PropertyConfig pc, Option dbOptions.maxOpenFiles(pc.getMaxOpenFiles()); } - - /** - * Set propertyMap of Storage object from Config via StorageConfig bean. - */ /** * Set propertyMap from StorageConfig bean list. No Config parameter needed. */ diff --git a/common/src/main/java/org/tron/core/config/args/StorageConfig.java b/common/src/main/java/org/tron/core/config/args/StorageConfig.java index 3d7046ebae2..5f8efffb9f3 100644 --- a/common/src/main/java/org/tron/core/config/args/StorageConfig.java +++ b/common/src/main/java/org/tron/core/config/args/StorageConfig.java @@ -39,30 +39,19 @@ public class StorageConfig { // Raw storage config sub-tree, kept for setCacheStrategies/setDbRoots which // have dynamic keys that ConfigBeanFactory cannot bind. - @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private Config rawStorageConfig; - public Config getRawStorageConfig() { - return rawStorageConfig; - } - // LevelDB per-database option overrides (default, defaultM, defaultL). - // Excluded from auto-binding: optional partial overrides that ConfigBeanFactory cannot handle. - @Getter(lombok.AccessLevel.NONE) + // @Setter(NONE): optional keys commented out in reference.conf; ConfigBeanFactory + // would throw if it required them. Values are assigned in fromConfig(). @Setter(lombok.AccessLevel.NONE) private DbOptionOverride defaultDbOption; - @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private DbOptionOverride defaultMDbOption; - @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private DbOptionOverride defaultLDbOption; - public DbOptionOverride getDefaultDbOption() { return defaultDbOption; } - public DbOptionOverride getDefaultMDbOption() { return defaultMDbOption; } - public DbOptionOverride getDefaultLDbOption() { return defaultLDbOption; } - @Getter @Setter public static class DbConfig { diff --git a/common/src/main/java/org/tron/core/config/args/VmConfig.java b/common/src/main/java/org/tron/core/config/args/VmConfig.java index 00ba85aa6cc..3ff1136f33e 100644 --- a/common/src/main/java/org/tron/core/config/args/VmConfig.java +++ b/common/src/main/java/org/tron/core/config/args/VmConfig.java @@ -2,24 +2,18 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigBeanFactory; -import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; /** * VM configuration bean. Field names match config.conf keys under the "vm" section. - * Most fields are bound automatically via ConfigBeanFactory; opt-in fields that - * must stay absent from reference.conf are bound manually after hasPath checks. */ @Slf4j @Getter @Setter public class VmConfig { - private static final String CONSTANT_CALL_TIMEOUT_MS_KEY = "constantCallTimeoutMs"; - static final long MAX_CONSTANT_CALL_TIMEOUT_MS = Long.MAX_VALUE / 1_000L; - private boolean supportConstant = false; private long maxEnergyLimitForConstant = 100_000_000L; private int lruCacheSize = 500; @@ -32,10 +26,6 @@ public class VmConfig { private boolean saveInternalTx = false; private boolean saveFeaturedInternalTx = false; private boolean saveCancelAllUnfreezeV2Details = false; - // Excluded from ConfigBeanFactory binding (no setter): the property is - // intentionally absent from reference.conf so {@code Config#hasPath} alone - // signals operator opt-in. Bound manually in {@link #fromConfig}. - @Setter(AccessLevel.NONE) private long constantCallTimeoutMs = 0L; /** @@ -46,11 +36,11 @@ public class VmConfig { public static VmConfig fromConfig(Config config) { Config vmSection = config.getConfig("vm"); VmConfig vmConfig = ConfigBeanFactory.create(vmSection, VmConfig.class); - vmConfig.postProcess(vmSection); + vmConfig.postProcess(); return vmConfig; } - private void postProcess(Config vmSection) { + private void postProcess() { // clamp maxEnergyLimitForConstant if (maxEnergyLimitForConstant < 3_000_000L) { maxEnergyLimitForConstant = 3_000_000L; @@ -71,22 +61,9 @@ private void postProcess(Config vmSection) { + "vm.saveInternalTx or vm.saveFeaturedInternalTx is off."); } - // constantCallTimeoutMs is excluded from ConfigBeanFactory binding (no - // setter) and intentionally absent from reference.conf, so hasPath alone - // tells us whether the operator opted in. Only positive values that can be - // safely converted to microseconds are valid. - if (vmSection.hasPath(CONSTANT_CALL_TIMEOUT_MS_KEY)) { - long value = vmSection.getLong(CONSTANT_CALL_TIMEOUT_MS_KEY); - if (value <= 0L) { - throw new IllegalArgumentException( - "vm.constantCallTimeoutMs must be > 0 when configured, got " + value); - } - if (value > MAX_CONSTANT_CALL_TIMEOUT_MS) { - throw new IllegalArgumentException( - "vm.constantCallTimeoutMs must be <= " + MAX_CONSTANT_CALL_TIMEOUT_MS - + " to fit VM deadline conversion, got " + value); - } - constantCallTimeoutMs = value; + if (constantCallTimeoutMs < 0 || constantCallTimeoutMs > Long.MAX_VALUE / 1000) { + throw new IllegalArgumentException("vm.constantCallTimeoutMs must be >= 0 and <= " + + Long.MAX_VALUE / 1000 + " to fit VM deadline conversion, got " + constantCallTimeoutMs); } } } diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 3f41c556f96..0864f4d5126 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -25,18 +25,16 @@ # Key naming rules (required for ConfigBeanFactory auto-binding): # - Use standard camelCase: maxConnections, syncFetchBatchNum, etc. # -# Keys that cannot auto-bind (handled manually in bean fromConfig): +# Keys that cannot auto-bind (handled via normalizeNonStandardKeys() or manual reads): # -# 1. committee.pBFTExpireNum — lowercase "p" then uppercase "BFT": -# setPBFTExpireNum -> property "PBFTExpireNum" (capital P), -# mismatches config key "pBFTExpireNum" (lowercase p). +# 1. committee.pBFTExpireNum / committee.allowPBFT — normalized to camelCase in +# CommitteeConfig.normalizeNonStandardKeys() before ConfigBeanFactory binding. # -# 2. node.isOpenFullTcpDisconnect — boolean "is" prefix: -# getter isOpenFullTcpDisconnect() -> property "openFullTcpDisconnect", -# mismatches config key "isOpenFullTcpDisconnect". +# 2. node.isOpenFullTcpDisconnect — normalized to "openFullTcpDisconnect" in +# NodeConfig.normalizeNonStandardKeys() before ConfigBeanFactory binding. # -# 3. node.shutdown.BlockTime/BlockHeight/BlockCount — PascalCase keys: -# setBlockTime -> property "blockTime", mismatches "BlockTime". +# 3. node.shutdown.BlockTime/BlockHeight/BlockCount — optional PascalCase nested keys; +# read manually in NodeConfig.fromConfig() after ConfigBeanFactory binding. # # ============================================================================= @@ -166,8 +164,6 @@ node.metrics = { node { # Trust node for solidity node (example: "127.0.0.1:50051"). - # Empty string here = "not configured"; Args.java bridge converts "" → null so the - # runtime behavior matches develop (trustNodeAddr is null unless user sets the key). trustNode = "" # Expose extension api to public or not @@ -702,6 +698,9 @@ vm = { # Max retry time for executing transaction in estimating energy estimateEnergyMaxRetry = 3 + + # Max TVM execution time (ms) for constant calls. 0 means no effect + constantCallTimeoutMs = 0 } # Governance proposal toggle parameters. All default to 0 (disabled). diff --git a/common/src/test/java/org/tron/core/config/args/CommitteeConfigTest.java b/common/src/test/java/org/tron/core/config/args/CommitteeConfigTest.java index 962b6a349ab..559198100fb 100644 --- a/common/src/test/java/org/tron/core/config/args/CommitteeConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/CommitteeConfigTest.java @@ -20,8 +20,8 @@ private static Config withRef() { public void testDefaults() { CommitteeConfig cc = CommitteeConfig.fromConfig(withRef()); assertEquals(0, cc.getAllowCreationOfContracts()); - assertEquals(0, cc.getAllowPBFT()); - assertEquals(20, cc.getPBFTExpireNum()); + assertEquals(0, cc.getAllowPbft()); + assertEquals(20, cc.getPbftExpireNum()); assertEquals(0, cc.getUnfreezeDelayDays()); assertEquals(0, cc.getAllowDynamicEnergy()); } @@ -32,8 +32,8 @@ public void testFromConfig() { "committee { allowCreationOfContracts = 1, allowPBFT = 1, pBFTExpireNum = 30 }"); CommitteeConfig cc = CommitteeConfig.fromConfig(config); assertEquals(1, cc.getAllowCreationOfContracts()); - assertEquals(1, cc.getAllowPBFT()); - assertEquals(30, cc.getPBFTExpireNum()); + assertEquals(1, cc.getAllowPbft()); + assertEquals(30, cc.getPbftExpireNum()); } @Test diff --git a/common/src/test/java/org/tron/core/config/args/EventConfigTest.java b/common/src/test/java/org/tron/core/config/args/EventConfigTest.java index 361d9f48581..ca0cbefaddd 100644 --- a/common/src/test/java/org/tron/core/config/args/EventConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/EventConfigTest.java @@ -79,4 +79,11 @@ public void testFilter() { assertEquals(2, ec.getFilter().getContractAddress().size()); assertEquals(1, ec.getFilter().getContractTopic().size()); } + + @Test + public void testTopicsEmptyList() { + EventConfig ec = EventConfig.fromConfig(withRef( + "event.subscribe.topics = []")); + assertTrue(ec.getTopics().isEmpty()); + } } diff --git a/common/src/test/java/org/tron/core/config/args/NodeConfigTest.java b/common/src/test/java/org/tron/core/config/args/NodeConfigTest.java index d4fbc05e730..bbc2d2475ee 100644 --- a/common/src/test/java/org/tron/core/config/args/NodeConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/NodeConfigTest.java @@ -245,8 +245,6 @@ public void testValidContractProtoThreadsExplicitPreserved() { @Test public void testTrustNodeNotDefaultedByReferenceConf() { - // reference.conf intentionally omits `node.trustNode` so that empty configs - // preserve develop's behavior (trustNodeAddr stays null in the Args bridge). NodeConfig nc = NodeConfig.fromConfig(withRef()); assertTrue(nc.getTrustNode() == null || nc.getTrustNode().isEmpty()); } diff --git a/common/src/test/java/org/tron/core/config/args/VmConfigTest.java b/common/src/test/java/org/tron/core/config/args/VmConfigTest.java index e406ef24e7b..99015a8c012 100644 --- a/common/src/test/java/org/tron/core/config/args/VmConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/VmConfigTest.java @@ -2,10 +2,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import org.junit.Assert; import org.junit.Test; public class VmConfigTest { @@ -90,8 +92,8 @@ public void testEstimateEnergyMaxRetryBoundaryValues() { } // =========================================================================== - // Constant-call timeout (issue #6681). The validation rule: any positive - // value that fits VM deadline conversion is accepted, but zero/negative is + // Constant-call timeout (issue #6681). The validation rule: any zero or positive + // value that fits VM deadline conversion is accepted, but negative is // rejected ONLY when the operator explicitly set the property in their // config. Absence keeps the in-Java default (0L = "share the // block-processing deadline"). @@ -99,7 +101,7 @@ public void testEstimateEnergyMaxRetryBoundaryValues() { @Test public void testConstantCallTimeoutDefaultWhenAbsent() { - // No path in the config, no entry in reference.conf -> default 0L kept, + // reference.conf default is 0; absence of a user override keeps that default; // no validation triggered. VmConfig vm = VmConfig.fromConfig(withRef()); assertEquals(0L, vm.getConstantCallTimeoutMs()); @@ -107,6 +109,8 @@ public void testConstantCallTimeoutDefaultWhenAbsent() { @Test public void testConstantCallTimeoutAcceptsAnyPositiveValue() { + assertEquals(0L, VmConfig.fromConfig( + withRef("vm { constantCallTimeoutMs = 0 }")).getConstantCallTimeoutMs()); assertEquals(1L, VmConfig.fromConfig( withRef("vm { constantCallTimeoutMs = 1 }")).getConstantCallTimeoutMs()); assertEquals(50L, VmConfig.fromConfig( @@ -117,39 +121,20 @@ public void testConstantCallTimeoutAcceptsAnyPositiveValue() { withRef("vm { constantCallTimeoutMs = 5000 }")).getConstantCallTimeoutMs()); } - @Test - public void testConstantCallTimeoutZeroRejectedWhenExplicitlyConfigured() { - // Operator wrote `= 0` in config -> treated as a misconfiguration even - // though it equals the in-Java default. Forces an explicit positive value. - try { - VmConfig.fromConfig(withRef("vm { constantCallTimeoutMs = 0 }")); - org.junit.Assert.fail("expected IllegalArgumentException for explicit 0"); - } catch (IllegalArgumentException ex) { - org.junit.Assert.assertTrue(ex.getMessage(), - ex.getMessage().contains("constantCallTimeoutMs")); - } - } - @Test public void testConstantCallTimeoutNegativeRejected() { - try { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { VmConfig.fromConfig(withRef("vm { constantCallTimeoutMs = -1 }")); - org.junit.Assert.fail("expected IllegalArgumentException for negative ms"); - } catch (IllegalArgumentException ex) { - org.junit.Assert.assertTrue(ex.getMessage(), - ex.getMessage().contains("constantCallTimeoutMs")); - } + }); + Assert.assertTrue(thrown.getMessage().contains("constantCallTimeoutMs")); } @Test public void testConstantCallTimeoutOverflowRejected() { - long value = VmConfig.MAX_CONSTANT_CALL_TIMEOUT_MS + 1L; - try { + long value = Long.MAX_VALUE / 1000 + 1L; + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { VmConfig.fromConfig(withRef("vm { constantCallTimeoutMs = " + value + " }")); - org.junit.Assert.fail("expected IllegalArgumentException for overflowing ms"); - } catch (IllegalArgumentException ex) { - org.junit.Assert.assertTrue(ex.getMessage(), - ex.getMessage().contains("deadline conversion")); - } + }); + Assert.assertTrue(thrown.getMessage().contains("deadline conversion")); } } diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index ec808267c75..8d8e2500c9f 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -467,8 +467,8 @@ private static void applyCommitteeConfig(CommitteeConfig cc) { PARAMETER.allowProtoFilterNum = cc.getAllowProtoFilterNum(); PARAMETER.allowAccountStateRoot = cc.getAllowAccountStateRoot(); PARAMETER.changedDelegation = cc.getChangedDelegation(); - PARAMETER.allowPBFT = cc.getAllowPBFT(); - PARAMETER.pBFTExpireNum = cc.getPBFTExpireNum(); + PARAMETER.allowPBFT = cc.getAllowPbft(); + PARAMETER.pBFTExpireNum = cc.getPbftExpireNum(); PARAMETER.allowTvmFreeze = cc.getAllowTvmFreeze(); PARAMETER.allowTvmVote = cc.getAllowTvmVote(); PARAMETER.allowTvmLondon = cc.getAllowTvmLondon(); @@ -610,10 +610,7 @@ private static void applyNodeConfig(NodeConfig nc) { PARAMETER.maxHttpConnectNumber = nc.getMaxHttpConnectNumber(); PARAMETER.netMaxTrxPerSecond = nc.getNetMaxTrxPerSecond(); - if (StringUtils.isEmpty(PARAMETER.trustNodeAddr)) { - String trustNode = nc.getTrustNode(); - PARAMETER.trustNodeAddr = StringUtils.isEmpty(trustNode) ? null : trustNode; - } + PARAMETER.trustNodeAddr = nc.getTrustNode(); PARAMETER.validateSignThreadNum = nc.getValidateSignThreadNum(); PARAMETER.walletExtensionApi = nc.isWalletExtensionApi(); diff --git a/framework/src/main/java/org/tron/program/FullNode.java b/framework/src/main/java/org/tron/program/FullNode.java index 308cb9a1c69..96b9f73d577 100644 --- a/framework/src/main/java/org/tron/program/FullNode.java +++ b/framework/src/main/java/org/tron/program/FullNode.java @@ -1,8 +1,8 @@ package org.tron.program; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.util.ObjectUtils; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; @@ -35,7 +35,7 @@ public static void main(String[] args) { } if (parameter.isSolidityNode()) { logger.info("Solidity node is running."); - if (ObjectUtils.isEmpty(parameter.getTrustNodeAddr())) { + if (StringUtils.isEmpty(parameter.getTrustNodeAddr())) { throw new TronError(new IllegalArgumentException("Trust node is not set."), TronError.ErrCode.SOLID_NODE_INIT); } diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index d6d3ab236a6..0686890f030 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -750,7 +750,7 @@ vm = { # Omit the property entirely to keep the default behaviour of sharing the # block-processing deadline. Migration note: if previously running --debug # to extend constant calls, switch to this option (--debug also extends - # block-processing, which is unsafe; see issue #6266). + # block-processing, which is unsafe; see issue #6266). Default: 0. # constantCallTimeoutMs = 100 } diff --git a/framework/src/test/java/org/tron/common/ParameterTest.java b/framework/src/test/java/org/tron/common/ParameterTest.java index 563f487f635..91bb580a3b4 100644 --- a/framework/src/test/java/org/tron/common/ParameterTest.java +++ b/framework/src/test/java/org/tron/common/ParameterTest.java @@ -216,7 +216,6 @@ public void testCommonParameter() { assertEquals(1000, parameter.getRateLimiterGlobalQps()); parameter.setRateLimiterGlobalIpQps(100); assertEquals(100, parameter.getRateLimiterGlobalIpQps()); - assertNull(parameter.getOverlay()); assertNull(parameter.getEventPluginConfig()); assertNull(parameter.getEventFilter()); parameter.setCryptoEngine(ECKey_ENGINE); diff --git a/framework/src/test/java/org/tron/core/config/args/OverlayTest.java b/framework/src/test/java/org/tron/core/config/args/OverlayTest.java deleted file mode 100644 index 1b7045c5b21..00000000000 --- a/framework/src/test/java/org/tron/core/config/args/OverlayTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * java-tron is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * java-tron is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.tron.core.config.args; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class OverlayTest { - - private Overlay overlay = new Overlay(); - - @Before - public void setOverlay() { - overlay.setPort(8080); - } - - @Test(expected = IllegalArgumentException.class) - public void whenSetOutOfBoundsPort() { - overlay.setPort(-1); - } - - @Test - public void getOverlay() { - Assert.assertEquals(8080, overlay.getPort()); - } -} diff --git a/protocol/src/main/protos/api/api.proto b/protocol/src/main/protos/api/api.proto index 6082d989182..8b79c8cb0d3 100644 --- a/protocol/src/main/protos/api/api.proto +++ b/protocol/src/main/protos/api/api.proto @@ -2,7 +2,6 @@ syntax = "proto3"; package protocol; import "core/Tron.proto"; -import "google/api/annotations.proto"; import "core/contract/asset_issue_contract.proto"; import "core/contract/account_contract.proto"; From 260585c9397b4b6c0921ad097483533e61f813db Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Fri, 15 May 2026 15:02:21 +0800 Subject: [PATCH 10/57] fix(jsonrpc): make the JSON-RPC output more compliant with specification (#6763) --- .../core/services/jsonrpc/JsonRpcServlet.java | 49 +++++- .../services/jsonrpc/JsonRpcServletTest.java | 154 ++++++++++++++++++ 2 files changed, 196 insertions(+), 7 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index 2093930ca98..29869403988 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -1,6 +1,8 @@ package org.tron.core.services.jsonrpc; +import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.StreamReadConstraints; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -30,7 +32,19 @@ @Slf4j(topic = "API") public class JsonRpcServlet extends RateLimiterServlet { - private static final ObjectMapper MAPPER = new ObjectMapper(); + // Snapshot of node.http.maxNestingDepth / maxTokenCount at class-load time (after Args.setParam). + private static final ObjectMapper MAPPER = buildMapper(); + + private static ObjectMapper buildMapper() { + CommonParameter p = CommonParameter.getInstance(); + JsonFactory factory = JsonFactory.builder() + .streamReadConstraints(StreamReadConstraints.builder() + .maxNestingDepth(p.getMaxNestingDepth()) + .maxTokenCount(p.getMaxTokenCount()) + .build()) + .build(); + return new ObjectMapper(factory); + } private enum JsonRpcError { PARSE_ERROR(-32700), @@ -97,11 +111,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I try { rootNode = MAPPER.readTree(body); if (rootNode == null || rootNode.isMissingNode()) { - writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "Parse error", null, false); + writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false); return; } } catch (JsonProcessingException e) { - writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "Parse error", null, false); + writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false); + return; + } + + if (!rootNode.isObject() && !rootNode.isArray()) { + writeJsonRpcError(resp, JsonRpcError.INVALID_REQUEST, "Invalid Request", null, false); return; } @@ -159,8 +178,10 @@ private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxRes JsonNode subRequest = rootNode.get(i); if (overflow) { - // Notifications (no "id") do not get a response even on overflow. - if (subRequest.has("id")) { + if (!subRequest.isObject()) { + batchResult.add(buildErrorNode(JsonRpcError.INVALID_REQUEST, "Invalid Request", null)); + } else if (subRequest.has("id")) { + // Notifications (no "id") do not get a response even on overflow. batchResult.add(buildErrorNode(JsonRpcError.RESPONSE_TOO_LARGE, "Response exceeds the limit of " + maxResponseSize + " bytes", subRequest.get("id"))); @@ -168,6 +189,19 @@ private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxRes continue; } + if (!subRequest.isObject()) { + ObjectNode errNode = buildErrorNode(JsonRpcError.INVALID_REQUEST, "Invalid Request", null); + byte[] errBytes = MAPPER.writeValueAsBytes(errNode); + int addition = errBytes.length + (!batchResult.isEmpty() ? 1 : 0); + if (maxResponseSize > 0 && accumulatedSize + addition > maxResponseSize) { + overflow = true; + } else { + accumulatedSize += addition; + } + batchResult.add(errNode); + continue; + } + byte[] subBody; try { subBody = MAPPER.writeValueAsBytes(subRequest); @@ -213,13 +247,14 @@ private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxRes // JSON-RPC 2.0 §6: MUST NOT return an empty Array when there are no response objects. if (batchResult.isEmpty()) { + resp.setContentType("application/json-rpc"); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentLength(0); return; } byte[] finalBytes = MAPPER.writeValueAsBytes(batchResult); - resp.setContentType("application/json-rpc; charset=utf-8"); + resp.setContentType("application/json-rpc"); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentLength(finalBytes.length); resp.getOutputStream().write(finalBytes); @@ -261,7 +296,7 @@ private void writeJsonRpcError(HttpServletResponse resp, JsonRpcError error, Str } else { bytes = MAPPER.writeValueAsBytes(errorObj); } - resp.setContentType("application/json-rpc; charset=utf-8"); + resp.setContentType("application/json-rpc"); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentLength(bytes.length); resp.getOutputStream().write(bytes); diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java index fa45ca48876..b66298d6779 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java @@ -245,6 +245,160 @@ public void normalRequest_commitsRpcServerResponse() throws Exception { assertArrayEquals(rpcResp, resp.getContentAsByteArray()); } + // --- Content-Type header: must be application/json-rpc (no charset suffix) --- + + @Test + public void errorResponse_contentTypeIsApplicationJsonRpc() throws Exception { + MockHttpServletResponse resp = doPost("not valid json"); + assertEquals("application/json-rpc", resp.getContentType()); + } + + @Test + public void batchResponse_contentTypeIsApplicationJsonRpc() throws Exception { + byte[] singleResp = "{\"jsonrpc\":\"2.0\",\"result\":\"ok\",\"id\":1}" + .getBytes(StandardCharsets.UTF_8); + doAnswer(inv -> { + OutputStream out = inv.getArgument(1); + out.write(singleResp); + return 0; + }).when(mockRpcServer).handleRequest(any(InputStream.class), any(OutputStream.class)); + + MockHttpServletResponse resp = doPost("[{\"id\":1}]"); + assertEquals("application/json-rpc", resp.getContentType()); + } + + @Test + public void allNotificationBatch_contentTypeIsApplicationJsonRpc() throws Exception { + // notification: rpcServer returns 0 bytes → empty batchResult → early return path + doAnswer(inv -> 0).when(mockRpcServer) + .handleRequest(any(InputStream.class), any(OutputStream.class)); + + MockHttpServletResponse resp = doPost("[{\"method\":\"eth_blockNumber\"}]"); + assertEquals(200, resp.getStatus()); + assertEquals(0, resp.getContentLength()); + assertEquals("application/json-rpc", resp.getContentType()); + } + + // --- Primitive root node → Invalid Request (-32600), id must be JSON null --- + + @Test + public void primitiveRootNull_returnsInvalidRequestWithJsonNullId() throws Exception { + MockHttpServletResponse resp = doPost("null"); + assertEquals(200, resp.getStatus()); + JsonNode body = MAPPER.readTree(resp.getContentAsString()); + assertFalse(body.isArray()); + assertEquals("2.0", body.get("jsonrpc").asText()); + assertEquals(-32600, body.get("error").get("code").asInt()); + assertTrue("id must be JSON null, not the string \"null\"", body.get("id").isNull()); + assertFalse("id must not be a string", body.get("id").isTextual()); + } + + @Test + public void primitiveRootBoolean_returnsInvalidRequest() throws Exception { + MockHttpServletResponse resp = doPost("true"); + assertEquals(200, resp.getStatus()); + assertEquals(-32600, + MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + } + + @Test + public void primitiveRootNumber_returnsInvalidRequest() throws Exception { + MockHttpServletResponse resp = doPost("123"); + assertEquals(200, resp.getStatus()); + assertEquals(-32600, + MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + } + + @Test + public void primitiveRootString_returnsInvalidRequest() throws Exception { + MockHttpServletResponse resp = doPost("\"hello\""); + assertEquals(200, resp.getStatus()); + assertEquals(-32600, + MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + } + + // --- Non-object element inside a batch → Invalid Request per element --- + + @Test + public void batchWithNestedArray_returnsInvalidRequestArray() throws Exception { + MockHttpServletResponse resp = doPost("[[]]"); + assertEquals(200, resp.getStatus()); + JsonNode body = MAPPER.readTree(resp.getContentAsString()); + assertTrue("response must be a JSON array", body.isArray()); + assertEquals(1, body.size()); + assertEquals(-32600, body.get(0).get("error").get("code").asInt()); + assertTrue("id in batch error must be JSON null", body.get(0).get("id").isNull()); + } + + @Test + public void batchWithMixedObjectAndArray_objectProcessedArrayRejected() throws Exception { + byte[] singleResp = "{\"jsonrpc\":\"2.0\",\"result\":\"ok\",\"id\":1}" + .getBytes(StandardCharsets.UTF_8); + doAnswer(inv -> { + OutputStream out = inv.getArgument(1); + out.write(singleResp); + return 0; + }).when(mockRpcServer).handleRequest(any(InputStream.class), any(OutputStream.class)); + + MockHttpServletResponse resp = doPost("[{\"id\":1}, []]"); + assertEquals(200, resp.getStatus()); + JsonNode body = MAPPER.readTree(resp.getContentAsString()); + assertTrue("response must be a JSON array", body.isArray()); + assertEquals(2, body.size()); + assertEquals("ok", body.get(0).get("result").asText()); + assertEquals(-32600, body.get(1).get("error").get("code").asInt()); + } + + @Test + public void batchWithNumericAndStringElements_allGetInvalidRequest() throws Exception { + MockHttpServletResponse resp = doPost("[42, \"foo\", true]"); + assertEquals(200, resp.getStatus()); + JsonNode body = MAPPER.readTree(resp.getContentAsString()); + assertTrue("response must be a JSON array", body.isArray()); + assertEquals(3, body.size()); + for (int i = 0; i < 3; i++) { + assertEquals(-32600, body.get(i).get("error").get("code").asInt()); + } + } + + // --- StreamReadConstraints: maxNestingDepth and maxTokenCount must be enforced --- + + @Test + public void excessivelyNestedRequest_returnsParseError() throws Exception { + int limit = CommonParameter.getInstance().getMaxNestingDepth(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i <= limit; i++) { + sb.append('['); + } + sb.append('0'); + for (int i = 0; i <= limit; i++) { + sb.append(']'); + } + + MockHttpServletResponse resp = doPost(sb.toString()); + assertEquals(200, resp.getStatus()); + assertEquals(-32700, + MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + } + + @Test + public void tooManyTokens_returnsParseError() throws Exception { + int limit = CommonParameter.getInstance().getMaxTokenCount(); + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < limit; i++) { + if (i > 0) { + sb.append(','); + } + sb.append('0'); + } + sb.append(']'); + + MockHttpServletResponse resp = doPost(sb.toString()); + assertEquals(200, resp.getStatus()); + assertEquals(-32700, + MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + } + // --- helpers --- private MockHttpServletResponse doPost(String body) throws Exception { From 8c57f0babe21c009d45b14fe5d6e0fe525d09926 Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Thu, 21 May 2026 16:10:59 +0800 Subject: [PATCH 11/57] fix(net): map BLOCK_MERKLE_INVALID to BAD_BLOCK disconnect reason (#6791) - P2pEventHandlerImpl.processException's switch did not include the BLOCK_MERKLE_ERROR type introduced by PR #6716. The new type fell through to the default branch, so peers whose blocks failed merkle root validation were disconnected with ReasonCode.UNKNOWN instead of BAD_BLOCK, missing the bad-peer ban window in PeerConnection.processDisconnect. - Add the case alongside BAD_BLOCK and BLOCK_SIGN_INVALID so the disconnect reason is reported as BAD_BLOCK end-to-end. - Rename the two enum constants for naming consistency: BLOCK_SIGN_ERROR -> BLOCK_SIGN_INVALID, BLOCK_MERKLE_ERROR -> BLOCK_MERKLE_INVALID, and reword their descriptions to 'sign failed' / 'merkle failed' to match the imperative style used by the other nearby enum entries. All call sites updated. - Add two regression tests in P2pEventHandlerImplTest: one pins BLOCK_MERKLE_INVALID -> BAD_BLOCK (the actual fix); the other pins BLOCK_SIGN_INVALID -> BAD_BLOCK so a future switch refactor cannot silently drop either back to UNKNOWN. --- .../org/tron/core/exception/P2pException.java | 4 +- .../tron/core/net/P2pEventHandlerImpl.java | 3 +- .../org/tron/core/net/TronNetDelegate.java | 8 +-- .../core/net/service/sync/SyncService.java | 4 +- .../core/net/P2pEventHandlerImplTest.java | 52 +++++++++++++++++++ .../tron/core/net/TronNetDelegateTest.java | 2 +- 6 files changed, 63 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/tron/core/exception/P2pException.java b/common/src/main/java/org/tron/core/exception/P2pException.java index eae830627c2..5c2f21778a3 100644 --- a/common/src/main/java/org/tron/core/exception/P2pException.java +++ b/common/src/main/java/org/tron/core/exception/P2pException.java @@ -50,8 +50,8 @@ public enum TypeEnum { TRX_EXE_FAILED(12, "trx exe failed"), DB_ITEM_NOT_FOUND(13, "DB item not found"), PROTOBUF_ERROR(14, "protobuf inconsistent"), - BLOCK_SIGN_ERROR(15, "block sign error"), - BLOCK_MERKLE_ERROR(16, "block merkle error"), + BLOCK_SIGN_INVALID(15, "block sign invalid"), + BLOCK_MERKLE_INVALID(16, "block merkle invalid"), RATE_LIMIT_EXCEEDED(17, "rate limit exceeded"), DEFAULT(100, "default exception"); diff --git a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java index b9173b95cde..f703779c616 100644 --- a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java +++ b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java @@ -272,7 +272,8 @@ private void processException(PeerConnection peer, TronMessage msg, Exception ex code = Protocol.ReasonCode.BAD_TX; break; case BAD_BLOCK: - case BLOCK_SIGN_ERROR: + case BLOCK_SIGN_INVALID: + case BLOCK_MERKLE_INVALID: code = Protocol.ReasonCode.BAD_BLOCK; break; case NO_SUCH_MESSAGE: diff --git a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java index 804c3fffa39..5f1540b672e 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java +++ b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java @@ -312,7 +312,7 @@ public void processBlock(BlockCapsule block, boolean isSync) throws P2pException logger.error("Process block failed, {}, reason: {}", blockId.getString(), e.getMessage()); if (e instanceof BadBlockException && ((BadBlockException) e).getType().equals(CALC_MERKLE_ROOT_FAILED)) { - throw new P2pException(TypeEnum.BLOCK_MERKLE_ERROR, e); + throw new P2pException(TypeEnum.BLOCK_MERKLE_INVALID, e); } else { throw new P2pException(TypeEnum.BAD_BLOCK, e); } @@ -347,10 +347,10 @@ public void validSignature(BlockCapsule block) throws P2pException { flag = block.validateSignature(dbManager.getDynamicPropertiesStore(), dbManager.getAccountStore()); } catch (Exception e) { - throw new P2pException(TypeEnum.BLOCK_SIGN_ERROR, e); + throw new P2pException(TypeEnum.BLOCK_SIGN_INVALID, e); } if (!flag) { - throw new P2pException(TypeEnum.BLOCK_SIGN_ERROR, "valid signature failed."); + throw new P2pException(TypeEnum.BLOCK_SIGN_INVALID, "valid signature failed."); } } @@ -363,7 +363,7 @@ public boolean validBlock(BlockCapsule block) throws P2pException { try { block.validateMerkleRoot(); } catch (BadBlockException e) { - throw new P2pException(TypeEnum.BLOCK_MERKLE_ERROR, e.getMessage()); + throw new P2pException(TypeEnum.BLOCK_MERKLE_INVALID, e.getMessage()); } validSignature(block); return witnessScheduleStore.getActiveWitnesses().contains(block.getWitnessAddress()); diff --git a/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java b/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java index 0ffe69db097..bd656d9c41e 100644 --- a/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java +++ b/framework/src/main/java/org/tron/core/net/service/sync/SyncService.java @@ -342,8 +342,8 @@ private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection) } catch (P2pException p2pException) { logger.error("Process sync block {} failed, type: {}", blockId.getString(), p2pException.getType()); - attackFlag = p2pException.getType().equals(TypeEnum.BLOCK_SIGN_ERROR) - || p2pException.getType().equals(TypeEnum.BLOCK_MERKLE_ERROR); + attackFlag = p2pException.getType().equals(TypeEnum.BLOCK_SIGN_INVALID) + || p2pException.getType().equals(TypeEnum.BLOCK_MERKLE_INVALID); flag = false; } catch (Exception e) { logger.error("Process sync block {} failed", blockId.getString(), e); diff --git a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java index 2e79bbf5809..93b84450f7b 100644 --- a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java +++ b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java @@ -1,8 +1,10 @@ package org.tron.core.net; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import java.lang.reflect.Method; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import org.junit.Assert; @@ -14,6 +16,7 @@ import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Sha256Hash; import org.tron.core.config.args.Args; +import org.tron.core.exception.P2pException; import org.tron.core.net.message.TronMessage; import org.tron.core.net.message.adv.FetchInvDataMessage; import org.tron.core.net.message.adv.InventoryMessage; @@ -223,4 +226,53 @@ public void testUpdateLastInteractiveTime() throws Exception { method.invoke(p2pEventHandler, peer, message); Assert.assertTrue(peer.getLastInteractiveTime() >= t1); } + + /** + * Regression for PR #6716: validateMerkleRoot introduced + * P2pException.TypeEnum.BLOCK_MERKLE_INVALID, but processException's switch + * did not include the new type, so the peer was disconnected with + * ReasonCode.UNKNOWN instead of BAD_BLOCK. This test pins that + * BLOCK_MERKLE_INVALID is mapped to BAD_BLOCK (and gets the bad-peer ban + * window via PeerConnection.processDisconnect). + */ + @Test + public void testProcessExceptionMapsBlockMerkleErrorToBadBlock() throws Exception { + P2pEventHandlerImpl handler = new P2pEventHandlerImpl(); + PeerConnection peer = mock(PeerConnection.class); + Mockito.when(peer.getInetSocketAddress()) + .thenReturn(new InetSocketAddress("127.0.0.1", 18888)); + + P2pException ex = new P2pException( + P2pException.TypeEnum.BLOCK_MERKLE_INVALID, "merkle mismatch"); + + Method method = handler.getClass().getDeclaredMethod("processException", + PeerConnection.class, TronMessage.class, Exception.class); + method.setAccessible(true); + method.invoke(handler, peer, null, ex); + + verify(peer).disconnect(Protocol.ReasonCode.BAD_BLOCK); + } + + /** + * Companion sanity check: BLOCK_SIGN_INVALID already mapped correctly + * before this fix; pin it so future refactors do not silently drop it + * (or BLOCK_MERKLE_INVALID) back to UNKNOWN. + */ + @Test + public void testProcessExceptionMapsBlockSignErrorToBadBlock() throws Exception { + P2pEventHandlerImpl handler = new P2pEventHandlerImpl(); + PeerConnection peer = mock(PeerConnection.class); + Mockito.when(peer.getInetSocketAddress()) + .thenReturn(new InetSocketAddress("127.0.0.1", 18888)); + + P2pException ex = new P2pException( + P2pException.TypeEnum.BLOCK_SIGN_INVALID, "bad signature"); + + Method method = handler.getClass().getDeclaredMethod("processException", + PeerConnection.class, TronMessage.class, Exception.class); + method.setAccessible(true); + method.invoke(handler, peer, null, ex); + + verify(peer).disconnect(Protocol.ReasonCode.BAD_BLOCK); + } } diff --git a/framework/src/test/java/org/tron/core/net/TronNetDelegateTest.java b/framework/src/test/java/org/tron/core/net/TronNetDelegateTest.java index 7e584581d2b..4c16f28930c 100644 --- a/framework/src/test/java/org/tron/core/net/TronNetDelegateTest.java +++ b/framework/src/test/java/org/tron/core/net/TronNetDelegateTest.java @@ -169,7 +169,7 @@ public void testValidBlockMerkleRoot() throws Exception { tronNetDelegate.validBlock(tampered); Assert.fail("Expected P2pException for tampered merkle root"); } catch (P2pException e) { - Assert.assertEquals(TypeEnum.BLOCK_MERKLE_ERROR, e.getType()); + Assert.assertEquals(TypeEnum.BLOCK_MERKLE_INVALID, e.getType()); } } } From 78bc75d71df3cd57d22b2e337061b293af487a2d Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Thu, 21 May 2026 18:25:53 +0800 Subject: [PATCH 12/57] fix(security): re-verify block signature during fork replay (#6777) - Manager.switchFork now re-validates each replayed block's witness signature before applying. The witness account's permission can change between forks (via permission-update transactions), so a signature that was valid on the original chain may no longer be valid on the replay path. - Use `if (!validateSignature(...)) throw new ValidateSignatureException` rather than discarding the boolean return: validateSignature only throws on malformed signature bytes; an attacker-supplied valid-format signature with a wrong-signer address returns false. Discarding the return would let that attack through. - The existing switchFork catch list already includes ValidateSignatureException, so the new throw is wired into the existing switchback path with no additional handling. - Add three BlockCapsule.validateSignature contract tests pinning the two failure modes the fix relies on: signer-mismatch returns false, signer-match returns true, and a 65-byte malformed signature throws ValidateSignatureException. --- .../main/java/org/tron/core/db/Manager.java | 5 + .../tron/core/capsule/BlockCapsuleTest.java | 97 +++++++++++ .../org/tron/core/db/ManagerMockTest.java | 158 ++++++++++++++++++ 3 files changed, 260 insertions(+) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index a534b9d1c5d..667bfce034c 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1140,6 +1140,11 @@ private void switchFork(BlockCapsule newHead) Exception exception = null; // todo process the exception carefully later try (ISession tmpSession = revokingStore.buildSession()) { + if (!item.getBlk().validateSignature( + getDynamicPropertiesStore(), getAccountStore())) { + throw new ValidateSignatureException( + "switch fork: block " + item.getBlk().getNum() + " signature invalid"); + } applyBlock(item.getBlk().setSwitch(true)); tmpSession.commit(); } catch (AccountResourceInsufficientException diff --git a/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java b/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java index ca0844c2c16..b258fbf99a1 100644 --- a/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java +++ b/framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java @@ -1,5 +1,8 @@ package org.tron.core.capsule; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import com.google.protobuf.ByteString; import java.io.IOException; import java.util.ArrayList; @@ -21,6 +24,11 @@ import org.tron.core.config.args.Args; import org.tron.core.exception.BadBlockException; import org.tron.core.exception.BadItemException; +import org.tron.core.exception.ValidateSignatureException; +import org.tron.core.store.AccountStore; +import org.tron.core.store.DynamicPropertiesStore; +import org.tron.protos.Protocol.Block; +import org.tron.protos.Protocol.BlockHeader; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.BalanceContract.TransferContract; @@ -180,6 +188,95 @@ public void testGetTimeStamp() { Assert.assertEquals(1234L, blockCapsule0.getTimeStamp()); } + /** + * Pin the contract that switchFork's signature recheck relies on: + * when the recovered signer address does not match the witness address, + * validateSignature returns false (no exception). switchFork uses the + * boolean return to decide whether to throw, so this contract is what + * makes the fix work for "wrong signer" attacks. + */ + @Test + public void testValidateSignatureReturnsFalseWhenSignerMismatch() throws Exception { + String signerKey = PublicMethod.getRandomPrivateKey(); + String witnessKey = PublicMethod.getRandomPrivateKey(); + byte[] witnessAddress = PublicMethod.getAddressByteByPrivateKey(witnessKey); + + BlockCapsule block = new BlockCapsule(2, + Sha256Hash.wrap(ByteString.copyFrom(ByteArray.fromHexString( + "9938a342238077182498b464ac0292229938a342238077182498b464ac029222"))), + 4321, + ByteString.copyFrom(witnessAddress)); + block.sign(ByteArray.fromHexString(signerKey)); + + DynamicPropertiesStore dps = mock(DynamicPropertiesStore.class); + when(dps.getAllowMultiSign()).thenReturn(0L); + AccountStore accountStore = mock(AccountStore.class); + + Assert.assertFalse(block.validateSignature(dps, accountStore)); + } + + /** + * Same key path under the happy case: when signer == witness, validateSignature + * returns true. Guards against any future refactor that accidentally inverts + * the comparison or strips the witness check. + */ + @Test + public void testValidateSignatureReturnsTrueWhenSignerMatches() throws Exception { + String key = PublicMethod.getRandomPrivateKey(); + byte[] witnessAddress = PublicMethod.getAddressByteByPrivateKey(key); + + BlockCapsule block = new BlockCapsule(3, + Sha256Hash.wrap(ByteString.copyFrom(ByteArray.fromHexString( + "9938a342238077182498b464ac0292229938a342238077182498b464ac029222"))), + 5678, + ByteString.copyFrom(witnessAddress)); + block.sign(ByteArray.fromHexString(key)); + + DynamicPropertiesStore dps = mock(DynamicPropertiesStore.class); + when(dps.getAllowMultiSign()).thenReturn(0L); + AccountStore accountStore = mock(AccountStore.class); + + Assert.assertTrue(block.validateSignature(dps, accountStore)); + } + + /** + * The other failure mode switchFork must handle: signature bytes are + * malformed (cannot recover a public key). validateSignature wraps the + * underlying SignatureException as ValidateSignatureException, which the + * existing catch block in switchFork already handles. + */ + @Test(expected = ValidateSignatureException.class) + public void testValidateSignatureThrowsForMalformedSignature() throws Exception { + byte[] witnessAddress = PublicMethod.getAddressByteByPrivateKey( + PublicMethod.getRandomPrivateKey()); + + // 65-byte signature with valid length but garbage content — passes Rsv parsing + // but fails ECDSA recovery, surfacing SignatureException → ValidateSignatureException. + byte[] garbageSigBytes = new byte[65]; + Arrays.fill(garbageSigBytes, (byte) 0xAB); + ByteString garbageSig = ByteString.copyFrom(garbageSigBytes); + + BlockHeader.raw rawData = BlockHeader.raw.newBuilder() + .setNumber(4) + .setTimestamp(1111) + .setParentHash(ByteString.copyFrom(ByteArray.fromHexString( + "9938a342238077182498b464ac0292229938a342238077182498b464ac029222"))) + .setWitnessAddress(ByteString.copyFrom(witnessAddress)) + .build(); + BlockHeader header = BlockHeader.newBuilder() + .setRawData(rawData) + .setWitnessSignature(garbageSig) + .build(); + Block proto = Block.newBuilder().setBlockHeader(header).build(); + BlockCapsule block = new BlockCapsule(proto); + + DynamicPropertiesStore dps = mock(DynamicPropertiesStore.class); + when(dps.getAllowMultiSign()).thenReturn(0L); + AccountStore accountStore = mock(AccountStore.class); + + block.validateSignature(dps, accountStore); + } + @Test public void testConcurrentToString() throws InterruptedException { List threadList = new ArrayList<>(); diff --git a/framework/src/test/java/org/tron/core/db/ManagerMockTest.java b/framework/src/test/java/org/tron/core/db/ManagerMockTest.java index e3de0441c97..946bef022d2 100644 --- a/framework/src/test/java/org/tron/core/db/ManagerMockTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerMockTest.java @@ -5,12 +5,14 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.protobuf.Any; @@ -22,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import lombok.SneakyThrows; @@ -42,6 +45,7 @@ import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.ProgramResult; import org.tron.common.runtime.vm.LogInfo; +import org.tron.common.utils.Pair; import org.tron.common.utils.Sha256Hash; import org.tron.core.ChainBaseManager; import org.tron.core.capsule.BlockCapsule; @@ -49,6 +53,7 @@ import org.tron.core.capsule.TransactionInfoCapsule; import org.tron.core.capsule.utils.TransactionUtil; import org.tron.core.config.args.Args; +import org.tron.core.db2.ISession; import org.tron.core.exception.ContractSizeNotEqualToOneException; import org.tron.core.exception.DupTransactionException; import org.tron.core.exception.ItemNotFoundException; @@ -564,4 +569,157 @@ public void testPostContractTriggerSwallowsThrowable() throws Exception { } } + /** + * Covers the fork-replay signature recheck added in this PR: + * when a block being re-applied during switchFork fails witness signature + * validation, the new `if (!validateSignature) throw` block must fire, + * surfacing ValidateSignatureException through the existing catch list. + * + *

Strategy: spy(Manager), inject mocked khaosDb/revokingStore/chainBaseManager + * so switchFork enters the first apply loop with a single mock block whose + * validateSignature returns false. The throw is exercised; downstream + * switchback/finally exceptions from partially-mocked applyBlock are tolerated + * since the throw line is already executed before they run. + */ + @SneakyThrows + @Test + public void testSwitchForkRejectsBlockWithInvalidSignature() { + Manager dbManager = spy(new Manager()); + + // chainBaseManager + stores so getDynamicPropertiesStore() / getAccountStore() resolve. + ChainBaseManager cbm = mock(ChainBaseManager.class); + DynamicPropertiesStore dps = mock(DynamicPropertiesStore.class); + AccountStore accountStore = mock(AccountStore.class); + Sha256Hash sharedHash = Sha256Hash.ZERO_HASH; + when(cbm.getDynamicPropertiesStore()).thenReturn(dps); + when(cbm.getAccountStore()).thenReturn(accountStore); + when(dps.getLatestBlockHeaderHash()).thenReturn(sharedHash); + setField(dbManager, "chainBaseManager", cbm); + + // revokingStore.buildSession() returns a no-op ISession. + RevokingDatabase revokingStore = mock(RevokingDatabase.class); + ISession session = mock(ISession.class); + when(revokingStore.buildSession()).thenReturn(session); + setField(dbManager, "revokingStore", revokingStore); + + // khaosDb.getBranch returns (first=[badBlock], value=[oldBlock]). + // The bad block goes into the apply loop; the old block lets the while + // loops in the rollback/switchback paths exit immediately by matching + // parent hash to the current head hash. + KhaosDatabase khaosDb = mock(KhaosDatabase.class); + setField(dbManager, "khaosDb", khaosDb); + + BlockCapsule badBlock = mock(BlockCapsule.class); + BlockCapsule.BlockId badBlockId = mock(BlockCapsule.BlockId.class); + when(badBlock.getBlockId()).thenReturn(badBlockId); + when(badBlock.getNum()).thenReturn(100L); + when(badBlock.validateSignature(any(DynamicPropertiesStore.class), + any(AccountStore.class))).thenReturn(false); + + BlockCapsule oldBlock = mock(BlockCapsule.class); + BlockCapsule.BlockId oldBlockId = mock(BlockCapsule.BlockId.class); + when(oldBlock.getBlockId()).thenReturn(oldBlockId); + when(oldBlock.getParentHash()).thenReturn(sharedHash); + + LinkedList first = new LinkedList<>(); + first.add(new KhaosDatabase.KhaosBlock(badBlock)); + LinkedList value = new LinkedList<>(); + value.add(new KhaosDatabase.KhaosBlock(oldBlock)); + when(khaosDb.getBranch(any(BlockCapsule.BlockId.class), any(Sha256Hash.class))) + .thenReturn(new Pair<>(first, value)); + + Method switchFork = Manager.class.getDeclaredMethod("switchFork", BlockCapsule.class); + switchFork.setAccessible(true); + + // The throw fires before the finally's switchback runs. Switchback's applyBlock + // may surface another exception due to partial mocks; we tolerate any throwable + // here because the new code's throw has already been executed (line covered). + try { + switchFork.invoke(dbManager, badBlock); + } catch (Throwable ignored) { + // expected: switchback path partially mocked + } + + // The fix's contract: validateSignature was invoked on the replayed block. + verify(badBlock, atLeastOnce()).validateSignature( + any(DynamicPropertiesStore.class), any(AccountStore.class)); + } + + /** + * Symmetric "happy path" coverage: when validateSignature returns true, the + * throw is skipped and execution continues to applyBlock. Pins that the + * new check correctly inverts the boolean (no off-by-one in the `!`). + */ + @SneakyThrows + @Test + public void testSwitchForkPassesValidSignatureBlockToApply() { + Manager dbManager = spy(new Manager()); + + ChainBaseManager cbm = mock(ChainBaseManager.class); + DynamicPropertiesStore dps = mock(DynamicPropertiesStore.class); + AccountStore accountStore = mock(AccountStore.class); + Sha256Hash sharedHash = Sha256Hash.ZERO_HASH; + when(cbm.getDynamicPropertiesStore()).thenReturn(dps); + when(cbm.getAccountStore()).thenReturn(accountStore); + when(dps.getLatestBlockHeaderHash()).thenReturn(sharedHash); + setField(dbManager, "chainBaseManager", cbm); + + RevokingDatabase revokingStore = mock(RevokingDatabase.class); + ISession session = mock(ISession.class); + when(revokingStore.buildSession()).thenReturn(session); + setField(dbManager, "revokingStore", revokingStore); + + KhaosDatabase khaosDb = mock(KhaosDatabase.class); + setField(dbManager, "khaosDb", khaosDb); + + BlockCapsule goodBlock = mock(BlockCapsule.class); + BlockCapsule.BlockId goodBlockId = mock(BlockCapsule.BlockId.class); + when(goodBlock.getBlockId()).thenReturn(goodBlockId); + when(goodBlock.getNum()).thenReturn(100L); + when(goodBlock.validateSignature(any(DynamicPropertiesStore.class), + any(AccountStore.class))).thenReturn(true); + // setSwitch returns self for chained call from applyBlock argument expression. + when(goodBlock.setSwitch(true)).thenReturn(goodBlock); + + LinkedList first = new LinkedList<>(); + first.add(new KhaosDatabase.KhaosBlock(goodBlock)); + LinkedList value = new LinkedList<>(); + when(khaosDb.getBranch(any(BlockCapsule.BlockId.class), any(Sha256Hash.class))) + .thenReturn(new Pair<>(first, value)); + + Method switchFork = Manager.class.getDeclaredMethod("switchFork", BlockCapsule.class); + switchFork.setAccessible(true); + try { + switchFork.invoke(dbManager, goodBlock); + } catch (Throwable ignored) { + // applyBlock against a mocked BlockCapsule will NPE somewhere; tolerated. + } + + // Validation ran AND setSwitch was reached — proves the `if` did not short-circuit + // on the false branch when validateSignature returned true. + verify(goodBlock, atLeastOnce()).validateSignature( + any(DynamicPropertiesStore.class), any(AccountStore.class)); + verify(goodBlock, atLeastOnce()).setSwitch(true); + } + + private static void setField(Object target, String name, Object value) throws Exception { + Field f = target.getClass().getSuperclass() != null + ? findField(target.getClass(), name) + : target.getClass().getDeclaredField(name); + f.setAccessible(true); + f.set(target, value); + } + + private static Field findField(Class cls, String name) throws NoSuchFieldException { + Class c = cls; + while (c != null) { + try { + return c.getDeclaredField(name); + } catch (NoSuchFieldException e) { + c = c.getSuperclass(); + } + } + throw new NoSuchFieldException(name); + } + } \ No newline at end of file From 2c50400fe8b998c8abc086fa29f63d7d2496bd4b Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Fri, 22 May 2026 17:33:33 +0800 Subject: [PATCH 13/57] fix(security): cover consumed permission-change tx in getVerifyTxs (#6796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Initialize multiAddresses from ownerAddressSet so that getVerifyTxs still forces re-verification of an owner's later txs even after the owner's permission-change tx has been packed and is no longer present in pendingTransactions. Main changes: - Manager.getVerifyTxs: seed multiAddresses with ownerAddressSet to cover the case where a permission-change tx has been consumed but ownerAddressSet still retains the owner (kept alive by in-flight txs in pushTransactionQueue / rePushTransactions via filterOwnerAddress). - ManagerTest: add getVerifyTxsSkipsBlockWhenPermissionTxAlreadyConsumed reproducing the bypass — pending contains only B (transfer, old sig, isVerified=true), ownerAddressSet contains the owner, block contains only B without the permission-change tx. Assertion checks B is placed in the re-verify list instead of being short-circuited via setVerified. --- .../main/java/org/tron/core/db/Manager.java | 2 +- .../java/org/tron/core/db/ManagerTest.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 667bfce034c..3de0260b5c8 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1235,7 +1235,7 @@ public List getVerifyTxs(BlockCapsule block) { List txs = new ArrayList<>(); Map txMap = new HashMap<>(); - Set multiAddresses = new HashSet<>(); + Set multiAddresses = new HashSet<>(ownerAddressSet); pendingTransactions.forEach(capsule -> { String txId = Hex.toHexString(capsule.getTransactionId().getBytes()); diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index 87b4fcfdc77..717d6c4cf64 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -23,6 +23,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -876,6 +877,47 @@ public void getVerifyTxsTest() { Assert.assertEquals(txs.size(), 1); } + @Test + public void getVerifyTxsSkipsBlockWhenPermissionTxAlreadyConsumed() throws Exception { + // Scenario: a permission-change tx (A) for owner X has been processed and consumed, + // so it is no longer in pendingTransactions but ownerAddressSet still contains X. + // A later transfer tx (B) from X with the old signature enters pending with + // isVerified=true. A malicious SR produces a block containing only B (no A). + // getVerifyTxs must place B into the re-verify list rather than calling + // setVerified(true) just because B matches the pending entry. + TransferContract bContract = TransferContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom("f1".getBytes())) + .setAmount(7).build(); + TransactionCapsule bTx = new TransactionCapsule(bContract, ContractType.TransferContract); + String hexOwner = ByteArray.toHexString("f1".getBytes()); + + dbManager.getPendingTransactions().clear(); + dbManager.getPendingTransactions().add(bTx); + + Field field = Manager.class.getDeclaredField("ownerAddressSet"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + Set ownerAddressSet = (Set) field.get(dbManager); + Set backup = new HashSet<>(ownerAddressSet); + ownerAddressSet.clear(); + ownerAddressSet.add(hexOwner); + + try { + List blockTxs = new ArrayList<>(); + blockTxs.add(bTx.getInstance()); + BlockCapsule capsule = new BlockCapsule(0, ByteString.EMPTY, 0, blockTxs); + + List txs = dbManager.getVerifyTxs(capsule); + + Assert.assertEquals(1, txs.size()); + Assert.assertEquals(bTx.getTransactionId(), txs.get(0).getTransactionId()); + } finally { + ownerAddressSet.clear(); + ownerAddressSet.addAll(backup); + dbManager.getPendingTransactions().clear(); + } + } + @Test public void doNotSwitch() throws ValidateSignatureException, ContractValidateException, ContractExeException, From af882695176b1ce8899afd901cf3e176b5540f73 Mon Sep 17 00:00:00 2001 From: Asuka Date: Mon, 25 May 2026 12:33:16 +0800 Subject: [PATCH 14/57] fix(vm): canonicalize ModExp zero modulus output (TIP-871) (#6780) * fix(vm): canonicalize ModExp zero modulus output (TIP-871) * func(vm): remove account name for history block hash contract * test(db): align BlockHashHistory account name assertion --- .../tron/core/vm/PrecompiledContracts.java | 3 + .../org/tron/core/vm/program/Program.java | 4 + .../tron/core/db/HistoryBlockHashUtil.java | 5 +- .../common/runtime/vm/AllowTvmOsakaTest.java | 40 ++++ .../runtime/vm/TvmIssueVerifierTest.java | 209 ++++++++++++++++++ .../db/HistoryBlockHashIntegrationTest.java | 4 +- 6 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 framework/src/test/java/org/tron/common/runtime/vm/TvmIssueVerifierTest.java diff --git a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java index 1ac96b9d59d..0dc8fb31ada 100644 --- a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java +++ b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java @@ -705,6 +705,9 @@ public Pair execute(byte[] data) { // check if modulus is zero if (isZero(mod)) { + if (VMConfig.allowTvmOsaka()) { + return Pair.of(true, new byte[modLen]); + } return Pair.of(true, EMPTY_BYTE_ARRAY); } diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 80d972041dc..3ed968e1afa 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1616,6 +1616,10 @@ public ProgramTrace getTrace() { } public void createContract2(DataWord value, DataWord memStart, DataWord memSize, DataWord salt) { + if (VMConfig.allowTvmOsaka()) { + returnDataBuffer = null; // reset return buffer right before the call + } + byte[] senderAddress; if (VMConfig.allowTvmCompatibleEvm() && getCallDeep() == MAX_DEPTH) { stackPushZero(); diff --git a/framework/src/main/java/org/tron/core/db/HistoryBlockHashUtil.java b/framework/src/main/java/org/tron/core/db/HistoryBlockHashUtil.java index 19a0e278e08..36f7ee4928d 100644 --- a/framework/src/main/java/org/tron/core/db/HistoryBlockHashUtil.java +++ b/framework/src/main/java/org/tron/core/db/HistoryBlockHashUtil.java @@ -52,14 +52,13 @@ public class HistoryBlockHashUtil { // Account template for the new-account branch of {@code deploy()} (no prior // state at the canonical address). Equivalent to create2's - // {@code createAccount(addr, name, Contract)}: only type, accountName, and - // address are set. The pre-existing-account branch never uses this template + // {@code createAccount(addr, Contract)}: only type, and address + // are set. The pre-existing-account branch never uses this template // — it mutates the existing capsule in place to preserve balance / asset // state, mirroring the CREATE2 collision path. Safe to share: the proto is // immutable, and AccountCapsule mutations rebuild via {@code toBuilder}. private static final Account HISTORY_STORAGE_ACCOUNT = Account.newBuilder() .setType(Protocol.AccountType.Contract) - .setAccountName(ByteString.copyFromUtf8(HISTORY_STORAGE_NAME)) .setAddress(ByteString.copyFrom(HISTORY_STORAGE_ADDRESS)) .build(); diff --git a/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java b/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java index c7000175b00..8e2ab59b1f7 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java @@ -80,6 +80,46 @@ private static long getEnergy(int baseLen, int expLen, int modLen, byte[] expVal return modExp.getEnergyForData(buildModExpData(baseLen, expLen, modLen, expValue)); } + @Test + public void testModExpZeroModulusOutputLengthGatedByOsaka() { + ConfigLoader.disable = true; + + byte[] modLenZero = buildModExpData(1, 1, 0, new byte[]{0x03}); + byte[] modLenOne = buildModExpData(1, 1, 1, new byte[]{0x03}); + byte[] modLen32 = buildModExpData(1, 1, 32, new byte[]{0x03}); + + try { + VMConfig.initAllowTvmOsaka(0); + Pair result = modExp.execute(modLenZero); + Assert.assertTrue(result.getLeft()); + Assert.assertEquals(0, result.getRight().length); + + result = modExp.execute(modLenOne); + Assert.assertTrue(result.getLeft()); + Assert.assertEquals(0, result.getRight().length); + + result = modExp.execute(modLen32); + Assert.assertTrue(result.getLeft()); + Assert.assertEquals(0, result.getRight().length); + + VMConfig.initAllowTvmOsaka(1); + result = modExp.execute(modLenZero); + Assert.assertTrue(result.getLeft()); + Assert.assertEquals(0, result.getRight().length); + + result = modExp.execute(modLenOne); + Assert.assertTrue(result.getLeft()); + Assert.assertArrayEquals(new byte[1], result.getRight()); + + result = modExp.execute(modLen32); + Assert.assertTrue(result.getLeft()); + Assert.assertArrayEquals(new byte[32], result.getRight()); + } finally { + VMConfig.initAllowTvmOsaka(0); + ConfigLoader.disable = false; + } + } + @Test public void testEIP7883ModExpPricing() { ConfigLoader.disable = true; diff --git a/framework/src/test/java/org/tron/common/runtime/vm/TvmIssueVerifierTest.java b/framework/src/test/java/org/tron/common/runtime/vm/TvmIssueVerifierTest.java new file mode 100644 index 00000000000..4936ec8f4c8 --- /dev/null +++ b/framework/src/test/java/org/tron/common/runtime/vm/TvmIssueVerifierTest.java @@ -0,0 +1,209 @@ +package org.tron.common.runtime.vm; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.runtime.TVMTestResult; +import org.tron.common.runtime.TvmTestUtils; +import org.tron.common.utils.WalletUtil; +import org.tron.common.utils.client.utils.AbiUtil; +import org.tron.core.exception.ContractExeException; +import org.tron.core.exception.ContractValidateException; +import org.tron.core.exception.ReceiptCheckErrException; +import org.tron.core.exception.VMIllegalException; +import org.tron.core.vm.config.ConfigLoader; +import org.tron.protos.Protocol.Transaction; + +public class TvmIssueVerifierTest extends VMTestBase { + + private static final int WORD_SIZE = 32; + + private static final String ABI = + "[{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"code\",\"type\":\"bytes\"}," + + "{\"internalType\":\"uint256\",\"name\":\"salt\",\"type\":\"uint256\"}]," + + "\"name\":\"failedCreate2KeepsPriorReturnData\"," + + "\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"beforeSize\"," + + "\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"created\"," + + "\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"afterSize\"," + + "\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}," + + "{\"inputs\":[],\"name\":\"modexpZeroModulus\"," + + "\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"ok\"," + + "\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"sizeAfter\"," + + "\"type\":\"uint256\"},{\"internalType\":\"bytes32\",\"name\":\"copiedWord\"," + + "\"type\":\"bytes32\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}," + + "{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"code\",\"type\":\"bytes\"}," + + "{\"internalType\":\"uint256\",\"name\":\"salt\",\"type\":\"uint256\"}]," + + "\"name\":\"successfulCreate2KeepsPriorReturnData\"," + + "\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"beforeSize\"," + + "\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"created\"," + + "\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"afterSize\"," + + "\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"createdSize\"," + + "\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + + private static final String BYTECODE = + "6080604052348015600f57600080fd5b506105198061001f6000396000f3fe6080604052348015610010" + + "57600080fd5b50600436106100415760003560e01c80634ecba0f014610046578063543b525514610079" + + "5780639fefb5fd146100ab575b600080fd5b610060600480360381019061005b919061036b565b6100cb" + + "565b6040516100709493929190610417565b60405180910390f35b610093600480360381019061008e91" + + "9061036b565b610104565b6040516100a29392919061045c565b60405180910390f35b6100b361013656" + + "5b6040516100c2939291906104ac565b60405180910390f35b6000806000806112346000526020600060" + + "2060008060045af1503d9350848651602088016000f592503d9150823b905092959194509250565b6000" + + "80600061123460005260206000602060008060045af1503d9250838551602087016001f591503d905092" + + "50925092565b600080600080606367ffffffffffffffff8111156101575761015661020a565b5b604051" + + "9080825280601f01601f1916602001820160405280156101895781602001600182028036833780820191" + + "505090505b50905060208101600181526001602082015260016040820152600260608201536003606182" + + "01536000606282015360001960005260206000606383600060055af194503d9350600051925050509091" + + "92565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301" + + "169050919050565b7f4e487b710000000000000000000000000000000000000000000000000000000060" + + "0052604160045260246000fd5b610242826101f9565b810181811067ffffffffffffffff821117156102" + + "615761026061020a565b5b80604052505050565b60006102746101db565b90506102808282610239565b" + + "919050565b600067ffffffffffffffff8211156102a05761029f61020a565b5b6102a9826101f9565b90" + + "50602081019050919050565b82818337600083830152505050565b60006102d86102d384610285565b61" + + "026a565b9050828152602081018484840111156102f4576102f36101f4565b5b6102ff8482856102b656" + + "5b509392505050565b600082601f83011261031c5761031b6101ef565b5b813561032c84826020860161" + + "02c5565b91505092915050565b6000819050919050565b61034881610335565b811461035357600080fd" + + "5b50565b6000813590506103658161033f565b92915050565b6000806040838503121561038257610381" + + "6101e5565b5b600083013567ffffffffffffffff8111156103a05761039f6101ea565b5b6103ac858286" + + "01610307565b92505060206103bd85828601610356565b9150509250929050565b6103d081610335565b" + + "82525050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006104" + + "01826103d6565b9050919050565b610411816103f6565b82525050565b600060808201905061042c6000" + + "8301876103c7565b6104396020830186610408565b61044660408301856103c7565b6104536060830184" + + "6103c7565b95945050505050565b600060608201905061047160008301866103c7565b61047e60208301" + + "85610408565b61048b60408301846103c7565b949350505050565b6000819050919050565b6104a68161" + + "0493565b82525050565b60006060820190506104c160008301866103c7565b6104ce60208301856103c7" + + "565b6104db604083018461049d565b94935050505056fea2646970667358221220c9b28608a5295f3b52" + + "702e75aa5d40b18593bd0a9ff2e03e2274edbd42642c6a64736f6c634300081e0033"; + + @Before + public void enableVmFeatures() { + ConfigLoader.disable = false; + manager.getDynamicPropertiesStore().saveAllowTvmTransferTrc10(1); + manager.getDynamicPropertiesStore().saveAllowTvmConstantinople(1); + manager.getDynamicPropertiesStore().saveAllowTvmIstanbul(1); + manager.getDynamicPropertiesStore().saveAllowTvmLondon(1); + manager.getDynamicPropertiesStore().saveAllowTvmCompatibleEvm(1); + } + + @Test + public void verifyTvmOsakaFixesWithSolidity() + throws ContractExeException, ReceiptCheckErrException, + VMIllegalException, ContractValidateException { + byte[] verifierAddress = deployVerifier(); + + manager.getDynamicPropertiesStore().saveAllowTvmOsaka(0); + + TVMTestResult modExpResult = + trigger(verifierAddress, "modexpZeroModulus()", Collections.emptyList(), 100_000_000L); + byte[] modExpReturn = modExpResult.getRuntime().getResult().getHReturn(); + Assert.assertNull(modExpResult.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.ONE, word(modExpReturn, 0)); + Assert.assertEquals("MODEXP zero modulus currently returns empty returndata", + BigInteger.ZERO, word(modExpReturn, 1)); + + TVMTestResult create2Result = + trigger(verifierAddress, "failedCreate2KeepsPriorReturnData(bytes,uint256)", + Arrays.asList("00", 7L), 100_000_000L); + byte[] create2Return = create2Result.getRuntime().getResult().getHReturn(); + Assert.assertNull(create2Result.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.valueOf(32), word(create2Return, 0)); + Assert.assertEquals(BigInteger.ZERO, word(create2Return, 1)); + Assert.assertEquals("failed CREATE2 keeps the previous 32-byte return data buffer", + BigInteger.valueOf(32), word(create2Return, 2)); + + TVMTestResult preOsakaCreate2SuccessResult = + trigger(verifierAddress, "successfulCreate2KeepsPriorReturnData(bytes,uint256)", + Arrays.asList(initCodeReturningRuntime("00"), 8L), 100_000_000L); + byte[] preOsakaCreate2SuccessReturn = + preOsakaCreate2SuccessResult.getRuntime().getResult().getHReturn(); + Assert.assertNull(preOsakaCreate2SuccessResult.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.valueOf(32), word(preOsakaCreate2SuccessReturn, 0)); + Assert.assertTrue(word(preOsakaCreate2SuccessReturn, 1).signum() != 0); + Assert.assertEquals(BigInteger.valueOf(32), word(preOsakaCreate2SuccessReturn, 2)); + Assert.assertEquals(BigInteger.ONE, word(preOsakaCreate2SuccessReturn, 3)); + + manager.getDynamicPropertiesStore().saveAllowTvmOsaka(1); + + modExpResult = + trigger(verifierAddress, "modexpZeroModulus()", Collections.emptyList(), 100_000_000L); + modExpReturn = modExpResult.getRuntime().getResult().getHReturn(); + Assert.assertNull(modExpResult.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.ONE, word(modExpReturn, 0)); + Assert.assertEquals("MODEXP zero modulus returns modLen bytes after Osaka", + BigInteger.ONE, word(modExpReturn, 1)); + + create2Result = + trigger(verifierAddress, "failedCreate2KeepsPriorReturnData(bytes,uint256)", + Arrays.asList("00", 7L), 100_000_000L); + create2Return = create2Result.getRuntime().getResult().getHReturn(); + Assert.assertNull(create2Result.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.valueOf(32), word(create2Return, 0)); + Assert.assertEquals(BigInteger.ZERO, word(create2Return, 1)); + Assert.assertEquals("failed CREATE2 clears the previous return data buffer after Osaka", + BigInteger.ZERO, word(create2Return, 2)); + + TVMTestResult create2SuccessResult = + trigger(verifierAddress, "successfulCreate2KeepsPriorReturnData(bytes,uint256)", + Arrays.asList(initCodeReturningRuntime("00"), 9L), 100_000_000L); + byte[] create2SuccessReturn = create2SuccessResult.getRuntime().getResult().getHReturn(); + Assert.assertNull(create2SuccessResult.getRuntime().getRuntimeError()); + Assert.assertEquals(BigInteger.valueOf(32), word(create2SuccessReturn, 0)); + Assert.assertTrue(word(create2SuccessReturn, 1).signum() != 0); + Assert.assertEquals("successful CREATE2 clears the previous return data buffer after Osaka", + BigInteger.ZERO, word(create2SuccessReturn, 2)); + Assert.assertEquals(BigInteger.ONE, word(create2SuccessReturn, 3)); + } + + private byte[] deployVerifier() + throws ContractExeException, ReceiptCheckErrException, + VMIllegalException, ContractValidateException { + byte[] owner = Hex.decode(OWNER_ADDRESS); + Transaction trx = TvmTestUtils.generateDeploySmartContractAndGetTransaction( + "TvmIssueVerifier", owner, ABI, BYTECODE, 0, 1_000_000_000L, 0, null); + byte[] contractAddress = WalletUtil.generateContractAddress(trx); + Assert.assertNull(TvmTestUtils + .processTransactionAndReturnRuntime(trx, rootRepository, null) + .getRuntimeError()); + return contractAddress; + } + + private TVMTestResult trigger(byte[] contractAddress, String method, List args, + long feeLimit) + throws ContractExeException, ReceiptCheckErrException, + VMIllegalException, ContractValidateException { + String input = AbiUtil.parseMethod(method, args); + return TvmTestUtils.triggerContractAndReturnTvmTestResult(Hex.decode(OWNER_ADDRESS), + contractAddress, Hex.decode(input), 0, feeLimit, manager, null); + } + + private static BigInteger word(byte[] data, int index) { + int start = index * WORD_SIZE; + return new BigInteger(1, Arrays.copyOfRange(data, start, start + WORD_SIZE)); + } + + private static String initCodeReturningRuntime(String runtimeCode) { + byte[] runtime = Hex.decode(runtimeCode); + Assert.assertTrue(runtime.length <= 255); + + byte[] initCode = new byte[12 + runtime.length]; + initCode[0] = 0x60; + initCode[1] = (byte) runtime.length; + initCode[2] = 0x60; + initCode[3] = 0x0c; + initCode[4] = 0x60; + initCode[5] = 0x00; + initCode[6] = 0x39; + initCode[7] = 0x60; + initCode[8] = (byte) runtime.length; + initCode[9] = 0x60; + initCode[10] = 0x00; + initCode[11] = (byte) 0xf3; + System.arraycopy(runtime, 0, initCode, 12, runtime.length); + + return Hex.toHexString(initCode); + } +} diff --git a/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java b/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java index 186d897effa..1cb6f380252 100644 --- a/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java +++ b/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java @@ -352,8 +352,8 @@ public void deployCreatesCodeContractAndAccount() { assertTrue(chainBaseManager.getAccountStore().has(addr)); AccountCapsule account = chainBaseManager.getAccountStore().get(addr); - assertEquals(HistoryBlockHashUtil.HISTORY_STORAGE_NAME, - account.getAccountName().toStringUtf8()); + assertEquals("accountName must remain unset to mirror CREATE2-created accounts", + ByteString.EMPTY, account.getAccountName()); assertEquals(Protocol.AccountType.Contract, account.getType()); assertTrue("install marker must flip after a successful deploy", chainBaseManager.getDynamicPropertiesStore().isBlockHashHistoryInstalled()); From 0c741e32c622e00a3bab87e49b204bfab7613bdc Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Mon, 25 May 2026 16:33:07 +0800 Subject: [PATCH 15/57] feat(net): normalize inbound messages (#6797) * feat(net): normalize inbound messages * perf(net): skip wire-byte rewrite when sanitize is a no-op --- .../org/tron/core/capsule/BlockCapsule.java | 21 ++ .../tron/core/capsule/TransactionCapsule.java | 11 + .../src/main/java/org/tron/core/Wallet.java | 2 +- .../main/java/org/tron/core/db/Manager.java | 3 + .../core/net/message/adv/BlockMessage.java | 6 + .../net/messagehandler/BlockMsgHandler.java | 2 + .../adv/SanitizeUnknownFieldsTest.java | 200 ++++++++++++++++++ 7 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 framework/src/test/java/org/tron/core/net/message/adv/SanitizeUnknownFieldsTest.java diff --git a/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java index 34b7853d4d1..63acf64b64f 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java @@ -21,6 +21,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.UnknownFieldSet; import java.security.SignatureException; import java.util.ArrayList; import java.util.Arrays; @@ -328,6 +329,26 @@ public boolean hasWitnessSignature() { return !getInstance().getBlockHeader().getWitnessSignature().isEmpty(); } + public boolean sanitize() { + boolean blockHasUnknown = !this.block.getUnknownFields().asMap().isEmpty(); + boolean headerHasUnknown = !this.block.getBlockHeader().getUnknownFields().asMap().isEmpty(); + if (!blockHasUnknown && !headerHasUnknown) { + return false; + } + UnknownFieldSet empty = UnknownFieldSet.getDefaultInstance(); + Block.Builder builder = this.block.toBuilder(); + if (blockHasUnknown) { + builder.setUnknownFields(empty); + } + if (headerHasUnknown) { + builder.setBlockHeader(this.block.getBlockHeader().toBuilder() + .setUnknownFields(empty) + .build()); + } + this.block = builder.build(); + return true; + } + @Override public String toString() { StringBuilder toStringBuff = new StringBuilder(); diff --git a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java index bb4b70cde1b..8724a688548 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java @@ -28,6 +28,7 @@ import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.Internal; import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.UnknownFieldSet; import java.io.IOException; import java.security.SignatureException; import java.util.ArrayList; @@ -494,6 +495,16 @@ public static boolean validateSignature(Transaction transaction, return false; } + public boolean sanitize() { + if (this.transaction.getUnknownFields().asMap().isEmpty()) { + return false; + } + this.transaction = this.transaction.toBuilder() + .setUnknownFields(UnknownFieldSet.getDefaultInstance()) + .build(); + return true; + } + public void resetResult() { if (this.getInstance().getRetCount() > 0) { this.transaction = this.getInstance().toBuilder().clearRet().build(); diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 0482643d8d0..f7c2332303f 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -556,9 +556,9 @@ public GrpcAPI.Return broadcastTransaction(Transaction signedTransaction) { if (trx.getInstance().getRawData().getContractCount() == 0) { throw new ContractValidateException(ActuatorConstant.CONTRACT_NOT_EXIST); } - TransactionMessage message = new TransactionMessage(trx.getInstance().toByteArray()); trx.checkExpiration(chainBaseManager.getNextBlockSlotTime()); dbManager.pushTransaction(trx); + TransactionMessage message = new TransactionMessage(trx.getInstance().toByteArray()); int num = tronNetService.fastBroadcastTransaction(message); if (num == 0 && minEffectiveConnection != 0) { return builder.setResult(false).setCode(response_code.NOT_ENOUGH_EFFECTIVE_CONNECTION) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 3de0260b5c8..20cf5b98386 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1534,6 +1534,9 @@ public TransactionInfo processTransaction(final TransactionCapsule trxCap, Block String.format(" %s transaction signature validate failed", txId)); } + if (!trxCap.isInBlock()) { + trxCap.sanitize(); + } TransactionTrace trace = new TransactionTrace(trxCap, StoreFactory.getInstance(), new RuntimeImpl()); trxCap.setTrxTrace(trace); diff --git a/framework/src/main/java/org/tron/core/net/message/adv/BlockMessage.java b/framework/src/main/java/org/tron/core/net/message/adv/BlockMessage.java index d5aad2cd5c4..99be34e1bf1 100644 --- a/framework/src/main/java/org/tron/core/net/message/adv/BlockMessage.java +++ b/framework/src/main/java/org/tron/core/net/message/adv/BlockMessage.java @@ -28,6 +28,12 @@ public BlockMessage(BlockCapsule block) { this.block = block; } + public void sanitize() { + if (this.block.sanitize()) { + this.data = this.block.getData(); + } + } + public BlockId getBlockId() { return getBlockCapsule().getBlockId(); } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java index 3b9e86d4791..452209d575f 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java @@ -77,6 +77,8 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep check(peer, blockMessage); } + blockMessage.sanitize(); + if (peer.getSyncBlockRequested().containsKey(blockId)) { peer.getSyncBlockRequested().remove(blockId); peer.getSyncBlockInProcess().add(blockId); diff --git a/framework/src/test/java/org/tron/core/net/message/adv/SanitizeUnknownFieldsTest.java b/framework/src/test/java/org/tron/core/net/message/adv/SanitizeUnknownFieldsTest.java new file mode 100644 index 00000000000..7d883b7207d --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/message/adv/SanitizeUnknownFieldsTest.java @@ -0,0 +1,200 @@ +package org.tron.core.net.message.adv; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import com.google.protobuf.ByteString; +import com.google.protobuf.UnknownFieldSet; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.tron.common.overlay.message.Message; +import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.store.DynamicPropertiesStore; +import org.tron.protos.Protocol.Block; +import org.tron.protos.Protocol.BlockHeader; +import org.tron.protos.Protocol.Transaction; + +/** + * Verifies the {@code sanitize()} helpers on {@link BlockCapsule}, + * {@link TransactionCapsule} and {@link BlockMessage}: they strip outer + * unknown protobuf fields while leaving every consensus-hashed / signed + * region byte-identical. + */ +public class SanitizeUnknownFieldsTest { + + private static final UnknownFieldSet PADDING = UnknownFieldSet.newBuilder() + .addField(99999, UnknownFieldSet.Field.newBuilder() + .addLengthDelimited(ByteString.copyFrom(new byte[1024])) + .build()) + .build(); + + @BeforeClass + public static void setUp() { + // BlockMessage(byte[]) calls Message.isFilter() which dereferences the + // static DynamicPropertiesStore. The mock's primitive-long getter returns + // 0L by default, so isFilter() returns false. + Message.setDynamicPropertiesStore(Mockito.mock(DynamicPropertiesStore.class)); + } + + private static BlockHeader.raw sampleRawHeader() { + return BlockHeader.raw.newBuilder() + .setNumber(100) + .setTimestamp(123456789L) + .build(); + } + + private static Block sampleBlock() { + return Block.newBuilder() + .setBlockHeader(BlockHeader.newBuilder().setRawData(sampleRawHeader()).build()) + .build(); + } + + private static Transaction sampleTransaction() { + return Transaction.newBuilder() + .setRawData(Transaction.raw.newBuilder().setTimestamp(123456789L).build()) + .build(); + } + + // ---- BlockCapsule.sanitize ---- + + @Test + public void blockCapsuleSanitizeStripsBlockLevelUnknownFields() { + Block padded = sampleBlock().toBuilder().setUnknownFields(PADDING).build(); + BlockCapsule capsule = new BlockCapsule(padded); + long originalSize = capsule.getData().length; + + assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize()); + + assertTrue("Block-level unknown fields should be stripped", + capsule.getInstance().getUnknownFields().asMap().isEmpty()); + assertTrue("Sanitized capsule bytes should shrink", + capsule.getData().length < originalSize); + } + + @Test + public void blockCapsuleSanitizeStripsBlockHeaderOuterUnknownFields() { + BlockHeader paddedHeader = BlockHeader.newBuilder() + .setRawData(sampleRawHeader()) + .setUnknownFields(PADDING) + .build(); + Block padded = Block.newBuilder().setBlockHeader(paddedHeader).build(); + BlockCapsule capsule = new BlockCapsule(padded); + long originalSize = capsule.getData().length; + + assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize()); + + assertTrue("BlockHeader outer unknown fields should be stripped", + capsule.getInstance().getBlockHeader().getUnknownFields().asMap().isEmpty()); + assertTrue(capsule.getData().length < originalSize); + } + + @Test + public void blockCapsuleSanitizePreservesBlockHeaderRawData() { + Block clean = sampleBlock(); + Block padded = clean.toBuilder().setUnknownFields(PADDING).build(); + BlockCapsule capsule = new BlockCapsule(padded); + + capsule.sanitize(); + + assertEquals("BlockHeader.raw_data must be byte-identical so block hash matches", + clean.getBlockHeader().getRawData(), + capsule.getInstance().getBlockHeader().getRawData()); + } + + @Test + public void blockCapsuleSanitizeIsNoOpOnCleanBlock() { + Block clean = sampleBlock(); + BlockCapsule capsule = new BlockCapsule(clean); + Block beforeInstance = capsule.getInstance(); + byte[] beforeData = capsule.getData(); + + assertFalse("sanitize() should report no-op on a clean block", capsule.sanitize()); + + assertSame("Underlying Block reference should not be rebuilt", + beforeInstance, capsule.getInstance()); + assertArrayEquals("Clean block should pass through unchanged", beforeData, capsule.getData()); + } + + // ---- TransactionCapsule.sanitize ---- + + @Test + public void transactionCapsuleSanitizeStripsTopLevelUnknownFields() { + Transaction padded = sampleTransaction().toBuilder().setUnknownFields(PADDING).build(); + TransactionCapsule capsule = new TransactionCapsule(padded); + long originalSize = capsule.getData().length; + + assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize()); + + assertTrue("Transaction-level unknown fields should be stripped", + capsule.getInstance().getUnknownFields().asMap().isEmpty()); + assertTrue(capsule.getData().length < originalSize); + } + + @Test + public void transactionCapsuleSanitizePreservesTransactionId() { + Transaction clean = sampleTransaction(); + Transaction padded = clean.toBuilder().setUnknownFields(PADDING).build(); + TransactionCapsule cleanCapsule = new TransactionCapsule(clean); + TransactionCapsule paddedCapsule = new TransactionCapsule(padded); + + paddedCapsule.sanitize(); + + assertEquals("Padding outside raw_data must not change the transaction id", + cleanCapsule.getTransactionId(), + paddedCapsule.getTransactionId()); + } + + @Test + public void transactionCapsuleSanitizeIsNoOpOnCleanTransaction() { + Transaction clean = sampleTransaction(); + TransactionCapsule capsule = new TransactionCapsule(clean); + Transaction beforeInstance = capsule.getInstance(); + byte[] beforeData = capsule.getData(); + + assertFalse("sanitize() should report no-op on a clean transaction", capsule.sanitize()); + + assertSame("Underlying Transaction reference should not be rebuilt", + beforeInstance, capsule.getInstance()); + assertArrayEquals(beforeData, capsule.getData()); + } + + // ---- BlockMessage.sanitize ---- + + @Test + public void blockMessageSanitizeUpdatesBothCapsuleAndWireBytes() throws Exception { + Block padded = sampleBlock().toBuilder().setUnknownFields(PADDING).build(); + byte[] paddedBytes = padded.toByteArray(); + BlockMessage msg = new BlockMessage(paddedBytes); + assertArrayEquals("Constructor should not sanitize on its own", + paddedBytes, msg.getData()); + + msg.sanitize(); + + assertTrue("BlockCapsule should be sanitized", + msg.getBlockCapsule().getInstance().getUnknownFields().asMap().isEmpty()); + assertTrue("msg.data should also be rewritten to canonical bytes", + msg.getData().length < paddedBytes.length); + assertArrayEquals("msg.data should equal capsule.getData() after sanitize", + msg.getBlockCapsule().getData(), msg.getData()); + assertNotEquals("msg.data should no longer match the padded wire bytes", + paddedBytes.length, msg.getData().length); + } + + @Test + public void blockMessageSanitizeSkipsDataRewriteOnCleanBlock() throws Exception { + byte[] cleanBytes = sampleBlock().toByteArray(); + BlockMessage msg = new BlockMessage(cleanBytes); + byte[] before = msg.getData(); + + msg.sanitize(); + + assertSame("msg.data should not be rewritten on the no-op path", + before, msg.getData()); + } +} From 156af72d4c0ed6534780ff8bc50325db03573d2d Mon Sep 17 00:00:00 2001 From: Asuka Date: Mon, 25 May 2026 17:55:13 +0800 Subject: [PATCH 16/57] fix(vm): write TIP-2935 parent hash after witness permission check (#6800) --- .../main/java/org/tron/core/db/Manager.java | 3 ++- .../db/HistoryBlockHashIntegrationTest.java | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 20cf5b98386..d2aa42dfcea 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1633,7 +1633,6 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) { session.reset(); session.setValue(revokingStore.buildSession()); - HistoryBlockHashUtil.write(this, blockCapsule); accountStateCallBack.preExecute(blockCapsule); if (getDynamicPropertiesStore().getAllowMultiSign() == 1) { @@ -1646,6 +1645,8 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) { } } + HistoryBlockHashUtil.write(this, blockCapsule); + Set accountSet = new HashSet<>(); AtomicInteger shieldedTransCounts = new AtomicInteger(0); List toBePacked = new ArrayList<>(); diff --git a/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java b/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java index 1cb6f380252..be5a012c852 100644 --- a/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java +++ b/framework/src/test/java/org/tron/core/db/HistoryBlockHashIntegrationTest.java @@ -256,14 +256,18 @@ public void deploySkipsWhenForeignContractPresent() { * SR / validator parity: the producer's {@code generateBlock} simulation * loop and the validator's {@code processBlock} apply loop must see the * same storage state when transactions hit {@code HISTORY_STORAGE_ADDRESS}. - * That requires {@link HistoryBlockHashUtil#write} to run before the tx - * loop on both paths. {@code processBlock} writes at line 1858; this test - * pins the matching write inside {@code generateBlock}. + * Both paths run {@link HistoryBlockHashUtil#write} before their tx loop: + * {@code processBlock} after its {@code validBlock} guard, and + * {@code generateBlock} after the witness-permission guard, so a failed + * permission check never writes the parent hash. * - *

Spy {@code accountStateCallBack.preExecute} — called between the - * write and the tx loop on both paths — and snapshot the slot from inside - * the revoking session. Pre-fix the slot is empty (write never ran); - * post-fix it holds the parent block hash. + *

In {@code generateBlock} {@code preExecute} runs ahead of the write + * (it precedes the guard), so this spies + * {@code accountStateCallBack.executeGenerateFinish} — the last callback + * before {@code session.reset()} — and snapshots the slot from inside the + * revoking session. With no pending transactions the tx loop is a no-op, so + * reaching this callback means the write already ran: if it were dropped the + * slot would be empty; instead it holds the parent block hash. */ @Test public void generateBlockWritesParentHashBeforeTxLoop() throws Exception { @@ -286,7 +290,7 @@ public void generateBlockWritesParentHashBeforeTxLoop() throws Exception { chainBaseManager.getStorageRowStore()); captured.set(st.getValue(new DataWord(expectedSlot))); return inv.callRealMethod(); - }).when(spy).preExecute(Mockito.any(BlockCapsule.class)); + }).when(spy).executeGenerateFinish(); cbField.set(dbManager, spy); try { @@ -303,10 +307,11 @@ public void generateBlockWritesParentHashBeforeTxLoop() throws Exception { } assertNotNull( - "preExecute fired with an empty slot — write() must run before preExecute", + "executeGenerateFinish fired with an empty slot — " + + "write() must run during block generation", captured.get()); assertArrayEquals( - "slot must hold the parent block hash before the tx loop runs", + "slot must hold the parent block hash by the time generation finishes", expectedParentHash, captured.get().getData()); } From 03d9fe60a98a74deda00a202122b955c85153331 Mon Sep 17 00:00:00 2001 From: bladehan1 Date: Tue, 26 May 2026 10:40:05 +0800 Subject: [PATCH 17/57] feat(ci): enforce reference.conf CI check (#6795) --- .github/scripts/check_reference_conf.py | 319 ++++++++++++++++++++++++ .github/scripts/requirements.txt | 1 + .github/workflows/pr-check.yml | 16 ++ 3 files changed, 336 insertions(+) create mode 100644 .github/scripts/check_reference_conf.py create mode 100644 .github/scripts/requirements.txt diff --git a/.github/scripts/check_reference_conf.py b/.github/scripts/check_reference_conf.py new file mode 100644 index 00000000000..d9e2f3f20cf --- /dev/null +++ b/.github/scripts/check_reference_conf.py @@ -0,0 +1,319 @@ +#!/usr/bin/env python3 +"""Validate java-tron reference.conf key names and hierarchy depth. + +Rules enforced: + 1. Every user-defined segment of every key path must match ^[a-z][a-zA-Z0-9]*$: + starts with a lowercase ASCII letter, then ASCII letters/digits only. + Acronyms at position 1+ are accepted (e.g. `httpPBFTEnable`, + `openHistoryQueryWhenLiteFN`, `allowShieldedTRC20Transaction`) — only the + first character is constrained. This matches what java.beans.Introspector + and ConfigBeanFactory actually require for bean-property auto-binding. + 2. Total path depth must be <= MAX_DEPTH (5). Each list/array step counts + as one additional level. For example `rate.limiter.http[].component` + is 5 levels deep (rate=1, limiter=2, http=3, []=4, component=5). + 3. ALLOWLIST entries are exempt from the format rule (legacy keys that ship + in user configs; renaming would break compatibility). + 4. Service-binding port values must be unique. A leaf is a "service port" + when its last segment is `port` or ends in `Port` (camelCase) AND its + path contains no `[]` (list-element ports belong to per-element records, + not to the local process). Two distinct paths binding the same int value + would conflict at startup; reserved sentinels (0, -1) are exempt. + +Parsing strategy: delegated to pyhocon (https://github.com/chimpler/pyhocon), +the reference Python HOCON implementation. This avoids hand-rolled scanner +pitfalls (key = { ... } prefix loss, triple-strings, substitutions, includes, ++= operator, block comments). pyhocon returns a fully-merged ConfigTree where +dotted-form keys are expanded into nested objects — i.e. the same canonical +key set Typesafe Config / ConfigBeanFactory will see at runtime. + +Array handling: keys inside object-elements of arrays are also user-defined +config keys (e.g. each entry in `rate.limiter.rpc = [{ component=..., ... }]` +is parsed by RateLimiterConfig). The walker recurses into list elements and +treats the array step as a synthetic `[]` segment that contributes to depth +but is not itself validated as a name. Element keys are deduplicated across +list entries because well-formed arrays use homogeneous object shapes. + +Debug mode: pass `--debug` to print every parsed key with its depth, in +walk order (which mirrors the file top-to-bottom). Use this to eyeball the +parser's view against reference.conf. + +Exit code: 0 if clean, 1 if any violation remains after allowlist filtering, +2 on environment errors (missing pyhocon, file not found, parse failure). + +CI integration: invoked by the `Validate reference.conf key names and depth` +step of the `checkstyle` job in `.github/workflows/pr-check.yml`. The non-zero +exit on violations is what makes that step fail — there is intentionally NO +extra `exit 1` in the workflow shell wrapper. A single GHA `::error` workflow +command is also emitted unconditionally (not gated on the GITHUB_ACTIONS env +var) so local runs produce the same output as CI; the leading `::` is +harmless noise locally. +""" +import re +import sys +from pathlib import Path + +try: + from pyhocon import ConfigFactory, ConfigTree +except ImportError: + print( + "error: pyhocon is required. Install with `pip install pyhocon`.", + file=sys.stderr, + ) + sys.exit(2) + +# Set at the current max depth of reference.conf (5). No buffer: a mature +# project should not allow silent drift, so any new key going deeper must +# bump MAX_DEPTH via an explicit, reviewed change (deeper trees hurt +# readability and complicate ConfigBeanFactory mapping). +MAX_DEPTH = 5 +KEY_REGEX = re.compile(r'^[a-z][a-zA-Z0-9]*$') +# Legacy keys grandfathered to keep user `config.conf` files compatible. +# Do NOT extend this list for new keys — every new key must satisfy KEY_REGEX. +# A future rename + deprecation cycle can shrink this set back to empty. +ALLOWLIST = { + # PBFT acronym in capitals — predates the auto-binding convention. + "node.http.PBFTEnable", + "node.http.PBFTPort", + "node.rpc.PBFTEnable", + "node.rpc.PBFTPort", + # PascalCase exceptions handled manually in NodeConfig.fromConfig (not via + # ConfigBeanFactory). Currently commented out in reference.conf, so the + # parser does not see them today — listed here so the gate stays green if + # a future change uncomments them with defaults. + "node.shutdown.BlockTime", + "node.shutdown.BlockHeight", + "node.shutdown.BlockCount", +} + +# Sentinel port values exempt from the uniqueness check. 0 = disabled (the +# service does not bind); -1 = auto/unset placeholder. Any number of leaves +# may share these values. +PORT_SENTINELS = {0, -1} + + +def walk(node, path, depth): + """Yield (full_path, depth, is_leaf) for every reachable user-defined key. + + - ConfigTree key adds one depth level and contributes a name segment. + - list step adds one synthetic level rendered as `[]`. Element-internal + keys are walked once per unique sub-path (homogeneous object arrays + otherwise yield each field N times). + - Scalars / null / list-of-scalars produce no further keys. + + `depth` includes the array `[]` steps. `is_leaf` is True when the value + at this path is a scalar/list/null — i.e. not another ConfigTree — so + callers can filter leaves vs namespace intermediates. + """ + if isinstance(node, ConfigTree): + for k, v in node.items(): + new_path = f"{path}.{k}" if path else k + new_depth = depth + 1 + is_leaf = not isinstance(v, ConfigTree) + yield new_path, new_depth, is_leaf + yield from walk(v, new_path, new_depth) + elif isinstance(node, list): + array_path = f"{path}[]" + array_depth = depth + 1 + seen = set() + for elem in node: + # Object element: walk its keys. Nested list element (HOCON allows + # list-of-list, e.g. `a = [[{x=1}]]`): recurse so each inner [] step + # also contributes to depth. Scalar elements have no sub-keys. + if isinstance(elem, (ConfigTree, list)): + for sub_path, sub_depth, sub_leaf in walk(elem, array_path, array_depth): + if sub_path in seen: + continue + seen.add(sub_path) + yield sub_path, sub_depth, sub_leaf + + +def _is_port_segment(seg): + """Last-segment test for a service-binding port leaf. + + Matches `port` (exact) and any camelCase form ending in `Port` + (e.g. `fullNodePort`, `solidityPort`, `PBFTPort`). Deliberately rejects + lowercase `port` as a suffix inside a longer word (`transport`, + `support`) — those are not port keys. + """ + return seg == "port" or seg.endswith("Port") + + +def find_port_collisions(tree, keys): + """Group service-binding port leaves by integer value; return collisions. + + A leaf qualifies when (a) its last segment matches `_is_port_segment`, + and (b) its full path contains no `[]` step. Rule (b) excludes + list-element ports — e.g. `genesis.block.witnesses[].port` is the + advertised port of each genesis witness record, not a port the local + process binds, so two witnesses sharing a value is expected. + + Returns sorted list of (value, sorted_paths) for any value bound by more + than one path. Sentinel values in PORT_SENTINELS are excluded. Values + that are not coercible to int (substitutions like `${PORT}` resolved to + strings) are skipped silently — the format/depth gates do not look at + values either, and a non-numeric port is a different class of error. + """ + by_value = {} + for full_path, _depth, is_leaf in keys: + if not is_leaf: + continue + if "[]" in full_path: + continue + seg = full_path.split(".")[-1] + if not _is_port_segment(seg): + continue + try: + raw = tree.get(full_path) + except Exception: + continue + try: + value = int(raw) + except (TypeError, ValueError): + continue + if value in PORT_SENTINELS: + continue + by_value.setdefault(value, []).append(full_path) + return sorted( + (v, sorted(paths)) for v, paths in by_value.items() if len(paths) > 1 + ) + + +def main(argv): + debug = False + args = list(argv[1:]) + if args and args[0] == "--debug": + debug = True + args = args[1:] + if len(args) != 1: + print(f"usage: {argv[0]} [--debug] ", file=sys.stderr) + return 2 + path = Path(args[0]) + if not path.is_file(): + print(f"error: file not found: {path}", file=sys.stderr) + return 2 + + try: + tree = ConfigFactory.parse_file(str(path)) + except Exception as e: + print(f"error: failed to parse {path}: {e}", file=sys.stderr) + # Mirror the violation path: emit a single GHA annotation so the + # parse failure surfaces in the PR check summary, not just the log. + print(f"::error file={path},title=reference.conf::failed to parse: {e}") + return 2 + + keys = list(walk(tree, "", 0)) + + if debug: + # Keys are yielded in pyhocon insertion order, which mirrors the + # source file top-to-bottom. Eyeball this against reference.conf to + # confirm coverage; the depth column makes the array `[]` steps + # explicit so MAX_DEPTH math is verifiable by inspection. Trailing + # `/` marks namespace intermediates (have children); bare names are + # leaves — `grep -v '/$'` filters to just leaves. + leaf_count = sum(1 for _, _, lf in keys if lf) + print( + f"DEBUG: {len(keys)} parsed keys " + f"({leaf_count} leaves + {len(keys) - leaf_count} intermediates), " + f"walk order:" + ) + for full_path, depth, is_leaf in keys: + label = full_path if is_leaf else full_path + "/" + print(f" d={depth} {label}") + print() + + format_violations = [] + depth_violations = [] + + # Only check leaves: pyhocon expands a dotted-form declaration like + # `a.b.c = X` into intermediate ConfigTree nodes for `a` and `a.b`. A + # single user-written bad key would otherwise be reported once per + # intermediate AND once as the leaf, multiplying noise. The leaf path + # carries every segment, so checking just leaves covers all segments. + for full_path, depth, is_leaf in keys: + if not is_leaf: + continue + if full_path not in ALLOWLIST: + for seg in full_path.split('.'): + # Strip any number of trailing `[]` markers — nested arrays + # produce segments like `a[][]`. + while seg.endswith('[]'): + seg = seg[:-2] + if seg and not KEY_REGEX.match(seg): + format_violations.append((full_path, seg)) + break + + if depth > MAX_DEPTH: + depth_violations.append((full_path, depth)) + + format_violations.sort() + depth_violations.sort() + + port_collisions = find_port_collisions(tree, keys) + + if format_violations or depth_violations or port_collisions: + lines_out = [] + if format_violations: + lines_out.append( + f"Format violations ({len(format_violations)}) — " + f"each segment must match {KEY_REGEX.pattern}:" + ) + for full_path, seg in format_violations: + lines_out.append(f" format: {full_path} (segment: '{seg}')") + if depth_violations: + if lines_out: + lines_out.append("") + lines_out.append( + f"Depth violations ({len(depth_violations)}) — max depth is {MAX_DEPTH} " + f"(each `[]` array step counts as one level):" + ) + for full_path, depth in depth_violations: + lines_out.append( + f" depth: {full_path} (depth={depth}, max={MAX_DEPTH})" + ) + if port_collisions: + if lines_out: + lines_out.append("") + lines_out.append( + f"Port collisions ({len(port_collisions)}) — distinct service " + f"ports must bind distinct values (sentinels {sorted(PORT_SENTINELS)} exempt):" + ) + for value, paths in port_collisions: + lines_out.append( + f" port: value {value} bound by: {', '.join(paths)}" + ) + print("\n".join(lines_out)) + print() + + # Emit ONE consolidated GHA workflow annotation. All offending entries + # are packed into the annotation body via %0A (GHA's newline escape) + # so the entries are visible in the annotation summary, not just in + # the job log. + entries = [] + for full_path, seg in format_violations: + entries.append(f"format: {full_path} (segment '{seg}')") + for full_path, depth in depth_violations: + entries.append(f"depth: {full_path} (depth={depth}, max={MAX_DEPTH})") + for value, paths in port_collisions: + entries.append(f"port: value {value} bound by {', '.join(paths)}") + body = ( + f"reference.conf has {len(format_violations)} format + " + f"{len(depth_violations)} depth + {len(port_collisions)} port " + f"violation(s):%0A" + "%0A".join(entries) + ) + print(f"::error file={path},title=reference.conf::{body}") + print( + f"FAIL: {len(format_violations)} format + {len(depth_violations)} depth " + f"+ {len(port_collisions)} port violation(s) in {path}", + file=sys.stderr, + ) + return 1 + + print( + f"OK: {path} — {len(keys)} keys, all lowerCamelCase, depth <= {MAX_DEPTH}, " + f"service ports unique" + ) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/.github/scripts/requirements.txt b/.github/scripts/requirements.txt new file mode 100644 index 00000000000..502fc107f4a --- /dev/null +++ b/.github/scripts/requirements.txt @@ -0,0 +1 @@ +pyhocon==0.3.63 diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 19425209bbc..7ae169a8690 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -103,6 +103,22 @@ jobs: steps: - uses: actions/checkout@v5 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + cache: 'pip' + cache-dependency-path: .github/scripts/requirements.txt + + - name: Install pyhocon + run: pip install --quiet -r .github/scripts/requirements.txt + + - name: Validate reference.conf key names and depth + shell: bash + run: | + python3 .github/scripts/check_reference_conf.py \ + common/src/main/resources/reference.conf + - name: Set up JDK 17 uses: actions/setup-java@v5 with: From 0c1353647a45492e4d0bdf3a7f9ad14a21a7b16e Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 26 May 2026 14:19:28 +0800 Subject: [PATCH 18/57] refactor(config): remove unused storage index and json parsing config (#6794) --- .../common/parameter/CommonParameter.java | 7 +--- .../src/main/java/org/tron/core/Constant.java | 4 +++ .../org/tron/core/config/args/NodeConfig.java | 2 -- .../org/tron/core/config/args/Storage.java | 11 ------- .../tron/core/config/args/StorageConfig.java | 22 +------------ common/src/main/java/org/tron/json/JSON.java | 15 ++------- common/src/main/resources/reference.conf | 6 ---- .../core/config/args/StorageConfigTest.java | 1 - .../java/org/tron/core/config/args/Args.java | 14 -------- .../core/services/jsonrpc/JsonRpcServlet.java | 7 ++-- .../vm/BandWidthRuntimeOutOfTimeTest.java | 2 -- ...andWidthRuntimeOutOfTimeWithCheckTest.java | 2 -- .../runtime/vm/BandWidthRuntimeTest.java | 2 -- .../vm/BandWidthRuntimeWithCheckTest.java | 2 -- .../org/tron/core/config/args/ArgsTest.java | 33 ------------------- .../tron/core/config/args/StorageTest.java | 1 - .../tron/core/db/AccountIndexStoreTest.java | 4 +-- .../org/tron/core/db/AccountStoreTest.java | 4 +-- .../tron/core/db/TransactionHistoryTest.java | 4 +-- .../tron/core/db/TransactionRetStoreTest.java | 4 +-- .../tron/core/db/TransactionStoreTest.java | 1 - .../tron/core/db/TransactionTraceTest.java | 2 -- .../java/org/tron/core/db/TxCacheDBTest.java | 3 +- .../core/metrics/MetricsApiServiceTest.java | 2 -- .../services/jsonrpc/JsonRpcServletTest.java | 5 +-- .../tron/core/zksnark/LibrustzcashTest.java | 2 -- .../org/tron/core/zksnark/MerkleTreeTest.java | 2 -- .../src/test/java/org/tron/json/JsonTest.java | 5 +-- framework/src/test/resources/args-test.conf | 1 - .../src/test/resources/config-localtest.conf | 7 ---- .../src/test/resources/config-test-index.conf | 1 - .../test/resources/config-test-mainnet.conf | 1 - .../resources/config-test-storagetest.conf | 1 - framework/src/test/resources/config-test.conf | 1 - .../src/test/resources/config-duplicate.conf | 1 - 35 files changed, 23 insertions(+), 159 deletions(-) diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 7c8c16ed422..19d03f92a31 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -521,12 +521,7 @@ public class CommonParameter { @Getter @Setter public int pBFTHttpPort; - @Getter - @Setter - public int maxNestingDepth = 100; - @Getter - @Setter - public int maxTokenCount = 100_000; + @Getter @Setter public long pBFTExpireNum; // clearParam: 20 diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 1437d319346..ebd1bd4c398 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -63,4 +63,8 @@ public class Constant { // Network public static final String LOCAL_HOST = "127.0.0.1"; + // JSON parsing (DoS protection) + public static final int MAX_NESTING_DEPTH = 100; + public static final int MAX_TOKEN_COUNT = 100_000; + } diff --git a/common/src/main/java/org/tron/core/config/args/NodeConfig.java b/common/src/main/java/org/tron/core/config/args/NodeConfig.java index 82619726b7e..bb6bdc02f4e 100644 --- a/common/src/main/java/org/tron/core/config/args/NodeConfig.java +++ b/common/src/main/java/org/tron/core/config/args/NodeConfig.java @@ -199,8 +199,6 @@ public static class HttpConfig { private boolean solidityEnable = true; private int solidityPort = 8091; private long maxMessageSize = 4194304; - private int maxNestingDepth = 100; - private int maxTokenCount = 100_000; private boolean pBFTEnable = true; private int pBFTPort = 8092; } diff --git a/common/src/main/java/org/tron/core/config/args/Storage.java b/common/src/main/java/org/tron/core/config/args/Storage.java index 64a9efab7f1..f1317e04914 100644 --- a/common/src/main/java/org/tron/core/config/args/Storage.java +++ b/common/src/main/java/org/tron/core/config/args/Storage.java @@ -70,17 +70,6 @@ public class Storage { @Setter private int maxFlushCount; - /** - * Index storage directory: /path/to/{indexDirectory} - */ - @Getter - @Setter - private String indexDirectory; - - @Getter - @Setter - private String indexSwitch; - @Getter @Setter private boolean contractParseSwitch; diff --git a/common/src/main/java/org/tron/core/config/args/StorageConfig.java b/common/src/main/java/org/tron/core/config/args/StorageConfig.java index 5f8efffb9f3..e8823d81984 100644 --- a/common/src/main/java/org/tron/core/config/args/StorageConfig.java +++ b/common/src/main/java/org/tron/core/config/args/StorageConfig.java @@ -21,7 +21,6 @@ public class StorageConfig { private DbConfig db = new DbConfig(); - private IndexConfig index = new IndexConfig(); private TransHistoryConfig transHistory = new TransHistoryConfig(); private boolean needToUpdateAsset = true; private DbSettingsConfig dbSettings = new DbSettingsConfig(); @@ -60,29 +59,10 @@ public static class DbConfig { private String directory = "database"; } - @Getter - @Setter - public static class IndexConfig { - private String directory = "index"; - // "switch" is a Java keyword, but HOCON key is "index.switch" - // ConfigBeanFactory would look for setSwitch which works fine in Java - @Getter(lombok.AccessLevel.NONE) - @Setter(lombok.AccessLevel.NONE) - private String switchValue = "on"; - - public String getSwitch() { - return switchValue; - } - - public void setSwitch(String v) { - this.switchValue = v; - } - } - @Getter @Setter public static class TransHistoryConfig { - // "switch" is a Java keyword — same handling as IndexConfig + // "switch" is a reserved Java keyword; ConfigBeanFactory calls setSwitch() which works fine @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) private String switchValue = "on"; diff --git a/common/src/main/java/org/tron/json/JSON.java b/common/src/main/java/org/tron/json/JSON.java index 88678c49a44..571b9515ade 100644 --- a/common/src/main/java/org/tron/json/JSON.java +++ b/common/src/main/java/org/tron/json/JSON.java @@ -10,7 +10,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.ObjectNode; -import org.tron.common.parameter.CommonParameter; +import org.tron.core.Constant; /** * Drop-in replacement for {@code com.alibaba.fastjson.JSON}. @@ -22,15 +22,6 @@ @Deprecated public final class JSON { - // Initialization-order invariant: this class must NOT be loaded before - // Args.setParam() completes. The factory's StreamReadConstraints are a - // one-shot snapshot of CommonParameter at class-init time. If JSON is - // touched too early — e.g. a stray reference in startup code or in a static - // initializer that runs before Args — the snapshot captures CommonParameter's - // hardcoded defaults (100 / 100_000) and any user override of - // node.http.maxNestingDepth / maxTokenCount is silently ignored. - // Current production startup (FullNode.main) calls Args.setParam first and - // no path in that call chain references this class, so the invariant holds. static final ObjectMapper MAPPER = JsonMapper.builder(buildFactory()) // Fastjson Feature.AllowUnQuotedFieldNames (default ON) .enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES) @@ -67,9 +58,9 @@ public final class JSON { .build(); private static JsonFactory buildFactory() { - CommonParameter p = CommonParameter.getInstance(); return JsonFactory.builder().streamReadConstraints(StreamReadConstraints.builder() - .maxNestingDepth(p.getMaxNestingDepth()).maxTokenCount(p.getMaxTokenCount()) + .maxNestingDepth(Constant.MAX_NESTING_DEPTH) + .maxTokenCount(Constant.MAX_TOKEN_COUNT) .build()).build(); } diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 0864f4d5126..b17f04924df 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -49,10 +49,6 @@ storage { db.sync = false db.directory = "database" - # Index directory (legacy, not consumed by any runtime code, kept for CLI/test compatibility) - index.directory = "index" - index.switch = "on" - # Whether to write transaction result in transactionRetStore transHistory.switch = "on" @@ -245,8 +241,6 @@ node { # Maximum HTTP request body size, default 4MB. Independent from rpc.maxMessageSize. maxMessageSize = 4M - maxNestingDepth = 100 - maxTokenCount = 100000 } rpc { diff --git a/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java b/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java index ecb956e406a..d8700880cd0 100644 --- a/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java @@ -26,7 +26,6 @@ public void testDefaults() { assertEquals("LEVELDB", sc.getDb().getEngine()); assertFalse(sc.getDb().isSync()); assertEquals("database", sc.getDb().getDirectory()); - assertEquals("index", sc.getIndex().getDirectory()); assertTrue(sc.isNeedToUpdateAsset()); assertEquals(7, sc.getDbSettings().getLevelNumber()); assertEquals(5000, sc.getDbSettings().getMaxOpenFiles()); diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 8d8e2500c9f..74e9001177f 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -80,8 +80,6 @@ public class Args extends CommonParameter { m.put("--storage-db-directory", "storage.db.directory"); m.put("--storage-db-engine", "storage.db.engine"); m.put("--storage-db-synchronous", "storage.db.sync"); - m.put("--storage-index-directory", "storage.index.directory"); - m.put("--storage-index-switch", "storage.index.switch"); m.put("--storage-transactionHistory-switch", "storage.transHistory.switch"); m.put("--contract-parse-enable", "event.subscribe.contractParse"); m.put("--support-constant", "vm.supportConstant"); @@ -215,10 +213,6 @@ private static void applyStorageConfig(StorageConfig sc) { PARAMETER.storage.setDbEngine(sc.getDb().getEngine()); PARAMETER.storage.setDbSync(sc.getDb().isSync()); PARAMETER.storage.setDbDirectory(sc.getDb().getDirectory()); - PARAMETER.storage.setIndexDirectory(sc.getIndex().getDirectory()); - String indexSwitch = sc.getIndex().getSwitch(); - PARAMETER.storage.setIndexSwitch( - org.apache.commons.lang3.StringUtils.isNotEmpty(indexSwitch) ? indexSwitch : "on"); PARAMETER.storage.setTransactionHistorySwitch(sc.getTransHistory().getSwitch()); // contractParse is set in applyConfigParams alongside event config, not here PARAMETER.storage.setCheckpointVersion(sc.getCheckpoint().getVersion()); @@ -549,8 +543,6 @@ private static void applyNodeConfig(NodeConfig nc) { PARAMETER.solidityHttpPort = http.getSolidityPort(); PARAMETER.pBFTHttpPort = http.getPBFTPort(); PARAMETER.httpMaxMessageSize = http.getMaxMessageSize(); - PARAMETER.maxNestingDepth = http.getMaxNestingDepth(); - PARAMETER.maxTokenCount = http.getMaxTokenCount(); // ---- JSON-RPC sub-bean ---- NodeConfig.JsonRpcConfig jsonrpc = nc.getJsonrpc(); @@ -865,12 +857,6 @@ private static void applyCLIParams(CLIParameter cmd, JCommander jc) { if (assigned.contains("--contract-parse-enable")) { PARAMETER.storage.setContractParseSwitch(Boolean.valueOf(cmd.contractParseEnable)); } - if (assigned.contains("--storage-index-directory")) { - PARAMETER.storage.setIndexDirectory(cmd.storageIndexDirectory); - } - if (assigned.contains("--storage-index-switch")) { - PARAMETER.storage.setIndexSwitch(cmd.storageIndexSwitch); - } if (assigned.contains("--storage-transactionHistory-switch")) { PARAMETER.storage.setTransactionHistorySwitch(cmd.storageTransactionHistorySwitch); } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index 29869403988..a332757457f 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.parameter.CommonParameter; +import org.tron.core.Constant; import org.tron.core.services.filter.BufferedResponseWrapper; import org.tron.core.services.filter.CachedBodyRequestWrapper; import org.tron.core.services.http.RateLimiterServlet; @@ -32,15 +33,13 @@ @Slf4j(topic = "API") public class JsonRpcServlet extends RateLimiterServlet { - // Snapshot of node.http.maxNestingDepth / maxTokenCount at class-load time (after Args.setParam). private static final ObjectMapper MAPPER = buildMapper(); private static ObjectMapper buildMapper() { - CommonParameter p = CommonParameter.getInstance(); JsonFactory factory = JsonFactory.builder() .streamReadConstraints(StreamReadConstraints.builder() - .maxNestingDepth(p.getMaxNestingDepth()) - .maxTokenCount(p.getMaxTokenCount()) + .maxNestingDepth(Constant.MAX_NESTING_DEPTH) + .maxTokenCount(Constant.MAX_TOKEN_COUNT) .build()) .build(); return new ObjectMapper(factory); diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java index 582f5157b27..85829e474f0 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java @@ -61,7 +61,6 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static final String dbDirectory = "db_BandWidthRuntimeOutOfTimeTest_test"; - private static final String indexDirectory = "index_BandWidthRuntimeOutOfTimeTest_test"; private static final String OwnerAddress = "TCWHANtDDdkZCTo2T2peyEq3Eg9c2XB7ut"; private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA"; @@ -72,7 +71,6 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }, "config-test-mainnet.conf" diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java index 7e75f2b31d1..be8fc952188 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java @@ -63,7 +63,6 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static final String dbDirectory = "db_BandWidthRuntimeOutOfTimeTest_test"; - private static final String indexDirectory = "index_BandWidthRuntimeOutOfTimeTest_test"; private static final String OwnerAddress = "TCWHANtDDdkZCTo2T2peyEq3Eg9c2XB7ut"; private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA"; private static boolean init; @@ -73,7 +72,6 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }, "config-test-mainnet.conf" diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java index 8e38c08c4d8..1245f5cefd6 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java @@ -57,7 +57,6 @@ public class BandWidthRuntimeTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static final String dbDirectory = "db_BandWidthRuntimeTest_test"; - private static final String indexDirectory = "index_BandWidthRuntimeTest_test"; private static final String OwnerAddress = "TCWHANtDDdkZCTo2T2peyEq3Eg9c2XB7ut"; private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA"; private static final String TriggerOwnerTwoAddress = "TPMBUANrTwwQAPwShn7ZZjTJz1f3F8jknj"; @@ -69,7 +68,6 @@ public static void init() { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, }, "config-test-mainnet.conf" ); diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java index aae8cb5702d..13c75f3e40a 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java @@ -63,7 +63,6 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static final String dbDirectory = "db_BandWidthRuntimeWithCheckTest_test"; - private static final String indexDirectory = "index_BandWidthRuntimeWithCheckTest_test"; private static final String OwnerAddress = "TCWHANtDDdkZCTo2T2peyEq3Eg9c2XB7ut"; private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA"; private static final String TriggerOwnerTwoAddress = "TPMBUANrTwwQAPwShn7ZZjTJz1f3F8jknj"; @@ -75,7 +74,6 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, }, "config-test-mainnet.conf" ); diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 3ae5677fbda..43f21157f47 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -303,8 +303,6 @@ public void testCliOverridesStorageConfig() { "--storage-db-directory", "cli-db-dir", "--storage-db-engine", "ROCKSDB", "--storage-db-synchronous", "true", - "--storage-index-directory", "cli-index-dir", - "--storage-index-switch", "cli-index-switch", "--storage-transactionHistory-switch", "off", "--contract-parse-enable", "false" }, TestConstants.TEST_CONF); @@ -314,8 +312,6 @@ public void testCliOverridesStorageConfig() { Assert.assertEquals("cli-db-dir", parameter.getStorage().getDbDirectory()); Assert.assertEquals("ROCKSDB", parameter.getStorage().getDbEngine()); Assert.assertTrue(parameter.getStorage().isDbSync()); - Assert.assertEquals("cli-index-dir", parameter.getStorage().getIndexDirectory()); - Assert.assertEquals("cli-index-switch", parameter.getStorage().getIndexSwitch()); Assert.assertEquals("off", parameter.getStorage().getTransactionHistorySwitch()); Assert.assertFalse(parameter.getStorage().isContractParseSwitch()); @@ -413,35 +409,6 @@ public void testFetchBlockTimeoutClampedAboveMax() { Args.clearParam(); } - - @Test - public void testHttpJsonParseConstraints() { - Map override = new HashMap<>(); - override.put("storage.db.directory", "database"); - Config config = ConfigFactory.parseMap(override) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - - Assert.assertEquals(100, Args.getInstance().getMaxNestingDepth()); - Assert.assertEquals(100_000, Args.getInstance().getMaxTokenCount()); - Args.clearParam(); - } - - @Test - public void testHttpJsonParseConstraintsApplied() { - Map override = new HashMap<>(); - override.put("storage.db.directory", "database"); - override.put("node.http.maxNestingDepth", "42"); - override.put("node.http.maxTokenCount", "12345"); - Config config = ConfigFactory.parseMap(override) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - - Assert.assertEquals(42, Args.getInstance().getMaxNestingDepth()); - Assert.assertEquals(12345, Args.getInstance().getMaxTokenCount()); - Args.clearParam(); - } - @Test public void testFetchBlockTimeoutInRangeUnchanged() { Map override = new HashMap<>(); diff --git a/framework/src/test/java/org/tron/core/config/args/StorageTest.java b/framework/src/test/java/org/tron/core/config/args/StorageTest.java index eb349a2d146..a925b9c8fc0 100644 --- a/framework/src/test/java/org/tron/core/config/args/StorageTest.java +++ b/framework/src/test/java/org/tron/core/config/args/StorageTest.java @@ -42,7 +42,6 @@ public static void cleanup() { @Test public void getDirectory() { Assert.assertEquals("database", storage.getDbDirectory()); - Assert.assertEquals("index", storage.getIndexDirectory()); } @Test diff --git a/framework/src/test/java/org/tron/core/db/AccountIndexStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountIndexStoreTest.java index 4971132b8c5..1ae1ab4b029 100755 --- a/framework/src/test/java/org/tron/core/db/AccountIndexStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AccountIndexStoreTest.java @@ -16,7 +16,6 @@ public class AccountIndexStoreTest extends BaseTest { private static String dbDirectory = "db_AccountIndexStore_test"; - private static String indexDirectory = "index_AccountIndexStore_test"; @Resource private AccountIndexStore accountIndexStore; private static byte[] address = TransactionStoreTest.randomBytes(32); @@ -26,8 +25,7 @@ public class AccountIndexStoreTest extends BaseTest { Args.setParam( new String[]{ "--output-directory", dbPath(), - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory + "--storage-db-directory", dbDirectory }, TestConstants.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/AccountStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountStoreTest.java index 2fae33870cb..003c3fe4ab3 100755 --- a/framework/src/test/java/org/tron/core/db/AccountStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AccountStoreTest.java @@ -33,7 +33,6 @@ public class AccountStoreTest extends BaseTest { private static final byte[] data = TransactionStoreTest.randomBytes(32); private static String dbDirectory = "db_AccountStore_test"; - private static String indexDirectory = "index_AccountStore_test"; @Resource private AccountStore accountStore; @Resource @@ -48,8 +47,7 @@ public class AccountStoreTest extends BaseTest { Args.setParam( new String[]{ "--output-directory", dbPath(), - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory + "--storage-db-directory", dbDirectory }, TestConstants.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/TransactionHistoryTest.java b/framework/src/test/java/org/tron/core/db/TransactionHistoryTest.java index 676293efbc0..e6d5fbb7bcf 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionHistoryTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionHistoryTest.java @@ -17,7 +17,6 @@ public class TransactionHistoryTest extends BaseTest { private static final byte[] transactionId = TransactionStoreTest.randomBytes(32); private static String dbDirectory = "db_TransactionHistoryStore_test"; - private static String indexDirectory = "index_TransactionHistoryStore_test"; @Resource private TransactionHistoryStore transactionHistoryStore; @@ -27,8 +26,7 @@ public class TransactionHistoryTest extends BaseTest { Args.setParam( new String[]{ "--output-directory", dbPath(), - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory + "--storage-db-directory", dbDirectory }, TestConstants.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/TransactionRetStoreTest.java b/framework/src/test/java/org/tron/core/db/TransactionRetStoreTest.java index 6cd7af96577..3a13c7d5606 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionRetStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionRetStoreTest.java @@ -21,7 +21,6 @@ public class TransactionRetStoreTest extends BaseTest { private static final byte[] transactionId = TransactionStoreTest.randomBytes(32); private static final byte[] blockNum = ByteArray.fromLong(1); private static String dbDirectory = "db_TransactionRetStore_test"; - private static String indexDirectory = "index_TransactionRetStore_test"; @Resource private TransactionRetStore transactionRetStore; private static Transaction transaction; @@ -33,8 +32,7 @@ public class TransactionRetStoreTest extends BaseTest { static { Args.setParam(new String[]{"--output-directory", dbPath(), - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory}, TestConstants.TEST_CONF); + "--storage-db-directory", dbDirectory}, TestConstants.TEST_CONF); } @BeforeClass diff --git a/framework/src/test/java/org/tron/core/db/TransactionStoreTest.java b/framework/src/test/java/org/tron/core/db/TransactionStoreTest.java index 5341cffd171..b79c4cdfc14 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionStoreTest.java @@ -41,7 +41,6 @@ public class TransactionStoreTest extends BaseTest { private static final String WITNESS_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; private static String dbDirectory = "db_TransactionStore_test"; - private static String indexDirectory = "index_TransactionStore_test"; @Resource private TransactionStore transactionStore; diff --git a/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java b/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java index 08848fc9da1..162b2d793d4 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java @@ -52,7 +52,6 @@ public class TransactionTraceTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static String dbDirectory = "db_TransactionTrace_test"; - private static String indexDirectory = "index_TransactionTrace_test"; private static ByteString ownerAddress = ByteString.copyFrom(ByteArray.fromInt(1)); private static ByteString contractAddress = ByteString.copyFrom(ByteArray.fromInt(2)); private static String OwnerAddress = "TCWHANtDDdkZCTo2T2peyEq3Eg9c2XB7ut"; @@ -64,7 +63,6 @@ public class TransactionTraceTest extends BaseTest { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }, "config-test-mainnet.conf" diff --git a/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java b/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java index e47ef72a29d..1793fb0e6a9 100644 --- a/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java +++ b/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java @@ -18,9 +18,8 @@ public class TxCacheDBTest extends BaseTest { @BeforeClass public static void init() { String dbDirectory = "db_TransactionCache_test"; - String indexDirectory = "index_TransactionCache_test"; Args.setParam(new String[]{"--output-directory", dbPath(), "--storage-db-directory", - dbDirectory, "--storage-index-directory", indexDirectory}, TestConstants.TEST_CONF); + dbDirectory}, TestConstants.TEST_CONF); } @Test diff --git a/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java b/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java index 6894d91cdbe..f96a03d92e3 100644 --- a/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java +++ b/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java @@ -14,7 +14,6 @@ public class MetricsApiServiceTest extends BaseMethodTest { private static String dbDirectory = "metrics-database"; - private static String indexDirectory = "metrics-index"; private static int port = 10001; private MetricsApiService metricsApiService; private RpcApiService rpcApiService; @@ -23,7 +22,6 @@ public class MetricsApiServiceTest extends BaseMethodTest { protected String[] extraArgs() { return new String[]{ "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }; } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java index b66298d6779..c5e87384b99 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java @@ -25,6 +25,7 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.tron.common.parameter.CommonParameter; +import org.tron.core.Constant; public class JsonRpcServletTest { @@ -365,7 +366,7 @@ public void batchWithNumericAndStringElements_allGetInvalidRequest() throws Exce @Test public void excessivelyNestedRequest_returnsParseError() throws Exception { - int limit = CommonParameter.getInstance().getMaxNestingDepth(); + int limit = Constant.MAX_NESTING_DEPTH; StringBuilder sb = new StringBuilder(); for (int i = 0; i <= limit; i++) { sb.append('['); @@ -383,7 +384,7 @@ public void excessivelyNestedRequest_returnsParseError() throws Exception { @Test public void tooManyTokens_returnsParseError() throws Exception { - int limit = CommonParameter.getInstance().getMaxTokenCount(); + int limit = Constant.MAX_TOKEN_COUNT; StringBuilder sb = new StringBuilder("["); for (int i = 0; i < limit; i++) { if (i > 0) { diff --git a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java index 5d403b54f90..67a95e29beb 100644 --- a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java @@ -69,7 +69,6 @@ @Slf4j public class LibrustzcashTest extends BaseTest { private static final String dbDirectory = "db_Librustzcash_test"; - private static final String indexDirectory = "index_Librustzcash_test"; @Resource private Wallet wallet; @@ -79,7 +78,6 @@ public static void init() { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }, "config-test-mainnet.conf" diff --git a/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java b/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java index e21ba8010b5..407a0641f3a 100644 --- a/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java @@ -27,7 +27,6 @@ public class MerkleTreeTest extends BaseTest { public static final long totalBalance = 1000_0000_000_000L; private static final String dbDirectory = "db_ShieldedTransaction_test"; - private static final String indexDirectory = "index_ShieldedTransaction_test"; private static boolean init; static { @@ -35,7 +34,6 @@ public class MerkleTreeTest extends BaseTest { new String[]{ "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory, "--debug" }, "config-test-mainnet.conf" diff --git a/framework/src/test/java/org/tron/json/JsonTest.java b/framework/src/test/java/org/tron/json/JsonTest.java index 2a6d73931be..eb9fed8ff2c 100644 --- a/framework/src/test/java/org/tron/json/JsonTest.java +++ b/framework/src/test/java/org/tron/json/JsonTest.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Locale; import org.junit.Test; +import org.tron.core.Constant; /** * Tests for Jackson {@code JsonReadFeature} compatibility with Fastjson 1.x. @@ -369,8 +370,8 @@ public void testTypeUtilsCoercion() { @Test public void testJsonMapperHasConfiguredConstraints() { StreamReadConstraints sr = JSON.MAPPER.getFactory().streamReadConstraints(); - assertEquals(100, sr.getMaxNestingDepth()); - assertEquals(100_000L, sr.getMaxTokenCount()); + assertEquals(Constant.MAX_NESTING_DEPTH, sr.getMaxNestingDepth()); + assertEquals((long) Constant.MAX_TOKEN_COUNT, sr.getMaxTokenCount()); } @Test diff --git a/framework/src/test/resources/args-test.conf b/framework/src/test/resources/args-test.conf index db889483270..a65b44b0dff 100644 --- a/framework/src/test/resources/args-test.conf +++ b/framework/src/test/resources/args-test.conf @@ -9,7 +9,6 @@ storage { db.engine = "LEVELDB" db.directory = "database", - index.directory = "index", # You can custom these 14 databases' configs: diff --git a/framework/src/test/resources/config-localtest.conf b/framework/src/test/resources/config-localtest.conf index 4c6910e3d7a..26b273efa89 100644 --- a/framework/src/test/resources/config-localtest.conf +++ b/framework/src/test/resources/config-localtest.conf @@ -7,13 +7,6 @@ storage { # Directory for storing persistent data db.engine ="LEVELDB", db.directory = "database", - index.directory = "index", - - # This configuration item is only for SolidityNode. - # Turn off the index is "off", else "on". - # Turning off the index will significantly improve the performance of the SolidityNode sync block. - # You can turn off the index if you don't use the two interfaces getTransactionsToThis and getTransactionsFromThis. - index.switch = "on" # You can custom these 14 databases' configs: diff --git a/framework/src/test/resources/config-test-index.conf b/framework/src/test/resources/config-test-index.conf index 583064a37f5..8dbff89219c 100644 --- a/framework/src/test/resources/config-test-index.conf +++ b/framework/src/test/resources/config-test-index.conf @@ -8,7 +8,6 @@ storage { # Directory for storing persistent data db.directory = "database", - index.directory = "index", # You can custom these 14 databases' configs: diff --git a/framework/src/test/resources/config-test-mainnet.conf b/framework/src/test/resources/config-test-mainnet.conf index 938812f8214..ec2c0884cac 100644 --- a/framework/src/test/resources/config-test-mainnet.conf +++ b/framework/src/test/resources/config-test-mainnet.conf @@ -8,7 +8,6 @@ storage { # Directory for storing persistent data db.directory = "database", - index.directory = "index", # You can custom these 14 databases' configs: diff --git a/framework/src/test/resources/config-test-storagetest.conf b/framework/src/test/resources/config-test-storagetest.conf index 113c8371ba1..f0f993a2fb7 100644 --- a/framework/src/test/resources/config-test-storagetest.conf +++ b/framework/src/test/resources/config-test-storagetest.conf @@ -9,7 +9,6 @@ storage { db.engine = "LEVELDB" db.directory = "database", - index.directory = "index", # You can custom these 14 databases' configs: diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index 85172c37710..fbe4850db01 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -9,7 +9,6 @@ storage { db.engine = "LEVELDB" db.directory = "database", - index.directory = "index", # You can custom these 14 databases' configs: diff --git a/plugins/src/test/resources/config-duplicate.conf b/plugins/src/test/resources/config-duplicate.conf index f2eb7fbf357..05358016810 100644 --- a/plugins/src/test/resources/config-duplicate.conf +++ b/plugins/src/test/resources/config-duplicate.conf @@ -4,7 +4,6 @@ storage { db.engine = "LEVELDB", db.sync = false, db.directory = "database", - index.directory = "index", transHistory.switch = "on", properties = [ { From 01441dc6092c652e5ab81af6c307d39eb75f6b59 Mon Sep 17 00:00:00 2001 From: Federico2014 Date: Tue, 26 May 2026 14:58:53 +0800 Subject: [PATCH 19/57] fix(net): restrict admission signature length (#6782) --- .../src/main/java/org/tron/core/Constant.java | 3 +- .../org/tron/common/crypto/SignUtils.java | 18 ++++ .../src/main/java/org/tron/core/Wallet.java | 10 +++ .../TransactionsMsgHandler.java | 8 ++ .../core/net/service/relay/RelayService.java | 6 ++ .../java/org/tron/core/WalletMockTest.java | 48 +++++++++++ .../TransactionsMsgHandlerTest.java | 85 +++++++++++++++++++ .../core/net/services/RelayServiceTest.java | 24 ++++++ 8 files changed, 201 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index ebd1bd4c398..3a2c59d5139 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -19,7 +19,8 @@ public class Constant { public static final long MAXIMUM_TIME_UNTIL_EXPIRATION = 24 * 60 * 60 * 1_000L; //one day public static final long TRANSACTION_DEFAULT_EXPIRATION_TIME = 60 * 1_000L; //60 seconds public static final long TRANSACTION_FEE_POOL_PERIOD = 1; //1 blocks - public static final long PER_SIGN_LENGTH = 65L; + public static final int PER_SIGN_LENGTH = 65; + public static final int MAX_PER_SIGN_LENGTH = 68; public static final long MAX_CONTRACT_RESULT_SIZE = 2L; // Smart contract / Energy diff --git a/crypto/src/main/java/org/tron/common/crypto/SignUtils.java b/crypto/src/main/java/org/tron/common/crypto/SignUtils.java index b921d548e8b..e0e20fb2677 100644 --- a/crypto/src/main/java/org/tron/common/crypto/SignUtils.java +++ b/crypto/src/main/java/org/tron/common/crypto/SignUtils.java @@ -1,5 +1,8 @@ package org.tron.common.crypto; +import static org.tron.core.Constant.MAX_PER_SIGN_LENGTH; +import static org.tron.core.Constant.PER_SIGN_LENGTH; + import java.security.SecureRandom; import java.security.SignatureException; import org.tron.common.crypto.ECKey.ECDSASignature; @@ -8,6 +11,21 @@ public class SignUtils { + /** + * Strict signature-length check for admission entry-points (RPC broadcast, + * P2P transaction ingress, peer hello handshake). Accepts only sizes in + * [{@link org.tron.core.Constant#PER_SIGN_LENGTH PER_SIGN_LENGTH}, + * {@link org.tron.core.Constant#MAX_PER_SIGN_LENGTH MAX_PER_SIGN_LENGTH}]. + * + *

Consensus paths (e.g. {@code TransactionCapsule.checkWeight}) intentionally + * keep the looser {@code size < 65} check to remain compatible with historical + * on-chain signatures that carry trailing padding bytes; do not call this + * helper from those paths. + */ + public static boolean isValidLength(int size) { + return size >= PER_SIGN_LENGTH && size <= MAX_PER_SIGN_LENGTH; + } + public static SignInterface getGeneratedRandomSign( SecureRandom secureRandom, boolean isECKeyCryptoEngine) { if (isECKeyCryptoEngine) { diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index f7c2332303f..72b8d7090d9 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -505,6 +505,16 @@ public GrpcAPI.Return broadcastTransaction(Transaction signedTransaction) { trx.setTime(System.currentTimeMillis()); Sha256Hash txID = trx.getTransactionId(); try { + for (ByteString sig : signedTransaction.getSignatureList()) { + if (!SignUtils.isValidLength(sig.size())) { + String info = "Signature size is " + sig.size(); + logger.warn("Broadcast transaction {} has failed, {}.", txID, info); + return builder.setResult(false).setCode(response_code.SIGERROR) + .setMessage(ByteString.copyFromUtf8("Validate signature error: " + info)) + .build(); + } + } + if (tronNetDelegate.isBlockUnsolidified()) { logger.warn("Broadcast transaction {} has failed, block unsolidified.", txID); return builder.setResult(false).setCode(response_code.BLOCK_UNSOLIDIFIED) diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java index e153e21f331..52137c5881c 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java @@ -1,5 +1,6 @@ package org.tron.core.net.messagehandler; +import com.google.protobuf.ByteString; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.tron.common.crypto.SignUtils; import org.tron.common.es.ExecutorServiceManager; import org.tron.common.utils.Sha256Hash; import org.tron.core.ChainBaseManager; @@ -142,6 +144,12 @@ private void check(PeerConnection peer, TransactionsMessage msg) throws P2pExcep throw new P2pException(TypeEnum.BAD_TRX, "tx " + item.getHash() + " contract size should be greater than 0"); } + for (ByteString sig : trx.getSignatureList()) { + if (!SignUtils.isValidLength(sig.size())) { + throw new P2pException(TypeEnum.BAD_TRX, + "tx " + item.getHash() + " signature size is " + sig.size()); + } + } } } diff --git a/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java b/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java index 61ae6326e9f..d4e010ff21d 100644 --- a/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java +++ b/framework/src/main/java/org/tron/core/net/service/relay/RelayService.java @@ -150,6 +150,12 @@ public boolean checkHelloMessage(HelloMessage message, Channel channel) { return false; } + if (!SignUtils.isValidLength(msg.getSignature().size())) { + logger.warn("HelloMessage from {}, signature size is {}.", + channel.getInetAddress(), msg.getSignature().size()); + return false; + } + boolean flag; try { Sha256Hash hash = Sha256Hash.of(CommonParameter diff --git a/framework/src/test/java/org/tron/core/WalletMockTest.java b/framework/src/test/java/org/tron/core/WalletMockTest.java index 3e0c1a4461d..c9184bf276d 100644 --- a/framework/src/test/java/org/tron/core/WalletMockTest.java +++ b/framework/src/test/java/org/tron/core/WalletMockTest.java @@ -164,6 +164,54 @@ public void testCreateTransactionCapsuleWithoutValidateWithTimeout() } + @Test + public void testBroadcastTxInvalidSigLength() throws Exception { + Wallet wallet = new Wallet(); + TronNetDelegate tronNetDelegateMock = mock(TronNetDelegate.class); + Field field = wallet.getClass().getDeclaredField("tronNetDelegate"); + field.setAccessible(true); + field.set(wallet, tronNetDelegateMock); + + // signature shorter than 65 bytes → SIGERROR + Protocol.Transaction shortSig = Protocol.Transaction.newBuilder() + .addSignature(ByteString.copyFrom(new byte[64])) + .build(); + GrpcAPI.Return ret = wallet.broadcastTransaction(shortSig); + assertEquals(GrpcAPI.Return.response_code.SIGERROR, ret.getCode()); + + // signature longer than 68 bytes → SIGERROR + Protocol.Transaction longSig = Protocol.Transaction.newBuilder() + .addSignature(ByteString.copyFrom(new byte[69])) + .build(); + ret = wallet.broadcastTransaction(longSig); + assertEquals(GrpcAPI.Return.response_code.SIGERROR, ret.getCode()); + + // empty signature → SIGERROR + Protocol.Transaction emptySig = Protocol.Transaction.newBuilder() + .addSignature(ByteString.EMPTY) + .build(); + ret = wallet.broadcastTransaction(emptySig); + assertEquals(GrpcAPI.Return.response_code.SIGERROR, ret.getCode()); + + // tronNetDelegate must not be consulted because the request is rejected up front + Mockito.verify(tronNetDelegateMock, Mockito.never()).isBlockUnsolidified(); + + // 65-byte signature passes the length check and proceeds to downstream logic + when(tronNetDelegateMock.isBlockUnsolidified()).thenReturn(true); + Protocol.Transaction validSig = Protocol.Transaction.newBuilder() + .addSignature(ByteString.copyFrom(new byte[65])) + .build(); + ret = wallet.broadcastTransaction(validSig); + assertEquals(GrpcAPI.Return.response_code.BLOCK_UNSOLIDIFIED, ret.getCode()); + + // 68-byte signature (upper bound) also passes the length check + Protocol.Transaction paddedSig = Protocol.Transaction.newBuilder() + .addSignature(ByteString.copyFrom(new byte[68])) + .build(); + ret = wallet.broadcastTransaction(paddedSig); + assertEquals(GrpcAPI.Return.response_code.BLOCK_UNSOLIDIFIED, ret.getCode()); + } + @Test public void testBroadcastTransactionBlockUnsolidified() throws Exception { Wallet wallet = new Wallet(); diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java index abe69e76ff2..ed2121d360f 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java @@ -337,6 +337,91 @@ public void testDuplicateTransactionRejected() throws Exception { } } + @Test + public void testInvalidSigLength() throws Exception { + TransactionsMsgHandler handler = new TransactionsMsgHandler(); + handler.init(); + try { + PeerConnection peer = Mockito.mock(PeerConnection.class); + + BalanceContract.TransferContract transferContract = BalanceContract.TransferContract + .newBuilder() + .setAmount(10) + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString("121212a9cf"))) + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString("232323a9cf"))) + .build(); + + // signature shorter than 65 bytes → BAD_TRX + Protocol.Transaction shortSigTrx = Protocol.Transaction.newBuilder() + .setRawData(Protocol.Transaction.raw.newBuilder() + .addContract(Protocol.Transaction.Contract.newBuilder() + .setType(Protocol.Transaction.Contract.ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()) + .build()) + .addSignature(ByteString.copyFrom(new byte[64])) + .build(); + + List shortList = new ArrayList<>(); + shortList.add(shortSigTrx); + stubAdvInvRequest(peer, new TransactionsMessage(shortList)); + P2pException shortEx = Assert.assertThrows(P2pException.class, + () -> handler.processMessage(peer, new TransactionsMessage(shortList))); + Assert.assertEquals(TypeEnum.BAD_TRX, shortEx.getType()); + + // signature longer than 68 bytes → BAD_TRX + Protocol.Transaction longSigTrx = Protocol.Transaction.newBuilder() + .setRawData(Protocol.Transaction.raw.newBuilder() + .setRefBlockNum(1) + .addContract(Protocol.Transaction.Contract.newBuilder() + .setType(Protocol.Transaction.Contract.ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()) + .build()) + .addSignature(ByteString.copyFrom(new byte[69])) + .build(); + + List longList = new ArrayList<>(); + longList.add(longSigTrx); + stubAdvInvRequest(peer, new TransactionsMessage(longList)); + P2pException longEx = Assert.assertThrows(P2pException.class, + () -> handler.processMessage(peer, new TransactionsMessage(longList))); + Assert.assertEquals(TypeEnum.BAD_TRX, longEx.getType()); + + // exactly 65 bytes → passes the length check (no P2pException from check) + Protocol.Transaction validSigTrx = Protocol.Transaction.newBuilder() + .setRawData(Protocol.Transaction.raw.newBuilder() + .setRefBlockNum(2) + .addContract(Protocol.Transaction.Contract.newBuilder() + .setType(Protocol.Transaction.Contract.ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()) + .build()) + .addSignature(ByteString.copyFrom(new byte[65])) + .build(); + + List validList = new ArrayList<>(); + validList.add(validSigTrx); + stubAdvInvRequest(peer, new TransactionsMessage(validList)); + handler.processMessage(peer, new TransactionsMessage(validList)); + + // 68 bytes (upper bound) also passes the length check + Protocol.Transaction paddedSigTrx = Protocol.Transaction.newBuilder() + .setRawData(Protocol.Transaction.raw.newBuilder() + .setRefBlockNum(3) + .addContract(Protocol.Transaction.Contract.newBuilder() + .setType(Protocol.Transaction.Contract.ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()) + .build()) + .addSignature(ByteString.copyFrom(new byte[68])) + .build(); + + List paddedList = new ArrayList<>(); + paddedList.add(paddedSigTrx); + stubAdvInvRequest(peer, new TransactionsMessage(paddedList)); + handler.processMessage(peer, new TransactionsMessage(paddedList)); + } finally { + handler.close(); + } + } + @Test public void testIsBusyWithCachedTransactions() throws Exception { TransactionsMsgHandler handler = new TransactionsMsgHandler(); diff --git a/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java b/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java index 8585244b941..7c28757bd5c 100644 --- a/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/RelayServiceTest.java @@ -220,6 +220,30 @@ private void testCheckHelloMessage() { boolean res = service.checkHelloMessage(helloMessage, c1); Assert.assertTrue(res); + + HelloMessage shortSigMsg = new HelloMessage(node, System.currentTimeMillis(), + ChainBaseManager.getChainBaseManager()); + shortSigMsg.setHelloMessage(shortSigMsg.getHelloMessage().toBuilder() + .setAddress(address) + .setSignature(ByteString.copyFrom(new byte[64])) + .build()); + Assert.assertFalse(service.checkHelloMessage(shortSigMsg, c1)); + + HelloMessage longSigMsg = new HelloMessage(node, System.currentTimeMillis(), + ChainBaseManager.getChainBaseManager()); + longSigMsg.setHelloMessage(longSigMsg.getHelloMessage().toBuilder() + .setAddress(address) + .setSignature(ByteString.copyFrom(new byte[69])) + .build()); + Assert.assertFalse(service.checkHelloMessage(longSigMsg, c1)); + + HelloMessage emptySigMsg = new HelloMessage(node, System.currentTimeMillis(), + ChainBaseManager.getChainBaseManager()); + emptySigMsg.setHelloMessage(emptySigMsg.getHelloMessage().toBuilder() + .setAddress(address) + .setSignature(ByteString.EMPTY) + .build()); + Assert.assertFalse(service.checkHelloMessage(emptySigMsg, c1)); } catch (Exception e) { logger.info("", e); assert false; From 274541d0984f5a776c6bf0af533ae4a0d714b57e Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 26 May 2026 15:02:17 +0800 Subject: [PATCH 20/57] fix(log): suppress spurious error logs when solidity node exits (#6801) --- .../java/org/tron/program/SolidityNode.java | 16 +- .../org/tron/program/SolidityNodeTest.java | 603 +++++++++--------- 2 files changed, 333 insertions(+), 286 deletions(-) diff --git a/framework/src/main/java/org/tron/program/SolidityNode.java b/framework/src/main/java/org/tron/program/SolidityNode.java index 0998d8846c0..9dbe92fb78e 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -117,7 +117,15 @@ private void getBlock() { Block block = getBlockByNum(blockNum); blockQueue.put(block); blockNum = ID.incrementAndGet(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.info("getBlock interrupted, exiting."); + return; } catch (Exception e) { + if (!flag) { + logger.info("getBlock stopped during shutdown, last block: {}.", blockNum); + return; + } logger.error("Failed to get block {}, reason: {}.", blockNum, e.getMessage()); sleep(exceptionSleepTime); } @@ -194,6 +202,10 @@ private long getLastSolidityBlockNum() { blockNum, remoteBlockNum, System.currentTimeMillis() - time); return blockNum; } catch (Exception e) { + if (!flag) { + logger.info("getLastSolidityBlockNum stopped during shutdown."); + return 0; + } logger.error("Failed to get last solid blockNum: {}, reason: {}.", remoteBlockNum.get(), e.getMessage()); sleep(exceptionSleepTime); @@ -205,8 +217,8 @@ private long getLastSolidityBlockNum() { public void sleep(long time) { try { Thread.sleep(time); - } catch (Exception e1) { - logger.error(e1.getMessage()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } } diff --git a/framework/src/test/java/org/tron/program/SolidityNodeTest.java b/framework/src/test/java/org/tron/program/SolidityNodeTest.java index a02eb22364e..7842eed8484 100755 --- a/framework/src/test/java/org/tron/program/SolidityNodeTest.java +++ b/framework/src/test/java/org/tron/program/SolidityNodeTest.java @@ -75,7 +75,7 @@ private void setFlag(boolean value) throws Exception { f.set(solidityNode, value); } - // ── existing tests ──────────────────────────────────────────────────────────── + // ── gRPC / HTTP service integration ────────────────────────────────────────── @Test public void testSolidityGrpcCall() { @@ -115,7 +115,7 @@ public void testSolidityNodeHttpApiService() { Assert.assertTrue(true); } - // ── new tests ───────────────────────────────────────────────────────────────── + // ── lifecycle ───────────────────────────────────────────────────────────────── /** * @PostConstruct init() must create both executor services before run() is called. @@ -146,234 +146,312 @@ public void testOnApplicationEventSetsFlagFalse() throws Exception { } /** - * getBlockByNum() must throw RuntimeException (not return null) when - * flag=false, to prevent NullPointerException in blockQueue.put(). + * SolidityCondition must match when --solidity is passed so the bean is + * registered in the Spring context. */ - @Test(timeout = 1000) - public void testGetBlockByNumThrowsWhenClosed() throws Exception { - setFlag(false); - try { - Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); - m.setAccessible(true); - try { - m.invoke(solidityNode, 1L); - Assert.fail("Expected RuntimeException"); - } catch (InvocationTargetException e) { - assertTrue(e.getCause() instanceof RuntimeException); - assertEquals("SolidityNode is closing.", e.getCause().getMessage()); - } - } finally { - setFlag(true); - } + @Test + public void testSolidityConditionMatchesWhenSolidityFlagSet() { + assertTrue(Args.getInstance().isSolidityNode()); + SolidityNode.SolidityCondition condition = new SolidityNode.SolidityCondition(); + assertTrue(condition.matches( + mock(ConditionContext.class), + mock(AnnotatedTypeMetadata.class))); } /** - * getLastSolidityBlockNum() must return 0 (not throw) when flag=false so - * getBlock()'s while(flag) loop exits quietly without a misleading error log. + * resolveCompatibilityIssueIfUsingFullNodeDatabase() must update the solidified + * block num to match headBlockNum when solidity lags behind. */ - @Test(timeout = 1000) - public void testGetLastSolidityBlockNumReturnsZeroWhenClosed() throws Exception { - setFlag(false); + @Test(timeout = 2000) + public void testResolveCompatibilityIssueWhenSolidityLagsHead() throws Exception { + DynamicPropertiesStore mockStore = mock(DynamicPropertiesStore.class); + Mockito.when(mockStore.getLatestSolidifiedBlockNum()).thenReturn(3L); + ChainBaseManager mockCbm = mock(ChainBaseManager.class); + Mockito.when(mockCbm.getDynamicPropertiesStore()).thenReturn(mockStore); + Mockito.when(mockCbm.getHeadBlockNum()).thenReturn(10L); + + Field cbmField = getField("chainBaseManager"); + Object orig = cbmField.get(solidityNode); + cbmField.set(solidityNode, mockCbm); try { - Method m = SolidityNode.class.getDeclaredMethod("getLastSolidityBlockNum"); + Method m = SolidityNode.class.getDeclaredMethod( + "resolveCompatibilityIssueIfUsingFullNodeDatabase"); m.setAccessible(true); - long result = (long) m.invoke(solidityNode); - assertEquals(0L, result); + m.invoke(solidityNode); } finally { - setFlag(true); + cbmField.set(solidityNode, orig); } + Mockito.verify(mockStore).saveLatestSolidifiedBlockNum(10L); } /** - * SolidityCondition must match when --solidity is passed so the bean is - * registered in the Spring context. + * When databaseGrpcClient is non-null at shutdown time, its shutdown() must + * be called to close the gRPC channel. */ @Test - public void testSolidityConditionMatchesWhenSolidityFlagSet() { - assertTrue(Args.getInstance().isSolidityNode()); - SolidityNode.SolidityCondition condition = new SolidityNode.SolidityCondition(); - assertTrue(condition.matches( - mock(ConditionContext.class), - mock(AnnotatedTypeMetadata.class))); - } + public void testShutdownCallsDatabaseClientShutdown() throws Exception { + // Use a standalone instance so we don't destroy the shared Spring executor services. + SolidityNode node = new SolidityNode(); - // ── additional coverage tests ───────────────────────────────────────────────── + DynamicPropertiesStore mockStore = mock(DynamicPropertiesStore.class); + ChainBaseManager mockCbm = mock(ChainBaseManager.class); + Mockito.when(mockCbm.getDynamicPropertiesStore()).thenReturn(mockStore); + Mockito.when(mockCbm.getHeadBlockNum()).thenReturn(0L); + getField("chainBaseManager").set(node, mockCbm); - /** - * sleep() must return normally without throwing. - */ - @Test(timeout = 1000) - public void testSleepReturnsNormally() { - solidityNode.sleep(1); + Method initM = SolidityNode.class.getDeclaredMethod("init"); + initM.setAccessible(true); + initM.invoke(node); + + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + getField("databaseGrpcClient").set(node, mockClient); + + Method shutdownM = SolidityNode.class.getDeclaredMethod("shutdown"); + shutdownM.setAccessible(true); + shutdownM.invoke(node); + + Mockito.verify(mockClient).shutdown(); } + // ── sleep() ─────────────────────────────────────────────────────────────────── + /** - * sleep() must swallow InterruptedException so callers are not surprised; - * the thread continues after waking. + * sleep() must: + * - return normally without throwing on a plain call, + * - exit early when the thread is interrupted, + * - restore the interrupt flag so callers can observe it immediately. */ @Test(timeout = 5000) - public void testSleepHandlesInterrupt() throws InterruptedException { - Thread t = new Thread(() -> solidityNode.sleep(10_000)); + public void testSleep() throws InterruptedException { + // Normal: returns without throwing. + solidityNode.sleep(1); + + // Interrupt: exits early + restores flag. + boolean[] flagAfterSleep = {false}; + Thread t = new Thread(() -> { + solidityNode.sleep(10_000); + flagAfterSleep[0] = Thread.currentThread().isInterrupted(); + }); t.start(); Thread.sleep(50); t.interrupt(); t.join(2000); - assertFalse("sleep() should have returned after interrupt", t.isAlive()); + assertFalse("sleep() must return after interrupt", t.isAlive()); + assertTrue("sleep() must restore the interrupt flag", flagAfterSleep[0]); } + // ── getBlockByNum() ─────────────────────────────────────────────────────────── + /** - * getBlockByNum() must return the block when the gRPC client returns a block - * whose number matches the requested number. + * getBlockByNum() normal-path and transient-error recovery: + * - happy path: returns the block when the gRPC response number matches, + * - null response: warns and retries on the next iteration, + * - RPC exception: logs, sleeps, and succeeds on the second attempt. */ - @Test(timeout = 2000) - public void testGetBlockByNumReturnsMatchingBlock() throws Exception { - Block expected = blockWithNum(7L); - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getBlock(7L)).thenReturn(expected); - + @Test(timeout = 6000) + public void testGetBlockByNum() throws Exception { + Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); + m.setAccessible(true); Field clientField = getField("databaseGrpcClient"); Object orig = clientField.get(solidityNode); - clientField.set(solidityNode, mockClient); try { - Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); - m.setAccessible(true); + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + clientField.set(solidityNode, mockClient); + + // Happy path: matching block returned directly. + Mockito.when(mockClient.getBlock(7L)).thenReturn(blockWithNum(7L)); Block result = (Block) m.invoke(solidityNode, 7L); assertEquals(7L, result.getBlockHeader().getRawData().getNumber()); + + // Null response: warn + retry, succeed on second call. + Mockito.when(mockClient.getBlock(5L)) + .thenReturn(null) + .thenReturn(blockWithNum(5L)); + result = (Block) m.invoke(solidityNode, 5L); + assertEquals(5L, result.getBlockHeader().getRawData().getNumber()); + Mockito.verify(mockClient, Mockito.times(2)).getBlock(5L); + + // RPC exception: log + retry, succeed on second call. + Mockito.when(mockClient.getBlock(8L)) + .thenThrow(new RuntimeException("rpc error")) + .thenReturn(blockWithNum(8L)); + result = (Block) m.invoke(solidityNode, 8L); + assertEquals(8L, result.getBlockHeader().getRawData().getNumber()); } finally { clientField.set(solidityNode, orig); } } /** - * getLastSolidityBlockNum() must return the value obtained from the gRPC - * client when the call succeeds. + * getBlockByNum() shutdown paths: must throw RuntimeException (not return + * null) in two cases so callers can detect closure cleanly: + * - flag=false before the loop starts (immediate exit), + * - wrong block number returned and flag races to false during the retry sleep. */ - @Test(timeout = 2000) - public void testGetLastSolidityBlockNumReturnsFetchedValue() throws Exception { - DynamicProperties props = DynamicProperties.newBuilder() - .setLastSolidityBlockNum(99L).build(); - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getDynamicProperties()).thenReturn(props); + @Test(timeout = 5000) + public void testGetBlockByNumWhenClosed() throws Exception { + Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); + m.setAccessible(true); + + // flag=false: while condition exits immediately. + setFlag(false); + try { + try { + m.invoke(solidityNode, 1L); + Assert.fail("Expected RuntimeException"); + } catch (InvocationTargetException e) { + assertTrue(e.getCause() instanceof RuntimeException); + assertEquals("SolidityNode is closing.", e.getCause().getMessage()); + } + } finally { + setFlag(true); + } + // Wrong block number returned: flag goes false → loop exits → throws. + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + Mockito.when(mockClient.getBlock(9L)).thenAnswer(inv -> { + setFlag(false); + return blockWithNum(999L); + }); Field clientField = getField("databaseGrpcClient"); Object orig = clientField.get(solidityNode); clientField.set(solidityNode, mockClient); try { - Method m = SolidityNode.class.getDeclaredMethod("getLastSolidityBlockNum"); - m.setAccessible(true); - long result = (long) m.invoke(solidityNode); - assertEquals(99L, result); + try { + m.invoke(solidityNode, 9L); + Assert.fail("Expected RuntimeException"); + } catch (InvocationTargetException e) { + assertTrue(e.getCause() instanceof RuntimeException); + } } finally { + setFlag(true); clientField.set(solidityNode, orig); } } + // ── getLastSolidityBlockNum() ───────────────────────────────────────────────── + /** - * loopProcessBlock() must persist the solidified block num when pushVerifiedBlock - * succeeds and hitDown is false. + * getLastSolidityBlockNum() normal-path and retry: + * - happy path: returns the value from getDynamicProperties(), + * - RPC exception: logs, sleeps, and returns the value on the second attempt. */ - @Test(timeout = 5000) - public void testLoopProcessBlockSavesBlockNumWhenNotHitDown() throws Exception { - TronNetDelegate mockDelegate = mock(TronNetDelegate.class); - Mockito.when(mockDelegate.isHitDown()).thenReturn(false); - - long origSolidified = chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum(); - Field delegateField = getField("tronNetDelegate"); - Object origDelegate = delegateField.get(solidityNode); - delegateField.set(solidityNode, mockDelegate); + @Test(timeout = 4000) + public void testGetLastSolidityBlockNum() throws Exception { + Method m = SolidityNode.class.getDeclaredMethod("getLastSolidityBlockNum"); + m.setAccessible(true); + Field clientField = getField("databaseGrpcClient"); + Object orig = clientField.get(solidityNode); try { - invokeLoopProcessBlock(blockWithNum(55L)); - assertEquals(55L, chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum()); + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + clientField.set(solidityNode, mockClient); + + // Happy path. + Mockito.when(mockClient.getDynamicProperties()) + .thenReturn(DynamicProperties.newBuilder().setLastSolidityBlockNum(99L).build()); + assertEquals(99L, (long) m.invoke(solidityNode)); + + // RPC exception: retry, return value on second attempt. + Mockito.when(mockClient.getDynamicProperties()) + .thenThrow(new RuntimeException("rpc error")) + .thenReturn(DynamicProperties.newBuilder().setLastSolidityBlockNum(50L).build()); + assertEquals(50L, (long) m.invoke(solidityNode)); } finally { - chainBaseManager.getDynamicPropertiesStore() - .saveLatestSolidifiedBlockNum(origSolidified); - delegateField.set(solidityNode, origDelegate); + clientField.set(solidityNode, orig); } } /** - * loopProcessBlock() must NOT persist the solidified block num when hitDown - * is true, because the block was never pushed to BlockStore. + * getLastSolidityBlockNum() shutdown paths: must return 0 without looping in + * two cases: + * - flag=false before the loop starts (while condition fails), + * - exception thrown after flag races to false during the gRPC call. */ - @Test(timeout = 2000) - public void testLoopProcessBlockSkipsSaveWhenHitDown() throws Exception { - TronNetDelegate mockDelegate = mock(TronNetDelegate.class); - Mockito.when(mockDelegate.isHitDown()).thenReturn(true); + @Test(timeout = 3000) + public void testGetLastSolidityBlockNumWhenClosed() throws Exception { + Method m = SolidityNode.class.getDeclaredMethod("getLastSolidityBlockNum"); + m.setAccessible(true); - long origSolidified = chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum(); - Field delegateField = getField("tronNetDelegate"); - Object origDelegate = delegateField.get(solidityNode); - delegateField.set(solidityNode, mockDelegate); + // flag=false: while condition exits immediately, returns 0. + setFlag(false); try { - invokeLoopProcessBlock(blockWithNum(56L)); - assertEquals(origSolidified, chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum()); + assertEquals(0L, (long) m.invoke(solidityNode)); } finally { - delegateField.set(solidityNode, origDelegate); + setFlag(true); } - } - /** - * resolveCompatibilityIssueIfUsingFullNodeDatabase() must update the solidified - * block num to match headBlockNum when solidity lags behind. - */ - @Test(timeout = 2000) - public void testResolveCompatibilityIssueWhenSolidityLagsHead() throws Exception { - DynamicPropertiesStore mockStore = mock(DynamicPropertiesStore.class); - Mockito.when(mockStore.getLatestSolidifiedBlockNum()).thenReturn(3L); - ChainBaseManager mockCbm = mock(ChainBaseManager.class); - Mockito.when(mockCbm.getDynamicPropertiesStore()).thenReturn(mockStore); - Mockito.when(mockCbm.getHeadBlockNum()).thenReturn(10L); - - Field cbmField = getField("chainBaseManager"); - Object orig = cbmField.get(solidityNode); - cbmField.set(solidityNode, mockCbm); + // Exception while flag races to false: !flag guard returns 0 with INFO. + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + Mockito.when(mockClient.getDynamicProperties()).thenAnswer(inv -> { + setFlag(false); + throw new RuntimeException("channel closed during shutdown"); + }); + Field clientField = getField("databaseGrpcClient"); + Object orig = clientField.get(solidityNode); + clientField.set(solidityNode, mockClient); try { - Method m = SolidityNode.class.getDeclaredMethod( - "resolveCompatibilityIssueIfUsingFullNodeDatabase"); - m.setAccessible(true); - m.invoke(solidityNode); + assertEquals(0L, (long) m.invoke(solidityNode)); } finally { - cbmField.set(solidityNode, orig); + setFlag(true); + clientField.set(solidityNode, orig); } - Mockito.verify(mockStore).saveLatestSolidifiedBlockNum(10L); } - // ── shutdown / databaseGrpcClient lifecycle ────────────────────────────────── + // ── loopProcessBlock() ──────────────────────────────────────────────────────── /** - * When databaseGrpcClient is non-null at shutdown time, its shutdown() must - * be called to close the gRPC channel. + * loopProcessBlock() behaviour across three scenarios: + * - hitDown=false: solidified block num is persisted after a successful push, + * - hitDown=true: solidified block num is NOT updated (block not in store), + * - push throws on first attempt: retries after sleep and succeeds on second. */ - @Test - public void testShutdownCallsDatabaseClientShutdown() throws Exception { - // Use a standalone instance so we don't destroy the shared Spring executor services. - SolidityNode node = new SolidityNode(); - - DynamicPropertiesStore mockStore = mock(DynamicPropertiesStore.class); - ChainBaseManager mockCbm = mock(ChainBaseManager.class); - Mockito.when(mockCbm.getDynamicPropertiesStore()).thenReturn(mockStore); - Mockito.when(mockCbm.getHeadBlockNum()).thenReturn(0L); - getField("chainBaseManager").set(node, mockCbm); - - Method initM = SolidityNode.class.getDeclaredMethod("init"); - initM.setAccessible(true); - initM.invoke(node); - - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - getField("databaseGrpcClient").set(node, mockClient); - - Method shutdownM = SolidityNode.class.getDeclaredMethod("shutdown"); - shutdownM.setAccessible(true); - shutdownM.invoke(node); + @Test(timeout = 6000) + public void testLoopProcessBlock() throws Exception { + long origSolidified = chainBaseManager.getDynamicPropertiesStore() + .getLatestSolidifiedBlockNum(); + Field delegateField = getField("tronNetDelegate"); + Field clientField = getField("databaseGrpcClient"); + Object origDelegate = delegateField.get(solidityNode); + Object origClient = clientField.get(solidityNode); + try { + // hitDown=false: solidified block num must be saved. + TronNetDelegate notHitDown = mock(TronNetDelegate.class); + Mockito.when(notHitDown.isHitDown()).thenReturn(false); + delegateField.set(solidityNode, notHitDown); + invokeLoopProcessBlock(blockWithNum(55L)); + assertEquals(55L, chainBaseManager.getDynamicPropertiesStore() + .getLatestSolidifiedBlockNum()); - Mockito.verify(mockClient).shutdown(); + // hitDown=true: solidified block num must NOT change. + TronNetDelegate hitDown = mock(TronNetDelegate.class); + Mockito.when(hitDown.isHitDown()).thenReturn(true); + delegateField.set(solidityNode, hitDown); + invokeLoopProcessBlock(blockWithNum(56L)); + assertEquals(55L, chainBaseManager.getDynamicPropertiesStore() + .getLatestSolidifiedBlockNum()); // unchanged + + // Exception on first push: sleep, re-fetch, succeed on second push. + TronNetDelegate retryDelegate = mock(TronNetDelegate.class); + Mockito.when(retryDelegate.isHitDown()).thenReturn(false); + Mockito.doThrow(new RuntimeException("push failed")) + .doNothing() + .when(retryDelegate).pushVerifiedBlock(Mockito.any()); + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + Mockito.when(mockClient.getBlock(33L)).thenReturn(blockWithNum(33L)); + delegateField.set(solidityNode, retryDelegate); + clientField.set(solidityNode, mockClient); + invokeLoopProcessBlock(blockWithNum(33L)); + assertEquals(33L, chainBaseManager.getDynamicPropertiesStore() + .getLatestSolidifiedBlockNum()); + } finally { + chainBaseManager.getDynamicPropertiesStore() + .saveLatestSolidifiedBlockNum(origSolidified); + delegateField.set(solidityNode, origDelegate); + clientField.set(solidityNode, origClient); + } } - // ── getBlock() ─────────────────────────────────────────────────────────────── + // ── getBlock() ──────────────────────────────────────────────────────────────── /** * getBlock() must fetch a block via gRPC, place it in blockQueue, then exit @@ -382,24 +460,24 @@ public void testShutdownCallsDatabaseClientShutdown() throws Exception { @Test(timeout = 5000) @SuppressWarnings("unchecked") public void testGetBlockProcessesOneBlock() throws Exception { - long origID = atomicLong("ID").get(); + long origID = atomicLong("ID").get(); long origRemote = atomicLong("remoteBlockNum").get(); atomicLong("ID").set(0L); - atomicLong("remoteBlockNum").set(2L); // blockNum=1 <= 2, no sleep needed + atomicLong("remoteBlockNum").set(2L); DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); Mockito.when(mockClient.getBlock(1L)).thenAnswer(inv -> { - setFlag(false); // stop the loop after this iteration + setFlag(false); return blockWithNum(1L); }); TronNetDelegate mockDelegate = mock(TronNetDelegate.class); Mockito.when(mockDelegate.isHitDown()).thenReturn(false); - Field clientField = getField("databaseGrpcClient"); + Field clientField = getField("databaseGrpcClient"); Field delegateField = getField("tronNetDelegate"); - Object origClient = clientField.get(solidityNode); + Object origClient = clientField.get(solidityNode); Object origDelegate = delegateField.get(solidityNode); clientField.set(solidityNode, mockClient); delegateField.set(solidityNode, mockDelegate); @@ -412,7 +490,87 @@ public void testGetBlockProcessesOneBlock() throws Exception { m.invoke(solidityNode); assertEquals(1, queue.size()); - assertEquals(1L, queue.peek().getBlockHeader().getRawData().getNumber()); + Block peeked = queue.peek(); + Assert.assertNotNull("blockQueue must contain the fetched block", peeked); + assertEquals(1L, peeked.getBlockHeader().getRawData().getNumber()); + } finally { + setFlag(true); + queue.clear(); + atomicLong("ID").set(origID); + atomicLong("remoteBlockNum").set(origRemote); + clientField.set(solidityNode, origClient); + delegateField.set(solidityNode, origDelegate); + } + } + + /** + * getBlock() shutdown paths: + * - interrupted in blockQueue.put() by shutdownNow(): must exit cleanly with + * INFO (root cause of the original "reason: null" ERROR bug), + * - exception thrown while flag is already false: must exit cleanly with INFO + * instead of logging ERROR and retrying. + */ + @Test(timeout = 8000) + @SuppressWarnings("unchecked") + public void testGetBlockShutdownPaths() throws Exception { + long origID = atomicLong("ID").get(); + long origRemote = atomicLong("remoteBlockNum").get(); + Field clientField = getField("databaseGrpcClient"); + Field delegateField = getField("tronNetDelegate"); + Object origClient = clientField.get(solidityNode); + Object origDelegate = delegateField.get(solidityNode); + + LinkedBlockingDeque queue = + (LinkedBlockingDeque) getField("blockQueue").get(solidityNode); + try { + // ── Part 1: interrupt during blockQueue.put() ────────────────────────── + // Fill the queue to capacity so the next put() call blocks. + for (int i = 0; i < 100; i++) { + queue.offer(blockWithNum(i)); + } + assertEquals(100, queue.size()); + + atomicLong("ID").set(0L); + atomicLong("remoteBlockNum").set(10L); + + DatabaseGrpcClient putClient = mock(DatabaseGrpcClient.class); + Mockito.when(putClient.getBlock(1L)).thenReturn(blockWithNum(1L)); + TronNetDelegate mockDelegate = mock(TronNetDelegate.class); + Mockito.when(mockDelegate.isHitDown()).thenReturn(false); + clientField.set(solidityNode, putClient); + delegateField.set(solidityNode, mockDelegate); + + Method getBlockM = SolidityNode.class.getDeclaredMethod("getBlock"); + getBlockM.setAccessible(true); + Thread t = new Thread(() -> { + try { + getBlockM.invoke(solidityNode); + } catch (Exception e) { + Thread.currentThread().interrupt(); + } + }); + t.start(); + Thread.sleep(200); // let the thread block inside blockQueue.put() + t.interrupt(); // simulate ExecutorService.shutdownNow() + t.join(4000); + assertFalse("getBlock must exit cleanly when interrupted during put()", t.isAlive()); + queue.clear(); + setFlag(true); + + // ── Part 2: exception while flag is false ────────────────────────────── + atomicLong("ID").set(0L); + atomicLong("remoteBlockNum").set(10L); + + DatabaseGrpcClient closingClient = mock(DatabaseGrpcClient.class); + Mockito.when(closingClient.getBlock(1L)).thenAnswer(inv -> { + setFlag(false); // shutdown races with this gRPC call + throw new RuntimeException("channel closed during shutdown"); + }); + clientField.set(solidityNode, closingClient); + delegateField.set(solidityNode, mockDelegate); + + // Must return without throwing and without infinite retry. + getBlockM.invoke(solidityNode); } finally { setFlag(true); queue.clear(); @@ -423,7 +581,7 @@ public void testGetBlockProcessesOneBlock() throws Exception { } } - // ── processSolidityBlock() ─────────────────────────────────────────────────── + // ── processSolidityBlock() ──────────────────────────────────────────────────── /** * processSolidityBlock() must drain a block from the queue, process it, and @@ -498,129 +656,6 @@ public void testProcessSolidityBlockHandlesInterrupt() throws Exception { } } - // ── loopProcessBlock() retry path ──────────────────────────────────────────── - - /** - * When pushVerifiedBlock throws, loopProcessBlock() must retry after sleeping, - * re-fetching the block via getBlockByNum, and ultimately succeed. - */ - @Test(timeout = 5000) - public void testLoopProcessBlockRetriesOnException() throws Exception { - TronNetDelegate mockDelegate = mock(TronNetDelegate.class); - Mockito.when(mockDelegate.isHitDown()).thenReturn(false); - Mockito.doThrow(new RuntimeException("push failed")) - .doNothing() - .when(mockDelegate).pushVerifiedBlock(Mockito.any()); - - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getBlock(33L)).thenReturn(blockWithNum(33L)); - - long origSolidified = chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum(); - Field delegateField = getField("tronNetDelegate"); - Field clientField = getField("databaseGrpcClient"); - Object origDelegate = delegateField.get(solidityNode); - Object origClient = clientField.get(solidityNode); - delegateField.set(solidityNode, mockDelegate); - clientField.set(solidityNode, mockClient); - try { - invokeLoopProcessBlock(blockWithNum(33L)); - assertEquals(33L, chainBaseManager.getDynamicPropertiesStore() - .getLatestSolidifiedBlockNum()); - } catch (RuntimeException e) { - Assert.assertTrue(e.getMessage().contains("push failed")); - } finally { - chainBaseManager.getDynamicPropertiesStore() - .saveLatestSolidifiedBlockNum(origSolidified); - delegateField.set(solidityNode, origDelegate); - clientField.set(solidityNode, origClient); - } - } - - // ── getBlockByNum() retry paths ────────────────────────────────────────────── - - /** - * When the returned block number does not match, getBlockByNum() must warn - * and retry; it must throw RuntimeException when flag becomes false. - */ - @Test(timeout = 5000) - public void testGetBlockByNumWarnOnWrongNum() throws Exception { - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getBlock(9L)).thenAnswer(inv -> { - setFlag(false); // cause the retry loop to exit - return blockWithNum(999L); // deliberately wrong number - }); - - Field clientField = getField("databaseGrpcClient"); - Object orig = clientField.get(solidityNode); - clientField.set(solidityNode, mockClient); - try { - Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); - m.setAccessible(true); - try { - m.invoke(solidityNode, 9L); - Assert.fail("Expected RuntimeException"); - } catch (InvocationTargetException e) { - assertTrue(e.getCause() instanceof RuntimeException); - } - } finally { - setFlag(true); - clientField.set(solidityNode, orig); - } - } - - /** - * When the gRPC call throws, getBlockByNum() must log, sleep, and retry; - * on the second attempt it must return the correct block. - */ - @Test(timeout = 5000) - public void testGetBlockByNumRetriesOnException() throws Exception { - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getBlock(8L)) - .thenThrow(new RuntimeException("rpc error")) - .thenReturn(blockWithNum(8L)); - - Field clientField = getField("databaseGrpcClient"); - Object orig = clientField.get(solidityNode); - clientField.set(solidityNode, mockClient); - try { - Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); - m.setAccessible(true); - Block result = (Block) m.invoke(solidityNode, 8L); - assertEquals(8L, result.getBlockHeader().getRawData().getNumber()); - } finally { - clientField.set(solidityNode, orig); - } - } - - // ── getLastSolidityBlockNum() retry path ───────────────────────────────────── - - /** - * When getDynamicProperties() throws, getLastSolidityBlockNum() must log, - * sleep, and retry; on the second attempt it must return the fetched value. - */ - @Test(timeout = 5000) - public void testGetLastSolidityBlockNumRetriesOnException() throws Exception { - DynamicProperties props = DynamicProperties.newBuilder() - .setLastSolidityBlockNum(50L).build(); - DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); - Mockito.when(mockClient.getDynamicProperties()) - .thenThrow(new RuntimeException("rpc error")) - .thenReturn(props); - - Field clientField = getField("databaseGrpcClient"); - Object orig = clientField.get(solidityNode); - clientField.set(solidityNode, mockClient); - try { - Method m = SolidityNode.class.getDeclaredMethod("getLastSolidityBlockNum"); - m.setAccessible(true); - long result = (long) m.invoke(solidityNode); - assertEquals(50L, result); - } finally { - clientField.set(solidityNode, orig); - } - } - // ── private helpers ────────────────────────────────────────────────────────── private static Field getField(String name) throws Exception { From c8ba11977422f38a1f4cdb0807d9a60df45a6d41 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 26 May 2026 16:51:36 +0800 Subject: [PATCH 21/57] refactor(config): merge test config files (#6788) --- docs/implement-a-customized-actuator-en.md | 4 +- docs/implement-a-customized-actuator-zh.md | 4 +- .../java/org/tron/common/TestConstants.java | 6 +- .../vm/BandWidthRuntimeOutOfTimeTest.java | 3 +- ...andWidthRuntimeOutOfTimeWithCheckTest.java | 3 +- .../runtime/vm/BandWidthRuntimeTest.java | 3 +- .../vm/BandWidthRuntimeWithCheckTest.java | 3 +- .../PrecompiledContractsVerifyProofTest.java | 3 +- .../utils/client/utils/Sha256Sm3Hash.java | 9 - .../tron/core/ShieldedTRC20BuilderTest.java | 3 +- .../org/tron/core/config/args/ArgsTest.java | 11 +- .../core/config/args/LocalWitnessTest.java | 4 +- .../tron/core/config/args/StorageTest.java | 33 +- .../tron/core/db/TransactionTraceTest.java | 3 +- .../core/db/api/AssetUpdateHelperTest.java | 3 +- .../org/tron/core/jsonrpc/ApiUtilTest.java | 3 +- .../tron/core/zksnark/LibrustzcashTest.java | 3 +- .../org/tron/core/zksnark/MerkleTreeTest.java | 3 +- .../tron/core/zksnark/NoteEncDecryTest.java | 3 +- .../tron/core/zksnark/SendCoinShieldTest.java | 3 +- .../core/zksnark/ShieldedReceiveTest.java | 6 +- framework/src/test/resources/args-test.conf | 222 -------------- ...nfig-localtest.conf => config-shield.conf} | 17 -- .../src/test/resources/config-test-index.conf | 173 ----------- .../test/resources/config-test-mainnet.conf | 241 --------------- .../resources/config-test-storagetest.conf | 286 ------------------ framework/src/test/resources/config-test.conf | 18 -- .../java/org/tron/plugins/DbLiteTest.java | 3 +- 28 files changed, 71 insertions(+), 1005 deletions(-) delete mode 100644 framework/src/test/resources/args-test.conf rename framework/src/test/resources/{config-localtest.conf => config-shield.conf} (93%) delete mode 100644 framework/src/test/resources/config-test-index.conf delete mode 100644 framework/src/test/resources/config-test-mainnet.conf delete mode 100644 framework/src/test/resources/config-test-storagetest.conf diff --git a/docs/implement-a-customized-actuator-en.md b/docs/implement-a-customized-actuator-en.md index 76e1852824c..912a49c5d63 100644 --- a/docs/implement-a-customized-actuator-en.md +++ b/docs/implement-a-customized-actuator-en.md @@ -229,7 +229,7 @@ public class SumActuatorTest { @BeforeClass public static void init() throws IOException { Args.setParam(new String[]{"--output-directory", - temporaryFolder.newFolder().toString()}, "config-localtest.conf"); + temporaryFolder.newFolder().toString()}, "config-test.conf"); context = new TronApplicationContext(DefaultConfig.class); appTest = ApplicationFactory.create(context); appTest.startup(); @@ -255,7 +255,7 @@ public class SumActuatorTest { @Test public void sumActuatorTest() { - // this key is defined in config-localtest.conf as accountName=Sun + // this key is defined in config-test.conf as accountName=Sun String key = ""; byte[] address = PublicMethed.getFinalAddress(key); ECKey ecKey = null; diff --git a/docs/implement-a-customized-actuator-zh.md b/docs/implement-a-customized-actuator-zh.md index 1128849916a..9aa0e258127 100644 --- a/docs/implement-a-customized-actuator-zh.md +++ b/docs/implement-a-customized-actuator-zh.md @@ -231,7 +231,7 @@ public class SumActuatorTest { @BeforeClass public static void init() throws IOException { Args.setParam(new String[]{"--output-directory", - temporaryFolder.newFolder().toString()}, "config-localtest.conf"); + temporaryFolder.newFolder().toString()}, "config-test.conf"); context = new TronApplicationContext(DefaultConfig.class); appTest = ApplicationFactory.create(context); appTest.startup(); @@ -257,7 +257,7 @@ public class SumActuatorTest { @Test public void sumActuatorTest() { - // this key is defined in config-localtest.conf as accountName=Sun + // this key is defined in config-test.conf as accountName=Sun String key = ""; byte[] address = PublicMethed.getFinalAddress(key); ECKey ecKey = null; diff --git a/framework/src/test/java/org/tron/common/TestConstants.java b/framework/src/test/java/org/tron/common/TestConstants.java index 88f28688936..a6bf88434ed 100644 --- a/framework/src/test/java/org/tron/common/TestConstants.java +++ b/framework/src/test/java/org/tron/common/TestConstants.java @@ -23,11 +23,7 @@ public class TestConstants { public static final String TEST_CONF = "config-test.conf"; - public static final String NET_CONF = "config.conf"; - public static final String MAINNET_CONF = "config-test-mainnet.conf"; - public static final String LOCAL_CONF = "config-localtest.conf"; - public static final String STORAGE_CONF = "config-test-storagetest.conf"; - public static final String INDEX_CONF = "config-test-index.conf"; + public static final String SHIELD_CONF = "config-shield.conf"; /** * Skips the current test on ARM64 where LevelDB JNI is unavailable. diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java index 85829e474f0..86b26c24672 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java @@ -21,6 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.runtime.TvmTestUtils; import org.tron.common.utils.Commons; @@ -73,7 +74,7 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest { "--storage-db-directory", dbDirectory, "--debug" }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java index be8fc952188..bb5fbf36d55 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java @@ -21,6 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.runtime.TvmTestUtils; import org.tron.common.utils.Commons; @@ -74,7 +75,7 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest { "--storage-db-directory", dbDirectory, "--debug" }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java index 1245f5cefd6..fb682bcb50f 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java @@ -24,6 +24,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.runtime.TvmTestUtils; @@ -69,7 +70,7 @@ public static void init() { "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java index 13c75f3e40a..a05d6603874 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java @@ -21,6 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.runtime.TvmTestUtils; import org.tron.common.utils.Commons; @@ -75,7 +76,7 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest { "--output-directory", dbPath(), "--storage-db-directory", dbDirectory, }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java b/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java index 27e7891e6d8..9ea9ab922a6 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java @@ -15,6 +15,7 @@ import org.junit.Test; import org.tron.api.GrpcAPI.ShieldedTRC20Parameters; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.utils.FileUtil; @@ -60,7 +61,7 @@ public class PrecompiledContractsVerifyProofTest extends BaseTest { @BeforeClass public static void init() { - Args.setParam(new String[]{"--output-directory", dbPath()}, "config-test.conf"); + Args.setParam(new String[]{"--output-directory", dbPath()}, TestConstants.TEST_CONF); DEFAULT_OVK = ByteArray .fromHexString("030c8c2bc59fb3eb8afb047a8ea4b028743d23e7d38c6fa30908358431e2314d"); SHIELDED_CONTRACT_ADDRESS = WalletClient.decodeFromBase58Check(SHIELDED_CONTRACT_ADDRESS_STR); diff --git a/framework/src/test/java/org/tron/common/utils/client/utils/Sha256Sm3Hash.java b/framework/src/test/java/org/tron/common/utils/client/utils/Sha256Sm3Hash.java index fde88385794..a034f9e816a 100644 --- a/framework/src/test/java/org/tron/common/utils/client/utils/Sha256Sm3Hash.java +++ b/framework/src/test/java/org/tron/common/utils/client/utils/Sha256Sm3Hash.java @@ -47,15 +47,6 @@ public class Sha256Sm3Hash implements Serializable, Comparable { private final byte[] bytes; private static boolean isEckey = true; - /* static { - Config config = Configuration.getByPath("config.conf"); // it is needs set to be a constant - Config config = "crypto.engine"; - if (config.hasPath("crypto.engine")) { - isEckey = config.getString("crypto.engine").equalsIgnoreCase("eckey"); - System.out.println("Sha256Sm3Hash getConfig isEckey: " + isEckey); - } - }*/ - public Sha256Sm3Hash(long num, byte[] hash) { byte[] rawHashBytes = this.generateBlockId(num, hash); Preconditions.checkArgument(rawHashBytes.length == LENGTH); diff --git a/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java b/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java index 0a8fbac009c..00be867fd59 100644 --- a/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java +++ b/framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java @@ -22,6 +22,7 @@ import org.tron.api.GrpcAPI.ShieldedTRC20TriggerContractParameters; import org.tron.api.GrpcAPI.SpendAuthSigParameters; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.utils.PublicMethod; @@ -63,7 +64,7 @@ public class ShieldedTRC20BuilderTest extends BaseTest { private static final byte[] PUBLIC_TO_ADDRESS; static { - Args.setParam(new String[]{"--output-directory", dbPath()}, "config-test-mainnet.conf"); + Args.setParam(new String[]{"--output-directory", dbPath()}, TestConstants.TEST_CONF); SHIELDED_CONTRACT_ADDRESS = WalletClient.decodeFromBase58Check(SHIELDED_CONTRACT_ADDRESS_STR); DEFAULT_OVK = ByteArray .fromHexString("030c8c2bc59fb3eb8afb047a8ea4b028743d23e7d38c6fa30908358431e2314d"); diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 43f21157f47..ce479e06542 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -50,8 +50,7 @@ public class ArgsTest { @Test public void get() { - Args.setParam(new String[] {"-c", TestConstants.TEST_CONF, "--keystore-factory"}, - TestConstants.NET_CONF); + Args.setParam(new String[] {"--keystore-factory"}, TestConstants.TEST_CONF); CommonParameter parameter = Args.getInstance(); @@ -73,7 +72,7 @@ public void get() { Assert.assertEquals("database", parameter.getStorage().getDbDirectory()); - Assert.assertEquals(11, parameter.getSeedNode().getAddressList().size()); + Assert.assertEquals(0, parameter.getSeedNode().getAddressList().size()); GenesisBlock genesisBlock = parameter.getGenesisBlock(); @@ -147,12 +146,6 @@ public void testIpFromLibP2p() Assert.assertNotEquals(configuredExternalIp, parameter.getNodeExternalIp()); } - @Test - public void testOldRewardOpt() { - thrown.expect(IllegalArgumentException.class); - Args.setParam(new String[] {"-c", "args-test.conf"}, TestConstants.NET_CONF); - } - @Test public void testInitService() { Map storage = new HashMap<>(); diff --git a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java index 83a65926446..1b30518c7e3 100644 --- a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java +++ b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java @@ -177,10 +177,11 @@ public void testConstructor() { public void testLocalWitnessConfig() throws IOException { Args.setParam( new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "-w", "--debug"}, - "config-localtest.conf"); + TestConstants.SHIELD_CONF); LocalWitnesses witness = Args.getLocalWitnesses(); Assert.assertNotNull(witness.getPrivateKey()); Assert.assertNotNull(witness.getWitnessAccountAddress()); + Args.clearParam(); } @Test @@ -191,5 +192,6 @@ public void testNullLocalWitnessConfig() throws IOException { LocalWitnesses witness = Args.getLocalWitnesses(); Assert.assertNull(witness.getPrivateKey()); Assert.assertNull(witness.getWitnessAccountAddress()); + Args.clearParam(); } } diff --git a/framework/src/test/java/org/tron/core/config/args/StorageTest.java b/framework/src/test/java/org/tron/core/config/args/StorageTest.java index a925b9c8fc0..3c00c6ea00e 100644 --- a/framework/src/test/java/org/tron/core/config/args/StorageTest.java +++ b/framework/src/test/java/org/tron/core/config/args/StorageTest.java @@ -15,22 +15,51 @@ package org.tron.core.config.args; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; import java.io.File; import org.iq80.leveldb.CompressionType; import org.iq80.leveldb.Options; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; +import org.tron.common.TestConstants; import org.tron.common.utils.FileUtil; import org.tron.common.utils.StorageUtils; public class StorageTest { - private static Storage storage; + private static final Storage storage; static { - Args.setParam(new String[]{}, "config-test-storagetest.conf"); + Args.setParam(new String[]{}, TestConstants.TEST_CONF); storage = Args.getInstance().getStorage(); + setupStorage(); + } + + private static void setupStorage() { + Config cfg = ConfigFactory.parseString( + "storage.default.maxOpenFiles = 50\n" + + "storage.defaultM.maxOpenFiles = 500\n" + + "storage.defaultL.maxOpenFiles = 1000\n" + + "storage.properties = [\n" + + " { name = account, path = storage_directory_test,\n" + + " createIfMissing = true, paranoidChecks = true, verifyChecksums = true,\n" + + " compressionType = 1, blockSize = 4096,\n" + + " writeBufferSize = 10485760, cacheSize = 10485760, maxOpenFiles = 100 },\n" + + " { name = \"account-index\", path = storage_directory_test,\n" + + " createIfMissing = true, paranoidChecks = true, verifyChecksums = true,\n" + + " compressionType = 1, blockSize = 4096,\n" + + " writeBufferSize = 10485760, cacheSize = 10485760, maxOpenFiles = 100 },\n" + + " { name = test_name, path = test_path,\n" + + " createIfMissing = false, paranoidChecks = false, verifyChecksums = false,\n" + + " compressionType = 1, blockSize = 2,\n" + + " writeBufferSize = 3, cacheSize = 4, maxOpenFiles = 5 }\n" + + "]" + ).withFallback(ConfigFactory.load(TestConstants.TEST_CONF)); + StorageConfig sc = StorageConfig.fromConfig(cfg); + storage.setDefaultDbOptions(sc); + storage.setPropertyMapFromBean(sc.getProperties()); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java b/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java index 162b2d793d4..5917cd06603 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionTraceTest.java @@ -22,6 +22,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.runtime.TvmTestUtils; import org.tron.common.utils.ByteArray; @@ -65,7 +66,7 @@ public class TransactionTraceTest extends BaseTest { "--storage-db-directory", dbDirectory, "--debug" }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/core/db/api/AssetUpdateHelperTest.java b/framework/src/test/java/org/tron/core/db/api/AssetUpdateHelperTest.java index d1edd92c109..be4a0b87c1a 100644 --- a/framework/src/test/java/org/tron/core/db/api/AssetUpdateHelperTest.java +++ b/framework/src/test/java/org/tron/core/db/api/AssetUpdateHelperTest.java @@ -8,6 +8,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.common.utils.Sha256Hash; import org.tron.core.capsule.AccountCapsule; @@ -27,7 +28,7 @@ public class AssetUpdateHelperTest extends BaseTest { private static boolean init; static { - Args.setParam(new String[]{"-d", dbPath()}, "config-test-index.conf"); + Args.setParam(new String[]{"-d", dbPath()}, TestConstants.TEST_CONF); Args.getInstance().setSolidityNode(true); } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java b/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java index f62d47d5367..a74ca3a69a4 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/ApiUtilTest.java @@ -8,6 +8,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.core.capsule.BlockCapsule; import org.tron.core.config.args.Args; @@ -21,7 +22,7 @@ public class ApiUtilTest { @BeforeClass public static void init() { - Args.setParam(new String[]{}, "config-localtest.conf"); + Args.setParam(new String[]{}, TestConstants.TEST_CONF); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java index 67a95e29beb..b471aeb2e42 100644 --- a/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java @@ -29,6 +29,7 @@ import org.junit.Ignore; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.zksnark.IncrementalMerkleTreeContainer; @@ -80,7 +81,7 @@ public static void init() { "--storage-db-directory", dbDirectory, "--debug" }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); Args.getInstance().setAllowShieldedTransactionApi(true); ZksnarkInitService.librustzcashInitZksnarkParams(); diff --git a/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java b/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java index 407a0641f3a..cf50dc87fa6 100644 --- a/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/MerkleTreeTest.java @@ -10,6 +10,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.zksnark.IncrementalMerkleTreeContainer; @@ -36,7 +37,7 @@ public class MerkleTreeTest extends BaseTest { "--storage-db-directory", dbDirectory, "--debug" }, - "config-test-mainnet.conf" + TestConstants.TEST_CONF ); } diff --git a/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java b/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java index 3c3fb14b2b1..e41b9f64c9a 100644 --- a/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java @@ -8,6 +8,7 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; import org.tron.core.Wallet; import org.tron.core.capsule.AssetIssueCapsule; @@ -39,7 +40,7 @@ public class NoteEncDecryTest extends BaseTest { private Wallet wallet; static { - Args.setParam(new String[]{"--output-directory", dbPath()}, "config-localtest.conf"); + Args.setParam(new String[]{"--output-directory", dbPath()}, TestConstants.SHIELD_CONF); FROM_ADDRESS = Wallet.getAddressPreFixString() + "a7d8a35b260395c14aa456297662092ba3b76fc0"; } diff --git a/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java b/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java index 8693bf0716d..08de83ca8bf 100644 --- a/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java @@ -21,6 +21,7 @@ import org.junit.Test; import org.tron.api.GrpcAPI; import org.tron.common.BaseTest; +import org.tron.common.TestConstants; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; @@ -111,7 +112,7 @@ public class SendCoinShieldTest extends BaseTest { private static boolean init; static { - Args.setParam(new String[]{"--output-directory", dbPath()}, "config-test-mainnet.conf"); + Args.setParam(new String[]{"--output-directory", dbPath()}, TestConstants.TEST_CONF); Args.getInstance().setZenTokenId(String.valueOf(tokenId)); PUBLIC_ADDRESS_ONE = Wallet.getAddressPreFixString() + "a7d8a35b260395c14aa456297662092ba3b76fc0"; diff --git a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java index 7143cef43e2..0d14d6fbc26 100755 --- a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java @@ -1,6 +1,6 @@ package org.tron.core.zksnark; -import static org.tron.common.TestConstants.LOCAL_CONF; +import static org.tron.common.TestConstants.SHIELD_CONF; import static org.tron.common.utils.PublicMethod.getHexAddressByPrivateKey; import static org.tron.common.utils.PublicMethod.getRandomPrivateKey; @@ -34,7 +34,6 @@ import org.tron.common.crypto.ECKey; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; -import org.tron.common.utils.PublicMethod; import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.client.utils.TransactionUtils; import org.tron.common.zksnark.IncrementalMerkleTreeContainer; @@ -148,8 +147,7 @@ public class ShieldedReceiveTest extends BaseTest { private static boolean init; static { - Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, - LOCAL_CONF); + Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, SHIELD_CONF); ADDRESS_ONE_PRIVATE_KEY = getRandomPrivateKey(); FROM_ADDRESS = getHexAddressByPrivateKey(ADDRESS_ONE_PRIVATE_KEY); } diff --git a/framework/src/test/resources/args-test.conf b/framework/src/test/resources/args-test.conf deleted file mode 100644 index a65b44b0dff..00000000000 --- a/framework/src/test/resources/args-test.conf +++ /dev/null @@ -1,222 +0,0 @@ -net { - # type is deprecated and has no effect. - # type = mainnet -} - - -storage { - # Directory for storing persistent data - - db.engine = "LEVELDB" - db.directory = "database", - - # You can custom these 14 databases' configs: - - # account, account-index, asset-issue, block, block-index, - # block_KDB, peers, properties, recent-block, trans, - # utxo, votes, witness, witness_schedule. - - # Otherwise, db configs will remain defualt and data will be stored in - # the path of "output-directory" or which is set by "-d" ("--output-directory"). - - # Attention: name is a required field that must be set !!! - default = { - maxOpenFiles = 50 - } - defaultM = { - maxOpenFiles = 500 - } - defaultL = { - maxOpenFiles = 1000 - } - properties = [ - { - name = "account", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - { - name = "account-index", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - { # only for unit test - name = "test_name", - path = "test_path", - createIfMissing = false, - paranoidChecks = false, - verifyChecksums = false, - compressionType = 1, - blockSize = 2, - writeBufferSize = 3, - cacheSize = 4, - maxOpenFiles = 5 - }, - ] - - needToUpdateAsset = false - -} - -node.discovery = { - enable = true - persist = true - external.ip = "46.168.1.1" -} - -node { - - trustNode = "127.0.0.1:50051" - - listen.port = 18888 - - active = [] - - maxConnections = 30 - minConnections = 8 - minActiveConnections = 3 - inactiveThreshold = 600 //seconds - - p2p { - version = 43 # 43: testnet; 101: debug - } - - rpc { - port = 50051 - } - -} - -sync { - node.count = 30 -} - -seed.node = { - ip.list = [ - ] -} - -genesis.block = { - # Reserve balance - assets = [ - { - accountName = "Devaccount" - accountType = "AssetIssue" - address = "27d3byPxZXKQWfXX7sJvemJJuv5M65F3vjS" - balance = "10000000000000000" - }, - { - accountName = "Zion" - accountType = "AssetIssue" - address = "27fXgQ46DcjEsZ444tjZPKULcxiUfDrDjqj" - balance = "15000000000000000" - }, - { - accountName = "Sun" - accountType = "AssetIssue" - address = "27SWXcHuQgFf9uv49FknBBBYBaH3DUk4JPx" - balance = "10000000000000000" - }, - { - accountName = "Blackhole" - accountType = "AssetIssue" - address = "27WtBq2KoSy5v8VnVZBZHHJcDuWNiSgjbE3" - balance = "-9223372036854775808" - } - ] - - witnesses = [ - { - address: 27Ssb1WE8FArwJVRRb8Dwy3ssVGuLY8L3S1 - url = "http://Mercury.org", - voteCount = 105 - }, - { - address: 27anh4TDZJGYpsn4BjXzb7uEArNALxwiZZW - url = "http://Venus.org", - voteCount = 104 - }, - { - address: 27Wkfa5iEJtsKAKdDzSmF1b2gDm5s49kvdZ - url = "http://Earth.org", - voteCount = 103 - }, - { - address: 27bqKYX9Bgv7dgTY7xBw5SUHZ8EGaPSikjx - url = "http://Mars.org", - voteCount = 102 - }, - { - address: 27fASUY6qKtsaAEPz6QxhZac2KYVz2ZRTXW - url = "http://Jupiter.org", - voteCount = 101 - }, - { - address: 27Q3RSbiqm59VXcF8shQWHKbyztfso5FwvP - url = "http://Saturn.org", - voteCount = 100 - }, - { - address: 27YkUVSuvCK3K84DbnFnxYUxozpi793PTqZ - url = "http://Uranus.org", - voteCount = 99 - }, - { - address: 27kdTBTDJ16hK3Xqr8PpCuQJmje1b94CDJU - url = "http://Neptune.org", - voteCount = 98 - }, - { - address: 27mw9UpRy7inTMQ5kUzsdTc2QZ6KvtCX4uB - url = "http://Pluto.org", - voteCount = 97 - }, - { - address: 27QzC4PeQZJ2kFMUXiCo4S8dx3VWN5U9xcg - url = "http://Altair.org", - voteCount = 96 - }, - { - address: 27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh - url = "http://AlphaLyrae.org", - voteCount = 95 - } - ] - - timestamp = "0" #2017-8-26 12:00:00 - - parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" -} - - -localwitness = [ - -] - -block = { - needSyncCheck = true # first node : false, other : true -} - -vm = { - supportConstant = true - minTimeRatio = 0.0 - maxTimeRatio = 5.0 -} -committee = { - allowCreationOfContracts = 1 //mainnet:0 (reset by committee),test:1 - allowOldRewardOpt = 1 -} diff --git a/framework/src/test/resources/config-localtest.conf b/framework/src/test/resources/config-shield.conf similarity index 93% rename from framework/src/test/resources/config-localtest.conf rename to framework/src/test/resources/config-shield.conf index 26b273efa89..aad1ba8452f 100644 --- a/framework/src/test/resources/config-localtest.conf +++ b/framework/src/test/resources/config-shield.conf @@ -162,18 +162,7 @@ node { seed.node = { - # List of the seed nodes - # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] ip.list = [ - "127.0.0.1:6666", - // "127.0.0.1:7777", - // "127.0.0.1:8888", - // "127.0.0.1:9999", ] } @@ -274,9 +263,3 @@ committee = { allowTvmConstantinople = 1 allowTvmSolidity059 = 1 } - -log.level = { - root = "INFO" // TRACE;DEBUG;INFO;WARN;ERROR - allowCreationOfContracts = 1 //mainnet:0 (reset by committee),test:1 - allowMultiSign = 1 //mainnet:0 (reset by committee),test:1 -} diff --git a/framework/src/test/resources/config-test-index.conf b/framework/src/test/resources/config-test-index.conf deleted file mode 100644 index 8dbff89219c..00000000000 --- a/framework/src/test/resources/config-test-index.conf +++ /dev/null @@ -1,173 +0,0 @@ -net { - # type is deprecated and has no effect. - # type = mainnet -} - - -storage { - # Directory for storing persistent data - - db.directory = "database", - - # You can custom these 14 databases' configs: - - # account, account-index, asset-issue, block, block-index, - # block_KDB, peers, properties, recent-block, trans, - # utxo, votes, witness, witness_schedule. - - # Otherwise, db configs will remain defualt and data will be stored in - # the path of "output-directory" or which is set by "-d" ("--output-directory"). - - # Attention: name is a required field that must be set !!! - properties = [ - { - name = "account", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - { - name = "account-index", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - ] - - needToUpdateAsset = false - -} - -node.discovery = { - enable = true - persist = true - external.ip = "" -} - -node { - listen.port = 18888 - - active = [ - # Sample entries: - # { url = "enode://@hostname.com:30303" } - # { - # ip = hostname.com - # port = 30303 - # nodeId = e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c - # } - ] - - p2p { - version = 43 # 43: testnet; 101: debug - } - - http { - fullNodeEnable = false - solidityEnable = false - PBFTEnable = false - } - - jsonrpc { - httpFullNodeEnable = false - httpSolidityEnable = false - httpPBFTEnable = false - # maxBlockRange = 5000 - # maxSubTopics = 1000 - # maxBlockFilterNum = 30000 - } - - rpc { - port = 50051 - enable = false - solidityEnable = false - PBFTEnable = false - # Number of gRPC thread, default availableProcessors / 2 - # thread = 16 - - # The maximum number of concurrent calls permitted for each incoming connection - # maxConcurrentCallsPerConnection = - - # The HTTP/2 flow control window, default 1MB - # flowControlWindow = - - # Connection being idle for longer than which will be gracefully terminated - maxConnectionIdleInMillis = 60000 - - # Connection lasting longer than which will be gracefully terminated - # maxConnectionAgeInMillis = - - # The maximum message size allowed to be received on the server, default 4MB - # maxMessageSize = - - # The maximum size of header list allowed to be received, default 8192 - # maxHeaderListSize = - } - -} - -sync { - node.count = 30 -} - -seed.node = { - # List of the seed nodes - # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] - ip.list = [ - "47.254.16.55:18888", - "47.254.18.49:18888", - "18.188.111.53:18888", - "54.219.41.56:18888", - "35.169.113.187:18888", - "34.214.241.188:18888", - "47.254.146.147:18888", - "47.254.144.25:18888", - "47.91.246.252:18888", - "47.91.216.69:18888", - "39.106.220.120:18888" - ] -} - -genesis.block = { - # Reserve balance - assets = [ - { - accountName = "Blackhole" - accountType = "AssetIssue" - address = "THmtHi1Rzq4gSKYGEKv1DPkV7au6xU1AUB" - balance = "-9223372036854775808" - } - ] - - witnesses = [ - - ] - - timestamp = "0" #2017-8-26 12:00:00 - - parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" -} - -localwitness = [ - -] - -block = { - needSyncCheck = true # first node : false, other : true -} diff --git a/framework/src/test/resources/config-test-mainnet.conf b/framework/src/test/resources/config-test-mainnet.conf deleted file mode 100644 index ec2c0884cac..00000000000 --- a/framework/src/test/resources/config-test-mainnet.conf +++ /dev/null @@ -1,241 +0,0 @@ -net { - # type is deprecated and has no effect. - # type = mainnet -} - - -storage { - # Directory for storing persistent data - - db.directory = "database", - - # You can custom these 14 databases' configs: - - # account, account-index, asset-issue, block, block-index, - # block_KDB, peers, properties, recent-block, trans, - # utxo, votes, witness, witness_schedule. - - # Otherwise, db configs will remain defualt and data will be stored in - # the path of "output-directory" or which is set by "-d" ("--output-directory"). - - # Attention: name is a required field that must be set !!! - properties = [ - // { - // name = "account", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - // { - // name = "account-index", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - ] - - needToUpdateAsset = false -} - -node.discovery = { - enable = true - persist = true - external.ip = "46.168.1.1" -} - -node { - - trustNode = "127.0.0.1:50051" - - listen.port = 18888 - - active = [ - # Sample entries: - # { url = "enode://@hostname.com:30303" } - # { - # ip = hostname.com - # port = 30303 - # nodeId = e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c - # } - ] - - maxConnections = 30 - minConnections = 8 - minActiveConnections = 3 - - p2p { - version = 43 # 43: testnet; 101: debug - } - - http { - fullNodeEnable = false - solidityEnable = false - PBFTEnable = false - } - - jsonrpc { - httpFullNodeEnable = false - httpSolidityEnable = false - httpPBFTEnable = false - # maxBlockRange = 5000 - # maxSubTopics = 1000 - # maxBlockFilterNum = 50000 - # maxLogFilterNum = 20000 - } - - rpc { - enable = false - solidityEnable = false - PBFTEnable = false - } - -} - -sync { - node.count = 30 -} - -seed.node = { - # List of the seed nodes - # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] - ip.list = [ - "47.254.16.55:18888", - "47.254.18.49:18888", - "18.188.111.53:18888", - "54.219.41.56:18888", - "35.169.113.187:18888", - "34.214.241.188:18888", - "47.254.146.147:18888", - "47.254.144.25:18888", - "47.91.246.252:18888", - "47.91.216.69:18888", - "39.106.220.120:18888" - ] -} - -genesis.block = { - # Reserve balance - assets = [ - # { - # accountName = "tron" - # accountType = "AssetIssue" # Normal/AssetIssue/Contract - # address = "TFveVqgQKAdFa12DNnXTw7GHCDQK7fUVen" - # balance = "10" - # } - { - accountName = "Devaccount" - accountType = "AssetIssue" - address = "TPwJS5eC5BPGyMGtYTHNhPTB89sUWjDSSu" - balance = "10000000000000000" - }, - { - accountName = "Zion" - accountType = "AssetIssue" - address = "TSRNrjmrAbDdrsoqZsv7FZUtAo13fwoCzv" - balance = "15000000000000000" - }, - { - accountName = "Sun" - accountType = "AssetIssue" - address = "TDQE4yb3E7dvDjouvu8u7GgSnMZbxAEumV" - balance = "10000000000000000" - }, - { - accountName = "Blackhole" - accountType = "AssetIssue" - address = "THmtHi1Rzq4gSKYGEKv1DPkV7au6xU1AUB" - balance = "-9223372036854775808" - } - ] - - witnesses = [ - { - address: TDmHUBuko2qhcKBCGGafu928hMRj1tX2RW - url = "http://Mercury.org", - voteCount = 105 - }, - { - address: TMgPX8uBr8XbBboxQgMK3zNS4SgjUa3eiP - url = "http://Venus.org", - voteCount = 104 - }, - { - address: THeN2mPrrkr5U9Nzfb7xwgAwRqcFWcL7pR - url = "http://Earth.org", - voteCount = 103 - }, - { - address: TNj21CppEn6PzHHtdLHoNZRpLJnxogNnAX - url = "http://Mars.org", - voteCount = 102 - }, - { - address: TS48wDnTskrLU49kmZKRVfkHXd2NQ3dZP4 - url = "http://Jupiter.org", - voteCount = 101 - }, - { - address: TAw7uHQUJw8FqRzuYqmEDQkFCyCGE4JcsW - url = "http://Saturn.org", - voteCount = 100 - }, - { - address: TKeAx8bYkB25RsyNTQ9gUa75CuEVfFbF6N - url = "http://Uranus.org", - voteCount = 99 - }, - { - address: TXX9e8tvYxg5MMbcoYAvqVT2wiXyacjs65 - url = "http://Neptune.org", - voteCount = 98 - }, - { - address: TYpqwW7bfamDfDqXA9EMPhAfmArKMicxp9 - url = "http://Pluto.org", - voteCount = 97 - }, - { - address: TBstX5L37A1WZBEJPM9nNDnDFa2kcTVSmc - url = "http://Altair.org", - voteCount = 96 - }, - { - address: TGSzEq4t7oMTRcn1VxDghRu5r5bWAE5D1W - url = "http://AlphaLyrae.org", - voteCount = 95 - } - ] - - timestamp = "0" #2017-8-26 12:00:00 - - parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" -} - -localwitness = [ - -] - -block = { - needSyncCheck = true # first node : false, other : true -} - -committee = { - allowCreationOfContracts = 1 //mainnet:0 (reset by committee),test:1 -} diff --git a/framework/src/test/resources/config-test-storagetest.conf b/framework/src/test/resources/config-test-storagetest.conf deleted file mode 100644 index f0f993a2fb7..00000000000 --- a/framework/src/test/resources/config-test-storagetest.conf +++ /dev/null @@ -1,286 +0,0 @@ -net { - # type is deprecated and has no effect. - # type = mainnet -} - - -storage { - # Directory for storing persistent data - - db.engine = "LEVELDB" - db.directory = "database", - - # You can custom these 14 databases' configs: - - # account, account-index, asset-issue, block, block-index, - # block_KDB, peers, properties, recent-block, trans, - # utxo, votes, witness, witness_schedule. - - # Otherwise, db configs will remain defualt and data will be stored in - # the path of "output-directory" or which is set by "-d" ("--output-directory"). - - # Attention: name is a required field that must be set !!! - default = { - maxOpenFiles = 50 - } - defaultM = { - maxOpenFiles = 500 - } - defaultL = { - maxOpenFiles = 1000 - } - properties = [ - { - name = "account", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - { - name = "account-index", - path = "storage_directory_test", - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // compressed with snappy - blockSize = 4096, // 4 KB = 4 * 1024 B - writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - maxOpenFiles = 100 - }, - { # only for unit test - name = "test_name", - path = "test_path", - createIfMissing = false, - paranoidChecks = false, - verifyChecksums = false, - compressionType = 1, - blockSize = 2, - writeBufferSize = 3, - cacheSize = 4, - maxOpenFiles = 5 - }, - ] - - needToUpdateAsset = false - -} - -node.discovery = { - enable = true - persist = true - external.ip = "46.168.1.1" -} - -node { - - trustNode = "127.0.0.1:50051" - - listen.port = 18888 - - active = [ - # Sample entries: - # { url = "enode://@hostname.com:30303" } - # { - # ip = hostname.com - # port = 30303 - # nodeId = e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c - # } - ] - - maxConnections = 30 - minConnections = 8 - minActiveConnections = 3 - - p2p { - version = 43 # 43: testnet; 101: debug - } - - rpc { - port = 50051 - - # Number of gRPC thread, default availableProcessors / 2 - # thread = 16 - - # The maximum number of concurrent calls permitted for each incoming connection - # maxConcurrentCallsPerConnection = - - # The HTTP/2 flow control window, default 1MB - # flowControlWindow = - - # Connection being idle for longer than which will be gracefully terminated - maxConnectionIdleInMillis = 60000 - - # Connection lasting longer than which will be gracefully terminated - # maxConnectionAgeInMillis = - - # The maximum message size allowed to be received on the server, default 4MB - # maxMessageSize = - - # The maximum size of header list allowed to be received, default 8192 - # maxHeaderListSize = - } - -} - -sync { - node.count = 30 -} - -seed.node = { - # List of the seed nodes - # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] - ip.list = [ - "47.254.16.55:18888", - "47.254.18.49:18888", - "18.188.111.53:18888", - "54.219.41.56:18888", - "35.169.113.187:18888", - "34.214.241.188:18888", - "47.254.146.147:18888", - "47.254.144.25:18888", - "47.91.246.252:18888", - "47.91.216.69:18888", - "39.106.220.120:18888" - ] -} - -genesis.block = { - # Reserve balance - assets = [ - # { - # accountName = "tron" - # accountType = "AssetIssue" # Normal/AssetIssue/Contract - # address = "TFveVqgQKAdFa12DNnXTw7GHCDQK7fUVen" - # balance = "10" - # } - { - accountName = "Devaccount" - accountType = "AssetIssue" - address = "TPwJS5eC5BPGyMGtYTHNhPTB89sUWjDSSu" - balance = "10000000000000000" - }, - { - accountName = "Zion" - accountType = "AssetIssue" - address = "TSRNrjmrAbDdrsoqZsv7FZUtAo13fwoCzv" - balance = "15000000000000000" - }, - { - accountName = "Sun" - accountType = "AssetIssue" - address = "TDQE4yb3E7dvDjouvu8u7GgSnMZbxAEumV" - balance = "10000000000000000" - }, - { - accountName = "Blackhole" - accountType = "AssetIssue" - address = "THmtHi1Rzq4gSKYGEKv1DPkV7au6xU1AUB" - balance = "-9223372036854775808" - } - ] - - witnesses = [ - { - address: TDmHUBuko2qhcKBCGGafu928hMRj1tX2RW - url = "http://Mercury.org", - voteCount = 105 - }, - { - address: TMgPX8uBr8XbBboxQgMK3zNS4SgjUa3eiP - url = "http://Venus.org", - voteCount = 104 - }, - { - address: THeN2mPrrkr5U9Nzfb7xwgAwRqcFWcL7pR - url = "http://Earth.org", - voteCount = 103 - }, - { - address: TNj21CppEn6PzHHtdLHoNZRpLJnxogNnAX - url = "http://Mars.org", - voteCount = 102 - }, - { - address: TS48wDnTskrLU49kmZKRVfkHXd2NQ3dZP4 - url = "http://Jupiter.org", - voteCount = 101 - }, - { - address: TAw7uHQUJw8FqRzuYqmEDQkFCyCGE4JcsW - url = "http://Saturn.org", - voteCount = 100 - }, - { - address: TKeAx8bYkB25RsyNTQ9gUa75CuEVfFbF6N - url = "http://Uranus.org", - voteCount = 99 - }, - { - address: TXX9e8tvYxg5MMbcoYAvqVT2wiXyacjs65 - url = "http://Neptune.org", - voteCount = 98 - }, - { - address: TYpqwW7bfamDfDqXA9EMPhAfmArKMicxp9 - url = "http://Pluto.org", - voteCount = 97 - }, - { - address: TBstX5L37A1WZBEJPM9nNDnDFa2kcTVSmc - url = "http://Altair.org", - voteCount = 96 - }, - { - address: TGSzEq4t7oMTRcn1VxDghRu5r5bWAE5D1W - url = "http://AlphaLyrae.org", - voteCount = 95 - } - ] - - timestamp = "0" #2017-8-26 12:00:00 - - parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" -} - - -// Optional.The default is empty. -// It is used when the witness account has set the witnessPermission. -// When it is not empty, the localWitnessAccountAddress represents the address of the witness account, -// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. -// When it is empty,the localwitness is configured with the private key of the witness account. - -//localWitnessAccountAddress = - -localwitness = [ - -] - -block = { - needSyncCheck = true # first node : false, other : true -} - -vm = { - supportConstant = true - minTimeRatio = 0.0 - maxTimeRatio = 5.0 - - # In rare cases, transactions that will be within the specified maximum execution time (default 10(ms)) are re-executed and packaged - # longRunningTime = 10 -} -committee = { - allowCreationOfContracts = 1 //mainnet:0 (reset by committee),test:1 - allowOldRewardOpt = 1 - allowNewRewardAlgorithm = 1 -} diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index fbe4850db01..bb83449272b 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -219,25 +219,7 @@ sync { } seed.node = { - # List of the seed nodes - # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] ip.list = [ - "47.254.16.55:18888", - "47.254.18.49:18888", - "18.188.111.53:18888", - "54.219.41.56:18888", - "35.169.113.187:18888", - "34.214.241.188:18888", - "47.254.146.147:18888", - "47.254.144.25:18888", - "47.91.246.252:18888", - "47.91.216.69:18888", - "39.106.220.120:18888" ] } diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java index 960c1414769..4ee7567ec28 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java @@ -15,6 +15,7 @@ import org.junit.Rule; import org.junit.rules.TemporaryFolder; import org.tron.api.WalletGrpc; +import org.tron.common.TestConstants; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; @@ -74,7 +75,7 @@ public void init(String dbType, boolean historyBalanceLookup) throws IOException dbPath = folder.newFolder().toString(); Args.setParam(new String[] { "-d", dbPath, "-w", "--p2p-disable", "true", "--storage-db-engine", dbType}, - "config-localtest.conf"); + TestConstants.SHIELD_CONF); // allow account root Args.getInstance().setAllowAccountStateRoot(1); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); From 97bffb326db439af431810ddc34753a23fc5381a Mon Sep 17 00:00:00 2001 From: Federico2014 Date: Wed, 27 May 2026 14:52:50 +0800 Subject: [PATCH 22/57] fix(crypto): bind burn cipher nonce to nullifier (#6775) --- .../src/main/java/org/tron/core/Wallet.java | 97 +++++- .../zen/ShieldedTRC20ParametersBuilder.java | 61 +++- .../tron/core/zen/note/NoteEncryption.java | 132 +++++++- .../PrecompiledContractsVerifyProofTest.java | 118 +++++++ .../java/org/tron/core/WalletMockTest.java | 74 +++- .../tron/core/zen/note/BurnCipherTest.java | 300 +++++++++++++++++ .../tron/core/zksnark/NoteEncDecryTest.java | 318 ++++++++++++++++++ 7 files changed, 1060 insertions(+), 40 deletions(-) create mode 100644 framework/src/test/java/org/tron/core/zen/note/BurnCipherTest.java diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 72b8d7090d9..b705b26edc2 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -270,6 +270,8 @@ public class Wallet { "BurnNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])")); private static final byte[] SHIELDED_TRC20_LOG_TOPICS_BURN_TOKEN = Hash.sha3(ByteArray .fromString("TokenBurn(address,uint256,bytes32[3])")); + private static final byte[] SHIELDED_TRC20_LOG_TOPICS_NOTE_SPENT = Hash.sha3(ByteArray + .fromString("NoteSpent(bytes32)")); private static final String BROADCAST_TRANS_FAILED = "Broadcast transaction {} failed, {}."; @Getter @@ -3682,9 +3684,7 @@ public ShieldedTRC20Parameters createShieldedContractParameters( builder.setTransparentToAddress(transparentToAddressTvm); builder.setTransparentToAmount(toAmount); - Optional cipher = NoteEncryption.Encryption - .encryptBurnMessageByOvk(ovk, toAmount, transparentToAddress); - cipher.ifPresent(builder::setBurnCiphertext); + builder.setOvk(ovk); ExpandedSpendingKey expsk = new ExpandedSpendingKey(ask, nsk, ovk); GrpcAPI.SpendNoteTRC20 spendNote = shieldedSpends.get(0); @@ -3809,9 +3809,7 @@ public ShieldedTRC20Parameters createShieldedContractParametersWithoutAsk( System.arraycopy(transparentToAddress, 1, transparentToAddressTvm, 0, 20); builder.setTransparentToAddress(transparentToAddressTvm); builder.setTransparentToAmount(toAmount); - Optional cipher = NoteEncryption.Encryption - .encryptBurnMessageByOvk(ovk, toAmount, transparentToAddress); - cipher.ifPresent(builder::setBurnCiphertext); + builder.setOvk(ovk); GrpcAPI.SpendNoteTRC20 spendNote = shieldedSpends.get(0); buildShieldedTRC20InputWithAK(builder, spendNote, ak, nsk); if (receiveSize == 1) { @@ -3848,6 +3846,8 @@ private int getShieldedTRC20LogType(TransactionInfo.Log log, byte[] contractAddr return 3; } else if (Arrays.equals(topicsBytes, SHIELDED_TRC20_LOG_TOPICS_BURN_TOKEN)) { return 4; + } else if (Arrays.equals(topicsBytes, SHIELDED_TRC20_LOG_TOPICS_NOTE_SPENT)) { + return 5; } } return 0; @@ -3919,7 +3919,9 @@ private DecryptNotesTRC20 queryTRC20NoteByIvk(long startNum, long endNum, int index = 0; for (TransactionInfo.Log log : logList) { int logType = getShieldedTRC20LogType(log, shieldedTRC20ContractAddress); - if (logType > 0) { + // Only note-producing log types (1..3) advance the note index; + // TokenBurn (4) and NoteSpent (5) do not emit a leaf. + if (logType > 0 && logType < 4) { noteBuilder = DecryptNotesTRC20.NoteTx.newBuilder(); noteBuilder.setTxid(ByteString.copyFrom(txId)); noteBuilder.setIndex(index); @@ -4011,7 +4013,8 @@ public DecryptNotesTRC20 scanShieldedTRC20NotesByIvk( private Optional getNoteTxFromLogListByOvk( DecryptNotesTRC20.NoteTx.Builder builder, - TransactionInfo.Log log, byte[] ovk, int logType) throws ZksnarkException { + TransactionInfo.Log log, byte[] ovk, int logType, byte[] pendingNf) + throws ZksnarkException { byte[] logData = log.getData().toByteArray(); if (!ArrayUtils.isEmpty(logData)) { if (logType > 0 && logType < 4) { @@ -4050,18 +4053,36 @@ private Optional getNoteTxFromLogListByOvk( } } } else if (logType == 4) { - //Data = toAddress(32) + value(32) + ciphertext(80) + padding(16) + // Data = toAddress(32) + value(32) + cipher(80) + nonce(12) + reserved/version(4) + if (logData.length < 64 + NoteEncryption.Encryption.BURN_CIPHER_RECORD_SIZE) { + return Optional.empty(); + } byte[] logToAddress = ByteArray.subArray(logData, 12, 32); byte[] logAmountArray = ByteArray.subArray(logData, 32, 64); byte[] cipher = ByteArray.subArray(logData, 64, 144); + byte[] nonceFromLog = ByteArray.subArray(logData, 144, + 144 + NoteEncryption.Encryption.BURN_NONCE_LEN); + byte[] reservedFromLog = ByteArray.subArray(logData, + 144 + NoteEncryption.Encryption.BURN_NONCE_LEN, + 144 + NoteEncryption.Encryption.BURN_NONCE_LEN + + NoteEncryption.Encryption.BURN_RESERVED_LEN); BigInteger logAmount = ByteUtil.bytesToBigInteger(logAmountArray); byte[] plaintext; byte[] amountArray = new byte[32]; byte[] decryptedAddress = new byte[20]; + + byte[] addr21FromLog = new byte[21]; + addr21FromLog[0] = Wallet.getAddressPreFixByte(); + System.arraycopy(logToAddress, 0, addr21FromLog, 1, 20); Optional decryptedText = NoteEncryption.Encryption - .decryptBurnMessageByOvk(ovk, cipher); + .decryptBurnMessageByOvk(ovk, cipher, nonceFromLog, reservedFromLog, pendingNf, + logAmountArray, addr21FromLog); + if (decryptedText.isPresent()) { plaintext = decryptedText.get(); + if (plaintext[32] != Wallet.getAddressPreFixByte()) { + return Optional.empty(); + } System.arraycopy(plaintext, 0, amountArray, 0, 32); System.arraycopy(plaintext, 33, decryptedAddress, 0, 20); BigInteger decryptedAmount = ByteUtil.bytesToBigInteger(amountArray); @@ -4101,15 +4122,24 @@ public DecryptNotesTRC20 scanShieldedTRC20NotesByOvk(long startNum, long endNum, if (!Objects.isNull(logList)) { Optional noteTx; int index = 0; + byte[] pendingNf = null; for (TransactionInfo.Log log : logList) { int logType = getShieldedTRC20LogType(log, shieldedTRC20ContractAddress); - if (logType > 0) { + if (logType == 5) { + byte[] logData = log.getData().toByteArray(); + if (logData.length >= 32) { + pendingNf = ByteArray.subArray(logData, 0, 32); + } + } else if (logType > 0) { noteBuilder = DecryptNotesTRC20.NoteTx.newBuilder(); noteBuilder.setTxid(ByteString.copyFrom(txid)); noteBuilder.setIndex(index); index += 1; - noteTx = getNoteTxFromLogListByOvk(noteBuilder, log, ovk, logType); + noteTx = getNoteTxFromLogListByOvk(noteBuilder, log, ovk, logType, pendingNf); noteTx.ifPresent(builder::addNoteTxs); + if (logType == 4) { + pendingNf = null; + } } } } @@ -4293,12 +4323,49 @@ public BytesMessage getTriggerInputForShieldedTRC20Contract( parameterType); if (parametersBuilder.getShieldedTRC20ParametersType() == ShieldedTRC20ParametersType.BURN) { byte[] burnCiper = ByteArray.fromHexString(shieldedTRC20Parameters.getTriggerContractInput()); - if (!ArrayUtils.isEmpty(burnCiper) && burnCiper.length == 80) { - parametersBuilder.setBurnCiphertext(burnCiper); - } else { + if (ArrayUtils.isEmpty(burnCiper) + || burnCiper.length != NoteEncryption.Encryption.BURN_CIPHER_RECORD_SIZE) { + if (!ArrayUtils.isEmpty(burnCiper) && burnCiper.length == 80) { + throw new ZksnarkException( + "legacy 80-byte burn cipher is deprecated and rejected; expected " + + NoteEncryption.Encryption.BURN_CIPHER_RECORD_SIZE + "-byte burn record"); + } throw new ZksnarkException( "invalid shielded TRC-20 contract parameters for burn trigger input"); } + // v2-only: length alone would accept a legacy all-zero suffix and bypass + // the nf-bound nonce. Require reserved==v2 marker and nonce==derive(nf). + byte[] reserved = Arrays.copyOfRange(burnCiper, + NoteEncryption.Encryption.BURN_RESERVED_OFFSET, + NoteEncryption.Encryption.BURN_RESERVED_OFFSET + + NoteEncryption.Encryption.BURN_RESERVED_LEN); + if (!Arrays.equals(reserved, NoteEncryption.Encryption.getBurnRecordV2Marker())) { + throw new ZksnarkException( + "burn trigger input must be v2 (reserved=0x00000001); legacy/unknown markers rejected"); + } + if (shieldedTRC20Parameters.getSpendDescriptionList().size() != 1) { + throw new ZksnarkException( + "burn trigger input requires exactly one spendDescription for nf-bound nonce"); + } + byte[] nf = shieldedTRC20Parameters.getSpendDescription(0).getNullifier().toByteArray(); + if (nf.length != 32) { + throw new ZksnarkException( + "burn trigger input requires 32-byte spendDescription.nullifier"); + } + byte[] nonceFromInput = Arrays.copyOfRange(burnCiper, + NoteEncryption.Encryption.BURN_NONCE_OFFSET, + NoteEncryption.Encryption.BURN_NONCE_OFFSET + + NoteEncryption.Encryption.BURN_NONCE_LEN); + byte[] amount32 = ByteUtil.bigIntegerToBytes(value, 32); + byte[] addr21 = new byte[21]; + addr21[0] = Wallet.getAddressPreFixByte(); + System.arraycopy(transparentToAddressTvm, 0, addr21, 1, 20); + byte[] expectedNonce = NoteEncryption.Encryption.deriveBurnNonce(nf, amount32, addr21); + if (!Arrays.equals(nonceFromInput, expectedNonce)) { + throw new ZksnarkException( + "burn trigger input nonce does not match nonce bound to (nf, amount, addr)"); + } + parametersBuilder.setBurnCiphertext(burnCiper); } String input = parametersBuilder .getTriggerContractInput(shieldedTRC20Parameters, spendAuthoritySignature, value, false, diff --git a/framework/src/main/java/org/tron/core/zen/ShieldedTRC20ParametersBuilder.java b/framework/src/main/java/org/tron/core/zen/ShieldedTRC20ParametersBuilder.java index 4b980c7b7c9..4ee4f75a171 100644 --- a/framework/src/main/java/org/tron/core/zen/ShieldedTRC20ParametersBuilder.java +++ b/framework/src/main/java/org/tron/core/zen/ShieldedTRC20ParametersBuilder.java @@ -30,6 +30,7 @@ import org.tron.core.zen.address.PaymentAddress; import org.tron.core.zen.note.Note; import org.tron.core.zen.note.NoteEncryption; +import org.tron.core.zen.note.NoteEncryption.Encryption; import org.tron.core.zen.note.OutgoingPlaintext; import org.tron.protos.contract.ShieldContract; import org.tron.protos.contract.ShieldContract.ReceiveDescription; @@ -61,7 +62,17 @@ public class ShieldedTRC20ParametersBuilder { @Setter private BigInteger transparentToAmount; @Setter - private byte[] burnCiphertext = new byte[80]; + private byte[] ovk; + private byte[] burnCiphertext = new byte[Encryption.BURN_CIPHER_RECORD_SIZE]; + + public void setBurnCiphertext(byte[] burnCiphertext) { + if (burnCiphertext == null + || burnCiphertext.length != Encryption.BURN_CIPHER_RECORD_SIZE) { + throw new IllegalArgumentException( + "burnCiphertext must be " + Encryption.BURN_CIPHER_RECORD_SIZE + " bytes"); + } + this.burnCiphertext = burnCiphertext.clone(); + } public ShieldedTRC20ParametersBuilder() { @@ -207,6 +218,9 @@ private ReceiveDescriptionCapsule generateOutputProof(ReceiveDescriptionInfo out private void createSpendAuth(byte[] dataToBeSigned) throws ZksnarkException { for (int i = 0; i < spends.size(); i++) { + if (spends.get(i).expsk == null) { + throw new ZksnarkException("missing expanded spending key for spend authorization"); + } byte[] result = new byte[64]; JLibrustzcash.librustzcashSaplingSpendSig( new LibrustzcashParam.SpendSigParams(spends.get(i).expsk.getAsk(), @@ -292,6 +306,25 @@ public ShieldedTRC20Parameters build(boolean withAsk) throws ZksnarkException { SpendDescriptionInfo spend = spends.get(0); spendDescription = generateSpendProof(spend, ctx).getInstance(); builder.addSpendDescription(spendDescription); + + if (ovk == null && spend.expsk != null) { + ovk = spend.expsk.getOvk(); + } + if (ovk == null) { + throw new ZksnarkException("missing ovk for burn encryption"); + } + byte[] nf = spendDescription.getNullifier().toByteArray(); + byte[] transparentToAddressTvm = normalizeTransparentToAddress(transparentToAddress); + byte[] addr21 = new byte[21]; + addr21[0] = Wallet.getAddressPreFixByte(); + System.arraycopy(transparentToAddressTvm, 0, addr21, 1, 20); + Optional cipherOpt = Encryption.encryptBurnMessageByOvk( + ovk, transparentToAmount, addr21, nf); + if (!cipherOpt.isPresent()) { + throw new ZksnarkException("encrypt burn message failed"); + } + burnCiphertext = cipherOpt.get(); + mergedBytes = ByteUtil.merge(shieldedTRC20Address, encodeSpendDescriptionWithoutSpendAuthSig(spendDescription)); if (receives.size() == 1) { @@ -302,7 +335,7 @@ public ShieldedTRC20Parameters build(boolean withAsk) throws ZksnarkException { encodeCencCout(receiveDescription)); } mergedBytes = ByteUtil - .merge(mergedBytes, transparentToAddress, ByteArray.fromLong(valueBalance)); + .merge(mergedBytes, transparentToAddressTvm, ByteArray.fromLong(valueBalance)); value = transparentToAmount; builder.setParameterType("burn"); break; @@ -476,12 +509,10 @@ private String burnParamsToHexString(GrpcAPI.ShieldedTRC20Parameters burnParams, throw new IllegalArgumentException("the value must be positive"); } - if (ArrayUtils.isEmpty(transparentToAddress)) { - throw new IllegalArgumentException("the transparent payTo address is null"); - } + byte[] transparentToAddressTvm = normalizeTransparentToAddress(transparentToAddress); payTo[11] = Wallet.getAddressPreFixByte(); - System.arraycopy(transparentToAddress, 0, payTo, 12, 20); + System.arraycopy(transparentToAddressTvm, 0, payTo, 12, 20); ShieldContract.SpendDescription spendDesc = burnParams.getSpendDescription(0); byte[] spendAuthSign; @@ -492,7 +523,6 @@ private String burnParamsToHexString(GrpcAPI.ShieldedTRC20Parameters burnParams, } byte[] mergedBytes; - byte[] zeros = new byte[16]; mergedBytes = ByteUtil.merge( spendDesc.getNullifier().toByteArray(), spendDesc.getAnchor().toByteArray(), @@ -503,8 +533,7 @@ private String burnParamsToHexString(GrpcAPI.ShieldedTRC20Parameters burnParams, ByteUtil.bigIntegerToBytes(value, 32), burnParams.getBindingSignature().toByteArray(), payTo, - burnCiphertext, - zeros + burnCiphertext ); byte[] outputOffsetBytes; // 32 @@ -524,7 +553,7 @@ private String burnParamsToHexString(GrpcAPI.ShieldedTRC20Parameters burnParams, coffsetBytes = ByteUtil.longTo32Bytes(mergedBytes.length + 32 * 3 + 32L * 9); countBytes = ByteUtil.longTo32Bytes(1L); ReceiveDescription recvDesc = burnParams.getReceiveDescription(0); - zeros = new byte[12]; + byte[] zeros = new byte[12]; mergedBytes = ByteUtil .merge(mergedBytes, outputOffsetBytes, @@ -542,6 +571,18 @@ private String burnParamsToHexString(GrpcAPI.ShieldedTRC20Parameters burnParams, return Hex.toHexString(mergedBytes); } + private byte[] normalizeTransparentToAddress(byte[] transparentToAddress) { + if (transparentToAddress != null && transparentToAddress.length == 20) { + return transparentToAddress; + } + if (transparentToAddress != null && transparentToAddress.length == 21) { + byte[] transparentToAddressTvm = new byte[20]; + System.arraycopy(transparentToAddress, 1, transparentToAddressTvm, 0, 20); + return transparentToAddressTvm; + } + throw new IllegalArgumentException("invalid transparentToAddress for burn encryption"); + } + public void addSpend( ExpandedSpendingKey expsk, Note note, diff --git a/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java b/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java index 7d9de4ff596..048f90dd9d2 100644 --- a/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java +++ b/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java @@ -8,10 +8,13 @@ import static org.tron.core.zen.note.NoteEncryption.Encryption.NOTEENCRYPTION_CIPHER_KEYSIZE; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Optional; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; +import org.tron.common.crypto.Hash; import org.tron.common.utils.ByteUtil; import org.tron.common.zksnark.JLibrustzcash; import org.tron.common.zksnark.JLibsodium; @@ -111,6 +114,19 @@ public OutCiphertext encryptToOurselves( public static class Encryption { public static final int NOTEENCRYPTION_CIPHER_KEYSIZE = 32; + public static final int BURN_CIPHER_LEN = 80; + public static final int BURN_NONCE_LEN = 12; + public static final int BURN_RESERVED_LEN = 4; + public static final int BURN_CIPHER_RECORD_SIZE = 96; + public static final int BURN_NONCE_OFFSET = BURN_CIPHER_LEN; + public static final int BURN_RESERVED_OFFSET = BURN_NONCE_OFFSET + BURN_NONCE_LEN; + private static final byte[] BURN_RECORD_V2_MARKER = new byte[]{0, 0, 0, 1}; + private static final byte[] BURN_NONCE_DOMAIN = + "Ztron_BurnNonce".getBytes(StandardCharsets.UTF_8); + + public static byte[] getBurnRecordV2Marker() { + return BURN_RECORD_V2_MARKER.clone(); + } /** * generate ock by ovk, cv, cm, epk @@ -246,47 +262,137 @@ public static Optional attemptOutDecryption( } /** - * encrypt the message by ovk used for scanning + * encrypt burn message with nonce bound to (nf, amount, addr21), returns a 96B record: + * cipher(80) + nonce(12) + reserved/version(4). */ public static Optional encryptBurnMessageByOvk(byte[] ovk, BigInteger toAmount, - byte[] transparentToAddress) + byte[] transparentToAddress, byte[] nf) throws ZksnarkException { + if (ovk == null || ovk.length != NOTEENCRYPTION_CIPHER_KEYSIZE) { + throw new ZksnarkException("invalid ovk length"); + } + if (transparentToAddress == null || transparentToAddress.length != 21) { + throw new ZksnarkException("invalid transparentToAddress length"); + } + if (nf == null || nf.length != 32) { + throw new ZksnarkException("invalid nullifier length"); + } byte[] plaintext = new byte[64]; byte[] amountArray = ByteUtil.bigIntegerToBytes(toAmount, 32); - byte[] cipherNonce = new byte[12]; - byte[] cipher = new byte[80]; + byte[] nonce = deriveBurnNonce(nf, amountArray, transparentToAddress); + byte[] cipher = new byte[BURN_CIPHER_LEN]; System.arraycopy(amountArray, 0, plaintext, 0, 32); - System.arraycopy(transparentToAddress, 0, plaintext, 32, - 21); + System.arraycopy(transparentToAddress, 0, plaintext, 32, 21); if (JLibsodium.cryptoAeadChacha20Poly1305IetfEncrypt(new Chacha20Poly1305IetfEncryptParams( cipher, null, plaintext, - 64, null, 0, null, cipherNonce, ovk)) != 0) { + 64, null, 0, null, nonce, ovk)) != 0) { return Optional.empty(); } - return Optional.of(cipher); + byte[] record = new byte[BURN_CIPHER_RECORD_SIZE]; + System.arraycopy(cipher, 0, record, 0, BURN_CIPHER_LEN); + System.arraycopy(nonce, 0, record, BURN_NONCE_OFFSET, BURN_NONCE_LEN); + System.arraycopy(BURN_RECORD_V2_MARKER, 0, record, BURN_RESERVED_OFFSET, BURN_RESERVED_LEN); + return Optional.of(record); + } + + /** + * Derive a 12-byte ChaCha20-Poly1305 nonce from (nf, amount, addr21). + * Binding the plaintext fields ensures that repeated encryption with the same nf + * but different amount/addr produces distinct nonces, preserving AEAD nonce + * uniqueness even when the same input note is used to generate multiple burn + * trigger inputs off-chain. + */ + public static byte[] deriveBurnNonce(byte[] nf, byte[] amount32, byte[] addr21) { + if (nf == null || nf.length != 32) { + throw new IllegalArgumentException("invalid nullifier length"); + } + if (amount32 == null || amount32.length != 32) { + throw new IllegalArgumentException("invalid amount length"); + } + if (addr21 == null || addr21.length != 21) { + throw new IllegalArgumentException("invalid addr21 length"); + } + byte[] tagged = new byte[BURN_NONCE_DOMAIN.length + nf.length + amount32.length + + addr21.length]; + int off = 0; + System.arraycopy(BURN_NONCE_DOMAIN, 0, tagged, off, BURN_NONCE_DOMAIN.length); + off += BURN_NONCE_DOMAIN.length; + System.arraycopy(nf, 0, tagged, off, nf.length); + off += nf.length; + System.arraycopy(amount32, 0, tagged, off, amount32.length); + off += amount32.length; + System.arraycopy(addr21, 0, tagged, off, addr21.length); + byte[] hash = Hash.sha3(tagged); + byte[] nonce = new byte[BURN_NONCE_LEN]; + System.arraycopy(hash, 0, nonce, 0, BURN_NONCE_LEN); + return nonce; } /** - * decrypt the message by ovk used for scanning + * decrypt burn message. The trailing 4-byte reserved field is treated as an explicit + * record-version marker: + * - reserved = 0x00000000 and nonce = 0x000000000000000000000000 -> legacy v1 path. + * - reserved = 0x00000001 -> v2 path; nonce must equal + * deriveBurnNonce(nf, amount32, addr21) using the public log fields. + * - any other reserved value -> reject. */ - public static Optional decryptBurnMessageByOvk(byte[] ovk, byte[] ciphertext) + public static Optional decryptBurnMessageByOvk(byte[] ovk, byte[] ciphertext, + byte[] nonceFromLog, byte[] reservedFromLog, byte[] nf, byte[] amount32, byte[] addr21) throws ZksnarkException { + if (ovk == null || ovk.length != NOTEENCRYPTION_CIPHER_KEYSIZE) { + throw new ZksnarkException("invalid ovk length"); + } + if (ciphertext == null || ciphertext.length != BURN_CIPHER_LEN + || nonceFromLog == null || nonceFromLog.length != BURN_NONCE_LEN + || reservedFromLog == null || reservedFromLog.length != BURN_RESERVED_LEN) { + return Optional.empty(); + } + + byte[] effectiveNonce; + if (isAllZero(reservedFromLog)) { + if (!isAllZero(nonceFromLog)) { + return Optional.empty(); + } + effectiveNonce = nonceFromLog; + } else if (Arrays.equals(reservedFromLog, BURN_RECORD_V2_MARKER)) { + if (nf == null || nf.length != 32 + || amount32 == null || amount32.length != 32 + || addr21 == null || addr21.length != 21) { + return Optional.empty(); + } + byte[] derived = deriveBurnNonce(nf, amount32, addr21); + if (!Arrays.equals(nonceFromLog, derived)) { + return Optional.empty(); + } + effectiveNonce = nonceFromLog; + } else { + return Optional.empty(); + } + byte[] outPlaintext = new byte[64]; - byte[] cipherNonce = new byte[12]; if (JLibsodium.cryptoAeadChacha20poly1305IetfDecrypt(new Chacha20poly1305IetfDecryptParams( outPlaintext, null, null, - ciphertext, 80, + ciphertext, BURN_CIPHER_LEN, null, 0, - cipherNonce, ovk)) != 0) { + effectiveNonce, ovk)) != 0) { return Optional.empty(); } return Optional.of(outPlaintext); } + private static boolean isAllZero(byte[] data) { + for (byte b : data) { + if (b != 0) { + return false; + } + } + return true; + } + public static class EncCiphertext { @Getter diff --git a/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java b/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java index 9ea9ab922a6..080441bfaf4 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java @@ -1126,6 +1126,124 @@ public void verifyBurnWrongDataLength() throws ZksnarkException { Assert.assertEquals(0, result[31]); } + @Test + public void buildBurnRejectsInvalidTransparentToAddress() throws ZksnarkException { + long value = 100L; + SpendingKey senderSk = SpendingKey.random(); + ExpandedSpendingKey senderExpsk = senderSk.expandedSpendingKey(); + FullViewingKey senderFvk = senderSk.fullViewingKey(); + IncomingViewingKey senderIvk = senderFvk.inViewingKey(); + byte[] rcm = new byte[32]; + JLibrustzcash.librustzcashSaplingGenerateR(rcm); + PaymentAddress senderPaymentAddress = senderIvk.address(DiversifierT.random()).orElse(null); + assertNotNull(senderPaymentAddress); + + ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder(); + builder.setShieldedTRC20ParametersType(ShieldedTRC20ParametersType.BURN); + builder.setShieldedTRC20Address(SHIELDED_CONTRACT_ADDRESS); + builder.setTransparentToAmount(BigInteger.valueOf(value)); + builder.setTransparentToAddress(new byte[19]); + + Note senderNote = new Note(senderPaymentAddress.getD(), senderPaymentAddress.getPkD(), + value, rcm, new byte[512]); + byte[][] cm = new byte[1][32]; + System.arraycopy(senderNote.cm(), 0, cm[0], 0, 32); + IncrementalMerkleTreeContainer tree = new IncrementalMerkleTreeContainer( + new IncrementalMerkleTreeCapsule()); + IncrementalMerkleVoucherContainer voucher = addSimpleMerkleVoucherContainer(tree, cm); + byte[] path = decodePath(voucher.path().encode()); + byte[] anchor = voucher.root().getContent().toByteArray(); + long position = voucher.position(); + builder.addSpend(senderExpsk, senderNote, anchor, path, position); + + try { + builder.build(true); + Assert.fail("expected ZksnarkException for invalid transparentToAddress"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage().contains("invalid transparentToAddress")); + } + } + + @Test + public void buildBurnRejectsMissingOvk() throws ZksnarkException { + long value = 100L; + SpendingKey senderSk = SpendingKey.random(); + ExpandedSpendingKey senderExpsk = senderSk.expandedSpendingKey(); + FullViewingKey senderFvk = senderSk.fullViewingKey(); + IncomingViewingKey senderIvk = senderFvk.inViewingKey(); + byte[] rcm = new byte[32]; + JLibrustzcash.librustzcashSaplingGenerateR(rcm); + PaymentAddress senderPaymentAddress = senderIvk.address(DiversifierT.random()).orElse(null); + assertNotNull(senderPaymentAddress); + + ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder(); + builder.setShieldedTRC20ParametersType(ShieldedTRC20ParametersType.BURN); + builder.setShieldedTRC20Address(SHIELDED_CONTRACT_ADDRESS); + builder.setTransparentToAmount(BigInteger.valueOf(value)); + builder.setTransparentToAddress(PUBLIC_TO_ADDRESS); + + Note senderNote = new Note(senderPaymentAddress.getD(), senderPaymentAddress.getPkD(), + value, rcm, new byte[512]); + byte[][] cm = new byte[1][32]; + System.arraycopy(senderNote.cm(), 0, cm[0], 0, 32); + IncrementalMerkleTreeContainer tree = new IncrementalMerkleTreeContainer( + new IncrementalMerkleTreeCapsule()); + IncrementalMerkleVoucherContainer voucher = addSimpleMerkleVoucherContainer(tree, cm); + byte[] path = decodePath(voucher.path().encode()); + byte[] anchor = voucher.root().getContent().toByteArray(); + long position = voucher.position(); + builder.addSpend(senderFvk.getAk(), senderExpsk.getNsk(), senderNote, + Note.generateR(), anchor, path, position); + + try { + builder.build(false); + Assert.fail("expected ZksnarkException for missing ovk"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage().contains("missing ovk for burn encryption")); + } + } + + @Test + public void buildBurnRejectsMissingExpandedSpendingKeyForWithAsk() throws ZksnarkException { + long value = 100L; + SpendingKey senderSk = SpendingKey.random(); + ExpandedSpendingKey senderExpsk = senderSk.expandedSpendingKey(); + FullViewingKey senderFvk = senderSk.fullViewingKey(); + IncomingViewingKey senderIvk = senderFvk.inViewingKey(); + byte[] rcm = new byte[32]; + JLibrustzcash.librustzcashSaplingGenerateR(rcm); + PaymentAddress senderPaymentAddress = senderIvk.address(DiversifierT.random()).orElse(null); + assertNotNull(senderPaymentAddress); + + ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder(); + builder.setShieldedTRC20ParametersType(ShieldedTRC20ParametersType.BURN); + builder.setShieldedTRC20Address(SHIELDED_CONTRACT_ADDRESS); + builder.setTransparentToAmount(BigInteger.valueOf(value)); + builder.setTransparentToAddress(PUBLIC_TO_ADDRESS); + builder.setOvk(senderFvk.getOvk()); + + Note senderNote = new Note(senderPaymentAddress.getD(), senderPaymentAddress.getPkD(), + value, rcm, new byte[512]); + byte[][] cm = new byte[1][32]; + System.arraycopy(senderNote.cm(), 0, cm[0], 0, 32); + IncrementalMerkleTreeContainer tree = new IncrementalMerkleTreeContainer( + new IncrementalMerkleTreeCapsule()); + IncrementalMerkleVoucherContainer voucher = addSimpleMerkleVoucherContainer(tree, cm); + byte[] path = decodePath(voucher.path().encode()); + byte[] anchor = voucher.root().getContent().toByteArray(); + long position = voucher.position(); + builder.addSpend(senderFvk.getAk(), senderExpsk.getNsk(), senderNote, + Note.generateR(), anchor, path, position); + + try { + builder.build(true); + Assert.fail("expected ZksnarkException for missing expanded spending key"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage().contains( + "missing expanded spending key for spend authorization")); + } + } + @Test public void verifyMintWrongLeafcount() throws ZksnarkException { long value = 100L; diff --git a/framework/src/test/java/org/tron/core/WalletMockTest.java b/framework/src/test/java/org/tron/core/WalletMockTest.java index c9184bf276d..2f4c08d8f9f 100644 --- a/framework/src/test/java/org/tron/core/WalletMockTest.java +++ b/framework/src/test/java/org/tron/core/WalletMockTest.java @@ -7,10 +7,12 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import com.google.common.cache.Cache; @@ -24,6 +26,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -39,6 +42,7 @@ import org.tron.common.utils.ByteUtil; import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.client.WalletClient; +import org.tron.common.zksnark.JLibrustzcash; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.ContractCapsule; @@ -76,6 +80,7 @@ import org.tron.core.zen.address.ExpandedSpendingKey; import org.tron.core.zen.address.KeyIo; import org.tron.core.zen.address.PaymentAddress; +import org.tron.core.zen.note.Note; import org.tron.protos.Protocol; import org.tron.protos.contract.BalanceContract; import org.tron.protos.contract.ShieldContract; @@ -1190,9 +1195,10 @@ public void testGetShieldedTRC20LogTypeReturnsCorrectInt() throws Exception { "MintNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])", "TransferNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])", "BurnNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])", - "TokenBurn(address,uint256,bytes32[3])" + "TokenBurn(address,uint256,bytes32[3])", + "NoteSpent(bytes32)" }; - int[] expectedTypes = {1, 2, 3, 4}; + int[] expectedTypes = {1, 2, 3, 4, 5}; for (int i = 0; i < eventSignatures.length; i++) { byte[] topicHash = Hash.sha3(ByteArray.fromString(eventSignatures[i])); @@ -1206,6 +1212,70 @@ public void testGetShieldedTRC20LogTypeReturnsCorrectInt() throws Exception { } } + @Test + public void scanShieldedTRC20NotesByIvkSkipsNoteSpentIndex() throws Exception { + final String SHIELDED_CONTRACT_ADDRESS_STR = "TGAmX5AqVUoXCf8MoHxbuhQPmhGfWTnEgA"; + byte[] contractAddress = WalletClient.decodeFromBase58Check(SHIELDED_CONTRACT_ADDRESS_STR); + byte[] addressWithoutPrefix = new byte[20]; + System.arraycopy(contractAddress, 1, addressWithoutPrefix, 0, 20); + + byte[] noteSpentTopic = Hash.sha3(ByteArray.fromString("NoteSpent(bytes32)")); + Protocol.TransactionInfo.Log noteSpentLog = Protocol.TransactionInfo.Log.newBuilder() + .setAddress(ByteString.copyFrom(addressWithoutPrefix)) + .addTopics(ByteString.copyFrom(noteSpentTopic)) + .setData(ByteString.copyFrom(new byte[32])) + .build(); + + byte[] transferTopic = Hash.sha3(ByteArray.fromString( + "TransferNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])")); + // getNoteTxFromLogListByIvk slices bytes 0..708; only `pos` (bytes 0..32) is read here. + byte[] transferData = new byte[708]; + Protocol.TransactionInfo.Log transferLog = Protocol.TransactionInfo.Log.newBuilder() + .setAddress(ByteString.copyFrom(addressWithoutPrefix)) + .addTopics(ByteString.copyFrom(transferTopic)) + .setData(ByteString.copyFrom(transferData)) + .build(); + + Protocol.TransactionInfo info = Protocol.TransactionInfo.newBuilder() + .addLog(noteSpentLog) + .addLog(transferLog) + .build(); + + Protocol.Block block = Protocol.Block.newBuilder() + .addTransactions(Protocol.Transaction.newBuilder().build()) + .build(); + GrpcAPI.BlockList blockList = GrpcAPI.BlockList.newBuilder().addBlock(block).build(); + + Wallet wallet = spy(new Wallet()); + doReturn(blockList).when(wallet).getBlocksByLimitNext(anyLong(), anyLong()); + doReturn(info).when(wallet).getTransactionInfoById(any()); + + // Bypass the real ZK crypto: return a valid note and a deterministic payment address + // so the scanner reaches the index-assignment branch. + Note fakeNote = new Note(new DiversifierT(), new byte[32], 100L, + new byte[32], new byte[512]); + boolean prevAllow = CommonParameter.getInstance().isAllowShieldedTransactionApi(); + CommonParameter.getInstance().setAllowShieldedTransactionApi(true); + try (MockedStatic noteMock = mockStatic(Note.class); + MockedStatic rustMock = mockStatic(JLibrustzcash.class); + MockedStatic keyIoMock = mockStatic(KeyIo.class)) { + noteMock.when(() -> Note.decrypt(any(byte[].class), any(byte[].class), + any(byte[].class), any(byte[].class))).thenReturn(Optional.of(fakeNote)); + rustMock.when(() -> JLibrustzcash.librustzcashIvkToPkd(any())).thenReturn(true); + keyIoMock.when(() -> KeyIo.encodePaymentAddress(any())).thenReturn("zaddrFake"); + + byte[] ivk = new byte[32]; + GrpcAPI.DecryptNotesTRC20 result = wallet.scanShieldedTRC20NotesByIvk( + 0L, 1L, contractAddress, ivk, new byte[0], new byte[0]); + + assertEquals(1, result.getNoteTxsCount()); + assertEquals("TransferNewLeaf must get index 0; NoteSpent must not advance the counter", + 0L, result.getNoteTxs(0).getIndex()); + } finally { + CommonParameter.getInstance().setAllowShieldedTransactionApi(prevAllow); + } + } + @Test public void testBuildShieldedTRC20InputWithAK() throws ZksnarkException { Wallet wallet = new Wallet(); diff --git a/framework/src/test/java/org/tron/core/zen/note/BurnCipherTest.java b/framework/src/test/java/org/tron/core/zen/note/BurnCipherTest.java new file mode 100644 index 00000000000..40a3100c669 --- /dev/null +++ b/framework/src/test/java/org/tron/core/zen/note/BurnCipherTest.java @@ -0,0 +1,300 @@ +package org.tron.core.zen.note; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Optional; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.utils.ByteUtil; +import org.tron.core.exception.ZksnarkException; +import org.tron.core.zen.note.NoteEncryption.Encryption; + +public class BurnCipherTest { + + private static final byte[] OVK = buildTestBytes(32, 1); + private static final byte[] NF = buildTestBytes(32, 7); + private static final byte[] ADDR_21 = buildAddr21((byte) 0x41); + + private static byte[] buildTestBytes(int len, int seed) { + byte[] data = new byte[len]; + for (int i = 0; i < len; i++) { + data[i] = (byte) (i * 3 + seed); + } + return data; + } + + private static byte[] buildAddr21(byte prefix) { + byte[] addr = new byte[21]; + addr[0] = prefix; + for (int i = 1; i < 21; i++) { + addr[i] = (byte) (i * 2); + } + return addr; + } + + private static byte[] amount32(BigInteger amount) { + return ByteUtil.bigIntegerToBytes(amount, 32); + } + + private static byte[] extractCipher(byte[] record) { + return Arrays.copyOf(record, Encryption.BURN_CIPHER_LEN); + } + + private static byte[] extractNonce(byte[] record) { + return Arrays.copyOfRange(record, + Encryption.BURN_NONCE_OFFSET, + Encryption.BURN_NONCE_OFFSET + Encryption.BURN_NONCE_LEN); + } + + private static byte[] extractReserved(byte[] record) { + return Arrays.copyOfRange(record, + Encryption.BURN_RESERVED_OFFSET, + Encryption.BURN_RESERVED_OFFSET + Encryption.BURN_RESERVED_LEN); + } + + // ---------- constants ---------- + + @Test + public void testBurnCipherSize() { + Assert.assertEquals(80, Encryption.BURN_CIPHER_LEN); + Assert.assertEquals(12, Encryption.BURN_NONCE_LEN); + Assert.assertEquals(4, Encryption.BURN_RESERVED_LEN); + Assert.assertEquals(96, Encryption.BURN_CIPHER_RECORD_SIZE); + } + + // ---------- encrypt ---------- + + @Test + public void testEncryptProduces96ByteRecord() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + Optional recordOpt = Encryption.encryptBurnMessageByOvk( + OVK, amount, ADDR_21, NF); + Assert.assertTrue(recordOpt.isPresent()); + Assert.assertEquals(Encryption.BURN_CIPHER_RECORD_SIZE, recordOpt.get().length); + } + + @Test + public void testRecordReservedBytesCarryV2Marker() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + Assert.assertArrayEquals(new byte[]{0, 0, 0, 1}, extractReserved(record)); + } + + @Test + public void testNonceEmbeddedInRecord() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] nonce = extractNonce(record); + boolean allZero = true; + for (byte b : nonce) { + if (b != 0) { + allZero = false; + break; + } + } + Assert.assertFalse(allZero); + } + + @Test + public void testNonceDeterminism() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + byte[] record1 = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] record2 = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + Assert.assertArrayEquals(record1, record2); + } + + @Test + public void testDifferentNfProducesDifferentRecord() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + byte[] nf2 = new byte[32]; + nf2[0] = (byte) 0xFF; + + byte[] record1 = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] record2 = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, nf2).get(); + Assert.assertFalse(Arrays.equals(record1, record2)); + } + + @Test + public void testDifferentAmountProducesDifferentNonce() throws ZksnarkException { + byte[] record1 = Encryption.encryptBurnMessageByOvk( + OVK, BigInteger.valueOf(1000000), ADDR_21, NF).get(); + byte[] record2 = Encryption.encryptBurnMessageByOvk( + OVK, BigInteger.valueOf(2000000), ADDR_21, NF).get(); + Assert.assertFalse(Arrays.equals(extractNonce(record1), extractNonce(record2))); + } + + @Test + public void testDifferentAddrProducesDifferentNonce() throws ZksnarkException { + byte[] addr2 = ADDR_21.clone(); + addr2[5] ^= (byte) 0xFF; + BigInteger amount = BigInteger.valueOf(1000000); + byte[] record1 = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] record2 = Encryption.encryptBurnMessageByOvk(OVK, amount, addr2, NF).get(); + Assert.assertFalse(Arrays.equals(extractNonce(record1), extractNonce(record2))); + } + + // ---------- encrypt input validation ---------- + + @Test(expected = ZksnarkException.class) + public void testEncryptRejectsNullNf() throws ZksnarkException { + Encryption.encryptBurnMessageByOvk(OVK, BigInteger.ONE, ADDR_21, null); + } + + @Test(expected = ZksnarkException.class) + public void testEncryptRejectsShortOvk() throws ZksnarkException { + Encryption.encryptBurnMessageByOvk(new byte[16], BigInteger.ONE, ADDR_21, NF); + } + + @Test(expected = ZksnarkException.class) + public void testEncryptRejectsBadAddrLength() throws ZksnarkException { + Encryption.encryptBurnMessageByOvk(OVK, BigInteger.ONE, new byte[20], NF); + } + + // ---------- decrypt round-trip ---------- + + @Test + public void testEncryptDecryptRoundTrip() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(1000000); + + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + + Optional plainOpt = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, extractReserved(record), NF, amount32(amount), ADDR_21); + Assert.assertTrue(plainOpt.isPresent()); + byte[] plaintext = plainOpt.get(); + + byte[] decryptedAmount = new byte[32]; + System.arraycopy(plaintext, 0, decryptedAmount, 0, 32); + Assert.assertEquals(amount, ByteUtil.bytesToBigInteger(decryptedAmount)); + + byte[] decryptedAddr = new byte[21]; + System.arraycopy(plaintext, 32, decryptedAddr, 0, 21); + Assert.assertArrayEquals(ADDR_21, decryptedAddr); + } + + @Test + public void testDecryptWithWrongNfFails() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + + byte[] wrongNf = new byte[32]; + wrongNf[0] = (byte) 0xFF; + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, extractReserved(record), wrongNf, amount32(amount), ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptWithWrongAmountFails() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, extractReserved(record), NF, + amount32(BigInteger.valueOf(500001)), ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptWithWrongAddrFails() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + byte[] wrongAddr = ADDR_21.clone(); + wrongAddr[10] ^= (byte) 0xFF; + + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, extractReserved(record), NF, amount32(amount), wrongAddr); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptWithNullNfFailsForV2() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, extractReserved(record), null, amount32(amount), ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptWithWrongNfLengthFailsForV2() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + byte[] reserved = extractReserved(record); + byte[] amt = amount32(amount); + + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, reserved, new byte[31], amt, ADDR_21).isPresent()); + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, reserved, new byte[33], amt, ADDR_21).isPresent()); + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, reserved, new byte[0], amt, ADDR_21).isPresent()); + } + + @Test + public void testDecryptWithTamperedNonceFails() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + + byte[] tamperedNonce = new byte[Encryption.BURN_NONCE_LEN]; + tamperedNonce[0] = (byte) 0xDE; + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, tamperedNonce, extractReserved(record), NF, amount32(amount), ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptWithUnknownReservedMarkerFails() throws ZksnarkException { + BigInteger amount = BigInteger.valueOf(500000); + byte[] record = Encryption.encryptBurnMessageByOvk(OVK, amount, ADDR_21, NF).get(); + byte[] cipher = extractCipher(record); + byte[] nonce = extractNonce(record); + byte[] badReserved = new byte[]{0, 0, 0, 2}; + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, cipher, nonce, badReserved, NF, amount32(amount), ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + // ---------- decrypt input validation ---------- + + @Test(expected = ZksnarkException.class) + public void testDecryptRejectsNullOvk() throws ZksnarkException { + Encryption.decryptBurnMessageByOvk(null, new byte[80], new byte[12], new byte[4], NF, + new byte[32], ADDR_21); + } + + @Test + public void testDecryptRejectsBadCipherLength() throws ZksnarkException { + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, new byte[64], new byte[12], new byte[4], NF, new byte[32], ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptRejectsNullNonce() throws ZksnarkException { + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, new byte[80], null, new byte[4], NF, new byte[32], ADDR_21); + Assert.assertFalse(result.isPresent()); + } + + @Test + public void testDecryptRejectsNullReserved() throws ZksnarkException { + Optional result = Encryption.decryptBurnMessageByOvk( + OVK, new byte[80], new byte[12], null, NF, new byte[32], ADDR_21); + Assert.assertFalse(result.isPresent()); + } +} diff --git a/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java b/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java index e41b9f64c9a..d94f66bde7f 100644 --- a/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/NoteEncDecryTest.java @@ -1,15 +1,21 @@ package org.tron.core.zksnark; import com.google.protobuf.ByteString; +import java.lang.reflect.Method; +import java.math.BigInteger; import java.util.Optional; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.tron.api.GrpcAPI; import org.tron.common.BaseTest; import org.tron.common.TestConstants; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.ByteUtil; +import org.tron.common.zksnark.JLibsodium; +import org.tron.common.zksnark.JLibsodiumParam.Chacha20Poly1305IetfEncryptParams; import org.tron.core.Wallet; import org.tron.core.capsule.AssetIssueCapsule; import org.tron.core.config.args.Args; @@ -18,7 +24,9 @@ import org.tron.core.zen.note.NoteEncryption.Encryption; import org.tron.core.zen.note.NoteEncryption.Encryption.OutCiphertext; import org.tron.core.zen.note.OutgoingPlaintext; +import org.tron.protos.Protocol.TransactionInfo; import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract; +import org.tron.protos.contract.ShieldContract; @Slf4j public class NoteEncDecryTest extends BaseTest { @@ -194,4 +202,314 @@ public void testDecryptEncWithEpk() throws ZksnarkException { Assert.assertArrayEquals(rcm, result2.getRcm()); Assert.assertEquals(4000, result2.getValue()); } + + @Test + public void testBurnMessageOvkLegacyZeroNonce() throws ZksnarkException { + byte[] ovk = new byte[]{ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}; + byte[] toAddress = new byte[21]; + toAddress[0] = Wallet.getAddressPreFixByte(); + toAddress[20] = 0x42; + BigInteger amount = BigInteger.valueOf(99L); + + byte[] plaintext = new byte[64]; + byte[] amountArr = ByteUtil.bigIntegerToBytes(amount, 32); + System.arraycopy(amountArr, 0, plaintext, 0, 32); + System.arraycopy(toAddress, 0, plaintext, 32, 21); + byte[] zeroNonce = new byte[12]; + byte[] v1Cipher = new byte[Encryption.BURN_CIPHER_LEN]; + int rc = JLibsodium.cryptoAeadChacha20Poly1305IetfEncrypt( + new Chacha20Poly1305IetfEncryptParams( + v1Cipher, null, plaintext, 64, null, 0, null, zeroNonce, ovk)); + Assert.assertEquals(0, rc); + + Optional p1 = Encryption.decryptBurnMessageByOvk( + ovk, v1Cipher, new byte[12], new byte[4], null, null, null); + Assert.assertTrue(p1.isPresent()); + Assert.assertArrayEquals(plaintext, p1.get()); + + byte[] wrongNonce = new byte[12]; + wrongNonce[0] = 1; + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + ovk, v1Cipher, wrongNonce, new byte[4], null, null, null).isPresent()); + + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + ovk, v1Cipher, new byte[11], new byte[4], null, null, null).isPresent()); + Assert.assertFalse(Encryption.decryptBurnMessageByOvk( + ovk, v1Cipher, null, new byte[4], null, null, null).isPresent()); + } + + @Test + public void testGetTriggerInputBurnV2Accepted() throws Exception { + byte[] nf = new byte[32]; + for (int i = 0; i < nf.length; i++) { + nf[i] = (byte) (i + 1); + } + byte[] burnRecord = buildV2BurnRecord(nf); + GrpcAPI.ShieldedTRC20Parameters trc20Params = buildBurnTrc20Params(burnRecord, nf); + GrpcAPI.ShieldedTRC20TriggerContractParameters req = buildBurnTriggerRequest( + trc20Params, BigInteger.ONE); + GrpcAPI.BytesMessage out = wallet.getTriggerInputForShieldedTRC20Contract(req); + Assert.assertNotNull(out); + } + + @Test + public void testGetTriggerInputBurnLegacy96ByteRecordRejected() throws Exception { + byte[] allZeroRecord = new byte[Encryption.BURN_CIPHER_RECORD_SIZE]; + byte[] nf = new byte[32]; + GrpcAPI.ShieldedTRC20Parameters trc20Params = buildBurnTrc20Params(allZeroRecord, nf); + GrpcAPI.ShieldedTRC20TriggerContractParameters req = buildBurnTriggerRequest( + trc20Params, BigInteger.ONE); + try { + wallet.getTriggerInputForShieldedTRC20Contract(req); + Assert.fail("expected ZksnarkException for legacy 96-byte burn record"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains("v2")); + } + } + + @Test + public void testGetTriggerInputBurnUnknownReservedRejected() throws Exception { + byte[] nf = new byte[32]; + nf[0] = 0x5A; + byte[] record = buildV2BurnRecord(nf); + // mutate reserved to an unknown marker (0x00000002). + record[Encryption.BURN_RESERVED_OFFSET + Encryption.BURN_RESERVED_LEN - 1] = 2; + GrpcAPI.ShieldedTRC20Parameters trc20Params = buildBurnTrc20Params(record, nf); + GrpcAPI.ShieldedTRC20TriggerContractParameters req = buildBurnTriggerRequest( + trc20Params, BigInteger.ONE); + try { + wallet.getTriggerInputForShieldedTRC20Contract(req); + Assert.fail("expected ZksnarkException for unknown reserved marker"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains("v2")); + } + } + + @Test + public void testGetTriggerInputBurnNonceMismatchRejected() throws Exception { + byte[] nf = new byte[32]; + nf[0] = 0x11; + byte[] record = buildV2BurnRecord(nf); + // flip one nonce byte so it no longer matches deriveBurnNonce(nf, amount, addr). + record[Encryption.BURN_NONCE_OFFSET] ^= (byte) 0xFF; + GrpcAPI.ShieldedTRC20Parameters trc20Params = buildBurnTrc20Params(record, nf); + GrpcAPI.ShieldedTRC20TriggerContractParameters req = buildBurnTriggerRequest( + trc20Params, BigInteger.ONE); + try { + wallet.getTriggerInputForShieldedTRC20Contract(req); + Assert.fail("expected ZksnarkException for mismatched nf-bound nonce"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains("nonce")); + } + } + + @Test + public void testGetTriggerInputBurn80ByteCipherRejected() throws Exception { + byte[] legacyCipher = new byte[Encryption.BURN_CIPHER_LEN]; + byte[] nf = new byte[32]; + GrpcAPI.ShieldedTRC20Parameters trc20Params = buildBurnTrc20Params(legacyCipher, nf); + GrpcAPI.ShieldedTRC20TriggerContractParameters req = buildBurnTriggerRequest( + trc20Params, BigInteger.ONE); + try { + wallet.getTriggerInputForShieldedTRC20Contract(req); + Assert.fail("expected ZksnarkException for 80-byte burn cipher"); + } catch (ZksnarkException e) { + Assert.assertTrue(e.getMessage().contains("deprecated")); + } + } + + private static byte[] buildV2BurnRecord(byte[] nf) { + byte[] record = new byte[Encryption.BURN_CIPHER_RECORD_SIZE]; + // cipher(0..80) left as zeros — getTriggerInputForShieldedTRC20Contract only + // checks reserved marker and nonce binding to (nf, amount, addr), not cipher decryptability. + byte[] amount32 = ByteUtil.bigIntegerToBytes(BigInteger.ONE, 32); + byte[] addr21 = new byte[21]; + addr21[0] = Wallet.getAddressPreFixByte(); + byte[] nonce = Encryption.deriveBurnNonce(nf, amount32, addr21); + System.arraycopy(nonce, 0, record, Encryption.BURN_NONCE_OFFSET, Encryption.BURN_NONCE_LEN); + byte[] marker = Encryption.getBurnRecordV2Marker(); + System.arraycopy(marker, 0, record, Encryption.BURN_RESERVED_OFFSET, + Encryption.BURN_RESERVED_LEN); + return record; + } + + @Test + public void testGetNoteTxFromLogListByOvkBurnTooShort() throws Exception { + Wallet w = new Wallet(); + byte[] ovk = new byte[32]; + byte[] logData = new byte[64 + Encryption.BURN_CIPHER_RECORD_SIZE - 1]; + TransactionInfo.Log log = TransactionInfo.Log.newBuilder() + .setData(ByteString.copyFrom(logData)).build(); + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder builder = + GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(); + + Method m = Wallet.class.getDeclaredMethod("getNoteTxFromLogListByOvk", + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder.class, + TransactionInfo.Log.class, byte[].class, int.class, byte[].class); + m.setAccessible(true); + Object result = m.invoke(w, builder, log, ovk, 4, null); + Assert.assertFalse(((Optional) result).isPresent()); + } + + @Test + public void testGetNoteTxFromLogListByOvkBurnRoundTrip() throws Exception { + Wallet w = new Wallet(); + byte[] ovk = new byte[32]; + for (int i = 0; i < 32; i++) { + ovk[i] = (byte) (i + 1); + } + BigInteger amount = BigInteger.valueOf(1000L); + byte[] toAddress = new byte[21]; + toAddress[0] = Wallet.getAddressPreFixByte(); + toAddress[20] = 0x42; + byte[] nf = new byte[32]; + nf[0] = (byte) 0xAB; + + Optional recordOpt = Encryption.encryptBurnMessageByOvk( + ovk, amount, toAddress, nf); + Assert.assertTrue(recordOpt.isPresent()); + byte[] record = recordOpt.get(); + + byte[] logData = new byte[64 + Encryption.BURN_CIPHER_RECORD_SIZE]; + System.arraycopy(toAddress, 1, logData, 12, 20); + byte[] valBytes = ByteUtil.bigIntegerToBytes(amount, 32); + System.arraycopy(valBytes, 0, logData, 32, 32); + System.arraycopy(record, 0, logData, 64, Encryption.BURN_CIPHER_RECORD_SIZE); + + TransactionInfo.Log log = TransactionInfo.Log.newBuilder() + .setData(ByteString.copyFrom(logData)).build(); + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder builder = + GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(); + + Method m = Wallet.class.getDeclaredMethod("getNoteTxFromLogListByOvk", + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder.class, + TransactionInfo.Log.class, byte[].class, int.class, byte[].class); + m.setAccessible(true); + Object result = m.invoke(w, builder, log, ovk, 4, nf); + Assert.assertTrue(((Optional) result).isPresent()); + } + + @Test + public void testGetNoteTxFromLogListByOvkBurnMissingNfRejected() throws Exception { + Wallet w = new Wallet(); + byte[] ovk = new byte[32]; + for (int i = 0; i < 32; i++) { + ovk[i] = (byte) (i + 1); + } + BigInteger amount = BigInteger.valueOf(1000L); + byte[] toAddress = new byte[21]; + toAddress[0] = Wallet.getAddressPreFixByte(); + toAddress[20] = 0x42; + byte[] nf = new byte[32]; + nf[0] = (byte) 0xAB; + + Optional recordOpt = Encryption.encryptBurnMessageByOvk( + ovk, amount, toAddress, nf); + Assert.assertTrue(recordOpt.isPresent()); + byte[] record = recordOpt.get(); + + byte[] logData = new byte[64 + Encryption.BURN_CIPHER_RECORD_SIZE]; + System.arraycopy(toAddress, 1, logData, 12, 20); + byte[] valBytes = ByteUtil.bigIntegerToBytes(amount, 32); + System.arraycopy(valBytes, 0, logData, 32, 32); + System.arraycopy(record, 0, logData, 64, Encryption.BURN_CIPHER_RECORD_SIZE); + + TransactionInfo.Log log = TransactionInfo.Log.newBuilder() + .setData(ByteString.copyFrom(logData)).build(); + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder builder = + GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(); + + Method m = Wallet.class.getDeclaredMethod("getNoteTxFromLogListByOvk", + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder.class, + TransactionInfo.Log.class, byte[].class, int.class, byte[].class); + m.setAccessible(true); + Object result = m.invoke(w, builder, log, ovk, 4, null); + Assert.assertFalse(((Optional) result).isPresent()); + } + + @Test + public void testGetNoteTxFromLogListByOvkTwoBurnsCursorPairing() throws Exception { + Wallet w = new Wallet(); + byte[] ovk = new byte[32]; + for (int i = 0; i < 32; i++) { + ovk[i] = (byte) (i + 1); + } + byte[] toAddress = new byte[21]; + toAddress[0] = Wallet.getAddressPreFixByte(); + toAddress[20] = 0x42; + + byte[] nf1 = new byte[32]; + nf1[0] = (byte) 0xAA; + byte[] nf2 = new byte[32]; + nf2[0] = (byte) 0xBB; + BigInteger amount1 = BigInteger.valueOf(1000L); + BigInteger amount2 = BigInteger.valueOf(2000L); + + TransactionInfo.Log log1 = buildBurnLog(ovk, amount1, toAddress, nf1); + TransactionInfo.Log log2 = buildBurnLog(ovk, amount2, toAddress, nf2); + + Method m = Wallet.class.getDeclaredMethod("getNoteTxFromLogListByOvk", + GrpcAPI.DecryptNotesTRC20.NoteTx.Builder.class, + TransactionInfo.Log.class, byte[].class, int.class, byte[].class); + m.setAccessible(true); + + // correct cursor pairing: each log decrypted with its own nf + Optional r1 = (Optional) m.invoke( + w, GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(), log1, ovk, 4, nf1); + Optional r2 = (Optional) m.invoke( + w, GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(), log2, ovk, 4, nf2); + Assert.assertTrue("burn1 should decrypt with nf1", r1.isPresent()); + Assert.assertTrue("burn2 should decrypt with nf2", r2.isPresent()); + GrpcAPI.DecryptNotesTRC20.NoteTx tx1 = (GrpcAPI.DecryptNotesTRC20.NoteTx) r1.get(); + GrpcAPI.DecryptNotesTRC20.NoteTx tx2 = (GrpcAPI.DecryptNotesTRC20.NoteTx) r2.get(); + Assert.assertEquals(amount1.toString(10), tx1.getToAmount()); + Assert.assertEquals(amount2.toString(10), tx2.getToAmount()); + + // mis-paired cursor: nonce-from-log mismatches sha3(domain||nf||amount||addr), strict rejects + Optional bad1 = (Optional) m.invoke( + w, GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(), log1, ovk, 4, nf2); + Optional bad2 = (Optional) m.invoke( + w, GrpcAPI.DecryptNotesTRC20.NoteTx.newBuilder(), log2, ovk, 4, nf1); + Assert.assertFalse("burn1 must not decrypt under nf2", bad1.isPresent()); + Assert.assertFalse("burn2 must not decrypt under nf1", bad2.isPresent()); + } + + private TransactionInfo.Log buildBurnLog(byte[] ovk, BigInteger amount, byte[] toAddress, + byte[] nf) throws ZksnarkException { + Optional recordOpt = Encryption.encryptBurnMessageByOvk(ovk, amount, toAddress, nf); + Assert.assertTrue(recordOpt.isPresent()); + byte[] record = recordOpt.get(); + byte[] logData = new byte[64 + Encryption.BURN_CIPHER_RECORD_SIZE]; + System.arraycopy(toAddress, 1, logData, 12, 20); + byte[] valBytes = ByteUtil.bigIntegerToBytes(amount, 32); + System.arraycopy(valBytes, 0, logData, 32, 32); + System.arraycopy(record, 0, logData, 64, Encryption.BURN_CIPHER_RECORD_SIZE); + return TransactionInfo.Log.newBuilder() + .setData(ByteString.copyFrom(logData)).build(); + } + + private GrpcAPI.ShieldedTRC20Parameters buildBurnTrc20Params(byte[] cipher, byte[] nf) { + ShieldContract.SpendDescription spend = ShieldContract.SpendDescription.newBuilder() + .setNullifier(ByteString.copyFrom(nf)) + .build(); + return GrpcAPI.ShieldedTRC20Parameters.newBuilder() + .setParameterType("burn") + .setTriggerContractInput(ByteArray.toHexString(cipher)) + .addSpendDescription(spend) + .build(); + } + + private GrpcAPI.ShieldedTRC20TriggerContractParameters buildBurnTriggerRequest( + GrpcAPI.ShieldedTRC20Parameters trc20Params, BigInteger value) { + byte[] toAddress = new byte[21]; + toAddress[0] = Wallet.getAddressPreFixByte(); + return GrpcAPI.ShieldedTRC20TriggerContractParameters.newBuilder() + .setShieldedTRC20Parameters(trc20Params) + .addSpendAuthoritySignature(GrpcAPI.BytesMessage.getDefaultInstance()) + .setAmount(value.toString()) + .setTransparentToAddress(ByteString.copyFrom(toAddress)) + .build(); + } } From 4b5d37d597fbfdf0b5c9fc7486a374f1050fb1e8 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Thu, 28 May 2026 10:45:15 +0800 Subject: [PATCH 23/57] refactor(config): overhaul config docs, fix defaults, remove dead params (#6790) --- .../common/parameter/CommonParameter.java | 3 - .../org/tron/core/config/args/NodeConfig.java | 89 ++-- common/src/main/resources/reference.conf | 193 +++++--- docs/configuration-conventions.md | 226 +++++++++ docs/configuration.md | 259 +++++++++++ .../java/org/tron/core/config/args/Args.java | 12 +- framework/src/main/resources/config.conf | 431 ++---------------- .../java/org/tron/common/ParameterTest.java | 2 - .../org/tron/core/config/args/ArgsTest.java | 174 ++----- .../src/test/resources/config-shield.conf | 47 -- framework/src/test/resources/config-test.conf | 66 +-- 11 files changed, 752 insertions(+), 750 deletions(-) create mode 100644 docs/configuration-conventions.md create mode 100644 docs/configuration.md diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 19d03f92a31..eeb92fdbd60 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -316,9 +316,6 @@ public class CommonParameter { public List backupMembers; @Getter @Setter - public long receiveTcpMinDataLength; // clearParam: 2048 - @Getter - @Setter public boolean isOpenFullTcpDisconnect; @Getter @Setter diff --git a/common/src/main/java/org/tron/core/config/args/NodeConfig.java b/common/src/main/java/org/tron/core/config/args/NodeConfig.java index bb6bdc02f4e..2158f56d0ba 100644 --- a/common/src/main/java/org/tron/core/config/args/NodeConfig.java +++ b/common/src/main/java/org/tron/core/config/args/NodeConfig.java @@ -17,6 +17,7 @@ // ConfigBeanFactory auto-binds all fields including sub-beans, dot-notation keys, // PBFT fields, and list fields. Only legacy key fallbacks and PascalCase shutdown // keys are read manually. +// Always construct via {@link #fromConfig} — direct construction skips postProcess() clamping. @Slf4j @Getter @Setter @@ -77,7 +78,6 @@ public String getDiscoveryExternalIp() { private ValidContractProtoConfig validContractProto = new ValidContractProtoConfig(); private int shieldedTransInPendingMaxCounts = 10; private long blockCacheTimeout = 60; - private long receiveTcpMinDataLength = 2048; private int maxTransactionPendingSize = 2000; private long pendingTransactionTimeout = 60000; private int maxTrxCacheSize = 50_000; @@ -215,10 +215,10 @@ public static class RpcConfig { private int pBFTPort = 50071; private int thread = 0; - private int maxConcurrentCallsPerConnection = 2147483647; + private int maxConcurrentCallsPerConnection = 0; private int flowControlWindow = 1048576; - private long maxConnectionIdleInMillis = Long.MAX_VALUE; - private long maxConnectionAgeInMillis = Long.MAX_VALUE; + private long maxConnectionIdleInMillis = 0; + private long maxConnectionAgeInMillis = 0; private int maxMessageSize = 4194304; private int maxHeaderListSize = 8192; private int maxRstStream = 0; @@ -277,8 +277,8 @@ public static class DnsConfig { private String dnsPrivate = ""; private List knownUrls = new ArrayList<>(); private List staticNodes = new ArrayList<>(); - private int maxMergeSize = 0; - private double changeThreshold = 0.0; + private int maxMergeSize = 5; + private double changeThreshold = 0.1; private String serverType = ""; private String accessKeyId = ""; private String accessKeySecret = ""; @@ -294,8 +294,7 @@ public static class DnsConfig { * since ConfigBeanFactory expects typed bean lists, not string lists. */ public static NodeConfig fromConfig(Config config) { - Config section = normalizeNonStandardKeys( - normalizeMaxMessageSizes(config).getConfig("node")); + Config section = normalizeNonStandardKeys(config.getConfig("node")); // Auto-bind all fields and sub-beans. ConfigBeanFactory fails fast with a // descriptive path on any `= null` value @@ -304,20 +303,28 @@ public static NodeConfig fromConfig(Config config) { // --- Legacy key fallbacks (backward compatibility) --- // node.maxActiveNodes (old) -> maxConnections (new) if (section.hasPath("maxActiveNodes")) { + logger.warn("Configuring [node.maxActiveNodes] is deprecated and will be removed in a future " + + "release. Please use [node.maxConnections] instead."); nc.maxConnections = section.getInt("maxActiveNodes"); if (section.hasPath("connectFactor")) { + logger.warn("Configuring [node.connectFactor] is deprecated and will be removed in a future " + + "release."); nc.minConnections = (int) (nc.maxConnections * section.getDouble("connectFactor")); } if (section.hasPath("activeConnectFactor")) { + logger.warn("Configuring [node.activeConnectFactor] is deprecated and will be removed in a " + + "future release."); nc.minActiveConnections = (int) (nc.maxConnections * section.getDouble("activeConnectFactor")); } } if (section.hasPath("maxActiveNodesWithSameIp")) { + logger.warn("Configuring [node.maxActiveNodesWithSameIp] is deprecated and will be removed " + + "in a future release. Please use [node.maxConnectionsWithSameIp] instead."); nc.maxConnectionsWithSameIp = section.getInt("maxActiveNodesWithSameIp"); } - // Legacy key fallback: node.fullNodeAllowShieldedTransaction -> allowShieldedTransactionApi. + // Legacy key fallback: node.allowShieldedTransactionApi wins fullNodeAllowShieldedTransaction if (section.hasPath("allowShieldedTransactionApi")) { nc.allowShieldedTransactionApi = section.getBoolean("allowShieldedTransactionApi"); @@ -351,6 +358,16 @@ private void postProcess() { rpc.thread = (Runtime.getRuntime().availableProcessors() + 1) / 2; } + if (rpc.maxConcurrentCallsPerConnection == 0) { + rpc.maxConcurrentCallsPerConnection = Integer.MAX_VALUE; + } + if (rpc.maxConnectionIdleInMillis == 0) { + rpc.maxConnectionIdleInMillis = Long.MAX_VALUE; + } + if (rpc.maxConnectionAgeInMillis == 0) { + rpc.maxConnectionAgeInMillis = Long.MAX_VALUE; + } + // validateSignThreadNum: 0 = auto-detect if (validateSignThreadNum == 0) { validateSignThreadNum = Runtime.getRuntime().availableProcessors(); @@ -374,6 +391,14 @@ private void postProcess() { syncFetchBatchNum = 100; } + // fetchBlock.timeout : clamp to [100, 1000] + if (fetchBlock.timeout > 1000) { + fetchBlock.timeout = 1000; + } + if (fetchBlock.timeout < 100) { + fetchBlock.timeout = 100; + } + // maxPendingBlockSize: clamp to [50, 2000] if (maxPendingBlockSize > 2000) { maxPendingBlockSize = 2000; @@ -425,6 +450,20 @@ private void postProcess() { if (maxTrxCacheSize < 2000) { maxTrxCacheSize = 2000; } + + // maxMessageSize: reject negative values + if (rpc.maxMessageSize < 0) { + throw new TronError("node.rpc.maxMessageSize must be non-negative, got: " + + rpc.maxMessageSize, PARAMETER_INIT); + } + if (http.maxMessageSize < 0) { + throw new TronError("node.http.maxMessageSize must be non-negative, got: " + + http.maxMessageSize, PARAMETER_INIT); + } + if (jsonrpc.maxMessageSize < 0) { + throw new TronError("node.jsonrpc.maxMessageSize must be non-negative, got: " + + jsonrpc.maxMessageSize, PARAMETER_INIT); + } } // =========================================================================== @@ -465,36 +504,4 @@ private static Config normalizeNonStandardKeys(Config section) { return section; } - /** - * Pre-normalize size paths so ConfigBeanFactory's primitive int/long binding succeeds - * for human-readable values like "4m" / "128MB". For each maxMessageSize key, parse - * via getMemorySize, validate non-negative and <= Integer.MAX_VALUE, and write the - * numeric byte value back into the Config tree. Validation errors propagate before - * bean creation so the failure points at the user-facing config path. - */ - private static Config normalizeMaxMessageSizes(Config config) { - String[] paths = { - "node.rpc.maxMessageSize", - "node.http.maxMessageSize", - "node.jsonrpc.maxMessageSize" - }; - Config result = config; - for (String path : paths) { - if (config.hasPath(path)) { - long bytes = parseMaxMessageSize(config, path); - result = result.withValue(path, ConfigValueFactory.fromAnyRef(bytes)); - } - } - return result; - } - - private static long parseMaxMessageSize(Config config, String key) { - long value = config.getMemorySize(key).toBytes(); - if (value < 0 || value > Integer.MAX_VALUE) { - throw new TronError(key + " must be non-negative and <= " - + Integer.MAX_VALUE + ", got: " + value, PARAMETER_INIT); - } - return value; - } - } diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index b17f04924df..549e280bbe1 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -68,19 +68,27 @@ storage { # } # setting can improve leveldb performance .... end, deprecated for arm - # Example per-database overrides: + # Per-database storage configuration overrides. Otherwise databases use global defaults and store + # data in "output-directory" or the directory specified by the "-d" / "--output-directory" option. + # Attention: name is a required field that must be set! + # The name and path properties take effect for both LevelDB and RocksDB storage engines, + # while additional properties (createIfMissing, paranoidChecks, compressionType, etc.) + # only take effect when using LevelDB. + # Example: + # properties = [ # { # name = "account", # path = "storage_directory_test", - # createIfMissing = true, + # createIfMissing = true, // deprecated for arm start # paranoidChecks = true, # verifyChecksums = true, # compressionType = 1, // compressed with snappy # blockSize = 4096, // 4 KB = 4 * 1024 B # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # maxOpenFiles = 100 - # } + # maxOpenFiles = 100 // deprecated for arm end + # }, + # ] properties = [] needToUpdateAsset = true @@ -134,12 +142,11 @@ node.discovery = { # } node.backup { - port = 10001 - priority = 0 - keepAliveInterval = 3000 + port = 10001 # UDP listen port; each member should have the same configuration + priority = 0 # Node priority; each member should use a different priority + keepAliveInterval = 3000 # Keep-alive interval (ms); each member should have the same configuration members = [ - # "ip", - # "ip" + # "ip", # Peer IP list, better to add at most one IP; must not contain this node's own IP ] } @@ -184,7 +191,12 @@ node { maxHttpConnectNumber = 50 minParticipationRate = 0 - # Whether to enable shielded transaction API + # WARNING: Some shielded transaction APIs require sending private keys as parameters. + # Calling these APIs on untrusted or remote nodes may leak your private keys. + # It is recommended to invoke them locally for development and testing. + # To opt in, set: allowShieldedTransactionApi = true + # Migration: the legacy key node.fullNodeAllowShieldedTransaction is still supported + # but deprecated; please migrate to node.allowShieldedTransactionApi. # allowShieldedTransactionApi = false # Whether to print config log at startup @@ -203,8 +215,7 @@ node { isOpenFullTcpDisconnect = false inactiveThreshold = 600 // seconds maxFastForwardNum = 4 - activeConnectFactor = 0.1 - connectFactor = 0.6 + # Legacy alias `maxActiveNodesWithSameIp` is still accepted from user config # (see NodeConfig alias-fallback) but is intentionally NOT defaulted here — # shipping it in reference.conf would always mask the modern `maxConnectionsWithSameIp`. @@ -239,8 +250,9 @@ node { PBFTEnable = true PBFTPort = 8092 - # Maximum HTTP request body size, default 4MB. Independent from rpc.maxMessageSize. - maxMessageSize = 4M + # Maximum HTTP request body size (default 4M). Setting to 0 rejects all non-empty request bodies. + # Independent from rpc.maxMessageSize. + maxMessageSize = 4194304 } rpc { @@ -255,19 +267,20 @@ node { thread = 0 # Maximum concurrent calls per incoming connection - # No limit on concurrent calls per connection - maxConcurrentCallsPerConnection = 2147483647 + # 0 means No limit on concurrent calls per connection + maxConcurrentCallsPerConnection = 0 # HTTP/2 flow control window (bytes), default 1MB flowControlWindow = 1048576 - # Connection idle timeout (ms). No limit by default. - maxConnectionIdleInMillis = 9223372036854775807 + # Connection idle timeout (ms). Connections idle longer than this are gracefully terminated. 0 = no limit + maxConnectionIdleInMillis = 0 - # Connection max age (ms). No limit by default. - maxConnectionAgeInMillis = 9223372036854775807 + # Connection max age (ms). 0 = no limit + maxConnectionAgeInMillis = 0 - # Maximum message size (bytes), default 4MB + # Maximum gRPC message size in bytes (default 4194304, ~4MB). + # Must be a non-negative integer. Setting to 0 rejects all non-empty messages. maxMessageSize = 4194304 # Maximum header list size (bytes), default 8192 @@ -320,7 +333,6 @@ node { blockCacheTimeout = 60 # TCP and transaction limits - receiveTcpMinDataLength = 2048 maxTransactionPendingSize = 2000 pendingTransactionTimeout = 60000 # total cached trx across handler queues + pending + rePush @@ -337,28 +349,50 @@ node { validContractProto.threads = 0 dns { + # DNS URLs to discover peers, format: tree://{pubkey}@{domain}. Default: empty. treeUrls = [ # "tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@main.trondisco.net", ] + + # Enable or disable DNS publish. Default: false. publish = false + # DNS domain to publish nodes, required if publish is true. dnsDomain = "" + # DNS private key used to publish, required if publish is true, hex string of length 64. dnsPrivate = "" + # Known DNS URLs to publish if publish is true, format: tree://{pubkey}@{domain}. Default: empty. knownUrls = [] + # Static nodes to publish on DNS, "ip:port". staticNodes = [] - maxMergeSize = 0 - changeThreshold = 0.0 + # Merge several nodes into a leaf of tree, should be 1~5. + maxMergeSize = 5 + # Only update DNS data when node change percent exceeds this threshold. + changeThreshold = 0.1 + # DNS server to publish, required if publish is true. Supported values: "aws", "aliyun". serverType = "" + # Access key ID of AWS or Aliyun API, required if publish is true. accessKeyId = "" + # Access key secret of AWS or Aliyun API, required if publish is true. accessKeySecret = "" + # Endpoint of Aliyun DNS server, required if serverType is "aliyun". aliyunDnsEndpoint = "" + # Region of AWS API (e.g. "us-east-1"), required if serverType is "aws". awsRegion = "" + # Host zone ID of AWS domain, required if serverType is "aws". awsHostZoneId = "" } # Open history query APIs on lite FullNode (may return null for some queries) openHistoryQueryWhenLiteFN = false + # Deprecated: these fields were used by the old connection-factor algorithm. + # They are still accepted from user config for backward compatibility but have no effect. + activeConnectFactor = 0.1 + connectFactor = 0.6 + jsonrpc { + # Note: Before release_4.8.1, if you turn on jsonrpc and run it for a while and then turn it off, + # you will not be able to get the data from eth_getLogs for that period of time. Default: false httpFullNodeEnable = false httpFullNodePort = 8545 httpSolidityEnable = false @@ -380,8 +414,8 @@ node { maxResponseSize = 26214400 # Allowed maximum number for newFilter, <=0 means no limit maxLogFilterNum = 20000 - # Maximum JSON-RPC request body size, default 4MB. Independent from rpc.maxMessageSize. - maxMessageSize = 4M + # Maximum JSON-RPC request body size in bytes (default 4194304, ~4MB). Independent from rpc.maxMessageSize. + maxMessageSize = 4194304 } # Disabled API list (works for http, rpc and pbft, not jsonrpc). Case insensitive. @@ -393,9 +427,18 @@ node { ## Rate limiter config rate.limiter = { - # Strategies: GlobalPreemptibleAdapter, QpsRateLimiterAdapter, IPQPSRateLimiterAdapter - # Default: QpsRateLimiterAdapter with qps=1000 - + # Each HTTP servlet and gRPC method can have its own rate-limit strategy. + # Three blocking strategies are available: + # GlobalPreemptibleAdapter – limits maximum concurrent requests globally. + # paramString = "permit=N" (N = max concurrent calls) + # QpsRateLimiterAdapter – limits average QPS across all callers. + # paramString = "qps=N" (N may be a decimal) + # IPQPSRateLimiterAdapter – limits average QPS per source IP. + # paramString = "qps=N" (N may be a decimal) + # If no strategy is configured for an endpoint, QpsRateLimiterAdapter with + # qps=1000 is applied automatically. + + # Per-servlet HTTP rate limits. component is the servlet class simple name. http = [ # { # component = "GetNowBlockServlet", @@ -414,6 +457,7 @@ rate.limiter = { # } ] + # Per-method gRPC rate limits. component is "package.ServiceName/MethodName". rpc = [ # { # component = "protocol.Wallet/GetBlockByLatestNum2", @@ -433,14 +477,20 @@ rate.limiter = { ] p2p = { - syncBlockChain = 3.0 - fetchInvData = 3.0 - disconnect = 1.0 + # QPS ceiling for individual P2P message types received from peers. + # Values are doubles; fractional QPS is allowed (e.g. 0.5 = one per 2 s). + syncBlockChain = 3.0 # SyncBlockChain handshake messages + fetchInvData = 3.0 # FetchInvData (block/tx fetch) messages + disconnect = 1.0 # Disconnect messages } + # Node-wide QPS ceiling across all HTTP + gRPC requests combined. global.qps = 50000 + # Per-source-IP QPS ceiling across all HTTP + gRPC requests from that IP. global.ip.qps = 10000 + # Default per-endpoint QPS limit applied to any endpoint with no explicit strategy. global.api.qps = 1000 + # true = reject over-limit requests immediately; false = queue and block the caller. apiNonBlocking = false } @@ -481,7 +531,21 @@ seed.node = { ] } +## Genesis block config +# WARNING: All nodes in the same network must have identical genesis.block settings. +# Any change here produces a different genesis block hash and creates an incompatible chain. genesis.block = { + # Pre-allocated accounts created at block 0, before any transactions are processed. + # Fields: + # accountName – human-readable label stored on-chain; must not be blank + # accountType – one of: Normal, AssetIssue, Contract + # address – Base58Check-encoded account address (T...) + # balance – initial balance in SUN (1 TRX = 1,000,000 SUN); stored as String + # to accommodate values that exceed Integer range + # Mainnet special accounts: + # Zion – holds the initial circulating supply (99,000,000,000 TRX = 99×10¹⁵ SUN) + # Sun – the founding account; starts at 0 + # Blackhole – receives burned TRX; initialized to Long.MIN_VALUE so it can only increase assets = [ { accountName = "Zion" @@ -503,6 +567,12 @@ genesis.block = { } ] + # Initial Super Representatives at block 0. + # Fields: + # address – Base58Check-encoded SR address (T...) + # url – SR's public URL (informational only, stored on-chain) + # voteCount – initial vote count; seeds SR ranking before any user votes are cast + # The 27 witnesses with the highest voteCount produce the first round of blocks. witnesses = [ { address: THKJYuUmMKKARNf7s2VT51g5uPY6KEqnat, @@ -641,12 +711,20 @@ genesis.block = { } ] + # Genesis block timestamp in milliseconds since Unix epoch. Must be >= 0. + # Stored as a numeric String to accommodate Long-range values. timestamp = "0" + # Hash of the genesis block's conceptual parent. This is a fixed sentinel value + # embedded in the genesis block header; changing it changes the genesis block hash + # and therefore the chain identity. parentHash = "0xe58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f" } # Optional. Used when the witness account has set witnessPermission. +# When it is not empty, the localWitnessAccountAddress represents the address of the witness account, +# and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. +# When it is empty,the localwitness is configured with the private key of the witness account. # localWitnessAccountAddress = localwitness = [ @@ -693,7 +771,16 @@ vm = { # Max retry time for executing transaction in estimating energy estimateEnergyMaxRetry = 3 - # Max TVM execution time (ms) for constant calls. 0 means no effect + # Max TVM execution time (ms) for constant calls — applies to + # triggerconstantcontract, triggersmartcontract dispatched to view/pure + # functions, estimateenergy, eth_call, eth_estimateGas, and any other RPC + # routed through the constant-call path. When set, must be a positive + # integer that fits VM deadline conversion and is used verbatim as the + # per-call deadline (no clamp against the network's maxCpuTimeOfOneTx). + # Omit the property entirely to keep the default behaviour of sharing the + # block-processing deadline. Migration note: if previously running --debug + # to extend constant calls, switch to this option (--debug also extends + # block-processing, which is unsafe; see issue #6266). Default: 0 (no effect). constantCallTimeoutMs = 0 } @@ -752,34 +839,38 @@ event.subscribe = { enable = false native = { - useNativeQueue = false - bindport = 5555 - sendqueuelength = 1000 + useNativeQueue = false // if true, use native message queue, else use event plugin. + bindport = 5555 // bind port + sendqueuelength = 1000 // max length of send queue } version = 0 + # Specify the starting block number to sync historical events. Only applicable when version = 1. + # After performing a full event sync, set this value to 0 or a negative number. startSyncBlockNum = 0 - path = "" - server = "" + path = "" // absolute path of plugin + server = "" // target server address to receive event triggers, "ip:port" + # dbname|username|password. To auto-create indexes on missing collections, append |2: + # dbname|username|password|2 (if collection exists, indexes must be created manually). dbconfig = "" contractParse = true topics = [ { - triggerName = "block" + triggerName = "block" // block trigger, the value can't be modified enable = false - topic = "block" - solidified = false + topic = "block" // plugin topic, the value could be modified + solidified = false // if set true, just need solidified block. Default: false }, { triggerName = "transaction" enable = false topic = "transaction" solidified = false - ethCompatible = false + ethCompatible = false // if set true, add transactionIndex, cumulativeEnergyUsed, preCumulativeLogCount, logList, energyUnitPrice. Default: false }, { - triggerName = "contractevent" + triggerName = "contractevent" // contractevent represents contractlog data decoded by the ABI. enable = false topic = "contractevent" }, @@ -787,11 +878,11 @@ event.subscribe = { triggerName = "contractlog" enable = false topic = "contractlog" - redundancy = false + redundancy = false // if set true, contractevent will also be regarded as contractlog }, { - triggerName = "solidity" - enable = true + triggerName = "solidity" // solidity block trigger (just block number and timestamp), the value can't be modified + enable = false topic = "solidity" }, { @@ -803,18 +894,18 @@ event.subscribe = { triggerName = "soliditylog" enable = false topic = "soliditylog" - redundancy = false + redundancy = false // if set true, solidityevent will also be regarded as soliditylog } ] filter = { - fromblock = "" - toblock = "" + fromblock = "" // "", "earliest", or a specific block number as the beginning of the queried range + toblock = "" // "", "latest", or a specific block number as end of the queried range contractAddress = [ - "" + "" // contract address to subscribe; "" means any contract address ] contractTopic = [ - "" + "" // contract topic to subscribe; "" means any contract topic ] } } diff --git a/docs/configuration-conventions.md b/docs/configuration-conventions.md new file mode 100644 index 00000000000..c9265b9544e --- /dev/null +++ b/docs/configuration-conventions.md @@ -0,0 +1,226 @@ +# HOCON Configuration Conventions for Developers + +This document covers the rules and patterns that developers must follow when adding or modifying configuration parameters in java-tron. Violations cause silent misreads, startup failures, or hard-to-diagnose defaults being applied instead of user-supplied values. + +## Configuration Parameter vs. Constant: Which One to Use? + +Before writing any code, decide whether the value belongs in a config file or in source code as a constant. Getting this wrong creates dead configuration surface (parameters that exist but are never tuned) or inflexibility (values that should be adjustable but aren't). + +### Use a configuration parameter when + +- **Different deployments legitimately need different values.** Port numbers, peer lists, storage paths, block-production timeouts, and rate limits vary by environment (mainnet / testnet / private chain) or by hardware capacity. +- **Operators may need to tune the value without rebuilding.** Examples: thread pool sizes, connection limits, QPS caps. +- **The value is an on/off feature flag with production-safe semantics.** The flag must be safe to flip while the rest of the system is unchanged (e.g. `node.rpc.reflectionService`, `vm.estimateEnergy`). +- **The default differs across deployment scenarios.** If the mainnet default and the private-chain default are different, it belongs in config so each can override. + +### Use a constant when + +- **No operator would ever need to change it.** Protocol-level numbers (address prefix bytes, transaction size ceilings, energy unit ratios) are part of the chain specification — changing them causes a fork. +- **The value is a technical limit determined by the implementation, not the deployment.** Jackson `StreamReadConstraints` (`MAX_NESTING_DEPTH`, `MAX_TOKEN_COUNT`) guard against malformed input; no legitimate request comes close to the limit and no operator tunes it. +- **The "configurability" is an illusion.** If the value is captured in a `static final` field at class-load time (before config is applied), a config key is misleading — it appears tunable but changes are silently ignored. Convert to a constant and document why. +- **The value is derived from other constants or from the Java runtime.** Use `Runtime.getRuntime().availableProcessors()` or arithmetic on existing constants; don't push the formula into a config file. +- **No code path reads the value after assignment.** A parameter that exists in `reference.conf` and propagates through `NodeConfig → Args → CommonParameter` but is never consumed by business logic is dead weight. Delete it entirely (see `receiveTcpMinDataLength` as a past example). + +### The warning signs of a misplaced parameter + +| Symptom | Likely problem | +|---------|---------------| +| Parameter exists in `reference.conf` but `grep` finds no call site beyond the binding chain | Dead parameter — delete it | +| Value is read from a `static final` field initialized before `Args.setParam()` | Config change is silently ignored — convert to constant | +| Operator sets the value and nothing changes | Same as above, or value is clamped away in `postProcess()` | +| Parameter controls something that would cause a network fork if mismatched across nodes | Must be a constant, not configurable | +| Parameter has been at its default value in every known deployment for over a year | Candidate for removal or promotion to constant | + +## How Config Keys Bind to Java Fields + +java-tron uses [Typesafe Config](https://github.com/lightbend/config)'s `ConfigBeanFactory` to map a HOCON section to a Java bean automatically. The mapping algorithm is: + +1. For each field `fooBar` in the bean, `ConfigBeanFactory` looks for a HOCON key named `fooBar`. +2. The bean class must expose a public setter (`setFooBar`) — in practice this is provided by Lombok `@Setter`. +3. If the key is absent from the config, the field keeps its Java default value (the one assigned in the field declaration). +4. If the key is present but the type does not match, binding fails with a `ConfigException` at startup. + +The binding entry point for each top-level section looks like: + +```java +// "node" section → NodeConfig bean +Config section = config.getConfig("node"); +NodeConfig nc = ConfigBeanFactory.create(section, NodeConfig.class); +``` + +## Key Naming: Use camelCase + +**All keys in `reference.conf` and `config.conf` must use standard camelCase.** + +`ConfigBeanFactory` derives the expected key name from the Java setter via the JavaBean Introspector: `setFooBar` → property name `fooBar` → expected HOCON key `fooBar`. If the key in the config file uses a different casing, the binding silently skips it and the field keeps its Java default. + +```hocon +# Correct +node { + maxConnections = 30 + syncFetchBatchNum = 2000 +} + +# Wrong — ConfigBeanFactory cannot find these +node { + MaxConnections = 30 # PascalCase → ignored + sync_fetch_batch_num = 2000 # snake_case → ignored + max-connections = 30 # kebab-case → ignored +} +``` + +### The PBFT Exception + +Two legacy keys under `committee` (`allowPBFT`, `pBFTExpireNum`) and the HTTP/RPC fields (`PBFTEnable`, `PBFTPort`) were introduced with non-standard casing before this rule was established. They are retained as-is in the config files for backward compatibility. **Do not model new keys after them.** + +For `allowPBFT` and `pBFTExpireNum`, `CommitteeConfig.normalizeNonStandardKeys()` renames them to proper camelCase (`allowPbft`, `pbftExpireNum`) before handing the section to `ConfigBeanFactory`. If you ever need to accept a non-standard key from users while binding to a standard field, follow this same pattern. + +### The `is` Prefix Exception + +A HOCON key named `isOpenFullTcpDisconnect` produces the setter `setIsOpenFullTcpDisconnect`, but the JavaBean Introspector derives the property name as `openFullTcpDisconnect` (stripping `is`), so `ConfigBeanFactory` looks for key `openFullTcpDisconnect`. `NodeConfig.normalizeNonStandardKeys()` renames the key at read time for backward compatibility. **Do not add new keys with an `is` prefix.** + +## Nesting Depth + +The CI gate enforces a hard ceiling of **5 levels** (the historical maximum in `reference.conf`). New parameters should stay within **3 levels** from the top-level section. The gap between 3 and 5 is reserved for legacy paths that already exist — it is not a license to add new deep keys. + +``` +level 1: node { ... } +level 2: node { rpc { ... } } +level 3: node { rpc { flowControl { ... } } } ← limit for new keys +level 4+: node { rpc { flowControl { window { ... } } } } ← legacy only; do not add new keys here +level 6+: rejected by CI gate unconditionally +``` + +Each level of nesting requires a corresponding inner static bean class. If you find yourself going beyond 3 levels deep, consider flattening by moving the leaf keys up one level or using a longer camelCase key at level 2. + +## Configuration Loading Order + +java-tron loads configuration in two layers at startup: + +``` +Priority (highest wins): + 1. User config file — passed via -c; replaces the bundled config.conf entirely + 2. reference.conf — always loaded from inside the jar; provides defaults for every key +``` + +When a user passes `-c /path/to/node.conf`, the bundled `config.conf` is **not loaded at all** — it is completely replaced by the user's file. `reference.conf` is the only built-in file that is guaranteed to be read in every deployment. + +When `-c` is omitted (development or quick-start), the bundled `config.conf` fills the same role a user file would: it overrides `reference.conf` defaults for the keys it declares. + +The practical consequence for developers: **the default value you put in `reference.conf` is the value every production node uses.** The bundled `config.conf` only matters for users who start the node without `-c`. + +## Adding a New Parameter: Checklist + +When adding a configuration parameter, all four steps are required in the same commit. + +### Step 1 — Add the key to `reference.conf` with its default value + +`reference.conf` (in `common/src/main/resources/`) must contain every key the code reads. This is the single source of truth for defaults. Add a brief inline comment explaining the key's purpose and valid range. + +```hocon +node { + # Maximum number of transaction verifier threads. 0 = auto (availableProcessors). + myNewOption = 0 +} +``` + +### Step 2 — Add the field to the corresponding bean class + +Add a field whose name **exactly matches** the HOCON key, with the same default value as `reference.conf`. If the field is in a sub-bean, ensure the sub-bean is mapped correctly. + +```java +// NodeConfig.java +private int myNewOption = 0; // 0 = auto +``` + +Lombok `@Getter` and `@Setter` on the class provide the accessor methods that `ConfigBeanFactory` needs. Do not write them by hand. + +### Step 3 — Add clamping / validation in `postProcess()` if needed + +Every bean's `postProcess()` (called from `fromConfig()` after binding) is where out-of-range values are clamped and cross-field invariants are enforced. Do not add defensive checks scattered through the rest of the codebase. + +```java +// in NodeConfig.postProcess() +if (myNewOption == 0) { + myNewOption = Runtime.getRuntime().availableProcessors(); +} +if (myNewOption > 64) { + myNewOption = 64; +} +``` + +### Step 4 — Add the key to `config.conf` only if the default is intentionally different + +`config.conf` (in `framework/src/main/resources/`) is the sample user config shipped with the distribution. Only add your new key there if the value users should start with differs from the `reference.conf` default, or if the key needs a visible comment for users. + +Remember: in any real deployment the user passes `-c` and the bundled `config.conf` is bypassed entirely (see [Configuration Loading Order](#configuration-loading-order)). `reference.conf` is where your default actually takes effect — make sure it is safe and correct before touching `config.conf`. + +## Field Types and HOCON Value Types + +| Java field type | HOCON value | Notes | +|-------------------|-------------|-------| +| `boolean` | `true` / `false` | | +| `int` / `long` | numeric | Must be a plain integer; human-readable sizes (`4m`, `128MB`) are not supported | +| `double` | numeric | | +| `String` | `"value"` | Null HOCON values must be normalized to `""` before binding (see `normalizeNonStandardKeys`) | +| `List` | `["a", "b"]` | Must be read manually; `ConfigBeanFactory` does not handle `List` | +| Inner bean | `{ key = val }` | The Java field type must be the inner static class | + +### List Fields + +`ConfigBeanFactory` handles `List` but not `List`. Read string-list fields manually after `ConfigBeanFactory.create()`: + +```java +NodeConfig nc = ConfigBeanFactory.create(section, NodeConfig.class); +nc.active = section.getStringList("active"); +``` + +## Backward Compatibility and Legacy Keys + +When renaming a key, keep reading the old key as a fallback for at least one major release: + +```java +// fromConfig() — after ConfigBeanFactory binding +if (section.hasPath("oldKeyName")) { + nc.newFieldName = section.getInt("oldKeyName"); + logger.warn("Config key [section.oldKeyName] is deprecated; use [section.newKeyName]."); +} +``` + +Never remove the old key from this fallback read without a deprecation period and a release note. + +## Optional Keys (Not in `reference.conf`) + +Most keys should be in `reference.conf`. Use optional keys (absent from `reference.conf`, only read if present) sparingly — only for parameters where the presence/absence itself carries meaning. Read them with `hasPath()` guards and annotate the Java field with `@Setter(lombok.AccessLevel.NONE)` to prevent `ConfigBeanFactory` from requiring the key: + +```java +@Setter(lombok.AccessLevel.NONE) +private String shutdownBlockTime = ""; // "" = not set + +// in fromConfig(), after ConfigBeanFactory.create(): +nc.shutdownBlockTime = section.hasPath("shutdown.BlockTime") + ? section.getString("shutdown.BlockTime") : ""; +``` + +## Key Naming Conventions Summary + +| Rule | Good | Bad | +|------|------|-----| +| Standard camelCase | `maxConnections` | `MaxConnections`, `max_connections`, `max-connections` | +| No `is` prefix | `openFullTcpDisconnect` | `isOpenFullTcpDisconnect` | +| No all-caps acronym prefix | `pbftExpireNum`, `pBFTPort`* | `PBFTExpireNum` | +| New keys: nesting ≤ 3 levels | `node.rpc.maxMessageSize` | `node.rpc.limits.size.max` | +| Java field name matches HOCON key exactly | field `maxConnections` ↔ key `maxConnections` | field `maxConns` ↔ key `maxConnections` | + +\* `PBFTEnable` / `PBFTPort` are legacy exceptions; do not model new keys after them. + +## Where to Find Existing Patterns + +| Pattern | Reference location | +|---------|-------------------| +| Standard flat scalar binding | `VmConfig.java`, `BlockConfig.java` | +| Sub-bean nesting | `NodeConfig.HttpConfig`, `NodeConfig.RpcConfig` | +| Legacy key fallback | `NodeConfig.fromConfig()` (`maxActiveNodes`, `maxActiveNodesWithSameIp`) | +| Non-standard key normalization | `CommitteeConfig.normalizeNonStandardKeys()`, `NodeConfig.normalizeNonStandardKeys()` | +| Optional PascalCase keys | `NodeConfig.fromConfig()` (`shutdown.BlockTime/Height/Count`) | +| `postProcess()` clamping | `NodeConfig.postProcess()`, `CommitteeConfig.postProcess()` | diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 00000000000..28b53b1970c --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,259 @@ +# Node Configuration Guide + +This guide explains the two-layer configuration system used by java-tron and walks through the most common customizations a node operator needs. + +## How Configuration Files Work Together + +java-tron uses [Typesafe Config](https://github.com/lightbend/config) and applies configuration in priority order at startup: + +| File | Location | Purpose | +|------|----------|---------| +| `reference.conf` | Bundled inside the jar (`common` module) | Declares every parameter with its default value | +| Bundled `config.conf` | Bundled inside the jar (`framework` module) | Shipped template; active only when `-c` is omitted | +| Your config file (e.g. `node.conf`) | Operator-supplied, passed via `-c` | Overrides values that differ from defaults; replaces the bundled `config.conf` entirely | + +**Loading priority:** values in your config file always win. Any parameter your file omits is automatically filled in from `reference.conf`. You never need to copy the entire `reference.conf` into your own file — only include the parameters you actually want to change. + +``` +startup resolution order (highest wins): + 1. your config file (passed with -c; replaces bundled config.conf) + 2. bundled config.conf (only when -c is omitted) + 3. reference.conf (always loaded; fallback for every key) +``` + +`reference.conf` is the authoritative source of truth for every parameter name and its default. When in doubt, consult that file to see what a parameter does and what value the node will use if you leave it out. + +## Starting a Node with a Config File + +```bash +# Using the distribution script +java-tron-1.0.0/bin/FullNode -c /path/to/node.conf + +# Using the jar directly +java -jar FullNode.jar -c /path/to/node.conf + +# SR (Super Representative) mode +java-tron-1.0.0/bin/FullNode -c /path/to/node.conf -w +``` + +If `-c` is omitted, the node loads the `config.conf` bundled inside the jar (the same file shipped with the distribution) merged with `reference.conf` as fallback. The bundled file already enables discovery/persist for mainnet operation. For production, copy it out, edit, and pass the edited copy via `-c` to make your configuration visible to operators. + +## Minimal Config File + +Your config file only needs to contain what you want to change. The following is sufficient for a mainnet full node: + +```hocon +node.discovery = { + enable = true + persist = true +} + +node { + listen.port = 18888 + minParticipationRate = 15 + p2p.version = 11111 # mainnet +} + +seed.node.ip.list = [ + "3.225.171.164:18888", + "52.8.46.215:18888", + # ... (see reference.conf for the full seed list) +] +``` + +## Common Configuration Sections + +### Network and P2P (`node`, `node.discovery`, `seed.node`) + +```hocon +node.discovery = { + enable = true # join the peer-discovery network + persist = true # save discovered peers across restarts +} + +node { + listen.port = 18888 # TCP port for peer connections + maxConnections = 30 # maximum peer connections + minConnections = 8 # minimum peer connections to maintain + minParticipationRate = 15 # minimum % of active witnesses before producing blocks + + p2p { + version = 11111 # Mainnet:11111 Nile:201910292 Shasta:1 + } +} + +seed.node.ip.list = [ + "3.225.171.164:18888", + # add more entries as needed +] +``` + +### HTTP and gRPC APIs (`node.http`, `node.rpc`) + +```hocon +node { + http { + fullNodeEnable = true + fullNodePort = 8090 + solidityEnable = true + solidityPort = 8091 + } + + rpc { + enable = true + port = 50051 + solidityEnable = true + solidityPort = 50061 + # Maximum concurrent calls per connection. 0 = no limit. + maxConcurrentCallsPerConnection = 0 + # Idle connection timeout (ms). 0 = no limit. + maxConnectionIdleInMillis = 0 + # Minimum active connections required before broadcasting transactions. + minEffectiveConnection = 1 + } +} +``` + +To disable an API endpoint that you do not want to expose publicly, set its `Enable` flag to `false` or add endpoints to `node.disabledApi`: + +```hocon +node.disabledApi = [ + "getaccount", + "getnowblock2" +] +``` + +### Storage Engine (`storage`) + +```hocon +storage { + db.engine = "LEVELDB" # "LEVELDB" or "ROCKSDB"; ARM64 requires "ROCKSDB" + db.sync = false # set true for maximum durability (slower writes) + db.directory = "database" +} +``` + +To override the storage path for individual databases: + +```hocon +storage.properties = [ + { + name = "account", + path = "/data/tron/account-db" + } +] +``` + +### Block Production (Super Representatives) + +```hocon +# Plain private key (use localwitnesskeystore for production) +localwitness = [ + "your-private-key-hex" +] + +# Recommended: keystore file +# localwitnesskeystore = [ +# "/path/to/localwitnesskeystore.json" +# ] + +# Required when the witness account has delegated block-signing to a separate key +# localWitnessAccountAddress = "T..." +``` + +### JSON-RPC (Ethereum-compatible, `node.jsonrpc`) + +```hocon +node.jsonrpc { + httpFullNodeEnable = true + httpFullNodePort = 8545 + maxBlockRange = 5000 # max block range for eth_getLogs + maxResponseSize = 26214400 # 25 MB +} +``` + +### Event Subscription (`event.subscribe`) + +```hocon +event.subscribe = { + enable = true + native { + useNativeQueue = true + bindport = 5555 + sendqueuelength = 1000 + } + topics = [ + { triggerName = "block", enable = true, topic = "block" }, + { triggerName = "transaction", enable = true, topic = "transaction" }, + { triggerName = "solidity", enable = true, topic = "solidity" } + ] +} +``` + +### Rate Limiting (`rate.limiter`) + +```hocon +rate.limiter = { + # Available strategies: + # GlobalPreemptibleAdapter — semaphore-based, paramString = "permit=N" + # QpsRateLimiterAdapter — node-wide QPS cap, paramString = "qps=N" + # IPQPSRateLimiterAdapter — per-IP QPS cap, paramString = "qps=N" + + http = [ + { + component = "GetAccountServlet", + strategy = "IPQPSRateLimiterAdapter", + paramString = "qps=10" + } + ] + + global.qps = 50000 + global.ip.qps = 10000 +} +``` + +### Dynamic Config Reload (`node.dynamicConfig`) + +When enabled, the node re-reads your config file periodically without restarting: + +```hocon +node.dynamicConfig = { + enable = true + checkInterval = 600 # seconds between checks +} +``` + +Not all parameters support hot-reload. Parameters that affect node identity, genesis block, or database layout require a full restart. + +## Parameters You Should Not Change + +| Parameter | Reason | +|-----------|--------| +| `crypto.engine` | Changing the key-derivation algorithm will fork the node | +| `genesis.block.*` | Must be identical on every node in the network | +| `committee.*` | Controlled by on-chain governance proposals; manual overrides are for private chains only | +| `node.p2p.version` | Must match the network (11111 for mainnet) | +| `enery.limit.block.num` | Intentional typo preserved for backward compatibility; do not rename | + +## Applying a Config Change + +1. Edit your config file — only add or change the keys you need. +2. If `node.dynamicConfig.enable = true`, wait up to `checkInterval` seconds; the node picks up the change automatically. +3. Otherwise, restart the node: `kill ` then relaunch with the same `-c` flag. +4. Check startup logs for a `[config]` line confirming the file was loaded and watch for any `ERROR` lines about unknown or invalid keys. + +## Viewing Effective Configuration + +At startup, the node unconditionally logs a summary of key parameters under `Net config`, `Backup config`, `Code version`, `DB config`, and `shutDown config` headers (see `Args.logConfig()` for the exact fields). For parameters not in this summary, you must inspect runtime behavior or consult `reference.conf` directly — the full merged configuration is never dumped. + +Note: `node.openPrintLog` is a separate flag that controls runtime verbosity of P2P/inventory/pending-tx logs, not startup config logging. + +## Full Reference + +Every parameter with its default value and an inline comment is documented in: + +``` +common/src/main/resources/reference.conf +``` + +When you need the authoritative default for a parameter or want to understand what a key does, consult that file directly. diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 74e9001177f..0bca242606e 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -575,14 +575,7 @@ private static void applyNodeConfig(NodeConfig nc) { // ---- Flat scalar fields ---- PARAMETER.nodeEffectiveCheckEnable = nc.isEffectiveCheckEnable(); - // fetchBlock.timeout — range check [100, 1000], default 500 - int fetchTimeout = nc.getFetchBlockTimeout(); - if (fetchTimeout > 1000) { - fetchTimeout = 1000; - } else if (fetchTimeout < 100) { - fetchTimeout = 100; - } - PARAMETER.fetchBlockTimeout = fetchTimeout; + PARAMETER.fetchBlockTimeout = nc.getFetchBlockTimeout(); PARAMETER.maxConnections = nc.getMaxConnections(); PARAMETER.minConnections = nc.getMinConnections(); @@ -606,7 +599,6 @@ private static void applyNodeConfig(NodeConfig nc) { PARAMETER.validateSignThreadNum = nc.getValidateSignThreadNum(); PARAMETER.walletExtensionApi = nc.isWalletExtensionApi(); - PARAMETER.receiveTcpMinDataLength = nc.getReceiveTcpMinDataLength(); PARAMETER.isOpenFullTcpDisconnect = nc.isOpenFullTcpDisconnect(); PARAMETER.nodeDetectEnable = nc.isNodeDetectEnable(); @@ -622,7 +614,7 @@ private static void applyNodeConfig(NodeConfig nc) { PARAMETER.shieldedTransInPendingMaxCounts = nc.getShieldedTransInPendingMaxCounts(); PARAMETER.agreeNodeCount = nc.getAgreeNodeCount(); - PARAMETER.setOpenHistoryQueryWhenLiteFN(nc.isOpenHistoryQueryWhenLiteFN()); + PARAMETER.openHistoryQueryWhenLiteFN = nc.isOpenHistoryQueryWhenLiteFN(); PARAMETER.nodeMetricsEnable = nc.isMetricsEnable(); PARAMETER.openPrintLog = nc.isOpenPrintLog(); PARAMETER.openTransactionSort = nc.isOpenTransactionSort(); diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index 0686890f030..1176dd46311 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -4,7 +4,6 @@ net { } storage { - # Directory for storing persistent data db.engine = "LEVELDB", // deprecated for arm, because arm only support "ROCKSDB". db.sync = false, db.directory = "database", @@ -12,57 +11,18 @@ storage { # Whether to write transaction result in transactionRetStore transHistory.switch = "on", - # setting can improve leveldb performance .... start, deprecated for arm - # node: if this will increase process fds,you may be check your ulimit if 'too many open files' error occurs - # see https://github.com/tronprotocol/tips/blob/master/tip-343.md for detail - # if you find block sync has lower performance, you can try this settings - # default = { - # maxOpenFiles = 100 - # } - # defaultM = { - # maxOpenFiles = 500 - # } - # defaultL = { - # maxOpenFiles = 1000 - # } - # setting can improve leveldb performance .... end, deprecated for arm - - # You can customize the configuration for each database. Otherwise, the database settings will use - # their defaults, and data will be stored in the "output-directory" or in the directory specified - # by the "-d" or "--output-directory" option. Attention: name is a required field that must be set! - # In this configuration, the name and path properties take effect for both LevelDB and RocksDB storage engines, - # while the additional properties (such as createIfMissing, paranoidChecks, compressionType, etc.) only take effect when using LevelDB. + # Per-database storage path overrides (name is required; see reference.conf for full property list). properties = [ - # { - # name = "account", - # path = "storage_directory_test", - # createIfMissing = true, // deprecated for arm start - # paranoidChecks = true, - # verifyChecksums = true, - # compressionType = 1, // compressed with snappy - # blockSize = 4096, // 4 KB = 4 * 1024 B - # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # maxOpenFiles = 100 // deprecated for arm end - # }, - # { - # name = "account-index", - # path = "storage_directory_test", - # createIfMissing = true, - # paranoidChecks = true, - # verifyChecksums = true, - # compressionType = 1, // compressed with snappy - # blockSize = 4096, // 4 KB = 4 * 1024 B - # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # maxOpenFiles = 100 - # }, + # { + # name = "account", + # path = "storage_directory_test", + # maxOpenFiles = 100 + # }, ] needToUpdateAsset = true - # dbsettings is needed when using rocksdb as the storage implement (db.engine="ROCKSDB"). - # we'd strongly recommend that do not modify it unless you know every item's meaning clearly. + # RocksDB settings (only used when db.engine = "ROCKSDB"). See reference.conf for details. dbSettings = { levelNumber = 7 # compactThreads = 32 @@ -77,24 +37,8 @@ storage { balance.history.lookup = false - # checkpoint.version = 2 - # checkpoint.sync = true - - # the estimated number of block transactions (default 1000, min 100, max 10000). - # so the total number of cached transactions is 65536 * txCache.estimatedTransactions - # txCache.estimatedTransactions = 1000 - - # if true, transaction cache initialization will be faster. Default: false + # If true, transaction cache initialization will be faster. Default: false txCache.initOptimization = true - - # The number of blocks flushed to db in each batch during node syncing. Default: 1 - # snapshot.maxFlushCount = 1 - - # data root setting, for check data, currently, only reward-vi is used. - # merkleRoot = { - # reward-vi = 9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8 // main-net, Sha256Hash, hexString - # } - } node.discovery = { @@ -102,24 +46,17 @@ node.discovery = { persist = true } -# custom stop condition -#node.shutdown = { -# BlockTime = "54 59 08 * * ?" # if block header time in persistent db matched. -# BlockHeight = 33350800 # if block header height in persistent db matched. -# BlockCount = 12 # block sync count after node start. -#} +# Custom stop condition +# node.shutdown = { +# BlockTime = "54 59 08 * * ?" # if block header time in persistent db matched. +# BlockHeight = 33350800 # if block header height in persistent db matched. +# BlockCount = 12 # block sync count after node start. +# } node.backup { - # udp listen port, each member should have the same configuration port = 10001 - - # my priority, each member should use different priority priority = 8 - - # time interval to send keepAlive message, each member should have the same configuration keepAliveInterval = 3000 - - # peer's ip list, can't contain mine members = [ # "ip", # "ip" @@ -132,17 +69,13 @@ crypto { } node.metrics = { - # prometheus metrics prometheus { enable = false port = 9527 } - } node { - # trust node for solidity node - # trustNode = "ip:port" trustNode = "127.0.0.1:50051" # expose extension api to public or not @@ -151,50 +84,14 @@ node { listen.port = 18888 fetchBlock.timeout = 200 - # syncFetchBatchNum = 2000 - - # Maximum number of blocks allowed in-flight (requested but not yet processed). - # Throttles block download to reduce memory pressure during sync. - # Range: [50, 2000], default: 500 - # maxPendingBlockSize = 500 - - # Maximum total number of cached transactions (handler queues + pending + rePush). - # When exceeded, the node stops accepting TRX INV messages from peers. - # maxTrxCacheSize = 50000 - - # Number of validate sign thread, default availableProcessors - # validateSignThreadNum = 16 maxConnections = 30 - minConnections = 8 - minActiveConnections = 3 - maxConnectionsWithSameIp = 2 - maxHttpConnectNumber = 50 - minParticipationRate = 15 - # WARNING: Some shielded transaction APIs require sending private keys as parameters. - # Calling these APIs on untrusted or remote nodes may leak your private keys. - # It is recommended to invoke them locally for development and testing. - # To opt in, set: allowShieldedTransactionApi = true - # Migration: the legacy key node.fullNodeAllowShieldedTransaction is still supported - # but deprecated; please migrate to node.allowShieldedTransactionApi. - # allowShieldedTransactionApi = false - - # openPrintLog = true - - # If set to true, SR packs transactions into a block in descending order of fee; otherwise, it packs - # them based on their receive timestamp. Default: false - # openTransactionSort = false - - # The threshold for the number of broadcast transactions received from each peer every second, - # transactions exceeding this threshold will be discarded - # maxTps = 1000 - isOpenFullTcpDisconnect = false inactiveThreshold = 600 //seconds @@ -204,14 +101,12 @@ node { active = [ # Active establish connection in any case - # Sample entries: # "ip:port", # "ip:port" ] passive = [ # Passive accept connection in any case - # Sample entries: # "ip:port", # "ip:port" ] @@ -228,12 +123,6 @@ node { solidityPort = 8091 PBFTEnable = true PBFTPort = 8092 - - # The maximum request body size for HTTP API, default 4M (4194304 bytes). - # Supports human-readable sizes: 4m, 4MB, 4194304. - # Must be non-negative and <= 2147483647 (Integer.MAX_VALUE, ~2 GiB). - # Setting to 0 rejects all non-empty request bodies (not "unlimited"). - # maxMessageSize = 4m } rpc { @@ -244,223 +133,51 @@ node { PBFTEnable = true PBFTPort = 50071 - # Number of gRPC thread, default availableProcessors / 2 - # thread = 16 - - # The maximum number of concurrent calls permitted for each incoming connection - # maxConcurrentCallsPerConnection = - - # The HTTP/2 flow control window, default 1MB - # flowControlWindow = - - # Connection being idle for longer than which will be gracefully terminated maxConnectionIdleInMillis = 60000 - - # Connection lasting longer than which will be gracefully terminated - # maxConnectionAgeInMillis = - - # The maximum message size allowed to be received on the server, default 4M (4194304 bytes). - # Supports human-readable sizes: 4m, 4MB, 4194304. - # Must be non-negative and <= 2147483647 (Integer.MAX_VALUE, ~2 GiB). - # Setting to 0 rejects all non-empty request bodies (not "unlimited"). - # maxMessageSize = 4m - - # The maximum size of header list allowed to be received, default 8192 - # maxHeaderListSize = - - # The number of RST_STREAM frames allowed to be sent per connection per period for grpc, by default there is no limit. - # maxRstStream = - - # The number of seconds per period for grpc - # secondsPerWindow = - - # Transactions can only be broadcast if the number of effective connections is reached. minEffectiveConnection = 1 - # The switch of the reflection service, effective for all gRPC services, used for grpcurl tool. Default: false + # The switch of the reflection service for grpcurl tool. Default: false reflectionService = false } - # number of solidity thread in the FullNode. - # If accessing solidity rpc and http interface timeout, could increase the number of threads, - # The default value is the number of cpu cores of the machine. - # solidity.threads = 8 - - # Limits the maximum percentage (default 75%) of producing block interval - # to provide sufficient time to perform other operations e.g. broadcast block - # blockProducedTimeOut = 75 - - # Limits the maximum number (default 700) of transaction from network layer - # netMaxTrxPerSecond = 700 - - # Whether to enable the node detection function. Default: false - # nodeDetectEnable = false - - # use your ipv6 address for node discovery and tcp connection. Default: false - # enableIpv6 = false - - # if your node's highest block num is below than all your pees', try to acquire new connection. Default: false - # effectiveCheckEnable = false - - # Dynamic loading configuration function, disabled by default dynamicConfig = { # enable = false - # checkInterval = 600 // Check interval of Configuration file's change, default is 600 seconds + # checkInterval = 600 } - # Whether to continue broadcast transactions after at least maxUnsolidifiedBlocks are not solidified. Default: false - # unsolidifiedBlockCheck = false - # maxUnsolidifiedBlocks = 54 - dns { - # dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty treeUrls = [ #"tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@main.trondisco.net", ] - - # enable or disable dns publish. Default: false - # publish = false - - # dns domain to publish nodes, required if publish is true - # dnsDomain = "nodes1.example.org" - - # dns private key used to publish, required if publish is true, hex string of length 64 - # dnsPrivate = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" - - # known dns urls to publish if publish is true, url format tree://{pubkey}@{domain}, default empty - # knownUrls = [ - #"tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes2.example.org", - # ] - - # staticNodes = [ - # static nodes to published on dns - # Sample entries: - # "ip:port", - # "ip:port" - # ] - - # merge several nodes into a leaf of tree, should be 1~5 - # maxMergeSize = 5 - - # only nodes change percent is bigger then the threshold, we update data on dns - # changeThreshold = 0.1 - - # dns server to publish, required if publish is true, only aws or aliyun is support - # serverType = "aws" - - # access key id of aws or aliyun api, required if publish is true, string - # accessKeyId = "your-key-id" - - # access key secret of aws or aliyun api, required if publish is true, string - # accessKeySecret = "your-key-secret" - - # if publish is true and serverType is aliyun, it's endpoint of aws dns server, string - # aliyunDnsEndpoint = "alidns.aliyuncs.com" - - # if publish is true and serverType is aws, it's region of aws api, such as "eu-south-1", string - # awsRegion = "us-east-1" - - # if publish is true and server-type is aws, it's host zone id of aws's domain, string - # awsHostZoneId = "your-host-zone-id" } - # open the history query APIs(http&GRPC) when node is a lite FullNode, - # like {getBlockByNum, getBlockByID, getTransactionByID...}. Default: false. - # note: above APIs may return null even if blocks and transactions actually are on the blockchain - # when opening on a lite FullNode. only open it if the consequences being clearly known - # openHistoryQueryWhenLiteFN = false - jsonrpc { - # The maximum request body size for JSON-RPC API, default 4M (4194304 bytes). - # Supports human-readable sizes: 4m, 4MB, 4194304. - # Must be non-negative and <= 2147483647 (Integer.MAX_VALUE, ~2 GiB). - # Setting to 0 rejects all non-empty request bodies (not "unlimited"). - # maxMessageSize = 4m - - # Note: Before release_4.8.1, if you turn on jsonrpc and run it for a while and then turn it off, - # you will not be able to get the data from eth_getLogs for that period of time. Default: false - # httpFullNodeEnable = false - # httpFullNodePort = 8545 - # httpSolidityEnable = false - # httpSolidityPort = 8555 - # httpPBFTEnable = false - # httpPBFTPort = 8565 - - # The maximum blocks range to retrieve logs for eth_getLogs, default: 5000, <=0 means no limit + httpFullNodeEnable = false + httpFullNodePort = 8545 + maxBlockRange = 5000 - # Allowed max address count in filter request, default: 1000, <=0 means no limit maxAddressSize = 1000 - # The maximum number of allowed topics within a topic criteria, default: 1000, <=0 means no limit maxSubTopics = 1000 - # Allowed maximum number for blockFilter, default: 50000, <=0 means no limit maxBlockFilterNum = 50000 - # Allowed batch size, default: 100, <=0 means no limit maxBatchSize = 100 - # Allowed max response byte size, default: 26214400 (25 MB), <=0 means no limit maxResponseSize = 26214400 - # Allowed maximum number for newFilter, <=0 means no limit maxLogFilterNum = 20000 - # Maximum JSON-RPC request body size, default 4MB. Independent from rpc.maxMessageSize. - maxMessageSize = 4M + maxMessageSize = 4194304 } - # Disabled api list, it will work for http, rpc and pbft, both FullNode and SolidityNode, - # but not jsonrpc. The setting is case insensitive, GetNowBlock2 is equal to getnowblock2 disabledApi = [ # "getaccount", # "getnowblock2" ] - } ## rate limiter config rate.limiter = { - # Every api could only set a specific rate limit strategy. Three strategy are supported: - # GlobalPreemptibleAdapter: The number of preemptible resource or maximum concurrent requests globally. - # QpsRateLimiterAdapter: qps is the average request count in one second supported by the server, it could be a Double or a Integer. - # IPQPSRateLimiterAdapter: similar to the QpsRateLimiterAdapter, qps could be a Double or a Integer. - # If not set, QpsRateLimiterAdapter with qps=1000 is the default strategy. - # - # Sample entries: - # + # See reference.conf for available strategies (GlobalPreemptibleAdapter, QpsRateLimiterAdapter, IPQPSRateLimiterAdapter). http = [ - # { - # component = "GetNowBlockServlet", - # strategy = "GlobalPreemptibleAdapter", - # paramString = "permit=1" - # }, - - # { - # component = "GetAccountServlet", - # strategy = "IPQPSRateLimiterAdapter", - # paramString = "qps=1" - # }, - - # { - # component = "ListWitnessesServlet", - # strategy = "QpsRateLimiterAdapter", - # paramString = "qps=1" - # } ], rpc = [ - # { - # component = "protocol.Wallet/GetBlockByLatestNum2", - # strategy = "GlobalPreemptibleAdapter", - # paramString = "permit=1" - # }, - - # { - # component = "protocol.Wallet/GetAccount", - # strategy = "IPQPSRateLimiterAdapter", - # paramString = "qps=1" - # }, - - # { - # component = "protocol.Wallet/ListWitnesses", - # strategy = "QpsRateLimiterAdapter", - # paramString = "qps=1" - # }, ] p2p = { @@ -473,8 +190,7 @@ rate.limiter = { global.qps = 50000 # IP-based global qps, default 10000 global.ip.qps = 10000 - # If true, API rate limiters reject immediately on overload (non-blocking). - # If false (default), callers wait for a permit (blocking, the legacy behaviour). + # If true, API rate limiters reject immediately on overload (non-blocking). Default: false apiNonBlocking = false } @@ -483,11 +199,6 @@ rate.limiter = { seed.node = { # List of the seed nodes # Seed nodes are stable full nodes - # example: - # ip.list = [ - # "ip:port", - # "ip:port" - # ] ip.list = [ "3.225.171.164:18888", "52.8.46.215:18888", @@ -690,10 +401,10 @@ genesis.block = { parentHash = "0xe58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f" } -# Optional. The default is empty. It is used when the witness account has set the witnessPermission. -# When it is not empty, the localWitnessAccountAddress represents the address of the witness account, -# and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. -# When it is empty,the localwitness is configured with the private key of the witness account. +# Optional. Used when the witness account has set witnessPermission. +# localWitnessAccountAddress is the witness account address; +# localwitness is configured with the private key of the witnessPermissionAddress. +# When empty, localwitness is the private key of the witness account itself. # localWitnessAccountAddress = localwitness = [ @@ -707,100 +418,24 @@ block = { needSyncCheck = true maintenanceTimeInterval = 21600000 // 6 hours: 21600000(ms) proposalExpireTime = 259200000 // default value: 3 days: 259200000(ms), Note: this value is controlled by committee proposal - # checkFrozenTime = 1 // for test only } # Transaction reference block, default is "solid", configure to "head" may cause TaPos error trx.reference.block = "solid" // "head" or "solid" -# This property sets the number of milliseconds after the creation of the transaction that is expired, default value is 60000. -# trx.expiration.timeInMilliseconds = 60000 - vm = { supportConstant = false maxEnergyLimitForConstant = 100000000 minTimeRatio = 0.0 maxTimeRatio = 5.0 saveInternalTx = false - # lruCacheSize = 500 - # vmTrace = false - - # Indicates whether the node stores featured internal transactions, such as freeze, vote and so on. Default: false. - # saveFeaturedInternalTx = false - - # Indicates whether the node stores the details of the internal transactions generated by the CANCELALLUNFREEZEV2 opcode, - # such as bandwidth/energy/tronpower cancel amount. Default: false. - # saveCancelAllUnfreezeV2Details = false - - # In rare cases, transactions that will be within the specified maximum execution time (default 10(ms)) are re-executed and packaged - # longRunningTime = 10 - - # Indicates whether the node support estimate energy API. Default: false. - # estimateEnergy = false - - # Indicates the max retry time for executing transaction in estimating energy. Default 3. - # estimateEnergyMaxRetry = 3 - - # Max TVM execution time (ms) for constant calls — applies to - # triggerconstantcontract, triggersmartcontract dispatched to view/pure - # functions, estimateenergy, eth_call, eth_estimateGas, and any other RPC - # routed through the constant-call path. When set, must be a positive - # integer that fits VM deadline conversion and is used verbatim as the - # per-call deadline (no clamp against the network's maxCpuTimeOfOneTx). - # Omit the property entirely to keep the default behaviour of sharing the - # block-processing deadline. Migration note: if previously running --debug - # to extend constant calls, switch to this option (--debug also extends - # block-processing, which is unsafe; see issue #6266). Default: 0. - # constantCallTimeoutMs = 100 } # These parameters are designed for private chain testing only and cannot be freely switched on or off in production systems. +# In production, they are controlled by on-chain committee proposals. committee = { allowCreationOfContracts = 0 //mainnet:0 (reset by committee),test:1 allowAdaptiveEnergy = 0 //mainnet:0 (reset by committee),test:1 - # allowCreationOfContracts = 0 - # allowMultiSign = 0 - # allowAdaptiveEnergy = 0 - # allowDelegateResource = 0 - # allowSameTokenName = 0 - # allowTvmTransferTrc10 = 0 - # allowTvmConstantinople = 0 - # allowTvmSolidity059 = 0 - # forbidTransferToContract = 0 - # allowShieldedTRC20Transaction = 0 - # allowTvmIstanbul = 0 - # allowMarketTransaction = 0 - # allowProtoFilterNum = 0 - # allowAccountStateRoot = 0 - # changedDelegation = 0 - # allowPBFT = 0 - # pBFTExpireNum = 0 - # allowTransactionFeePool = 0 - # allowBlackHoleOptimization = 0 - # allowNewResourceModel = 0 - # allowReceiptsMerkleRoot = 0 - # allowTvmFreeze = 0 - # allowTvmVote = 0 - # unfreezeDelayDays = 0 - # allowTvmLondon = 0 - # allowTvmCompatibleEvm = 0 - # allowNewRewardAlgorithm = 0 - # allowAccountAssetOptimization = 0 - # allowAssetOptimization = 0 - # allowNewReward = 0 - # memoFee = 0 - # allowDelegateOptimization = 0 - # allowDynamicEnergy = 0 - # dynamicEnergyThreshold = 0 - # dynamicEnergyMaxFactor = 0 - # allowTvmShangHai = 0 - # allowOldRewardOpt = 0 - # allowEnergyAdjustment = 0 - # allowStrictMath = 0 - # allowTvmCancun = 0 - # allowTvmBlob = 0 - # consensusLogicOptimization = 0 - # allowOptimizedReturnValueOfChainId = 0 } event.subscribe = { @@ -811,17 +446,13 @@ event.subscribe = { sendqueuelength = 1000 //max length of send queue } version = 0 - # Specify the starting block number to sync historical events. This is only applicable when version = 1. + # Specify the starting block number to sync historical events. Only applicable when version = 1. # After performing a full event sync, set this value to 0 or a negative number. # startSyncBlockNum = 1 path = "" // absolute path of plugin - server = "" // target server address to receive event triggers - # dbname|username|password, if you want to create indexes for collections when the collections - # are not exist, you can add version and set it to 2, as dbname|username|password|version - # if you use version 2 and one collection not exists, it will create index automaticaly; - # if you use version 2 and one collection exists, it will not create index, you must create index manually; - dbconfig = "" + server = "" // target server address to receive event triggers, "ip:port" + dbconfig = "" // dbname|username|password (append |2 to auto-create indexes on missing collections) contractParse = true topics = [ { @@ -850,7 +481,7 @@ event.subscribe = { }, { triggerName = "solidity" // solidity block trigger(just include solidity block number and timestamp), the value can't be modified - enable = true // Default: true + enable = false topic = "solidity" }, { diff --git a/framework/src/test/java/org/tron/common/ParameterTest.java b/framework/src/test/java/org/tron/common/ParameterTest.java index 91bb580a3b4..0b66c96462c 100644 --- a/framework/src/test/java/org/tron/common/ParameterTest.java +++ b/framework/src/test/java/org/tron/common/ParameterTest.java @@ -176,8 +176,6 @@ public void testCommonParameter() { assertEquals(2, parameter.getEstimateEnergyMaxRetry()); parameter.setKeepAliveInterval(1000); assertEquals(1000, parameter.getKeepAliveInterval()); - parameter.setReceiveTcpMinDataLength(10); - assertEquals(10, parameter.getReceiveTcpMinDataLength()); parameter.setOpenFullTcpDisconnect(false); assertFalse(parameter.isOpenFullTcpDisconnect()); parameter.setNodeDetectEnable(false); diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index ce479e06542..076a8ab5387 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -17,7 +17,6 @@ import com.google.common.collect.Lists; import com.typesafe.config.Config; -import com.typesafe.config.ConfigException; import com.typesafe.config.ConfigFactory; import io.grpc.internal.GrpcUtil; import io.grpc.netty.NettyServerBuilder; @@ -39,6 +38,7 @@ import org.tron.common.utils.DecodeUtil; import org.tron.common.utils.LocalWitnesses; import org.tron.common.utils.PublicMethod; +import org.tron.core.exception.ContractValidateException; import org.tron.core.exception.TronError; @Slf4j @@ -370,12 +370,9 @@ public void testConfigStorageDefaults() { } // =========================================================================== - // Boundary tests for clamps applied in Args.java bridge code (not in - // bean postProcess()). + // Boundary tests for node.fetchBlock.timeout clamping. // - // fetchBlockTimeout is read from NodeConfig but clamped in Args.applyNodeConfig - // to range [100, 1000]. Pin this clamp here so any future refactor that moves - // it (e.g. into NodeConfig.postProcess()) preserves the behavior. + // The clamp to [100, 1000] is applied in NodeConfig.postProcess(). // =========================================================================== @Test @@ -475,79 +472,14 @@ public void testAllowShieldedTransactionApiDefault() { } @Test - public void testMaxMessageSizeHumanReadable() { + public void testMaxMessageSizePureNumber() { Map configMap = new HashMap<>(); configMap.put("storage.db.directory", "database"); - // --- KB tier: binary (k/K/Ki/KiB = 1024) vs SI (kB = 1000) --- - configMap.put("node.rpc.maxMessageSize", "512k"); - configMap.put("node.http.maxMessageSize", "512K"); - configMap.put("node.jsonrpc.maxMessageSize", "512kB"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - Assert.assertEquals(512 * 1024, Args.getInstance().getMaxMessageSize()); - Assert.assertEquals(512 * 1024, Args.getInstance().getHttpMaxMessageSize()); - Assert.assertEquals(512 * 1000, Args.getInstance().getJsonRpcMaxMessageSize()); - Args.clearParam(); - - configMap.put("node.rpc.maxMessageSize", "256Ki"); - configMap.put("node.http.maxMessageSize", "256KiB"); - configMap.put("node.jsonrpc.maxMessageSize", "256kB"); - config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - Assert.assertEquals(256 * 1024, Args.getInstance().getMaxMessageSize()); - Assert.assertEquals(256 * 1024, Args.getInstance().getHttpMaxMessageSize()); - Assert.assertEquals(256 * 1000, Args.getInstance().getJsonRpcMaxMessageSize()); - Args.clearParam(); - - // --- MB tier: binary (m/M/Mi/MiB = 1024*1024) vs SI (MB = 1000*1000) --- - configMap.put("node.rpc.maxMessageSize", "4m"); - configMap.put("node.http.maxMessageSize", "8M"); - configMap.put("node.jsonrpc.maxMessageSize", "2MB"); - config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - Assert.assertEquals(4 * 1024 * 1024, Args.getInstance().getMaxMessageSize()); - Assert.assertEquals(8 * 1024 * 1024, Args.getInstance().getHttpMaxMessageSize()); - Assert.assertEquals(2 * 1000 * 1000, Args.getInstance().getJsonRpcMaxMessageSize()); - Args.clearParam(); - - configMap.put("node.rpc.maxMessageSize", "4Mi"); - configMap.put("node.http.maxMessageSize", "4MiB"); - configMap.put("node.jsonrpc.maxMessageSize", "4MB"); - config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - Assert.assertEquals(4 * 1024 * 1024, Args.getInstance().getMaxMessageSize()); - Assert.assertEquals(4 * 1024 * 1024, Args.getInstance().getHttpMaxMessageSize()); - Assert.assertEquals(4 * 1000 * 1000, Args.getInstance().getJsonRpcMaxMessageSize()); - Args.clearParam(); - - // --- GB tier: binary (g/G/Gi/GiB) vs SI (GB) --- - // All three paths are int-bounded; values up to Integer.MAX_VALUE are accepted. - configMap.put("node.rpc.maxMessageSize", "4m"); - configMap.put("node.http.maxMessageSize", "1g"); - configMap.put("node.jsonrpc.maxMessageSize", "1GB"); - config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - Args.applyConfigParams(config); - Assert.assertEquals(4 * 1024 * 1024, Args.getInstance().getMaxMessageSize()); - Assert.assertEquals(1024L * 1024 * 1024, Args.getInstance().getHttpMaxMessageSize()); - Assert.assertEquals(1000L * 1000 * 1000, Args.getInstance().getJsonRpcMaxMessageSize()); - Args.clearParam(); - - // --- raw integer (backward compatible): treated as bytes --- configMap.put("node.rpc.maxMessageSize", "4194304"); configMap.put("node.http.maxMessageSize", "4194304"); configMap.put("node.jsonrpc.maxMessageSize", "4194304"); - config = ConfigFactory.defaultOverrides() + Config config = ConfigFactory.defaultOverrides() .withFallback(ConfigFactory.parseMap(configMap)) .withFallback(ConfigFactory.defaultReference()); Args.applyConfigParams(config); @@ -556,7 +488,6 @@ public void testMaxMessageSizeHumanReadable() { Assert.assertEquals(4 * 1024 * 1024, Args.getInstance().getJsonRpcMaxMessageSize()); Args.clearParam(); - // --- zero is allowed --- configMap.put("node.rpc.maxMessageSize", "0"); configMap.put("node.http.maxMessageSize", "0"); configMap.put("node.jsonrpc.maxMessageSize", "0"); @@ -571,82 +502,39 @@ public void testMaxMessageSizeHumanReadable() { } @Test - public void testRpcMaxMessageSizeExceedsIntMax() { - Map configMap = new HashMap<>(); - configMap.put("storage.db.directory", "database"); - configMap.put("node.rpc.maxMessageSize", "3g"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - TronError e = Assert.assertThrows(TronError.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue(e.getMessage().contains("node.rpc.maxMessageSize must be non-negative")); - } - - @Test - public void testHttpMaxMessageSizeExceedsIntMax() { - Map configMap = new HashMap<>(); - configMap.put("storage.db.directory", "database"); - configMap.put("node.http.maxMessageSize", "2Gi"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - TronError e = Assert.assertThrows(TronError.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue(e.getMessage().contains("node.http.maxMessageSize must be non-negative")); - } - - @Test - public void testJsonRpcMaxMessageSizeExceedsIntMax() { - Map configMap = new HashMap<>(); - configMap.put("storage.db.directory", "database"); - configMap.put("node.jsonrpc.maxMessageSize", "2Gi"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - TronError e = Assert.assertThrows(TronError.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue( - e.getMessage().contains("node.jsonrpc.maxMessageSize must be non-negative")); - } - - @Test - public void testMaxMessageSizeNegativeValue() { - Map configMap = new HashMap<>(); - configMap.put("storage.db.directory", "database"); - configMap.put("node.rpc.maxMessageSize", "-4m"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue(e.getMessage().contains("negative")); - } - - @Test - public void testMaxMessageSizeInvalidUnit() { - Map configMap = new HashMap<>(); - configMap.put("storage.db.directory", "database"); - configMap.put("node.rpc.maxMessageSize", "4x"); - Config config = ConfigFactory.defaultOverrides() - .withFallback(ConfigFactory.parseMap(configMap)) - .withFallback(ConfigFactory.defaultReference()); - ConfigException.BadValue e = Assert.assertThrows(ConfigException.BadValue.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue(e.getMessage().contains("Could not parse size-in-bytes unit")); + public void testMaxMessageSizeNegativeValueRejected() { + // Negative maxMessageSize must be rejected at startup which threw TronError(PARAMETER_INIT) + // for negative values). + for (String key : new String[]{ + "node.rpc.maxMessageSize", "node.http.maxMessageSize", "node.jsonrpc.maxMessageSize"}) { + Map configMap = new HashMap<>(); + configMap.put("storage.db.directory", "database"); + configMap.put(key, "-1"); + Config config = ConfigFactory.defaultOverrides() + .withFallback(ConfigFactory.parseMap(configMap)) + .withFallback(ConfigFactory.defaultReference()); + TronError e = Assert.assertThrows(TronError.class, () -> Args.applyConfigParams(config)); + Assert.assertEquals(TronError.ErrCode.PARAMETER_INIT, e.getErrCode()); + Args.clearParam(); + } } @Test - public void testMaxMessageSizeNonNumeric() { - Map configMap = new HashMap<>(); + public void testRpcMaxMessageSizeExceedsIntMax() { + // HOCON's Config.getInt() throws when a numeric value exceeds int range. + // This documents the failure mode for node.rpc.maxMessageSize (int field). + Map configMap = new HashMap<>(); configMap.put("storage.db.directory", "database"); - configMap.put("node.http.maxMessageSize", "abc"); + configMap.put("node.rpc.maxMessageSize", (long) Integer.MAX_VALUE + 1); Config config = ConfigFactory.defaultOverrides() .withFallback(ConfigFactory.parseMap(configMap)) .withFallback(ConfigFactory.defaultReference()); - ConfigException.BadValue e = Assert.assertThrows(ConfigException.BadValue.class, - () -> Args.applyConfigParams(config)); - Assert.assertTrue(e.getMessage().contains("No number in size-in-bytes value")); + try { + Args.applyConfigParams(config); + Assert.fail("Expected RuntimeException for maxMessageSize > Integer.MAX_VALUE"); + } catch (RuntimeException e) { + // ConfigBeanFactory/HOCON throws when binding a long out of int range + } } // ===== checkBackupMembers() tests ===== diff --git a/framework/src/test/resources/config-shield.conf b/framework/src/test/resources/config-shield.conf index aad1ba8452f..1c185f8f82f 100644 --- a/framework/src/test/resources/config-shield.conf +++ b/framework/src/test/resources/config-shield.conf @@ -19,30 +19,6 @@ storage { # Attention: name is a required field that must be set !!! properties = [ - // { - // name = "account", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - // { - // name = "account-index", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, ] } @@ -200,23 +176,6 @@ genesis.block = { url = "http://Test.org", voteCount = 106 }, - // { - // address: TPrLL5ckUdMaPNgJYmGv23qtYjBE34aBf8 - // url = "http://Mercury.org", - // voteCount = 105 - // }, - // { - // address: TEZBh76rouEQpB2zqYVopbRXGx7RfyWorT - // #address: 27TfVERREG3FeWMHEAQ95tWHG4sb3ANn3Qe - // url = "http://Venus.org", - // voteCount = 104 - // }, - // { - // address: TN27wbfCLEN1gP2PZAxHgU3QZrntsLyxdj - // #address: 27b8RUuyZnNPFNZGct2bZkNu9MnGWNAdH3Z - // url = "http://Earth.org", - // voteCount = 103 - // }, ] timestamp = "0" #2017-8-26 12:00:00 @@ -224,12 +183,6 @@ genesis.block = { parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" } -// Optional.The default is empty. -// It is used when the witness account has set the witnessPermission. -// When it is not empty, the localWitnessAccountAddress represents the address of the witness account, -// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. -// When it is empty,the localwitness is configured with the private key of the witness account. - //localWitnessAccountAddress = TN3zfjYUmMFK3ZsHSsrdJoNRtGkQmZLBLz localwitness = [ diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index bb83449272b..2277346234b 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -3,7 +3,6 @@ net { # type = mainnet } - storage { # Directory for storing persistent data @@ -19,44 +18,20 @@ storage { # Otherwise, db configs will remain defualt and data will be stored in # the path of "output-directory" or which is set by "-d" ("--output-directory"). - # Attention: name is a required field that must be set !!! + # Per-database storage path overrides (name is required; see reference.conf for full property list). properties = [ - // { - // name = "account", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - // { - // name = "account-index", - // path = "storage_directory_test", - // createIfMissing = true, - // paranoidChecks = true, - // verifyChecksums = true, - // compressionType = 1, // compressed with snappy - // blockSize = 4096, // 4 KB = 4 * 1024 B - // writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - // maxOpenFiles = 100 - // }, - // { # only for unit test - // name = "test_name", - // path = "test_path", - // createIfMissing = false, - // paranoidChecks = false, - // verifyChecksums = false, - // compressionType = 1, - // blockSize = 2, - // writeBufferSize = 3, - // cacheSize = 4, - // maxOpenFiles = 5 - // }, + # { + # name = "account", + # path = "storage_directory_test", + # createIfMissing = true, // deprecated for arm start + # paranoidChecks = true, + # verifyChecksums = true, + # compressionType = 1, // compressed with snappy + # blockSize = 4096, // 4 KB = 4 * 1024 B + # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B + # maxOpenFiles = 100 // deprecated for arm end + # }, ] needToUpdateAsset = false @@ -89,13 +64,6 @@ node { listen.port = 18888 active = [ - # Sample entries: - # { url = "enode://@hostname.com:30303" } - # { - # ip = hostname.com - # port = 30303 - # nodeId = e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c - # } ] inactiveThreshold = 600 //seconds @@ -321,17 +289,9 @@ genesis.block = { parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" } - -// Optional.The default is empty. -// It is used when the witness account has set the witnessPermission. -// When it is not empty, the localWitnessAccountAddress represents the address of the witness account, -// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account. -// When it is empty,the localwitness is configured with the private key of the witness account. - //localWitnessAccountAddress = localwitness = [ - ] block = { From a771d440d9f768a80f64de968ae9a15dfc03fc8b Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 2 Jun 2026 18:16:51 +0800 Subject: [PATCH 24/57] fix(db): fix storage config properties (#6806) --- .../main/java/org/tron/core/config/README.md | 7 +- .../org/tron/core/config/args/Storage.java | 33 +------ .../tron/core/config/args/StorageConfig.java | 96 +++++++++++-------- common/src/main/resources/reference.conf | 49 +++++++--- .../core/config/args/StorageConfigTest.java | 92 ++++++++++++++++++ .../tron/core/config/args/StorageTest.java | 50 +++++----- framework/src/test/resources/config-test.conf | 7 +- 7 files changed, 215 insertions(+), 119 deletions(-) diff --git a/common/src/main/java/org/tron/core/config/README.md b/common/src/main/java/org/tron/core/config/README.md index c34994519d9..1380c98984e 100644 --- a/common/src/main/java/org/tron/core/config/README.md +++ b/common/src/main/java/org/tron/core/config/README.md @@ -28,10 +28,7 @@ storage { { name = "account", path = "/path/to/accout", // relative or absolute path - createIfMissing = true, - paranoidChecks = true, - verifyChecksums = true, - compressionType = 1, // 0 - no compression, 1 - compressed with snappy + # following are only used for LevelDB blockSize = 4096, // 4 KB = 4 * 1024 B writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B @@ -43,7 +40,7 @@ storage { ``` -As shown in the example above, the `accout` database will be stored in the path of `/path/to/accout/database` while the index be stored in `/path/to/accout/index`. And, the example also shows our default value of LevelDB options(Start from `createIfMissing` and end at `maxOpenFiles`). Please refer to the docs of [LevelDB](https://github.com/google/leveldb/blob/master/doc/index.md#performance) to figure out the details of these options. +As shown in the example above, the `accout` database will be stored in the path of `/path/to/accout/database` while the index be stored in `/path/to/accout/index`. And, the example also shows our default value of LevelDB options(Start from `blockSize` and end at `maxOpenFiles`). Please refer to the docs of [LevelDB](https://github.com/google/leveldb/blob/master/doc/index.md#performance) to figure out the details of these options. ## gRPC diff --git a/common/src/main/java/org/tron/core/config/args/Storage.java b/common/src/main/java/org/tron/core/config/args/Storage.java index f1317e04914..16dd8295be1 100644 --- a/common/src/main/java/org/tron/core/config/args/Storage.java +++ b/common/src/main/java/org/tron/core/config/args/Storage.java @@ -25,7 +25,6 @@ import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; -import org.iq80.leveldb.CompressionType; import org.iq80.leveldb.Options; import org.tron.common.cache.CacheStrategies; import org.tron.common.cache.CacheType; @@ -170,26 +169,13 @@ private Property createPropertyFromBean(StorageConfig.PropertyConfig pc) { } Options dbOptions = newDefaultDbOptions(property.getName()); - applyPropertyOptions(pc, dbOptions); + // PropertyConfig is-a DbOptionOverride: apply only user-specified (non-null) overrides + // so unset fields keep the per-tier defaults already applied by newDefaultDbOptions. + applyDbOptionOverride(pc, dbOptions); property.setDbOptions(dbOptions); return property; } - /** - * Apply LevelDB options from PropertyConfig bean values. - */ - private static void applyPropertyOptions(StorageConfig.PropertyConfig pc, Options dbOptions) { - dbOptions.createIfMissing(pc.isCreateIfMissing()); - dbOptions.paranoidChecks(pc.isParanoidChecks()); - dbOptions.verifyChecksums(pc.isVerifyChecksums()); - dbOptions.compressionType( - CompressionType.getCompressionTypeByPersistentId(pc.getCompressionType())); - dbOptions.blockSize(pc.getBlockSize()); - dbOptions.writeBufferSize(pc.getWriteBufferSize()); - dbOptions.cacheSize(pc.getCacheSize()); - dbOptions.maxOpenFiles(pc.getMaxOpenFiles()); - } - /** * Set propertyMap from StorageConfig bean list. No Config parameter needed. */ @@ -247,19 +233,6 @@ public Options newDefaultDbOptions(String name) { // Apply only user-specified overrides (non-null fields) to LevelDB Options. private static void applyDbOptionOverride( StorageConfig.DbOptionOverride o, Options dbOptions) { - if (o.getCreateIfMissing() != null) { - dbOptions.createIfMissing(o.getCreateIfMissing()); - } - if (o.getParanoidChecks() != null) { - dbOptions.paranoidChecks(o.getParanoidChecks()); - } - if (o.getVerifyChecksums() != null) { - dbOptions.verifyChecksums(o.getVerifyChecksums()); - } - if (o.getCompressionType() != null) { - dbOptions.compressionType( - CompressionType.getCompressionTypeByPersistentId(o.getCompressionType())); - } if (o.getBlockSize() != null) { dbOptions.blockSize(o.getBlockSize()); } diff --git a/common/src/main/java/org/tron/core/config/args/StorageConfig.java b/common/src/main/java/org/tron/core/config/args/StorageConfig.java index e8823d81984..2c6c3e60a41 100644 --- a/common/src/main/java/org/tron/core/config/args/StorageConfig.java +++ b/common/src/main/java/org/tron/core/config/args/StorageConfig.java @@ -28,6 +28,8 @@ public class StorageConfig { private CheckpointConfig checkpoint = new CheckpointConfig(); private SnapshotConfig snapshot = new SnapshotConfig(); private TxCacheConfig txCache = new TxCacheConfig(); + // ConfigBeanFactory requires all bean fields present per item, so we parse manually. + @Setter(lombok.AccessLevel.NONE) private List properties = new ArrayList<>(); // merkleRoot is a nested object (e.g. { reward-vi = "hash..." }) not a string. @@ -54,6 +56,7 @@ public class StorageConfig { @Getter @Setter public static class DbConfig { + private String engine = "LEVELDB"; private boolean sync = false; private String directory = "database"; @@ -62,6 +65,7 @@ public static class DbConfig { @Getter @Setter public static class TransHistoryConfig { + // "switch" is a reserved Java keyword; ConfigBeanFactory calls setSwitch() which works fine @Getter(lombok.AccessLevel.NONE) @Setter(lombok.AccessLevel.NONE) @@ -79,6 +83,7 @@ public void setSwitch(String v) { @Getter @Setter public static class DbSettingsConfig { + private int levelNumber = 7; private int compactThreads = 0; // 0 = auto: max(availableProcessors, 1) private int blocksize = 16; @@ -100,11 +105,13 @@ void postProcess() { @Getter @Setter public static class BalanceConfig { + private HistoryConfig history = new HistoryConfig(); @Getter @Setter public static class HistoryConfig { + private boolean lookup = false; } } @@ -112,6 +119,7 @@ public static class HistoryConfig { @Getter @Setter public static class CheckpointConfig { + private int version = 1; private boolean sync = true; } @@ -119,6 +127,7 @@ public static class CheckpointConfig { @Getter @Setter public static class SnapshotConfig { + private int maxFlushCount = 1; // Reject out-of-range values. Mirrors develop Storage.getSnapshotMaxFlushCountFromConfig. @@ -135,6 +144,7 @@ void postProcess() { @Getter @Setter public static class TxCacheConfig { + private int estimatedTransactions = 1000; private boolean initOptimization = false; @@ -148,19 +158,14 @@ void postProcess() { } } + // A named database entry: name/path plus the optional LevelDB option overrides + // inherited from DbOptionOverride (boxed types, null = "inherit per-tier defaults"). @Getter @Setter - public static class PropertyConfig { + public static class PropertyConfig extends DbOptionOverride { + private String name = ""; private String path = ""; - private boolean createIfMissing = true; - private boolean paranoidChecks = true; - private boolean verifyChecksums = true; - private int compressionType = 1; - private int blockSize = 4096; - private int writeBufferSize = 10485760; - private long cacheSize = 10485760; - private int maxOpenFiles = 100; } // Defaults come from reference.conf (loaded globally via Configuration.java) @@ -170,6 +175,7 @@ public static StorageConfig fromConfig(Config config) { StorageConfig sc = ConfigBeanFactory.create(section, StorageConfig.class); sc.rawStorageConfig = section; + sc.properties = readProperties(section); // Read optional LevelDB option overrides (default, defaultM, defaultL). sc.defaultDbOption = readDbOption(section, "default"); @@ -187,45 +193,17 @@ public static StorageConfig fromConfig(Config config) { @Getter @Setter public static class DbOptionOverride { - private Boolean createIfMissing; - private Boolean paranoidChecks; - private Boolean verifyChecksums; - private Integer compressionType; + private Integer blockSize; private Integer writeBufferSize; private Long cacheSize; private Integer maxOpenFiles; } - // Read optional LevelDB option override (default/defaultM/defaultL). - // Not bean-bound: users may only set a subset of keys (e.g. just maxOpenFiles), - // ConfigBeanFactory requires all fields present so partial overrides would fail. - private static DbOptionOverride readDbOption(Config section, String key) { - if (!section.hasPath(key)) { - return null; - } - ConfigObject conf = section.getObject(key); - DbOptionOverride o = new DbOptionOverride(); - if (conf.containsKey("createIfMissing")) { - o.setCreateIfMissing( - Boolean.parseBoolean(conf.get("createIfMissing").unwrapped().toString())); - } - if (conf.containsKey("paranoidChecks")) { - o.setParanoidChecks( - Boolean.parseBoolean(conf.get("paranoidChecks").unwrapped().toString())); - } - if (conf.containsKey("verifyChecksums")) { - o.setVerifyChecksums( - Boolean.parseBoolean(conf.get("verifyChecksums").unwrapped().toString())); - } - if (conf.containsKey("compressionType")) { - String param = conf.get("compressionType").unwrapped().toString(); - try { - o.setCompressionType(Integer.parseInt(param)); - } catch (NumberFormatException e) { - throwIllegalArgumentException("compressionType", Integer.class, param); - } - } + // Shared LevelDB option parser used by both readDbOption and readProperties. + // Fills the given target (boxed fields, null means "not specified by user") so the + // same parser can populate a plain DbOptionOverride or a PropertyConfig (which extends it). + private static void readLevelDbOptions(ConfigObject conf, DbOptionOverride o) { if (conf.containsKey("blockSize")) { String param = conf.get("blockSize").unwrapped().toString(); try { @@ -258,9 +236,43 @@ private static DbOptionOverride readDbOption(Config section, String key) { throwIllegalArgumentException("maxOpenFiles", Integer.class, param); } } + } + + // Read optional LevelDB option override for default/defaultM/defaultL keys. + private static DbOptionOverride readDbOption(Config section, String key) { + if (!section.hasPath(key)) { + return null; + } + DbOptionOverride o = new DbOptionOverride(); + readLevelDbOptions(section.getObject(key), o); return o; } + // Parse storage.properties list manually: ConfigBeanFactory requires every bean field to be + // present in each list item, but name+path-only entries (all LevelDB opts commented out) are + // valid — missing fields fall back to PropertyConfig Java defaults. + private static List readProperties(Config section) { + if (!section.hasPath("properties")) { + return new ArrayList<>(); + } + List items = section.getObjectList("properties"); + List result = new ArrayList<>(items.size()); + for (ConfigObject obj : items) { + PropertyConfig p = new PropertyConfig(); + if (obj.containsKey("name")) { + p.setName(obj.get("name").unwrapped().toString()); + } + if (obj.containsKey("path")) { + p.setPath(obj.get("path").unwrapped().toString()); + } + // Boxed nullable fields: unset options stay null so they inherit the per-tier + // defaults applied by newDefaultDbOptions instead of resetting them. + readLevelDbOptions(obj, p); + result.add(p); + } + return result; + } + private static void throwIllegalArgumentException(String param, Class type, String actual) { throw new IllegalArgumentException( String.format("[storage.properties] %s must be %s type, actual: %s.", diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 549e280bbe1..be3fefb2645 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -44,49 +44,68 @@ net { } storage { - # Database engine: "LEVELDB" or "ROCKSDB" (ARM only supports ROCKSDB) + # Database engine: "LEVELDB" or "ROCKSDB" (ARM only supports ROCKSDB), case-insensitive db.engine = "LEVELDB" + + # Controls the database write strategy. + # true - Synchronous writes. Higher durability, lower performance. + # false - Asynchronous writes. Higher performance, but recent writes may be + # lost if the machine crashes before data is flushed to disk. + # Asynchronous writes can significantly improve FullNode block sync performance. db.sync = false + db.directory = "database" # Whether to write transaction result in transactionRetStore transHistory.switch = "on" - # Per-database LevelDB option overrides. Default: empty (all databases use global defaults). - # setting can improve leveldb performance .... start, deprecated for arm - # node: if this will increase process fds, you may check your ulimit if 'too many open files' error occurs - # see https://github.com/tronprotocol/tips/blob/master/tip-343.md for detail - # if you find block sync has lower performance, you can try this settings + # Per-database LevelDB option overrides.Default: empty. All databases use global LevelDB settings. + # These settings can be tuned to improve database performance during block synchronization. + # Note: + # - Increasing `maxOpenFiles` may significantly increase file descriptor usage. + # - If "Too many open files" errors occur, check the system `ulimit` configuration. + # - See TIP-343 for tuning recommendations: + # https://github.com/tronprotocol/tips/blob/master/tip-343.md + # The following presets are provided as default. If block synchronization + # performance is unsatisfactory, consider adjusting the settings accordingly. + # + # Global default settings: # default = { + # blockSize = 4096, // 4 KB + # writeBufferSize = 16777216, // 16 MB + # cacheSize = 33554432, // 32 MB # maxOpenFiles = 100 # } + # Default for bulk-read databases: code, contract # defaultM = { - # maxOpenFiles = 500 + # blockSize = 4096, // 4 KB + # writeBufferSize = 67108864, // 64 MB + # cacheSize = 33554432, // 32 MB + # maxOpenFiles = 100 // recommend 500 for production # } + # Default for frequently accessed databases: account, delegation, storage-row # defaultL = { - # maxOpenFiles = 1000 + # blockSize = 4096, // 4 KB + # writeBufferSize = 67108864, // 64 MB + # cacheSize = 33554432, // 32 MB + # maxOpenFiles = 100 // recommend 1000 for production # } - # setting can improve leveldb performance .... end, deprecated for arm # Per-database storage configuration overrides. Otherwise databases use global defaults and store # data in "output-directory" or the directory specified by the "-d" / "--output-directory" option. # Attention: name is a required field that must be set! # The name and path properties take effect for both LevelDB and RocksDB storage engines, - # while additional properties (createIfMissing, paranoidChecks, compressionType, etc.) + # while additional 4 properties (blockSize, writeBufferSize, cacheSize, maxOpenFiles) # only take effect when using LevelDB. # Example: # properties = [ # { # name = "account", # path = "storage_directory_test", - # createIfMissing = true, // deprecated for arm start - # paranoidChecks = true, - # verifyChecksums = true, - # compressionType = 1, // compressed with snappy # blockSize = 4096, // 4 KB = 4 * 1024 B # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # maxOpenFiles = 100 // deprecated for arm end + # maxOpenFiles = 100 # }, # ] properties = [] diff --git a/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java b/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java index d8700880cd0..e3f1925a763 100644 --- a/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java +++ b/common/src/test/java/org/tron/core/config/args/StorageConfigTest.java @@ -2,12 +2,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import java.util.List; import org.junit.Test; import org.tron.common.math.StrictMathWrapper; +import org.tron.core.config.args.StorageConfig.PropertyConfig; public class StorageConfigTest { @@ -134,4 +137,93 @@ public void testTxCacheEstimatedWithinRangePreserved() { withRef("storage.txCache.estimatedTransactions = 5000")); assertEquals(5000, sc.getTxCache().getEstimatedTransactions()); } + + // ---- readProperties() ---- + + private static List props(String storageProperties) { + return StorageConfig.fromConfig(withRef(storageProperties)).getProperties(); + } + + @Test + public void testPropertiesDefaultEmpty() { + // reference.conf sets storage.properties = [] + assertTrue(StorageConfig.fromConfig(withRef()).getProperties().isEmpty()); + assertTrue(props("storage.properties = []").isEmpty()); + } + + @Test + public void testPropertiesNameAndPathOnly() { + // All LevelDB options omitted: name/path set, the four boxed fields stay null so + // they inherit the per-tier defaults applied later by newDefaultDbOptions. + List list = props( + "storage.properties = [ { name = account, path = some_path } ]"); + assertEquals(1, list.size()); + PropertyConfig p = list.get(0); + assertEquals("account", p.getName()); + assertEquals("some_path", p.getPath()); + assertNull(p.getBlockSize()); + assertNull(p.getWriteBufferSize()); + assertNull(p.getCacheSize()); + assertNull(p.getMaxOpenFiles()); + } + + @Test + public void testPropertiesNameOnlyKeepsEmptyPath() { + PropertyConfig p = props("storage.properties = [ { name = account } ]").get(0); + assertEquals("account", p.getName()); + assertEquals("", p.getPath()); + } + + @Test + public void testPropertiesFullOverrideParsed() { + PropertyConfig p = props( + "storage.properties = [ { name = foo, path = bar," + + " blockSize = 2, writeBufferSize = 3, cacheSize = 4, maxOpenFiles = 5 } ]").get(0); + assertEquals(Integer.valueOf(2), p.getBlockSize()); + assertEquals(Integer.valueOf(3), p.getWriteBufferSize()); + assertEquals(Long.valueOf(4L), p.getCacheSize()); + assertEquals(Integer.valueOf(5), p.getMaxOpenFiles()); + } + + @Test + public void testPropertiesPartialOverrideLeavesOthersNull() { + // Only blockSize is set; the other three stay null (inherit defaults). + PropertyConfig p = props( + "storage.properties = [ { name = foo, path = bar, blockSize = 8192 } ]").get(0); + assertEquals(Integer.valueOf(8192), p.getBlockSize()); + assertNull(p.getWriteBufferSize()); + assertNull(p.getCacheSize()); + assertNull(p.getMaxOpenFiles()); + } + + @Test + public void testPropertiesMultipleEntriesInOrder() { + List list = props( + "storage.properties = [" + + " { name = first, path = p1 }," + + " { name = second, path = p2, maxOpenFiles = 7 } ]"); + assertEquals(2, list.size()); + assertEquals("first", list.get(0).getName()); + assertNull(list.get(0).getMaxOpenFiles()); + assertEquals("second", list.get(1).getName()); + assertEquals(Integer.valueOf(7), list.get(1).getMaxOpenFiles()); + } + + @Test + public void testPropertiesMissingNameKeepsEmpty() { + // readProperties does not require name (validation is deferred to Storage); name stays "". + PropertyConfig p = props("storage.properties = [ { path = bar } ]").get(0); + assertEquals("", p.getName()); + assertEquals("bar", p.getPath()); + } + + @Test(expected = IllegalArgumentException.class) + public void testPropertiesInvalidIntegerRejected() { + props("storage.properties = [ { name = foo, blockSize = not_a_number } ]"); + } + + @Test(expected = IllegalArgumentException.class) + public void testPropertiesInvalidLongRejected() { + props("storage.properties = [ { name = foo, cacheSize = not_a_number } ]"); + } } diff --git a/framework/src/test/java/org/tron/core/config/args/StorageTest.java b/framework/src/test/java/org/tron/core/config/args/StorageTest.java index 3c00c6ea00e..c6b954838ca 100644 --- a/framework/src/test/java/org/tron/core/config/args/StorageTest.java +++ b/framework/src/test/java/org/tron/core/config/args/StorageTest.java @@ -18,7 +18,6 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import java.io.File; -import org.iq80.leveldb.CompressionType; import org.iq80.leveldb.Options; import org.junit.AfterClass; import org.junit.Assert; @@ -44,17 +43,16 @@ private static void setupStorage() { + "storage.defaultL.maxOpenFiles = 1000\n" + "storage.properties = [\n" + " { name = account, path = storage_directory_test,\n" - + " createIfMissing = true, paranoidChecks = true, verifyChecksums = true,\n" - + " compressionType = 1, blockSize = 4096,\n" - + " writeBufferSize = 10485760, cacheSize = 10485760, maxOpenFiles = 100 },\n" + + " blockSize = 4096, writeBufferSize = 10485760, cacheSize = 10485760,\n" + + " maxOpenFiles = 100 },\n" + " { name = \"account-index\", path = storage_directory_test,\n" - + " createIfMissing = true, paranoidChecks = true, verifyChecksums = true,\n" - + " compressionType = 1, blockSize = 4096,\n" - + " writeBufferSize = 10485760, cacheSize = 10485760, maxOpenFiles = 100 },\n" + + " blockSize = 4096, writeBufferSize = 10485760, cacheSize = 10485760,\n" + + " maxOpenFiles = 100 },\n" + " { name = test_name, path = test_path,\n" - + " createIfMissing = false, paranoidChecks = false, verifyChecksums = false,\n" - + " compressionType = 1, blockSize = 2,\n" - + " writeBufferSize = 3, cacheSize = 4, maxOpenFiles = 5 }\n" + + " blockSize = 2, writeBufferSize = 3, cacheSize = 4, maxOpenFiles = 5 },\n" + // name/path-only entries: LevelDB options omitted, must inherit per-tier defaults + + " { name = delegation, path = test_path },\n" + + " { name = code, path = test_path }\n" + "]" ).withFallback(ConfigFactory.load(TestConstants.TEST_CONF)); StorageConfig sc = StorageConfig.fromConfig(cfg); @@ -83,30 +81,18 @@ public void getPath() { @Test public void getOptions() { Options options = StorageUtils.getOptionsByDbName("account"); - Assert.assertTrue(options.createIfMissing()); - Assert.assertTrue(options.paranoidChecks()); - Assert.assertTrue(options.verifyChecksums()); - Assert.assertEquals(CompressionType.SNAPPY, options.compressionType()); Assert.assertEquals(4096, options.blockSize()); Assert.assertEquals(10485760, options.writeBufferSize()); Assert.assertEquals(10485760L, options.cacheSize()); Assert.assertEquals(100, options.maxOpenFiles()); options = StorageUtils.getOptionsByDbName("test_name"); - Assert.assertFalse(options.createIfMissing()); - Assert.assertFalse(options.paranoidChecks()); - Assert.assertFalse(options.verifyChecksums()); - Assert.assertEquals(CompressionType.SNAPPY, options.compressionType()); Assert.assertEquals(2, options.blockSize()); Assert.assertEquals(3, options.writeBufferSize()); Assert.assertEquals(4L, options.cacheSize()); Assert.assertEquals(5, options.maxOpenFiles()); options = StorageUtils.getOptionsByDbName("some_name_not_exists"); - Assert.assertTrue(options.createIfMissing()); - Assert.assertTrue(options.paranoidChecks()); - Assert.assertTrue(options.verifyChecksums()); - Assert.assertEquals(CompressionType.SNAPPY, options.compressionType()); Assert.assertEquals(4 * 1024, options.blockSize()); Assert.assertEquals(16 * 1024 * 1024, options.writeBufferSize()); Assert.assertEquals(32 * 1024 * 1024L, options.cacheSize()); @@ -125,4 +111,24 @@ public void getOptions() { Assert.assertEquals(50, options.maxOpenFiles()); } + /** + * A properties entry that only sets name/path (all LevelDB options omitted) must inherit + * the per-tier defaults from newDefaultDbOptions instead of resetting them to the + * PropertyConfig defaults. Both "delegation" (DB_L) and "code" (DB_M) are listed with + * name/path only, so they must keep their tier writeBufferSize/maxOpenFiles. + */ + @Test + public void nameAndPathOnlyInheritsTierDefaults() { + Options ldb = StorageUtils.getOptionsByDbName("delegation"); + Assert.assertEquals(64 * 1024 * 1024, ldb.writeBufferSize()); + Assert.assertEquals(1000, ldb.maxOpenFiles()); + // unset cacheSize/blockSize inherit the base defaults, not PropertyConfig's old 10 MB + Assert.assertEquals(32 * 1024 * 1024L, ldb.cacheSize()); + Assert.assertEquals(4 * 1024, ldb.blockSize()); + + Options mdb = StorageUtils.getOptionsByDbName("code"); + Assert.assertEquals(64 * 1024 * 1024, mdb.writeBufferSize()); + Assert.assertEquals(500, mdb.maxOpenFiles()); + } + } diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index 2277346234b..a7bf77654cb 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -23,14 +23,11 @@ storage { # { # name = "account", # path = "storage_directory_test", - # createIfMissing = true, // deprecated for arm start - # paranoidChecks = true, - # verifyChecksums = true, - # compressionType = 1, // compressed with snappy + # # following are only used for LevelDB # blockSize = 4096, // 4 KB = 4 * 1024 B # writeBufferSize = 10485760, // 10 MB = 10 * 1024 * 1024 B # cacheSize = 10485760, // 10 MB = 10 * 1024 * 1024 B - # maxOpenFiles = 100 // deprecated for arm end + # maxOpenFiles = 100 # }, ] From ddd8c7bb83cef07ff2d0dc5580f46f24e732feea Mon Sep 17 00:00:00 2001 From: zz Date: Tue, 2 Jun 2026 01:48:32 +0800 Subject: [PATCH 25/57] feat(vm): optimize the check for create2 --- .../src/main/java/org/tron/core/vm/program/Program.java | 3 +++ actuator/src/main/java/org/tron/core/vm/utils/MUtil.java | 6 ++++++ common/src/main/java/org/tron/core/config/Parameter.java | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 80d972041dc..31789547013 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1621,6 +1621,9 @@ public void createContract2(DataWord value, DataWord memStart, DataWord memSize, stackPushZero(); return; } + if (getCallDeep() == MAX_DEPTH) { + MUtil.checkCPUTimeForCreate2(); + } if (VMConfig.allowTvmIstanbul()) { senderAddress = getContextAddress(); } else { diff --git a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java index c94f28b3a2f..e3e67129dad 100644 --- a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java +++ b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java @@ -64,4 +64,10 @@ public static void checkCPUTime() { throw new OutOfTimeException("CPU timeout for 0x0a executing"); } } + + public static void checkCPUTimeForCreate2() { + if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_1_1)) { + throw new OutOfTimeException("CPU timeout for create2 executing"); + } + } } diff --git a/common/src/main/java/org/tron/core/config/Parameter.java b/common/src/main/java/org/tron/core/config/Parameter.java index 8ec27ed3eb5..9dbe062cd47 100644 --- a/common/src/main/java/org/tron/core/config/Parameter.java +++ b/common/src/main/java/org/tron/core/config/Parameter.java @@ -28,7 +28,8 @@ public enum ForkBlockVersionEnum { VERSION_4_7_7(31, 1596780000000L, 80), VERSION_4_8_0(32, 1596780000000L, 80), VERSION_4_8_0_1(33, 1596780000000L, 70), - VERSION_4_8_1(34, 1596780000000L, 80); + VERSION_4_8_1(34, 1596780000000L, 80), + VERSION_4_8_1_1(35, 1596780000000L, 70); // if add a version, modify BLOCK_VERSION simultaneously @Getter @@ -77,7 +78,7 @@ public class ChainConstant { public static final int SINGLE_REPEAT = 1; public static final int BLOCK_FILLED_SLOTS_NUMBER = 128; public static final int MAX_FROZEN_NUMBER = 1; - public static final int BLOCK_VERSION = 34; + public static final int BLOCK_VERSION = 35; public static final long FROZEN_PERIOD = 86_400_000L; public static final long DELEGATE_PERIOD = 3 * 86_400_000L; public static final long TRX_PRECISION = 1000_000L; From 30752bf7ac345a30f21317e9ad82d3197db913cf Mon Sep 17 00:00:00 2001 From: zz Date: Tue, 2 Jun 2026 17:29:58 +0800 Subject: [PATCH 26/57] feat(vm): optimize the check for ModExp --- .../main/java/org/tron/core/vm/PrecompiledContracts.java | 7 ++++++- actuator/src/main/java/org/tron/core/vm/utils/MUtil.java | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java index 654a76db33b..cc11a58717f 100644 --- a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java +++ b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java @@ -622,6 +622,8 @@ public static class ModExp extends PrecompiledContract { private static final int ARGS_OFFSET = 32 * 3; // addresses length part + private static final int UPPER_BOUND = 1024; + @Override public long getEnergyForData(byte[] data) { @@ -633,7 +635,6 @@ public long getEnergyForData(byte[] data) { int expLen = parseLen(data, 1); int modLen = parseLen(data, 2); - byte[] expHighBytes = parseBytes(data, addSafely(ARGS_OFFSET, baseLen), min(expLen, 32, VMConfig.disableJavaLangMath())); @@ -660,6 +661,10 @@ public Pair execute(byte[] data) { int expLen = parseLen(data, 1); int modLen = parseLen(data, 2); + if (baseLen == 0 && modLen == 0 && expLen > UPPER_BOUND) { + MUtil.checkCPUTimeForModExp(); + } + BigInteger base = parseArg(data, ARGS_OFFSET, baseLen); BigInteger exp = parseArg(data, addSafely(ARGS_OFFSET, baseLen), expLen); BigInteger mod = parseArg(data, addSafely(addSafely(ARGS_OFFSET, baseLen), expLen), modLen); diff --git a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java index e3e67129dad..e07360e6863 100644 --- a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java +++ b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java @@ -70,4 +70,10 @@ public static void checkCPUTimeForCreate2() { throw new OutOfTimeException("CPU timeout for create2 executing"); } } + + public static void checkCPUTimeForModExp() { + if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_1_1)) { + throw new OutOfTimeException("CPU timeout for modExp executing"); + } + } } From a45af58672d5fb77c97fdb456d0fdd6c78cc0a96 Mon Sep 17 00:00:00 2001 From: zz Date: Tue, 2 Jun 2026 19:20:49 +0800 Subject: [PATCH 27/57] test(vm): add tests for create2/modExp checks --- .../runtime/vm/Create2ModExpForkTest.java | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java diff --git a/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java b/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java new file mode 100644 index 00000000000..bef91eaef37 --- /dev/null +++ b/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java @@ -0,0 +1,177 @@ +package org.tron.common.runtime.vm; + +import java.util.Arrays; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.runtime.InternalTransaction; +import org.tron.common.utils.ForkController; +import org.tron.core.Constant; +import org.tron.core.config.Parameter.ForkBlockVersionEnum; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ContractValidateException; +import org.tron.core.store.StoreFactory; +import org.tron.core.vm.PrecompiledContracts; +import org.tron.core.vm.PrecompiledContracts.PrecompiledContract; +import org.tron.core.vm.config.ConfigLoader; +import org.tron.core.vm.config.VMConfig; +import org.tron.core.vm.program.Program; +import org.tron.core.vm.program.Program.OutOfTimeException; +import org.tron.core.vm.program.invoke.ProgramInvokeMockImpl; +import org.tron.core.vm.utils.MUtil; +import org.tron.protos.Protocol; + + +@Slf4j +public class Create2ModExpForkTest extends BaseTest { + + // mirrors the private Program.MAX_DEPTH + private static final int MAX_CALL_DEPTH = 64; + + // mirrors PrecompiledContracts.ModExp.UPPER_BOUND + private static final int MOD_EXP_UPPER_BOUND = 1024; + + // ModExp precompile address (0x05) + private static final DataWord MOD_EXP_ADDR = new DataWord( + "0000000000000000000000000000000000000000000000000000000000000005"); + + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF); + CommonParameter.getInstance().setDebug(true); + } + + @AfterClass + public static void destroy() { + ConfigLoader.disable = false; + VMConfig.initVmHardFork(false); + VMConfig.initAllowTvmCompatibleEvm(0); + Args.clearParam(); + } + + @Before + public void setUp() { + ForkController.instance().init(chainBaseManager); + deactivateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + } + + @Test + public void checkCPUTimeForCreate2_isGatedByFork() { + MUtil.checkCPUTimeForCreate2(); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForCreate2); + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); + } + + @Test + public void checkCPUTimeForModExp_isGatedByFork() { + MUtil.checkCPUTimeForModExp(); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForModExp); + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); + } + + @Test + public void modExp_degenerateInput_throwsOnlyAfterFork() { + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); + byte[] data = buildModExpInput(MOD_EXP_UPPER_BOUND + 1); + + Pair out = modExp.execute(data); + Assert.assertTrue(out.getLeft()); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, () -> modExp.execute(data)); + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); + } + + @Test + public void modExp_atUpperBound_doesNotThrowAfterFork() { + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); + Pair out = modExp.execute(buildModExpInput(MOD_EXP_UPPER_BOUND)); + Assert.assertTrue(out.getLeft()); + } + + @Test + public void createContract2_atMaxDepth_legacyPath_throwsAfterFork() + throws ContractValidateException { + VMConfig.initAllowTvmCompatibleEvm(0); + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + Program program = buildProgramAtMaxDepth(); + OutOfTimeException ex = Assert.assertThrows(OutOfTimeException.class, + () -> program.createContract2( + DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO())); + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); + } + + @Test + public void createContract2_atMaxDepth_compatibleEvmOn_doesNotThrow() + throws ContractValidateException { + VMConfig.initAllowTvmCompatibleEvm(1); + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + Program program = buildProgramAtMaxDepth(); + program.createContract2(DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO()); + Assert.assertEquals(DataWord.ZERO(), program.getStack().pop()); + } + + // ---- helpers --------------------------------------------------------------------------------- + + private Program buildProgramAtMaxDepth() throws ContractValidateException { + StoreFactory.init(); + StoreFactory storeFactory = StoreFactory.getInstance(); + storeFactory.setChainBaseManager(chainBaseManager); + byte[] ops = new byte[] {0}; + ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl(storeFactory, ops, ops) { + @Override + public int getCallDeep() { + return MAX_CALL_DEPTH; + } + }; + Program program = new Program(ops, ops, invoke, + new InternalTransaction(Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + program.setRootTransactionId(new byte[32]); + return program; + } + + private byte[] buildModExpInput(int expLen) { + byte[] data = new byte[96]; + byte[] expLenWord = new DataWord(expLen).getData(); + System.arraycopy(expLenWord, 0, data, 32, 32); + return data; + } + + private void activateFork(ForkBlockVersionEnum forkVersion) { + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + chainBaseManager.getDynamicPropertiesStore().statsByVersion(forkVersion.getValue(), stats); + long maintenanceTimeInterval = + chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); + long hardForkTime = ((forkVersion.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(hardForkTime + 1); + } + + private void deactivateFork(ForkBlockVersionEnum forkVersion) { + chainBaseManager.getDynamicPropertiesStore() + .statsByVersion(forkVersion.getValue(), new byte[27]); + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(0L); + } +} From 144571a73099e11dca0435a1906d59ef1ebe96b0 Mon Sep 17 00:00:00 2001 From: zz Date: Tue, 2 Jun 2026 19:43:02 +0800 Subject: [PATCH 28/57] feat(version): update version to 4.8.1.1 --- framework/src/main/java/org/tron/program/Version.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index de3f91f0a5c..2a8f53b5ef2 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -4,7 +4,7 @@ public class Version { public static final String VERSION_NAME = "GreatVoyage-v4.8.0.1-1-g44a4bc8263"; public static final String VERSION_CODE = "18636"; - private static final String VERSION = "4.8.1"; + private static final String VERSION = "4.8.1.1"; public static String getVersion() { return VERSION; From d5d9f465fb8fdead113a9ccbc109f6dafeabb7c1 Mon Sep 17 00:00:00 2001 From: halibobo1205 Date: Tue, 2 Jun 2026 20:10:32 +0800 Subject: [PATCH 29/57] feat(ci): add PR pipeline and system-test workflows New workflows: - pr-build.yml: multi-OS build matrix (macOS, Ubuntu, RockyLinux, Debian11) and changed-line/overall coverage gate - pr-check.yml: PR title/body lint + Checkstyle - pr-reviewer.yml: scope-based reviewer auto-assignment - pr-cancel.yml: cancel in-progress runs when PR is closed unmerged - system-test.yml: spin up FullNode and run the system-test suite Existing workflows: - codeql.yml: bump to v4/v5 actions, switch to manual build-mode with JDK 8, add paths-ignore for docs-only changes - math-check.yml: bump checkout/upload-artifact/github-script versions --- .github/workflows/codeql.yml | 36 +-- .github/workflows/math-check.yml | 6 +- .github/workflows/pr-build.yml | 498 ++++++++++++++++++++++++++++++ .github/workflows/pr-cancel.yml | 55 ++++ .github/workflows/pr-check.yml | 131 ++++++++ .github/workflows/pr-reviewer.yml | 144 +++++++++ .github/workflows/system-test.yml | 95 ++++++ 7 files changed, 940 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/pr-build.yml create mode 100644 .github/workflows/pr-cancel.yml create mode 100644 .github/workflows/pr-check.yml create mode 100644 .github/workflows/pr-reviewer.yml create mode 100644 .github/workflows/system-test.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 5a0f120e116..9c3af93f787 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -6,6 +6,9 @@ on: pull_request: # The branches below must be a subset of the branches above branches: [ 'develop' ] + paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', + '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', + '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] schedule: - cron: '6 10 * * 0' @@ -29,36 +32,25 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v5 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3 + uses: github/codeql-action/init@v4 with: languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. + build-mode: manual - # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v3 - - # ℹ️ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - # If the Autobuild fails above, remove it and uncomment the following three lines. - # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + - name: Set up JDK 8 + uses: actions/setup-java@v5 + with: + java-version: '8' + distribution: 'temurin' - # - run: | - # echo "Run, Build Application using script" - # ./location_of_script_within_repo/buildscript.sh + - name: Build + run: ./gradlew build -x test --no-daemon - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 + uses: github/codeql-action/analyze@v4 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/math-check.yml b/.github/workflows/math-check.yml index 0f0255815d5..a5db3351a94 100644 --- a/.github/workflows/math-check.yml +++ b/.github/workflows/math-check.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Check for java.lang.Math usage id: check-math @@ -55,14 +55,14 @@ jobs: - name: Upload findings if: steps.check-math.outputs.math_found == 'true' - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v6 with: name: math-usage-report path: math_usage.txt - name: Create comment if: github.event_name == 'pull_request' && steps.check-math.outputs.math_found == 'true' - uses: actions/github-script@v6 + uses: actions/github-script@v8 with: script: | const fs = require('fs'); diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml new file mode 100644 index 00000000000..52e56304243 --- /dev/null +++ b/.github/workflows/pr-build.yml @@ -0,0 +1,498 @@ +name: PR Build + +on: + pull_request: + branches: [ 'master','develop', 'release_**' ] + types: [ opened, synchronize, reopened ] + paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', + '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', + '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] + workflow_dispatch: + inputs: + job: + description: 'Job to run: all / macos / ubuntu / rockylinux / debian11' + required: false + default: 'all' + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + + build-macos: + name: Build macos26 (JDK ${{ matrix.java }} / ${{ matrix.arch }}) + if: ${{ github.event_name == 'pull_request' || inputs.job == 'all' || inputs.job == 'macos' }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 60 + strategy: + fail-fast: false + matrix: + include: + - java: '17' + runner: macos-26 + arch: aarch64 + + steps: + - uses: actions/checkout@v5 + + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v5 + with: + java-version: ${{ matrix.java }} + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: macos26-${{ matrix.arch }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: macos26-${{ matrix.arch }}-gradle- + + - name: Build + run: ./gradlew clean build --no-daemon + + - name: Toolkit jar smoke test + run: | + set -e + JAR=plugins/build/libs/Toolkit.jar + java -jar "$JAR" help + java -jar "$JAR" db --help + java -jar "$JAR" db archive -h + + build-ubuntu: + name: Build ubuntu24 (JDK 17 / aarch64) + if: ${{ github.event_name == 'pull_request' || inputs.job == 'all' || inputs.job == 'ubuntu' }} + runs-on: ubuntu-24.04-arm + timeout-minutes: 60 + + steps: + - uses: actions/checkout@v5 + + - name: Set up JDK 17 + uses: actions/setup-java@v5 + with: + java-version: '17' + distribution: 'temurin' + + - name: Check Java version + run: java -version + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ubuntu24-aarch64-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: ubuntu24-aarch64-gradle- + + - name: Build + run: ./gradlew clean build --no-daemon + + - name: Toolkit jar smoke test + run: | + set -e + JAR=plugins/build/libs/Toolkit.jar + java -jar "$JAR" help + java -jar "$JAR" db --help + java -jar "$JAR" db archive -h + + docker-build-rockylinux: + name: Build rockylinux (JDK 8 / x86_64) + if: ${{ github.event_name == 'pull_request' || inputs.job == 'all' || inputs.job == 'rockylinux' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + + container: + image: rockylinux:8 + + env: + GRADLE_USER_HOME: /github/home/.gradle + LANG: en_US.UTF-8 + LC_ALL: en_US.UTF-8 + + steps: + - name: Checkout code + uses: actions/checkout@v5 + + - name: Install dependencies (Rocky 8 + JDK8) + run: | + set -euxo pipefail + dnf -y install java-1.8.0-openjdk-devel git wget unzip which jq bc curl glibc-langpack-en + dnf -y groupinstall "Development Tools" + + - name: Check Java version + run: java -version + + - name: Cache Gradle + uses: actions/cache@v4 + with: + path: | + /github/home/.gradle/caches + /github/home/.gradle/wrapper + key: rockylinux-x86_64-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: | + rockylinux-x86_64-gradle- + + - name: Stop Gradle daemon + run: ./gradlew --stop || true + + - name: Build + run: ./gradlew clean build --no-daemon + + - name: Toolkit jar smoke test + run: | + set -e + JAR=plugins/build/libs/Toolkit.jar + java -jar "$JAR" help + java -jar "$JAR" db --help + java -jar "$JAR" db archive -h + + + docker-build-debian11: + name: Build debian11 (JDK 8 / x86_64) + if: ${{ github.event_name == 'pull_request' || inputs.job == 'all' || inputs.job == 'debian11' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + + container: + image: eclipse-temurin:8-jdk # base image is Debian 11 (Bullseye) + + defaults: + run: + shell: bash + + env: + GRADLE_USER_HOME: /github/home/.gradle + + steps: + - name: Checkout code + uses: actions/checkout@v5 + + - name: Install dependencies (Debian + build tools) + run: | + set -euxo pipefail + apt-get update + apt-get install -y git wget unzip build-essential curl jq + + - name: Check Java version + run: java -version + + - name: Cache Gradle + uses: actions/cache@v4 + with: + path: | + /github/home/.gradle/caches + /github/home/.gradle/wrapper + key: debian11-x86_64-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: | + debian11-x86_64-gradle- + + - name: Build + run: ./gradlew clean build --no-daemon --no-build-cache + + - name: Toolkit jar smoke test + run: | + set -e + JAR=plugins/build/libs/Toolkit.jar + java -jar "$JAR" help + java -jar "$JAR" db --help + java -jar "$JAR" db archive -h + + + - name: Generate module coverage reports + run: ./gradlew jacocoTestReport --no-daemon + + - name: Upload PR coverage reports + uses: actions/upload-artifact@v6 + with: + name: jacoco-coverage-pr + path: | + **/build/reports/jacoco/test/jacocoTestReport.xml + if-no-files-found: error + + coverage-base: + name: Coverage Base (JDK 8 / x86_64) + if: ${{ github.event_name == 'pull_request' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + container: + image: eclipse-temurin:8-jdk # base image is Debian 11 (Bullseye) + defaults: + run: + shell: bash + env: + GRADLE_USER_HOME: /github/home/.gradle + permissions: + contents: read + + steps: + - name: Checkout code + uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.base.sha }} + + - name: Install dependencies (Debian + build tools) + run: | + set -euxo pipefail + apt-get update + apt-get install -y git wget unzip build-essential curl jq + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + /github/home/.gradle/caches + /github/home/.gradle/wrapper + key: coverage-base-x86_64-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: | + coverage-base-x86_64-gradle- + + - name: Build (base) + run: ./gradlew clean build --no-daemon --no-build-cache + + - name: Generate module coverage reports (base) + run: ./gradlew jacocoTestReport --no-daemon + + - name: Upload base coverage reports + uses: actions/upload-artifact@v6 + with: + name: jacoco-coverage-base + path: | + **/build/reports/jacoco/test/jacocoTestReport.xml + if-no-files-found: error + + coverage-gate: + name: Coverage Gate + needs: [docker-build-debian11, coverage-base] + if: ${{ github.event_name == 'pull_request' }} + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + + steps: + - name: Checkout code + uses: actions/checkout@v5 + with: + fetch-depth: 0 + + - name: Download base coverage reports + uses: actions/download-artifact@v8 + with: + name: jacoco-coverage-base + path: coverage/base + + - name: Download PR coverage reports + uses: actions/download-artifact@v8 + with: + name: jacoco-coverage-pr + path: coverage/pr + + - name: Collect coverage report paths + id: collect-xml + run: | + BASE_XMLS=$(find coverage/base -name "jacocoTestReport.xml" | sort | paste -sd, -) + PR_XMLS=$(find coverage/pr -name "jacocoTestReport.xml" | sort | paste -sd, -) + if [ -z "$BASE_XMLS" ] || [ -z "$PR_XMLS" ]; then + echo "Missing jacocoTestReport.xml files for base or PR." + exit 1 + fi + echo "base_xmls=$BASE_XMLS" >> "$GITHUB_OUTPUT" + echo "pr_xmls=$PR_XMLS" >> "$GITHUB_OUTPUT" + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Changed-line coverage (diff-cover) + id: diff-cover + env: + BASE_REF: ${{ github.event.pull_request.base.ref }} + run: | + set -euo pipefail + pip install --quiet 'diff-cover==9.2.0' + + # Ensure the base branch ref is available locally for diff-cover. + git fetch --no-tags origin "+refs/heads/${BASE_REF}:refs/remotes/origin/${BASE_REF}" + + PR_XMLS=$(find coverage/pr -name "jacocoTestReport.xml" | sort) + SRC_ROOTS=$(find . -type d -path '*/src/main/java' \ + -not -path './coverage/*' -not -path './.git/*' | sort) + if [ -z "$SRC_ROOTS" ]; then + echo "No src/main/java directories found; cannot run diff-cover." >&2 + exit 1 + fi + + set +e + diff-cover $PR_XMLS \ + --compare-branch="origin/${BASE_REF}" \ + --src-roots $SRC_ROOTS \ + --fail-under=0 \ + --json-report=diff-cover.json \ + --markdown-report=diff-cover.md + DIFF_RC=$? + set -e + + if [ ! -f diff-cover.json ]; then + echo "diff-cover did not produce JSON report (exit=${DIFF_RC})." >&2 + exit 1 + fi + + TOTAL_NUM_LINES=$(jq -r '.total_num_lines // 0' diff-cover.json) + if [ "${TOTAL_NUM_LINES}" = "0" ]; then + echo "No changed Java source lines; skipping changed-line gate." + echo "changed_line_coverage=NA" >> "$GITHUB_OUTPUT" + else + CHANGED_LINE_COVERAGE=$(jq -r '.total_percent_covered // empty' diff-cover.json) + if [ -z "$CHANGED_LINE_COVERAGE" ]; then + echo "Unable to parse changed-line coverage from diff-cover.json." + exit 1 + fi + echo "changed_line_coverage=${CHANGED_LINE_COVERAGE}" >> "$GITHUB_OUTPUT" + fi + + { + echo "### Changed-line Coverage (diff-cover)" + echo "" + if [ -f diff-cover.md ] && [ -s diff-cover.md ]; then + cat diff-cover.md + else + echo "_diff-cover produced no report._" + fi + } >> "$GITHUB_STEP_SUMMARY" + + - name: Aggregate base coverage + id: jacoco-base + uses: madrapps/jacoco-report@v1.7.2 + with: + paths: ${{ steps.collect-xml.outputs.base_xmls }} + token: ${{ secrets.GITHUB_TOKEN }} + min-coverage-overall: 0 + min-coverage-changed-files: 0 + skip-if-no-changes: true + comment-type: summary + title: '## Base Coverage Snapshot' + update-comment: false + + - name: Aggregate PR coverage + id: jacoco-pr + uses: madrapps/jacoco-report@v1.7.2 + with: + paths: ${{ steps.collect-xml.outputs.pr_xmls }} + token: ${{ secrets.GITHUB_TOKEN }} + min-coverage-overall: 0 + min-coverage-changed-files: 0 + skip-if-no-changes: true + comment-type: summary + title: '## PR Code Coverage Report' + update-comment: false + + - name: Enforce coverage gates + env: + BASE_OVERALL_RAW: ${{ steps.jacoco-base.outputs.coverage-overall }} + PR_OVERALL_RAW: ${{ steps.jacoco-pr.outputs.coverage-overall }} + CHANGED_LINE_RAW: ${{ steps.diff-cover.outputs.changed_line_coverage }} + run: | + set -euo pipefail + + MIN_CHANGED=60 + MAX_DROP=-0.1 + + sanitize() { + echo "$1" | tr -d ' %' + } + is_number() { + [[ "$1" =~ ^-?[0-9]+([.][0-9]+)?$ ]] + } + compare_float() { + # Usage: compare_float "" + # Example: compare_float "1.2 >= -0.1" + awk "BEGIN { if ($1) print 1; else print 0 }" + } + + # 1) Parse metrics from jacoco-report outputs + BASE_OVERALL="$(sanitize "$BASE_OVERALL_RAW")" + PR_OVERALL="$(sanitize "$PR_OVERALL_RAW")" + CHANGED_LINE="$(sanitize "$CHANGED_LINE_RAW")" + + if ! is_number "$BASE_OVERALL" || ! is_number "$PR_OVERALL"; then + echo "Failed to parse coverage values: base='${BASE_OVERALL}', pr='${PR_OVERALL}'." + exit 1 + fi + + # 2) Compare metrics against thresholds + DELTA=$(awk -v pr="$PR_OVERALL" -v base="$BASE_OVERALL" 'BEGIN { printf "%.4f", pr - base }') + DELTA_OK=$(compare_float "${DELTA} >= ${MAX_DROP}") + + if [ "$CHANGED_LINE" = "NA" ]; then + CHANGED_LINE_OK=1 + CHANGED_LINE_STATUS="SKIPPED (no changed Java source lines)" + elif [ -z "$CHANGED_LINE" ] || [ "$CHANGED_LINE" = "NaN" ] || ! is_number "$CHANGED_LINE"; then + echo "Failed to parse changed-line coverage: changed-line='${CHANGED_LINE}'." + exit 1 + else + CHANGED_LINE_OK=$(compare_float "${CHANGED_LINE} > ${MIN_CHANGED}") + if [ "$CHANGED_LINE_OK" -eq 1 ]; then + CHANGED_LINE_STATUS="PASS (> ${MIN_CHANGED}%)" + else + CHANGED_LINE_STATUS="FAIL (<= ${MIN_CHANGED}%)" + fi + fi + + # 3) Output base metrics (always visible in logs + step summary) + OVERALL_STATUS="PASS (>= ${MAX_DROP}%)" + if [ "$DELTA_OK" -ne 1 ]; then + OVERALL_STATUS="FAIL (< ${MAX_DROP}%)" + fi + + if [ "$CHANGED_LINE" = "NA" ]; then + CHANGED_LINE_DISPLAY="NA" + else + CHANGED_LINE_DISPLAY="${CHANGED_LINE}%" + fi + + METRICS_TEXT=$(cat <> "$GITHUB_STEP_SUMMARY" + + # 4) Decide CI pass/fail + if [ "$DELTA_OK" -ne 1 ]; then + echo "Coverage gate failed: overall coverage dropped more than 0.1%." + echo "base=${BASE_OVERALL}% pr=${PR_OVERALL}% delta=${DELTA}%" + exit 1 + fi + + if [ "$CHANGED_LINE_OK" -ne 1 ]; then + echo "Coverage gate failed: changed-line coverage must be > 60%." + echo "changed-line=${CHANGED_LINE}%" + exit 1 + fi + + echo "Coverage gates passed." diff --git a/.github/workflows/pr-cancel.yml b/.github/workflows/pr-cancel.yml new file mode 100644 index 00000000000..bbd0e68c235 --- /dev/null +++ b/.github/workflows/pr-cancel.yml @@ -0,0 +1,55 @@ +name: Cancel PR Workflows on Close + +on: + pull_request: + types: [ closed ] + +permissions: + actions: write + +jobs: + cancel: + name: Cancel In-Progress Workflows + if: github.event.pull_request.merged == false + runs-on: ubuntu-latest + steps: + - name: Cancel PR Build and System Test + uses: actions/github-script@v8 + with: + script: | + const workflows = ['pr-build.yml', 'system-test.yml', 'codeql.yml']; + const headSha = context.payload.pull_request.head.sha; + const prNumber = context.payload.pull_request.number; + + for (const workflowId of workflows) { + for (const status of ['in_progress', 'queued']) { + const runs = await github.paginate( + github.rest.actions.listWorkflowRuns, + { + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: workflowId, + status, + event: 'pull_request', + per_page: 100, + }, + (response) => response.data.workflow_runs + ); + + for (const run of runs) { + if (!run) { + continue; + } + const prs = Array.isArray(run.pull_requests) ? run.pull_requests : []; + const isTargetPr = prs.length === 0 || prs.some((pr) => pr.number === prNumber); + if (run.head_sha === headSha && isTargetPr) { + await github.rest.actions.cancelWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: run.id, + }); + console.log(`Cancelled ${workflowId} run #${run.id} (${status})`); + } + } + } + } diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml new file mode 100644 index 00000000000..19425209bbc --- /dev/null +++ b/.github/workflows/pr-check.yml @@ -0,0 +1,131 @@ +name: PR Check + +on: + push: + branches: [ 'master', 'release_**' ] + pull_request: + branches: [ 'develop', 'release_**' ] + types: [ opened, edited, synchronize, reopened ] + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: true + +jobs: + pr-lint: + name: PR Lint + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + + steps: + - name: Validate PR title and description + uses: actions/github-script@v8 + with: + script: | + const title = context.payload.pull_request.title; + const body = context.payload.pull_request.body; + const errors = []; + const warnings = []; + + const allowedTypes = ['feat','fix','refactor','docs','style','test','chore','ci','perf','build','revert']; + const knownScopes = [ + 'framework','chainbase','actuator','consensus','common','crypto','plugins','protocol', + 'net','db','vm','tvm','api','jsonrpc','rpc','http','event','config', + 'block','proposal','trie','log','metrics','test','docker','version', + 'freezeV2','DynamicEnergy','stable-coin','reward','lite','toolkit' + ]; + + // 1. Title length check + if (!title || title.trim().length < 10) { + errors.push('PR title is too short (minimum 10 characters).'); + } + if (title && title.length > 72) { + errors.push(`PR title is too long (${title.length}/72 characters).`); + } + + // 2. Conventional format check + const conventionalRegex = /^(feat|fix|refactor|docs|style|test|chore|ci|perf|build|revert)(\([^)]+\))?:\s\S.*/; + if (title && !conventionalRegex.test(title)) { + errors.push( + 'PR title must follow conventional format: `type(scope): description`\n' + + ' Allowed types: ' + allowedTypes.map(t => `\`${t}\``).join(', ') + '\n' + + ' Example: `feat(tvm): add blob opcodes`' + ); + } + + // 3. No trailing period + if (title && title.endsWith('.')) { + errors.push('PR title should not end with a period (.).'); + } + + // 4. Description part should not start with a capital letter + if (title) { + const descMatch = title.match(/^\w+(?:\([^)]+\))?:\s*(.+)/); + if (descMatch) { + const desc = descMatch[1]; + if (/^[A-Z]/.test(desc)) { + errors.push('Description should not start with a capital letter.'); + } + } + } + + // 5. Scope validation (warning only) + if (title) { + const scopeMatch = title.match(/^\w+\(([^)]+)\):/); + if (scopeMatch && !knownScopes.includes(scopeMatch[1])) { + warnings.push(`Unknown scope \`${scopeMatch[1]}\`. See CONTRIBUTING.md for known scopes.`); + } + } + + // 6. PR description check + if (!body || body.trim().length < 20) { + errors.push('PR description is too short or empty (minimum 20 characters). Please describe what this PR does and why.'); + } + + // Output warnings + for (const w of warnings) { + core.warning(w); + } + + // Output result + if (errors.length > 0) { + const docLink = 'See [CONTRIBUTING.md](https://github.com/' + context.repo.owner + '/' + context.repo.repo + '/blob/develop/CONTRIBUTING.md#pull-request-guidelines) for details.'; + const message = '### PR Lint Failed\n\n' + errors.map(e => `- ${e}`).join('\n') + '\n\n' + docLink; + core.setFailed(message); + } else { + core.info('PR lint passed.'); + } + + checkstyle: + name: Checkstyle + runs-on: ubuntu-24.04-arm + + steps: + - uses: actions/checkout@v5 + + - name: Set up JDK 17 + uses: actions/setup-java@v5 + with: + java-version: '17' + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: ${{ runner.os }}-gradle- + + - name: Run Checkstyle + run: ./gradlew :framework:checkstyleMain :framework:checkstyleTest :plugins:checkstyleMain + + - name: Upload Checkstyle reports + if: failure() + uses: actions/upload-artifact@v6 + with: + name: checkstyle-reports + path: | + framework/build/reports/checkstyle/ + plugins/build/reports/checkstyle/ diff --git a/.github/workflows/pr-reviewer.yml b/.github/workflows/pr-reviewer.yml new file mode 100644 index 00000000000..bf124acf576 --- /dev/null +++ b/.github/workflows/pr-reviewer.yml @@ -0,0 +1,144 @@ +name: Auto Assign Reviewers + +on: + pull_request_target: + branches: [ 'develop', 'release_**' ] + types: [ opened, edited, reopened ] + +jobs: + assign-reviewers: + name: Assign Reviewers by Scope + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - name: Assign reviewers based on PR title scope + uses: actions/github-script@v8 + with: + script: | + const title = context.payload.pull_request.title; + const prAuthor = context.payload.pull_request.user.login; + + // ── Scope → Reviewer mapping ────────────────────────────── + const scopeReviewers = { + 'framework': ['xxo1shine', '317787106'], + 'chainbase': ['halibobo1205', 'lvs0075'], + 'db': ['halibobo1205', 'xxo1shine'], + 'trie': ['halibobo1205', '317787106'], + 'actuator': ['yanghang8612', 'lxcmyf'], + 'consensus': ['lvs0075', 'xxo1shine'], + 'protocol': ['lvs0075', 'waynercheung'], + 'common': ['xxo1shine', 'lxcmyf'], + 'crypto': ['Federico2014', '3for'], + 'net': ['317787106', 'xxo1shine'], + 'vm': ['yanghang8612', 'CodeNinjaEvan'], + 'tvm': ['yanghang8612', 'CodeNinjaEvan'], + 'jsonrpc': ['0xbigapple', 'bladehan1'], + 'rpc': ['317787106', 'Sunny6889'], + 'http': ['Sunny6889', 'bladehan1'], + 'event': ['xxo1shine', '0xbigapple'], + 'config': ['317787106', 'halibobo1205'], + 'backup': ['xxo1shine', '317787106'], + 'lite': ['bladehan1', 'halibobo1205'], + 'toolkit': ['halibobo1205', 'Sunny6889'], + 'plugins': ['halibobo1205', 'Sunny6889'], + 'docker': ['3for', 'kuny0707'], + 'test': ['bladehan1', 'lxcmyf'], + 'metrics': ['halibobo1205', 'Sunny6889'], + 'api': ['0xbigapple', 'waynercheung', 'bladehan1'], + 'ci': ['bladehan1', 'halibobo1205'], + }; + const defaultReviewers = ['halibobo1205', '317787106']; + + // ── Normalize helper ───────────────────────────────────── + // Strip spaces, hyphens, underscores and lower-case so that + // "VM", " json rpc ", "chain-base", "Json_Rpc" all normalize + // to their canonical key form ("vm", "jsonrpc", "chainbase"). + const normalize = s => s.toLowerCase().replace(/[\s\-_]/g, ''); + + // ── Extract scope from conventional commit title ────────── + // Format: type(scope): description + // Also supports: type(scope1,scope2): description + const scopeMatch = title.match(/^\w+\(([^)]+)\):/); + const rawScope = scopeMatch ? scopeMatch[1] : null; + + core.info(`PR title : ${title}`); + core.info(`Raw scope: ${rawScope || '(none)'}`); + + // ── Skip if reviewers already assigned ────────────────── + const pr = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + }); + const existing = pr.data.requested_reviewers || []; + if (existing.length > 0) { + core.info(`Reviewers already assigned (${existing.map(r => r.login).join(', ')}). Skipping.`); + return; + } + + // ── Determine reviewers ─────────────────────────────────── + // 1. Split by comma to support multi-scope: feat(vm,rpc): ... + // 2. Normalize each scope token + // 3. Match against keys: exact match first, then contains match + // (longest key wins to avoid "net" matching inside "jsonrpc") + let matched = new Set(); + let matchedScopes = []; + + if (rawScope) { + const tokens = rawScope.split(',').map(s => normalize(s.trim())); + // Pre-sort keys by length descending so longer keys match first + const sortedKeys = Object.keys(scopeReviewers) + .sort((a, b) => b.length - a.length); + + for (const token of tokens) { + if (!token) continue; + // Exact match + if (scopeReviewers[token]) { + matchedScopes.push(token); + scopeReviewers[token].forEach(r => matched.add(r)); + continue; + } + // Contains match: token contains a key, or key contains token + // Prefer longest key that matches + const found = sortedKeys.find(k => token.includes(k) || k.includes(token)); + if (found) { + matchedScopes.push(`${token}→${found}`); + scopeReviewers[found].forEach(r => matched.add(r)); + } + } + } + + let reviewers = matched.size > 0 + ? [...matched] + : defaultReviewers; + + core.info(`Matched scopes: ${matchedScopes.length > 0 ? matchedScopes.join(', ') : '(none — using default)'}`); + core.info(`Candidate reviewers: ${reviewers.join(', ')}`); + + // Exclude the PR author from the reviewer list + reviewers = reviewers.filter(r => r.toLowerCase() !== prAuthor.toLowerCase()); + + if (reviewers.length === 0) { + core.info('No eligible reviewers after excluding PR author. Skipping.'); + return; + } + + core.info(`Assigning reviewers: ${reviewers.join(', ')}`); + + // ── Request reviews ─────────────────────────────────────── + try { + await github.rest.pulls.requestReviewers({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + reviewers: reviewers, + }); + core.info('Reviewers assigned successfully.'); + } catch (error) { + // If a reviewer is not a collaborator the API returns 422; + // log the error but do not fail the workflow. + core.warning(`Failed to assign some reviewers: ${error.message}`); + } diff --git a/.github/workflows/system-test.yml b/.github/workflows/system-test.yml new file mode 100644 index 00000000000..f6184fb0efc --- /dev/null +++ b/.github/workflows/system-test.yml @@ -0,0 +1,95 @@ +name: System Test + +on: + push: + branches: [ 'master', 'release_**' ] + pull_request: + branches: [ 'develop', 'release_**' ] + types: [ opened, synchronize, reopened ] + paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', + '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', + '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + system-test: + name: System Test (JDK 8 / x86_64) + runs-on: ubuntu-latest + timeout-minutes: 60 + + steps: + - name: Set up JDK 8 + uses: actions/setup-java@v5 + with: + java-version: '8' + distribution: 'temurin' + + - name: Clone system-test + uses: actions/checkout@v5 + with: + repository: tronprotocol/system-test + ref: release_workflow + path: system-test + + - name: Checkout java-tron + uses: actions/checkout@v5 + with: + path: java-tron + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-system-test-${{ hashFiles('java-tron/**/*.gradle', 'java-tron/**/gradle-wrapper.properties') }} + restore-keys: ${{ runner.os }}-gradle-system-test- + + - name: Build java-tron + working-directory: java-tron + run: ./gradlew clean build -x test --no-daemon + + - name: Copy config and start FullNode + run: | + cp system-test/testcase/src/test/resources/config-system-test.conf java-tron/ + cd java-tron + nohup java -jar build/libs/FullNode.jar --witness -c config-system-test.conf > fullnode.log 2>&1 & + echo "FullNode started, waiting for it to be ready..." + + MAX_ATTEMPTS=60 + INTERVAL=5 + for i in $(seq 1 $MAX_ATTEMPTS); do + if curl -s --fail "http://localhost:8090/wallet/getblockbynum?num=1" > /dev/null 2>&1; then + echo "FullNode is ready! (attempt $i)" + exit 0 + fi + echo "Waiting... (attempt $i/$MAX_ATTEMPTS)" + sleep $INTERVAL + done + + echo "FullNode failed to start within $((MAX_ATTEMPTS * INTERVAL)) seconds." + echo "=== FullNode log (last 50 lines) ===" + tail -50 fullnode.log || true + exit 1 + + - name: Run system tests + working-directory: system-test + run: | + if [ ! -f solcDIR/solc-linux-0.8.6 ]; then + echo "ERROR: solc binary not found at solcDIR/solc-linux-0.8.6" + exit 1 + fi + cp solcDIR/solc-linux-0.8.6 solcDIR/solc + ./gradlew clean --no-daemon + ./gradlew --info stest --no-daemon + + - name: Upload FullNode log + if: always() + uses: actions/upload-artifact@v6 + with: + name: fullnode-log + path: java-tron/fullnode.log + if-no-files-found: warn From 52d7d9d23e367fc2d9be2e508eed88609af220f4 Mon Sep 17 00:00:00 2001 From: halibobo1205 Date: Tue, 2 Jun 2026 21:24:55 +0800 Subject: [PATCH 30/57] feat(config): fix git.properties NPE --- framework/src/main/java/org/tron/core/config/args/Args.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 46695986c1f..499c78b8c54 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -342,6 +342,10 @@ private static String getCommitIdAbbrev() { try { InputStream in = Thread.currentThread() .getContextClassLoader().getResourceAsStream("git.properties"); + if (in == null) { + logger.warn("git.properties not found on classpath"); + return ""; + } properties.load(in); } catch (IOException e) { logger.warn("Load resource failed,git.properties {}", e.getMessage()); From a79693e4508c05650cc474f23e7f97451d2861ec Mon Sep 17 00:00:00 2001 From: halibobo1205 Date: Tue, 2 Jun 2026 21:57:17 +0800 Subject: [PATCH 31/57] update a new version. version name:GreatVoyage-v4.8.1-6-g52d7d9d23e,version code:18643 --- framework/src/main/java/org/tron/program/Version.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 2a8f53b5ef2..4240b81a715 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -2,8 +2,8 @@ public class Version { - public static final String VERSION_NAME = "GreatVoyage-v4.8.0.1-1-g44a4bc8263"; - public static final String VERSION_CODE = "18636"; + public static final String VERSION_NAME = "GreatVoyage-v4.8.1-6-g52d7d9d23e"; + public static final String VERSION_CODE = "18643"; private static final String VERSION = "4.8.1.1"; public static String getVersion() { From a41321ca15f3a74d71a4937719e93cf043200703 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Wed, 3 Jun 2026 13:48:26 +0800 Subject: [PATCH 32/57] fix(net): exit solidity node block sync on shutdown (#6804) --- README.md | 2 +- common/src/main/resources/reference.conf | 11 - .../org/tron/core/net/TronNetDelegate.java | 4 +- .../java/org/tron/program/SolidityNode.java | 8 +- .../core/zksnark/ShieldedReceiveTest.java | 279 ++++++++++-------- .../org/tron/program/SolidityNodeTest.java | 42 +++ 6 files changed, 209 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 575409b3a96..be84b44150b 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ The TRON network is mainly divided into: - **Private Networks** Customized TRON networks set up by private entities for testing, development, or specific use cases. -Network selection is performed by specifying the appropriate configuration file upon full-node startup. Mainnet configuration: [config.conf](framework/src/main/resources/config.conf); Nile testnet configuration: [config-nile.conf](https://github.com/tron-nile-testnet/nile-testnet/blob/master/framework/src/main/resources/config-nile.conf) +Network selection is performed by specifying the appropriate configuration file upon full-node startup. Built-in configuration template: [reference.conf](common/src/main/resources/reference.conf); Mainnet configuration: [config.conf](framework/src/main/resources/config.conf); Nile testnet configuration: [config-nile.conf](https://github.com/tron-nile-testnet/nile-testnet/blob/master/framework/src/main/resources/config-nile.conf) ### 1. Join the TRON main network Launch a main-network full node with the built-in default configuration: diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index be3fefb2645..8b69f6ef917 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -25,17 +25,6 @@ # Key naming rules (required for ConfigBeanFactory auto-binding): # - Use standard camelCase: maxConnections, syncFetchBatchNum, etc. # -# Keys that cannot auto-bind (handled via normalizeNonStandardKeys() or manual reads): -# -# 1. committee.pBFTExpireNum / committee.allowPBFT — normalized to camelCase in -# CommitteeConfig.normalizeNonStandardKeys() before ConfigBeanFactory binding. -# -# 2. node.isOpenFullTcpDisconnect — normalized to "openFullTcpDisconnect" in -# NodeConfig.normalizeNonStandardKeys() before ConfigBeanFactory binding. -# -# 3. node.shutdown.BlockTime/BlockHeight/BlockCount — optional PascalCase nested keys; -# read manually in NodeConfig.fromConfig() after ConfigBeanFactory binding. -# # ============================================================================= net { diff --git a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java index 5f1540b672e..23050f5218d 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java +++ b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java @@ -111,7 +111,9 @@ public class TronNetDelegate { @PostConstruct public void init() { hitThread = new Thread(() -> { - LockSupport.park(); + while (!hitDown && !Thread.currentThread().isInterrupted()) { + LockSupport.park(); + } // to Guarantee Some other thread invokes unpark with the current thread as the target if (hitDown && exit) { System.exit(0); diff --git a/framework/src/main/java/org/tron/program/SolidityNode.java b/framework/src/main/java/org/tron/program/SolidityNode.java index 9dbe92fb78e..beb9ede2e14 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -122,7 +122,7 @@ private void getBlock() { logger.info("getBlock interrupted, exiting."); return; } catch (Exception e) { - if (!flag) { + if (!flag || tronNetDelegate.isHitDown()) { logger.info("getBlock stopped during shutdown, last block: {}.", blockNum); return; } @@ -185,6 +185,10 @@ private Block getBlockByNum(long blockNum) { sleep(exceptionSleepTime); } } catch (Exception e) { + if (!flag || tronNetDelegate.isHitDown()) { + logger.info("getBlockByNum stopped during shutdown, block: {}.", blockNum); + break; + } logger.error("Failed to get block: {}, reason: {}.", blockNum, e.getMessage()); sleep(exceptionSleepTime); } @@ -202,7 +206,7 @@ private long getLastSolidityBlockNum() { blockNum, remoteBlockNum, System.currentTimeMillis() - time); return blockNum; } catch (Exception e) { - if (!flag) { + if (!flag || tronNetDelegate.isHitDown()) { logger.info("getLastSolidityBlockNum stopped during shutdown."); return 0; } diff --git a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java index 0d14d6fbc26..5854b731e97 100755 --- a/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java @@ -8,6 +8,7 @@ import com.google.protobuf.Any; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import java.lang.reflect.Field; import java.security.SignatureException; import java.util.Arrays; import java.util.HashSet; @@ -45,6 +46,8 @@ import org.tron.common.zksnark.LibrustzcashParam.IvkToPkdParams; import org.tron.common.zksnark.LibrustzcashParam.OutputProofParams; import org.tron.common.zksnark.LibrustzcashParam.SpendSigParams; +import org.tron.consensus.dpos.DposSlot; +import org.tron.consensus.dpos.DposTask; import org.tron.core.Wallet; import org.tron.core.actuator.Actuator; import org.tron.core.actuator.ActuatorCreator; @@ -140,14 +143,16 @@ public class ShieldedReceiveTest extends BaseTest { @Resource private ConsensusService consensusService; @Resource + private DposTask dposTask; + @Resource private Wallet wallet; @Resource - private TransactionUtil transactionUtil; + private DposSlot dposSlot; private static boolean init; static { - Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, SHIELD_CONF); + Args.setParam(new String[] {"--output-directory", dbPath(), "-w"}, SHIELD_CONF); ADDRESS_ONE_PRIVATE_KEY = getRandomPrivateKey(); FROM_ADDRESS = getHexAddressByPrivateKey(ADDRESS_ONE_PRIVATE_KEY); } @@ -331,7 +336,7 @@ public void testBroadcastBeforeAllowZksnark() //Add public address sign transactionCap = TransactionUtils.addTransactionSign(transactionCap.getInstance(), - ADDRESS_ONE_PRIVATE_KEY, chainBaseManager.getAccountStore()); + ADDRESS_ONE_PRIVATE_KEY, chainBaseManager.getAccountStore()); try { dbManager.pushTransaction(transactionCap); } catch (Exception e) { @@ -433,7 +438,7 @@ public String[] generateSpendAndOutputParams() throws ZksnarkException, BadItemE boolean ok2 = JLibrustzcash.librustzcashSaplingCheckOutput(checkOutputParams); Assert.assertTrue(ok2); - return new String[]{ByteArray.toHexString(checkSpendParamsData), + return new String[] {ByteArray.toHexString(checkSpendParamsData), ByteArray.toHexString(dataToBeSigned), ByteArray.toHexString(checkOutputParams.encode())}; } @@ -2402,128 +2407,158 @@ public void pushSameSkAndScanAndSpend() throws Exception { assert ecKey != null; byte[] witnessAddress = ecKey.getAddress(); WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(witnessAddress)); - chainBaseManager.addWitness(ByteString.copyFrom(witnessAddress)); - - //sometimes generate block failed, try several times. - long time = System.currentTimeMillis(); - Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey); - dbManager.pushBlock(new BlockCapsule(block)); - - //create transactions - chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); - chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); - ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); - - // generate spend proof - SpendingKey sk = SpendingKey - .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); - ExpandedSpendingKey expsk = sk.expandedSpendingKey(); - byte[] senderOvk = expsk.getOvk(); - PaymentAddress address = sk.defaultAddress(); - Note note = new Note(address, 1000 * 1000000L); - IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer(note.cm()); - byte[] anchor = voucher.root().getContent().toByteArray(); - chainBaseManager.getMerkleContainer() - .putMerkleTreeIntoStore(anchor, voucher.getVoucherCapsule().getTree()); - builder.addSpend(expsk, note, anchor, voucher); - - // generate output proof - SpendingKey sk2 = SpendingKey.random(); - FullViewingKey fullViewingKey = sk2.fullViewingKey(); - IncomingViewingKey incomingViewingKey = fullViewingKey.inViewingKey(); - - byte[] memo = org.tron.keystore.Wallet.generateRandomBytes(512); - - //send coin to 2 different address generated by same sk - DiversifierT d1 = DiversifierT.random(); - PaymentAddress paymentAddress1 = incomingViewingKey.address(d1).get(); - builder.addOutput(senderOvk, paymentAddress1, - (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2, memo); - - DiversifierT d2 = DiversifierT.random(); - PaymentAddress paymentAddress2 = incomingViewingKey.address(d2).get(); - builder.addOutput(senderOvk, paymentAddress2, - (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2, memo); - - TransactionCapsule transactionCap = builder.build(); + // Stop the consensus task before modifying the witness schedule: DposTask uses the same + // localwitness key and would otherwise race to produce blocks at the same slot, + // triggering fork resolution and making the test slow. + consensusService.stop(); + try { + chainBaseManager.addWitness(ByteString.copyFrom(witnessAddress)); + + long time = nextScheduledTime(witnessCapsule.getAddress()); + Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey); + dbManager.pushBlock(new BlockCapsule(block)); + + //create transactions + chainBaseManager.getDynamicPropertiesStore().saveAllowShieldedTransaction(1); + chainBaseManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(1000 * 1000000L); + ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet); + + // generate spend proof + SpendingKey sk = SpendingKey + .decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee"); + ExpandedSpendingKey expsk = sk.expandedSpendingKey(); + byte[] senderOvk = expsk.getOvk(); + PaymentAddress address = sk.defaultAddress(); + Note note = new Note(address, 1000 * 1000000L); + IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer(note.cm()); + byte[] anchor = voucher.root().getContent().toByteArray(); + chainBaseManager.getMerkleContainer() + .putMerkleTreeIntoStore(anchor, voucher.getVoucherCapsule().getTree()); + builder.addSpend(expsk, note, anchor, voucher); + + // generate output proof + SpendingKey sk2 = SpendingKey.random(); + FullViewingKey fullViewingKey = sk2.fullViewingKey(); + IncomingViewingKey incomingViewingKey = fullViewingKey.inViewingKey(); + + byte[] memo = org.tron.keystore.Wallet.generateRandomBytes(512); + + //send coin to 2 different address generated by same sk + DiversifierT d1 = DiversifierT.random(); + PaymentAddress paymentAddress1 = incomingViewingKey.address(d1).get(); + builder.addOutput(senderOvk, paymentAddress1, + (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2, memo); + + DiversifierT d2 = DiversifierT.random(); + PaymentAddress paymentAddress2 = incomingViewingKey.address(d2).get(); + builder.addOutput(senderOvk, paymentAddress2, + (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2, memo); - byte[] trxId = transactionCap.getTransactionId().getBytes(); - boolean ok = dbManager.pushTransaction(transactionCap); - Assert.assertTrue(ok); + TransactionCapsule transactionCap = builder.build(); - Thread.sleep(500); - //package transaction to block - block = getSignedBlock(witnessCapsule.getAddress(), time + 3000, privateKey); - dbManager.pushBlock(new BlockCapsule(block)); - - BlockCapsule blockCapsule3 = new BlockCapsule(wallet.getNowBlock()); - Assert.assertEquals("blocknum != 2", 2, blockCapsule3.getNum()); - - block = getSignedBlock(witnessCapsule.getAddress(), time + 6000, privateKey); - dbManager.pushBlock(new BlockCapsule(block)); - - // scan note by ivk - byte[] receiverIvk = incomingViewingKey.getValue(); - DecryptNotes notes1 = wallet.scanNoteByIvk(0, 100, receiverIvk); - Assert.assertEquals(2, notes1.getNoteTxsCount()); - - // scan note by ivk and mark - DecryptNotesMarked notes3 = wallet.scanAndMarkNoteByIvk(0, 100, receiverIvk, - fullViewingKey.getAk(), fullViewingKey.getNk()); - Assert.assertEquals(2, notes3.getNoteTxsCount()); - - // scan note by ovk - DecryptNotes notes2 = wallet.scanNoteByOvk(0, 100, senderOvk); - Assert.assertEquals(2, notes2.getNoteTxsCount()); - - // to spend received note above. - ZenTransactionBuilder builder2 = new ZenTransactionBuilder(wallet); - - //query merkleinfo - OutputPointInfo.Builder request = OutputPointInfo.newBuilder(); - for (int i = 0; i < notes1.getNoteTxsCount(); i++) { - OutputPoint.Builder outPointBuild = OutputPoint.newBuilder(); - outPointBuild.setHash(ByteString.copyFrom(trxId)); - outPointBuild.setIndex(i); - request.addOutPoints(outPointBuild.build()); - } - request.setBlockNum(1); - IncrementalMerkleVoucherInfo merkleVoucherInfo = wallet - .getMerkleTreeVoucherInfo(request.build()); - - //build spend proof. allow only one note in spend - ExpandedSpendingKey expsk2 = sk2.expandedSpendingKey(); - for (int i = 0; i < 1; i++) { - org.tron.api.GrpcAPI.Note grpcNote = notes1.getNoteTxs(i).getNote(); - PaymentAddress paymentAddress = KeyIo.decodePaymentAddress(grpcNote.getPaymentAddress()); - Note note2 = new Note(paymentAddress.getD(), - paymentAddress.getPkD(), - grpcNote.getValue(), - grpcNote.getRcm().toByteArray() - ); + byte[] trxId = transactionCap.getTransactionId().getBytes(); + boolean ok = dbManager.pushTransaction(transactionCap); + Assert.assertTrue(ok); + + Thread.sleep(500); + //package transaction to block + long expectedBlockNum = chainBaseManager.getDynamicPropertiesStore() + .getLatestBlockHeaderNumber() + 1; + block = getSignedBlock(witnessCapsule.getAddress(), + nextScheduledTime(witnessCapsule.getAddress()), privateKey); + dbManager.pushBlock(new BlockCapsule(block)); + + BlockCapsule blockCapsule3 = new BlockCapsule(wallet.getNowBlock()); + Assert.assertEquals("unexpected block number", expectedBlockNum, blockCapsule3.getNum()); + + block = getSignedBlock(witnessCapsule.getAddress(), + nextScheduledTime(witnessCapsule.getAddress()), privateKey); + dbManager.pushBlock(new BlockCapsule(block)); + + // scan note by ivk + byte[] receiverIvk = incomingViewingKey.getValue(); + DecryptNotes notes1 = wallet.scanNoteByIvk(0, 100, receiverIvk); + Assert.assertEquals(2, notes1.getNoteTxsCount()); + + // scan note by ivk and mark + DecryptNotesMarked notes3 = wallet.scanAndMarkNoteByIvk(0, 100, receiverIvk, + fullViewingKey.getAk(), fullViewingKey.getNk()); + Assert.assertEquals(2, notes3.getNoteTxsCount()); + + // scan note by ovk + DecryptNotes notes2 = wallet.scanNoteByOvk(0, 100, senderOvk); + Assert.assertEquals(2, notes2.getNoteTxsCount()); + + // to spend received note above. + ZenTransactionBuilder builder2 = new ZenTransactionBuilder(wallet); + + //query merkleinfo + OutputPointInfo.Builder request = OutputPointInfo.newBuilder(); + for (int i = 0; i < notes1.getNoteTxsCount(); i++) { + OutputPoint.Builder outPointBuild = OutputPoint.newBuilder(); + outPointBuild.setHash(ByteString.copyFrom(trxId)); + outPointBuild.setIndex(i); + request.addOutPoints(outPointBuild.build()); + } + request.setBlockNum(1); + IncrementalMerkleVoucherInfo merkleVoucherInfo = wallet + .getMerkleTreeVoucherInfo(request.build()); + + //build spend proof. allow only one note in spend + ExpandedSpendingKey expsk2 = sk2.expandedSpendingKey(); + for (int i = 0; i < 1; i++) { + org.tron.api.GrpcAPI.Note grpcNote = notes1.getNoteTxs(i).getNote(); + PaymentAddress paymentAddress = KeyIo.decodePaymentAddress(grpcNote.getPaymentAddress()); + Note note2 = new Note(paymentAddress.getD(), + paymentAddress.getPkD(), + grpcNote.getValue(), + grpcNote.getRcm().toByteArray() + ); + + IncrementalMerkleVoucherContainer voucher2 = + new IncrementalMerkleVoucherContainer( + new IncrementalMerkleVoucherCapsule(merkleVoucherInfo.getVouchers(i))); + byte[] anchor2 = voucher2.root().getContent().toByteArray(); + builder2.addSpend(expsk2, note2, anchor2, voucher2); + } - IncrementalMerkleVoucherContainer voucher2 = - new IncrementalMerkleVoucherContainer( - new IncrementalMerkleVoucherCapsule(merkleVoucherInfo.getVouchers(i))); - byte[] anchor2 = voucher2.root().getContent().toByteArray(); - builder2.addSpend(expsk2, note2, anchor2, voucher2); + //build output proof + SpendingKey sk3 = SpendingKey.random(); + FullViewingKey fvk3 = sk3.fullViewingKey(); + IncomingViewingKey ivk3 = fvk3.inViewingKey(); + + DiversifierT d3 = DiversifierT.random(); + PaymentAddress paymentAddress3 = incomingViewingKey.address(d3).get(); + byte[] memo3 = org.tron.keystore.Wallet.generateRandomBytes(512); + builder2.addOutput(expsk2.getOvk(), paymentAddress3, + (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2 - wallet + .getShieldedTransactionFee(), memo3); + + TransactionCapsule transactionCap2 = builder2.build(); + boolean ok2 = dbManager.pushTransaction(transactionCap2); + Assert.assertTrue(ok2); + } finally { + // DposTask.init() does not reset isRunning (it stays false after stop()), so force it back + // to true via reflection before restarting. + Field isRunning = DposTask.class.getDeclaredField("isRunning"); + isRunning.setAccessible(true); + isRunning.set(dposTask, true); + consensusService.start(); + } + } + + // Returns the earliest timestamp at which witnessAddr is the DPoS-scheduled producer, + // relative to the current chain head. Using this avoids relying on the genesis-only + // bypass in validBlock() (latestBlockHeaderNumber == 0) when prior tests have pushed blocks. + private long nextScheduledTime(ByteString witnessAddr) { + int size = chainBaseManager.getWitnessScheduleStore().getActiveWitnesses().size(); + for (long slot = 1; slot <= size; slot++) { + if (dposSlot.getScheduledWitness(slot).equals(witnessAddr)) { + return dposSlot.getTime(slot); + } } - - //build output proof - SpendingKey sk3 = SpendingKey.random(); - FullViewingKey fvk3 = sk3.fullViewingKey(); - IncomingViewingKey ivk3 = fvk3.inViewingKey(); - - DiversifierT d3 = DiversifierT.random(); - PaymentAddress paymentAddress3 = incomingViewingKey.address(d3).get(); - byte[] memo3 = org.tron.keystore.Wallet.generateRandomBytes(512); - builder2.addOutput(expsk2.getOvk(), paymentAddress3, - (1000 * 1000000L - wallet.getShieldedTransactionFee()) / 2 - wallet - .getShieldedTransactionFee(), memo3); - - TransactionCapsule transactionCap2 = builder2.build(); - boolean ok2 = dbManager.pushTransaction(transactionCap2); - Assert.assertTrue(ok2); + throw new IllegalStateException("No scheduled slot for witness within " + + size + " slots: " + ByteArray.toHexString(witnessAddr.toByteArray())); } @Test diff --git a/framework/src/test/java/org/tron/program/SolidityNodeTest.java b/framework/src/test/java/org/tron/program/SolidityNodeTest.java index 7842eed8484..ade00374bc4 100755 --- a/framework/src/test/java/org/tron/program/SolidityNodeTest.java +++ b/framework/src/test/java/org/tron/program/SolidityNodeTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -329,6 +330,47 @@ public void testGetBlockByNumWhenClosed() throws Exception { } } + /** + * getBlockByNum() must break immediately — without a 1-second sleep — when a + * gRPC exception is thrown while flag races to false (the P3 shutdown-race fix). + * The invocation time is measured directly so the assertion is independent of + * Spring-context startup overhead. + */ + @Test(timeout = 5000) + public void testGetBlockByNumNoErrorOnExceptionDuringShutdown() throws Exception { + Method m = SolidityNode.class.getDeclaredMethod("getBlockByNum", long.class); + m.setAccessible(true); + Field clientField = getField("databaseGrpcClient"); + Object origClient = clientField.get(solidityNode); + setFlag(true); // precondition: while(flag) must be entered; do not rely on test-ordering + try { + DatabaseGrpcClient mockClient = mock(DatabaseGrpcClient.class); + // flag races to false inside the gRPC call — exact close() race + Mockito.when(mockClient.getBlock(42L)).thenAnswer(inv -> { + setFlag(false); + throw new RuntimeException("channel closed during shutdown"); + }); + clientField.set(solidityNode, mockClient); + + long start = System.currentTimeMillis(); + InvocationTargetException t = assertThrows(InvocationTargetException.class, () -> { + m.invoke(solidityNode, 42L); + }); + assertTrue(t.getCause() instanceof RuntimeException); + assertEquals("SolidityNode is closing.", t.getCause().getMessage()); + long elapsed = System.currentTimeMillis() - start; + // Without the fix the catch sleeps exceptionSleepTime (1000 ms) before + // re-checking the while condition. With the fix it breaks immediately. + assertTrue("Expected break without sleep (<500 ms), got " + elapsed + " ms", + elapsed < 500); + // No retry: exactly one gRPC call must be made. + Mockito.verify(mockClient, Mockito.times(1)).getBlock(42L); + } finally { + setFlag(true); + clientField.set(solidityNode, origClient); + } + } + // ── getLastSolidityBlockNum() ───────────────────────────────────────────────── /** From 7cda02d098b5bef48a86d6a2425f3a81d1a351d5 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang <50477615+warku123@users.noreply.github.com> Date: Wed, 3 Jun 2026 15:04:08 +0800 Subject: [PATCH 33/57] feat(ci): add integration test workflows (single-node + multinode) (#6789) --- .../workflows/integration-test-multinode.yml | 119 ++++++++++++++++++ .../integration-test-single-node.yml | 80 ++++++++++++ .github/workflows/pr-cancel.yml | 66 ++++++---- .github/workflows/system-test.yml | 95 -------------- 4 files changed, 238 insertions(+), 122 deletions(-) create mode 100644 .github/workflows/integration-test-multinode.yml create mode 100644 .github/workflows/integration-test-single-node.yml delete mode 100644 .github/workflows/system-test.yml diff --git a/.github/workflows/integration-test-multinode.yml b/.github/workflows/integration-test-multinode.yml new file mode 100644 index 00000000000..99a7b54e022 --- /dev/null +++ b/.github/workflows/integration-test-multinode.yml @@ -0,0 +1,119 @@ +name: Integration Test Multinode (Full) + +on: + push: + branches: [ 'master', 'release_**' ] + pull_request: + branches: [ 'develop', 'release_**' ] + types: [ opened, synchronize, reopened ] + paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', + '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', + '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + multinode-full: + name: Integration Test Multinode Full (JDK 8 / x86_64) + runs-on: ubuntu-latest + timeout-minutes: 60 + + steps: + - name: Checkout java-tron + uses: actions/checkout@v5 + + - name: Set up JDK 8 + uses: actions/setup-java@v5 + with: + java-version: '8' + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-multinode-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: ${{ runner.os }}-gradle-multinode- + + - name: Build FullNode.jar + run: ./gradlew clean build -x test --no-daemon + + - name: Build local java-tron Docker image (wraps PR-built FullNode.jar) + run: | + mkdir -p /tmp/tron-image + cp build/libs/FullNode.jar /tmp/tron-image/ + cat > /tmp/tron-image/Dockerfile <<'EOF' + FROM tronprotocol/java-tron:latest + COPY FullNode.jar /java-tron/lib/FullNode.jar + EOF + docker build -t java-tron-local:pr /tmp/tron-image + + - name: Pull integration-test image + run: docker pull troninfra/troninfra-ci:latest + + - name: Extract compose configs to host (for DinD path-alignment) + run: | + # start-multinode.sh builds HOST_COMPOSE_DIR as: + # ${HOST_WORKDIR}/docker/multi-node + # so the files must live at $HOST_WORKDIR/docker/multi-node/ on the + # host. Set HOST_WORKDIR to the workspace root and extract + # /app/docker/ 1:1 into workspace/docker/ — the subdirectories + # (multi-node/, single-node/) don't collide with java-tron's own + # docker/ files. + docker create --name it-extract troninfra/troninfra-ci:latest + docker cp it-extract:/app/docker/. "${{ github.workspace }}/docker/" + docker rm -f it-extract + + - name: Run multinode full tests + run: | + # --network host: multinode tests talk to nodes via 127.0.0.1:50051 etc. + # DinD socket + HOST_WORKDIR path-alignment lets the container orchestrate + # the 3-witness compose stack via the host daemon. + # Don't override --workdir so the container's default /app entrypoint works. + docker run --name integration-multinode \ + --network host \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v "${{ github.workspace }}:${{ github.workspace }}" \ + -v "${{ github.workspace }}/docker/multi-node:/app/docker/multi-node" \ + -e HOST_WORKDIR="${{ github.workspace }}" \ + -e TRON_IMAGE=java-tron-local:pr \ + -e JAVA_HOME=/usr/lib/jvm/temurin-8 \ + -e JAVA_HOME_17=/opt/java/openjdk \ + troninfra/troninfra-ci:latest \ + --multinode --clean + + - name: Extract test reports from container + if: always() + run: | + mkdir -p integration-reports + docker cp integration-multinode:/app/build/reports/. integration-reports/reports/ 2>/dev/null || true + docker cp integration-multinode:/app/build/test-results/. integration-reports/test-results/ 2>/dev/null || true + docker cp integration-multinode:/app/build/test-output.log integration-reports/ 2>/dev/null || true + + - name: Collect witness node logs + if: always() + run: | + mkdir -p integration-reports/node-logs + for c in tron-mn-node1 tron-mn-node2 tron-mn-node3 tron-mn-mongodb; do + docker logs "$c" > "integration-reports/node-logs/${c}.log" 2>&1 || true + done + + - name: Tear down compose stack + if: always() + run: | + docker rm -f tron-mn-node1 tron-mn-node2 tron-mn-node3 tron-mn-mongodb 2>/dev/null || true + docker network rm multi-node_tron-net 2>/dev/null || true + docker rm -f integration-multinode 2>/dev/null || true + + - name: Upload test reports + if: always() + uses: actions/upload-artifact@v6 + with: + name: integration-multinode-report + path: integration-reports/ + if-no-files-found: warn diff --git a/.github/workflows/integration-test-single-node.yml b/.github/workflows/integration-test-single-node.yml new file mode 100644 index 00000000000..a68c243efcc --- /dev/null +++ b/.github/workflows/integration-test-single-node.yml @@ -0,0 +1,80 @@ +name: Integration Test Single Node (Full) + +on: + push: + branches: [ 'master', 'release_**' ] + pull_request: + branches: [ 'develop', 'release_**' ] + types: [ opened, synchronize, reopened ] + paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', + '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', + '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + integration: + name: Integration Test Single Node Full (JDK 8 / x86_64) + runs-on: ubuntu-latest + timeout-minutes: 45 + + steps: + - name: Checkout java-tron + uses: actions/checkout@v5 + + - name: Set up JDK 8 + uses: actions/setup-java@v5 + with: + java-version: '8' + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-integration-test-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }} + restore-keys: ${{ runner.os }}-gradle-integration-test- + + - name: Build FullNode.jar + run: ./gradlew clean build -x test --no-daemon + + - name: Pull integration-test image + run: docker pull troninfra/troninfra-ci:latest + + - name: Run integration tests + run: | + # JAVA_HOME=JDK 8 so FullNode runs on the same JVM family as + # production (a few assertions check `java.version` starts with + # "1.8"). JAVA_HOME_17 keeps Gradle on JDK 17 for the test + # tooling, which requires Java 17. + docker run --name integration-test \ + -e FULLNODE_JAR=/javatron/FullNode.jar \ + -e JAVA_HOME=/usr/lib/jvm/temurin-8 \ + -e JAVA_HOME_17=/opt/java/openjdk \ + -v "${{ github.workspace }}/build/libs/FullNode.jar:/javatron/FullNode.jar:ro" \ + troninfra/troninfra-ci:latest \ + --clean + + - name: Extract test reports from container + if: always() + run: | + mkdir -p integration-reports + docker cp integration-test:/app/build/reports/. integration-reports/reports/ 2>/dev/null || true + docker cp integration-test:/app/build/test-results/. integration-reports/test-results/ 2>/dev/null || true + docker cp integration-test:/app/build/test-output.log integration-reports/ 2>/dev/null || true + docker cp integration-test:/app/node/node.log integration-reports/ 2>/dev/null || true + docker cp integration-test:/app/node/data/logs/tron.log integration-reports/ 2>/dev/null || true + docker rm -f integration-test 2>/dev/null || true + + - name: Upload test reports + if: always() + uses: actions/upload-artifact@v6 + with: + name: integration-test-report + path: integration-reports/ + if-no-files-found: warn diff --git a/.github/workflows/pr-cancel.yml b/.github/workflows/pr-cancel.yml index bbd0e68c235..3213026d3f9 100644 --- a/.github/workflows/pr-cancel.yml +++ b/.github/workflows/pr-cancel.yml @@ -13,43 +13,55 @@ jobs: if: github.event.pull_request.merged == false runs-on: ubuntu-latest steps: - - name: Cancel PR Build and System Test + - name: Cancel PR Build and Integration Tests uses: actions/github-script@v8 with: script: | - const workflows = ['pr-build.yml', 'system-test.yml', 'codeql.yml']; + const workflows = [ + 'pr-build.yml', + 'codeql.yml', + 'integration-test-single-node.yml', + 'integration-test-multinode.yml', + ]; const headSha = context.payload.pull_request.head.sha; const prNumber = context.payload.pull_request.number; for (const workflowId of workflows) { - for (const status of ['in_progress', 'queued']) { - const runs = await github.paginate( - github.rest.actions.listWorkflowRuns, - { - owner: context.repo.owner, - repo: context.repo.repo, - workflow_id: workflowId, - status, - event: 'pull_request', - per_page: 100, - }, - (response) => response.data.workflow_runs - ); - - for (const run of runs) { - if (!run) { - continue; - } - const prs = Array.isArray(run.pull_requests) ? run.pull_requests : []; - const isTargetPr = prs.length === 0 || prs.some((pr) => pr.number === prNumber); - if (run.head_sha === headSha && isTargetPr) { - await github.rest.actions.cancelWorkflowRun({ + // Wrap each workflow iteration so a missing / renamed file + // doesn't take down the whole cancel job — other workflows + // in the list still get processed. + try { + for (const status of ['in_progress', 'queued']) { + const runs = await github.paginate( + github.rest.actions.listWorkflowRuns, + { owner: context.repo.owner, repo: context.repo.repo, - run_id: run.id, - }); - console.log(`Cancelled ${workflowId} run #${run.id} (${status})`); + workflow_id: workflowId, + status, + event: 'pull_request', + per_page: 100, + }, + (response) => response.data.workflow_runs + ); + + for (const run of runs) { + if (!run) { + continue; + } + const prs = Array.isArray(run.pull_requests) ? run.pull_requests : []; + const isTargetPr = prs.length === 0 || prs.some((pr) => pr.number === prNumber); + if (run.head_sha === headSha && isTargetPr) { + await github.rest.actions.cancelWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: run.id, + }); + console.log(`Cancelled ${workflowId} run #${run.id} (${status})`); + } } } + } catch (err) { + console.log(`Skipping ${workflowId}: ${err.message}`); } } diff --git a/.github/workflows/system-test.yml b/.github/workflows/system-test.yml deleted file mode 100644 index f6184fb0efc..00000000000 --- a/.github/workflows/system-test.yml +++ /dev/null @@ -1,95 +0,0 @@ -name: System Test - -on: - push: - branches: [ 'master', 'release_**' ] - pull_request: - branches: [ 'develop', 'release_**' ] - types: [ opened, synchronize, reopened ] - paths-ignore: [ '**/*.md', '.gitignore', '**/.gitignore', '.editorconfig', - '.gitattributes', 'docs/**', 'CHANGELOG', '.github/ISSUE_TEMPLATE/**', - '.github/PULL_REQUEST_TEMPLATE/**', '.github/CODEOWNERS' ] - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -jobs: - system-test: - name: System Test (JDK 8 / x86_64) - runs-on: ubuntu-latest - timeout-minutes: 60 - - steps: - - name: Set up JDK 8 - uses: actions/setup-java@v5 - with: - java-version: '8' - distribution: 'temurin' - - - name: Clone system-test - uses: actions/checkout@v5 - with: - repository: tronprotocol/system-test - ref: release_workflow - path: system-test - - - name: Checkout java-tron - uses: actions/checkout@v5 - with: - path: java-tron - - - name: Cache Gradle packages - uses: actions/cache@v4 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-system-test-${{ hashFiles('java-tron/**/*.gradle', 'java-tron/**/gradle-wrapper.properties') }} - restore-keys: ${{ runner.os }}-gradle-system-test- - - - name: Build java-tron - working-directory: java-tron - run: ./gradlew clean build -x test --no-daemon - - - name: Copy config and start FullNode - run: | - cp system-test/testcase/src/test/resources/config-system-test.conf java-tron/ - cd java-tron - nohup java -jar build/libs/FullNode.jar --witness -c config-system-test.conf > fullnode.log 2>&1 & - echo "FullNode started, waiting for it to be ready..." - - MAX_ATTEMPTS=60 - INTERVAL=5 - for i in $(seq 1 $MAX_ATTEMPTS); do - if curl -s --fail "http://localhost:8090/wallet/getblockbynum?num=1" > /dev/null 2>&1; then - echo "FullNode is ready! (attempt $i)" - exit 0 - fi - echo "Waiting... (attempt $i/$MAX_ATTEMPTS)" - sleep $INTERVAL - done - - echo "FullNode failed to start within $((MAX_ATTEMPTS * INTERVAL)) seconds." - echo "=== FullNode log (last 50 lines) ===" - tail -50 fullnode.log || true - exit 1 - - - name: Run system tests - working-directory: system-test - run: | - if [ ! -f solcDIR/solc-linux-0.8.6 ]; then - echo "ERROR: solc binary not found at solcDIR/solc-linux-0.8.6" - exit 1 - fi - cp solcDIR/solc-linux-0.8.6 solcDIR/solc - ./gradlew clean --no-daemon - ./gradlew --info stest --no-daemon - - - name: Upload FullNode log - if: always() - uses: actions/upload-artifact@v6 - with: - name: fullnode-log - path: java-tron/fullnode.log - if-no-files-found: warn From ba5b0127439b14a8f6da77b878286e7ca5527f6e Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Wed, 3 Jun 2026 16:02:24 +0800 Subject: [PATCH 34/57] fix(merkle): build MerkleTree per-instance to fix concurrent race (#6816) MerkleTree was a shared volatile singleton holding per-build mutable state (leaves/hashList/root). When merkle validation runs concurrently (e.g. pre-broadcast validation on the P2P handler threads alongside the apply path), concurrent calls mutated the shared leaves list and threw ArrayIndexOutOfBoundsException (surfaced on an ARM SR due to its weaker memory model), which escaped the BadBlockException catch and dropped peers. - MerkleTree: replace the getInstance() singleton with a static build() factory returning a thread-confined instance; drop the now-inaccurate @NotThreadSafe. - BlockCapsule.calcMerkleRoot: use MerkleTree.build(ids); keeps the config-driven hash engine, so no consensus behavior change. - MerkleTreeTest: switch call sites to build(); un-ignore testConcurrent and turn it into a regression test asserting 1000 concurrent builds succeed. --- .../org/tron/core/capsule/BlockCapsule.java | 2 +- .../tron/core/capsule/utils/MerkleTree.java | 29 +++++------------ .../core/capsule/utils/MerkleTreeTest.java | 31 ++++++++----------- 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java index 63acf64b64f..e6cbd52e595 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java @@ -227,7 +227,7 @@ public Sha256Hash calcMerkleRoot() { .map(TransactionCapsule::getMerkleHash) .collect(Collectors.toCollection(ArrayList::new)); - return MerkleTree.getInstance().createTree(ids).getRoot().getHash(); + return MerkleTree.build(ids).getRoot().getHash(); } public void validateMerkleRoot() throws BadBlockException { diff --git a/chainbase/src/main/java/org/tron/core/capsule/utils/MerkleTree.java b/chainbase/src/main/java/org/tron/core/capsule/utils/MerkleTree.java index 94d22f4b474..cb6f299e872 100644 --- a/chainbase/src/main/java/org/tron/core/capsule/utils/MerkleTree.java +++ b/chainbase/src/main/java/org/tron/core/capsule/utils/MerkleTree.java @@ -5,41 +5,28 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import lombok.Getter; -import net.jcip.annotations.NotThreadSafe; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Sha256Hash; @Getter -@NotThreadSafe public class MerkleTree { - private static volatile MerkleTree instance; private List hashList; private List leaves; private Leaf root; - public static MerkleTree getInstance() { - if (instance == null) { - synchronized (MerkleTree.class) { - if (instance == null) { - instance = new MerkleTree(); - } - } - } - return instance; - } - - public MerkleTree createTree(List hashList) { - this.leaves = new ArrayList<>(); - this.hashList = hashList; - List leaves = createLeaves(hashList); + public static MerkleTree build(List hashList) { + MerkleTree tree = new MerkleTree(); + tree.hashList = hashList; + tree.leaves = new ArrayList<>(); + List leaves = tree.createLeaves(hashList); while (leaves.size() > 1) { - leaves = createParentLeaves(leaves); + leaves = tree.createParentLeaves(leaves); } - this.root = leaves.get(0); - return this; + tree.root = leaves.get(0); + return tree; } private List createParentLeaves(List leaves) { diff --git a/framework/src/test/java/org/tron/core/capsule/utils/MerkleTreeTest.java b/framework/src/test/java/org/tron/core/capsule/utils/MerkleTreeTest.java index 88e95f9653e..c9fea6bce45 100644 --- a/framework/src/test/java/org/tron/core/capsule/utils/MerkleTreeTest.java +++ b/framework/src/test/java/org/tron/core/capsule/utils/MerkleTreeTest.java @@ -10,10 +10,7 @@ import java.util.stream.IntStream; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; import org.tron.common.utils.MerkleRoot; @@ -23,9 +20,6 @@ @Slf4j public class MerkleTreeTest { - @Rule - public ExpectedException exception = ExpectedException.none(); - private static List getHash(int hashNum) { List hashList = new ArrayList(); for (int i = 0; i < hashNum; i++) { @@ -102,7 +96,7 @@ private static int getRank(int num) { public void test0HashNum() { List hashList = getHash(0); //Empty list. Exception e = Assert.assertThrows(Exception.class, - () -> MerkleTree.getInstance().createTree(hashList)); + () -> MerkleTree.build(hashList)); Assert.assertTrue(e instanceof IndexOutOfBoundsException); } @@ -117,7 +111,7 @@ public void test0HashNum() { */ public void test1HashNum() { List hashList = getHash(1); - MerkleTree tree = MerkleTree.getInstance().createTree(hashList); + MerkleTree tree = MerkleTree.build(hashList); Leaf root = tree.getRoot(); Assert.assertEquals(root.getHash(), hashList.get(0)); @@ -140,7 +134,7 @@ public void test1HashNum() { */ public void test2HashNum() { List hashList = getHash(2); - MerkleTree tree = MerkleTree.getInstance().createTree(hashList); + MerkleTree tree = MerkleTree.build(hashList); Leaf root = tree.getRoot(); Assert.assertEquals(root.getHash(), computeHash(hashList.get(0), hashList.get(1))); @@ -178,14 +172,13 @@ public void testAnyHashNum() { for (int hashNum = 1; hashNum <= maxNum; hashNum++) { int maxRank = getRank(hashNum); List hashList = getHash(hashNum); - MerkleTree tree = MerkleTree.getInstance().createTree(hashList); + MerkleTree tree = MerkleTree.build(hashList); Leaf root = tree.getRoot(); pareTree(root, hashList, maxRank, 0, 0); } } @Test - @Ignore public void testConcurrent() { Sha256Hash root1 = Sha256Hash.wrap( ByteString.fromHex("6cb38b4f493db8bacf26123cd4253bbfc530c708b97b3747e782f64097c3c482")); @@ -197,14 +190,16 @@ public void testConcurrent() { List list2 = IntStream.range(0, 10000).mapToObj(i -> Sha256Hash.of(true, ("byte2-" + i).getBytes(StandardCharsets.UTF_8))) .collect(Collectors.toList()); - Assert.assertEquals(root1, MerkleTree.getInstance().createTree(list1).getRoot().getHash()); - Assert.assertEquals(root2, MerkleTree.getInstance().createTree(list2).getRoot().getHash()); + Assert.assertEquals(root1, MerkleTree.build(list1).getRoot().getHash()); + Assert.assertEquals(root2, MerkleTree.build(list2).getRoot().getHash()); Assert.assertEquals(root1, MerkleRoot.root(list1)); Assert.assertEquals(root2, MerkleRoot.root(list2)); - exception.expect(ArrayIndexOutOfBoundsException.class); - IntStream.range(0, 1000).parallel().forEach(i -> Assert.assertEquals( - MerkleTree.getInstance().createTree(i % 2 == 0 ? list1 : list2).getRoot().getHash(), - MerkleRoot.root(i % 2 == 0 ? list1 : list2)) - ); + // MerkleTree.build is now per-instance with no shared state, so concurrent builds + // must yield correct roots without ArrayIndexOutOfBoundsException. + IntStream.range(0, 1000).parallel().forEach(i -> { + List list = i % 2 == 0 ? list1 : list2; + Sha256Hash expect = i % 2 == 0 ? root1 : root2; + Assert.assertEquals(expect, MerkleTree.build(list).getRoot().getHash()); + }); } } From 4e80f8ffa9a24e2fe66f48131200954fd3d96c60 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 3 Jun 2026 17:14:40 +0800 Subject: [PATCH 35/57] feat(version): merge master to 4.8.2 (#6817) * feat(*): disable exchange transaction (#6507) * update a new version. version name:GreatVoyage-v4.8.0-1-g45e3bf88ca,version code:18634 (#6508) * Merge release_v4.8.1 to master (#6541) * update a new version. version name:GreatVoyage-v4.8.0.1-1-g44a4bc8263,version code:18636 (#6542) * feat(vm): optimize the check for create2 * feat(vm): optimize the check for ModExp * test(vm): add tests for create2/modExp checks * feat(version): update version to 4.8.1.1 * feat(ci): add PR pipeline and system-test workflows New workflows: - pr-build.yml: multi-OS build matrix (macOS, Ubuntu, RockyLinux, Debian11) and changed-line/overall coverage gate - pr-check.yml: PR title/body lint + Checkstyle - pr-reviewer.yml: scope-based reviewer auto-assignment - pr-cancel.yml: cancel in-progress runs when PR is closed unmerged - system-test.yml: spin up FullNode and run the system-test suite Existing workflows: - codeql.yml: bump to v4/v5 actions, switch to manual build-mode with JDK 8, add paths-ignore for docs-only changes - math-check.yml: bump checkout/upload-artifact/github-script versions * feat(config): fix git.properties NPE * update a new version. version name:GreatVoyage-v4.8.1-6-g52d7d9d23e,version code:18643 --------- Co-authored-by: YAaron <4241080+kuny0707@users.noreply.github.com> Co-authored-by: zz --- .../tron/core/vm/PrecompiledContracts.java | 4 + .../org/tron/core/vm/program/Program.java | 3 + .../java/org/tron/core/vm/utils/MUtil.java | 12 ++ .../java/org/tron/core/config/Parameter.java | 5 +- .../main/java/org/tron/program/Version.java | 4 +- .../runtime/vm/Create2ModExpForkTest.java | 178 ++++++++++++++++++ 6 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java diff --git a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java index 0dc8fb31ada..3993e8ed835 100644 --- a/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java +++ b/actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java @@ -699,6 +699,10 @@ public Pair execute(byte[] data) { return Pair.of(false, EMPTY_BYTE_ARRAY); } + if (baseLen == 0 && modLen == 0 && expLen > UPPER_BOUND) { + MUtil.checkCPUTimeForModExp(); + } + BigInteger base = parseArg(data, ARGS_OFFSET, baseLen); BigInteger exp = parseArg(data, addSafely(ARGS_OFFSET, baseLen), expLen); BigInteger mod = parseArg(data, addSafely(addSafely(ARGS_OFFSET, baseLen), expLen), modLen); diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 3ed968e1afa..41822df2391 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1625,6 +1625,9 @@ public void createContract2(DataWord value, DataWord memStart, DataWord memSize, stackPushZero(); return; } + if (getCallDeep() == MAX_DEPTH) { + MUtil.checkCPUTimeForCreate2(); + } if (VMConfig.allowTvmIstanbul()) { senderAddress = getContextAddress(); } else { diff --git a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java index c94f28b3a2f..e07360e6863 100644 --- a/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java +++ b/actuator/src/main/java/org/tron/core/vm/utils/MUtil.java @@ -64,4 +64,16 @@ public static void checkCPUTime() { throw new OutOfTimeException("CPU timeout for 0x0a executing"); } } + + public static void checkCPUTimeForCreate2() { + if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_1_1)) { + throw new OutOfTimeException("CPU timeout for create2 executing"); + } + } + + public static void checkCPUTimeForModExp() { + if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_1_1)) { + throw new OutOfTimeException("CPU timeout for modExp executing"); + } + } } diff --git a/common/src/main/java/org/tron/core/config/Parameter.java b/common/src/main/java/org/tron/core/config/Parameter.java index 5349ef8d875..233f1d9ef7a 100644 --- a/common/src/main/java/org/tron/core/config/Parameter.java +++ b/common/src/main/java/org/tron/core/config/Parameter.java @@ -29,7 +29,8 @@ public enum ForkBlockVersionEnum { VERSION_4_8_0(32, 1596780000000L, 80), VERSION_4_8_0_1(33, 1596780000000L, 70), VERSION_4_8_1(34, 1596780000000L, 80), - VERSION_4_8_2(35, 1596780000000L, 80); + VERSION_4_8_1_1(35, 1596780000000L, 70), + VERSION_4_8_2(36, 1596780000000L, 80); // if add a version, modify BLOCK_VERSION simultaneously @Getter @@ -78,7 +79,7 @@ public class ChainConstant { public static final int SINGLE_REPEAT = 1; public static final int BLOCK_FILLED_SLOTS_NUMBER = 128; public static final int MAX_FROZEN_NUMBER = 1; - public static final int BLOCK_VERSION = 35; + public static final int BLOCK_VERSION = 36; public static final long FROZEN_PERIOD = 86_400_000L; public static final long DELEGATE_PERIOD = 3 * 86_400_000L; public static final long TRX_PRECISION = 1000_000L; diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 3ce7ce20312..73e4f1e826b 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -2,8 +2,8 @@ public class Version { - public static final String VERSION_NAME = "GreatVoyage-v4.8.0.1-1-g44a4bc8263"; - public static final String VERSION_CODE = "18636"; + public static final String VERSION_NAME = "GreatVoyage-v4.8.1-6-g52d7d9d23e"; + public static final String VERSION_CODE = "18643"; private static final String VERSION = "4.8.2"; public static String getVersion() { diff --git a/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java b/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java new file mode 100644 index 00000000000..6fbecb4c87c --- /dev/null +++ b/framework/src/test/java/org/tron/common/runtime/vm/Create2ModExpForkTest.java @@ -0,0 +1,178 @@ +package org.tron.common.runtime.vm; + +import java.util.Arrays; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.TestConstants; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.runtime.InternalTransaction; +import org.tron.common.utils.ForkController; +import org.tron.core.Constant; +import org.tron.core.config.Parameter.ForkBlockVersionEnum; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ContractValidateException; +import org.tron.core.store.StoreFactory; +import org.tron.core.vm.PrecompiledContracts; +import org.tron.core.vm.PrecompiledContracts.PrecompiledContract; +import org.tron.core.vm.config.ConfigLoader; +import org.tron.core.vm.config.VMConfig; +import org.tron.core.vm.program.Program; +import org.tron.core.vm.program.Program.OutOfTimeException; +import org.tron.core.vm.program.invoke.ProgramInvokeMockImpl; +import org.tron.core.vm.utils.MUtil; +import org.tron.protos.Protocol; + + +@Slf4j +public class Create2ModExpForkTest extends BaseTest { + + // mirrors the private Program.MAX_DEPTH + private static final int MAX_CALL_DEPTH = 64; + + // mirrors PrecompiledContracts.ModExp.UPPER_BOUND + private static final int MOD_EXP_UPPER_BOUND = 1024; + + // ModExp precompile address (0x05) + private static final DataWord MOD_EXP_ADDR = new DataWord( + "0000000000000000000000000000000000000000000000000000000000000005"); + + @BeforeClass + public static void init() { + Args.setParam(new String[]{"--output-directory", dbPath(), "--debug"}, TestConstants.TEST_CONF); + CommonParameter.getInstance().setDebug(true); + } + + @AfterClass + public static void destroy() { + ConfigLoader.disable = false; + VMConfig.initVmHardFork(false); + VMConfig.initAllowTvmCompatibleEvm(0); + Args.clearParam(); + } + + @Before + public void setUp() { + ForkController.instance().init(chainBaseManager); + deactivateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + } + + @Test + public void checkCPUTimeForCreate2_isGatedByFork() { + MUtil.checkCPUTimeForCreate2(); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForCreate2); + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); + } + + @Test + public void checkCPUTimeForModExp_isGatedByFork() { + MUtil.checkCPUTimeForModExp(); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForModExp); + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); + } + + @Test + public void modExp_degenerateInput_throwsOnlyAfterFork() { + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); + byte[] data = buildModExpInput(MOD_EXP_UPPER_BOUND + 1); + + Pair out = modExp.execute(data); + Assert.assertTrue(out.getLeft()); + + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + OutOfTimeException ex = + Assert.assertThrows(OutOfTimeException.class, () -> modExp.execute(data)); + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); + } + + @Test + public void modExp_atUpperBound_doesNotThrowAfterFork() { + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); + Pair out = modExp.execute(buildModExpInput(MOD_EXP_UPPER_BOUND)); + Assert.assertTrue(out.getLeft()); + } + + @Test + public void createContract2_atMaxDepth_legacyPath_throwsAfterFork() + throws ContractValidateException { + VMConfig.initAllowTvmCompatibleEvm(0); + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + Program program = buildProgramAtMaxDepth(); + OutOfTimeException ex = Assert.assertThrows(OutOfTimeException.class, + () -> program.createContract2( + DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO())); + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); + } + + @Test + public void createContract2_atMaxDepth_compatibleEvmOn_doesNotThrow() + throws ContractValidateException { + VMConfig.initAllowTvmCompatibleEvm(1); + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); + + Program program = buildProgramAtMaxDepth(); + program.createContract2(DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO()); + Assert.assertEquals(DataWord.ZERO(), program.getStack().pop()); + } + + // ---- helpers --------------------------------------------------------------------------------- + + private Program buildProgramAtMaxDepth() throws ContractValidateException { + StoreFactory.init(); + StoreFactory storeFactory = StoreFactory.getInstance(); + storeFactory.setChainBaseManager(chainBaseManager); + byte[] ops = new byte[] {0}; + ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl(storeFactory, ops, ops) { + @Override + public int getCallDeep() { + return MAX_CALL_DEPTH; + } + }; + Program program = new Program(ops, ops, invoke, + new InternalTransaction(Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + program.setRootTransactionId(new byte[32]); + return program; + } + + private byte[] buildModExpInput(int expLen) { + byte[] data = new byte[96]; + byte[] expLenWord = new DataWord(expLen).getData(); + System.arraycopy(expLenWord, 0, data, 32, 32); + return data; + } + + private void activateFork(ForkBlockVersionEnum forkVersion) { + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + chainBaseManager.getDynamicPropertiesStore().statsByVersion(forkVersion.getValue(), stats); + long maintenanceTimeInterval = + chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); + long hardForkTime = ((forkVersion.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(hardForkTime + 1); + } + + private void deactivateFork(ForkBlockVersionEnum forkVersion) { + chainBaseManager.getDynamicPropertiesStore() + .statsByVersion(forkVersion.getValue(), new byte[27]); + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(0L); + } +} From c9f604f16e298c1013400ec8dd7f8a7a5cce2206 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Fri, 5 Jun 2026 11:11:52 +0800 Subject: [PATCH 36/57] fix(ci): upgrade actions/cache to v5 and fix container git checkout (#6808) --- .../workflows/integration-test-multinode.yml | 2 +- .../integration-test-single-node.yml | 2 +- .github/workflows/pr-build.yml | 32 +++++++++---------- .github/workflows/pr-check.yml | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.github/workflows/integration-test-multinode.yml b/.github/workflows/integration-test-multinode.yml index 99a7b54e022..fadfc2168d2 100644 --- a/.github/workflows/integration-test-multinode.yml +++ b/.github/workflows/integration-test-multinode.yml @@ -32,7 +32,7 @@ jobs: distribution: 'temurin' - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ~/.gradle/caches diff --git a/.github/workflows/integration-test-single-node.yml b/.github/workflows/integration-test-single-node.yml index a68c243efcc..b0c10247a7f 100644 --- a/.github/workflows/integration-test-single-node.yml +++ b/.github/workflows/integration-test-single-node.yml @@ -32,7 +32,7 @@ jobs: distribution: 'temurin' - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ~/.gradle/caches diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml index dd005f98b74..191d8be778c 100644 --- a/.github/workflows/pr-build.yml +++ b/.github/workflows/pr-build.yml @@ -43,7 +43,7 @@ jobs: distribution: 'temurin' - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ~/.gradle/caches @@ -82,7 +82,7 @@ jobs: run: java -version - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ~/.gradle/caches @@ -117,20 +117,20 @@ jobs: LC_ALL: en_US.UTF-8 steps: - - name: Checkout code - uses: actions/checkout@v5 - - name: Install dependencies (Rocky 8 + JDK8) run: | set -euxo pipefail dnf -y install java-1.8.0-openjdk-devel git wget unzip which jq bc curl glibc-langpack-en dnf -y groupinstall "Development Tools" + - name: Checkout code + uses: actions/checkout@v5 + - name: Check Java version run: java -version - name: Cache Gradle - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | /github/home/.gradle/caches @@ -174,20 +174,20 @@ jobs: GRADLE_USER_HOME: /github/home/.gradle steps: - - name: Checkout code - uses: actions/checkout@v5 - - name: Install dependencies (Debian + build tools) run: | set -euxo pipefail apt-get update apt-get install -y git wget unzip build-essential curl jq + - name: Checkout code + uses: actions/checkout@v5 + - name: Check Java version run: java -version - name: Cache Gradle - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | /github/home/.gradle/caches @@ -238,19 +238,19 @@ jobs: contents: read steps: - - name: Checkout code - uses: actions/checkout@v5 - with: - ref: ${{ github.event.pull_request.base.sha }} - - name: Install dependencies (Debian + build tools) run: | set -euxo pipefail apt-get update apt-get install -y git wget unzip build-essential curl jq + - name: Checkout code + uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.base.sha }} + - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | /github/home/.gradle/caches diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 7ae169a8690..b6988c2c4d3 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -126,7 +126,7 @@ jobs: distribution: 'temurin' - name: Cache Gradle packages - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ~/.gradle/caches From a9420c03e013add14a141da30f4205218a3845e6 Mon Sep 17 00:00:00 2001 From: bladehan1 Date: Fri, 5 Jun 2026 11:13:48 +0800 Subject: [PATCH 37/57] test(config): add reference.conf to bean parity gate (#6803) --- .../core/config/args/ConfigParityCheck.java | 713 ++++++++++++++++++ .../config/args/ConfigParityGateTest.java | 238 ++++++ 2 files changed, 951 insertions(+) create mode 100644 common/src/test/java/org/tron/core/config/args/ConfigParityCheck.java create mode 100644 common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java diff --git a/common/src/test/java/org/tron/core/config/args/ConfigParityCheck.java b/common/src/test/java/org/tron/core/config/args/ConfigParityCheck.java new file mode 100644 index 00000000000..051ebeaef00 --- /dev/null +++ b/common/src/test/java/org/tron/core/config/args/ConfigParityCheck.java @@ -0,0 +1,713 @@ +package org.tron.core.config.args; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigObject; +import com.typesafe.config.ConfigValue; +import com.typesafe.config.ConfigValueType; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import lombok.extern.slf4j.Slf4j; + +/** + * Shared helpers for reference.conf <-> {@code *Config} bean parity tests. + * Asserts that every HOCON key under a section binds to a writable bean + * property and matches the bean's default. Drift fails the build at PR time + * instead of waiting for {@code ConfigBeanFactory} to throw at startup. + *

+ * {@code [parity-*]} audit log lines land in the JUnit XML {@code } + * when the gate runs in isolation; if mixed with tests that boot a tron main, + * production logback may redirect them to {@code logs/} on disk. + */ +@Slf4j(topic = "test") +final class ConfigParityCheck { + + private ConfigParityCheck() { + + } + + private static Map writablePropertyDescriptors(Class beanClass) { + try { + Map m = new TreeMap<>(); + for (PropertyDescriptor pd : + Introspector.getBeanInfo(beanClass, Object.class).getPropertyDescriptors()) { + if (pd.getWriteMethod() != null) { + m.put(pd.getName(), pd); + } + } + return m; + } catch (java.beans.IntrospectionException e) { + throw new AssertionError("Introspector failed on " + beanClass.getName(), e); + } + } + + /** + * {@code shapeMismatches}: HOCON key matches a bean property of nested + * {@code *Config} type but the HOCON value is not OBJECT — walker cannot + * recurse, and downstream binding would throw {@code WrongType}. + */ + private static final class OrphanCounters { + int total; + int bound; + final Set orphans = new TreeSet<>(); + final Set allowlisted = new TreeSet<>(); + final Set shapeMismatches = new TreeSet<>(); + } + + /** + * Fails when reference.conf has keys under {@code sectionPath} (recursively + * through nested {@code *Config} sub-sections) that the bean cannot bind. + * Recurses into a sub-config when the property type satisfies + * {@link #isRecursiveConfigBean} AND the HOCON value is an OBJECT. + */ + static void assertNoHoconOrphans( + String sectionPath, Class beanClass, Set allowedHoconOrphans) { + Config section = ConfigFactory.defaultReference().getConfig(sectionPath); + OrphanCounters c = walkAndLogHoconOrphans( + sectionPath, section, beanClass, allowedHoconOrphans); + AGGREGATES.hoconKey += c.total; + AGGREGATES.hoconBound += c.bound; + AGGREGATES.hoconAllowlisted += c.allowlisted.size(); + AGGREGATES.beans.add(beanClass); + failOnHoconOrphans(sectionPath, beanClass, c); + } + + /** Overload for meta-tests: walks the supplied Config directly, skips AGGREGATES. */ + static void assertNoHoconOrphans( + String label, Config section, Class beanClass, + Set allowedHoconOrphans) { + OrphanCounters c = walkAndLogHoconOrphans( + label, section, beanClass, allowedHoconOrphans); + failOnHoconOrphans(label, beanClass, c); + } + + private static OrphanCounters walkAndLogHoconOrphans( + String label, Config section, Class beanClass, Set allowed) { + OrphanCounters c = new OrphanCounters(); + walkHoconOrphans(beanClass, section, "", allowed, c); + logger.info("[parity-hocon] {} -> {}: hoconKey={}, bound={}, allowlisted={}{}", + label, beanClass.getSimpleName(), c.total, c.bound, + c.allowlisted.size(), c.allowlisted.isEmpty() ? "" : " " + c.allowlisted); + return c; + } + + private static void failOnHoconOrphans( + String label, Class beanClass, OrphanCounters c) { + if (!c.shapeMismatches.isEmpty()) { + throw new AssertionError( + "reference.conf has " + label + ".* keys whose HOCON value " + + "shape does not match the bean property type (expected OBJECT " + + "for nested *Config bean): " + c.shapeMismatches); + } + if (!c.orphans.isEmpty()) { + throw new AssertionError( + "reference.conf has " + label + ".* keys with no matching " + + beanClass.getSimpleName() + " property (at any nesting level) " + + "and not in allowlist: " + c.orphans); + } + } + + private static void walkHoconOrphans( + Class beanClass, Config section, String prefix, + Set allowed, OrphanCounters c) { + Map props = writablePropertyDescriptors(beanClass); + for (String key : new TreeSet<>(section.root().keySet())) { + c.total++; + String qualified = prefix + key; + PropertyDescriptor pd = props.get(key); + if (pd == null) { + if (allowed.contains(qualified)) { + c.allowlisted.add(qualified); + } else { + c.orphans.add(qualified); + } + continue; + } + c.bound++; + Class type = pd.getPropertyType(); + if (isRecursiveConfigBean(type)) { + ConfigValueType valueType = section.root().get(key).valueType(); + if (valueType != ConfigValueType.OBJECT) { + c.shapeMismatches.add(qualified + " (bean type " + + type.getSimpleName() + " requires OBJECT, got " + valueType + ")"); + continue; + } + walkHoconOrphans(type, section.getConfig(key), qualified + ".", allowed, c); + } + } + } + + /** + * Fails when a writable bean property (reachable from {@code beanClass} + * through nested {@code *Config} recursion) has no HOCON key under + * {@code sectionPath} and is not in {@code allowedBeanOrphans}. + */ + static void assertNoBeanOrphans( + String sectionPath, Class beanClass, Set allowedBeanOrphans) { + Config section = ConfigFactory.defaultReference().getConfig(sectionPath); + OrphanCounters c = walkAndLogBeanOrphans( + sectionPath, section, beanClass, allowedBeanOrphans); + AGGREGATES.beanKey += c.total; + AGGREGATES.beanHasKey += c.bound; + AGGREGATES.beanAllowlisted += c.allowlisted.size(); + AGGREGATES.beans.add(beanClass); + failOnBeanOrphans(sectionPath, beanClass, c); + } + + /** Overload for meta-tests: walks the supplied Config directly, skips AGGREGATES. */ + static void assertNoBeanOrphans( + String label, Config section, Class beanClass, + Set allowedBeanOrphans) { + OrphanCounters c = walkAndLogBeanOrphans( + label, section, beanClass, allowedBeanOrphans); + failOnBeanOrphans(label, beanClass, c); + } + + private static OrphanCounters walkAndLogBeanOrphans( + String label, Config section, Class beanClass, Set allowed) { + OrphanCounters c = new OrphanCounters(); + walkBeanOrphans(beanClass, section, "", allowed, c); + logger.info("[parity-bean] {} -> {}: beanKey={}, hasKey={}, allowlisted={}{}", + label, beanClass.getSimpleName(), c.total, c.bound, + c.allowlisted.size(), c.allowlisted.isEmpty() ? "" : " " + c.allowlisted); + return c; + } + + private static void failOnBeanOrphans( + String label, Class beanClass, OrphanCounters c) { + if (!c.shapeMismatches.isEmpty()) { + throw new AssertionError( + beanClass.getSimpleName() + " has nested *Config properties whose " + + "HOCON value shape under " + label + ".* is not OBJECT: " + + c.shapeMismatches); + } + if (!c.orphans.isEmpty()) { + throw new AssertionError( + beanClass.getSimpleName() + " has properties with no matching " + + label + ".* HOCON key (at any nesting level) " + + "and not in allowlist: " + c.orphans); + } + } + + private static void walkBeanOrphans( + Class beanClass, Config section, String prefix, + Set allowed, OrphanCounters c) { + Set keys = section.root().keySet(); + Map props = writablePropertyDescriptors(beanClass); + for (Map.Entry e : props.entrySet()) { + c.total++; + String name = e.getKey(); + String qualified = prefix + name; + PropertyDescriptor pd = e.getValue(); + if (!keys.contains(name)) { + if (allowed.contains(qualified)) { + c.allowlisted.add(qualified); + } else { + c.orphans.add(qualified); + } + continue; + } + c.bound++; + Class type = pd.getPropertyType(); + if (isRecursiveConfigBean(type)) { + ConfigValueType valueType = section.root().get(name).valueType(); + if (valueType != ConfigValueType.OBJECT) { + c.shapeMismatches.add(qualified + " (bean type " + + type.getSimpleName() + " requires OBJECT, got " + valueType + ")"); + continue; + } + walkBeanOrphans(type, section.getConfig(name), qualified + ".", allowed, c); + } + } + } + + /** Build an immutable allowlist from string literals. */ + static Set allowlist(String... names) { + Set s = new HashSet<>(Arrays.asList(names)); + return Collections.unmodifiableSet(s); + } + + /** + * Fails when an allowlist entry no longer resolves to a live target. Prevents + * allowlist rot: a renamed/removed key/property must drop its grandfathering + * entry in the same PR (cf. Cassandra's PROPERTIES_TO_IGNORE long-term decay). + */ + static void assertAllowlistEntriesAreLive( + String sectionPath, Class beanClass, + Set allowedHoconOrphans, + Set allowedBeanOrphans, + Set allowedDivergent) { + Config section = ConfigFactory.defaultReference().getConfig(sectionPath); + runAllowlistEntriesAreLive(sectionPath, section, beanClass, + allowedHoconOrphans, allowedBeanOrphans, allowedDivergent); + } + + /** Overload for meta-tests: see {@link #assertNoHoconOrphans(String, Config, Class, Set)}. */ + static void assertAllowlistEntriesAreLive( + String label, Config section, Class beanClass, + Set allowedHoconOrphans, + Set allowedBeanOrphans, + Set allowedDivergent) { + runAllowlistEntriesAreLive(label, section, beanClass, + allowedHoconOrphans, allowedBeanOrphans, allowedDivergent); + } + + private static void runAllowlistEntriesAreLive( + String label, Config section, Class beanClass, + Set allowedHoconOrphans, + Set allowedBeanOrphans, + Set allowedDivergent) { + List dead = new ArrayList<>(); + + for (String k : allowedHoconOrphans) { + if (!section.hasPath(k)) { + dead.add("hoconOrphan: " + k + " (no longer in reference.conf[" + label + "])"); + } + } + for (String k : allowedBeanOrphans) { + if (!beanPropertyExists(beanClass, k)) { + dead.add("beanOrphan: " + k + " (no longer a writable property of " + + beanClass.getSimpleName() + ")"); + } + } + for (String k : allowedDivergent) { + boolean hoconLive = section.hasPath(k); + boolean beanLive = beanPropertyExists(beanClass, k); + if (!hoconLive || !beanLive) { + dead.add("divergent: " + k + + " (hocon=" + (hoconLive ? "live" : "dead") + + ", bean=" + (beanLive ? "live" : "dead") + ")"); + } + } + + logger.info("[parity-sweep] {} -> {}: hoconOrphans={}, beanOrphans={}, divergent={}, dead={}", + label, beanClass.getSimpleName(), + allowedHoconOrphans.size(), allowedBeanOrphans.size(), + allowedDivergent.size(), dead.size()); + + if (!dead.isEmpty()) { + throw new AssertionError( + "Dead allowlist entries on " + label + " / " + + beanClass.getSimpleName() + " — drop them or restore the " + + "underlying key/property:\n " + String.join("\n ", dead)); + } + } + + /** True iff dotted {@code qualifiedName} resolves to a writable bean property. */ + private static boolean beanPropertyExists(Class beanClass, String qualifiedName) { + Class cursor = beanClass; + String[] segments = qualifiedName.split("\\."); + for (int i = 0; i < segments.length; i++) { + PropertyDescriptor pd = writablePropertyDescriptors(cursor).get(segments[i]); + if (pd == null) { + return false; + } + if (i == segments.length - 1) { + return true; + } + Class type = pd.getPropertyType(); + if (!isRecursiveConfigBean(type)) { + return false; + } + cursor = type; + } + return true; + } + + /** Sentinel: property type outside the dispatcher matrix — hard failure. */ + private static final Object SKIP = new Object(); + + /** Sentinel: property type is a nested {@code *Config} bean to recurse into. */ + private static final Object RECURSE = new Object(); + + /** + * Asserts every writable bean property has a default value equal to its + * reference.conf value. Supported scalar types: {@code int / long / boolean / + * double / float} (and boxed forms), {@code String}, {@code List}. Nested + * {@code *Config} beans are recursed into and matched by dotted name. + *

+ * Skipped: properties with no HOCON key at the current scope, and properties + * named in {@code allowedDivergent} (intentional asymmetry). Properties whose + * type isn't in the dispatcher matrix fail — no silent escape; extend the + * dispatcher or (if genuinely uncomparable) re-introduce a per-section + * {@code typeSkip} allowlist. + */ + static void assertDefaultValuesMatch( + String sectionPath, Class beanClass, Set allowedDivergent) { + Config section = ConfigFactory.defaultReference().getConfig(sectionPath); + Counters c = new Counters(); + List mismatches = runDefaultValuesMatch( + sectionPath, section, beanClass, allowedDivergent, c); + + AGGREGATES.defBeanKey += c.total; + AGGREGATES.defMatched += c.matched; + AGGREGATES.defHoconRecursedKey += c.recursed; + AGGREGATES.defSkipAllow += c.skipAllow.size(); + AGGREGATES.defSkipNoKey += c.skipNoKey.size(); + AGGREGATES.beans.add(beanClass); + + failOnDefaultValueMismatches(sectionPath, beanClass, mismatches); + } + + /** Overload for meta-tests: see {@link #assertNoHoconOrphans(String, Config, Class, Set)}. */ + static void assertDefaultValuesMatch( + String label, Config section, Class beanClass, + Set allowedDivergent) { + Counters c = new Counters(); + List mismatches = runDefaultValuesMatch( + label, section, beanClass, allowedDivergent, c); + failOnDefaultValueMismatches(label, beanClass, mismatches); + } + + private static List runDefaultValuesMatch( + String label, Config section, Class beanClass, + Set allowedDivergent, Counters c) { + Object bean; + try { + bean = beanClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + throw new AssertionError("cannot instantiate " + beanClass.getName(), e); + } + List mismatches = new ArrayList<>(); + compareBean(beanClass, bean, section, "", allowedDivergent, mismatches, c); + logger.info("[parity-default] {} -> {}: beanKey={}, matched={}, hoconRecursedKey={}, " + + "divergent-allow={}{}, skip-no-key={}{}", + label, beanClass.getSimpleName(), + c.total, c.matched, c.recursed, + c.skipAllow.size(), c.skipAllow.isEmpty() ? "" : " " + c.skipAllow, + c.skipNoKey.size(), c.skipNoKey.isEmpty() ? "" : " " + c.skipNoKey); + return mismatches; + } + + private static void failOnDefaultValueMismatches( + String label, Class beanClass, List mismatches) { + if (!mismatches.isEmpty()) { + throw new AssertionError( + "Default-value drift between " + beanClass.getSimpleName() + + " and reference.conf[" + label + "]:\n " + + String.join("\n ", mismatches)); + } + } + + /** + * Per-walk accounting. Invariant: {@code total == matched + recursed + + * skipAllow.size() + skipNoKey.size() + mismatches.size()}. Adding a loop + * exit without bumping a counter silently hides coverage drift. + */ + private static final class Counters { + int total; + int matched; + int recursed; + final Set skipAllow = new TreeSet<>(); + final Set skipNoKey = new TreeSet<>(); + } + + private static void compareBean( + Class beanClass, Object beanDefault, Config section, String prefix, + Set allowedDivergent, List mismatches, Counters c) { + PropertyDescriptor[] pds; + try { + pds = Introspector.getBeanInfo(beanClass, Object.class).getPropertyDescriptors(); + } catch (java.beans.IntrospectionException e) { + throw new AssertionError(e); + } + for (PropertyDescriptor pd : pds) { + if (pd.getWriteMethod() == null) { + // @Setter(NONE) for manual post-bind reads — orphan checks cover this side. + continue; + } + c.total++; + String name = pd.getName(); + String qualified = prefix + name; + if (pd.getReadMethod() == null) { + // Write-only property: ConfigBeanFactory binds but nothing reads it back. + mismatches.add(qualified + ": bean property is write-only " + + "(setter present, no getter) — default value cannot be verified " + + "and the bound value cannot be observed; add a getter or drop the field"); + continue; + } + if (allowedDivergent.contains(qualified)) { + c.skipAllow.add(qualified); + continue; + } + if (!section.hasPath(name)) { + c.skipNoKey.add(qualified); + continue; + } + Class type = pd.getPropertyType(); + // Shape guard: nested *Config bean expects HOCON OBJECT; surface as a + // clean mismatch instead of letting getConfig(name) throw WrongType. + if (isRecursiveConfigBean(type)) { + ConfigValueType valueType = section.root().get(name).valueType(); + if (valueType != ConfigValueType.OBJECT) { + mismatches.add(qualified + ": bean type " + type.getSimpleName() + + " requires HOCON OBJECT, got " + valueType); + continue; + } + } + Object hoconValue; + try { + hoconValue = readTypedHoconValue(section, name, type); + } catch (RuntimeException e) { + mismatches.add(qualified + ": type-incompatible HOCON value (" + + e.getClass().getSimpleName() + ": " + e.getMessage() + ")"); + continue; + } + if (hoconValue == RECURSE) { + c.recursed++; + Object nested; + try { + nested = pd.getReadMethod().invoke(beanDefault); + } catch (ReflectiveOperationException e) { + throw new AssertionError( + "cannot read " + qualified + " on " + beanClass.getName(), e); + } + if (nested == null) { + mismatches.add(qualified + ": nested " + type.getSimpleName() + + " field is null on a freshly-constructed " + beanClass.getSimpleName() + + " — initialize the field inline (= new " + type.getSimpleName() + "())"); + continue; + } + compareBean(type, nested, section.getConfig(name), qualified + ".", + allowedDivergent, mismatches, c); + continue; + } + if (hoconValue == SKIP) { + mismatches.add(qualified + ": Java type " + type.getSimpleName() + + " not in readTypedHoconValue dispatcher — extend the dispatcher, " + + "or re-introduce a per-section typeSkip allowlist if the type " + + "genuinely cannot be value-compared"); + continue; + } + Object actualDefault; + try { + actualDefault = pd.getReadMethod().invoke(beanDefault); + } catch (ReflectiveOperationException e) { + throw new AssertionError( + "cannot read " + qualified + " on " + beanClass.getName(), e); + } + if (!Objects.equals(actualDefault, hoconValue)) { + // Stamp the runtime type on each side so e.g. Integer(10) vs Long(10) + // doesn't look like `bean=10, reference.conf=10`. + mismatches.add(qualified + ": bean=" + format(actualDefault) + + " (" + typeOf(actualDefault) + ")" + + ", reference.conf=" + format(hoconValue) + + " (" + typeOf(hoconValue) + ")"); + continue; + } + c.matched++; + } + } + + /** Type dispatcher. Returns {@link #RECURSE} for nested *Config, {@link #SKIP} otherwise. */ + private static Object readTypedHoconValue(Config cfg, String path, Class type) { + if (type == int.class || type == Integer.class) { + return cfg.getInt(path); + } + if (type == long.class || type == Long.class) { + return cfg.getLong(path); + } + if (type == boolean.class || type == Boolean.class) { + return cfg.getBoolean(path); + } + if (type == double.class || type == Double.class) { + return cfg.getDouble(path); + } + if (type == float.class || type == Float.class) { + return (float) cfg.getDouble(path); + } + if (type == String.class) { + return cfg.getString(path); + } + if (type == List.class) { + return cfg.getList(path).unwrapped(); + } + if (isRecursiveConfigBean(type) && cfg.hasPath(path)) { + return RECURSE; + } + return SKIP; + } + + /** + * Recursion gate: a non-array/enum/interface class under {@code org.tron.*} + * with a default constructor. Keeps the walker inside project-owned beans. + */ + private static boolean isRecursiveConfigBean(Class type) { + if (type.isPrimitive() || type.isArray() || type.isEnum() || type.isInterface()) { + return false; + } + if (!type.getName().startsWith("org.tron.")) { + return false; + } + try { + type.getDeclaredConstructor(); + return true; + } catch (NoSuchMethodException e) { + return false; + } + } + + /** + * Cross-section accumulators. Bumped by each helper at the end of its work + * (before throwing) so partial coverage is still reflected. + * {@link #logAggregateSummary} emits one summary line per gate plus + * independently-computed reference totals as a sanity check. + */ + private static final class Aggregates { + int hoconKey; + int hoconBound; + int hoconAllowlisted; + int beanKey; + int beanHasKey; + int beanAllowlisted; + int defBeanKey; + int defMatched; + int defHoconRecursedKey; + int defSkipAllow; + int defSkipNoKey; + // root-level bean classes touched by any helper; recursion walks nested *Config on its own. + final Set> beans = new LinkedHashSet<>(); + } + + private static final Aggregates AGGREGATES = new Aggregates(); + + /** Reset accumulators. Call from {@code @BeforeClass} for clean re-runs in the same JVM. */ + static void resetAggregates() { + AGGREGATES.hoconKey = 0; + AGGREGATES.hoconBound = 0; + AGGREGATES.hoconAllowlisted = 0; + AGGREGATES.beanKey = 0; + AGGREGATES.beanHasKey = 0; + AGGREGATES.beanAllowlisted = 0; + AGGREGATES.defBeanKey = 0; + AGGREGATES.defMatched = 0; + AGGREGATES.defHoconRecursedKey = 0; + AGGREGATES.defSkipAllow = 0; + AGGREGATES.defSkipNoKey = 0; + AGGREGATES.beans.clear(); + } + + /** + * Emit per-gate totals + file-coverage alignment + * {@code file-hoconKey == checkSection + cantCheckSection} and bean-tree + * alignment across {@code parity-bean} / {@code parity-default} / the + * independently-counted registry total. Reviewers can sum columns visually + * to spot a walker that silently skipped a property. + * + * @param checkSectionTopLevels top-level keys hosting a registered Section + * @param cantCheckSectionTopLevels remaining top-level keys (out of parity scope) + */ + static void logAggregateSummary( + Set checkSectionTopLevels, + Set cantCheckSectionTopLevels) { + ConfigObject refRoot = ConfigFactory.parseResources("reference.conf").root(); + int hoconKeyInFile = countHoconKeysRecursive(refRoot); + int checkSectionKey = sumTopLevelSubtreeSize(refRoot, checkSectionTopLevels); + int cantCheckSectionKey = sumTopLevelSubtreeSize(refRoot, cantCheckSectionTopLevels); + + int beanKeyInRegistry = 0; + for (Class b : AGGREGATES.beans) { + beanKeyInRegistry += countBeanSettersRecursive(b); + } + + logger.info("[parity-summary] parity-hocon : hoconKey={}, bound={}, allowlisted={}", + AGGREGATES.hoconKey, AGGREGATES.hoconBound, AGGREGATES.hoconAllowlisted); + logger.info("[parity-summary] parity-bean : beanKey={}, hasKey={}, allowlisted={}", + AGGREGATES.beanKey, AGGREGATES.beanHasKey, AGGREGATES.beanAllowlisted); + logger.info("[parity-summary] parity-default: beanKey={}, matched={}, hoconRecursedKey={}, " + + "divergent-allow={}, skip-no-key={}", + AGGREGATES.defBeanKey, AGGREGATES.defMatched, AGGREGATES.defHoconRecursedKey, + AGGREGATES.defSkipAllow, AGGREGATES.defSkipNoKey); + logger.info("[parity-summary] checkSection {} top-levels {}: hoconKey={} " + + "(= parity-hocon-walked({}) + path-segments-and-internal({}))", + checkSectionTopLevels.size(), checkSectionTopLevels, checkSectionKey, + AGGREGATES.hoconKey, checkSectionKey - AGGREGATES.hoconKey); + logger.info("[parity-summary] cantCheckSection {} top-levels {}: hoconKey={} " + + "(validation skipped; not in checkSection scope)", + cantCheckSectionTopLevels.size(), cantCheckSectionTopLevels, + cantCheckSectionKey); + logger.info("[parity-summary] hocon-align : file-hoconKey({}) = " + + "checkSection({}) + cantCheckSection({})", + hoconKeyInFile, checkSectionKey, cantCheckSectionKey); + logger.info("[parity-summary] bean-align : registry-beanKey({}, across {} bean classes) " + + "= parity-bean({}) = parity-default({})", + beanKeyInRegistry, AGGREGATES.beans.size(), + AGGREGATES.beanKey, AGGREGATES.defBeanKey); + } + + private static int sumTopLevelSubtreeSize(ConfigObject refRoot, Set topLevelKeys) { + int n = 0; + for (String k : topLevelKeys) { + if (!refRoot.containsKey(k)) { + continue; + } + n++; + ConfigValue v = refRoot.get(k); + if (v.valueType() == ConfigValueType.OBJECT) { + n += countHoconKeysRecursive((ConfigObject) v); + } + } + return n; + } + + private static int countHoconKeysRecursive(ConfigObject obj) { + int n = 0; + for (String k : obj.keySet()) { + n++; + ConfigValue v = obj.get(k); + if (v.valueType() == ConfigValueType.OBJECT) { + n += countHoconKeysRecursive((ConfigObject) v); + } + } + return n; + } + + private static int countBeanSettersRecursive(Class beanClass) { + int n = 0; + for (PropertyDescriptor pd : writablePropertyDescriptors(beanClass).values()) { + n++; + Class t = pd.getPropertyType(); + if (isRecursiveConfigBean(t)) { + n += countBeanSettersRecursive(t); + } + } + return n; + } + + private static String typeOf(Object o) { + return o == null ? "null" : o.getClass().getSimpleName(); + } + + private static String format(Object o) { + if (o == null) { + return "null"; + } + if (o instanceof String) { + return "\"" + o + "\""; + } + if (o instanceof List) { + List list = (List) o; + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < list.size(); i++) { + if (i > 0) { + sb.append(", "); + } + sb.append(format(list.get(i))); + } + sb.append("]"); + return sb.toString(); + } + return String.valueOf(o); + } +} diff --git a/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java b/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java new file mode 100644 index 00000000000..67b02e556aa --- /dev/null +++ b/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java @@ -0,0 +1,238 @@ +package org.tron.core.config.args; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Build-time gate that pins every (section, bean) tuple in {@link #SECTIONS} + * so the entire reference.conf <-> {@code *Config} contract is managed in + * one place. Drift fails the build at PR time instead of waiting for + * {@code ConfigBeanFactory} to throw at process startup. + *

+ * Per-section {@code *ConfigTest} files cover behavioural tests (defaults, + * clamps, alias fallbacks); they do not own parity. Adding a new + * {@code *Config} bean: add a {@link Section} entry below. Adding an + * allowlist entry: include an inline rationale comment; new keys are + * expected to bind 1:1 via {@code ConfigBeanFactory} without exception. + */ +public class ConfigParityGateTest { + + private static final class Section { + final String path; + final Class bean; + final Set hoconOrphans; + final Set beanOrphans; + final Set divergent; + + Section(String path, Class bean, + Set hoconOrphans, Set beanOrphans, + Set divergent) { + this.path = path; + this.bean = bean; + this.hoconOrphans = hoconOrphans; + this.beanOrphans = beanOrphans; + this.divergent = divergent; + } + } + + // legacy acronym casing; normalizeNonStandardKeys renames PBFT -> Pbft before bind + private static final Set COMMITTEE_HOCON_ORPHANS = + ConfigParityCheck.allowlist( + "allowPBFT", + "pBFTExpireNum" + ); + + private static final Set COMMITTEE_BEAN_ORPHANS = + ConfigParityCheck.allowlist( + "allowPbft", // bound from HOCON allowPBFT via normalize hook + "pbftExpireNum" // bound from HOCON pBFTExpireNum via normalize hook + ); + + // native: Java reserved word; bound to bean field nativeQueue, read manually after bind. + // topics: list items have optional fields; EventConfig binds the list manually with + // TOPIC_DEFAULTS fallback (field uses @Setter(NONE)). + private static final Set EVENT_HOCON_ORPHANS = + ConfigParityCheck.allowlist( + "native", + "topics" + ); + + // FilterConfig: reference.conf ships [""] as a schema placeholder so operators see + // the expected element type; bean default is [] (genuinely empty). Both mean "no filter". + private static final Set EVENT_DIVERGENT_DEFAULTS = + ConfigParityCheck.allowlist( + "filter.contractAddress", // bean=[] vs reference.conf=[""] schema placeholder + "filter.contractTopic" // bean=[] vs reference.conf=[""] schema placeholder + ); + + // Genesis fields are mainnet seed data with no sensible in-Java default. + private static final Set GENESIS_DIVERGENT_DEFAULTS = + ConfigParityCheck.allowlist( + "timestamp", // mainnet genesis timestamp, no in-Java default + "parentHash", // mainnet genesis parentHash, no in-Java default + "assets", // seed accounts (Zion / Sun / Blackhole); bean ships empty list + "witnesses" // 27 standby witness nodes; bean ships empty list + ); + + private static final Set NODE_HOCON_ORPHANS = + ConfigParityCheck.allowlist( + "isOpenFullTcpDisconnect", // normalized to bean field openFullTcpDisconnect + "metrics" // delegated to MetricsConfig.fromConfig + ); + + private static final Set NODE_BEAN_ORPHANS = + ConfigParityCheck.allowlist( + "openFullTcpDisconnect" // HOCON ships isOpenFullTcpDisconnect; renamed + ); + + private static final Set NODE_DIVERGENT_DEFAULTS = + ConfigParityCheck.allowlist( + "fastForward" // seed node list, no Java-side default + ); + + // Top-level meta-gate: every reference.conf top-level key must be covered by a + // Section entry above or listed here with a rationale. Closes the "new section + // sneaks in" hole. See everyReferenceConfTopLevelKeyIsCovered. + private static final Set TOP_LEVEL_NON_BEAN = + ConfigParityCheck.allowlist( + "crypto", // MiscConfig.cryptoEngine manual-read root + "enery", // MiscConfig manual-read root (preserves historical typo of "energy") + "localwitness", // bound by LocalWitnessConfig, not in the *ConfigBean factory pattern + "net", // deprecated wrapper for net.type; intentionally empty in reference.conf + "seed", // MiscConfig.seedNodeIpList manual-read root (seed.node.ip.list) + "trx" // MiscConfig.trxReferenceBlock manual-read root (trx.reference.block) + ); + + private static final List

SECTIONS; + + static { + Set empty = Collections.emptySet(); + List
s = new ArrayList<>(); + // ctor args: (path, beanClass, hoconOrphans, beanOrphans, divergent) + s.add(new Section("block", BlockConfig.class, + empty, empty, empty)); + s.add(new Section("committee", CommitteeConfig.class, + COMMITTEE_HOCON_ORPHANS, COMMITTEE_BEAN_ORPHANS, empty)); + s.add(new Section("event.subscribe", EventConfig.class, + EVENT_HOCON_ORPHANS, empty, EVENT_DIVERGENT_DEFAULTS)); + s.add(new Section("genesis.block", GenesisConfig.class, + empty, empty, GENESIS_DIVERGENT_DEFAULTS)); + s.add(new Section("node", NodeConfig.class, + NODE_HOCON_ORPHANS, NODE_BEAN_ORPHANS, NODE_DIVERGENT_DEFAULTS)); + s.add(new Section("node.metrics", MetricsConfig.class, + empty, empty, empty)); + s.add(new Section("rate.limiter", RateLimiterConfig.class, + empty, empty, empty)); + s.add(new Section("storage", StorageConfig.class, + empty, empty, empty)); + s.add(new Section("vm", VmConfig.class, + empty, empty, empty)); + SECTIONS = Collections.unmodifiableList(s); + } + + @BeforeClass + public static void resetAggregates() { + ConfigParityCheck.resetAggregates(); + } + + /** Emit cross-section [parity-summary] totals + file-coverage alignment. */ + @AfterClass + public static void logAggregateSummary() { + Set checkSectionTopLevels = new TreeSet<>(); + for (Section s : SECTIONS) { + checkSectionTopLevels.add(s.path.split("\\.", 2)[0]); + } + ConfigParityCheck.logAggregateSummary( + checkSectionTopLevels, TOP_LEVEL_NON_BEAN); + } + + @Test + public void hoconKeysAreBound() { + for (Section s : SECTIONS) { + ConfigParityCheck.assertNoHoconOrphans(s.path, s.bean, s.hoconOrphans); + } + } + + @Test + public void beanPropertiesHaveHoconKeys() { + for (Section s : SECTIONS) { + ConfigParityCheck.assertNoBeanOrphans(s.path, s.bean, s.beanOrphans); + } + } + + @Test + public void defaultValuesMatch() { + List failures = new ArrayList<>(); + for (Section s : SECTIONS) { + try { + ConfigParityCheck.assertDefaultValuesMatch( + s.path, s.bean, s.divergent); + } catch (AssertionError e) { + failures.add(e.getMessage()); + } + } + if (!failures.isEmpty()) { + throw new AssertionError( + failures.size() + " section(s) failed default-value parity:\n\n" + + String.join("\n\n", failures)); + } + } + + /** + * Fails when any allowlist entry no longer resolves to a live HOCON path or + * bean property — i.e. the underlying key/property was renamed or removed + * but the grandfathering entry was left behind. + */ + @Test + public void allowlistEntriesAreLive() { + List failures = new ArrayList<>(); + for (Section s : SECTIONS) { + try { + ConfigParityCheck.assertAllowlistEntriesAreLive( + s.path, s.bean, s.hoconOrphans, s.beanOrphans, s.divergent); + } catch (AssertionError e) { + failures.add(e.getMessage()); + } + } + if (!failures.isEmpty()) { + throw new AssertionError( + failures.size() + " section(s) have dead allowlist entries:\n\n" + + String.join("\n\n", failures)); + } + } + + /** + * Fails when reference.conf grows a top-level key not covered by a Section + * or {@link #TOP_LEVEL_NON_BEAN}. Uses {@code parseResources} so JVM system + * properties don't pollute the top-level key set. + */ + @Test + public void everyReferenceConfTopLevelKeyIsCovered() { + Config refFile = ConfigFactory.parseResources("reference.conf"); + Set topKeys = new TreeSet<>(refFile.root().keySet()); + Set covered = new TreeSet<>(); + for (Section s : SECTIONS) { + covered.add(s.path.split("\\.", 2)[0]); + } + covered.addAll(TOP_LEVEL_NON_BEAN); + + Set orphans = new TreeSet<>(topKeys); + orphans.removeAll(covered); + if (!orphans.isEmpty()) { + throw new AssertionError( + "reference.conf has top-level keys not covered by SECTIONS and not in " + + "TOP_LEVEL_NON_BEAN: " + orphans + + ". Either add a new Section entry (preferred — auto-binds via " + + "*Config bean) or register the key under TOP_LEVEL_NON_BEAN with " + + "an inline rationale."); + } + } +} From 8bffa19a0744ebc92e1fd11c25b511f7f02fb5a0 Mon Sep 17 00:00:00 2001 From: bladehan1 Date: Fri, 5 Jun 2026 16:03:30 +0800 Subject: [PATCH 38/57] feat(ci,config): add comment coverage gate for reference.conf (#6810) --- .github/scripts/check_reference_comments.py | 358 ++++++++++++++++++ .github/workflows/pr-build.yml | 8 +- .github/workflows/pr-check.yml | 6 + common/src/main/resources/reference.conf | 270 +++++++------ .../config/args/ConfigParityGateTest.java | 9 +- 5 files changed, 530 insertions(+), 121 deletions(-) create mode 100644 .github/scripts/check_reference_comments.py diff --git a/.github/scripts/check_reference_comments.py b/.github/scripts/check_reference_comments.py new file mode 100644 index 00000000000..0dd32f391bc --- /dev/null +++ b/.github/scripts/check_reference_comments.py @@ -0,0 +1,358 @@ +#!/usr/bin/env python3 +"""Validate reference.conf comment coverage. + +Rules enforced: + 1. Every user-defined key line must have a comment. + 2. A key is documented by either an inline comment on the same line or a + comment on the immediately preceding line. Blank lines do not count. + 3. Object fields repeated across array elements are checked only on their + first occurrence within that array. + +Design scope — basic coverage gate, not a full HOCON parser +----------------------------------------------------------- +This script is deliberately line-oriented. pyhocon is not used because it +discards comments, and this gate only needs enough structure to track braces +and arrays. + +As a consequence, several HOCON constructs are handled in a simplified way. +Each known limitation is listed below together with its practical risk level +for reference.conf. The gate is intentionally kept simple: reference.conf +uses a small, stable subset of HOCON syntax, and the constructs below are +either forbidden by the project's config conventions or have never appeared +in the file. + +Known limitations (all rated LOW risk for reference.conf): + + A. Silent miss — keys matched by none of the patterns below are neither + checked nor flagged; they pass silently: + + * Quoted keys: "my-key" = value + KEY_LINE requires [A-Za-z_] at the start; a leading '"' never matches. + reference.conf uses only plain lowerCamelCase keys — risk: none. + + * Hyphenated keys: my-key = value + KEY_LINE allows only [A-Za-z0-9_]; '-' is excluded. + reference.conf has no hyphenated keys — risk: none. + + * Append operator: foo += bar + KEY_LINE ends with [:={]; '+' before '=' is not in that set. + reference.conf does not use '+=' — risk: none. + + * Inline-object sub-keys: outer = {inner = 1} + KEY_LINE.match() anchors to the line start, so only the first key on + each line ('outer') is detected; 'inner' inside the braces is missed. + reference.conf expands every block across multiple lines — risk: none. + + * Second key on a bare-value line: a = 1, b = 2 + re.match() matches only at the start; 'b' is invisible to KEY_LINE. + reference.conf never puts two assignments on one line — risk: none. + + B. False positive — non-key content incorrectly flagged as a missing key: + + * Triple-quoted multi-line strings (key = \"\"\" ... \"\"\") + strip_quoted() is line-oriented and does not track triple-quote spans + across lines. Lines inside the string body that look like 'word = ...' + are matched by KEY_LINE and reported as keys lacking comments. + reference.conf contains no triple-quoted strings — risk: none. + If triple-quoted strings are ever introduced, add a triple-quote span + tracker at the top of the collect_keys() loop (see inline comment there). + + C. False pass — a key with no real comment is incorrectly classified as + documented: + + * Block opened on the next line: key =\n{ + opening_after_key() only scans the current line for '{' or '['. + If the opening brace appears on the next line, no named frame is + pushed for the key, so array-element deduplication silently stops + working for that block's contents. + reference.conf always opens blocks on the same line as the key + (e.g. "genesis.block = {") — risk: none. + + * Bare URL value: key = http://example.com + has_inline_comment() sees '//' in the URL and returns True, treating + the URL as an inline comment. Quoting the URL ("http://...") avoids + this because strip_quoted() removes the string contents before the + comment scan. reference.conf contains no bare (unquoted) URLs and all + such values are either quoted or absent — risk: none. +""" +import re +import sys +from pathlib import Path + +KEY_LINE = re.compile(r"^\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*[:={]") +COMMENT_LINE = re.compile(r"^\s*(#|//)") + + +def strip_quoted(line): + """Remove quoted string contents while preserving comments and delimiters.""" + out = [] + quote = None + escaped = False + i = 0 + while i < len(line): + ch = line[i] + if quote: + if escaped: + escaped = False + elif ch == "\\": + escaped = True + elif ch == quote: + quote = None + out.append(ch) + i += 1 + continue + if ch in ('"', "'"): + quote = ch + out.append(ch) + i += 1 + continue + out.append(ch) + i += 1 + return "".join(out) + + +def strip_comments(line): + """Strip # and // comments outside quotes.""" + text = strip_quoted(line) + i = 0 + while i < len(text): + ch = text[i] + if ch == "#": + return text[:i] + if ch == "/" and i + 1 < len(text) and text[i + 1] == "/": + return text[:i] + i += 1 + return text + + +def has_inline_comment(line): + text = strip_quoted(line) + i = 0 + while i < len(text): + if text[i] == "#": + return True + if text[i] == "/" and i + 1 < len(text) and text[i + 1] == "/": + return True + i += 1 + return False + + +def has_prevline_comment(lines, index): + if index == 0: + return False + prev = lines[index - 1] + return bool(prev.strip()) and bool(COMMENT_LINE.match(prev)) + + +def opening_after_key(code, match): + pos = match.end() - 1 + ch = code[pos] + if ch in "{[": + return ch, pos + if ch in ":=": + i = pos + 1 + while i < len(code) and code[i].isspace(): + i += 1 + if i < len(code) and code[i] in "{[": + return code[i], i + return None, None + + +def nearest_array_frame(stack): + for frame in reversed(stack): + if frame["type"] == "array": + return frame + return None + + +def pop_frame(stack, closer): + target_type = "object" if closer == "}" else "array" + while stack: + frame = stack.pop() + if frame["type"] == target_type: + return + + +def scan_structure(code, stack, key_open_pos=None): + i = 0 + while i < len(code): + ch = code[i] + if key_open_pos is not None and i == key_open_pos: + i += 1 + continue + if ch == "{": + stack.append({"type": "object", "name": None, "seen": set()}) + elif ch == "[": + stack.append({"type": "array", "name": None, "seen": set()}) + elif ch == "}": + pop_frame(stack, "}") + elif ch == "]": + pop_frame(stack, "]") + i += 1 + + +def collect_keys(path, list_all=False): + """Scan *path* line by line and classify every HOCON key. + + Returns + ------- + missing : list of (line_no, key) + Keys that lack a comment and are not exempt. Empty means the file + passes the gate. + seen_rows : list of (line_no, key, status) + One entry per matched key line, in file order. Populated only when + *list_all* is True (``--list`` flag); always empty otherwise. + status is one of: "commented" | "dedup" | "missing". + """ + lines = path.read_text(encoding="utf-8").splitlines() + + # stack — bracket-nesting context, one frame per open { or [. + # Each frame is a dict: + # "type" : "object" | "array" + # "name" : str | None — the key that opened this block, or None for + # anonymous braces/brackets. + # "seen" : set — only meaningful on array frames: the set of + # key names already encountered inside this array. + # Enables deduplication so that repeated keys in + # homogeneous array elements (e.g. rate.limiter + # entries) are only checked on their first + # occurrence. + stack = [] + + # missing — accumulates (line_no, key) for every key that is neither + # exempt nor deduplicated yet has no comment. Drives the exit-1 path. + missing = [] + + # seen_rows — full audit log for --list mode: (line_no, key, status). + # Built only when list_all=True to avoid wasting memory in normal runs. + seen_rows = [] + + for index, raw in enumerate(lines): + line_no = index + 1 + + # code: raw line with comment text removed. Used for KEY_LINE + # matching and bracket counting so that "#" / "//" inside values + # do not confuse the structural parser. + code = strip_comments(raw) + + stripped = raw.lstrip() + is_comment = stripped.startswith("#") or stripped.startswith("//") + + # Skip pure comment lines; never treat them as key lines. + match = None if is_comment else KEY_LINE.match(code) + + key = None + status = "non-key" + key_open_pos = None # position in `code` of the { or [ that this key opens + if match: + key = match.group(1) + + # opener: "{" or "[" when the key introduces a block/array on + # the same line (e.g. "node {" or "active = ["). + # key_open_pos: char index of that opener inside `code`, passed + # to scan_structure so it is not counted a second time. + opener, key_open_pos = opening_after_key(code, match) + + # --- Array deduplication --- + # Find the innermost enclosing array frame (if any). Within an + # array, all elements share the same schema, so only the first + # occurrence of each key name needs a comment. + deduped = False + array_frame = nearest_array_frame(stack) + if array_frame is not None: + if key in array_frame["seen"]: + # Already checked on an earlier array element — skip. + deduped = True + else: + # First time we see this key in this array; record it and + # fall through to the normal comment check below. + array_frame["seen"].add(key) + + # --- Comment check --- + # A key is considered documented if it has an inline comment on + # the same line *or* a non-blank comment on the immediately + # preceding line (blank lines between comment and key do NOT + # count as "preceding"). + commented = has_inline_comment(raw) or has_prevline_comment(lines, index) + + # Assign the final status in priority order. + if deduped: + status = "dedup" + elif commented: + status = "commented" + else: + status = "missing" + missing.append((line_no, key)) + + # If this key opens a new block or array, push a fresh frame so + # that nested keys and future deduplication operate in the correct + # scope. We push *after* classifying the key itself so that the + # key is judged in its *parent* scope, not inside itself. + if opener: + stack.append({ + "type": "object" if opener == "{" else "array", + "name": key, + "seen": set(), + }) + + # Walk any remaining { } [ ] characters in `code` that were NOT the + # opener just pushed above. This keeps the stack in sync for lines + # that contain multiple brackets (e.g. closing braces after a value). + scan_structure(code, stack, key_open_pos) + + if list_all and match: + seen_rows.append((line_no, key, status)) + + return missing, seen_rows + + +def main(argv): + list_all = False + args = list(argv[1:]) + if "--list" in args: + list_all = True + args.remove("--list") + if len(args) != 1: + print(f"usage: {argv[0]} [--list] ", file=sys.stderr) + return 2 + + path = Path(args[0]) + if not path.is_file(): + print(f"error: file not found: {path}", file=sys.stderr) + return 2 + + missing, seen_rows = collect_keys(path, list_all) + + if list_all: + for line_no, key, status in seen_rows: + print(f"{line_no}: {key} [{status}]") + print() + + if missing: + lines_out = [ + f"Comment coverage violations ({len(missing)}) — each key " + "needs an inline or immediately preceding comment:" + ] + for line_no, key in missing: + lines_out.append(f" comment: line {line_no}: {key}") + print("\n".join(lines_out)) + print() + + entries = [f"line {line_no}: {key}" for line_no, key in missing] + body = ( + f"reference.conf has {len(missing)} comment coverage violation(s):%0A" + + "%0A".join(entries) + ) + print(f"::error file={path},title=reference.conf::{body}") + print( + f"FAIL: {len(missing)} comment coverage violation(s) in {path}", + file=sys.stderr, + ) + return 1 + + print(f"OK: {path} — all keys have comments") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml index 191d8be778c..f35538c0961 100644 --- a/.github/workflows/pr-build.yml +++ b/.github/workflows/pr-build.yml @@ -260,9 +260,15 @@ jobs: coverage-base-x86_64-gradle- - name: Build (base) + # Test failures on the base branch are tolerated: merge-order races can + # leave the base with a pre-existing failing test that is unrelated to + # this PR. The only output we need from this job is the jacoco XML for + # coverage diffing, so we must not let a stale test failure block it. + continue-on-error: true run: ./gradlew clean build --no-daemon --no-build-cache - name: Test with RocksDB engine (base) + continue-on-error: true run: ./gradlew :framework:testWithRocksDb --no-daemon --no-build-cache - name: Generate module coverage reports (base) @@ -274,7 +280,7 @@ jobs: name: jacoco-coverage-base path: | **/build/reports/jacoco/test/jacocoTestReport.xml - if-no-files-found: error + if-no-files-found: warn coverage-gate: name: Coverage Gate diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index b6988c2c4d3..506a823a4f7 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -119,6 +119,12 @@ jobs: python3 .github/scripts/check_reference_conf.py \ common/src/main/resources/reference.conf + - name: Validate reference.conf comment coverage + shell: bash + run: | + python3 .github/scripts/check_reference_comments.py \ + common/src/main/resources/reference.conf + - name: Set up JDK 17 uses: actions/setup-java@v5 with: diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index 8b69f6ef917..f1e4907274f 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -27,11 +27,13 @@ # # ============================================================================= +# Network type placeholder; deprecated and has no effect. net { # type is deprecated and has no effect. # type = mainnet } +# Storage engine and database settings. storage { # Database engine: "LEVELDB" or "ROCKSDB" (ARM only supports ROCKSDB), case-insensitive db.engine = "LEVELDB" @@ -43,7 +45,7 @@ storage { # Asynchronous writes can significantly improve FullNode block sync performance. db.sync = false - db.directory = "database" + db.directory = "database" # Database directory under the node --output-directory path. # Whether to write transaction result in transactionRetStore transHistory.switch = "on" @@ -99,27 +101,27 @@ storage { # ] properties = [] - needToUpdateAsset = true + needToUpdateAsset = true # Whether to run legacy asset update logic. # RocksDB settings (only used when db.engine = "ROCKSDB") # Strongly recommend NOT modifying unless you know every item's meaning clearly. dbSettings = { - levelNumber = 7 + levelNumber = 7 // Number of RocksDB levels. compactThreads = 0 // 0 = auto: max(availableProcessors, 1) blocksize = 16 // n * KB maxBytesForLevelBase = 256 // n * MB - maxBytesForLevelMultiplier = 10 - level0FileNumCompactionTrigger = 2 + maxBytesForLevelMultiplier = 10 // Level size multiplier. + level0FileNumCompactionTrigger = 2 // L0 files that trigger compaction. targetFileSizeBase = 64 // n * MB - targetFileSizeMultiplier = 1 - maxOpenFiles = 5000 + targetFileSizeMultiplier = 1 // Target file size multiplier. + maxOpenFiles = 5000 // Maximum open files for RocksDB. } - balance.history.lookup = false + balance.history.lookup = false # Whether to enable historical balance lookup. # Checkpoint version for snapshot mechanism. Version 2 enables V2 snapshot. checkpoint.version = 1 - checkpoint.sync = true + checkpoint.sync = true # Sync flag for V2 checkpoint writes; ignored when checkpoint.version = 1 (default). # Estimated number of block transactions (default 1000, min 100, max 10000). # Total cached transactions = 65536 * txCache.estimatedTransactions @@ -136,10 +138,11 @@ storage { # } } +# Node discovery settings. node.discovery = { - enable = false - persist = false - external.ip = "" + enable = false # Whether to enable node discovery. + persist = false # Whether to persist discovered peers. + external.ip = "" # External IP advertised to peers. } # Custom stop condition @@ -149,10 +152,12 @@ node.discovery = { # BlockCount = 12 # block sync count after node start # } +# Backup node election settings. node.backup { port = 10001 # UDP listen port; each member should have the same configuration priority = 0 # Node priority; each member should use a different priority keepAliveInterval = 3000 # Keep-alive interval (ms); each member should have the same configuration + # Backup member IP list. members = [ # "ip", # Peer IP list, better to add at most one IP; must not contain this node's own IP ] @@ -160,19 +165,22 @@ node.backup { # Algorithm for generating public key from private key. Do not modify to avoid forks. crypto { - engine = "eckey" + engine = "eckey" # Signature engine. } # Energy limit block number (config key has typo "enery" preserved for backward compatibility) enery.limit.block.num = 4727890 +# Node metrics settings. node.metrics = { + # Prometheus exporter settings. prometheus { - enable = false - port = 9527 + enable = false # Whether to enable Prometheus metrics. + port = 9527 # Prometheus exporter port. } } +# Node runtime, networking, and API settings. node { # Trust node for solidity node (example: "127.0.0.1:50051"). trustNode = "" @@ -180,8 +188,8 @@ node { # Expose extension api to public or not walletExtensionApi = false - listen.port = 18888 - fetchBlock.timeout = 500 + listen.port = 18888 # P2P listen port. + fetchBlock.timeout = 500 # Block fetch timeout (ms). # Number of blocks to fetch in one batch during sync. Range: [100, 2000]. syncFetchBatchNum = 2000 @@ -192,12 +200,12 @@ node { # Number of validate sign threads, 0 = auto (availableProcessors) validateSignThreadNum = 0 - maxConnections = 30 - minConnections = 8 - minActiveConnections = 3 - maxConnectionsWithSameIp = 2 - maxHttpConnectNumber = 50 - minParticipationRate = 0 + maxConnections = 30 # Maximum peer connections. + minConnections = 8 # Minimum peer connections to maintain. + minActiveConnections = 3 # Minimum active peer connections. + maxConnectionsWithSameIp = 2 # Maximum peer connections per IP. + maxHttpConnectNumber = 50 # Maximum HTTP connections. + minParticipationRate = 0 # Minimum SR participation rate. # WARNING: Some shielded transaction APIs require sending private keys as parameters. # Calling these APIs on untrusted or remote nodes may leak your private keys. @@ -220,56 +228,63 @@ node { # Max block inv hashes accepted per peer per second. Minimum: 1. maxBlockInvPerSecond = 10 + # In P2P service, whether any peer connection can be disconnected, when peers connection over maxConnections. isOpenFullTcpDisconnect = false inactiveThreshold = 600 // seconds - maxFastForwardNum = 4 + maxFastForwardNum = 4 # Number of SRs after the block-producing SR that a fast-forward node forwards blocks to. # Legacy alias `maxActiveNodesWithSameIp` is still accepted from user config # (see NodeConfig alias-fallback) but is intentionally NOT defaulted here — # shipping it in reference.conf would always mask the modern `maxConnectionsWithSameIp`. metricsEnable = false + # P2P protocol version settings. p2p { version = 11111 # Mainnet:11111; Nile:201910292; Shasta:1 } + # Peers to actively connect to. active = [ # Active establish connection in any case # "ip:port", # "ip:port" ] + # List of IP addresses from which incoming connections are accepted; port numbers are configured but ignored. passive = [ # Passive accept connection in any case # "ip:port", # "ip:port" ] + # Fast-forward peer list. fastForward = [ "100.27.171.62:18888", "15.188.6.125:18888" ] + # HTTP API settings. http { - fullNodeEnable = true - fullNodePort = 8090 - solidityEnable = true - solidityPort = 8091 - PBFTEnable = true - PBFTPort = 8092 + fullNodeEnable = true # Whether to enable FullNode HTTP API. + fullNodePort = 8090 # FullNode HTTP API port. + solidityEnable = true # Whether to enable Solidity HTTP API. + solidityPort = 8091 # Solidity HTTP API port. + PBFTEnable = true # Whether to enable PBFT HTTP API. + PBFTPort = 8092 # PBFT HTTP API port. # Maximum HTTP request body size (default 4M). Setting to 0 rejects all non-empty request bodies. # Independent from rpc.maxMessageSize. maxMessageSize = 4194304 } + # gRPC API settings. rpc { - enable = true - port = 50051 - solidityEnable = true - solidityPort = 50061 - PBFTEnable = true - PBFTPort = 50071 + enable = true # Whether to enable FullNode gRPC API. + port = 50051 # FullNode gRPC API port. + solidityEnable = true # Whether to enable Solidity gRPC API. + solidityPort = 50061 # Solidity gRPC API port. + PBFTEnable = true # Whether to enable PBFT gRPC API. + PBFTPort = 50071 # PBFT gRPC API port. # Number of gRPC threads, 0 = auto (availableProcessors / 2) thread = 0 @@ -305,7 +320,7 @@ node { # Reflection service switch for grpcurl tool reflectionService = false - trxCacheEnable = false + trxCacheEnable = false # Whether to enable transaction cache in broadcast transaction API(rpc and http). } # Number of solidity threads in FullNode. @@ -331,18 +346,18 @@ node { # Dynamic loading configuration function dynamicConfig = { - enable = false - checkInterval = 600 + enable = false # Whether to enable dynamic config loading. + checkInterval = 600 # Dynamic config check interval (s). } # Block solidification check unsolidifiedBlockCheck = false - maxUnsolidifiedBlocks = 54 - blockCacheTimeout = 60 + maxUnsolidifiedBlocks = 54 # Maximum unsolidified blocks allowed,when accept transaction + blockCacheTimeout = 60 # Block cache timeout (min) in P2P service # TCP and transaction limits maxTransactionPendingSize = 2000 - pendingTransactionTimeout = 60000 + pendingTransactionTimeout = 60000 # Pending transaction timeout (ms). # total cached trx across handler queues + pending + rePush maxTrxCacheSize = 50000 @@ -351,11 +366,12 @@ node { # Shielded transaction (ZK) zenTokenId = "000000" - shieldedTransInPendingMaxCounts = 10 + shieldedTransInPendingMaxCounts = 10 # Max shielded transactions in pending pool. # Contract proto validation thread pool (0 = auto: availableProcessors) validContractProto.threads = 0 + # DNS discovery and publish settings. dns { # DNS URLs to discover peers, format: tree://{pubkey}@{domain}. Default: empty. treeUrls = [ @@ -396,17 +412,18 @@ node { # Deprecated: these fields were used by the old connection-factor algorithm. # They are still accepted from user config for backward compatibility but have no effect. activeConnectFactor = 0.1 - connectFactor = 0.6 + connectFactor = 0.6 # Deprecated connection factor. + # JSON-RPC API settings. jsonrpc { # Note: Before release_4.8.1, if you turn on jsonrpc and run it for a while and then turn it off, # you will not be able to get the data from eth_getLogs for that period of time. Default: false httpFullNodeEnable = false - httpFullNodePort = 8545 - httpSolidityEnable = false - httpSolidityPort = 8555 - httpPBFTEnable = false - httpPBFTPort = 8565 + httpFullNodePort = 8545 # FullNode JSON-RPC HTTP port. + httpSolidityEnable = false # Whether to enable Solidity JSON-RPC HTTP API. + httpSolidityPort = 8555 # Solidity JSON-RPC HTTP port. + httpPBFTEnable = false # Whether to enable PBFT JSON-RPC HTTP API. + httpPBFTPort = 8565 # PBFT JSON-RPC HTTP port. # The maximum blocks range to retrieve logs for eth_getLogs, default: 5000, <=0 means no limit maxBlockRange = 5000 @@ -484,6 +501,7 @@ rate.limiter = { # } ] + # P2P message rate limits. p2p = { # QPS ceiling for individual P2P message types received from peers. # Values are doubles; fractional QPS is allowed (e.g. 0.5 = one per 2 s). @@ -502,7 +520,9 @@ rate.limiter = { apiNonBlocking = false } +# Bootstrap seed node settings. seed.node = { + # Seed node addresses. ip.list = [ "3.225.171.164:18888", "52.8.46.215:18888", @@ -556,10 +576,10 @@ genesis.block = { # Blackhole – receives burned TRX; initialized to Long.MIN_VALUE so it can only increase assets = [ { - accountName = "Zion" - accountType = "AssetIssue" - address = "TLLM21wteSPs4hKjbxgmH1L6poyMjeTbHm" - balance = "99000000000000000" + accountName = "Zion" # human-readable label stored on-chain; must not be blank + accountType = "AssetIssue" # one of: Normal, AssetIssue, Contract + address = "TLLM21wteSPs4hKjbxgmH1L6poyMjeTbHm" # Base58Check-encoded account address (T...) + balance = "99000000000000000" # initial balance in SUN (1 TRX = 1,000,000 SUN); stored as String }, { accountName = "Sun" @@ -583,9 +603,9 @@ genesis.block = { # The 27 witnesses with the highest voteCount produce the first round of blocks. witnesses = [ { - address: THKJYuUmMKKARNf7s2VT51g5uPY6KEqnat, - url = "http://GR1.com", - voteCount = 100000026 + address: THKJYuUmMKKARNf7s2VT51g5uPY6KEqnat, # Base58Check-encoded SR address (T...) + url = "http://GR1.com", # SR's public URL (informational only, stored on-chain) + voteCount = 100000026 # initial vote count; seeds SR ranking before any user votes are cast }, { address: TVDmPWGYxgi5DNeW8hXrzrhY8Y6zgxPNg4, @@ -735,6 +755,7 @@ genesis.block = { # When it is empty,the localwitness is configured with the private key of the witness account. # localWitnessAccountAddress = +# Local witness private keys. localwitness = [ ] @@ -742,8 +763,9 @@ localwitness = [ # "localwitnesskeystore.json" # ] +# Block processing settings. block = { - needSyncCheck = false + needSyncCheck = false // Whether to check sync before producing blocks. maintenanceTimeInterval = 21600000 // 6 hours (ms) proposalExpireTime = 259200000 // 3 days (ms), controlled by committee proposal checkFrozenTime = 1 // maintenance periods to check frozen balance (test only) @@ -755,14 +777,15 @@ trx.reference.block = "solid" # Transaction expiration time in milliseconds. trx.expiration.timeInMilliseconds = 60000 +# TVM execution settings. vm = { - supportConstant = false - maxEnergyLimitForConstant = 100000000 - minTimeRatio = 0.0 - maxTimeRatio = 5.0 - saveInternalTx = false - lruCacheSize = 500 - vmTrace = false + supportConstant = false # Whether to support constant contract calls. + maxEnergyLimitForConstant = 100000000 # Max energy for constant calls. + minTimeRatio = 0.0 # Minimum VM time ratio. + maxTimeRatio = 5.0 # Maximum VM time ratio. + saveInternalTx = false # Whether to save internal transactions. + lruCacheSize = 500 # VM LRU cache size. + vmTrace = false # Whether to enable VM trace output. # Whether to store featured internal transactions (freeze, vote, etc.) saveFeaturedInternalTx = false @@ -792,67 +815,70 @@ vm = { constantCallTimeoutMs = 0 } -# Governance proposal toggle parameters. All default to 0 (disabled). -# Controlled by on-chain committee proposals, not manual configuration. -# Setting them in config is only for private chain testing. +# Governance / feature-flag parameters. Most are controlled by on-chain committee proposals; +# a few (e.g. allowNewRewardAlgorithm) are startup-only flags. +# Comments list the /wallet/getchainparameters API key and ProposalType ID where applicable. +# All default to 0 (disabled) unless noted. Manual config is for private-chain testing only. committee = { - allowCreationOfContracts = 0 - allowMultiSign = 0 - allowAdaptiveEnergy = 0 - allowDelegateResource = 0 - allowSameTokenName = 0 - allowTvmTransferTrc10 = 0 - allowTvmConstantinople = 0 - allowTvmSolidity059 = 0 - forbidTransferToContract = 0 - allowShieldedTRC20Transaction = 0 - allowTvmIstanbul = 0 - allowMarketTransaction = 0 - allowProtoFilterNum = 0 - allowAccountStateRoot = 0 - changedDelegation = 0 - allowPBFT = 0 - pBFTExpireNum = 20 - allowTransactionFeePool = 0 - allowBlackHoleOptimization = 0 - allowNewResourceModel = 0 - allowReceiptsMerkleRoot = 0 - allowTvmFreeze = 0 - allowTvmVote = 0 - unfreezeDelayDays = 0 - allowTvmLondon = 0 - allowTvmCompatibleEvm = 0 - allowHigherLimitForMaxCpuTimeOfOneTx = 0 - allowNewRewardAlgorithm = 0 - allowOptimizedReturnValueOfChainId = 0 - allowTvmShangHai = 0 - allowOldRewardOpt = 0 - allowEnergyAdjustment = 0 - allowStrictMath = 0 - consensusLogicOptimization = 0 - allowTvmCancun = 0 - allowTvmBlob = 0 - allowAccountAssetOptimization = 0 - allowAssetOptimization = 0 - allowNewReward = 0 - memoFee = 0 - allowDelegateOptimization = 0 - allowDynamicEnergy = 0 - dynamicEnergyThreshold = 0 - dynamicEnergyIncreaseFactor = 0 - dynamicEnergyMaxFactor = 0 + allowCreationOfContracts = 0 # getAllowCreationOfContracts, #9: enable smart contract creation + allowMultiSign = 0 # getAllowMultiSign, #20: enable account permission multi-signature + allowAdaptiveEnergy = 0 # getAllowAdaptiveEnergy, #21: enable adaptive energy limits + allowDelegateResource = 0 # getAllowDelegateResource, #16: enable delegated resource operations + allowSameTokenName = 0 # getAllowSameTokenName, #15: allow duplicate TRC10 token names + allowTvmTransferTrc10 = 0 # getAllowTvmTransferTrc10, #18: allow TRC10 transfer in TVM + allowTvmConstantinople = 0 # getAllowTvmConstantinople, #26: enable Constantinople TVM rules + allowTvmSolidity059 = 0 # getAllowTvmSolidity059, #32: enable Solidity 0.5.9 TVM rules + forbidTransferToContract = 0 # getForbidTransferToContract, #35: forbid direct transfers to contracts + allowShieldedTRC20Transaction = 0 # getAllowShieldedTRC20Transaction, #39: enable shielded TRC20 transfers + allowTvmIstanbul = 0 # getAllowTvmIstanbul, #41: enable Istanbul TVM rules + allowMarketTransaction = 0 # getAllowMarketTransaction, #44: enable market transactions + allowProtoFilterNum = 0 # getAllowProtoFilterNum, #24: enable protobuf field-number filtering + allowAccountStateRoot = 0 # getAllowAccountStateRoot, #25: enable account state root + changedDelegation = 0 # getChangeDelegation, #30: enable delegation changes + allowPBFT = 0 # getAllowPBFT, #40: enable PBFT consensus + pBFTExpireNum = 20 # PBFT message expiration: drop BLOCK messages whose block number lags head by more than this many blocks + allowTransactionFeePool = 0 # getAllowTransactionFeePool, #48: enable transaction fee pool + allowBlackHoleOptimization = 0 # getAllowOptimizeBlackHole, #49: enable blackhole account optimization + allowNewResourceModel = 0 # getAllowNewResourceModel, #51: enable new resource model + allowReceiptsMerkleRoot = 0 # receiptsMerkleRoot: receipts Merkle root (not yet applied at runtime) + allowTvmFreeze = 0 # getAllowTvmFreeze, #52: enable freeze operations in TVM + allowTvmVote = 0 # getAllowTvmVote, #59: enable vote operations in TVM + unfreezeDelayDays = 0 # getUnfreezeDelayDays, #70: resource unfreeze delay days [1, 365] + allowTvmLondon = 0 # getAllowTvmLondon, #63: enable London TVM rules + allowTvmCompatibleEvm = 0 # getAllowTvmCompatibleEvm, #60: enable EVM-compatible TVM behavior + allowHigherLimitForMaxCpuTimeOfOneTx = 0 # getAllowHigherLimitForMaxCpuTimeOfOneTx, #65: allow higher tx CPU limit + allowNewRewardAlgorithm = 0 # newRewardAlgorithm: enable new reward algorithm + allowOptimizedReturnValueOfChainId = 0 # getAllowOptimizedReturnValueOfChainId, #71: optimize CHAINID return value + allowTvmShangHai = 0 # getAllowTvmShangHai, #76: enable Shanghai TVM rules + allowOldRewardOpt = 0 # getAllowOldRewardOpt, #79: enable old reward optimization + allowEnergyAdjustment = 0 # getAllowEnergyAdjustment, #81: enable energy adjustment + allowStrictMath = 0 # getAllowStrictMath, #87: enable strict arithmetic checks + consensusLogicOptimization = 0 # getConsensusLogicOptimization, #88: enable consensus logic optimization + allowTvmCancun = 0 # getAllowTvmCancun, #83: enable Cancun TVM rules + allowTvmBlob = 0 # getAllowTvmBlob, #89: enable blob-related TVM opcodes (BLOBHASH, BLOBBASEFEE) + allowAccountAssetOptimization = 0 # getAllowAccountAssetOptimization, #53: enable account asset optimization + allowAssetOptimization = 0 # getAllowAssetOptimization, #66: enable asset optimization + allowNewReward = 0 # getAllowNewReward, #67: enable new reward logic + memoFee = 0 # getMemoFee, #68: memo fee in SUN [0, 1000000000] + allowDelegateOptimization = 0 # getAllowDelegateOptimization, #69: enable delegate optimization + allowDynamicEnergy = 0 # getAllowDynamicEnergy, #72: enable contract dynamic energy model + dynamicEnergyThreshold = 0 # getDynamicEnergyThreshold, #73: usage threshold for dynamic energy + dynamicEnergyIncreaseFactor = 0 # getDynamicEnergyIncreaseFactor, #74: dynamic energy increase factor + dynamicEnergyMaxFactor = 0 # getDynamicEnergyMaxFactor, #75: maximum dynamic energy factor } +# Event subscription settings. event.subscribe = { - enable = false + enable = false # Whether to enable event subscription. + # Native event queue settings. native = { useNativeQueue = false // if true, use native message queue, else use event plugin. bindport = 5555 // bind port sendqueuelength = 1000 // max length of send queue } - version = 0 + version = 0 # Event subscription version. # Specify the starting block number to sync historical events. Only applicable when version = 1. # After performing a full event sync, set this value to 0 or a negative number. startSyncBlockNum = 0 @@ -861,12 +887,13 @@ event.subscribe = { # dbname|username|password. To auto-create indexes on missing collections, append |2: # dbname|username|password|2 (if collection exists, indexes must be created manually). dbconfig = "" - contractParse = true + contractParse = true # Whether to parse contract event data. + # Event trigger topics. topics = [ { triggerName = "block" // block trigger, the value can't be modified - enable = false + enable = false // Whether to enable this trigger. topic = "block" // plugin topic, the value could be modified solidified = false // if set true, just need solidified block. Default: false }, @@ -875,7 +902,9 @@ event.subscribe = { enable = false topic = "transaction" solidified = false - ethCompatible = false // if set true, add transactionIndex, cumulativeEnergyUsed, preCumulativeLogCount, logList, energyUnitPrice. Default: false + // if set true, add transactionIndex, cumulativeEnergyUsed, preCumulativeLogCount, logList, energyUnitPrice. + // Default: false + ethCompatible = false }, { triggerName = "contractevent" // contractevent represents contractlog data decoded by the ABI. @@ -906,14 +935,17 @@ event.subscribe = { } ] + # Event filter settings. filter = { fromblock = "" // "", "earliest", or a specific block number as the beginning of the queried range toblock = "" // "", "latest", or a specific block number as end of the queried range + // Contract addresses to subscribe; "" means any contract address. contractAddress = [ - "" // contract address to subscribe; "" means any contract address + "" ] + // Contract topics to subscribe; "" means any contract topic. contractTopic = [ - "" // contract topic to subscribe; "" means any contract topic + "" ] } } diff --git a/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java b/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java index 67b02e556aa..cbfedb96643 100644 --- a/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java +++ b/common/src/test/java/org/tron/core/config/args/ConfigParityGateTest.java @@ -82,6 +82,13 @@ private static final class Section { "witnesses" // 27 standby witness nodes; bean ships empty list ); + // properties: List parsed manually via StorageConfig.readProperties(); + // ConfigBeanFactory cannot bind list-of-object fields, so the gate sees it as unbound. + private static final Set STORAGE_HOCON_ORPHANS = + ConfigParityCheck.allowlist( + "properties" // manually parsed by StorageConfig.readProperties() + ); + private static final Set NODE_HOCON_ORPHANS = ConfigParityCheck.allowlist( "isOpenFullTcpDisconnect", // normalized to bean field openFullTcpDisconnect @@ -132,7 +139,7 @@ private static final class Section { s.add(new Section("rate.limiter", RateLimiterConfig.class, empty, empty, empty)); s.add(new Section("storage", StorageConfig.class, - empty, empty, empty)); + STORAGE_HOCON_ORPHANS, empty, empty)); s.add(new Section("vm", VmConfig.class, empty, empty, empty)); SECTIONS = Collections.unmodifiableList(s); From 1691fddbd4f8df35df31713cc7772273d7d03360 Mon Sep 17 00:00:00 2001 From: Federico2014 Date: Tue, 9 Jun 2026 14:39:07 +0800 Subject: [PATCH 39/57] fix(api): bound and truncate api signatures (#6820) --- .../org/tron/core/utils/TransactionUtil.java | 24 +++- .../tron/core/capsule/TransactionCapsule.java | 6 +- .../src/main/java/org/tron/core/Wallet.java | 42 ++++-- .../org/tron/core/services/http/Util.java | 19 ++- .../test/java/org/tron/core/WalletTest.java | 128 ++++++++++++++++++ .../actuator/utils/TransactionUtilTest.java | 90 ++++++++++++ .../org/tron/core/services/http/UtilTest.java | 67 +++++++++ 7 files changed, 352 insertions(+), 24 deletions(-) diff --git a/actuator/src/main/java/org/tron/core/utils/TransactionUtil.java b/actuator/src/main/java/org/tron/core/utils/TransactionUtil.java index 53d6caf5691..8c8a69b7dfe 100644 --- a/actuator/src/main/java/org/tron/core/utils/TransactionUtil.java +++ b/actuator/src/main/java/org/tron/core/utils/TransactionUtil.java @@ -18,6 +18,7 @@ import static org.tron.common.crypto.Hash.sha3omit12; import static org.tron.common.math.Maths.max; import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_COST_BASE_SIZE; +import static org.tron.core.Constant.PER_SIGN_LENGTH; import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION; import com.google.common.base.CaseFormat; @@ -183,8 +184,30 @@ public static String makeUpperCamelMethod(String originName) { .replace("_", ""); } + public static Transaction truncateSignatures(Transaction trx) { + Transaction.Builder builder = trx.toBuilder().clearSignature(); + for (ByteString sig : trx.getSignatureList()) { + if (sig.size() > PER_SIGN_LENGTH) { + builder.addSignature(ByteString.copyFrom(sig.substring(0, PER_SIGN_LENGTH).toByteArray())); + } else { + builder.addSignature(sig); + } + } + return builder.build(); + } + public TransactionSignWeight getTransactionSignWeight(Transaction trx) { TransactionSignWeight.Builder tswBuilder = TransactionSignWeight.newBuilder(); + Result.Builder resultBuilder = Result.newBuilder(); + if (trx.getSignatureCount() > chainBaseManager.getDynamicPropertiesStore() + .getTotalSignNum()) { + resultBuilder.setCode(Result.response_code.OTHER_ERROR); + resultBuilder.setMessage("too many signatures"); + tswBuilder.setResult(resultBuilder); + return tswBuilder.build(); + } + + trx = truncateSignatures(trx); TransactionExtention.Builder trxExBuilder = TransactionExtention.newBuilder(); trxExBuilder.setTransaction(trx); trxExBuilder.setTxid(ByteString.copyFrom(Sha256Hash.hash(CommonParameter @@ -193,7 +216,6 @@ public TransactionSignWeight getTransactionSignWeight(Transaction trx) { retBuilder.setResult(true).setCode(response_code.SUCCESS); trxExBuilder.setResult(retBuilder); tswBuilder.setTransaction(trxExBuilder); - Result.Builder resultBuilder = Result.newBuilder(); if (trx.getRawData().getContractCount() == 0) { resultBuilder.setCode(Result.response_code.OTHER_ERROR); diff --git a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java index 8724a688548..b3f560541cf 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java @@ -251,7 +251,7 @@ public static long checkWeight(Permission permission, List sigs, byt long weight = getWeight(permission, address); if (weight == 0) { throw new PermissionException( - ByteArray.toHexString(sig.toByteArray()) + " is signed by " + encode58Check(address) + ByteArray.toHexString(hash) + " is signed by " + encode58Check(address) + " but it is not contained of permission."); } if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_7_1)) { @@ -631,7 +631,7 @@ public void addSign(byte[] privateKey, AccountStore accountStore) .signHash(getTransactionId().getBytes()))); this.transaction = this.transaction.toBuilder().addSignature(sig).build(); } - + private static void checkPermission(int permissionId, Permission permission, Transaction.Contract contract) throws PermissionException { if (permissionId != 0) { if (permission.getType() != PermissionType.Active) { @@ -714,7 +714,7 @@ public boolean validateSignature(AccountStore accountStore, } } isVerified = true; - } + } return true; } diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index b705b26edc2..079b8e6f3e9 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -228,6 +228,8 @@ import org.tron.protos.Protocol.MarketOrderPairList; import org.tron.protos.Protocol.MarketPrice; import org.tron.protos.Protocol.MarketPriceList; +import org.tron.protos.Protocol.Permission; +import org.tron.protos.Protocol.Permission.PermissionType; import org.tron.protos.Protocol.Proposal; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract; @@ -628,6 +630,17 @@ public GrpcAPI.Return broadcastTransaction(Transaction signedTransaction) { public TransactionApprovedList getTransactionApprovedList(Transaction trx) { TransactionApprovedList.Builder tswBuilder = TransactionApprovedList.newBuilder(); + TransactionApprovedList.Result.Builder resultBuilder = TransactionApprovedList.Result + .newBuilder(); + if (trx.getSignatureCount() > chainBaseManager.getDynamicPropertiesStore() + .getTotalSignNum()) { + resultBuilder.setCode(TransactionApprovedList.Result.response_code.OTHER_ERROR); + resultBuilder.setMessage("too many signatures"); + tswBuilder.setResult(resultBuilder); + return tswBuilder.build(); + } + + trx = TransactionUtil.truncateSignatures(trx); TransactionExtention.Builder trxExBuilder = TransactionExtention.newBuilder(); trxExBuilder.setTransaction(trx); trxExBuilder.setTxid(ByteString.copyFrom(Sha256Hash.hash(CommonParameter @@ -636,8 +649,6 @@ public TransactionApprovedList getTransactionApprovedList(Transaction trx) { retBuilder.setResult(true).setCode(response_code.SUCCESS); trxExBuilder.setResult(retBuilder); tswBuilder.setTransaction(trxExBuilder); - TransactionApprovedList.Result.Builder resultBuilder = TransactionApprovedList.Result - .newBuilder(); if (trx.getRawData().getContractCount() == 0) { resultBuilder.setCode(TransactionApprovedList.Result.response_code.OTHER_ERROR); @@ -650,21 +661,26 @@ public TransactionApprovedList getTransactionApprovedList(Transaction trx) { if (account == null) { throw new PermissionException("Account does not exist!"); } + int permissionId = contract.getPermissionId(); + Permission permission = account.getPermissionById(permissionId); + if (permission == null) { + throw new PermissionException("Permission for this, does not exist!"); + } + if (permissionId != 0) { + if (permission.getType() != PermissionType.Active) { + throw new PermissionException("Permission type is wrong!"); + } + //check operations + if (!WalletUtil.checkPermissionOperations(permission, contract)) { + throw new PermissionException("Permission denied!"); + } + } if (trx.getSignatureCount() > 0) { - List approveList = new ArrayList(); + List approveList = new ArrayList<>(); byte[] hash = Sha256Hash.hash(CommonParameter .getInstance().isECKeyCryptoEngine(), trx.getRawData().toByteArray()); - for (ByteString sig : trx.getSignatureList()) { - if (sig.size() < 65) { - throw new SignatureFormatException( - "Signature size is " + sig.size()); - } - String base64 = TransactionCapsule.getBase64FromByteString(sig); - byte[] address = SignUtils.signatureToAddress(hash, base64, Args.getInstance() - .isECKeyCryptoEngine()); - approveList.add(ByteString.copyFrom(address)); //out put approve list. - } + TransactionCapsule.checkWeight(permission, trx.getSignatureList(), hash, approveList); tswBuilder.addAllApprovedList(approveList); } resultBuilder.setCode(TransactionApprovedList.Result.response_code.SUCCESS); diff --git a/framework/src/main/java/org/tron/core/services/http/Util.java b/framework/src/main/java/org/tron/core/services/http/Util.java index c4556e42c76..5be2495e1f7 100644 --- a/framework/src/main/java/org/tron/core/services/http/Util.java +++ b/framework/src/main/java/org/tron/core/services/http/Util.java @@ -210,9 +210,12 @@ public static String printTransactionSignWeight(TransactionSignWeight transactio String string = JsonFormat.printToString(transactionSignWeight, selfType); JSONObject jsonObject = JSONObject.parseObject(string); JSONObject jsonObjectExt = jsonObject.getJSONObject(TRANSACTION); - jsonObjectExt.put(TRANSACTION, - printTransactionToJSON(transactionSignWeight.getTransaction().getTransaction(), selfType)); - jsonObject.put(TRANSACTION, jsonObjectExt); + if (jsonObjectExt != null) { + jsonObjectExt.put(TRANSACTION, + printTransactionToJSON(transactionSignWeight.getTransaction().getTransaction(), + selfType)); + jsonObject.put(TRANSACTION, jsonObjectExt); + } return jsonObject.toJSONString(); } @@ -221,10 +224,12 @@ public static String printTransactionApprovedList(TransactionApprovedList transa String string = JsonFormat.printToString(transactionApprovedList, selfType); JSONObject jsonObject = JSONObject.parseObject(string); JSONObject jsonObjectExt = jsonObject.getJSONObject(TRANSACTION); - jsonObjectExt.put(TRANSACTION, - printTransactionToJSON(transactionApprovedList.getTransaction().getTransaction(), - selfType)); - jsonObject.put(TRANSACTION, jsonObjectExt); + if (jsonObjectExt != null) { + jsonObjectExt.put(TRANSACTION, + printTransactionToJSON(transactionApprovedList.getTransaction().getTransaction(), + selfType)); + jsonObject.put(TRANSACTION, jsonObjectExt); + } return jsonObject.toJSONString(); } diff --git a/framework/src/test/java/org/tron/core/WalletTest.java b/framework/src/test/java/org/tron/core/WalletTest.java index 0df8d6cdc2c..9dbab338b67 100644 --- a/framework/src/test/java/org/tron/core/WalletTest.java +++ b/framework/src/test/java/org/tron/core/WalletTest.java @@ -1440,5 +1440,133 @@ public void testGetSolidBlock() { Block block = wallet.getSolidBlock(); assertEquals(block2, block); } + + @Test + public void testApprovedListSigBound() { + ECKey ecKey = new ECKey(Utils.getRandom()); + AccountCapsule owner = new AccountCapsule( + ByteString.copyFromUtf8("approved-owner"), + ByteString.copyFrom(ecKey.getAddress()), + Protocol.AccountType.Normal, + initBalance); + chainBaseManager.getAccountStore().put(ecKey.getAddress(), owner); + // Default owner permission: a single key with weight 1, so keysCount == 1. + int keysCount = owner.getPermissionById(0).getKeysCount(); + assertEquals(1, keysCount); + + Transaction unsigned = Transaction.newBuilder().setRawData( + Transaction.raw.newBuilder().addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(TransferContract.newBuilder().setAmount(1) + .setOwnerAddress(ByteString.copyFrom(ecKey.getAddress())) + .setToAddress(ByteString.copyFrom( + ByteArray.fromHexString(RECEIVER_ADDRESS))) + .build())).build()).build()).build(); + + // One valid 65-byte [r][s][recId] signature by the owner. + TransactionCapsule capsule = new TransactionCapsule(unsigned); + capsule.sign(ecKey.getPrivKeyBytes()); + ByteString oneSig = capsule.getInstance().getSignature(0); + + // Within keysCount: the single valid signature is recovered, result is SUCCESS. + GrpcAPI.TransactionApprovedList okList = wallet.getTransactionApprovedList( + unsigned.toBuilder().addSignature(oneSig).build()); + assertEquals(GrpcAPI.TransactionApprovedList.Result.response_code.SUCCESS, + okList.getResult().getCode()); + assertEquals(1, okList.getApprovedListCount()); + + // More signatures than keysCount: checkWeight rejects before recovering any of them, + // so the unbounded ecrecover loop can no longer be triggered. + Transaction.Builder overLimit = unsigned.toBuilder(); + for (int i = 0; i < keysCount + 1; i++) { + overLimit.addSignature(oneSig); + } + GrpcAPI.TransactionApprovedList rejected = + wallet.getTransactionApprovedList(overLimit.build()); + assertEquals(GrpcAPI.TransactionApprovedList.Result.response_code.OTHER_ERROR, + rejected.getResult().getCode()); + assertEquals(0, rejected.getApprovedListCount()); + Assert.assertFalse(rejected.getResult().getMessage().isEmpty()); + } + + @Test + public void testApprovedListSigTruncate() { + ECKey ecKey = new ECKey(Utils.getRandom()); + AccountCapsule owner = new AccountCapsule( + ByteString.copyFromUtf8("approved-owner-trunc"), + ByteString.copyFrom(ecKey.getAddress()), + Protocol.AccountType.Normal, + initBalance); + chainBaseManager.getAccountStore().put(ecKey.getAddress(), owner); + + Transaction unsigned = Transaction.newBuilder().setRawData( + Transaction.raw.newBuilder().addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(TransferContract.newBuilder().setAmount(1) + .setOwnerAddress(ByteString.copyFrom(ecKey.getAddress())) + .setToAddress(ByteString.copyFrom( + ByteArray.fromHexString(RECEIVER_ADDRESS))) + .build())).build()).build()).build(); + + TransactionCapsule capsule = new TransactionCapsule(unsigned); + capsule.sign(ecKey.getPrivKeyBytes()); + ByteString validSig = capsule.getInstance().getSignature(0); + assertEquals(65, validSig.size()); + + // Pad the 65-byte signature with trailing junk bytes. + ByteString oversized = validSig.concat( + ByteString.copyFrom(new byte[] {1, 2, 3, 4, 5})); + assertEquals(70, oversized.size()); + + GrpcAPI.TransactionApprovedList reply = wallet.getTransactionApprovedList( + unsigned.toBuilder().addSignature(oversized).build()); + + // Recovery still succeeds and resolves the owner. + assertEquals(GrpcAPI.TransactionApprovedList.Result.response_code.SUCCESS, + reply.getResult().getCode()); + assertEquals(1, reply.getApprovedListCount()); + // The echoed-back transaction has the signature truncated to 65 bytes. + Transaction echoed = reply.getTransaction().getTransaction(); + assertEquals(1, echoed.getSignatureCount()); + assertEquals(65, echoed.getSignature(0).size()); + assertEquals(validSig, echoed.getSignature(0)); + } + + @Test + public void testApprovedListTooManySigs() { + ECKey ecKey = new ECKey(Utils.getRandom()); + AccountCapsule owner = new AccountCapsule( + ByteString.copyFromUtf8("total-sign-num-owner"), + ByteString.copyFrom(ecKey.getAddress()), + Protocol.AccountType.Normal, + initBalance); + chainBaseManager.getAccountStore().put(ecKey.getAddress(), owner); + + Transaction unsigned = Transaction.newBuilder().setRawData( + Transaction.raw.newBuilder().addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(TransferContract.newBuilder().setAmount(1) + .setOwnerAddress(ByteString.copyFrom(ecKey.getAddress())) + .setToAddress(ByteString.copyFrom( + ByteArray.fromHexString(RECEIVER_ADDRESS))) + .build())).build()).build()).build(); + + TransactionCapsule capsule = new TransactionCapsule(unsigned); + capsule.sign(ecKey.getPrivKeyBytes()); + ByteString oneSig = capsule.getInstance().getSignature(0); + + int totalSignNum = chainBaseManager.getDynamicPropertiesStore().getTotalSignNum(); + Transaction.Builder overLimit = unsigned.toBuilder(); + for (int i = 0; i < totalSignNum + 1; i++) { + overLimit.addSignature(oneSig); + } + + GrpcAPI.TransactionApprovedList rejected = + wallet.getTransactionApprovedList(overLimit.build()); + assertEquals(GrpcAPI.TransactionApprovedList.Result.response_code.OTHER_ERROR, + rejected.getResult().getCode()); + Assert.assertTrue(rejected.getResult().getMessage().contains("too many signatures")); + assertEquals(0, rejected.getApprovedListCount()); + } } diff --git a/framework/src/test/java/org/tron/core/actuator/utils/TransactionUtilTest.java b/framework/src/test/java/org/tron/core/actuator/utils/TransactionUtilTest.java index 15842bfa2c8..54e611e0aac 100644 --- a/framework/src/test/java/org/tron/core/actuator/utils/TransactionUtilTest.java +++ b/framework/src/test/java/org/tron/core/actuator/utils/TransactionUtilTest.java @@ -13,18 +13,23 @@ import static org.tron.core.utils.TransactionUtil.validAssetName; import static org.tron.core.utils.TransactionUtil.validTokenAbbrName; +import com.google.protobuf.Any; import com.google.protobuf.ByteString; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.tron.api.GrpcAPI.TransactionSignWeight; import org.tron.common.BaseTest; import org.tron.common.TestConstants; +import org.tron.common.crypto.ECKey; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.Utils; import org.tron.core.ChainBaseManager; import org.tron.core.Constant; import org.tron.core.Wallet; @@ -36,14 +41,19 @@ import org.tron.protos.Protocol; import org.tron.protos.Protocol.AccountType; import org.tron.protos.Protocol.Transaction; +import org.tron.protos.Protocol.Transaction.Contract; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.BalanceContract.DelegateResourceContract; +import org.tron.protos.contract.BalanceContract.TransferContract; @Slf4j(topic = "capsule") public class TransactionUtilTest extends BaseTest { private static String OWNER_ADDRESS; + @Resource + private TransactionUtil transactionUtil; + /** * Init . */ @@ -452,4 +462,84 @@ public void testConcurrentToString() throws InterruptedException { } Assert.assertTrue(true); } + + @Test + public void testSignWeightSigTruncate() { + ECKey ecKey = new ECKey(Utils.getRandom()); + AccountCapsule owner = new AccountCapsule( + ByteString.copyFromUtf8("sign-weight-owner"), + ByteString.copyFrom(ecKey.getAddress()), + AccountType.Normal, + 10_000_000_000L); + chainBaseManager.getAccountStore().put(ecKey.getAddress(), owner); + + Transaction unsigned = Transaction.newBuilder().setRawData( + Transaction.raw.newBuilder().addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(TransferContract.newBuilder().setAmount(1) + .setOwnerAddress(ByteString.copyFrom(ecKey.getAddress())) + .setToAddress(ByteString.copyFrom( + ByteArray.fromHexString(OWNER_ADDRESS))) + .build())).build()).build()).build(); + + TransactionCapsule capsule = new TransactionCapsule(unsigned); + capsule.sign(ecKey.getPrivKeyBytes()); + ByteString validSig = capsule.getInstance().getSignature(0); + assertEquals(65, validSig.size()); + + // Pad the 65-byte signature with trailing junk bytes. + ByteString oversized = validSig.concat( + ByteString.copyFrom(new byte[] {1, 2, 3, 4, 5})); + assertEquals(70, oversized.size()); + + TransactionSignWeight reply = transactionUtil.getTransactionSignWeight( + unsigned.toBuilder().addSignature(oversized).build()); + + // Recovery still resolves the owner (weight reached the default threshold). + assertEquals(TransactionSignWeight.Result.response_code.ENOUGH_PERMISSION, + reply.getResult().getCode()); + assertEquals(1, reply.getApprovedListCount()); + // The echoed-back transaction has the signature truncated to 65 bytes. + Transaction echoed = reply.getTransaction().getTransaction(); + assertEquals(1, echoed.getSignatureCount()); + assertEquals(65, echoed.getSignature(0).size()); + assertEquals(validSig, echoed.getSignature(0)); + } + + @Test + public void testSignWeightTooManySigs() { + ECKey ecKey = new ECKey(Utils.getRandom()); + AccountCapsule owner = new AccountCapsule( + ByteString.copyFromUtf8("sign-weight-total-num"), + ByteString.copyFrom(ecKey.getAddress()), + AccountType.Normal, + 10_000_000_000L); + chainBaseManager.getAccountStore().put(ecKey.getAddress(), owner); + + Transaction unsigned = Transaction.newBuilder().setRawData( + Transaction.raw.newBuilder().addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(TransferContract.newBuilder().setAmount(1) + .setOwnerAddress(ByteString.copyFrom(ecKey.getAddress())) + .setToAddress(ByteString.copyFrom( + ByteArray.fromHexString(OWNER_ADDRESS))) + .build())).build()).build()).build(); + + TransactionCapsule capsule = new TransactionCapsule(unsigned); + capsule.sign(ecKey.getPrivKeyBytes()); + ByteString oneSig = capsule.getInstance().getSignature(0); + + int totalSignNum = chainBaseManager.getDynamicPropertiesStore().getTotalSignNum(); + Transaction.Builder overLimit = unsigned.toBuilder(); + for (int i = 0; i < totalSignNum + 1; i++) { + overLimit.addSignature(oneSig); + } + + TransactionSignWeight reply = transactionUtil.getTransactionSignWeight( + overLimit.build()); + assertEquals(TransactionSignWeight.Result.response_code.OTHER_ERROR, + reply.getResult().getCode()); + Assert.assertTrue(reply.getResult().getMessage().contains("too many signatures")); + assertEquals(0, reply.getApprovedListCount()); + } } diff --git a/framework/src/test/java/org/tron/core/services/http/UtilTest.java b/framework/src/test/java/org/tron/core/services/http/UtilTest.java index ebcb530bca3..49b8f848e3a 100644 --- a/framework/src/test/java/org/tron/core/services/http/UtilTest.java +++ b/framework/src/test/java/org/tron/core/services/http/UtilTest.java @@ -14,6 +14,7 @@ import org.tron.core.capsule.AccountCapsule; import org.tron.core.config.args.Args; import org.tron.core.utils.TransactionUtil; +import org.tron.json.JSONObject; import org.tron.protos.Protocol; import org.tron.protos.Protocol.Transaction; @@ -189,4 +190,70 @@ public void testPackTransaction() { TransactionSignWeight txSignWeight = transactionUtil.getTransactionSignWeight(transaction); Assert.assertNotNull(txSignWeight); } + + private Transaction buildTooManySigsTransaction() { + String strTransaction = "{\n" + + " \"visible\": false,\n" + + " \"txID\": \"fc33817936b06e50d4b6f1797e62f52d69af6c0da580a607241a9c03a48e390e\",\n" + + " \"raw_data\": {\n" + + " \"contract\": [\n" + + " {\n" + + " \"parameter\": {\n" + + " \"value\": {\n" + + " \"amount\": 10,\n" + + " \"owner_address\":\"41c076305e35aea1fe45a772fcaaab8a36e87bdb55\"," + + " \"to_address\": \"415624c12e308b03a1a6b21d9b86e3942fac1ab92b\"\n" + + " },\n" + + " \"type_url\": \"type.googleapis.com/protocol.TransferContract\"\n" + + " },\n" + + " \"type\": \"TransferContract\"\n" + + " }\n" + + " ],\n" + + " \"ref_block_bytes\": \"d8ed\",\n" + + " \"ref_block_hash\": \"2e066c3259e756f5\",\n" + + " \"expiration\": 1651906644000,\n" + + " \"timestamp\": 1651906586162\n" + + " },\n" + + " \"raw_data_hex\": \"0a02d8ed22082e066c3259e756f540a090bcea89305a65080112610a2d747970" + + "652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e74726163741230" + + "0a1541c076305e35aea1fe45a772fcaaab8a36e87bdb551215415624c12e308b03a1a6b21d9b86e3942fac1a" + + "b92b180a70b2ccb8ea8930\"\n" + + "}"; + Transaction transaction = Util.packTransaction(strTransaction, false); + int totalSignNum = dbManager.getDynamicPropertiesStore().getTotalSignNum(); + ByteString dummySig = ByteString.copyFrom(new byte[65]); + Transaction.Builder builder = transaction.toBuilder(); + for (int i = 0; i < totalSignNum + 1; i++) { + builder.addSignature(dummySig); + } + return builder.build(); + } + + @Test + public void testPrintApprovedListTooManySigsHttpPath() { + Transaction transaction = buildTooManySigsTransaction(); + TransactionApprovedList reply = wallet.getTransactionApprovedList(transaction); + Assert.assertEquals(TransactionApprovedList.Result.response_code.OTHER_ERROR, + reply.getResult().getCode()); + // The early-return reply has no transaction; the HTTP print helper must not throw. + String json = Util.printTransactionApprovedList(reply, false); + JSONObject jsonObject = JSONObject.parseObject(json); + Assert.assertNull(jsonObject.getJSONObject("transaction")); + Assert.assertTrue(jsonObject.getJSONObject("result").getString("message") + .contains("too many signatures")); + } + + @Test + public void testPrintSignWeightTooManySigsHttpPath() { + Transaction transaction = buildTooManySigsTransaction(); + TransactionSignWeight reply = transactionUtil.getTransactionSignWeight(transaction); + Assert.assertEquals(TransactionSignWeight.Result.response_code.OTHER_ERROR, + reply.getResult().getCode()); + // The early-return reply has no transaction; the HTTP print helper must not throw. + String json = Util.printTransactionSignWeight(reply, false); + JSONObject jsonObject = JSONObject.parseObject(json); + Assert.assertNull(jsonObject.getJSONObject("transaction")); + Assert.assertTrue(jsonObject.getJSONObject("result").getString("message") + .contains("too many signatures")); + } } From ea3ffb4e5bd99fdfe179aac326d61a58b465a6ac Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Thu, 11 Jun 2026 16:50:48 +0800 Subject: [PATCH 40/57] fix(jsonrpc): harden RPC/HTTP parameter validation (#6828) --- .../java/org/tron/common/utils/Commons.java | 11 +- .../tron/core/services/http/JsonFormat.java | 13 +- .../core/services/jsonrpc/JsonRpcApiUtil.java | 125 ++++++++++++--- .../services/jsonrpc/TronJsonRpcImpl.java | 60 ++++---- .../services/jsonrpc/filters/LogFilter.java | 7 + .../jsonrpc/filters/LogFilterWrapper.java | 5 +- .../org/tron/core/jsonrpc/JsonRpcTest.java | 49 +++++- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 102 +++++++++++- .../services/http/GetAccountServletTest.java | 32 ++++ .../services/http/GetRewardServletTest.java | 31 ++++ .../services/jsonrpc/BuildArgumentsTest.java | 10 ++ .../services/jsonrpc/JsonRpcApiUtilTest.java | 145 +++++++++++++++++- 12 files changed, 528 insertions(+), 62 deletions(-) diff --git a/chainbase/src/main/java/org/tron/common/utils/Commons.java b/chainbase/src/main/java/org/tron/common/utils/Commons.java index b121e84ecfe..99c20d67f11 100644 --- a/chainbase/src/main/java/org/tron/common/utils/Commons.java +++ b/chainbase/src/main/java/org/tron/common/utils/Commons.java @@ -21,6 +21,8 @@ public class Commons { public static final int ASSET_ISSUE_COUNT_LIMIT_MAX = 1000; + public static final int BASE58_ADDRESS_LENGTH = 34; + public static byte[] decode58Check(String input) { byte[] decodeCheck = Base58.decode(input); if (decodeCheck.length <= 4) { @@ -41,9 +43,16 @@ public static byte[] decode58Check(String input) { return null; } + /** + * Decode a Base58Check address string to its 21-byte form. + */ public static byte[] decodeFromBase58Check(String addressBase58) { if (StringUtils.isEmpty(addressBase58)) { - logger.warn("Warning: Address is empty !!"); + logger.debug("address is empty !!"); + return null; + } + if (addressBase58.length() != BASE58_ADDRESS_LENGTH) { + logger.debug("invalid Base58 address length"); return null; } byte[] address = decode58Check(addressBase58); diff --git a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java index 1dab6c7b941..70b59e72b4d 100644 --- a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java +++ b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java @@ -1313,7 +1313,18 @@ static ByteString unescapeBytesSelfType(String input, final String fliedName) throws InvalidEscapeSequence { //Address base58 -> ByteString if (HttpSelfFormatFieldName.isAddressFormat(fliedName)) { - return ByteString.copyFrom(Commons.decodeFromBase58Check(input)); + byte[] addressBytes = null; + try { + addressBytes = Commons.decodeFromBase58Check(input); + } catch (IllegalArgumentException e) { + // Base58.decode throws on illegal chars -> leave addressBytes null (treated as invalid) + } + if (addressBytes == null) { + // empty / wrong-length / bad-checksum / illegal chars -> all invalid addresses; throw a + // clear error instead of letting ByteString.copyFrom(null) throw a bare NPE. + throw new InvalidEscapeSequence("invalid address for field: " + fliedName); + } + return ByteString.copyFrom(addressBytes); } //Normal String -> ByteString diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java index 6a0957d62d2..f4bba9fbf37 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java @@ -8,11 +8,13 @@ import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; import org.tron.api.GrpcAPI.AssetIssueList; import org.tron.common.crypto.Hash; +import org.tron.common.math.StrictMathWrapper; import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.vm.DataWord; import org.tron.common.utils.ByteArray; @@ -60,6 +62,7 @@ public class JsonRpcApiUtil { 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 final String TX_INDEX_ERROR = "invalid index value"; private static final SecureRandom random = new SecureRandom(); @@ -395,19 +398,24 @@ public static long getUnfreezeAssetAmount(byte[] addressBytes, Wallet wallet) { */ public static byte[] addressCompatibleToByteArray(String hexAddress) throws JsonRpcInvalidParamsException { + // ADDRESS_SIZE (42) is the hex length of a 21-byte address; +2 leaves room for the optional + // "0x" prefix, so a 0x-prefixed 21-byte address (0x41..., 44 chars) is still accepted. + if (hexAddress == null || hexAddress.length() > DecodeUtil.ADDRESS_SIZE + 2) { + throw new JsonRpcInvalidParamsException("invalid address"); + } byte[] addressByte; try { addressByte = ByteArray.fromHexString(hexAddress); if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 && addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { - throw new JsonRpcInvalidParamsException("invalid address hash value"); + throw new JsonRpcInvalidParamsException("invalid address"); } if (addressByte.length == DecodeUtil.ADDRESS_SIZE / 2 - 1) { addressByte = ByteUtil.merge(new byte[] {DecodeUtil.addressPreFixByte}, addressByte); } else if (addressByte[0] != ByteArray.fromHexString(DecodeUtil.addressPreFixString)[0]) { // addressByte.length == DecodeUtil.ADDRESS_SIZE / 2 - throw new JsonRpcInvalidParamsException("invalid address hash value"); + throw new JsonRpcInvalidParamsException("invalid address"); } } catch (Exception e) { throw new JsonRpcInvalidParamsException(e.getMessage()); @@ -415,26 +423,65 @@ public static byte[] addressCompatibleToByteArray(String hexAddress) return addressByte; } + /** Matches a 32-byte hash hex string: optional 0x prefix + 64 hex chars (also caps length). */ + public static final String HASH_REGEX = "^(0x)?[0-9a-fA-F]{64}$"; + /** - * convert 40 hex string of address to byte array, padding 0 ahead if length is odd. + * Convert a hash hex string (optional 0x prefix) to a byte array, validating + * format and length via {@link #HASH_REGEX} first. */ - public static byte[] addressToByteArray(String hexAddress) throws JsonRpcInvalidParamsException { - byte[] addressByte = ByteArray.fromHexString(hexAddress); - if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { - throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); + public static byte[] hashToByteArray(String hash) throws JsonRpcInvalidParamsException { + if (hash == null || !Pattern.matches(HASH_REGEX, hash)) { + throw new JsonRpcInvalidParamsException("invalid hash value"); } - return new DataWord(addressByte).getLast20Bytes(); + byte[] bHash; + try { + bHash = ByteArray.fromHexString(hash); + } catch (Exception e) { + throw new JsonRpcInvalidParamsException(e.getMessage()); + } + return bHash; } /** - * check if topic is hex string of size 64, padding 0 ahead if length is odd. + * Matches a 32-byte topic hex string: optional 0x prefix + 63 or 64 hex chars. + */ + public static final String TOPIC_REGEX = "^(0x)?[0-9a-fA-F]{63,64}$"; + + /** + * Convert a topic hex string (optional 0x prefix, leading zero may be omitted) to a 32-byte + * array, validating format and length via {@link #TOPIC_REGEX} first. */ public static byte[] topicToByteArray(String hexTopic) throws JsonRpcInvalidParamsException { - byte[] topicByte = ByteArray.fromHexString(hexTopic); - if (topicByte.length != 32) { + if (hexTopic == null || !Pattern.matches(TOPIC_REGEX, hexTopic)) { + throw new JsonRpcInvalidParamsException("invalid topic: " + hexTopic); + } + try { + return ByteArray.fromHexString(hexTopic); + } catch (Exception e) { throw new JsonRpcInvalidParamsException("invalid topic: " + hexTopic); } - return topicByte; + } + + /** + * convert 40 hex string of address to byte array, padding 0 ahead if length is odd. + */ + public static byte[] addressToByteArray(String hexAddress) throws JsonRpcInvalidParamsException { + if (hexAddress == null) { + throw new JsonRpcInvalidParamsException("address is null"); + } else if (hexAddress.length() > DecodeUtil.ADDRESS_SIZE) { + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); + } + byte[] addressByte; + try { + addressByte = ByteArray.fromHexString(hexAddress); + } catch (Exception e) { + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); + } + if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); + } + return new DataWord(addressByte).getLast20Bytes(); } public static boolean paramStringIsNull(String string) { @@ -499,7 +546,10 @@ public static long parseQuantityValue(String value) throws JsonRpcInvalidParamsE throw new JsonRpcInvalidParamsException("invalid param value: invalid hex number"); } } - + // QUANTITY is unsigned; reject a signed ("0x-..") value instead of returning a negative. + if (callValue < 0) { + throw new JsonRpcInvalidParamsException("invalid param value: negative"); + } return callValue; } @@ -603,10 +653,11 @@ public static long parseBlockTag(String tag, Wallet wallet) /** * Max allowed length for a JSON-RPC block number hex/decimal input. * API-level DoS guard: rejects pathological inputs before BigInteger parsing, - * whose cost grows quadratically with length. Covers hex (0x + 64 chars for - * uint256) and decimal (78 chars for uint256) representations with headroom. + * whose cost grows quadratically with length. A block number fits a signed long, + * so the longest valid input is 19 chars (decimal Long.MAX_VALUE) or 18 (0x + 16 + * hex); 20 leaves a small margin. */ - private static final int MAX_BLOCK_NUM_HEX_LEN = 100; + private static final int MAX_BLOCK_NUM_HEX_LEN = 20; /** * Parse a JSON-RPC block number (hex "0x..." or decimal) into a long, @@ -636,16 +687,50 @@ public static long parseBlockNumber(String blockNum) } /** - * 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. + * Parse a block tag, or a 0x-prefixed hex block number. */ public static long parseBlockNumber(String blockNumOrTag, Wallet wallet) throws JsonRpcInvalidParamsException { if (isBlockTag(blockNumOrTag)) { return parseBlockTag(blockNumOrTag, wallet); } - return ByteArray.jsonHexToLong(blockNumOrTag); + if (blockNumOrTag == null || !blockNumOrTag.startsWith("0x")) { + throw new JsonRpcInvalidParamsException("Incorrect hex syntax"); + } + return parseBlockNumber(blockNumOrTag); + } + + /** + * Max hex digits of a 32-bit int (0x7FFFFFFF). A transaction index fits a signed int, so the + * longest valid input is "0x" + 8 hex digits; the +2 in the guard covers the prefix. + */ + private static final int MAX_TX_INDEX_HEX_LEN = 8; + + /** + * Parse a 0x-prefixed hex transaction index at the JSON-RPC boundary. + */ + public static int parseTxIndex(String index) throws JsonRpcInvalidParamsException { + if (index == null || index.length() > MAX_TX_INDEX_HEX_LEN + 2) { + throw new JsonRpcInvalidParamsException(TX_INDEX_ERROR); + } + try { + return ByteArray.jsonHexToInt(index); + } catch (Exception e) { + throw new JsonRpcInvalidParamsException(TX_INDEX_ERROR); + } + } + + /** + * Compute feeLimit = gas * energyFee with overflow protection. A gas value large enough to + * overflow a signed 64-bit feeLimit is rejected as invalid-params instead of silently wrapping + * to a bogus (possibly negative) value. + */ + public static long calcFeeLimit(long gas, long energyFee) throws JsonRpcInvalidParamsException { + try { + return StrictMathWrapper.multiplyExact(gas, energyFee); + } catch (ArithmeticException e) { + throw new JsonRpcInvalidParamsException("invalid gas: fee limit overflow"); + } } public static String generateFilterId() { diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 4d919b81ece..82568755892 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -3,15 +3,18 @@ import static org.tron.core.Wallet.CONTRACT_VALIDATE_ERROR; import static org.tron.core.services.http.Util.setTransactionExtraData; import static org.tron.core.services.http.Util.setTransactionPermissionId; -import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.BLOCK_NUM_ERROR; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.FINALIZED_STR; +import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.HASH_REGEX; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.LATEST_STR; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.addressCompatibleToByteArray; +import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.calcFeeLimit; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.generateFilterId; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getEnergyUsageTotal; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTransactionIndex; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTxID; +import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.hashToByteArray; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseBlockNumber; +import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseTxIndex; import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract; import com.google.common.annotations.VisibleForTesting; @@ -157,7 +160,11 @@ public enum RequestSource { private final Map blockFilter2ResultSolidity = new ConcurrentHashMap<>(); - public static final String HASH_REGEX = "(0x)?[a-zA-Z0-9]{64}$"; + // Storage key is a 32-byte word: 64 hex chars + optional "0x" prefix = 66 max. + // Reject oversized input before fromHexString / new DataWord, which would otherwise + // throw a RuntimeException whose message embeds the whole hex (amplifying log output) + // and surface as a -32603 Internal error instead of -32602 invalid params. + public static final int MAX_STORAGE_KEY_HEX_LEN = 66; public static final String INVALID_BLOCK_RANGE = "invalid block range params"; @@ -366,20 +373,6 @@ public BlockResult ethGetBlockByNumber(String blockNumOrTag, Boolean fullTransac return (b == null ? null : getBlockResult(b, fullTransactionObjects)); } - private byte[] hashToByteArray(String hash) throws JsonRpcInvalidParamsException { - if (!Pattern.matches(HASH_REGEX, hash)) { - throw new JsonRpcInvalidParamsException("invalid hash value"); - } - - byte[] bHash; - try { - bHash = ByteArray.fromHexString(hash); - } catch (Exception e) { - throw new JsonRpcInvalidParamsException(e.getMessage()); - } - return bHash; - } - /** * Reject any block selector that is not "latest". * Accepts "latest" silently; throws for other tags, numeric blocks, or invalid input. @@ -612,8 +605,19 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT throws JsonRpcInvalidParamsException { requireLatestBlockTag(blockNumOrTag); + if (storageIdx == null || storageIdx.length() > MAX_STORAGE_KEY_HEX_LEN) { + throw new JsonRpcInvalidParamsException("invalid storage key value"); + } + byte[] addressByte = addressCompatibleToByteArray(address); + DataWord index; + try { + index = new DataWord(ByteArray.fromHexString(storageIdx)); + } catch (Exception e) { + throw new JsonRpcInvalidParamsException("invalid storage key value"); + } + // get contract from contractStore BytesMessage.Builder build = BytesMessage.newBuilder(); BytesMessage bytesMessage = build.setValue(ByteString.copyFrom(addressByte)).build(); @@ -627,7 +631,7 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT storage.setContractVersion(smartContract.getVersion()); storage.generateAddrHash(smartContract.getTrxHash().toByteArray()); - DataWord value = storage.getValue(new DataWord(ByteArray.fromHexString(storageIdx))); + DataWord value = storage.getValue(index); return ByteArray.toJsonHex(value == null ? new byte[32] : value.getData()); } @@ -812,14 +816,9 @@ private TransactionResult formatTransactionResult(TransactionInfo transactioninf private TransactionResult getTransactionByBlockAndIndex(Block block, String index) throws JsonRpcInvalidParamsException { - int txIndex; - try { - txIndex = ByteArray.jsonHexToInt(index); - } catch (Exception e) { - throw new JsonRpcInvalidParamsException("invalid index value"); - } + int txIndex = parseTxIndex(index); - if (txIndex >= block.getTransactionsCount()) { + if (txIndex < 0 || txIndex >= block.getTransactionsCount()) { return null; } @@ -934,9 +933,10 @@ public List getBlockReceipts(String blockNumOrHashOrTag) Block block = null; - if (Pattern.matches(HASH_REGEX, blockNumOrHashOrTag)) { + if (blockNumOrHashOrTag != null && Pattern.matches(HASH_REGEX, blockNumOrHashOrTag)) { block = getBlockByJsonHash(blockNumOrHashOrTag); } else { + // null falls through to getBlockByNumOrTag -> parseBlockNumber -> -32602 (not an NPE) block = getBlockByNumOrTag(blockNumOrHashOrTag); } @@ -1141,7 +1141,11 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress, ABI.Builder abiBuilder = ABI.newBuilder(); if (StringUtils.isNotEmpty(args.getAbi())) { String abiStr = "{" + "\"entrys\":" + args.getAbi() + "}"; - JsonFormat.merge(abiStr, abiBuilder, args.isVisible()); + try { + JsonFormat.merge(abiStr, abiBuilder, args.isVisible()); + } catch (StackOverflowError e) { + throw new JsonRpcInvalidParamsException("invalid abi"); + } } SmartContract.Builder smartBuilder = SmartContract.newBuilder(); @@ -1167,7 +1171,7 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress, .createTransactionCapsule(build.build(), ContractType.CreateSmartContract).getInstance(); Transaction.Builder txBuilder = tx.toBuilder(); Transaction.raw.Builder rawBuilder = tx.getRawData().toBuilder(); - rawBuilder.setFeeLimit(args.parseGas() * wallet.getEnergyFee()); + rawBuilder.setFeeLimit(calcFeeLimit(args.parseGas(), wallet.getEnergyFee())); txBuilder.setRawData(rawBuilder); tx = setTransactionPermissionId(args.getPermissionId(), txBuilder.build()); @@ -1216,7 +1220,7 @@ private TransactionJson buildTriggerSmartContractTransaction(byte[] ownerAddress Transaction.Builder txBuilder = tx.toBuilder(); Transaction.raw.Builder rawBuilder = tx.getRawData().toBuilder(); - rawBuilder.setFeeLimit(args.parseGas() * wallet.getEnergyFee()); + rawBuilder.setFeeLimit(calcFeeLimit(args.parseGas(), wallet.getEnergyFee())); txBuilder.setRawData(rawBuilder); Transaction trx = wallet diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java index d2bd58f6c56..03232f3549d 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java @@ -57,6 +57,10 @@ public LogFilter(FilterRequest fr) throws JsonRpcInvalidParamsException { List addr = new ArrayList<>(); int i = 0; for (Object s : (ArrayList) fr.getAddress()) { + if (!(s instanceof String)) { + throw new JsonRpcInvalidParamsException( + String.format("invalid address at index %d: %s", i, s)); + } try { addr.add(addressToByteArray((String) s)); i++; @@ -93,6 +97,9 @@ public LogFilter(FilterRequest fr) throws JsonRpcInvalidParamsException { List t = new ArrayList<>(); for (Object s : ((ArrayList) topic)) { + if (!(s instanceof String)) { + throw new JsonRpcInvalidParamsException("invalid topic(s): " + s); + } try { t.add(new DataWord(topicToByteArray((String) s)).getData()); } catch (JsonRpcInvalidParamsException e) { diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java index 0331ab3694a..3f9582d4825 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java @@ -6,7 +6,6 @@ import com.google.protobuf.ByteString; import lombok.Getter; 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.jsonrpc.JsonRpcInvalidParamsException; @@ -35,14 +34,14 @@ public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet long fromBlockSrc; long toBlockSrc; if (fr.getBlockHash() != null) { - String blockHash = ByteArray.fromHex(fr.getBlockHash()); if (fr.getFromBlock() != null || fr.getToBlock() != null) { throw new JsonRpcInvalidParamsException( "cannot specify both BlockHash and FromBlock/ToBlock, choose one or the other"); } + byte[] blockHashBytes = JsonRpcApiUtil.hashToByteArray(fr.getBlockHash()); Block block = null; if (wallet != null) { - block = wallet.getBlockById(ByteString.copyFrom(ByteArray.fromHexString(blockHash))); + block = wallet.getBlockById(ByteString.copyFrom(blockHashBytes)); } if (block == null) { throw new JsonRpcInvalidParamsException("invalid blockHash"); diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java index 5f577194dff..49f875f3823 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java @@ -143,13 +143,13 @@ public void testAddressCompatibleToByteArray() { try { addressCompatibleToByteArray(rawAddress.substring(1)); } catch (JsonRpcInvalidParamsException e) { - Assert.assertEquals("invalid address hash value", e.getMessage()); + Assert.assertEquals("invalid address", e.getMessage()); } try { addressCompatibleToByteArray(rawAddress + "00"); } catch (JsonRpcInvalidParamsException e) { - Assert.assertEquals("invalid address hash value", e.getMessage()); + Assert.assertEquals("invalid address", e.getMessage()); } } @@ -177,6 +177,22 @@ public void testAddressToByteArray() { } catch (JsonRpcInvalidParamsException e) { Assert.fail(); } + + // oversized input rejected before fromHexString + try { + addressToByteArray("0x" + new String(new char[64]).replace('\0', 'a')); + Assert.fail(); + } catch (JsonRpcInvalidParamsException e) { + Assert.assertTrue(e.getMessage().contains("invalid address")); + } + + // invalid hex char -> invalid params, not a leaked DecoderException + try { + addressToByteArray("0x548794500882809695a8a687866e76d4271a1abz"); + Assert.fail(); + } catch (JsonRpcInvalidParamsException e) { + Assert.assertTrue(e.getMessage().contains("invalid address")); + } } /** @@ -185,7 +201,7 @@ public void testAddressToByteArray() { @Test public void testLogFilter() { - //topic must be 64 hex string + //topic must be 63 or 64 hex string, full 64-char form here try { new LogFilter(new FilterRequest(null, null, null, new String[] {"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"}, @@ -194,6 +210,19 @@ public void testLogFilter() { Assert.fail(); } + //63-char form: leading zero stripped by some clients, padded back to the same topic + String paddedAddressTopic = + "000000000000000000000000f0cc5a2a84cd0f68ed1667070934542d673acbd8"; + try { + LogFilter full = new LogFilter(new FilterRequest(null, null, null, + new String[] {null, "0x" + paddedAddressTopic}, null)); + LogFilter stripped = new LogFilter(new FilterRequest(null, null, null, + new String[] {null, "0x" + paddedAddressTopic.substring(1)}, null)); + Assert.assertArrayEquals(full.getTopics().get(1)[0], stripped.getTopics().get(1)[0]); + } catch (JsonRpcInvalidParamsException e) { + Assert.fail(); + } + try { new LogFilter(new FilterRequest(null, null, null, new String[] {"0x0"}, null)); } catch (JsonRpcInvalidParamsException e) { @@ -209,6 +238,20 @@ public void testLogFilter() { Assert.assertTrue(e.getMessage().contains("invalid topic")); } + // non-string element in address array -> -32602, not a leaked ClassCastException + JsonRpcInvalidParamsException badAddrElement = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> new LogFilter(new FilterRequest(null, null, + new ArrayList<>(Collections.singletonList(1)), null, null))); + Assert.assertEquals("invalid address at index 0: 1", badAddrElement.getMessage()); + + // non-string element in nested topic array -> -32602, not a leaked ClassCastException + JsonRpcInvalidParamsException badTopicElement = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> new LogFilter(new FilterRequest(null, null, null, + new Object[] {new ArrayList<>(Collections.singletonList(1))}, null))); + Assert.assertEquals("invalid topic(s): 1", badTopicElement.getMessage()); + // topic size should be <= 4 try { new LogFilter(new FilterRequest(null, null, null, diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index f753045d259..870ce317663 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -56,6 +56,7 @@ import org.tron.core.services.jsonrpc.TronJsonRpcImpl; import org.tron.core.services.jsonrpc.filters.LogFilterWrapper; import org.tron.core.services.jsonrpc.types.BlockResult; +import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.TransactionReceipt; import org.tron.core.services.jsonrpc.types.TransactionResult; import org.tron.json.JSON; @@ -514,11 +515,9 @@ public void testBlockTagParsing() { () -> parseBlockNumber("abc", wallet)); Assert.assertEquals("Incorrect hex syntax", abcEx.getMessage()); - // parseBlockNumber: malformed hex -> throws Exception hexEx = Assert.assertThrows(Exception.class, () -> parseBlockNumber("0xxabc", wallet)); - // https://bugs.openjdk.org/browse/JDK-8176425, from JDK 12, the exception message is changed - Assert.assertTrue(hexEx.getMessage().startsWith("For input string: \"xabc\"")); + Assert.assertEquals("invalid block number", hexEx.getMessage()); } @Test @@ -579,10 +578,29 @@ public void testGetStorageAt() { () -> tronJsonRpc.getStorageAt("", "", "abc")); Assert.assertEquals("invalid block number", e6.getMessage()); + // storageIdx length oversized -> invalid storage key value + String addr = "0xabd4b9367799eaa3197fecb144eb71de1e049abc"; + Exception e7 = Assert.assertThrows(Exception.class, + () -> tronJsonRpc.getStorageAt(addr, + "0x" + new String(new char[65]).replace('\0', 'a'), "latest")); + Assert.assertEquals("invalid storage key value", e7.getMessage()); + + // storageIdx is null -> invalid storage key value + Exception e8 = Assert.assertThrows(Exception.class, + () -> tronJsonRpc.getStorageAt(addr, null, "latest")); + Assert.assertEquals("invalid storage key value", e8.getMessage()); + + // storageIdx is valid length but decodes to >32 bytes (66 hex chars without 0x = 33 bytes): + // DataWord rejects it -> invalid storage key value (-32602), not a leaked Internal error. + Exception e9 = Assert.assertThrows(Exception.class, + () -> tronJsonRpc.getStorageAt(addr, + new String(new char[66]).replace('\0', 'a'), "latest")); + Assert.assertEquals("invalid storage key value", e9.getMessage()); + // latest happy path: address is an account, not a contract, so returns 32 zero bytes try { String value = tronJsonRpc.getStorageAt( - "0xabd4b9367799eaa3197fecb144eb71de1e049abc", "0x0", "latest"); + addr, "0x0", "latest"); Assert.assertEquals(ByteArray.toJsonHex(new byte[32]), value); } catch (Exception e) { Assert.fail(); @@ -674,6 +692,31 @@ public void testGetTransactionByBlockNumberAndIndex() { Assert.fail(); } + // negative index is out of range too -> null (not an Internal error) + try { + TransactionResult result = tronJsonRpc.getTransactionByBlockNumberAndIndex( + ByteArray.toJsonHex(blockCapsule1.getNum()), "0x-1"); + Assert.assertNull(result); + } catch (Exception e) { + Assert.fail(); + } + + // leading zeros are tolerated: "0x00" parses to index 0 + try { + TransactionResult result = tronJsonRpc.getTransactionByBlockNumberAndIndex( + ByteArray.toJsonHex(blockCapsule1.getNum()), "0x00"); + Assert.assertNotNull(result); + Assert.assertEquals(ByteArray.toJsonHex(0L), result.getTransactionIndex()); + } catch (Exception e) { + Assert.fail(); + } + + // oversized index (> 8 hex digits) rejected before parsing + Exception oversizedEx = Assert.assertThrows(Exception.class, + () -> tronJsonRpc.getTransactionByBlockNumberAndIndex( + ByteArray.toJsonHex(blockCapsule1.getNum()), "0x123456789")); + Assert.assertEquals("invalid index value", oversizedEx.getMessage()); + // latest -> blockCapsule1 (head) try { TransactionResult result = tronJsonRpc.getTransactionByBlockNumberAndIndex("latest", "0x0"); @@ -1006,6 +1049,29 @@ public void testLogFilterWrapper() { Assert.fail(); } + JsonRpcInvalidParamsException shortHashEx = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> new LogFilterWrapper(new FilterRequest(null, null, null, + null, "0x111111"), + 100, null, false)); + Assert.assertEquals("invalid hash value", shortHashEx.getMessage()); + + JsonRpcInvalidParamsException oversizedHashEx = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> new LogFilterWrapper(new FilterRequest(null, null, null, + null, + "0x" + new String(new char[1000]).replace('\0', 'a')), + 100, null, false)); + Assert.assertEquals("invalid hash value", oversizedHashEx.getMessage()); + + JsonRpcInvalidParamsException validHashEx = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> new LogFilterWrapper(new FilterRequest(null, null, null, + null, + "0x" + new String(new char[64]).replace('\0', 'a')), + 100, null, false)); + Assert.assertEquals("invalid blockHash", validHashEx.getMessage()); + // reset Args.getInstance().setJsonRpcMaxBlockRange(oldMaxBlockRange); } @@ -1312,6 +1378,10 @@ public void testGetBlockReceipts() { () -> tronJsonRpc.getBlockReceipts("test")); Assert.assertEquals("invalid block number", testReceiptsEx.getMessage()); + Exception nullReceiptsEx = Assert.assertThrows(Exception.class, + () -> tronJsonRpc.getBlockReceipts(null)); + Assert.assertEquals("invalid block number", nullReceiptsEx.getMessage()); + try { List transactionReceiptList = tronJsonRpc.getBlockReceipts("0x2"); Assert.assertNull(transactionReceiptList); @@ -1389,6 +1459,30 @@ public void testBuildTransactionTransfer() { } } + @Test + public void testBuildTransactionRejectsDeeplyNestedAbi() { + // A pathological ABI with deep nesting overflows the recursive JsonFormat parser. + // buildCreateSmartContractTransaction must surface this as invalid-params (-32602), + // not let a StackOverflowError escape as a generic internal error. + int depth = 200_000; + StringBuilder abi = new StringBuilder("[],\"x\":"); + for (int i = 0; i < depth; i++) { + abi.append('['); + } + for (int i = 0; i < depth; i++) { + abi.append(']'); + } + + BuildArguments args = new BuildArguments(); + args.setFrom("0xabd4b9367799eaa3197fecb144eb71de1e049abc"); + args.setData("60806040"); + args.setAbi(abi.toString()); + + JsonRpcInvalidParamsException e = Assert.assertThrows(JsonRpcInvalidParamsException.class, + () -> tronJsonRpc.buildTransaction(args)); + Assert.assertEquals("invalid abi", e.getMessage()); + } + @Test public void testWeb3ClientVersion() { try { diff --git a/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java index 466917d0cd5..31cfc174a77 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java @@ -14,6 +14,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.tron.common.crypto.ECKey; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.StringUtil; import org.tron.protos.Protocol.Account; public class GetAccountServletTest extends BaseHttpTest { @@ -57,4 +58,35 @@ public void testGetAccountGet() throws Exception { assertFalse("Should not contain error", content.contains("\"Error\"")); assertTrue("Should contain address", content.contains("address")); } + + @Test + public void testGetAccountPostOversizedBase58Address() throws Exception { + String oversized = addrStr + "0"; + String jsonParam = "{\"address\": \"" + oversized + "\", \"visible\": true}"; + MockHttpServletRequest request = postRequest(jsonParam); + MockHttpServletResponse response = newResponse(); + servlet.doPost(request, response); + String content = response.getContentAsString(); + // null from decodeFromBase58Check now surfaces a clear "invalid address" error instead of a + // bare NullPointerException (JsonFormat checks for null before ByteString.copyFrom). + assertTrue("oversized address should surface a clear invalid-address error: " + content, + content.contains("invalid address")); + } + + @Test + public void testGetAccountPostVisibleBase58Address() throws Exception { + // visible=true happy path: a valid 34-char Base58 address decodes back to the same 21 bytes + // (decodeFromBase58Check success path) and returns the account with no error. + String base58Addr = StringUtil.encode58Check(address); + String jsonParam = "{\"address\": \"" + base58Addr + "\", \"visible\": true}"; + MockHttpServletRequest request = postRequest(jsonParam); + + MockHttpServletResponse response = newResponse(); + servlet.doPost(request, response); + assertEquals(200, response.getStatus()); + verify(wallet).getAccount(argThat(req -> req != null && req.getAddress().equals(addr))); + String content = response.getContentAsString(); + assertFalse("Should not contain error", content.contains("\"Error\"")); + assertTrue("Should contain address", content.contains("address")); + } } diff --git a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java index 76f85da5d8f..9afa5607a66 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java @@ -130,4 +130,35 @@ public void getByBlankParamTest() { } } + @Test + public void getRewardByOversizedValidCharAddressTest() { + // 41-char, all-valid-Base58 address: the length guard returns null -> reward 0. + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + MockHttpServletResponse response = new MockHttpServletResponse(); + request.addParameter("address", "T" + new String(new char[40]).replace('\0', 'a')); + new GetRewardServlet().doPost(request, response); + try { + JSONObject result = JSONObject.parseObject(response.getContentAsString()); + Assert.assertEquals(0, (int) result.get("reward")); + Assert.assertNull(result.get("Error")); + } catch (UnsupportedEncodingException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void getRewardByOversizedIllegalCharAddressTest() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + MockHttpServletResponse response = new MockHttpServletResponse(); + request.addParameter("address", "T" + new String(new char[40]).replace('\0', '0')); + new GetRewardServlet().doPost(request, response); + try { + JSONObject result = JSONObject.parseObject(response.getContentAsString()); + Assert.assertEquals(0, (int) result.get("reward")); + Assert.assertNull(result.get("Error")); + } catch (UnsupportedEncodingException e) { + Assert.fail(e.getMessage()); + } + } + } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java index 753d93d47f4..3f2a91bea1c 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java @@ -69,6 +69,16 @@ public void testParseGas() throws JsonRpcInvalidParamsException { Assert.assertEquals(16L, gas); } + @Test + public void parseValueRejectsNegative() { + // QUANTITY is unsigned; a signed "0x-.." value must be rejected, not returned as negative. + BuildArguments args = new BuildArguments(); + args.setValue("0x-1"); + JsonRpcInvalidParamsException e = Assert.assertThrows(JsonRpcInvalidParamsException.class, + () -> args.parseValue()); + Assert.assertEquals("invalid param value: negative", e.getMessage()); + } + @Test public void resolveData_inputOnly_returnsInput() throws JsonRpcInvalidParamsException { BuildArguments args = new BuildArguments(); diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcApiUtilTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcApiUtilTest.java index 6aaeea2cc4e..f719446df72 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcApiUtilTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcApiUtilTest.java @@ -49,8 +49,8 @@ public void parseBlockNumberRejectsOverflow() { @Test public void parseBlockNumberRejectsOversized() { - // 101 chars exceeds the 100-char limit - String tooLong = "0x" + new String(new char[99]).replace('\0', 'a'); + // 21 chars exceeds the 20-char limit + String tooLong = "0x" + new String(new char[18]).replace('\0', '0') + "1"; JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, () -> JsonRpcApiUtil.parseBlockNumber(tooLong)); assertEquals("invalid block number", e.getMessage()); @@ -75,4 +75,145 @@ public void parseBlockNumberRejectsEmpty() { assertThrows(JsonRpcInvalidParamsException.class, () -> JsonRpcApiUtil.parseBlockNumber("")); } + + @Test + public void addressCompatibleToByteArrayNormal() + throws JsonRpcInvalidParamsException { + String addr = "0xabd4b9367799eaa3197fecb144eb71de1e049abc"; + assertEquals(21, JsonRpcApiUtil.addressCompatibleToByteArray(addr).length); + } + + @Test + public void addressCompatibleToByteArrayRejectsOversized() { + // 45 chars + String justOver = "0x" + new String(new char[43]).replace('\0', 'a'); + JsonRpcInvalidParamsException e1 = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.addressCompatibleToByteArray(justOver)); + assertEquals("invalid address", e1.getMessage()); + } + + @Test + public void addressCompatibleToByteArrayRejectsNull() { + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.addressCompatibleToByteArray(null)); + assertEquals("invalid address", e.getMessage()); + } + + @Test + public void hashToByteArrayAcceptsValidHex() throws JsonRpcInvalidParamsException { + String hash = "0x" + new String(new char[64]).replace('\0', 'a'); + assertEquals(32, JsonRpcApiUtil.hashToByteArray(hash).length); + } + + @Test + public void hashToByteArrayRejectsNonHexChars() { + String hash = "0x" + new String(new char[64]).replace('\0', 'g'); + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.hashToByteArray(hash)); + assertEquals("invalid hash value", e.getMessage()); + } + + @Test + public void topicToByteArrayAcceptsFullLengthHex() throws JsonRpcInvalidParamsException { + String topic = "0x" + new String(new char[64]).replace('\0', 'a'); + assertEquals(32, JsonRpcApiUtil.topicToByteArray(topic).length); + } + + @Test + public void topicToByteArrayPadsMissingLeadingZero() throws JsonRpcInvalidParamsException { + String stripped = "0x" + new String(new char[63]).replace('\0', 'a'); + byte[] parsed = JsonRpcApiUtil.topicToByteArray(stripped); + assertEquals(32, parsed.length); + assertEquals(0x0a, parsed[0]); + } + + @Test + public void topicToByteArrayRejectsNonHexChars() { + String topic = "0x" + new String(new char[64]).replace('\0', 'g'); + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.topicToByteArray(topic)); + assertEquals("invalid topic: " + topic, e.getMessage()); + } + + @Test + public void topicToByteArrayRejectsWrongLength() { + // 62 chars (two zeros stripped) and 65 chars are both invalid + String tooShort = "0x" + new String(new char[62]).replace('\0', 'a'); + assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.topicToByteArray(tooShort)); + String tooLong = "0x" + new String(new char[65]).replace('\0', 'a'); + assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.topicToByteArray(tooLong)); + assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.topicToByteArray(null)); + } + + @Test + public void parseTxIndexAcceptsHex() throws JsonRpcInvalidParamsException { + assertEquals(0x1a, JsonRpcApiUtil.parseTxIndex("0x1a")); + assertEquals(0, JsonRpcApiUtil.parseTxIndex("0x0")); + // 8 hex digits is the max width; 0x7fffffff is Integer.MAX_VALUE + assertEquals(Integer.MAX_VALUE, JsonRpcApiUtil.parseTxIndex("0x7fffffff")); + } + + @Test + public void parseTxIndexRejectsMissingPrefix() { + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.parseTxIndex("1a")); + assertEquals("invalid index value", e.getMessage()); + } + + @Test + public void parseTxIndexAcceptsLeadingZeros() throws JsonRpcInvalidParamsException { + // leading zeros are tolerated (only length is capped); "0x01"/"0x00" parse normally + assertEquals(1, JsonRpcApiUtil.parseTxIndex("0x01")); + assertEquals(0, JsonRpcApiUtil.parseTxIndex("0x00")); + } + + @Test + public void parseTxIndexRejectsOversized() { + // 9 hex digits exceeds the 8-digit (0x + 8) limit -> rejected before parsing + String tooLong = "0x" + new String(new char[9]).replace('\0', '1'); + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.parseTxIndex(tooLong)); + assertEquals("invalid index value", e.getMessage()); + } + + @Test + public void parseTxIndexRejectsEmptyAndNull() { + JsonRpcInvalidParamsException e1 = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.parseTxIndex("0x")); + assertEquals("invalid index value", e1.getMessage()); + JsonRpcInvalidParamsException e2 = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.parseTxIndex(null)); + assertEquals("invalid index value", e2.getMessage()); + } + + @Test + public void parseTxIndexRejectsMalformedHex() { + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.parseTxIndex("0xGG")); + assertEquals("invalid index value", e.getMessage()); + } + + @Test + public void parseTxIndexParsesNegativeForCallerRangeCheck() throws JsonRpcInvalidParamsException { + // "0x-1" is syntactically accepted and parsed to a negative int; the RPC handler maps + // any out-of-range index (negative or >= tx count) to a null result. + assertEquals(-1, JsonRpcApiUtil.parseTxIndex("0x-1")); + } + + @Test + public void calcFeeLimitNormal() throws JsonRpcInvalidParamsException { + assertEquals(8400L, JsonRpcApiUtil.calcFeeLimit(20L, 420L)); + assertEquals(0L, JsonRpcApiUtil.calcFeeLimit(0L, 420L)); + } + + @Test + public void calcFeeLimitRejectsOverflow() { + // gas * energyFee overflows int64 -> rejected instead of silently wrapping to a bogus feeLimit + JsonRpcInvalidParamsException e = assertThrows(JsonRpcInvalidParamsException.class, + () -> JsonRpcApiUtil.calcFeeLimit(Long.MAX_VALUE, 420L)); + assertEquals("invalid gas: fee limit overflow", e.getMessage()); + } } From c3263dc97d9dbdb0947ed8d84623719bd803e9d0 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Thu, 11 Jun 2026 16:52:14 +0800 Subject: [PATCH 41/57] fix(jsonrpc): post log/block filters for reorg-applied blocks (#6819) --- .../main/java/org/tron/core/db/Manager.java | 16 +++ .../java/org/tron/core/db/ManagerTest.java | 132 ++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index d2aa42dfcea..01da3002611 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1201,6 +1201,8 @@ private void switchFork(BlockCapsule newHead) } } } + // only reached when the whole new branch applied cleanly; a failed switch rethrows above + reApplyLogsFilter(first); } } @@ -2284,6 +2286,20 @@ private void reOrgLogsFilter() { } } + // Post the FULL-stream block and logs filters for each block of the new canonical branch + // (oldest-first). Must be kept in sync with the FULL-filter section of blockTrigger. + // Solidity filters are intentionally not posted here: solidification events for these + // blocks arrive later, when postSolidityFilter runs against the then-canonical chain. + private void reApplyLogsFilter(List newBranch) { + if (CommonParameter.getInstance().isJsonRpcHttpFullNodeEnable()) { + for (KhaosBlock khaosBlock : newBranch) { + BlockCapsule blockCapsule = khaosBlock.getBlk(); + postBlockFilter(blockCapsule, false); + postLogsFilter(blockCapsule, false, false); + } + } + } + private void postBlockFilter(final BlockCapsule blockCapsule, boolean solidified) { BlockFilterCapsule blockFilterCapsule = new BlockFilterCapsule(blockCapsule, solidified); diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index 717d6c4cf64..e342fa43222 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -40,7 +40,11 @@ import org.tron.common.TestConstants; import org.tron.common.crypto.ECKey; import org.tron.common.logsfilter.EventPluginLoader; +import org.tron.common.logsfilter.capsule.BlockFilterCapsule; +import org.tron.common.logsfilter.capsule.FilterTriggerCapsule; +import org.tron.common.logsfilter.capsule.LogsFilterCapsule; import org.tron.common.logsfilter.trigger.ContractLogTrigger; +import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.RuntimeImpl; import org.tron.common.utils.ByteArray; import org.tron.common.utils.Commons; @@ -1540,4 +1544,132 @@ public void adjustBalance(AccountStore accountStore, byte[] accountAddress, long Commons.adjustBalance(accountStore, accountAddress, amount, chainManager.getDynamicPropertiesStore().disableJavaLangMath()); } + + /** + * Drives a real reorg and asserts what Manager posts to the jsonrpc filterCapsuleQueue. + */ + @Test + public void switchForkShouldPostFullNodeFilterForNewBranch() throws Exception { + CommonParameter.getInstance().jsonRpcHttpFullNodeEnable = true; + // filterProcessLoop only starts when isJsonRpcFilterEnabled() held at Manager.init() time; it + // was false then, so filterCapsuleQueue is produce-only here and fully observable. + + // bootstrap a head with a known witness + String key = PublicMethod.getRandomPrivateKey(); + byte[] privateKey = ByteArray.fromHexString(key); + final ECKey ecKey = ECKey.fromPrivate(privateKey); + byte[] address = ecKey.getAddress(); + ByteString addressByte = ByteString.copyFrom(address); + chainManager.getAccountStore().put(addressByte.toByteArray(), + new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build())); + WitnessCapsule witnessCapsule = new WitnessCapsule(addressByte); + chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>()); + chainManager.addWitness(addressByte); + chainManager.getWitnessStore().put(address, witnessCapsule); + Block block = blockGenerate.getSignedBlock( + witnessCapsule.getAddress(), 1533529947843L, privateKey); + dbManager.pushBlock(new BlockCapsule(block)); + + Map keys = addTestWitnessAndAccount(); + keys.put(addressByte, key); + + // fund an owner; transfers go owner -> witness 'address' (an existing account) + ECKey ownerKey = new ECKey(Utils.getRandom()); + byte[] owner = ownerKey.getAddress(); + AccountCapsule ownerAccount = new AccountCapsule( + Protocol.Account.newBuilder().setAddress(ByteString.copyFrom(owner)).build()); + ownerAccount.setBalance(1_000_000_000L); + chainManager.getAccountStore().put(owner, ownerAccount); + + long t = 1533529947843L; + long base = chainManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber(); + + // common ancestor P (empty) — fork point and tapos reference + BlockCapsule p = createTestBlockCapsule(t + 3000, base + 1, + chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), keys); + dbManager.pushBlock(p); + + long expiration = t + 1_000_000L; + BlockingQueue queue = + ReflectUtils.getFieldValue(dbManager, "filterCapsuleQueue"); + queue.clear(); + + // old branch: A carries a transfer; applied via the normal extend path + BlockCapsule a = blockWithTransfer(t + 6000, base + 2, p.getBlockId().getByteString(), keys, + transfer(owner, address, 1L, p, expiration)); + dbManager.pushBlock(a); + Assert.assertEquals("control: head should be A after normal extend", + a.getBlockId(), chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash()); + Assert.assertTrue("control: normal-path block A's logs must reach FULL stream (added)", + hasLogsFilterCapsule(queue, a, false)); + Assert.assertTrue("control: normal-path block A must reach the FULL block-filter stream", + hasBlockFilterCapsule(queue, a)); + + // heavier competing branch P -> B1 -> B2, each carrying a transfer, to force switchFork + BlockCapsule b1 = blockWithTransfer(t + 6001, base + 2, p.getBlockId().getByteString(), keys, + transfer(owner, address, 2L, p, expiration)); + dbManager.pushBlock(b1); // num <= head -> kept in khaosDb, no switch yet + BlockCapsule b2 = blockWithTransfer(t + 9000, base + 3, b1.getBlockId().getByteString(), keys, + transfer(owner, address, 3L, p, expiration)); + dbManager.pushBlock(b2); // num > head & parent != head -> triggers switchFork + + Assert.assertEquals("reorg must switch the canonical head to the competing branch (B2)", + b2.getBlockId(), chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash()); + + // reorg withdraws the orphaned old-branch logs (removed=true) + Assert.assertTrue("reorg: orphaned block A's logs must be withdrawn (removed=true)", + hasLogsFilterCapsule(queue, a, true)); + // the fix: new canonical blocks' logs and block filters are delivered + Assert.assertTrue("reorg: new canonical block B1's logs must reach FULL stream (added)", + hasLogsFilterCapsule(queue, b1, false)); + Assert.assertTrue("reorg: new canonical block B2's logs must reach FULL stream (added)", + hasLogsFilterCapsule(queue, b2, false)); + Assert.assertTrue("reorg: new canonical block B1 must reach the FULL block-filter stream", + hasBlockFilterCapsule(queue, b1)); + Assert.assertTrue("reorg: new canonical block B2 must reach the FULL block-filter stream", + hasBlockFilterCapsule(queue, b2)); + } + + private TransactionCapsule transfer(byte[] owner, byte[] to, long amount, + BlockCapsule refBlock, long expiration) { + TransferContract contract = TransferContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(owner)) + .setToAddress(ByteString.copyFrom(to)) + .setAmount(amount).build(); + TransactionCapsule tx = new TransactionCapsule(contract, ContractType.TransferContract); + tx.setReference(refBlock.getNum(), refBlock.getBlockId().getBytes()); + tx.setExpiration(expiration); + return tx; + } + + private BlockCapsule blockWithTransfer(long time, long number, ByteString parentHash, + Map keys, TransactionCapsule tx) { + ByteString witnessAddress = dposSlot.getScheduledWitness(dposSlot.getSlot(time)); + BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(parentHash), time, + witnessAddress); + blockCapsule.addTransaction(tx); + blockCapsule.generatedByMyself = true; + blockCapsule.setMerkleRoot(); + blockCapsule.sign(ByteArray.fromHexString(keys.get(witnessAddress))); + return blockCapsule; + } + + private boolean hasLogsFilterCapsule(BlockingQueue queue, BlockCapsule b, + boolean removed) { + String blockHash = b.getBlockId().toString(); + return queue.stream() + .filter(c -> c instanceof LogsFilterCapsule) + .map(c -> (LogsFilterCapsule) c) + .anyMatch(c -> !c.isSolidified() && c.isRemoved() == removed + && blockHash.equals(c.getBlockHash())); + } + + private boolean hasBlockFilterCapsule(BlockingQueue queue, + BlockCapsule b) { + String blockHash = b.getBlockId().toString(); + return queue.stream() + .filter(c -> c instanceof BlockFilterCapsule) + .map(c -> (BlockFilterCapsule) c) + .anyMatch(c -> !c.isSolidified() && blockHash.equals(c.getBlockHash())); + } } From 28f4e416edf881598b04a0ba5270f41abfad08b7 Mon Sep 17 00:00:00 2001 From: Asuka Date: Fri, 12 Jun 2026 12:03:55 +0800 Subject: [PATCH 42/57] feat(vm): check CREATE2 max depth under Osaka (#6807) * feat(vm): check CREATE2 max depth under Osaka * feat(vm): clear residual logs on exception escape paths under Osaka Mirror the in-try failure path: failed txs must not leak event logs into TransactionInfo/Bloom. Gated on Osaka to preserve replay. --- .../org/tron/core/actuator/VMActuator.java | 10 +++ .../org/tron/core/vm/program/Program.java | 3 +- .../common/runtime/VMActuatorMockTest.java | 88 +++++++++++++++++++ .../common/runtime/vm/OperationsTest.java | 66 ++++++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 framework/src/test/java/org/tron/common/runtime/VMActuatorMockTest.java diff --git a/actuator/src/main/java/org/tron/core/actuator/VMActuator.java b/actuator/src/main/java/org/tron/core/actuator/VMActuator.java index 1b0e8a6637f..f604013325e 100644 --- a/actuator/src/main/java/org/tron/core/actuator/VMActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/VMActuator.java @@ -267,6 +267,7 @@ public void execute(Object object) throws ContractExeException { result = program.getResult(); result.setException(e); result.rejectInternalTransactions(); + clearExceptionResult(result); result.setRuntimeError(result.getException().getMessage()); logger.info("JVMStackOverFlowException: {}", result.getException().getMessage()); } catch (OutOfTimeException e) { @@ -274,6 +275,7 @@ public void execute(Object object) throws ContractExeException { result = program.getResult(); result.setException(e); result.rejectInternalTransactions(); + clearExceptionResult(result); result.setRuntimeError(result.getException().getMessage()); logger.info("timeout: {}", result.getException().getMessage()); } catch (Throwable e) { @@ -282,6 +284,7 @@ public void execute(Object object) throws ContractExeException { } result = program.getResult(); result.rejectInternalTransactions(); + clearExceptionResult(result); if (Objects.isNull(result.getException())) { logger.error(e.getMessage(), e); result.setException(new RuntimeException("Unknown Throwable")); @@ -310,6 +313,13 @@ public void execute(Object object) throws ContractExeException { } + private void clearExceptionResult(ProgramResult result) { + if (VMConfig.allowTvmOsaka()) { + result.getDeleteAccounts().clear(); + result.getLogInfoList().clear(); + } + } + private void create() throws ContractValidateException { if (!rootRepository.getDynamicPropertiesStore().supportVM()) { diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 41822df2391..590859a9fef 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1621,7 +1621,8 @@ public void createContract2(DataWord value, DataWord memStart, DataWord memSize, } byte[] senderAddress; - if (VMConfig.allowTvmCompatibleEvm() && getCallDeep() == MAX_DEPTH) { + if ((VMConfig.allowTvmCompatibleEvm() || VMConfig.allowTvmOsaka()) + && getCallDeep() == MAX_DEPTH) { stackPushZero(); return; } diff --git a/framework/src/test/java/org/tron/common/runtime/VMActuatorMockTest.java b/framework/src/test/java/org/tron/common/runtime/VMActuatorMockTest.java new file mode 100644 index 00000000000..ab147f57a79 --- /dev/null +++ b/framework/src/test/java/org/tron/common/runtime/VMActuatorMockTest.java @@ -0,0 +1,88 @@ +package org.tron.common.runtime; + +import static org.mockito.ArgumentMatchers.any; + +import java.lang.reflect.Field; +import java.util.Collections; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.tron.common.runtime.vm.DataWord; +import org.tron.common.runtime.vm.LogInfo; +import org.tron.core.actuator.VMActuator; +import org.tron.core.db.TransactionContext; +import org.tron.core.vm.OperationRegistry; +import org.tron.core.vm.VM; +import org.tron.core.vm.config.VMConfig; +import org.tron.core.vm.program.Program; + +public class VMActuatorMockTest { + + @BeforeClass + public static void init() { + // warm up the registry so VM.play(..., OperationRegistry.getTable()) arg eval is safe + OperationRegistry.init(); + } + + private void runCatchPathTest(Throwable thrownByVm, boolean osakaOn, int expectedSize) + throws Exception { + boolean prevOsaka = VMConfig.allowTvmOsaka(); + VMConfig.initAllowTvmOsaka(osakaOn ? 1 : 0); + try (MockedStatic vmMock = Mockito.mockStatic(VM.class)) { + Program program = Mockito.mock(Program.class); + ProgramResult result = new ProgramResult(); + result.addLogInfo(new LogInfo(new byte[20], Collections.emptyList(), new byte[0])); + result.addDeleteAccount(new DataWord(1)); + Mockito.when(program.getResult()).thenReturn(result); + + vmMock.when(() -> VM.play(any(), any())).thenThrow(thrownByVm); + + VMActuator actuator = new VMActuator(false); + Field f = VMActuator.class.getDeclaredField("program"); + f.setAccessible(true); + f.set(actuator, program); + + TransactionContext context = Mockito.mock(TransactionContext.class); + Mockito.when(context.getProgramResult()).thenReturn(new ProgramResult()); + + actuator.execute(context); + + Assert.assertEquals(expectedSize, result.getLogInfoList().size()); + Assert.assertEquals(expectedSize, result.getDeleteAccounts().size()); + } finally { + VMConfig.initAllowTvmOsaka(prevOsaka ? 1 : 0); + } + } + + @Test + public void osakaClearsLogOnOutOfTime() throws Exception { + runCatchPathTest(new Program.OutOfTimeException("timeout"), true, 0); + } + + @Test + public void osakaClearsLogOnJvmStackOverflow() throws Exception { + runCatchPathTest(new Program.JVMStackOverFlowException(), true, 0); + } + + @Test + public void osakaClearsLogOnThrowable() throws Exception { + runCatchPathTest(new RuntimeException("boom"), true, 0); + } + + @Test + public void preOsakaKeepsLogOnOutOfTime() throws Exception { + runCatchPathTest(new Program.OutOfTimeException("timeout"), false, 1); + } + + @Test + public void preOsakaKeepsLogOnJvmStackOverflow() throws Exception { + runCatchPathTest(new Program.JVMStackOverFlowException(), false, 1); + } + + @Test + public void preOsakaKeepsLogOnThrowable() throws Exception { + runCatchPathTest(new RuntimeException("boom"), false, 1); + } +} diff --git a/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java b/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java index 651248bd9e4..a1627f4f2e2 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java @@ -788,6 +788,72 @@ Op.CALL, new DataWord(10000), VMConfig.initAllowTvmSelfdestructRestriction(0); } + @Test + public void testCreate2MaxDepthWithOsakaOnly() throws ContractValidateException { + boolean allowTvmCompatibleEvm = VMConfig.allowTvmCompatibleEvm(); + boolean allowTvmOsaka = VMConfig.allowTvmOsaka(); + VMConfig.initAllowTvmCompatibleEvm(0); + VMConfig.initAllowTvmOsaka(1); + try { + invoke = new ProgramInvokeMockImpl() { + @Override + public int getCallDeep() { + return 64; + } + }; + Protocol.Transaction trx = Protocol.Transaction.getDefaultInstance(); + InternalTransaction interTrx = + new InternalTransaction(trx, InternalTransaction.TrxType.TRX_UNKNOWN_TYPE); + program = new Program(new byte[0], new byte[0], invoke, interTrx); + program.setRootTransactionId(new byte[32]); + + program.createContract2(DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO()); + + Assert.assertEquals(DataWord.ZERO(), program.getStack().pop()); + Assert.assertTrue(program.getResult().getInternalTransactions().isEmpty()); + } finally { + VMConfig.initAllowTvmCompatibleEvm(allowTvmCompatibleEvm ? 1 : 0); + VMConfig.initAllowTvmOsaka(allowTvmOsaka ? 1 : 0); + } + } + + @Test + public void testCreate2MaxDepthWithNeitherFlag() throws ContractValidateException { + boolean allowTvmCompatibleEvm = VMConfig.allowTvmCompatibleEvm(); + boolean allowTvmOsaka = VMConfig.allowTvmOsaka(); + VMConfig.initAllowTvmCompatibleEvm(0); + VMConfig.initAllowTvmOsaka(0); + try { + byte[] contractAddr = Hex.decode("41471fd3ad3e9eeadeec4608b92d16ce6b500704cc"); + invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], contractAddr) { + @Override + public int getCallDeep() { + return 64; + } + + @Override + public boolean byTestingSuite() { + return true; + } + }; + program = new Program(null, null, invoke, + new InternalTransaction(Protocol.Transaction.getDefaultInstance(), + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); + program.setRootTransactionId(new byte[32]); + + program.createContract2(DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO()); + + // With neither flag enabled the MAX_DEPTH short-circuit must not fire: CREATE2 + // proceeds, records an internal transaction and pushes the new contract + // address (not 0), unlike the Osaka/CompatibleEvm guarded path above. + Assert.assertFalse(program.getResult().getInternalTransactions().isEmpty()); + Assert.assertFalse(program.getStack().pop().isZero()); + } finally { + VMConfig.initAllowTvmCompatibleEvm(allowTvmCompatibleEvm ? 1 : 0); + VMConfig.initAllowTvmOsaka(allowTvmOsaka ? 1 : 0); + } + } + // TIP-854 outer-frame containment: a CALL to validateMultiSign or // batchValidateSign with malformed calldata must (a) push 0 onto the outer // stack, (b) leave the outer frame free of any propagated exception, and From 6a67f29e39048206e18af7ec75f7ed5f876ec044 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Fri, 12 Jun 2026 17:18:36 +0800 Subject: [PATCH 43/57] fix(config): update comment of reference.conf (#6834) --- common/src/main/resources/reference.conf | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/main/resources/reference.conf b/common/src/main/resources/reference.conf index f1e4907274f..25fc4832e55 100644 --- a/common/src/main/resources/reference.conf +++ b/common/src/main/resources/reference.conf @@ -196,13 +196,13 @@ node { # Max in-flight (requested but not yet processed) blocks during sync. Range: [50, 2000]. maxPendingBlockSize = 500 - # Number of validate sign threads, default availableProcessors # Number of validate sign threads, 0 = auto (availableProcessors) validateSignThreadNum = 0 maxConnections = 30 # Maximum peer connections. minConnections = 8 # Minimum peer connections to maintain. minActiveConnections = 3 # Minimum active peer connections. + # Legacy alias `maxActiveNodesWithSameIp` is still accepted but deprecated maxConnectionsWithSameIp = 2 # Maximum peer connections per IP. maxHttpConnectNumber = 50 # Maximum HTTP connections. minParticipationRate = 0 # Minimum SR participation rate. @@ -233,9 +233,9 @@ node { inactiveThreshold = 600 // seconds maxFastForwardNum = 4 # Number of SRs after the block-producing SR that a fast-forward node forwards blocks to. - # Legacy alias `maxActiveNodesWithSameIp` is still accepted from user config - # (see NodeConfig alias-fallback) but is intentionally NOT defaulted here — - # shipping it in reference.conf would always mask the modern `maxConnectionsWithSameIp`. + # Deprecated. Enables legacy MetricsUtil counters/histograms and registers + # the monitor gRPC service (MonitorApi) on the RPC server. + # This is independent of the Prometheus exporter configured under node.metrics.prometheus. metricsEnable = false # P2P protocol version settings. @@ -453,7 +453,7 @@ node { ## Rate limiter config rate.limiter = { # Each HTTP servlet and gRPC method can have its own rate-limit strategy. - # Three blocking strategies are available: + # Three API rate-limit strategies are available: # GlobalPreemptibleAdapter – limits maximum concurrent requests globally. # paramString = "permit=N" (N = max concurrent calls) # QpsRateLimiterAdapter – limits average QPS across all callers. From 31af29f6bb745abbe4918dbbfbbd27a5e0ab4f94 Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Mon, 15 Jun 2026 12:22:37 +0800 Subject: [PATCH 44/57] feat(event): support rollback (removed) for block and transaction log triggers (#6833) On a chain reorg, BlockLogTrigger/TransactionLogTrigger subscribers previously never learned that an orphaned block's events were rolled back. Add the same "removed" semantics already used by ContractTrigger. - add a `removed` field to BlockLogTrigger/TransactionLogTrigger and the matching setRemoved() on their capsules - Manager (event version 0): refactor postBlockTrigger to emit a single real-time block (with removed), move the solidified-mode batch into postSolidityTrigger, add reOrgBlockTrigger to re-emit erased blocks with removed=true, rename reApplyLogsFilter to reApplyBlockEvents and re-emit the re-applied fork branch's block/tx triggers (forward), thread removed through processTransactionTrigger/postTransactionTrigger - RealtimeEventService (event version 1): post block/tx triggers synchronously to the plugin with the removed flag instead of the async triggerCapsuleQueue, avoiding overwrite of the shared cached capsule; drop the unused manager field - tests: capsule removed passthrough, version-0 emit (testReOrgBlockTriggerRemoved), rewritten RealtimeEventServiceTest, updated ManagerTest stub --- .../logsfilter/trigger/BlockLogTrigger.java | 8 + .../trigger/TransactionLogTrigger.java | 6 + .../capsule/BlockLogTriggerCapsule.java | 4 + .../capsule/TransactionLogTriggerCapsule.java | 4 + .../main/java/org/tron/core/db/Manager.java | 120 ++++++++++----- .../services/event/RealtimeEventService.java | 24 +-- .../TransactionLogTriggerCapsuleTest.java | 18 +++ .../capsule/BlockLogTriggerCapsuleTest.java | 8 + .../java/org/tron/core/db/ManagerTest.java | 143 +++++++++++++++++- .../core/event/RealtimeEventServiceTest.java | 44 +++--- 10 files changed, 305 insertions(+), 74 deletions(-) diff --git a/common/src/main/java/org/tron/common/logsfilter/trigger/BlockLogTrigger.java b/common/src/main/java/org/tron/common/logsfilter/trigger/BlockLogTrigger.java index b878597045d..8b27bb6ff4c 100644 --- a/common/src/main/java/org/tron/common/logsfilter/trigger/BlockLogTrigger.java +++ b/common/src/main/java/org/tron/common/logsfilter/trigger/BlockLogTrigger.java @@ -27,6 +27,12 @@ public class BlockLogTrigger extends Trigger { @Setter private List transactionList = new ArrayList<>(); + // true when this block is being rolled back due to a chain reorg (fork switch); + // mirrors the Ethereum log "removed" semantics already used by ContractTrigger. + @Getter + @Setter + private boolean removed; + public BlockLogTrigger() { setTriggerName(Trigger.BLOCK_TRIGGER_NAME); } @@ -44,6 +50,8 @@ public String toString() { .append(transactionSize) .append(", latestSolidifiedBlockNumber: ") .append(latestSolidifiedBlockNumber) + .append(", removed: ") + .append(removed) .append(", transactionList: ") .append(transactionList).toString(); } diff --git a/common/src/main/java/org/tron/common/logsfilter/trigger/TransactionLogTrigger.java b/common/src/main/java/org/tron/common/logsfilter/trigger/TransactionLogTrigger.java index a4fb1fddb79..7ccc17f9a1d 100644 --- a/common/src/main/java/org/tron/common/logsfilter/trigger/TransactionLogTrigger.java +++ b/common/src/main/java/org/tron/common/logsfilter/trigger/TransactionLogTrigger.java @@ -97,6 +97,12 @@ public class TransactionLogTrigger extends Trigger { @Setter private Map extMap; + // true when this transaction is being rolled back due to a chain reorg (fork switch); + // mirrors the Ethereum log "removed" semantics already used by ContractTrigger. + @Getter + @Setter + private boolean removed; + public TransactionLogTrigger() { setTriggerName(Trigger.TRANSACTION_TRIGGER_NAME); } diff --git a/framework/src/main/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsule.java b/framework/src/main/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsule.java index b714134ff60..de040d33d44 100644 --- a/framework/src/main/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsule.java +++ b/framework/src/main/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsule.java @@ -27,6 +27,10 @@ public void setLatestSolidifiedBlockNumber(long latestSolidifiedBlockNumber) { blockLogTrigger.setLatestSolidifiedBlockNumber(latestSolidifiedBlockNumber); } + public void setRemoved(boolean removed) { + blockLogTrigger.setRemoved(removed); + } + @Override public void processTrigger() { EventPluginLoader.getInstance().postBlockTrigger(blockLogTrigger); diff --git a/framework/src/main/java/org/tron/common/logsfilter/capsule/TransactionLogTriggerCapsule.java b/framework/src/main/java/org/tron/common/logsfilter/capsule/TransactionLogTriggerCapsule.java index 958b3c0fd78..cf1737d9d22 100644 --- a/framework/src/main/java/org/tron/common/logsfilter/capsule/TransactionLogTriggerCapsule.java +++ b/framework/src/main/java/org/tron/common/logsfilter/capsule/TransactionLogTriggerCapsule.java @@ -377,6 +377,10 @@ public void setLatestSolidifiedBlockNumber(long latestSolidifiedBlockNumber) { transactionLogTrigger.setLatestSolidifiedBlockNumber(latestSolidifiedBlockNumber); } + public void setRemoved(boolean removed) { + transactionLogTrigger.setRemoved(removed); + } + private List getInternalTransactionList( List internalTransactionList) { List pojoList = new ArrayList<>(); diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index 01da3002611..eab76db9e3f 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1127,6 +1127,7 @@ private void switchFork(BlockCapsule newHead) .equals(binaryTree.getValue().peekLast().getParentHash())) { if (EventPluginLoader.getInstance().getVersion() == 0) { reOrgContractTrigger(); + reOrgBlockTrigger(); } reOrgLogsFilter(); eraseBlock(); @@ -1202,7 +1203,7 @@ private void switchFork(BlockCapsule newHead) } } // only reached when the whole new branch applied cleanly; a failed switch rethrows above - reApplyLogsFilter(first); + reApplyBlockEvents(first); } } @@ -1435,9 +1436,10 @@ void blockTrigger(final BlockCapsule block, long oldSolid, long newSolid) { return; } - // if event subscribe is enabled, post block trigger to queue - postBlockTrigger(block); + // if event subscribe is enabled, post block trigger to queue (real-time, not removed) + postBlockTrigger(block, false); // if event subscribe is enabled, post solidity trigger to queue + // (also emits solidified-mode block/transaction triggers) postSolidityTrigger(newSolid); } catch (Exception e) { logger.error("Block trigger failed. head: {}, oldSolid: {}, newSolid: {}", @@ -2205,6 +2207,27 @@ private void postSolidityFilter(final long oldSolidNum, final long latestSolidif } private void postSolidityTrigger(final long latestSolidifiedBlockNumber) { + // solidified-mode block trigger: emit the newly-solidified blocks (never removed, + // since solidified blocks cannot be reorged). + if (eventPluginLoaded && EventPluginLoader.getInstance().isBlockLogTriggerEnable() + && EventPluginLoader.getInstance().isBlockLogTriggerSolidified()) { + for (BlockCapsule capsule : getContinuousBlockCapsule(latestSolidifiedBlockNumber)) { + BlockLogTriggerCapsule blockLogTriggerCapsule = new BlockLogTriggerCapsule(capsule); + blockLogTriggerCapsule.setLatestSolidifiedBlockNumber(latestSolidifiedBlockNumber); + if (!triggerCapsuleQueue.offer(blockLogTriggerCapsule)) { + logger.info("Too many triggers, block trigger lost: {}.", capsule.getBlockId()); + } + } + } + + // solidified-mode transaction trigger: emit transactions of the newly-solidified blocks. + if (eventPluginLoaded && EventPluginLoader.getInstance().isTransactionLogTriggerEnable() + && EventPluginLoader.getInstance().isTransactionLogTriggerSolidified()) { + for (BlockCapsule capsule : getContinuousBlockCapsule(latestSolidifiedBlockNumber)) { + processTransactionTrigger(capsule, false); + } + } + if (eventPluginLoaded && EventPluginLoader.getInstance().isSolidityLogTriggerEnable()) { for (Long i : Args.getSolidityContractLogTriggerMap().keySet()) { postSolidityLogContractTrigger(i, latestSolidifiedBlockNumber); @@ -2233,7 +2256,7 @@ private void postSolidityTrigger(final long latestSolidifiedBlockNumber) { lastUsedSolidityNum = latestSolidifiedBlockNumber; } - private void processTransactionTrigger(BlockCapsule newBlock) { + private void processTransactionTrigger(BlockCapsule newBlock, boolean removed) { List transactionCapsuleList = newBlock.getTransactions(); // need to set eth compatible data from transactionInfoList @@ -2252,7 +2275,7 @@ private void processTransactionTrigger(BlockCapsule newBlock) { transactionCapsule.setBlockNum(newBlock.getNum()); cumulativeEnergyUsed += postTransactionTrigger(transactionCapsule, newBlock, i, - cumulativeEnergyUsed, cumulativeLogCount, transactionInfo, energyUnitPrice); + cumulativeEnergyUsed, cumulativeLogCount, transactionInfo, energyUnitPrice, removed); cumulativeLogCount += transactionInfo.getLogCount(); } @@ -2261,12 +2284,12 @@ private void processTransactionTrigger(BlockCapsule newBlock) { newBlock.getNum(), "the sizes of transactionInfoList and transactionCapsuleList are not equal"); for (TransactionCapsule e : newBlock.getTransactions()) { - postTransactionTrigger(e, newBlock); + postTransactionTrigger(e, newBlock, removed); } } } else { for (TransactionCapsule e : newBlock.getTransactions()) { - postTransactionTrigger(e, newBlock); + postTransactionTrigger(e, newBlock, removed); } } } @@ -2290,7 +2313,12 @@ private void reOrgLogsFilter() { // (oldest-first). Must be kept in sync with the FULL-filter section of blockTrigger. // Solidity filters are intentionally not posted here: solidification events for these // blocks arrive later, when postSolidityFilter runs against the then-canonical chain. - private void reApplyLogsFilter(List newBranch) { + // Re-emit the per-block subscription events for a newly-applied fork branch after a chain + // reorg: JSON-RPC block/logs filters and event-subscribe block/transaction triggers. The + // fork-switch path returns before blockTrigger() runs, so without this these forward events + // would be lost for the re-applied blocks (contract triggers are already re-emitted during + // applyBlock). All emitted as forward (removed=false): these blocks are now canonical. + private void reApplyBlockEvents(List newBranch) { if (CommonParameter.getInstance().isJsonRpcHttpFullNodeEnable()) { for (KhaosBlock khaosBlock : newBranch) { BlockCapsule blockCapsule = khaosBlock.getBlk(); @@ -2298,6 +2326,12 @@ private void reApplyLogsFilter(List newBranch) { postLogsFilter(blockCapsule, false, false); } } + + if (EventPluginLoader.getInstance().getVersion() == 0) { + for (KhaosBlock khaosBlock : newBranch) { + postBlockTrigger(khaosBlock.getBlk(), false); + } + } } private void postBlockFilter(final BlockCapsule blockCapsule, boolean solidified) { @@ -2324,39 +2358,26 @@ private void postLogsFilter(final BlockCapsule blockCapsule, boolean solidified, } } - void postBlockTrigger(final BlockCapsule blockCapsule) { - // process block trigger + // Real-time block/transaction triggers for a single block. The solidified-mode batch is + // handled in postSolidityTrigger (driven by solidification advancement), so here we only + // emit for triggers configured as non-solidified. {@code removed=true} re-emits the same + // trigger when the block is rolled back by a chain reorg (see reOrgBlockTrigger). + void postBlockTrigger(final BlockCapsule blockCapsule, boolean removed) { long solidityBlkNum = getDynamicPropertiesStore().getLatestSolidifiedBlockNum(); - if (eventPluginLoaded && EventPluginLoader.getInstance().isBlockLogTriggerEnable()) { - List capsuleList = new ArrayList<>(); - if (EventPluginLoader.getInstance().isBlockLogTriggerSolidified()) { - capsuleList = getContinuousBlockCapsule(solidityBlkNum); - } else { - capsuleList.add(blockCapsule); - } - for (BlockCapsule capsule : capsuleList) { - BlockLogTriggerCapsule blockLogTriggerCapsule = new BlockLogTriggerCapsule(capsule); - blockLogTriggerCapsule.setLatestSolidifiedBlockNumber(solidityBlkNum); - if (!triggerCapsuleQueue.offer(blockLogTriggerCapsule)) { - logger.info("Too many triggers, block trigger lost: {}.", capsule.getBlockId()); - } + if (eventPluginLoaded && EventPluginLoader.getInstance().isBlockLogTriggerEnable() + && !EventPluginLoader.getInstance().isBlockLogTriggerSolidified()) { + BlockLogTriggerCapsule blockLogTriggerCapsule = new BlockLogTriggerCapsule(blockCapsule); + blockLogTriggerCapsule.setLatestSolidifiedBlockNumber(solidityBlkNum); + blockLogTriggerCapsule.setRemoved(removed); + if (!triggerCapsuleQueue.offer(blockLogTriggerCapsule)) { + logger.info("Too many triggers, block trigger lost: {}.", blockCapsule.getBlockId()); } } - // process transaction trigger - if (eventPluginLoaded && EventPluginLoader.getInstance().isTransactionLogTriggerEnable()) { - List capsuleList = new ArrayList<>(); - if (EventPluginLoader.getInstance().isTransactionLogTriggerSolidified()) { - capsuleList = getContinuousBlockCapsule(solidityBlkNum); - } else { - // need to reset block - capsuleList.add(blockCapsule); - } - - for (BlockCapsule capsule : capsuleList) { - processTransactionTrigger(capsule); - } + if (eventPluginLoaded && EventPluginLoader.getInstance().isTransactionLogTriggerEnable() + && !EventPluginLoader.getInstance().isTransactionLogTriggerSolidified()) { + processTransactionTrigger(blockCapsule, removed); } } @@ -2382,11 +2403,13 @@ private List getContinuousBlockCapsule(long solidityBlkNum) { // cumulativeEnergyUsed is the total of energy used before the current transaction private long postTransactionTrigger(final TransactionCapsule trxCap, final BlockCapsule blockCap, int index, long preCumulativeEnergyUsed, - long cumulativeLogCount, final TransactionInfo transactionInfo, long energyUnitPrice) { + long cumulativeLogCount, final TransactionInfo transactionInfo, long energyUnitPrice, + boolean removed) { TransactionLogTriggerCapsule trx = new TransactionLogTriggerCapsule(trxCap, blockCap, index, preCumulativeEnergyUsed, cumulativeLogCount, transactionInfo, energyUnitPrice); trx.setLatestSolidifiedBlockNumber(getDynamicPropertiesStore() .getLatestSolidifiedBlockNum()); + trx.setRemoved(removed); if (!triggerCapsuleQueue.offer(trx)) { logger.info("Too many triggers, transaction trigger lost: {}.", trxCap.getTransactionId()); } @@ -2396,10 +2419,11 @@ private long postTransactionTrigger(final TransactionCapsule trxCap, private void postTransactionTrigger(final TransactionCapsule trxCap, - final BlockCapsule blockCap) { + final BlockCapsule blockCap, boolean removed) { TransactionLogTriggerCapsule trx = new TransactionLogTriggerCapsule(trxCap, blockCap); trx.setLatestSolidifiedBlockNumber(getDynamicPropertiesStore() .getLatestSolidifiedBlockNum()); + trx.setRemoved(removed); if (!triggerCapsuleQueue.offer(trx)) { logger.info("Too many triggers, transaction trigger lost: {}.", trxCap.getTransactionId()); } @@ -2424,6 +2448,26 @@ private void reOrgContractTrigger() { clearSolidityContractTriggerCache(getHeadBlockNum()); } + // On a chain reorg, re-emit the block/transaction triggers of the block being erased with + // removed=true, so subscribers can roll back. Only real-time (non-solidified) triggers were + // ever emitted for this block, so postBlockTrigger(.., true) naturally no-ops in solidified + // mode. Called in the erase loop before eraseBlock(), so the old head is still current head. + private void reOrgBlockTrigger() { + if (eventPluginLoaded + && (EventPluginLoader.getInstance().isBlockLogTriggerEnable() + || EventPluginLoader.getInstance().isTransactionLogTriggerEnable())) { + logger.info("Switch fork occurred, post reOrgBlockTrigger."); + try { + BlockCapsule oldHeadBlock = chainBaseManager.getBlockById( + getDynamicPropertiesStore().getLatestBlockHeaderHash()); + postBlockTrigger(oldHeadBlock, true); + } catch (BadItemException | ItemNotFoundException e) { + logger.error("Block header hash does not exist or is bad: {}.", + getDynamicPropertiesStore().getLatestBlockHeaderHash()); + } + } + } + private void clearSolidityContractTriggerCache(long blockNum) { if (eventPluginLoaded && (EventPluginLoader.getInstance().isSolidityEventTriggerEnable() diff --git a/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java b/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java index 5aee55b1c13..cef16cd81c1 100644 --- a/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java +++ b/framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java @@ -12,7 +12,6 @@ import org.tron.common.es.ExecutorServiceManager; import org.tron.common.logsfilter.EventPluginLoader; import org.tron.common.logsfilter.trigger.Trigger; -import org.tron.core.db.Manager; import org.tron.core.services.event.bo.BlockEvent; import org.tron.core.services.event.bo.Event; @@ -25,9 +24,6 @@ public class RealtimeEventService { @Getter private static Object contractLock = new Object(); - @Autowired - private Manager manager; - @Autowired private SolidEventService solidEventService; @@ -77,25 +73,31 @@ public synchronized void work() { public void flush(BlockEvent blockEvent, boolean isRemove) { logger.info("Flush realtime event {}", blockEvent.getBlockId().getString()); + // Post block/transaction triggers synchronously to the plugin (processTrigger -> + // EventPluginLoader serializes immediately) instead of the async triggerCapsuleQueue: the + // capsule is a shared cached object whose removed flag is set per-flush, so an async consumer + // could read it after a later flush overwrote it. This mirrors how contract triggers below + // are posted directly. isRemove=true re-emits the block/transaction as rolled back on a reorg. if (instance.isBlockLogTriggerEnable() - && !instance.isBlockLogTriggerSolidified() - && !isRemove) { + && !instance.isBlockLogTriggerSolidified()) { if (blockEvent.getBlockLogTriggerCapsule() == null) { logger.warn("BlockLogTriggerCapsule is null. {}", blockEvent.getBlockId().getString()); } else { - manager.getTriggerCapsuleQueue().offer(blockEvent.getBlockLogTriggerCapsule()); + blockEvent.getBlockLogTriggerCapsule().setRemoved(isRemove); + blockEvent.getBlockLogTriggerCapsule().processTrigger(); } } if (instance.isTransactionLogTriggerEnable() - && !instance.isTransactionLogTriggerSolidified() - && !isRemove) { + && !instance.isTransactionLogTriggerSolidified()) { if (blockEvent.getTransactionLogTriggerCapsules() == null) { logger.warn("TransactionLogTriggerCapsules is null. {}", blockEvent.getBlockId().getString()); } else { - blockEvent.getTransactionLogTriggerCapsules().forEach(v -> - manager.getTriggerCapsuleQueue().offer(v)); + blockEvent.getTransactionLogTriggerCapsules().forEach(v -> { + v.setRemoved(isRemove); + v.processTrigger(); + }); } } diff --git a/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java b/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java index ce0f63ef7a4..1d559adb6d0 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java @@ -35,6 +35,24 @@ public void setup() { System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString()); } + @Test + public void testSetRemoved() { + BalanceContract.TransferContract.Builder builder = + BalanceContract.TransferContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS))) + .setAmount(1000L); + transactionCapsule = new TransactionCapsule(builder.build(), + Protocol.Transaction.Contract.ContractType.TransferContract); + TransactionLogTriggerCapsule triggerCapsule = + new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule); + + // default is false (forward emit); reorg rollback sets it to true + Assert.assertFalse(triggerCapsule.getTransactionLogTrigger().isRemoved()); + triggerCapsule.setRemoved(true); + Assert.assertTrue(triggerCapsule.getTransactionLogTrigger().isRemoved()); + } + @Test public void testConstructorWithUnfreezeBalanceTrxCapsule() { BalanceContract.UnfreezeBalanceContract.Builder builder2 = diff --git a/framework/src/test/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsuleTest.java b/framework/src/test/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsuleTest.java index f77869b8650..60e51144a40 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsuleTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsuleTest.java @@ -32,4 +32,12 @@ public void testSetLatestSolidifiedBlockNumber() { Assert.assertEquals(100, blockLogTriggerCapsule.getBlockLogTrigger().getLatestSolidifiedBlockNumber()); } + + @Test + public void testSetRemoved() { + // default is false (forward emit); reorg rollback sets it to true + Assert.assertFalse(blockLogTriggerCapsule.getBlockLogTrigger().isRemoved()); + blockLogTriggerCapsule.setRemoved(true); + Assert.assertTrue(blockLogTriggerCapsule.getBlockLogTrigger().isRemoved()); + } } diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index e342fa43222..b31af7557fe 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -41,8 +42,11 @@ import org.tron.common.crypto.ECKey; import org.tron.common.logsfilter.EventPluginLoader; import org.tron.common.logsfilter.capsule.BlockFilterCapsule; +import org.tron.common.logsfilter.capsule.BlockLogTriggerCapsule; import org.tron.common.logsfilter.capsule.FilterTriggerCapsule; import org.tron.common.logsfilter.capsule.LogsFilterCapsule; +import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule; +import org.tron.common.logsfilter.capsule.TriggerCapsule; import org.tron.common.logsfilter.trigger.ContractLogTrigger; import org.tron.common.parameter.CommonParameter; import org.tron.common.runtime.RuntimeImpl; @@ -1381,7 +1385,8 @@ public void isExchangeTransactionNonExchangeContractReturnsFalse() throws Except @Test public void blockTrigger() { Manager manager = spy(new Manager()); - doThrow(new RuntimeException("postBlockTrigger mock")).when(manager).postBlockTrigger(any()); + doThrow(new RuntimeException("postBlockTrigger mock")).when(manager) + .postBlockTrigger(any(), anyBoolean()); TronError thrown = Assert.assertThrows(TronError.class, () -> manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1)); Assert.assertEquals(TronError.ErrCode.EVENT_SUBSCRIBE_ERROR, thrown.getErrCode()); @@ -1425,6 +1430,142 @@ public void testReOrgContractTriggerClearsMap() throws Exception { } } + private EventPluginLoader installMockLoader() throws Exception { + ReflectUtils.setFieldValue(dbManager, "eventPluginLoaded", true); + EventPluginLoader mockLoader = mock(EventPluginLoader.class); + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + instanceField.set(null, mockLoader); + return mockLoader; + } + + private void restoreLoader(EventPluginLoader original) throws Exception { + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + instanceField.set(null, original); + ReflectUtils.setFieldValue(dbManager, "eventPluginLoaded", false); + dbManager.getTriggerCapsuleQueue().clear(); + } + + private BlockCapsule blockWithOneTransfer() { + BlockCapsule block = new BlockCapsule(1, chainManager.getGenesisBlockId(), + System.currentTimeMillis(), ByteString.EMPTY); + TransferContract tc = TransferContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(new byte[21])) + .setToAddress(ByteString.copyFrom(new byte[21])) + .setAmount(1L).build(); + block.addTransaction(new TransactionCapsule(tc, ContractType.TransferContract)); + return block; + } + + @Test + public void testReOrgBlockTriggerRemoved() throws Exception { + // version-0 reorg emit core: postBlockTrigger threads the removed flag onto both the block + // and transaction triggers. reOrgBlockTrigger calls postBlockTrigger(block, true) (rollback), + // reApplyBlockEvents calls postBlockTrigger(block, false) (forward); both delegate here. + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + EventPluginLoader originalLoader = (EventPluginLoader) instanceField.get(null); + EventPluginLoader mockLoader = installMockLoader(); + when(mockLoader.isBlockLogTriggerEnable()).thenReturn(true); + when(mockLoader.isBlockLogTriggerSolidified()).thenReturn(false); + when(mockLoader.isTransactionLogTriggerEnable()).thenReturn(true); + when(mockLoader.isTransactionLogTriggerSolidified()).thenReturn(false); + when(mockLoader.isTransactionLogTriggerEthCompatible()).thenReturn(false); + + BlockingQueue queue = dbManager.getTriggerCapsuleQueue(); + queue.clear(); + BlockCapsule block = blockWithOneTransfer(); + try { + // rollback: block + transaction triggers re-emitted with removed=true + dbManager.postBlockTrigger(block, true); + Assert.assertEquals(2, queue.size()); + Assert.assertTrue(((BlockLogTriggerCapsule) queue.poll()).getBlockLogTrigger().isRemoved()); + Assert.assertTrue(((TransactionLogTriggerCapsule) queue.poll()) + .getTransactionLogTrigger().isRemoved()); + + // forward: removed=false + dbManager.postBlockTrigger(block, false); + Assert.assertEquals(2, queue.size()); + Assert.assertFalse(((BlockLogTriggerCapsule) queue.poll()).getBlockLogTrigger().isRemoved()); + Assert.assertFalse(((TransactionLogTriggerCapsule) queue.poll()) + .getTransactionLogTrigger().isRemoved()); + } finally { + restoreLoader(originalLoader); + } + } + + @Test + public void testReApplyBlockEvents() throws Exception { + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + EventPluginLoader originalLoader = (EventPluginLoader) instanceField.get(null); + EventPluginLoader mockLoader = installMockLoader(); + when(mockLoader.getVersion()).thenReturn(0); + when(mockLoader.isBlockLogTriggerEnable()).thenReturn(true); + when(mockLoader.isBlockLogTriggerSolidified()).thenReturn(false); + when(mockLoader.isTransactionLogTriggerEnable()).thenReturn(false); + + BlockingQueue queue = dbManager.getTriggerCapsuleQueue(); + queue.clear(); + BlockCapsule block = new BlockCapsule(1, chainManager.getGenesisBlockId(), + System.currentTimeMillis(), ByteString.EMPTY); + List branch = new ArrayList<>(); + branch.add(new KhaosDatabase.KhaosBlock(block)); + try { + Method m = Manager.class.getDeclaredMethod("reApplyBlockEvents", List.class); + m.setAccessible(true); + m.invoke(dbManager, branch); + // forward block trigger emitted for the re-applied fork branch (removed=false) + Assert.assertEquals(1, queue.size()); + Assert.assertFalse(((BlockLogTriggerCapsule) queue.poll()) + .getBlockLogTrigger().isRemoved()); + } finally { + restoreLoader(originalLoader); + } + } + + @Test + public void testReOrgBlockTrigger() throws Exception { + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + EventPluginLoader originalLoader = (EventPluginLoader) instanceField.get(null); + EventPluginLoader mockLoader = installMockLoader(); + when(mockLoader.isBlockLogTriggerEnable()).thenReturn(true); + when(mockLoader.isTransactionLogTriggerEnable()).thenReturn(false); + try { + Method m = Manager.class.getDeclaredMethod("reOrgBlockTrigger"); + m.setAccessible(true); + // exercises the fetch of the old head block + try/catch; must not throw + m.invoke(dbManager); + } finally { + restoreLoader(originalLoader); + } + } + + @Test + public void testPostSolidityTriggerSolidified() throws Exception { + Field instanceField = EventPluginLoader.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + EventPluginLoader originalLoader = (EventPluginLoader) instanceField.get(null); + EventPluginLoader mockLoader = installMockLoader(); + when(mockLoader.isBlockLogTriggerEnable()).thenReturn(true); + when(mockLoader.isBlockLogTriggerSolidified()).thenReturn(true); + when(mockLoader.isTransactionLogTriggerEnable()).thenReturn(true); + when(mockLoader.isTransactionLogTriggerSolidified()).thenReturn(true); + when(mockLoader.isTransactionLogTriggerEthCompatible()).thenReturn(false); + // make getContinuousBlockCapsule cover the current head block + ReflectUtils.setFieldValue(dbManager, "lastUsedSolidityNum", -1L); + try { + Method m = Manager.class.getDeclaredMethod("postSolidityTrigger", long.class); + m.setAccessible(true); + // exercises the solidified-mode block/transaction batch emission + m.invoke(dbManager, dbManager.getHeadBlockNum()); + } finally { + restoreLoader(originalLoader); + } + } + @Test public void testClearSolidityContractTriggerCache() throws Exception { long blockNum = 999L; diff --git a/framework/src/test/java/org/tron/core/event/RealtimeEventServiceTest.java b/framework/src/test/java/org/tron/core/event/RealtimeEventServiceTest.java index 91dcea71322..f58f725195c 100644 --- a/framework/src/test/java/org/tron/core/event/RealtimeEventServiceTest.java +++ b/framework/src/test/java/org/tron/core/event/RealtimeEventServiceTest.java @@ -5,21 +5,17 @@ import com.google.protobuf.ByteString; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.BlockingQueue; -import org.eclipse.jetty.util.BlockingArrayQueue; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; import org.tron.common.logsfilter.EventPluginLoader; import org.tron.common.logsfilter.capsule.BlockLogTriggerCapsule; import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule; -import org.tron.common.logsfilter.capsule.TriggerCapsule; import org.tron.common.logsfilter.trigger.ContractEventTrigger; import org.tron.common.logsfilter.trigger.ContractLogTrigger; import org.tron.common.utils.ReflectUtils; import org.tron.common.utils.Sha256Hash; import org.tron.core.capsule.BlockCapsule; -import org.tron.core.db.Manager; import org.tron.core.services.event.BlockEventCache; import org.tron.core.services.event.RealtimeEventService; import org.tron.core.services.event.bo.BlockEvent; @@ -56,39 +52,39 @@ public void test() throws Exception { EventPluginLoader instance = mock(EventPluginLoader.class); ReflectUtils.setFieldValue(realtimeEventService, "instance", instance); - BlockingQueue queue = new BlockingArrayQueue<>(); - Manager manager = mock(Manager.class); - Mockito.when(manager.getTriggerCapsuleQueue()).thenReturn(queue); - ReflectUtils.setFieldValue(realtimeEventService, "manager", manager); - BlockCapsule blockCapsule = new BlockCapsule(0L, Sha256Hash.ZERO_HASH, 0L, ByteString.copyFrom(BlockEventCacheTest.getBlockId())); - be2.setBlockLogTriggerCapsule(new BlockLogTriggerCapsule(blockCapsule)); + // spy so processTrigger() is a no-op (does not reach the real EventPluginLoader), + // while setRemoved() still mutates the real trigger so the removed flag can be asserted. + BlockLogTriggerCapsule blockCap = Mockito.spy(new BlockLogTriggerCapsule(blockCapsule)); + Mockito.doNothing().when(blockCap).processTrigger(); + be2.setBlockLogTriggerCapsule(blockCap); Mockito.when(instance.isBlockLogTriggerEnable()).thenReturn(true); Mockito.when(instance.isBlockLogTriggerSolidified()).thenReturn(false); - realtimeEventService.add(event); - realtimeEventService.work(); - - Assert.assertEquals(0, queue.size()); - - event = new Event(be2, false); - realtimeEventService.add(event); - realtimeEventService.work(); + // reorg rollback: block trigger re-emitted (posted synchronously) with removed=true + realtimeEventService.flush(be2, true); + Assert.assertTrue(blockCap.getBlockLogTrigger().isRemoved()); - Assert.assertEquals(1, queue.size()); + // forward: block trigger posted with removed=false + realtimeEventService.flush(be2, false); + Assert.assertFalse(blockCap.getBlockLogTrigger().isRemoved()); + // posted directly to the plugin both times, never via the async queue + Mockito.verify(blockCap, Mockito.times(2)).processTrigger(); be2.setBlockLogTriggerCapsule(null); - queue.poll(); + TransactionLogTriggerCapsule txCap = mock(TransactionLogTriggerCapsule.class); List list = new ArrayList<>(); - list.add(mock(TransactionLogTriggerCapsule.class)); + list.add(txCap); be2.setTransactionLogTriggerCapsules(list); - Mockito.when(instance.isTransactionLogTriggerEnable()).thenReturn(true); Mockito.when(instance.isTransactionLogTriggerSolidified()).thenReturn(false); - realtimeEventService.flush(be2, event.isRemove()); - Assert.assertEquals(1, queue.size()); + + // rollback: tx trigger posted synchronously with removed=true + realtimeEventService.flush(be2, true); + Mockito.verify(txCap).setRemoved(true); + Mockito.verify(txCap).processTrigger(); be2.setTransactionLogTriggerCapsules(null); From bb077620ada85aa1371e9fa7d91bba8f7400d17f Mon Sep 17 00:00:00 2001 From: bladehan1 Date: Tue, 16 Jun 2026 18:30:07 +0800 Subject: [PATCH 45/57] fix(api): hide jetty internals in oversized request 413 response (#6843) --- .../tron/common/application/HttpService.java | 37 ++++++++ .../common/jetty/SizeLimitHandlerTest.java | 91 ++++++++++++++++++- 2 files changed, 125 insertions(+), 3 deletions(-) diff --git a/framework/src/main/java/org/tron/common/application/HttpService.java b/framework/src/main/java/org/tron/common/application/HttpService.java index 1318fd96527..1dea271ec69 100644 --- a/framework/src/main/java/org/tron/common/application/HttpService.java +++ b/framework/src/main/java/org/tron/common/application/HttpService.java @@ -16,12 +16,22 @@ package org.tron.common.application; import com.google.common.annotations.VisibleForTesting; +import java.io.IOException; +import java.nio.ByteBuffer; import java.util.concurrent.CompletableFuture; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.ConnectionLimit; +import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.SizeLimitHandler; import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.BufferUtil; import org.tron.core.config.args.Args; @Slf4j(topic = "rpc") @@ -72,6 +82,7 @@ protected void initServer() { if (maxHttpConnectNumber > 0) { this.apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, this.apiServer)); } + this.apiServer.setErrorHandler(new OversizedRequestErrorHandler()); } protected ServletContextHandler initContextHandler() { @@ -88,4 +99,30 @@ protected ServletContextHandler initContextHandler() { protected void addFilter(ServletContextHandler context) { } + + /** + * For oversized requests (the 413 thrown by SizeLimitHandler during dispatch) logs the + * detail server-side and returns the short, uniform bad-message page, instead of the + * default error page that leaks the exception stack and internal request sizes. All + * other errors keep Jetty's default handling. + */ + private static final class OversizedRequestErrorHandler extends ErrorHandler { + + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, + HttpServletResponse response) throws IOException, ServletException { + if (response.getStatus() == HttpStatus.PAYLOAD_TOO_LARGE_413) { + Throwable cause = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); + logger.info("Reject oversized request, uri: {}, detail: {}", + request.getRequestURI(), cause == null ? "413" : cause.getMessage()); + baseRequest.setHandled(true); + ByteBuffer body = badMessageError(HttpStatus.PAYLOAD_TOO_LARGE_413, + HttpStatus.getMessage(HttpStatus.PAYLOAD_TOO_LARGE_413), + baseRequest.getResponse().getHttpFields()); + response.getOutputStream().write(BufferUtil.toArray(body)); + return; + } + super.handle(target, baseRequest, request, response); + } + } } diff --git a/framework/src/test/java/org/tron/common/jetty/SizeLimitHandlerTest.java b/framework/src/test/java/org/tron/common/jetty/SizeLimitHandlerTest.java index 64108943ad5..145eda6d398 100644 --- a/framework/src/test/java/org/tron/common/jetty/SizeLimitHandlerTest.java +++ b/framework/src/test/java/org/tron/common/jetty/SizeLimitHandlerTest.java @@ -1,8 +1,12 @@ package org.tron.common.jetty; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.Socket; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.servlet.http.HttpServlet; @@ -33,7 +37,7 @@ /** * Tests {@link org.eclipse.jetty.server.handler.SizeLimitHandler} body-size - * enforcement configured in {@link HttpService#initContextHandler()}. + * enforcement configured in {@link HttpService}. * * Covers: accept/reject by size, UTF-8 byte counting, independent limits * across HttpService instances, chunked transfer, and zero-limit behavior. @@ -152,10 +156,69 @@ public void testHttpBodyWithinLimit() throws Exception { Assert.assertEquals(200, post(httpServerUri, new StringEntity("small body"))); } + /** + * An oversized request must return 413 carrying the short, uniform bad-message + * page produced by the custom ErrorHandler in {@link HttpService} - + * not the default Jetty error page that leaks the exception stack, class name + * and the internal request size. + */ @Test public void testHttpBodyExceedsLimit() throws Exception { - Assert.assertEquals(413, - post(httpServerUri, new StringEntity(repeat('a', HTTP_MAX_BODY_SIZE + 1)))); + HttpPost req = new HttpPost(httpServerUri); + req.setEntity(new StringEntity(repeat('a', HTTP_MAX_BODY_SIZE + 1))); + HttpResponse resp = client.execute(req); + + String body = EntityUtils.toString(resp.getEntity()); + + // return value: 413 + Assert.assertEquals(413, resp.getStatusLine().getStatusCode()); + + // returned page: short uniform bad-message page with the generic reason + Assert.assertTrue("should render the short bad-message page", + body.contains("Bad Message 413")); + Assert.assertTrue("reason should be the generic status message", + body.contains("Payload Too Large")); + + // must NOT leak Jetty internals + Assert.assertFalse("must not leak exception class / stack frames", + body.contains("org.eclipse.jetty")); + } + + /** + * A malformed Content-Length is rejected by the HTTP parser (onBadMessage -> + * ErrorHandler.badMessageError()), a different path from the 413 dispatch handler. + * Confirms the custom ErrorHandler leaves other 4xx untouched: still the default + * 400 bad-message page, not rerouted through the 413 branch. + */ + @Test + public void testBadContentLengthReturnsDefault400() throws Exception { + String raw = "POST / HTTP/1.1\r\n" + + "Host: localhost\r\n" + + "Content-Length: +450\r\n" + + "\r\n"; + String resp = sendRaw(httpServerUri, raw); + + Assert.assertTrue("expected 400, got: " + firstLine(resp), resp.startsWith("HTTP/1.1 400")); + Assert.assertTrue("should be the default bad-message page", resp.contains("Bad Message 400")); + Assert.assertFalse("must not be rerouted through the 413 branch", + resp.contains("Payload Too Large")); + } + + /** + * A request-line URI longer than the request header buffer is rejected by the HTTP + * parser (414), again via badMessageError(), not the 413 dispatch handler. Confirms it + * is unaffected by the custom ErrorHandler. + */ + @Test + public void testOversizedUriReturnsDefault414() throws Exception { + String raw = "GET /" + repeat('a', 9000) + " HTTP/1.1\r\n" // request line > default 8KB + + "Host: localhost\r\n" + + "\r\n"; + String resp = sendRaw(httpServerUri, raw); + + Assert.assertTrue("expected 414, got: " + firstLine(resp), resp.startsWith("HTTP/1.1 414")); + Assert.assertFalse("must not be rerouted through the 413 branch", + resp.contains("Payload Too Large")); } @Test @@ -324,4 +387,26 @@ private int post(URI uri, HttpEntity entity) throws Exception { private static String repeat(char c, int n) { return new String(new char[n]).replace('\0', c); } + + /** Sends a raw HTTP request over a socket and returns the full response (until EOF). */ + private static String sendRaw(URI uri, String rawRequest) throws Exception { + try (Socket socket = new Socket(uri.getHost(), uri.getPort())) { + socket.getOutputStream().write(rawRequest.getBytes(StandardCharsets.US_ASCII)); + socket.getOutputStream().flush(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + InputStream in = socket.getInputStream(); + byte[] buf = new byte[4096]; + int n; + while ((n = in.read(buf)) != -1) { + out.write(buf, 0, n); + } + return new String(out.toByteArray(), StandardCharsets.UTF_8); + } + } + + /** First line (status line) of a raw HTTP response. */ + private static String firstLine(String resp) { + int idx = resp.indexOf("\r\n"); + return idx < 0 ? resp : resp.substring(0, idx); + } } From 087107985e2f675ab8b172eef8363b1aee86bc05 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 17 Jun 2026 15:24:39 +0800 Subject: [PATCH 46/57] refactor(http): improve JsonFormat parser robustness (#6845) Improve the hand-rolled JsonFormat parser used by HTTP wallet APIs so field-heavy and deeply nested JSON no longer overflows the request thread stack. mergeField now consumes one field per call and leaves comma iteration to the caller. JsonFormat also enforces Constant.MAX_NESTING_DEPTH for object/array descent and returns ParseException instead of allowing StackOverflowError to escape. Compatibility notes: direct JsonFormat.merge callers now reject nesting beyond 100 levels; trailing commas on known-field parse paths (top-level fields, known message fields, and known repeated fields) may be accepted, while trailing commas inside unknown nested object/array fields remain rejected; malicious deep-nesting requests now surface as parse errors instead of container-level HTTP 500s. --- .../tron/core/services/http/JsonFormat.java | 79 +++++--- .../services/jsonrpc/TronJsonRpcImpl.java | 2 +- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 5 +- .../core/services/http/JsonFormatTest.java | 184 +++++++++++++++++- .../TriggerConstantContractServletTest.java | 49 +++++ 5 files changed, 287 insertions(+), 32 deletions(-) create mode 100644 framework/src/test/java/org/tron/core/services/http/TriggerConstantContractServletTest.java diff --git a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java index 70b59e72b4d..b2b2eeec8d7 100644 --- a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java +++ b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java @@ -57,6 +57,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import org.tron.common.utils.ByteArray; import org.tron.common.utils.Commons; import org.tron.common.utils.StringUtil; +import org.tron.core.Constant; import org.tron.json.JSON; import org.tron.protos.contract.BalanceContract; @@ -291,6 +292,7 @@ public static void merge(CharSequence input, tokenizer.consume("{"); // Needs to happen when the object starts. while (!tokenizer.tryConsume("}")) { // Continue till the object is done mergeField(tokenizer, extensionRegistry, builder, selfType); + tokenizer.tryConsume(","); } // Test to make sure the tokenizer has reached the end of the stream. if (!tokenizer.atEnd()) { @@ -556,8 +558,9 @@ protected static StringBuilder toStringBuilder(Readable input) throws IOExceptio } /** - * Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is - * detected after the field ends, the next field will be parsed automatically + * Parse a single field from {@code tokenizer} and merge it into {@code builder}. Exactly one + * field is consumed; the caller ({@code merge} / {@code handleObject}) consumes any trailing + * ',' and loops over the remaining fields. */ protected static void mergeField(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder, @@ -628,11 +631,6 @@ protected static void mergeField(Tokenizer tokenizer, handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown, selfType); } } - - if (tokenizer.tryConsume(",")) { - // Continue with the next field - mergeField(tokenizer, extensionRegistry, builder, selfType); - } } private static void handleMissingField(Tokenizer tokenizer, @@ -642,18 +640,28 @@ private static void handleMissingField(Tokenizer tokenizer, if ("{".equals(tokenizer.currentToken())) { // Message structure tokenizer.consume("{"); - do { - tokenizer.consumeIdentifier(); - handleMissingField(tokenizer, extensionRegistry, builder); - } while (tokenizer.tryConsume(",")); - tokenizer.consume("}"); + tokenizer.enterRecursion(); + try { + do { + tokenizer.consumeIdentifier(); + handleMissingField(tokenizer, extensionRegistry, builder); + } while (tokenizer.tryConsume(",")); + tokenizer.consume("}"); + } finally { + tokenizer.exitRecursion(); + } } else if ("[".equals(tokenizer.currentToken())) { // Collection tokenizer.consume("["); - do { - handleMissingField(tokenizer, extensionRegistry, builder); - } while (tokenizer.tryConsume(",")); - tokenizer.consume("]"); + tokenizer.enterRecursion(); + try { + do { + handleMissingField(tokenizer, extensionRegistry, builder); + } while (tokenizer.tryConsume(",")); + tokenizer.consume("]"); + } finally { + tokenizer.exitRecursion(); + } } else { //if (!",".equals(tokenizer.currentToken)){ // Primitive value if ("null".equals(tokenizer.currentToken())) { @@ -807,20 +815,25 @@ private static Object handleObject(Tokenizer tokenizer, } tokenizer.consume("{"); - String endToken = "}"; + tokenizer.enterRecursion(); + try { + String endToken = "}"; - while (!tokenizer.tryConsume(endToken)) { - if (tokenizer.atEnd()) { - throw tokenizer.parseException("Expected \"" + endToken + "\"."); - } - mergeField(tokenizer, extensionRegistry, subBuilder, selfType); - if (tokenizer.tryConsume(",")) { - // there are more fields in the object, so continue - continue; + while (!tokenizer.tryConsume(endToken)) { + if (tokenizer.atEnd()) { + throw tokenizer.parseException("Expected \"" + endToken + "\"."); + } + mergeField(tokenizer, extensionRegistry, subBuilder, selfType); + if (tokenizer.tryConsume(",")) { + // there are more fields in the object, so continue + continue; + } } - } - return subBuilder.build(); + return subBuilder.build(); + } finally { + tokenizer.exitRecursion(); + } } /** @@ -1290,6 +1303,18 @@ protected static class Tokenizer { // errors *after* consuming). private int previousLine = 0; private int previousColumn = 0; + private int currentDepth = 0; + + public void enterRecursion() throws ParseException { + if (currentDepth >= Constant.MAX_NESTING_DEPTH) { + throw parseException("Hit recursion limit."); + } + ++currentDepth; + } + + public void exitRecursion() { + --currentDepth; + } /** * Construct a tokenizer that parses tokens from the given text. diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 82568755892..825b1ff2f3e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -1143,7 +1143,7 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress, String abiStr = "{" + "\"entrys\":" + args.getAbi() + "}"; try { JsonFormat.merge(abiStr, abiBuilder, args.isVisible()); - } catch (StackOverflowError e) { + } catch (JsonFormat.ParseException | StackOverflowError e) { throw new JsonRpcInvalidParamsException("invalid abi"); } } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 870ce317663..5b2875fc248 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -1461,9 +1461,8 @@ public void testBuildTransactionTransfer() { @Test public void testBuildTransactionRejectsDeeplyNestedAbi() { - // A pathological ABI with deep nesting overflows the recursive JsonFormat parser. - // buildCreateSmartContractTransaction must surface this as invalid-params (-32602), - // not let a StackOverflowError escape as a generic internal error. + // A deeply nested ABI must surface as invalid-params (-32602), not as a generic + // internal error. int depth = 200_000; StringBuilder abi = new StringBuilder("[],\"x\":"); for (int i = 0; i < depth; i++) { diff --git a/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java b/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java index a8525b0f526..e38af6ead28 100644 --- a/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java +++ b/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java @@ -17,6 +17,7 @@ import org.junit.After; import org.junit.Test; import org.mockito.Mockito; +import org.tron.core.Constant; import org.tron.protos.Protocol; public class JsonFormatTest { @@ -252,4 +253,185 @@ public void testParseInteger() throws Exception { assertTrue(cause instanceof NumberFormatException); } -} \ No newline at end of file + /* + * Compatibility-preserved behavior: these cases pass before and after this fix. + * They guard unknown-field skipping, repeated-field syntax, and accepted depth. + */ + @Test + public void testMissingFieldSkipsNestedObjectAndArray() throws Exception { + String input = "{\"zzz\":{\"a\":1,\"b\":[true,false,null,{\"c\":\"d\"}],\"e\":{\"f\":2}}," + + "\"address\":\"61646472657373\"}"; + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.merge(input, builder, false); + + assertEquals(ByteString.copyFromUtf8("address"), builder.getAddress()); + } + + @Test + public void testTrailingCommaInKnownRepeatedField() throws Exception { + Protocol.Proposal.Builder builder = Protocol.Proposal.newBuilder(); + + JsonFormat.merge("{\"approvals\":[\"00\",\"01\",]}", builder, false); + + Protocol.Proposal proposal = builder.build(); + assertEquals(2, proposal.getApprovalsCount()); + assertEquals(ByteString.copyFrom(new byte[] {0}), proposal.getApprovals(0)); + assertEquals(ByteString.copyFrom(new byte[] {1}), proposal.getApprovals(1)); + } + + @Test + public void testKnownRepeatedPrimitiveFieldRejectsNestedArray() { + Protocol.Proposal.Builder builder = Protocol.Proposal.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge("{\"approvals\":[[\"00\"]]}", builder, false)); + + assertTrue(e.getMessage().contains("Expected string.")); + } + + @Test + public void testKnownRepeatedMessageFieldRejectsNestedArray() { + Protocol.Block.Builder builder = Protocol.Block.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge("{\"transactions\":[[]]}", builder, false)); + + assertTrue(e.getMessage().contains("Expected \"{\".")); + } + + @Test + public void testMissingFieldRejectsTrailingCommaInNestedObject() { + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge("{\"zzz\":{\"a\":1,}}", builder)); + + assertTrue(e.getMessage().contains("Expected identifier")); + } + + @Test + public void testMissingFieldRejectsTrailingCommaInNestedArray() { + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge("{\"zzz\":[1,]}", builder)); + + assertTrue(e.getMessage().contains("Expected string.")); + } + + @Test + public void testNestingWithinLimit() throws Exception { + int depth = Constant.MAX_NESTING_DEPTH / 2; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < depth; i++) { + sb.append("{\"zzz\":"); + } + sb.append("1"); + for (int i = 0; i < depth; i++) { + sb.append("}"); + } + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + JsonFormat.merge(sb.toString(), builder); + assertNotNull(builder.build()); + } + + /* + * Behavior changed by this fix: the previous parser either missed the depth guard + * or failed through recursive comma handling / stack overflow. + */ + @Test + public void testTrailingCommaInParsedObjects() throws Exception { + String input = "{\"genesisBlockId\":{\"hash\":\"00\",\"number\":1,}," + + "\"address\":\"61646472657373\",}"; + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.merge(input, builder, false); + + Protocol.HelloMessage message = builder.build(); + assertEquals(ByteString.copyFrom(new byte[] {0}), message.getGenesisBlockId().getHash()); + assertEquals(1L, message.getGenesisBlockId().getNumber()); + assertEquals(ByteString.copyFromUtf8("address"), message.getAddress()); + } + + @Test + public void testMissingFieldRejectsObjectBeyondNestingLimit() { + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge(buildUnknownNestedObject(Constant.MAX_NESTING_DEPTH + 1), builder)); + + assertTrue(e.getMessage().contains("Hit recursion limit.")); + } + + @Test + public void testMissingFieldRejectsArrayBeyondNestingLimit() { + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge(buildUnknownNestedArray(Constant.MAX_NESTING_DEPTH + 1), builder)); + + assertTrue(e.getMessage().contains("Hit recursion limit.")); + } + + @Test + public void testDeeplyNestedObject() { + int depth = 100_000; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < depth; i++) { + sb.append("{\"zzz\":"); + } + sb.append("1"); + for (int i = 0; i < depth; i++) { + sb.append("}"); + } + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + assertThrows(JsonFormat.ParseException.class, () -> JsonFormat.merge(sb.toString(), builder)); + } + + @Test + public void testDeeplyNestedArray() { + int depth = 100_000; + StringBuilder sb = new StringBuilder(); + sb.append("{\"zzz\":"); + for (int i = 0; i < depth; i++) { + sb.append("["); + } + sb.append("1"); + for (int i = 0; i < depth; i++) { + sb.append("]"); + } + sb.append("}"); + Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); + assertThrows(JsonFormat.ParseException.class, () -> JsonFormat.merge(sb.toString(), builder)); + } + + private String buildUnknownNestedObject(int depth) { + StringBuilder sb = new StringBuilder(); + sb.append("{"); + for (int i = 0; i < depth; i++) { + sb.append("\"zzz\":{"); + } + sb.append("\"leaf\":1"); + for (int i = 0; i < depth; i++) { + sb.append("}"); + } + sb.append("}"); + return sb.toString(); + } + + private String buildUnknownNestedArray(int depth) { + StringBuilder sb = new StringBuilder(); + sb.append("{\"zzz\":"); + for (int i = 0; i < depth; i++) { + sb.append("["); + } + sb.append("1"); + for (int i = 0; i < depth; i++) { + sb.append("]"); + } + sb.append("}"); + return sb.toString(); + } + +} diff --git a/framework/src/test/java/org/tron/core/services/http/TriggerConstantContractServletTest.java b/framework/src/test/java/org/tron/core/services/http/TriggerConstantContractServletTest.java new file mode 100644 index 00000000000..2a139f8a158 --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/http/TriggerConstantContractServletTest.java @@ -0,0 +1,49 @@ +package org.tron.core.services.http; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletResponse; +import org.tron.common.crypto.ECKey; +import org.tron.common.utils.ByteArray; +import org.tron.core.capsule.TransactionCapsule; + +public class TriggerConstantContractServletTest extends BaseHttpTest { + + private TriggerConstantContractServlet servlet; + + @Override + protected void setUpMocks() throws Exception { + servlet = new TriggerConstantContractServlet(); + injectWallet(servlet); + } + + @Test + public void testManyFlatFieldsDoesNotOverflowStack() throws Exception { + String owner = ByteArray.toHexString(new ECKey().getAddress()); + String contract = ByteArray.toHexString(new ECKey().getAddress()); + + StringBuilder body = new StringBuilder(256 * 1024) + .append("{\"owner_address\":\"").append(owner).append('"') + .append(",\"contract_address\":\"").append(contract).append('"') + .append(",\"data\":\"00\""); + for (int i = 0; i < 20_000; i++) { + body.append(",\"x").append(i).append("\":1"); + } + body.append('}'); + + when(wallet.createTransactionCapsule(any(), any())) + .thenReturn(new TransactionCapsule(MINIMAL_TX)); + when(wallet.triggerConstantContract(any(), any(), any(), any())) + .thenReturn(MINIMAL_TX); + + MockHttpServletResponse response = newResponse(); + servlet.doPost(postRequest(body.toString()), response); + + assertEquals(200, response.getStatus()); + verify(wallet).triggerConstantContract(any(), any(), any(), any()); + } +} From f40f188dbedb3700428b70b43609b24c2dc26ce0 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Wed, 17 Jun 2026 18:10:49 +0800 Subject: [PATCH 47/57] fix(jsonrpc): enforce maxBlockRange on eth_getFilterLogs (#6842) * fix(jsonrpc): enforce maxBlockRange on eth_getFilterLogs * style(jsonrpc): fix styles and comments --- .../services/jsonrpc/TronJsonRpcImpl.java | 7 ++++- .../jsonrpc/filters/LogFilterWrapper.java | 21 +++++++++----- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 29 +++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 825b1ff2f3e..973de6c6b88 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -1548,7 +1548,8 @@ public LogFilterElement[] getLogs(FilterRequest fr) throws JsonRpcInvalidParamsE } @Override - public LogFilterElement[] getFilterLogs(String filterId) throws ExecutionException, + public LogFilterElement[] getFilterLogs(String filterId) throws + JsonRpcInvalidParamsException, ExecutionException, InterruptedException, BadItemException, ItemNotFoundException, JsonRpcMethodNotFoundException, JsonRpcTooManyResultException { disableInPBFT("eth_getFilterLogs"); @@ -1568,6 +1569,10 @@ public LogFilterElement[] getFilterLogs(String filterId) throws ExecutionExcepti LogFilterWrapper logFilterWrapper = eventFilter2Result.get(filterId).getLogFilterWrapper(); long currentMaxBlockNum = wallet.getNowBlock().getBlockHeader().getRawData().getNumber(); + // re-check the block range against the current head: the filter was created without the cap + // (eth_newFilter), so enforce it here to prevent an unbounded scan. + logFilterWrapper.validateBlockRange(currentMaxBlockNum); + return getLogsByLogFilterWrapper(logFilterWrapper, currentMaxBlockNum); } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java index 3f9582d4825..0fdf174bb50 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java @@ -98,16 +98,23 @@ 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 - && min(toBlockSrc, currentMaxBlockNum) - fromBlockSrc > maxBlockRange) { - throw new JsonRpcInvalidParamsException("exceed max block range: " + maxBlockRange); - } } this.fromBlock = fromBlockSrc; this.toBlock = toBlockSrc; + + // eth_getLogs enforces the block range at construction time. eth_newFilter creates the + // wrapper with checkBlockRange=false (no creation-time gate); eth_getFilterLogs re-runs this + // check against the current head before scanning so the cap cannot be bypassed. + if (checkBlockRange) { + validateBlockRange(currentMaxBlockNum); + } + } + + public void validateBlockRange(long currentMaxBlockNum) throws JsonRpcInvalidParamsException { + int maxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange(); + if (maxBlockRange > 0 && min(toBlock, currentMaxBlockNum) - fromBlock > maxBlockRange) { + throw new JsonRpcInvalidParamsException("exceed max block range: " + maxBlockRange); + } } } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 5b2875fc248..6ef74ce2a1c 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -1328,6 +1328,35 @@ public void testLogFilterTagHandling() { } } + @Test + public void testGetFilterLogsEnforcesBlockRange() throws Exception { + int oldMaxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange(); + Args.getInstance().setJsonRpcMaxBlockRange(5_000); + try { + // toBlock=0xf4240 (1_000_000) is clamped to head (LATEST_BLOCK_NUM = 10_000), so the span + // is min(1_000_000, 10_000) - 0 = 10_000 > 5_000 cap. + String wideFilterId = tronJsonRpc.newFilter( + new FilterRequest("0x0", "0xf4240", null, null, null)); + Assert.assertNotNull(wideFilterId); + + // eth_getFilterLogs must reject it at query time (previously bypassed -> unbounded scan). + JsonRpcInvalidParamsException ex = Assert.assertThrows( + JsonRpcInvalidParamsException.class, + () -> tronJsonRpc.getFilterLogs(wideFilterId)); + Assert.assertEquals("exceed max block range: 5000", ex.getMessage()); + + // A within-cap filter (span 0) is still served normally - no false rejection. + String headHex = ByteArray.toJsonHex(blockCapsule1.getNum()); + int expectedLogs = blockCapsule1.getTransactions().size() * 2; + String okFilterId = tronJsonRpc.newFilter( + new FilterRequest(headHex, headHex, null, null, null)); + LogFilterElement[] okResult = tronJsonRpc.getFilterLogs(okFilterId); + Assert.assertEquals(expectedLogs, okResult.length); + } finally { + Args.getInstance().setJsonRpcMaxBlockRange(oldMaxBlockRange); + } + } + @Test public void testGetBlockReceipts() { From 95d84d3be9f149b14acaa5e56f244a16764bdafe Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Wed, 17 Jun 2026 18:11:14 +0800 Subject: [PATCH 48/57] fix(api): tighten json parsing limits (#6844) --- .../src/main/java/org/tron/core/Constant.java | 2 +- .../core/services/jsonrpc/JsonRpcServlet.java | 7 +++++- .../services/jsonrpc/TronJsonRpcImpl.java | 2 +- .../services/jsonrpc/JsonRpcServletTest.java | 23 +++++++++++++++---- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 3a2c59d5139..5d3f3099c91 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -65,7 +65,7 @@ public class Constant { public static final String LOCAL_HOST = "127.0.0.1"; // JSON parsing (DoS protection) - public static final int MAX_NESTING_DEPTH = 100; + public static final int MAX_NESTING_DEPTH = 20; public static final int MAX_TOKEN_COUNT = 100_000; } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index a332757457f..ca249da4e5d 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.StreamReadConstraints; +import com.fasterxml.jackson.core.exc.StreamConstraintsException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -114,7 +115,11 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I return; } } catch (JsonProcessingException e) { - writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false); + if (e instanceof StreamConstraintsException) { + writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, e.getMessage(), null, false); + } else { + writeJsonRpcError(resp, JsonRpcError.PARSE_ERROR, "JSON parse error", null, false); + } return; } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 973de6c6b88..6be47886117 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -1143,7 +1143,7 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress, String abiStr = "{" + "\"entrys\":" + args.getAbi() + "}"; try { JsonFormat.merge(abiStr, abiBuilder, args.isVisible()); - } catch (JsonFormat.ParseException | StackOverflowError e) { + } catch (Exception e) { throw new JsonRpcInvalidParamsException("invalid abi"); } } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java index c5e87384b99..d6c843b5aea 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcServletTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; @@ -62,6 +63,8 @@ public void invalidJson_returnsParseError() throws Exception { JsonNode body = MAPPER.readTree(resp.getContentAsString()); assertFalse(body.isArray()); assertEquals(-32700, body.get("error").get("code").asInt()); + // A non-constraint JsonProcessingException keeps the generic message (else branch). + assertEquals("JSON parse error", body.get("error").get("message").asText()); assertEquals("2.0", body.get("jsonrpc").asText()); assertTrue(body.get("id").isNull()); } @@ -378,8 +381,14 @@ public void excessivelyNestedRequest_returnsParseError() throws Exception { MockHttpServletResponse resp = doPost(sb.toString()); assertEquals(200, resp.getStatus()); - assertEquals(-32700, - MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + JsonNode error = MAPPER.readTree(resp.getContentAsString()).get("error"); + assertEquals(-32700, error.get("code").asInt()); + // StreamConstraintsException message must be surfaced verbatim, not the generic text, + // so callers can tell which constraint (nesting depth) was hit. + String message = error.get("message").asText(); + assertNotEquals("JSON parse error", message); + assertTrue("expected a nesting-depth constraint message, got: " + message, + message.contains("nesting depth") && message.contains("exceeds the maximum allowed")); } @Test @@ -396,8 +405,14 @@ public void tooManyTokens_returnsParseError() throws Exception { MockHttpServletResponse resp = doPost(sb.toString()); assertEquals(200, resp.getStatus()); - assertEquals(-32700, - MAPPER.readTree(resp.getContentAsString()).get("error").get("code").asInt()); + JsonNode error = MAPPER.readTree(resp.getContentAsString()).get("error"); + assertEquals(-32700, error.get("code").asInt()); + // StreamConstraintsException message must be surfaced verbatim, not the generic text, + // so callers can tell which constraint (token count) was hit. + String message = error.get("message").asText(); + assertNotEquals("JSON parse error", message); + assertTrue("expected a token-count constraint message, got: " + message, + message.contains("Token count") && message.contains("exceeds the maximum allowed")); } // --- helpers --- From 3a9ccfe48c6a57847bb94ed8a58f5ff96da8dfb3 Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Tue, 23 Jun 2026 15:14:22 +0800 Subject: [PATCH 49/57] fix(net): reject unknown inventory type in P2P inv/fetch-inv handling (#6851) Validate the inventory type at the inbound entry points and reject any value other than TRX or BLOCK with P2pException(BAD_MESSAGE), before the type is used for cache insertion or outbound fetch construction: - P2pEventHandlerImpl.checkInvRateLimit: add else branch for unknown type - InventoryMsgHandler.check: add type allowlist check - FetchInvDataMsgHandler.check: validate raw getInventoryType() instead of getInvMessageType() (which mapped non-BLOCK types to TRX) Use getTypeValue() when building the error message to avoid calling getNumber() on an UNRECOGNIZED enum value. Add regression tests for all three entry points. Co-authored-by: wb --- .../tron/core/net/P2pEventHandlerImpl.java | 6 +++- .../FetchInvDataMsgHandler.java | 8 +++-- .../messagehandler/InventoryMsgHandler.java | 4 +++ .../core/net/P2pEventHandlerImplTest.java | 30 +++++++++++++++++++ .../FetchInvDataMsgHandlerTest.java | 21 +++++++++++++ .../InventoryMsgHandlerTest.java | 26 ++++++++++++++++ 6 files changed, 92 insertions(+), 3 deletions(-) diff --git a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java index f703779c616..9dd950ae57b 100644 --- a/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java +++ b/framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java @@ -215,7 +215,8 @@ private void processMessage(PeerConnection peer, byte[] data) { } } - private boolean checkInvRateLimit(PeerConnection peer, InventoryMessage msg) { + private boolean checkInvRateLimit(PeerConnection peer, InventoryMessage msg) + throws P2pException { InventoryType invType = msg.getInventoryType(); int currentSize = msg.getInventory().getIdsCount(); MessageStatistics stats = peer.getPeerStatistics().messageStatistics; @@ -237,6 +238,9 @@ private boolean checkInvRateLimit(PeerConnection peer, InventoryMessage msg) { peer.getInetAddress(), count, currentSize, maxBlockInvIn10s); return false; } + } else { + throw new P2pException(P2pException.TypeEnum.BAD_MESSAGE, + "unknown inventory type: " + msg.getInventory().getTypeValue()); } return true; } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java index b1f26468081..2d6d47d1dd1 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java @@ -160,9 +160,13 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg, "FetchInvData contains duplicate hashes, size: " + hashList.size()); } - MessageTypes type = fetchInvDataMsg.getInvMessageType(); + InventoryType invType = fetchInvDataMsg.getInventoryType(); + if (invType != InventoryType.TRX && invType != InventoryType.BLOCK) { + throw new P2pException(TypeEnum.BAD_MESSAGE, + "unknown inventory type: " + fetchInvDataMsg.getInventory().getTypeValue()); + } - if (type == MessageTypes.TRX) { + if (invType == InventoryType.TRX) { for (Sha256Hash hash : fetchInvDataMsg.getHashList()) { if (peer.getAdvInvSpread().getIfPresent(new Item(hash, InventoryType.TRX)) == null) { throw new P2pException(TypeEnum.BAD_MESSAGE, "not spread inv: " + hash); diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/InventoryMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/InventoryMsgHandler.java index 59232a8d258..f96f7f0b0ff 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/InventoryMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/InventoryMsgHandler.java @@ -63,6 +63,10 @@ private boolean check(PeerConnection peer, InventoryMessage inventoryMessage) } InventoryType type = inventoryMessage.getInventoryType(); + if (type != InventoryType.TRX && type != InventoryType.BLOCK) { + throw new P2pException(TypeEnum.BAD_MESSAGE, + "unknown inventory type: " + inventoryMessage.getInventory().getTypeValue()); + } int size = hashList.size(); if (peer.isNeedSyncFromPeer() || peer.isNeedSyncFromUs()) { diff --git a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java index 93b84450f7b..52cdfa9c826 100644 --- a/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java +++ b/framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java @@ -3,6 +3,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import com.google.protobuf.ByteString; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -212,6 +213,35 @@ public void testCheckInvRateLimitBlockBoundary() throws Exception { peer.getPeerStatistics().messageStatistics.tronInBlockInventoryElement.getCount(10)); } + @Test + public void testCheckInvRateLimitUnknownTypeRejected() throws Exception { + CommonParameter parameter = CommonParameter.getInstance(); + parameter.setMaxTps(10); + parameter.setMaxBlockInvPerSecond(10); + + PeerStatistics peerStatistics = new PeerStatistics(); + PeerConnection peer = mock(PeerConnection.class); + Mockito.when(peer.getPeerStatistics()).thenReturn(peerStatistics); + + P2pEventHandlerImpl handler = new P2pEventHandlerImpl(); + Method method = handler.getClass() + .getDeclaredMethod("processMessage", PeerConnection.class, byte[].class); + method.setAccessible(true); + + // craft an Inventory whose enum type holds an undefined value (2) + Protocol.Inventory inv = Protocol.Inventory.newBuilder() + .setTypeValue(2) + .addIds(ByteString.copyFrom(new byte[32])) + .build(); + InventoryMessage msg = new InventoryMessage(inv.toByteArray()); + Assert.assertEquals(InventoryType.UNRECOGNIZED, msg.getInventoryType()); + + method.invoke(handler, peer, msg.getSendBytes()); + + // checkInvRateLimit throws BAD_MESSAGE -> processException -> disconnect(BAD_PROTOCOL) + verify(peer).disconnect(Protocol.ReasonCode.BAD_PROTOCOL); + } + @Test public void testUpdateLastInteractiveTime() throws Exception { PeerConnection peer = new PeerConnection(); diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java index e8ec4257814..7ea6337443d 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java @@ -156,6 +156,27 @@ public void testDuplicateHashRejected() throws Exception { } } + @Test + public void testUnknownInventoryTypeRejected() throws Exception { + FetchInvDataMsgHandler handler = new FetchInvDataMsgHandler(); + PeerConnection peer = Mockito.mock(PeerConnection.class); + + // craft a FetchInvData whose enum type holds an undefined value (2) + Protocol.Inventory inv = Protocol.Inventory.newBuilder() + .setTypeValue(2) + .addIds(com.google.protobuf.ByteString.copyFrom(new byte[32])) + .build(); + FetchInvDataMessage msg = new FetchInvDataMessage(inv.toByteArray()); + Assert.assertEquals(Protocol.Inventory.InventoryType.UNRECOGNIZED, msg.getInventoryType()); + + try { + handler.processMessage(peer, msg); + Assert.fail("Expected P2pException for unknown inventory type"); + } catch (P2pException e) { + Assert.assertEquals(P2pException.TypeEnum.BAD_MESSAGE, e.getType()); + } + } + @Test public void testRateLimiter() { List blockIds = new LinkedList<>(); diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/InventoryMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/InventoryMsgHandlerTest.java index 3d24ff2a4bf..9e2523dff2d 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/InventoryMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/InventoryMsgHandlerTest.java @@ -2,6 +2,7 @@ import static org.mockito.Mockito.mock; +import com.google.protobuf.ByteString; import java.lang.reflect.Field; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -18,6 +19,7 @@ import org.tron.core.net.message.adv.InventoryMessage; import org.tron.core.net.peer.PeerConnection; import org.tron.p2p.connection.Channel; +import org.tron.protos.Protocol.Inventory; import org.tron.protos.Protocol.Inventory.InventoryType; public class InventoryMsgHandlerTest { @@ -74,6 +76,30 @@ public void testDuplicateHashesRejected() throws Exception { } } + @Test + public void testUnknownInventoryTypeRejected() throws Exception { + InventoryMsgHandler handler = new InventoryMsgHandler(); + Args.setParam(new String[] {}, TestConstants.TEST_CONF); + + // craft an Inventory whose enum type holds an undefined value (2) + Inventory inv = Inventory.newBuilder() + .setTypeValue(2) + .addIds(ByteString.copyFrom(new byte[32])) + .build(); + InventoryMessage msg = new InventoryMessage(inv.toByteArray()); + Assert.assertEquals(InventoryType.UNRECOGNIZED, msg.getInventoryType()); + + PeerConnection peer = new PeerConnection(); + peer.setChannel(getChannel("1.0.0.5", 1000)); + + try { + handler.processMessage(peer, msg); + Assert.fail("Expected P2pException for unknown inventory type"); + } catch (P2pException e) { + Assert.assertEquals(P2pException.TypeEnum.BAD_MESSAGE, e.getType()); + } + } + private Channel getChannel(String host, int port) throws Exception { Channel channel = new Channel(); InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port); From 58f6e64bd8b7b3fd98af874c400032e6ccc83892 Mon Sep 17 00:00:00 2001 From: Asuka Date: Fri, 26 Jun 2026 16:09:25 +0800 Subject: [PATCH 50/57] fix(vm): isolate constant-call config, self-dispatch vote-witness cost (#6857) * fix(vm): self-dispatch vote-witness cost by proposal flag getVoteWitnessCost3 falls back to cost2 when Osaka is off, and cost2 to the legacy cost when energy adjustment is off. The energy then stays correct even if a stale cost function lingers in the shared jump table after a reorg, or is read by a constant call whose view has the proposal off. * fix(vm): isolate constant-call config view to prevent global pollution Constant calls bound to a lagging solidity/PBFT snapshot loaded their flags into the process-global VMConfig, racing block processing during a proposal's activation-to-solidification window and risking a fork. Route a constant call's config into a thread-local snapshot; the block/broadcast path keeps writing (and reading) the global. --- .../org/tron/core/actuator/VMActuator.java | 2 +- .../java/org/tron/core/vm/EnergyCost.java | 8 + .../org/tron/core/vm/config/ConfigLoader.java | 66 ++--- .../org/tron/core/vm/config/VMConfig.java | 226 ++++++++++-------- .../src/main/java/org/tron/core/Wallet.java | 11 +- .../runtime/vm/VMConfigIsolationTest.java | 108 +++++++++ .../runtime/vm/VoteWitnessCost3Test.java | 53 ++++ 7 files changed, 339 insertions(+), 135 deletions(-) create mode 100644 framework/src/test/java/org/tron/common/runtime/vm/VMConfigIsolationTest.java diff --git a/actuator/src/main/java/org/tron/core/actuator/VMActuator.java b/actuator/src/main/java/org/tron/core/actuator/VMActuator.java index f604013325e..d785951027b 100644 --- a/actuator/src/main/java/org/tron/core/actuator/VMActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/VMActuator.java @@ -120,7 +120,7 @@ public void validate(Object object) throws ContractValidateException { } // Load Config - ConfigLoader.load(context.getStoreFactory()); + ConfigLoader.load(context.getStoreFactory(), isConstantCall); // Warm up registry class OperationRegistry.init(); trx = context.getTrxCap().getInstance(); diff --git a/actuator/src/main/java/org/tron/core/vm/EnergyCost.java b/actuator/src/main/java/org/tron/core/vm/EnergyCost.java index 3641548b3e5..a2c4b59fdc5 100644 --- a/actuator/src/main/java/org/tron/core/vm/EnergyCost.java +++ b/actuator/src/main/java/org/tron/core/vm/EnergyCost.java @@ -365,6 +365,10 @@ public static long getVoteWitnessCost(Program program) { } public static long getVoteWitnessCost2(Program program) { + if (!VMConfig.allowEnergyAdjustment()) { + return getVoteWitnessCost(program); + } + Stack stack = program.getStack(); long oldMemSize = program.getMemSize(); DataWord amountArrayLength = stack.get(stack.size() - 1).clone(); @@ -388,6 +392,10 @@ public static long getVoteWitnessCost2(Program program) { } public static long getVoteWitnessCost3(Program program) { + if (!VMConfig.allowTvmOsaka()) { + return getVoteWitnessCost2(program); + } + Stack stack = program.getStack(); long oldMemSize = program.getMemSize(); BigInteger amountArrayLength = stack.get(stack.size() - 1).value(); diff --git a/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java b/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java index 881eb861bea..35480935742 100644 --- a/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java +++ b/actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java @@ -13,40 +13,48 @@ public class ConfigLoader { //only for unit test public static boolean disable = false; - public static void load(StoreFactory storeFactory) { + // isolate=true: a constant call bound to a non-HEAD (solidity/PBFT) snapshot installs its + // snapshot into a thread-local view instead of the process-wide global, so it cannot pollute + // the flags the block-processing path reads concurrently. + public static void load(StoreFactory storeFactory, boolean isolate) { if (!disable) { DynamicPropertiesStore ds = storeFactory.getChainBaseManager().getDynamicPropertiesStore(); VMConfig.setVmTrace(CommonParameter.getInstance().isVmTrace()); if (ds != null) { VMConfig.initVmHardFork(checkForEnergyLimit(ds)); - VMConfig.initAllowMultiSign(ds.getAllowMultiSign()); - VMConfig.initAllowTvmTransferTrc10(ds.getAllowTvmTransferTrc10()); - VMConfig.initAllowTvmConstantinople(ds.getAllowTvmConstantinople()); - VMConfig.initAllowTvmSolidity059(ds.getAllowTvmSolidity059()); - VMConfig.initAllowShieldedTRC20Transaction(ds.getAllowShieldedTRC20Transaction()); - VMConfig.initAllowTvmIstanbul(ds.getAllowTvmIstanbul()); - VMConfig.initAllowTvmFreeze(ds.getAllowTvmFreeze()); - VMConfig.initAllowTvmVote(ds.getAllowTvmVote()); - VMConfig.initAllowTvmLondon(ds.getAllowTvmLondon()); - VMConfig.initAllowTvmCompatibleEvm(ds.getAllowTvmCompatibleEvm()); - VMConfig.initAllowHigherLimitForMaxCpuTimeOfOneTx( - ds.getAllowHigherLimitForMaxCpuTimeOfOneTx()); - VMConfig.initAllowTvmFreezeV2(ds.supportUnfreezeDelay() ? 1 : 0); - VMConfig.initAllowOptimizedReturnValueOfChainId( - ds.getAllowOptimizedReturnValueOfChainId()); - VMConfig.initAllowDynamicEnergy(ds.getAllowDynamicEnergy()); - VMConfig.initDynamicEnergyThreshold(ds.getDynamicEnergyThreshold()); - VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor()); - VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor()); - VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai()); - VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment()); - VMConfig.initAllowStrictMath(ds.getAllowStrictMath()); - VMConfig.initAllowTvmCancun(ds.getAllowTvmCancun()); - VMConfig.initDisableJavaLangMath(ds.getConsensusLogicOptimization()); - VMConfig.initAllowTvmBlob(ds.getAllowTvmBlob()); - VMConfig.initAllowTvmSelfdestructRestriction(ds.getAllowTvmSelfdestructRestriction()); - VMConfig.initAllowTvmOsaka(ds.getAllowTvmOsaka()); - VMConfig.initAllowHardenResourceCalculation(ds.getAllowHardenResourceCalculation()); + VMConfig.Snapshot snapshot = new VMConfig.Snapshot(); + snapshot.allowMultiSign = ds.getAllowMultiSign() == 1; + snapshot.allowTvmTransferTrc10 = ds.getAllowTvmTransferTrc10() == 1; + snapshot.allowTvmConstantinople = ds.getAllowTvmConstantinople() == 1; + snapshot.allowTvmSolidity059 = ds.getAllowTvmSolidity059() == 1; + snapshot.allowShieldedTRC20Transaction = ds.getAllowShieldedTRC20Transaction() == 1; + snapshot.allowTvmIstanbul = ds.getAllowTvmIstanbul() == 1; + snapshot.allowTvmFreeze = ds.getAllowTvmFreeze() == 1; + snapshot.allowTvmVote = ds.getAllowTvmVote() == 1; + snapshot.allowTvmLondon = ds.getAllowTvmLondon() == 1; + snapshot.allowTvmCompatibleEvm = ds.getAllowTvmCompatibleEvm() == 1; + snapshot.allowHigherLimitForMaxCpuTimeOfOneTx = + ds.getAllowHigherLimitForMaxCpuTimeOfOneTx() == 1; + snapshot.allowTvmFreezeV2 = ds.supportUnfreezeDelay(); + snapshot.allowOptimizedReturnValueOfChainId = ds.getAllowOptimizedReturnValueOfChainId() == 1; + snapshot.allowDynamicEnergy = ds.getAllowDynamicEnergy() == 1; + snapshot.dynamicEnergyThreshold = ds.getDynamicEnergyThreshold(); + snapshot.dynamicEnergyIncreaseFactor = ds.getDynamicEnergyIncreaseFactor(); + snapshot.dynamicEnergyMaxFactor = ds.getDynamicEnergyMaxFactor(); + snapshot.allowTvmShanghai = ds.getAllowTvmShangHai() == 1; + snapshot.allowEnergyAdjustment = ds.getAllowEnergyAdjustment() == 1; + snapshot.allowStrictMath = ds.getAllowStrictMath() == 1; + snapshot.allowTvmCancun = ds.getAllowTvmCancun() == 1; + snapshot.disableJavaLangMath = ds.getConsensusLogicOptimization() == 1; + snapshot.allowTvmBlob = ds.getAllowTvmBlob() == 1; + snapshot.allowTvmSelfdestructRestriction = ds.getAllowTvmSelfdestructRestriction() == 1; + snapshot.allowTvmOsaka = ds.getAllowTvmOsaka() == 1; + snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1; + if (isolate) { + VMConfig.setLocalSnapshot(snapshot); + } else { + VMConfig.setGlobalSnapshot(snapshot); + } } } } diff --git a/common/src/main/java/org/tron/core/vm/config/VMConfig.java b/common/src/main/java/org/tron/core/vm/config/VMConfig.java index 94c1e50284e..304ced33698 100644 --- a/common/src/main/java/org/tron/core/vm/config/VMConfig.java +++ b/common/src/main/java/org/tron/core/vm/config/VMConfig.java @@ -13,57 +13,74 @@ public class VMConfig { @Setter private static boolean vmTrace = false; - private static boolean ALLOW_TVM_TRANSFER_TRC10 = false; - - private static boolean ALLOW_TVM_CONSTANTINOPLE = false; - - private static boolean ALLOW_MULTI_SIGN = false; - - private static boolean ALLOW_TVM_SOLIDITY_059 = false; - - private static boolean ALLOW_SHIELDED_TRC20_TRANSACTION = false; - - private static boolean ALLOW_TVM_ISTANBUL = false; - - private static boolean ALLOW_TVM_FREEZE = false; - - private static boolean ALLOW_TVM_VOTE = false; - - private static boolean ALLOW_TVM_LONDON = false; - - private static boolean ALLOW_TVM_COMPATIBLE_EVM = false; - - private static boolean ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX = false; - - private static boolean ALLOW_TVM_FREEZE_V2 = false; - - private static boolean ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID = false; - - private static boolean ALLOW_DYNAMIC_ENERGY = false; - - private static long DYNAMIC_ENERGY_THRESHOLD = 0L; - - private static long DYNAMIC_ENERGY_INCREASE_FACTOR = 0L; - - private static long DYNAMIC_ENERGY_MAX_FACTOR = 0L; - - private static boolean ALLOW_TVM_SHANGHAI = false; - - private static boolean ALLOW_ENERGY_ADJUSTMENT = false; - - private static boolean ALLOW_STRICT_MATH = false; - - private static boolean ALLOW_TVM_CANCUN = false; - - private static Boolean DISABLE_JAVA_LANG_MATH = false; - - private static boolean ALLOW_TVM_BLOB = false; - - private static boolean ALLOW_TVM_SELFDESTRUCT_RESTRICTION = false; - - private static boolean ALLOW_TVM_OSAKA = false; - - private static boolean ALLOW_HARDEN_RESOURCE_CALCULATION = false; + /** + * Snapshot of all chain/store-derived VM config flags. The block-processing (HEAD) path + * installs it as the process-wide {@link #globalSnapshot}; a constant call executing against a + * non-HEAD (solidity/PBFT) snapshot installs its own view into {@link #localSnapshot} so it never + * overwrites the flags the consensus path relies on. + */ + public static class Snapshot { + public boolean allowTvmTransferTrc10; + public boolean allowTvmConstantinople; + public boolean allowMultiSign; + public boolean allowTvmSolidity059; + public boolean allowShieldedTRC20Transaction; + public boolean allowTvmIstanbul; + public boolean allowTvmFreeze; + public boolean allowTvmVote; + public boolean allowTvmLondon; + public boolean allowTvmCompatibleEvm; + public boolean allowHigherLimitForMaxCpuTimeOfOneTx; + public boolean allowTvmFreezeV2; + public boolean allowOptimizedReturnValueOfChainId; + public boolean allowDynamicEnergy; + public long dynamicEnergyThreshold; + public long dynamicEnergyIncreaseFactor; + public long dynamicEnergyMaxFactor; + public boolean allowTvmShanghai; + public boolean allowEnergyAdjustment; + public boolean allowStrictMath; + public boolean allowTvmCancun; + public boolean disableJavaLangMath; + public boolean allowTvmBlob; + public boolean allowTvmSelfdestructRestriction; + public boolean allowTvmOsaka; + public boolean allowHardenResourceCalculation; + } + + // HEAD / block-processing config, written by the consensus path; read by everyone with no + // thread-local override. volatile so a wholesale install is safely published across threads. + private static volatile Snapshot globalSnapshot = new Snapshot(); + + // Per-thread override used only by constant calls bound to a non-HEAD (solidity/PBFT) snapshot. + private static final ThreadLocal localSnapshot = new ThreadLocal<>(); + + private static Snapshot current() { + Snapshot local = localSnapshot.get(); + return local != null ? local : globalSnapshot; + } + + /** + * Install the process-wide (HEAD / block-processing) config and drop any thread-local view. + */ + public static void setGlobalSnapshot(Snapshot snapshot) { + globalSnapshot = snapshot; + localSnapshot.remove(); + } + + /** + * Install a thread-local config view for a constant call executing against a non-HEAD snapshot. + */ + public static void setLocalSnapshot(Snapshot snapshot) { + localSnapshot.set(snapshot); + } + + /** + * Drop the thread-local config view so this thread falls back to the global config. + */ + public static void clearLocalSnapshot() { + localSnapshot.remove(); + } private VMConfig() { } @@ -80,108 +97,111 @@ public static void initVmHardFork(boolean pass) { CommonParameter.ENERGY_LIMIT_HARD_FORK = pass; } + // The init* setters below mutate the global (HEAD) config in place. They are kept for tests and + // legacy callers; production config loading goes through ConfigLoader -> setGlobalSnapshot, which + // publishes a fresh Snapshot wholesale via the volatile field. public static void initAllowMultiSign(long allow) { - ALLOW_MULTI_SIGN = allow == 1; + globalSnapshot.allowMultiSign = allow == 1; } public static void initAllowTvmTransferTrc10(long allow) { - ALLOW_TVM_TRANSFER_TRC10 = allow == 1; + globalSnapshot.allowTvmTransferTrc10 = allow == 1; } public static void initAllowTvmConstantinople(long allow) { - ALLOW_TVM_CONSTANTINOPLE = allow == 1; + globalSnapshot.allowTvmConstantinople = allow == 1; } public static void initAllowTvmSolidity059(long allow) { - ALLOW_TVM_SOLIDITY_059 = allow == 1; + globalSnapshot.allowTvmSolidity059 = allow == 1; } public static void initAllowShieldedTRC20Transaction(long allow) { - ALLOW_SHIELDED_TRC20_TRANSACTION = allow == 1; + globalSnapshot.allowShieldedTRC20Transaction = allow == 1; } public static void initAllowTvmIstanbul(long allow) { - ALLOW_TVM_ISTANBUL = allow == 1; + globalSnapshot.allowTvmIstanbul = allow == 1; } public static void initAllowTvmFreeze(long allow) { - ALLOW_TVM_FREEZE = allow == 1; + globalSnapshot.allowTvmFreeze = allow == 1; } public static void initAllowTvmVote(long allow) { - ALLOW_TVM_VOTE = allow == 1; + globalSnapshot.allowTvmVote = allow == 1; } public static void initAllowTvmLondon(long allow) { - ALLOW_TVM_LONDON = allow == 1; + globalSnapshot.allowTvmLondon = allow == 1; } public static void initAllowTvmCompatibleEvm(long allow) { - ALLOW_TVM_COMPATIBLE_EVM = allow == 1; + globalSnapshot.allowTvmCompatibleEvm = allow == 1; } public static void initAllowHigherLimitForMaxCpuTimeOfOneTx(long allow) { - ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX = allow == 1; + globalSnapshot.allowHigherLimitForMaxCpuTimeOfOneTx = allow == 1; } public static void initAllowTvmFreezeV2(long allow) { - ALLOW_TVM_FREEZE_V2 = allow == 1; + globalSnapshot.allowTvmFreezeV2 = allow == 1; } public static void initAllowOptimizedReturnValueOfChainId(long allow) { - ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID = allow == 1; + globalSnapshot.allowOptimizedReturnValueOfChainId = allow == 1; } public static void initAllowDynamicEnergy(long allow) { - ALLOW_DYNAMIC_ENERGY = allow == 1; + globalSnapshot.allowDynamicEnergy = allow == 1; } public static void initDynamicEnergyThreshold(long threshold) { - DYNAMIC_ENERGY_THRESHOLD = threshold; + globalSnapshot.dynamicEnergyThreshold = threshold; } public static void initDynamicEnergyIncreaseFactor(long increaseFactor) { - DYNAMIC_ENERGY_INCREASE_FACTOR = increaseFactor; + globalSnapshot.dynamicEnergyIncreaseFactor = increaseFactor; } public static void initDynamicEnergyMaxFactor(long maxFactor) { - DYNAMIC_ENERGY_MAX_FACTOR = maxFactor; + globalSnapshot.dynamicEnergyMaxFactor = maxFactor; } public static void initAllowTvmShangHai(long allow) { - ALLOW_TVM_SHANGHAI = allow == 1; + globalSnapshot.allowTvmShanghai = allow == 1; } public static void initAllowEnergyAdjustment(long allow) { - ALLOW_ENERGY_ADJUSTMENT = allow == 1; + globalSnapshot.allowEnergyAdjustment = allow == 1; } public static void initAllowStrictMath(long allow) { - ALLOW_STRICT_MATH = allow == 1; + globalSnapshot.allowStrictMath = allow == 1; } public static void initAllowTvmCancun(long allow) { - ALLOW_TVM_CANCUN = allow == 1; + globalSnapshot.allowTvmCancun = allow == 1; } public static void initDisableJavaLangMath(long allow) { - DISABLE_JAVA_LANG_MATH = allow == 1; + globalSnapshot.disableJavaLangMath = allow == 1; } public static void initAllowTvmBlob(long allow) { - ALLOW_TVM_BLOB = allow == 1; + globalSnapshot.allowTvmBlob = allow == 1; } public static void initAllowTvmSelfdestructRestriction(long allow) { - ALLOW_TVM_SELFDESTRUCT_RESTRICTION = allow == 1; + globalSnapshot.allowTvmSelfdestructRestriction = allow == 1; } public static void initAllowTvmOsaka(long allow) { - ALLOW_TVM_OSAKA = allow == 1; + globalSnapshot.allowTvmOsaka = allow == 1; } public static void initAllowHardenResourceCalculation(long allow) { - ALLOW_HARDEN_RESOURCE_CALCULATION = allow == 1; + globalSnapshot.allowHardenResourceCalculation = allow == 1; } public static boolean getEnergyLimitHardFork() { @@ -189,106 +209,106 @@ public static boolean getEnergyLimitHardFork() { } public static boolean allowTvmTransferTrc10() { - return ALLOW_TVM_TRANSFER_TRC10; + return current().allowTvmTransferTrc10; } public static boolean allowTvmConstantinople() { - return ALLOW_TVM_CONSTANTINOPLE; + return current().allowTvmConstantinople; } public static boolean allowMultiSign() { - return ALLOW_MULTI_SIGN; + return current().allowMultiSign; } public static boolean allowTvmSolidity059() { - return ALLOW_TVM_SOLIDITY_059; + return current().allowTvmSolidity059; } public static boolean allowShieldedTRC20Transaction() { - return ALLOW_SHIELDED_TRC20_TRANSACTION; + return current().allowShieldedTRC20Transaction; } public static boolean allowTvmIstanbul() { - return ALLOW_TVM_ISTANBUL; + return current().allowTvmIstanbul; } public static boolean allowTvmFreeze() { - return ALLOW_TVM_FREEZE; + return current().allowTvmFreeze; } public static boolean allowTvmVote() { - return ALLOW_TVM_VOTE; + return current().allowTvmVote; } public static boolean allowTvmLondon() { - return ALLOW_TVM_LONDON; + return current().allowTvmLondon; } public static boolean allowTvmCompatibleEvm() { - return ALLOW_TVM_COMPATIBLE_EVM; + return current().allowTvmCompatibleEvm; } public static boolean allowHigherLimitForMaxCpuTimeOfOneTx() { - return ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX; + return current().allowHigherLimitForMaxCpuTimeOfOneTx; } public static boolean allowTvmFreezeV2() { - return ALLOW_TVM_FREEZE_V2; + return current().allowTvmFreezeV2; } public static boolean allowOptimizedReturnValueOfChainId() { - return ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID; + return current().allowOptimizedReturnValueOfChainId; } public static boolean allowDynamicEnergy() { - return ALLOW_DYNAMIC_ENERGY; + return current().allowDynamicEnergy; } public static long getDynamicEnergyThreshold() { - return DYNAMIC_ENERGY_THRESHOLD; + return current().dynamicEnergyThreshold; } public static long getDynamicEnergyIncreaseFactor() { - return DYNAMIC_ENERGY_INCREASE_FACTOR; + return current().dynamicEnergyIncreaseFactor; } public static long getDynamicEnergyMaxFactor() { - return DYNAMIC_ENERGY_MAX_FACTOR; + return current().dynamicEnergyMaxFactor; } public static boolean allowTvmShanghai() { - return ALLOW_TVM_SHANGHAI; + return current().allowTvmShanghai; } public static boolean allowEnergyAdjustment() { - return ALLOW_ENERGY_ADJUSTMENT; + return current().allowEnergyAdjustment; } public static boolean allowStrictMath() { - return ALLOW_STRICT_MATH; + return current().allowStrictMath; } public static boolean allowTvmCancun() { - return ALLOW_TVM_CANCUN; + return current().allowTvmCancun; } public static boolean disableJavaLangMath() { - return DISABLE_JAVA_LANG_MATH; + return current().disableJavaLangMath; } public static boolean allowTvmBlob() { - return ALLOW_TVM_BLOB; + return current().allowTvmBlob; } public static boolean allowTvmSelfdestructRestriction() { - return ALLOW_TVM_SELFDESTRUCT_RESTRICTION; + return current().allowTvmSelfdestructRestriction; } public static boolean allowTvmOsaka() { - return ALLOW_TVM_OSAKA; + return current().allowTvmOsaka; } public static boolean allowHardenResourceCalculation() { - return ALLOW_HARDEN_RESOURCE_CALCULATION; + return current().allowHardenResourceCalculation; } } diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 079b8e6f3e9..ac54cb2b7ff 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -202,6 +202,7 @@ import org.tron.core.store.VotesStore; import org.tron.core.store.WitnessStore; import org.tron.core.utils.TransactionUtil; +import org.tron.core.vm.config.VMConfig; import org.tron.core.vm.program.Program; import org.tron.core.zen.ShieldedTRC20ParametersBuilder; import org.tron.core.zen.ShieldedTRC20ParametersBuilder.ShieldedTRC20ParametersType; @@ -3157,8 +3158,14 @@ public Transaction callConstantContract(TransactionCapsule trxCap, StoreFactory.getInstance(), true, false); VMActuator vmActuator = new VMActuator(true); - vmActuator.validate(context); - vmActuator.execute(context); + try { + vmActuator.validate(context); + vmActuator.execute(context); + } finally { + // constant call runs on a pooled RPC worker; drop its thread-local VM config view so it + // can never leak into a later (block/broadcast) execution on the same thread. + VMConfig.clearLocalSnapshot(); + } ProgramResult result = context.getProgramResult(); if (!isEstimating && result.getException() != null diff --git a/framework/src/test/java/org/tron/common/runtime/vm/VMConfigIsolationTest.java b/framework/src/test/java/org/tron/common/runtime/vm/VMConfigIsolationTest.java new file mode 100644 index 00000000000..845db6dd6af --- /dev/null +++ b/framework/src/test/java/org/tron/common/runtime/vm/VMConfigIsolationTest.java @@ -0,0 +1,108 @@ +package org.tron.common.runtime.vm; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.tron.core.vm.config.VMConfig; + +public class VMConfigIsolationTest { + + // Tests mutate the process-wide static VMConfig; snapshot it before each test and restore it + // after so this class never pollutes other VM tests sharing the same JVM fork (forkEvery=100). + private VMConfig.Snapshot savedGlobal; + + @Before + public void snapshotConfig() { + VMConfig.clearLocalSnapshot(); + savedGlobal = snapshotGlobal(); + } + + @After + public void restoreConfig() { + VMConfig.clearLocalSnapshot(); + VMConfig.setGlobalSnapshot(savedGlobal); + } + + /** + * A constant call's thread-local config view must not pollute the global config that the + * (concurrent) block-processing path reads. This is the core Problem-2 guarantee. + */ + @Test + public void testLocalConfigDoesNotPolluteGlobalAcrossThreads() throws InterruptedException { + VMConfig.initAllowTvmOsaka(1); // global (HEAD) view: activated + assertTrue(VMConfig.allowTvmOsaka()); // no thread-local -> reads global + + VMConfig.Snapshot local = new VMConfig.Snapshot(); + local.allowTvmOsaka = false; // simulate a not-yet-solidified snapshot + VMConfig.setLocalSnapshot(local); + + // this thread now sees its own (solidity) view... + assertFalse(VMConfig.allowTvmOsaka()); + + // ...but another thread (e.g. block processing) must still see the global HEAD value. + AtomicBoolean otherThreadSaw = new AtomicBoolean(false); + Thread t = new Thread(() -> otherThreadSaw.set(VMConfig.allowTvmOsaka())); + t.start(); + t.join(); + assertTrue("global config must be unaffected by another thread's local view", + otherThreadSaw.get()); + + // after dropping the local view, this thread falls back to the global value again. + VMConfig.clearLocalSnapshot(); + assertTrue(VMConfig.allowTvmOsaka()); + } + + /** + * A block/broadcast load (setGlobalSnapshot) must drop any thread-local view, so the consensus + * path can never read a constant call's leaked snapshot left on the same pooled worker thread. + */ + @Test + public void testSetGlobalConfigDropsLocalView() { + VMConfig.Snapshot local = new VMConfig.Snapshot(); + local.allowTvmOsaka = true; + VMConfig.setLocalSnapshot(local); + assertTrue(VMConfig.allowTvmOsaka()); + + VMConfig.Snapshot head = new VMConfig.Snapshot(); + head.allowTvmOsaka = false; + VMConfig.setGlobalSnapshot(head); + assertFalse("setGlobalSnapshot must drop the thread-local view", VMConfig.allowTvmOsaka()); + } + + // Deep-copy the current global config through the public getters (no thread-local set here, so + // the getters read the global) so @After can restore the exact prior state. + private static VMConfig.Snapshot snapshotGlobal() { + VMConfig.Snapshot snapshot = new VMConfig.Snapshot(); + snapshot.allowTvmTransferTrc10 = VMConfig.allowTvmTransferTrc10(); + snapshot.allowTvmConstantinople = VMConfig.allowTvmConstantinople(); + snapshot.allowMultiSign = VMConfig.allowMultiSign(); + snapshot.allowTvmSolidity059 = VMConfig.allowTvmSolidity059(); + snapshot.allowShieldedTRC20Transaction = VMConfig.allowShieldedTRC20Transaction(); + snapshot.allowTvmIstanbul = VMConfig.allowTvmIstanbul(); + snapshot.allowTvmFreeze = VMConfig.allowTvmFreeze(); + snapshot.allowTvmVote = VMConfig.allowTvmVote(); + snapshot.allowTvmLondon = VMConfig.allowTvmLondon(); + snapshot.allowTvmCompatibleEvm = VMConfig.allowTvmCompatibleEvm(); + snapshot.allowHigherLimitForMaxCpuTimeOfOneTx = VMConfig.allowHigherLimitForMaxCpuTimeOfOneTx(); + snapshot.allowTvmFreezeV2 = VMConfig.allowTvmFreezeV2(); + snapshot.allowOptimizedReturnValueOfChainId = VMConfig.allowOptimizedReturnValueOfChainId(); + snapshot.allowDynamicEnergy = VMConfig.allowDynamicEnergy(); + snapshot.dynamicEnergyThreshold = VMConfig.getDynamicEnergyThreshold(); + snapshot.dynamicEnergyIncreaseFactor = VMConfig.getDynamicEnergyIncreaseFactor(); + snapshot.dynamicEnergyMaxFactor = VMConfig.getDynamicEnergyMaxFactor(); + snapshot.allowTvmShanghai = VMConfig.allowTvmShanghai(); + snapshot.allowEnergyAdjustment = VMConfig.allowEnergyAdjustment(); + snapshot.allowStrictMath = VMConfig.allowStrictMath(); + snapshot.allowTvmCancun = VMConfig.allowTvmCancun(); + snapshot.disableJavaLangMath = VMConfig.disableJavaLangMath(); + snapshot.allowTvmBlob = VMConfig.allowTvmBlob(); + snapshot.allowTvmSelfdestructRestriction = VMConfig.allowTvmSelfdestructRestriction(); + snapshot.allowTvmOsaka = VMConfig.allowTvmOsaka(); + snapshot.allowHardenResourceCalculation = VMConfig.allowHardenResourceCalculation(); + return snapshot; + } +} diff --git a/framework/src/test/java/org/tron/common/runtime/vm/VoteWitnessCost3Test.java b/framework/src/test/java/org/tron/common/runtime/vm/VoteWitnessCost3Test.java index 75b11f4ab9d..2c7aa238033 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/VoteWitnessCost3Test.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/VoteWitnessCost3Test.java @@ -1,7 +1,9 @@ package org.tron.common.runtime.vm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -142,11 +144,14 @@ public void testLargeArrayLengthOverflow() { zeroOffset, largeLength, 0); boolean overflowCaught = false; + VMConfig.initAllowTvmOsaka(1); // cost3 self-dispatches; exercise the v3 (BigInteger) path try { EnergyCost.getVoteWitnessCost3(program); } catch (Program.OutOfMemoryException e) { // cost3 should detect memory overflow via checkMemorySize overflowCaught = true; + } finally { + VMConfig.initAllowTvmOsaka(0); } assertTrue("cost3 should throw memoryOverflow for huge array length", overflowCaught); } @@ -161,10 +166,13 @@ public void testLargeOffsetOverflow() { new DataWord(0), new DataWord(1), 0); boolean overflowCaught = false; + VMConfig.initAllowTvmOsaka(1); // cost3 self-dispatches; exercise the v3 (BigInteger) path try { EnergyCost.getVoteWitnessCost3(program); } catch (Program.OutOfMemoryException e) { overflowCaught = true; + } finally { + VMConfig.initAllowTvmOsaka(0); } assertTrue("cost3 should throw memoryOverflow for huge offset", overflowCaught); } @@ -239,4 +247,49 @@ public void testOperationRegistryWithOsaka() { VMConfig.initAllowTvmOsaka(0); } } + + @Test + public void testCost3FallsBackToCost2WhenOsakaOff() { + // cost3 self-dispatches: with osaka off it delegates to cost2, so a cost3 left in the shared + // jump table (e.g. read by a constant call whose view has osaka off) still charges v2 energy. + String maxHex = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; + DataWord huge = new DataWord(maxHex); + DataWord zero = new DataWord(0); + + VMConfig.initAllowTvmOsaka(0); + long viaCost3 = + EnergyCost.getVoteWitnessCost3(mockProgram(zero, new DataWord(1), zero, huge, 0)); + long viaCost2 = + EnergyCost.getVoteWitnessCost2(mockProgram(zero, new DataWord(1), zero, huge, 0)); + assertEquals("cost3 must equal cost2 when osaka is off", viaCost2, viaCost3); + + // sanity: with osaka on, cost3 instead runs v3 and detects the overflow that cost2 wraps away. + VMConfig.initAllowTvmOsaka(1); + try { + EnergyCost.getVoteWitnessCost3(mockProgram(zero, new DataWord(1), zero, huge, 0)); + fail("cost3 with osaka on must overflow-throw on the huge length"); + } catch (Program.OutOfMemoryException expected) { + // expected + } finally { + VMConfig.initAllowTvmOsaka(0); + } + } + + @Test + public void testCost2FallsBackToLegacyWhenEnergyAdjustmentOff() { + // cost2 self-dispatches: with energyAdjustment off it delegates to the legacy cost. + VMConfig.initAllowEnergyAdjustment(0); + try { + long viaCost2 = EnergyCost.getVoteWitnessCost2(mockProgram(0, 2, 128, 2, 0)); + long viaLegacy = EnergyCost.getVoteWitnessCost(mockProgram(0, 2, 128, 2, 0)); + assertEquals("cost2 must equal legacy cost when energyAdjustment is off", + viaLegacy, viaCost2); + + // sanity: with energyAdjustment on, cost2 differs from the legacy cost for this input. + VMConfig.initAllowEnergyAdjustment(1); + assertNotEquals(viaLegacy, EnergyCost.getVoteWitnessCost2(mockProgram(0, 2, 128, 2, 0))); + } finally { + VMConfig.initAllowEnergyAdjustment(1); + } + } } From f15c1ea741d6e48d49b8d703f01eb34d001c567e Mon Sep 17 00:00:00 2001 From: Asuka Date: Sat, 27 Jun 2026 08:52:45 +0800 Subject: [PATCH 51/57] perf(vm): hoist allowDynamicEnergy out of the opcode loop (#6858) --- actuator/src/main/java/org/tron/core/vm/VM.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/actuator/src/main/java/org/tron/core/vm/VM.java b/actuator/src/main/java/org/tron/core/vm/VM.java index b1d7b027601..585c52b0ce0 100644 --- a/actuator/src/main/java/org/tron/core/vm/VM.java +++ b/actuator/src/main/java/org/tron/core/vm/VM.java @@ -23,8 +23,10 @@ public static void play(Program program, JumpTable jumpTable) { try { long factor = DYNAMIC_ENERGY_FACTOR_DECIMAL; long energyUsage = 0L; + // hoist once per execution: avoids a per-opcode VMConfig.current() thread-local lookup + final boolean allowDynamicEnergy = VMConfig.allowDynamicEnergy(); - if (VMConfig.allowDynamicEnergy()) { + if (allowDynamicEnergy) { factor = program.updateContextContractFactor(); } @@ -47,7 +49,7 @@ public static void play(Program program, JumpTable jumpTable) { String opName = Op.getNameOf(op.getOpcode()); /* spend energy before execution */ long energy = op.getEnergyCost(program); - if (VMConfig.allowDynamicEnergy()) { + if (allowDynamicEnergy) { long actualEnergy = energy; // CALL Ops have special calculation on energy. if (CALL_OPS.contains(op.getOpcode())) { @@ -101,7 +103,7 @@ public static void play(Program program, JumpTable jumpTable) { } } - if (VMConfig.allowDynamicEnergy()) { + if (allowDynamicEnergy) { program.addContextContractUsage(energyUsage); } From 839dea8c6801e208e77489b475fcded9d4e71eb4 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 30 Jun 2026 10:23:24 +0800 Subject: [PATCH 52/57] update libp2p from v2.2.7 to v2.2.8 (#6859) --- common/build.gradle | 3 +-- gradle/verification-metadata.xml | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/common/build.gradle b/common/build.gradle index 4309d3dc69a..3fc955f9add 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -21,8 +21,7 @@ dependencies { api 'org.aspectj:aspectjrt:1.9.8' api 'org.aspectj:aspectjweaver:1.9.8' api 'org.aspectj:aspectjtools:1.9.8' - api group: 'com.github.tronprotocol', name: 'libp2p', version: 'release-v2.2.8-SNAPSHOT',{ - //api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.7',{ + api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.8',{ exclude group: 'io.grpc', module: 'grpc-context' exclude group: 'io.grpc', module: 'grpc-core' exclude group: 'io.grpc', module: 'grpc-netty' diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 8c55e3b52b0..832d2728f0b 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -1084,15 +1084,15 @@ - - - + + + - - + + - - + + From b5624497c142db9203e3a3fe39b6a9374426a017 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Thu, 2 Jul 2026 16:53:41 +0800 Subject: [PATCH 53/57] feat(http): treat null JsonFormat fields as absent (#6863) 1. Treat known protobuf fields with literal null as absent so HTTP APIs recover the 4.8.1 fastjson-compatible behavior for null object fields. 2. This relaxes JSON-RPC buildTransaction ABI handling because that path now accepts null ABI fields instead of returning invalid abi. 3. TRON's JsonFormat now aligns more closely with protobuf Java JsonFormat: field-level null is treated as absent, while null elements inside repeated arrays remain invalid. --- .../tron/core/services/http/JsonFormat.java | 4 ++ .../tron/core/jsonrpc/JsonrpcServiceTest.java | 46 +++++++++++++++++++ .../http/DeployContractServletTest.java | 27 +++++++++++ .../services/http/GetAccountServletTest.java | 14 ++++++ .../core/services/http/JsonFormatTest.java | 33 +++++++++++++ .../org/tron/core/services/http/UtilTest.java | 45 ++++++++++++++++++ .../src/test/java/org/tron/json/JsonTest.java | 17 +++++++ 7 files changed, 186 insertions(+) diff --git a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java index b2b2eeec8d7..e6ccb4e4d17 100644 --- a/framework/src/main/java/org/tron/core/services/http/JsonFormat.java +++ b/framework/src/main/java/org/tron/core/services/http/JsonFormat.java @@ -620,6 +620,10 @@ protected static void mergeField(Tokenizer tokenizer, if (field != null) { tokenizer.consume(":"); + // Match protobuf JsonFormat: a field whose value is null is treated as absent. + if (tokenizer.tryConsume("null")) { + return; + } boolean array = tokenizer.tryConsume("["); if (array) { diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 6ef74ce2a1c..75bb59d2e5a 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -1488,6 +1488,52 @@ public void testBuildTransactionTransfer() { } } + @Test + public void testBuildCreateSmartContractAcceptsNullAbiOutputsOverHttp() { + fullNodeJsonRpcHttpService.start(); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + JsonObject buildArgs = new JsonObject(); + buildArgs.addProperty("from", "0xabd4b9367799eaa3197fecb144eb71de1e049abc"); + buildArgs.addProperty("data", "608060405234801561001057600080fd5b50"); + buildArgs.addProperty("gas", "0x3b9aca00"); + buildArgs.addProperty("abi", "[{\"inputs\":[],\"name\":\"test\",\"outputs\":null," + + "\"type\":\"function\"}]"); + JsonArray params = new JsonArray(); + params.add(buildArgs); + JsonObject requestBody = new JsonObject(); + requestBody.addProperty("jsonrpc", "2.0"); + requestBody.addProperty("method", "buildTransaction"); + requestBody.add("params", params); + requestBody.addProperty("id", 1); + + HttpPost httpPost = new HttpPost("http://127.0.0.1:" + + CommonParameter.getInstance().getJsonRpcHttpFullNodePort() + "/jsonrpc"); + httpPost.addHeader("Content-Type", "application/json"); + httpPost.setEntity(new StringEntity(requestBody.toString())); + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + String resp = EntityUtils.toString(response.getEntity()); + JSONObject json = JSON.parseObject(resp); + Assert.assertNull(resp, json.getJSONObject("error")); + JSONObject tx = json.getJSONObject("result").getJSONObject("transaction"); + Assert.assertNotNull("transaction must be a JSON object", tx); + + JSONArray contracts = tx.getJSONObject("raw_data").getJSONArray("contract"); + Assert.assertEquals(1, contracts.size()); + JSONObject contract = contracts.getJSONObject(0); + Assert.assertEquals("CreateSmartContract", contract.getString("type")); + JSONObject value = contract.getJSONObject("parameter").getJSONObject("value"); + JSONObject abi = value.getJSONObject("new_contract").getJSONObject("abi"); + JSONArray entrys = abi.getJSONArray("entrys"); + Assert.assertEquals(1, entrys.size()); + Assert.assertFalse(entrys.getJSONObject(0).containsKey("outputs")); + } + } catch (Exception e) { + Assert.fail(e.getMessage()); + } finally { + fullNodeJsonRpcHttpService.stop(); + } + } + @Test public void testBuildTransactionRejectsDeeplyNestedAbi() { // A deeply nested ABI must surface as invalid-params (-32602), not as a generic diff --git a/framework/src/test/java/org/tron/core/services/http/DeployContractServletTest.java b/framework/src/test/java/org/tron/core/services/http/DeployContractServletTest.java index 83fb64880c3..703f278c890 100644 --- a/framework/src/test/java/org/tron/core/services/http/DeployContractServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/DeployContractServletTest.java @@ -55,4 +55,31 @@ && addressEquals(((CreateSmartContract) c).getOwnerAddress(), eq(Protocol.Transaction.Contract.ContractType.CreateSmartContract)); assertTransactionResponse(response); } + + @Test + public void testDeployContractOmitsNullAbiOutputs() throws Exception { + String jsonParam = "{" + + "\"owner_address\": \"4199357684BC659F5166046B56C95A0E99F1265CD1\"," + + "\"name\": \"TestContract\"," + + "\"abi\": [{\"inputs\":[],\"name\":\"test\",\"outputs\":null," + + "\"type\":\"function\"}]," + + "\"bytecode\": \"608060405234801561001057600080fd5b50\"," + + "\"fee_limit\": 1000000000," + + "\"call_value\": 0," + + "\"consume_user_resource_percent\": 100," + + "\"origin_energy_limit\": 10000000" + + "}"; + MockHttpServletRequest request = postRequest(jsonParam); + + MockHttpServletResponse response = newResponse(); + servlet.doPost(request, response); + assertEquals(200, response.getStatus()); + verify(wallet).createTransactionCapsule( + argThat(c -> c instanceof CreateSmartContract + && ((CreateSmartContract) c).getNewContract().getAbi().getEntrysCount() == 1 + && ((CreateSmartContract) c).getNewContract().getAbi().getEntrys(0) + .getOutputsCount() == 0), + eq(Protocol.Transaction.Contract.ContractType.CreateSmartContract)); + assertTransactionResponse(response); + } } diff --git a/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java index 31cfc174a77..1c1d42c9a5c 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetAccountServletTest.java @@ -46,6 +46,20 @@ public void testGetAccountPost() throws Exception { assertTrue("Should contain address", content.contains("address")); } + @Test + public void testGetAccountPostNullAddressKeepsDefault() throws Exception { + MockHttpServletRequest request = postRequest("{\"address\": null}"); + + MockHttpServletResponse response = newResponse(); + servlet.doPost(request, response); + assertEquals(200, response.getStatus()); + verify(wallet).getAccount(argThat(req -> req != null + && req.getAddress().equals(ByteString.EMPTY))); + String content = response.getContentAsString(); + assertFalse("Should not contain error", content.contains("\"Error\"")); + assertTrue("Should contain address", content.contains("address")); + } + @Test public void testGetAccountGet() throws Exception { MockHttpServletRequest request = getRequest("address", addrStr); diff --git a/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java b/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java index e38af6ead28..46d1743c5b9 100644 --- a/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java +++ b/framework/src/test/java/org/tron/core/services/http/JsonFormatTest.java @@ -1,6 +1,7 @@ package org.tron.core.services.http; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -19,6 +20,9 @@ import org.mockito.Mockito; import org.tron.core.Constant; import org.tron.protos.Protocol; +import org.tron.protos.contract.ProposalContract.ProposalCreateContract; +import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract; +import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry; public class JsonFormatTest { @After @@ -280,6 +284,25 @@ public void testTrailingCommaInKnownRepeatedField() throws Exception { assertEquals(ByteString.copyFrom(new byte[] {1}), proposal.getApprovals(1)); } + @Test + public void testKnownFieldNullIsSkipped() throws Exception { + Protocol.HelloMessage.Builder hello = Protocol.HelloMessage.newBuilder(); + JsonFormat.merge("{\"address\":null}", hello, false); + assertEquals(ByteString.EMPTY, hello.getAddress()); + + Entry.Builder entry = Entry.newBuilder(); + JsonFormat.merge("{\"outputs\":null}", entry, false); + assertEquals(0, entry.getOutputsCount()); + + CreateSmartContract.Builder contract = CreateSmartContract.newBuilder(); + JsonFormat.merge("{\"new_contract\":null}", contract, false); + assertFalse(contract.hasNewContract()); + + ProposalCreateContract.Builder proposal = ProposalCreateContract.newBuilder(); + JsonFormat.merge("{\"parameters\":null}", proposal, false); + assertTrue(proposal.getParametersMap().isEmpty()); + } + @Test public void testKnownRepeatedPrimitiveFieldRejectsNestedArray() { Protocol.Proposal.Builder builder = Protocol.Proposal.newBuilder(); @@ -300,6 +323,16 @@ public void testKnownRepeatedMessageFieldRejectsNestedArray() { assertTrue(e.getMessage().contains("Expected \"{\".")); } + @Test + public void testKnownRepeatedMessageFieldRejectsNullElement() { + Entry.Builder builder = Entry.newBuilder(); + + JsonFormat.ParseException e = assertThrows(JsonFormat.ParseException.class, + () -> JsonFormat.merge("{\"outputs\":[null]}", builder, false)); + + assertTrue(e.getMessage().contains("Expected \"{\".")); + } + @Test public void testMissingFieldRejectsTrailingCommaInNestedObject() { Protocol.HelloMessage.Builder builder = Protocol.HelloMessage.newBuilder(); diff --git a/framework/src/test/java/org/tron/core/services/http/UtilTest.java b/framework/src/test/java/org/tron/core/services/http/UtilTest.java index 49b8f848e3a..c619fd0de54 100644 --- a/framework/src/test/java/org/tron/core/services/http/UtilTest.java +++ b/framework/src/test/java/org/tron/core/services/http/UtilTest.java @@ -17,6 +17,7 @@ import org.tron.json.JSONObject; import org.tron.protos.Protocol; import org.tron.protos.Protocol.Transaction; +import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract; public class UtilTest extends BaseTest { @@ -191,6 +192,50 @@ public void testPackTransaction() { Assert.assertNotNull(txSignWeight); } + @Test + public void testPackCreateSmartContractOmitsNullAbiOutputs() throws Exception { + String strTransaction = "{\n" + + " \"visible\": false,\n" + + " \"raw_data\": {\n" + + " \"contract\": [\n" + + " {\n" + + " \"parameter\": {\n" + + " \"value\": {\n" + + " \"owner_address\":\"41c076305e35aea1fe45a772fcaaab8a36e87bdb55\"," + + " \"new_contract\": {\n" + + " \"origin_address\":" + + " \"41c076305e35aea1fe45a772fcaaab8a36e87bdb55\"," + + " \"name\":\"TestContract\"," + + " \"abi\": {\n" + + " \"entrys\": [\n" + + " {\"inputs\":[],\"name\":\"test\"," + + " \"outputs\":null,\"type\":\"function\"}\n" + + " ]\n" + + " }\n" + + " }\n" + + " },\n" + + " \"type_url\":" + + " \"type.googleapis.com/protocol.CreateSmartContract\"\n" + + " },\n" + + " \"type\": \"CreateSmartContract\"\n" + + " }\n" + + " ],\n" + + " \"ref_block_bytes\": \"d8ed\",\n" + + " \"ref_block_hash\": \"2e066c3259e756f5\",\n" + + " \"expiration\": 1651906644000,\n" + + " \"timestamp\": 1651906586162\n" + + " }\n" + + "}"; + + Transaction transaction = Util.packTransaction(strTransaction, false); + Assert.assertNotNull(transaction); + Assert.assertEquals(1, transaction.getRawData().getContractCount()); + CreateSmartContract contract = transaction.getRawData().getContract(0) + .getParameter().unpack(CreateSmartContract.class); + Assert.assertEquals(1, contract.getNewContract().getAbi().getEntrysCount()); + Assert.assertEquals(0, contract.getNewContract().getAbi().getEntrys(0).getOutputsCount()); + } + private Transaction buildTooManySigsTransaction() { String strTransaction = "{\n" + " \"visible\": false,\n" diff --git a/framework/src/test/java/org/tron/json/JsonTest.java b/framework/src/test/java/org/tron/json/JsonTest.java index eb9fed8ff2c..f430188611d 100644 --- a/framework/src/test/java/org/tron/json/JsonTest.java +++ b/framework/src/test/java/org/tron/json/JsonTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertTrue; import com.fasterxml.jackson.core.StreamReadConstraints; +import com.fasterxml.jackson.databind.node.NullNode; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; @@ -158,6 +159,22 @@ public void testToJSONString() { assertEquals("\"hi\"", JSON.toJSONString("hi", true)); } + @Test + public void testExplicitNullNodeSerializationIsPreserved() { + JSONObject parsedNull = JSON.parseObject("{\"a\":null,\"b\":1}"); + assertTrue(parsedNull.containsKey("a")); + assertNull(parsedNull.get("a")); + assertEquals("{\"a\":null,\"b\":1}", parsedNull.toJSONString()); + + JSONObject explicitNull = new JSONObject().put("a", NullNode.getInstance()).put("b", 1); + assertTrue(explicitNull.containsKey("a")); + assertEquals("{\"a\":null,\"b\":1}", explicitNull.toJSONString()); + + explicitNull.put("a", (Object) null); + assertFalse(explicitNull.containsKey("a")); + assertEquals("{\"b\":1}", explicitNull.toJSONString()); + } + @Test public void testJsonObjectGetters() { JSONObject o = JSON.parseObject( From 7ab89456e5c604d11a72e4187199a87ba0ec756e Mon Sep 17 00:00:00 2001 From: xxo1_shine Date: Thu, 2 Jul 2026 17:24:59 +0800 Subject: [PATCH 54/57] fix(db): re-verify all transaction signatures on fork switch (#6864) The switchFork new-branch apply loop only re-validated each block's witness signature; the transactions inside were applied through the isVerified cache. That flag caches a signature-verification result computed against a specific account-permission state, but a fork switch re-applies blocks on a rewound, diverged chain state where those permissions may have changed. Trusting the stale flag lets a transaction skip verification and be accepted with a signature no longer valid under the fork-chain state. - Manager.switchFork: clear isVerified on every transaction of each block on the branch being switched to, before applyBlock, forcing full signature re-validation against the fork-chain state. The switch-back path (original main branch) is left untouched: it reproduces the exact original state, so its cached verifications stay valid and resetting them would only cost perf. - ManagerTest: add switchForkShouldResetTransactionSignVerifiedOnNewBranch, which drives a real reorg and asserts the new branch's transaction gets its cached verification flag cleared (fails without the fix). --- .../main/java/org/tron/core/db/Manager.java | 7 ++ .../java/org/tron/core/db/ManagerTest.java | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index eab76db9e3f..9d7a7c979b9 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1146,6 +1146,13 @@ private void switchFork(BlockCapsule newHead) throw new ValidateSignatureException( "switch fork: block " + item.getBlk().getNum() + " signature invalid"); } + // The new branch is applied on a rewound, diverged state where account permissions + // may have changed, so a cached signature-verification result is no longer + // trustworthy. Clear it to force every transaction to re-validate its signature + // against the fork-chain state. + for (TransactionCapsule tx : item.getBlk().getTransactions()) { + tx.setVerified(false); + } applyBlock(item.getBlk().setSwitch(true)); tmpSession.commit(); } catch (AccountResourceInsufficientException diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index b31af7557fe..958a132fbbf 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -3,9 +3,11 @@ import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.tron.common.utils.Commons.adjustAssetBalanceV2; import static org.tron.common.utils.Commons.adjustTotalShieldedPoolValue; @@ -1771,6 +1773,77 @@ public void switchForkShouldPostFullNodeFilterForNewBranch() throws Exception { hasBlockFilterCapsule(queue, b2)); } + /** + * A fork switch re-applies the new branch on a rewound, diverged state, so any signature + * verification cached on those transactions (isVerified) must be cleared to force + * re-validation against the fork-chain state. Drives a real reorg and asserts that switchFork + * resets isVerified on the transactions of the branch it switches to. + */ + @Test + public void switchForkShouldResetTransactionSignVerifiedOnNewBranch() throws Exception { + // bootstrap a head with a known witness + String key = PublicMethod.getRandomPrivateKey(); + byte[] privateKey = ByteArray.fromHexString(key); + final ECKey ecKey = ECKey.fromPrivate(privateKey); + byte[] address = ecKey.getAddress(); + ByteString addressByte = ByteString.copyFrom(address); + chainManager.getAccountStore().put(addressByte.toByteArray(), + new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build())); + WitnessCapsule witnessCapsule = new WitnessCapsule(addressByte); + chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>()); + chainManager.addWitness(addressByte); + chainManager.getWitnessStore().put(address, witnessCapsule); + Block block = blockGenerate.getSignedBlock( + witnessCapsule.getAddress(), 1533529947843L, privateKey); + dbManager.pushBlock(new BlockCapsule(block)); + + Map keys = addTestWitnessAndAccount(); + keys.put(addressByte, key); + + // fund an owner; transfers go owner -> witness 'address' (an existing account) + ECKey ownerKey = new ECKey(Utils.getRandom()); + byte[] owner = ownerKey.getAddress(); + AccountCapsule ownerAccount = new AccountCapsule( + Protocol.Account.newBuilder().setAddress(ByteString.copyFrom(owner)).build()); + ownerAccount.setBalance(1_000_000_000L); + chainManager.getAccountStore().put(owner, ownerAccount); + + long t = 1533529947843L; + long base = chainManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber(); + long expiration = t + 1_000_000L; + + // common ancestor P (empty) — fork point and tapos reference + BlockCapsule p = createTestBlockCapsule(t + 3000, base + 1, + chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), keys); + dbManager.pushBlock(p); + + // old branch: A extends P via the normal path and becomes head + BlockCapsule a = blockWithTransfer(t + 6000, base + 2, p.getBlockId().getByteString(), keys, + transfer(owner, address, 1L, p, expiration)); + dbManager.pushBlock(a); + Assert.assertEquals("control: head should be A after normal extend", + a.getBlockId(), chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash()); + + // heavier competing branch P -> B1 -> B2 forces switchFork; spy the tx on the branch we + // switch to and pre-mark it verified to mimic a stale cache computed on a different state + BlockCapsule b1 = blockWithTransfer(t + 6001, base + 2, p.getBlockId().getByteString(), keys, + transfer(owner, address, 2L, p, expiration)); + dbManager.pushBlock(b1); // num <= head -> kept in khaosDb, no switch yet + + TransactionCapsule forkTx = transfer(owner, address, 3L, p, expiration); + forkTx.setVerified(true); + TransactionCapsule spyTx = spy(forkTx); + BlockCapsule b2 = blockWithTransfer(t + 9000, base + 3, b1.getBlockId().getByteString(), keys, + spyTx); + dbManager.pushBlock(b2); // num > head & parent != head -> triggers switchFork + + Assert.assertEquals("reorg must switch the canonical head to the competing branch (B2)", + b2.getBlockId(), chainManager.getDynamicPropertiesStore().getLatestBlockHeaderHash()); + // switchFork must clear the cached verification flag on the new branch's transaction so it + // re-validates against the fork-chain state + verify(spyTx, atLeastOnce()).setVerified(false); + } + private TransactionCapsule transfer(byte[] owner, byte[] to, long amount, BlockCapsule refBlock, long expiration) { TransferContract contract = TransferContract.newBuilder() From bc6b26f344be53a9fafc3f0e889f03b028b64ef8 Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Wed, 8 Jul 2026 14:53:58 +0800 Subject: [PATCH 55/57] fix(jsonrpc): don't print stacktrace in jsonrpc when request overload (#6867) --- .../core/services/filter/HttpInterceptor.java | 8 +++- .../services/http/RateLimiterServlet.java | 5 ++- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 16 +++---- .../services/filter/HttpInterceptorTest.java | 44 +++++++++++++++++++ .../services/http/RateLimiterServletTest.java | 30 +++++++++++++ 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 framework/src/test/java/org/tron/core/services/filter/HttpInterceptorTest.java diff --git a/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java b/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java index 2ff8a5ad321..ed20630b780 100644 --- a/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java +++ b/framework/src/main/java/org/tron/core/services/filter/HttpInterceptor.java @@ -1,6 +1,5 @@ package org.tron.core.services.filter; -import com.google.common.base.Strings; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -9,6 +8,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.http.BadMessageException; +import org.eclipse.jetty.http.HttpStatus; import org.tron.common.prometheus.MetricKeys; import org.tron.common.prometheus.MetricLabels; import org.tron.common.prometheus.Metrics; @@ -66,6 +67,10 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha } MetricsUtil.meterMark(MetricsKey.NET_API_QPS, 1); MetricsUtil.meterMark(MetricsKey.NET_API_FAIL_QPS, 1); + if (e instanceof BadMessageException + && ((BadMessageException) e).getCode() == HttpStatus.PAYLOAD_TOO_LARGE_413) { + throw (BadMessageException) e; + } } } @@ -75,4 +80,3 @@ public void destroy() { } - diff --git a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java index f488c32df4c..b5ae7d58623 100644 --- a/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.http.BadMessageException; import org.springframework.beans.factory.annotation.Autowired; import org.tron.common.parameter.RateLimiterInitialization; import org.tron.common.prometheus.MetricKeys; @@ -133,7 +134,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) resp.getWriter() .println(Util.printErrorMsg(new IllegalAccessException("lack of computing resources"))); } - } catch (ServletException | IOException e) { + } catch (ServletException | IOException | BadMessageException e) { throw e; } catch (Exception unexpected) { logger.error("Http Api {}, Method:{}. Error:", url, req.getMethod(), unexpected); @@ -149,4 +150,4 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) } } } -} \ No newline at end of file +} diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 75bb59d2e5a..e8d14ace060 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -26,6 +26,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.bouncycastle.util.encoders.Hex; +import org.eclipse.jetty.http.HttpStatus; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -1572,8 +1573,7 @@ public void testWeb3ClientVersion() { * Verifies SizeLimitHandler integration with the real JsonRpcServlet + jsonrpc4j stack. * * Covers: normal request no regression, Content-Length oversized 413, - * and chunked oversized handled gracefully (body truncated, 200 + empty body - * because jsonrpc4j absorbs the BadMessageException). + * and chunked oversized 413 during streaming body reads. */ @Test public void testJsonRpcSizeLimitIntegration() { @@ -1609,11 +1609,11 @@ public void testJsonRpcSizeLimitIntegration() { overPost.setEntity(new StringEntity( new String(new char[(int) testLimit + 1]).replace('\0', 'x'))); resp = httpClient.execute(overPost); - Assert.assertEquals(413, resp.getStatusLine().getStatusCode()); + Assert.assertEquals(HttpStatus.PAYLOAD_TOO_LARGE_413, + resp.getStatusLine().getStatusCode()); resp.close(); - // Chunked oversized -> BadMessageException thrown during body read, - // absorbed by jsonrpc4j catch(Exception) -> 200 with empty body. + // Chunked oversized -> BadMessageException thrown during body read. // Body read IS truncated at the limit - OOM protection effective. byte[] chunkedData = new String(new char[(int) testLimit * 2]) .replace('\0', 'x').getBytes("UTF-8"); @@ -1621,10 +1621,8 @@ public void testJsonRpcSizeLimitIntegration() { chunkedPost.setEntity(new InputStreamEntity( new ByteArrayInputStream(chunkedData), -1)); resp = httpClient.execute(chunkedPost); - Assert.assertEquals(200, resp.getStatusLine().getStatusCode()); - body = EntityUtils.toString(resp.getEntity()); - Assert.assertTrue("Chunked oversized should return empty body" - + " (jsonrpc4j absorbs BadMessageException)", body.isEmpty()); + Assert.assertEquals(HttpStatus.PAYLOAD_TOO_LARGE_413, + resp.getStatusLine().getStatusCode()); resp.close(); } } catch (Exception e) { diff --git a/framework/src/test/java/org/tron/core/services/filter/HttpInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/HttpInterceptorTest.java new file mode 100644 index 00000000000..b293e8047b5 --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/filter/HttpInterceptorTest.java @@ -0,0 +1,44 @@ +package org.tron.core.services.filter; + +import static org.junit.Assert.assertThrows; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import org.eclipse.jetty.http.BadMessageException; +import org.eclipse.jetty.http.HttpStatus; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +public class HttpInterceptorTest { + + private final HttpInterceptor interceptor = new HttpInterceptor(); + + @Test + public void testOversizedBadMessagePropagates() { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/jsonrpc"); + request.setServletPath("/jsonrpc"); + MockHttpServletResponse response = new MockHttpServletResponse(); + FilterChain chain = (req, resp) -> { + throw new BadMessageException(HttpStatus.PAYLOAD_TOO_LARGE_413, + "Request body is too large"); + }; + + BadMessageException e = assertThrows(BadMessageException.class, + () -> interceptor.doFilter(request, response, chain)); + + org.junit.Assert.assertEquals(HttpStatus.PAYLOAD_TOO_LARGE_413, e.getCode()); + } + + @Test + public void testNonOversizedExceptionIsStillSwallowed() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/jsonrpc"); + request.setServletPath("/jsonrpc"); + MockHttpServletResponse response = new MockHttpServletResponse(); + FilterChain chain = (req, resp) -> { + throw new ServletException("expected"); + }; + + interceptor.doFilter(request, response, chain); + } +} diff --git a/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java b/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java index 8cca558d151..26826c5709d 100644 --- a/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/RateLimiterServletTest.java @@ -16,6 +16,8 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.BadMessageException; +import org.eclipse.jetty.http.HttpStatus; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -69,6 +71,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) { } } + static class OversizedRequestServlet extends RateLimiterServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) { + throw new BadMessageException(HttpStatus.PAYLOAD_TOO_LARGE_413, + "Request body is too large"); + } + } + /** * GlobalRateLimiter's static initializer calls Args.getInstance().getRateLimiterGlobalQps(). * Without Args being initialized the default QPS is 0, causing RateLimiter.create(0) to throw. @@ -250,4 +260,24 @@ public void testNullRateLimiterConsultsOnlyGlobal() throws Exception { globalMock.verify(() -> GlobalRateLimiter.acquirePermit(any()), times(1)); } } + + @Test + public void testOversizedRequestBadMessagePropagates() throws Exception { + OversizedRequestServlet oversizedServlet = new OversizedRequestServlet(); + Field f = RateLimiterServlet.class.getDeclaredField("container"); + f.setAccessible(true); + f.set(oversizedServlet, container); + MockHttpServletRequest postRequest = new MockHttpServletRequest("POST", "/jsonrpc"); + postRequest.setRemoteAddr("10.0.0.1"); + postRequest.setServletPath("/jsonrpc"); + + try (MockedStatic globalMock = mockStatic(GlobalRateLimiter.class)) { + globalMock.when(() -> GlobalRateLimiter.acquirePermit(any())).thenReturn(true); + + BadMessageException e = assertThrows(BadMessageException.class, + () -> oversizedServlet.service(postRequest, response)); + + assertEquals(HttpStatus.PAYLOAD_TOO_LARGE_413, e.getCode()); + } + } } From 851575de486b037c63bc82eebb143966e98b785d Mon Sep 17 00:00:00 2001 From: 317787106 <317787106@qq.com> Date: Tue, 14 Jul 2026 20:08:46 +0800 Subject: [PATCH 56/57] revert go_package to github.com/tronprotocol/grpc-gateway (#6874) --- protocol/src/main/protos/api/api.proto | 2 +- protocol/src/main/protos/api/zksnark.proto | 2 +- protocol/src/main/protos/core/Discover.proto | 2 +- protocol/src/main/protos/core/Tron.proto | 2 +- protocol/src/main/protos/core/TronInventoryItems.proto | 2 +- protocol/src/main/protos/core/contract/account_contract.proto | 2 +- .../src/main/protos/core/contract/asset_issue_contract.proto | 2 +- protocol/src/main/protos/core/contract/balance_contract.proto | 2 +- protocol/src/main/protos/core/contract/common.proto | 2 +- protocol/src/main/protos/core/contract/exchange_contract.proto | 2 +- protocol/src/main/protos/core/contract/market_contract.proto | 2 +- protocol/src/main/protos/core/contract/proposal_contract.proto | 2 +- protocol/src/main/protos/core/contract/shield_contract.proto | 2 +- protocol/src/main/protos/core/contract/smart_contract.proto | 2 +- protocol/src/main/protos/core/contract/storage_contract.proto | 2 +- .../src/main/protos/core/contract/vote_asset_contract.proto | 2 +- protocol/src/main/protos/core/contract/witness_contract.proto | 2 +- 17 files changed, 17 insertions(+), 17 deletions(-) diff --git a/protocol/src/main/protos/api/api.proto b/protocol/src/main/protos/api/api.proto index 8b79c8cb0d3..f8d13a6bbd3 100644 --- a/protocol/src/main/protos/api/api.proto +++ b/protocol/src/main/protos/api/api.proto @@ -16,7 +16,7 @@ import "core/contract/shield_contract.proto"; option java_package = "org.tron.api"; //Specify the name of the package that generated the Java file option java_outer_classname = "GrpcAPI"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/api"; +option go_package = "github.com/tronprotocol/grpc-gateway/api"; service Wallet { diff --git a/protocol/src/main/protos/api/zksnark.proto b/protocol/src/main/protos/api/zksnark.proto index 4bbca3b3964..bc0764cb529 100644 --- a/protocol/src/main/protos/api/zksnark.proto +++ b/protocol/src/main/protos/api/zksnark.proto @@ -5,7 +5,7 @@ import "core/Tron.proto"; option java_package = "org.tron.api"; //Specify the name of the package that generated the Java file option java_outer_classname = "ZksnarkGrpcAPI"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/api"; +option go_package = "github.com/tronprotocol/grpc-gateway/api"; service TronZksnark { rpc CheckZksnarkProof (ZksnarkRequest) returns (ZksnarkResponse) { diff --git a/protocol/src/main/protos/core/Discover.proto b/protocol/src/main/protos/core/Discover.proto index c455c96af72..b9190812791 100644 --- a/protocol/src/main/protos/core/Discover.proto +++ b/protocol/src/main/protos/core/Discover.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos"; //Specify the name of the package that generated the Java file option java_outer_classname = "Discover"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message Endpoint { bytes address = 1; diff --git a/protocol/src/main/protos/core/Tron.proto b/protocol/src/main/protos/core/Tron.proto index 6a294c32b0c..a68e841bb60 100644 --- a/protocol/src/main/protos/core/Tron.proto +++ b/protocol/src/main/protos/core/Tron.proto @@ -8,7 +8,7 @@ package protocol; option java_package = "org.tron.protos"; //Specify the name of the package that generated the Java file option java_outer_classname = "Protocol"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; enum AccountType { Normal = 0; diff --git a/protocol/src/main/protos/core/TronInventoryItems.proto b/protocol/src/main/protos/core/TronInventoryItems.proto index 9dde38fb34c..a82d2de4552 100644 --- a/protocol/src/main/protos/core/TronInventoryItems.proto +++ b/protocol/src/main/protos/core/TronInventoryItems.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos"; //Specify the name of the package that generated the Java file option java_outer_classname = "TronInventoryItems"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message InventoryItems { int32 type = 1; diff --git a/protocol/src/main/protos/core/contract/account_contract.proto b/protocol/src/main/protos/core/contract/account_contract.proto index 6f85441dd26..d3180048f43 100644 --- a/protocol/src/main/protos/core/contract/account_contract.proto +++ b/protocol/src/main/protos/core/contract/account_contract.proto @@ -19,7 +19,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "Contract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; import "core/Tron.proto"; diff --git a/protocol/src/main/protos/core/contract/asset_issue_contract.proto b/protocol/src/main/protos/core/contract/asset_issue_contract.proto index 79800c73e53..9e8ff463d52 100644 --- a/protocol/src/main/protos/core/contract/asset_issue_contract.proto +++ b/protocol/src/main/protos/core/contract/asset_issue_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "AssetIssueContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message AssetIssueContract { string id = 41; diff --git a/protocol/src/main/protos/core/contract/balance_contract.proto b/protocol/src/main/protos/core/contract/balance_contract.proto index 2bc6fafd40d..ea1c96270d6 100644 --- a/protocol/src/main/protos/core/contract/balance_contract.proto +++ b/protocol/src/main/protos/core/contract/balance_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "FreezeBalanceContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; import "core/contract/common.proto"; diff --git a/protocol/src/main/protos/core/contract/common.proto b/protocol/src/main/protos/core/contract/common.proto index ba125e131f2..8af929bd52d 100644 --- a/protocol/src/main/protos/core/contract/common.proto +++ b/protocol/src/main/protos/core/contract/common.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "common"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; enum ResourceCode { BANDWIDTH = 0x00; diff --git a/protocol/src/main/protos/core/contract/exchange_contract.proto b/protocol/src/main/protos/core/contract/exchange_contract.proto index 4d4cc185810..8b8878f04f5 100644 --- a/protocol/src/main/protos/core/contract/exchange_contract.proto +++ b/protocol/src/main/protos/core/contract/exchange_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "ExchangeCreateContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message ExchangeCreateContract { bytes owner_address = 1; diff --git a/protocol/src/main/protos/core/contract/market_contract.proto b/protocol/src/main/protos/core/contract/market_contract.proto index 310fcacf217..e1274350036 100644 --- a/protocol/src/main/protos/core/contract/market_contract.proto +++ b/protocol/src/main/protos/core/contract/market_contract.proto @@ -3,7 +3,7 @@ syntax = "proto3"; package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message MarketSellAssetContract { bytes owner_address = 1; diff --git a/protocol/src/main/protos/core/contract/proposal_contract.proto b/protocol/src/main/protos/core/contract/proposal_contract.proto index 126790ca874..35bb9ca7647 100644 --- a/protocol/src/main/protos/core/contract/proposal_contract.proto +++ b/protocol/src/main/protos/core/contract/proposal_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "ProposalApproveContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message ProposalApproveContract { bytes owner_address = 1; diff --git a/protocol/src/main/protos/core/contract/shield_contract.proto b/protocol/src/main/protos/core/contract/shield_contract.proto index 4b2f329b73e..660f9ddf77d 100644 --- a/protocol/src/main/protos/core/contract/shield_contract.proto +++ b/protocol/src/main/protos/core/contract/shield_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "ShieldedTransferContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; // for shielded transaction diff --git a/protocol/src/main/protos/core/contract/smart_contract.proto b/protocol/src/main/protos/core/contract/smart_contract.proto index 6406cdc2a04..c913f7f7577 100644 --- a/protocol/src/main/protos/core/contract/smart_contract.proto +++ b/protocol/src/main/protos/core/contract/smart_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "CreateSmartContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; import "core/Tron.proto"; diff --git a/protocol/src/main/protos/core/contract/storage_contract.proto b/protocol/src/main/protos/core/contract/storage_contract.proto index d10f0ea041e..f04bf716e79 100644 --- a/protocol/src/main/protos/core/contract/storage_contract.proto +++ b/protocol/src/main/protos/core/contract/storage_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "BuyStorageBytesContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message BuyStorageBytesContract { bytes owner_address = 1; diff --git a/protocol/src/main/protos/core/contract/vote_asset_contract.proto b/protocol/src/main/protos/core/contract/vote_asset_contract.proto index 48930a7546e..d3b8e5b779e 100644 --- a/protocol/src/main/protos/core/contract/vote_asset_contract.proto +++ b/protocol/src/main/protos/core/contract/vote_asset_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "VoteAssetContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message VoteAssetContract { bytes owner_address = 1; diff --git a/protocol/src/main/protos/core/contract/witness_contract.proto b/protocol/src/main/protos/core/contract/witness_contract.proto index b02096cee81..5021fbf9a78 100644 --- a/protocol/src/main/protos/core/contract/witness_contract.proto +++ b/protocol/src/main/protos/core/contract/witness_contract.proto @@ -4,7 +4,7 @@ package protocol; option java_package = "org.tron.protos.contract"; //Specify the name of the package that generated the Java file //option java_outer_classname = "WitnessCreateContract"; //Specify the class name of the generated Java file -option go_package = "github.com/tronprotocol/protocol/core/contract"; +option go_package = "github.com/tronprotocol/grpc-gateway/core"; message WitnessCreateContract { bytes owner_address = 1; From 1b61a73d715161af672a56f71513c5d811e7b89a Mon Sep 17 00:00:00 2001 From: halibobo1205 Date: Wed, 15 Jul 2026 19:14:40 +0800 Subject: [PATCH 57/57] update a new version. version name:GreatVoyage-v4.8.1.1-173-gaced0d5654,version code:18817 --- framework/src/main/java/org/tron/program/Version.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/tron/program/Version.java b/framework/src/main/java/org/tron/program/Version.java index 73e4f1e826b..bf435bf0956 100644 --- a/framework/src/main/java/org/tron/program/Version.java +++ b/framework/src/main/java/org/tron/program/Version.java @@ -2,8 +2,8 @@ public class Version { - public static final String VERSION_NAME = "GreatVoyage-v4.8.1-6-g52d7d9d23e"; - public static final String VERSION_CODE = "18643"; + public static final String VERSION_NAME = "GreatVoyage-v4.8.1.1-173-gaced0d5654"; + public static final String VERSION_CODE = "18817"; private static final String VERSION = "4.8.2"; public static String getVersion() {