Solidity

無限呼叫自身的遞歸合約

  • October 22, 2022

我想創建一個契約,以便在eth_call被呼叫時,契約返回 EVM 錯誤:CallTooDeep

嘗試過這樣的事情:

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.17;

contract CallToDeep {
   function test() public {
       address(this).test();
   }
}

但得到一個錯誤:

TypeError: Member "test" not found or not visible after argument-dependent lookup in address.
--> contracts/CallToDeep.sol:6:9:
 |
6 |         address(this).test();
 |         ^^^^^^^^^^^^^^^^^^


Error HH600: Compilation failed

我該如何解決這個錯誤?

如果沒有提供函式的導入介面,則不能只按函式名稱呼叫該函式。

嘗試這個:

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.17;

contract CallToDeep {
   function test() public {
      CallToDeep(address(this)).test();
   }
}

gasLimit: 29,900,000, 453 內部交易https://goerli.etherscan.io/tx/0xa4e15670efd5d966cce73b807292524f11810bfc5cba9cb08c52de6982d68b89

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.17;

contract CallToDeep {
   function test() public {
      CallToDeep(address(this)).test();
   }
}
{
   "jsonrpc": "2.0",
   "method": "eth_call",
   "params": [
       {
           "data": "0xf8a8fd6d",
           "to": "0xc20a1e280ab7786d718eb38b062b9004943d9eee"

       },
       "latest"
   ],
   "id": 2
}
{
   "jsonrpc": "2.0",
   "id": 2,
   "error": {
       "code": -32000,
       "message": "execution reverted"
   }
}

https://goerli.etherscan.io/tx/0x19c6ef37e7490b19f60b851de85d4a4b0fb472a9445502769f537d8e8ebb0a2e

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.17;

contract CallToDeep {
   function test() public {
      CallToDeep.test();
   }
}
{
   "jsonrpc": "2.0",
   "method": "eth_call",
   "params": [
       {
           "data": "0xf8a8fd6d",
           "to": "0x26538b1453083081c8180f2600261ddc61730846"

       },
       "latest"
   ],
   "id": 2
}
{
   "jsonrpc": "2.0",
   "id": 2,
   "error": {
       "code": -32000,
       "message": "stack limit reached 1024 (1023)"
   }
}

引用自:https://ethereum.stackexchange.com/questions/137924