Search in sources :

Example 11 with TransactionOutput

use of neo.model.core.TransactionOutput 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 12 with TransactionOutput

use of neo.model.core.TransactionOutput 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 13 with TransactionOutput

use of neo.model.core.TransactionOutput 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)

Example 14 with TransactionOutput

use of neo.model.core.TransactionOutput in project neo-java by coranos.

the class TestMinTx method test001Remark.

@Test
public void test001Remark() {
    // final String rpcNode = "http://seed2.neo.org:20332";//
    // CityOfZionUtil.getTestNetRpcNode();
    final String rpcNode = CityOfZionUtil.getTestNetRpcNode();
    LOG.info("test001Remark blockCount:{}:", RpcClientUtil.getBlockCount(1000, rpcNode, false));
    final byte[] txBa = new byte[800];
    txBa[0] = TransactionType.CONTRACT_TRANSACTION.getTypeByte();
    txBa[2] = 1;
    txBa[3] = TransactionAttributeUsage.REMARK_00.getTypeByte();
    txBa[4] = 4;
    final Transaction tx = new Transaction(ByteBuffer.wrap(txBa));
    tx.outputs.add(new TransactionOutput(ModelUtil.NEO_HASH, ModelUtil.getFixed8(BigInteger.ONE), ModelUtil.addressToScriptHash("AeKd54zJdgqXy41NgH1PicXTVcz3RdRFdh")));
    tx.inputs.add(new CoinReference(ModelUtil.getUInt256(ByteBuffer.wrap(Hex.decode("24ef2db3a509cd065c85ae33b5b905f30699d69237631598c5f182076619acc8"))), new UInt16(1)));
    LOG.info("test001Remark tx:{}:", tx.toJSONObject().toString(2));
    final JSONArray paramsJson = new JSONArray();
    paramsJson.put(Hex.encode(tx.toByteArray()));
    final JSONObject inputJson = new JSONObject();
    inputJson.put("jsonrpc", "2.0");
    inputJson.put("method", "sendrawtransaction");
    inputJson.put("params", paramsJson);
    inputJson.put("id", 1);
    final JSONObject outputJson = RpcClientUtil.post(1000, rpcNode, false, inputJson);
    Assert.assertNotNull("outputJson acnnot be null", outputJson);
    LOG.info("test001Remark outputJson:{}:", outputJson.toString(2));
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UInt16(neo.model.bytes.UInt16) Test(org.junit.Test)

Example 15 with TransactionOutput

use of neo.model.core.TransactionOutput 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)

Aggregations

TransactionOutput (neo.model.core.TransactionOutput)25 Transaction (neo.model.core.Transaction)17 JSONObject (org.json.JSONObject)16 UInt256 (neo.model.bytes.UInt256)15 TreeMap (java.util.TreeMap)11 Fixed8 (neo.model.bytes.Fixed8)11 CoinReference (neo.model.core.CoinReference)10 JSONArray (org.json.JSONArray)10 UInt160 (neo.model.bytes.UInt160)9 Block (neo.model.core.Block)9 Map (java.util.Map)8 Test (org.junit.Test)6 BlockDb (neo.model.db.BlockDb)5 UInt16 (neo.model.bytes.UInt16)4 Timestamp (java.sql.Timestamp)3 LocalNodeData (neo.network.model.LocalNodeData)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2