Search in sources :

Example 16 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class RpcServerUtil method onGetRawMempool.

/**
 * return the transactions in the unverified transaction pool.
 *
 * @param controller
 *            the controllers to use
 * @param id
 *            the id to use.
 * @return the transactions in the unverified transaction pool.
 */
private static JSONObject onGetRawMempool(final LocalControllerNode controller, final int id) {
    try {
        final JSONArray resultArray = new JSONArray();
        for (final Transaction transaction : controller.getLocalNodeData().getUnverifiedTransactionSet()) {
            final String hex = ModelUtil.toHexString(transaction.toByteArray());
            resultArray.put(hex);
        }
        final JSONObject response = new JSONObject();
        response.put(RESULT, resultArray);
        response.put(ID, id);
        response.put(JSONRPC, VERSION_2_0);
        return response;
    } catch (final RuntimeException e) {
        final JSONObject response = new JSONObject();
        response.put(ERROR, e.getMessage());
        final JSONArray expectedArray = new JSONArray();
        expectedArray.put(EXPECTED_GENERIC_HEX);
        response.put(EXPECTED, expectedArray);
        response.put(ACTUAL, expectedArray);
        return response;
    }
}
Also used : Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 17 with Transaction

use of neo.model.core.Transaction in project neo-java by coranos.

the class RpcServerUtil method onGetCityOfZionHistory.

/**
 * return the transaction history of the address.
 *
 * @param controller
 *            the controller to use.
 * @param address
 *            the address to use.
 * @return the balance of the address.
 */
private static JSONObject onGetCityOfZionHistory(final LocalControllerNode controller, final String address) {
    final UInt160 scriptHash = ModelUtil.addressToScriptHash(address);
    if (LOG.isTraceEnabled()) {
        LOG.trace("onGetCityOfZionHistory.scriptHash:{}", scriptHash);
    }
    try {
        final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
        final List<Transaction> transactionList = blockDb.getTransactionWithAccountList(scriptHash);
        final JSONArray historyJa = new JSONArray();
        if (transactionList != null) {
            for (final Transaction transaction : transactionList) {
                Fixed8 neo = ModelUtil.FIXED8_ZERO;
                Fixed8 gas = ModelUtil.FIXED8_ZERO;
                for (final TransactionOutput to : transaction.outputs) {
                    if (to.scriptHash.equals(scriptHash)) {
                        if (to.assetId.equals(ModelUtil.NEO_HASH)) {
                            neo = ModelUtil.add(neo, to.value);
                        }
                        if (to.assetId.equals(ModelUtil.GAS_HASH)) {
                            gas = ModelUtil.add(gas, to.value);
                        }
                    }
                }
                final JSONObject transactionResponse = new JSONObject();
                transactionResponse.put(GAS, ModelUtil.toRoundedDouble(gas.value));
                transactionResponse.put(NEO, ModelUtil.toRoundedLong(neo.value));
                final Long blockIndex = blockDb.getBlockIndexFromTransactionHash(transaction.getHash());
                transactionResponse.put("block_index", blockIndex);
                transactionResponse.put(TXID, transaction.getHash().toString());
                historyJa.put(transactionResponse);
            }
        }
        final JSONObject response = new JSONObject();
        response.put(ADDRESS, address);
        response.put(HISTORY, historyJa);
        response.put(NET, controller.getLocalNodeData().getNetworkName());
        return response;
    } catch (final RuntimeException e) {
        LOG.error("onGetCityOfZionHistory", 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 : TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) JSONArray(org.json.JSONArray) BlockDb(neo.model.db.BlockDb)

Example 18 with Transaction

use of neo.model.core.Transaction 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 19 with Transaction

use of neo.model.core.Transaction 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 20 with Transaction

use of neo.model.core.Transaction 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)

Aggregations

Transaction (neo.model.core.Transaction)49 JSONObject (org.json.JSONObject)26 Block (neo.model.core.Block)25 JSONArray (org.json.JSONArray)19 TransactionOutput (neo.model.core.TransactionOutput)17 Test (org.junit.Test)14 UInt256 (neo.model.bytes.UInt256)13 TreeMap (java.util.TreeMap)12 BlockDb (neo.model.db.BlockDb)9 Map (java.util.Map)8 Fixed8 (neo.model.bytes.Fixed8)8 UInt160 (neo.model.bytes.UInt160)8 CoinReference (neo.model.core.CoinReference)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 LocalNodeData (neo.network.model.LocalNodeData)5 Timestamp (java.sql.Timestamp)4 UInt16 (neo.model.bytes.UInt16)4 List (java.util.List)3 TreeSet (java.util.TreeSet)3