Search in sources :

Example 31 with UInt256

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

Example 32 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class RpcServerUtil method getAddressAssetMap.

/**
 * returns the address asset map.
 *
 * @param blockDb
 *            the block database to use.
 * @param transaction
 *            the transaction to use.
 * @return the address asset map.
 */
public static Map<UInt160, Map<UInt256, Long>> getAddressAssetMap(final BlockDb blockDb, final Transaction transaction) {
    final Map<UInt160, Map<UInt256, Long>> friendAssetMap = new TreeMap<>();
    for (final CoinReference cr : transaction.inputs) {
        final UInt256 prevHashReversed = cr.prevHash.reverse();
        final Transaction tiTx = blockDb.getTransactionWithHash(prevHashReversed);
        if (tiTx == null) {
            throw new RuntimeException("no transaction with prevHash:" + prevHashReversed);
        }
        final TransactionOutput ti = tiTx.outputs.get(cr.prevIndex.asInt());
        final UInt160 input = ti.scriptHash;
        if ((ti.assetId.equals(ModelUtil.NEO_HASH)) || (ti.assetId.equals(ModelUtil.GAS_HASH))) {
            MapUtil.increment(friendAssetMap, input, ti.assetId, ti.value.value, TreeMap.class);
        }
    }
    for (final TransactionOutput to : transaction.outputs) {
        final UInt160 output = to.scriptHash;
        if ((to.assetId.equals(ModelUtil.NEO_HASH)) || (to.assetId.equals(ModelUtil.GAS_HASH))) {
            MapUtil.increment(friendAssetMap, output, to.assetId, -to.value.value, TreeMap.class);
        }
    }
    return friendAssetMap;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) UInt256(neo.model.bytes.UInt256)

Example 33 with UInt256

use of neo.model.bytes.UInt256 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)

Example 34 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class LocalNodeDataSynchronizedUtil method verifyUnverifiedHeaders.

/**
 * verify any unverified headers, by checking if their block height is less than
 * the max block height in the blockchain.
 *
 * @param localNodeData
 *            the local node data to use.
 * @return true if any block was added to the chain.
 */
public static boolean verifyUnverifiedHeaders(final LocalNodeData localNodeData) {
    synchronized (localNodeData) {
        LOG.debug("STARTED verifyUnverifiedHeaders");
        boolean anyHeaderChanged = false;
        final Block highestBlock = localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex();
        final UInt256 highestBlockHash;
        final long highestBlockIndex;
        if (highestBlock == null) {
            highestBlockHash = GenesisBlockUtil.GENESIS_HASH;
            highestBlockIndex = 0;
        } else {
            highestBlockHash = highestBlock.hash;
            highestBlockIndex = highestBlock.getIndexAsLong();
        }
        removeHeadersNotOverBlockIndexUnsynchronized(localNodeData, highestBlockIndex);
        final Iterator<Header> unverifiedHeaderIt = localNodeData.getUnverifiedHeaderPoolSet().iterator();
        while (unverifiedHeaderIt.hasNext()) {
            final Header unverifiedHeader = unverifiedHeaderIt.next();
            if (LOG.isTraceEnabled()) {
                LOG.trace("INTERIM verifyUnverifiedHeaders, unverifiedHeader index:{};hash:{}; VerifiedHeaderPoolMap.isEmpty:{};", unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, localNodeData.getVerifiedHeaderPoolMap().isEmpty());
            }
            if (localNodeData.getVerifiedHeaderPoolMap().isEmpty()) {
                if (LOG.isTraceEnabled()) {
                    final String message = "INTERIM verifyUnverifiedHeaders[1]," + " unverifiedHeader index:{};hash:{};prevHash:{}; highestBlock index:{};hash:{};";
                    LOG.trace(message, unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash, highestBlockIndex, highestBlockHash);
                }
                if (unverifiedHeader.prevHash.equals(highestBlockHash)) {
                    localNodeData.getVerifiedHeaderPoolMap().put(unverifiedHeader.getIndexAsLong(), unverifiedHeader);
                    unverifiedHeaderIt.remove();
                    anyHeaderChanged = true;
                    if (LOG.isTraceEnabled()) {
                        final String message = "SUCCESS verifyUnverifiedHeaders[1]," + " unverifiedHeader index:{};hash:{};prevHash:{};";
                        LOG.trace(message, unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash);
                    }
                } else {
                    if (LOG.isTraceEnabled()) {
                        final String message = "FAILURE verifyUnverifiedHeaders[2]," + " unverifiedHeader index:{};hash:{};prevHash:{};highestBlockHash:{};";
                        LOG.trace(message, unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash, highestBlockHash);
                    }
                }
            } else {
                final long verifiedHeaderHeight = localNodeData.getVerifiedHeaderPoolMap().lastKey();
                final Header verifiedHeader = localNodeData.getVerifiedHeaderPoolMap().get(verifiedHeaderHeight);
                if (LOG.isTraceEnabled()) {
                    final String message = "INTERIM verifyUnverifiedHeaders[2]," + " unverifiedHeader index:{};hash:{};prevHash:{}; verifiedHeader index:{};hash:{};";
                    LOG.trace(message, unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash, verifiedHeaderHeight, verifiedHeader.hash);
                }
                if (unverifiedHeader.prevHash.equals(verifiedHeader.hash)) {
                    localNodeData.getVerifiedHeaderPoolMap().put(unverifiedHeader.getIndexAsLong(), unverifiedHeader);
                    unverifiedHeaderIt.remove();
                    anyHeaderChanged = true;
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("SUCCESS verifyUnverifiedHeaders, unverifiedHeader index:{};hash:{};prevHash:{};", unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash);
                    }
                } else {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("FAILURE verifyUnverifiedHeaders, unverifiedHeader index:{};hash:{};prevHash:{};", unverifiedHeader.getIndexAsLong(), unverifiedHeader.hash, unverifiedHeader.prevHash);
                    }
                }
            }
        }
        LOG.debug("SUCCESS verifyUnverifiedHeaders, anyHeaderChanged:{};", anyHeaderChanged);
        return anyHeaderChanged;
    }
}
Also used : Header(neo.model.core.Header) Block(neo.model.core.Block) UInt256(neo.model.bytes.UInt256)

