Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
17 changes: 17 additions & 0 deletions framework/src/test/java/org/tron/common/config/args/ArgsTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
383 changes: 383 additions & 0 deletions framework/src/test/java/org/tron/core/ShieldWalletTest.java

Large diffs are not rendered by default.

92 changes: 74 additions & 18 deletions framework/src/test/java/org/tron/core/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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());

Expand Down