繁体   English   中英

为什么我们不能通过智能合约将以太网发送到以太坊地址0x1

[英]Why we can't send ether to ethereum address 0x1 via smart contracts

有了这个下面的可靠性代码,我试图通过智能合约将以太网发送到以太坊钱包地址0x1 ,它就会失败。 但是,当我尝试直接从我的钱包发送以太网到地址0x1时 ,它就会成功。

pragma solidity ^0.4.24;

contract Transfer {

    constructor () public payable {
        // Deploy contract with 1000 wei for testing purpose
        require(msg.value == 1000);
    }

    function done() public {
        address(0).transfer(1); // Transaction success
    }

    function fail() public {
        address(1).transfer(1); // Transaction failed
    }

    function send(address account) public {
        account.transfer(1); // Transaction success (except 0x1)
    }

}

为什么我们不能通过合约将以太网发送到地址0x1

参考:

  1. 直接从我的钱包发送以太币是成功的https://ropsten.etherscan.io/tx/0x1fdc3a9d03e23b0838c23b00ff99739b775bf4dd7b5b7f2fa38043056f731cdc

  2. done()函数成功https://ropsten.etherscan.io/tx/0xd319c40fcf50bd8188ae039ce9d41830ab795e0f92d611b16efde0bfa1ee82cd

  3. fail()函数失败https://ropsten.etherscan.io/tx/0x0c98eafa0e608cfa66777f1c77267ce9bdf81c6476bdefe2a7615158d17b59ad


更新:

在研究了以太坊预先编译的合同之后,我在下面写了固态代码,通过智能合约将以太网发送到0x1地址并且它正在工作。

pragma solidity ^0.4.24;

contract Learning {

    constructor () public payable {
        // Deploy contract with 1000 wei for testing purpose
        require(msg.value == 1000);
    }

    function test() public returns (bool) {
        // Set minimum gas limit as 700 to send ether to 0x1
        transfer(0x0000000000000000000000000000000000000001, 1, 700);
        return true;
    }

    function transfer(address _account, uint _wei, uint _gas) private {
        require(_account.call.value(_wei).gas(_gas)());
    }
}

对于测试,只需使用1000 wei部署合同并执行test()函数。 它的工作:)

你不小心偶然发现了以太坊中鲜为人知的一个“特征”。 该链实际上有一些预编译合同( ecrecover附录E),其中一个生活在0x0000000000000000000000000000000000000001ecrecover合同)。

由于ecrecover合同的回退执行需要的数量超过transfer方法转发的2300 gas ,因此fail()函数会因ecrecover气体而失败。

0x0地址不是特殊合同,因此常规转接呼叫可以正常工作,就像使用任何其他地址一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM