Search in sources :

Example 21 with Transaction

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

the class BlockDbH2Impl method getTransactionWithAccountList.

@Override
public List<Transaction> getTransactionWithAccountList(final UInt160 account) {
    final JdbcTemplate t = new JdbcTemplate(ds);
    final String sql = getSql("getTransactionWithAccountList");
    final byte[] accountBa = account.toByteArray();
    final List<byte[]> dataList = t.queryForList(sql, byte[].class, accountBa);
    final List<Transaction> transactionList = new ArrayList<>();
    for (final byte[] data : dataList) {
        final Transaction transaction = new Transaction(ByteBuffer.wrap(data));
        transactionList.add(transaction);
    }
    return transactionList;
}
Also used : Transaction(neo.model.core.Transaction) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 22 with Transaction

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

the class BlockDbMapDbImpl method getTransactionWithHash.

@Override
public Transaction getTransactionWithHash(final UInt256 hash) {
    final BTreeMap<byte[], byte[]> map = getTransactionKeyByTransactionHashMap();
    final byte[] hashBa = hash.toByteArray();
    if (!map.containsKey(hashBa)) {
        return null;
    }
    final byte[] txKey = map.get(hashBa);
    final BTreeMap<byte[], byte[]> txMap = getTransactionsByKeyMap();
    final byte[] data = txMap.get(txKey);
    final Transaction transaction = new Transaction(ByteBuffer.wrap(data));
    transaction.recalculateHash();
    return transaction;
}
Also used : Transaction(neo.model.core.Transaction)

Example 23 with Transaction

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

the class BlockDbMapDbImpl method getTransactionsForBlock.

/**
 * return the block, with transactions added.
 *
 * @param block
 *            the block, to add transactions to.
 */
private void getTransactionsForBlock(final Block block) {
    final long blockIndex = block.getIndexAsLong();
    final BTreeMap<Long, byte[]> txKeyListMap = getByteArrayByBlockIndexMap(TRANSACTION_KEYS_BY_BLOCK_INDEX);
    final List<byte[]> txKeyBaList = getByteArrayList(txKeyListMap, blockIndex);
    final BTreeMap<byte[], byte[]> txMap = getTransactionsByKeyMap();
    for (final byte[] txKey : txKeyBaList) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("getTransactionsForBlock {} txKey:{}", blockIndex, ModelUtil.toHexString(txKey));
        }
        final byte[] data = txMap.get(txKey);
        final Transaction transaction = new Transaction(ByteBuffer.wrap(data));
        transaction.recalculateHash();
        block.getTransactionList().add(transaction);
    }
}
Also used : Transaction(neo.model.core.Transaction)

Example 24 with Transaction

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

the class BlockDbMapDbImpl method validate.

