Exemple #1
0
        public void TestContract_Update()
        {
            var engine = GetEngine(false, true);
            var script = new byte[1024 * 1024 + 1];

            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Update).Should().BeFalse();

            string manifestStr = new string(new char[ContractManifest.MaxLength + 1]);

            script = new byte[] { 0x01 };
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Update).Should().BeFalse();

            manifestStr = "";
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Update).Should().BeFalse();

            var manifest = ContractManifest.CreateDefault(script.ToScriptHash());

            byte[]  privkey = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                0x01,  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
            KeyPair key    = new KeyPair(privkey);
            ECPoint pubkey = key.PublicKey;

            byte[] signature = Crypto.Default.Sign(script.ToScriptHash().ToArray(), privkey, pubkey.EncodePoint(false).Skip(1).ToArray());
            manifest.Groups = new ContractGroup[]
            {
                new ContractGroup()
                {
                    PubKey    = pubkey,
                    Signature = signature
                }
            };
            var mockSnapshot = new Mock <Snapshot>();
            var state        = TestUtils.GetContract();

            state.Manifest.Features = ContractFeatures.HasStorage;
            var storageItem = new StorageItem
            {
                Value      = new byte[] { 0x01 },
                IsConstant = false
            };

            var storageKey = new StorageKey
            {
                ScriptHash = state.ScriptHash,
                Key        = new byte[] { 0x01 }
            };

            mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>(state.ScriptHash, state));
            mockSnapshot.SetupGet(p => p.Storages).Returns(new TestDataCache <StorageKey, StorageItem>(storageKey, storageItem));
            engine = new ApplicationEngine(TriggerType.Application, null, mockSnapshot.Object, 0);
            engine.LoadScript(state.Script);
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Update).Should().BeTrue();
        }
Exemple #2
0
        public void TestContract_Update()
        {
            var engine = GetEngine(false, true);
            var script = new byte[1024 * 1024 + 1];

            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeFalse();

            string manifestStr = new string(new char[ContractManifest.MaxLength + 1]);

            script = new byte[] { 0x01 };
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeFalse();

            manifestStr = "";
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeFalse();

            var manifest = ContractManifest.CreateDefault(script.ToScriptHash());

            byte[]  privkey = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                                0x01,  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
            KeyPair key    = new KeyPair(privkey);
            ECPoint pubkey = key.PublicKey;

            byte[] signature = Crypto.Sign(script.ToScriptHash().ToArray(), privkey, pubkey.EncodePoint(false).Skip(1).ToArray());
            manifest.Groups = new ContractGroup[]
            {
                new ContractGroup()
                {
                    PubKey    = pubkey,
                    Signature = signature
                }
            };
            var snapshot = Blockchain.Singleton.GetSnapshot();
            var state    = TestUtils.GetContract();

            state.Manifest.Features = ContractFeatures.HasStorage;
            var storageItem = new StorageItem
            {
                Value      = new byte[] { 0x01 },
                IsConstant = false
            };

            var storageKey = new StorageKey
            {
                Id  = state.Id,
                Key = new byte[] { 0x01 }
            };

            snapshot.Contracts.Add(state.ScriptHash, state);
            snapshot.Storages.Add(storageKey, storageItem);
            engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0);
            engine.LoadScript(state.Script);
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeTrue();
        }
Exemple #3
0
        public void TestExecutionEngine_GetCallingScriptHash()
        {
            // Test without

            var engine = GetEngine(true);

            InteropService.Invoke(engine, InteropService.Runtime.GetCallingScriptHash).Should().BeTrue();
            engine.CurrentContext.EvaluationStack.Pop().Should().Be(StackItem.Null);

            // Test real

            using ScriptBuilder scriptA = new ScriptBuilder();
            scriptA.Emit(OpCode.DROP); // Drop arguments
            scriptA.Emit(OpCode.DROP); // Drop method
            scriptA.EmitSysCall(InteropService.Runtime.GetCallingScriptHash);

            var contract = new ContractState()
            {
                Manifest = ContractManifest.CreateDefault(scriptA.ToArray().ToScriptHash()),
                Script   = scriptA.ToArray()
            };

            engine = GetEngine(true, true, false);
            engine.Snapshot.Contracts.Add(contract.ScriptHash, contract);

            using ScriptBuilder scriptB = new ScriptBuilder();
            scriptB.EmitAppCall(contract.ScriptHash, "");
            engine.LoadScript(scriptB.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());

            engine.ResultStack.Pop().GetSpan().ToHexString().Should().Be(scriptB.ToArray().ToScriptHash().ToArray().ToHexString());
        }
