Skip to content

Commit 39b71e5

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents be01e6f + 88dc229 commit 39b71e5

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
3333
.unpack(WitnessCreateContract.class);
3434
this.createWitness(witnessCreateContract);
3535
ret.setStatus(fee, code.SUCESS);
36-
} catch (final InvalidProtocolBufferException e) {
36+
} catch (InvalidProtocolBufferException e) {
37+
logger.debug(e.getMessage(), e);
38+
ret.setStatus(fee, code.FAILED);
39+
throw new ContractExeException(e.getMessage());
40+
} catch (BalanceInsufficientException e) {
3741
logger.debug(e.getMessage(), e);
3842
ret.setStatus(fee, code.FAILED);
3943
throw new ContractExeException(e.getMessage());
@@ -93,7 +97,7 @@ public long calcFee() {
9397
return 0;
9498
}
9599

96-
private void createWitness(final WitnessCreateContract witnessCreateContract) {
100+
private void createWitness(final WitnessCreateContract witnessCreateContract) throws BalanceInsufficientException {
97101
//Create Witness by witnessCreateContract
98102
final WitnessCapsule witnessCapsule = new WitnessCapsule(
99103
witnessCreateContract.getOwnerAddress(),
@@ -106,16 +110,10 @@ private void createWitness(final WitnessCreateContract witnessCreateContract) {
106110
.get(witnessCapsule.createDbKey());
107111
accountCapsule.setIsWitness(true);
108112
this.dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);
113+
dbManager.adjustBalance(witnessCreateContract.getOwnerAddress().toByteArray(),
114+
-dbManager.getDynamicPropertiesStore().getAccountUpgradeCost());
109115

110-
try {
111-
dbManager.adjustBalance(witnessCreateContract.getOwnerAddress().toByteArray(),
112-
-dbManager.getDynamicPropertiesStore().getAccountUpgradeCost());
113-
114-
dbManager.adjustBalance(this.dbManager.getAccountStore().getBlackhole().createDbKey(),
115-
+dbManager.getDynamicPropertiesStore().getAccountUpgradeCost());
116-
117-
} catch (BalanceInsufficientException e) {
118-
throw new RuntimeException(e);
119-
}
116+
dbManager.adjustBalance(this.dbManager.getAccountStore().getBlackhole().createDbKey(),
117+
+dbManager.getDynamicPropertiesStore().getAccountUpgradeCost());
120118
}
121119
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ public boolean validate() throws ContractValidateException {
5959
throw new ContractValidateException("Invalidate address");
6060
}
6161

62+
if (!dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray())) {
63+
throw new ContractValidateException("account does not exist");
64+
}
65+
6266
if (!TransactionUtil.validUrl(contract.getUpdateUrl().toByteArray())) {
6367
throw new ContractValidateException("Invalidate url");
6468
}
6569

6670
if (this.dbManager.getWitnessStore().get(contract.getOwnerAddress().toByteArray()) == null) {
67-
throw new ContractValidateException("Witness not existed");
71+
throw new ContractValidateException("Witness does not exist");
6872
}
6973
} catch (final Exception ex) {
7074
ex.printStackTrace();

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

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.tron.common.utils.FileUtil;
1717
import org.tron.core.Constant;
1818
import org.tron.core.Wallet;
19+
import org.tron.core.capsule.AccountCapsule;
1920
import org.tron.core.capsule.TransactionResultCapsule;
2021
import org.tron.core.capsule.WitnessCapsule;
2122
import org.tron.core.config.DefaultConfig;
@@ -24,6 +25,7 @@
2425
import org.tron.core.exception.ContractExeException;
2526
import org.tron.core.exception.ContractValidateException;
2627
import org.tron.protos.Contract;
28+
import org.tron.protos.Protocol;
2729
import org.tron.protos.Protocol.Transaction.Result.code;
2830

2931
@Slf4j
@@ -33,7 +35,10 @@ public class WitnessUpdateActuatorTest {
3335
private static Manager dbManager;
3436
private static final String dbPath = "output_WitnessUpdate_test";
3537
private static final String OWNER_ADDRESS;
36-
private static final String OWNER_ADDRESS_NOTEXIT;
38+
private static final String OWNER_ADDRESS_ACCOUNT_NAME = "test_account";
39+
private static final String OWNER_ADDRESS_NOT_WITNESS;
40+
private static final String OWNER_ADDRESS_NOT_WITNESS_ACCOUNT_NAME = "test_account1";
41+
private static final String OWNER_ADDRESS_NOTEXIST;
3742
private static final String URL = "https://tron.network";
3843
private static final String NewURL = "https://tron.org";
3944
private static final String OWNER_ADDRESS_INVALIATE = "aaaa";
@@ -42,8 +47,10 @@ public class WitnessUpdateActuatorTest {
4247
Args.setParam(new String[]{"--output-directory", dbPath}, Constant.TEST_CONF);
4348
context = new AnnotationConfigApplicationContext(DefaultConfig.class);
4449
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
45-
OWNER_ADDRESS_NOTEXIT =
50+
OWNER_ADDRESS_NOTEXIST =
4651
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
52+
OWNER_ADDRESS_NOT_WITNESS =
53+
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d427122222";
4754
}
4855

4956
/**
@@ -59,11 +66,28 @@ public static void init() {
5966
*/
6067
@Before
6168
public void createCapsule() {
69+
// address in accountStore and witnessStore
70+
AccountCapsule accountCapsule =
71+
new AccountCapsule(
72+
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
73+
ByteString.copyFromUtf8(OWNER_ADDRESS_ACCOUNT_NAME),
74+
Protocol.AccountType.Normal);
75+
dbManager.getAccountStore().put(ByteArray.fromHexString(OWNER_ADDRESS), accountCapsule);
6276
WitnessCapsule ownerCapsule = new WitnessCapsule(
6377
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), 10_000_000L, URL);
64-
6578
dbManager.getWitnessStore().put(ByteArray.fromHexString(OWNER_ADDRESS), ownerCapsule);
66-
dbManager.getWitnessStore().delete(ByteArray.fromHexString(OWNER_ADDRESS_NOTEXIT));
79+
80+
// address exist in accountStore, but is not witness
81+
AccountCapsule accountNotWitnessCapsule =
82+
new AccountCapsule(
83+
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_NOT_WITNESS)),
84+
ByteString.copyFromUtf8(OWNER_ADDRESS_NOT_WITNESS_ACCOUNT_NAME),
85+
Protocol.AccountType.Normal);
86+
dbManager.getAccountStore().put(ByteArray.fromHexString(OWNER_ADDRESS_NOT_WITNESS), accountNotWitnessCapsule);
87+
dbManager.getWitnessStore().delete(ByteArray.fromHexString(OWNER_ADDRESS_NOT_WITNESS));
88+
89+
// address does not exist in accountStore
90+
dbManager.getAccountStore().delete(ByteArray.fromHexString(OWNER_ADDRESS_NOTEXIST));
6791
}
6892

