首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确保只有Keepers可以调用我的合同的"performUpkeep“函数

确保只有Keepers可以调用我的合同的"performUpkeep“函数
EN

Ethereum用户
提问于 2021-11-01 16:50:36
回答 2查看 261关注 0票数 2

我有一个管理员的performUpKeep功能,这是关键的任务,只有管理员可以打电话,而不是一些随机的第三方,以激活合同更快。似乎链链管理员不断变化,所以我如何确保只有守门员才能调用合同?

EN

回答 2

Ethereum用户

回答已采纳

发布于 2021-11-01 18:03:32

理想情况下,您只需要验证checkUpKeep是否返回true,并且对输入进行一些数据验证,以确保它只能执行您希望它做的事情。根据链式github

  • @dev此方法的输入不应受信任,而且*方法的调用者甚至不应仅限于任何单个注册表。任何人都应该能够调用它,并且输入应该被验证,不能保证传入的数据是从performData返回的checkUpkeep。当performUpkeep事务等待确认时,这种情况可能是由于恶意的守护者、赛车守护者或简单的状态*更改造成的。*始终验证传入的数据。

以下代码不建议使用

如果你想忽视这件事..。(您不应该这么做),但是您可以向您的onlyKeepers函数中添加一个performUpkeep修饰符,这样只有Keepers注册中心才能调用您的合同。

代码语言:javascript
复制
    address public keepersRegistry = xxxxxx;
    modifier onlyKeepers() {
        require(msg.sender == keepersRegistry, "Ownable: caller is not keepers registry");
        _;
    }

然后用它包装你的函数。

代码语言:javascript
复制
function functionName() onlyKeepers {
....
}
票数 3
EN

Ethereum用户

发布于 2021-11-01 18:43:23

添加到前面的答案,因为我不确定它是否正确。

chainlink KeeperCompatibleInterface实现为performUpkeep函数指定:

代码语言:javascript
复制
    * @dev The input to this method should not be trusted, and the caller of the
    * method should not even be restricted to any single registry. Anyone should
    * be able call it, and the input should be validated, there is no guarantee
    * that the data passed in is the performData returned from checkUpkeep. This
    * could happen due to malicious keepers, racing keepers, or simply a state
    * change while the performUpkeep transaction is waiting for confirmation.
    * Always validate the data passed in.

其中最重要的一点显然是:

代码语言:javascript
复制
 * @dev The input to this method should not be trusted, and the caller of the
 * method should not even be restricted to any single registry.

考虑到您必须实现一个checkUpkeep函数,您只需在调用performUpkeep逻辑之前再次检查checkUpkeep的条件就可以保证自己的安全。当需要维修时,如果第三方要求你的合同,那就去做。如果没有,则恢复事务。

作为指定的这里

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/112677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档