Beispiel #1
0
        public Block MineBlock()
        {
            if (hash.Substring(0, _difficulty + 1) == _comparison)
            {
                GenerateNewHash();
            }

            var sw = new Stopwatch();

            _logger.LogInformation($"Miner started.");

            sw.Start();

            while (hash.Substring(0, _difficulty + 1) != _comparison)
            {
                nonce++;
                hash = _hasher.CalculateHash(hash + nonce.ToString());
            }

            sw.Stop();

            _logger.LogInformation($"Mining complete. Total time: {sw.Elapsed}");

            return(_newBlockManager.GenerateNewBlockAsync(hash).Result);
        }
Beispiel #2
0
        private Transaction[] GenerateTransactions(string prevHash)
        {
            var rand = new Random();

            var num = rand.Next(2, 30);
            var transactionArray = new Transaction[num];

            for (int i = 0; i < num; i++)
            {
                var sender     = Guid.NewGuid().ToString();
                var receiver   = Guid.NewGuid().ToString();
                var multiplier = rand.NextDouble();

                transactionArray[i] = new Transaction
                {
                    RecipientID         = receiver,
                    SenderID            = sender,
                    TransactionDateTime = DateTime.UtcNow.AddDays(-num),
                    TransactionAmount   = num * (decimal)multiplier,
                    TransactionID       = _hasher.CalculateHash($"{DateTime.UtcNow.AddDays(-num)}{sender}{receiver}{num * (decimal)multiplier}{prevHash}")
                };
            }

            return(transactionArray);
        }
        private Block CreateGenesisBlock()
        {
            var genesisData = new Data
            {
                PreviousBlockHash = "0",
                Transactions      = new Models.Transaction[]
                {
                    new Models.Transaction
                    {
                        TransactionDateTime = DateTime.UtcNow,
                        TransactionAmount   = 0.0M,
                        SenderID            = "00000000000000000000",
                        RecipientID         = "00000000000000000000",
                        TransactionID       = _hasher.CalculateHash($"{DateTime.UtcNow.ToLongTimeString()}{0.0M}0000000000000000000000000000000000000000" + "0")
                    }
                }
            };

            return(new Block(genesisData, genesisData.Transactions[0].TransactionID));
        }
Beispiel #4
0
        private List <Block> InitializeBlockChain()
        {
            var genesisData = new Data
            {
                PreviousBlockHash = "0",
                Transactions      = new Transaction[]
                {
                    new Transaction
                    {
                        TransactionDateTime = DateTime.UtcNow,
                        TransactionAmount   = 0.0M,
                        SenderID            = "00000000000000000000",
                        RecipientID         = "00000000000000000000",
                        TransactionID       = _hasher.CalculateHash($"{DateTime.UtcNow.ToLongTimeString()}{0.0M}0000000000000000000000000000000000000000" + "0")
                    }
                }
            };

            var block = new Block(genesisData, genesisData.Transactions[0].TransactionID);

            return(new List <Block> {
                block
            });
        }