Search in sources :

Example 11 with Transaction

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

the class TestDBH2 method test006putAndGetTransactionWithHash.

/**
 * test put, and getTransactionWithHash.
 */
@Test
public void test006putAndGetTransactionWithHash() {
    try (TestLocalControllerNode controller = getTestLocalControllerNode()) {
        final Block block = MockUtil.getMockBlock001();
        controller.getBlockDb().put(true, block);
        final Transaction expectedTransaction = block.getTransactionList().get(0);
        final Transaction actualTransaction = controller.getBlockDb().getTransactionWithHash(expectedTransaction.getHash());
        Assert.assertEquals("transactions should match.", expectedTransaction.toJSONObject().toString(2), actualTransaction.toJSONObject().toString(2));
    }
}
Also used : Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) Test(org.junit.Test)

Example 12 with Transaction

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

the class TestDBMapDb method test006putAndGetTransactionWithHash.

/**
 * test put, and getTransactionWithHash.
 */
@Test
public void test006putAndGetTransactionWithHash() {
    try (TestLocalControllerNode controller = getTestLocalControllerNode()) {
        final Block block = MockUtil.getMockBlock003();
        controller.getBlockDb().put(true, GenesisBlockUtil.GENESIS_BLOCK);
        controller.getBlockDb().put(true, block);
        final Transaction expectedTransaction = block.getTransactionList().get(0);
        final Transaction actualTransaction = controller.getBlockDb().getTransactionWithHash(expectedTransaction.getHash());
        Assert.assertEquals("transactions should match.", expectedTransaction.toJSONObject().toString(2), actualTransaction.toJSONObject().toString(2));
    }
}
Also used : Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) Test(org.junit.Test)

Example 13 with Transaction

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

the class BlockImportExportUtil method importBlocks.

/**
 * imports the blocks from the file.
 *
 * @param controller
 *            the controller.
 */
public static void importBlocks(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);
        long maxIndex = 0;
        try (InputStream fileIn = new FileInputStream(localNodeData.getChainExportDataFileName());
            BufferedInputStream buffIn = new BufferedInputStream(fileIn, 1024 * 1024 * 32);
            DataInputStream in = new DataInputStream(buffIn)) {
            final byte[] maxIndexBa = new byte[UInt32.SIZE];
            in.read(maxIndexBa);
            if (LOG.isTraceEnabled()) {
                LOG.info("import maxIndexBa asread {}", Hex.encode(maxIndexBa));
            }
            ArrayUtils.reverse(maxIndexBa);
            maxIndex = new UInt32(maxIndexBa).asLong();
            LOG.info("started import {}", INTEGER_FORMAT.format(maxIndex));
            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++) {
                final int length = Integer.reverseBytes(in.readInt());
                LOG.debug("STARTED import {} of {} length {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), INTEGER_FORMAT.format(length));
                final byte[] ba = new byte[length];
                in.read(ba);
                final Block block = new Block(ByteBuffer.wrap(ba));
                final boolean forceSynch = (blockIx % BlockDb.BLOCK_FORCE_SYNCH_INTERVAL) == 0;
                blockDb.put(forceSynch, block);
                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);
                    }
                }
                LOG.debug("SUCCESS import {} of {} hash {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), block.hash);
                MapUtil.increment(numBlocksByTxCountMap, String.valueOf(block.getTransactionList().size()));
                final Timestamp blockTs = block.getTimestamp();
                if (startMs < 0) {
                    startMs = blockTs.getTime();
                }
                final long ms = blockTs.getTime() - startMs;
                if (ms > (86400 * 1000)) {
                    blockDb.put(true);
                    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);
                    final long maxBlockHeaderIndex = maxBlockHeader.getIndexAsLong();
                    LOG.info("INTERIM import {} of {}, bx {}, tx {} json {}", INTEGER_FORMAT.format(blockIx), INTEGER_FORMAT.format(maxIndex), INTEGER_FORMAT.format(maxBlockHeaderIndex), 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();
                }
            }
            blockDb.put(true);
            LOG.info("SUCCESS import {}, synched", INTEGER_FORMAT.format(maxIndex));
        } catch (final IOException e) {
            if (e instanceof EOFException) {
                blockDb.put(true);
                final Block maxBlockHeader = blockDb.getHeaderOfBlockWithMaxIndex();
                LOG.error("FAILURE import {} of {}, synched, EOFException", INTEGER_FORMAT.format(maxBlockHeader.getIndexAsLong()), maxIndex);
                LOG.error("EOFException", e);
                return;
            } else {
                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) BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Timestamp(java.sql.Timestamp) BufferedInputStream(java.io.BufferedInputStream) Fixed8(neo.model.bytes.Fixed8) EOFException(java.io.EOFException) PrintWriter(java.io.PrintWriter) LocalNodeData(neo.network.model.LocalNodeData) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) 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 Transaction

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

