Merge pull request #23 from sei-protocol/refactor-buygas

Refactor BuyGas
This commit is contained in:
codchen 2024-04-15 14:47:17 +08:00 committed by GitHub
commit b95a71e169
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 12 deletions

View file

@ -180,7 +180,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
// indicates a core error meaning that the message would always fail for that particular
// state and would never be accepted within a block.
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, error) {
return NewStateTransition(evm, msg, gp).TransitionDb()
return NewStateTransition(evm, msg, gp, false).TransitionDb()
}
// StateTransition represents a state transition.
@ -212,15 +212,17 @@ type StateTransition struct {
initialGas uint64
state vm.StateDB
evm *vm.EVM
feeCharged bool
}
// NewStateTransition initialises and returns a new state transition object.
func NewStateTransition(evm *vm.EVM, msg *Message, gp *GasPool) *StateTransition {
func NewStateTransition(evm *vm.EVM, msg *Message, gp *GasPool, feeCharged bool) *StateTransition {
return &StateTransition{
gp: gp,
evm: evm,
msg: msg,
state: evm.StateDB,
gp: gp,
evm: evm,
msg: msg,
state: evm.StateDB,
feeCharged: feeCharged,
}
}
@ -232,7 +234,7 @@ func (st *StateTransition) to() common.Address {
return *st.msg.To
}
func (st *StateTransition) buyGas() error {
func (st *StateTransition) BuyGas() error {
mgval := new(big.Int).SetUint64(st.msg.GasLimit)
mgval = mgval.Mul(mgval, st.msg.GasPrice)
balanceCheck := new(big.Int).Set(mgval)
@ -256,10 +258,14 @@ func (st *StateTransition) buyGas() error {
if have, want := st.state.GetBalance(st.msg.From), balanceCheck; have.Cmp(want) < 0 {
return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want)
}
st.state.SubBalance(st.msg.From, mgval, tracing.BalanceDecreaseGasBuy)
return nil
}
func (st *StateTransition) initGas() error {
if err := st.gp.SubGas(st.msg.GasLimit); err != nil {
return err
}
if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil {
st.evm.Config.Tracer.OnGasChange(0, st.msg.GasLimit, tracing.GasChangeTxInitialBalance)
}
@ -267,7 +273,6 @@ func (st *StateTransition) buyGas() error {
st.gasRemaining += st.msg.GasLimit
st.initialGas = st.msg.GasLimit
st.state.SubBalance(st.msg.From, mgval, tracing.BalanceDecreaseGasBuy)
return nil
}
@ -346,7 +351,12 @@ func (st *StateTransition) preCheck() error {
}
}
}
return st.buyGas()
if !st.feeCharged {
if err := st.BuyGas(); err != nil {
return err
}
}
return st.initGas()
}
// TransitionDb will transition the state by applying the current message and

View file

@ -253,7 +253,7 @@ func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
}
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Tracer: tracer.Hooks})
snap := statedb.Snapshot()
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()), false)
if _, err = st.TransitionDb(); err != nil {
b.Fatalf("failed to execute transaction: %v", err)
}

View file

@ -99,7 +99,7 @@ func BenchmarkTransactionTrace(b *testing.B) {
for i := 0; i < b.N; i++ {
snap := statedb.Snapshot()
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()), false)
_, err = st.TransitionDb()
if err != nil {
b.Fatal(err)