Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
core: check msg.To() is nil or not before using it; make sure contrac…
Browse files Browse the repository at this point in the history
…tCreation is not freegas tx
  • Loading branch information
hydai committed May 26, 2019
1 parent 1f89df9 commit 557cc49
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
errInsufficientBalanceForGas = errors.New("insufficient balance to pay for gas")
errInsufficientContractBalanceForFreeGas = errors.New("insufficient contract balance to pay for gas")
errCallNonFreeGasFunctionWithZeroGasPrice = errors.New("zero gasPrice transaction cannot call the non-freegas function")
errCannotBuyGasFromNilAddress = errors.New("cannot buy gas from nil address")
)

/*
Expand Down Expand Up @@ -171,6 +172,9 @@ func (st *StateTransition) buyGasFromSender() error {
func (st *StateTransition) buyGasFromContract() error {
defaultGasPrice := st.evm.Context.Umbrella.DefaultGasPrice()
mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), defaultGasPrice)
if st.msg.To() == nil {
return errCannotBuyGasFromNilAddress
}
if st.state.GetBalance(*st.msg.To()).Cmp(mgval) < 0 {
return errInsufficientContractBalanceForFreeGas
}
Expand Down Expand Up @@ -224,7 +228,14 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
isFreeGasTX = false
)

if currentGasPrice.Cmp(big.NewInt(0)) == 0 && currentGasLimit > defaultGasLimit {
msg := st.msg
sender := vm.AccountRef(msg.From())
homestead := st.evm.ChainConfig().IsHomestead(st.evm.BlockNumber)
contractCreation := msg.To() == nil
contractCall := msg.To() != nil
zeroGasPrice := currentGasPrice.Cmp(big.NewInt(0)) == 0

if zeroGasPrice && currentGasLimit > defaultGasLimit && contractCall {
// FreeGas TX
isFreeGasTX = true
log.Debug("trying to call a freegas function", "err", nil)
Expand All @@ -243,11 +254,6 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
}
}

msg := st.msg
sender := vm.AccountRef(msg.From())
homestead := st.evm.ChainConfig().IsHomestead(st.evm.BlockNumber)
contractCreation := msg.To() == nil

// Pay intrinsic gas
gas, err := IntrinsicGas(st.data, contractCreation, homestead)
if err != nil {
Expand Down

0 comments on commit 557cc49

Please sign in to comment.