Search in sources :

Example 6 with UInt256

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

the class RpcServerUtil method onGetRawTransaction.

/**
 * responds to a "getrawtransaction" command.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the request id to use.
 * @param params
 *            the parameters to use.
 * @return the response.
 */
private static JSONObject onGetRawTransaction(final LocalControllerNode controller, final int id, final JSONArray params) {
    if (params.length() == 0) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "no parameters, expected a txid");
        response.put(EXPECTED, EXPECTED_GENERIC_HEX);
        response.put(ACTUAL, NULL);
        return response;
    } else {
        final boolean verbose;
        if (params.length() >= 2) {
            if (params.get(1) instanceof Number) {
                final long index = params.getLong(1);
                verbose = index == 1;
            } else {
                verbose = false;
            }
        } else {
            verbose = false;
        }
        final String txIdStr = params.getString(0);
        final byte[] ba = ModelUtil.decodeHex(txIdStr);
        final UInt256 txId = new UInt256(ByteBuffer.wrap(ba));
        final Transaction transaction;
        try {
            transaction = controller.getLocalNodeData().getBlockDb().getTransactionWithHash(txId);
        } catch (final RuntimeException e) {
            final JSONObject response = new JSONObject();
            response.put(ERROR, e.getMessage());
            response.put(EXPECTED, EXPECTED_GENERIC_HEX);
            response.put(ACTUAL, params.get(0));
            return response;
        }
        final JSONObject response = new JSONObject();
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        if (verbose) {
            response.put(RESULT, transaction.toJSONObject());
        } else {
            response.put(RESULT, Hex.encodeHexString(transaction.toByteArray()));
        }
        return response;
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) UInt256(neo.model.bytes.UInt256)

Example 7 with UInt256

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

the class RpcServerUtil method onGetCityOfZionClaims.

/**
 * return the available claims of the address.
 *
 * @param controller
 *            the controller to use.
 * @param address
 *            the address to use.
 * @return the balance of the address.
 */
private static JSONObject onGetCityOfZionClaims(final LocalControllerNode controller, final String address) {
    final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
    if (LOG.isTraceEnabled()) {
        LOG.trace("onGetCityOfZionClaims.scriptHash:{}", scriptHash);
    }
    try {
        final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
        final Map<UInt256, Map<TransactionOutput, CoinReference>> transactionOutputListMap = controller.getLocalNodeData().getBlockDb().getUnspentTransactionOutputListMap(scriptHash);
        final JSONArray claimJa = new JSONArray();
        if (transactionOutputListMap != null) {
            final Map<TransactionOutput, CoinReference> neoTransactionOutputListMap = transactionOutputListMap.get(ModelUtil.NEO_HASH);
            final Map<TransactionOutput, Long> blockIxByTxoMap = new TreeMap<>();
            final List<Transaction> transactionList = blockDb.getTransactionWithAccountList(scriptHash);
            for (final Transaction transaction : transactionList) {
                final long blockIx = blockDb.getBlockIndexFromTransactionHash(transaction.getHash());
                for (final TransactionOutput to : transaction.outputs) {
                    if (neoTransactionOutputListMap.containsKey(to)) {
                        blockIxByTxoMap.put(to, blockIx);
                    }
                }
            }
            for (final TransactionOutput output : neoTransactionOutputListMap.keySet()) {
                final CoinReference cr = neoTransactionOutputListMap.get(output);
                final JSONObject unspent = toUnspentJSONObject(false, output, cr);
                final JSONObject claim = new JSONObject();
                final String txHashStr = unspent.getString(TXID);
                claim.put(TXID, txHashStr);
                claim.put(INDEX, unspent.getLong(INDEX));
                claim.put(VALUE, unspent.getLong(VALUE));
                final UInt256 txHash = ModelUtil.getUInt256(ByteBuffer.wrap(ModelUtil.decodeHex(txHashStr)), true);
                final long start = blockDb.getBlockIndexFromTransactionHash(txHash);
                claim.put(START, start);
                final long end = blockIxByTxoMap.get(output);
                claim.put(END, end);
                claim.put(SYSFEE, computeSysFee(controller.getLocalNodeData().getTransactionSystemFeeMap(), blockDb, start, end));
                claim.put("claim", calculateBonus(claim));
                claimJa.put(claim);
            }
        }
        final JSONObject response = new JSONObject();
        response.put(ADDRESS, address);
        response.put(CLAIMS, claimJa);
        response.put(NET, controller.getLocalNodeData().getNetworkName());
        return response;
    } catch (final RuntimeException e) {
        LOG.error("onGetCityOfZionClaims", 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 : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt160(neo.model.bytes.UInt160) BlockDb(neo.model.db.BlockDb) Map(java.util.Map) TreeMap(java.util.TreeMap) UInt256(neo.model.bytes.UInt256)

Example 8 with UInt256

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

the class RpcServerUtil method onGetTransactionOutput.

/**
 * responds to a "gettxout" command.
 *
 * @param controller
 *            the controller to use.
 * @param id
 *            the request id to use.
 * @param params
 *            the parameters to use.
 * @return the response.
 */
private static JSONObject onGetTransactionOutput(final LocalControllerNode controller, final int id, final JSONArray params) {
    if (params.length() == 0) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "no parameters, expected a txid and an index");
        final JSONArray expectedParams = new JSONArray();
        expectedParams.put(EXPECTED_GENERIC_HEX);
        expectedParams.put(0);
        response.put(EXPECTED, expectedParams);
        response.put(ACTUAL, new JSONArray());
        return response;
    } else if (params.length() == 1) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, "only one parameter, expected a txid and an index");
        final JSONArray expectedParams = new JSONArray();
        expectedParams.put(EXPECTED_GENERIC_HEX);
        expectedParams.put(0);
        response.put(EXPECTED, expectedParams);
        response.put(ACTUAL, params);
        return response;
    } else {
        final String txIdStr = params.getString(0);
        final int outputsIndex = params.getInt(1);
        final byte[] ba = ModelUtil.decodeHex(txIdStr);
        final UInt256 txId = new UInt256(ByteBuffer.wrap(ba));
        final Transaction transaction;
        try {
            transaction = controller.getLocalNodeData().getBlockDb().getTransactionWithHash(txId);
        } catch (final RuntimeException e) {
            final JSONObject response = new JSONObject();
            response.put(ERROR, e.getMessage());
            final JSONArray expectedParams = new JSONArray();
            expectedParams.put(EXPECTED_GENERIC_HEX);
            expectedParams.put(0);
            response.put(ACTUAL, params);
            return response;
        }
        if (transaction.outputs.isEmpty()) {
            final JSONObject response = new JSONObject();
            response.put(ERROR, "transaction with hex \"" + txIdStr + "\" has no outputs.");
            final JSONArray expectedParams = new JSONArray();
            expectedParams.put(EXPECTED_GENERIC_HEX);
            expectedParams.put(0);
            response.put(EXPECTED, 1);
            response.put(ACTUAL, 0);
            return response;
        }
        if (outputsIndex >= transaction.outputs.size()) {
            final JSONObject response = new JSONObject();
            response.put(ERROR, "requested index \"" + outputsIndex + "\" is is too large, needs to be less than \"" + transaction.outputs.size() + "\"");
            final JSONArray expectedParams = new JSONArray();
            expectedParams.put(EXPECTED_GENERIC_HEX);
            expectedParams.put(0);
            response.put(EXPECTED, transaction.outputs.size() - 1);
            response.put(ACTUAL, outputsIndex);
            return response;
        }
        final JSONObject response = new JSONObject();
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        response.put(RESULT, transaction.outputs.get(outputsIndex).toJSONObject());
        return response;
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) JSONArray(org.json.JSONArray) UInt256(neo.model.bytes.UInt256)

