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;
}
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;
}
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;
}
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);
}
Aggregations