Search in sources :

Example 16 with UInt160

use of neo.model.bytes.UInt160 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;
    }
}
Also used : JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) Map(java.util.Map) TreeMap(java.util.TreeMap) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt160 (neo.model.bytes.UInt160)16 UInt256 (neo.model.bytes.UInt256)11 Fixed8 (neo.model.bytes.Fixed8)10 TransactionOutput (neo.model.core.TransactionOutput)9 TreeMap (java.util.TreeMap)8 Transaction (neo.model.core.Transaction)8 Map (java.util.Map)7 JSONObject (org.json.JSONObject)7 BlockDb (neo.model.db.BlockDb)5 CoinReference (neo.model.core.CoinReference)4 JSONArray (org.json.JSONArray)4 Block (neo.model.core.Block)3 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1