public IActionResult RemoveTransactions([FromQuery] RemoveTransactionsModel request)
        {
            Guard.NotNull(request, nameof(request));

            // Checks the request is valid.
            if (!this.ModelState.IsValid)
            {
                return(ModelStateErrors.BuildErrorResponse(this.ModelState));
            }

            try
            {
                HashSet <(uint256 transactionId, DateTimeOffset creationTime)> result;

                if (request.DeleteAll)
                {
                    result = this.walletManager.RemoveAllTransactions(request.WalletName);
                }
                else
                {
                    if (request.TransactionsIds == null || request.TransactionsIds.Any(trx => trx == null))
                    {
                        throw new WalletException("Transaction ids need to be specified if the 'all' flag is not set.");
                    }

                    IEnumerable <uint256> ids = request.TransactionsIds.Select(uint256.Parse);
                    result = this.walletManager.RemoveTransactionsByIdsLocked(request.WalletName, ids);
                }

                // If the user chose to resync the wallet after removing transactions.
                if (result.Any() && request.ReSync)
                {
                    // From the list of removed transactions, check which one is the oldest and retrieve the block right before that time.
                    DateTimeOffset earliestDate  = result.Min(r => r.creationTime);
                    ChainedHeader  chainedHeader = this.chain.GetBlock(this.chain.GetHeightAtTime(earliestDate.DateTime));

                    // Update the wallet and save it to the file system.
                    Wallet wallet = this.walletManager.GetWallet(request.WalletName);
                    wallet.SetLastBlockDetailsByCoinType(this.coinType, chainedHeader);
                    this.walletManager.SaveWallet(wallet);

                    // Start the syncing process from the block before the earliest transaction was seen.
                    this.walletSyncManager.SyncFromHeight(chainedHeader.Height - 1);
                }

                IEnumerable <RemovedTransactionModel> model = result.Select(r => new RemovedTransactionModel
                {
                    TransactionId = r.transactionId,
                    CreationTime  = r.creationTime
                });

                return(this.Json(model));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
Пример #2
0
 public async Task <IActionResult> RemoveTransactions([FromQuery] RemoveTransactionsModel request,
                                                      CancellationToken cancellationToken = default(CancellationToken))
 {
     return(await this.Execute(request, cancellationToken,
                               async (req, token) => this.Json(await this.walletService.RemoveTransactions(req, token))));
 }