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));
}
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);
}
}
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();
}
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;
}
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);
}
}
Aggregations