跳至主要内容

产品随想 | 读《精通以太坊——实现数字合约》 Mastering Ethereum


快速术语表

  • 断言 Assert
    在 Solidity 中,assert(false) 编译为 0xfe, 是一个无效的操作码,用尽所有剩余的燃气(Gas),并恢复所有更改。 当 assert() 语句失败时,说明发生了严重的错误或异常,你必须修复你的代码。 你应该使用 assert 来避免永远不应该发生的条件。

  • 去中心化应用 DApp
    去中心化应用 Decentralised Application. 狭义上,它至少是智能合约和 web 用户界面。更广泛地说,DApp 是一个基于开放式,分散式,点对点基础架构服务的 Web 应用程序。另外,许多 DApp 包括去中心化存储和/或消息协议和平台。

  • 以太坊虚拟机 EVM
    Ethereum Virtual Machine, 基于栈的,执行字节码的虚拟机。在以太坊中,执行模型指定了系统状态如何在给定一系列字节码指令和少量环境数据的情况下发生改变。 这是通过虚拟状态机的正式模型指定的。

  • 燃气 Gas
    以太坊用于执行智能合约的虚拟燃料。以太坊虚拟机使用会计机制来衡量天然气的消耗量并限制计算资源的消耗。参见“图灵完备”。 燃气是执行智能合约的每条指令产生的计算单位。燃气与以太加密货币挂钩。燃气类似于蜂窝网络上的通话时间。因此,以法定货币进行交易的价格是 gas (ETH /gas)(法定货币/ETH)。

  • Gavin Wood
    Gavin Wood 是英国的程序员,以太坊的联合创始人和前 CTO。在2014年8月他提出了Solidity,用于编写智能合约的面向合约的编程语言。

  • 图灵完备 Turing Complete
    在计算理论中,如果数据操纵规则(如计算机的指令集,程序设计语言或细胞自动机)可用于模拟任何图灵机,则它被称为图灵完备或计算上通用的。这个概念是以英国数学家和计算机科学家阿兰图灵命名的。

第一章 什么是以太坊

  • 从计算机科学的角度来说,以太坊是一种确定性但实际上无界的状态机,它有两个基本功能,第一个是全局可访问的单例状态,第二个是对状态进行更改的虚拟机。

  • 与具有非常有限的脚本语言的比特币不同,以太坊被设计成一个通用可编程区块链,运行一个_虚拟机_,能够执行任意和无限复杂的代码。比特币的脚本语言故意被限制为简单的真/假消费条件判断,以太坊的语言是_图灵完备的_,这意味着它相当于一台通用计算机,可以运行理论图灵机可以运行的任何计算。

  • 一些参考文献
    以太坊黄皮书: https://ethereum.github.io/yellowpaper/paper.pdf
    褐皮书”:为更广泛的读者以不太正式的语言重写了“黄皮书”: https://github.com/chronaeon/beigepaper
    ÐΞVp2p 网络协议: https://github.com/ethereum/wiki/wiki/ÐΞVp2p-Wire-Protocol
    以太坊状态机 —— 一个“Awesome”资源列表 https://github.com/ethereum/wiki/wiki/Ethereum-Virtual-Machine-(EVM)-Awesome-List
    LevelDB 数据库 (最经常用于存储区块链本地副本): http://leveldb.org
    Merkle Patricia Trees: https://github.com/ethereum/wiki/wiki/Patricia-Tree
    Ethash 工作量证明共识算法: https://github.com/ethereum/wiki/wiki/Ethash
    Casper 权益证明 v1 实现指南: https://github.com/ethereum/research/wiki/Casper-Version-1-Implementation-Guide
    Go-Ethereum (Geth) 客户端: https://geth.ethereum.org/
    Parity 以太坊客户端: https://parity.io/

  • 以太坊的突破性创新是将存储程序计算机的通用计算架构与去中心化区块链相结合,从而创建分布式单状态(单例)世界计算机。以太坊程序“到处”运行,但却产生了共识规则所保证的共同(共识)状态。

  • 在世界计算机中,滥用资源的程序会滥用世界资源。如果以太坊无法预测资源使用情况,以太坊如何限制智能合约使用的资源?
    为了应对这一挑战,以太坊引入了称为 燃气 _gas_的计量机制。随着EVM执行智能合约,它会仔细考虑每条指令(计算,数据访问等)。每条指令都有一个以燃气为单位的预定成本。当交易触发智能合约的执行时,它必须包含一定量的燃气,用以设定运行智能合约可消耗的计算上限。
    ——需要Gas机制的原因

