Search in sources :

Example 1 with ScriptBuilder

use of neo.vm.ScriptBuilder 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)

Aggregations

UInt160 (neo.model.bytes.UInt160)1 ScriptBuilder (neo.vm.ScriptBuilder)1 ApplicationEngine (neo.vm.contract.ApplicationEngine)1 CachedScriptTable (neo.vm.contract.CachedScriptTable)1 ContractState (neo.vm.contract.ContractState)1 StateReader (neo.vm.contract.StateReader)1