Search in sources :

Example 11 with CoinReference

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

the class CoinReferenceFactory method toObject.

@Override
public CoinReference toObject(final ByteBuffer bb) {
    bb.getLong();
    final byte[] prevHashBa = ModelUtil.getVariableLengthByteArray(bb);
    final byte[] prevIndexBa = ModelUtil.getVariableLengthByteArray(bb);
    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 12 with CoinReference

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

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

the class RpcServerUtil method toUnspentJSONArray.

/**
 * converts a map of TransactionOutputs and CoinReferences to a json array of
 * unspent transaction outputs.
 *
 * @param map
 *            the map to use.
 * @param withDecimals
 *            if true, the value should have decimals.
 * @return the json array of unspent transaction outputs.
 */
private static JSONArray toUnspentJSONArray(final Map<TransactionOutput, CoinReference> map, final boolean withDecimals) {
    if (map == null) {
        return null;
    }
    final JSONArray array = new JSONArray();
    for (final TransactionOutput output : map.keySet()) {
        final CoinReference cr = map.get(output);
        final JSONObject elt = toUnspentJSONObject(withDecimals, output, cr);
        array.put(elt);
    }
    return array;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 14 with CoinReference

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

the class TestCoreToJson method test009CoinReference.

/**
 * test CoinReference.
 */
@Test
public void test009CoinReference() {
    final CoinReference coinReference = MockUtil.getCoinReference000();
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test009CoinReference");
    final String expectedStr = new JSONObject(expectedStrRaw).toString();
    final String actualStr = coinReference.toString();
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : CoinReference(neo.model.core.CoinReference) JSONObject(org.json.JSONObject) 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