Exemple #4
0
        public void TestGetContractState()
        {
            byte[] script;
            using (var sb = new ScriptBuilder())
            {
                sb.EmitSysCall(InteropService.System_Runtime_GetInvocationCounter);
                script = sb.ToArray();
            }

            ContractState state = new ContractState
            {
                Script   = new byte[] { (byte)OpCode.DROP, (byte)OpCode.DROP }.Concat(script).ToArray(),
                Manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"))
            };

            JObject response = CreateResponse(1);

            response["result"] = state.ToJson();
            MockResponse(response.ToString());

            var result = rpc.GetContractState("17694b31cc7ee215cea2ded146e0b2b28768fc46");

            Assert.AreEqual(state.Script.ToHexString(), result.Script.ToHexString());
            Assert.AreEqual(state.Manifest.Abi.EntryPoint.Name, result.Manifest.Abi.EntryPoint.Name);
        }
Exemple #5
0
 internal static ContractState GetContract()
 {
     return(new ContractState
     {
         Script = new byte[] { 0x01, 0x01, 0x01, 0x01 },
         Manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"))
     });
 }
Exemple #6
0
        public void TestCanCall()
        {
            var temp = ContractManifest.CreateDefault(UInt160.Zero);

            temp.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" });

            Assert.AreEqual(true, temp.CanCall(ContractManifest.CreateDefault(UInt160.Zero), "AAA"));
        }
Exemple #7
0
        public void ParseFromJson_Default()
        {
            var json     = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[],""extra"":null}";
            var manifest = ContractManifest.Parse(json);

            Assert.AreEqual(manifest.ToString(), json);
            Assert.AreEqual(manifest.ToString(), ContractManifest.CreateDefault(UInt160.Zero).ToString());
            Assert.IsTrue(manifest.IsValid(UInt160.Zero));
        }
Exemple #8
0
 internal static ContractState GetContract(byte[] script)
 {
     return(new ContractState
     {
         Id = 1,
         Script = script,
         Manifest = ContractManifest.CreateDefault(script.ToScriptHash())
     });
 }
Exemple #9
0
 public void TestSetup()
 {
     manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"));
     contract = new ContractState
     {
         Script   = script,
         Manifest = manifest
     };
 }
Exemple #10
0
        public void TestClone()
        {
            var expected = ContractManifest.CreateDefault(UInt160.Zero);

            expected.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" });

            var actual = expected.Clone();

            Assert.AreEqual(actual.ToString(), expected.ToString());
        }
