Ejemplo n.º 1
0
        public IActionResult Generate([FromBody] MiningRequest request)
        {
            Guard.NotNull(request, nameof(request));

            try
            {
                if (this.network.Consensus.IsProofOfStake &&
                    this.consensusManager.Tip.Height > this.network.Consensus.LastPOWBlock)
                {
                    return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.MethodNotAllowed, "Method not allowed",
                                                           string.Format(LastPowBlockExceededMessage, this.network.Consensus.LastPOWBlock)));
                }

                if (!this.ModelState.IsValid)
                {
                    var errors = this.ModelState.Values.SelectMany(e => e.Errors.Select(m => m.ErrorMessage));
                    return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, "Formatting error",
                                                           string.Join(Environment.NewLine, errors)));
                }

                var blockCount = request.BlockCount;

                if (blockCount <= 0)
                {
                    return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.Forbidden, "Invalid request",
                                                           "The number of blocks to mine must be higher than zero."));
                }

                this.logger.LogDebug("({0}:{1})", nameof(request.BlockCount), blockCount);

                var accountReference = GetAccount();
                var address          = this.walletManager.GetUnusedAddress(accountReference);

                var generateBlocksModel = new GenerateBlocksModel
                {
                    Blocks = this.powMining.GenerateBlocks(
                        new ReserveScript(new BitcoinWitPubKeyAddress(address.Bech32Address, this.network)
                                          .ScriptPubKey), (ulong)blockCount, int.MaxValue)
                };

                this.logger.LogTrace("(-):*.{0}={1}", "Generated block count", generateBlocksModel.Blocks.Count);

                return(Json(generateBlocksModel));
            }
            catch (Exception e)
            {
                this.logger.LogError(ExceptionOccurredMessage, e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
        public IActionResult Generate([FromBody] MiningRequest request)
        {
            Guard.NotNull(request, nameof(request));

            try
            {
                if (!this.ModelState.IsValid)
                {
                    IEnumerable <string> errors = this.ModelState.Values.SelectMany(e => e.Errors.Select(m => m.ErrorMessage));
                    return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, "Formatting error", string.Join(Environment.NewLine, errors)));
                }

                int blockCount = request.BlockCount;

                if (blockCount <= 0)
                {
                    return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.Forbidden, "Invalid request", "The number of blocks to mine must be higher than zero."));
                }

                this.logger.LogTrace("({0}:{1})", nameof(request.BlockCount), blockCount);

                WalletAccountReference accountReference = this.GetAccount();
                HdAddress address = this.walletManager.GetUnusedAddress(accountReference);

                var generateBlocksModel = new GenerateBlocksModel
                {
                    Blocks = this.powMining.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)blockCount, int.MaxValue)
                };

                this.logger.LogTrace("(-):*.{0}={1}", "Generated block count", generateBlocksModel.Blocks.Count);

                return(this.Json(generateBlocksModel));
            }
            catch (Exception e)
            {
                this.logger.LogError(ExceptionOccurredMessage, e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }