Search in sources :

Example 16 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class CoinReferenceMapToObject method toObject.

@Override
public CoinReference toObject(final Map<String, Object> map) {
    final byte[] prevHashBa = getBytes(map, "prev_transaction_hash");
    final byte[] prevIndexBa = getBytes(map, "prev_transaction_output_index");
    final UInt256 prevHash = new UInt256(ByteBuffer.wrap(prevHashBa));
    final UInt16 prevIndex = new UInt16(prevIndexBa);
    final CoinReference coinReference = new CoinReference(prevHash, prevIndex);
    return coinReference;
}
Also used : CoinReference(neo.model.core.CoinReference) UInt16(neo.model.bytes.UInt16) UInt256(neo.model.bytes.UInt256)

Example 17 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class TransactionOutputMapToObject method toObject.

@Override
public TransactionOutput toObject(final Map<String, Object> map) {
    final byte[] assetIdBa = getBytes(map, "asset_id");
    final byte[] valueBa = getBytes(map, "value");
    ArrayUtils.reverse(valueBa);
    final byte[] scriptHashBa = getBytes(map, "script_hash");
    final UInt256 assetId = new UInt256(ByteBuffer.wrap(assetIdBa));
    final Fixed8 value = new Fixed8(ByteBuffer.wrap(valueBa));
    final UInt160 scriptHash = new UInt160(scriptHashBa);
    final TransactionOutput transactionOutput = new TransactionOutput(assetId, value, scriptHash);
    return transactionOutput;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Fixed8(neo.model.bytes.Fixed8) UInt160(neo.model.bytes.UInt160) UInt256(neo.model.bytes.UInt256)

Example 18 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class BlockImportExportUtil method getNetworkFee.

/**
 * return the network fee.
 *
 * @param blockDb
 *            the block database.
 * @param tx
 *            the transaction.
 * @param systemFee
 *            the system fee.
 * @return the network fee.
 */
private static Fixed8 getNetworkFee(final BlockDb blockDb, final Transaction tx, final Fixed8 systemFee) {
    switch(tx.type) {
        case MINER_TRANSACTION:
        case CLAIM_TRANSACTION:
        case ENROLLMENT_TRANSACTION:
        case ISSUE_TRANSACTION:
        case REGISTER_TRANSACTION:
            LOG.trace("txType:{}; No Network Fee", tx.type);
            return ModelUtil.FIXED8_ZERO;
        default:
    }
    Fixed8 totalInput = ModelUtil.FIXED8_ZERO;
    for (final CoinReference cr : tx.inputs) {
        final UInt256 prevHashReversed = cr.prevHash.reverse();
        final Transaction tiTx = blockDb.getTransactionWithHash(prevHashReversed);
        final int prevIndex = cr.prevIndex.asInt();
        final TransactionOutput txOut = tiTx.outputs.get(prevIndex);
        if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
            totalInput = ModelUtil.add(totalInput, txOut.value);
        }
    }
    Fixed8 totalOutput = ModelUtil.FIXED8_ZERO;
    for (final TransactionOutput txOut : tx.outputs) {
        if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
            totalOutput = ModelUtil.add(totalOutput, txOut.value);
        }
    }
    if (totalInput.equals(ModelUtil.FIXED8_ZERO) && totalOutput.equals(ModelUtil.FIXED8_ZERO) && systemFee.equals(ModelUtil.FIXED8_ZERO)) {
        LOG.trace("txType:{}; Inout,Output, and System fees are all zero, No Network Fee", tx.type);
        return ModelUtil.FIXED8_ZERO;
    }
    final Fixed8 totalFee;
    try {
        totalFee = ModelUtil.subtract(totalOutput, totalInput);
    } catch (final RuntimeException e) {
        LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, tx.getHash());
        throw new RuntimeException("error calculating totalFee", e);
    }
    final Fixed8 networkFee;
    ;
    try {
        networkFee = ModelUtil.subtract(systemFee, totalFee);
    } catch (final RuntimeException e) {
        LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; totalFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, totalFee, tx.getHash());
        throw new RuntimeException("error calculating networkFee", e);
    }
    return networkFee;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) Fixed8(neo.model.bytes.Fixed8) UInt256(neo.model.bytes.UInt256)

Example 19 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class AbstractJsonMockBlockDb method getAccountAssetValueMap.

@Override
public final Map<UInt160, Map<UInt256, Fixed8>> getAccountAssetValueMap() {
    final Map<UInt160, Map<UInt256, Fixed8>> accountAssetValueMap = new TreeMap<>();
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (final Transaction transaction : block.getTransactionList()) {
            for (final TransactionOutput output : transaction.outputs) {
                if (!accountAssetValueMap.containsKey(output.scriptHash)) {
                    accountAssetValueMap.put(output.scriptHash, new TreeMap<>());
                }
                final Map<UInt256, Fixed8> assetValueMap = accountAssetValueMap.get(output.scriptHash);
                final Fixed8 value = output.value;
                if (assetValueMap.containsKey(output.assetId)) {
                    final Fixed8 oldValue = assetValueMap.get(output.assetId);
                    final Fixed8 newValue = ModelUtil.add(value, oldValue);
                    assetValueMap.put(output.assetId, newValue);
                } else {
                    assetValueMap.put(output.assetId, value);
                }
            }
        }
    }
    return accountAssetValueMap;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) Block(neo.model.core.Block) TreeMap(java.util.TreeMap) Map(java.util.Map) UInt256(neo.model.bytes.UInt256)

Example 20 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class AbstractJsonMockBlockDb method getUnspentTransactionOutputListMap.

@Override
public Map<UInt256, Map<TransactionOutput, CoinReference>> getUnspentTransactionOutputListMap(final UInt160 account) {
    final Map<UInt256, Map<TransactionOutput, CoinReference>> assetIdTxoMap = new TreeMap<>();
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (int txIx = 0; txIx < block.getTransactionList().size(); txIx++) {
            final Transaction transaction = block.getTransactionList().get(txIx);
            for (final TransactionOutput output : transaction.outputs) {
                if (output.scriptHash.equals(account)) {
                    if (!assetIdTxoMap.containsKey(output.assetId)) {
                        assetIdTxoMap.put(output.assetId, new TreeMap<>());
                    }
                    final CoinReference cr = new CoinReference(transaction.getHash(), new UInt16(txIx));
                    assetIdTxoMap.get(output.assetId).put(output, cr);
                }
            }
        }
    }
    return assetIdTxoMap;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) UInt16(neo.model.bytes.UInt16) TreeMap(java.util.TreeMap) Map(java.util.Map) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt256 (neo.model.bytes.UInt256)36 TransactionOutput (neo.model.core.TransactionOutput)15 JSONObject (org.json.JSONObject)14 TreeMap (java.util.TreeMap)13 Fixed8 (neo.model.bytes.Fixed8)13 Transaction (neo.model.core.Transaction)13 Map (java.util.Map)11 UInt160 (neo.model.bytes.UInt160)11 CoinReference (neo.model.core.CoinReference)11 Block (neo.model.core.Block)8 JSONArray (org.json.JSONArray)7 UInt16 (neo.model.bytes.UInt16)5 BlockDb (neo.model.db.BlockDb)5 ArrayList (java.util.ArrayList)3 Header (neo.model.core.Header)3 BTreeMap (org.mapdb.BTreeMap)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvPayload (neo.model.network.InvPayload)2 Color (java.awt.Color)1