哪个是对现实世界的映射?
注: 本系列文章是闭源情况下根据现有公开资料的黑盒分析,仅供参考。
DCEP(Digital Currency Electronic Payment)央行数字货币已进入内测期,很多同学与开发者都非常关心这件事。因此笔者决定着手一系列的文章,用谨慎的研究态度从技术的角度对 DCEP 进行黑盒分析,希望能给我们的开发实践提供一些借鉴意义。
UTXO(Unspent Transaction Output)模型与 Account 模型是用以记账的最流行的两种模型。
专业的定义:
大白话定义:
关于 UTXO 账户模型与 Account 模型的更具体的技术内容可以看如下链接:
也许你觉得我接下来要分析二者的优劣了。但别着急,让我们问一个更本质的问题:二者哪个映射了现实世界?
这个问题在我们进行计算机领域的学习与研究的时候至关重要。有的时候我们觉得某种设计「很怪」、「难以理解」的本质原因是因为它并非现实世界的 1:1 映射,而仅是一种仿真。
然后我们来编写程序。程序的结构要严格保持与问题的结构一致,即每一个 真实世界里的活动都严格映射到我们编程语言中的一个并发进程上。如果从问题 到程序的映射比例为 1:1,我们就说程序与问题是同构(isomorphic)的。
映射比例为 1:1 这一点非常重要。因为这样可以使得问题和解之间的概念隔 阂最小化。如果比例不为 1:1,程序就会很快退化而变得难以理解。在使用非面 向并发的编程语言来解决并发问题时,这种退化是非常常见的。在非面向并发的 编程语言中,为了解决一个问题,通常要由同一个语言级的线程或进程来强制控 制多个独立的活动,这就必然导致清晰性的损失,并且会使程序滋生复杂的、难 以复现的错误。
—— 《面对软件错误构建可靠的分布式系统 》by Joe Armstrong
一眼看去,Account 模型似乎更像现实世界的映射,因为按人头记账嘛,古已有之,但真的是这样吗?
让我们再往里走一层,思考一下计算机系统中的基本元素。我们会发现我们可以这样对计算机中的元素进行二分——「信息」与「资源」。
同样,我们生活中的元素也可以区分为信息和资源:
理想的情况下,现实中的资源映射到计算机中,应该依然是资源的形式;信息映射到计算机中,应该依然是信息的形式。
<pre style="margin: 10px 5px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; font-family: Roboto, "Courier New", Consolas, Inconsolata, Courier, monospace; font-size: 15px; line-height: 1.4em; display: block !important;"><section class="code-snippetfix code-snippetjs" style="margin: 10px 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 14px; color: rgb(51, 51, 51); position: relative; background-color: rgba(0, 0, 0, 0.03); border: 1px solid rgb(240, 240, 240); border-radius: 2px; display: flex; line-height: 26px;"><ul class="code-snippetline-index code-snippetjs" style="margin: 0px; padding: 1em; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; counter-reset: line 0; flex-shrink: 0; height: 106px; list-style-type: none;"><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li></ul><pre class="code-snippet__js" data-lang="" style="margin: 0px; padding: 1em 1em 1em 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; overflow-x: auto; white-space: normal; -webkit-box-flex: 1; flex: 1 1 0%;"><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 现实世界 计算机世界</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 信息 ————————> 信息</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important;"> 资源 ————————> 资源</span></code></pre></section></pre>
但是!事实并非如此。很多情况下,我们用计算机中的「信息」,仿真现实世界中的「资源」。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(51, 51, 51); font-size: 17px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><section class="code-snippetfix code-snippetjs" style="margin: 10px 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 14px; color: rgb(51, 51, 51); position: relative; background-color: rgba(0, 0, 0, 0.03); border: 1px solid rgb(240, 240, 240); border-radius: 2px; display: flex; line-height: 26px;"><ul class="code-snippetline-index code-snippetjs" style="margin: 0px; padding: 1em; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; counter-reset: line 0; flex-shrink: 0; height: 132px; list-style-type: none;"><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li></ul><pre class="code-snippet__js" data-lang="php" style="margin: 0px; padding: 1em 1em 1em 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; overflow-x: auto; white-space: normal; -webkit-box-flex: 1; flex: 1 1 0%;"><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 现实世界 计算机世界</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 信息 ————————> 信息</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ↑</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 资源 ———————————+</span></code></pre></section></pre>
例如,我们希望在游戏里的「屠龙宝刀」是一种数量有限的资源,实际上它是可以复制的数据,只不过游戏厂商用代码限制了它的复制。
相对于「映射」,「仿真」设计会带来很多不可控的因素。例如厂商说游戏里只能有十把屠龙宝刀,但是玩家通过特殊的方式可能造成第十一把的出现。
那么 UTXO 模型与 Account 模型,哪个是「映射」,哪个是「仿真」呢?
答案很有意思:
记账模型在很久很久以前就出现了,因此在设计一个计算机记账系统时,自然而然会想到将传统的纸质记账映射到计算机记账中。
但是 —— 传统的纸质记账并非是物理货币系统的映射,而是仿真。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(51, 51, 51); font-size: 17px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><section class="code-snippetfix code-snippetjs" style="margin: 10px 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 14px; color: rgb(51, 51, 51); position: relative; background-color: rgba(0, 0, 0, 0.03); border: 1px solid rgb(240, 240, 240); border-radius: 2px; display: flex; line-height: 26px;"><ul class="code-snippetline-index code-snippetjs" style="margin: 0px; padding: 1em; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; counter-reset: line 0; flex-shrink: 0; height: 80px; list-style-type: none;"><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li></ul><pre class="code-snippet__js" data-lang="" style="margin: 0px; padding: 1em 1em 1em 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; overflow-x: auto; white-space: normal; -webkit-box-flex: 1; flex: 1 1 0%;"><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important;"> 仿真 映射</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">物理货币系统 ——————> 传统纸质记账 ——————> Account 模型</span></code></pre></section></pre>
然后,钱币在物理货币中是「资源」,但在传统记账系统中就变成了「信息」,投射到 Account 模型中,自然还是信息。
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(51, 51, 51); font-size: 17px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><section class="code-snippetfix code-snippetjs" style="margin: 10px 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 14px; color: rgb(51, 51, 51); position: relative; background-color: rgba(0, 0, 0, 0.03); border: 1px solid rgb(240, 240, 240); border-radius: 2px; display: flex; line-height: 26px;"><ul class="code-snippetline-index code-snippetjs" style="margin: 0px; padding: 1em; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; counter-reset: line 0; flex-shrink: 0; height: 106px; list-style-type: none;"><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li></ul><pre class="code-snippet__js" data-lang="" style="margin: 0px; padding: 1em 1em 1em 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; overflow-x: auto; white-space: normal; -webkit-box-flex: 1; flex: 1 1 0%;"><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important;"> 仿真 映射</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">物理货币系统 ——————> 传统纸质记账 ——————> Account 模型</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 资源 —————> 信息 —————> 信息</span></code></pre></section></pre>
带来不可控因素的实际是「物理货币系统」到「记账系统」的仿真过程。因此我们可以看到,在手工记账年代会出现很多的纰漏与错误。后来有了计算机记账系统,这些纰漏与错误由于计算机的强大而被弥补,但新的系统构建的过程依然非常艰难与成本高昂——我们现在见到的银行的「计算机记账系统」是几十年不断摸索的产物。
所以,比特币诞生的时候,中本聪决定换种思路 —— 直接做物理货币系统的映射:
<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(51, 51, 51); font-size: 17px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><section class="code-snippetfix code-snippetjs" style="margin: 10px 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 14px; color: rgb(51, 51, 51); position: relative; background-color: rgba(0, 0, 0, 0.03); border: 1px solid rgb(240, 240, 240); border-radius: 2px; display: flex; line-height: 26px;"><ul class="code-snippetline-index code-snippetjs" style="margin: 0px; padding: 1em; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; counter-reset: line 0; flex-shrink: 0; height: 106px; list-style-type: none;"><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li><li style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; list-style-type: none; text-align: right;"></li></ul><pre class="code-snippet__js" data-lang="" style="margin: 0px; padding: 1em 1em 1em 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; overflow-x: auto; white-space: normal; -webkit-box-flex: 1; flex: 1 1 0%;"><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box; overflow-wrap: break-word !important;"> 映射</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;">物理货币系统 ——————> UTXO 模型</span></code><code style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important; text-align: left; font-size: 14px; white-space: pre; display: flex; position: relative; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;"><span class="code-snippet_outer" style="margin: 0px; padding: 0px; max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 资源 —————> 资源</span></code></pre></section></pre>
我把一块钱给小明,小明给小张,在 Account 模型里,是三人账户金额的加加减减;在 UTXO 模型中,则是这枚硬币在三个地址间发生转移的过程。
数据库没有账户,只有每一枚货币的转移旅程。
笔者的意思并非 UTXO 模型比 Account 模型更优,而是这两种模型来源于不同的角度。把握了这个本质性的区别,接下来我们便会更好地理解两种模型的差异,以及这两种模型分别适合哪些业务场景。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!