use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class CoinReferenceMapToObject method toObject.
@Override
public CoinReference toObject(final Map<String, Object> map) {
final byte[] prevHashBa = getBytes(map, "prev_transaction_hash");
final byte[] prevIndexBa = getBytes(map, "prev_transaction_output_index");
final UInt256 prevHash = new UInt256(ByteBuffer.wrap(prevHashBa));
final UInt16 prevIndex = new UInt16(prevIndexBa);
final CoinReference coinReference = new CoinReference(prevHash, prevIndex);
return coinReference;
}
use of neo.model.bytes.UInt256 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.UInt256 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.UInt256 in project neo-java by coranos.
the class AbstractJsonMockBlockDb method getAccountAssetValueMap.
@Override
public final Map<UInt160, Map<UInt256, Fixed8>> getAccountAssetValueMap() {
final Map<UInt160, Map<UInt256, Fixed8>> accountAssetValueMap = new TreeMap<>();
final JSONArray mockBlockDb = getMockBlockDb();
for (int ix = 0; ix < mockBlockDb.length(); ix++) {
final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
final Block block = getBlock(mockBlock, true);
for (final Transaction transaction : block.getTransactionList()) {
for (final TransactionOutput output : transaction.outputs) {
if (!accountAssetValueMap.containsKey(output.scriptHash)) {
accountAssetValueMap.put(output.scriptHash, new TreeMap<>());
}
final Map<UInt256, Fixed8> assetValueMap = accountAssetValueMap.get(output.scriptHash);
final Fixed8 value = output.value;
if (assetValueMap.containsKey(output.assetId)) {
final Fixed8 oldValue = assetValueMap.get(output.assetId);
final Fixed8 newValue = ModelUtil.add(value, oldValue);
assetValueMap.put(output.assetId, newValue);
} else {
assetValueMap.put(output.assetId, value);
}
}
}
}
return accountAssetValueMap;
}
use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class AbstractJsonMockBlockDb method getUnspentTransactionOutputListMap.
@Override
public Map<UInt256, Map<TransactionOutput, CoinReference>> getUnspentTransactionOutputListMap(final UInt160 account) {
final Map<UInt256, Map<TransactionOutput, CoinReference>> assetIdTxoMap = new TreeMap<>();
final JSONArray mockBlockDb = getMockBlockDb();
for (int ix = 0; ix < mockBlockDb.length(); ix++) {
final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
final Block block = getBlock(mockBlock, true);
for (int txIx = 0; txIx < block.getTransactionList().size(); txIx++) {
final Transaction transaction = block.getTransactionList().get(txIx);
for (final TransactionOutput output : transaction.outputs) {
if (output.scriptHash.equals(account)) {
if (!assetIdTxoMap.containsKey(output.assetId)) {
assetIdTxoMap.put(output.assetId, new TreeMap<>());
}
final CoinReference cr = new CoinReference(transaction.getHash(), new UInt16(txIx));
assetIdTxoMap.get(output.assetId).put(output, cr);
}
}
}
}
return assetIdTxoMap;
}
Aggregations