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
4 changes: 0 additions & 4 deletions src/main/java/org/tron/common/utils/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ public static int byteArrayToInt(byte[] b) {
return new BigInteger(1, b).intValue();
}

public static boolean isNullOrZeroArray(byte[] array) {
return (array == null) || (array.length == 0);
}

public static boolean isSingleZero(byte[] array) {
return (array.length == 1 && array[0] == 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -38,7 +39,6 @@
import org.tron.common.overlay.message.Message;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Utils;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
Expand Down Expand Up @@ -112,7 +112,7 @@ public static void setAddressPreFixByte(byte addressPreFixByte) {
}

public static boolean addressValid(byte[] address) {
if (ByteUtil.isNullOrZeroArray(address)) {
if (ArrayUtils.isEmpty(address)) {
logger.warn("Warning: Address is empty !!");
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/tron/core/actuator/WitnessCreateActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.capsule.utils.TransactionUtil;
import org.tron.core.db.Manager;
import org.tron.core.exception.BalanceInsufficientException;
import org.tron.core.exception.ContractExeException;
Expand Down Expand Up @@ -55,6 +56,11 @@ public boolean validate() throws ContractValidateException {
if (!Wallet.addressValid(contract.getOwnerAddress().toByteArray())) {
throw new ContractValidateException("Invalidate address");
}

if (!TransactionUtil.validUrl(contract.getUrl().toByteArray())) {
throw new ContractValidateException("Invalidate url");
}

Preconditions.checkArgument(
this.dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray()),
"account[" + readableOwnerAddress + "] not exists");
Expand All @@ -70,7 +76,6 @@ public boolean validate() throws ContractValidateException {
accountCapsule.getBalance() >= dbManager.getDynamicPropertiesStore()
.getAccountUpgradeCost(),
"balance < AccountUpgradeCost");

} catch (final Exception ex) {
ex.printStackTrace();
throw new ContractValidateException(ex.getMessage());
Expand Down Expand Up @@ -112,8 +117,5 @@ private void createWitness(final WitnessCreateContract witnessCreateContract) {
} catch (BalanceInsufficientException e) {
throw new RuntimeException(e);
}


}

}
15 changes: 13 additions & 2 deletions src/main/java/org/tron/core/capsule/utils/TransactionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.ByteUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.core.Wallet;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.protos.Contract.TransferContract;
Expand Down Expand Up @@ -51,7 +51,7 @@ private static boolean checkBalance(long totalBalance, long totalSpent) {
}

public static boolean validAccountName(byte[] accountName) {
if (ByteUtil.isNullOrZeroArray(accountName)) {
if (ArrayUtils.isEmpty(accountName)) {
return false;
}
if (accountName.length > 32) {
Expand All @@ -68,6 +68,17 @@ public static boolean validAccountName(byte[] accountName) {
}
return true;
}

public static boolean validUrl(byte[] url) {
if (ArrayUtils.isEmpty(url)) {
return false;
}
if (url.length > 256) {
return false;
}
// other rules.
return true;
}
/**
* Get sender.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ private Any getContract(String address, String url) {
.build());
}

private Any getContract(String address, ByteString url) {
return Any.pack(
Contract.WitnessCreateContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(address)))
.setUrl(url)
.build());
}

/**
* first createWitness,result is success.
*/
Expand Down Expand Up @@ -142,11 +150,10 @@ public void secondCreateAccount() {
try {
actuator.validate();
actuator.execute(ret);

Assert.assertFalse(true);
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Witness[" + OWNER_ADDRESS_SECOND + "] has existed", e.getMessage());

} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}
Expand All @@ -164,16 +171,86 @@ public void invalidateAddress() {
actuator.validate();
actuator.execute(ret);
fail("Invalidate address");
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Invalidate address", e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}
}

/**
* use Invalidate url createWitness,result is failed,exception is "Invalidate url".
*/
@Test
public void invalidateUrlTest() {
TransactionResultCapsule ret = new TransactionResultCapsule();
//Url cannot empty
try {
WitnessCreateActuator actuator = new WitnessCreateActuator(
getContract(OWNER_ADDRESS_FRIST, ByteString.EMPTY), dbManager);
actuator.validate();
actuator.execute(ret);
fail("Invalidate url");
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Invalidate url", e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

Assert.assertEquals("Invalidate address", e.getMessage());
//256 bytes
String url256Bytes = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
//Url length can not greater than 256
try {
WitnessCreateActuator actuator = new WitnessCreateActuator(
getContract(OWNER_ADDRESS_FRIST, ByteString.copyFromUtf8(url256Bytes + "0")), dbManager);
actuator.validate();
actuator.execute(ret);
fail("Invalidate url");
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Invalidate url", e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

// 1 byte url is ok.
try {
WitnessCreateActuator actuator = new WitnessCreateActuator(
getContract(OWNER_ADDRESS_FRIST, "0"), dbManager);
actuator.validate();
actuator.execute(ret);
Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
WitnessCapsule witnessCapsule =
dbManager.getWitnessStore().get(ByteArray.fromHexString(OWNER_ADDRESS_FRIST));
Assert.assertNotNull(witnessCapsule);
Assert.assertEquals(witnessCapsule.getInstance().getUrl(), "0");
Assert.assertTrue(true);
} catch (ContractValidateException e) {
Assert.assertFalse(e instanceof ContractValidateException);
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

dbManager.getWitnessStore().delete(ByteArray.fromHexString(OWNER_ADDRESS_FRIST));
// 256 bytes url is ok.
try {
WitnessCreateActuator actuator = new WitnessCreateActuator(
getContract(OWNER_ADDRESS_FRIST, url256Bytes), dbManager);
actuator.validate();
actuator.execute(ret);
Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
WitnessCapsule witnessCapsule =
dbManager.getWitnessStore().get(ByteArray.fromHexString(OWNER_ADDRESS_FRIST));
Assert.assertNotNull(witnessCapsule);
Assert.assertEquals(witnessCapsule.getInstance().getUrl(), url256Bytes);
Assert.assertTrue(true);
} catch (ContractValidateException e) {
Assert.assertFalse(e instanceof ContractValidateException);
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}
}

/**
Expand All @@ -189,7 +266,6 @@ public void noAccount() {
actuator.validate();
actuator.execute(ret);
fail("account[+OWNER_ADDRESS_NOACCOUNT+] not exists");

} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("account[" + OWNER_ADDRESS_NOACCOUNT + "] not exists", e.getMessage());
Expand All @@ -202,7 +278,7 @@ public void noAccount() {
/**
* use Account ,result is failed,exception is "account not exists".
*/
// @Test
@Test
public void balanceNotSufficient() {
AccountCapsule balanceNotSufficientCapsule =
new AccountCapsule(
Expand All @@ -221,11 +297,9 @@ public void balanceNotSufficient() {
actuator.execute(ret);
fail("witnessAccount has balance[" + balanceNotSufficientCapsule.getBalance()
+ "] < MIN_BALANCE[100]");

} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("witnessAccount has balance[" + balanceNotSufficientCapsule.getBalance()
+ "] < MIN_BALANCE[100]", e.getMessage());
Assert.assertEquals("balance < AccountUpgradeCost", e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}
Expand Down