예제 #1
0
        private NodeResponse PostNewTransaction()
        {
            var nodeResponse = new NodeResponse {
                DataRows = new Dictionary <string, string>()
            };

            Transaction transaction;

            try
            {
                transaction = this.Bind <Transaction>();
            }
            catch (ModelBindingException)
            {
                nodeResponse.HttpCode       = HttpStatusCode.BadRequest;
                nodeResponse.ResponseString = "Не удалось получить данные модели из запроса!";
                return(nodeResponse);
            }

            var validationErrors = new List <string>();

            if (string.IsNullOrEmpty(transaction.UserHash))
            {
                validationErrors.Add("Не передан хэш пользователя!");
            }

            if (transaction.Signature is null)
            {
                validationErrors.Add("Не передана подпись!");
            }

            if (transaction.Data is null)
            {
                validationErrors.Add("Не переданы данные!");
            }

            if (!transaction.Valid)
            {
                validationErrors.Add("Подпись не совпадает с переданным ключом!");
            }

            if (validationErrors.Count != 0)
            {
                Logger.Log.Error("Провести транзакцию не удалось!:\n" +
                                 $"{string.Join("\r\n", validationErrors)}");

                nodeResponse.HttpCode       = HttpStatusCode.BadRequest;
                nodeResponse.ResponseString = string.Join("\r\n", validationErrors);

                return(nodeResponse);
            }

            Machine.AddNewTransaction(transaction);
            if (!Machine.Pending)
            {
                NodeBalance.BroadcastNewBlock();
            }

            nodeResponse.HttpCode       = HttpStatusCode.OK;
            nodeResponse.ResponseString = "Added new transaction";

            return(nodeResponse);
        }