Search in sources :

Example 6 with Fixed8

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

the class ModelUtil method subtract.

/**
 * subtracts two Fixed8 values.
 *
 * @param left
 *            the left value
 * @param right
 *            the right value.
 * @return left minus right
 */
public static Fixed8 subtract(final Fixed8 left, final Fixed8 right) {
    final BigInteger leftBi = left.toPositiveBigInteger();
    final BigInteger rightBi = right.toPositiveBigInteger();
    final BigInteger newBi = rightBi.subtract(leftBi);
    if (newBi.signum() < 0) {
        throw new RuntimeException("tried to subtract " + leftBi + "(Fixed8:" + left + ")  from " + rightBi + " (Fixed8:" + right + ")" + " cannot have a negative fixed8 with value " + newBi + ".");
    }
    final Fixed8 newValue = getFixed8(newBi);
    return newValue;
}
Also used : Fixed8(neo.model.bytes.Fixed8) BigInteger(java.math.BigInteger)

Example 7 with Fixed8

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

the class BlockDbH2Impl method getAccountAssetValueMap.

@Override
public Map<UInt160, Map<UInt256, Fixed8>> getAccountAssetValueMap() {
    final JdbcTemplate jdbcOperations = new JdbcTemplate(ds);
    final String sql = getSql("getAccountAssetValueMap");
    final List<Map<String, Object>> mapList = jdbcOperations.queryForList(sql);
    final Map<UInt160, Map<UInt256, Fixed8>> accountAssetValueMap = new TreeMap<>();
    final TransactionOutputMapToObject mapToObject = new TransactionOutputMapToObject();
    for (final Map<String, Object> map : mapList) {
        final TransactionOutput output = mapToObject.toObject(map);
        if (!accountAssetValueMap.containsKey(output.scriptHash)) {
            accountAssetValueMap.put(output.scriptHash, new TreeMap<>());
        }
        final Map<UInt256, Fixed8> assetValueMap = accountAssetValueMap.get(output.scriptHash);
        assetValueMap.put(output.assetId, output.value);
    }
    return accountAssetValueMap;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TreeMap(java.util.TreeMap) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) JSONObject(org.json.JSONObject) Map(java.util.Map) TreeMap(java.util.TreeMap) UInt256(neo.model.bytes.UInt256)

Example 8 with Fixed8

use of neo.model.bytes.Fixed8 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 9 with Fixed8

use of neo.model.bytes.Fixed8 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 10 with Fixed8

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

the class BlockImportExportUtil method exportBlocks.

/**
 * exports the blocks to the file.
 *
 * @param controller
 *            the controller.
 */
