Search in sources :

Example 26 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ApiAion method estimateNrg.

// TODO: refactor these ad-hoc transaction creations - violates DRY and is messy
public long estimateNrg(ArgTxCall _params) {
    if (_params == null) {
        throw new NullPointerException();
    }
    Address from = _params.getFrom();
    if (from.equals(Address.EMPTY_ADDRESS())) {
        LOG.error("<send-transaction msg=invalid-from-address>");
        return -1L;
    }
    ECKey key = this.getAccountKey(from.toString());
    if (key == null) {
        LOG.error("<send-transaction msg=account-not-found>");
        return -1L;
    }
    try {
        // Transaction is executed as local transaction, no need to retrieve the real nonce.
        byte[] nonce = BigInteger.ZERO.toByteArray();
        AionTransaction tx = new AionTransaction(nonce, _params.getTo(), _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice());
        tx.sign(key);
        return this.ac.estimateTxNrg(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
    } catch (Exception ex) {
        return -1L;
    }
}
Also used : Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction)

Example 27 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ApiAion method createContract.

public ContractCreateResult createContract(ArgTxCall _params) {
    Address from = _params.getFrom();
    if (from == null || from.equals(Address.EMPTY_ADDRESS())) {
        return null;
    }
    ECKey key = this.getAccountKey(from.toString());
    if (key == null) {
        LOG.debug("ApiAion.createContract - null key");
        return null;
    } else {
        try {
            synchronized (pendingState) {
                byte[] nonce = !(_params.getNonce().equals(BigInteger.ZERO)) ? _params.getNonce().toByteArray() : pendingState.bestNonce(Address.wrap(key.getAddress())).toByteArray();
                AionTransaction tx = new AionTransaction(nonce, from, null, _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice());
                tx.sign(key);
                pendingState.addPendingTransaction(tx);
                ContractCreateResult c = new ContractCreateResult();
                c.address = tx.getContractAddress();
                c.transId = tx.getHash();
                return c;
            }
        } catch (Exception ex) {
            LOG.error("ApiAion.createContract - exception: [{}]", ex.getMessage());
            return null;
        }
    }
}
Also used : Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction)

Example 28 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ApiWeb3Aion method eth_getTransactionCount.

public Object eth_getTransactionCount(String _address, Object _bnOrId) {
    Address address = new Address(_address);
    String bnOrId = "latest";
    if (_bnOrId != null && !_bnOrId.equals(null))
        bnOrId = _bnOrId + "";
    return TypeConverter.toJsonHex(getRepoByJsonBlockId(bnOrId).getNonce(address));
}
Also used : Address(org.aion.base.type.Address) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString)

Example 29 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ApiWeb3Aion method eth_getBalance.

public Object eth_getBalance(String _address, Object _bnOrId) throws Exception {
    Address address = new Address(_address);
    String bnOrId = "latest";
    if (_bnOrId != null && !_bnOrId.equals(null))
        bnOrId = _bnOrId + "";
    BigInteger balance = getRepoByJsonBlockId(bnOrId).getBalance(address);
    return TypeConverter.toJsonHex(balance);
}
Also used : Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString)

Example 30 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class A0BlockHeader method fromRLP.

/**
 * Construct a block header from RLP
 *
 * @param rlpHeader
 * @param isUnsafe
 * @return
 */
public static A0BlockHeader fromRLP(RLPList rlpHeader, boolean isUnsafe) throws Exception {
    Builder builder = new Builder();
    if (isUnsafe) {
        builder.fromUnsafeSource();
    }
    builder.withParentHash(rlpHeader.get(RPL_BH_PARENTHASH).getRLPData());
    builder.withCoinbase(new Address(rlpHeader.get(RPL_BH_COINBASE).getRLPData()));
    builder.withStateRoot(rlpHeader.get(RPL_BH_STATEROOT).getRLPData());
    byte[] txTrieRoot = rlpHeader.get(RPL_BH_TXTRIE).getRLPData();
    if (txTrieRoot != null) {
        builder.withTxTrieRoot(txTrieRoot);
    }
    byte[] receiptTrieRoot = rlpHeader.get(RPL_BH_RECEIPTTRIE).getRLPData();
    if (receiptTrieRoot != null) {
        builder.withReceiptTrieRoot(receiptTrieRoot);
    }
    builder.withLogsBloom(rlpHeader.get(RPL_BH_LOGSBLOOM).getRLPData());
    builder.withDifficulty(rlpHeader.get(RPL_BH_DIFFICULTY).getRLPData());
    byte[] nrBytes = rlpHeader.get(RPL_BH_NUMBER).getRLPData();
    byte[] tsBytes = rlpHeader.get(RPL_BH_TIMESTAMP).getRLPData();
    if (nrBytes != null) {
        builder.withNumber(nrBytes);
    }
    if (tsBytes != null) {
        builder.withTimestamp(tsBytes);
    }
    builder.withExtraData(rlpHeader.get(RPL_BH_EXTRADATA).getRLPData());
    builder.withNonce(rlpHeader.get(RPL_BH_NONCE).getRLPData());
    builder.withSolution(rlpHeader.get(RPL_BH_SOLUTION).getRLPData());
    byte[] energyConsumedBytes = rlpHeader.get(RPL_BH_NRG_CONSUMED).getRLPData();
    byte[] energyLimitBytes = rlpHeader.get(RPL_BH_NRG_LIMIT).getRLPData();
    if (energyConsumedBytes != null) {
        builder.withEnergyConsumed(energyConsumedBytes);
    }
    if (energyLimitBytes != null) {
        builder.withEnergyLimit(energyLimitBytes);
    }
    return builder.build();
}
Also used : Address(org.aion.base.type.Address)

Aggregations

Address (org.aion.base.type.Address)61 Test (org.junit.Test)34 BigInteger (java.math.BigInteger)29 AionTransaction (org.aion.zero.types.AionTransaction)23 ITransaction (org.aion.base.type.ITransaction)14 ECKey (org.aion.crypto.ECKey)13 DataWord (org.aion.mcf.vm.types.DataWord)11 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)9 IRepositoryCache (org.aion.base.db.IRepositoryCache)8 HashMap (java.util.HashMap)6 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)5 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)5 AccountState (org.aion.mcf.core.AccountState)5 AionBlock (org.aion.zero.impl.types.AionBlock)5 ArrayList (java.util.ArrayList)4 ImportResult (org.aion.mcf.core.ImportResult)4 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepository (org.aion.base.db.IRepository)3 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)3