Exemple #11
0
        public void TestIsAllowed()
        {
            ContractManifest   contractManifest1   = ContractManifest.CreateDefault(UInt160.Zero);
            ContractPermission contractPermission1 = ContractPermission.DefaultPermission;

            contractPermission1.Contract = ContractPermissionDescriptor.Create(UInt160.Zero);
            Assert.AreEqual(true, contractPermission1.IsAllowed(contractManifest1, "AAA"));
            contractPermission1.Contract = ContractPermissionDescriptor.CreateWildcard();

            ContractManifest   contractManifest2   = ContractManifest.CreateDefault(UInt160.Zero);
            ContractPermission contractPermission2 = ContractPermission.DefaultPermission;

            contractPermission2.Contract = ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001"));
            Assert.AreEqual(false, contractPermission2.IsAllowed(contractManifest2, "AAA"));
            contractPermission2.Contract = ContractPermissionDescriptor.CreateWildcard();

            Random random3 = new Random();

            byte[] privateKey3 = new byte[32];
            random3.NextBytes(privateKey3);
            ECPoint          publicKey3        = ECCurve.Secp256r1.G * privateKey3;
            ContractManifest contractManifest3 = ContractManifest.CreateDefault(UInt160.Zero);

            contractManifest3.Groups = new ContractGroup[] { new ContractGroup()
                                                             {
                                                                 PubKey = publicKey3
                                                             } };
            ContractPermission contractPermission3 = ContractPermission.DefaultPermission;

            contractPermission3.Contract = ContractPermissionDescriptor.Create(publicKey3);
            Assert.AreEqual(true, contractPermission3.IsAllowed(contractManifest3, "AAA"));
            contractPermission3.Contract = ContractPermissionDescriptor.CreateWildcard();

            Random random4 = new Random();

            byte[] privateKey41 = new byte[32];
            random4.NextBytes(privateKey41);
            ECPoint publicKey41 = ECCurve.Secp256r1.G * privateKey41;

            byte[] privateKey42 = new byte[32];
            random4.NextBytes(privateKey42);
            ECPoint          publicKey42       = ECCurve.Secp256r1.G * privateKey42;
            ContractManifest contractManifest4 = ContractManifest.CreateDefault(UInt160.Zero);

            contractManifest4.Groups = new ContractGroup[] { new ContractGroup()
                                                             {
                                                                 PubKey = publicKey42
                                                             } };
            ContractPermission contractPermission4 = ContractPermission.DefaultPermission;

            contractPermission4.Contract = ContractPermissionDescriptor.Create(publicKey41);
            Assert.AreEqual(false, contractPermission4.IsAllowed(contractManifest4, "AAA"));
            contractPermission4.Contract = ContractPermissionDescriptor.CreateWildcard();
        }
Exemple #12
0
        public void ParseFromJson_Features()
        {
            var json     = @"{""groups"":[],""features"":{""storage"":true,""payable"":true},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
            var manifest = ContractManifest.Parse(json);

            Assert.AreEqual(manifest.ToJson().ToString(), json);

            var check = ContractManifest.CreateDefault(UInt160.Zero);

            check.Features = ContractFeatures.HasStorage | ContractFeatures.Payable;
            Assert.AreEqual(manifest.ToString(), check.ToString());
        }
Exemple #13
0
        public void ParseFromJson_SafeMethods()
        {
            var json     = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[""balanceOf""],""extra"":null}";
            var manifest = ContractManifest.Parse(json);

            Assert.AreEqual(manifest.ToString(), json);

            var check = ContractManifest.CreateDefault(UInt160.Zero);

            check.SafeMethods = WildcardContainer <string> .Create("balanceOf");

            Assert.AreEqual(manifest.ToString(), check.ToString());
        }
Exemple #14
0
        public void ParseFromJson_Groups()
        {
            var json     = @"{""groups"":[{""pubKey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[],""extra"":null}";
            var manifest = ContractManifest.Parse(json);

            Assert.AreEqual(manifest.ToString(), json);

            var check = ContractManifest.CreateDefault(UInt160.Zero);

            check.Groups = new ContractGroup[] { new ContractGroup()
                                                 {
                                                     PubKey = ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", ECCurve.Secp256r1), Signature = "41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141".HexToBytes()
                                                 } };
            Assert.AreEqual(manifest.ToString(), check.ToString());
        }
Exemple #15
0
        public void TestDeserializeAndSerialize()
        {
            MemoryStream stream   = new MemoryStream();
            BinaryWriter writer   = new BinaryWriter(stream);
            BinaryReader reader   = new BinaryReader(stream);
            var          expected = ContractManifest.CreateDefault(UInt160.Zero);

            expected.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" });

            expected.Serialize(writer);
            stream.Seek(0, SeekOrigin.Begin);
            var actual = ContractManifest.CreateDefault(UInt160.Zero);

            actual.Deserialize(reader);
            Assert.AreEqual(expected.SafeMethods.ToString(), actual.SafeMethods.ToString());
            Assert.AreEqual(expected.SafeMethods.Count, 1);
        }
Exemple #16
0
        protected NativeContract()
        {
            this.ServiceHash = ServiceName.ToInteropMethodHash();
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(ServiceHash);
                this.Script = sb.ToArray();
            }
            this.Hash     = Script.ToScriptHash();
            this.Manifest = ContractManifest.CreateDefault(this.Hash);
            List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>();
            List <string> safeMethods = new List <string>();

            foreach (MethodInfo method in GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = method.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                string name = attribute.Name ?? (method.Name.ToLower()[0] + method.Name.Substring(1));
                descriptors.Add(new ContractMethodDescriptor
                {
                    Name       = name,
                    ReturnType = attribute.ReturnType,
                    Parameters = attribute.ParameterTypes.Zip(attribute.ParameterNames, (t, n) => new ContractParameterDefinition {
                        Type = t, Name = n
                    }).ToArray()
                });
                if (attribute.SafeMethod)
                {
                    safeMethods.Add(name);
                }
                methods.Add(name, new ContractMethodMetadata
                {
                    Delegate        = (Func <ApplicationEngine, VMArray, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, VMArray, StackItem>), this),
                    Price           = attribute.Price,
                    AllowedTriggers = attribute.AllowedTriggers
                });
            }
            this.Manifest.Abi.Methods = descriptors.ToArray();
            this.Manifest.SafeMethods = WildCardContainer <string> .Create(safeMethods.ToArray());

            contracts.Add(this);
        }
Exemple #17
0
        public void TestDeployContract()
        {
            byte[] script;
            var manifest = ContractManifest.CreateDefault(new byte[1].ToScriptHash());
            manifest.Features = ContractFeatures.HasStorage | ContractFeatures.Payable;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(InteropService.Neo_Contract_Create, new byte[1], manifest.ToString());
                script = sb.ToArray();
            }

            UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter());

            ContractClient contractClient = new ContractClient(rpcClientMock.Object);
            var result = contractClient.DeployContract(new byte[1], manifest, keyPair1);

            Assert.IsNotNull(result);
        }
Exemple #18
0
        public void ParseFromJson_Permissions()
        {
            var json     = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safeMethods"":[]}";
            var manifest = ContractManifest.Parse(json);

            Assert.AreEqual(manifest.ToString(), json);

            var check = ContractManifest.CreateDefault(UInt160.Zero);

            check.Permissions = new[]
            {
                new ContractPermission()
                {
                    Contract = ContractPermissionDescriptor.Create(UInt160.Zero),
                    Methods  = WildCardContainer <string> .Create("method1", "method2")
                }
            };
            Assert.AreEqual(manifest.ToString(), check.ToString());
        }
Exemple #19
0
        public void TestContract_Create()
        {
            var engine = GetEngine(false, true);
            var script = new byte[1024 * 1024 + 1];

            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            string manifestStr = new string(new char[ContractManifest.MaxLength + 1]);

            script = new byte[] { 0x01 };
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            var manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"));

            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            manifest.Abi.Hash = script.ToScriptHash();
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeTrue();

            var snapshot = Blockchain.Singleton.GetSnapshot();
            var state    = TestUtils.GetContract();

            snapshot.Contracts.Add(state.ScriptHash, state);
            engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0);
            engine.LoadScript(new byte[] { 0x01 });
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(state.Script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();
        }