第2章 以太坊基础

  • 事实上,加密货币功能是服务于以太坊作为世界计算机的功能; 一个去中心化的智能合约平台。以太旨在用于支付运行的 smart contracts,这是在称为_Ethereum虚拟机(EVM)_的模拟计算机上运行的计算机程序。
    ——讲的有点绕

  • EVM是一个全球性的单例,这意味着它的运作方式就好像它是一个全球性的单实例计算机,无处不在。以太坊网络上的每个节点运行EVM的本地副本以验证合约执行,而以太坊区块链记录此世界计算机在处理交易和智能合约时变化的 状态。

  • 我们在MetaMask钱包中创建的账户类型称为 Externally Owned Account(EOA) 。外部所有账户是那些拥有私人密钥的账户,它控制对资金或合约的访问。现在,你可能猜测还有另一种帐户,_合约_帐户。合约账户由以太坊区块链记录,由EVM执行的软件程序的逻辑所拥有(和控制)。

第3章 以太坊客户端

  • 流行的移动钱包包括Jaxx,Status和Trust Wallet。我们列举这些作为流行手机钱包的例子(不是对这些钱包的安全或功能的认可)。

第4章 以太坊测试网(Testnets)

  • 测试网络(简称testnet)用于模拟以太网主网的行为。有一些公开的测试网络可以替代以太坊区块链。这些网络上的货币毫无价值,但它们仍然很有用,因为合约和协议变更的功能可以在不中断以太网主网或使用真实货币的情况下进行测试。

  • Proof-of-Authority 是一种协议,它只将造币的负载分配给授权和可信的签名者,他们可以根据自己的判断并随时以发币频率分发新的区块。

第5章 密钥,地址

  • 这些类型的密码学证明是以太坊和大多数区块链系统的关键数学工具,广泛用于以太坊应用。讽刺的是,加密并不是以太坊的重要组成部分,因为它的通信和交易数据没有加密,也不需要加密以保护系统。
    ——着实讽刺

第6章 钱包

  • 在较高层次上,钱包是作为主要用户界面的应用程序。钱包控制对用户资金的访问,管理密钥和地址,追踪余额以及创建和签署交易。另外,一些以太坊钱包还可以与合约(如代币)进行交互。

  • 狭义上讲,从程序员的角度来看,“钱包”一词是指用于存储和管理用户密钥的系统。每个“钱包”都有一个密钥管理组件。对于一些钱包来说,这就是全部。其他一些钱包是更广泛类别的一部分,即“浏览器”,它是以太坊去中心化应用或“DApps”的接口。在“钱包”这个术语下混合的各种类别之间没有明确的区别。

  • 关于以太坊的一个常见误解是以太坊钱包包含ether或代币。实际上,钱包只包含密钥。ether或其他代币记录在以太坊区块链中。用户通过使用钱包中的密钥签署交易来控制网络上的代币。从某种意义上说,以太坊钱包是一个 钥匙串 keychain

第7章 交易

  • 看待交易的另一种方式是,它们是唯一可触发状态更改或导致合约在EVM中执行的东西。以太坊是一个全球的单实例状态机器,交易是唯一可以让状态机“运动”,改变状态的东西。合约不会自行运行。以太坊不会在后台运行。一切都始于交易。

