假设一个函数接受一个地址数组,如下所示:
function setVoters(address[] _inputAddresses) public ownerOnly {
// [...]
}
使用上述函数的同一合约有一个定义为映射的变量:
mapping(address => bool) voter;
在数组上循环并将其推送到映射是否被认为是耗油/开销的最佳选择,还是如果函数接受一个地址并通过某些JavaScript功能从给定的UI进行迭代会更好?
选项a:
function setVoters(address[] _inputAddresses) public ownerOnly {
// [...]
for (uint index = 0; index < _inputAddresses.length; index++) {
voter[_inputAddresses[index]] = true;
}
}
vs
选项b:
function setVoter(address _inputAddress) public ownerOnly {
// [...]
voter[_inputAddress] = true;
}
JavaScript应该是这样的
// loop condition starts here
await task.methods.setVoter(address[key]).send({
from: accounts[0]
});
// loop condition ends here
发布于 2018-07-25 07:21:05
在气体效率方面最好的是选项a,调用一个函数需要相当多的气体,所以如果你在一个大的tx上做所有的事情,而不是在许多小的tx上做,你会花更少的钱。
https://stackoverflow.com/questions/51500880
复制相似问题