Exemple #20
0
        public void TestContract_Create()
        {
            var engine = GetEngine(false, true);
            var script = new byte[1024 * 1024 + 1];

            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            string manifestStr = new string(new char[ContractManifest.MaxLength + 1]);

            script = new byte[] { 0x01 };
            engine.CurrentContext.EvaluationStack.Push(manifestStr);
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            var manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"));

            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();

            manifest.Abi.Hash = script.ToScriptHash();
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeTrue();

            var mockSnapshot = new Mock <Snapshot>();
            var state        = TestUtils.GetContract();

            mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>(state.ScriptHash, state));
            engine = new ApplicationEngine(TriggerType.Application, null, mockSnapshot.Object, 0);
            engine.LoadScript(new byte[] { 0x01 });
            engine.CurrentContext.EvaluationStack.Push(manifest.ToString());
            engine.CurrentContext.EvaluationStack.Push(state.Script);
            InteropService.Invoke(engine, InteropService.Neo_Contract_Create).Should().BeFalse();
        }
Exemple #21
0
        public void Runtime_GetNotifications_Test()
        {
            UInt160 scriptHash2;
            var     snapshot = Blockchain.Singleton.GetSnapshot();

            using (var script = new ScriptBuilder())
            {
                // Drop arguments

                script.Emit(OpCode.NIP);

                // Notify method

                script.EmitSysCall(InteropService.Runtime.Notify);

                // Add return

                script.EmitPush(true);

                // Mock contract

                scriptHash2 = script.ToArray().ToScriptHash();

                snapshot.Contracts.Delete(scriptHash2);
                snapshot.Contracts.Add(scriptHash2, new Neo.Ledger.ContractState()
                {
                    Script   = script.ToArray(),
                    Manifest = ContractManifest.CreateDefault(scriptHash2),
                });
            }

            // Wrong length

            using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true))
                using (var script = new ScriptBuilder())
                {
                    // Retrive

                    script.EmitPush(1);
                    script.EmitSysCall(InteropService.Runtime.GetNotifications);

                    // Execute

                    engine.LoadScript(script.ToArray());

                    Assert.AreEqual(VMState.FAULT, engine.Execute());
                }

            // All test

            using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true))
                using (var script = new ScriptBuilder())
                {
                    // Notification 1 -> 13

                    script.EmitPush(13);
                    script.EmitSysCall(InteropService.Runtime.Notify);

                    // Call script

                    script.EmitAppCall(scriptHash2, "test");

                    // Drop return

                    script.Emit(OpCode.DROP);

                    // Receive all notifications

                    script.Emit(OpCode.PUSHNULL);
                    script.EmitSysCall(InteropService.Runtime.GetNotifications);

                    // Execute

                    engine.LoadScript(script.ToArray());
                    var currentScriptHash = engine.EntryScriptHash;

                    Assert.AreEqual(VMState.HALT, engine.Execute());
                    Assert.AreEqual(1, engine.ResultStack.Count);
                    Assert.AreEqual(2, engine.Notifications.Count);

                    Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(VM.Types.Array));

                    var array = (VM.Types.Array)engine.ResultStack.Pop();

                    // Check syscall result

                    AssertNotification(array[1], scriptHash2, "test");
                    AssertNotification(array[0], currentScriptHash, 13);

                    // Check notifications

                    Assert.AreEqual(scriptHash2, engine.Notifications[1].ScriptHash);
                    Assert.AreEqual("test", engine.Notifications[1].State.GetString());

                    Assert.AreEqual(currentScriptHash, engine.Notifications[0].ScriptHash);
                    Assert.AreEqual(13, engine.Notifications[0].State.GetBigInteger());
                }

            // Script notifications

            using (var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true))
                using (var script = new ScriptBuilder())
                {
                    // Notification 1 -> 13

                    script.EmitPush(13);
                    script.EmitSysCall(InteropService.Runtime.Notify);

                    // Call script

                    script.EmitAppCall(scriptHash2, "test");

                    // Drop return

                    script.Emit(OpCode.DROP);

                    // Receive all notifications

                    script.EmitPush(scriptHash2.ToArray());
                    script.EmitSysCall(InteropService.Runtime.GetNotifications);

                    // Execute

                    engine.LoadScript(script.ToArray());
                    var currentScriptHash = engine.EntryScriptHash;

                    Assert.AreEqual(VMState.HALT, engine.Execute());
                    Assert.AreEqual(1, engine.ResultStack.Count);
                    Assert.AreEqual(2, engine.Notifications.Count);

                    Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(VM.Types.Array));

                    var array = (VM.Types.Array)engine.ResultStack.Pop();

                    // Check syscall result

                    AssertNotification(array[0], scriptHash2, "test");

                    // Check notifications

                    Assert.AreEqual(scriptHash2, engine.Notifications[1].ScriptHash);
                    Assert.AreEqual("test", engine.Notifications[1].State.GetString());

                    Assert.AreEqual(currentScriptHash, engine.Notifications[0].ScriptHash);
                    Assert.AreEqual(13, engine.Notifications[0].State.GetBigInteger());
                }

            // Clean storage

            snapshot.Contracts.Delete(scriptHash2);
        }
        public void Test_CreateCallDestroy()
        {
            // Create

            byte[] script;
            using (var scriptBuilder = new ScriptBuilder())
            {
                // Drop arguments

                scriptBuilder.Emit(VM.OpCode.DROP);
                scriptBuilder.Emit(VM.OpCode.DROP);

                // Return 123

                scriptBuilder.EmitPush(123);
                script = scriptBuilder.ToArray();
            }

            var manifest = ContractManifest.CreateDefault(script.ToScriptHash());

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());

            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("create", script, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsTrue(item.Type == VM.Types.StackItemType.InteropInterface);
            var ledger = (item as InteropInterface).GetInterface <Ledger.ContractState>();

            Assert.AreEqual(manifest.Hash, ledger.ScriptHash);

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), Null.Null, Null.Null);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetBigInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0, item.GetByteLength());

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
        public void Test_Update()
        {
            // Create

            byte[] scriptUpdate;
            using (var scriptBuilder = new ScriptBuilder())
            {
                // Drop arguments

                scriptBuilder.Emit(VM.OpCode.DROP);
                scriptBuilder.Emit(VM.OpCode.DROP);

                // Return 124

                scriptBuilder.EmitPush(123);
                scriptBuilder.Emit(VM.OpCode.INC);
                scriptUpdate = scriptBuilder.ToArray();
            }

            var manifestUpdate = ContractManifest.CreateDefault(scriptUpdate.ToScriptHash());

            byte[] script;
            using (var scriptBuilder = new ScriptBuilder())
            {
                // Drop arguments

                scriptBuilder.Emit(VM.OpCode.DROP);
                scriptBuilder.Emit(VM.OpCode.DROP);

                // Return 123

                scriptBuilder.EmitPush(123);

                // Update

                scriptBuilder.EmitSysCall(InteropService.Neo_Contract_Update, scriptUpdate, manifestUpdate.ToJson().ToString());
                script = scriptBuilder.ToArray();
            }

            var manifest = ContractManifest.CreateDefault(script.ToScriptHash());

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("Call", manifest.Hash.ToArray());

            Assert.AreEqual(VMState.FAULT, _engine.State);

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("Call", manifestUpdate.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("Create", script, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(InteropInterface <Ledger.ContractState>));
            Assert.AreEqual(manifest.Hash,
                            ((InteropInterface <Ledger.ContractState>)item).GetInterface <Ledger.ContractState>().ScriptHash);

            // Call & Update

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("Call", manifest.Hash.ToArray(), Null.Null, Null.Null);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(ByteArray));
            Assert.AreEqual(123, item.GetBigInteger());

            // Call Again

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("Call", manifestUpdate.Hash.ToArray(), Null.Null, Null.Null);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(124, item.GetBigInteger());

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("Call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
        }
Exemple #24
0
        public void TestGetHash()
        {
            var temp = ContractManifest.CreateDefault(UInt160.Zero);

            Assert.AreEqual(temp.Abi.Hash, temp.Hash);
        }
Exemple #25
0
        public void TestGetSize()
        {
            var temp = ContractManifest.CreateDefault(UInt160.Zero);

            Assert.AreEqual(366, temp.Size);
        }