深入探讨以太坊虚拟机 (EVM)

币圈资讯 阅读:51 2024-04-22 11:03:18 评论:0
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

作者:Gimer Cervera,以太坊智能合约开发者 翻译:善欧巴,比特币买卖交易网

介绍

本文深入探讨以太坊虚拟机 (EVM)和Solidity Assembly,以实现智能合约优化和安全性。

以太坊虚拟机(EVM)是以太坊网络的核心组件。EVM 是一款软件,允许部署和执行用高级语言(例如 Solidity)编写的智能合约。编写合约后,将其编译为字节码并部署到EVM。EVM 运行在以太坊网络的每个节点上。

Solidity Assembly 是一种低级编程语言,允许开发人员在更接近 EVM 本身的级别编写代码。它提供了对智能合约执行的更精细的控制,允许仅通过更高级别的 Solidity 代码无法实现的优化和定制。

Solidity 中用于内联汇编的语言称为Yul。该编程语言充当编译为 EVM 字节码的中介。它被设计为一种低级语言,使开发人员能够更细粒度地控制智能合约的执行。它可以在独立模式下使用,也可以在 Solidity 中进行内联汇编。Yul 被设计为一种基于低级堆栈的语言,使开发人员能够编写更优化、更高效的代码。在解释 Solidity 组装之前,我们需要了解 EVM 的组件如何工作。

EVM 是一个准图灵完备的状态机。在这种情况下,术语“准”意味着流程的执行仅限于有限数量的计算步骤,具体取决于任何给定智能合约执行可用的 Gas 量。这就是以太坊处理停止问题和执行可能(恶意或意外)永远运行的情况的方式。这样就避免了以太坊平台全面瘫痪的情况。

Gas是一个衡量在以太坊中完成交易所需的计算量的概念。交易成本以以太币支付,并与 Gas 和 Gas 价格相关。我们在此过程中的目标是学习如何在不影响安全性的情况下最大限度地减少消耗的Gas总量。

代码优化问题

内联汇编是一种在较低级别访问 EVM 的方法。它绕过了 Solidity 的几个重要的安全功能和检查。正确使用内联汇编可以显着降低执行成本。但是,您应该仅将其用于需要它的任务,并且仅当您知道自己在做什么时。使用内联汇编优化代码可能会给您的代码带来新的安全问题。要掌握内联汇编,我们需要了解 EVM 及其组件的工作原理。

在EVM中,每次第一次访问任何存储变量时都必须付费,这称为“冷”访问,需要花费2100个gas。第二次或连续一次被称为“热”访问,需要花费 100 Gas。

以下代码是我们如何使用 Yul 优化代码的示例。函数SetData1以传统方式使用 Solidity为全局变量值设置新值。我们第一次分配这个新值时需要花费 22514 个gas。第二个花费要少得多,即 5414 Gas。

okS662ywe9UkmCKVxHjWe0i81keFEhE51fLqNKWs.png

函数setData2实现内联汇编。内联汇编块由 assembly { … } 标记,其中大括号内的代码是 Yul 语言的代码。此时无需了解源代码,只需记住该软件正在较低级别访问存储空间即可。因此,执行成本会更低。

在我们的示例中,第一次修改该值将花费 22484 个 Gas。连续几次,成本为 5384 Gas。差异可能看起来并不显着,但是我们应该考虑到这段代码可能会执行数千次。

ZlEcJa5qttwq275Wh8CSJLzQvYgFSEUD9OuI4tp3.png

为什么存储这么贵?请记住,我们处于一个去中心化的世界,数据不仅仅存储在一个地方,而是存储在数以万计的节点上。如果未来的交易需要访问或更改它,它还必须可供网络中的每个节点轻松使用。该数据的总体成本等于其消耗的存储空间和在整个网络上生成该数据的计算量的总和。

EVM 堆栈、存储和内存

EVM 是一种基于堆栈的机器,它在称为堆栈的数据结构上运行,该结构保存值并执行操作。EVM 有自己的一组指令(称为操作码),用于执行读取和写入存储、调用其他合约以及执行数学运算等任务。堆栈按照后进先出 (LIFO)方式运行,请参见图 1,这意味着最近插入的项存储在堆栈的顶部,并且是第一个要删除的项。

ErTKbn1SRoeL6bCljlu7WoUgpf9Cw4tKmpq5xTLT.png

当执行智能合约时,EVM 创建一个包含各种数据结构和状态变量的执行上下文。执行完成后,执行上下文将被丢弃,为下一个合约做好准备。在执行期间,EVM 会维护一个临时内存,该内存在事务之间不会持续存在。EVM 执行深度为 1024 项的堆栈机。每个项目都是一个 256 位字,选择此大小是为了便于使用 256 位哈希和椭圆曲线加密。

