Skip to content

Commit

Permalink
Fix gas by dividing by 1mm (ethereum#221)
Browse files Browse the repository at this point in the history
* fix: gas consumption

* fix: divide gas before sending to ovm

* Improve var name

Co-authored-by: Mark Tyneway <[email protected]>
  • Loading branch information
karlfloersch and tynes authored Feb 25, 2021
1 parent 88fade2 commit 24918aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"math"
"math/big"
"os"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -162,16 +161,20 @@ func (st *StateTransition) useGas(amount uint64) error {

func (st *StateTransition) buyGas() error {
mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice)
if st.state.GetBalance(st.msg.From()).Cmp(mgval) < 0 {
return errInsufficientBalanceForGas
if !vm.UsingOVM {
if st.state.GetBalance(st.msg.From()).Cmp(mgval) < 0 {
return errInsufficientBalanceForGas
}
}
if err := st.gp.SubGas(st.msg.Gas()); err != nil {
return err
}
st.gas += st.msg.Gas()

st.initialGas = st.msg.Gas()
st.state.SubBalance(st.msg.From(), mgval)
if !vm.UsingOVM {
st.state.SubBalance(st.msg.From(), mgval)
}
return nil
}

Expand All @@ -180,7 +183,7 @@ func (st *StateTransition) preCheck() error {
if st.msg.CheckNonce() {
nonce := st.state.GetNonce(st.msg.From())
if nonce < st.msg.Nonce() {
if os.Getenv("USING_OVM") == "true" {
if vm.UsingOVM {
// The nonce never increments for L1ToL2 txs
qo := st.msg.QueueOrigin()
l1ToL2 := uint64(types.QueueOriginL1ToL2)
Expand Down
9 changes: 7 additions & 2 deletions core/state_transition_ovm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func asOvmMessage(tx *types.Transaction, signer types.Signer, decompressor commo
// does not equal zero or one, we have an invalid parameter and need to throw an error.
// This is technically a duplicate check because it happens inside of
// `tx.AsMessage` as well.
v = big.NewInt(int64(v.Uint64() - 35 - 2*tx.ChainId().Uint64()))
v = new(big.Int).SetUint64(v.Uint64() - 35 - 2*tx.ChainId().Uint64())
if v.Uint64() != 0 && v.Uint64() != 1 {
index := tx.GetMeta().Index
if index == nil {
Expand All @@ -102,6 +102,11 @@ func asOvmMessage(tx *types.Transaction, signer types.Signer, decompressor commo
target = *tx.To()
}

// Divide the gas price by one million to compress it
// before it is send to the sequencer entrypoint. This is to save
// space on calldata.
gasPrice := new(big.Int).Div(msg.GasPrice(), new(big.Int).SetUint64(1000000))

// Sequencer uses a custom encoding structure --
// We originally receive sequencer transactions encoded in this way, but we decode them before
// inserting into Geth so we can make transactions easily parseable. However, this means that
Expand All @@ -112,7 +117,7 @@ func asOvmMessage(tx *types.Transaction, signer types.Signer, decompressor commo
data.Write(fillBytes(s, 32)) // 32 bytes: Signature `s` parameter
data.Write(fillBytes(v, 1)) // 1 byte: Signature `v` parameter
data.Write(fillBytes(big.NewInt(int64(msg.Gas())), 3)) // 3 bytes: Gas limit
data.Write(fillBytes(msg.GasPrice(), 3)) // 3 bytes: Gas price
data.Write(fillBytes(gasPrice, 3)) // 3 bytes: Gas price
data.Write(fillBytes(big.NewInt(int64(msg.Nonce())), 3)) // 3 bytes: Nonce
data.Write(target.Bytes()) // 20 bytes: Target address
data.Write(msg.Data()) // ?? bytes: Transaction data
Expand Down

0 comments on commit 24918aa

Please sign in to comment.