Solidity Language description

Solidity Language Description

contract

  • address

  • functions

    • receive()和fallback()

    • 函数调用success和returndata

      accounts[0].transfer(to=accounts[1].address, amount=1, data=“112”)

      pragma solidity ^0.8.9;
      
      import "@openzeppelin/contracts/utils/Address.sol";
      
      
      contract Bridge {
      
      }
      
      contract Bridge2 {
      
          fallback() external{}
      
      }
      
      interface CC {
          function H() external;
          function H2() external returns(uint256);
          function H3() external returns(bool);
          function H4() external returns(bytes memory);
      }
      
      
      contract  A {
          address public addr;
      
          event EV(bool, bytes);
      
          function setAddr(address _addr) external {
              addr = _addr;
          }
      
          //addr = EOA, revert
          //addr = ContractWithoutFallback, revert
          //addr = ContractWithFallbck, normal
          function H() external {
              CC(addr).H();
          }
          //addr = EOA, revert
          //addr = ContractWithoutFallback, revert
          //addr = ContractWithFallbck, revert
          function H2() external {
              CC(addr).H2();
          }
          //addr = EOA, revert
          //addr = ContractWithoutFallback, revert
          //addr = ContractWithFallbck, revert
          function H3() external {
              CC(addr).H3();
          }
          //addr = EOA, revert
          //addr = ContractWithoutFallback, revert
          //addr = ContractWithFallbck, revert
          function H4() external {
              CC(addr).H4();
          }
      }
      
      contract A2 {
          using Address for address;
      
          address public addr;
      
          event EV(bool, bytes);
      
          function setAddr(address _addr) external {
              addr = _addr;
          }
      
          //addr = EOA, success=true, returndata=0x0
          //addr = ContractWithoutFallback, success=false, returndata=0x0
          //addr = ContractWithFallbck, success=true, returndata=0x0
          function H() external {
              (bool success, bytes memory returndata) = addr.call("");
              emit EV(success,returndata);
          }
          //addr = EOA, success=true, returndata=0x0
          //addr = ContractWithoutFallback, success=false, returndata=0x0
          //addr = ContractWithFallbck, success=true, returndata=0x0
          function H2() external {
              (bool success, bytes memory returndata) = addr.call("");
              emit EV(success,returndata);
          }
          //addr = EOA, success=true, returndata=0x0
          //addr = ContractWithoutFallback, success=false, returndata=0x0
          //addr = ContractWithFallbck, success=true, returndata=0x0
          function H3() external {
              (bool success, bytes memory returndata) = addr.call("");
              emit EV(success,returndata);
          }
          //addr = EOA, success=true, returndata=0x0
          //addr = ContractWithoutFallback, success=false, returndata=0x0
          //addr = ContractWithFallbck, success=true, returndata=0x0
          function H4() external {
              (bool success, bytes memory returndata) = addr.call("");
              emit EV(success,returndata);
          }
      }
      
  • k

  • proxy

    Metamorphic合约本质,create2

  • size

    如何缩减合约大小:

    • 一个是合并同类项,将相同逻辑部分作为一个内部函数存在

    • 使用库

    • 使用代理合约

    • 避免额外的变量

    • 缩短错误信息

    • runs调整

      默认值为 200,表示它试图在一个函数被调用 200 次的情况下优化字节码。 如果您将其改为 1,相当于告诉优化器针对每个函数只运行一次的情况进行优化。 一个仅运行一次的优化函数意味着它对部署本身进行了优化。 请注意,这将会增加运行函数的 gas 成本,所以,您可能不想这样做。

  • bytecode

    NameDecription
    bytecodehardhat编译时文件, 同creationCode
    creationCode同type(C).creationCode , transaction的input
    deployBytecodehardhat编译时文件, 存储在区块链中的code,type(C).runtimeCode

    At a high level, the wallet deploying the contract sends a transaction to the null address with the transaction data laid out in three parts:

    <init code><creation code><constructor paramters>
    

    creationcode:

    <init code><runtimecode>
    

    There are two types of code for smart contracts, as show below: the creation code: this is the contract’s bytecode that includes the instruction for deploying the contract and running the constructor logic.the runtime code: this is the final bytecode of the contract once it has been deployed on the blockchain.

    The runtime bytecode for a contract can be retrieved on-chain by using an assembly block and calling excodecopy(a).The hash of the runtime bytecode is returned from excodehash(a). web3.eth.getCode(B[0].address)

    • solidity存储变量寻址计算

Layout in storage

storage > memory > calldata.

  • calldata

  • memory

    four 32-byte slots.

    • 0x00-0x3f(64 bytes): scratch space for hashing methods
    • 0x40-0x5f(32 bytes): currently allocated memory size
    • 0x60-0x7f(32 bytes): zero slot

    scratch space can be used between statements.

    zero slot is used as initial value for dynamic memory array and should never be written to(the free memory pointer points to 0x80 initially).

    solidity always places new objects at the free memory pointer and memory is never freed.

  • stroage

  • stack

    stack数量限制.(error: stack too deep)

Resources