To build a more efficient peer-to-peer (P2P) network, the role of nodes on the network would be classified into two parts: heavyweight nodes and lightweight nodes. The most important and heavy tasks such as creating new blocks, verifying transactions and etc, will be assigned to these heavyweight nodes which owning high bandwidth capacity and strong computing power as well. In contrast, lightweight nodes merely support a very simple mode named the simplified payment verification (SPV) mode where only few privileges like paying and requesting its own receipt from neighbor heavyweight nodes are allowed to.
Q1: 为什么要将P2P网络中的节点划分为轻节点和重节点?
不妨把轻节点理解为物联网中算力带宽有限的智能节点,把重节点理解为Blockchain中带宽算力超强的矿机——基于这样的前提下:智能节点能够在区块链上参与付款等操作,但由于没有足够空间存放几十Giga的Blocks历史数据,算力也不足等原因,某些操作是无法实现的!比如:验证过去某个交易数据是否在主链上。由于节点之间实际功能上不对称,所以出现了分工协调的协议。
Q2: 智能节点可以参与付款没错,但自身足够空间下载和验证Blocks交易数据,那么,它是怎么知道自己付款成功了没有,怎么知道某些交易有没有在主链上?
很好,这是问题的关键!智能节点交易完成之后,自己是无法知道自己的交易数据,比如无法知道自己的交易数据是 Tx0 是否被成功写入区块的——唯一的办法是寻求邻居重节点的帮助:
邻居重节点要怎么帮助轻节点呢?整个Blocks数据全部丢给轻节点,体积太大太重了,轻节点受不了,怎么办?怎么办?怎么办?
一种办法:重节点可以丢个 A “filter lookup table of Blocks” 给轻节点,因为 “filter lookup table” 体积很小很轻,又包含了 Blocks中的 Transactions(不可能全部精准,天下没有免费的午餐,其本质是用耗费算力时间,损失精度为代价来换取空间上的缩小)。实现原理上讲:Filter 这种机制提供一种整个集合与某个元素进行哈希值碰撞的途径,一旦哈希值成功碰撞,说明该元素在这个集合,否则不在。另外,Filter 的安全的参数范围的控制,数学推导等等,还有很多东西,感兴趣可以探索学习下 “Bloom Filter” 的原理。
Q3: SPV机制为什么能提高的Blockchain性能?
总而言之,并不是每一个Blockchain的P2P网络上的节点都是用来挖矿的,有些节点只是想在P2P网络上进行一个交易,并且验证自个儿交易是否成功——这些对于SPV机制而言可以很好地满足。另外SPV机制的优点:某些验证操作不再需要下载整个区块数据,仅仅只需下载一个SPV的 lookup table——这减少了不必要的P2P网络带宽的浪费,这就是SPV机制最大的贡献。
Github相关项目
Bloom Filter: https://github.com/armon/bloomd
SPV Blockchain: https://github.com/keeshux/BitcoinSPV
欢迎关注“Aha实验室”微信公众号
参考
[1]. Gervais A, Capkun S, Karame G O, et al. On the privacy provisions of bloom filters in lightweight bitcoin clients[C]//Proceedings of the 30th Annual Computer Security Applications Conference. ACM, 2014: 326-335.
[2]. Almeida P S, Baquero C, Preguiça N, et al. Scalable bloom filters[J]. Information Processing Letters, 2007, 101(6): 255-261.