public void TestFromJson() { JString jstring = new JString("*"); WildcardContainer <string> s = WildcardContainer <string> .FromJson(jstring, u => u.AsString()); s.Should().BeEmpty(); jstring = new JString("hello world"); Action action = () => WildcardContainer <string> .FromJson(jstring, u => u.AsString()); action.Should().Throw <FormatException>(); JObject alice = new JObject(); alice["name"] = "alice"; alice["age"] = 30; JArray jarray = new JArray { alice }; WildcardContainer <string> r = WildcardContainer <string> .FromJson(jarray, u => u.AsString()); r[0].Should().Be("{\"name\":\"alice\",\"age\":30}"); JBoolean jbool = new JBoolean(); action = () => WildcardContainer <string> .FromJson(jbool, u => u.AsString()); action.Should().Throw <FormatException>(); }
public void TestContract_Call() { var snapshot = TestBlockchain.GetTestSnapshot(); string method = "method"; var args = new VM.Types.Array { 0, 1 }; var state = TestUtils.GetContract(method, args.Count); var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot); engine.LoadScript(new byte[] { 0x01 }); engine.Snapshot.AddContract(state.Hash, state); engine.CallContract(state.Hash, method, CallFlags.All, args); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]); state.Manifest.Permissions[0].Methods = WildcardContainer <string> .Create("a"); engine.Snapshot.DeleteContract(state.Hash); engine.Snapshot.AddContract(state.Hash, state); Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(state.Hash, method, CallFlags.All, args)); state.Manifest.Permissions[0].Methods = WildcardContainer <string> .CreateWildcard(); engine.Snapshot.DeleteContract(state.Hash); engine.Snapshot.AddContract(state.Hash, state); engine.CallContract(state.Hash, method, CallFlags.All, args); engine.Snapshot.DeleteContract(state.Hash); engine.Snapshot.AddContract(state.Hash, state); Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(UInt160.Zero, method, CallFlags.All, args)); }
public async Task TestDeployContract() { byte[] script; var manifest = new ContractManifest() { Permissions = new[] { ContractPermission.DefaultPermission }, Abi = new ContractAbi() { Events = new ContractEventDescriptor[0], Methods = new ContractMethodDescriptor[0] }, Groups = new ContractGroup[0], Trusts = WildcardContainer <UInt160> .Create(), SupportedStandards = new string[] { "NEP-10" }, Extra = null, }; using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", true, new byte[1], manifest.ToString()); script = sb.ToArray(); } UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter()); ContractClient contractClient = new ContractClient(rpcClientMock.Object); var result = await contractClient.CreateDeployContractTxAsync(new byte[1], manifest, keyPair1); Assert.IsNotNull(result); }
public static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism public static ContractManifest CreateDefaultManifest() { return(new ContractManifest() { Name = "testManifest", Groups = new ContractGroup[0], SupportedStandards = Array.Empty <string>(), Abi = new ContractAbi() { Events = new ContractEventDescriptor[0], Methods = new[] { new ContractMethodDescriptor { Name = "testMethod", Parameters = new ContractParameterDefinition[0], ReturnType = ContractParameterType.Void, Offset = 0, Safe = true } } }, Permissions = new[] { ContractPermission.DefaultPermission }, Trusts = WildcardContainer <UInt160> .Create(), Extra = null }); }
public void TestContract_Call() { var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract("method"); state.Manifest.Features = ContractFeatures.HasStorage; string method = "method"; var args = new VM.Types.Array { 0, 1 }; snapshot.Contracts.Add(state.ScriptHash, state); var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(new byte[] { 0x01 }); engine.CallContract(state.ScriptHash, method, args); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]); state.Manifest.Permissions[0].Methods = WildcardContainer <string> .Create("a"); Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(state.ScriptHash, method, args)); state.Manifest.Permissions[0].Methods = WildcardContainer <string> .CreateWildcard(); engine.CallContract(state.ScriptHash, method, args); Assert.ThrowsException <InvalidOperationException>(() => engine.CallContract(UInt160.Zero, method, args)); }
public void TestGetItem() { string[] s = new string[] { "hello", "world" }; WildcardContainer <string> container = WildcardContainer <string> .Create(s); container[0].Should().Be("hello"); container[1].Should().Be("world"); }
public void TestCanCall() { var temp = TestUtils.CreateDefaultManifest(UInt160.Zero); temp.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" }); Assert.AreEqual(true, temp.CanCall(TestUtils.CreateDefaultManifest(UInt160.Zero), "AAA")); }
protected NativeContract() { using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitPush(Name); sb.EmitSysCall(ApplicationEngine.Neo_Native_Call); this.Script = sb.ToArray(); } this.Hash = Script.ToScriptHash(); 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.RequiredCallFlags.HasFlag(CallFlags.AllowModifyStates)) { safeMethods.Add(name); } methods.Add(name, new ContractMethodMetadata { Delegate = (Func <ApplicationEngine, Array, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, Array, StackItem>), this), Price = attribute.Price, RequiredCallFlags = attribute.RequiredCallFlags }); } this.Manifest = new ContractManifest { Permissions = new[] { ContractPermission.DefaultPermission }, Abi = new ContractAbi() { Hash = Hash, Events = new ContractEventDescriptor[0], Methods = descriptors.ToArray() }, Features = ContractFeatures.NoProperty, Groups = new ContractGroup[0], SafeMethods = WildcardContainer <string> .Create(safeMethods.ToArray()), Trusts = WildcardContainer <UInt160> .Create(), Extra = null, }; contractsList.Add(this); contractsNameDictionary.Add(Name, this); contractsHashDictionary.Add(Hash, this); }
protected NativeContract() { List <ContractMethodMetadata> descriptors = new List <ContractMethodMetadata>(); foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>(); if (attribute is null) { continue; } descriptors.Add(new ContractMethodMetadata(member, attribute)); } descriptors = descriptors.OrderBy(p => p.Name).ThenBy(p => p.Parameters.Length).ToList(); byte[] script; using (ScriptBuilder sb = new ScriptBuilder()) { foreach (ContractMethodMetadata method in descriptors) { method.Descriptor.Offset = sb.Offset; sb.EmitPush(0); //version methods.Add(sb.Offset, method); sb.EmitSysCall(ApplicationEngine.System_Contract_CallNative); sb.Emit(OpCode.RET); } script = sb.ToArray(); } this.Nef = new NefFile { Compiler = "neo-core-v3.0", Tokens = Array.Empty <MethodToken>(), Script = script }; this.Nef.CheckSum = NefFile.ComputeChecksum(Nef); this.Hash = Helper.GetContractHash(UInt160.Zero, 0, Name); this.Manifest = new ContractManifest { Name = Name, Groups = Array.Empty <ContractGroup>(), SupportedStandards = Array.Empty <string>(), Abi = new ContractAbi() { Events = Array.Empty <ContractEventDescriptor>(), Methods = descriptors.Select(p => p.Descriptor).ToArray() }, Permissions = new[] { ContractPermission.DefaultPermission }, Trusts = WildcardContainer <UInt160> .Create(), Extra = null }; if (ProtocolSettings.Default.NativeActivations.TryGetValue(Name, out uint activationIndex)) { this.ActiveBlockIndex = activationIndex; } contractsList.Add(this); contractsDictionary.Add(Hash, this); }
public void TestClone() { var expected = TestUtils.CreateDefaultManifest(UInt160.Zero); expected.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" }); var actual = expected.Clone(); Assert.AreEqual(actual.ToString(), expected.ToString()); }
public void GetHashData() { var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot); Assert.ThrowsException <ArgumentException>(() => new MethodCallback(engine, UInt160.Zero, "_test")); var contract = new ContractState() { Manifest = new ContractManifest() { Permissions = new ContractPermission[0], Groups = new ContractGroup[0], Trusts = WildcardContainer <UInt160> .Create(), Abi = new ContractAbi() { Methods = new ContractMethodDescriptor[] { new ContractMethodDescriptor() { Name = "test", Parameters = new ContractParameterDefinition[0] } }, Events = new ContractEventDescriptor[0], }, }, Script = new byte[] { 1, 2, 3 }, Hash = new byte[] { 1, 2, 3 }.ToScriptHash() }; engine.LoadScript(contract.Script); engine.Snapshot.AddContract(contract.Hash, contract); Assert.ThrowsException <InvalidOperationException>(() => new MethodCallback(engine, contract.Hash, "test")); contract.Manifest.Permissions = new ContractPermission[] { new ContractPermission() { Contract = ContractPermissionDescriptor.Create(contract.Hash), Methods = WildcardContainer <string> .Create("test") } }; var data = new MethodCallback(engine, contract.Hash, "test"); Assert.AreEqual(0, engine.CurrentContext.EvaluationStack.Count); var array = new VM.Types.Array(); data.LoadContext(engine, array); Assert.AreEqual(3, engine.CurrentContext.EvaluationStack.Count); Assert.AreEqual("9bc4860bb936abf262d7a51f74b4304833fee3b2", engine.Pop <VM.Types.ByteString>().GetSpan().ToHexString()); Assert.AreEqual("test", engine.Pop <VM.Types.ByteString>().GetString()); Assert.IsTrue(engine.Pop() == array); }
protected NativeContract() { using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitPush(Name); sb.EmitSysCall(ApplicationEngine.Neo_Native_Call); this.Script = sb.ToArray(); } this.Hash = Script.ToScriptHash(); List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>(); List <string> safeMethods = new List <string>(); foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>(); if (attribute is null) { continue; } ContractMethodMetadata metadata = new ContractMethodMetadata(member, attribute); descriptors.Add(new ContractMethodDescriptor { Name = metadata.Name, ReturnType = ToParameterType(metadata.Handler.ReturnType), Parameters = metadata.Parameters.Select(p => new ContractParameterDefinition { Type = ToParameterType(p.Type), Name = p.Name }).ToArray() }); if (!attribute.RequiredCallFlags.HasFlag(CallFlags.AllowModifyStates)) { safeMethods.Add(metadata.Name); } methods.Add(metadata.Name, metadata); } this.Manifest = new ContractManifest { Groups = System.Array.Empty <ContractGroup>(), Features = ContractFeatures.NoProperty, SupportedStandards = new string[0], Abi = new ContractAbi() { Hash = Hash, Events = System.Array.Empty <ContractEventDescriptor>(), Methods = descriptors.ToArray() }, Permissions = new[] { ContractPermission.DefaultPermission }, Trusts = WildcardContainer <UInt160> .Create(), SafeMethods = WildcardContainer <string> .Create(safeMethods.ToArray()), Extra = null }; contractsList.Add(this); contractsNameDictionary.Add(Name, this); contractsHashDictionary.Add(Hash, this); }
public void TestGetCount() { string[] s = new string[] { "hello", "world" }; WildcardContainer <string> container = WildcardContainer <string> .Create(s); container.Count.Should().Be(2); s = null; container = WildcardContainer <string> .Create(s); container.Count.Should().Be(0); }
public void ParseFromJson_SafeMethods() { var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[""balanceOf""],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = TestUtils.CreateDefaultManifest(UInt160.Zero); check.SafeMethods = WildcardContainer <string> .Create("balanceOf"); Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void ParseFromJson_Trust() { 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"":[""0x0000000000000000000000000000000000000001""],""safeMethods"":[],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = ContractManifest.CreateDefault(UInt160.Zero); check.Trusts = WildcardContainer <UInt160> .Create(UInt160.Parse("0x0000000000000000000000000000000000000001")); Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void ParseFromJson_SafeMethods() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[""balanceOf""],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = TestUtils.CreateDefaultManifest(); check.SafeMethods = WildcardContainer <string> .Create("balanceOf"); Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void ParseFromJson_Trust() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safemethods"":[],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = TestUtils.CreateDefaultManifest(UInt160.Zero); check.Trusts = WildcardContainer <UInt160> .Create(UInt160.Parse("0x0000000000000000000000000000000000000001")); Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void ParseFromJson_Trust() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToJson().ToString(), json); var check = TestUtils.CreateDefaultManifest(); check.Trusts = WildcardContainer <ContractPermissionDescriptor> .Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001"))); Assert.AreEqual(manifest.ToJson().ToString(), check.ToJson().ToString()); }
public void TestIEnumerableGetEnumerator() { string[] s = new string[] { "hello", "world" }; WildcardContainer <string> container = WildcardContainer <string> .Create(s); IEnumerable enumerable = container; var enumerator = enumerable.GetEnumerator(); foreach (string _ in s) { enumerator.MoveNext(); enumerator.Current.Should().Be(_); } }
public void TestCanCall() { var temp = new ContractState() { Manifest = TestUtils.CreateDefaultManifest() }; temp.Manifest.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" }); Assert.AreEqual(true, temp.CanCall(new ContractState() { Hash = UInt160.Zero, Manifest = TestUtils.CreateDefaultManifest() }, "AAA")); }
public static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism public static ContractManifest CreateDefaultManifest() { return(new ContractManifest() { Name = "testManifest", Groups = new ContractGroup[0], SupportedStandards = Array.Empty <string>(), Abi = new ContractAbi() { Events = new ContractEventDescriptor[0], Methods = new ContractMethodDescriptor[0] }, Permissions = new[] { ContractPermission.DefaultPermission }, Trusts = WildcardContainer <UInt160> .Create(), Extra = null }); }
public void TestDeserializeAndSerialize() { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); BinaryReader reader = new BinaryReader(stream); var expected = TestUtils.CreateDefaultManifest(UInt160.Zero); expected.SafeMethods = WildcardContainer <string> .Create(new string[] { "AAA" }); expected.Serialize(writer); stream.Seek(0, SeekOrigin.Begin); var actual = TestUtils.CreateDefaultManifest(UInt160.Zero); actual.Deserialize(reader); Assert.AreEqual(expected.SafeMethods.ToString(), actual.SafeMethods.ToString()); Assert.AreEqual(expected.SafeMethods.Count, 1); }
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, Array, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, Array, StackItem>), this), Price = attribute.Price, RequiredCallFlags = attribute.SafeMethod ? CallFlags.None : CallFlags.AllowModifyStates }); } this.Manifest.Abi.Methods = descriptors.ToArray(); this.Manifest.SafeMethods = WildcardContainer <string> .Create(safeMethods.ToArray()); contracts.Add(this); }
public static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism public static ContractManifest CreateDefaultManifest(UInt160 hash) { return(new ContractManifest() { Permissions = new[] { ContractPermission.DefaultPermission }, Abi = new ContractAbi() { Hash = hash, Events = new ContractEventDescriptor[0], Methods = new ContractMethodDescriptor[0] }, Features = ContractFeatures.NoProperty, Groups = new ContractGroup[0], SafeMethods = WildcardContainer <string> .Create(), Trusts = WildcardContainer <UInt160> .Create(), Extra = null, }); }
public void ParseFromJson_Permissions() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = TestUtils.CreateDefaultManifest(UInt160.Zero); check.Permissions = new[] { new ContractPermission() { Contract = ContractPermissionDescriptor.Create(UInt160.Zero), Methods = WildcardContainer <string> .Create("method1", "method2") } }; Assert.AreEqual(manifest.ToString(), check.ToString()); }
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(_); } }
public void TestContract_Call() { var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract("method"); state.Manifest.Features = ContractFeatures.HasStorage; byte[] method = Encoding.UTF8.GetBytes("method"); var args = new VM.Types.Array { 0, 1 }; snapshot.Contracts.Add(state.ScriptHash, state); var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 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.Contract.Call).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]); engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]); 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.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.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.Contract.Call).Should().BeFalse(); }
public async Task TestDeployContract() { byte[] script; var manifest = new ContractManifest() { Permissions = new[] { ContractPermission.DefaultPermission }, Abi = new ContractAbi() { Hash = new byte[1].ToScriptHash(), Events = new ContractEventDescriptor[0], Methods = new ContractMethodDescriptor[0] }, Groups = new ContractGroup[0], SafeMethods = WildcardContainer <string> .Create(), Trusts = WildcardContainer <UInt160> .Create(), SupportedStandards = new string[] { "NEP-10" }, Extra = null, }; manifest.Features = ContractFeatures.HasStorage | ContractFeatures.Payable; using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitSysCall(ApplicationEngine.System_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 = await contractClient.CreateDeployContractTxAsync(new byte[1], manifest, keyPair1); Assert.IsNotNull(result); }
public void GetContract() { var contract = new ContractState() { Hash = new byte[] { 0x01, 0x02, 0x03 }.ToScriptHash(), Nef = new NefFile() { Script = new byte[] { 0x01, 0x02, 0x03 }, Compiler = "neon-test", Tokens = System.Array.Empty <MethodToken>() }, Manifest = new ContractManifest() { Name = "Name", SupportedStandards = System.Array.Empty <string>(), Groups = System.Array.Empty <ContractGroup>(), Trusts = WildcardContainer <ContractPermissionDescriptor> .Create(), Permissions = System.Array.Empty <ContractPermission>(), Abi = new ContractAbi() { Methods = System.Array.Empty <ContractMethodDescriptor>(), Events = System.Array.Empty <ContractEventDescriptor>(), }, } }; _engine.Snapshot.ContractAdd(contract); // Not found _engine.Reset(); var result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(UInt160.Zero.ToArray()), new ByteString(new byte[20])); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Null)); // Found + Manifest _engine.Reset(); result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Manifest"))); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Struct)); var ritem = new ContractManifest(); ((IInteroperable)ritem).FromStackItem(item); Assert.AreEqual(contract.Manifest.ToString(), ritem.ToString()); // Found + UpdateCounter _engine.Reset(); result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("UpdateCounter"))); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(0, item.GetInteger()); // Found + Id _engine.Reset(); result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Id"))); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(0, item.GetInteger()); // Found + Hash _engine.Reset(); result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Hash"))); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); item = result.Pop(); Assert.IsInstanceOfType(item, typeof(ByteString)); CollectionAssert.AreEqual(contract.Hash.ToArray(), item.GetSpan().ToArray()); // Found + Uknown property _engine.Reset(); _ = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("ASD"))); Assert.AreEqual(VMState.FAULT, _engine.State); }
protected NativeContract() { this.Id = --id_counter; byte[] script; using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitPush(Id); sb.EmitSysCall(ApplicationEngine.System_Contract_CallNative); script = sb.ToArray(); } this.Nef = new NefFile { Compiler = nameof(ScriptBuilder), Version = "3.0", Tokens = System.Array.Empty <MethodToken>(), Script = script }; this.Nef.CheckSum = NefFile.ComputeChecksum(Nef); this.Hash = Helper.GetContractHash(UInt160.Zero, script); List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>(); foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>(); if (attribute is null) { continue; } ContractMethodMetadata metadata = new ContractMethodMetadata(member, attribute); descriptors.Add(new ContractMethodDescriptor { Name = metadata.Name, ReturnType = ToParameterType(metadata.Handler.ReturnType), Parameters = metadata.Parameters.Select(p => new ContractParameterDefinition { Type = ToParameterType(p.Type), Name = p.Name }).ToArray(), Safe = (attribute.RequiredCallFlags & ~CallFlags.ReadOnly) == 0 }); methods.Add(metadata.Name, metadata); } this.Manifest = new ContractManifest { Name = Name, Groups = System.Array.Empty <ContractGroup>(), SupportedStandards = new string[0], Abi = new ContractAbi() { Events = System.Array.Empty <ContractEventDescriptor>(), Methods = descriptors.ToArray() }, Permissions = new[] { ContractPermission.DefaultPermission }, Trusts = WildcardContainer <UInt160> .Create(), Extra = null }; if (ProtocolSettings.Default.NativeActivations.TryGetValue(Name, out uint activationIndex)) { this.ActiveBlockIndex = activationIndex; } contractsList.Add(this); contractsIdDictionary.Add(Id, this); contractsHashDictionary.Add(Hash, this); }