static bool SubmitMinedBlockInfo(string myAddress, ulong nonce, DateTime dateCreated, string blockHash)
        {
            var body = new MinedBlockInfoRequest
            {
                DateCreated   = dateCreated,
                Nonce         = nonce,
                BlockDataHash = blockHash
            };

            var res = HttpUtils.DoApiPost <MinedBlockInfoRequest, SubmitBlockResponse>(url, "api/mining/submit-block/", body);

            if (res == null)
            {
                Console.WriteLine($"[{DateTime.UtcNow}] Error occured while trying to call the node API..");
                return(false);
            }

            if (res.Status == BlockResponseStatus.Error)
            {
                Console.WriteLine($"Error {res.Message}");
                return(false);
            }

            return(true);
        }
Exemple #2
0
 public SubmitBlockResponse Post([FromBody] MinedBlockInfoRequest data)
 {
     return(_blockchainService.SubmitBlockInfo(data));
 }
        public SubmitBlockResponse SubmitBlockInfo(MinedBlockInfoRequest data)
        {
            lock (dbService.GetBlocksLockObject())
            {
                lock (dbService.GetTransactionsLockObject())
                {
                    var info = dbService.GetMiningInfo(data.BlockDataHash);
                    if (null == info)
                    {
                        return(new SubmitBlockResponse
                        {
                            Status = BlockResponseStatus.Error,
                            Message = "Wrong block hash!"
                        });
                    }

                    var mbi = new MinedBlockInfo(info)
                    {
                        Nonce       = data.Nonce,
                        DateCreated = data.DateCreated,
                    };

                    if (mbi.BlockHash.StartsWith("".PadLeft(appSettings.Difficulty, '0')))
                    {
                        var success = dbService.TryAddBlock(mbi);

                        if (!success)
                        {
                            return(new SubmitBlockResponse
                            {
                                Status = BlockResponseStatus.Error,
                                Message = "Old block! Someone mined it already"
                            });
                        }

                        //UPDATE transactions in the block.
                        mbi.Transactions.ForEach(t =>
                        {
                            t.MinedInBlockIndex  = info.Index;
                            t.TransferSuccessful = true;
                            dbService.RemoveTransaction(t);
                        });

                        PropagateBlockToPeers(mbi);

                        return(new SubmitBlockResponse
                        {
                            Status = BlockResponseStatus.Success,
                            Message = $"Block is valid"
                        });
                    }
                    else
                    {
                        return(new SubmitBlockResponse
                        {
                            Status = BlockResponseStatus.Error,
                            Message = $"Hash must start with {appSettings.Difficulty} zeroes"
                        });
                    }
                }
            }
        }