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

96
坠叶飘香
2018.08.03 17:55 字数 20

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;
}
比特币
Web note ad 1