the class RpcServerUtil method onGetRawTransaction.

/**
 * responds to a "getrawtransaction" command.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the request id to use.
 * @param params
 *            the parameters to use.
 * @return the response.
 */
private static JSONObject onGetRawTransaction(final LocalControllerNode controller, final int id, final JSONArray params) {
    if (params.length() == 0) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "no parameters, expected a txid");
        response.put(EXPECTED, EXPECTED_GENERIC_HEX);
        response.put(ACTUAL, NULL);
        return response;
    } else {
        final boolean verbose;
        if (params.length() >= 2) {
            if (params.get(1) instanceof Number) {
                final long index = params.getLong(1);
                verbose = index == 1;
            } else {
                verbose = false;
            }
        } else {
            verbose = false;
        }
        final String txIdStr = params.getString(0);
        final byte[] ba = ModelUtil.decodeHex(txIdStr);
        final UInt256 txId = new UInt256(ByteBuffer.wrap(ba));
        final Transaction transaction;
        try {
            transaction = controller.getLocalNodeData().getBlockDb().getTransactionWithHash(txId);
        } catch (final RuntimeException e) {
            final JSONObject response = new JSONObject();
            response.put(ERROR, e.getMessage());
            response.put(EXPECTED, EXPECTED_GENERIC_HEX);
            response.put(ACTUAL, params.get(0));
            return response;
        }
        final JSONObject response = new JSONObject();
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        if (verbose) {
            response.put(RESULT, transaction.toJSONObject());
        } else {
            response.put(RESULT, Hex.encodeHexString(transaction.toByteArray()));
        }
        return response;
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) UInt256(neo.model.bytes.UInt256)

Example 15 with Transaction

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

the class RpcServerUtil method onSendRawTransaction.

/**
 * sends a raw transaction to the blockchain.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the id to use.
 * @param params
 *            the parameters to use.
 * @return true if successful
 */
private static JSONObject onSendRawTransaction(final LocalControllerNode controller, final int id, final JSONArray params) {
    if (params.length() == 0) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "no parameters, expected a hex encoded transaction");
        final JSONArray expectedParams = new JSONArray();
        expectedParams.put(EXPECTED_GENERIC_HEX);
        expectedParams.put(0);
        response.put(EXPECTED, expectedParams);
        response.put(ACTUAL, new JSONArray());
        return response;
    }
    try {
        final String hex = params.getString(0);
        final byte[] ba = ModelUtil.decodeHex(hex);
        final Transaction tx = new Transaction(ByteBuffer.wrap(ba));
        controller.getLocalNodeData().getUnverifiedTransactionSet().add(tx);
    } catch (final RuntimeException e) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, e.getMessage());
        response.put(EXPECTED, true);
        response.put(ACTUAL, params.get(0));
        return response;
    }
    final JSONObject response = new JSONObject();
    response.put(RESULT, true);
    response.put(ID, id);
    response.put(JSONRPC, VERSION_2_0);
    return response;
}
Also used : JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) JSONArray(org.json.JSONArray)

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