Search in sources :

Example 1 with UInt32

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

the class MockUtil method getMockBlock002.

private static Block getMockBlock002() {
    final int minSize = (UInt32.SIZE * 3) + (UInt256.SIZE * 2) + (UInt64.SIZE) + (UInt160.SIZE) + 4;
    final byte[] ba = new byte[minSize];
    final int indexOffset = (UInt32.SIZE * 2) + (UInt256.SIZE * 2);
    final UInt32 index = new UInt32(1);
    System.arraycopy(index.getBytesCopy(), 0, ba, indexOffset, index.getByteLength());
    return new Block(ByteBuffer.wrap(ba));
}
Also used : Block(neo.model.core.Block) UInt32(neo.model.bytes.UInt32)

Example 2 with UInt32

use of neo.model.bytes.UInt32 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 3 with UInt32

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

the class Message method toByteArray.

/**
 * return the message as a byte array.
 *
 * @return the message as a byte array.
 * @throws IOException
 *             if an error occurs.
 * @throws UnsupportedEncodingException
 *             if an error occurs.
 */
public byte[] toByteArray() throws IOException, UnsupportedEncodingException {
    final byte[] magicBa = NetworkUtil.getIntByteArray(magic);
    ArrayUtils.reverse(magicBa);
    final UInt32 magicObj = new UInt32(magicBa);
    final byte[] checksum = calculateChecksum(payloadBa).toByteArray();
    ArrayUtils.reverse(checksum);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bout.write(magicObj.getBytesCopy());
    NetworkUtil.writeString(bout, 12, command);
    if (LOG.isTraceEnabled()) {
        LOG.trace("createMessage magic+command {}", Hex.encodeHexString(bout.toByteArray()));
    }
    final byte[] lengthBa = NetworkUtil.getIntByteArray(payloadBa.length);
    ArrayUtils.reverse(lengthBa);
    if (LOG.isTraceEnabled()) {
        LOG.trace("createMessage lengthBa {}", Hex.encodeHexString(lengthBa));
    }
    bout.write(lengthBa);
    if (LOG.isTraceEnabled()) {
        LOG.trace("createMessage magic+command+length {}", Hex.encodeHexString(bout.toByteArray()));
        LOG.trace("createMessage checksum {}", Hex.encodeHexString(checksum));
    }
    bout.write(checksum);
    if (LOG.isTraceEnabled()) {
        LOG.trace("createMessage magic+command+length+checksum {}", Hex.encodeHexString(bout.toByteArray()));
    }
    bout.write(payloadBa);
    if (LOG.isTraceEnabled()) {
        LOG.trace("createMessage payloadBa {}", Hex.encodeHexString(payloadBa));
        LOG.trace("createMessage magic+command+length+checksum+payload {}", Hex.encodeHexString(bout.toByteArray()));
    }
    return bout.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) UInt32(neo.model.bytes.UInt32)

Example 4 with UInt32

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

the class BlockUtil method getBlockHeightBa.

/**
 * converts the block height to a byte array.
 *
 * @param blockHeight
 *            the block height to use.
 * @return the block height as a byte array.
 */
public static byte[] getBlockHeightBa(final long blockHeight) {
    final UInt32 indexObj = new UInt32(blockHeight);
    final byte[] indexBa = indexObj.toByteArray();
    return indexBa;
}
Also used : UInt32(neo.model.bytes.UInt32)

Example 5 with UInt32

use of neo.model.bytes.UInt32 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

UInt32 (neo.model.bytes.UInt32)5 Block (neo.model.core.Block)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 Timestamp (java.sql.Timestamp)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 Fixed8 (neo.model.bytes.Fixed8)2 Transaction (neo.model.core.Transaction)2 TransactionOutput (neo.model.core.TransactionOutput)2 BlockDb (neo.model.db.BlockDb)2 LocalNodeData (neo.network.model.LocalNodeData)2 JSONObject (org.json.JSONObject)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1