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