回复:JSON-RPC 方法构想:列出比给定 txid 更新的交易
原始发布页
打开来源页面查看 1 个交叉存档
以这种方式使用 listtransactions 并不安全。
我知道,我因为对 listtransactions 持谨慎态度而受到过批评。让我解释一下我为何有所顾虑。
交易是动态的。过去的交易可能变成未确认状态、消失后又回来、变得无效并消失,或者被另一笔双重支付交易取代。它们的日期可能改变,顺序也可能改变。
程序员自然而然会想这样使用 listtransactions:把自上次查询以来的新交易给我,我会自行保存它们的累计数或静态记录。在所有常规使用场景中,这似乎都能正常工作,但如果你把金额用于任何用途,这种做法就极易被利用:
- 你如何得知一笔过去的交易变得无效并消失了?
- 区块链发生重组时,交易再次得到确认,很容易被重复计算。
- 一笔交易可能被一笔 txid 不同的双重支付交易取代。你会把两笔支出都计算进去。
那种认为你只需查看新交易、因为先前交易已经看过的模型并不成立。旧交易随时都可能变化。
任何时候,只要你根据收到的付款金额采取行动,就始终需要回到 bitcoin 查询当前余额总额(或者使用 move 或 sendfrom),并为余额可能减少做好准备。
现在有了账户功能,可以更容易地按正确方式处理,我们也更适合加入 listtransactions 了。
英文原文
展开英文原文
It's not safe to use listtransactions this way.
I know I've been criticized for being reluctant about listtransactions. Let me explain my reluctance.
Transactions are dynamic. Past transactions can become unconfirmed, go away and come back, become invalid and disappear, or be replaced by a different double-spend. Their date can change, their order can change.
Programmers are naturally inclined to want to use listtransactions like this: feed me the new transactions since I last asked, and I'll keep my own tally or static record of them. This will seem to work in all regular use, but if you use the amounts for anything, it is highly exploitable:
- How do you know if a past transaction becomes invalid and disappears?
- When there's a block-chain reorg, it would be easy to double-count transactions when they get confirmed again.
- A transaction can be replaced by a double-spend with a different txid. You would count both spends.
The model where you assume you only need to see new transactions because you've already seen previous transactions is not true. Old transactions can change at any time.
Any time you take an action based on payment amounts received, you always need to go back to bitcoin and ask for a current balance total (or use move or sendfrom), and be ready for the possibility that it can go down.
Now that we have the Accounts feature making it easier to do it the right way, we're better prepared to have listtransactions.