use of neo.model.bytes.Fixed8 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.Fixed8 in project neo-java by coranos.
the class LocalControllerNode method getTransactionSystemFeeMap.
/**
* return the map of system fees to transaction types.
*
* @param localJson
* the local json to use.
* @return the map of system fees to transaction types.
*/
private Map<TransactionType, Fixed8> getTransactionSystemFeeMap(final JSONObject localJson) {
final Map<TransactionType, Fixed8> transactionSystemFeeMap = new EnumMap<>(TransactionType.class);
final JSONObject transactionSystemFeeJson = localJson.getJSONObject(ConfigurationUtil.SYSTEM_FEE);
for (final String key : transactionSystemFeeJson.keySet()) {
final long value = transactionSystemFeeJson.getLong(key);
final TransactionType txType = TransactionType.valueOf(key);
transactionSystemFeeMap.put(txType, ModelUtil.getFixed8(BigInteger.valueOf(value)));
}
for (final TransactionType txType : TransactionType.values()) {
if (!transactionSystemFeeMap.containsKey(txType)) {
LOG.error("TransactionType {} has no SystemFee in the configuration, setting SystemFee to zero", txType);
transactionSystemFeeMap.put(txType, ModelUtil.FIXED8_ZERO);
}
}
return transactionSystemFeeMap;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class RpcServerUtil method onGetCityOfZionHistory.
/**
* return the transaction history of the address.
*
* @param controller
* the controller to use.
* @param address
* the address to use.
* @return the balance of the address.
*/
private static JSONObject onGetCityOfZionHistory(final LocalControllerNode controller, final String address) {
final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
if (LOG.isTraceEnabled()) {
LOG.trace("onGetCityOfZionHistory.scriptHash:{}", scriptHash);
}
try {
final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
final List<Transaction> transactionList = blockDb.getTransactionWithAccountList(scriptHash);
final JSONArray historyJa = new JSONArray();
if (transactionList != null) {
for (final Transaction transaction : transactionList) {
Fixed8 neo = ModelUtil.FIXED8_ZERO;
Fixed8 gas = ModelUtil.FIXED8_ZERO;
for (final TransactionOutput to : transaction.outputs) {
if (to.scriptHash.equals(scriptHash)) {
if (to.assetId.equals(ModelUtil.NEO_HASH)) {
neo = ModelUtil.add(neo, to.value);
}
if (to.assetId.equals(ModelUtil.GAS_HASH)) {
gas = ModelUtil.add(gas, to.value);
}
}
}
final JSONObject transactionResponse = new JSONObject();
transactionResponse.put(GAS, ModelUtil.toRoundedDouble(gas.value));
transactionResponse.put(NEO, ModelUtil.toRoundedLong(neo.value));
final Long blockIndex = blockDb.getBlockIndexFromTransactionHash(transaction.getHash());
transactionResponse.put("block_index", blockIndex);
transactionResponse.put(TXID, transaction.getHash().toString());
historyJa.put(transactionResponse);
}
}
final JSONObject response = new JSONObject();
response.put(ADDRESS, address);
response.put(HISTORY, historyJa);
response.put(NET, controller.getLocalNodeData().getNetworkName());
return response;
} catch (final RuntimeException e) {
LOG.error("onGetCityOfZionHistory", e);
final JSONObject response = new JSONObject();
if (e.getMessage() == null) {
response.put(ERROR, e.getClass().getName());
} else {
response.put(ERROR, e.getMessage());
}
response.put(EXPECTED, EXPECTED_GENERIC_HEX);
response.put(ACTUAL, address);
return response;
}
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class BlockDbH2Impl method getAssetValueMap.
@Override
public Map<UInt256, Fixed8> getAssetValueMap(final UInt160 account) {
final JdbcTemplate jdbcOperations = new JdbcTemplate(ds);
final String sql = getSql("getAssetValueMap");
final List<Map<String, Object>> mapList = jdbcOperations.queryForList(sql, account);
final Map<UInt256, Fixed8> assetValueMap = new TreeMap<>();
final TransactionOutputMapToObject mapToObject = new TransactionOutputMapToObject();
for (final Map<String, Object> map : mapList) {
final TransactionOutput output = mapToObject.toObject(map);
assetValueMap.put(output.assetId, output.value);
}
return assetValueMap;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class TransactionOutputFactory method toObject.
@Override
public TransactionOutput toObject(final ByteBuffer bb) {
bb.getLong();
final byte[] assetIdBa = ModelUtil.getVariableLengthByteArray(bb);
final byte[] valueBa = ModelUtil.getVariableLengthByteArray(bb);
ArrayUtils.reverse(valueBa);
final byte[] scriptHashBa = ModelUtil.getVariableLengthByteArray(bb);
final UInt256 assetId = new UInt256(ByteBuffer.wrap(assetIdBa));
final Fixed8 value = new Fixed8(ByteBuffer.wrap(valueBa));
final UInt160 scriptHash = new UInt160(scriptHashBa);
final TransactionOutput transactionOutput = new TransactionOutput(assetId, value, scriptHash);
return transactionOutput;
}
Aggregations