Exemple #1
0
        public void TestInvoke(UInt160 contractHash, ETriggerType trigger, string operation = null, [PromptCommandParameterBody] object[] parameters = null)
        {
            if (_scriptTable.GetScript(contractHash.ToArray(), false) == null)
            {
                throw new ArgumentNullException("Contract not found");
            }

            var args = new ExecutionEngineArgs
            {
                ScriptTable = _scriptTable,
                Logger      = new ExecutionEngineLogger(ELogVerbosity.StepInto),
                Trigger     = trigger
            };

            var log = new StringBuilder();

            args.Logger.OnStepInto += context =>
            {
                log.AppendLine(context.NextInstruction.ToString());
            };

            using (var script = new ScriptBuilder())
                using (var vm = _vmFactory.Create(args))
                {
                    script.EmitMainPush(operation, parameters);
                    script.EmitAppCall(contractHash.ToArray());

                    vm.LoadScript(script);

                    var ret    = vm.Execute();
                    var result = new
                    {
                        vm.State,
                        Result = vm.ResultStack,
                        vm.ConsumedGas
                    };

                    _consoleHandler.WriteObject(result, PromptOutputStyle.json);
                }

            //_logger.LogDebug("Execution opcodes:" + Environment.NewLine + log.ToString());
        }
        public void TestSmartVote(byte[] script)
        {
            const string VoteId = "q01";

            // Create arguments

            var args = new ExecutionEngineArgs()
            {
                InteropService  = new DummyInteropService(),
                ScriptTable     = new DummyScriptTable(),
                Trigger         = ETriggerType.Application,
                MessageProvider = new DummyMessageProvider(),
                Logger          = new ExecutionEngineLogger(ELogVerbosity.None)
            };

            args.Logger.OnStepInto += (context) =>
            { Console.WriteLine(context.ToString()); };

            args.InteropService.OnLog += (sender, e) =>
            { Console.WriteLine("Log: " + e.Message); };
            args.InteropService.OnNotify += (sender, e) =>
            { Console.WriteLine("Notification: " + e.State.ToString()); };

            using (var arguments = new ScriptBuilder())
                using (var engine = CreateEngine(args))
                // for (int x = 0; x < 5000; x++) // Benchmark
                {
                    // Register proposal

                    Console.WriteLine("** Register proposal **" + Environment.NewLine);

                    arguments.EmitMainPush("register_proposal", new object[] { VoteId, "My proposal", new byte[20], new byte[20] });

                    engine.Clean(0);
                    int scriptIndex = engine.LoadScript(script);
                    engine.LoadScript(arguments);

                    // Execute

                    Assert.IsTrue(engine.Execute());

                    using (var item = engine.ResultStack.Pop <IntegerStackItem>())
                    {
                        Assert.AreEqual(item.Value, 0x01);
                    }

                    CheckClean(engine);
                    // Vote

                    Console.WriteLine(Environment.NewLine + "** Vote **" + Environment.NewLine);

                    arguments.Clear();
                    arguments.EmitMainPush("vote", new object[] { VoteId, new byte[20], 1 });

                    engine.Clean(1);
                    Assert.IsTrue(engine.LoadScript(scriptIndex));
                    engine.LoadScript(arguments);

                    // Execute

                    Assert.IsTrue(engine.Execute());

                    using (var item = engine.ResultStack.Pop <IntegerStackItem>())
                    {
                        Assert.AreEqual(item.Value, 0x01);
                    }

                    CheckClean(engine);

                    // Count

                    Console.WriteLine(Environment.NewLine + "** Count **" + Environment.NewLine);

                    arguments.Clear();
                    arguments.EmitMainPush("count", new object[] { VoteId });

                    engine.Clean(2);
                    Assert.IsTrue(engine.LoadScript(scriptIndex));
                    engine.LoadScript(arguments);

                    // Execute

                    Assert.IsTrue(engine.Execute());

                    using (var item = engine.ResultStack.Pop <IntegerStackItem>())
                    {
                        Assert.AreEqual(item.Value, 0x01);
                    }

                    CheckClean(engine);
                }
        }