准备知识
以太坊黄皮书拜占庭版本中编号为65的公式刻画了单个交易执行结束后,总的可返还的gas的计算方法。
refund gas
就是要计算等式右边第二个项,即min( , )。
其中Ar
的含义如下【在黄皮书6.1节】:
下面的代码是geth(core/state_transition.go)中对编号为65的公式的实现:
func (st *StateTransition) refundGas() {
// Apply refund counter, capped to half of the used gas.
refund := st.gasUsed() / 2
if refund > st.state.GetRefund() {
refund = st.state.GetRefund()
}
st.gas += refund
// Return ETH for remaining gas, exchanged at the original rate.
remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gas), st.gasPrice)
st.state.AddBalance(st.msg.From(), remaining)
// Also return remaining gas to the block gas