第8章 智能合约

  • 以太坊有两种不同类型的账户:外部所有账户(EOAs)和合约账户。EOAs由以太坊以外的软件(如钱包应用程序)控制。合约帐户由在以太坊虚拟机(EVM)内运行的软件控制。两种类型的帐户都通过以太坊地址标识。在本节中,我们将讨论第二种类型,合约账户和控制它们的软件:智能合约。

  • 计算机程序:智能合约只是计算机程序。合约这个词在这方面没有法律意义。 不可变的:一旦部署,智能合约的代码不能改变。与传统软件不同,修改智能合约的唯一方法是部署新实例。 确定性的:智能合约的结果对于运行它的每个人来说都是一样的,包括调用它们的交易的上下文,以及执行时以太坊区块链的状态。 EVM上下文:智能合约以非常有限的执行上下文运行。他们可以访问自己的状态,调用它们的交易的上下文以及有关最新块的一些信息。 去中心化的世界计算机:EVM在每个以太坊节点上作为本地实例运行,但由于EVM的所有实例都在相同的初始状态下运行并产生相同的最终状态,因此整个系统作为单台世界计算机运行。

  • 重要的是,如果合约只有被交易调用时才会运行。以太坊的所有智能合约均由EOA发起的交易执行。合约可以调用另一个合约,其中又可以调用另一个合约,等等。但是这种执行链中的第一个合约必须始终由EOA的交易调用。合约永远不会“自行”运行,或“在后台运行”。在交易触发执行,直接或间接地作为合约调用链的一部分之前,合约在区块链上实际上是“休眠”的。

  • 合约的代码不能更改。然而合约可以被“删除”,从区块链上删除代码和它的内部状态(变量)。要删除合约,你需要执行称为 SELFDESTRUCT(以前称为 SUICIDE )的EVM操作码,该操作码将区块链中的合约移除。该操作花费“负的gas”,从而激励储存状态的释放。以这种方式删除合约不会删除合约的交易历史(过去),因为区块链本身是不可变的。但它确实会从所有未来的区块中移除合约状态。

  • 智能合约给程序员带来了很大的负担:错误会花费金钱。因此,编写不会产生意想不到的影响的智能合约至关重要。要做到这一点,你必须能够清楚地推断程序的预期行为。因此,声明式语言在智能合约中比在通用软件中扮演更重要的角色。不过,正如你将在下面看到的那样,最丰富的智能合约语言是命令式的(Solidity)。

第9章 开发工具,框架和库

第10章 Tokens

  • 单词_Token_来源于古英语“tacen”,意思是符号或符号。常用来表示私人发行的类似硬币的物品,价值不大,例如交通Token,洗衣Token,游乐场Token。

第11章 去中心化应用 (DApps)

  • 智能合约用于存储去中心化应用程序的业务逻辑,状态和计算; 将智能合约视为常规应用程序中的服务器端组件。

  • 在以太坊智能合约上部署服务器端逻辑的一个优点是,你可以构建一个更复杂的架构,智能合约可以在其中相互读取和写入数据。部署智能合约后,未来许多其他开发人员都可以使用你的业务逻辑,而无需你管理和维护代码。

  • 由于gas成本高,智能合约目前不适合存储大量数据。因此,大多数DApps将利用去中心化存储(如IPFS或Swarm)来存储和分发大型静态资产,如图像,视频和客户端应用程序(HTML,CSS,JavaScript)。

第12章 Oracles

  • 许多区块链应用程序需要访问外部信息。这就是oracles发挥作用的地方。可以将Oracles定义为离线数据的权威来源,允许智能合约使用外部信息接收和条件执行 - 它们可以被视为弥合链上链下之间鸿沟的机制。允许智能合约根据实际事件和数据强制执行合约关系,大大扩展了其范围。可能由oracles提供的数据示例包括:
    来自物理来源的随机数/熵(例如量子/热现象):公平地选择彩票智能合约中的赢家
    与自然灾害相关的参数触发器:触发巨灾债券智能合约(对于飓风债券来说的风速)
    汇率数据:将稳定币与法定货币准确挂钩
    资本市场数据:代币化资产/证券的定价篮子
    基准参考数据:将利率纳入智能金融衍生品
    静态/伪静态数据:安全标识符,国家/地区代码,货币代码
    时间和间隔数据:事件触发器以精确的SI时间测量为基础
    天气数据:基于天气预报的保险费计算
    政治事件:预测市场决议
    体育赛事:预测市场决议和幻想体育合约
    地理位置数据:供应链跟踪
    损害赔偿:保险合约
    其他区块链上发生的事件:互操作函数
    交易天然气价格:gas价格oracles
    航班延误:保险合约