EVM 具有以下组件,见图 2:

  • 堆栈:EVM 的堆栈是一种按后输入先输出 (LIFO) 方式运行的数据结构,用于在智能合约执行期间存储临时值。

  • 存储:永久存储,是以太坊状态的一部分,仅在第一次初始化为零。

  • 内存:易失性、动态大小的字节数组,用于存储合约执行期间的中间数据。每次创建新的执行上下文时,内存都会初始化为零。

  • Calldata:这也是一个易失性数据存储区域,类似于内存。然而它存储不可变的数据。它旨在保存作为智能合约交易的一部分发送的数据。

  • 程序计数器:程序计数器 (PC) 指向 EVM 要执行的下一条指令。PC通常在一条指令执行后增加一个字节。

  • 虚拟ROM:智能合约作为字节码存储在该区域中。虚拟 ROM 是只读的。

byS0PEfxE9E3EAlqlseO5P36kgSUAJ4RXlEpWJxG.png

EVM 堆栈

在该架构中,程序的指令和数据保存在内存中,程序的执行由指向堆栈顶部的堆栈指针控制。堆栈指针跟踪下一个值或指令将在堆栈上保存或检索的位置。当程序运行时,它将值添加到堆栈中并对已经存在的值执行操作。当代码想要将两个数字相加时,它将数字压入堆栈,然后对顶部的两个值执行加法操作。然后结果返回到堆栈。

SLyBgI6MX0FvAU6B3i9jQkAvd9V1ACc7FR1Mlkp3.png

基于堆栈的架构最重要的特征之一是它允许高度简单且高效的操作执行。由于堆栈是一种 LIFO 数据结构,因此可以轻松快速地处理数据和指令。

EVM 有自己的一组指令,称为操作码。操作码用于执行读取和写入存储、调用其他合约以及执行数学运算等任务。EVM 指令集提供您可能期望的大部分操作,包括:

  • 堆栈操作:POP、PUSH、DUP、SWAP

  • 算术/比较/按位:ADD、SUB、GT、LT、AND、OR

  • 环境:CALLER、CALLVALUE、NUMBER

  • 内存操作:MLOAD、MSTORE、MSTORE8、MSIZE

  • 存储操作:SLOAD、SSTORE

  • 程序计数器相关操作码:JUMP、JUMPI、PC、JUMPDEST

  • 停止操作码:STOP、RETURN、REVERT、INVALID、SELFDESTRUCT

EVM存储

EVM 存储是非易失性空间,保存 256 位 –> 256 位的键值对。合约中的存储槽位总数为 2²⁵⁶,这是一个非常庞大的槽位数量。区块链上的每个智能合约都有自己的存储空间。

在函数调用期间,存储用于在函数调用之间需要记住的数据。它用于存储即使在智能合约执行结束后也需要可用的变量和数据结构。

1HMGlMM7NtpaHHsnMiAq1AEaILWPXt4dG3VwqyKy.png

访问存储的操作码是:SLOAD 和 SSTORE

该帐户的存储是永久数据存储,仅由智能合约使用。外部拥有的帐户 (EOA) 将始终没有代码且存储空间为空。

EVM内存

内存是架构中的易失性内存,其数据在区块链中不持久。内存是一种随机访问数据结构,在智能合约执行期间存储临时数据。

S3MF42KoITdlBytrmQc3NNV8NNmTkTWz2DqDUO1A.png

内存分为四部分:2 个槽用于暂存空间,1 个槽用于空闲内存指针,0 槽和 1 个槽指向可用的空闲内存。前 64 个字节的空间将由散列方法使用,散列方法在最终返回最终输出之前需要临时空间来存储中间输出。

空闲内存指针只是指向空闲内存开始位置的指针。它确保智能合约跟踪哪些内存位置已被写入以及哪些仍然可用。这可以防止合约覆盖已分配给另一个变量的某些内存。图 6 显示了内存是如何划分的:

g3imQHTd89wyatS2hNwKZHK4Ys4SANntyutufP7c.png

内存用于存储不需要保存在存储器中的变量和数据结构。智能合约执行期间可以调整内存大小,但访问速度比堆栈更慢且成本更高。

考虑内存是零初始化的,用于访问内存的操作码是:MLOAD、MSTORE、MSTORE8

概括

在本文中,我们回顾了与以太坊虚拟机(EVM)相关的一些基本概念。实现内联汇编代码需要深入了解 EVM。这是因为我们正在与 EVM 的一些组件进行交互。在以后的课程中,我们将更详细地分析其他 EVM 元素,例如:存储、内存和 Calldata。此外,我们还将回顾字节码、Gas 和应用程序二进制接口 (ABI) 等重要概念。最后,我们将讨论操作码的工作原理以及更多内联汇编示例,以安全地优化智能合约的执行。


