使用标签的 JSON-RPC 编程技巧
原始发布页
打开来源页面查看 1 个交叉存档
我添加了与标签有关的函数,以帮助管理每个用户的多个地址。新增或改名的函数如下: getreceivedbyaddress -- 单个地址收到的金额 getreceivedbylabel -- 带有此标签的所有地址收到的金额 listreceivedbyaddress -- 列出地址及其收到的金额 listreceivedbylabel -- 列出标签及其收到的金额 setlabel -- 为求完整而提供的其他标签函数 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(或者线性 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.