use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class ModelUtil method subtract.
/**
* subtracts two Fixed8 values.
*
* @param left
* the left value
* @param right
* the right value.
* @return left minus right
*/
public static Fixed8 subtract(final Fixed8 left, final Fixed8 right) {
final BigInteger leftBi = left.toPositiveBigInteger();
final BigInteger rightBi = right.toPositiveBigInteger();
final BigInteger newBi = rightBi.subtract(leftBi);
if (newBi.signum() < 0) {
throw new RuntimeException("tried to subtract " + leftBi + "(Fixed8:" + left + ") from " + rightBi + " (Fixed8:" + right + ")" + " cannot have a negative fixed8 with value " + newBi + ".");
}
final Fixed8 newValue = getFixed8(newBi);
return newValue;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class BlockDbH2Impl method getAccountAssetValueMap.
@Override
public Map<UInt160, Map<UInt256, Fixed8>> getAccountAssetValueMap() {
final JdbcTemplate jdbcOperations = new JdbcTemplate(ds);
final String sql = getSql("getAccountAssetValueMap");
final List<Map<String, Object>> mapList = jdbcOperations.queryForList(sql);
final Map<UInt160, Map<UInt256, Fixed8>> accountAssetValueMap = new TreeMap<>();
final TransactionOutputMapToObject mapToObject = new TransactionOutputMapToObject();
for (final Map<String, Object> map : mapList) {
final TransactionOutput output = mapToObject.toObject(map);
if (!accountAssetValueMap.containsKey(output.scriptHash)) {
accountAssetValueMap.put(output.scriptHash, new TreeMap<>());
}
final Map<UInt256, Fixed8> assetValueMap = accountAssetValueMap.get(output.scriptHash);
assetValueMap.put(output.assetId, output.value);
}
return accountAssetValueMap;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class TransactionOutputMapToObject method toObject.
@Override
public TransactionOutput toObject(final Map<String, Object> map) {
final byte[] assetIdBa = getBytes(map, "asset_id");
final byte[] valueBa = getBytes(map, "value");
ArrayUtils.reverse(valueBa);
final byte[] scriptHashBa = getBytes(map, "script_hash");
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;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class BlockImportExportUtil method getNetworkFee.
/**
* return the network fee.
*
* @param blockDb
* the block database.
* @param tx
* the transaction.
* @param systemFee
* the system fee.
* @return the network fee.
*/
private static Fixed8 getNetworkFee(final BlockDb blockDb, final Transaction tx, final Fixed8 systemFee) {
switch(tx.type) {
case MINER_TRANSACTION:
case CLAIM_TRANSACTION:
case ENROLLMENT_TRANSACTION:
case ISSUE_TRANSACTION:
case REGISTER_TRANSACTION:
LOG.trace("txType:{}; No Network Fee", tx.type);
return ModelUtil.FIXED8_ZERO;
default:
}
Fixed8 totalInput = ModelUtil.FIXED8_ZERO;
for (final CoinReference cr : tx.inputs) {
final UInt256 prevHashReversed = cr.prevHash.reverse();
final Transaction tiTx = blockDb.getTransactionWithHash(prevHashReversed);
final int prevIndex = cr.prevIndex.asInt();
final TransactionOutput txOut = tiTx.outputs.get(prevIndex);
if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
totalInput = ModelUtil.add(totalInput, txOut.value);
}
}
Fixed8 totalOutput = ModelUtil.FIXED8_ZERO;
for (final TransactionOutput txOut : tx.outputs) {
if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
totalOutput = ModelUtil.add(totalOutput, txOut.value);
}
}
if (totalInput.equals(ModelUtil.FIXED8_ZERO) && totalOutput.equals(ModelUtil.FIXED8_ZERO) && systemFee.equals(ModelUtil.FIXED8_ZERO)) {
LOG.trace("txType:{}; Inout,Output, and System fees are all zero, No Network Fee", tx.type);
return ModelUtil.FIXED8_ZERO;
}
final Fixed8 totalFee;
try {
totalFee = ModelUtil.subtract(totalOutput, totalInput);
} catch (final RuntimeException e) {
LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, tx.getHash());
throw new RuntimeException("error calculating totalFee", e);
}
final Fixed8 networkFee;
;
try {
networkFee = ModelUtil.subtract(systemFee, totalFee);
} catch (final RuntimeException e) {
LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; totalFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, totalFee, tx.getHash());
throw new RuntimeException("error calculating networkFee", e);
}
return networkFee;
}
use of neo.model.bytes.Fixed8 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