public FormResumeSetting(Form formMain, Resume resume, MineService mineService)
 {
     InitializeComponent();
     this.formMain = formMain as FormMain;
     this.resume = resume;
     this.mineService = mineService;
 }
Example #2
0
        public void CreateMine_IntArray_ReturnsMine(int[] mineInput)
        {
            var coordinateServiceStub = new Mock <ICoordinateService>();

            coordinateServiceStub.Setup(x => x.Create(mineInput[0], mineInput[1]))
            .Returns(new Coordinate {
                X = mineInput[0], Y = mineInput[1]
            });
            var mineService = new MineService(coordinateServiceStub.Object);
            var mine        = mineService.CreateMine(mineInput);

            Assert.NotNull(mine);
        }
Example #3
0
        public void DetonateMine_ReturnsDetonatedStatus()
        {
            var coordinateServiceStub = new Mock <ICoordinateService>();

            coordinateServiceStub.Setup(x => x.Create(0, 0))
            .Returns(new Coordinate {
                X = 0, Y = 0
            });
            var mineService = new MineService(coordinateServiceStub.Object);
            var mine        = mineService.CreateMine(new[] { 0, 0 });

            mineService.Detonate(mine);
            Assert.Equal(MineStatus.Detonated, mine.Status);
        }
Example #4
0
        public void CreateMine_IntArray_ReturnsValidMinePositionY(int[] mineInput, int expectedValue)
        {
            var coordinateServiceStub = new Mock <ICoordinateService>();

            coordinateServiceStub.Setup(x => x.Create(mineInput[0], mineInput[1]))
            .Returns(new Coordinate {
                X = mineInput[0], Y = mineInput[1]
            });
            var mineService = new MineService(coordinateServiceStub.Object);
            var mine        = mineService.CreateMine(mineInput);
            var result      = mine.Position.Y;

            Assert.Equal(result, expectedValue);
        }
Example #5
0
 static void Main(string[] args)
 {
     MineService.EscapeMines();
 }
Example #6
0
        static void Main(string[] args)
        {
            var app = new CommandLineApplication();

            CompositeResolver.RegisterAndSetAsDefault(
                ReadonlyBytesResolver.Instance,
                BuiltinResolver.Instance,
                DynamicEnumResolver.Instance,
                DynamicGenericResolver.Instance,
                DynamicObjectResolver.Instance
                );

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                Converters = new List <JsonConverter>
                {
                    new IPEndPointConverter()
                }
            };

            var cryptoService = new CryptoService();
            var mineService   = new MineService(cryptoService);
            var nodeService   = new NodeService(cryptoService, mineService);

            app.Name = "mvchain";
            app.HelpOption(helpOptionString);

            app.OnExecute(() =>
            {
                Printer.Print(appTitle);
                app.ShowHelp();
                return(0);
            });

            app.Command("keygen", (command) =>
            {
                command.Description = "Generate a set of public and private pair keys";
                command.HelpOption(helpOptionString);

                command.OnExecute(() =>
                {
                    var keypair = cryptoService.GenerateKeypair();
                    Printer.Print(KeyPair.From(keypair));
                    return(0);
                });
            });

            app.Command("genesis", (command) =>
            {
                command.Description = "Generate the genesis block";
                command.HelpOption(helpOptionString);

                var keypairopt = command.Option(
                    "-kp|--keypair <FILE>",
                    "Files that contains keypair json",
                    CommandOptionType.SingleValue)
                                 .IsRequired(allowEmptyStrings: false, errorMessage: "keypair file needed");

                keypairopt.ShortName = "kp";

                var genesisblockopt = command.Option(
                    "-g|--genesis <FILE>",
                    "The genesis block file that will be created",
                    CommandOptionType.SingleValue)
                                      .IsRequired(allowEmptyStrings: false, errorMessage: "Genesis block file name required");

                genesisblockopt.ShortName = "g";

                command.OnExecute(() =>
                {
                    mineService.MineGenesisBlock(keypairopt.Value(), genesisblockopt.Value());
                    return(0);
                });
            });

            app.Command("config", (command) =>
            {
                command.Description = "Generate the default config file";
                command.HelpOption(helpOptionString);

                var genFileOpt = command.Option(
                    "-gf|--generate",
                    "generate a file with the default config",
                    CommandOptionType.NoValue);

                command.OnExecute(() =>
                {
                    DefaultConfigGenerator.GenerateDefaultConfig(genFileOpt.HasValue());
                    return(0);
                });
            });

            app.Command("run", (command) =>
            {
                command.Description = "Run the blockchain node";
                command.HelpOption(helpOptionString);

                var configOpt = command.Option(
                    "-c|--config <FILE>",
                    "Path to the node configuration file, default value can be generated using config -g command",
                    CommandOptionType.SingleValue)
                                .IsRequired(allowEmptyStrings: false, errorMessage: "Config file path required");

                command.OnExecute(() =>
                {
                    nodeService.BootUp(configOpt.Value());

                    var newBlock = new Block()
                    {
                        PreviousHash = MineService.EmptyHash,
                        Difficulty   = MineService.Difficulty,
                        Nonce        = 0,
                        Timestamp    = DateTime.UtcNow,
                    };

                    nodeService.AddBlock(newBlock);
                });
            });

            try
            {
                app.Execute(args);
            }
            catch (Exception exception) when(
                exception is CommandParsingException ||
                exception is InvalidDataException)
            {
                Printer.Print(exception.Message);
            }
        }
