Search in sources :

Example 1 with IPrecompiledDataWord

use of org.aion.precompiled.type.IPrecompiledDataWord in project aion by aionnetwork.

the class TotalCurrencyContract method executeUpdateTotalBalance.

private PrecompiledTransactionResult executeUpdateTotalBalance(byte[] input, long nrg) {
    // update total portion
    if (nrg < COST) {
        return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("OUT_OF_NRG"), 0);
    }
    if (input.length < 114) {
        return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("FAILURE"), 0);
    }
    // process input data
    int offset = 0;
    PrecompiledDataWord chainId = PrecompiledDataWord.fromInt(input[0]);
    offset++;
    byte signum = input[1];
    offset++;
    byte[] amount = new byte[16];
    byte[] sign = new byte[96];
    System.arraycopy(input, offset, amount, 0, 16);
    offset += 16;
    System.arraycopy(input, offset, sign, 0, 96);
    IExternalCapabilitiesForPrecompiled capabilities = CapabilitiesProvider.getExternalCapabilities();
    byte[] payload = new byte[18];
    System.arraycopy(input, 0, payload, 0, 18);
    boolean b = capabilities.verifyEd25519Signature(payload, sign);
    if (!b) {
        return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("FAILURE"), 0);
    }
    // verify public key matches owner
    if (!this.ownerAddress.equals(new AionAddress(capabilities.getEd25519Address(sign)))) {
        return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("FAILURE"), 0);
    }
    // payload processing
    IPrecompiledDataWord totalCurr = this.externalState.getStorageValue(this.address, chainId);
    BigInteger totalCurrBI = totalCurr == null ? BigInteger.ZERO : BIUtil.toBI(totalCurr.copyOfData());
    BigInteger value = BIUtil.toBI(amount);
    if (signum != 0x0 && signum != 0x1) {
        return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("FAILURE"), 0);
    }
    BigInteger finalValue;
    if (signum == 0x0) {
        // addition
        finalValue = totalCurrBI.add(value);
    } else {
        // subtraction
        if (value.compareTo(totalCurrBI) > 0) {
            return new PrecompiledTransactionResult(TransactionStatus.nonRevertedFailure("FAILURE"), 0);
        }
        finalValue = totalCurrBI.subtract(value);
    }
    // store result and successful exit
    this.externalState.addStorageValue(this.address, chainId, PrecompiledDataWord.fromBytes(finalValue.toByteArray()));
    return new PrecompiledTransactionResult(TransactionStatus.successful(), nrg - COST);
}
Also used : IExternalCapabilitiesForPrecompiled(org.aion.precompiled.type.IExternalCapabilitiesForPrecompiled) PrecompiledTransactionResult(org.aion.precompiled.PrecompiledTransactionResult) AionAddress(org.aion.types.AionAddress) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord) BigInteger(java.math.BigInteger) PrecompiledDataWord(org.aion.precompiled.type.PrecompiledDataWord) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord)

Aggregations

BigInteger (java.math.BigInteger)1 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)1 IExternalCapabilitiesForPrecompiled (org.aion.precompiled.type.IExternalCapabilitiesForPrecompiled)1 IPrecompiledDataWord (org.aion.precompiled.type.IPrecompiledDataWord)1 PrecompiledDataWord (org.aion.precompiled.type.PrecompiledDataWord)1 AionAddress (org.aion.types.AionAddress)1