private List <MinedBlockInfo> GetWholeChainFromPeer(Peer peer)
        {
            var resp = HttpUtils.DoApiGet <List <MinedBlockInfoResponse> >(peer.Url, GET_SYNC_BLOCKCHAIN_API_PATH);

            var chain = resp?.ConvertAll(i =>
                                         new MinedBlockInfo(MiningBlockInfo.FromMinedBlockInfo(i))
            {
                DateCreated = i.DateCreated,
                Nonce       = i.Nonce
            });

            return(chain);
        }
        private bool IsPeersBlockValid(MinedBlockInfo block)
        {
            var mbi = new MinedBlockInfo(MiningBlockInfo.FromMinedBlockInfo(block))
            {
                Nonce       = block.Nonce,
                DateCreated = block.DateCreated,
            };

            var isGenesisBlock       = block.Index == 0;
            var hashCoversDifficulty = mbi.BlockHash.StartsWith("".PadLeft(appSettings.Difficulty, '0'));
            var hashesMatch          = mbi.BlockHash == block.BlockHash;

            return(hashesMatch && (hashCoversDifficulty || isGenesisBlock));
        }
        public MiningBlockInfo GetMiningBlockInfo(string address)
        {
            lock (dbService.GetTransactionsLockObject())
            {
                var transactions = new List <Transaction>(dbService.GetTransactions().ToArray());//shallow copy, so we can keep a snapshot
                var lbi          = dbService.GetLastBlock().Index + 1;

                //reward for the miner
                var t = new Transaction()
                {
                    DateCreated        = DateTime.UtcNow,
                    Fee                = 0,
                    To                 = address,
                    Value              = appSettings.MinerReward + transactions.Aggregate(0, (sum, tr) => sum + tr.Fee),
                    From               = ZERO_HASH,
                    MinedInBlockIndex  = lbi,
                    TransferSuccessful = true
                };

                t.TransactionHashHex = t.CalculateTransactionHashHex();

                transactions.Insert(0, t);

                var info = new MiningBlockInfo
                {
                    Difficulty        = appSettings.Difficulty,
                    Index             = lbi,
                    MinedBy           = address,
                    PreviousBlockHash = dbService.GetLastBlock().BlockHash,
                    Transactions      = transactions
                };

                dbService.AddMiningInfo(info);

                return(MiningBlockInfoResponse.FromMiningBlockInfo(info));
            }
        }
Пример #4
0
 public void AddMiningInfo(MiningBlockInfo info)
 {
     allBlockInfos.TryAdd(info.BlockDataHash, info);
 }