aboutsummaryrefslogtreecommitdiffstats
path: root/state
diff options
context:
space:
mode:
Diffstat (limited to 'state')
-rw-r--r--state/dump.go2
-rw-r--r--state/state_object.go25
-rw-r--r--state/state_test.go6
-rw-r--r--state/statedb.go19
4 files changed, 23 insertions, 29 deletions
diff --git a/state/dump.go b/state/dump.go
index 40ecff50c..ac646480c 100644
--- a/state/dump.go
+++ b/state/dump.go
@@ -28,7 +28,7 @@ func (self *StateDB) Dump() []byte {
it := self.trie.Iterator()
for it.Next() {
- stateObject := NewStateObjectFromBytes(it.Key, it.Value)
+ stateObject := NewStateObjectFromBytes(it.Key, it.Value, self.db)
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.Nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
account.Storage = make(map[string]string)
diff --git a/state/state_object.go b/state/state_object.go
index 420ad9757..c1c78bee0 100644
--- a/state/state_object.go
+++ b/state/state_object.go
@@ -28,6 +28,7 @@ func (self Storage) Copy() Storage {
}
type StateObject struct {
+ db ethutil.Database
// Address of the object
address []byte
// Shared attributes
@@ -57,28 +58,20 @@ func (self *StateObject) Reset() {
self.State.Reset()
}
-func NewStateObject(addr []byte) *StateObject {
+func NewStateObject(addr []byte, db ethutil.Database) *StateObject {
// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
address := ethutil.Address(addr)
- object := &StateObject{address: address, balance: new(big.Int), gasPool: new(big.Int)}
- object.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, ""))
+ object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int)}
+ object.State = New(nil, db) //New(trie.New(ethutil.Config.Db, ""))
object.storage = make(Storage)
object.gasPool = new(big.Int)
return object
}
-func NewContract(address []byte, balance *big.Int, root []byte) *StateObject {
- contract := NewStateObject(address)
- contract.balance = balance
- contract.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, string(root)))
-
- return contract
-}
-
-func NewStateObjectFromBytes(address, data []byte) *StateObject {
- object := &StateObject{address: address}
+func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateObject {
+ object := &StateObject{address: address, db: db}
object.RlpDecode(data)
return object
@@ -242,7 +235,7 @@ func (self *StateObject) RefundGas(gas, price *big.Int) {
}
func (self *StateObject) Copy() *StateObject {
- stateObject := NewStateObject(self.Address())
+ stateObject := NewStateObject(self.Address(), self.db)
stateObject.balance.Set(self.balance)
stateObject.codeHash = ethutil.CopyBytes(self.codeHash)
stateObject.Nonce = self.Nonce
@@ -310,13 +303,13 @@ func (c *StateObject) RlpDecode(data []byte) {
c.Nonce = decoder.Get(0).Uint()
c.balance = decoder.Get(1).BigInt()
- c.State = New(ptrie.New(decoder.Get(2).Bytes(), ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface()))
+ c.State = New(decoder.Get(2).Bytes(), c.db) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface()))
c.storage = make(map[string]*ethutil.Value)
c.gasPool = new(big.Int)
c.codeHash = decoder.Get(3).Bytes()
- c.Code, _ = ethutil.Config.Db.Get(c.codeHash)
+ c.Code, _ = c.db.Get(c.codeHash)
}
// Storage change object. Used by the manifest for notifying changes to
diff --git a/state/state_test.go b/state/state_test.go
index 1f76eff0f..7c54cedc0 100644
--- a/state/state_test.go
+++ b/state/state_test.go
@@ -5,7 +5,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
- "github.com/ethereum/go-ethereum/ptrie"
)
type StateSuite struct {
@@ -25,10 +24,9 @@ func (s *StateSuite) TestDump(c *checker.C) {
}
func (s *StateSuite) SetUpTest(c *checker.C) {
- db, _ := ethdb.NewMemDatabase()
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
- ethutil.Config.Db = db
- s.state = New(ptrie.New(nil, db))
+ db, _ := ethdb.NewMemDatabase()
+ s.state = New(nil, db)
}
func (s *StateSuite) TestSnapshot(c *checker.C) {
diff --git a/state/statedb.go b/state/statedb.go
index 6a11fc328..de7314790 100644
--- a/state/statedb.go
+++ b/state/statedb.go
@@ -17,7 +17,7 @@ var statelogger = logger.NewLogger("STATE")
// * Contracts
// * Accounts
type StateDB struct {
- //Trie *trie.Trie
+ db ethutil.Database
trie *ptrie.Trie
stateObjects map[string]*StateObject
@@ -30,8 +30,10 @@ type StateDB struct {
}
// Create a new state from a given trie
-func New(trie *ptrie.Trie) *StateDB {
- return &StateDB{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
+//func New(trie *ptrie.Trie) *StateDB {
+func New(root []byte, db ethutil.Database) *StateDB {
+ trie := ptrie.New(root, db)
+ return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
}
func (self *StateDB) EmptyLogs() {
@@ -138,7 +140,7 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
addr := stateObject.Address()
if len(stateObject.CodeHash()) > 0 {
- ethutil.Config.Db.Put(stateObject.CodeHash(), stateObject.Code)
+ self.db.Put(stateObject.CodeHash(), stateObject.Code)
}
self.trie.Update(addr, stateObject.RlpEncode())
@@ -165,7 +167,7 @@ func (self *StateDB) GetStateObject(addr []byte) *StateObject {
return nil
}
- stateObject = NewStateObjectFromBytes(addr, []byte(data))
+ stateObject = NewStateObjectFromBytes(addr, []byte(data), self.db)
self.SetStateObject(stateObject)
return stateObject
@@ -191,7 +193,7 @@ func (self *StateDB) NewStateObject(addr []byte) *StateObject {
statelogger.Debugf("(+) %x\n", addr)
- stateObject := NewStateObject(addr)
+ stateObject := NewStateObject(addr, self.db)
self.stateObjects[string(addr)] = stateObject
return stateObject
@@ -212,7 +214,8 @@ func (s *StateDB) Cmp(other *StateDB) bool {
func (self *StateDB) Copy() *StateDB {
if self.trie != nil {
- state := New(self.trie.Copy())
+ state := New(nil, self.db)
+ state.trie = self.trie.Copy()
for k, stateObject := range self.stateObjects {
state.stateObjects[k] = stateObject.Copy()
}
@@ -305,7 +308,7 @@ func (self *StateDB) Update(gasUsed *big.Int) {
// FIXME trie delete is broken
if deleted {
- valid, t2 := ptrie.ParanoiaCheck(self.trie, ethutil.Config.Db)
+ valid, t2 := ptrie.ParanoiaCheck(self.trie, self.db)
if !valid {
statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.trie.Root(), t2.Root())