第13章 gas(Gas)

  • Gas是以太坊用于衡量程序执行一个或一组动作所需计算量的单位。交易或合约执行的每项操作都需要一定数量的gas; 所需的gas数量与正在执行的计算步骤的类型和数量有关。与仅以千字节(kB)计算交易规模的比特币交易费相比,以太坊交易费必须考虑智能合约代码可以执行的任意数量的计算步骤。程序执行的操作数越多,运行完成的成本就越高。

  • 虽然gas有价格,但它不能“拥有”也不能“花”。gas仅存在于以太坊虚拟机(EVM)内部,作为计算工作量的计数。发起方被收取ether交易费,然后转换为gas,然后转回到ether,作为矿工的块奖励。这些转换步骤用于将计算的价格(与“工作量”相关)与ether的价格(与市场波动相关)分开。

第14章 以太坊虚拟机

  • 实际处理内部状态和计算的协议部分称为以太坊虚拟机(EVM)。从实际角度来看,EVM可以被认为是包含数百万个对象的大型去中心化计算机。

第15章 共识

  • 共识度量是可测量的数据,区块链网络的节点必须在该数据上达成一致,以便为每个块中包含的数据创建并保持一致。在区块链技术中,每次将新块添加到链中时,每个网络节点都会测量并批准一致性度量。作为共识度量的结果,区块链充当了从一个确定可验证的事实延伸到下一个事实的真理链。由于共识度量,区块链协议的节点变为 迷你公证人 mini-notaries,能够从真实的节点中立即分辨出区块链的错误副本,并将该事实报告给整个网络。这些措施是必需的,以便阻止通过提交包含虚假信息的区块来欺骗网络不良行为者。由于一致性度量,区块链不仅创建了的完整性,而且长期保持不变。共识度量有多种形式,但对于此讨论而言,最重要的两种是基于风险的度量和基于工作量的度量。

  • 授权证明(PoA)是PoS一致性算法的子集,主要由测试网和私有或联盟网络使用。在基于PoA的区块链中,交易有效性最终由一组经批准的链上账户确定,称为“授权节点”。确定授权节点的标准是通过网络治理结构中编写的方法确定性地决定的。

第16章 Vyper: 面向合约的编程语言

第17章 节点间的通信 —— 一个简单的视角

第18章 Appendix A: 以太坊标准

第19章 以太坊分叉历史

Popular posts from 产品随想的博客

Interview at the All Things Digital D5 Conference, Steve and Bill Gates spoke with journalists Kara Swisher and Walt Mossberg onstage in May 2007.

Kara Swisher: The first question I was interested in asking is what you think each has contributed to the computer and technology industry— starting with you, Steve, for Bill, and vice versa. Steve Jobs: Well, Bill built the first software company in the industry. And I think he built the first software company before anybody really in our industry knew what a software company was, except for these guys. And that was huge. That was really huge. And the business model that they ended up pursuing turned out to be the one that worked really well for the industry. I think the biggest thing was, Bill was really focused on software before almost anybody else had a clue that it was really the software that— KS: Was important? SJ: That’s what I see. I mean, a lot of other things you could say, but that’s the high-order bit. And I think building a company’s really hard, and it requires your greatest persuasive abilities to hire the best ...

Commencement Address at Stanford University--“Stay hungry. Stay foolish.”

I am honored to be with you today for your commencement from one of the finest universities in the world. Truth be told— I never graduated from college. This is the closest I’ve ever gotten to a college graduation. Today I want to tell you three stories from my life. That’s it. No big deal. Just three stories. The first story is about connecting the dots. I dropped out of Reed College after the first six months but then stayed around as a drop-in for another eighteen months or so before I really quit. So why did I drop out? It started before I was born. My biological mother was a young, unwed graduate student, and she decided to put me up for adoption. She felt very strongly that I should be adopted by college graduates, so everything was all set for me to be adopted at birth by a lawyer and his wife. Except that when I popped out they decided at the last minute that they really wanted a girl. So my parents, who were on a waiting...

产品随想 | 周刊 第43期:历史上的今天