@Override
public void validate() {
    LOG.info("STARTED validate");
    try {
        final Block block0 = getBlock(0, false);
        if (!block0.hash.equals(GenesisBlockUtil.GENESIS_HASH)) {
            throw new RuntimeException("height 0 block hash \"" + block0.hash.toHexString() + "\" does not match genesis block hash \"" + GenesisBlockUtil.GENESIS_HASH.toHexString() + "\".");
        }
        long lastInfoMs = System.currentTimeMillis();
        long blockHeight = 0;
        long lastGoodBlockIndex = -1;
        final long maxBlockCount = getBlockCount();
        boolean blockHeightNoLongerValid = false;
        final String maxBlockCountStr;
        if (LOG.isDebugEnabled() || LOG.isErrorEnabled()) {
            maxBlockCountStr = NumberFormat.getIntegerInstance().format(maxBlockCount);
        } else {
            maxBlockCountStr = null;
        }
        LOG.info("INTERIM validate, clear account list STARTED");
        final BTreeMap<byte[], byte[]> assetAndValueByAccountMap = getAssetAndValueByAccountMap();
        assetAndValueByAccountMap.clear();
        LOG.info("INTERIM validate, clear account list SUCCESS");
        LOG.info("INTERIM validate, clear transaction output state STARTED");
        getTransactionByAccountAndIndexMap().clear();
        getTransactionByAccountMaxIndexMap().clear();
        LOG.info("INTERIM validate, clear  transaction output state SUCCESS");
        while (blockHeight < maxBlockCount) {
            final String blockHeightStr;
            if (LOG.isDebugEnabled() || LOG.isErrorEnabled()) {
                blockHeightStr = NumberFormat.getIntegerInstance().format(blockHeight);
            } else {
                blockHeightStr = null;
            }
            LOG.debug("INTERIM DEBUG validate {} of {} STARTED ", blockHeightStr, maxBlockCountStr);
            final Block block = getBlock(blockHeight, true);
            if (block == null) {
                LOG.error("INTERIM validate {} of {} FAILURE, block not found in blockchain.", blockHeightStr, maxBlockCountStr);
                blockHeightNoLongerValid = true;
            } else if ((blockHeight != 0) && (!containsBlockWithHash(block.prevHash))) {
                LOG.error("INTERIM validate {} of {} FAILURE, prevHash {} not found in blockchain.", blockHeightStr, maxBlockCountStr, block.prevHash.toHexString());
                deleteBlockAtHeight(blockHeight);
                blockHeightNoLongerValid = true;
            } else if (block.getIndexAsLong() != blockHeight) {
                LOG.error("INTERIM validate {} of {} FAILURE, indexAsLong {} does not match blockchain.", blockHeightStr, maxBlockCountStr, block.getIndexAsLong());
                deleteBlockAtHeight(blockHeight);
                blockHeightNoLongerValid = true;
            } else if (blockHeightNoLongerValid) {
                LOG.error("INTERIM validate {} of {} FAILURE, block height tainted.", blockHeightStr, maxBlockCountStr, block.getIndexAsLong());
                deleteBlockAtHeight(blockHeight);
            } else {
                if (System.currentTimeMillis() > (lastInfoMs + 30000)) {
                    final String numberOfAccountsStr = NumberFormat.getIntegerInstance().format(assetAndValueByAccountMap.size());
                    LOG.info("INTERIM INFO  validate {} of {} SUCCESS, number of accounts:{}; date:{}", blockHeightStr, maxBlockCountStr, numberOfAccountsStr, block.getTimestamp());
                    lastInfoMs = System.currentTimeMillis();
                } else {
                    LOG.debug("INTERIM DEBUG validate {} of {} SUCCESS.", blockHeightStr, maxBlockCountStr);
                }
                final long blockIndex = block.getIndexAsLong();
                int transactionIndex = 0;
                final Map<ByteBuffer, byte[]> txKeyByTxHashMap = new TreeMap<>();
                for (final Transaction transaction : block.getTransactionList()) {
                    final byte[] transactionKeyBa = getTransactionKey(blockIndex, transactionIndex);
                    txKeyByTxHashMap.put(ByteBuffer.wrap(transaction.getHash().toByteArray()), transactionKeyBa);
                    transactionIndex++;
                }
                putWithByteBufferKey(TRANSACTION_KEY_BY_HASH, txKeyByTxHashMap);
                try {
                    updateAssetAndValueByAccountMap(block, false);
                } catch (final Exception e) {
                    throw new RuntimeException("validate: error updating assets for block [" + block.getIndexAsLong() + "]" + block.hash, e);
                }
                lastGoodBlockIndex = block.getIndexAsLong();
            }
            final boolean forceSynch = (lastGoodBlockIndex % BLOCK_FORCE_SYNCH_INTERVAL) == 0;
            if (forceSynch) {
                LOG.info("INTERIM validate, partial commit STARTED index {}", lastGoodBlockIndex);
                commitValidation(lastGoodBlockIndex);
                LOG.info("INTERIM validate, partial commit SUCCESS index {}", lastGoodBlockIndex);
            }
            blockHeight++;
        }
        LOG.info("INTERIM validate, commit STARTED index {}", lastGoodBlockIndex);
        commitValidation(lastGoodBlockIndex);
        LOG.info("INTERIM validate, commit SUCCESS index {}", lastGoodBlockIndex);
        LOG.info("SUCCESS validate");
    } catch (final Exception e) {
        LOG.error("FAILURE validate", e);
        db.rollback();
        throw new RuntimeException(e);
    }
}
Also used : Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) Map(java.util.Map) TreeMap(java.util.TreeMap) BTreeMap(org.mapdb.BTreeMap) NotImplementedException(org.apache.commons.lang3.NotImplementedException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 25 with Transaction

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

the class ModelUtil method getTransactionOutput.

/**
 * returns the transaction output for this coin reference.
 *
 * @param blockDb
 *            the block database to ues.
 * @param coinReference
 *            the coin reference to use.
 * @return the TransactionOutput.
 */
public static TransactionOutput getTransactionOutput(final BlockDb blockDb, final CoinReference coinReference) {
    final UInt256 prevHashReversed = coinReference.prevHash.reverse();
    final Transaction tiTx = blockDb.getTransactionWithHash(prevHashReversed);
    final int prevIndex = coinReference.prevIndex.asInt();
    final TransactionOutput ti = tiTx.outputs.get(prevIndex);
    return ti;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) UInt256(neo.model.bytes.UInt256)

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