EVM

 
 
 
Four Types of Storage
  • Storage (state variable, saved forever as opposed to only for the tx)
    • The storage location is permanent data, which means that this data can be accessed into all functions within the contract, just like the hard disk data of computer where all the data gets stored permanently. Keep in mind that storage data location is expensive compared to other data locations.
  • Memory (function parameters including return parameters)
    • The memory location is temporary data and cheaper than the storage location. It can only be accessible within the function. Usually, Memory data is used to save temporary variables for calculation during function execution. Once the function gets executed, its contents are discarded. You can think of it as a RAM of each individual function.
  • Calldata (immutable memory,除此等同于memory, more gas efficient)
    • Calldata is non-modifiable and non-persistent data location where all the passing values to the function are stored. Also, Calldata is the default location of parameters (not return parameters) of external functions.
  • Stack
    • Stack is a non-persistent data maintained by EVM (Ethereum Virtual Machine). EVM uses stack data location to load the variables during execution. Stack location has the limitation up to 1024 levels.
  • Rules
    • State Variables are always stored in the storage,类似于全局变量,函数内部对变量的调动和修改会影响变量本身的取值
    • pragma solidity ^0.8.0; contract DataLocation { //storage uint256 public tokenCounter; object = {a: "1"} function create(string memory _svg) public { ... tokenCounter += 1; }
      contract DataLocation { uint storage stateVariable; //Expected identifier but got 'storage' uint[] memory stateArray; //Linter: Parse error: extraneous input 'memory' expecting {'from', 'error', 'calldata', 'revert', 'callback', 'override', 'constant', 'immutable', 'leave', 'internal', 'payable', 'private', 'public', 'constructor', 'receive', Identifier} [undefined] }
    • Function parameters including return parameters are by default stored in memory. 如果是函数原生定义的可以不做声明,但其他情况需要声明为memory,确保函数内对该变量的调用和修改不会影响变量本身。
    • contract DataLocation { function addUp(uint256 num1,uint256 num2) public pure returns(uint256 result){ return num1 + num2} }
    • Calldata is almost the same as memory but you cannot manipulate (change) the variable. It's also a bit more gas efficient, Use memory if you want to be able to manipulate the values and calldata when you don't
    • Function parameters (not including returns parameters) of external function are stored in the Calldata.
    • Storage let's you directly modify what your smart contract is storing. Meaning that the change will be "saved" to your smart contract. Use storage when you want to make an instance of a variable that your smart contract will "save" in its bytcode (I.e for changes that need to persist between transactions, like balances). Ideally though you just change the variable from storage directly and don't pass around a storage variable.
合约内部调用另外合约的四种调用方式
  • Call
  • Callcode
  • Delegatecall
  • Staticall
区块链 以太坊 虚拟机 EVM 详解【附源码】_软件工程小施同学_51CTO博客
虚拟机用来 交易分两种: 在执行交易时需要支付油费。 智能合约之间的调用有四种方式。 以太坊虚拟机,简称 EVM,是用来执行以太坊上的交易的。 业务流程如下图: 输入一笔交易,内部会转换成一个 Message 对象,传入 EVM 执行。 如果是一笔普通转账交易,那么直接修改 StateDB 中对应的账户余额即可。 如果是的创建或者调用,则通过 EVM 中的解释器加载和执行字节码,执行过程中可能会查询或者修改 StateDB。 每笔交易过来,不管三七二十一先需要收取一笔固定油费,计算方法如下: 如果你的交易不带额外数据(Payload),比如普通转账,那么需要收取 21000 的油费。 如果你的交易携带额外数据,那么这部分数据也是需要收费的,具体来说是按字节收费: 所以你会看到很多做合约优化的,目的就是减少数据中不为 0 的字节数量,从而降低油费 gas 消耗。 交易会被转换成一个 Message 对象传入 EVM,而 EVM 则会根据 Message 生成一个 Contract 对象以便后续执行: 可以看到,Contract 中会根据合约地址,从 StateDB 中加载对应的代码,后面就可以送入解释器执行了。 另外,执行合约能够消耗的油费有一个上限,就是节点配置的每个区块能够容纳的 GasLimit 。 代码跟输入都有了,就可以送入解释器执行了。EVM 是基于栈的虚拟机,解释器中需要操作四大组件: EVM 的每条指令称为一个
区块链 以太坊 虚拟机 EVM 详解【附源码】_软件工程小施同学_51CTO博客
  • Take-out