Products Huberman Lab   https://hubermanlab.com/ 一款聚焦于健康的播客 今日热榜   https://tophub.today/ 聚合展示,国内各热门榜单,对跟进热点非常有帮助,热点运营的好帮手 SketchyBar   https://github.com/FelixKratz/SketchyBar A highly customizable macOS status bar replacement Mac菜单栏定制 自定义程度很高,看作者展示的案例,暂时没想出这样的好处(不过应用本身的编辑,确实也没啥意义)生命在于折腾吧! Thanks-Mirror   https://github.com/eryajf/Thanks-Mirror 整理记录各个包管理器,系统镜像,以及常用软件的好用镜像,Thanks Mirror。 Musicn   https://github.com/zonemeen/musicn 一个下载高品质音乐的命令行工具,音乐来源: 咪咕 Planet Minecraft A creative Minecraft community fansite sharing maps, minecraft skins, resource packs, servers, mods, and more. 里面有很多动人的故事 可能是世界上最大的Minecraft社区,从2010年至今 The Uncensored Library   https://www.uncensoredlibrary.com/en blockworks   https://www.blockworks.uk/ "Distinctive maps for Minecraft that have educated players and risen to the level of art" 游戏也可以让人有更高的实现,而不仅仅是沉迷其中,国外游戏厂商比我们做的好太多 Minecraft_Memory_Bypass_GUI   https://github.com/xingchuanzhen/Minecraft_Memory_Bypass_GUI 绕过Minecraft...

巴菲特致股东信-1976年

 笔记: 为什么选择轻资产行业:当竞争疯狂时,不会强迫加入降价大战 最终选择了费雪的思想,选择能理解的优秀企业,以合理的价格买入并长期拥有 翻译: 雪球:https://xueqiu.com/6217262310/131440258 备份:https://archive.ph/XLK0S 原文: To the Stockholders of Berkshire Hathaway Inc, After two dismal years, operating results in 1976 improved significantly. Last year we said the degree of progress in insurance underwriting would determine whether our gain in earnings would be "moderate" or "major." As it turned out, earnings exceeded even the high end of our expectations. In large part, this was due to the outstanding efforts of Phil Liesche's managerial group at National Indemnity Company. In dollar terms, operating earnings came to $16,073,000, or $16.47 per share. While this is a record figure, we consider return on shareholders' equity to be a much more significant yardstick of economic performance. Here our result was 17.3%, moderately above our long-term average and even further above the average o...

巴菲特致股东信-1973年

 笔记: 在上一年度预测的今年竞争加剧导致利润下滑,真的发生了 翻译Link: 雪球:https://xueqiu.com/6217262310/131257618 备份:https://archive.ph/KIfdT 原文: To the Stockholders of Berkshire Hathaway Inc.: Our financial results for 1973 were satisfactory, with operating earnings of $11,930,592, producing a return of 17.4% on beginning stockholders' equity. Although operating earnings improved from $11.43 to $12.18 per share, earnings on equity decreased from the 19.8% of 1972. This decline occurred because the gain in earnings was not commensurate with the increase in shareholders' investment. We had forecast in last year's report that such a decline was likely. Unfortunately, our forecast proved to be correct. Our textile, banking, and most insurance operations had good years, but certain segments of the insurance business turned in poor results. Overall, our insurance business continues to be a most attractive area in which to employ capital. Management'...

巴菲特致股东信-1975年

 笔记: 华盛顿邮报已成为伯克希尔第一重仓股 翻译: 雪球:https://xueqiu.com/6217262310/131409324 备份:https://archive.ph/4hgK3 原文: To the Stockholders of Berkshire Hathaway Inc.: Last year, when discussing the prospects for 1975, we stated “the outlook for 1975 is not encouraging.” This forecast proved to be distressingly accurate. Our operating earnings for 1975 were $6,713,592, or $6.85 per share, producing a return on beginning shareholders ’ equity of 7.6%. This is the lowest return on equity experienced since 1967. Furthermore, as explained later in this letter, a large segment of these earnings resulted from Federal income tax refunds which will not be available to assist performance in 1976. On balance, however, current trends indicate a somewhat brighter 1976. Operations and prospects will be discussed in greater detail below, under specific industry titles. Our expectation is that significantly better results in textiles, earnings added from recent acquisitio...