Author Ethereum Intelligent Contract Developer Translation Shanouba Bitcoin Trading Network Introduction This paper deeply discusses Ethereum Virtual Machine and the realization of intelligent contract optimization and security. Ethereum Virtual Machine is the core component of Ethereum Network. It is a software that allows the deployment and execution of contracts written in high-level languages, such as smart contracts, and then compiles them into bytecodes and deploys them to every node running in Ethereum Network. It is a low-level programming language that allows developers to be closer to their own level. Writing code, which provides finer control over the execution of smart contracts, allows optimization and customization that cannot be achieved only through higher-level code. The language used for inline assembly is called the programming language, which acts as the intermediary for compiling into bytecode. It is designed as a low-level language to enable developers to control the execution of smart contracts more finely. It can be used in independent mode or inline assembly in. It is designed as a language based on low-level stack to enable developers to write more. Optimizing more efficient code before explaining the assembly, we need to know how the components work. It is a quasi-Turing complete state machine. In this case, the term quasi means that the execution of the process is limited to a limited number of calculation steps, which depends on the available amount of any given intelligent contract execution. This is the way for Ethereum to deal with the stop problem and the execution of the situation that may be malicious or unexpected forever running, thus avoiding the complete paralysis of the Ethereum platform, and it is a measure to complete the handover in Ethereum. The concept of easy-to-need calculation, the transaction cost is paid in etheric currency and related to the price. Our goal in this process is to learn how to minimize the total amount of consumption without affecting security. Code optimization is a method of accessing at a lower level. It bypasses several important security functions and checks that the correct use of inline assembly can significantly reduce the execution cost, but you should only use it for tasks that need it and only when you know what you are doing. When to use inline assembly to optimize code may bring new security problems to your code. To master inline assembly, we need to understand the working principle of its components. Every time we access any storage variable for the first time, we have to pay for it. This is called cold access, and it takes a second time or a continuous time to be called hot access. The following code is how to use the example function of optimized code to set a new value for the global variable value in the traditional way. We need to allocate this new value for the first time. The second cost is much less, that is, the function realizes inline assembly, and the inline assembly block is marked, in which the code in braces is the language code. At this time, you don't need to know the source code, just remember that the software is accessing the storage space at a lower level, so the execution cost will be lower. In our example, it will take several consecutive times to modify the value for the first time. The difference may not seem significant, but we should consider why this code may be executed thousands of times. Please remember. We are in a decentralized world. Data is not only stored in one place, but also stored in tens of thousands of nodes. If future transactions need to access or change it, it must also be easily used by every node in the network. The total cost of the data is equal to the sum of the storage space it consumes and the amount of calculation to generate the data on the whole network. Stack storage and memory is a stack-based machine. It runs on a data structure called stack, saves values and performs operations. One's own set of instructions is called operation code, which is used for reading, writing, storing, calling other contracts, and performing mathematical operations. The task stack runs in LIFO mode. Please refer to the figure. This means that the recently inserted item is stored at the top of the stack and is the first item to be deleted. When the smart contract is executed, an execution context containing various data structures and state variables is created. After the execution is completed, the execution context will be discarded to prepare for the next contract, and a temporary one will be maintained during the execution. There will not be a stack machine with execution depth between transactions in this memory. Each item is a bit word. This size is selected to facilitate the use of bit hash and elliptic curve encryption. The stack with the following components is a data structure that operates in a last-in first-out mode. It is used to store temporary values during the execution of smart contracts. Permanent storage is part of the Ethereum state. It is only initialized to zero memory volatility at the first time. A byte array with dynamic size is used for storage. Intermediate data during contract execution, the memory will be initialized to zero every time a new execution context is created. This is also a volatile data storage area similar to memory, but it stores immutable data. It is designed to store data sent as part of smart contract transactions. The program counter points to the next instruction to be executed. Usually, after an instruction is executed, a byte is added. Virtual smart contracts are stored in this area as bytecodes, and the virtual read-only stack is in this architecture. The instructions and data of the program are stored in memory, and the execution of the program is controlled by the stack pointer pointing to the top of the stack. The stack pointer tracks the position where the next value or instruction will be saved or retrieved on the stack. When the program runs, it adds the value to the stack and performs operations on the existing value. When the code wants to add two numbers, it pushes the number onto the stack and then performs the addition operation on the top two values, and then the result is returned to the stack. One of the most important features of the stack-based architecture is that it allows. Highly simple and efficient operation execution. Because the stack is a data structure, data and instructions can be processed easily and quickly. It has its own set of instructions called opcodes, which are used to perform reading and writing, storing, calling other contracts, and performing mathematical operations. The instruction set provides most of the operations you may expect, including stack operation arithmetic comparison, memory operation according to bit environment, storage of operating procedures, counter related opcodes, stopping opcode storage, and storing bits in nonvolatile space. The total number of storage slots in the key-value pair contract is this is a very large number of slots. Each smart contract in the blockchain has its own storage space, which is used to store data that needs to be remembered between function calls. It is used to store variables and data structures that need to be available even after the smart contract is executed. The storage operation code of the account is permanent data storage, and only the smart contract uses the externally owned account, which will always have no code and the storage space is empty. Memory is volatile memory in the architecture, and its data is not persistent memory in the blockchain, which is a random access. 比特币今日价格行情网_okx交易所app_永续合约_比特币怎么买卖交易_虚拟币交易所平台

文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明 网址:https://netpsp.com/?id=61824

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
关注我们

若遇到问题,加微信客服---清歌

搜索
排行榜
扫一扫,加我为微信好友加我为微信好友