コード例 #1
0
ファイル: Node.cs プロジェクト: svetoslavpetkov/BlockChain
        private MiningContext BuildNewMinerJob(Block blockForMine)
        {
            string prevBlockHash = LastBlock.BlockHash;

            MiningContext context = new MiningContext();

            context.BlockIndex    = blockForMine.Index;
            context.BlockHash     = blockForMine.BlockDataHash;
            context.Difficulty    = Difficulty;
            context.PrevBlockHash = prevBlockHash;
            context.Timestamp     = blockForMine.CreatedDate;

            return(context);
        }
コード例 #2
0
ファイル: Node.cs プロジェクト: svetoslavpetkov/BlockChain
        internal MiningContext GetBlockForMine(string minerAddress)
        {
            Block blockInProgressForMiner = null;

            BlocksInProgress.TryGetValue(minerAddress, out blockInProgressForMiner);

            if (blockInProgressForMiner == null)
            {
                Block         blockForMine = BuildBlock();
                MiningContext context      = BuildNewMinerJob(blockForMine);
                BlocksInProgress[minerAddress] = blockForMine;

                return(context);
            }
            else
            {
                bool hasNewBlock          = LastBlock.Index >= blockInProgressForMiner.Index;
                bool hasNewerTransactions = false;

                // still mining same block
                if (LastBlock.Index == blockInProgressForMiner.Index - 1)
                {
                    hasNewerTransactions = PendingTransactions.Count > blockInProgressForMiner.Transactions.Count;
                }

                if (hasNewBlock || hasNewerTransactions)
                {
                    Block         blockForMine = BuildBlock();
                    MiningContext context      = BuildNewMinerJob(blockForMine);
                    BlocksInProgress[minerAddress] = blockForMine;

                    return(context);
                }

                return(BuildNewMinerJob(blockInProgressForMiner));
            }
        }