Example #7
0
        // Composition Root - 组合根:
        // 设计模式。在程序的入口处进行抽象-具体的依赖关系配置。
        // Pure DI
        public static void Main()
        {
            // 出创世区块:通过执行一批交易来初始化整条链,一般包括部署系统合约,如共识合约,多资产合约等,并对系统合约做初步配置
            //   准备交易
            var transactionPool = new TransactionPool
            {
                Logger = new ConsoleLogger()
            };

            foreach (var genesisTransaction in TransactionGenerationHelper.GetGenesisTransactions())
            {
                transactionPool.AddTransaction(genesisTransaction);
            }

            //   打包创世区块
            //     准备MineService
            var blockValidationService = new BlockValidationService(new List <IBlockValidationProvider>
            {
                new BlockTransactionValidationProvider
                {
                    Logger = new ConsoleLogger()
                },
                new BlockBasicInformationValidationProvider
                {
                    Logger = new ConsoleLogger()
                }
            })
            {
                Logger = new ConsoleLogger()
            };
            var blockChainService = new BlockChainService(new PureDIChainIdProvider(), blockValidationService)
            {
                Logger = new ConsoleLogger()
            };
            var transactionExecutingService = new TransactionExecutingService
            {
                Logger = new ConsoleLogger()
            };

            var mineService = new MineService(transactionPool, blockChainService, transactionExecutingService);

            //     生产创世区块
            var genesisBlock = mineService.Mine();

            //     把创世区块添加到链上
            blockChainService.AppendBlock(genesisBlock);
            //   创世区块不广播,每一个节点都独立生成创世区块

            // 所以NetworkService实例较晚才出现
            var networkService = new NetworkService
            {
                Logger = new ConsoleLogger()
            };

            // 开始产生其他区块 - 挖矿
            // Demo: 出2个块
            var count = 2;

            while (count > 0)
            {
                // 交易应该是从网络收取的,这里随机生成一些
                var txs = TransactionGenerationHelper.GetSomeRandomTransactions();
                // 正常流程:从网络中收到交易,验证交易,然后丢进交易池
                foreach (var tx in txs)
                {
                    transactionPool.AddTransaction(tx);
                }
                // 交易池里已经有交易了,然后开始出块
                var block = mineService.Mine();
                // 拿到区块以后,自己做一些列验证,没问题的话就广播,并添加到本地的链上
                networkService.BroadcastBlock(block);
                blockChainService.AppendBlock(block);
                count--;
            }
            // 实际上,共识决定了谁应该在什么时候出块,以及收到一个区块以后该怎么验证这个区块的合法性
        }