Skip to content

Commit 92f357d

Browse files
authored
Merge pull request #628 from tronprotocol/develop-evan-unittest0
Develop evan unittest0
2 parents 8bd1a9f + 0f52fa4 commit 92f357d

File tree

8 files changed

+104
-55
lines changed

8 files changed

+104
-55
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@
4343
import org.tron.core.capsule.WitnessCapsule;
4444
import org.tron.core.db.AccountStore;
4545
import org.tron.core.db.Manager;
46-
import org.tron.core.exception.*;
46+
import org.tron.core.exception.ContractExeException;
47+
import org.tron.core.exception.ContractValidateException;
48+
import org.tron.core.exception.DupTransactionException;
49+
import org.tron.core.exception.StoreException;
50+
import org.tron.core.exception.TaposException;
51+
import org.tron.core.exception.TooBigTransactionException;
52+
import org.tron.core.exception.ValidateBandwidthException;
53+
import org.tron.core.exception.ValidateSignatureException;
4754
import org.tron.core.net.message.TransactionMessage;
4855
import org.tron.core.net.node.NodeImpl;
4956
import org.tron.protos.Contract.AccountCreateContract;
@@ -191,6 +198,7 @@ public Account getBalance(Account account) {
191198
/**
192199
* Create a transaction by contract.
193200
*/
201+
@Deprecated
194202
public Transaction createTransaction(TransferContract contract) {
195203
AccountStore accountStore = dbManager.getAccountStore();
196204
return new TransactionCapsule(contract, accountStore).getInstance();
@@ -244,7 +252,7 @@ public Transaction createTransaction(VoteWitnessContract voteWitnessContract) {
244252
public Transaction createTransaction(AssetIssueContract assetIssueContract) {
245253
return new TransactionCapsule(assetIssueContract).getInstance();
246254
}
247-
255+
@Deprecated
248256
public Transaction createTransaction(WitnessCreateContract witnessCreateContract) {
249257
return new TransactionCapsule(witnessCreateContract).getInstance();
250258
}
@@ -287,11 +295,11 @@ public WitnessList getWitnessList() {
287295
.forEach(witnessCapsule -> builder.addWitnesses(witnessCapsule.getInstance()));
288296
return builder.build();
289297
}
290-
298+
@Deprecated
291299
public Transaction createTransaction(TransferAssetContract transferAssetContract) {
292300
return new TransactionCapsule(transferAssetContract).getInstance();
293301
}
294-
302+
@Deprecated
295303
public Transaction createTransaction(
296304
ParticipateAssetIssueContract participateAssetIssueContract) {
297305
return new TransactionCapsule(participateAssetIssueContract).getInstance();

src/main/java/org/tron/core/actuator/CreateAccountActuator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public class CreateAccountActuator extends AbstractActuator {
2323
}
2424

2525
@Override
26+
@Deprecated //Can not create account by api. Need send more than 1 trx , will create account if not exit.
2627
public boolean execute(TransactionResultCapsule ret)
2728
throws ContractExeException {
2829
long fee = calcFee();
2930
try {
3031

3132
AccountCreateContract accountCreateContract = contract.unpack(AccountCreateContract.class);
32-
AccountCapsule accountCapsule = new AccountCapsule(accountCreateContract);
33+
AccountCapsule accountCapsule = new AccountCapsule(accountCreateContract,
34+
System.currentTimeMillis());
3335
dbManager.getAccountStore()
3436
.put(accountCreateContract.getOwnerAddress().toByteArray(), accountCapsule);
3537
ret.setStatus(fee, code.SUCESS);
@@ -42,6 +44,7 @@ public boolean execute(TransactionResultCapsule ret)
4244
}
4345

4446
@Override
47+
@Deprecated //Can not create account by api. Need send more than 1 trx , will create account if not exit.
4548
public boolean validate() throws ContractValidateException {
4649
try {
4750
if (!contract.is(AccountCreateContract.class)) {
@@ -69,11 +72,13 @@ public boolean validate() throws ContractValidateException {
6972
}
7073

7174
@Override
75+
@Deprecated //Can not create account by api. Need send more than 1 trx , will create account if not exit.
7276
public ByteString getOwnerAddress() throws InvalidProtocolBufferException {
7377
return contract.unpack(AccountCreateContract.class).getOwnerAddress();
7478
}
7579

7680
@Override
81+
@Deprecated //Can not create account by api. Need send more than 1 trx , will create account if not exit.
7782
public long calcFee() {
7883
return 0;
7984
}

src/main/java/org/tron/core/actuator/TransferActuator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public boolean validate() throws ContractValidateException {
106106
throw new ContractValidateException(
107107
"For a non-existent account transfer, the minimum amount is 1 TRX");
108108
}
109-
toAccount = new AccountCapsule(transferContract.getToAddress(), AccountType.Normal);
109+
toAccount = new AccountCapsule(transferContract.getToAddress(), AccountType.Normal, System.currentTimeMillis());
110110
dbManager.getAccountStore().put(transferContract.getToAddress().toByteArray(), toAccount);
111111
} else {
112112
//check to account balance if overflow

src/main/java/org/tron/core/actuator/TransferAssetActuator.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.tron.core.exception.ContractExeException;
3030
import org.tron.core.exception.ContractValidateException;
3131
import org.tron.protos.Contract.TransferAssetContract;
32-
import org.tron.protos.Protocol.AccountType;
3332
import org.tron.protos.Protocol.Transaction.Result.code;
3433

3534
public class TransferAssetActuator extends AbstractActuator {
@@ -131,14 +130,12 @@ public boolean validate() throws ContractValidateException {
131130
AccountCapsule toAccount = this.dbManager.getAccountStore()
132131
.get(transferAssetContract.getToAddress().toByteArray());
133132
if (toAccount == null) {
134-
toAccount = new AccountCapsule(transferAssetContract.getToAddress(), AccountType.Normal);
135-
dbManager.getAccountStore()
136-
.put(transferAssetContract.getToAddress().toByteArray(), toAccount);
137-
} else {
138-
assetBalance = toAccount.getAssetMap().get(ByteArray.toStr(nameKey));
139-
if (assetBalance != null) {
140-
assetBalance = Math.addExact(assetBalance, amount); //check if overflow
141-
}
133+
throw new ContractValidateException("To account is not exit!");
134+
}
135+
136+
assetBalance = toAccount.getAssetMap().get(ByteArray.toStr(nameKey));
137+
if (assetBalance != null) {
138+
assetBalance = Math.addExact(assetBalance, amount); //check if overflow
142139
}
143140
} catch (InvalidProtocolBufferException e) {
144141
throw new ContractValidateException(e.getMessage());

src/main/java/org/tron/core/capsule/AccountCapsule.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public AccountCapsule(final AccountCreateContract contract) {
7777
.build();
7878
}
7979

80+
/**
81+
* construct account from AccountCreateContract and creatTime.
82+
*/
83+
public AccountCapsule(final AccountCreateContract contract, long createTime) {
84+
this.account = Account.newBuilder()
85+
.setAccountName(contract.getAccountName())
86+
.setType(contract.getType())
87+
.setAddress(contract.getOwnerAddress())
88+
.setTypeValue(contract.getTypeValue())
89+
.setCreateTime(createTime)
90+
.build();
91+
}
92+
8093
/**
8194
* construct account from AccountUpdateContract
8295
*/
@@ -107,6 +120,17 @@ public AccountCapsule(ByteString address,
107120
.build();
108121
}
109122

123+
/**
124+
* get account from address.
125+
*/
126+
public AccountCapsule(ByteString address,
127+
AccountType accountType, long createTime) {
128+
this.account = Account.newBuilder()
129+
.setType(accountType)
130+
.setAddress(address)
131+
.setCreateTime(createTime)
132+
.build();
133+
}
110134

111135
public AccountCapsule(Account account) {
112136
this.account = account;

src/main/java/org/tron/core/services/RpcApiService.java

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.tron.core.exception.ContractValidateException;
4949
import org.tron.core.exception.StoreException;
5050
import org.tron.protos.Contract;
51-
import org.tron.protos.Contract.AccountCreateContract;
5251
import org.tron.protos.Contract.AssetIssueContract;
5352
import org.tron.protos.Contract.ParticipateAssetIssueContract;
5453
import org.tron.protos.Contract.TransferAssetContract;
@@ -381,22 +380,6 @@ public void broadcastTransaction(Transaction req,
381380
responseObserver.onCompleted();
382381
}
383382

384-
@Override
385-
public void createAccount(AccountCreateContract request,
386-
StreamObserver<Transaction> responseObserver) {
387-
try {
388-
responseObserver.onNext(
389-
createTransactionCapsule(request, ContractType.AccountCreateContract).getInstance());
390-
} catch (ContractValidateException e) {
391-
responseObserver
392-
.onNext(null);
393-
logger.debug("ContractValidateException", e.getMessage());
394-
responseObserver.onNext(null);
395-
}
396-
responseObserver.onCompleted();
397-
}
398-
399-
400383
@Override
401384
public void createAssetIssue(AssetIssueContract request,
402385
StreamObserver<Transaction> responseObserver) {
@@ -578,28 +561,27 @@ public void listNodes(EmptyMessage request, StreamObserver<NodeList> responseObs
578561
@Override
579562
public void transferAsset(TransferAssetContract request,
580563
StreamObserver<Transaction> responseObserver) {
581-
ByteString fromBs = request.getOwnerAddress();
582-
583-
if (fromBs != null) {
584-
585-
Transaction trx = wallet.createTransaction(request);
586-
responseObserver.onNext(trx);
587-
} else {
588-
responseObserver.onNext(null);
564+
try {
565+
responseObserver
566+
.onNext(createTransactionCapsule(request, ContractType.TransferAssetContract).getInstance());
567+
} catch (ContractValidateException e) {
568+
responseObserver
569+
.onNext(null);
570+
logger.debug("ContractValidateException", e.getMessage());
589571
}
590572
responseObserver.onCompleted();
591573
}
592574

593575
@Override
594576
public void participateAssetIssue(ParticipateAssetIssueContract request,
595577
StreamObserver<Transaction> responseObserver) {
596-
ByteString fromBs = request.getOwnerAddress();
597-
598-
if (fromBs != null) {
599-
Transaction trx = wallet.createTransaction(request);
600-
responseObserver.onNext(trx);
601-
} else {
602-
responseObserver.onNext(null);
578+
try {
579+
responseObserver
580+
.onNext(createTransactionCapsule(request, ContractType.ParticipateAssetIssueContract).getInstance());
581+
} catch (ContractValidateException e) {
582+
responseObserver
583+
.onNext(null);
584+
logger.debug("ContractValidateException", e.getMessage());
603585
}
604586
responseObserver.onCompleted();
605587
}

src/test/java/org/tron/core/actuator/TransferActuatorTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,39 @@ public void noExitToAccount() {
332332
} finally {
333333
dbManager.getAccountStore().delete(ByteArray.fromHexString(To_ACCOUNT_INVALIATE));
334334
}
335+
}
336+
337+
@Test
338+
/**
339+
* If to account not exit, transfer trx must great than 1_000_000L drop.
340+
*/
341+
public void noExitToAccountTooLittle() {
342+
TransferActuator actuator = new TransferActuator(
343+
getContract(999_999L, OWNER_ADDRESS, To_ACCOUNT_INVALIATE), dbManager);
344+
TransactionResultCapsule ret = new TransactionResultCapsule();
345+
try {
346+
AccountCapsule noExitAccount = dbManager.getAccountStore()
347+
.get(ByteArray.fromHexString(To_ACCOUNT_INVALIATE));
348+
Assert.assertTrue(null == noExitAccount);
349+
actuator.validate();
350+
Assert.assertEquals(noExitAccount.getBalance(), 0);
351+
actuator.execute(ret);
352+
} catch (ContractValidateException e) {
353+
Assert.assertTrue(e instanceof ContractValidateException);
354+
AccountCapsule noExitAccount = dbManager.getAccountStore()
355+
.get(ByteArray.fromHexString(To_ACCOUNT_INVALIATE));
356+
Assert.assertTrue(null == noExitAccount);
357+
AccountCapsule owner = dbManager.getAccountStore()
358+
.get(ByteArray.fromHexString(OWNER_ADDRESS));
359+
AccountCapsule toAccount = dbManager.getAccountStore()
360+
.get(ByteArray.fromHexString(TO_ADDRESS));
361+
Assert.assertEquals(owner.getBalance(), OWNER_BALANCE);
362+
Assert.assertEquals(toAccount.getBalance(), TO_BALANCE);
363+
} catch (ContractExeException e) {
364+
Assert.assertFalse(e instanceof ContractExeException);
365+
} finally {
366+
dbManager.getAccountStore().delete(ByteArray.fromHexString(To_ACCOUNT_INVALIATE));
367+
}
335368

336369
}
337370

src/test/java/org/tron/core/actuator/TransferAssetActuatorTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ public void noExitOwnerAccount() {
336336
* If to account not exit, creat it.
337337
*/
338338
public void noExitToAccount() {
339-
TransferAssetActuator actuator = new TransferAssetActuator(getContract(100L, OWNER_ADDRESS, NOT_EXIT_ADDRESS), dbManager);
339+
TransferAssetActuator actuator = new TransferAssetActuator(
340+
getContract(100L, OWNER_ADDRESS, NOT_EXIT_ADDRESS), dbManager);
340341
TransactionResultCapsule ret = new TransactionResultCapsule();
341342
try {
342343
AccountCapsule noExitAccount = dbManager.getAccountStore()
@@ -348,19 +349,18 @@ public void noExitToAccount() {
348349
Assert.assertFalse(null == noExitAccount); //Had created.
349350
Assert.assertEquals(noExitAccount.getBalance(), 0);
350351
actuator.execute(ret);
352+
} catch (ContractValidateException e) {
353+
Assert.assertTrue(e instanceof ContractValidateException);
354+
Assert.assertEquals("To account is not exit!", e.getMessage());
351355
AccountCapsule owner = dbManager.getAccountStore()
352356
.get(ByteArray.fromHexString(OWNER_ADDRESS));
353357
Assert
354-
.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE - 100);
355-
noExitAccount = dbManager.getAccountStore()
358+
.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE);
359+
AccountCapsule noExitAccount = dbManager.getAccountStore()
356360
.get(ByteArray.fromHexString(NOT_EXIT_ADDRESS));
357-
Assert.assertEquals(noExitAccount.getAssetMap().get(ASSET_NAME).longValue(), 100);
358-
} catch (ContractValidateException e) {
359-
Assert.assertFalse(e instanceof ContractValidateException);
361+
Assert.assertTrue(noExitAccount == null);
360362
} catch (ContractExeException e) {
361363
Assert.assertFalse(e instanceof ContractExeException);
362-
} finally {
363-
dbManager.getAccountStore().delete(ByteArray.fromHexString(NOT_EXIT_ADDRESS));
364364
}
365365

366366
}

0 commit comments

Comments
 (0)