Search in sources :

Example 1 with UInt16

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

the class BlockDbH2Impl method getTransactionIndex.

/**
 * converts a transaction index byte array into an integer.
 *
 * @param transactionIndexBa
 *            the byte array to use.
 * @return the integer index.
 */
private int getTransactionIndex(final byte[] transactionIndexBa) {
    final UInt16 transactionIndexObj = new UInt16(transactionIndexBa);
    final int transactionIndex = transactionIndexObj.asInt();
    return transactionIndex;
}
Also used : UInt16(neo.model.bytes.UInt16)

Example 2 with UInt16

use of neo.model.bytes.UInt16 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 3 with UInt16

use of neo.model.bytes.UInt16 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)

Example 4 with UInt16

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

the class AbstractJsonMockBlockDb method getUnspentTransactionOutputListMap.

@Override
public Map<UInt256, Map<TransactionOutput, CoinReference>> getUnspentTransactionOutputListMap(final UInt160 account) {
    final Map<UInt256, Map<TransactionOutput, CoinReference>> assetIdTxoMap = new TreeMap<>();
    final JSONArray mockBlockDb = getMockBlockDb();
    for (int ix = 0; ix < mockBlockDb.length(); ix++) {
        final JSONObject mockBlock = mockBlockDb.getJSONObject(ix);
        final Block block = getBlock(mockBlock, true);
        for (int txIx = 0; txIx < block.getTransactionList().size(); txIx++) {
            final Transaction transaction = block.getTransactionList().get(txIx);
            for (final TransactionOutput output : transaction.outputs) {
                if (output.scriptHash.equals(account)) {
                    if (!assetIdTxoMap.containsKey(output.assetId)) {
                        assetIdTxoMap.put(output.assetId, new TreeMap<>());
                    }
                    final CoinReference cr = new CoinReference(transaction.getHash(), new UInt16(txIx));
                    assetIdTxoMap.get(output.assetId).put(output, cr);
                }
            }
        }
    }
    return assetIdTxoMap;
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) JSONObject(org.json.JSONObject) Transaction(neo.model.core.Transaction) Block(neo.model.core.Block) UInt16(neo.model.bytes.UInt16) TreeMap(java.util.TreeMap) Map(java.util.Map) UInt256(neo.model.bytes.UInt256)

Example 5 with UInt16

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

the class BlockDbMapDbImpl method updateAssetAndValueByAccountMap.

/**
 * updates the asset and value by account map.
 *
 * @param block
 *            the block to update.
 * @param reverse
 *            if true, reverse the update.
 */