6993
private Any getContract(String address, String url) {
@@ -197,21 +221,41 @@ public void invalidateUrlTest() {
197221
}
198222

199223
/**
200-
* use AccountStore not exists Address createWitness,result is failed,exception is "account not
201-
* exists".
224+
* use AccountStore not exists Address createWitness,result is failed,exception is
225+
* "Witness does not exist"
226+
*/
227+
@Test
228+
public void notExistWitness() {
229+
WitnessUpdateActuator actuator = new WitnessUpdateActuator(
230+
getContract(OWNER_ADDRESS_NOT_WITNESS, URL), dbManager);
231+
TransactionResultCapsule ret = new TransactionResultCapsule();
232+
try {
233+
actuator.validate();
234+
actuator.execute(ret);
235+
fail("witness [+OWNER_ADDRESS_NOACCOUNT+] not exists");
236+
} catch (ContractValidateException e) {
237+
Assert.assertTrue(e instanceof ContractValidateException);
238+
Assert.assertEquals("Witness does not exist", e.getMessage());
239+
} catch (ContractExeException e) {
240+
Assert.assertFalse(e instanceof ContractExeException);
241+
}
242+
}
243+
244+
/**
245+
* if account does not exist in accountStore, the test will throw a Exception
202246
*/
203247
@Test
204-
public void notExitWitness() {
248+
public void notExistAccount() {
205249
WitnessUpdateActuator actuator = new WitnessUpdateActuator(
206-
getContract(OWNER_ADDRESS_NOTEXIT, URL), dbManager);
250+
getContract(OWNER_ADDRESS_NOTEXIST, URL), dbManager);
207251
TransactionResultCapsule ret = new TransactionResultCapsule();
208252
try {
209253
actuator.validate();
210254
actuator.execute(ret);
211-
fail("account[+OWNER_ADDRESS_NOACCOUNT+] not exists");
255+
fail("account does not exist");
212256
} catch (ContractValidateException e) {
213257
Assert.assertTrue(e instanceof ContractValidateException);
214-
Assert.assertEquals("Witness not existed", e.getMessage());
258+
Assert.assertEquals("account does not exist", e.getMessage());
215259
} catch (ContractExeException e) {
216260
Assert.assertFalse(e instanceof ContractExeException);
217261
}

0 commit comments

Comments
 (0)