Пример #1
0
        public void UpdateParameters()
        {
            nonce     = 0;
            timestamp = (uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

            lock (GateKeeper.TransactionPoolLock)
            {
                lock (GateKeeper.BalanceLedgerLock)
                {
                    transactions = _transactionPool.GetMineableTransactions();

                    if (transactions.Count > 100)
                    {
                        transactions = transactions.Take(100).ToList();
                    }
                }
            }

            lock (GateKeeper.ChainManagerLock)
            {
                var topBlock = _chainManager.GetBlock(_chainManager.Height);

                if (topBlock.Height > lastBlockHeight)
                {
                    numberOfAttempts = 0;
                    lastBlockHeight  = topBlock.Height;
                }

                height            = topBlock.Height + 1;
                previousBlockHash = topBlock.GetHash();

                lastBlockTimeStamp = topBlock.Timestamp;

                if (height == 1)
                {
                    difficulty = Block.LowestDifficulty;
                }
                else if (height % Program.DifficultyRecalibration == 1)
                {
                    var recalibrationBlock = _chainManager.GetBlock(height - Program.DifficultyRecalibration);
                    difficulty = ChainManager.CalculateDifficulty(recalibrationBlock, timestamp);
                }
                else
                {
                    difficulty = topBlock.Difficulty;
                }
            }
        }
Пример #2
0
        public Miner(BalanceLedger balanceLedger, TransactionPool transactionPool, ChainManager chainManager)
        {
            _balanceLedger   = balanceLedger;
            _transactionPool = transactionPool;
            _chainManager    = chainManager;

            var minerAddressBytes = HexConverter.ToBytes(Program.Settings.minerAddr);

            if (minerAddressBytes != null && minerAddressBytes.Length < 34)
            {
                minerAddress = ByteManipulator.BigEndianTruncate(minerAddressBytes, 33);
                Console.WriteLine($"[Miner] Miner initialized with address {HexConverter.ToPrefixString(minerAddress)}");
            }
            else
            {
                Console.WriteLine("[Miner] Warning: Invalid address. Miner initialized with address 0x0");
                minerAddress = new byte[33];
            }
        }