Пример #1
0
        public async Task <IActionResult> Add(string hash_сustomer, string hash_executor,
                                              string order_sum, string prepaid_expense, string condition, string is_Done, string is_freze)
        {
            var is_Done_bool  = String2Bool.Convert(is_Done);
            var is_freze_bool = String2Bool.Convert(is_Done);

            User         user         = null;
            Transactions transactions = null;

            if (ModelState.IsValid)
            {
                user = await _context.User.FirstOrDefaultAsync(u => u.Login == User.Identity.Name);

                if (user != null)
                {
                    if (user.balance >= Convert.ToDouble(order_sum))
                    {
                        string contract_hash = await ShaEncoder.GenerateSHA256String($"{order_sum}{prepaid_expense}{DateTime.Now}");

                        var contract = new Contract
                        {
                            hash_сustomer   = user.Hash,
                            hash_еxecutor   = hash_executor,
                            order_sum       = Convert.ToInt32(order_sum) - Convert.ToInt32(prepaid_expense),
                            prepaid_expense = prepaid_expense,
                            is_freze        = true,
                            is_Done         = false,
                            contractID      = contract_hash
                        };

                        var json = JsonConvert.SerializeObject(contract);

                        await _blockchainService.AddBlockAsync(await _blockchainService.generateNextBlockAsync(json, user.Hash, hash_executor));

                        transactions = new Transactions
                        {
                            original_wallet    = user.Hash,
                            destination_wallet = hash_executor,
                            info      = json,
                            timestamp = DateTime.Now.ToString()
                        };

                        user.balance = Convert.ToInt16(prepaid_expense);

                        _context.User.Update(user);
                        await _context.Transactions.AddAsync(transactions);

                        await _context.SaveChangesAsync();

                        return(RedirectToAction("Index", "Home"));
                    }

                    return(Content("Недостаточно средств"));
                }

                ModelState.AddModelError("", "Некорректные логин и(или) пароль");
            }

            return(View());
        }
        public async Task ParallelExecuteAsync_Test()
        {
            var chain = await BlockchainService.GetChainAsync();

            (PrepareTransactions, KeyPairs) = await OsTestHelper.PrepareTokenForParallel(10);

            Block = OsTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, PrepareTransactions);
            //await BlockExecutingService.ExecuteBlockAsync(Block.Header, PrepareTransactions);
            await OsTestHelper.BroadcastTransactions(PrepareTransactions);

            Block = await MinerService.MineAsync(chain.BestChainHash, chain.BestChainHeight,
                                                 TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4));

            await BlockchainService.AddBlockAsync(Block);

            await BlockAttachService.AttachBlockAsync(Block);

            SystemTransactions = await OsTestHelper.GenerateTransferTransactions(1);

            CancellableTransactions = OsTestHelper.GenerateTransactionsWithoutConflict(KeyPairs);
            Block = OsTestHelper.GenerateBlock(Block.GetHash(), Block.Height,
                                               SystemTransactions.Concat(CancellableTransactions));

            await OsTestHelper.BroadcastTransactions(SystemTransactions.Concat(CancellableTransactions));

            var block = await BlockExecutingService.ExecuteBlockAsync(Block.Header,
                                                                      SystemTransactions, CancellableTransactions, CancellationToken.None);

            block.TransactionIds.Count().ShouldBeGreaterThan(10);
        }
Пример #3
0
        public async Task <IActionResult> Add([FromQuery] string token, string Hash, string diagnosis,
                                              string diagnosis_fully, string first_aid, string drugs, string is_important)
        {
            bool boolean;

            try
            {
                if (is_important == "true")
                {
                    boolean = true;
                }
                else
                {
                    boolean = false;
                }

                var user = await _context.User.FirstOrDefaultAsync(u => u.Hash == Hash);

                var medical = new Medical
                {
                    diagnosis       = diagnosis,
                    diagnosis_fully = diagnosis_fully,
                    first_aid       = first_aid,
                    drugs           = drugs,
                    is_important    = boolean
                };

                string json = JsonConvert.SerializeObject(medical);
                _blockchainService.AddBlockAsync(await _blockchainService.generateNextBlockAsync(json, Hash));

                return(RedirectToAction("Index", "Home"));
            }
            catch
            {
                return(View());
            }
        }
Пример #4
0
        public async Task <IActionResult> smartcontractadd([FromQuery] string hash_сustomer, [FromQuery] string hash_еxecutor,
                                                           [FromQuery] string order_sum, [FromQuery] string prepaid_expense, [FromQuery] string condition)
        {
            try
            {
                var contract_hash = await ShaEncoder.GenerateSHA256String($"{order_sum}{prepaid_expense}{DateTime.Now}");

                var contract = new Contract
                {
                    hash_сustomer   = hash_сustomer,
                    hash_еxecutor   = hash_еxecutor,
                    order_sum       = Convert.ToInt32(order_sum) - Convert.ToInt32(prepaid_expense),
                    prepaid_expense = prepaid_expense,
                    is_freze        = true,
                    is_Done         = false,
                    contractID      = contract_hash
                };

                var json = JsonConvert.SerializeObject(contract);
                await _blockchainService.AddBlockAsync(await _blockchainService.generateNextBlockAsync(json, hash_сustomer, hash_еxecutor));

                var transactions = new Transactions
                {
                    original_wallet    = hash_сustomer,
                    destination_wallet = hash_еxecutor,
                    info      = json,
                    timestamp = DateTime.Now.ToString()
                };

                var user = await _context.User.FirstOrDefaultAsync(u => u.Hash == hash_еxecutor);

                user.balance = Convert.ToInt16(prepaid_expense);
                _context.User.Update(user);

                await _context.Transactions.AddAsync(transactions);

                await _context.SaveChangesAsync();

                var data_response = new response_api();
                data_response.request_Info        = new request_info();
                data_response.request_Info.answer = "OK";
                data_response.request_Info.code   = "200";
                data_response.send_data           = new send_data();

                List <Block> blocks = _context.Block.Where(u => u.wallet_hash == hash_сustomer && u.destination_wallet_hash == hash_еxecutor).ToList();

                List <Contract> contracts = new List <Contract>();
                foreach (var block in blocks)
                {
                    contracts.Add(JsonConvert.DeserializeObject <Contract>(block.data));
                }

                data_response.send_data.contracts = contracts;

                return(Content(JsonConvert.SerializeObject(data_response), "application/json"));
            }
            catch
            {
                var answ = new response_api();
                answ.request_Info        = new request_info();
                answ.request_Info.code   = "403";
                answ.request_Info.answer = "BadInfo";

                return(Content(JsonConvert.SerializeObject(answ), "application/json"));
            }
        }