core, core/state: move gas tracking out of core/state
The amount of gas available for tx execution was tracked in the StateObject representing the coinbase account. This commit makes the gas counter a separate type in package core, which avoids unintended consequences of intertwining the counter with state logic.
This commit is contained in:
@ -1,39 +0,0 @@
|
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type GasLimitErr struct {
|
||||
Message string
|
||||
Is, Max *big.Int
|
||||
}
|
||||
|
||||
func IsGasLimitErr(err error) bool {
|
||||
_, ok := err.(*GasLimitErr)
|
||||
|
||||
return ok
|
||||
}
|
||||
func (err *GasLimitErr) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
func GasLimitError(is, max *big.Int) *GasLimitErr {
|
||||
return &GasLimitErr{Message: fmt.Sprintf("GasLimit error. Max %s, transaction would take it to %s", max, is), Is: is, Max: max}
|
||||
}
|
@ -75,11 +75,6 @@ type StateObject struct {
|
||||
// Cached storage (flushed when updated)
|
||||
storage Storage
|
||||
|
||||
// Total gas pool is the total amount of gas currently
|
||||
// left if this object is the coinbase. Gas is directly
|
||||
// purchased of the coinbase.
|
||||
gasPool *big.Int
|
||||
|
||||
// Mark for deletion
|
||||
// When an object is marked for deletion it will be delete from the trie
|
||||
// during the "update" phase of the state transition
|
||||
@ -89,10 +84,9 @@ type StateObject struct {
|
||||
}
|
||||
|
||||
func NewStateObject(address common.Address, db ethdb.Database) *StateObject {
|
||||
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
|
||||
object := &StateObject{db: db, address: address, balance: new(big.Int), dirty: true}
|
||||
object.trie, _ = trie.NewSecure(common.Hash{}, db)
|
||||
object.storage = make(Storage)
|
||||
object.gasPool = new(big.Int)
|
||||
return object
|
||||
}
|
||||
|
||||
@ -121,7 +115,6 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db ethdb.Datab
|
||||
object.codeHash = extobject.CodeHash
|
||||
object.trie = trie
|
||||
object.storage = make(map[string]common.Hash)
|
||||
object.gasPool = new(big.Int)
|
||||
object.code, _ = db.Get(extobject.CodeHash)
|
||||
return object
|
||||
}
|
||||
@ -209,36 +202,9 @@ func (c *StateObject) St() Storage {
|
||||
return c.storage
|
||||
}
|
||||
|
||||
//
|
||||
// Gas setters and getters
|
||||
//
|
||||
|
||||
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
||||
func (c *StateObject) ReturnGas(gas, price *big.Int) {}
|
||||
|
||||
func (self *StateObject) SetGasLimit(gasLimit *big.Int) {
|
||||
self.gasPool = new(big.Int).Set(gasLimit)
|
||||
self.dirty = true
|
||||
|
||||
if glog.V(logger.Core) {
|
||||
glog.Infof("%x: gas (+ %v)", self.Address(), self.gasPool)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateObject) SubGas(gas, price *big.Int) error {
|
||||
if self.gasPool.Cmp(gas) < 0 {
|
||||
return GasLimitError(self.gasPool, gas)
|
||||
}
|
||||
self.gasPool.Sub(self.gasPool, gas)
|
||||
self.dirty = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *StateObject) AddGas(gas, price *big.Int) {
|
||||
self.gasPool.Add(self.gasPool, gas)
|
||||
self.dirty = true
|
||||
}
|
||||
|
||||
func (self *StateObject) Copy() *StateObject {
|
||||
stateObject := NewStateObject(self.Address(), self.db)
|
||||
stateObject.balance.Set(self.balance)
|
||||
@ -248,7 +214,6 @@ func (self *StateObject) Copy() *StateObject {
|
||||
stateObject.code = common.CopyBytes(self.code)
|
||||
stateObject.initCode = common.CopyBytes(self.initCode)
|
||||
stateObject.storage = self.storage.Copy()
|
||||
stateObject.gasPool.Set(self.gasPool)
|
||||
stateObject.remove = self.remove
|
||||
stateObject.dirty = self.dirty
|
||||
stateObject.deleted = self.deleted
|
||||
|
@ -138,7 +138,6 @@ func TestSnapshot2(t *testing.T) {
|
||||
so0 := state.GetStateObject(stateobjaddr0)
|
||||
so0.balance = big.NewInt(42)
|
||||
so0.nonce = 43
|
||||
so0.gasPool = big.NewInt(44)
|
||||
so0.code = []byte{'c', 'a', 'f', 'e'}
|
||||
so0.codeHash = so0.CodeHash()
|
||||
so0.remove = true
|
||||
@ -150,7 +149,6 @@ func TestSnapshot2(t *testing.T) {
|
||||
so1 := state.GetStateObject(stateobjaddr1)
|
||||
so1.balance = big.NewInt(52)
|
||||
so1.nonce = 53
|
||||
so1.gasPool = big.NewInt(54)
|
||||
so1.code = []byte{'c', 'a', 'f', 'e', '2'}
|
||||
so1.codeHash = so1.CodeHash()
|
||||
so1.remove = true
|
||||
@ -207,9 +205,6 @@ func compareStateObjects(so0, so1 *StateObject, t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
if so0.gasPool.Cmp(so1.gasPool) != 0 {
|
||||
t.Fatalf("GasPool mismatch: have %v, want %v", so0.gasPool, so1.gasPool)
|
||||
}
|
||||
if so0.remove != so1.remove {
|
||||
t.Fatalf("Remove mismatch: have %v, want %v", so0.remove, so1.remove)
|
||||
}
|
||||
|
Reference in New Issue
Block a user