6

计算Solidity 函数的Gas 消耗

 3 years ago
source link: https://learnblockchain.cn/article/2716
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

计算Solidity 函数的Gas 消耗

本文通过字符串连接函数为例,来看看如何计算及对比 Solidity 函数的 gas 消耗

Solidity 是以太坊区块链默认使用的智能合约语言,本文讨论了 Solidity 中使用合约和库函数时的 gas 消耗。

在 Datona 实验室的身份合约模板开发过程中,我们希望提供一些有用的错误信息,就需要一些字符串操作,如字符串连接,例如:

function TransferTo(address _address, uint amount) public onlyOwner {
    require(amount <= unallocated, concat("Invalid amount. "
        "Available:", stringOfUint(unallocated)));
    // whatever
}

在Solidity 编译器中,字符串连接是很方便的:

string memory result = string(abi.encodePacked("trust", "worthy"))

但是我们想用一个更有意义的名字来包装它,并将其包含在其他有用的字符串实用函数中(如整数的字符串)

当然,我们想尽可能减少 gas 费用,因为相比普通系统,像 Solidity 这样的区块链语言运行起来是非常昂贵的,毕竟 gas 费用可是一笔不小的钱。

为了增加字符串连接功能(它只是一个pure函数,并不能访问状态信息),Solidity 智能合约提供了如下访问方式:

(1) 直接使用编译器提供的方法,如:abi.encodePacked
(2) 继承自基类合约的访问,使用内部(直接)调用
(3) 继承自基类合约的访问,使用外部(间接)调用
(4) 组件合约的访问,使用外部(间接)调用
(5) 库合约的访问,使用内部(直接)调用
(6) 库合约的访问,使用外部(间接)调用

不能通过内部(直接)调用的方式来访问一个组件合约。

参考创建合约,后面章节也有涉及到连接选项的库函数的说明。

接下来的几节会演示不同连接选项的实现。

(2)和(3)继承自基类合约

基类合约提供内部和外部的字符串连接函数(下面归为public),如下所示:

contract Base {
    function Internal(string memory sm0, string memory sm1) 
        internal pure returns (string memory)
    {
        return string(abi.encodePacked(sm0, sm1));
    }
    function External(string memory sm0, string memory sm1)
        public pure returns (string memory)
    {
        return string(abi.encodePacked(sm0, sm1));
    }
}

这里的函数名有点特别,只是为了这篇文章而取的,每一个函数都实现了字符串连接,我们关注的是不同连接选项的性能。

为了能正常使用函数,我们必须实现一个继承合约:

contract MyContract is Base {
    // whatever
}

这些函数可以通过点符号来访问,或者直接省略掉基类合约的名字(你公司的代码标准文档也许有自己的一些规定)

string memory sm = Base.Internal("pass", "word");
    string memory xx = Internal("what", "ever");

(4) 访问组件合约

组件合约被声明为合约的一个组成部分,必须在声明时或在构造函数中创建。

contract Component is Base {...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK