美文网首页比特币源码学习笔记
比特币源码阅读(获取余额-获取钱包)

比特币源码阅读(获取余额-获取钱包)

作者: 坠叶飘香 | 来源:发表于2018-08-03 17:55 被阅读0次

    src/wallet/rpcwallet.cpp

    std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
    {
        std::string wallet_name;
        if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {//从request获得钱包名称
            std::shared_ptr<CWallet> pwallet = GetWallet(wallet_name); //通过名称获得钱包
            if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
            return pwallet;
        }
        std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();//获得所有钱包
        return wallets.size() == 1 || (request.fHelp && wallets.size() > 0) ? wallets[0] : nullptr; //满足条件情况下返回wallets[0] 
    }
    

    获得所有钱包

    static CCriticalSection cs_wallets;
    static std::vector<std::shared_ptr<CWallet>> vpwallets GUARDED_BY(cs_wallets); //线程安全?
    
    std::vector<std::shared_ptr<CWallet>> GetWallets()
    {
        LOCK(cs_wallets);
        return vpwallets;
    }
    

    通过钱包名称获得钱包

    std::shared_ptr<CWallet> GetWallet(const std::string& name)
    {
        LOCK(cs_wallets);
        for (const std::shared_ptr<CWallet>& wallet : vpwallets) { //遍历vector
            if (wallet->GetName() == name) return wallet;
        }
        return nullptr;
    }
    

    相关文章

      网友评论

        本文标题:比特币源码阅读(获取余额-获取钱包)

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