Skip to content
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

Clean up transaction result code #720

Merged
merged 2 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,24 @@ public ExecutionResult execute(byte[] input, long nrg) {
// check length for both operations
if (input.length < 131) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect input length".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect input length".getBytes());
}

int domainNameLength = input[0];
if (domainNameLength < 0)
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect input length".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect input length".getBytes());

// check if input is too short for extension function
if (input.length < 130 + domainNameLength)
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect input length".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect input length".getBytes());
int balanceLength = input[129 + domainNameLength];

if (balanceLength > 0) {
if (input.length < 130 + domainNameLength + balanceLength) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect input length".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect input length".getBytes());
}
}

Expand All @@ -206,14 +206,14 @@ public ExecutionResult execute(byte[] input, long nrg) {
// check if the domain name already has active parent domain
if (hasActiveParentDomain(domainNameRaw))
return new ExecutionResult(
ResultCode.INTERNAL_ERROR,
ResultCode.FAILURE,
nrg - COST,
"the given domain name has a parent that is already active".getBytes());

// check if the domain name is valid to register
if (!isValidDomainName(domainNameRaw))
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "domain name is invalid".getBytes());
ResultCode.FAILURE, nrg - COST, "domain name is invalid".getBytes());

// add zeros for storing
byte[] domainNameInBytesWithZeros = addLeadingZeros(domainNameInBytes);
Expand All @@ -232,12 +232,12 @@ public ExecutionResult execute(byte[] input, long nrg) {
// verify public key matches owner
if (!b) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect signature".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect signature".getBytes());
}

if (!bidderAddress.equals(Address.wrap(sig.getAddress()))) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "incorrect key".getBytes());
ResultCode.FAILURE, nrg - COST, "incorrect key".getBytes());
}

// if this domain name does not have an callerAddress
Expand All @@ -263,17 +263,17 @@ public ExecutionResult execute(byte[] input, long nrg) {
// check if bidValue is valid (greater than 0)
if (bidValue.compareTo(new BigInteger("0")) < 0)
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "negative bid value".getBytes());
ResultCode.FAILURE, nrg - COST, "negative bid value".getBytes());

// check bidder addr and its balance
if (this.track.hasAccountState(bidderAddress)) {
if (this.track.getAccountState(bidderAddress).getBalance().compareTo(bidValue) < 0) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - COST, "insufficient balance".getBytes());
ResultCode.FAILURE, nrg - COST, "insufficient balance".getBytes());
}
} else
return new ExecutionResult(
ResultCode.INTERNAL_ERROR,
ResultCode.FAILURE,
nrg - COST,
"bidder account does not exist".getBytes());

Expand Down Expand Up @@ -341,7 +341,7 @@ private ExecutionResult extensionRequest(Address domainAddress, long nrg) {
if (expireDateFromStorage.getTime() < currentDate.getTime()
|| difference > ACTIVE_TIME.intValue()) {
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, COST - nrg, "already been extended".getBytes());
ResultCode.FAILURE, COST - nrg, "already been extended".getBytes());
}

// add the new expire date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public AionNameServiceContract(
public ExecutionResult execute(byte[] input, long nrg) {
// check for correct input length
if (input.length != 130 && input.length != 226)
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);

// declare variables for parsing the byte[] input and storing each value
byte[] addressFirstPart = new byte[16];
Expand Down Expand Up @@ -185,12 +185,12 @@ public ExecutionResult execute(byte[] input, long nrg) {
subdomainName = new String(trimmedSubdomainNameInBytes, "UTF-8");

if (!isValidDomainName(this.domainName)) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

domains.put(this.domainName, this.address);
} catch (UnsupportedEncodingException a) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
}

Expand All @@ -201,12 +201,12 @@ public ExecutionResult execute(byte[] input, long nrg) {

boolean b = ECKeyEd25519.verify(data, sig.getSignature(), sig.getPubkey(null));
if (!b) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// verify public key matches owner
if (!this.ownerAddress.equals(Address.wrap(sig.getAddress()))) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// operation: {1-setResolver, 2-setTTL, 3-transferOwnership, 4-transferSubdomainOwnership}
Expand Down Expand Up @@ -237,7 +237,7 @@ public ExecutionResult execute(byte[] input, long nrg) {
addressSecondPart,
subdomainName);
default:
return new ExecutionResult(ResultCode.INTERNAL_ERROR, nrg); // unsupported operation
return new ExecutionResult(ResultCode.FAILURE, nrg); // unsupported operation
}
}