Example 9 with UInt256

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

the class RpcServerUtil method onGetCityOfZionTransaction.

/**
 * return the transaction as a JSON object.
 *
 * @param controller
 *            the controller to use.
 * @param transactionHex
 *            the transaction hex to use.
 * @return the transaction as a JSON object.
 */
private static JSONObject onGetCityOfZionTransaction(final LocalControllerNode controller, final String transactionHex) {
    final Transaction transaction;
    try {
        final byte[] ba = ModelUtil.decodeHex(transactionHex);
        final UInt256 txId = new UInt256(ByteBuffer.wrap(ba));
        transaction = controller.getLocalNodeData().getBlockDb().getTransactionWithHash(txId);
    } catch (final RuntimeException 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, transactionHex);
        return response;
    }
    return transaction.toJSONObject();
}
Also used : Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt256(neo.model.bytes.UInt256)

Example 10 with UInt256

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

the class BlockDbH2Impl method getUnspentTransactionOutputListMap.

@Override
public Map<UInt256, Map<TransactionOutput, CoinReference>> getUnspentTransactionOutputListMap(final UInt160 account) {
    synchronized (this) {
        if (closed) {
            return null;
        }
    }
    final JdbcTemplate t = new JdbcTemplate(ds);
    final String sql = getSql("getUnspentTransactionOutputListMap");
    final List<Map<String, Object>> mapList = t.queryForList(sql, account.toByteArray());
    final AbstractMapToObject<TransactionOutput> toMapToObject = new TransactionOutputMapToObject();
    final AbstractMapToObject<CoinReference> crMapToObject = new CoinReferenceMapToObject();
    final Map<UInt256, Map<TransactionOutput, CoinReference>> assetIdTxoMap = new TreeMap<>();
    for (final Map<String, Object> map : mapList) {
        final TransactionOutput to = toMapToObject.toObject(map);
        final CoinReference cr = crMapToObject.toObject(map);
        if (!assetIdTxoMap.containsKey(to.assetId)) {
            assetIdTxoMap.put(to.assetId, new TreeMap<>());
        }
        assetIdTxoMap.get(to.assetId).put(to, cr);
    }
    return assetIdTxoMap;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Map(java.util.Map) TreeMap(java.util.TreeMap) 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