From 91b6f333c5c601c1173fd6ab399aab0295938dab Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Thu, 31 Aug 2023 12:03:38 +0800 Subject: [PATCH 01/31] test(net):add message handler test --- .../FetchInvDataMsgHandlerTest.java | 60 +++++++++ .../PbftDataSyncHandlerTest.java | 51 ++++++++ .../messagehandler/PbftMsgHandlerTest.java | 119 ++++++++++++++++++ .../TransactionsMsgHandlerTest.java | 87 +++++++++++++ 4 files changed, 317 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java create mode 100644 framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java create mode 100644 framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java 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 e885741c29c..f3258deace2 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,65 @@ package org.tron.core.net.messagehandler; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.lang.reflect.Field; +import java.util.concurrent.TimeUnit; +import java.util.LinkedList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.tron.common.utils.ReflectUtils; +import org.tron.common.utils.Sha256Hash; +import org.tron.core.capsule.BlockCapsule; +import org.tron.core.config.Parameter; +import org.tron.core.net.TronNetDelegate; +import org.tron.core.net.message.adv.BlockMessage; +import org.tron.core.net.message.adv.FetchInvDataMessage; +import org.tron.core.net.peer.Item; +import org.tron.core.net.peer.PeerConnection; +import org.tron.core.net.service.adv.AdvService; +import org.tron.protos.Protocol; + + + public class FetchInvDataMsgHandlerTest { + @Test + public void testProcessMessage() throws Exception { + FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler(); + PeerConnection peer = Mockito.mock(PeerConnection.class); + TronNetDelegate tronNetDelegate = Mockito.mock(TronNetDelegate.class); + AdvService advService = Mockito.mock(AdvService.class); + + Field field = FetchInvDataMsgHandler.class.getDeclaredField("tronNetDelegate"); + field.setAccessible(true); + field.set(fetchInvDataMsgHandler, tronNetDelegate); + + Mockito.when(tronNetDelegate.allowPBFT()).thenReturn(false); + + BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(); + List blockIds = new LinkedList<>(); + blockIds.add(blockId); + + Cache advInvSpread = CacheBuilder.newBuilder().maximumSize(20000) + .expireAfterWrite(1, TimeUnit.HOURS).recordStats().build(); + Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread); + Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true); + Mockito.when(peer.isSyncFinish()).thenReturn(false); + Mockito.when(peer.getBlockBothHave()).thenReturn(blockId); + Cache syncBlockIdCache = CacheBuilder.newBuilder() + .maximumSize(2 * Parameter.NetConstants.SYNC_FETCH_BATCH_NUM).recordStats().build(); + Mockito.when(peer.getSyncBlockIdCache()).thenReturn(syncBlockIdCache); + Mockito.when(peer.getLastSyncBlockId()).thenReturn(blockId); + BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, + System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString()); + Mockito.when(advService.getMessage(new Item(blockId, Protocol.Inventory.InventoryType.BLOCK))) + .thenReturn(new BlockMessage(blockCapsule)); + ReflectUtils.setFieldValue(fetchInvDataMsgHandler, "advService", advService); + + fetchInvDataMsgHandler.processMessage(peer, + new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK)); + Assert.assertNotNull(syncBlockIdCache.getIfPresent(blockId)); + } } diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java new file mode 100644 index 00000000000..37911cb1b53 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java @@ -0,0 +1,51 @@ +package org.tron.core.net.messagehandler; + +import com.google.protobuf.ByteString; +import java.lang.reflect.Field; +import java.util.ArrayList; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.tron.common.utils.Sha256Hash; +import org.tron.core.ChainBaseManager; +import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.PbftSignCapsule; +import org.tron.core.db.PbftSignDataStore; +import org.tron.core.net.message.pbft.PbftCommitMessage; +import org.tron.core.store.DynamicPropertiesStore; +import org.tron.protos.Protocol; + +public class PbftDataSyncHandlerTest { + @Test + public void testProcessMessage() throws Exception { + PbftDataSyncHandler pbftDataSyncHandler = new PbftDataSyncHandler(); + BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, + System.currentTimeMillis(), ByteString.EMPTY); + Protocol.PBFTMessage.Raw.Builder rawBuilder = Protocol.PBFTMessage.Raw.newBuilder(); + rawBuilder.setViewN(blockCapsule.getNum()) + .setEpoch(0) + .setDataType(Protocol.PBFTMessage.DataType.BLOCK) + .setMsgType(Protocol.PBFTMessage.MsgType.PREPREPARE) + .setData(blockCapsule.getBlockId().getByteString()); + Protocol.PBFTMessage.Raw raw = rawBuilder.build(); + PbftSignCapsule pbftSignCapsule = new PbftSignCapsule(raw.toByteString(), new ArrayList<>()); + PbftCommitMessage pbftCommitMessage = new PbftCommitMessage(pbftSignCapsule); + pbftDataSyncHandler.processMessage(null, pbftCommitMessage); + Assert.assertEquals(Protocol.PBFTMessage.Raw.parseFrom( + pbftCommitMessage.getPBFTCommitResult().getData()).getViewN(), 1); + + DynamicPropertiesStore dynamicPropertiesStore = Mockito.mock(DynamicPropertiesStore.class); + PbftSignDataStore pbftSignDataStore = Mockito.mock(PbftSignDataStore.class); + ChainBaseManager chainBaseManager = Mockito.mock(ChainBaseManager.class); + Mockito.when(chainBaseManager.getDynamicPropertiesStore()).thenReturn(dynamicPropertiesStore); + Mockito.when(dynamicPropertiesStore.allowPBFT()).thenReturn(true); + Mockito.when(dynamicPropertiesStore.getMaintenanceTimeInterval()).thenReturn(600L); + Mockito.when(chainBaseManager.getPbftSignDataStore()).thenReturn(pbftSignDataStore); + + Field field = PbftDataSyncHandler.class.getDeclaredField("chainBaseManager"); + field.setAccessible(true); + field.set(pbftDataSyncHandler, chainBaseManager); + + pbftDataSyncHandler.processPBFTCommitData(blockCapsule); + } +} 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 new file mode 100644 index 00000000000..bb1a915cd52 --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java @@ -0,0 +1,119 @@ +package org.tron.core.net.messagehandler; + +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.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.tron.common.application.TronApplicationContext; +import org.tron.common.crypto.SignInterface; +import org.tron.common.crypto.SignUtils; +import org.tron.common.utils.FileUtil; +import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.ReflectUtils; +import org.tron.common.utils.Sha256Hash; +import org.tron.consensus.base.Param; +import org.tron.consensus.pbft.message.PbftMessage; +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.consensus.PbftBaseImpl; +import org.tron.core.exception.P2pException; +import org.tron.core.net.TronNetService; +import org.tron.core.net.message.MessageTypes; +import org.tron.core.net.peer.PeerConnection; +import org.tron.core.net.peer.PeerManager; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.protos.Protocol; +import static org.mockito.Mockito.mock; + +public class PbftMsgHandlerTest { + private static TronApplicationContext context; + private PeerConnection peer; + private static String dbPath = "output-pbft-message-handler-test"; + + + @BeforeClass + public static void init() { + Args.setParam(new String[] {"--output-directory", dbPath, "--debug"}, + Constant.TEST_CONF); + context = new TronApplicationContext(DefaultConfig.class); + + TronNetService tronNetService = context.getBean(TronNetService.class); + Parameter.p2pConfig = new P2pConfig(); + ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig); + } + + @AfterClass + public static void destroy() { + Args.clearParam(); + context.destroy(); + FileUtil.deleteDir(new File(dbPath)); + } + + @Before + 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 + } + } + + @Test + public void testPbft() throws Exception { + 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(context, c1); + Assert.assertEquals(1, PeerManager.getPeers().size()); + Assert.assertFalse(c1.isDisconnect()); + + peer = PeerManager.getPeers().get(0); + BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, + System.currentTimeMillis(), ByteString.EMPTY); + PbftMessage pbftMessage = new PbftMessage(); + Protocol.PBFTMessage.Raw.Builder rawBuilder = Protocol.PBFTMessage.Raw.newBuilder(); + Protocol.PBFTMessage.Builder builder = Protocol.PBFTMessage.newBuilder(); + rawBuilder.setViewN(blockCapsule.getNum()) + .setEpoch(0) + .setDataType(Protocol.PBFTMessage.DataType.BLOCK) + .setMsgType(Protocol.PBFTMessage.MsgType.PREPREPARE) + .setData(blockCapsule.getBlockId().getByteString()); + Protocol.PBFTMessage.Raw raw = rawBuilder.build(); + builder.setRawData(raw); + SignInterface sign = SignUtils.fromPrivate(Hex.decode(PublicMethod.getRandomPrivateKey()), + true); + builder.setSignature(ByteString.copyFrom(sign.Base64toBytes(sign.signHash( + Sha256Hash.hash(true, raw.toByteArray()))))); + Protocol.PBFTMessage message = builder.build(); + pbftMessage.setType(MessageTypes.PBFT_MSG.asByte()); + pbftMessage.setPbftMessage(message); + pbftMessage.setData(message.toByteArray()); + pbftMessage.setSwitch(blockCapsule.isSwitch()); + Param.getInstance().setPbftInterface(context.getBean(PbftBaseImpl.class)); + peer.setNeedSyncFromPeer(false); + //Mockito.doNothing().when(pbftMessage).analyzeSignature(); + try { + context.getBean(PbftMsgHandler.class).processMessage(peer, pbftMessage); + } catch (P2pException e) { + Assert.assertEquals(P2pException.TypeEnum.BAD_MESSAGE, e.getType()); + } + + Assert.assertEquals(1, PeerManager.getPeers().size()); + } +} 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 new file mode 100644 index 00000000000..2a5cacbe0cc --- /dev/null +++ b/framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java @@ -0,0 +1,87 @@ +package org.tron.core.net.messagehandler; + +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; +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.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; +import org.tron.core.net.TronNetDelegate; +import org.tron.core.net.message.adv.TransactionMessage; +import org.tron.core.net.message.adv.TransactionsMessage; +import org.tron.core.net.peer.Item; +import org.tron.core.net.peer.PeerConnection; +import org.tron.core.net.service.adv.AdvService; +import org.tron.protos.Protocol; +import org.tron.protos.contract.BalanceContract; + +@Slf4j +public class TransactionsMsgHandlerTest extends BaseTest { + @BeforeClass + public static void init() { + dbPath = "output_trxmsghandler_test"; + Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, + Constant.TEST_CONF); + + } + + @Test + public void testProcessMessage() { + TransactionsMsgHandler transactionsMsgHandler = new TransactionsMsgHandler(); + try { + Assert.assertFalse(transactionsMsgHandler.isBusy()); + + transactionsMsgHandler.init(); + + PeerConnection peer = Mockito.mock(PeerConnection.class); + TronNetDelegate tronNetDelegate = Mockito.mock(TronNetDelegate.class); + AdvService advService = Mockito.mock(AdvService.class); + + Field field = TransactionsMsgHandler.class.getDeclaredField("tronNetDelegate"); + field.setAccessible(true); + field.set(transactionsMsgHandler, tronNetDelegate); + + BalanceContract.TransferContract transferContract = BalanceContract.TransferContract + .newBuilder() + .setAmount(10) + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString("121212a9cf"))) + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString("232323a9cf"))).build(); + + long transactionTimestamp = DateTime.now().minusDays(4).getMillis(); + Protocol.Transaction trx = Protocol.Transaction.newBuilder().setRawData( + Protocol.Transaction.raw.newBuilder().setTimestamp(transactionTimestamp) + .setRefBlockNum(1) + .addContract( + Protocol.Transaction.Contract.newBuilder() + .setType(Protocol.Transaction.Contract.ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()).build()) + .build(); + Map advInvRequest = new ConcurrentHashMap<>(); + Item item = new Item(new TransactionMessage(trx).getMessageId(), + Protocol.Inventory.InventoryType.TRX); + advInvRequest.put(item, 0L); + Mockito.when(peer.getAdvInvRequest()).thenReturn(advInvRequest); + + List transactionList = new ArrayList<>(); + transactionList.add(trx); + transactionsMsgHandler.processMessage(peer, new TransactionsMessage(transactionList)); + Assert.assertNull(advInvRequest.get(item)); + //Thread.sleep(10); + } catch (Exception e) { + logger.error("error", e); + } finally { + transactionsMsgHandler.close(); + } + } +} From 04b182682c09b95155fcdc8b26a0de0af03f814b Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Thu, 31 Aug 2023 14:38:15 +0800 Subject: [PATCH 02/31] test(net):fix checkstyle issues --- .../org/tron/core/net/messagehandler/PbftMsgHandlerTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 bb1a915cd52..3fec10fc163 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 @@ -1,5 +1,7 @@ package org.tron.core.net.messagehandler; +import static org.mockito.Mockito.mock; + import com.google.protobuf.ByteString; import java.io.File; import java.lang.reflect.Field; @@ -36,7 +38,7 @@ import org.tron.p2p.base.Parameter; import org.tron.p2p.connection.Channel; import org.tron.protos.Protocol; -import static org.mockito.Mockito.mock; + public class PbftMsgHandlerTest { private static TronApplicationContext context; From 868faec56880327605d3b87761afdcf7ea409700 Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Thu, 31 Aug 2023 14:41:38 +0800 Subject: [PATCH 03/31] Revert "test(net):fix checkstyle issues" This reverts commit 04b18268 --- .../org/tron/core/net/messagehandler/PbftMsgHandlerTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 3fec10fc163..bb1a915cd52 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 @@ -1,7 +1,5 @@ package org.tron.core.net.messagehandler; -import static org.mockito.Mockito.mock; - import com.google.protobuf.ByteString; import java.io.File; import java.lang.reflect.Field; @@ -38,7 +36,7 @@ import org.tron.p2p.base.Parameter; import org.tron.p2p.connection.Channel; import org.tron.protos.Protocol; - +import static org.mockito.Mockito.mock; public class PbftMsgHandlerTest { private static TronApplicationContext context; From 6fdfd0173b8535b64b00b3f31bd5229f292ee425 Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Thu, 31 Aug 2023 14:42:27 +0800 Subject: [PATCH 04/31] test(net):fix checkstyle issues --- .../core/net/messagehandler/FetchInvDataMsgHandlerTest.java | 2 +- .../org/tron/core/net/messagehandler/PbftMsgHandlerTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 f3258deace2..404d275276a 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 @@ -3,9 +3,9 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.lang.reflect.Field; -import java.util.concurrent.TimeUnit; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; 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 bb1a915cd52..3fec10fc163 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 @@ -1,5 +1,7 @@ package org.tron.core.net.messagehandler; +import static org.mockito.Mockito.mock; + import com.google.protobuf.ByteString; import java.io.File; import java.lang.reflect.Field; @@ -36,7 +38,7 @@ import org.tron.p2p.base.Parameter; import org.tron.p2p.connection.Channel; import org.tron.protos.Protocol; -import static org.mockito.Mockito.mock; + public class PbftMsgHandlerTest { private static TronApplicationContext context; From c175194f6481c457eb87f53e54b73f0a25b5a0d4 Mon Sep 17 00:00:00 2001 From: liukai Date: Mon, 28 Aug 2023 16:18:47 +0800 Subject: [PATCH 05/31] test(case): fix code style --- .../tron/core/db/AccountAssetStoreTest.java | 139 ++++++++++++++++++ .../org/tron/core/db/AssetIssueStoreTest.java | 91 ++++++++++++ .../tron/core/db/AssetIssueV2StoreTest.java | 92 ++++++++++++ .../org/tron/core/db/ContractStoreTest.java | 102 +++++++++++++ .../core/db/DelegatedResourceStoreTest.java | 72 +++++++++ .../org/tron/core/db/DelegationStoreTest.java | 72 +++++++++ 6 files changed, 568 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/ContractStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/DelegationStoreTest.java diff --git a/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java new file mode 100644 index 00000000000..df670fb55ad --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java @@ -0,0 +1,139 @@ +package org.tron.core.db; + +import com.google.common.primitives.Longs; +import com.google.protobuf.ByteString; + +import java.util.Map; +import javax.annotation.Resource; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +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.config.args.Args; +import org.tron.core.store.AccountAssetStore; +import org.tron.core.store.AccountStore; +import org.tron.protos.Protocol; +import org.tron.protos.contract.AssetIssueContractOuterClass; + +public class AccountAssetStoreTest extends BaseTest { + + private static final byte[] ASSET_KEY = "20000".getBytes(); + private static AccountCapsule ownerCapsule; + + private static String OWNER_ADDRESS = Wallet.getAddressPreFixString() + + "abd4b9367799eaa3197fecb144eb71de1e049abc"; + private static final long TOTAL_SUPPLY = 1000_000_000L; + private static final int TRX_NUM = 10; + private static final int NUM = 1; + private static final long START_TIME = 1; + private static final long END_TIME = 2; + private static final int VOTE_SCORE = 2; + private static final String DESCRIPTION = "TRX"; + private static final String URL = "https://tron.network"; + + @Resource + private AccountAssetStore accountAssetStore; + + @Resource + private AccountStore accountStore; + + static { + dbPath = "db_AccountAssetStore_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + accountAssetStore.put(ASSET_KEY, Longs.toByteArray(200L)); + + ownerCapsule = + new AccountCapsule( + ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), + ByteString.copyFromUtf8("owner"), + Protocol.AccountType.AssetIssue); + } + + + private long createAsset(String tokenName) { + long id = chainBaseManager.getDynamicPropertiesStore().getTokenIdNum() + 1; + chainBaseManager.getDynamicPropertiesStore().saveTokenIdNum(id); + AssetIssueContractOuterClass.AssetIssueContract assetIssueContract = + AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) + .setName(ByteString.copyFrom(ByteArray.fromString(tokenName))) + .setId(Long.toString(id)) + .setTotalSupply(TOTAL_SUPPLY) + .setTrxNum(TRX_NUM) + .setNum(NUM) + .setStartTime(START_TIME) + .setEndTime(END_TIME) + .setVoteScore(VOTE_SCORE) + .setDescription(ByteString.copyFrom(ByteArray.fromString(DESCRIPTION))) + .setUrl(ByteString.copyFrom(ByteArray.fromString(URL))) + .build(); + AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract); + chainBaseManager.getAssetIssueV2Store() + .put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); + try { + ownerCapsule.addAssetV2(ByteArray.fromString(String.valueOf(id)), TOTAL_SUPPLY); + } catch (Exception e) { + e.printStackTrace(); + } + accountStore.put(ownerCapsule.getAddress().toByteArray(), ownerCapsule); + return id; + } + + @Test + public void testPut() { + byte[] key = "10000".getBytes(); + accountAssetStore.put(key, Longs.toByteArray(100L)); + byte[] bytes = accountAssetStore.get(key); + Assert.assertEquals(100L, Longs.fromByteArray(bytes)); + } + + @Test + public void testGet() { + byte[] bytes = accountAssetStore.get(ASSET_KEY); + Assert.assertEquals(200L, Longs.fromByteArray(bytes)); + } + + @Test + public void testGetAccountAssets() { + long assetKey = createAsset("testToken1"); + AccountCapsule accountCapsule = accountStore.get(ownerCapsule.getAddress().toByteArray()); + long assetValue = accountCapsule.getAssetV2(String.valueOf(assetKey)); + Assert.assertEquals(assetValue, TOTAL_SUPPLY); + } + + @Test + public void testGetAllAssets() { + long assetKey1 = createAsset("testToken1"); + long assetKey2 = createAsset("testToken2"); + AccountCapsule accountCapsule = accountStore.get(ownerCapsule.getAddress().toByteArray()); + + Map allAssets = accountAssetStore.getAllAssets(accountCapsule.getInstance()); + Long assetValue1 = allAssets.get(String.valueOf(assetKey1)); + Assert.assertNotNull(assetValue1); + + Long assetV1 = accountCapsule.getAssetV2(String.valueOf(assetKey1)); + Assert.assertEquals(assetValue1, assetV1); + + Long assetValue2 = allAssets.get(String.valueOf(assetKey2)); + Assert.assertNotNull(assetValue2); + + Long assetV2 = accountCapsule.getAssetV2(String.valueOf(assetKey2)); + Assert.assertEquals(assetValue1, assetV2); + } + +} diff --git a/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java b/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java new file mode 100644 index 00000000000..8e448c246e2 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java @@ -0,0 +1,91 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.AssetIssueCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.AssetIssueStore; +import org.tron.protos.contract.AssetIssueContractOuterClass; + +public class AssetIssueStoreTest extends BaseTest { + + private static final String NAME = "test-asset"; + private static final long TOTAL_SUPPLY = 10000L; + private static final int TRX_NUM = 10000; + private static final int NUM = 100000; + private static final String DESCRIPTION = "myCoin"; + private static final String URL = "tron.network"; + + @Resource + private AssetIssueStore assetIssueStore; + + static { + dbPath = "db_AssetIssueStoreTest_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; + AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, NAME); + assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); + } + + private AssetIssueCapsule createAssetIssue(long id, String name) { + dbManager.getDynamicPropertiesStore().saveTokenIdNum(id); + AssetIssueContractOuterClass.AssetIssueContract assetIssueContract = + AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + .setName(ByteString.copyFrom(ByteArray.fromString(name))).setId(Long.toString(id)) + .setTotalSupply(TOTAL_SUPPLY) + .setTrxNum(TRX_NUM).setNum(NUM).setStartTime(1).setEndTime(100).setVoteScore(2) + .setDescription(ByteString.copyFrom(ByteArray.fromString(DESCRIPTION))) + .setUrl(ByteString.copyFrom(ByteArray.fromString(URL))).build(); + AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract); + return assetIssueCapsule; + } + + @Test + public void testPut() { + long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; + String issueName = "test-asset2"; + AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, issueName); + assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); + AssetIssueCapsule assetIssueCapsule1 = assetIssueStore.get(ByteArray.fromString(issueName)); + + Assert.assertNotNull(assetIssueCapsule1); + Assert.assertEquals(issueName, new String(assetIssueCapsule1.getName().toByteArray())); + } + + @Test + public void testGet() { + AssetIssueCapsule assetIssueCapsule = assetIssueStore.get(ByteArray.fromString(NAME)); + Assert.assertNotNull(assetIssueCapsule); + Assert.assertEquals(NAME, new String(assetIssueCapsule.getName().toByteArray())); + Assert.assertEquals(TOTAL_SUPPLY, assetIssueCapsule.getInstance().getTotalSupply()); + } + + @Test + public void testDelete() { + long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; + String issueName = "test-asset-delete"; + AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, issueName); + assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); + AssetIssueCapsule assetIssueCapsule1 = assetIssueStore.get(ByteArray.fromString(issueName)); + Assert.assertNotNull(assetIssueCapsule1); + assetIssueStore.delete(assetIssueCapsule1.createDbKey()); + AssetIssueCapsule assetIssueCapsule2 = assetIssueStore.get(ByteArray.fromString(issueName)); + Assert.assertNull(assetIssueCapsule2); + + } +} diff --git a/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java b/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java new file mode 100644 index 00000000000..c24706fcc40 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java @@ -0,0 +1,92 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +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.capsule.AssetIssueCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.AssetIssueV2Store; +import org.tron.protos.contract.AssetIssueContractOuterClass; + + +public class AssetIssueV2StoreTest extends BaseTest { + + static { + dbPath = "db_AssetIssueV2StoreTest_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + private AssetIssueCapsule assetIssueCapsule; + + @Resource + private AssetIssueV2Store assetIssueV2Store; + + @Before + public void init() { + String firstTokenId = "abc"; + assetIssueCapsule = + new AssetIssueCapsule( + AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + .setName(ByteString.copyFrom(firstTokenId.getBytes())) + .build()); + assetIssueCapsule.setId(String.valueOf(1L)); + assetIssueV2Store + .put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); + } + + @Test + public void testPut() { + String firstTokenId = "efg"; + assetIssueCapsule = + new AssetIssueCapsule( + AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + .setName(ByteString.copyFrom(firstTokenId.getBytes())) + .build()); + assetIssueCapsule.setId(String.valueOf(2L)); + assetIssueV2Store + .put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); + AssetIssueCapsule assetIssueCapsule = + assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); + Assert.assertNotNull(assetIssueCapsule); + String assetName = new String(assetIssueCapsule.getName().toByteArray()); + Assert.assertEquals(firstTokenId, assetName); + } + + @Test + public void testGet() { + AssetIssueCapsule assetIssueCapsule1 = assetIssueV2Store.get(assetIssueCapsule.createDbV2Key()); + Assert.assertNotNull(assetIssueCapsule1); + String assetName = new String(assetIssueCapsule1.getName().toByteArray()); + Assert.assertEquals("abc", assetName); + } + + @Test + public void testDelete() { + String firstTokenId = "hij"; + assetIssueCapsule = + new AssetIssueCapsule( + AssetIssueContractOuterClass.AssetIssueContract.newBuilder() + .setName(ByteString.copyFrom(firstTokenId.getBytes())) + .build()); + assetIssueCapsule.setId(String.valueOf(2L)); + assetIssueV2Store + .put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); + AssetIssueCapsule assetIssueCapsule = + assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); + Assert.assertNotNull(assetIssueCapsule); + + assetIssueV2Store.delete(assetIssueCapsule.createDbV2Key()); + AssetIssueCapsule assetIssueCapsule1 = + assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); + Assert.assertNull(assetIssueCapsule1); + } +} diff --git a/framework/src/test/java/org/tron/core/db/ContractStoreTest.java b/framework/src/test/java/org/tron/core/db/ContractStoreTest.java new file mode 100644 index 00000000000..6b461c02bfe --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/ContractStoreTest.java @@ -0,0 +1,102 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.Wallet; +import org.tron.core.capsule.ContractCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.ContractStore; +import org.tron.protos.contract.SmartContractOuterClass; + +public class ContractStoreTest extends BaseTest { + + private static final String SMART_CONTRACT_NAME = "smart_contract_test"; + private static final String CONTRACT_ADDRESS = "111111"; + private static final long SOURCE_ENERGY_LIMIT = 10L; + private static String OWNER_ADDRESS; + + static { + dbPath = "db_ContractStoreTest_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + @Resource + private ContractStore contractStore; + + @Before + public void init() { + SmartContractOuterClass.SmartContract.Builder contract = + createContract(CONTRACT_ADDRESS, SMART_CONTRACT_NAME); + contractStore.put( + ByteArray.fromHexString(CONTRACT_ADDRESS), + new ContractCapsule(contract.build())); + } + + private SmartContractOuterClass.SmartContract.Builder createContract( + String contractAddress, String contractName) { + OWNER_ADDRESS = + Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc"; + SmartContractOuterClass.SmartContract.Builder builder = + SmartContractOuterClass.SmartContract.newBuilder(); + builder.setName(contractName); + builder.setOriginAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))); + builder.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(contractAddress))); + builder.setOriginEnergyLimit(SOURCE_ENERGY_LIMIT); + return builder; + } + + @Test + public void testGet() { + ContractCapsule contractCapsule = contractStore.get(ByteArray.fromHexString(CONTRACT_ADDRESS)); + byte[] originAddressByte = contractCapsule.getOriginAddress(); + String address = ByteArray.toHexString(originAddressByte); + Assert.assertEquals(OWNER_ADDRESS, address); + } + + @Test + public void testPut() { + String contractAddress = "22222222"; + String contractName = "test_contract_name"; + SmartContractOuterClass.SmartContract.Builder contract = + createContract(contractAddress, contractName); + + contractStore.put( + ByteArray.fromHexString(contractAddress), + new ContractCapsule(contract.build())); + + ContractCapsule contractCapsule = contractStore.get(ByteArray.fromHexString(contractAddress)); + Assert.assertNotNull(contractCapsule); + String name = contractCapsule.getInstance().getName(); + Assert.assertEquals(contractName, name); + } + + @Test + public void testDelete() { + String contractAddress = "3333333"; + String contractName = "test_contract_name3333"; + SmartContractOuterClass.SmartContract.Builder contract = + createContract(contractAddress, contractName); + contractStore.put( + ByteArray.fromHexString(contractAddress), + new ContractCapsule(contract.build())); + ContractCapsule contractCapsule = contractStore.get(ByteArray.fromHexString(contractAddress)); + Assert.assertNotNull(contractCapsule); + String name = contractCapsule.getInstance().getName(); + Assert.assertEquals(contractName, name); + + contractStore.delete(ByteArray.fromHexString(contractAddress)); + ContractCapsule contractCapsule2 = contractStore.get(ByteArray.fromHexString(contractAddress)); + Assert.assertNull(contractCapsule2); + } +} diff --git a/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java b/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java new file mode 100644 index 00000000000..e10838b12dd --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java @@ -0,0 +1,72 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.DelegatedResourceCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.DelegatedResourceStore; + +public class DelegatedResourceStoreTest extends BaseTest { + private static final long BALANCE = 1_000_000; + private static final long EXPIRE_TIME = 1000L; + private static final String OWNER_ADDRESS = "111111111111"; + private static final String RECEIVER_ADDRESS = "222222222222"; + private static DelegatedResourceCapsule delegatedResourceCapsule; + + @Resource + private DelegatedResourceStore delegatedResourceStore; + + static { + dbPath = "db_DelegatedResourceStore_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + delegatedResourceCapsule = create(OWNER_ADDRESS); + delegatedResourceStore.put(delegatedResourceCapsule.createDbKey(), + delegatedResourceCapsule); + } + + @Test + public void testGet() { + DelegatedResourceCapsule delegatedResource = delegatedResourceStore + .get(delegatedResourceCapsule.createDbKey()); + Assert.assertNotNull(delegatedResource); + Assert.assertEquals(delegatedResourceCapsule.getFrom(), delegatedResource.getFrom()); + } + + @Test + public void testPut() { + DelegatedResourceCapsule delegatedResourceCapsule = create("333333333333"); + byte[] key = delegatedResourceCapsule.createDbKey(); + delegatedResourceStore.put(key, delegatedResourceCapsule); + + DelegatedResourceCapsule delegatedResourceCapsule1 = delegatedResourceStore.get(key); + Assert.assertNotNull(delegatedResourceCapsule1); + Assert.assertEquals(BALANCE, delegatedResourceCapsule1.getFrozenBalanceForEnergy()); + } + + public DelegatedResourceCapsule create(String address) { + byte[] ownerAddress = ByteArray.fromHexString(address); + byte[] receiverAddress = ByteArray.fromHexString(RECEIVER_ADDRESS); + DelegatedResourceCapsule delegatedResourceCapsule = new DelegatedResourceCapsule( + ByteString.copyFrom(ownerAddress), + ByteString.copyFrom(receiverAddress)); + + delegatedResourceCapsule.setFrozenBalanceForEnergy(BALANCE, EXPIRE_TIME); + return delegatedResourceCapsule; + } + +} diff --git a/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java b/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java new file mode 100644 index 00000000000..1bfbbb33660 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java @@ -0,0 +1,72 @@ +package org.tron.core.db; + +import javax.annotation.Resource; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.BytesCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.DelegationStore; + + +public class DelegationStoreTest extends BaseTest { + + private static final String OWNER_ADDRESS = "11111111111"; + private static final long CYCLE = 100; + private static final long VALUE = 10_000_000; + + @Resource + private DelegationStore delegationStore; + + static { + dbPath = "db_DelegationStore_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + create(); + } + + public void create() { + byte[] key = buildRewardKey(CYCLE, ByteArray.fromHexString(OWNER_ADDRESS)); + delegationStore.put(key, new BytesCapsule(ByteArray + .fromLong(VALUE))); + } + + private byte[] buildRewardKey(long cycle, byte[] address) { + return (cycle + "-" + Hex.toHexString(address) + "-reward").getBytes(); + } + + @Test + public void testGet() { + byte[] key = buildRewardKey(CYCLE, ByteArray.fromHexString(OWNER_ADDRESS)); + BytesCapsule bytesCapsule = delegationStore.get(key); + Assert.assertNotNull(bytesCapsule); + long actualValue = ByteArray.toLong(bytesCapsule.getData()); + Assert.assertEquals(VALUE, actualValue); + } + + @Test + public void testPut() { + long value = 20_000_000; + byte[] key = buildRewardKey(CYCLE, ByteArray.fromHexString("2222222222222")); + delegationStore.put(key, new BytesCapsule(ByteArray + .fromLong(20_000_000))); + + BytesCapsule bytesCapsule = delegationStore.get(key); + Assert.assertNotNull(bytesCapsule); + long actualValue = ByteArray.toLong(bytesCapsule.getData()); + Assert.assertEquals(value, actualValue); + } + +} From 6748bcef39d63418648598b49176c1af03313b68 Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Thu, 7 Sep 2023 15:03:13 +0800 Subject: [PATCH 06/31] test(net):optimize test code --- .../core/net/messagehandler/PbftDataSyncHandlerTest.java | 6 ++++++ .../core/net/messagehandler/TransactionsMsgHandlerTest.java | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java index 37911cb1b53..d1fdfaa5d90 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/PbftDataSyncHandlerTest.java @@ -1,8 +1,10 @@ package org.tron.core.net.messagehandler; +import com.alibaba.fastjson.JSON; import com.google.protobuf.ByteString; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Map; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -47,5 +49,9 @@ public void testProcessMessage() throws Exception { field.set(pbftDataSyncHandler, chainBaseManager); pbftDataSyncHandler.processPBFTCommitData(blockCapsule); + Field field1 = PbftDataSyncHandler.class.getDeclaredField("pbftCommitMessageCache"); + field1.setAccessible(true); + Map map = JSON.parseObject(JSON.toJSONString(field1.get(pbftDataSyncHandler)), Map.class); + Assert.assertFalse(map.containsKey(0)); } } 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 2a5cacbe0cc..fe58928e3bc 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 @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import lombok.extern.slf4j.Slf4j; import org.joda.time.DateTime; import org.junit.Assert; import org.junit.BeforeClass; @@ -26,7 +25,6 @@ import org.tron.protos.Protocol; import org.tron.protos.contract.BalanceContract; -@Slf4j public class TransactionsMsgHandlerTest extends BaseTest { @BeforeClass public static void init() { @@ -79,7 +77,7 @@ public void testProcessMessage() { Assert.assertNull(advInvRequest.get(item)); //Thread.sleep(10); } catch (Exception e) { - logger.error("error", e); + Assert.fail(); } finally { transactionsMsgHandler.close(); } From 1e500f080b9e55f6bda8e1ea95f7c816d2ec29df Mon Sep 17 00:00:00 2001 From: liukai Date: Thu, 7 Sep 2023 17:19:29 +0800 Subject: [PATCH 07/31] feat(config): update jacoco config --- actuator/build.gradle | 2 +- chainbase/build.gradle | 2 +- common/build.gradle | 2 +- consensus/build.gradle | 2 +- crypto/build.gradle | 13 +++++++++++++ framework/build.gradle | 2 +- plugins/build.gradle | 4 ++-- 7 files changed, 20 insertions(+), 7 deletions(-) diff --git a/actuator/build.gradle b/actuator/build.gradle index 15f661be9bd..a0ce72b0ee6 100644 --- a/actuator/build.gradle +++ b/actuator/build.gradle @@ -52,7 +52,7 @@ jacocoTestReport { xml.enabled = true html.enabled = true } - executionData.from = '../framework/build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) afterEvaluate { classDirectories.from = classDirectories.files.collect { fileTree(dir: it,) diff --git a/chainbase/build.gradle b/chainbase/build.gradle index fb1778d2fc6..a2868aa78b4 100644 --- a/chainbase/build.gradle +++ b/chainbase/build.gradle @@ -80,7 +80,7 @@ jacocoTestReport { xml.enabled = true html.enabled = true } - executionData.from = '../framework/build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) afterEvaluate { classDirectories.from = classDirectories.files.collect { fileTree(dir: it,) diff --git a/common/build.gradle b/common/build.gradle index 0ed13763a1e..641ac4d07cf 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -68,7 +68,7 @@ jacocoTestReport { xml.enabled = true html.enabled = true } - executionData.from = '../framework/build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) afterEvaluate { classDirectories.from = classDirectories.files.collect { fileTree(dir: it,) diff --git a/consensus/build.gradle b/consensus/build.gradle index c12662446f1..78f60bd7530 100644 --- a/consensus/build.gradle +++ b/consensus/build.gradle @@ -48,7 +48,7 @@ jacocoTestReport { xml.enabled = true html.enabled = true } - executionData.from = '../framework/build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) afterEvaluate { classDirectories.from = classDirectories.files.collect { fileTree(dir: it,) diff --git a/crypto/build.gradle b/crypto/build.gradle index 19ae8e805fe..5bbfc630c45 100644 --- a/crypto/build.gradle +++ b/crypto/build.gradle @@ -15,3 +15,16 @@ dependencies { compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69' compile project(":common") } + +jacocoTestReport { + reports { + xml.enabled = true + html.enabled = true + } + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) + afterEvaluate { + classDirectories.from = classDirectories.files.collect { + fileTree(dir: it,) + } + } +} diff --git a/framework/build.gradle b/framework/build.gradle index 9bc93cacf33..c894ae1e03d 100644 --- a/framework/build.gradle +++ b/framework/build.gradle @@ -165,7 +165,7 @@ jacocoTestReport { csv.enabled false html.destination file("${buildDir}/jacocoHtml") } - executionData.from = 'build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) } def binaryRelease(taskName, jarName, mainClass) { diff --git a/plugins/build.gradle b/plugins/build.gradle index 9f4cb3ef59b..eca66dca27d 100644 --- a/plugins/build.gradle +++ b/plugins/build.gradle @@ -76,7 +76,7 @@ test { exceptionFormat = 'full' } jacoco { - destinationFile = file("$buildDir/jacoco/jacocoTest.exec") + destinationFile = file("../framework/build/jacoco/jacocoTest1.exec") classDumpDir = file("$buildDir/jacoco/classpathdumps") } } @@ -87,7 +87,7 @@ jacocoTestReport { csv.enabled false html.destination file("${buildDir}/jacocoHtml") } - executionData.from = 'build/jacoco/jacocoTest.exec' + getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec")) } def binaryRelease(taskName, jarName, mainClass) { From 17558c54ed1891cd293fdf17858ddcd2d96db2a9 Mon Sep 17 00:00:00 2001 From: liukai Date: Thu, 7 Sep 2023 21:32:02 +0800 Subject: [PATCH 08/31] test(case): add WitnessScheduleStoreTest test case --- .../core/db/WitnessScheduleStoreTest.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java diff --git a/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java b/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java new file mode 100644 index 00000000000..c12e5fdc413 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java @@ -0,0 +1,106 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import java.util.Arrays; +import java.util.List; +import javax.annotation.Resource; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; +import org.tron.core.store.WitnessScheduleStore; + +public class WitnessScheduleStoreTest extends BaseTest { + + @Resource + private WitnessScheduleStore witnessScheduleStore; + private static final String KEY1 = "41f08012b4881c320eb40b80f1228731898824e09d"; + private static final String KEY2 = "41df309fef25b311e7895562bd9e11aab2a58816d2"; + private static final String KEY3 = "41F8C7ACC4C08CF36CA08FC2A61B1F5A7C8DEA7BEC"; + + private static final String CURRENT_KEY1 = "411D7ABA13EA199A63D1647E58E39C16A9BB9DA689"; + private static final String CURRENT_KEY2 = "410694981B116304ED21E05896FB16A6BC2E91C92C"; + private static final String CURRENT_KEY3 = "411155D10415FAC16A8F4CB2F382CE0E0F0A7E64CC"; + + private static List witnessAddresses; + private static List currentShuffledWitnesses; + + + static { + dbPath = "output-WitnessScheduleStore-test"; + Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF); + } + + @Before + public void init() { + witnessAddresses = Arrays.asList( + getByteString(KEY1), + getByteString(KEY2), + getByteString(KEY3) + ); + + currentShuffledWitnesses = Arrays.asList( + getByteString(CURRENT_KEY1), + getByteString(CURRENT_KEY2), + getByteString(CURRENT_KEY3) + ); + } + + private ByteString getByteString(String address) { + return ByteString.copyFrom( + ByteArray.fromHexString(address)); + } + + @Test + public void tetSaveActiveWitnesses() { + witnessScheduleStore.saveActiveWitnesses(witnessAddresses); + List activeWitnesses = witnessScheduleStore.getActiveWitnesses(); + Assert.assertNotNull(activeWitnesses); + Assert.assertEquals(3, activeWitnesses.size()); + ByteString firstWitness = activeWitnesses.get(0); + Assert.assertEquals(getByteString(KEY1), firstWitness); + } + + @Test + public void testGetActiveWitnesses() { + witnessScheduleStore.saveActiveWitnesses(witnessAddresses); + List activeWitnesses = witnessScheduleStore.getActiveWitnesses(); + Assert.assertNotNull(activeWitnesses); + Assert.assertEquals(3, activeWitnesses.size()); + ByteString firstWitness = activeWitnesses.get(0); + ByteString secondWitness = activeWitnesses.get(1); + ByteString thirdWitness = activeWitnesses.get(2); + Assert.assertEquals(getByteString(KEY1), firstWitness); + Assert.assertEquals(getByteString(KEY2), secondWitness); + Assert.assertEquals(getByteString(KEY3), thirdWitness); + } + + @Test + public void testSaveCurrentShuffledWitnesses() { + witnessScheduleStore.saveCurrentShuffledWitnesses(currentShuffledWitnesses); + List currentWitnesses = witnessScheduleStore.getCurrentShuffledWitnesses(); + Assert.assertNotNull(currentWitnesses); + Assert.assertEquals(3, currentWitnesses.size()); + ByteString firstWitness = currentWitnesses.get(0); + Assert.assertEquals(getByteString(CURRENT_KEY1), firstWitness); + } + + @Test + public void GetCurrentShuffledWitnesses() { + witnessScheduleStore.saveCurrentShuffledWitnesses(currentShuffledWitnesses); + List currentWitnesses = witnessScheduleStore.getCurrentShuffledWitnesses(); + Assert.assertNotNull(currentWitnesses); + Assert.assertEquals(3, currentWitnesses.size()); + ByteString firstWitness = currentWitnesses.get(0); + ByteString secondWitness = currentWitnesses.get(1); + ByteString thirdWitness = currentWitnesses.get(2); + Assert.assertEquals(getByteString(CURRENT_KEY1), firstWitness); + Assert.assertEquals(getByteString(CURRENT_KEY2), secondWitness); + Assert.assertEquals(getByteString(CURRENT_KEY3), thirdWitness); + } + +} From 9d089e7ac7f1b9d213b0044725270a9f2a76d5eb Mon Sep 17 00:00:00 2001 From: chengtx01 Date: Wed, 13 Sep 2023 17:28:49 +0800 Subject: [PATCH 09/31] test(net):create folder with temporaryFolder --- .../core/net/messagehandler/TransactionsMsgHandlerTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 fe58928e3bc..47ffde30ac3 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 @@ -28,8 +28,7 @@ public class TransactionsMsgHandlerTest extends BaseTest { @BeforeClass public static void init() { - dbPath = "output_trxmsghandler_test"; - Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, + Args.setParam(new String[]{"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF); } From 47fe70240192e0a60c261dcc0c3d525f383b6d70 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:42:58 +0800 Subject: [PATCH 10/31] feat(db/checkPointV2Store): change log level from info to debug for close (#5499) --- .../org/tron/core/store/CheckPointV2Store.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 046df7f6643..2f952e6b82a 100644 --- a/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java +++ b/chainbase/src/main/java/org/tron/core/store/CheckPointV2Store.java @@ -1,6 +1,7 @@ package org.tron.core.store; import com.google.protobuf.InvalidProtocolBufferException; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.tron.core.db.TronDatabase; import org.tron.core.exception.BadItemException; @@ -9,6 +10,7 @@ import java.util.Spliterator; import java.util.function.Consumer; +@Slf4j(topic = "DB") public class CheckPointV2Store extends TronDatabase { @Autowired @@ -50,4 +52,19 @@ public Spliterator spliterator() { protected void init() { } + /** + * close the database. + */ + @Override + public void close() { + logger.debug("******** Begin to close {}. ********", getName()); + try { + dbSource.closeDB(); + } catch (Exception e) { + logger.warn("Failed to close {}.", getName(), e); + } finally { + logger.debug("******** End to close {}. ********", getName()); + } + } + } From c97617c0f6ba5d2df9efc20fb8157ab3e6854f33 Mon Sep 17 00:00:00 2001 From: Kayle Date: Tue, 19 Sep 2023 09:24:13 +0800 Subject: [PATCH 11/31] fix(test\db): fix wrong output path (#5501) Co-authored-by: liukai --- .../src/test/java/org/tron/core/db/AccountAssetStoreTest.java | 3 +-- .../src/test/java/org/tron/core/db/AssetIssueStoreTest.java | 3 +-- .../src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java | 3 +-- .../src/test/java/org/tron/core/db/ContractStoreTest.java | 3 +-- .../test/java/org/tron/core/db/DelegatedResourceStoreTest.java | 3 +-- .../src/test/java/org/tron/core/db/DelegationStoreTest.java | 3 +-- .../test/java/org/tron/core/db/WitnessScheduleStoreTest.java | 3 +-- 7 files changed, 7 insertions(+), 14 deletions(-) diff --git a/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java index df670fb55ad..48c24d98af1 100644 --- a/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AccountAssetStoreTest.java @@ -44,10 +44,9 @@ public class AccountAssetStoreTest extends BaseTest { private AccountStore accountStore; static { - dbPath = "db_AccountAssetStore_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java b/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java index 8e448c246e2..34a4a8507d6 100644 --- a/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AssetIssueStoreTest.java @@ -26,10 +26,9 @@ public class AssetIssueStoreTest extends BaseTest { private AssetIssueStore assetIssueStore; static { - dbPath = "db_AssetIssueStoreTest_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java b/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java index c24706fcc40..e92027e3a28 100644 --- a/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java +++ b/framework/src/test/java/org/tron/core/db/AssetIssueV2StoreTest.java @@ -16,10 +16,9 @@ public class AssetIssueV2StoreTest extends BaseTest { static { - dbPath = "db_AssetIssueV2StoreTest_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/ContractStoreTest.java b/framework/src/test/java/org/tron/core/db/ContractStoreTest.java index 6b461c02bfe..391a2013636 100644 --- a/framework/src/test/java/org/tron/core/db/ContractStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/ContractStoreTest.java @@ -22,10 +22,9 @@ public class ContractStoreTest extends BaseTest { private static String OWNER_ADDRESS; static { - dbPath = "db_ContractStoreTest_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java b/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java index e10838b12dd..8878ff0dcf3 100644 --- a/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/DelegatedResourceStoreTest.java @@ -23,10 +23,9 @@ public class DelegatedResourceStoreTest extends BaseTest { private DelegatedResourceStore delegatedResourceStore; static { - dbPath = "db_DelegatedResourceStore_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java b/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java index 1bfbbb33660..d63015ae064 100644 --- a/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/DelegationStoreTest.java @@ -23,10 +23,9 @@ public class DelegationStoreTest extends BaseTest { private DelegationStore delegationStore; static { - dbPath = "db_DelegationStore_test"; Args.setParam( new String[]{ - "--output-directory", dbPath, + "--output-directory", dbPath(), }, Constant.TEST_CONF ); diff --git a/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java b/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java index c12e5fdc413..7588b1c7add 100644 --- a/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/WitnessScheduleStoreTest.java @@ -31,8 +31,7 @@ public class WitnessScheduleStoreTest extends BaseTest { static { - dbPath = "output-WitnessScheduleStore-test"; - Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF); + Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); } @Before From 6703d13e1c129948158993435b182cf9e12f2e91 Mon Sep 17 00:00:00 2001 From: wubin01 Date: Tue, 19 Sep 2023 10:41:21 +0800 Subject: [PATCH 12/31] feat(api): add http/grpc interface rate limit --- .../org/tron/common/parameter/CommonParameter.java | 3 +++ common/src/main/java/org/tron/core/Constant.java | 2 ++ .../src/main/java/org/tron/core/config/args/Args.java | 6 ++++++ .../tron/core/services/http/RateLimiterServlet.java | 5 +++-- .../services/ratelimiter/RateLimiterInterceptor.java | 4 +++- .../services/ratelimiter/strategy/QpsStrategy.java | 3 ++- .../java/org/tron/common/config/args/ArgsTest.java | 10 ++++++++++ framework/src/test/resources/config-test.conf | 1 + 8 files changed, 30 insertions(+), 4 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 aa9f4e40f14..94d398c4b86 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -415,6 +415,9 @@ public class CommonParameter { @Setter public int rateLimiterGlobalIpQps; @Getter + @Setter + public int rateLimiterGlobalApiQps; + @Getter public DbBackupConfig dbBackupConfig; @Getter public RocksDbSettings rocksDBCustomSettings; diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 3e859f6fd96..54ff30d9e65 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -256,6 +256,8 @@ public class Constant { public static final String RATE_LIMITER_GLOBAL_IP_QPS = "rate.limiter.global.ip.qps"; + public static final String RATE_LIMITER_GLOBAL_API_QPS = "rate.limiter.global.api.qps"; + public static final String COMMITTEE_CHANGED_DELEGATION = "committee.changedDelegation"; public static final String CRYPTO_ENGINE = "crypto.engine"; 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 74a755aca7f..b3a6821f3b2 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 @@ -224,6 +224,7 @@ public static void clearParam() { PARAMETER.memoFee = 0; PARAMETER.rateLimiterGlobalQps = 50000; PARAMETER.rateLimiterGlobalIpQps = 10000; + PARAMETER.rateLimiterGlobalApiQps = 1000; PARAMETER.p2pDisable = false; PARAMETER.dynamicConfigEnable = false; PARAMETER.dynamicConfigCheckInterval = 600; @@ -957,6 +958,10 @@ public static void setParam(final String[] args, final String confFileName) { config.hasPath(Constant.RATE_LIMITER_GLOBAL_IP_QPS) ? config .getInt(Constant.RATE_LIMITER_GLOBAL_IP_QPS) : 10000; + PARAMETER.rateLimiterGlobalApiQps = + config.hasPath(Constant.RATE_LIMITER_GLOBAL_API_QPS) ? config + .getInt(Constant.RATE_LIMITER_GLOBAL_API_QPS) : 1000; + PARAMETER.rateLimiterInitialization = getRateLimiterFromConfig(config); PARAMETER.changedDelegation = @@ -1627,6 +1632,7 @@ public static void logConfig() { logger.info("Node effective check enable: {}", parameter.isNodeEffectiveCheckEnable()); logger.info("Rate limiter global qps: {}", parameter.getRateLimiterGlobalQps()); logger.info("Rate limiter global ip qps: {}", parameter.getRateLimiterGlobalIpQps()); + logger.info("Rate limiter global api qps: {}", parameter.getRateLimiterGlobalApiQps()); logger.info("************************ Backup config ************************"); logger.info("Backup priority: {}", parameter.getBackupPriority()); logger.info("Backup listen port: {}", parameter.getBackupPort()); 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 eb8b7b86257..062750f914e 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 @@ -31,6 +31,7 @@ public abstract class RateLimiterServlet extends HttpServlet { private static final String KEY_PREFIX_HTTP = "http_"; private static final String ADAPTER_PREFIX = "org.tron.core.services.ratelimiter.adapter."; + private static final int QPS = Args.getInstance().getRateLimiterGlobalApiQps(); @Autowired private RateLimiterContainer container; @@ -62,7 +63,7 @@ private void addRateContainer() { } else { constructor = c.getConstructor(); - obj = constructor.newInstance(); + obj = constructor.newInstance("qps=" + QPS); container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj); } success = true; @@ -78,7 +79,7 @@ private void addRateContainer() { // if the specific rate limiter strategy of servlet is not defined or fail to add, // then add a default Strategy. try { - IRateLimiter rateLimiter = new DefaultBaseQqsAdapter("qps=1000"); + IRateLimiter rateLimiter = new DefaultBaseQqsAdapter("qps=" + QPS); container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), rateLimiter); } catch (Exception e) { logger.warn( 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 d629ae05abd..d6803dc76d7 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 @@ -31,6 +31,8 @@ public class RateLimiterInterceptor implements ServerInterceptor { private static final String KEY_PREFIX_RPC = "rpc_"; + private static final int QPS = Args.getInstance().getRateLimiterGlobalApiQps(); + @Autowired private RateLimiterContainer container; @@ -41,7 +43,7 @@ public void init(Server server) { for (ServerServiceDefinition service : server.getServices()) { for (ServerMethodDefinition method : service.getMethods()) { container.add(KEY_PREFIX_RPC, method.getMethodDescriptor().getFullMethodName(), - new DefaultBaseQqsAdapter("qps=1000")); + new DefaultBaseQqsAdapter("qps=" + QPS)); } } 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 a4d56af8ba2..8c18af34da9 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 @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.tron.core.config.args.Args; @Slf4j public class QpsStrategy extends Strategy { public static final String STRATEGY_PARAM_QPS = "qps"; - public static final Double DEFAULT_QPS = 100D; + public static final int DEFAULT_QPS = Args.getInstance().getRateLimiterGlobalApiQps(); private RateLimiter rateLimiter; public QpsStrategy(String paramString) { diff --git a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java index 2e29b38824f..aa9ac14964d 100644 --- a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java @@ -1,13 +1,21 @@ package org.tron.common.config.args; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; + 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.client.WalletGrpcClient; import org.tron.common.parameter.RateLimiterInitialization; +import org.tron.common.utils.client.GrpcClient; +import org.tron.common.utils.client.WalletClient; import org.tron.core.Constant; import org.tron.core.config.args.Args; @@ -31,6 +39,7 @@ public void destroy() { @Test public void testConfig() { + Args.logConfig(); Assert.assertEquals(Args.getInstance().getMaxTransactionPendingSize(), 2000); Assert.assertEquals(Args.getInstance().getPendingTransactionTimeout(), 60_000); Assert.assertEquals(Args.getInstance().getMaxFastForwardNum(), 3); @@ -39,6 +48,7 @@ public void testConfig() { Assert.assertFalse(Args.getInstance().isNodeEffectiveCheckEnable()); Assert.assertEquals(Args.getInstance().getRateLimiterGlobalQps(), 1000); Assert.assertEquals(Args.getInstance().getRateLimiterGlobalIpQps(), 1000); + Assert.assertEquals(Args.getInstance().getRateLimiterGlobalApiQps(), 100); Assert.assertEquals(Args.getInstance().p2pDisable, true); Assert.assertEquals(Args.getInstance().getMaxTps(), 1000); RateLimiterInitialization rateLimiter = Args.getInstance().getRateLimiterInitialization(); diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index d506521965f..236502d3360 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -348,6 +348,7 @@ committee = { rate.limiter.global.qps = 1000 rate.limiter.global.ip.qps = 1000 +rate.limiter.global.api.qps = 100 rate.limiter.http = [ { component = "GetNowBlockServlet", From 290753a6da719324c959e9ba1ae076fb650409c0 Mon Sep 17 00:00:00 2001 From: liuxincheng Date: Tue, 19 Sep 2023 19:10:19 +0800 Subject: [PATCH 13/31] fix(api): optimize API call compatibility --- .../main/java/org/tron/core/services/http/Util.java | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 6874ec875d0..6eac3d6cf94 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 @@ -613,6 +613,9 @@ public static void validateParameter(String contract) throws InvalidParameterExc } public static String getJsonString(String str) { + if (isValidJson(str)) { + return str; + } if (StringUtils.isEmpty(str)) { return EMPTY; } @@ -631,4 +634,12 @@ public static String getJsonString(String str) { return json.toString(); } + public static boolean isValidJson(String json) { + try { + JSON.parse(json); + return true; + } catch (Exception e) { + return false; + } + } } From 7ffd2fcb1ec10cd58893362b9e50623ca59c1673 Mon Sep 17 00:00:00 2001 From: Simon <107106002+CarlChaoCarl@users.noreply.github.com> Date: Tue, 19 Sep 2023 19:58:41 +0800 Subject: [PATCH 14/31] fix(event/mechanism): fix NPE in TransactionLogTriggerCapsule (#5500) --- .../capsule/TransactionLogTriggerCapsule.java | 14 +++++--- .../TransactionLogTriggerCapsuleTest.java | 33 +++++++++++++++---- 2 files changed, 36 insertions(+), 11 deletions(-) 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 ab6ea018cf8..f5d457f541d 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 @@ -175,8 +175,10 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule .encode58Check(unfreezeBalanceContract.getReceiverAddress().toByteArray())); } transactionLogTrigger.setAssetName("trx"); - transactionLogTrigger.setAssetAmount( - transactionInfo.getUnfreezeAmount()); + if (Objects.nonNull(transactionInfo)) { + transactionLogTrigger.setAssetAmount( + transactionInfo.getUnfreezeAmount()); + } break; case FreezeBalanceV2Contract: FreezeBalanceV2Contract freezeBalanceV2Contract = contractParameter @@ -204,7 +206,9 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule transactionLogTrigger.setFromAddress(StringUtil.encode58Check( withdrawExpireUnfreezeContract.getOwnerAddress().toByteArray())); transactionLogTrigger.setAssetName("trx"); - transactionLogTrigger.setAssetAmount(transactionInfo.getWithdrawExpireAmount()); + if (Objects.nonNull(transactionInfo)) { + transactionLogTrigger.setAssetAmount(transactionInfo.getWithdrawExpireAmount()); + } break; case DelegateResourceContract: DelegateResourceContract delegateResourceContract = contractParameter @@ -238,7 +242,9 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule transactionLogTrigger.setFromAddress(StringUtil .encode58Check(cancelAllUnfreezeV2Contract.getOwnerAddress().toByteArray())); transactionLogTrigger.setAssetName("trx"); - transactionLogTrigger.setExtMap(transactionInfo.getCancelUnfreezeV2AmountMap()); + if (Objects.nonNull(transactionInfo)) { + transactionLogTrigger.setExtMap(transactionInfo.getCancelUnfreezeV2AmountMap()); + } break; default: break; 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 d8da2cc540d..76dd6e99158 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java @@ -1,6 +1,7 @@ package org.tron.common.logsfilter; import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION; +import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH; import com.google.protobuf.ByteString; import org.junit.Assert; @@ -37,12 +38,18 @@ public void testConstructorWithUnfreezeBalanceTrxCapsule() { .setReceiverAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS))); transactionCapsule = new TransactionCapsule(builder2.build(), Protocol.Transaction.Contract.ContractType.UnfreezeBalanceContract); + Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder(); + builder.setUnfreezeAmount(TRX_PRECISION + 1000); - TransactionLogTriggerCapsule triggerCapsule = - new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule); + + TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule( + transactionCapsule, blockCapsule,0,0,0, + builder.build(),0); Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress()); Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress()); + Assert.assertEquals(TRX_PRECISION + 1000, + triggerCapsule.getTransactionLogTrigger().getAssetAmount()); } @@ -93,12 +100,17 @@ public void testConstructorWithWithdrawExpireTrxCapsule() { transactionCapsule = new TransactionCapsule(builder2.build(), Protocol.Transaction.Contract.ContractType.WithdrawExpireUnfreezeContract); - TransactionLogTriggerCapsule triggerCapsule = - new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule); + Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder(); + builder.setWithdrawExpireAmount(TRX_PRECISION + 1000); + + TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule( + transactionCapsule, blockCapsule,0,0,0, + builder.build(),0); Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress()); Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName()); - Assert.assertEquals(0L, triggerCapsule.getTransactionLogTrigger().getAssetAmount()); + Assert.assertEquals(TRX_PRECISION + 1000, + triggerCapsule.getTransactionLogTrigger().getAssetAmount()); } @@ -150,10 +162,17 @@ public void testConstructorWithCancelAllUnfreezeTrxCapsule() { transactionCapsule = new TransactionCapsule(builder2.build(), Protocol.Transaction.Contract.ContractType.CancelAllUnfreezeV2Contract); - TransactionLogTriggerCapsule triggerCapsule = - new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule); + Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder(); + builder.clearCancelUnfreezeV2Amount().putCancelUnfreezeV2Amount( + BANDWIDTH.name(), TRX_PRECISION + 2000); + + TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule( + transactionCapsule, blockCapsule,0,0,0, + builder.build(),0); Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress()); + Assert.assertEquals(TRX_PRECISION + 2000, + triggerCapsule.getTransactionLogTrigger().getExtMap().get(BANDWIDTH.name()).longValue()); } } \ No newline at end of file From 703a8663315c55515129b48ff86aa84ea0e7da57 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 20 Sep 2023 18:42:41 +0800 Subject: [PATCH 15/31] fix(db/trans-cache): avoid recovering a wrong cached data (#5505) --- .../org/tron/core/db/TransactionCache.java | 8 ++++++-- .../org/tron/core/db2/common/TxCacheDB.java | 20 ++++++++++++++----- .../org/tron/core/config/DefaultConfig.java | 7 ------- .../main/java/org/tron/core/db/Manager.java | 2 +- .../org/tron/core/db/TxCacheDBInitTest.java | 20 +++++++++++++++---- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/chainbase/src/main/java/org/tron/core/db/TransactionCache.java b/chainbase/src/main/java/org/tron/core/db/TransactionCache.java index 70b42ca7226..58ed9be9145 100644 --- a/chainbase/src/main/java/org/tron/core/db/TransactionCache.java +++ b/chainbase/src/main/java/org/tron/core/db/TransactionCache.java @@ -3,16 +3,20 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; import org.tron.core.capsule.BytesCapsule; import org.tron.core.db2.common.TxCacheDB; +import org.tron.core.store.DynamicPropertiesStore; @Slf4j +@Component public class TransactionCache extends TronStoreWithRevoking { @Autowired public TransactionCache(@Value("trans-cache") String dbName, - RecentTransactionStore recentTransactionStore) { - super(new TxCacheDB(dbName, recentTransactionStore)); + @Autowired RecentTransactionStore recentTransactionStore, + @Autowired DynamicPropertiesStore dynamicPropertiesStore) { + super(new TxCacheDB(dbName, recentTransactionStore, dynamicPropertiesStore)); } public void initCache() { 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 cefe014959e..8e5a2ee6787 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 @@ -40,6 +40,7 @@ import org.tron.core.db.RecentTransactionItem; import org.tron.core.db.RecentTransactionStore; import org.tron.core.db.common.iterator.DBIterator; +import org.tron.core.store.DynamicPropertiesStore; @Slf4j(topic = "DB") public class TxCacheDB implements DB, Flusher { @@ -59,7 +60,6 @@ public class TxCacheDB implements DB, Flusher { private BloomFilter[] bloomFilters = new BloomFilter[2]; // filterStartBlock record the start block of the active filter private volatile long filterStartBlock = INVALID_BLOCK; - private volatile long currentBlockNum = INVALID_BLOCK; // currentFilterIndex records the index of the active filter private volatile int currentFilterIndex = 0; @@ -75,6 +75,8 @@ public class TxCacheDB implements DB, Flusher { // replace persistentStore and optimizes startup performance private RecentTransactionStore recentTransactionStore; + private DynamicPropertiesStore dynamicPropertiesStore; + private final Path cacheFile0; private final Path cacheFile1; private final Path cacheProperties; @@ -85,11 +87,13 @@ public class TxCacheDB implements DB, Flusher { @Setter private volatile boolean alive; - public TxCacheDB(String name, RecentTransactionStore recentTransactionStore) { + public TxCacheDB(String name, RecentTransactionStore recentTransactionStore, + DynamicPropertiesStore dynamicPropertiesStore) { this.name = name; this.TRANSACTION_COUNT = CommonParameter.getInstance().getStorage().getEstimatedBlockTransactions(); this.recentTransactionStore = recentTransactionStore; + this.dynamicPropertiesStore = dynamicPropertiesStore; String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine(); if ("LEVELDB".equals(dbEngine.toUpperCase())) { this.persistentStore = new LevelDB( @@ -211,7 +215,6 @@ public void put(byte[] key, byte[] value) { MAX_BLOCK_SIZE * TRANSACTION_COUNT); } bloomFilters[currentFilterIndex].put(key); - currentBlockNum = blockNum; if (lastMetricBlock != blockNum) { lastMetricBlock = blockNum; Metrics.gaugeSet(MetricKeys.Gauge.TX_CACHE, @@ -356,8 +359,14 @@ private boolean loadProperties() { Properties properties = new Properties(); properties.load(r); filterStartBlock = Long.parseLong(properties.getProperty("filterStartBlock")); - currentBlockNum = Long.parseLong(properties.getProperty("currentBlockNum")); + long currentBlockNum = Long.parseLong(properties.getProperty("currentBlockNum")); + long currentBlockNumFromDB = dynamicPropertiesStore.getLatestBlockHeaderNumberFromDB(); currentFilterIndex = Integer.parseInt(properties.getProperty("currentFilterIndex")); + if (currentBlockNum != currentBlockNumFromDB) { + throw new IllegalStateException( + String.format("currentBlockNum not match. filter: %d, db: %d", + currentBlockNum, currentBlockNumFromDB)); + } logger.info("filterStartBlock: {}, currentBlockNum: {}, currentFilterIndex: {}, load done.", filterStartBlock, currentBlockNum, currentFilterIndex); return true; @@ -369,6 +378,7 @@ private boolean loadProperties() { private void writeProperties() { try (Writer w = Files.newBufferedWriter(this.cacheProperties, StandardCharsets.UTF_8)) { Properties properties = new Properties(); + long currentBlockNum = dynamicPropertiesStore.getLatestBlockHeaderNumberFromDB(); properties.setProperty("filterStartBlock", String.valueOf(filterStartBlock)); properties.setProperty("currentBlockNum", String.valueOf(currentBlockNum)); properties.setProperty("currentFilterIndex", String.valueOf(currentFilterIndex)); @@ -382,7 +392,7 @@ private void writeProperties() { @Override public TxCacheDB newInstance() { - return new TxCacheDB(name, recentTransactionStore); + return new TxCacheDB(name, recentTransactionStore, dynamicPropertiesStore); } @Override diff --git a/framework/src/main/java/org/tron/core/config/DefaultConfig.java b/framework/src/main/java/org/tron/core/config/DefaultConfig.java index 2c4c2a8717e..7cc32d9a581 100755 --- a/framework/src/main/java/org/tron/core/config/DefaultConfig.java +++ b/framework/src/main/java/org/tron/core/config/DefaultConfig.java @@ -11,9 +11,7 @@ import org.springframework.context.annotation.Import; import org.tron.common.utils.StorageUtils; import org.tron.core.config.args.Args; -import org.tron.core.db.RecentTransactionStore; import org.tron.core.db.RevokingDatabase; -import org.tron.core.db.TransactionCache; import org.tron.core.db.backup.BackupRocksDBAspect; import org.tron.core.db.backup.NeedBeanCondition; import org.tron.core.db2.core.SnapshotManager; @@ -93,11 +91,6 @@ public HttpApiOnPBFTService getHttpApiOnPBFTService() { return null; } - @Bean - public TransactionCache transactionCache() { - return new TransactionCache("trans-cache", appCtx.getBean(RecentTransactionStore.class)); - } - @Bean @Conditional(NeedBeanCondition.class) public BackupRocksDBAspect backupRocksDBAspect() { 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 9a2f53dd69a..b0d902eb84a 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -179,8 +179,8 @@ public class Manager { @Setter public boolean eventPluginLoaded = false; private int maxTransactionPendingSize = Args.getInstance().getMaxTransactionPendingSize(); - @Autowired(required = false) @Getter + @Autowired private TransactionCache transactionCache; @Autowired private KhaosDatabase khaosDb; diff --git a/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java b/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java index e415476d739..b976cf5f2da 100644 --- a/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java +++ b/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java @@ -15,6 +15,7 @@ import org.tron.core.capsule.BytesCapsule; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; +import org.tron.core.store.DynamicPropertiesStore; import org.tron.keystore.Wallet; @Slf4j @@ -53,11 +54,22 @@ public void reload() { queryTransaction(); db.close(); defaultListableBeanFactory.destroySingleton("transactionCache"); - TransactionCache transactionCache = new TransactionCache("transactionCache", - context.getBean(RecentTransactionStore.class)); - transactionCache.initCache(); - defaultListableBeanFactory.registerSingleton("transactionCache",transactionCache); + db = new TransactionCache("transactionCache", + context.getBean(RecentTransactionStore.class), + context.getBean(DynamicPropertiesStore.class)); + db.initCache(); + defaultListableBeanFactory.registerSingleton("transactionCache",db); queryTransaction(); + db.close(); + defaultListableBeanFactory.destroySingleton("transactionCache"); + db = new TransactionCache("transactionCache", + context.getBean(RecentTransactionStore.class), + context.getBean(DynamicPropertiesStore.class)); + DynamicPropertiesStore dynamicPropertiesStore = context.getBean(DynamicPropertiesStore.class); + dynamicPropertiesStore.saveLatestBlockHeaderNumber(1); + defaultListableBeanFactory.registerSingleton("transactionCache",db); + db.initCache(); + Assert.assertFalse(db.has(hash[65538])); } private void putTransaction() { From 9842b3ff1416c26b27d0296c334b03e4c483e68e Mon Sep 17 00:00:00 2001 From: lxcmyf Date: Thu, 21 Sep 2023 18:36:31 +0800 Subject: [PATCH 16/31] fix(freezeV2): remove duplicate judgments (#5508) --- .../java/org/tron/core/actuator/DelegateResourceActuator.java | 2 +- .../tron/core/vm/nativecontract/DelegateResourceProcessor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java b/actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java index 776bd952711..161f22cfb17 100755 --- a/actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java +++ b/actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java @@ -189,7 +189,7 @@ public boolean validate() throws ContractValidateException { byte[] receiverAddress = delegateResourceContract.getReceiverAddress().toByteArray(); - if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) { + if (!DecodeUtil.addressValid(receiverAddress)) { throw new ContractValidateException("Invalid receiverAddress"); } diff --git a/actuator/src/main/java/org/tron/core/vm/nativecontract/DelegateResourceProcessor.java b/actuator/src/main/java/org/tron/core/vm/nativecontract/DelegateResourceProcessor.java index b3772970248..bb0d4e8297c 100644 --- a/actuator/src/main/java/org/tron/core/vm/nativecontract/DelegateResourceProcessor.java +++ b/actuator/src/main/java/org/tron/core/vm/nativecontract/DelegateResourceProcessor.java @@ -93,7 +93,7 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra byte[] receiverAddress = param.getReceiverAddress(); - if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) { + if (!DecodeUtil.addressValid(receiverAddress)) { throw new ContractValidateException("Invalid receiverAddress"); } if (Arrays.equals(receiverAddress, ownerAddress)) { From 6802f34064627fec15dcb20ed7c2a503fa7951c0 Mon Sep 17 00:00:00 2001 From: lurais <107600021+lurais@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:26:55 +0800 Subject: [PATCH 17/31] feat(test): add db test (#5461) Co-authored-by: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Co-authored-by: morgan.peng --- .../leveldb/LevelDbDataSourceImplTest.java | 9 ++ .../leveldb/RocksDbDataSourceImplTest.java | 9 ++ .../java/org/tron/core/db/AbiStoreTest.java | 61 +++++++ .../tron/core/db/AccountTraceStoreTest.java | 57 +++++++ .../tron/core/db/BalanceTraceStoreTest.java | 131 +++++++++++++++ .../java/org/tron/core/db/CodeStoreTest.java | 74 +++++++++ ...elegatedResourceAccountIndexStoreTest.java | 150 ++++++++++++++++++ 7 files changed, 491 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/db/AbiStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/AccountTraceStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/BalanceTraceStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/CodeStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/DelegatedResourceAccountIndexStoreTest.java 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 c774d10c172..50a55d24a0f 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 @@ -27,7 +27,9 @@ import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -113,6 +115,13 @@ public void testReset() { Args.getInstance().getOutputDirectory(), "test_reset"); dataSource.resetDb(); assertEquals(0, dataSource.allKeys().size()); + assertEquals("LEVELDB", dataSource.getEngine()); + assertEquals("test_reset", dataSource.getName()); + assertEquals(Sets.newHashSet(), dataSource.getlatestValues(0)); + assertEquals(Collections.emptyMap(), dataSource.getNext(key1, 0)); + assertEquals(new ArrayList<>(), dataSource.getKeysNext(key1, 0)); + assertEquals(Sets.newHashSet(), dataSource.getValuesNext(key1, 0)); + assertEquals(Sets.newHashSet(), dataSource.getlatestValues(0)); dataSource.closeDB(); } diff --git a/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java b/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java index 51a5ddc96ee..ed37c1e4bcd 100644 --- a/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java +++ b/framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java @@ -9,7 +9,9 @@ import com.google.common.collect.Sets; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -95,6 +97,13 @@ public void testReset() { Args.getInstance().getOutputDirectory(), "test_reset"); dataSource.resetDb(); assertEquals(0, dataSource.allKeys().size()); + assertEquals("ROCKSDB", dataSource.getEngine()); + assertEquals("test_reset", dataSource.getName()); + assertEquals(Sets.newHashSet(), dataSource.getlatestValues(0)); + assertEquals(Collections.emptyMap(), dataSource.getNext(key1, 0)); + assertEquals(new ArrayList<>(), dataSource.getKeysNext(key1, 0)); + assertEquals(Sets.newHashSet(), dataSource.getValuesNext(key1, 0)); + assertEquals(Sets.newHashSet(), dataSource.getlatestValues(0)); dataSource.closeDB(); } diff --git a/framework/src/test/java/org/tron/core/db/AbiStoreTest.java b/framework/src/test/java/org/tron/core/db/AbiStoreTest.java new file mode 100644 index 00000000000..0cb134c50ce --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/AbiStoreTest.java @@ -0,0 +1,61 @@ +package org.tron.core.db; + +import static org.tron.common.utils.PublicMethod.jsonStr2Abi; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.AbiCapsule; +import org.tron.core.capsule.AccountCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.AbiStore; +import org.tron.core.store.AccountIndexStore; +import org.tron.protos.Protocol.AccountType; +import org.tron.protos.contract.SmartContractOuterClass; + +public class AbiStoreTest extends BaseTest { + + @Resource + private AbiStore abiStore; + + private static final byte[] contractAddr = Hex.decode( + "41000000000000000000000000000000000000dEaD"); + + private static final SmartContractOuterClass.SmartContract.ABI SOURCE_ABI = jsonStr2Abi( + "[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\"" + + ":\"constructor\"}]"); + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testPut() { + abiStore.put(contractAddr, new AbiCapsule(SOURCE_ABI)); + Assert.assertEquals(abiStore.has(contractAddr), Boolean.TRUE); + } + + @Test + public void testGet() { + abiStore.put(contractAddr, new AbiCapsule(SOURCE_ABI)); + AbiCapsule abiCapsule = abiStore.get(contractAddr); + Assert.assertEquals(abiCapsule.getInstance(), SOURCE_ABI); + } + + @Test + public void testGetTotalAbi() { + abiStore.put(contractAddr, new AbiCapsule(SOURCE_ABI)); + Assert.assertEquals(abiStore.getTotalABIs(), 1); + } +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/db/AccountTraceStoreTest.java b/framework/src/test/java/org/tron/core/db/AccountTraceStoreTest.java new file mode 100644 index 00000000000..aa87f903ad3 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/AccountTraceStoreTest.java @@ -0,0 +1,57 @@ +package org.tron.core.db; + +import com.google.common.primitives.Bytes; +import com.google.common.primitives.Longs; +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +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.exception.BadItemException; +import org.tron.core.exception.ItemNotFoundException; +import org.tron.core.store.AccountIndexStore; +import org.tron.core.store.AccountTraceStore; +import org.tron.protos.Protocol.AccountType; + +public class AccountTraceStoreTest extends BaseTest { + + @Resource + private AccountTraceStore accountTraceStore; + private static byte[] address = TransactionStoreTest.randomBytes(32); + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + + @Test + public void testRecordBalanceWithBlock() throws BadItemException, ItemNotFoundException { + accountTraceStore.recordBalanceWithBlock(address,1,9999); + Assert.assertNotNull(accountTraceStore.get(Bytes.concat(address, + Longs.toByteArray(1L ^ Long.MAX_VALUE)))); + } + + @Test + public void testGetPrevBalance() { + accountTraceStore.recordBalanceWithBlock(address,2,9999); + Pair pair = accountTraceStore.getPrevBalance(address,2); + Assert.assertEquals((long)pair.getKey(),2L); + Assert.assertEquals((long)pair.getValue(), 0L); + byte[] address2 = TransactionStoreTest.randomBytes(21); + accountTraceStore.recordBalanceWithBlock(address2,3,99); + Pair pair2 = accountTraceStore.getPrevBalance(address2, 3); + Assert.assertEquals((long)pair2.getKey(),3L); + Assert.assertEquals((long)pair2.getValue(), 99L); + } +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/db/BalanceTraceStoreTest.java b/framework/src/test/java/org/tron/core/db/BalanceTraceStoreTest.java new file mode 100644 index 00000000000..82547a997da --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/BalanceTraceStoreTest.java @@ -0,0 +1,131 @@ +package org.tron.core.db; + +import static org.junit.Assert.assertEquals; +import static org.tron.protos.Protocol.Transaction.Contract.ContractType.TransferContract; +import static org.tron.protos.Protocol.Transaction.Result.contractResult.SUCCESS; + +import com.google.protobuf.ByteString; +import java.util.Arrays; +import javax.annotation.Resource; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.BlockBalanceTraceCapsule; +import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.BalanceTraceStore; +import org.tron.protos.Protocol; +import org.tron.protos.contract.BalanceContract; + + +public class BalanceTraceStoreTest extends BaseTest { + + @Resource + private BalanceTraceStore balanceTraceStoreUnderTest; + + private static final byte[] contractAddr = Hex.decode( + "41000000000000000000000000000000000000dEaD"); + + BlockCapsule blockCapsule = new BlockCapsule(Protocol.Block.newBuilder().setBlockHeader( + Protocol.BlockHeader.newBuilder().setRawData(Protocol.BlockHeader.raw.newBuilder() + .setParentHash(ByteString.copyFrom(ByteArray.fromHexString( + "0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))))).build()); + final TransactionCapsule transactionCapsule = + new TransactionCapsule(Protocol.Transaction.newBuilder().build()); + BalanceContract.TransactionBalanceTrace transactionBalanceTrace = + BalanceContract.TransactionBalanceTrace.newBuilder() + .setTransactionIdentifier(transactionCapsule.getTransactionId().getByteString()) + .setType(TransferContract.name()) + .setStatus(SUCCESS.name()) + .build(); + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Before + public void clear() { + balanceTraceStoreUnderTest.resetCurrentTransactionTrace(); + balanceTraceStoreUnderTest.resetCurrentBlockTrace(); + } + + + @Test + public void testSetCurrentTransactionId() throws Exception { + balanceTraceStoreUnderTest.setCurrentBlockId(blockCapsule); + balanceTraceStoreUnderTest.setCurrentTransactionId(transactionCapsule); + Assert.assertEquals(balanceTraceStoreUnderTest.getCurrentTransactionId(), + transactionCapsule.getTransactionId()); + } + + @Test + public void testSetCurrentBlockId() { + balanceTraceStoreUnderTest.setCurrentBlockId(blockCapsule); + Assert.assertEquals(blockCapsule.getBlockId(), balanceTraceStoreUnderTest.getCurrentBlockId()); + } + + @Test + public void testResetCurrentTransactionTrace() { + balanceTraceStoreUnderTest.setCurrentBlockId(blockCapsule); + balanceTraceStoreUnderTest.setCurrentTransactionId(transactionCapsule); + balanceTraceStoreUnderTest.resetCurrentTransactionTrace(); + balanceTraceStoreUnderTest.resetCurrentBlockTrace(); + Assert.assertNotNull(balanceTraceStoreUnderTest.getCurrentTransactionId()); + Assert.assertNull(balanceTraceStoreUnderTest.getCurrentTransactionBalanceTrace()); + } + + @Test + public void testInitCurrentBlockBalanceTrace() { + balanceTraceStoreUnderTest.initCurrentBlockBalanceTrace(blockCapsule); + Assert.assertNull(balanceTraceStoreUnderTest.getCurrentBlockId()); + } + + @Test + public void testInitCurrentTransactionBalanceTrace() { + balanceTraceStoreUnderTest.setCurrentBlockId(blockCapsule); + balanceTraceStoreUnderTest.initCurrentTransactionBalanceTrace(transactionCapsule); + Assert.assertEquals(blockCapsule.getBlockId(), balanceTraceStoreUnderTest.getCurrentBlockId()); + Assert.assertNull(balanceTraceStoreUnderTest.getCurrentTransactionId()); + } + + @Test + public void testUpdateCurrentTransactionStatus() { + balanceTraceStoreUnderTest.setCurrentBlockId(blockCapsule); + balanceTraceStoreUnderTest.updateCurrentTransactionStatus(""); + Assert.assertNull(balanceTraceStoreUnderTest.getCurrentTransactionBalanceTrace()); + } + + @Test + public void testGetBlockBalanceTrace() throws Exception { + BlockBalanceTraceCapsule blockBalanceTraceCapsule = new BlockBalanceTraceCapsule(blockCapsule); + balanceTraceStoreUnderTest.put(ByteArray.fromLong(blockCapsule.getNum()), + blockBalanceTraceCapsule); + final BlockBalanceTraceCapsule result = + balanceTraceStoreUnderTest.getBlockBalanceTrace(blockCapsule.getBlockId()); + assertEquals(Arrays.toString(result.getData()), + Arrays.toString(blockBalanceTraceCapsule.getData())); + } + + @Test + public void testGetTransactionBalanceTrace() throws Exception { + BlockBalanceTraceCapsule blockBalanceTraceCapsule = new BlockBalanceTraceCapsule(blockCapsule); + blockBalanceTraceCapsule.addTransactionBalanceTrace(transactionBalanceTrace); + balanceTraceStoreUnderTest.put(ByteArray.fromLong(blockCapsule.getNum()), + blockBalanceTraceCapsule); + final BalanceContract.TransactionBalanceTrace result = + balanceTraceStoreUnderTest.getTransactionBalanceTrace(blockCapsule.getBlockId(), + transactionCapsule.getTransactionId()); + Assert.assertEquals(result.getStatus(),"SUCCESS"); + } + +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/db/CodeStoreTest.java b/framework/src/test/java/org/tron/core/db/CodeStoreTest.java new file mode 100644 index 00000000000..59bfba2236a --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/CodeStoreTest.java @@ -0,0 +1,74 @@ +package org.tron.core.db; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import javax.annotation.Resource; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.CodeCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.CodeStore; + +public class CodeStoreTest extends BaseTest { + + private static final byte[] contractAddr1 = Hex.decode( + "41000000000000000000000000000000000000dEaD"); + private static final byte[] contractAddr2 = Hex.decode( + "41000000000000000000000000000000000000dEbD"); + private static final byte[] contractAddr3 = Hex.decode( + "41000000000000000000000000000000000000dEcD"); + + private static String codeString = + "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d28015" + + "61002a57600080fd5b50600436106100495760003560e01c806385bb7d69146100555761004a565b5b61" + + "0052610073565b50005b61005d610073565b60405161006a91906100b9565b60405180910390f35b6000" + + "80600090505b60028110156100a657808261009091906100d4565b915060018161009f91906100d4565b" + + "905061007b565b5090565b6100b38161012a565b82525050565b60006020820190506100ce6000830184" + + "6100aa565b92915050565b60006100df8261012a565b91506100ea8361012a565b9250827fffffffffff" + + "ffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561011f5761011e61013456" + + "5b5b828201905092915050565b6000819050919050565b7f4e487b710000000000000000000000000000" + + "0000000000000000000000000000600052601160045260246000fdfea26474726f6e58221220f3d01983" + + "23c67293b97323c101e294e6d2cac7fb29555292675277e11c275a4b64736f6c63430008060033"; + private static final CodeCapsule codeCapsule = new CodeCapsule(ByteArray + .fromHexString(codeString)); + + @Resource + private CodeStore codeStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() { + codeStore.put(contractAddr1, codeCapsule); + final CodeCapsule result = codeStore.get(contractAddr1); + assertEquals(result.toString(), Arrays.toString(ByteArray.fromHexString(codeString))); + } + + @Test + public void testGetTotalCodes() throws Exception { + codeStore.put(contractAddr1, codeCapsule); + codeStore.put(codeCapsule.getCodeHash().getBytes(), codeCapsule); + final long result = codeStore.getTotalCodes(); + assertEquals(2L, result); + } + + @Test + public void testFindCodeByHash() { + codeStore.put(codeCapsule.getCodeHash().getBytes(), codeCapsule); + final byte[] result = codeStore.findCodeByHash(codeCapsule.getCodeHash().getBytes()); + assertEquals(Arrays.toString(result), Arrays.toString(ByteArray.fromHexString(codeString))); + } +} \ No newline at end of file diff --git a/framework/src/test/java/org/tron/core/db/DelegatedResourceAccountIndexStoreTest.java b/framework/src/test/java/org/tron/core/db/DelegatedResourceAccountIndexStoreTest.java new file mode 100644 index 00000000000..fd5932603e3 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/DelegatedResourceAccountIndexStoreTest.java @@ -0,0 +1,150 @@ +package org.tron.core.db; + +import com.google.common.primitives.Bytes; +import com.google.protobuf.ByteString; +import java.util.Collections; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.common.utils.DecodeUtil; +import org.tron.core.Constant; +import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.DelegatedResourceAccountIndexStore; + + +public class DelegatedResourceAccountIndexStoreTest extends BaseTest { + + @Resource + private DelegatedResourceAccountIndexStore delegatedResourceAccountIndexStore; + + String owner1 = DecodeUtil.addressPreFixString + "548794500882809695a8a687866e76d4271a1abc"; + private static final byte[] FROM_PREFIX = {0x01}; + private static final byte[] TO_PREFIX = {0x02}; + private static final byte[] V2_FROM_PREFIX = {0x03}; + private static final byte[] V2_TO_PREFIX = {0x04}; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() { + delegatedResourceAccountIndexStore.put(ByteArray.fromHexString(owner1), + new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom("testGet".getBytes()))); + final DelegatedResourceAccountIndexCapsule result = + delegatedResourceAccountIndexStore.get(ByteArray.fromHexString(owner1)); + Assert.assertNotNull(result); + Assert.assertEquals(result.getAccount(), ByteString.copyFrom("testGet".getBytes())); + } + + @Test + public void testConvert() { + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = + new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom("testConvert".getBytes())); + delegatedResourceAccountIndexCapsule.setAllFromAccounts( + Collections.singletonList(ByteString.copyFrom("testConvertFrom".getBytes()))); + delegatedResourceAccountIndexCapsule.setAllToAccounts( + Collections.singletonList(ByteString.copyFrom("testConvertTo".getBytes()))); + delegatedResourceAccountIndexStore.put(ByteArray.fromHexString(owner1), + delegatedResourceAccountIndexCapsule); + delegatedResourceAccountIndexStore.convert(ByteArray.fromHexString(owner1)); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule1 = + delegatedResourceAccountIndexStore.get(Bytes.concat(FROM_PREFIX, + ByteArray.fromHexString(owner1), "testConvertTo".getBytes())); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule2 = + delegatedResourceAccountIndexStore.get(Bytes.concat(TO_PREFIX, + "testConvertTo".getBytes(), ByteArray.fromHexString(owner1))); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule1); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule2); + } + + @Test + public void testDelegate() throws Exception { + delegatedResourceAccountIndexStore.delegate("testDelegateFrom".getBytes(), + "testDelegateTo".getBytes(),1L); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule1 = + delegatedResourceAccountIndexStore.get(Bytes.concat(FROM_PREFIX, + "testDelegateFrom".getBytes(), "testDelegateTo".getBytes())); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule2 = + delegatedResourceAccountIndexStore.get(Bytes.concat(TO_PREFIX, "testDelegateTo".getBytes(), + "testDelegateFrom".getBytes())); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule1); + Assert.assertEquals(delegatedResourceAccountIndexCapsule1.getTimestamp(),1); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule2); + Assert.assertEquals(delegatedResourceAccountIndexCapsule2.getTimestamp(),1); + } + + @Test + public void testDelegateV2() { + delegatedResourceAccountIndexStore.delegateV2("testDelegatev2From".getBytes(), + "testDelegatev2To".getBytes(),2L); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule1 = + delegatedResourceAccountIndexStore.get(Bytes.concat(V2_FROM_PREFIX, + "testDelegatev2From".getBytes(), "testDelegatev2To".getBytes())); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule2 = + delegatedResourceAccountIndexStore.get(Bytes.concat(V2_TO_PREFIX, + "testDelegatev2To".getBytes(), "testDelegatev2From".getBytes())); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule1); + Assert.assertEquals(delegatedResourceAccountIndexCapsule1.getTimestamp(),2); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule2); + Assert.assertEquals(delegatedResourceAccountIndexCapsule2.getTimestamp(),2); + } + + @Test + public void testUnDelegate() throws Exception { + delegatedResourceAccountIndexStore.delegate("testDelegateFrom".getBytes(), + "testDelegateTo".getBytes(),1L); + delegatedResourceAccountIndexStore.unDelegate("testDelegateFrom".getBytes(), + "testDelegateTo".getBytes()); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule1 = + delegatedResourceAccountIndexStore.get(Bytes.concat(FROM_PREFIX, + "testDelegateFrom".getBytes(), "testDelegateTo".getBytes())); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule2 = + delegatedResourceAccountIndexStore.get(Bytes.concat(TO_PREFIX, + "testDelegateTo".getBytes(), "testDelegateFrom".getBytes())); + Assert.assertNull(delegatedResourceAccountIndexCapsule1); + Assert.assertNull(delegatedResourceAccountIndexCapsule2); + } + + @Test + public void testUnDelegateV2() { + delegatedResourceAccountIndexStore.delegateV2("testDelegateFrom".getBytes(), + "testDelegateTo".getBytes(),1L); + delegatedResourceAccountIndexStore.unDelegateV2("testDelegateFrom".getBytes(), + "testDelegateTo".getBytes()); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule1 = + delegatedResourceAccountIndexStore.get(Bytes.concat(V2_FROM_PREFIX, + "testDelegateFrom".getBytes(), "testDelegateTo".getBytes())); + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule2 = + delegatedResourceAccountIndexStore.get(Bytes.concat(V2_TO_PREFIX, + "testDelegateTo".getBytes(), "testDelegateFrom".getBytes())); + Assert.assertNull(delegatedResourceAccountIndexCapsule1); + Assert.assertNull(delegatedResourceAccountIndexCapsule2); + } + + @Test + public void testGetIndex() throws Exception { + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = + delegatedResourceAccountIndexStore.getIndex("testGetIndex".getBytes()); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule.getFromAccountsList()); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule.getToAccountsList()); + } + + @Test + public void testGetV2Index() { + DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = + delegatedResourceAccountIndexStore.getV2Index("testGetV2Index".getBytes()); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule.getFromAccountsList()); + Assert.assertNotNull(delegatedResourceAccountIndexCapsule.getToAccountsList()); + } +} \ No newline at end of file From c78c42e77a8db0fcbd3ece5fe1e83cefc71debbd Mon Sep 17 00:00:00 2001 From: lurais <107600021+lurais@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:02:41 +0800 Subject: [PATCH 18/31] test(db): add test case (#5503) Co-authored-by: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Co-authored-by: morgan.peng --- .../org/tron/core/db/ExchangeStoreTest.java | 62 ++++++++++++++ .../org/tron/core/db/ExchangeV2StoreTest.java | 44 ++++++++++ .../db/IncrementalMerkleTreeStoreTest.java | 51 ++++++++++++ .../tron/core/db/MarketAccountStoreTest.java | 37 +++++++++ .../tron/core/db/MarketOrderStoreTest.java | 47 +++++++++++ .../core/db/MarketPairToPriceStoreTest.java | 83 +++++++++++++++++++ .../org/tron/core/db/ProposalStoreTest.java | 72 ++++++++++++++++ .../tron/core/db/TreeBlockIndexStoreTest.java | 52 ++++++++++++ 8 files changed, 448 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/db/ExchangeStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/ExchangeV2StoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/IncrementalMerkleTreeStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/MarketAccountStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/MarketOrderStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/MarketPairToPriceStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/ProposalStoreTest.java create mode 100644 framework/src/test/java/org/tron/core/db/TreeBlockIndexStoreTest.java diff --git a/framework/src/test/java/org/tron/core/db/ExchangeStoreTest.java b/framework/src/test/java/org/tron/core/db/ExchangeStoreTest.java new file mode 100644 index 00000000000..f48ac11301d --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/ExchangeStoreTest.java @@ -0,0 +1,62 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import java.util.List; +import javax.annotation.Resource; +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.capsule.ExchangeCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.ExchangeStore; +import org.tron.protos.Protocol; + +public class ExchangeStoreTest extends BaseTest { + + @Resource + private ExchangeStore exchangeStore; + private byte[] exchangeKey1; + private byte[] exchangeKey2; + + static { + Args.setParam( + new String[] { + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + Protocol.Exchange.Builder builder = Protocol.Exchange.newBuilder(); + builder.setExchangeId(1L).setCreatorAddress(ByteString.copyFromUtf8("Address1")); + ExchangeCapsule exchangeCapsule = new ExchangeCapsule(builder.build()); + exchangeKey1 = exchangeCapsule.createDbKey(); + chainBaseManager.getExchangeStore().put(exchangeKey1, exchangeCapsule); + builder.setExchangeId(2L).setCreatorAddress(ByteString.copyFromUtf8("Address2")); + exchangeCapsule = new ExchangeCapsule(builder.build()); + exchangeKey2 = exchangeCapsule.createDbKey(); + chainBaseManager.getExchangeStore().put(exchangeKey2, exchangeCapsule); + } + + + @Test + public void testGet() throws Exception { + final ExchangeCapsule result = exchangeStore.get(exchangeKey1); + Assert.assertNotNull(result); + Assert.assertEquals(result.getID(), 1); + } + + @Test + public void testGetAllExchanges() { + List exchangeCapsuleList = exchangeStore.getAllExchanges(); + ExchangeCapsule exchangeCapsule1 = exchangeCapsuleList.get(0); + ExchangeCapsule exchangeCapsule2 = exchangeCapsuleList.get(1); + Assert.assertEquals(exchangeCapsuleList.size(), 2); + Assert.assertEquals(exchangeCapsule1.getCreatorAddress(), ByteString.copyFromUtf8("Address1")); + Assert.assertEquals(exchangeCapsule2.getCreatorAddress(), ByteString.copyFromUtf8("Address2")); + } +} diff --git a/framework/src/test/java/org/tron/core/db/ExchangeV2StoreTest.java b/framework/src/test/java/org/tron/core/db/ExchangeV2StoreTest.java new file mode 100644 index 00000000000..2b1e2ea31ea --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/ExchangeV2StoreTest.java @@ -0,0 +1,44 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.capsule.ExchangeCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.ExchangeV2Store; +import org.tron.protos.Protocol; + +public class ExchangeV2StoreTest extends BaseTest { + + @Resource + private ExchangeV2Store exchangeV2Store; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() throws Exception { + byte[] key = putToExchangeV2(); + final ExchangeCapsule result = exchangeV2Store.get(key); + Assert.assertNotNull(result); + Assert.assertEquals(result.getID(), 1); + } + + private byte[] putToExchangeV2() { + Protocol.Exchange.Builder builder = Protocol.Exchange.newBuilder().setExchangeId(1L) + .setCreatorAddress(ByteString.copyFromUtf8("Address1")); + ExchangeCapsule exchangeCapsule = new ExchangeCapsule(builder.build()); + byte[] exchangeKey1 = exchangeCapsule.createDbKey(); + exchangeV2Store.put(exchangeKey1, exchangeCapsule); + return exchangeKey1; + } +} diff --git a/framework/src/test/java/org/tron/core/db/IncrementalMerkleTreeStoreTest.java b/framework/src/test/java/org/tron/core/db/IncrementalMerkleTreeStoreTest.java new file mode 100644 index 00000000000..643f86d3fe5 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/IncrementalMerkleTreeStoreTest.java @@ -0,0 +1,51 @@ +package org.tron.core.db; + +import javax.annotation.Resource; +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.capsule.IncrementalMerkleTreeCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.IncrementalMerkleTreeStore; + +public class IncrementalMerkleTreeStoreTest extends BaseTest { + + private static final byte[] incrementalMerkleTreeData = {10, 0}; + + @Resource + private IncrementalMerkleTreeStore incrementalMerkleTreeStore; + + static { + Args.setParam( + new String[] { + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + incrementalMerkleTreeStore.put("Address1".getBytes(), new IncrementalMerkleTreeCapsule( + incrementalMerkleTreeData)); + } + + @Test + public void testGet() throws Exception { + final IncrementalMerkleTreeCapsule result = + incrementalMerkleTreeStore.get("Address1".getBytes()); + Assert.assertNotNull(result); + Assert.assertEquals(result.getInstance(), new IncrementalMerkleTreeCapsule( + incrementalMerkleTreeData).getInstance()); + } + + @Test + public void testContain() throws Exception { + final boolean result1 = incrementalMerkleTreeStore.contain("Address1".getBytes()); + final boolean result2 = incrementalMerkleTreeStore.contain("Address2".getBytes()); + Assert.assertTrue(result1); + Assert.assertFalse(result2); + } +} diff --git a/framework/src/test/java/org/tron/core/db/MarketAccountStoreTest.java b/framework/src/test/java/org/tron/core/db/MarketAccountStoreTest.java new file mode 100644 index 00000000000..51bf1c5d7fc --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/MarketAccountStoreTest.java @@ -0,0 +1,37 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.capsule.MarketAccountOrderCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.MarketAccountStore; + +public class MarketAccountStoreTest extends BaseTest { + + @Resource + private MarketAccountStore marketAccountStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() throws Exception { + String address = "Address1"; + marketAccountStore.put(address.getBytes(), + new MarketAccountOrderCapsule(ByteString.copyFrom(address.getBytes()))); + final MarketAccountOrderCapsule result = marketAccountStore.get(address.getBytes()); + Assert.assertNotNull(result); + Assert.assertEquals(result.getOwnerAddress(), ByteString.copyFrom(address.getBytes())); + } + +} diff --git a/framework/src/test/java/org/tron/core/db/MarketOrderStoreTest.java b/framework/src/test/java/org/tron/core/db/MarketOrderStoreTest.java new file mode 100644 index 00000000000..33126783e9a --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/MarketOrderStoreTest.java @@ -0,0 +1,47 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.capsule.MarketOrderCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.MarketOrderStore; +import org.tron.protos.Protocol; + +public class MarketOrderStoreTest extends BaseTest { + + @Resource + private MarketOrderStore marketOrderStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() throws Exception { + byte[] orderId = "testGet".getBytes(); + marketOrderStore.put(orderId, + new MarketOrderCapsule(Protocol.MarketOrder.newBuilder() + .setOrderId(ByteString.copyFrom(orderId)) + .setSellTokenId(ByteString.copyFrom("addr1".getBytes())) + .setSellTokenQuantity(200L) + .setBuyTokenId(ByteString.copyFrom("addr2".getBytes())) + .setBuyTokenQuantity(100L) + .build())); + final MarketOrderCapsule result = marketOrderStore.get(orderId); + Assert.assertNotNull(result); + Assert.assertEquals(new String(result.getSellTokenId()), "addr1"); + Assert.assertEquals(result.getSellTokenQuantity(), 200L); + Assert.assertEquals(new String(result.getBuyTokenId()), "addr2"); + Assert.assertEquals(result.getBuyTokenQuantity(), 100L); + } + +} diff --git a/framework/src/test/java/org/tron/core/db/MarketPairToPriceStoreTest.java b/framework/src/test/java/org/tron/core/db/MarketPairToPriceStoreTest.java new file mode 100644 index 00000000000..adf315bb92e --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/MarketPairToPriceStoreTest.java @@ -0,0 +1,83 @@ +package org.tron.core.db; + +import static org.junit.Assert.assertEquals; + +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.BytesCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.store.MarketPairPriceToOrderStore; +import org.tron.core.store.MarketPairToPriceStore; + +public class MarketPairToPriceStoreTest extends BaseTest { + + @Resource + private MarketPairToPriceStore marketPairToPriceStore; + + @Resource + private MarketPairPriceToOrderStore marketPairPriceToOrderStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testGet() throws Exception { + marketPairToPriceStore.put("testGet".getBytes(), new BytesCapsule( + ByteArray.fromString("11.0"))); + final BytesCapsule result = marketPairToPriceStore.get("testGet".getBytes()); + Assert.assertNotNull(result); + Assert.assertEquals(new String(result.getData()), "11.0"); + } + + @Test + public void testGetPriceNum() { + marketPairToPriceStore.put("testGetPriceNum".getBytes(), new BytesCapsule( + ByteArray.fromLong(100))); + final long result = marketPairToPriceStore.getPriceNum("testGetPriceNum".getBytes()); + assertEquals(100L, result); + assertEquals(0L, marketPairToPriceStore.getPriceNum("testGetPriceNum1".getBytes())); + } + + @Test + public void testGetPriceNumByTokenId() { + marketPairToPriceStore.setPriceNum("tokenId1".getBytes(), "tokenId2".getBytes(),99); + final long result = + marketPairToPriceStore.getPriceNum("tokenId1".getBytes(), "tokenId2".getBytes()); + assertEquals(99L, result); + assertEquals(0L, marketPairToPriceStore.getPriceNum("tokenId2".getBytes(), + "tokenId1".getBytes())); + } + + @Test + public void testSetPriceNum() { + marketPairToPriceStore.setPriceNum("testSetPriceNum1".getBytes(), 98L); + long result = marketPairToPriceStore.getPriceNum("testSetPriceNum1".getBytes()); + assertEquals(result, 98); + } + + @Test + public void testSetPriceNumByToken() { + marketPairToPriceStore.setPriceNum("token3".getBytes(), "token4".getBytes(), 97L); + long result = marketPairToPriceStore.getPriceNum("token3".getBytes(), "token4".getBytes()); + assertEquals(result, 97); + } + + @Test + public void testAddNewPriceKey() { + marketPairToPriceStore + .addNewPriceKey("token5".getBytes(), "token6".getBytes(), marketPairPriceToOrderStore); + long result = marketPairToPriceStore.getPriceNum("token5".getBytes(), "token6".getBytes()); + assertEquals(1, result); + } + +} diff --git a/framework/src/test/java/org/tron/core/db/ProposalStoreTest.java b/framework/src/test/java/org/tron/core/db/ProposalStoreTest.java new file mode 100644 index 00000000000..b08402d33a0 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/ProposalStoreTest.java @@ -0,0 +1,72 @@ +package org.tron.core.db; + +import static org.junit.Assert.assertThrows; + +import com.google.protobuf.ByteString; +import java.util.List; + +import javax.annotation.Resource; +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.capsule.ProposalCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ItemNotFoundException; +import org.tron.core.store.ProposalStore; +import org.tron.protos.Protocol; + +public class ProposalStoreTest extends BaseTest { + + @Resource + private ProposalStore proposalStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Before + public void init() { + Protocol.Proposal.Builder builder = Protocol.Proposal.newBuilder() + .setProposalId(1L).putParameters(1,99).setState(Protocol.Proposal.State.PENDING) + .setProposerAddress(ByteString.copyFromUtf8("Address1")); + proposalStore.put("1".getBytes(), new ProposalCapsule(builder.build())); + builder.setProposalId(2L).setState(Protocol.Proposal.State.APPROVED).setProposerAddress( + ByteString.copyFromUtf8("Address2")); + proposalStore.put("2".getBytes(), new ProposalCapsule(builder.build())); + } + + @Test + public void testGet() throws Exception { + final ProposalCapsule result = proposalStore.get("1".getBytes()); + Assert.assertNotNull(result); + Assert.assertEquals(result.getID(),1); + Assert.assertEquals(result.getProposalAddress(), ByteString.copyFromUtf8("Address1")); + assertThrows(ItemNotFoundException.class, + () -> proposalStore.get("testGet1".getBytes())); + } + + @Test + public void testGetAllProposals() { + final List result = proposalStore.getAllProposals(); + Assert.assertEquals(result.size(), 2); + Assert.assertEquals(result.get(0).getID(), 1); + Assert.assertEquals(result.get(0).getProposalAddress(), ByteString.copyFromUtf8("Address1")); + } + + @Test + public void testGetSpecifiedProposals() { + final List result = + proposalStore.getSpecifiedProposals(Protocol.Proposal.State.PENDING, 1); + Assert.assertEquals(result.size(), 1); + Assert.assertEquals(result.get(0).getID(), 1); + Assert.assertEquals(result.get(0).getProposalAddress(), ByteString.copyFromUtf8("Address1")); + } + +} diff --git a/framework/src/test/java/org/tron/core/db/TreeBlockIndexStoreTest.java b/framework/src/test/java/org/tron/core/db/TreeBlockIndexStoreTest.java new file mode 100644 index 00000000000..19d1329e580 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/TreeBlockIndexStoreTest.java @@ -0,0 +1,52 @@ +package org.tron.core.db; + +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.Constant; +import org.tron.core.capsule.BytesCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ItemNotFoundException; +import org.tron.core.store.TreeBlockIndexStore; + +public class TreeBlockIndexStoreTest extends BaseTest { + + @Resource + private TreeBlockIndexStore treeBlockIndexStore; + + static { + Args.setParam( + new String[]{ + "--output-directory", dbPath() + }, + Constant.TEST_CONF + ); + } + + @Test + public void testPut() throws ItemNotFoundException { + treeBlockIndexStore.put(1L, "testPut".getBytes()); + byte[] result = treeBlockIndexStore.get(1L); + Assert.assertEquals(new String(result),"testPut"); + } + + @Test + public void testGetByNum() throws Exception { + treeBlockIndexStore.put(2L, "testGetByNum".getBytes()); + byte[] result = treeBlockIndexStore.get(2L); + Assert.assertEquals(new String(result),"testGetByNum"); + Assert.assertThrows(ItemNotFoundException.class, () -> treeBlockIndexStore.get(0L)); + } + + @Test + public void testGet() throws Exception { + treeBlockIndexStore.put(3L, "testGet".getBytes()); + final BytesCapsule result = treeBlockIndexStore.get(ByteArray.fromLong(3L)); + Assert.assertEquals(new String(result.getData()),"testGet"); + Assert.assertThrows(ItemNotFoundException.class, () -> treeBlockIndexStore + .get(ByteArray.fromLong(0L))); + } + +} From 9406d24b5665088de0c9e8652385b49d2351d74c Mon Sep 17 00:00:00 2001 From: lxcmyf Date: Mon, 25 Sep 2023 16:00:23 +0800 Subject: [PATCH 19/31] fix(api): add resource price query API for soliditynode (#5516) --- .../http/solidity/SolidityNodeHttpApiService.java | 15 ++++++++++----- .../java/org/tron/program/SolidityNodeTest.java | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) 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 a1808b4fa86..0c66b220e09 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 @@ -22,6 +22,7 @@ import org.tron.core.services.http.GetAssetIssueListByNameServlet; import org.tron.core.services.http.GetAssetIssueListServlet; import org.tron.core.services.http.GetAvailableUnfreezeCountServlet; +import org.tron.core.services.http.GetBandwidthPricesServlet; import org.tron.core.services.http.GetBlockByIdServlet; import org.tron.core.services.http.GetBlockByLatestNumServlet; import org.tron.core.services.http.GetBlockByLimitNextServlet; @@ -35,6 +36,7 @@ import org.tron.core.services.http.GetDelegatedResourceAccountIndexV2Servlet; import org.tron.core.services.http.GetDelegatedResourceServlet; import org.tron.core.services.http.GetDelegatedResourceV2Servlet; +import org.tron.core.services.http.GetEnergyPricesServlet; import org.tron.core.services.http.GetExchangeByIdServlet; import org.tron.core.services.http.GetMarketOrderByAccountServlet; import org.tron.core.services.http.GetMarketOrderByIdServlet; @@ -66,7 +68,6 @@ public class SolidityNodeHttpApiService extends HttpService { @Autowired private GetAccountServlet getAccountServlet; - @Autowired private GetTransactionByIdSolidityServlet getTransactionByIdServlet; @Autowired @@ -91,7 +92,6 @@ public class SolidityNodeHttpApiService extends HttpService { private GetExchangeByIdServlet getExchangeByIdServlet; @Autowired private ListExchangesServlet listExchangesServlet; - @Autowired private ListWitnessesServlet listWitnessesServlet; @Autowired @@ -154,15 +154,16 @@ public class SolidityNodeHttpApiService extends HttpService { private TriggerConstantContractServlet triggerConstantContractServlet; @Autowired private EstimateEnergyServlet estimateEnergyServlet; - @Autowired private GetTransactionInfoByBlockNumServlet getTransactionInfoByBlockNumServlet; - @Autowired private HttpApiAccessFilter httpApiAccessFilter; - @Autowired private GetBlockServlet getBlockServlet; + @Autowired + private GetBandwidthPricesServlet getBandwidthPricesServlet; + @Autowired + private GetEnergyPricesServlet getEnergyPricesServlet; @Override @@ -279,6 +280,10 @@ public void start() { context.addServlet(new ServletHolder(getRewardServlet), "/walletsolidity/getReward"); context.addServlet(new ServletHolder(getBurnTrxServlet), "/walletsolidity/getburntrx"); context.addServlet(new ServletHolder(getBlockServlet), "/walletsolidity/getblock"); + context.addServlet(new ServletHolder(getBandwidthPricesServlet), + "/walletsolidity/getbandwidthprices"); + context.addServlet(new ServletHolder(getEnergyPricesServlet), + "/walletsolidity/getenergyprices"); // http access filter context.addFilter(new FilterHolder(httpApiAccessFilter), "/walletsolidity/*", diff --git a/framework/src/test/java/org/tron/program/SolidityNodeTest.java b/framework/src/test/java/org/tron/program/SolidityNodeTest.java index 3ec8959e821..a77fa2fa8c6 100755 --- a/framework/src/test/java/org/tron/program/SolidityNodeTest.java +++ b/framework/src/test/java/org/tron/program/SolidityNodeTest.java @@ -13,6 +13,7 @@ import org.tron.core.Constant; import org.tron.core.config.args.Args; import org.tron.core.services.RpcApiService; +import org.tron.core.services.http.solidity.SolidityNodeHttpApiService; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.DynamicProperties; @@ -21,6 +22,8 @@ public class SolidityNodeTest extends BaseTest { @Resource RpcApiService rpcApiService; + @Resource + SolidityNodeHttpApiService solidityNodeHttpApiService; static { try { @@ -94,4 +97,11 @@ public void testSolidityGrpcCall() { rpcApiService.stop(); } + @Test + public void testSolidityNodeHttpApiService() { + solidityNodeHttpApiService.init(Args.getInstance()); + solidityNodeHttpApiService.start(); + solidityNodeHttpApiService.stop(); + Assert.assertTrue(true); + } } From 962a3dbe97a67c2399faac6d57a2fae974905c94 Mon Sep 17 00:00:00 2001 From: Brown Jiang <317787106@qq.com> Date: Tue, 26 Sep 2023 10:33:12 +0800 Subject: [PATCH 20/31] feat(net):update libp2p (#5517) --- common/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/build.gradle b/common/build.gradle index fc95dd385b7..8cca0853dc5 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -53,7 +53,7 @@ dependencies { compile 'org.aspectj:aspectjrt:1.8.13' compile 'org.aspectj:aspectjweaver:1.8.13' compile 'org.aspectj:aspectjtools:1.8.13' - compile group: 'com.github.tronprotocol', name: 'libp2p', version: 'test-v2.0.2',{ + compile group: 'com.github.tronprotocol', name: 'libp2p', version: 'test-v2.0.3',{ exclude group: 'io.grpc', module: 'grpc-context' exclude group: 'io.grpc', module: 'grpc-core' exclude group: 'io.grpc', module: 'grpc-netty' From fe7715de604212de80e712cd8533a345a91ae2a2 Mon Sep 17 00:00:00 2001 From: Kayle Date: Sat, 7 Oct 2023 10:17:02 +0800 Subject: [PATCH 21/31] feat(LiteFullNodeTool): mark LiteFullNodeTool as deprecated (#5509) --------- Co-authored-by: liukai --- .../main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java | 3 +++ .../src/test/java/org/tron/program/LiteFullNodeToolTest.java | 1 + 2 files changed, 4 insertions(+) diff --git a/framework/src/main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java b/framework/src/main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java index f2535c7caa5..40c371c58e0 100644 --- a/framework/src/main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java +++ b/framework/src/main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java @@ -39,6 +39,7 @@ import org.tron.tool.litefullnode.iterator.DBIterator; @Slf4j(topic = "tool") +@Deprecated public class LiteFullNodeTool { private static final long START_TIME = System.currentTimeMillis() / 1000; @@ -556,6 +557,8 @@ private void run(Args argv) { * main. */ public static void main(String[] args) { + logger.info("LiteFullNodeTool is deprecated and it will be removed in the next major release," + + " use Toolkit.jar db lite instead, parameters are fully compatible."); Args argv = new Args(); CommonParameter.getInstance().setValidContractProtoThreadNum(1); LiteFullNodeTool tool = new LiteFullNodeTool(); diff --git a/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java b/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java index c23ab213006..d1da0bf00d8 100644 --- a/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java +++ b/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java @@ -24,6 +24,7 @@ import org.tron.tool.litefullnode.LiteFullNodeTool; @Slf4j +@Deprecated public class LiteFullNodeToolTest { private TronApplicationContext context; From c6239406be14e775dc3a0e1e08c7ad29a44dd30a Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:53:39 +0800 Subject: [PATCH 22/31] feat(db/trans-cache): verify cached files with crc32c (#5523) --- .../org/tron/core/db2/common/TxCacheDB.java | 86 +++++++++++++++---- 1 file changed, 70 insertions(+), 16 deletions(-) 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 8e5a2ee6787..3da6cd9475a 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 @@ -2,9 +2,13 @@ import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.google.common.io.ByteSource; import com.google.common.primitives.Longs; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; @@ -18,6 +22,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Properties; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; @@ -79,6 +84,8 @@ public class TxCacheDB implements DB, Flusher { private final Path cacheFile0; private final Path cacheFile1; + private String crc32c0; + private String crc32c1; private final Path cacheProperties; private final Path cacheDir; private AtomicBoolean isValid = new AtomicBoolean(false); @@ -281,13 +288,18 @@ private boolean recovery() { CompletableFuture tk1 = loadProperties.thenApplyAsync( v -> recovery(1, this.cacheFile1)); - return CompletableFuture.allOf(tk0, tk1).thenApply(v -> { - logger.info("recovery bloomFilters success."); - return true; - }).exceptionally(this::handleException).join(); + try { + return CompletableFuture.allOf(tk0, tk1).thenApply(v -> { + logger.info("recovery bloomFilters success."); + return true; + }).exceptionally(this::handleException).join(); + } finally { + clearCrc32c(); + } } private boolean recovery(int index, Path file) { + checkCrc32c(index, file); try (InputStream in = new BufferedInputStream(Files.newInputStream(file, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE))) { logger.info("recovery bloomFilter[{}] from file.", index); @@ -329,24 +341,38 @@ private void dump() { () -> dump(0, this.cacheFile0)); CompletableFuture task1 = CompletableFuture.runAsync( () -> dump(1, this.cacheFile1)); - CompletableFuture.allOf(task0, task1).thenRun(() -> { - writeProperties(); - logger.info("dump bloomFilters done."); - - }).exceptionally(e -> { - logger.info("dump bloomFilters to file failed. {}", e.getMessage()); - return null; - }).join(); + try { + CompletableFuture.allOf(task0, task1).thenRun(() -> { + writeProperties(); + logger.info("dump bloomFilters done."); + }).exceptionally(e -> { + logger.info("dump bloomFilters to file failed. {}", e.getMessage()); + return null; + }).join(); + } finally { + clearCrc32c(); + } } private void dump(int index, Path file) { + logger.info("dump bloomFilters[{}] to file.", index); + long start = System.currentTimeMillis(); try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(file))) { - logger.info("dump bloomFilters[{}] to file.", index); - long start = System.currentTimeMillis(); bloomFilters[index].writeTo(out); - logger.info("dump bloomFilters[{}] to file done,filter: {}, filter-fpp: {}, cost {} ms.", + } catch (Exception e) { + throw new RuntimeException(e); + } + try { + String crc32c = getCrc32c(file); + if (index == 0) { + this.crc32c0 = crc32c; + } else { + this.crc32c1 = crc32c; + } + logger.info("dump bloomFilters[{}] to file done,filter: {}, filter-fpp: {}, " + + "crc32c: {}, cost {} ms.", index, bloomFilters[index].approximateElementCount(), bloomFilters[index].expectedFpp(), - System.currentTimeMillis() - start); + crc32c, System.currentTimeMillis() - start); } catch (Exception e) { throw new RuntimeException(e); } @@ -367,6 +393,8 @@ private boolean loadProperties() { String.format("currentBlockNum not match. filter: %d, db: %d", currentBlockNum, currentBlockNumFromDB)); } + this.crc32c0 = properties.getProperty("crc32c0"); + this.crc32c1 = properties.getProperty("crc32c1"); logger.info("filterStartBlock: {}, currentBlockNum: {}, currentFilterIndex: {}, load done.", filterStartBlock, currentBlockNum, currentFilterIndex); return true; @@ -382,6 +410,8 @@ private void writeProperties() { properties.setProperty("filterStartBlock", String.valueOf(filterStartBlock)); properties.setProperty("currentBlockNum", String.valueOf(currentBlockNum)); properties.setProperty("currentFilterIndex", String.valueOf(currentFilterIndex)); + properties.setProperty("crc32c0", this.crc32c0); + properties.setProperty("crc32c1", this.crc32c1); properties.store(w, "Generated by the application. PLEASE DO NOT EDIT! "); logger.info("filterStartBlock: {}, currentBlockNum: {}, currentFilterIndex: {}, write done.", filterStartBlock, currentBlockNum, currentFilterIndex); @@ -390,6 +420,30 @@ private void writeProperties() { } } + private String getCrc32c(Path file) throws IOException { + ByteSource byteSource = com.google.common.io.Files.asByteSource(file.toFile()); + HashCode hc = byteSource.hash(Hashing.crc32c()); + return hc.toString(); + } + + private void checkCrc32c(int index, Path file) { + try { + String actual = getCrc32c(file); + String expect = index == 0 ? this.crc32c0 : this.crc32c1; + if (!Objects.equals(actual, expect)) { + throw new IllegalStateException("crc32c not match. index: " + index + ", expect: " + expect + + ", actual: " + actual); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private void clearCrc32c() { + this.crc32c0 = null; + this.crc32c1 = null; + } + @Override public TxCacheDB newInstance() { return new TxCacheDB(name, recentTransactionStore, dynamicPropertiesStore); From 94e1a8488261c46215b9089808d216c6c042efc7 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:21:00 +0800 Subject: [PATCH 23/31] test(db): fix org.iq80.leveldb.DBException: Closed (#5528) --- .../src/test/java/org/tron/common/BaseTest.java | 13 +++++++++++++ .../org/tron/core/db/NullifierStoreTest.java | 3 --- .../java/org/tron/core/db/TxCacheDBTest.java | 4 ---- .../filter/HttpApiAccessFilterTest.java | 11 ++++------- .../filter/LiteFnQueryHttpFilterTest.java | 11 ++++------- .../http/TriggerSmartContractServletTest.java | 3 --- .../java/org/tron/program/SupplementTest.java | 17 +++-------------- 7 files changed, 24 insertions(+), 38 deletions(-) diff --git a/framework/src/test/java/org/tron/common/BaseTest.java b/framework/src/test/java/org/tron/common/BaseTest.java index c17d10d6ae4..959a746a141 100644 --- a/framework/src/test/java/org/tron/common/BaseTest.java +++ b/framework/src/test/java/org/tron/common/BaseTest.java @@ -2,6 +2,7 @@ import com.google.protobuf.ByteString; import java.io.IOException; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.AfterClass; @@ -12,6 +13,7 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.tron.common.application.Application; import org.tron.common.crypto.ECKey; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Sha256Hash; @@ -37,6 +39,16 @@ public abstract class BaseTest { @Resource protected ChainBaseManager chainBaseManager; + @Resource + protected Application appT; + + private static Application appT1; + + + @PostConstruct + private void prepare() { + appT1 = appT; + } public static String dbPath() { try { @@ -49,6 +61,7 @@ public static String dbPath() { @AfterClass public static void destroy() { + appT1.shutdown(); Args.clearParam(); } diff --git a/framework/src/test/java/org/tron/core/db/NullifierStoreTest.java b/framework/src/test/java/org/tron/core/db/NullifierStoreTest.java index 96b2fc1a74d..6070182a5c1 100644 --- a/framework/src/test/java/org/tron/core/db/NullifierStoreTest.java +++ b/framework/src/test/java/org/tron/core/db/NullifierStoreTest.java @@ -7,7 +7,6 @@ import org.junit.BeforeClass; import org.junit.Test; import org.tron.common.BaseTest; -import org.tron.common.application.Application; import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.capsule.BytesCapsule; @@ -21,8 +20,6 @@ public class NullifierStoreTest extends BaseTest { private static final byte[] TRX_TWO = randomBytes(32); private static final byte[] TRX_TWO_NEW = randomBytes(32); @Resource - public Application AppT; - @Resource private NullifierStore nullifierStore; private static BytesCapsule nullifier1; private static BytesCapsule nullifier2; 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 1b49228a968..55557cf51b5 100644 --- a/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java +++ b/framework/src/test/java/org/tron/core/db/TxCacheDBTest.java @@ -1,11 +1,9 @@ package org.tron.core.db; -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.application.Application; import org.tron.common.utils.ByteArray; import org.tron.core.Constant; import org.tron.core.capsule.BytesCapsule; @@ -13,8 +11,6 @@ import org.tron.keystore.Wallet; public class TxCacheDBTest extends BaseTest { - @Resource - private Application appT; /** * Init data. 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 6423252560b..5f883fc8c07 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 @@ -17,7 +17,6 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; -import org.tron.common.application.Application; import org.tron.common.parameter.CommonParameter; import org.tron.core.Constant; import org.tron.core.config.args.Args; @@ -27,8 +26,6 @@ public class HttpApiAccessFilterTest extends BaseTest { - @Resource - private Application appTest; @Resource private FullNodeHttpApiService httpApiService; @Resource @@ -49,10 +46,10 @@ public class HttpApiAccessFilterTest extends BaseTest { */ @Before public void init() { - appTest.addService(httpApiService); - appTest.addService(httpApiOnSolidityService); - appTest.addService(httpApiOnPBFTService); - appTest.startup(); + appT.addService(httpApiService); + appT.addService(httpApiOnSolidityService); + appT.addService(httpApiOnPBFTService); + appT.startup(); } @Test 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 38e0b054c2b..5fd4711273e 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 @@ -19,7 +19,6 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; -import org.tron.common.application.Application; import org.tron.core.Constant; import org.tron.core.config.args.Args; import org.tron.core.services.http.FullNodeHttpApiService; @@ -32,8 +31,6 @@ public class LiteFnQueryHttpFilterTest extends BaseTest { private final String ip = "127.0.0.1"; private int fullHttpPort; @Resource - private Application appTest; - @Resource private FullNodeHttpApiService httpApiService; @Resource private HttpApiOnSolidityService httpApiOnSolidityService; @@ -51,10 +48,10 @@ public class LiteFnQueryHttpFilterTest extends BaseTest { */ @Before public void init() { - appTest.addService(httpApiService); - appTest.addService(httpApiOnSolidityService); - appTest.addService(httpApiOnPBFTService); - appTest.startup(); + appT.addService(httpApiService); + appT.addService(httpApiOnSolidityService); + appT.addService(httpApiOnPBFTService); + appT.startup(); } @Test diff --git a/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java b/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java index 384c0cada6c..f363b7fbefc 100644 --- a/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java @@ -11,7 +11,6 @@ import org.junit.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.tron.common.BaseTest; -import org.tron.common.application.Application; import org.tron.common.utils.ByteArray; import org.tron.common.utils.client.Configuration; import org.tron.common.utils.client.utils.HttpMethed; @@ -35,8 +34,6 @@ public class TriggerSmartContractServletTest extends BaseTest { @Resource private FullNodeHttpApiService httpApiService; - @Resource - private Application appT; @BeforeClass public static void init() throws Exception { diff --git a/framework/src/test/java/org/tron/program/SupplementTest.java b/framework/src/test/java/org/tron/program/SupplementTest.java index f370a871006..ffa6b14f46b 100644 --- a/framework/src/test/java/org/tron/program/SupplementTest.java +++ b/framework/src/test/java/org/tron/program/SupplementTest.java @@ -9,15 +9,10 @@ import java.math.BigInteger; import javax.annotation.Resource; 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.junit.runner.RunWith; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.tron.common.BaseTest; import org.tron.common.config.DbBackupConfig; import org.tron.common.entity.PeerInfo; import org.tron.common.utils.CompactEncoder; @@ -26,19 +21,13 @@ import org.tron.core.Constant; import org.tron.core.capsule.StorageRowCapsule; import org.tron.core.capsule.utils.RLP; -import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.services.http.HttpSelfFormatFieldName; import org.tron.core.store.StorageRowStore; import org.tron.keystore.WalletUtils; -@RunWith(SpringJUnit4ClassRunner.class) -@DirtiesContext -@ContextConfiguration(classes = {DefaultConfig.class}) -public class SupplementTest { +public class SupplementTest extends BaseTest { - @ClassRule - public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); private static String dbPath; @Resource @@ -49,7 +38,7 @@ public class SupplementTest { @BeforeClass public static void init() throws IOException { - dbPath = temporaryFolder.newFolder().toString(); + dbPath = dbPath(); Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, Constant.TEST_CONF); } From 283230aa9c4426c5b8ea2d5853e80e121a1eb155 Mon Sep 17 00:00:00 2001 From: Brown Jiang <317787106@qq.com> Date: Wed, 11 Oct 2023 15:20:54 +0800 Subject: [PATCH 24/31] test(api): add test cases of Wallet (#5442) * add wallet test --- .../src/main/java/org/tron/core/Wallet.java | 2 +- .../org/tron/common/config/args/ArgsTest.java | 17 + .../java/org/tron/core/ShieldWalletTest.java | 383 ++++++++++++++++++ .../test/java/org/tron/core/WalletTest.java | 92 ++++- .../tron/core/db/TransactionExpireTest.java | 49 ++- .../core/zksnark/MerkleContainerTest.java | 1 + .../core/zksnark/ShieldedReceiveTest.java | 16 +- 7 files changed, 537 insertions(+), 23 deletions(-) create mode 100644 framework/src/test/java/org/tron/core/ShieldWalletTest.java diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index fdea2a555a8..0865e202974 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -4100,7 +4100,7 @@ private void checkBigIntegerRange(BigInteger in) throws ContractValidateExceptio } } - private byte[] getShieldedContractScalingFactor(byte[] contractAddress) + public byte[] getShieldedContractScalingFactor(byte[] contractAddress) throws ContractExeException { String methodSign = "scalingFactor()"; byte[] selector = new byte[4]; diff --git a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java index 8e2e1accb4a..b95d47bfa39 100644 --- a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java @@ -1,6 +1,9 @@ package org.tron.common.config.args; +import com.beust.jcommander.JCommander; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -46,4 +49,18 @@ public void testConfig() { Assert.assertEquals(rateLimiter.getHttpMap().size(), 1); Assert.assertEquals(rateLimiter.getRpcMap().size(), 0); } + + @Test + public void testHelpMessage() { + JCommander jCommander = JCommander.newBuilder().addObject(Args.PARAMETER).build(); + Method method; + try { + method = Args.class.getDeclaredMethod("printVersion"); + method.setAccessible(true); + method.invoke(Args.class); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + Assert.fail(); + } + Args.printHelp(jCommander); + } } diff --git a/framework/src/test/java/org/tron/core/ShieldWalletTest.java b/framework/src/test/java/org/tron/core/ShieldWalletTest.java new file mode 100644 index 00000000000..5bc2a31c9d0 --- /dev/null +++ b/framework/src/test/java/org/tron/core/ShieldWalletTest.java @@ -0,0 +1,383 @@ +package org.tron.core; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.tron.core.zen.ZksnarkInitService.librustzcashInitZksnarkParams; + +import java.math.BigInteger; +import javax.annotation.Resource; +import org.junit.Assert; +import org.junit.Test; +import org.tron.api.GrpcAPI.PrivateParameters; +import org.tron.api.GrpcAPI.PrivateParametersWithoutAsk; +import org.tron.api.GrpcAPI.PrivateShieldedTRC20Parameters; +import org.tron.api.GrpcAPI.PrivateShieldedTRC20ParametersWithoutAsk; +import org.tron.api.GrpcAPI.ShieldedAddressInfo; +import org.tron.api.GrpcAPI.ShieldedTRC20Parameters; +import org.tron.common.BaseTest; +import org.tron.common.utils.ByteArray; +import org.tron.core.capsule.TransactionCapsule; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ContractExeException; +import org.tron.core.services.http.JsonFormat; +import org.tron.core.services.http.JsonFormat.ParseException; + + +public class ShieldWalletTest extends BaseTest { + + @Resource + private Wallet wallet; + + static { + Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF); + } + + @Test + public void testCreateShieldedTransaction1() { + librustzcashInitZksnarkParams(); + String transactionStr1 = new String(ByteArray.fromHexString( + "0x7b0a20202020227472616e73706172656e745f66726f6d5f61646472657373223a202234433930413" + + "73241433344414546324536383932343545463430303839443634314345414337373433323433414233" + + "31333136303030222c0a202020202266726f6d5f616d6f756e74223a203130303030303030302c0a202" + + "02020226f766b223a223566643165636466643637396232616461333337623235306231303234373138" + + "6633316439636237316361653664666363396131613637626134373033643863222c0a2020202022736" + + "869656c6465645f7265636569766573223a205b0a20202020202020207b0a2020202020202020202020" + + "20226e6f7465223a207b0a202020202020202020202020202020202276616c7565223a2039303030303" + + "030302c0a20202020202020202020202020202020227061796d656e745f61646472657373223a20227a" + + "74726f6e3179616d336a723270746d79706c707030747978387974707a36736a64786364657a306a333" + + "36673636d656a76376e6863346171336d766b687275726772777671653932636a6c68356b3367222c0a" + + "202020202020202020202020202020202272636d223a202237323330353362636266656364663564613" + + "63663313861623033373634373665663330386336316237616265383931623263303165393033626362" + + "3837633065222c0a20202020202020202020202020202020226d656d6f223a20223232323232220a202" + + "0202020202020202020207d0a20202020202020207d0a202020205d0a7d")); + PrivateParameters.Builder builder1 = PrivateParameters.newBuilder(); + try { + JsonFormat.merge(transactionStr1, builder1, false); + } catch (ParseException e) { + Assert.fail(); + } + + try { + TransactionCapsule transactionCapsule = wallet.createShieldedTransaction(builder1.build()); + Assert.assertNotNull(transactionCapsule); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testCreateShieldedTransaction2() { + librustzcashInitZksnarkParams(); + String transactionStr2 = new String(ByteArray.fromHexString( + "7b0a202020202261736b223a20223938666430333136376632333437623534643737323338343137663" + + "6373038643537323939643938376362613838353564653037626532346236316464653064222c0a2020" + + "2020226e736b223a2022643063623330653764306634653435333535363134333463303462616339316" + + "63039643230653664366234363361623031626534663531386230363839653039222c0a20202020226f" + + "766b223a202235666431656364666436373962326164613333376232353062313032343731386633316" + + "439636237316361653664666363396131613637626134373033643863222c0a2020202022736869656c" + + "6465645f7370656e6473223a205b0a20202020202020207b200a090909226e6f7465223a207b0a20202" + + "0202020202020202020202020202276616c7565223a2039303030303030302c0a202020202020202020" + + "20202020202020227061796d656e745f61646472657373223a20227a74726f6e3179616d336a7232707" + + "46d79706c707030747978387974707a36736a64786364657a306a33336673636d656a76376e68633461" + + "71336d766b687275726772777671653932636a6c68356b3367222c0a202020202020202020202020202" + + "020202272636d223a202237323330353362636266656364663564613636633138616230333736343736" + + "656633303863363162376162653839316232633031653930336263623837633065222c0a20202020202" + + "020202020202020202020226d656d6f223a20223232323232220a2020202020202020202020207d2c0a" + + "20202020202020202020202022616c706861223a2022323936656531633532633933663734306433616" + + "23162373265366463663561626231643666343965643865646236663334656633666462366537366335" + + "353063222c0a20202020202020202020202022766f7563686572223a207b0a202020202020202020202" + + "0202274726565223a207b0a20202020202020202020202020202020226c656674223a207b0a20202020" + + "2020202020202020202020202020202022636f6e74656e74223a2022346565653236313963343862613" + + "26264303235303235363330636430313362616361663337653961323162323965333938393735623763" + + "653864363436613265220a202020202020202020202020202020207d2c0a20202020202020202020202" + + "020202020227269676874223a207b0a202020202020202020202020202020202020202022636f6e7465" + + "6e74223a202235333863326139313537313333356334666138383866396234336239666164643931633" + + "334666366326265666130333931353932613564366535343539623537220a2020202020202020202020" + + "20202020207d0a2020202020202020202020207d2c0a202020202020202020202020227274223a20226" + + "33539376436633235663131623938613638303034343838393138386638613934616237393265353432" + + "32313461386636346632363334616535346535353531220a20202020202020207d2c0a2020202020202" + + "020202020202270617468223a2022323032306232656564303331643464366134663032613039376638" + + "30623534636331353431643431363363366236663539373166383862366534316433356335333831343" + + "23031323933356631346236373635303962383165623439656632356633393236396564373233303932" + + "33386234633134353830333534346236343664636136326432306531663334623033346434613363643" + + "23835353765323930376562663939306339313866363465636235306139346630316436666461356361" + + "35633765663732323032386537623834316463626334376363656236396437636238643934323435666" + + "23763623262613361376136626331386631336639343566376462643665326132306135313232633038" + + "66663963313631643963613666633436323037333339366337643764333865386565343863646233626" + + "56137653232333031333465643661323064326531363432633961343632323239323839653562306533" + + "62376639303038653033303163626239333338356565306532316461323534353037336362353832303" + + "13664363235323936383937316138336461383532316436353338326536316630313736363436643737" + + "31633931353238653332373665653435333833653461323066656530653532383032636230633436623" + + "16562346433373663363236393766343735396636633839313766613335323537313230326664373738" + + "66643731323230346336393337643738663432363835663834623433616433623762303066383132383" + + "53636326638356336613638656631316436326164316133656530383530323030373639353537626336" + + "38326231626633303836343666643062323265363438653862396539386635376532396635616634306" + + "63665646238333365326334393230303865656162306331336162643630363965363331303139376266" + + "38306639633165613664653738666431396362616532346434613532306536636633303233323038643" + + "56661343365356131306431313630356163373433306261316635643831666231623638643239613634" + + "30343035373637373439653834313532373637333230366163613834343864383236336535343764356" + + "66632393530653265643338333965393938643331636263366163396664353762633630303262313539" + + "32313632306364316338646266366533616363376138303433396263343936326366323562396463653" + + "76338393666336135626437303830336663356130653333636630303230366564623136643031393037" + + "62373539393737643736353064616437653365633034396166316133643837353338306236393763383" + + "63263396563356435316332303165613636373566393535316565623964666161613932343762633938" + + "35383237306433643361346335616661373137376139383464356564316265323435313230643661636" + + "46564663935663630386530396661353366623433646364303939303437353732366335313331323130" + + "63396535636165616239376630653634326632306264373462323561616362393233373861383731626" + + "63237643232356366633236626163613334346131656133356664643934353130663364313537303832" + + "63323031623737646163346432346662373235386333633532383730346335393433306236333037313" + + "86265633438363432313833373032316366373564616236353132306563363737313134633237323036" + + "66356465626331633165643636663935653262313838356461356237626533643733366231646539383" + + "53739343733303438323034373737633837373661336231653639623733613632666137303166613466" + + "37613632383264396165653263376136623832653739333764373038316332336332306261343962363" + + "53966626430623733333432313165613661396439646631383563373537653730616138316461353632" + + "66623931326238346634396263653732323034336666353435376631336239323662363164663535326" + + "43465343032656536646331343633663939613533356639613731333433393236346435623631366232" + + "30376239396162646333373330393931636339323734373237643764383264323863623739346564626" + + "33730333462346630303533666637633462363830343434323064366336333961633234623436626431" + + "39333431633931623133666463616233313538316464616637663134313133333661323731663364306" + + "16135323831333230386163396366396333393165336664343238393164323732333861383161386135" + + "63316433613732623162636265613863663434613538636537333839363133323039313264383262326" + + "33262636132333166373165666366363137333766626630613038626566613034313632313561656566" + + "35336538626236643233333930613230653131306465363563393037623964656134616530626438336" + + "13462306135316265613137353634366136346331326234633966393331623263623331623439323064" + + "38323833333836656632656630376562646262343338336331326137333961393533613464366530643" + + "66662313133396134303336643639336266626236633230666665396663303366313862313736633939" + + "38383036343339666630626238616431393361666462323762326363626338383835363931366464383" + + "03465333432303831376465333661623264353766656230373736333462636137373831396338653062" + + "64323938633034663666656430653661383363633133353663613135353230346565653236313963343" + + "86261326264303235303235363330636430313362616361663337653961323162323965333938393735" + + "62376365386436343661326530313030303030303030303030303030220a20202020202020207d0a202" + + "020205d2c0a2020202022736869656c6465645f7265636569766573223a205b0a20202020202020207b" + + "0a202020202020202020202020226e6f7465223a207b0a2020202020202020202020202020202022766" + + "16c7565223a2038303030303030302c0a20202020202020202020202020202020227061796d656e745f" + + "61646472657373223a20227a74726f6e31356b657a70736d71303568336d616a6e6c7a6834306772796" + + "a6d676d3630783265686a7967633771387336376d6c753835376375747177613334386e76706765706e" + + "35786b336735727278222c0a202020202020202020202020202020202272636d223a202237323330353" + + "36263626665636466356461363663313861623033373634373665663330386336316237616265383931" + + "6232633031653930336263623837633065222c0a20202020202020202020202020202020226d656d6f2" + + "23a2022313131313131313131220a2020202020202020202020207d0a20202020202020207d0a202020" + + "205d2c0a202020202276697369626c6522203a20747275650a7d")); + PrivateParameters.Builder builder2 = PrivateParameters.newBuilder(); + try { + JsonFormat.merge(transactionStr2, builder2, true); + } catch (ParseException e) { + Assert.fail(); + } + + try { + TransactionCapsule transactionCapsule = wallet.createShieldedTransaction(builder2.build()); + Assert.assertNotNull(transactionCapsule); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testCreateShieldedTransactionWithoutSpendAuthSig() { + librustzcashInitZksnarkParams(); + String transactionStr3 = new String(ByteArray.fromHexString( + "7b0a2020202022616b223a2022373161643638633466353035373464356164333735343863626538363" + + "63031663732393662393161306362303535353733313462373830383437323730326465222c0a202020" + + "20226e736b223a202264306362333065376430663465343533353536313433346330346261633931663" + + "039643230653664366234363361623031626534663531386230363839653039222c0a20202020226f76" + + "6b223a20223566643165636466643637396232616461333337623235306231303234373138663331643" + + "9636237316361653664666363396131613637626134373033643863222c0a2020202022736869656c64" + + "65645f7370656e6473223a205b0a20202020202020207b0a202020202020202020202020226e6f74652" + + "23a207b0a202020202020202020202020202020202276616c7565223a2039303030303030302c0a2020" + + "2020202020202020202020202020227061796d656e745f61646472657373223a20227a74726f6e31796" + + "16d336a723270746d79706c707030747978387974707a36736a64786364657a306a33336673636d656a" + + "76376e6863346171336d766b687275726772777671653932636a6c68356b3367222c0a2020202020202" + + "02020202020202020202272636d223a2022373233303533626362666563646635646136366331386162" + + "30333736343736656633303863363162376162653839316232633031653930336263623837633065222" + + "c0a20202020202020202020202020202020226d656d6f223a20223232323232220a2020202020202020" + + "202020207d2c0a20202020202020202020202022616c706861223a20223239366565316335326339336" + + "63734306433616231623732653664636635616262316436663439656438656462366633346566336664" + + "62366537366335353063222c0a20202020202020202020202022766f7563686572223a207b0a2020202" + + "02020202020202020202020202274726565223a207b0a20202020202020202020202020202020202020" + + "20226c656674223a207b0a20202020202020202020202020202020202020202020202022636f6e74656" + + "e74223a2022346565653236313963343862613262643032353032353633306364303133626163616633" + + "37653961323162323965333938393735623763653864363436613265220a20202020202020202020202" + + "020202020202020207d2c0a2020202020202020202020202020202020202020227269676874223a207b" + + "0a20202020202020202020202020202020202020202020202022636f6e74656e74223a2022353338633" + + "26139313537313333356334666138383866396234336239666164643931633334666366326265666130" + + "333931353932613564366535343539623537220a20202020202020202020202020202020202020207d0" + + "a202020202020202020202020202020207d2c0a20202020202020202020202020202020227274223a20" + + "22633539376436633235663131623938613638303034343838393138386638613934616237393265353" + + "43232313461386636346632363334616535346535353531220a2020202020202020202020207d2c0a20" + + "20202020202020202020202270617468223a20223230323062326565643033316434643661346630326" + + "13039376638306235346363313534316434313633633662366635393731663838623665343164333563" + + "35333831343230313239333566313462363736353039623831656234396566323566333932363965643" + + "73233303932333862346331343538303335343462363436646361363264323065316633346230333464" + + "34613363643238353537653239303765626639393063393138663634656362353061393466303164366" + + "66461356361356337656637323230323865376238343164636263343763636562363964376362386439" + + "34323435666237636232626133613761366263313866313366393435663764626436653261323061353" + + "13232633038666639633136316439636136666334363230373333393663376437643338653865653438" + + "63646233626561376532323330313334656436613230643265313634326339613436323232393238396" + + "53562306533623766393030386530333031636262393333383565653065323164613235343530373363" + + "62353832303136643632353239363839373161383364613835323164363533383265363166303137363" + + "63436643737316339313532386533323736656534353338336534613230666565306535323830326362" + + "30633436623165623464333736633632363937663437353966366338393137666133353235373132303" + + "26664373738666437313232303463363933376437386634323638356638346234336164336237623030" + + "66383132383536363266383563366136386566313164363261643161336565303835303230303736393" + + "53537626336383262316266333038363436666430623232653634386538623965393866353765323966" + + "35616634306636656462383333653263343932303038656561623063313361626436303639653633313" + + "03139376266383066396331656136646537386664313963626165323464346135323065366366333032" + + "33323038643566613433653561313064313136303561633734333062613166356438316662316236386" + + "43239613634303430353736373734396538343135323736373332303661636138343438643832363365" + + "35343764356666323935306532656433383339653939386433316362633661633966643537626336303" + + "03262313539323136323063643163386462663665336163633761383034333962633439363263663235" + + "62396463653763383936663361356264373038303366633561306533336366303032303665646231366" + + "43031393037623735393937376437363530646164376533656330343961663161336438373533383062" + + "36393763383632633965633564353163323031656136363735663935353165656239646661616139323" + + "43762633938353832373064336433613463356166613731373761393834643565643162653234353132" + + "30643661636465646639356636303865303966613533666234336463643039393034373537323663353" + + "13331323130633965356361656162393766306536343266323062643734623235616163623932333738" + + "61383731626632376432323563666332366261636133343461316561333566646439343531306633643" + + "13537303832633230316237376461633464323466623732353863336335323837303463353934333062" + + "36333037313862656334383634323138333730323163663735646162363531323065633637373131346" + + "33237323036663564656263316331656436366639356532623138383564613562376265336437333662" + + "31646539383537393437333034383230343737376338373736613362316536396237336136326661373" + + "03166613466376136323832643961656532633761366238326537393337643730383163323363323062" + + "61343962363539666264306237333334323131656136613964396466313835633735376537306161383" + + "16461353632666239313262383466343962636537323230343366663534353766313362393236623631" + + "64663535326434653430326565366463313436336639396135333566396137313334333932363464356" + + "23631366232303762393961626463333733303939316363393237343732376437643832643238636237" + + "39346564626337303334623466303035336666376334623638303434343230643663363339616332346" + + "23436626431393334316339316231336664636162333135383164646166376631343131333336613237" + + "31663364306161353238313332303861633963663963333931653366643432383931643237323338613" + + "83161386135633164336137326231626362656138636634346135386365373338393631333230393132" + + "64383262326332626361323331663731656663663631373337666266306130386265666130343136323" + + "13561656566353365386262366432333339306132306531313064653635633930376239646561346165" + + "30626438336134623061353162656131373536343661363463313262346339663933316232636233316" + + "23439323064383238333338366566326566303765626462623433383363313261373339613935336134" + + "64366530643666623131333961343033366436393362666262366332306666653966633033663138623" + + "13736633939383830363433396666306262386164313933616664623237623263636263383838353639" + + "31366464383034653334323038313764653336616232643537666562303737363334626361373738313" + + "96338653062643239386330346636666564306536613833636331333536636131353532303465656532" + + "36313963343862613262643032353032353633306364303133626163616633376539613231623239653" + + "3393839373562376365386436343661326530313030303030303030303030303030220a202020202020" + + "20207d0a202020205d2c0a2020202022736869656c6465645f7265636569766573223a205b0a2020202" + + "0202020207b0a202020202020202020202020226e6f7465223a207b0a20202020202020202020202020" + + "2020202276616c7565223a2038303030303030302c0a202020202020202020202020202020202270617" + + "96d656e745f61646472657373223a20227a74726f6e31356b657a70736d71303568336d616a6e6c7a68" + + "34306772796a6d676d3630783265686a7967633771387336376d6c753835376375747177613334386e7" + + "6706765706e35786b336735727278222c0a202020202020202020202020202020202272636d223a2022" + + "37323330353362636266656364663564613636633138616230333736343736656633303863363162376" + + "162653839316232633031653930336263623837633065222c0a20202020202020202020202020202020" + + "226d656d6f223a2022313131313131313131220a2020202020202020202020207d0a202020202020202" + + "07d0a202020205d2c0a202020202276697369626c65223a20747275650a7d")); + PrivateParametersWithoutAsk.Builder builder3 = PrivateParametersWithoutAsk.newBuilder(); + try { + JsonFormat.merge(transactionStr3, builder3, false); + } catch (ParseException e) { + Assert.fail(); + } + + try { + TransactionCapsule transactionCapsule = wallet.createShieldedTransactionWithoutSpendAuthSig( + builder3.build()); + Assert.assertNotNull(transactionCapsule); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testGetNewShieldedAddress() { + librustzcashInitZksnarkParams(); + try { + ShieldedAddressInfo shieldedAddressInfo = wallet.getNewShieldedAddress(); + Assert.assertNotNull(shieldedAddressInfo); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testCreateShieldedContractParameters() throws ContractExeException { + librustzcashInitZksnarkParams(); + Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true); + Wallet wallet1 = spy(new Wallet()); + + doReturn(BigInteger.valueOf(1).toByteArray()) + .when(wallet1).getShieldedContractScalingFactor( + ByteArray.fromHexString("4144007979359ECAC395BBD3CEF8060D3DF2DC3F01")); + String parameter = new String(ByteArray.fromHexString( + "7b0a202020202261736b223a22633235313365396533303834393439333262643832653063653533363" + + "63264313734323164393062373261383437316130613132623835353261333336653032222c0a202020" + + "20226e736b223a223463366266336464346130363433643230623632386637653435393830633565313" + + "8376630376135316436663365383661616631616239313663303765623064222c0a20202020226f766b" + + "223a2231376135386439613530353864613665343263613132636432383964306136616131363962393" + + "236633138653139626361353138623864366638363734653433222c0a202020202266726f6d5f616d6f" + + "756e74223a22313030222c0a2020202022736869656c6465645f7265636569766573223a5b0a2020202" + + "0202020207b0a202020202020202020202020226e6f7465223a7b0a2020202020202020202020202020" + + "20202276616c7565223a3130302c0a20202020202020202020202020202020227061796d656e745f616" + + "46472657373223a227a74726f6e317939397536656a71656e757076666b70356736713679716b703061" + + "34346334386374613064643567656a7471613476323768716132636768667664786e6d6e65683671717" + + "1303366613735222c0a202020202020202020202020202020202272636d223a22313662366635653430" + + "34343461623765656162313161653636313363323766333531313739373165666138376237313536306" + + "235383133383239633933393064220a2020202020202020202020207d0a20202020202020207d0a2020" + + "20205d2c0a2020202022736869656c6465645f54524332305f636f6e74726163745f616464726573732" + + "23a22343134343030373937393335394543414333393542424433434546383036304433444632444333" + + "463031220a7d")); + PrivateShieldedTRC20Parameters.Builder builder = PrivateShieldedTRC20Parameters.newBuilder(); + try { + JsonFormat.merge(parameter, builder, false); + } catch (ParseException e) { + Assert.fail(); + } + + try { + ShieldedTRC20Parameters shieldedTRC20Parameters = wallet1.createShieldedContractParameters( + builder.build()); + Assert.assertNotNull(shieldedTRC20Parameters); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void testCreateShieldedContractParametersWithoutAsk() throws ContractExeException { + librustzcashInitZksnarkParams(); + Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true); + + Wallet wallet1 = spy(new Wallet()); + doReturn(BigInteger.valueOf(1).toByteArray()) + .when(wallet1).getShieldedContractScalingFactor( + ByteArray.fromHexString("4144007979359ECAC395BBD3CEF8060D3DF2DC3F01")); + String parameter = new String(ByteArray.fromHexString( + "7b0a2020202022616b223a2230656261373361343865323934396561356461613133626365663466646" + + "633613561613966336232363830363763663831313233333938613833386665336363222c0a20202020" + + "226e736b223a22346336626633646434613036343364323062363238663765343539383063356531383" + + "76630376135316436663365383661616631616239313663303765623064222c0a20202020226f766b22" + + "3a223137613538643961353035386461366534326361313263643238396430613661613136396239323" + + "6633138653139626361353138623864366638363734653433222c0a202020202266726f6d5f616d6f75" + + "6e74223a22313030222c0a2020202022736869656c6465645f7265636569766573223a5b0a202020202" + + "02020207b0a202020202020202020202020226e6f7465223a7b0a202020202020202020202020202020" + + "202276616c7565223a3130302c0a20202020202020202020202020202020227061796d656e745f61646" + + "472657373223a227a74726f6e317939397536656a71656e757076666b70356736713679716b70306134" + + "346334386374613064643567656a7471613476323768716132636768667664786e6d6e6568367171713" + + "03366613735222c0a202020202020202020202020202020202272636d223a2231366236663565343034" + + "34346162376565616231316165363631336332376633353131373937316566613837623731353630623" + + "5383133383239633933393064220a2020202020202020202020207d0a20202020202020207d0a202020" + + "205d2c0a2020202022736869656c6465645f54524332305f636f6e74726163745f61646472657373223" + + "a2234313434303037393739333539454341433339354242443343454638303630443344463244433346" + + "3031220a7d")); + PrivateShieldedTRC20ParametersWithoutAsk.Builder builder = + PrivateShieldedTRC20ParametersWithoutAsk.newBuilder(); + try { + JsonFormat.merge(parameter, builder, false); + } catch (ParseException e) { + Assert.fail(); + } + + try { + ShieldedTRC20Parameters shieldedTRC20Parameters = + wallet1.createShieldedContractParametersWithoutAsk(builder.build()); + Assert.assertNotNull(shieldedTRC20Parameters); + } catch (Exception e) { + Assert.fail(); + } + } +} diff --git a/framework/src/test/java/org/tron/core/WalletTest.java b/framework/src/test/java/org/tron/core/WalletTest.java index 7461ff44c08..357a2d2ca08 100644 --- a/framework/src/test/java/org/tron/core/WalletTest.java +++ b/framework/src/test/java/org/tron/core/WalletTest.java @@ -39,6 +39,7 @@ import org.junit.Ignore; import org.junit.Test; import org.tron.api.GrpcAPI; +import org.tron.api.GrpcAPI.AccountNetMessage; import org.tron.api.GrpcAPI.AssetIssueList; import org.tron.api.GrpcAPI.BlockList; import org.tron.api.GrpcAPI.ExchangeList; @@ -130,6 +131,7 @@ public class WalletTest extends BaseTest { private static Transaction transaction4; private static Transaction transaction5; private static AssetIssueCapsule Asset1; + private static AssetIssueCapsule Asset2; private static final String OWNER_ADDRESS; private static final String RECEIVER_ADDRESS; @@ -235,11 +237,11 @@ private void addTransactionInfoToStore(Transaction transaction) { private static Transaction getBuildTransaction( TransferContract transferContract, long transactionTimestamp, long refBlockNum) { return Transaction.newBuilder().setRawData( - Transaction.raw.newBuilder().setTimestamp(transactionTimestamp) - .setRefBlockNum(refBlockNum) - .addContract( - Contract.newBuilder().setType(ContractType.TransferContract) - .setParameter(Any.pack(transferContract)).build()).build()) + Transaction.raw.newBuilder().setTimestamp(transactionTimestamp) + .setRefBlockNum(refBlockNum) + .addContract( + Contract.newBuilder().setType(ContractType.TransferContract) + .setParameter(Any.pack(transferContract)).build()).build()) .build(); } @@ -288,18 +290,26 @@ private void addBlockToStore(Block block) { private static Block getBuildBlock(long timestamp, long num, long witnessId, String witnessAddress, Transaction transaction, Transaction transactionNext) { return Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData( - raw.newBuilder().setTimestamp(timestamp).setNumber(num).setWitnessId(witnessId) - .setWitnessAddress(ByteString.copyFrom(ByteArray.fromHexString(witnessAddress))) - .build()).build()).addTransactions(transaction).addTransactions(transactionNext) + raw.newBuilder().setTimestamp(timestamp).setNumber(num).setWitnessId(witnessId) + .setWitnessAddress(ByteString.copyFrom(ByteArray.fromHexString(witnessAddress))) + .build()).build()).addTransactions(transaction).addTransactions(transactionNext) .build(); } private void buildAssetIssue() { AssetIssueContract.Builder builder = AssetIssueContract.newBuilder(); + builder.setOwnerAddress(ByteString.copyFromUtf8("Address1")); builder.setName(ByteString.copyFromUtf8("Asset1")); Asset1 = new AssetIssueCapsule(builder.build()); chainBaseManager.getAssetIssueStore().put(Asset1.createDbKey(), Asset1); + + AssetIssueContract.Builder builder2 = AssetIssueContract.newBuilder(); + builder2.setOwnerAddress(ByteString.copyFromUtf8("Address2")); + builder2.setName(ByteString.copyFromUtf8("Asset2")); + builder2.setId("id2"); + Asset2 = new AssetIssueCapsule(builder2.build()); + chainBaseManager.getAssetIssueV2Store().put(Asset2.getId().getBytes(), Asset2); } private void buildProposal() { @@ -499,6 +509,52 @@ public void getPaginatedAssetIssueList() { } } + @Test + public void testGetAssetIssueByAccount() { + buildAssetIssue(); + // + AssetIssueList assetIssueList = wallet.getAssetIssueByAccount( + ByteString.copyFromUtf8("Address1")); + Assert.assertEquals(1, assetIssueList.getAssetIssueCount()); + } + + @Test + public void testGetAssetIssueList() { + buildAssetIssue(); + // + AssetIssueList assetIssueList = wallet.getAssetIssueList(); + Assert.assertEquals(1, assetIssueList.getAssetIssueCount()); + } + + @Test + public void testGetAssetIssueListByName() { + buildAssetIssue(); + // + AssetIssueList assetIssueList = wallet.getAssetIssueListByName( + ByteString.copyFromUtf8("Asset1")); + Assert.assertEquals(1, assetIssueList.getAssetIssueCount()); + } + + @Test + public void testGetAssetIssueById() { + buildAssetIssue(); + // + AssetIssueContract assetIssueContract = wallet.getAssetIssueById("id2"); + Assert.assertNotNull(assetIssueContract); + } + + @Test + public void testGetAccountNet() { + ByteString addressByte = ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)); + AccountCapsule accountCapsule = + new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build()); + accountCapsule.setBalance(1000_000_000L); + dbManager.getChainBaseManager().getAccountStore() + .put(accountCapsule.createDbKey(), accountCapsule); + AccountNetMessage accountNetMessage = wallet.getAccountNet(addressByte); + Assert.assertNotNull(accountNetMessage); + } + @Test public void getPaginatedProposalList() { buildProposal(); @@ -686,8 +742,8 @@ public void testGetDelegatedResourceAccountIndex() { } private Any getDelegatedContractForCpu(String ownerAddress, String receiverAddress, - long frozenBalance, - long duration) { + long frozenBalance, + long duration) { return Any.pack( BalanceContract.FreezeBalanceContract.newBuilder() .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ownerAddress))) @@ -723,13 +779,13 @@ private void freezeCpuForOwner() { } private Any getDelegateContractForBandwidth(String ownerAddress, String receiveAddress, - long unfreezeBalance) { + long unfreezeBalance) { return getLockedDelegateContractForBandwidth(ownerAddress, receiveAddress, unfreezeBalance, false); } private Any getLockedDelegateContractForBandwidth(String ownerAddress, String receiveAddress, - long unfreezeBalance, boolean lock) { + long unfreezeBalance, boolean lock) { return Any.pack(BalanceContract.DelegateResourceContract.newBuilder() .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ownerAddress))) .setReceiverAddress(ByteString.copyFrom(ByteArray.fromHexString(receiveAddress))) @@ -949,7 +1005,7 @@ public void testGetAccountById() { chainBaseManager.getAccountIdIndexStore().put(ownerCapsule); Protocol.Account account = wallet.getAccountById( Protocol.Account.newBuilder().setAccountId(ByteString.copyFromUtf8("1001")).build()); - Assert.assertEquals(ownerCapsule.getAddress(),account.getAddress()); + Assert.assertEquals(ownerCapsule.getAddress(), account.getAddress()); } @Test @@ -967,18 +1023,18 @@ public void testGetAssetIssueByName() { String assetName = "My_asset"; String id = "10001"; AssetIssueCapsule assetCapsule = new AssetIssueCapsule(ByteArray.fromHexString(OWNER_ADDRESS), - id,assetName,"abbr", 1_000_000_000_000L,6); + id, assetName, "abbr", 1_000_000_000_000L, 6); chainBaseManager.getAssetIssueStore().put(assetCapsule.createDbKey(), assetCapsule); chainBaseManager.getAssetIssueV2Store().put(assetCapsule.createDbV2Key(), assetCapsule); try { AssetIssueContract assetIssue = wallet.getAssetIssueByName(ByteString.copyFromUtf8(assetName)); - Assert.assertEquals(ByteString.copyFromUtf8(assetName),assetIssue.getName()); - Assert.assertEquals(id,assetIssue.getId()); + Assert.assertEquals(ByteString.copyFromUtf8(assetName), assetIssue.getName()); + Assert.assertEquals(id, assetIssue.getId()); chainBaseManager.getDynamicPropertiesStore().saveAllowSameTokenName(1); assetIssue = wallet.getAssetIssueByName(ByteString.copyFromUtf8(assetName)); - Assert.assertEquals(ByteString.copyFromUtf8(assetName),assetIssue.getName()); - Assert.assertEquals(id,assetIssue.getId()); + Assert.assertEquals(ByteString.copyFromUtf8(assetName), assetIssue.getName()); + Assert.assertEquals(id, assetIssue.getId()); } catch (NonUniqueObjectException e) { Assert.fail(e.getMessage()); } 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 843e54d38a8..5243405b7e6 100644 --- a/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java +++ b/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java @@ -3,7 +3,6 @@ import com.google.protobuf.ByteString; import java.io.IOException; import java.util.Arrays; - import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Assert; @@ -13,6 +12,7 @@ import org.junit.rules.TemporaryFolder; import org.tron.api.GrpcAPI; import org.tron.api.GrpcAPI.Return.response_code; +import org.tron.api.GrpcAPI.TransactionApprovedList; import org.tron.common.application.TronApplicationContext; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; @@ -21,10 +21,13 @@ import org.tron.common.utils.Sha256Hash; import org.tron.core.Constant; import org.tron.core.Wallet; +import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.TransactionCapsule; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; +import org.tron.protos.Protocol; +import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.BalanceContract.TransferContract; @@ -97,4 +100,48 @@ public void testExpireTransaction() { GrpcAPI.Return result = wallet.broadcastTransaction(transactionCapsule.getInstance()); Assert.assertEquals(response_code.TRANSACTION_EXPIRATION_ERROR, result.getCode()); } + + @Test + public void testTransactionApprovedList() { + byte[] address = Args.getLocalWitnesses() + .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()); + TransferContract transferContract = TransferContract.newBuilder() + .setAmount(1L) + .setOwnerAddress(ByteString.copyFrom(address)) + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString( + (Wallet.getAddressPreFixString() + "A389132D6639FBDA4FBC8B659264E6B7C90DB086")))) + .build(); + TransactionCapsule transactionCapsule = + new TransactionCapsule(transferContract, ContractType.TransferContract); + transactionCapsule.setReference(blockCapsule.getNum(), blockCapsule.getBlockId().getBytes()); + transactionCapsule.sign(ByteArray.fromHexString(Args.getLocalWitnesses().getPrivateKey())); + + TransactionApprovedList transactionApprovedList = wallet.getTransactionApprovedList( + transactionCapsule.getInstance()); + Assert.assertTrue( + transactionApprovedList.getResult().getMessage().contains("Account does not exist!")); + + ByteString addressByte = ByteString.copyFrom(address); + AccountCapsule accountCapsule = + new AccountCapsule(Protocol.Account.newBuilder().setAddress(addressByte).build()); + accountCapsule.setBalance(1000_000_000L); + dbManager.getChainBaseManager().getAccountStore() + .put(accountCapsule.createDbKey(), accountCapsule); + transactionApprovedList = wallet.getTransactionApprovedList(transactionCapsule.getInstance()); + Assert.assertEquals("", transactionApprovedList.getResult().getMessage()); + + byte[] randomSig = org.tron.keystore.Wallet.generateRandomBytes(64); + Transaction transaction = transactionCapsule.getInstance().toBuilder().clearSignature() + .addSignature(ByteString.copyFrom(randomSig)).build(); + transactionApprovedList = wallet.getTransactionApprovedList(transaction); + Assert.assertEquals(TransactionApprovedList.Result.response_code.SIGNATURE_FORMAT_ERROR, + transactionApprovedList.getResult().getCode()); + + randomSig = org.tron.keystore.Wallet.generateRandomBytes(65); + transaction = transactionCapsule.getInstance().toBuilder().clearSignature() + .addSignature(ByteString.copyFrom(randomSig)).build(); + transactionApprovedList = wallet.getTransactionApprovedList(transaction); + Assert.assertEquals(TransactionApprovedList.Result.response_code.COMPUTE_ADDRESS_ERROR, + transactionApprovedList.getResult().getCode()); + } } diff --git a/framework/src/test/java/org/tron/core/zksnark/MerkleContainerTest.java b/framework/src/test/java/org/tron/core/zksnark/MerkleContainerTest.java index 6216ab41f3f..183a504ee35 100644 --- a/framework/src/test/java/org/tron/core/zksnark/MerkleContainerTest.java +++ b/framework/src/test/java/org/tron/core/zksnark/MerkleContainerTest.java @@ -23,6 +23,7 @@ import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; +import org.tron.protos.contract.ShieldContract.IncrementalMerkleTree; import org.tron.protos.contract.ShieldContract.IncrementalMerkleVoucherInfo; import org.tron.protos.contract.ShieldContract.OutputPoint; import org.tron.protos.contract.ShieldContract.OutputPointInfo; 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 819bfede557..c963a92bb9d 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 org.junit.Test; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.api.GrpcAPI.DecryptNotes; +import org.tron.api.GrpcAPI.DecryptNotesMarked; import org.tron.api.GrpcAPI.ReceiveNote; import org.tron.api.GrpcAPI.SpendAuthSigParameters; import org.tron.api.GrpcAPI.TransactionExtention; @@ -2377,8 +2378,8 @@ public void pushSameSkAndScanAndSpend() throws Exception { chainBaseManager.addWitness(ByteString.copyFrom(witnessAddress)); //sometimes generate block failed, try several times. - - Block block = getSignedBlock(witnessCapsule.getAddress(), 0, privateKey); + long time = System.currentTimeMillis(); + Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey); dbManager.pushBlock(new BlockCapsule(block)); //create transactions @@ -2426,17 +2427,25 @@ public void pushSameSkAndScanAndSpend() throws Exception { Thread.sleep(500); //package transaction to block - block = getSignedBlock(witnessCapsule.getAddress(), 0, privateKey); + 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()); @@ -2452,6 +2461,7 @@ public void pushSameSkAndScanAndSpend() throws Exception { outPointBuild.setIndex(i); request.addOutPoints(outPointBuild.build()); } + request.setBlockNum(1); IncrementalMerkleVoucherInfo merkleVoucherInfo = wallet .getMerkleTreeVoucherInfo(request.build()); From e987efb94812bfed6097ee102262711d0bd47eb6 Mon Sep 17 00:00:00 2001 From: Brown Jiang <317787106@qq.com> Date: Thu, 12 Oct 2023 16:55:44 +0800 Subject: [PATCH 25/31] feat(net): fix bug of getting head block concurrently (#5532) * fix bug of getting head block concurrently --- .../messagehandler/SyncBlockChainMsgHandler.java | 16 ++++++++-------- .../SyncBlockChainMsgHandlerTest.java | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) 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 3c83bc51e47..958ebfe5561 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 @@ -37,8 +37,8 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep long remainNum = 0; List summaryChainIds = syncBlockChainMessage.getBlockIds(); - - LinkedList blockIds = getLostBlockIds(summaryChainIds); + BlockId headID = tronNetDelegate.getHeadBlockId(); + LinkedList blockIds = getLostBlockIds(summaryChainIds, headID); if (blockIds.size() == 0) { logger.warn("Can't get lost block Ids"); @@ -48,7 +48,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep peer.setNeedSyncFromUs(false); } else { peer.setNeedSyncFromUs(true); - remainNum = tronNetDelegate.getHeadBlockId().getNum() - blockIds.peekLast().getNum(); + remainNum = headID.getNum() - blockIds.peekLast().getNum(); } peer.setLastSyncBlockId(blockIds.peekLast()); @@ -85,14 +85,15 @@ private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2p return true; } - private LinkedList getLostBlockIds(List blockIds) throws P2pException { + private LinkedList getLostBlockIds(List blockIds, BlockId headID) + throws P2pException { BlockId unForkId = getUnForkId(blockIds); - LinkedList ids = getBlockIds(unForkId.getNum()); + LinkedList ids = getBlockIds(unForkId.getNum(), headID); if (ids.isEmpty() || !unForkId.equals(ids.peekFirst())) { unForkId = getUnForkId(blockIds); - ids = getBlockIds(unForkId.getNum()); + ids = getBlockIds(unForkId.getNum(), headID); } return ids; @@ -114,8 +115,7 @@ private BlockId getUnForkId(List blockIds) throws P2pException { return unForkId; } - private LinkedList getBlockIds(Long unForkNum) throws P2pException { - BlockId headID = tronNetDelegate.getHeadBlockId(); + private LinkedList getBlockIds(Long unForkNum, BlockId headID) throws P2pException { long headNum = headID.getNum(); long len = Math.min(headNum, unForkNum + NetConstants.SYNC_FETCH_BATCH_NUM); 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 25108e19b70..d4ad32dd34f 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 @@ -15,6 +15,7 @@ import org.tron.common.application.TronApplicationContext; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.BlockCapsule.BlockId; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.exception.P2pException; @@ -68,18 +69,18 @@ public void testProcessMessage() throws Exception { Assert.assertTrue(!f); Method method1 = handler.getClass().getDeclaredMethod( - "getLostBlockIds", List.class); + "getLostBlockIds", List.class, BlockId.class); method1.setAccessible(true); try { - method1.invoke(handler, blockIds); + method1.invoke(handler, blockIds, new BlockCapsule.BlockId()); } catch (InvocationTargetException e) { Assert.assertEquals("unForkId is null", e.getTargetException().getMessage()); } Method method2 = handler.getClass().getDeclaredMethod( - "getBlockIds", Long.class); + "getBlockIds", Long.class, BlockId.class); method2.setAccessible(true); - List list = (List) method2.invoke(handler, 0L); + List list = (List) method2.invoke(handler, 0L, new BlockCapsule.BlockId()); Assert.assertEquals(1, list.size()); } From 2ab26773fb1aa5de33e9f0f33ee10333833683f0 Mon Sep 17 00:00:00 2001 From: lxcmyf Date: Mon, 16 Oct 2023 11:50:04 +0800 Subject: [PATCH 26/31] fix(api):rollback due to insufficient compatibility (#5537) --- .../http/GetAssetIssueListServlet.java | 30 ++++------------- .../services/http/GetNowBlockServlet.java | 33 +++++-------------- .../core/services/http/ListNodesServlet.java | 30 ++++------------- .../services/http/ListProposalsServlet.java | 30 ++++------------- 4 files changed, 29 insertions(+), 94 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java b/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java index 01ec00dbef3..3968554a671 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java @@ -1,8 +1,5 @@ package org.tron.core.services.http; -import static org.tron.core.services.http.Util.existVisible; - -import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -22,7 +19,12 @@ public class GetAssetIssueListServlet extends RateLimiterServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - response(response, visible); + AssetIssueList reply = wallet.getAssetIssueList(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } catch (Exception e) { Util.processError(e, response); } @@ -30,24 +32,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - try { - PostParams params = PostParams.getPostParams(request); - boolean visible = Util.getVisible(request); - if (!existVisible(request)) { - visible = params.isVisible(); - } - response(response, visible); - } catch (Exception e) { - Util.processError(e, response); - } - } - - private void response(HttpServletResponse response, boolean visible) throws IOException { - AssetIssueList reply = wallet.getAssetIssueList(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + doGet(request, response); } } diff --git a/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java b/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java index 78e7178cfe3..56e01d557f5 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java @@ -1,9 +1,5 @@ package org.tron.core.services.http; -import static org.tron.core.services.http.Util.existVisible; -import static org.tron.core.services.http.Util.getVisible; - -import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -22,8 +18,13 @@ public class GetNowBlockServlet extends RateLimiterServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { - boolean visible = getVisible(request); - response(response, visible); + boolean visible = Util.getVisible(request); + Block reply = wallet.getNowBlock(); + if (reply != null) { + response.getWriter().println(Util.printBlock(reply, visible)); + } else { + response.getWriter().println("{}"); + } } catch (Exception e) { Util.processError(e, response); } @@ -31,24 +32,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - try { - PostParams params = PostParams.getPostParams(request); - boolean visible = getVisible(request); - if (!existVisible(request)) { - visible = params.isVisible(); - } - response(response, visible); - } catch (Exception e) { - Util.processError(e, response); - } - } - - private void response(HttpServletResponse response, boolean visible) throws IOException { - Block reply = wallet.getNowBlock(); - if (reply != null) { - response.getWriter().println(Util.printBlock(reply, visible)); - } else { - response.getWriter().println("{}"); - } + doGet(request, response); } } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java b/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java index fb0b877832b..6b7b2b251a8 100644 --- a/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java @@ -1,8 +1,5 @@ package org.tron.core.services.http; -import static org.tron.core.services.http.Util.existVisible; - -import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -22,7 +19,12 @@ public class ListNodesServlet extends RateLimiterServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - response(response, visible); + NodeList reply = wallet.listNodes(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } catch (Exception e) { Util.processError(e, response); } @@ -30,24 +32,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - try { - PostParams params = PostParams.getPostParams(request); - boolean visible = Util.getVisible(request); - if (!existVisible(request)) { - visible = params.isVisible(); - } - response(response, visible); - } catch (Exception e) { - Util.processError(e, response); - } - } - - private void response(HttpServletResponse response, boolean visible) throws IOException { - NodeList reply = wallet.listNodes(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + doGet(request, response); } } diff --git a/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java b/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java index e309963edda..a3b26d4afc6 100644 --- a/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java @@ -1,8 +1,5 @@ package org.tron.core.services.http; -import static org.tron.core.services.http.Util.existVisible; - -import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -22,7 +19,12 @@ public class ListProposalsServlet extends RateLimiterServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - response(response, visible); + ProposalList reply = wallet.getProposalList(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } catch (Exception e) { Util.processError(e, response); } @@ -30,24 +32,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - try { - PostParams params = PostParams.getPostParams(request); - boolean visible = Util.getVisible(request); - if (!existVisible(request)) { - visible = params.isVisible(); - } - response(response, visible); - } catch (Exception e) { - Util.processError(e, response); - } - } - - private void response(HttpServletResponse response, boolean visible) throws IOException { - ProposalList reply = wallet.getProposalList(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + doGet(request, response); } } From 1cb7f2cf7f8ac1086c3dd0824bbcaecf9d3339ea Mon Sep 17 00:00:00 2001 From: liukai Date: Mon, 16 Oct 2023 11:53:09 +0800 Subject: [PATCH 27/31] feat(version): update version --- 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 b0837871d5d..54eb5d3de6e 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.1.1-343-g959e0199de"; public static final String VERSION_CODE = "18031"; - private static final String VERSION = "4.7.2"; + private static final String VERSION = "4.7.3"; public static String getVersion() { return VERSION; From 87db52abccb5288f1d18ec6be16a5685a1068044 Mon Sep 17 00:00:00 2001 From: Brown Jiang <317787106@qq.com> Date: Mon, 16 Oct 2023 18:36:57 +0800 Subject: [PATCH 28/31] update libp2p version to release 2.1.0 (#5539) --- common/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/build.gradle b/common/build.gradle index 8cca0853dc5..0431316c009 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -53,7 +53,7 @@ dependencies { compile 'org.aspectj:aspectjrt:1.8.13' compile 'org.aspectj:aspectjweaver:1.8.13' compile 'org.aspectj:aspectjtools:1.8.13' - compile group: 'com.github.tronprotocol', name: 'libp2p', version: 'test-v2.0.3',{ + compile group: 'io.github.tronprotocol', name: 'libp2p', version: '2.1.0',{ exclude group: 'io.grpc', module: 'grpc-context' exclude group: 'io.grpc', module: 'grpc-core' exclude group: 'io.grpc', module: 'grpc-netty' From d1f0ff3d3ce3bc0075899fed7f661500e759a117 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 18 Oct 2023 06:26:14 +0800 Subject: [PATCH 29/31] add switch for transaction cache initialization optimization (#5543) --- .../main/java/org/tron/core/db2/common/TxCacheDB.java | 9 +++++++++ .../main/java/org/tron/core/config/args/Storage.java | 10 ++++++++++ .../src/main/java/org/tron/core/config/args/Args.java | 2 ++ framework/src/main/resources/config.conf | 2 ++ framework/src/test/resources/config-test.conf | 2 ++ 5 files changed, 25 insertions(+) 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 3da6cd9475a..9d2409685c9 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 @@ -89,6 +89,7 @@ public class TxCacheDB implements DB, Flusher { private final Path cacheProperties; private final Path cacheDir; private AtomicBoolean isValid = new AtomicBoolean(false); + private boolean txCacheInitOptimization; @Getter @Setter @@ -128,6 +129,8 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore, this.cacheFile0 = Paths.get(cacheDir.toString(), "bloomFilters_0"); this.cacheFile1 = Paths.get(cacheDir.toString(), "bloomFilters_1"); this.cacheProperties = Paths.get(cacheDir.toString(), "txCache.properties"); + this.txCacheInitOptimization = CommonParameter.getInstance() + .getStorage().isTxCacheInitOptimization(); } @@ -280,6 +283,12 @@ public void reset() { } private boolean recovery() { + if (!txCacheInitOptimization) { + logger.info("txCache init optimization is disabled, skip fast recovery mode."); + logger.info("If you want fast recovery mode," + + " please set `storage.txCache.initOptimization = true` in config.conf."); + return false; + } FileUtil.createDirIfNotExists(this.cacheDir.toString()); logger.info("recovery bloomFilters start."); CompletableFuture loadProperties = CompletableFuture.supplyAsync(this::loadProperties); 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 778d4d0141c..22dc239c167 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 @@ -77,6 +77,7 @@ public class Storage { private static final String CHECKPOINT_SYNC_KEY = "storage.checkpoint.sync"; private static final String CACHE_STRATEGIES = "storage.cache.strategies"; + public static final String TX_CACHE_INIT_OPTIMIZATION = "storage.txCache.initOptimization"; /** * Default values of directory @@ -145,6 +146,10 @@ public class Storage { @Setter private int estimatedBlockTransactions; + @Getter + @Setter + private boolean txCacheInitOptimization = false; + // second cache private final Map cacheStrategies = Maps.newConcurrentMap(); @@ -235,6 +240,11 @@ public static int getEstimatedTransactionsFromConfig(final Config config) { return estimatedTransactions; } + public static boolean getTxCacheInitOptimizationFromConfig(final Config config) { + return config.hasPath(TX_CACHE_INIT_OPTIMIZATION) + && config.getBoolean(TX_CACHE_INIT_OPTIMIZATION); + } + public void setCacheStrategies(Config config) { if (config.hasPath(CACHE_STRATEGIES)) { 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 b3a6821f3b2..5c2d76afbb1 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 @@ -520,6 +520,8 @@ public static void setParam(final String[] args, final String confFileName) { PARAMETER.storage.setEstimatedBlockTransactions( Storage.getEstimatedTransactionsFromConfig(config)); + PARAMETER.storage.setTxCacheInitOptimization( + Storage.getTxCacheInitOptimizationFromConfig(config)); PARAMETER.storage.setMaxFlushCount(Storage.getSnapshotMaxFlushCountFromConfig(config)); PARAMETER.storage.setDefaultDbOptions(config); diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index c049bbbebc7..91f108b74a9 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -95,6 +95,8 @@ 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 } node.discovery = { diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index 236502d3360..cf7ca5e2f42 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -66,6 +66,8 @@ 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 = 50 + # if true, transaction cache initialization will be faster. default false + txCache.initOptimization = true } From 5bafc0ce1f606e254568329f8157be64e47bc976 Mon Sep 17 00:00:00 2001 From: allen <56535423+jwrct@users.noreply.github.com> Date: Wed, 25 Oct 2023 02:47:06 -0500 Subject: [PATCH 30/31] Add block height check when processing PBFT messages (#5548) * fix(pbft): Add block height check when processing PBFT messages --- .../tron/common/parameter/CommonParameter.java | 3 +++ .../src/main/java/org/tron/core/Constant.java | 1 + .../java/org/tron/core/config/args/Args.java | 5 +++++ .../java/org/tron/core/net/TronNetDelegate.java | 8 ++++++++ .../core/net/messagehandler/PbftMsgHandler.java | 17 +++++++++++++++++ 5 files changed, 34 insertions(+) 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 8c4cd23f09a..f8f6e6c5edb 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -518,6 +518,9 @@ public class CommonParameter { public int pBFTHttpPort; @Getter @Setter + public long pBFTExpireNum; + @Getter + @Setter public long oldSolidityBlockNum = -1; @Getter/**/ diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 54ff30d9e65..ba867793b41 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -303,6 +303,7 @@ public class Constant { public static final String SEED_NODE_IP_LIST = "seed.node.ip.list"; public static final String NODE_METRICS_ENABLE = "node.metricsEnable"; public static final String COMMITTEE_ALLOW_PBFT = "committee.allowPBFT"; + public static final String COMMITTEE_PBFT_EXPIRE_NUM = "committee.pBFTExpireNum"; public static final String NODE_AGREE_NODE_COUNT = "node.agreeNodeCount"; public static final String COMMITTEE_ALLOW_TRANSACTION_FEE_POOL = "committee.allowTransactionFeePool"; 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 5c2d76afbb1..75140dd5f54 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 @@ -149,6 +149,7 @@ public static void clearParam() { PARAMETER.fullNodeHttpPort = 0; PARAMETER.solidityHttpPort = 0; PARAMETER.pBFTHttpPort = 0; + PARAMETER.pBFTExpireNum = 20; PARAMETER.jsonRpcHttpFullNodePort = 0; PARAMETER.jsonRpcHttpSolidityPort = 0; PARAMETER.jsonRpcHttpPBFTPort = 0; @@ -974,6 +975,10 @@ public static void setParam(final String[] args, final String confFileName) { config.hasPath(Constant.COMMITTEE_ALLOW_PBFT) ? config .getLong(Constant.COMMITTEE_ALLOW_PBFT) : 0; + PARAMETER.pBFTExpireNum = + config.hasPath(Constant.COMMITTEE_PBFT_EXPIRE_NUM) ? config + .getLong(Constant.COMMITTEE_PBFT_EXPIRE_NUM) : 20; + PARAMETER.agreeNodeCount = config.hasPath(Constant.NODE_AGREE_NODE_COUNT) ? config .getInt(Constant.NODE_AGREE_NODE_COUNT) : MAX_ACTIVE_WITNESS_NUM * 2 / 3 + 1; PARAMETER.agreeNodeCount = PARAMETER.agreeNodeCount > MAX_ACTIVE_WITNESS_NUM 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 4d22f98d680..5d09e67908d 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetDelegate.java +++ b/framework/src/main/java/org/tron/core/net/TronNetDelegate.java @@ -357,4 +357,12 @@ public Object getForkLock() { return dbManager.getForkLock(); } + public long getNextMaintenanceTime() { + return chainBaseManager.getDynamicPropertiesStore().getNextMaintenanceTime(); + } + + public long getMaintenanceTimeInterval() { + return chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); + } + } diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java index 44eed8d1c2f..4e4cc858898 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/PbftMsgHandler.java @@ -11,9 +11,12 @@ import org.tron.consensus.pbft.PbftManager; import org.tron.consensus.pbft.message.PbftBaseMessage; import org.tron.consensus.pbft.message.PbftMessage; +import org.tron.core.config.args.Args; import org.tron.core.exception.P2pException; +import org.tron.core.net.TronNetDelegate; import org.tron.core.net.TronNetService; import org.tron.core.net.peer.PeerConnection; +import org.tron.protos.Protocol.PBFTMessage.DataType; @Component public class PbftMsgHandler { @@ -26,10 +29,24 @@ public class PbftMsgHandler { @Autowired private PbftManager pbftManager; + @Autowired + private TronNetDelegate tronNetDelegate; + public void processMessage(PeerConnection peer, PbftMessage msg) throws Exception { if (Param.getInstance().getPbftInterface().isSyncing()) { return; } + if (msg.getDataType().equals(DataType.BLOCK) + && tronNetDelegate.getHeadBlockId().getNum() - msg.getNumber() + > Args.getInstance().getPBFTExpireNum()) { + return; + } + long currentEpoch = tronNetDelegate.getNextMaintenanceTime(); + long expireEpoch = 2 * tronNetDelegate.getMaintenanceTimeInterval(); + if (msg.getDataType().equals(DataType.SRL) + && currentEpoch - msg.getEpoch() > expireEpoch) { + return; + } msg.analyzeSignature(); String key = buildKey(msg); Lock lock = striped.get(key); From 381c52c5830ab60db02dd7a94d6e8c0dbe0ffef5 Mon Sep 17 00:00:00 2001 From: liukai Date: Wed, 25 Oct 2023 19:31:35 +0800 Subject: [PATCH 31/31] update a new version. version name:GreatVoyage-v4.7.2-140-g9d13f9cb69,version code:18173 --- 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 54eb5d3de6e..4740daa9d5e 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.1.1-343-g959e0199de"; - public static final String VERSION_CODE = "18031"; + public static final String VERSION_NAME = "GreatVoyage-v4.7.2-140-g9d13f9cb69"; + public static final String VERSION_CODE = "18173"; private static final String VERSION = "4.7.3"; public static String getVersion() {