第2节:合约部署

Reversing and debugging EVM Smart contracts: Deployment of a smart contract-part2

无payable

pragma solidity ^0.8.0;  
contract Test {      
    uint balance;      
    constructor() {         
        balance = 9;     
    } 
}

EVM中,存储相关一共有三个区域:

  1. stack
  2. memory
  3. storage

在执行构造函数之前,依然会做前置校验:msg.value等

涉及到构造函数的部分,主要是使用了SSTORE,将balance = 9存储到storage中,然后退出

中间比较含糊的是memory的地址问题,需要进一步理解(先忽略)

有payable

pragma solidity ^0.8.0;  
contract Test {      
    uint balance;      
    constructor() payable {         
        balance = 9;     
    } 
}
  1. opcode中,缺少了对msg.value校验对逻辑,因为已经不需要限定为0了
  2. 所以加上payable后opcode对size变小了,部署需要的gas也变少了

增加参数

pragma solidity ^0.8.0;  
    contract Test {       
        uint balance;      
        constructor(uint a,uint b) payable { 
            balance = 9;     
    } 
}
  • 配合使用CODESIZE, CODECOPY, 将参数拷贝到内存中;(1,2)
  • data字段是bytecode,evm会按照逻辑解析,并执行bytecode,这是指令
  • 验证参数个数,确定是2个,否则revert

总结

  1. It stores the free memory pointer as every smart contract does.
  2. It copies the 2 arguments provided by transaction data and store them to memory.
  3. It verifies that we entered at least 2 arguments in the constructor. (not less)
  4. It copies the 2 arguments in memory in to the stack.
  5. It executes the constructor by setting balance to 9.
  6. It copies the code into memory and stop the execution.

Tips

  • Every solidity smart contracts starts by mstore(0x40,0x80), which is 0x6080604052 in hex.