Search in sources :

Example 1 with CoinReference

use of neo.model.core.CoinReference 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 2 with CoinReference

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

Example 3 with CoinReference

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

the class CoinReferenceMapToObject method toObject.

@Override
public CoinReference toObject(final Map<String, Object> map) {
    final byte[] prevHashBa = getBytes(map, "prev_transaction_hash");
    final byte[] prevIndexBa = getBytes(map, "prev_transaction_output_index");
    final UInt256 prevHash = new UInt256(ByteBuffer.wrap(prevHashBa));
    final UInt16 prevIndex = new UInt16(prevIndexBa);
    final CoinReference coinReference = new CoinReference(prevHash, prevIndex);
    return coinReference;
}
Also used : CoinReference(neo.model.core.CoinReference) UInt16(neo.model.bytes.UInt16) UInt256(neo.model.bytes.UInt256)

Example 4 with CoinReference

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

the class BlockImportExportUtil method getNetworkFee.

/**
 * return the network fee.
 *
 * @param blockDb
 *            the block database.
 * @param tx
 *            the transaction.
 * @param systemFee
 *            the system fee.
 * @return the network fee.
 */
private static Fixed8 getNetworkFee(final BlockDb blockDb, final Transaction tx, final Fixed8 systemFee) {
    switch(tx.type) {
        case MINER_TRANSACTION:
        case CLAIM_TRANSACTION:
        case ENROLLMENT_TRANSACTION:
        case ISSUE_TRANSACTION:
        case REGISTER_TRANSACTION:
            LOG.trace("txType:{}; No Network Fee", tx.type);
            return ModelUtil.FIXED8_ZERO;
        default:
    }
    Fixed8 totalInput = ModelUtil.FIXED8_ZERO;
    for (final CoinReference cr : tx.inputs) {
        final UInt256 prevHashReversed = cr.prevHash.reverse();
        final Transaction tiTx = blockDb.getTransactionWithHash(prevHashReversed);
        final int prevIndex = cr.prevIndex.asInt();
        final TransactionOutput txOut = tiTx.outputs.get(prevIndex);
        if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
            totalInput = ModelUtil.add(totalInput, txOut.value);
        }
    }
    Fixed8 totalOutput = ModelUtil.FIXED8_ZERO;
    for (final TransactionOutput txOut : tx.outputs) {
        if (txOut.assetId.equals(ModelUtil.GAS_HASH)) {
            totalOutput = ModelUtil.add(totalOutput, txOut.value);
        }
    }
    if (totalInput.equals(ModelUtil.FIXED8_ZERO) && totalOutput.equals(ModelUtil.FIXED8_ZERO) && systemFee.equals(ModelUtil.FIXED8_ZERO)) {
        LOG.trace("txType:{}; Inout,Output, and System fees are all zero, No Network Fee", tx.type);
        return ModelUtil.FIXED8_ZERO;
    }
    final Fixed8 totalFee;
    try {
        totalFee = ModelUtil.subtract(totalOutput, totalInput);
    } catch (final RuntimeException e) {
        LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, tx.getHash());
        throw new RuntimeException("error calculating totalFee", e);
    }
    final Fixed8 networkFee;
    ;
    try {
        networkFee = ModelUtil.subtract(systemFee, totalFee);
    } catch (final RuntimeException e) {
        LOG.error("txType:{}; totalInput:{}; totalOutput:{}; systemFee:{}; totalFee:{}; hash:{};", tx.type, totalInput, totalOutput, systemFee, totalFee, tx.getHash());
        throw new RuntimeException("error calculating networkFee", e);
    }
    return networkFee;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) Fixed8(neo.model.bytes.Fixed8) UInt256(neo.model.bytes.UInt256)

Example 5 with CoinReference

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

the class TestMinTx method test001Remark.

@Test
public void test001Remark() {
    // final String rpcNode = "http://seed2.neo.org:20332";//
    // CityOfZionUtil.getTestNetRpcNode();
    final String rpcNode = CityOfZionUtil.getTestNetRpcNode();
    LOG.info("test001Remark blockCount:{}:", RpcClientUtil.getBlockCount(1000, rpcNode, false));
    final byte[] txBa = new byte[800];
    txBa[0] = TransactionType.CONTRACT_TRANSACTION.getTypeByte();
    txBa[2] = 1;
    txBa[3] = TransactionAttributeUsage.REMARK_00.getTypeByte();
    txBa[4] = 4;
    final Transaction tx = new Transaction(ByteBuffer.wrap(txBa));
    tx.outputs.add(new TransactionOutput(ModelUtil.NEO_HASH, ModelUtil.getFixed8(BigInteger.ONE), ModelUtil.addressToScriptHash("AeKd54zJdgqXy41NgH1PicXTVcz3RdRFdh")));
    tx.inputs.add(new CoinReference(ModelUtil.getUInt256(ByteBuffer.wrap(Hex.decode("24ef2db3a509cd065c85ae33b5b905f30699d69237631598c5f182076619acc8"))), new UInt16(1)));
    LOG.info("test001Remark tx:{}:", tx.toJSONObject().toString(2));
    final JSONArray paramsJson = new JSONArray();
    paramsJson.put(Hex.encode(tx.toByteArray()));
    final JSONObject inputJson = new JSONObject();
    inputJson.put("jsonrpc", "2.0");
    inputJson.put("method", "sendrawtransaction");
    inputJson.put("params", paramsJson);
    inputJson.put("id", 1);
    final JSONObject outputJson = RpcClientUtil.post(1000, rpcNode, false, inputJson);
    Assert.assertNotNull("outputJson acnnot be null", outputJson);
    LOG.info("test001Remark outputJson:{}:", outputJson.toString(2));
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UInt16(neo.model.bytes.UInt16) Test(org.junit.Test)

Aggregations

CoinReference (neo.model.core.CoinReference)14 UInt256 (neo.model.bytes.UInt256)11 TransactionOutput (neo.model.core.TransactionOutput)10 Transaction (neo.model.core.Transaction)8 TreeMap (java.util.TreeMap)6 UInt16 (neo.model.bytes.UInt16)6 JSONObject (org.json.JSONObject)6 Map (java.util.Map)5 UInt160 (neo.model.bytes.UInt160)4 JSONArray (org.json.JSONArray)4 Fixed8 (neo.model.bytes.Fixed8)3 Test (org.junit.Test)3 Block (neo.model.core.Block)2 BlockDb (neo.model.db.BlockDb)2 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