区块链笔记:关于ABI和分析ABI中的字段,关于Gas,Gas消耗计算,Gas消耗表

关于ABI

  • 全称 Application Binary Interface 应用二进制接口
  • 描述智能合约调用编解码二进制信息
  • 在智能合约里,我们所能调用的是一些定义的方法,当我们在外部调用智能合约方法的时候
  • 到底传什么类型的参数,参数会有多大,返回值是什么等等这些信息,我们可以通过不同的语言来编写客户端调用
  • 这就要求,我们到底调用合约里面的具体什么方法?需要有一个二进制层面的描述,这个就叫做ABI
  • 与API有很大的差别
    • API是应用层面的编程调用接口,是宏观层面函数级别的
    • ABI是更加微观深入,是二进制级别的定义
    • 包含JSON格式的描述信息,关于function和event事件
  • 下面是一段ABI
"abi": [
    {
      "constant": true,
      "inputs": [
        {
          "internalType": "uint256",
          "name": "a",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "b",
          "type": "uint256"
        }
      ],
      "name": "Add",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "pure",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [
        {
          "internalType": "uint256",
          "name": "a",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "b",
          "type": "uint256"
        }
      ],
      "name": "Reduce",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "pure",
      "type": "function"
    }
]
  • 上面是具体的一些ABI的描述信息
  • 其实就是指对智能合约里面调用层面的接口方法的具体的描述
  • 我们在调用智能合约里面方法的时候,一定要有一个ABI的
  • 否则外部的调用方法就没法知道这个合约方法到底输入什么,返回什么
  • 如何查看ABI?
    • 使用truffle编译合约后可到build目录中查看
    • 目录:build/contracts/xxx.json

分析ABI

以描述Function的ABI格式为例

字段名 作用
name 方法名称
type 方法类型
inputs 输入参数
name 参数名称
type 参数类型
components 描述结构体
outputs 输出值
payable 是否可以接收Ether
constant 是否改写区块链状态
stateMutability pure\view 备注:pure指与账本无关的纯工具方法,view返回一些账本信息,且只读

关于Gas

  • Gas 英文翻译是汽油,是指在以太坊中对智能合约的部署,方法调用所需要消耗的以太币
  • 也就是说在以太坊中对合约的使用是有成本的,但是对账本数据读取是不需要消耗Gas的
  • 一般来说占用账本的存储,需要让虚拟机EVM执行某些转账或更改状态属性等才会小号Gas

Gas消耗的计算

  • eth.estimateGas({from:eth.accounts[0], to:eth.accounts[1], value:1000})
  • estimateGas这是以太节点支持的方法,可以计算比如转账1000的以太币需要消耗多少Gas
  • 这个方法可以在以太坊节点的控制台执行,也可以通过RPC来调用
  • 一般我们可以通过对Gas的计算,然后乘上Gas price来获得某个动作需要消耗的以太币

Gas消耗表

Operation Name Gas Cost Remark
step 1 default amount per execution cycle
stop 0 free
suicide 0 free
sha3 20
sload 20 get from permannet storage
sstore 100 put into permanent storage
balance 20
create 100 contract creation
call 20 initiating a read-only call
memory 1 every additional word when expanding memory
txdata 5 every byte of data or code for a transaction
transaction 500 base fee transaction
contract creation 53000 changed in homestead from 21000
  • EVM 执行的是字节码指令
  • 对于每一个步骤的执行会消耗1个单位的gas
  • 0表示不需要消耗
  • 如果需要进行sha3的哈希计算,需要消耗20个单位gas
  • 最下面创建一个合约需要消耗53000个单位gas,在以太坊一开始的版本中创建一个合约需要21000个单位gas, 这里有一个涨价了
  • 这里都是gas的消耗,注意:gas需要乘上gas price才是以太币的成本
发布了450 篇原创文章 · 获赞 254 · 访问量 72万+

猜你喜欢

转载自blog.csdn.net/Tyro_java/article/details/105008171
gas