예제 #1
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
                });
            }
            this.Manifest.Abi.Methods = descriptors.ToArray();
            this.Manifest.SafeMethods = WildCardContainer <string> .Create(safeMethods.ToArray());

            contracts.Add(this);
        }
예제 #2
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());
        }
예제 #3
0
        public void TestGetEnumerator()
        {
            string[] s = null;
            IReadOnlyList <string>     rs        = (IReadOnlyList <string>) new string[0];
            WildCardContainer <string> container = WildCardContainer <string> .Create(s);

            IEnumerator <string> enumerator = container.GetEnumerator();

            enumerator.Should().Be(rs.GetEnumerator());

            s         = new string[] { "hello", "world" };
            container = WildCardContainer <string> .Create(s);

            enumerator = container.GetEnumerator();
            foreach (string _ in s)
            {
                enumerator.MoveNext();
                enumerator.Current.Should().Be(_);
            }
        }
예제 #4
0
        public void TestContract_Call()
        {
            var mockSnapshot = new Mock <Snapshot>();
            var state        = TestUtils.GetContract();

            state.Manifest.Features = ContractFeatures.HasStorage;
            byte[] method = Encoding.UTF8.GetBytes("method");
            byte[] args   = new byte[0];
            mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>(state.ScriptHash, state));
            var engine = new ApplicationEngine(TriggerType.Application, null, mockSnapshot.Object, 0);

            engine.LoadScript(new byte[] { 0x01 });

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeTrue();
            engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString().Should().Be(method.ToHexString());
            engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString().Should().Be(args.ToHexString());

            state.Manifest.Permissions[0].Methods = WildCardContainer <string> .Create("a");

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeFalse();
            state.Manifest.Permissions[0].Methods = WildCardContainer <string> .CreateWildcard();

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray());
            InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeTrue();

            engine.CurrentContext.EvaluationStack.Push(args);
            engine.CurrentContext.EvaluationStack.Push(method);
            engine.CurrentContext.EvaluationStack.Push(UInt160.Zero.ToArray());
            InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeFalse();
        }