public async Task <IExecutive> GetExecutiveAsync(Address contractAddress, Hash chainId) { var pool = GetPoolFor(contractAddress); if (pool.TryTake(out var executive)) { return(executive); } // get registration var reg = await _smartContractManager.GetAsync(contractAddress); // get runner var runner = _smartContractRunnerFactory.GetRunner(reg.Category); if (runner == null) { throw new NotSupportedException($"Runner for category {reg.Category} is not registered."); } // get account dataprovider var dataProvider = DataProvider.GetRootDataProvider(chainId, contractAddress); dataProvider.StateStore = _stateStore; // run smartcontract executive info and return executive executive = await runner.RunAsync(reg); executive.SetStateStore(_stateStore); executive.SetSmartContractContext(new SmartContractContext() { ChainId = chainId, ContractAddress = contractAddress, DataProvider = dataProvider, SmartContractService = this }); return(executive); }
public async Task TestDepolyContract() { var chainId = Hash.Generate(); var runner = _smartContractRunnerFactory.GetRunner(0); var contractCType = typeof(TestContractC); var contractBType = typeof(TestContractB); var contractAType = typeof(TestContractA); var contractCTemplate = runner.ExtractMetadata(contractCType); var contractBTemplate = runner.ExtractMetadata(contractBType); var contractATemplate = runner.ExtractMetadata(contractAType); var addrA = Address.FromString("TestContractA"); var addrB = Address.FromString("TestContractB"); var addrC = Address.FromString("TestContractC"); Console.WriteLine(addrC); await _functionMetadataService.DeployContract(chainId, addrC, contractCTemplate); Assert.Equal(new FunctionMetadata( new HashSet <string>(), new HashSet <Resource>(new [] { new Resource(addrC.Value.ToByteArray().ToHex() + ".resource4", DataAccessMode.AccountSpecific) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrC.DumpHex() + ".Func0"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(), new HashSet <Resource>(new [] { new Resource(addrC.Value.ToByteArray().ToHex() + ".resource5", DataAccessMode.ReadOnlyAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrC.DumpHex() + ".Func1"))); await _functionMetadataService.DeployContract(chainId, addrB, contractBTemplate); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrC.DumpHex() + ".Func1" }), new HashSet <Resource>(new [] { new Resource(addrC.Value.ToByteArray().ToHex() + ".resource5", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrB.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.AccountSpecific), })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrB.DumpHex() + ".Func0"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(), new HashSet <Resource>(new [] { new Resource(addrB.Value.ToByteArray().ToHex() + ".resource3", DataAccessMode.ReadOnlyAccountSharing), })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrB.DumpHex() + ".Func1"))); await _functionMetadataService.DeployContract(chainId, addrA, contractATemplate); Assert.Equal(new FunctionMetadata( new HashSet <string>(), new HashSet <Resource>()), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func0(int)"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrA.DumpHex() + ".Func1" }), new HashSet <Resource>(new [] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource0", DataAccessMode.AccountSpecific), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func0"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrA.DumpHex() + ".Func2" }), new HashSet <Resource>(new[] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func1"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(), new HashSet <Resource>(new[] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), (await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func2")))); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrA.DumpHex() + ".Func0", addrB.DumpHex() + ".Func0", addrC.DumpHex() + ".Func0" }), new HashSet <Resource>(new[] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource0", DataAccessMode.AccountSpecific), new Resource(addrB.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.AccountSpecific), new Resource(addrC.Value.ToByteArray().ToHex() + ".resource5", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrC.Value.ToByteArray().ToHex() + ".resource4", DataAccessMode.AccountSpecific), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func3"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrA.DumpHex() + ".Func2" }), new HashSet <Resource>(new[] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func4"))); Assert.Equal(new FunctionMetadata( new HashSet <string>(new [] { addrA.DumpHex() + ".Func3", addrB.DumpHex() + ".Func1" }), new HashSet <Resource>(new[] { new Resource(addrA.Value.ToByteArray().ToHex() + ".resource0", DataAccessMode.AccountSpecific), new Resource(addrB.Value.ToByteArray().ToHex() + ".resource3", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrB.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.AccountSpecific), new Resource(addrC.Value.ToByteArray().ToHex() + ".resource5", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrC.Value.ToByteArray().ToHex() + ".resource4", DataAccessMode.AccountSpecific), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource(addrA.Value.ToByteArray().ToHex() + ".resource2", DataAccessMode.ReadWriteAccountSharing) })), await _dataStore.GetAsync <FunctionMetadata>(DataPath.CalculatePointerForMetadata(chainId, addrA.DumpHex() + ".Func5"))); var callGraph = new SerializedCallGraph { Vertices = { addrC.DumpHex() + ".Func0", addrC.DumpHex() + ".Func1", addrB.DumpHex() + ".Func0", addrB.DumpHex() + ".Func1", addrA.DumpHex() + ".Func0(int)", addrA.DumpHex() + ".Func0", addrA.DumpHex() + ".Func1", addrA.DumpHex() + ".Func2", addrA.DumpHex() + ".Func3", addrA.DumpHex() + ".Func4", addrA.DumpHex() + ".Func5" }, Edges = { new GraphEdge { Source = addrB.DumpHex() + ".Func0", Target = addrC.DumpHex() + ".Func1" }, new GraphEdge { Source = addrA.DumpHex() + ".Func0", Target = addrA.DumpHex() + ".Func1" }, new GraphEdge { Source = addrA.DumpHex() + ".Func1", Target = addrA.DumpHex() + ".Func2" }, new GraphEdge { Source = addrA.DumpHex() + ".Func3", Target = addrB.DumpHex() + ".Func0" }, new GraphEdge { Source = addrA.DumpHex() + ".Func3", Target = addrA.DumpHex() + ".Func0" }, new GraphEdge { Source = addrA.DumpHex() + ".Func3", Target = addrC.DumpHex() + ".Func0" }, new GraphEdge { Source = addrA.DumpHex() + ".Func4", Target = addrA.DumpHex() + ".Func2" }, new GraphEdge { Source = addrA.DumpHex() + ".Func5", Target = addrB.DumpHex() + ".Func1" }, new GraphEdge { Source = addrA.DumpHex() + ".Func5", Target = addrA.DumpHex() + ".Func3" } } }; Assert.Equal(callGraph, await _dataStore.GetAsync <SerializedCallGraph>(chainId.OfType(HashType.CallingGraph))); }