-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test(db): add test for db #5461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
halibobo1205
merged 3 commits into
tronprotocol:release_v4.7.3
from
lurais:feature/add_cover_test
Sep 22, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
framework/src/test/java/org/tron/core/db/AbiStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
|
lurais marked this conversation as resolved.
|
||
| 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); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
framework/src/test/java/org/tron/core/db/AccountTraceStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
lurais marked this conversation as resolved.
|
||
| 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); | ||
|
lurais marked this conversation as resolved.
|
||
| Assert.assertNotNull(accountTraceStore.get(Bytes.concat(address, | ||
| Longs.toByteArray(1L ^ Long.MAX_VALUE)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetPrevBalance() { | ||
| accountTraceStore.recordBalanceWithBlock(address,2,9999); | ||
| Pair<Long, Long> 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<Long,Long> pair2 = accountTraceStore.getPrevBalance(address2, 3); | ||
| Assert.assertEquals((long)pair2.getKey(),3L); | ||
| Assert.assertEquals((long)pair2.getValue(), 99L); | ||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
framework/src/test/java/org/tron/core/db/BalanceTraceStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
|
|
||
| } |
74 changes: 74 additions & 0 deletions
74
framework/src/test/java/org/tron/core/db/CodeStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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))); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.