private void updateAssetAndValueByAccountMap(final Block block, final boolean reverse) {
    final BTreeMap<byte[], byte[]> assetAndValueByAccountMap = getAssetAndValueByAccountMap();
    final BTreeMap<byte[], byte[]> transactionByAccountAndIndexMap = getTransactionByAccountAndIndexMap();
    final BTreeMap<byte[], Long> transactionByAccountMaxIndexMap = getTransactionByAccountMaxIndexMap();
    final BTreeMap<byte[], Boolean> transactionOutputSpentStateMap = getTransactionOutputSpentStateMap();
    LOG.debug("updateAssetAndValueByAccountMap STARTED block;{};reverse;{};numberOfAccounts:{}", block.getIndexAsLong(), reverse, assetAndValueByAccountMap.size());
    for (final Transaction t : block.getTransactionList()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("updateAssetAndValueByAccountMap INTERIM tx:{}", t.getHash());
        }
        for (final CoinReference cr : t.inputs) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("updateAssetAndValueByAccountMap INTERIM cr:{}", cr.toJSONObject());
            }
            final byte[] crBa = cr.toByteArray();
            if (!transactionOutputSpentStateMap.containsKey(crBa)) {
                throw new RuntimeException("referenced transaction output was never a transaction input:" + cr);
            }
            final boolean oldSpendState;
            final boolean newSpendState;
            if (reverse) {
                oldSpendState = true;
                newSpendState = false;
            } else {
                oldSpendState = false;
                newSpendState = true;
            }
            if (transactionOutputSpentStateMap.get(crBa) == oldSpendState) {
                transactionOutputSpentStateMap.put(crBa, newSpendState);
                final UInt256 prevHashReversed = cr.prevHash.reverse();
                final Transaction tiTx = getTransactionWithHash(prevHashReversed);
                if (tiTx == null) {
                    throw new RuntimeException("no transaction with prevHash:" + prevHashReversed + " in block[1] " + block.hash + " index[1] " + block.getIndexAsLong());
                }
                final int prevIndex = cr.prevIndex.asInt();
                if (prevIndex >= tiTx.outputs.size()) {
                    throw new RuntimeException("prevIndex:" + prevIndex + " exceeds output size:" + tiTx.outputs.size() + "; in block[2] " + block.hash + " index[2] " + block.getIndexAsLong());
                }
                final TransactionOutput ti = tiTx.outputs.get(prevIndex);
                final UInt160 input = ti.scriptHash;
                final byte[] inputBa = input.toByteArray();
                final Map<UInt256, Fixed8> accountAssetValueMap = ensureAccountExists(assetAndValueByAccountMap, inputBa);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("TI beforeMap {}", accountAssetValueMap);
                }
                if (!accountAssetValueMap.containsKey(ti.assetId)) {
                    accountAssetValueMap.put(ti.assetId, ModelUtil.FIXED8_ZERO);
                }
                final Fixed8 oldValue = accountAssetValueMap.get(ti.assetId);
                final Fixed8 newValue;
                if (reverse) {
                    newValue = ModelUtil.add(ti.value, oldValue);
                } else {
                    newValue = ModelUtil.subtract(ti.value, oldValue);
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM input;{};", ModelUtil.scriptHashToAddress(input));
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM ti.assetId:{} oldValue:{};", ti.assetId, oldValue);
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM ti.assetId:{} to.value:{};", ti.assetId, ti.value);
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM ti.assetId:{} newValue:{};", ti.assetId, newValue);
                }
                if (newValue.equals(ModelUtil.FIXED8_ZERO)) {
                    accountAssetValueMap.remove(ti.assetId);
                } else {
                    accountAssetValueMap.put(ti.assetId, newValue);
                }
                if (accountAssetValueMap.isEmpty()) {
                    assetAndValueByAccountMap.remove(inputBa);
                } else {
                    putAssetValueMap(assetAndValueByAccountMap, inputBa, accountAssetValueMap);
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("TI afterMap {}", ensureAccountExists(assetAndValueByAccountMap, inputBa));
                }
            } else {
                if (reverse) {
                    throw new RuntimeException("referenced transaction output is not already spent:" + cr);
                } else {
                    throw new RuntimeException("referenced transaction output is already spent:" + cr);
                }
            }
        }
        try {
            for (int outputIx = 0; outputIx < t.outputs.size(); outputIx++) {
                final TransactionOutput to = t.outputs.get(outputIx);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM to:{}", to.toJSONObject());
                }
                final UInt160 output = to.scriptHash;
                final byte[] outputBa = output.toByteArray();
                final Map<UInt256, Fixed8> accountAssetValueMap = ensureAccountExists(assetAndValueByAccountMap, outputBa);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("TO beforeMap {}", accountAssetValueMap);
                }
                if (!accountAssetValueMap.containsKey(to.assetId)) {
                    accountAssetValueMap.put(to.assetId, ModelUtil.FIXED8_ZERO);
                }
                final Fixed8 oldValue = accountAssetValueMap.get(to.assetId);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM output;{};", ModelUtil.scriptHashToAddress(output));
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM to.assetId:{} oldValue:{};", to.assetId, oldValue);
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM to.assetId:{} to.value:{};", to.assetId, to.value);
                }
                final Fixed8 newValue;
                if (reverse) {
                    newValue = ModelUtil.subtract(to.value, oldValue);
                } else {
                    newValue = ModelUtil.add(oldValue, to.value);
                }
                accountAssetValueMap.put(to.assetId, newValue);
                if (accountAssetValueMap.isEmpty()) {
                    assetAndValueByAccountMap.remove(outputBa);
                } else {
                    putAssetValueMap(assetAndValueByAccountMap, outputBa, accountAssetValueMap);
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("updateAssetAndValueByAccountMap INTERIM to.assetId:{} newValue:{};", to.assetId, newValue);
                    LOG.debug("TO afterMap {}", ensureAccountExists(assetAndValueByAccountMap, outputBa));
                }
                final CoinReference cr = new CoinReference(t.getHash().reverse(), new UInt16(outputIx));
                if (reverse) {
                    transactionOutputSpentStateMap.remove(cr.toByteArray());
                } else {
                    transactionOutputSpentStateMap.put(cr.toByteArray(), false);
                }
            }
            final byte[] tBa = t.toByteArray();
            final long index;
            if (transactionByAccountMaxIndexMap.containsKey(tBa)) {
                index = transactionByAccountMaxIndexMap.get(tBa);
            } else {
                index = 0;
            }
            transactionByAccountMaxIndexMap.put(tBa, index + 1);
            final byte[] accountKeyBa = getAccountKey(tBa, index);
            transactionByAccountAndIndexMap.put(accountKeyBa, tBa);
        } catch (final RuntimeException e) {
            final String msg = "error processing transaction type " + t.type + " hash " + t.getHash();
            throw new RuntimeException(msg, e);
        }
    }
    LOG.debug("updateAssetAndValueByAccountMap SUCCESS block;{};numberOfAccounts:{}", block.getIndexAsLong(), assetAndValueByAccountMap.size());
}
Also used : CoinReference(neo.model.core.CoinReference) TransactionOutput(neo.model.core.TransactionOutput) Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160) Fixed8(neo.model.bytes.Fixed8) UInt16(neo.model.bytes.UInt16) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt16 (neo.model.bytes.UInt16)7 CoinReference (neo.model.core.CoinReference)6 UInt256 (neo.model.bytes.UInt256)5 Transaction (neo.model.core.Transaction)4 TransactionOutput (neo.model.core.TransactionOutput)4 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Fixed8 (neo.model.bytes.Fixed8)1 UInt160 (neo.model.bytes.UInt160)1 Block (neo.model.core.Block)1 Test (org.junit.Test)1 BTreeMap (org.mapdb.BTreeMap)1