1.交易携带的data所需要gas
-
初始gas:53000或者21000
- data([]byte)的byte中不为0的每个需要68gas
- data([]byte)的byte中为0的每个需要4gas
- 最终结果:初始gas + byte不为0的个数 * 68 + byte为0的个数 * 4
- 如果最终结果大于math.MaxUint64,返回ErrOutOfGas
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
func IntrinsicGas(data []byte, contractCreation, homestead bool) (uint64, error) {
// Set the starting gas for the raw transaction
var gas uint64
if contractCreation && homestead {
gas = params.TxGasContractCreation //53000
} else {
gas = params.TxGas //21000
}
// Bump the required gas by the amount of transactional data
if len(data) > 0 { //交易携带的data的长度
// Zero and non-zero bytes are priced differently
var nz uint64
for _, byt := range data {
if byt != 0 { //统计byte有多少个不为0的byte
nz++
}
}
fmt.Println("test state_transition.got IntrinsicGas math.MaxUint64 = ", math.MaxUint64)
// Make sure we don't exceed uint64 for all data combinations
if (math.MaxUint64-gas)/params.TxDataNonZeroGas < nz { //不会因为nz太大导致导致最终gas超过 math.MaxUint64
return 0, vm.ErrOutOfGas
}
gas += nz * params.TxDataNonZeroGas //每个非0byte的gas为68
z := uint64(len(data)) - nz //byte为0的个数
if (math.MaxUint64-gas)/params.TxDataZeroGas < z { //每个0 byte的gas为4
return 0, vm.ErrOutOfGas
}
gas += z * params.TxDataZeroGas
}
return gas, nil
}