0x01 大量参数的问题
随着智能合约要处理的问题越来越复杂,引入的配置参数也越来越多,这些配置参数通常作为状态变量放在合约里,并且需要引入管理员权限来设置这些变量。通常我们都是像下面这个代码一样去处理这些变量:
address public token;
uint256 public minAmount;
uint256 public maxSize;
bytes32 public hash;
event SetToken(address oldToken, address newToken);
event SetMinAmount(uint256 oldMinAmount, uint256 newMinAmount);
event SetMaxSize(uint256 oldMaxSize, uint256 newMaxSize);
event SetHash(bytes32 oldToken, bytes32 newHash);
function setToken(address _token) external onlyAdmin {
emit SetToken(token, _token);
token = _token;
}
function setMinAmount(uint256 _minAmount) external onlyAdmin {
emit SetMin(minAmount, _minAmount);
min = _min;
}
function setMaxSize(uint256 _maxSize) external onlyAdmin {
emit SetMax(maxSize, _maxSize);
max = _max;
}
function setHash(bytes32 _hash) external onlyAdmin {
emit SetHash(hash, _hash);
hash = _hash;
}
这种写法,一个变量配一个事件一个 setter, 正常工作是没啥问题的,只是随着变量数量的增加,代码膨胀起来会非常快,并且看起来都是些类似的代码。
0x02 合并参数设置
为了减少这些模版代码,有一种做法是把设置方法合并,就像下面这样:
function setParameters(address _token, uint256 _minAmount, uint256 _maxSize, bytes32 _hash) external onlyAdmin {
......
}
这确实可以减少一些模版代码,但带来的最直接的问题就是每次都要把所有参数都设置一遍,带来误设参数的风险。
0x03 对参数进行类型抽象
另外一种做法是对参数进行类型抽象,就像下面这样:
address public token;
uint256 public minAmount;
uint256 public maxSize;
bytes32 public hash;
event SetParameter(bytes32 parameter, bytes value);
error InvalidParameter(bytes32 parameter);
function setParameter(bytes32 parameter, bytes calldata value) external onlyAdmin {
if (parameter == "token") token = abi.decode(value, (address));
else if (parameter == "minAmount") minAmount = abi.decode(value, (uint256));
else if (parameter == "maxSize") maxSize = abi.decode(value, (uint256));
else if (parameter == "hash") hash = abi.decode(value, (bytes32));
else revert InvalidParameter(parameter);
emit SetParameter(parameter, value);
}
这种写法,代码看起来更紧凑了,代价就是设置参数的时候要确定参数名称,并且对参数的值进行 encode 处理。
如果不想对参数进行编码处理,可以使用对不同类型参数进行重载的方式,像下面这样:
function setParameter(bytes32 parameter, address value) external onlyAdmin {
if (parameter == "token") token = abi.decode(value, (address));
else revert InvalidParameter(parameter);
emit SetParameter(parameter, abi.encode(value));
}
function setParameter(bytes32 parameter, uint256 value) external onlyAdmin {
if (parameter == "minAmount") minAmount = abi.decode(value, (uint256));
else if (parameter == "maxSize") maxSize = abi.decode(value, (uint256));
else revert InvalidParameter(parameter);
emit SetParameter(parameter, abi.encode(value));
}
function setParameter(bytes32 parameter, bytes32 value) external onlyAdmin {
if (parameter == "hash") hash = abi.decode(value, (bytes32));
else revert InvalidParameter(parameter);
emit SetParameter(parameter, abi.encode(value));
}
参数也可以使用 enum 类型,像下面这样
enum ParameterType {
NotSupported,
Token,
MinAmount,
MaxSize
Hash
}
function setParameter(ParameterType parameter, bytes calldata value) external onlyAdmin {
if (parameter == ParameterType.Token) token = abi.decode(value, (address));
else if (parameter == ParameterType.MinAmount) minAmount = abi.decode(value, (uint256));
else if (parameter == ParameterType.MaxSize) maxSize = abi.decode(value, (uint256));
else if (parameter == ParameterType.Hash) hash = abi.decode(value, (bytes32));
else revert InvalidParameter(parameter);
emit SetParameter(parameter, value);
}
0x04 file 模式
上面的这种做法,也被称作 file 模式,在几个项目里都有体现
比如在 MakerDAO 的这个代码里](https://github.com/makerdao/dss/blob/fa4f6630afb0624d04a003e920b0d71a00331d98/src/vat.sol#L104)%E9%87%8C),就有下面的代码片段:
function file(bytes32 what, uint data) external auth {
require(live == 1, "Vat/not-live");
if (what == "Line") Line = data;
else revert("Vat/file-unrecognized-param");
}
function file(bytes32 ilk, bytes32 what, uint data) external auth {
require(live == 1, "Vat/not-live");
if (what == "spot") [ilks[ilk].spot](http://ilks[ilk].spot/) = data;
else if (what == "line") ilks[ilk].line = data;
else if (what == "dust") ilks[ilk].dust = data;
else revert("Vat/file-unrecognized-param");
}
在 Astaria 的这个代码里](https://github.com/AstariaXYZ/astaria-core/blob/f4be95017b09dd5b78741cdffae4e07c0b06f68b/src/LienToken.sol#L82)%E9%87%8C),有下面的代码片段:
function file(File calldata incoming) external requiresAuth {
FileType what = incoming.what;
bytes memory data = incoming.data;
LienStorage storage s = _loadLienStorageSlot();
if (what == FileType.CollateralToken) {
s.COLLATERAL_TOKEN = ICollateralToken(abi.decode(data, (address)));
} else if (what == FileType.AstariaRouter) {
s.ASTARIA_ROUTER = IAstariaRouter(abi.decode(data, (address)));
} else {
revert UnsupportedFile();
}
emit FileUpdated(what, data);
}
0x05 总结
当合约里需要维护大量配置参数时,通常的写法会让合约代码快速膨胀,这时可以采用对参数进行编码的方式使代码更加紧凑,不过具体采用哪种写法,还要看具体情况而定。
网友评论