17

fisco bcos中solidity销毁合约,删除合约的方法

 3 years ago
source link: https://learnblockchain.cn/article/1645
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.

fisco bcos中solidity销毁合约删除合约的方法

3I3qIrf.png!mobile 1.字节码中有一个selfdestruct指令,用于销毁合约。所以只需要暴露出自毁接口即可:

contract Mortal{ 
//自毁 
function destroy() public
{ selfdestruct(msg.sender); 
    } 
      }

2.Automatic Deprecation - 允许合约自动停止服务

如果你希望一个合约在指定期限后停止服务,而不需要人工介入,可以使用Automatic Deprecation模式。

contract AutoDeprecated{
 
    uint private _deadline;
 
    function setDeadline(uint time) public {
        _deadline = time;
    }
 
    modifier notExpired(){
        require(now <= _deadline);
        _;
    }
 
    function service() public notExpired{ 
        //some code    
    } 
}

当用户调用service,notExpired修饰符会先进行日期检测,这样,一旦过了特定时间,调用就会因过期而被拦截在notExpired层。

ps: 合约部署前只能是内置了上述的函数,才能手动触发或者定时触发销毁。否则部署的合约会永久性的运行在系统之中。

3I3qIrf.png!mobile 1.字节码中有一个selfdestruct指令,用于销毁合约。所以只需要暴露出自毁接口即可:

contract Mortal{ 
//自毁 
function destroy() public
{ selfdestruct(msg.sender); 
    } 
      }

2.Automatic Deprecation - 允许合约自动停止服务

如果你希望一个合约在指定期限后停止服务,而不需要人工介入,可以使用Automatic Deprecation模式。

contract AutoDeprecated{

    uint private _deadline;

    function setDeadline(uint time) public {
        _deadline = time;
    }

    modifier notExpired(){
        require(now <= _deadline);
        _;
    }

    function service() public notExpired{ 
        //some code    
    } 
}

当用户调用service,notExpired修饰符会先进行日期检测,这样,一旦过了特定时间,调用就会因过期而被拦截在notExpired层。

ps: 合约部署前只能是内置了上述的函数,才能手动触发或者定时触发销毁。否则部署的合约会永久性的运行在系统之中。

本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

  • 发表于 8分钟前
  • 阅读 ( 26 )
  • 学分 ( 0 )
  • 分类:FISCO BCOS

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK