Ejemplo n.º 1
0
        /// <summary>
        /// executes the script until it finishes, fails or hits a breakpoint
        /// </summary>
        public DebuggerState Run()
        {
            do
            {
                lastState = Step();
            } while (lastState.state == DebuggerState.State.Running);

            return(lastState);
        }
Ejemplo n.º 2
0
        public void Reset(DataNode inputs)
        {
            if (lastState.state == DebuggerState.State.Reset)
            {
                return;
            }

            if (currentTransaction == null)
            {
                //throw new Exception("Transaction not set");
                currentTransaction = new Transaction(this.blockchain.currentBlock);
            }

            _usedGas = 0;

            currentTransaction.emulator = this;
            engine = new ExecutionEngine(currentTransaction, Crypto.Default, null, interop);
            engine.LoadScript(contractBytes);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                var items = new Stack <object>();

                if (inputs != null)
                {
                    foreach (var item in inputs.Children)
                    {
                        var obj = NeoEmulator.ConvertArgument(item);
                        items.Push(obj);
                    }
                }

                while (items.Count > 0)
                {
                    var item = items.Pop();
                    EmitObject(sb, item);
                }

                engine.LoadScript(sb.ToArray());
            }

            foreach (var pos in _breakpoints)
            {
                engine.AddBreakPoint((uint)pos);
            }

            engine.Reset();

            lastState          = new DebuggerState(DebuggerState.State.Reset, 0);
            currentTransaction = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// executes a single instruction in the current script, and returns the last script offset
        /// </summary>
        public DebuggerState Step()
        {
            if (lastState.state == DebuggerState.State.Finished || lastState.state == DebuggerState.State.Invalid)
            {
                return(lastState);
            }

            engine.ExecuteSingleStep();

            try
            {
                lastOffset = engine.CurrentContext.InstructionPointer;

                var    opcode = engine.lastOpcode;
                double opCost;

                if (opcode <= OpCode.PUSH16)
                {
                    opCost = 0;
                }
                else
                {
                    switch (opcode)
                    {
                    case OpCode.SYSCALL:
                    {
                        var callInfo = interop.FindCall(engine.lastSysCall);
                        opCost = (callInfo != null) ? callInfo.gasCost : 0;

                        if (engine.lastSysCall.EndsWith("Storage.Put"))
                        {
                            opCost *= (Storage.lastStorageLength / 1024.0);
                        }
                        break;
                    }

                    case OpCode.CHECKMULTISIG:
                    case OpCode.CHECKSIG: opCost = 0.1; break;

                    case OpCode.APPCALL:
                    case OpCode.TAILCALL:
                    case OpCode.SHA256:
                    case OpCode.SHA1: opCost = 0.01; break;

                    case OpCode.HASH256:
                    case OpCode.HASH160: opCost = 0.02; break;

                    case OpCode.NOP: opCost = 0; break;

                    default: opCost = 0.001; break;
                    }
                }

                _usedGas += opCost;
            }
            catch
            {
                // failed to get instruction pointer
            }

            if (engine.State.HasFlag(VMState.FAULT))
            {
                lastState = new DebuggerState(DebuggerState.State.Exception, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.BREAK))
            {
                lastState = new DebuggerState(DebuggerState.State.Break, lastOffset);
                return(lastState);
            }

            if (engine.State.HasFlag(VMState.HALT))
            {
                lastState = new DebuggerState(DebuggerState.State.Finished, lastOffset);
                return(lastState);
            }

            lastState = new DebuggerState(DebuggerState.State.Running, lastOffset);
            return(lastState);
        }
Ejemplo n.º 4
0
        public void Reset(DataNode inputs)
        {
            if (lastState.state == DebuggerState.State.Reset)
            {
                return;
            }

            if (currentTransaction == null)
            {
                //throw new Exception("Transaction not set");
                currentTransaction = new Transaction(this.blockchain.currentBlock);
            }

            usedGas         = 0;
            usedOpcodeCount = 0;

            currentTransaction.emulator = this;
            engine = new ExecutionEngine(currentTransaction, Crypto.Default, null, interop);
            engine.LoadScript(contractBytes);

            foreach (var output in currentTransaction.outputs)
            {
                if (output.hash == this.currentHash)
                {
                    output.hash = new UInt160(engine.CurrentContext.ScriptHash);
                }
            }

            foreach (var pos in _breakpoints)
            {
                engine.AddBreakPoint((uint)pos);
            }

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                var items = new Stack <object>();

                if (inputs != null)
                {
                    foreach (var item in inputs.Children)
                    {
                        var obj = NeoEmulator.ConvertArgument(item);
                        items.Push(obj);
                    }
                }

                while (items.Count > 0)
                {
                    var item = items.Pop();
                    EmitObject(sb, item);
                }

                var loaderScript = sb.ToArray();
                //System.IO.File.WriteAllBytes("loader.avm", loaderScript);
                engine.LoadScript(loaderScript);
            }

            //engine.Reset();

            lastState          = new DebuggerState(DebuggerState.State.Reset, 0);
            currentTransaction = null;
        }