第158篇 合约安全-selfdestruct

 solidity 可以通过调用 selfdestruct 从区块链中删除合约;

selfdestruct 将合约中存储的所有剩余 Ether 发送到指定地址。

弱点:恶意合约可以使用 selfdestruct 来强制向任何合约发送以太币。

1.示例合约

一个模拟业务合约 EtherGame

  1. 合约是一场游戏,用户的目标是成为第 7 位存入 1 ether 的玩家;
  2. 玩家一次只能存入 1 ether;
  3. 获胜者将能够提取所有 ether;

一个攻击合约 Attack

  1. 本合约销毁时会发送 ether 到指定的合约;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract EtherGame {
    uint public targetAmount = 7 ether;
    address public winner;

    function deposit() public payable {
        require(msg.value == 1 ether, "You can only send 1 Ether");

        uint balance = address(this).bal

猜你喜欢

转载自blog.csdn.net/wonderBlock/article/details/128520591
158