Search in sources :

Example 26 with Transaction

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

the class StateReader method CheckWitness.

// private boolean CheckStorageContext(final StorageContext context) {
// final ContractState contract = Contracts.TryGet(context.ScriptHash);
// if (contract == null) {
// return false;
// }
// if (!contract.HasStorage) {
// return false;
// }
// return true;
// }
// public void Dispose()
// {
// foreach (IDisposable disposable in disposables)
// disposable.Dispose();
// disposables.Clear();
// }
protected boolean CheckWitness(final BlockDb blockDb, final ExecutionEngine engine, final UInt160 hash) {
    final Transaction container = (Transaction) engine.getScriptContainer();
    final UInt160[] _hashes_for_verifying = container.getScriptHashesForVerifying(blockDb);
    return Arrays.asList(_hashes_for_verifying).contains(hash);
}
Also used : Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160)

Example 27 with Transaction

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

the class BlockDbH2Impl method getTransactionWithHash.

@Override
public Transaction getTransactionWithHash(final UInt256 hash) {
    final JdbcTemplate t = new JdbcTemplate(ds);
    final String sql = getSql("getTransactionWithHash");
    final List<Map<String, Object>> dataList = t.queryForList(sql, hash.toByteArray());
    if (dataList.isEmpty()) {
        return null;
    }
    final Map<String, Object> data = dataList.get(0);
    final byte[] blockIndexBa = (byte[]) data.get("block_index");
    final byte[] transactionIndexBa = (byte[]) data.get(TRANSACTION_INDEX);
    final byte[] transactionBa = (byte[]) data.get("transaction");
    final int transactionIndex = getTransactionIndex(transactionIndexBa);
    final Transaction transaction = new Transaction(ByteBuffer.wrap(transactionBa));
    final Map<Integer, List<TransactionOutput>> outputsMap = getMapList(t, "getTransactionOutputsWithBlockAndTransactionIndex", new TransactionOutputMapToObject(), blockIndexBa, transactionIndexBa);
    transaction.outputs.addAll(outputsMap.get(transactionIndex));
    final Map<Integer, List<CoinReference>> inputsMap = getMapList(t, "getTransactionInputsWithBlockAndTransactionIndex", new CoinReferenceMapToObject(), blockIndexBa, transactionIndexBa);
    transaction.inputs.addAll(inputsMap.get(transactionIndex));
    final Map<Integer, List<Witness>> scriptsMap = getMapList(t, "getTransactionScriptsWithBlockAndTransactionIndex", new WitnessMapToObject(), blockIndexBa, transactionIndexBa);
    transaction.scripts.addAll(scriptsMap.get(transactionIndex));
    return transaction;
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 28 with Transaction

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

the class BlockDbH2Impl method getTransactionsForBlock.

/**
 * return the block, with transactions added.
 *
 * @param block
 *            the block, to add transactions to.
 */
private void getTransactionsForBlock(final Block block) {
    final JdbcTemplate t = new JdbcTemplate(ds);
    final String sql = getSql("getTransactionsWithIndex");
    final byte[] blockIndexBa = block.index.toByteArray();
    final List<byte[]> dataList = t.queryForList(sql, byte[].class, blockIndexBa);
    for (final byte[] data : dataList) {
        final Transaction transaction = new Transaction(ByteBuffer.wrap(data));
        block.getTransactionList().add(transaction);
    }
    getTransactionOutputsWithIndex(block, t, blockIndexBa);
    getTransactionInputsWithIndex(block, t, blockIndexBa);
    getTransactionScriptsWithIndex(block, t, blockIndexBa);
}
Also used : Transaction(neo.model.core.Transaction) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 29 with Transaction

use of neo.model.core.Transaction 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 30 with Transaction

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

Transaction (neo.model.core.Transaction)49 JSONObject (org.json.JSONObject)26 Block (neo.model.core.Block)25 JSONArray (org.json.JSONArray)19 TransactionOutput (neo.model.core.TransactionOutput)17 Test (org.junit.Test)14 UInt256 (neo.model.bytes.UInt256)13 TreeMap (java.util.TreeMap)12 BlockDb (neo.model.db.BlockDb)9 Map (java.util.Map)8 Fixed8 (neo.model.bytes.Fixed8)8 UInt160 (neo.model.bytes.UInt160)8 CoinReference (neo.model.core.CoinReference)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 LocalNodeData (neo.network.model.LocalNodeData)5 Timestamp (java.sql.Timestamp)4 UInt16 (neo.model.bytes.UInt16)4 List (java.util.List)3 TreeSet (java.util.TreeSet)3