Example 35 with UInt256

use of neo.model.bytes.UInt256 in project neo-java by coranos.

the class LocalNodeDataSynchronizedUtil method requestBlocksUnsynchronized.

/**
 * request blocks from the remote node, this is the unsynchronized helper
 * method..
 *
 * @param localNodeData
 *            the local node data to use.
 * @param remoteNodeData
 *            the remote node data to use.
 */
private static void requestBlocksUnsynchronized(final LocalNodeData localNodeData, final RemoteNodeData remoteNodeData) {
    if (!localNodeData.getVerifiedHeaderPoolMap().isEmpty()) {
        final List<UInt256> hashs = new ArrayList<>();
        final Iterator<Entry<Long, Header>> headerIt = localNodeData.getVerifiedHeaderPoolMap().entrySet().iterator();
        while ((hashs.size() < InvPayload.MAX_HASHES) && headerIt.hasNext()) {
            final Entry<Long, Header> headerElt = headerIt.next();
            final Header header = headerElt.getValue();
            final UInt256 hashRaw = header.hash;
            if (localNodeData.getBlockDb().containsBlockWithHash(hashRaw)) {
                headerIt.remove();
            } else {
                final byte[] ba = hashRaw.getBytesCopy();
                final UInt256 hash = new UInt256(ba);
                hashs.add(hash);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("requestBlocks send {} getblocks {} {}", remoteNodeData.getHostAddress(), header.getIndexAsLong(), hash.toReverseHexString());
                }
            }
        }
        MessageUtil.sendGetData(remoteNodeData, localNodeData, InventoryType.BLOCK, hashs.toArray(new UInt256[0]));
    } else {
        if (localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex() == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("requestBlocks send {} hash is genesis.", remoteNodeData.getHostAddress());
            }
            MessageUtil.sendGetData(remoteNodeData, localNodeData, InventoryType.BLOCK, GenesisBlockUtil.GENESIS_HASH);
        } else {
            LOG.info("SKIPPING requestBlocks, no hashes.");
        }
    }
}
Also used : Entry(java.util.Map.Entry) Header(neo.model.core.Header) ArrayList(java.util.ArrayList) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt256 (neo.model.bytes.UInt256)36 TransactionOutput (neo.model.core.TransactionOutput)15 JSONObject (org.json.JSONObject)14 TreeMap (java.util.TreeMap)13 Fixed8 (neo.model.bytes.Fixed8)13 Transaction (neo.model.core.Transaction)13 Map (java.util.Map)11 UInt160 (neo.model.bytes.UInt160)11 CoinReference (neo.model.core.CoinReference)11 Block (neo.model.core.Block)8 JSONArray (org.json.JSONArray)7 UInt16 (neo.model.bytes.UInt16)5 BlockDb (neo.model.db.BlockDb)5 ArrayList (java.util.ArrayList)3 Header (neo.model.core.Header)3 BTreeMap (org.mapdb.BTreeMap)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvPayload (neo.model.network.InvPayload)2 Color (java.awt.Color)1