美文网首页
比特币源码研读(22)-交易(8)交易流程分析

比特币源码研读(22)-交易(8)交易流程分析

作者: electroman | 来源:发表于2018-04-19 20:45 被阅读122次

    //确认交易

        SendConfirmationDialogconfirmationDialog(tr("Confirm send coins"),

           questionString.arg(formatted.join("<br />")),SEND_CONFIRM_DELAY, this);

        confirmationDialog.exec();

        QMessageBox::StandardButton retval =(QMessageBox::StandardButton)confirmationDialog.result();

        if(retval != QMessageBox::Yes)

        {

            fNewRecipientAllowed = true;

            return;

        }

    其中SendConfirmationDialog是一个类,用于确认是否发送比特币,如何返回的不是yes,则直接退出

           //发送交易

        // now send the prepared transaction

        WalletModel::SendCoinsReturn sendStatus =model->sendCoins(currentTransaction);

    SendCoinsReturn是一个结构体。其定义如下:

       // Return status record for SendCoins, contains error id + information

       struct SendCoinsReturn

        {

           SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed ="")

               : status(_status),

                 reasonCommitFailed(_reasonCommitFailed)

           {

           }

           StatusCode status;

           QString reasonCommitFailed;

    };

    从注释可以看出该结构体用于返回发送币的状态信息,返回的信息记录在变量sendStatus上。

           //处理交易时返回来的状态信息

        // process sendStatus and on error generatemessage shown to user

        processSendCoinsReturn(sendStatus);

    其中函数processSendCoinsReturn是处理状态信息

    其函数定义如下:

    voidSendCoinsDialog::processSendCoinsReturn(const WalletModel::SendCoinsReturn&sendCoinsReturn, const QString &msgArg)

    {

        QPair msgParams;

        // Default to a warning message, overrideif error message is needed

        msgParams.second =CClientUIInterface::MSG_WARNING;

        // This comment is specific toSendCoinsDialog usage of WalletModel::SendCoinsReturn.

        // WalletModel::TransactionCommitFailed isused only in WalletModel::sendCoins()

        // all others are used only inWalletModel::prepareTransaction()

        //

        switch(sendCoinsReturn.status)

        {

        case WalletModel::InvalidAddress:                    //无效地址

            msgParams.first = tr("Therecipient address is not valid. Please recheck.");

            break;

        case WalletModel::InvalidAmount:                    //无效的数额

            msgParams.first = tr("The amountto pay must be larger than 0.");

            break;

        case WalletModel::AmountExceedsBalance:             //发送数额超过了余额

            msgParams.first = tr("The amountexceeds your balance.");

            break;

        case WalletModel::AmountWithFeeExceedsBalance:        //包括交易费在内的总额超过了余额

            msgParams.first = tr("The totalexceeds your balance when the %1 transaction fee isincluded.").arg(msgArg);

            break;

        case WalletModel::DuplicateAddress:                             //发现了双重地址,每个uxto之能有一个地址

            msgParams.first = tr("Duplicateaddress found: addresses should only be used once each.");

            break;

        caseWalletModel::TransactionCreationFailed:                 //交易创建失败

            msgParams.first = tr("Transactioncreation failed!");

            msgParams.second = CClientUIInterface::MSG_ERROR;

            break;

        case WalletModel::TransactionCommitFailed:                         //交易确认失败

            msgParams.first = tr("Thetransaction was rejected with the following reason:%1").arg(sendCoinsReturn.reasonCommitFailed);

            msgParams.second = CClientUIInterface::MSG_ERROR;

            break;

        case WalletModel::AbsurdFee:                         //交易费超过交易额的1%就会被认为是荒谬的过高的交易费而被拒绝

            msgParams.first = tr("A fee higherthan %1 is considered an absurdly highfee.").arg(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(),maxTxFee));

            break;

        caseWalletModel::PaymentRequestExpired:  //交易请求过期

            msgParams.first = tr("Paymentrequest expired.");

            msgParams.second =CClientUIInterface::MSG_ERROR;

            break;

        // included to prevent a compiler warning.

        case WalletModel::OK:

        default:

            return;

        }

        Q_EMIT message(tr("Send Coins"),msgParams.first, msgParams.second);

    }

      if (sendStatus.status == WalletModel::OK)

        {

            accept();

            CoinControlDialog::coinControl->UnSelectAll();

            coinControlUpdateLabels();

        }

        fNewRecipientAllowed = true;

    当交易状态返回的是WalletModel::OK,进行

    至此:on_sendButton_clicked函数分析完毕。

    区块链研习社比特币源码研读班  electroman

    QYB地址:QVR2eUwbx43YMkWjbaAQCYdoDmpC1ohnRk

    相关文章

      网友评论

          本文标题:比特币源码研读(22)-交易(8)交易流程分析

          本文链接:https://www.haomeiwen.com/subject/dcsgkftx.html