Renaissance Labs

Posted on Feb 24, 2022Read on Mirror.xyz

Learn Solidity Series 1: constant and view

What is constant ?

Functions can be declared constant in which case they promise not to modify the state.

也就是说当执行函数时不会去修改区块中的数据状态时,那么这个函数就可以被声明成constant的,比如说getter类的方法。

同时,当函数被constant修饰时也是提示web3js(或其他json-rpc客户端)调用此方法时要使用eth_call函数而不是eth_sendTransaction。

constant需要编程时明确指定,即使状态不会改变,编译器也不会自动添加。一般情况下调用constant声明的方法不需要花费gas,如果未使用constant修饰的函数在调用的过程中可能会生成一笔交易并且产生交易费用。

constant VS view

constant on functions is an alias to view, but this is deprecated and will be dropped in version 0.5.0.

constant是view的别名,不过constant在0.5.0版本中将会被去掉。这也是我们在写智能合约时需要注意的事项。目前网络上的示例基本上还都采用constant来进行修饰。

那么,文档中已经描述这两者是相同的,那么为什么要用view来替代constant呢?基本上原因是这样的,使用constant有一定的误导性,比如用constant修饰的方法返回的结果并不是常量,而是根据一定的情况有所变化。而且,用constant来修饰并不是那么细致入微。因此,引入了更有意义和更有用的view和pure来代替constant。

Before and after

Before:

  • constant修饰的函数不应该修改状态(尚未完全强制执行);
  • constant修饰的变量(类中的变量而不是方法)每次调用时都会被重新计算;

After:

  • 关键词view用来修饰函数,替换掉constant。调用view修饰的函数不能改变未来与任何合约交互的行为。这意味着被修饰的函数不能使用SSTORE,不能发送或接收以太币,只能调用其他view或pure修饰的函数。
  • 关键字pure用来修饰函数,是在view修饰函数上附加了一些限制,函数的结果仅取决于函数的参数。这意味着它不能使用SSTORE,SLOAD,不能发送或接收以太币,不能使用msg或block而只能调用其他pure函数。
  • 关键字constant针对函数无效。
  • 任何用constant修饰的变量将不能被修改(可以由优化器放入内存或字节码中)