Expand Down Expand Up @@ -275,7 +275,7 @@ private ExecutionResult transferOwnership(
if (nrg < TRANSFER_COST) return new ExecutionResult(ResultCode.OUT_OF_NRG, 0);

if (!isValidOwnerAddress(Address.wrap(combineTwoBytes(addr1, addr2))))
return new ExecutionResult(ResultCode.INTERNAL_ERROR, nrg);
return new ExecutionResult(ResultCode.FAILURE, nrg);

Address.wrap(combineTwoBytes(addr1, addr2));
storeResult(hash1, hash2, addr1, addr2);
Expand All @@ -299,7 +299,7 @@ private ExecutionResult transferSubdomainOwnership(
if (nrg < TRANSFER_COST) return new ExecutionResult(ResultCode.OUT_OF_NRG, 0);

if (!isValidOwnerAddress(Address.wrap(combineTwoBytes(addr1, addr2))))
return new ExecutionResult(ResultCode.INTERNAL_ERROR, nrg);
return new ExecutionResult(ResultCode.FAILURE, nrg);

Address sdAddress = Address.wrap(subdomainAddress);

Expand All @@ -308,7 +308,7 @@ private ExecutionResult transferSubdomainOwnership(
this.track.addStorageRow(sdAddress, new DataWord(hash2), new DataWord(addr2));
return new ExecutionResult(ResultCode.SUCCESS, nrg - TRANSFER_COST);
}
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public IExecutionResult execute(byte[] input, long nrgLimit) {
result[0] = verify ? (byte) 1 : (byte) 0;
return new ExecutionResult(ExecutionResult.ResultCode.SUCCESS, nrgLimit - COST, result);
} catch (Exception e) {
return new ExecutionResult(ExecutionResult.ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ExecutionResult.ResultCode.FAILURE, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ExecutionResult execute(byte[] input, long nrg) {
// check length
if (input.length < 1)
return new ExecutionResult(
ResultCode.INTERNAL_ERROR, nrg - DEFAULT_COST, "input too short".getBytes());
ResultCode.FAILURE, nrg - DEFAULT_COST, "input too short".getBytes());

byte[] hash = keccak256(input);
return new ExecutionResult(ResultCode.SUCCESS, nrg - DEFAULT_COST - additionalNRG, hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ public ExecutionResult execute(byte[] input, long nrg) {
return new ExecutionResult(ResultCode.OUT_OF_NRG, 0);
}
if (input == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (input.length < 1) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

int operation = input[0];
Expand All @@ -272,7 +272,7 @@ public ExecutionResult execute(byte[] input, long nrg) {
case 1:
return sendTransaction(input, nrg);
default:
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
}

Expand All @@ -285,7 +285,7 @@ public ExecutionResult execute(byte[] input, long nrg) {
*/
private ExecutionResult createWallet(byte[] input, long nrg) {
if (input.length < 1 + Long.BYTES) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

ByteBuffer thresh = ByteBuffer.allocate(Long.BYTES);
Expand All @@ -299,14 +299,14 @@ private ExecutionResult createWallet(byte[] input, long nrg) {
return new ExecutionResult(ResultCode.INVALID_NRG_LIMIT, nrg);
}
if (owners == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if ((owners.size() < MIN_OWNERS) || (owners.size() > MAX_OWNERS)) {
// sanity check... owners should be null in both these cases really
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if ((threshold < MIN_THRESH) || (threshold > owners.size())) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

Address wallet = initNewWallet(owners, threshold);
Expand All @@ -324,7 +324,7 @@ private ExecutionResult createWallet(byte[] input, long nrg) {
private ExecutionResult sendTransaction(byte[] input, long nrg) {
int length = input.length;
if (length > 1 + ADDR_LEN + (SIG_LEN * MAX_OWNERS) + AMOUNT_LEN + Long.BYTES + ADDR_LEN) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

int walletStart = 1;
Expand All @@ -335,7 +335,7 @@ private ExecutionResult sendTransaction(byte[] input, long nrg) {

// Ensures input is min expected size except possibly signatures, which is checked below.
if (sigsStart > amountStart) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

Address wallet = new Address(Arrays.copyOfRange(input, walletStart, sigsStart));
Expand All @@ -353,19 +353,19 @@ private ExecutionResult sendTransaction(byte[] input, long nrg) {
}
if (track.getStorageValue(wallet, new DataWord(getMetaDataKey())) == null) {
// Then wallet is not the address of a multi-sig wallet.
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (sigs == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

byte[] msg = constructMsg(wallet, track.getNonce(wallet), recipient, amount, nrgPrice);
if (amount.compareTo(BigInteger.ZERO) < 0) {
// Attempt to transfer negative amount.
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (!areValidSignatures(wallet, sigs, msg)) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (track.getBalance(wallet).compareTo(amount) < 0) {
// Attempt to transfer more than available balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ public TRSqueryContract(
@Override
public ExecutionResult execute(byte[] input, long nrgLimit) {
if (input == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (input.length == 0) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
if (nrgLimit < COST) {
return new ExecutionResult(ResultCode.OUT_OF_NRG, 0);
Expand All @@ -207,7 +207,7 @@ public ExecutionResult execute(byte[] input, long nrgLimit) {
case 5:
return availableForWithdrawalAt(input, nrgLimit);
default:
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}
}

Expand All @@ -232,7 +232,7 @@ private ExecutionResult isStarted(byte[] input, long nrgLimit) {
final int len = 33;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

byte[] result = new byte[1];
Expand Down Expand Up @@ -264,7 +264,7 @@ private ExecutionResult isLocked(byte[] input, long nrgLimit) {
final int len = 33;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

byte[] result = new byte[1];
Expand Down Expand Up @@ -297,7 +297,7 @@ private ExecutionResult isDirectDepositEnabled(byte[] input, long nrgLimit) {
final int len = 33;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

byte[] result = new byte[1];
Expand Down Expand Up @@ -337,7 +337,7 @@ private ExecutionResult period(byte[] input, long nrgLimit) {
final int len = 33;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// Grab the contract address and block number and determine the period.
Expand Down Expand Up @@ -375,7 +375,7 @@ private ExecutionResult periodAt(byte[] input, long nrgLimit) {
final int len = 41;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// Grab the contract address and block number and determine the period.
Expand All @@ -387,7 +387,7 @@ private ExecutionResult periodAt(byte[] input, long nrgLimit) {
long blockNum = blockBuf.getLong();

if (blockNum <= 0) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

return determinePeriod(contract, blockchain.getBlockByNumber(blockNum), nrgLimit);
Expand Down Expand Up @@ -420,13 +420,13 @@ private ExecutionResult availableForWithdrawalAt(byte[] input, long nrgLimit) {
final int len = 41;

if (input.length != len) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

Address contract = Address.wrap(Arrays.copyOfRange(input, indexContract, indexTimestamp));
byte[] specs = getContractSpecs(contract);
if (specs == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// If a contract has its funds open then the fraction is always 1.
Expand All @@ -440,7 +440,7 @@ private ExecutionResult availableForWithdrawalAt(byte[] input, long nrgLimit) {
// This operation is only well-defined when the contract has a start time. Thus the contract
// must be in the following state: live.
if (!isContractLive(contract)) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
Expand Down Expand Up @@ -500,7 +500,7 @@ private ExecutionResult determinePeriod(Address contract, IBlock block, long nrg

byte[] specs = getContractSpecs(contract);
if (specs == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

// If contract is not yet live we are in period 0.
Expand All @@ -511,7 +511,7 @@ private ExecutionResult determinePeriod(Address contract, IBlock block, long nrg

// Grab the timestamp of block number blockNum and calculate the period the contract is in.
if (block == null) {
return new ExecutionResult(ResultCode.INTERNAL_ERROR, 0);
return new ExecutionResult(ResultCode.FAILURE, 0);
}

long blockTime = block.getTimestamp();
Expand Down
Loading