Search in sources :

Example 1 with UInt160

use of neo.model.bytes.UInt160 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 2 with UInt160

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

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

the class TransactionOutputFactory method toObject.

@Override
public TransactionOutput toObject(final ByteBuffer bb) {
    bb.getLong();
    final byte[] assetIdBa = ModelUtil.getVariableLengthByteArray(bb);
    final byte[] valueBa = ModelUtil.getVariableLengthByteArray(bb);
    ArrayUtils.reverse(valueBa);
    final byte[] scriptHashBa = ModelUtil.getVariableLengthByteArray(bb);
    final UInt256 assetId = new UInt256(ByteBuffer.wrap(assetIdBa));
    final Fixed8 value = new Fixed8(ByteBuffer.wrap(valueBa));
    final UInt160 scriptHash = new UInt160(scriptHashBa);
    final TransactionOutput transactionOutput = new TransactionOutput(assetId, value, scriptHash);
    return transactionOutput;
}
Also used : TransactionOutput(neo.model.core.TransactionOutput) Fixed8(neo.model.bytes.Fixed8) UInt160(neo.model.bytes.UInt160) UInt256(neo.model.bytes.UInt256)

Example 4 with UInt160

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

the class VerifyScriptUtil method verifyScripts.

/**
 * verify a script's execution.
 *
 * @param blockDb
 *            the blockdb to use.
 * @param tx
 *            the transaction to use.
 * @return true if the script was executed, and did not halt, and left nothing
 *         on the stack.
 */
public static ScriptVerificationResultEnum verifyScripts(final BlockDb blockDb, final Transaction tx) {
    final UInt160[] hashes = tx.getScriptHashesForVerifying(blockDb);
    if (hashes.length != tx.scripts.size()) {
        return ScriptVerificationResultEnum.FAIL_HASH_SCRIPT_COUNT_DIFFERS;
    }
    for (int i = 0; i < hashes.length; i++) {
        byte[] verification = tx.scripts.get(i).getCopyOfVerificationScript();
        if (verification.length == 0) {
            final ScriptBuilder sb = new ScriptBuilder();
            sb.emitAppCall(hashes[i].toByteArray(), false);
            verification = sb.toByteArray();
        } else {
            final UInt160 txScriptHash = tx.scripts.get(i).getScriptHash();
            if (!hashes[i].equals(txScriptHash)) {
                return ScriptVerificationResultEnum.FAIL_HASH_MISMATCH;
            }
        }
        final StateReader service = new StateReader();
        final CachedScriptTable table = new CachedScriptTable(blockDb.getStates(UInt160.class, ContractState.class));
        final ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, tx, table, service, ModelUtil.FIXED8_ZERO, false);
        engine.loadScript(verification, false);
        engine.loadScript(tx.scripts.get(i).getCopyOfInvocationScript(), true);
        if (!engine.Execute()) {
            return ScriptVerificationResultEnum.FAIL_ENGINE_EXECUTE;
        }
        if (engine.evaluationStack.getCount() != 1) {
            return ScriptVerificationResultEnum.FAIL_STACK_CONTAINS_MANY;
        }
        if (!engine.evaluationStack.pop().getBoolean()) {
            return ScriptVerificationResultEnum.FAIL_STACK_CONTAINS_FALSE;
        }
    }
    return ScriptVerificationResultEnum.PASS;
}
Also used : ContractState(neo.vm.contract.ContractState) UInt160(neo.model.bytes.UInt160) StateReader(neo.vm.contract.StateReader) ScriptBuilder(neo.vm.ScriptBuilder) ApplicationEngine(neo.vm.contract.ApplicationEngine) CachedScriptTable(neo.vm.contract.CachedScriptTable)

Example 5 with UInt160

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

the class StateReader method CheckWitness.

// private boolean CheckStorageContext(final StorageContext context) {
// final ContractState contract = Contracts.TryGet(context.ScriptHash);
// if (contract == null) {
// return false;
// }
// if (!contract.HasStorage) {
// return false;
// }
// return true;
// }
// public void Dispose()
// {
// foreach (IDisposable disposable in disposables)
// disposable.Dispose();
// disposables.Clear();
// }
protected boolean CheckWitness(final BlockDb blockDb, final ExecutionEngine engine, final UInt160 hash) {
    final Transaction container = (Transaction) engine.getScriptContainer();
    final UInt160[] _hashes_for_verifying = container.getScriptHashesForVerifying(blockDb);
    return Arrays.asList(_hashes_for_verifying).contains(hash);
}
Also used : Transaction(neo.model.core.Transaction) UInt160(neo.model.bytes.UInt160)

Aggregations

UInt160 (neo.model.bytes.UInt160)16 UInt256 (neo.model.bytes.UInt256)11 Fixed8 (neo.model.bytes.Fixed8)10 TransactionOutput (neo.model.core.TransactionOutput)9 TreeMap (java.util.TreeMap)8 Transaction (neo.model.core.Transaction)8 Map (java.util.Map)7 JSONObject (org.json.JSONObject)7 BlockDb (neo.model.db.BlockDb)5 CoinReference (neo.model.core.CoinReference)4 JSONArray (org.json.JSONArray)4 Block (neo.model.core.Block)3 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 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1