ethereumjs/ethereumjs-vm-2-API文档

本文详细介绍了以太坊虚拟机(EVM)的主要功能和操作,包括处理区块、交易和智能合约代码的运行机制。阐述了如何使用EVM进行区块处理、交易验证、智能合约执行,并解释了其内部状态管理和资源消耗模型。

https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/index.md

vm.runBlockchain

Processes blocks and adds them to the blockchain

处理区块并将其添加到区块链中

Parameters输入参数

  • blockchain Blockchain A blockchain that to process 一个处理的区块链
  • cb Function the callback function 回调函数

 

 

VM

VM Class, new VM(opts) creates a new VM object

VM类,VM(opts)将创建一个VM对象,运行起来实现的感觉与ganache相似

Parameters输入参数

  • opts Object
    • opts.stateManager StateManager a StateManager instance to use as the state store (Beta API) 用于状态存储的StateManager实例(还在实验阶段)其实这个就是区块上的state merkle-patricia树,用于存储区块上的所有账户的信息
    • opts.state Trie a merkle-patricia-tree instance for the state tree (ignored if stateManager is passed) 用于状态树的一个merkle-patricia树(如果上面的stateManager已经传递,这个就可以忽略)
    • opts.blockchain Blockchain a blockchain object for storing/retrieving blocks (ignored if stateManager is passed) 为了存储/检索区块的区块链对象(如果上面的stateManager已经传递,这个就可以忽略
    • opts.chain (String | Number) the chain the VM operates on [default: 'mainnet']  VM操作在那个链上(默认是mainnet链)
    • opts.hardfork String hardfork rules to be used [default: 'byzantium', supported: 'byzantium',  'constantinople' (will throw on unsupported)]    使用的硬分支规则(默认是byzantium',还支持'byzantium',  'constantinople',如果输入的是不支持的将抛出错误)  
    • opts.activatePrecompiles Boolean create entries in the state tree for the precompiled contracts 为了预编译合约在状态树上创建条目,其实就是在state merkle-patricia树上手动添加一些账户的信息,就跟ganache一样,一开始就有可用的账户
    • opts.allowUnlimitedContractSize Boolean allows unlimited contract sizes while debugging. By setting this to true, the check for contract size limit of 24KB (see EIP-170) is bypassed. (default: false; ONLY set to true during debugging) 当调试时,允许不限制的合约大小。通过将其设置为true,来忽视合约大小设置为24KB的检查(细节看本博客ethereum/EIPs-170 Contract code size limit)。(默认为false,只在调试时设置为true)
    • opts.emitFreeLogs Boolean Changes the behavior of the LOG opcode, the gas cost of the opcode becomes zero and calling it using STATICCALL won't throw. (default: false; ONLY set to true during debugging) 改变日志操作吗的行为,gas开销的操作码变为0,使用STATICCALL操作码调用它将抛出错误(默认为false,只有在调试时设置为true)
     

 

 

vm.runBlock

Processes the block running all of the transactions it contains and updating the miner's account

处理区块,运行其中的所有交易并更新矿工账户

Parameters输入参数

  • opts
    • opts.block Block the Block to process 处理的区块
    • opts.generate Boolean [gen=false] whether to generate the stateRoot, if false runBlock will check the stateRoot of the block against the Trie 是否生成stateRoot,如果设置为false,runBlock将会靠着前缀树来查看区块的stateRoot
  • cb runBlock~callback callback 回调函数

 

 

runBlock~callback

Callback for runBlock method

runBlock的回调

Type: Function

Parameters输入参数

  • error Error an error that may have happened or null  发生的错误或null
  • results Object
    • results.receipts Array the receipts from the transactions in the block 区块中来自交易的receipt
    • results.results Array 返回结果

 

vm.runTx

Process a transaction. Run the vm. Transfers eth. Checks balances.

处理交易。运行虚拟机。交易eth。查看余额。

Parameters输入参数

  • opts
    • opts.tx Transaction a Transaction to run 运行的交易
    • opts.skipNonce Boolean skips the nonce check  跳过nonce检查
    • opts.skipBalance Boolean skips the balance check  跳过余额检查
    • opts.block Block the block to which the tx belongs, if no block is given a default one is created  交易属于的区块,如果没有,则创建一个默认块
  • cb runTx~callback the callback 回调函数

 

runTx~callback

Callback for runTx method

runTx方法的回调

Type: Function

Parameters输入参数

  • error Error an error that may have happened or null  发生的错误或null
  • results Object
    • results.amountSpent BN the amount of ether used by this transaction as a bignum  被这个交易使用的ether的数量,BN格式
    • results.gasUsed BN the amount of gas as a bignum used by the transaction  被这个交易使用的gas,BN格式
    • results.gasRefund BN the amount of gas as a bignum that was refunded during the transaction (i.e. gasUsed = totalGasConsumed - gasRefund)  在交易中被退还的gas,BN格式(使用的gas = 总使用的gas - 退还的gas)
  • vm VM contains the results from running the code, if any, as described in vm.runCode(params, cb)  虚拟机,如果存在,包含通过运行代码得到的结果,就像vm.runCode(params, cb)所说

 

vm.runCode

Runs EVM code

运行以太坊虚拟机代码

Parameters输入参数

  • opts Object
    • opts.account Account the Account that the executing code belongs to. If omitted an empty account will be used 执行的代码属于的账户。如果省略则使用空账户
    • opts.address Buffer the address of the account that is executing this code. The address should be a Buffer of bytes. Defaults to 0 执行这个代码的账户的地址。地址应该是字节的buffer格式。默认为0
    • opts.block Block the Block the tx belongs to. If omitted a blank block will be used 这个交易属于的区块。如果省略则使用一个黑块
    • opts.caller Buffer the address that ran this code. The address should be a Buffer of 20bits. Defaults to 0 运行这个代码的地址。这个地址应该为20bits的buffer格式,默认为0
    • opts.code Buffer the EVM code to run given as a Buffer 以buffer格式给出的用来运行的以太坊虚拟机的代码
    • opts.data Buffer the input data 输入的数据
    • opts.gasLimit Buffer the gas limit for the code 这个代码使用的gas的限制
    • opts.origin Buffer the address where the call originated from. The address should be a Buffer of 20bits. Defaults to 0 这个调用起源的地址。这个地址应该为20bits的buffer格式,默认为0
    • opts.value Buffer the value in ether that is being sent to opt.address. Defaults to 0 被送给opt.address的ether。默认为0
  • cb runCode~callback callback 回调函数

 

runCode~callback

Callback for runCode method

runCode方法的回调

Type: Function

Parameters输入参数

  • error Error an error that may have happened or null  发生的错误或null
  • results Object
    • results.gas BN the amount of gas left 剩下的gas数量
    • results.gasUsed BN the amount of gas as a bignum the code used to run  以bignum格式给出的这个代码用来运行的gas数量
    • results.gasRefund BN a bignum containing the amount of gas to refund from deleting storage values从删除的存储值中得到包含退还的gas的数量的大数
    • results.selfdestruct Object an Object with keys for accounts that have selfdestructed and values for balance transfer recipient accounts 包含自我毁灭的账户密钥和转移接收账户余额的值的对象
    • results.logs Array an Array of logs that the contract emitted 合约发出的日志数组
    • results.exception Number 0 if the contract encountered an exception, 1 otherwise 如果合约遇到了异常则为0,否则为1
    • results.exceptionError String a String describing the exception if there was one 如果有异常则用来描述异常的字符串
    • results.return Buffer a Buffer containing the value that was returned by the contract 包含被合约返回的值的buffer

 

Event: beforeBlock

The beforeBlock event  beforeBlock事件

Type: Object

Properties属性

  • block Block emits the block that is about to be processed 发出关于将要被处理的区块

 

Event: afterBlock

The afterBlock event afterBlock事件

Type: Object

Properties属性

  • result Object emits the results of processing a block 发出处理的区块的结果

 

Event: beforeTx

The beforeTx event

Type: Object

Properties

  • tx Transaction emits the Transaction that is about to be processed 发出将要被处理的交易

 

Event: afterTx

The afterTx event

Type: Object

Properties

  • result Object result of the transaction 交易的结果

 

 

Event: step

The step event for trace output追踪输出的step事件

Type: Object

Properties

  • pc Number representing the program counter  代表项目的计数
  • opcode String the next opcode to be ran  下一个运行的操作码
  • gasLeft BN amount of gasLeft  剩下的gas数量
  • stack Array an Array of Buffers containing the stack  包含堆栈的Buffers数组
  • account Account the Account which owns the code running  拥有运行代码的账户
  • address Buffer the address of the account  账户的地址
  • depth Number the current number of calls deep the contract is   合约所在的目前的调用深度数
  • memory Buffer the memory of the VM as a buffer 虚拟机的内存
  • stateManager StateManager a StateManager instance (Beta API)  一个StateManager实例

 

转载于:https://www.cnblogs.com/wanghui-garcia/p/10082906.html

<think>我们正在检查特定文件是否存在。用户询问的是如何检查 `bin/realview/openwrt-realview-vmlinux-initramfs.elf` 文件是否存在。 由于这是一个文件路径,我们可以使用命令行工具来检查。在Linux、macOS或Windows(使用PowerShell或命令提示符)中,我们可以使用相应的命令。 根据用户上一次的问题,他们之前询问了安装wget,但这次的问题是关于检查文件是否存在,与wget无关。因此,我们直接回答如何检查文件是否存在。 方法: 1. 使用命令行(终端或命令提示符)进入包含目标目录的上级目录,或者直接使用绝对路径。 2. 使用检查文件是否存在的命令。 在Linux/macOS中,我们可以使用`test`命令(在脚本中常用)或者`ls`命令,但更直接的是使用`test`命令配合`if`语句,或者直接使用`ls`并检查输出。但为了简单和通用,我们可以使用`ls`命令,或者使用`test`命令(即`[ -f ... ]`)并输出结果。 在Windows中,我们可以使用`dir`命令或者`if exist`命令。 由于用户没有指定操作系统,我们将提供跨平台的解决方案。 但是,注意用户给出的路径是Unix风格的路径(使用正斜杠),所以很可能是在Linux/macOS环境下。不过,为了全面,我们同时提供Windows的方法。 具体步骤: 对于Linux/macOS: ```bash # 使用test命令检查文件是否存在,并在终端输出结果 if [ -f "bin/realview/openwrt-realview-vmlinux-initramfs.elf" ]; then echo "文件存在" else echo "文件不存在" fi ``` 或者,更简洁地,我们可以使用一行命令: ```bash [ -f "bin/realview/openwrt-realview-vmlinux-initramfs.elf" ] && echo "文件存在" || echo "文件不存在" ``` 另外,也可以使用`ls`命令,但可能会输出错误信息(如果文件不存在),所以可以重定向错误信息: ```bash ls bin/realview/openwrt-realview-vmlinux-initramfs.elf 2>/dev/null && echo "文件存在" || echo "文件不存在" ``` 对于Windows(命令提示符): ```cmd if exist "bin\realview\openwrt-realview-vmlinux-initramfs.elf" (echo 文件存在) else (echo 文件不存在) ``` 注意:在Windows中路径分隔符是反斜杠,所以需要将正斜杠替换成反斜杠。另外,如果路径中包含空格,需要使用双引号。 由于用户给出的路径是`bin/realview/openwrt-realview-vmlinux-initramfs.elf`,在Windows中应写成`bin\realview\openwrt-realview-vmlinux-initramfs.elf`。 另外,在Windows PowerShell中,我们可以使用: ```powershell if (Test-Path "bin\realview\openwrt-realview-vmlinux-initramfs.elf") { Write-Host "文件存在" } else { Write-Host "文件不存在" } ``` 但是,考虑到用户可能是在OpenWrt开发环境中(因为路径中有openwrt字样),所以更可能是Linux环境。因此,我们重点放在Linux/macOS上。 如果用户希望直接运行一个命令而不需要条件判断的输出,也可以直接使用`ls`命令并查看输出,或者使用`test`命令的退出码(通过`$?`查看)。 然而,用户的问题是如何检查,所以我们需要给出明确的检查方法。 因此,我们给出以下回答:</think>要检查文件 `bin/realview/openwrt-realview-vmlinux-initramfs.elf` 是否存在,可以使用以下命令行方法(根据操作系统选择): ### Linux/macOS 系统 ```bash # 方法1:使用test命令(推荐) [ -f "bin/realview/openwrt-realview-vmlinux-initramfs.elf" ] && echo "文件存在" || echo "文件不存在" # 方法2:使用ls命令 ls bin/realview/openwrt-realview-vmlinux-initramfs.elf >/dev/null 2>&1 && echo "文件存在" || echo "文件不存在" ``` ### Windows 系统 ```batch :: 方法1:使用if exist if exist "bin\realview\openwrt-realview-vmlinux-initramfs.elf" (echo 文件存在) else (echo 文件不存在) :: 方法2:使用dir dir "bin\realview\openwrt-realview-vmlinux-initramfs.elf" >nul 2>&1 && echo 文件存在 || echo 文件不存在 ``` ### 脚本化检查(跨平台) ```bash #!/bin/bash FILE_PATH="bin/realview/openwrt-realview-vmlinux-initramfs.elf" if [[ -f $FILE_PATH ]]; then echo "✅ 文件存在:$FILE_PATH" echo "文件大小: $(du -h $FILE_PATH | cut -f1)" else echo "❌ 文件不存在:$FILE_PATH" echo "检查路径: $(pwd)/$FILE_PATH" fi ``` ### 关键参数说明: 1. `-f`:检测是否为常规文件(非目录/符号链接) 2. `>/dev/null` 或 `>nul`:隐藏成功输出 3. `2>&1`:将错误输出重定向到标准输出 4. `&&`:前一条命令成功时执行后续命令 5. `||`:前一条命令失败时执行后续命令 > **注意**:路径区分大小写!如果路径包含空格,需用双引号包裹路径(如 `"my dir/file"`)。Linux/macOS 使用正斜杠`/`,Windows 使用反斜杠`\`[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值