比特币 RPC 命令「getwalletinfo」
$ bitcoin-cli help getwalletinfo getwalletinfo 返回一个各种钱包状态信息的对象。 结果: { "walletversion": xxxxx, (数字)钱包版本 "balance": xxxxxxx, (数字)钱包中以 BTC 为单位已确认的总余额 "unconfirmed_balance": xxx,(数字)钱包中以 BTC 为单位未确认的总余额 "immature_balance": xxxxxx,(数字)钱包中以 BTC 为单位未成熟的总余额 "txcount": xxxxxxx, (数字)钱包中交易总数 "keypoololdest": xxxxxx, (数字)密钥池中最早预生成密钥(从格林尼治时间开始以秒为单位)的时间戳 "keypoolsize": xxxx, (数字)预生成新密钥的数量 "unlocked_until": ttt, (数字)从格林尼治时间(1970-01-01 午夜)开始以秒为单位钱包用于转账的解锁时间戳,若钱包被锁定则为 0 "paytxfee": x.xxxx, (数字)交易费配置,以 BTC/kB 为单位设置 } 例子: > bitcoin-cli getwalletinfo > curl --user myusername:mypassword --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getwalletinfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
源码剖析
getwalletinfo
对应的函数在文件 rpcserver.h
中被引用。
extern UniValue getwalletinfo(const UniValue& params, bool fHelp);
实现在文件 rpcwallet.cpp
中。
UniValue getwalletinfo(const UniValue& params, bool fHelp)
{
if (!EnsureWalletIsAvailable(fHelp)) // 1. 确保钱包可用
return NullUniValue;
if (fHelp || params.size() != 0)
throw runtime_error(
"getwalletinfo\n"
"Returns an object containing various wallet state info.\n"
"\nResult:\n"
"{\n"
" \"walletversion\": xxxxx, (numeric) the wallet version\n"
" \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
" \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
" \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + "\n"
" \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
" \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since GMT epoch) of the oldest pre-generated key in the key pool\n"
" \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated\n"
" \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n"
" \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getwalletinfo", "")
+ HelpExampleRpc("getwalletinfo", "")
); // 2. 帮助内容
LOCK2(cs_main, pwalletMain->cs_wallet);
UniValue obj(UniValue::VOBJ); // 3. 各种钱包信息
obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance())));
obj.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwalletMain->GetUnconfirmedBalance())));
obj.push_back(Pair("immature_balance", ValueFromAmount(pwalletMain->GetImmatureBalance())));
obj.push_back(Pair("txcount", (int)pwalletMain->mapWallet.size()));
obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime()));
obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize()));
if (pwalletMain->IsCrypted())
obj.push_back(Pair("unlocked_until", nWalletUnlockTime));
obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
return obj;
}
1. 确保钱包可用
参考比特币 RPC 命令「fundrawtransaction」1. 确保钱包可用。
2. 帮助内容
参考比特币 RPC 命令「getbestblockhash」1. 帮助内容。