Пример #1
0
        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);
                }
        }