use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class BlockDbMapDbImpl method getByteArrayFromAssetValueMap.
/**
* converts a map of assets and values into a byte array.
*
* @param friendAssetValueMap
* the map to use.
* @return the byte array.
*/
private byte[] getByteArrayFromAssetValueMap(final Map<UInt256, Fixed8> friendAssetValueMap) {
final byte[] mapBa;
final ByteArrayOutputStream bout;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
NetworkUtil.writeVarInt(out, friendAssetValueMap.size());
for (final UInt256 key : friendAssetValueMap.keySet()) {
final Fixed8 value = friendAssetValueMap.get(key);
NetworkUtil.writeByteArray(out, key.toByteArray());
final byte[] valueBa = value.toByteArray();
ArrayUtils.reverse(valueBa);
NetworkUtil.writeByteArray(out, valueBa);
}
bout = out;
} catch (final IOException e) {
throw new RuntimeException(e);
}
mapBa = bout.toByteArray();
return mapBa;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class RpcServerUtil method onGetCityOfZionBalance.
/**
* return the balance of the address.
*
* @param controller
* the controller to use.
* @param address
* the address to use.
* @return the balance of the address.
*/
private static JSONObject onGetCityOfZionBalance(final LocalControllerNode controller, final String address) {
final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
if (LOG.isTraceEnabled()) {
LOG.trace("onGetCityOfZionBalance.scriptHash:{}", scriptHash);
}
try {
final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
final Map<UInt256, Fixed8> assetValueMap = blockDb.getAssetValueMap(scriptHash);
final Map<UInt256, Map<TransactionOutput, CoinReference>> transactionOutputListMap = blockDb.getUnspentTransactionOutputListMap(scriptHash);
if (assetValueMap == null) {
final JSONObject response = new JSONObject();
response.put(GAS, new JSONObject());
response.put(NEO, new JSONObject());
response.put(NET, controller.getLocalNodeData().getNetworkName());
return response;
}
final Fixed8 neo = assetValueMap.get(ModelUtil.NEO_HASH);
final Fixed8 gas = assetValueMap.get(ModelUtil.GAS_HASH);
final JSONObject response = new JSONObject();
final JSONObject neoJo = new JSONObject();
neoJo.put(UNSPENT, toUnspentJSONArray(transactionOutputListMap.get(ModelUtil.NEO_HASH), false));
neoJo.put(BALANCE, neo);
final JSONObject gasJo = new JSONObject();
gasJo.put(UNSPENT, toUnspentJSONArray(transactionOutputListMap.get(ModelUtil.GAS_HASH), true));
gasJo.put(BALANCE, gas);
response.put(GAS, gasJo);
response.put(NEO, neoJo);
response.put(NET, controller.getLocalNodeData().getNetworkName());
return response;
} catch (final RuntimeException e) {
LOG.error("onGetCityOfZionBalance", 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 RpcServerUtil method onGetAccountList.
/**
* returns the account list for accounts that were active between the given
* timestamps.
*
* @param controller
* the controller to use.
* @param id
* the id to use.
* @param params
* the parameters to use.
* @return the list of account data.
*/
private static JSONObject onGetAccountList(final LocalControllerNode controller, final int id, final JSONArray params) {
try {
LOG.trace("getaccountlist 0");
final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
final long fromTs = params.getLong(0);
final long toTs = params.getLong(1);
final long minHeight = 0;
final long maxHeight = blockDb.getBlockCount();
final long fromHeight = getHeightOfTs(controller, 0, minHeight, maxHeight, fromTs);
final long toHeight = getHeightOfTs(controller, 0, fromHeight, maxHeight, toTs);
LOG.trace("getaccountlist 1 fromHeight:{};toHeight:{};", fromHeight, toHeight);
LOG.trace("getaccountlist 2 accountStateCache STARTED");
final Map<UInt160, Map<UInt256, Fixed8>> addressStateCache = blockDb.getAccountAssetValueMap();
LOG.trace("getaccountlist 2 accountStateCache SUCCESS, count:{}", addressStateCache.size());
final Map<UInt160, Long> neoTxByAddress = new TreeMap<>();
final Map<UInt160, Long> gasTxByAddress = new TreeMap<>();
final Map<UInt160, Long> claimTxByAddress = new TreeMap<>();
final Map<UInt160, Long> neoInByAddress = new TreeMap<>();
final Map<UInt160, Long> gasInByAddress = new TreeMap<>();
final Map<UInt160, Long> neoOutByAddress = new TreeMap<>();
final Map<UInt160, Long> gasOutByAddress = new TreeMap<>();
final Map<UInt160, Long> firstTsByAddress = new TreeMap<>();
final Map<UInt160, Long> lastTsByAddress = new TreeMap<>();
for (long index = fromHeight; index < toHeight; index++) {
LOG.trace("getaccountlist 3 fromHeight:{};toHeight:{};index:{};", fromHeight, toHeight, index);
final Block block = blockDb.getFullBlockFromHeight(index);
for (final Transaction t : block.getTransactionList()) {
final Map<UInt160, Map<UInt256, Long>> addressAssetMap = getAddressAssetMap(blockDb, t);
for (final UInt160 friend : addressAssetMap.keySet()) {
if (!firstTsByAddress.containsKey(friend)) {
firstTsByAddress.put(friend, block.timestamp.asLong());
}
lastTsByAddress.put(friend, block.timestamp.asLong());
if (t.type.equals(TransactionType.CLAIM_TRANSACTION)) {
MapUtil.increment(claimTxByAddress, friend);
}
if (addressAssetMap.get(friend).containsKey(ModelUtil.NEO_HASH)) {
MapUtil.increment(neoTxByAddress, friend);
final long value = addressAssetMap.get(friend).get(ModelUtil.NEO_HASH);
if (value < 0) {
MapUtil.increment(neoInByAddress, friend, -value);
} else {
MapUtil.increment(neoOutByAddress, friend, value);
}
}
if (addressAssetMap.get(friend).containsKey(ModelUtil.GAS_HASH)) {
MapUtil.increment(gasTxByAddress, friend);
final long value = addressAssetMap.get(friend).get(ModelUtil.GAS_HASH);
if (value < 0) {
MapUtil.increment(gasInByAddress, friend, -value);
} else {
MapUtil.increment(gasOutByAddress, friend, value);
}
}
}
}
}
LOG.trace("getaccountlist 4 addressByAccount STARTED");
final Map<UInt160, String> addressByScriptHash = new TreeMap<>();
for (final UInt160 key : addressStateCache.keySet()) {
final String address = ModelUtil.scriptHashToAddress(key);
addressByScriptHash.put(key, address);
}
LOG.trace("getaccountlist 4 addressByAccount SUCCESS, address count:{};", addressByScriptHash.size());
LOG.trace("getaccountlist 5 returnList STARTED");
final JSONArray returnList = new JSONArray();
for (final UInt160 key : addressStateCache.keySet()) {
LOG.trace("getaccountlist 6 key:{};", key);
if (addressByScriptHash.containsKey(key)) {
final Map<UInt256, Fixed8> addressState = addressStateCache.get(key);
final String address = addressByScriptHash.get(key);
LOG.trace("getaccountlist 7 key:{}; address:{};", key, address);
final JSONObject entry = new JSONObject();
entry.put("account", address);
if (addressState.containsKey(ModelUtil.NEO_HASH)) {
entry.put(ModelUtil.NEO, ModelUtil.toRoundedLong(addressState.get(ModelUtil.NEO_HASH).value));
} else {
entry.put(ModelUtil.NEO, 0);
}
if (addressState.containsKey(ModelUtil.GAS_HASH)) {
entry.put(ModelUtil.GAS, ModelUtil.toRoundedDouble(addressState.get(ModelUtil.GAS_HASH).value));
} else {
entry.put(ModelUtil.GAS, 0);
}
if (neoInByAddress.containsKey(key)) {
entry.put(NEO_IN, ModelUtil.toRoundedLong(neoInByAddress.get(key)));
} else {
entry.put(NEO_IN, 0);
}
if (neoOutByAddress.containsKey(key)) {
entry.put(NEO_OUT, ModelUtil.toRoundedLong(neoOutByAddress.get(key)));
} else {
entry.put(NEO_OUT, 0);
}
if (gasInByAddress.containsKey(key)) {
entry.put(GAS_IN, ModelUtil.toRoundedDouble(gasInByAddress.get(key)));
} else {
entry.put(GAS_IN, 0);
}
if (gasOutByAddress.containsKey(key)) {
entry.put(GAS_OUT, ModelUtil.toRoundedDouble(gasOutByAddress.get(key)));
} else {
entry.put(GAS_OUT, 0);
}
if (neoTxByAddress.containsKey(key)) {
entry.put(NEO_TX, neoTxByAddress.get(key));
} else {
entry.put(NEO_TX, 0);
}
if (gasTxByAddress.containsKey(key)) {
entry.put(GAS_TX, gasTxByAddress.get(key));
} else {
entry.put(GAS_TX, 0);
}
if (claimTxByAddress.containsKey(key)) {
entry.put(CLAIM_TX, claimTxByAddress.get(key));
} else {
entry.put(CLAIM_TX, 0);
}
if (firstTsByAddress.containsKey(key)) {
entry.put(FIRST_TS, firstTsByAddress.get(key));
} else {
entry.put(FIRST_TS, 0);
}
if (lastTsByAddress.containsKey(key)) {
entry.put(LAST_TS, lastTsByAddress.get(key));
} else {
entry.put(LAST_TS, 0);
}
returnList.put(entry);
}
}
LOG.trace("getaccountlist 5 returnList SUCCESS, returnList.size:{};", returnList.length());
LOG.trace("getaccountlist 6 return");
final JSONObject response = new JSONObject();
response.put(ID, id);
response.put(JSONRPC, VERSION_2_0);
response.put(RESULT, returnList);
return response;
} catch (final RuntimeException e) {
LOG.error("error in onGetAccountList:", e);
final JSONObject response = new JSONObject();
response.put(ERROR, e.getMessage());
response.put(EXPECTED, new JSONArray());
response.put(ACTUAL, new JSONArray());
return response;
}
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class ModelUtil method add.
/**
* adds two Fixed8 values.
*
* @param value1
* the first value
* @param value2
* the second value.
* @return the sum of the two values.
*/
public static Fixed8 add(final Fixed8 value1, final Fixed8 value2) {
final BigInteger oldBi = value1.toPositiveBigInteger();
final BigInteger valBi = value2.toPositiveBigInteger();
final BigInteger newBi = oldBi.add(valBi);
final Fixed8 newValue = getFixed8(newBi);
return newValue;
}
use of neo.model.bytes.Fixed8 in project neo-java by coranos.
the class ModelUtil method getFixed8.
/**
* creates a new Fixed8 from a BigInteger.
*
* @param newBi
* the BigInteger to use.
* @return the new Fixed8.
*/
public static Fixed8 getFixed8(final BigInteger newBi) {
final byte[] ba = new byte[UInt64.SIZE];
final byte[] biBa = newBi.toByteArray();
final int destPos;
final int srcPos;
final int length;
if (biBa.length <= ba.length) {
destPos = UInt64.SIZE - biBa.length;
srcPos = 0;
length = biBa.length;
} else if (biBa[0] == 0) {
destPos = 0;
srcPos = 1;
length = biBa.length - 1;
} else {
destPos = UInt64.SIZE - biBa.length;
srcPos = 0;
length = biBa.length;
}
try {
System.arraycopy(biBa, srcPos, ba, destPos, length);
ArrayUtils.reverse(ba);
final Fixed8 newValue = new Fixed8(ByteBuffer.wrap(ba));
return newValue;
} catch (final ArrayIndexOutOfBoundsException e) {
final JSONObject msgJson = new JSONObject();
msgJson.put("ba", Hex.encodeHexString(ba));
msgJson.put("biBa", Hex.encodeHexString(biBa));
msgJson.put("destPos", destPos);
msgJson.put("srcPos", srcPos);
msgJson.put("length", length);
final String msg = msgJson.toString();
throw new RuntimeException(msg, e);
}
}
Aggregations