Notes on Solidity

by nemozqqz, 22 Nov 2018

Notes on Solidity

require VS assert

两者都在断言不满足的时候失败然后回滚,require比较友好,会退回剩余gas, assert会吃掉剩余gas。 require handles acceptable error, while assert means sth out of control.

transfer VS send VS call

三种不同层次的合约间调用。

如果直接调用合约的函数的话,默认是给所有的gas。其实大部分函数都能重入

区分 address.transfercontract.transfer,contract可能有一个叫transfer的函数。

call

区分solidity的call和EVM的call

call(gas, to ,value, input memory offset, output memory offset, output size)

calculating gas of a call is complex, see EIP150 (63/64?)

call(msg.gas- 2000,…) // call with some gas spared

memory/storage layout of array and map

arbitary write if index of array is controled??? Rewrite map values???

delete

delete in solidity sets storage/memory to zero, and refund gas

revert

revert会回滚当前call和它的subcall。正常函数调用的话,一级一级传递revert;但是call不会传递revert,return 0

A reentrancy honey pot。 ether发出去也是可以挽回的。。。

function f()public{
    if (msg.sender.call.value(1 ether){
        sth reverts
    }
}

All the inner calls reverts, except the outest(first) call,which just returns.

Class

constructor must be public or internal. A class’s constructor can be only used by it’s parent class, thus internal.

构造函数的return是runtime code, 使用return opcode可以在构造时强行逆天改命,而且etherscan 只verify了constructor code multi inheritance. Order matters

Balance

增加余额的几种方法

EOA or Contract

Solidity 0.5 updates