原始文献 / 论坛帖 / JSON-RPC programming tips using labels
JSON-RPC programming tips using labels
Bitcointalk · 2010-05-26 18:27:25 UTC
我加了跟 label 相关的函数,方便管理每个用户下的多个地址。新增或改名的函数有:
getreceivedbyaddress -- 单个地址收到的金额
getreceivedbylabel -- 该 label 下所有地址收到的金额
listreceivedbyaddress -- 列出地址及各自收到的金额
listreceivedbylabel -- 列出 label 及各自收到的金额
setlabel -- 为完整起见的杂项 label 函数
getlabel
getaddressesbylabel
为保持一致,我把 getamountreceived 改名为 getreceivedbyaddress、getallreceived 改名为 listreceivedbyaddress。旧名字还留着,免得破坏现有代码,但已废弃。
思路是:每次调用 getnewaddress 时都带上用户名,就可以用 "bylabel" 函数查到该用户所有地址收到的总额。你可以随意更换他的地址,不用操心跟踪他所有的旧地址。
自动更换用户收款地址的好办法:在展示他当前地址之前,先查一下这个地址有没有收到过东西,如果有就换个新的:
// Get a new address whenever the current one has received anything
if (strAddr == "" || getreceivedbyaddress(strAddr) > 0)
strAddr = getnewaddress(strUsername); // Label the address with username
Display(strAddr); // Display their current receiving address
// Get total received by all the user's addresses
getreceivedbylabel(strUsername, 0) // unconfirmed
getreceivedbylabel(strUsername, 1) // available balance
如果只是查某个特定用户的余额,比如响应他的一次页面请求,用 getreceivedbylabel;但如果是遍历所有用户,用 listreceivedbylabel 一次拿到完整列表再扫更好。用 getreceivedbylabel 去扫用户是 n 平方,用 listreceivedbylabel 是 n log n(或线性)。
只有当你需要轮询、收到钱就自动触发动作,而不是用户自己上网页看余额、再告诉你怎么处理时,才需要扫全用户。轮询也不用太频繁。如果你要求 1 个确认,平均也要 10 分钟,所以几分钟轮询一次以上没有意义。
如果你卖的是数字商品和服务,损失不大、被白嫖也转卖不了牟利,我觉得接受 0 确认没问题。
只有卖黄金或货币这种,才需要多个确认。
英文原文
I added label related functions to help with managing multiple addresses per user. New or renamed functions are:
getreceivedbyaddress -- amount received on a single address
getreceivedbylabel -- amount received by all addresses with this label
listreceivedbyaddress -- list addresses and amounts they've received
listreceivedbylabel -- list labels and amounts they've received
setlabel -- misc label functions for completeness
getlabel
getaddressesbylabel
For consistency I renamed getamountreceived->getreceivedbyaddress and getallreceived->listreceivedbyaddress. The old names are still there so as not to break existing code, but they're deprecated.
The idea is that if you give the username whenever you call getnewaddress, you can get the user's total received across all their addresses using the "bylabel" functions. You can freely change their address without worrying about tracking all their old addresses.
A good way to automate changing the user's receiving address: just before displaying their current address, check if it has been used to receive anything, if it has then replace it with a new one:
// Get a new address whenever the current one has received anything
if (strAddr == "" || getreceivedbyaddress(strAddr) > 0)
strAddr = getnewaddress(strUsername); // Label the address with username
Display(strAddr); // Display their current receiving address
// Get total received by all the user's addresses
getreceivedbylabel(strUsername, 0) // unconfirmed
getreceivedbylabel(strUsername, 1) // available balance
If you're just getting one particular user's balance, such as in response to a page request by that user, use getreceivedbylabel, but if you're scanning over all users, it's better to use listreceivedbylabel to get the complete list and scan against the result. Scanning users with getreceivedbylabel would be n-squared, using listreceivedbylabel is n-log-n (or n linear).
You should only really need to scan all users if you're polling in order to spontaneously take action in response to money received, rather than the user going to a webpage, seeing their balance and telling you what to do with it. It's not necessary to poll very frequently. If you require 1 confirmation, that'll take an average of 10 minutes anyway, so there's no point in polling more often than every few minutes.
If you're selling digital goods and services, where you don't lose much if someone gets a free access, and it can't be resold for profit, I think you're fine to accept 0 confirmations.
It's mostly only if you were selling gold or currency that you'd need multiple confirmations.
