Search in sources :

Example 1 with PrecompiledDataWord

use of org.aion.precompiled.type.PrecompiledDataWord 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)

Example 2 with PrecompiledDataWord

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

the class BridgeStorageConnector method setBundle.

/**
 * @implNote ATB-4 changes, we have a new requirement in the contract to store the value
 *     (transactionHash) of when the bundle was set into the block.
 *     <p>Therefore, where previously we were checking whether the bundle was valid based on a
 *     {@code true/false} assumption, we now check whether the bundle is valid based on whether
 *     the returned address equates to a zero word.
 *     <p>Documentation on the change can be found as part of v0.0.4 changes.
 */
public void setBundle(@Nonnull final byte[] key, @Nonnull final byte[] value) {
    assert key.length == 32;
    assert value.length == 32;
    byte[] h = ByteUtil.chop(CapabilitiesProvider.getExternalCapabilities().blake2b(ByteUtil.merge(M_ID.BUNDLE_MAP.id, key)));
    PrecompiledDataWord hWord = PrecompiledDataWord.fromBytes(h);
    this.setDWORD(hWord, value);
}
Also used : PrecompiledDataWord(org.aion.precompiled.type.PrecompiledDataWord) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord)

Example 3 with PrecompiledDataWord

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

the class BridgeStorageConnector method setActiveMember.

// TODO: this can be optimized
public void setActiveMember(@Nonnull final byte[] key, final boolean value) {
    assert key.length == 32;
    byte[] h = ByteUtil.chop(CapabilitiesProvider.getExternalCapabilities().blake2b(ByteUtil.merge(M_ID.ACTIVE_MAP.id, key)));
    PrecompiledDataWord hWord = PrecompiledDataWord.fromBytes(h);
    PrecompiledDataWord b = value ? PrecompiledDataWord.fromInt(1) : PrecompiledDataWord.fromInt(0);
    this.setWORD(hWord, b);
}
Also used : PrecompiledDataWord(org.aion.precompiled.type.PrecompiledDataWord) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord)

Example 4 with PrecompiledDataWord

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

the class BridgeStorageConnector method getActiveMember.

public boolean getActiveMember(@Nonnull final byte[] key) {
    assert key.length == 32;
    byte[] h = ByteUtil.chop(CapabilitiesProvider.getExternalCapabilities().blake2b(ByteUtil.merge(M_ID.ACTIVE_MAP.id, key)));
    PrecompiledDataWord hWord = PrecompiledDataWord.fromBytes(h);
    // C1 covered by getWORD
    byte[] activeMemberWord = this.getWORD(hWord);
    return activeMemberWord != null && (activeMemberWord[15] & 0x01) == 1;
}
Also used : PrecompiledDataWord(org.aion.precompiled.type.PrecompiledDataWord) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord)

Example 5 with PrecompiledDataWord

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

the class BridgeStorageConnector method setRingLocked.

// TODO: this can be optimized
public void setRingLocked(boolean value) {
    PrecompiledDataWord lockedDw = value ? PrecompiledDataWord.fromInt(1) : PrecompiledDataWord.fromInt(0);
    this.setWORD(S_OFFSET.RING_LOCKED.offset, lockedDw);
}
Also used : PrecompiledDataWord(org.aion.precompiled.type.PrecompiledDataWord) IPrecompiledDataWord(org.aion.precompiled.type.IPrecompiledDataWord)

Aggregations

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