public static void exportBlocks(final LocalControllerNode controller) {
    final LocalNodeData localNodeData = controller.getLocalNodeData();
    final BlockDb blockDb = localNodeData.getBlockDb();
    try (OutputStream statsFileOut = new FileOutputStream(localNodeData.getChainExportStatsFileName());
        PrintWriter statsWriter = new PrintWriter(statsFileOut, true)) {
        statsWriter.println(OPEN_BRACKET);
        try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(localNodeData.getChainExportDataFileName()), 1024 * 1024 * 32))) {
            final long maxIndex = blockDb.getHeaderOfBlockWithMaxIndex().getIndexAsLong();
            final byte[] maxIndexBa = new UInt32(maxIndex + 1).toByteArray();
            out.write(maxIndexBa);
            if (LOG.isTraceEnabled()) {
                LOG.trace("export maxIndexBa aswritten {}", Hex.encode(maxIndexBa));
            }
            long startMs = -1;
            long interimBlocks = 0;
            long interimBytes = 0;
            final long[] interimTx = new long[TransactionType.values().length];
            final long[] interimTxNetworkFees = new long[TransactionType.values().length];
            long totalTx = 0;
            final Map<String, Long> numBlocksByTxCountMap = new TreeMap<>();
            @SuppressWarnings("unchecked") final Set<UInt160>[] activeAccountSet = new Set[TransactionType.values().length];
            for (int txOrdinal = 0; txOrdinal < activeAccountSet.length; txOrdinal++) {
                activeAccountSet[txOrdinal] = new TreeSet<>();
            }
            long procStartMs = System.currentTimeMillis();
            for (long blockIx = 0; blockIx <= maxIndex; blockIx++) {
                LOG.debug("STARTED export {} of {} ", blockIx, maxIndex);
                final Block block = localNodeData.getBlockDb().getFullBlockFromHeight(blockIx);
                final byte[] ba = block.toByteArray();
                final int length = Integer.reverseBytes(ba.length);
                out.writeInt(length);
                out.write(ba);
                LOG.debug("SUCCESS export {} of {} length {}", blockIx, maxIndex, ba.length);
                final Timestamp blockTs = block.getTimestamp();
                interimBlocks++;
                interimBytes += ba.length;
                for (final Transaction tx : block.getTransactionList()) {
                    interimTx[tx.type.ordinal()]++;
                    final Fixed8 systemFee = localNodeData.getTransactionSystemFeeMap().get(tx.type);
                    interimTxNetworkFees[tx.type.ordinal()] += getNetworkFee(blockDb, tx, systemFee).value;
                    totalTx++;
                    for (final TransactionOutput txOut : tx.outputs) {
                        activeAccountSet[tx.type.ordinal()].add(txOut.scriptHash);
                    }
                }
                MapUtil.increment(numBlocksByTxCountMap, String.valueOf(block.getTransactionList().size()));
                if (startMs < 0) {
                    startMs = blockTs.getTime();
                }
                final long ms = blockTs.getTime() - startMs;
                if (ms > (86400 * 1000)) {
                    out.flush();
                    final Block maxBlockHeader = blockDb.getHeaderOfBlockWithMaxIndex();
                    final JSONObject stats = getStats(blockDb, interimBlocks, interimBytes, interimTx, activeAccountSet, procStartMs, blockTs, interimTxNetworkFees, numBlocksByTxCountMap);
                    if (blockIx > 0) {
                        statsWriter.println(COMMA);
                    }
                    statsWriter.println(stats);
                    LOG.info("INTERIM export {} of {}, bx {}, tx {} json {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), INTEGER_FORMAT.format(maxBlockHeader.getIndexAsLong()), INTEGER_FORMAT.format(totalTx), stats);
                    startMs = blockTs.getTime();
                    for (int ix = 0; ix < interimTx.length; ix++) {
                        interimTx[ix] = 0;
                        interimTxNetworkFees[ix] = 0;
                    }
                    interimBlocks = 0;
                    interimBytes = 0;
                    for (int txOrdinal = 0; txOrdinal < activeAccountSet.length; txOrdinal++) {
                        activeAccountSet[txOrdinal].clear();
                    }
                    numBlocksByTxCountMap.clear();
                    procStartMs = System.currentTimeMillis();
                }
            }
            out.flush();
        } catch (final IOException e) {
            throw new RuntimeException(e);
        } finally {
            statsWriter.println(CLOSE_BRACKET);
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) TransactionOutput(neo.model.core.TransactionOutput) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Timestamp(java.sql.Timestamp) Fixed8(neo.model.bytes.Fixed8) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter) LocalNodeData(neo.network.model.LocalNodeData) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) FileOutputStream(java.io.FileOutputStream) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) UInt32(neo.model.bytes.UInt32)

Aggregations

Fixed8 (neo.model.bytes.Fixed8)20 UInt256 (neo.model.bytes.UInt256)13 TransactionOutput (neo.model.core.TransactionOutput)11 TreeMap (java.util.TreeMap)10 UInt160 (neo.model.bytes.UInt160)10 JSONObject (org.json.JSONObject)10 Transaction (neo.model.core.Transaction)8 Map (java.util.Map)6 BlockDb (neo.model.db.BlockDb)6 Block (neo.model.core.Block)5 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3 CoinReference (neo.model.core.CoinReference)3 LocalNodeData (neo.network.model.LocalNodeData)3 JSONArray (org.json.JSONArray)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2