PageSourceSearch

https://www.pseudoyu.com/assets/blockchain_ethereum_basic-BjcAJF1x.js

js pseudoyu.com collected 2026-09-25 14:11:56 UTC 16,966 bytes, 6 lines download raw bytes

1import{u as s,_ as r}from"./WrapperPost.vue_vue_type_style_index_0_lang-bgNpJ1F0.js";import{c as i,o,w as u,a as e,d as l}from"./app-EMsQ9hhF.js";const g={__name:"blockchain_ethereum_basic",setup(d){const a={title:"Ethereum 核心技术解读",date:"2021-02-20T12:12:17.000+0800",lang:"zh",type:"note",duration:"8min",image:"https://image.pseudoyu.com/images/ethereum_overview.png",meta:[{property:"og:title",content:"Ethereum 核心技术解读"},{name:"twitter:title",content:"Ethereum 核心技术解读"},{property:"og:image",content:"https://image.pseudoyu.com/images/ethereum_overview.png"},{name:"twitter:image",content:"https://image.pseudoyu.com/images/ethereum_overview.png"},{name:"twitter:card",content:"summary_large_image"}]};return s({title:"Ethereum 核心技术解读",meta:[{property:"og:title",content:"Ethereum 核心技术解读"},{name:"twitter:title",content:"Ethereum 核心技术解读"},{property:"og:image",content:"https://image.pseudoyu.com/images/ethereum_overview.png"},{name:"twitter:image",content:"https://image.pseudoyu.com/images/ethereum_overview.png"},{name:"twitter:card",content:"summary_large_image"}]}),(h,t)=>{const n=r;return o(),i(n,{frontmatter:a},{default:u(()=>t[0]||(t[0]=[e("div",{class:"prose m-auto slide-enter-content"},[e("h2",{id:"前言",tabindex:"-1"},[l("前言 "),e("a",{class:"header-anchor",href:"#前言","aria-hidden":"true"},"#")]),e("p",null,[l("比特币作为一种去中心化的数字货币,是极其成功的,但受限于比特币脚本(非图灵完备,只能处理一些简单的逻辑),并不能处理很复杂的业务。而"),e("code",null,"Ethereum"),l("引入了智能合约,使去中心化的概念能够应用于更丰富的应用场景,因此也被称为区块链 2.0。本文将对以太坊核心技术进行解读,如有错漏,欢迎交流指正。")]),e("h2",{id:"ethereum-系统",tabindex:"-1"},[l("Ethereum 系统 "),e("a",{class:"header-anchor",href:"#ethereum-系统","aria-hidden":"true"},"#")]),e("p",null,[l("2014 年 1 月,俄罗斯开发者 Vitalik Buterin 发布了以太坊白皮书并成立团队,旨在创造一个集成更通用的脚本语言的区块链平台。其中一位成员 Dr. Gavin Wood 发布了一份黄皮书,涉及"),e("code",null,"Ethereum Virtual Machin(EVM)"),l("以太坊虚拟的相关技术,这就是"),e("code",null,"Ethereum"),l("的诞生。")]),e("p",null,[e("img",{src:"https://image.pseudoyu.com/images/ethereum_overview.png",alt:"ethereum_overview"})]),e("p",null,[l("简单来说,"),e("code",null,"Ethereum"),l("是一个开源的去中心化系统,使用区块链来存储系统状态变化,因此也被称为“世界计算机”;它支持开发者在区块链上部署运行不可变的程序,称为智能合约,因此可以支持广泛的应用场景;它使用数字货币"),e("code",null,"Ether"),l("来衡量系统资源消耗,激励更多人参与"),e("code",null,"Ethereum"),l("系统建设。")]),e("h3",{id:"去中心化应用-dapp",tabindex:"-1"},[l("去中心化应用 DApp "),e("a",{class:"header-anchor",href:"#去中心化应用-dapp","aria-hidden":"true"},"#")]),e("p",null,"狭义来说,DApp 其实就是一个集成了用户界面、支持智能合约、运行于以太坊区块链上的应用。"),e("p",null,[e("img",{src:"https://image.pseudoyu.com/images/ethereum_architecture.png",alt:"ethereum_architecture"})]),e("p",null,[l("如上图所示,"),e("code",null,"Ethereum"),l("应用实例部署在区块链网络上(智能合约运行于区块链虚拟机中),而 Web 程序只需要通过"),e("code",null,"Web3.js"),l("对区块链网络进行"),e("code",null,"RPC"),l("远程调用,这样用户就可以通过浏览器(DApp 浏览器或 MetaMask 等插件工具)访问去中心化服务应用了。")]),e("h3",{id:"账本",tabindex:"-1"},[l("账本 "),e("a",{class:"header-anchor",href:"#账本","aria-hidden":"true"},"#")]),e("p",null,[e("code",null,"Ethereum"),l("区块链是一个去中心化的账本(数据库),网络中的所有交易都会存储在区块链中,所有节点都要本地保存一份数据,并且确保每一笔交易的可信度;所有的交易都是公开且不可篡改的,网络中的所有节点都可以查看和验证。")]),e("h3",{id:"账户",tabindex:"-1"},[l("账户 "),e("a",{class:"header-anchor",href:"#账户","aria-hidden":"true"},"#")]),e("p",null,"当我们需要登录一个网站或系统(比如邮箱)时,往往需要一个帐号和一个密码,密码通过加密算法以暗文的形式存储在中心化的数据库中。然而,以太坊是一个去中心化的系统,那是怎么生成账户的呢?"),e("p",null,"和比特币系统原理类似"),e("ol",null,[e("li",null,[l("首先生成一个仅有自己知道的私钥,假设为"),e("code",null,"sk"),l(",采用"),e("code",null,"ECDSA(Elliptic Curve Digital Signature Algorithm)"),l("椭圆曲线算法生成对应的公钥"),e("code",null,"pk")]),e("li",null,[l("采用"),e("code",null,"keccak256"),l("算法对公钥"),e("code",null,"pk"),l("求哈希值")]),e("li",null,"截取后 160 位作为以太坊的地址")]),e("p",null,[l("用户的私钥和地址一起组成了以太坊的账户,可以存储余额、发起交易等(比特币的余额是通过计算所有的"),e("code",null,"UTXO"),l("得到的,而不是像以太坊一样存储在账户中)。")]),e("p",null,[l("其实"),e("code",null,"Ethereum"),l("账户分为两种类型,上述方式生成的叫"),e("code",null,"Externally Owned A
1ccounts(EOA)"),l(",外部账户,也就是常规用户拥有的账户,主要是用来发送/接收"),e("code",null,"Ether"),l("代币或者向智能合约发送交易(即调用智能合约)。")]),e("p",null,[l("而另一种则是"),e("code",null,"Contract Accounts"),l(",合约账户,不同于外部账户,这种账户是没有对应的私钥的,而是在部署合约的时候生成的,存储智能合约代码。值得注意的是,合约账户必须要被外部账户或者其他合约调用才能够发送或接收"),e("code",null,"Ether"),l(",而不能自己主动执行交易。")]),e("h3",{id:"钱包",tabindex:"-1"},[l("钱包 "),e("a",{class:"header-anchor",href:"#钱包","aria-hidden":"true"},"#")]),e("p",null,[l("存储和管理"),e("code",null,"Ethereum"),l("账户的软件/插件称为钱包,提供了诸如交易签名、余额管理等功能。钱包生成主要有两种方式,非确定性随机生成或根据随机种子生成。")]),e("h3",{id:"gas",tabindex:"-1"},[l("Gas "),e("a",{class:"header-anchor",href:"#gas","aria-hidden":"true"},"#")]),e("p",null,[e("code",null,"Ethereum"),l("网络上的操作也需要“手续费”,称为"),e("code",null,"Gas"),l(",在区块链上部署智能合约以及转账都需要消耗一定单位的"),e("code",null,"Gas"),l(",这也是鼓励矿工参与"),e("code",null,"Ethereum"),l("网络建设的激励机制,从而使整个网络更加安全、可靠。")]),e("p",null,[l("每个交易都可以设置相应的"),e("code",null,"Gas"),l("量和"),e("code",null,"Gas"),l("的价格,设置较高的"),e("code",null,"Gas"),l("费则往往矿工会更快处理你的交易,但为了预防交易多次执行消耗大量"),e("code",null,"Gas"),l("费,可以通过"),e("code",null,"Gas Limit"),l("来设置限制。"),e("code",null,"Gas"),l("相关信息可以通过 "),e("a",{href:"https://etherscan.io/gastracker",target:"_blank",rel:"noopener"},"Ethereum Gas Tracker"),l(" 工具进行查询。")]),e("pre",{class:"shiki shiki-themes vitesse-dark vitesse-light",style:{"--s-dark":"#dbd7caee","--s-light":"#393a34","--s-dark-bg":"#121212","--s-light-bg":"#ffffff"},tabindex:"0"},[e("code",{class:"language-sh"},[e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"If"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," START_GAS"),e("span",{style:{"--s-dark":"#C99076","--s-light":"#A65E2B"}}," *"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS_PRICE"),e("span",{style:{"--s-dark":"#CB7676","--s-light":"#AB5959"}}," >"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," caller.balance,"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," halt")]),l(`
2`),e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"Deduct"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," START_GAS"),e("span",{style:{"--s-dark":"#C99076","--s-light":"#A65E2B"}}," *"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS_PRICE"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," from"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," caller.balance")]),l(`
3`),e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"Set"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," ="),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," START_GAS")]),l(`
4`),e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"Run"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," code,"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," deducting"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," from"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS")]),l(`
5`),e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"For"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," negative"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," values,"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," add"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," to"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS_REFUND")]),l(`
6`),e("span",{class:"line"},[e("span",{style:{"--s-dark":"#80A665","--s-light":"#59873A"}},"After"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," termination,"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," add"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," GAS_REFUND"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," to"),e("span",{style:{"--s-dark":"#C98A7D","--s-light":"#B56959"}}," caller.balance")])])]),e("h3",{id:"智能合约",tabindex:"-1"},[l("智能合约 "),e("a",{class:"header-anchor",href:"#智能合约","aria-hidden":"true"},"#")]),e("p",null,[l("上文提到,"),e("code",null,"Ethereum"),l("区块链不仅仅存储交易信息,还会存储与执行智能合约代码。")]),e("p",null,[l("智能合约控制应用和交易逻辑,"),e("code",null,"Ethereum"),l("系统中的智能合约采用专属"),e("code",null,"Solidity"),l("语言,语法类似于"),e("code",null,"JavaScript"),l(",除此之外,还有"),e("code",null,"Vyper"),l("、"),e("code",null,"Bamboo"),l("等编程语言。智能合约代码会被编译为字节码并部署至区块链中,一旦上链则不可以再编辑。"),e("code",null,"EVM"),l("作为一个智能合约执行环境ï¼
6Œèƒ½å¤Ÿä¿éšœæ‰§è¡Œç»“果的确定性。")]),e("h4",{id:"智能合约示例:众筹",tabindex:"-1"},[l("智能合约示例:众筹 "),e("a",{class:"header-anchor",href:"#智能合约示例:众筹","aria-hidden":"true"},"#")]),e("p",null,"让我们想象一个更复杂的场景,假设我要众筹 10000 元开发一个新产品,通过现有众筹平台需要支付不菲的手续费,而且很难解决信任问题,于是,可以通过一个众筹的 DApp 来解决这个问题。"),e("p",null,"先为众筹设置一些规则"),e("ol",null,[e("li",null,"每个想参与众筹的人可以捐款 10-10000 元的金额"),e("li",null,"如果目标金额达成了,金额会通过智能合约发送给我(即众筹发起人)"),e("li",null,"如果目标在一定时间内(如 1 个月)没有达成,众筹的资金会原路返回至众筹用户"),e("li",null,"也可以设置一些规则,比如一周后,如果目标金额没有达成,用户可以申请退款")]),e("p",null,"因为这些众筹条款是通过智能合约实现并部署在公开的区块链上的,即使是发起者也不能篡改条款,且任何人都可以查看,解决了信任问题。"),e("p",null,[l("完整代码可以点击这里查看:"),e("a",{href:"https://www.toshblocks.com/solidity/complete-example-crowd-funding-smart-contract/",target:"_blank",rel:"noopener"},"Demo")]),e("h3",{id:"交易",tabindex:"-1"},[l("交易 "),e("a",{class:"header-anchor",href:"#交易","aria-hidden":"true"},"#")]),e("p",null,[l("在"),e("code",null,"Ethereum"),l("中,一个典型的交易是怎么样的呢?")]),e("ol",null,[e("li",null,"开发者部署智能合约至区块链"),e("li",null,"DApp 实例化合约、传入相应值以执行合约"),e("li",null,"DApp 对交易进行数字签名"),e("li",null,"本地对交易进行验证"),e("li",null,"广播交易至网络中"),e("li",null,"矿工节点接收交易并进行验证"),e("li",null,"矿工节点确认可信区块后广播至网络中"),e("li",null,"本地节点与网络进行同步,接收新区块")]),e("h3",{id:"架构",tabindex:"-1"},[l("架构 "),e("a",{class:"header-anchor",href:"#架构","aria-hidden":"true"},"#")]),e("p",null,[e("img",{src:"https://image.pseudoyu.com/images/ethereum_architecture_simple.png",alt:"ethereum_architecture_simple"})]),e("p",null,[e("code",null,"Ethereum"),l("采用的是一种"),e("code",null,"Order - Execute - Validate - Update State"),l("的系统架构。在这种架构下,当产生一笔新的交易,矿工会进行"),e("code",null,"PoW"),l("工作量证明机制的运算;验证完成后,将区块通过"),e("code",null,"gossip"),l("协议广播至网络中;网络中的其他节点接收到新区块后,也会对区块进行验证;最终,提交至区块链,更新状态。")]),e("p",null,[l("具体来看,"),e("code",null,"Ethereum"),l("系统有共识层、数据层、应用层等核心组件,其交互逻辑如下:")]),e("p",null,[e("img",{src:"https://image.pseudoyu.com/images/ethereum_architecture_concrete.png",alt:"ethereum_architecture_concrete"})]),e("p",null,[l("如上图所示,"),e("code",null,"Ethereum"),l("数据由"),e("code",null,"Transaction Root"),l("和"),e("code",null,"State Root"),l("组成。"),e("code",null,"Transaction Root"),l("是所有交易组成的树,包含"),e("code",null,"From"),l("、"),e("code",null,"To"),l("、"),e("code",null,"Data"),l("、"),e("code",null,"Value"),l("、"),e("code",null,"Gas Limit"),l("和"),e("code",null,"Gas Price"),l(";而"),e("code",null,"State Root"),l("则是所有账户组成的树,包含"),e("code",null,"Address"),l("、"),e("code",null,"Code"),l("、"),e("code",null,"Storage"),l("、"),e("code",null,"Balance"),l("和"),e("code",null,"Nonce"),l("。")]),e("h2",{id:"总结",tabindex:"-1"},[l("总结 "),e("a",{class:"header-anchor",href:"#总结","aria-hidden":"true"},"#")]),e("p",null,[l("以上就是对"),e("code",null,"Ethereum"),l("核心技术的一些解读,智能合约的引入给区块链的应用带来了更多可能性,但仍有很多安全性、隐私性和效率问题需要考虑。针对复杂的企业级应用场景,联盟链是更好的选择,后续将会对"),e("code",null,"Hyperledger Fabric"),l("进行详尽的分析,敬请期待!")]),e("h2",{id:"参考资料",tabindex:"-1"},[l("参考资料 "),e("a",{class:"header-anchor",href:"#参考资料","aria-hidden":"true"},"#")]),e("blockquote",null,[e("ol",null,[e("li",null,[e("a",{href:"https://msccs.cs.hku.hk/public/courses/2020/COMP7408A/",target:"_blank",rel:"noopener"},"COMP7408 Distributed Ledger and Blockchain Technology"),l(", "),e("em",null,"Professor S.M. Yiu, HKU")]),e("li",null,[e("a",{href:"https://www.udacity.com/course/blockchain-developer-nanodegree--nd1309",target:"_blank",rel:"noopener"},"Udacity Blockchain Developer Nanodegree"),l(", "),e("em",null,"Udacity")]),e("li",null,[e("a",{href:"https://www.bilibili.com/video/BV1Vt411X7JF",target:"_blank",rel:"noopener"},"区块链技术与应用"),l(","),e("em",null,"肖臻,北京大学")]),e("li",null,[e("a",{href:"https://www.ituring.com.cn/book/2434",target:"_blank",rel:"noopener"},"区块链技术进阶与实战"),l(","),e("em",null,"蔡亮 李启雷 梁秀波,浙江大学 | 趣链科技")]),e("li",null,[e("a",{href:"https://www.zastrin.com/courses/ethereum-primer/lessons/1-5",target:"_blank",rel:"noopener"},"Ethereum Architecture"),l(", "),e("em",null,"zastrin")]),e("li",null,[e("a",{href:"https://www.toshblocks.com/solidity/complete-example-crowd-funding-smart-contract/",target:"_blank",rel:"noopener"},"Learn Solidity: Complete Example: Crowd Funding Smart Contract"),l(", "),e("em",null,"TOSHBLOCKS")]
6)])])],-1)])),_:1})}}};export{g as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.