aboutsummaryrefslogtreecommitdiffstats
path: root/state
diff options
context:
space:
mode:
Diffstat (limited to 'state')
-rw-r--r--state/dump.go2
-rw-r--r--state/manifest.go61
-rw-r--r--state/state_object.go58
-rw-r--r--state/state_test.go6
-rw-r--r--state/statedb.go82
5 files changed, 44 insertions, 165 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/manifest.go b/state/manifest.go
deleted file mode 100644
index 994019a08..000000000
--- a/state/manifest.go
+++ /dev/null
@@ -1,61 +0,0 @@
-package state
-
-import (
- "fmt"
- "math/big"
-)
-
-// Object manifest
-//
-// The object manifest is used to keep changes to the state so we can keep track of the changes
-// that occurred during a state transitioning phase.
-type Manifest struct {
- Messages Messages
-}
-
-func NewManifest() *Manifest {
- m := &Manifest{}
- m.Reset()
-
- return m
-}
-
-func (m *Manifest) Reset() {
- m.Messages = nil
-}
-
-func (self *Manifest) AddMessage(msg *Message) *Message {
- self.Messages = append(self.Messages, msg)
-
- return msg
-}
-
-func (self *Manifest) SetHash(hash []byte) {
- for _, message := range self.Messages {
- message.Block = hash
- }
-}
-
-type Messages []*Message
-type Message struct {
- To, From []byte
- Input []byte
- Output []byte
- Path int
- Origin []byte
- Timestamp int64
- Coinbase []byte
- Block []byte
- Number *big.Int
- Value *big.Int
-
- ChangedAddresses [][]byte
-}
-
-func (self *Message) AddStorageChange(addr []byte) {
- self.ChangedAddresses = append(self.ChangedAddresses, addr)
-}
-
-func (self *Message) String() string {
- return fmt.Sprintf("Message{to: %x from: %x input: %x output: %x origin: %x coinbase: %x block: %x number: %v timestamp: %d path: %d value: %v", self.To, self.From, self.Input, self.Output, self.Origin, self.Coinbase, self.Block, self.Number, self.Timestamp, self.Path, self.Value)
-}
diff --git a/state/state_object.go b/state/state_object.go
index 420ad9757..340939a5d 100644
--- a/state/state_object.go
+++ b/state/state_object.go
@@ -6,7 +6,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
- "github.com/ethereum/go-ethereum/ptrie"
+ "github.com/ethereum/go-ethereum/trie"
)
type Code []byte
@@ -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
@@ -128,26 +121,6 @@ func (self *StateObject) SetState(k []byte, value *ethutil.Value) {
self.storage[string(key)] = value.Copy()
}
-/*
-// Iterate over each storage address and yield callback
-func (self *StateObject) EachStorage(cb trie.EachCallback) {
- // First loop over the uncommit/cached values in storage
- for key, value := range self.storage {
- // XXX Most iterators Fns as it stands require encoded values
- encoded := ethutil.NewValue(value.Encode())
- cb(key, encoded)
- }
-
- it := self.State.Trie.NewIterator()
- it.Each(func(key string, value *ethutil.Value) {
- // If it's cached don't call the callback.
- if self.storage[key] == nil {
- cb(key, value)
- }
- })
-}
-*/
-
func (self *StateObject) Sync() {
for key, value := range self.storage {
if value.Len() == 0 {
@@ -157,15 +130,6 @@ func (self *StateObject) Sync() {
self.setAddr([]byte(key), value)
}
-
- /*
- valid, t2 := ptrie.ParanoiaCheck(self.State.trie, ethutil.Config.Db)
- if !valid {
- statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Root(), t2.Root())
-
- self.State.trie = t2
- }
- */
}
func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
@@ -242,7 +206,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
@@ -280,7 +244,7 @@ func (c *StateObject) Init() Code {
return c.InitCode
}
-func (self *StateObject) Trie() *ptrie.Trie {
+func (self *StateObject) Trie() *trie.Trie {
return self.State.trie
}
@@ -310,13 +274,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..c83d59ed7 100644
--- a/state/statedb.go
+++ b/state/statedb.go
@@ -6,7 +6,7 @@ import (
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/ptrie"
+ "github.com/ethereum/go-ethereum/trie"
)
var statelogger = logger.NewLogger("STATE")
@@ -17,21 +17,20 @@ var statelogger = logger.NewLogger("STATE")
// * Contracts
// * Accounts
type StateDB struct {
- //Trie *trie.Trie
- trie *ptrie.Trie
+ db ethutil.Database
+ trie *trie.Trie
stateObjects map[string]*StateObject
- manifest *Manifest
-
refund map[string]*big.Int
logs Logs
}
// 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(root []byte, db ethutil.Database) *StateDB {
+ trie := trie.New(ethutil.CopyBytes(root), db)
+ return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int)}
}
func (self *StateDB) EmptyLogs() {
@@ -46,6 +45,13 @@ func (self *StateDB) Logs() Logs {
return self.logs
}
+func (self *StateDB) Refund(addr []byte, gas *big.Int) {
+ if self.refund[string(addr)] == nil {
+ self.refund[string(addr)] = new(big.Int)
+ }
+ self.refund[string(addr)].Add(self.refund[string(addr)], gas)
+}
+
// Retrieve the balance from the given address or 0 if object not found
func (self *StateDB) GetBalance(addr []byte) *big.Int {
stateObject := self.GetStateObject(addr)
@@ -56,13 +62,6 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
return ethutil.Big0
}
-func (self *StateDB) Refund(addr []byte, gas *big.Int) {
- if self.refund[string(addr)] == nil {
- self.refund[string(addr)] = new(big.Int)
- }
- self.refund[string(addr)].Add(self.refund[string(addr)], gas)
-}
-
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
stateObject := self.GetStateObject(addr)
if stateObject != nil {
@@ -102,6 +101,7 @@ func (self *StateDB) SetCode(addr, code []byte) {
}
}
+// TODO vars
func (self *StateDB) GetState(a, b []byte) []byte {
stateObject := self.GetStateObject(a)
if stateObject != nil {
@@ -138,7 +138,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 +165,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 +191,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
@@ -211,33 +211,27 @@ func (s *StateDB) Cmp(other *StateDB) bool {
}
func (self *StateDB) Copy() *StateDB {
- if self.trie != nil {
- state := New(self.trie.Copy())
- for k, stateObject := range self.stateObjects {
- state.stateObjects[k] = stateObject.Copy()
- }
-
- for addr, refund := range self.refund {
- state.refund[addr] = new(big.Int).Set(refund)
- }
-
- logs := make(Logs, len(self.logs))
- copy(logs, self.logs)
- state.logs = logs
+ state := New(nil, self.db)
+ state.trie = self.trie.Copy()
+ for k, stateObject := range self.stateObjects {
+ state.stateObjects[k] = stateObject.Copy()
+ }
- return state
+ for addr, refund := range self.refund {
+ state.refund[addr] = new(big.Int).Set(refund)
}
- return nil
+ logs := make(Logs, len(self.logs))
+ copy(logs, self.logs)
+ state.logs = logs
+
+ return state
}
func (self *StateDB) Set(state *StateDB) {
- if state == nil {
- panic("Tried setting 'state' to nil through 'Set'")
- }
-
self.trie = state.trie
self.stateObjects = state.stateObjects
+
self.refund = state.refund
self.logs = state.logs
}
@@ -288,34 +282,18 @@ func (self *StateDB) Refunds() map[string]*big.Int {
}
func (self *StateDB) Update(gasUsed *big.Int) {
- var deleted bool
self.refund = make(map[string]*big.Int)
for _, stateObject := range self.stateObjects {
if stateObject.remove {
self.DeleteStateObject(stateObject)
- deleted = true
} else {
stateObject.Sync()
self.UpdateStateObject(stateObject)
}
}
-
- // FIXME trie delete is broken
- if deleted {
- valid, t2 := ptrie.ParanoiaCheck(self.trie, ethutil.Config.Db)
- if !valid {
- statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.trie.Root(), t2.Root())
-
- self.trie = t2
- }
- }
-}
-
-func (self *StateDB) Manifest() *Manifest {
- return self.manifest
}
// Debug stuff