private void Initialize() { NewStorage(); var transactionManager = new TransactionManager(_dataStore, _logger); var transactionTraceManager = new TransactionTraceManager(_dataStore); _functionMetadataService = new FunctionMetadataService(_dataStore, _logger); var chainManagerBasic = new ChainManagerBasic(_dataStore); ChainService = new ChainService(chainManagerBasic, new BlockManagerBasic(_dataStore), transactionManager, transactionTraceManager, _dataStore, StateStore); _smartContractRunnerFactory = new SmartContractRunnerFactory(); var runner = new SmartContractRunner("../../../../AElf.Runtime.CSharp.Tests.TestContract/bin/Debug/netstandard2.0/"); _smartContractRunnerFactory.AddRunner(0, runner); _chainCreationService = new ChainCreationService(ChainService, new SmartContractService(new SmartContractManager(_dataStore), _smartContractRunnerFactory, StateStore, _functionMetadataService), _logger); SmartContractManager = new SmartContractManager(_dataStore); Task.Factory.StartNew(async() => { await Init(); }).Unwrap().Wait(); SmartContractService = new SmartContractService(SmartContractManager, _smartContractRunnerFactory, StateStore, _functionMetadataService); ChainService = new ChainService(new ChainManagerBasic(_dataStore), new BlockManagerBasic(_dataStore), new TransactionManager(_dataStore), new TransactionTraceManager(_dataStore), _dataStore, StateStore); }
private void Initialize() { _transactionManager = new TransactionManager(_dataStore, _logger); _transactionReceiptManager = new TransactionReceiptManager(_database); _smartContractManager = new SmartContractManager(_dataStore); _transactionResultManager = new TransactionResultManager(_dataStore); _transactionTraceManager = new TransactionTraceManager(_dataStore); _functionMetadataService = new FunctionMetadataService(_dataStore, _logger); _chainManagerBasic = new ChainManagerBasic(_dataStore); _chainService = new ChainService(_chainManagerBasic, new BlockManagerBasic(_dataStore), _transactionManager, _transactionTraceManager, _dataStore, StateStore); _smartContractRunnerFactory = new SmartContractRunnerFactory(); /*var runner = new SmartContractRunner("../../../../AElf.SDK.CSharp/bin/Debug/netstandard2.0/"); * _smartContractRunnerFactory.AddRunner(0, runner);*/ var runner = new SmartContractRunner(ContractCodes.TestContractFolder); _smartContractRunnerFactory.AddRunner(0, runner); _concurrencyExecutingService = new SimpleExecutingService( new SmartContractService(_smartContractManager, _smartContractRunnerFactory, StateStore, _functionMetadataService), _transactionTraceManager, StateStore, new ChainContextService(_chainService)); _chainCreationService = new ChainCreationService(_chainService, new SmartContractService(new SmartContractManager(_dataStore), _smartContractRunnerFactory, StateStore, _functionMetadataService), _logger); _binaryMerkleTreeManager = new BinaryMerkleTreeManager(_dataStore); _chainContextService = new ChainContextService(_chainService); _stateStore = new StateStore(_database); }
public void CodeCheckTest() { var bl1 = new List <string> { @"System\.Reflection\..*", @"System\.IO\..*", @"System\.Net\..*", @"System\.Threading\..*", }; RunnerConfig.Instance.SdkDir = _mock.SdkDir; RunnerConfig.Instance.BlackList = bl1; RunnerConfig.Instance.WhiteList = new List <string>(); var runner1 = new SmartContractRunner(); runner1.CodeCheck(_mock.ContractCode, true); var bl2 = new List <string> { @".*" }; RunnerConfig.Instance.BlackList = bl2; var runner2 = new SmartContractRunner(); Assert.Throws <InvalidCodeException>(() => runner2.CodeCheck(_mock.ContractCode, true)); }
protected override void ConfigureContainer(ContainerBuilder builder) { ChainConfig.Instance.ChainId = Hash.Generate().DumpHex(); NodeConfig.Instance.NodeAccount = Address.Generate().DumpHex(); var assembly1 = typeof(IDataProvider).Assembly; builder.RegisterAssemblyTypes(assembly1).AsImplementedInterfaces(); var assembly2 = typeof(ISerializer <>).Assembly; builder.RegisterAssemblyTypes(assembly2).AsImplementedInterfaces(); var assembly3 = typeof(DataProvider).Assembly; builder.RegisterAssemblyTypes(assembly3).AsImplementedInterfaces(); var assembly4 = typeof(BlockValidationService).Assembly; builder.RegisterAssemblyTypes(assembly4).AsImplementedInterfaces(); var assembly5 = typeof(Execution.ParallelTransactionExecutingService).Assembly; builder.RegisterAssemblyTypes(assembly5).AsImplementedInterfaces(); var assembly6 = typeof(AElf.Node.Node).Assembly; builder.RegisterAssemblyTypes(assembly6).AsImplementedInterfaces(); var assembly7 = typeof(BlockHeader).Assembly; builder.RegisterAssemblyTypes(assembly7).AsImplementedInterfaces(); builder.RegisterGeneric(typeof(Serializer <>)).As(typeof(ISerializer <>)); builder.RegisterModule(new LoggerAutofacModule()); builder.RegisterModule(new DatabaseAutofacModule()); builder.RegisterModule(new SmartContractAutofacModule()); builder.RegisterModule(new ChainAutofacModule()); builder.RegisterModule(new KernelAutofacModule()); builder.RegisterInstance(new TxPoolConfig()).As <ITxPoolConfig>(); builder.RegisterType <ChainService>().As <IChainService>(); builder.RegisterType <Grouper>().As <IGrouper>(); builder.RegisterType <ServicePack>().PropertiesAutowired(); builder.RegisterType <ActorEnvironment>().As <IActorEnvironment>().SingleInstance(); builder.RegisterType <SimpleExecutingService>().As <IExecutingService>(); var smartContractRunnerFactory = new SmartContractRunnerFactory(); var runner = new SmartContractRunner(ContractCodes.TestContractFolder); smartContractRunnerFactory.AddRunner(0, runner); smartContractRunnerFactory.AddRunner(1, runner); builder.RegisterInstance(smartContractRunnerFactory).As <ISmartContractRunnerFactory>().SingleInstance(); builder.RegisterType <TxValidator>().As <ITxValidator>(); builder.RegisterType <TxSignatureVerifier>().As <ITxSignatureVerifier>(); builder.RegisterType <TxRefBlockValidator>().As <ITxRefBlockValidator>(); builder.RegisterType <TxHub>().As <ITxHub>(); // configure your container // e.g. builder.RegisterModule<TestOverrideModule>(); }
static void Main(string[] args) { var confParser = new ConfigParser(); bool parsed; try { parsed = confParser.Parse(args); } catch (Exception e) { _logger.Error(e, "Exception while parse config."); throw; } if (!parsed) { return; } RunnerConfig.Instance.SdkDir = Path.GetDirectoryName(typeof(RunnerAElfModule).Assembly.Location); var runner = new SmartContractRunner(); var smartContractRunnerFactory = new SmartContractRunnerFactory(); smartContractRunnerFactory.AddRunner(0, runner); smartContractRunnerFactory.AddRunner(1, runner); // Setup ioc var container = SetupIocContainer(true, smartContractRunnerFactory); if (container == null) { _logger.Error("IoC setup failed."); return; } if (!CheckDBConnect(container)) { _logger.Error("Database connection failed."); return; } using (var scope = container.BeginLifetimeScope()) { var service = scope.Resolve <ActorEnvironment>(); service.InitWorkActorSystem(); Console.WriteLine("Press Control + C to terminate."); Console.CancelKeyPress += async(sender, eventArgs) => { await service.StopAsync(); }; service.TerminationHandle.Wait(); } }
public async Task TestFailCases(SmartContractRunner runner) { var groundTruthMap = new Dictionary <string, Dictionary <string, FunctionMetadataTemplate> >(); var exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractD)))); Assert.True(exception.Message.Contains("Duplicate name of field attributes in contract")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractE)))); Assert.True( exception.Message.Contains("Duplicate name of smart contract reference attributes in contract ")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractF)))); Assert.True(exception.Message.Contains("Unknown reference local field ${this}.resource1")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractG)))); Assert.True(exception.Message.Contains("Duplicate name of function attribute")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractH)))); Assert.True(exception.Message.Contains("contains unknown reference to it's own function")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractI)))); Assert.True(exception.Message.Contains("contains unknown local member reference to other contract")); exception = await Assert .ThrowsAsync <FunctionMetadataException>(() => Task.FromResult(runner.ExtractMetadata(typeof(TestContractJ)))); Assert.True(exception.Message.Contains("is Non-DAG thus nothing take effect")); }
protected override void ConfigureContainer(ContainerBuilder builder) { var assembly1 = typeof(IDataProvider).Assembly; builder.RegisterAssemblyTypes(assembly1).AsImplementedInterfaces(); var assembly2 = typeof(ISerializer <>).Assembly; builder.RegisterAssemblyTypes(assembly2).AsImplementedInterfaces(); var assembly3 = typeof(DataProvider).Assembly; builder.RegisterAssemblyTypes(assembly3).AsImplementedInterfaces(); var assembly4 = typeof(BlockValidationService).Assembly; builder.RegisterAssemblyTypes(assembly4).AsImplementedInterfaces(); var assembly5 = typeof(Execution.ParallelTransactionExecutingService).Assembly; builder.RegisterAssemblyTypes(assembly5).AsImplementedInterfaces(); var assembly6 = typeof(AElf.Node.Node).Assembly; builder.RegisterAssemblyTypes(assembly6).AsImplementedInterfaces(); var assembly7 = typeof(BlockHeader).Assembly; builder.RegisterAssemblyTypes(assembly7).AsImplementedInterfaces(); builder.RegisterGeneric(typeof(Serializer <>)).As(typeof(ISerializer <>)); builder.RegisterModule(new DatabaseAutofacModule()); builder.RegisterModule(new LoggerAutofacModule()); builder.RegisterModule(new ChainAutofacModule()); builder.RegisterModule(new KernelAutofacModule()); builder.RegisterModule(new SmartContractAutofacModule()); var smartContractRunnerFactory = new SmartContractRunnerFactory(); var runner = new SmartContractRunner("../../../../AElf.Runtime.CSharp.Tests.TestContract/bin/Debug/netstandard2.0/"); smartContractRunnerFactory.AddRunner(0, runner); smartContractRunnerFactory.AddRunner(1, runner); builder.RegisterInstance(smartContractRunnerFactory).As <ISmartContractRunnerFactory>().SingleInstance(); // configure your container // e.g. builder.RegisterModule<TestOverrideModule>(); }
public static async Task Main(string[] args) { BenchmarkOptions opts = null; Parser.Default.ParseArguments <BenchmarkOptions>(args) .WithParsed(o => { opts = o; }) .WithNotParsed(errs => {}); if (opts == null) { return; } if (opts.BenchmarkMethod == BenchmarkMethod.EvenGroup) { if (opts.SdkDir == null) { opts.SdkDir = Directory.GetCurrentDirectory(); Console.WriteLine("No Sdk directory in arg, choose current directory: " + opts.SdkDir); } if (opts.DllDir == null) { opts.DllDir = Directory.GetCurrentDirectory(); Console.WriteLine("No dll directory in arg, choose current directory: " + opts.DllDir); } if (!Directory.Exists(Path.GetFullPath(opts.SdkDir))) { Console.WriteLine("directory " + Path.GetFullPath(opts.SdkDir) + " not exist"); return; } if (!File.Exists(Path.GetFullPath(Path.Combine(opts.DllDir, opts.ContractDll)))) { Console.WriteLine( Path.GetFullPath(Path.Combine(opts.DllDir, opts.ContractDll) + " not exist")); return; } if (!File.Exists(Path.GetFullPath(Path.Combine(opts.DllDir, opts.ZeroContractDll)))) { Console.WriteLine( Path.GetFullPath(Path.Combine(opts.DllDir, opts.ZeroContractDll) + " not exist")); return; } if (opts.GroupRange.Count() != 2 || opts.GroupRange.ElementAt(0) > opts.GroupRange.ElementAt(1)) { Console.WriteLine( "please input 2 number to indicate the lower and upper bounds, where lower bound is lower than upper bound"); return; } if (opts.ConcurrencyLevel.HasValue) { ActorConfig.Instance.ConcurrencyLevel = opts.ConcurrencyLevel.Value; } //enable parallel feature for benchmark ParallelConfig.Instance.IsParallelEnable = true; var builder = new ContainerBuilder(); //builder.RegisterModule(new MainModule()); builder.RegisterModule(new DatabaseAutofacModule()); builder.RegisterModule(new LoggerAutofacModule()); builder.RegisterModule(new ChainAutofacModule()); builder.RegisterModule(new KernelAutofacModule()); builder.RegisterModule(new SmartContractAutofacModule()); builder.RegisterType <Benchmarks>().WithParameter("options", opts); var runner = new SmartContractRunner(opts.SdkDir); SmartContractRunnerFactory smartContractRunnerFactory = new SmartContractRunnerFactory(); smartContractRunnerFactory.AddRunner(0, runner); smartContractRunnerFactory.AddRunner(1, runner); builder.RegisterInstance(smartContractRunnerFactory).As <ISmartContractRunnerFactory>().SingleInstance(); if (ParallelConfig.Instance.IsParallelEnable) { builder.RegisterType <Grouper>().As <IGrouper>(); builder.RegisterType <ServicePack>().As <ServicePack>().PropertiesAutowired(); builder.RegisterType <ActorEnvironment>().As <IActorEnvironment>().SingleInstance(); builder.RegisterType <ParallelTransactionExecutingService>().As <IExecutingService>(); } else { builder.RegisterType <SimpleExecutingService>().As <IExecutingService>(); } var container = builder.Build(); if (container == null) { Console.WriteLine("IoC setup failed"); return; } if (!CheckDbConnect(container)) { Console.WriteLine("Database connection failed"); return; } using (var scope = container.BeginLifetimeScope()) { if (ParallelConfig.Instance.IsParallelEnable) { var actorEnv = scope.Resolve <IActorEnvironment>(); try { actorEnv.InitActorSystem(); } catch (Exception e) { Console.WriteLine(e); } } var benchmarkTps = scope.Resolve <Benchmarks>(); await benchmarkTps.InitContract(); Thread.Sleep(200); //sleep 200 ms to let async console print in order await benchmarkTps.BenchmarkEvenGroup(); } } else if (opts.BenchmarkMethod == BenchmarkMethod.GenerateAccounts) { var dataGenerator = new TransactionDataGenerator(opts); dataGenerator.PersistAddrsToFile(opts.AccountFileDir); } Console.WriteLine("\n\nPress any key to continue "); Console.ReadKey(); }
public async Task TestTryAddNewContract() { RunnerConfig.Instance.SdkDir = _mock.SdkDir; var runner = new SmartContractRunner(); var reg = new SmartContractRegistration { Category = 0, ContractBytes = ByteString.CopyFrom(_mock.ContractCode), ContractHash = Hash.FromRawBytes(_mock.ContractCode) }; var resC = runner.ExtractMetadata(typeof(TestContractC)); // Structure of the test data var groundTruthResC = new Dictionary <string, FunctionMetadataTemplate>( new[] // function metadata map for contract { new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func0", // local function name new FunctionMetadataTemplate( // local function metadata new HashSet <string>(), // calling set of this function metadata new HashSet <Resource>(new[] // local resource set of this function metadata { new Resource("${this}.resource4", DataAccessMode.AccountSpecific) // resource of the local resource set }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func1", new FunctionMetadataTemplate( new HashSet <string>(), new HashSet <Resource>(new[] { new Resource("${this}.resource5", DataAccessMode.ReadOnlyAccountSharing) }))) }); var groundTruthTemplateC = new ContractMetadataTemplate(typeof(TestContractC).FullName, groundTruthResC, new Dictionary <string, Address>()); Assert.Equal(groundTruthTemplateC, resC, new ContractMetadataTemplateEqualityComparer()); var resB = runner.ExtractMetadata(typeof(TestContractB)); var groundTruthResB = new Dictionary <string, FunctionMetadataTemplate>(new[] { new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func0", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${ContractC}.Func1" }), new HashSet <Resource>(new[] { new Resource("${this}.resource2", DataAccessMode.AccountSpecific) }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func1", new FunctionMetadataTemplate( new HashSet <string>(), new HashSet <Resource>(new[] { new Resource("${this}.resource3", DataAccessMode.ReadOnlyAccountSharing) }))) }); var refB = new Dictionary <string, Address>(new [] { new KeyValuePair <string, Address>("ContractC", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x456745674567456745674567456745674567"))), }); var groundTruthTemplateB = new ContractMetadataTemplate(typeof(TestContractB).FullName, groundTruthResB, refB); Assert.Equal(groundTruthTemplateB, resB, new ContractMetadataTemplateEqualityComparer()); var resA = runner.ExtractMetadata(typeof(TestContractA)); var groundTruthResA = new Dictionary <string, FunctionMetadataTemplate>(new[] { new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func0(int)", new FunctionMetadataTemplate( new HashSet <string>(), new HashSet <Resource>())), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func0", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${this}.Func1" }), new HashSet <Resource>(new[] { new Resource("${this}.resource0", DataAccessMode.AccountSpecific) }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func1", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${this}.Func2" }), new HashSet <Resource>(new[] { new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing) }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func2", new FunctionMetadataTemplate( new HashSet <string>(), new HashSet <Resource>(new[] { new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing), new Resource("${this}.resource2", DataAccessMode.ReadWriteAccountSharing) }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func3", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${_contractB}.Func0", "${this}.Func0", "${ContractC}.Func0" }), new HashSet <Resource>(new[] { new Resource("${this}.resource1", DataAccessMode.ReadOnlyAccountSharing) }))), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func4", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${this}.Func2", "${this}.Func2" }), new HashSet <Resource>())), new KeyValuePair <string, FunctionMetadataTemplate>( "${this}.Func5", new FunctionMetadataTemplate( new HashSet <string>(new[] { "${_contractB}.Func1", "${this}.Func3" }), new HashSet <Resource>())), }); var refA = new Dictionary <string, Address>(new [] { new KeyValuePair <string, Address>("ContractC", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x456745674567456745674567456745674567"))), new KeyValuePair <string, Address>("_contractB", Address.FromRawBytes(ByteArrayHelpers.FromHexString("0x123412341234123412341234123412341234"))), }); var groundTruthTemplateA = new ContractMetadataTemplate(typeof(TestContractA).FullName, groundTruthResA, refA); Assert.Equal(groundTruthTemplateA, resA, new ContractMetadataTemplateEqualityComparer()); //test fail cases await TestFailCases(runner); }