Steve Jobs introduced the iPhone on January 9, 2007.

This is a day I’ve been looking forward to for two and a half years. Link Every once in a while, a revolutionary product comes along that changes everything. And Apple has been— well, first of all, one’s very fortunate if you get to work on just one of these in your career. Apple’s been very fortunate. It’s been able to introduce a few of these into the world. In 1984, we introduced the Macintosh. It didn’t just change Apple, it changed the whole computer industry. In 2001, we introduced the first iPod, and it didn’t just change the way we all listen to music, it changed the entire music industry. Well, today, we’re introducing three revolutionary products of this class. The first one is a widescreen iPod with touch controls. The second is a revolutionary mobile phone. And the third is a breakthrough internet communications device. So, three things: a widescreen iPod with touch controls; a revolutionary mobile phone; and a breakthrough internet communicat...

产品随想 | 周刊 第90期:史家之绝唱,无韵之离骚

Why AI Will Save the World   https://a16z.com/2023/06/06/ai-will-save-the-world/ Marc Andreessen的雄文,十分有說服力,邏輯清晰 辯證了現今AI監管拋出的5個可能的AI問題 讀的過程中,腦海裏浮現的都是編程隨想那篇文章 为什么马克思是错的?——全面批判马列主义的知名著作导读   https://program-think.blogspot.com/2018/09/Book-Review-The-Errors-of-Marxism-Leninism.html 兩者的思維鏈條、敘事方式,非常相似 人民聖殿教   https://zh.wikipedia.org/zh-hk/人民圣殿教?useskin=vector 瓊斯自稱是神的化身,幾千年前轉世為釋迦牟尼,創建了佛教;後來又轉世為耶穌基督,創建了基督教;之後短期化身轉世為巴孛,建立巴哈伊信仰;最後轉世為列寧,將社會主義發揚光大。 邪教徒聲稱自己轉世成了列寧,這說明了什麼? Apple Vision   https://stratechery.com/2023/apple-vision Omnivore   https://github.com/omnivore-app/omnivore Omnivore is a complete, open source read-it-later solution for people who like reading. How the YouTube Algorithm Works in 2023: The Complete Guide   https://blog.hootsuite.com/how-the-youtube-algorithm-works/#A_brief_history_of_the_YouTube_algorithm 外人眼中的YouTube推薦算法變遷 Histography   https://histography.io/ “Histography" is interactive timeline that spans across 14 billion years of history, f...

巴菲特致股东信-1974年

 笔记: 价格战企业的逻辑:需要降价获取销量--->需要降低成本--->怎么降?扩大规模以摊低成本--->提高固定资产投入--->净资产回报率会降低 翻译: 雪球:https://xueqiu.com/6217262310/131257947 备份:https://archive.ph/5CEP6 原文: To the Stockholders of Berkshire Hathaway Inc.: Operating results for 1974 overall were unsatisfactory due to the poor performance of our insurance business. In last year's annual report some decline in profitability was predicted but the extent of this decline, which accelerated during the year, was a surprise. Operating earnings for 1974 were $8,383,576, or $8.56 per share, for a return on beginning shareholders' equity of 10.3%. This is the lowest return on equity realized since 1970. Our textile division and our bank both performed very well, turning in improved results against the already good figures of 1973. However, insurance underwriting, which has been mentioned in the last several annual reports as running at levels of unsustainable profitability, turned dramatically worse...

Interview with Steve Jobs, WGBH, 1990

Interviewer: what is it about this machine? Why is this machine so interesting? Why has it been so influential? Jobs: Ah ahm, I'll give you my point of view on it. I remember reading a magazine article a long time ago ah when I was ah twelve years ago maybe, in I think it was Scientific American . I'm not sure. And the article ahm proposed to measure the efficiency of locomotion for ah lots of species on planet earth to see which species was the most efficient at getting from point A to point B. Ah and they measured the kilocalories that each one expended. So ah they ranked them all and I remember that ahm...ah the Condor, Condor was the most efficient at [CLEARS THROAT] getting from point A to point B. And humankind, the crown of creation came in with a rather unimpressive showing about a third of the way down...