From 780abaec988df302e0c98f1a35e9af35b5623746 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 18:35:36 +0100 Subject: Switched to new trie --- state/dump.go | 26 ++-- state/state.go | 325 ------------------------------------------------- state/state_object.go | 42 ++++--- state/statedb.go | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 365 insertions(+), 354 deletions(-) delete mode 100644 state/state.go create mode 100644 state/statedb.go (limited to 'state') diff --git a/state/dump.go b/state/dump.go index c1f5ecf3a..40ecff50c 100644 --- a/state/dump.go +++ b/state/dump.go @@ -22,22 +22,23 @@ type World struct { func (self *StateDB) Dump() []byte { world := World{ - Root: ethutil.Bytes2Hex(self.Trie.GetRoot()), + Root: ethutil.Bytes2Hex(self.trie.Root()), Accounts: make(map[string]Account), } - self.Trie.NewIterator().Each(func(key string, value *ethutil.Value) { - stateObject := NewStateObjectFromBytes([]byte(key), value.Bytes()) + it := self.trie.Iterator() + for it.Next() { + stateObject := NewStateObjectFromBytes(it.Key, it.Value) 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) - stateObject.EachStorage(func(key string, value *ethutil.Value) { - value.Decode() - account.Storage[ethutil.Bytes2Hex([]byte(key))] = ethutil.Bytes2Hex(value.Bytes()) - }) - world.Accounts[ethutil.Bytes2Hex([]byte(key))] = account - }) + storageIt := stateObject.State.trie.Iterator() + for storageIt.Next() { + account.Storage[ethutil.Bytes2Hex(it.Key)] = ethutil.Bytes2Hex(it.Value) + } + world.Accounts[ethutil.Bytes2Hex(it.Key)] = account + } json, err := json.MarshalIndent(world, "", " ") if err != nil { @@ -50,7 +51,8 @@ func (self *StateDB) Dump() []byte { // Debug stuff func (self *StateObject) CreateOutputForDiff() { fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce) - self.EachStorage(func(addr string, value *ethutil.Value) { - fmt.Printf("%x %x\n", addr, value.Bytes()) - }) + it := self.State.trie.Iterator() + for it.Next() { + fmt.Printf("%x %x\n", it.Key, it.Value) + } } diff --git a/state/state.go b/state/state.go deleted file mode 100644 index f77da72f0..000000000 --- a/state/state.go +++ /dev/null @@ -1,325 +0,0 @@ -package state - -import ( - "math/big" - - "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/trie" -) - -var statelogger = logger.NewLogger("STATE") - -// StateDBs within the ethereum protocol are used to store anything -// within the merkle trie. StateDBs take care of caching and storing -// nested states. It's the general query interface to retrieve: -// * Contracts -// * Accounts -type StateDB struct { - // The trie for this structure - 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 *trie.Trie) *StateDB { - return &StateDB{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} -} - -func (self *StateDB) EmptyLogs() { - self.logs = nil -} - -func (self *StateDB) AddLog(log Log) { - self.logs = append(self.logs, log) -} - -func (self *StateDB) Logs() Logs { - return self.logs -} - -// 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) - if stateObject != nil { - return stateObject.balance - } - - 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 { - stateObject.AddBalance(amount) - } -} - -func (self *StateDB) GetNonce(addr []byte) uint64 { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - return stateObject.Nonce - } - - return 0 -} - -func (self *StateDB) SetNonce(addr []byte, nonce uint64) { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - stateObject.Nonce = nonce - } -} - -func (self *StateDB) GetCode(addr []byte) []byte { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - return stateObject.Code - } - - return nil -} - -func (self *StateDB) SetCode(addr, code []byte) { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - stateObject.SetCode(code) - } -} - -func (self *StateDB) GetState(a, b []byte) []byte { - stateObject := self.GetStateObject(a) - if stateObject != nil { - return stateObject.GetState(b).Bytes() - } - - return nil -} - -func (self *StateDB) SetState(addr, key []byte, value interface{}) { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - stateObject.SetState(key, ethutil.NewValue(value)) - } -} - -func (self *StateDB) Delete(addr []byte) bool { - stateObject := self.GetStateObject(addr) - if stateObject != nil { - stateObject.MarkForDeletion() - - return true - } - - return false -} - -// -// Setting, updating & deleting state object methods -// - -// Update the given state object and apply it to state trie -func (self *StateDB) UpdateStateObject(stateObject *StateObject) { - addr := stateObject.Address() - - if len(stateObject.CodeHash()) > 0 { - ethutil.Config.Db.Put(stateObject.CodeHash(), stateObject.Code) - } - - self.Trie.Update(string(addr), string(stateObject.RlpEncode())) -} - -// Delete the given state object and delete it from the state trie -func (self *StateDB) DeleteStateObject(stateObject *StateObject) { - self.Trie.Delete(string(stateObject.Address())) - - delete(self.stateObjects, string(stateObject.Address())) -} - -// Retrieve a state object given my the address. Nil if not found -func (self *StateDB) GetStateObject(addr []byte) *StateObject { - addr = ethutil.Address(addr) - - stateObject := self.stateObjects[string(addr)] - if stateObject != nil { - return stateObject - } - - data := self.Trie.Get(string(addr)) - if len(data) == 0 { - return nil - } - - stateObject = NewStateObjectFromBytes(addr, []byte(data)) - self.SetStateObject(stateObject) - - return stateObject -} - -func (self *StateDB) SetStateObject(object *StateObject) { - self.stateObjects[string(object.address)] = object -} - -// Retrieve a state object or create a new state object if nil -func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject { - stateObject := self.GetStateObject(addr) - if stateObject == nil { - stateObject = self.NewStateObject(addr) - } - - return stateObject -} - -// Create a state object whether it exist in the trie or not -func (self *StateDB) NewStateObject(addr []byte) *StateObject { - addr = ethutil.Address(addr) - - statelogger.Debugf("(+) %x\n", addr) - - stateObject := NewStateObject(addr) - self.stateObjects[string(addr)] = stateObject - - return stateObject -} - -// Deprecated -func (self *StateDB) GetAccount(addr []byte) *StateObject { - return self.GetOrNewStateObject(addr) -} - -// -// Setting, copying of the state methods -// - -func (s *StateDB) Cmp(other *StateDB) bool { - return s.Trie.Cmp(other.Trie) -} - -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 - - return state - } - - return nil -} - -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 -} - -func (s *StateDB) Root() []byte { - return s.Trie.GetRoot() -} - -// Resets the trie and all siblings -func (s *StateDB) Reset() { - s.Trie.Undo() - - // Reset all nested states - for _, stateObject := range s.stateObjects { - if stateObject.State == nil { - continue - } - - stateObject.Reset() - } - - s.Empty() -} - -// Syncs the trie and all siblings -func (s *StateDB) Sync() { - // Sync all nested states - for _, stateObject := range s.stateObjects { - if stateObject.State == nil { - continue - } - - stateObject.State.Sync() - } - - s.Trie.Sync() - - s.Empty() -} - -func (self *StateDB) Empty() { - self.stateObjects = make(map[string]*StateObject) - self.refund = make(map[string]*big.Int) -} - -func (self *StateDB) Refunds() map[string]*big.Int { - return self.refund -} - -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 := trie.ParanoiaCheck(self.Trie) - if !valid { - statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.Trie.GetRoot(), t2.GetRoot()) - - self.Trie = t2 - } - } -} - -func (self *StateDB) Manifest() *Manifest { - return self.manifest -} - -// Debug stuff -func (self *StateDB) CreateOutputForDiff() { - for _, stateObject := range self.stateObjects { - stateObject.CreateOutputForDiff() - } -} diff --git a/state/state_object.go b/state/state_object.go index b8af4e702..420ad9757 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/trie" + "github.com/ethereum/go-ethereum/ptrie" ) type Code []byte @@ -62,7 +62,7 @@ func NewStateObject(addr []byte) *StateObject { address := ethutil.Address(addr) object := &StateObject{address: address, balance: new(big.Int), gasPool: new(big.Int)} - object.State = New(trie.New(ethutil.Config.Db, "")) + object.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -72,7 +72,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.balance = balance - contract.State = New(trie.New(ethutil.Config.Db, string(root))) + contract.State = New(ptrie.New(nil, ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, string(root))) return contract } @@ -89,12 +89,12 @@ func (self *StateObject) MarkForDeletion() { statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance) } -func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { - return ethutil.NewValueFromBytes([]byte(c.State.Trie.Get(string(addr)))) +func (c *StateObject) getAddr(addr []byte) *ethutil.Value { + return ethutil.NewValueFromBytes([]byte(c.State.trie.Get(addr))) } -func (c *StateObject) SetAddr(addr []byte, value interface{}) { - c.State.Trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) +func (c *StateObject) setAddr(addr []byte, value interface{}) { + c.State.trie.Update(addr, ethutil.Encode(value)) } func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { @@ -113,7 +113,7 @@ func (self *StateObject) GetState(k []byte) *ethutil.Value { value := self.storage[string(key)] if value == nil { - value = self.GetAddr(key) + value = self.getAddr(key) if !value.IsNil() { self.storage[string(key)] = value @@ -128,6 +128,7 @@ 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 @@ -145,23 +146,26 @@ func (self *StateObject) EachStorage(cb trie.EachCallback) { } }) } +*/ func (self *StateObject) Sync() { for key, value := range self.storage { if value.Len() == 0 { - self.State.Trie.Delete(string(key)) + self.State.trie.Delete([]byte(key)) continue } - self.SetAddr([]byte(key), value) + self.setAddr([]byte(key), value) } - valid, t2 := trie.ParanoiaCheck(self.State.Trie) - if !valid { - statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Root(), t2.GetRoot()) + /* + 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 - } + self.State.trie = t2 + } + */ } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { @@ -276,8 +280,12 @@ func (c *StateObject) Init() Code { return c.InitCode } +func (self *StateObject) Trie() *ptrie.Trie { + return self.State.trie +} + func (self *StateObject) Root() []byte { - return self.State.Trie.GetRoot() + return self.Trie().Root() } func (self *StateObject) SetCode(code []byte) { @@ -302,7 +310,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.balance = decoder.Get(1).BigInt() - c.State = New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = New(ptrie.New(decoder.Get(2).Bytes(), ethutil.Config.Db)) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) diff --git a/state/statedb.go b/state/statedb.go new file mode 100644 index 000000000..6a11fc328 --- /dev/null +++ b/state/statedb.go @@ -0,0 +1,326 @@ +package state + +import ( + "bytes" + "math/big" + + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/ptrie" +) + +var statelogger = logger.NewLogger("STATE") + +// StateDBs within the ethereum protocol are used to store anything +// within the merkle trie. StateDBs take care of caching and storing +// nested states. It's the general query interface to retrieve: +// * Contracts +// * Accounts +type StateDB struct { + //Trie *trie.Trie + trie *ptrie.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 (self *StateDB) EmptyLogs() { + self.logs = nil +} + +func (self *StateDB) AddLog(log Log) { + self.logs = append(self.logs, log) +} + +func (self *StateDB) Logs() Logs { + return self.logs +} + +// 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) + if stateObject != nil { + return stateObject.balance + } + + 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 { + stateObject.AddBalance(amount) + } +} + +func (self *StateDB) GetNonce(addr []byte) uint64 { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + return stateObject.Nonce + } + + return 0 +} + +func (self *StateDB) SetNonce(addr []byte, nonce uint64) { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + stateObject.Nonce = nonce + } +} + +func (self *StateDB) GetCode(addr []byte) []byte { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + return stateObject.Code + } + + return nil +} + +func (self *StateDB) SetCode(addr, code []byte) { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + stateObject.SetCode(code) + } +} + +func (self *StateDB) GetState(a, b []byte) []byte { + stateObject := self.GetStateObject(a) + if stateObject != nil { + return stateObject.GetState(b).Bytes() + } + + return nil +} + +func (self *StateDB) SetState(addr, key []byte, value interface{}) { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + stateObject.SetState(key, ethutil.NewValue(value)) + } +} + +func (self *StateDB) Delete(addr []byte) bool { + stateObject := self.GetStateObject(addr) + if stateObject != nil { + stateObject.MarkForDeletion() + + return true + } + + return false +} + +// +// Setting, updating & deleting state object methods +// + +// Update the given state object and apply it to state trie +func (self *StateDB) UpdateStateObject(stateObject *StateObject) { + addr := stateObject.Address() + + if len(stateObject.CodeHash()) > 0 { + ethutil.Config.Db.Put(stateObject.CodeHash(), stateObject.Code) + } + + self.trie.Update(addr, stateObject.RlpEncode()) +} + +// Delete the given state object and delete it from the state trie +func (self *StateDB) DeleteStateObject(stateObject *StateObject) { + self.trie.Delete(stateObject.Address()) + + delete(self.stateObjects, string(stateObject.Address())) +} + +// Retrieve a state object given my the address. Nil if not found +func (self *StateDB) GetStateObject(addr []byte) *StateObject { + addr = ethutil.Address(addr) + + stateObject := self.stateObjects[string(addr)] + if stateObject != nil { + return stateObject + } + + data := self.trie.Get(addr) + if len(data) == 0 { + return nil + } + + stateObject = NewStateObjectFromBytes(addr, []byte(data)) + self.SetStateObject(stateObject) + + return stateObject +} + +func (self *StateDB) SetStateObject(object *StateObject) { + self.stateObjects[string(object.address)] = object +} + +// Retrieve a state object or create a new state object if nil +func (self *StateDB) GetOrNewStateObject(addr []byte) *StateObject { + stateObject := self.GetStateObject(addr) + if stateObject == nil { + stateObject = self.NewStateObject(addr) + } + + return stateObject +} + +// Create a state object whether it exist in the trie or not +func (self *StateDB) NewStateObject(addr []byte) *StateObject { + addr = ethutil.Address(addr) + + statelogger.Debugf("(+) %x\n", addr) + + stateObject := NewStateObject(addr) + self.stateObjects[string(addr)] = stateObject + + return stateObject +} + +// Deprecated +func (self *StateDB) GetAccount(addr []byte) *StateObject { + return self.GetOrNewStateObject(addr) +} + +// +// Setting, copying of the state methods +// + +func (s *StateDB) Cmp(other *StateDB) bool { + return bytes.Equal(s.trie.Root(), other.trie.Root()) +} + +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 + + return state + } + + return nil +} + +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 +} + +func (s *StateDB) Root() []byte { + return s.trie.Root() +} + +// Resets the trie and all siblings +func (s *StateDB) Reset() { + s.trie.Reset() + + // Reset all nested states + for _, stateObject := range s.stateObjects { + if stateObject.State == nil { + continue + } + + stateObject.Reset() + } + + s.Empty() +} + +// Syncs the trie and all siblings +func (s *StateDB) Sync() { + // Sync all nested states + for _, stateObject := range s.stateObjects { + if stateObject.State == nil { + continue + } + + stateObject.State.Sync() + } + + s.trie.Commit() + + s.Empty() +} + +func (self *StateDB) Empty() { + self.stateObjects = make(map[string]*StateObject) + self.refund = make(map[string]*big.Int) +} + +func (self *StateDB) Refunds() map[string]*big.Int { + return self.refund +} + +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 +func (self *StateDB) CreateOutputForDiff() { + for _, stateObject := range self.stateObjects { + stateObject.CreateOutputForDiff() + } +} -- cgit v1.2.3 From e2d1d832efe0623539c9d37ca8aee17d44e47067 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 2 Jan 2015 13:00:25 +0100 Subject: added nil check --- state/state_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'state') diff --git a/state/state_test.go b/state/state_test.go index 28e4fc5da..1f76eff0f 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -5,7 +5,7 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/ptrie" ) type StateSuite struct { @@ -18,9 +18,8 @@ var _ = checker.Suite(&StateSuite{}) func (s *StateSuite) TestDump(c *checker.C) { key := []byte{0x01} - value := "foo" - node := []interface{}{key, value} - s.state.Trie.Put(node) + value := []byte("foo") + s.state.trie.Update(key, value) dump := s.state.Dump() c.Assert(dump, checker.NotNil) } @@ -29,7 +28,7 @@ func (s *StateSuite) SetUpTest(c *checker.C) { db, _ := ethdb.NewMemDatabase() ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "") ethutil.Config.Db = db - s.state = New(trie.New(db, "")) + s.state = New(ptrie.New(nil, db)) } func (s *StateSuite) TestSnapshot(c *checker.C) { -- cgit v1.2.3 From ca1b2a1a91401255ab4e26cec7eb575b99ecb8da Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 3 Jan 2015 17:18:43 +0100 Subject: Changed prev_hash to block_hash, state transition now uses vm env * PREVHASH => BLOCKHASH( N ) * State transition object uses VMEnv as it's query interface * Updated vm.Enviroment has GetHash( n ) for BLOCKHASH instruction * Added GetHash to xeth, core, utils & test environments --- state/manifest.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'state') diff --git a/state/manifest.go b/state/manifest.go index 21cd04a1a..994019a08 100644 --- a/state/manifest.go +++ b/state/manifest.go @@ -30,6 +30,12 @@ func (self *Manifest) AddMessage(msg *Message) *Message { 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 -- cgit v1.2.3 From fed3e6a808921fb8274b50043c5c39a24a1bbccf Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 7 Jan 2015 13:17:48 +0100 Subject: Refactored ethutil.Config.Db out --- state/dump.go | 2 +- state/state_object.go | 25 +++++++++---------------- state/state_test.go | 6 ++---- state/statedb.go | 19 +++++++++++-------- 4 files changed, 23 insertions(+), 29 deletions(-) (limited to 'state') 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()) -- cgit v1.2.3 From db4aaedcbdb409e17ea3de161e7b24a80ba0a58c Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 8 Jan 2015 11:47:04 +0100 Subject: Moved ptrie => trie. Removed old trie --- state/state_object.go | 6 +++--- state/statedb.go | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index c1c78bee0..913c57a31 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 @@ -152,7 +152,7 @@ func (self *StateObject) Sync() { } /* - valid, t2 := ptrie.ParanoiaCheck(self.State.trie, ethutil.Config.Db) + valid, t2 := trie.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()) @@ -273,7 +273,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 } diff --git a/state/statedb.go b/state/statedb.go index de7314790..3176ab755 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") @@ -18,7 +18,7 @@ var statelogger = logger.NewLogger("STATE") // * Accounts type StateDB struct { db ethutil.Database - trie *ptrie.Trie + trie *trie.Trie stateObjects map[string]*StateObject @@ -30,9 +30,8 @@ type StateDB struct { } // Create a new state from a given trie -//func New(trie *ptrie.Trie) *StateDB { func New(root []byte, db ethutil.Database) *StateDB { - trie := ptrie.New(root, db) + trie := trie.New(root, db) return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} } @@ -308,7 +307,7 @@ func (self *StateDB) Update(gasUsed *big.Int) { // FIXME trie delete is broken if deleted { - valid, t2 := ptrie.ParanoiaCheck(self.trie, self.db) + valid, t2 := trie.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()) -- cgit v1.2.3 From 54927dc0e0b99009f92fbb7b28d71ae20179ce1e Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 29 Jan 2015 23:58:43 +0100 Subject: Fixed issue with Storage() * Storage() returned encoded values. They are now decode prior to hexing * Removed old code from state object * Updated coin --- state/state_object.go | 29 ----------------------------- 1 file changed, 29 deletions(-) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index 913c57a31..340939a5d 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -121,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 { @@ -150,15 +130,6 @@ func (self *StateObject) Sync() { self.setAddr([]byte(key), value) } - - /* - valid, t2 := trie.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 { -- cgit v1.2.3 From 79cd58bdf32c8e4e725480f9d6dc58f7325e07a0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 31 Jan 2015 17:23:46 +0100 Subject: removed old code --- state/statedb.go | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'state') diff --git a/state/statedb.go b/state/statedb.go index 3176ab755..1b6e274de 100644 --- a/state/statedb.go +++ b/state/statedb.go @@ -290,30 +290,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 := trie.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()) - - self.trie = t2 - } - } } func (self *StateDB) Manifest() *Manifest { -- cgit v1.2.3 From 623469cb6c6cdb6ff84dc2cb7d4409e9d7cf3f65 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 3 Feb 2015 06:56:19 -0800 Subject: Added missing whisper timestamp. Closes #284 --- state/statedb.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'state') diff --git a/state/statedb.go b/state/statedb.go index 1b6e274de..af054ff09 100644 --- a/state/statedb.go +++ b/state/statedb.go @@ -31,7 +31,7 @@ type StateDB struct { // Create a new state from a given trie func New(root []byte, db ethutil.Database) *StateDB { - trie := trie.New(root, db) + trie := trie.New(ethutil.CopyBytes(root), db) return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} } @@ -234,12 +234,9 @@ func (self *StateDB) Copy() *StateDB { } 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 } -- cgit v1.2.3 From 3f6baa45a7fb1ae5fd7966d2763a2a776a65eb96 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 11 Feb 2015 23:46:45 +0100 Subject: Documented methods & removed old manifest --- state/manifest.go | 61 ------------------------------------------------------- state/statedb.go | 51 +++++++++++++++++++--------------------------- 2 files changed, 21 insertions(+), 91 deletions(-) delete mode 100644 state/manifest.go (limited to 'state') 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/statedb.go b/state/statedb.go index af054ff09..c83d59ed7 100644 --- a/state/statedb.go +++ b/state/statedb.go @@ -22,8 +22,6 @@ type StateDB struct { stateObjects map[string]*StateObject - manifest *Manifest - refund map[string]*big.Int logs Logs @@ -32,7 +30,7 @@ type StateDB struct { // Create a new state from a given trie 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), manifest: NewManifest(), refund: make(map[string]*big.Int)} + return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int)} } func (self *StateDB) EmptyLogs() { @@ -47,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) @@ -57,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 { @@ -103,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 { @@ -212,25 +211,21 @@ func (s *StateDB) Cmp(other *StateDB) bool { } func (self *StateDB) Copy() *StateDB { - if self.trie != nil { - state := New(nil, self.db) - state.trie = 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) { @@ -301,10 +296,6 @@ func (self *StateDB) Update(gasUsed *big.Int) { } } -func (self *StateDB) Manifest() *Manifest { - return self.manifest -} - // Debug stuff func (self *StateDB) CreateOutputForDiff() { for _, stateObject := range self.stateObjects { -- cgit v1.2.3 From 2c454863f2f41fb232778a4cbd4cf135a58c4118 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 17 Feb 2015 13:10:18 +0100 Subject: Use new rlp (TODO requires some refactoring) --- state/state_object.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index 340939a5d..0c157403c 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -1,11 +1,13 @@ package state import ( + "bytes" "fmt" "math/big" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -71,8 +73,28 @@ func NewStateObject(addr []byte, db ethutil.Database) *StateObject { } func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateObject { + // TODO clean me up + var extobject struct { + Nonce uint64 + Balance *big.Int + Root []byte + CodeHash []byte + } + err := rlp.Decode(bytes.NewReader(data), &extobject) + if err != nil { + fmt.Println(err) + return nil + } + object := &StateObject{address: address, db: db} - object.RlpDecode(data) + //object.RlpDecode(data) + object.Nonce = extobject.Nonce + object.balance = extobject.Balance + object.codeHash = extobject.CodeHash + object.State = New(extobject.Root, db) + object.storage = make(map[string]*ethutil.Value) + object.gasPool = new(big.Int) + object.Code, _ = db.Get(extobject.CodeHash) return object } @@ -271,7 +293,6 @@ func (c *StateObject) CodeHash() ethutil.Bytes { func (c *StateObject) RlpDecode(data []byte) { decoder := ethutil.NewValueFromBytes(data) - c.Nonce = decoder.Get(0).Uint() c.balance = decoder.Get(1).BigInt() c.State = New(decoder.Get(2).Bytes(), c.db) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface())) -- cgit v1.2.3 From c14071df9da4ab3f5b372293e87184af9b91c09e Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 19 Feb 2015 13:34:27 +0100 Subject: Reset storage cache after sync --- state/state_object.go | 1 + 1 file changed, 1 insertion(+) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index 0c157403c..eaa91c713 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -152,6 +152,7 @@ func (self *StateObject) Sync() { self.setAddr([]byte(key), value) } + self.storage = make(Storage) } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { -- cgit v1.2.3 From fa4cbad315609e41d88c59ecbce7c6c6169fc57a Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 19 Feb 2015 22:33:22 +0100 Subject: Optimisations and fixed a couple of DDOS issues in the miner --- state/state_object.go | 4 ++++ state/statedb.go | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 20 deletions(-) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index eaa91c713..d50c9fd7a 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -53,6 +53,7 @@ type StateObject struct { // When an object is marked for deletion it will be delete from the trie // during the "update" phase of the state transition remove bool + dirty bool } func (self *StateObject) Reset() { @@ -211,6 +212,8 @@ func (self *StateObject) BuyGas(gas, price *big.Int) error { return GasLimitError(self.gasPool, gas) } + self.gasPool.Sub(self.gasPool, gas) + rGas := new(big.Int).Set(gas) rGas.Mul(rGas, price) @@ -241,6 +244,7 @@ func (self *StateObject) Copy() *StateObject { stateObject.storage = self.storage.Copy() stateObject.gasPool.Set(self.gasPool) stateObject.remove = self.remove + stateObject.dirty = self.dirty return stateObject } diff --git a/state/statedb.go b/state/statedb.go index c83d59ed7..8c8a21db9 100644 --- a/state/statedb.go +++ b/state/statedb.go @@ -78,43 +78,45 @@ func (self *StateDB) GetNonce(addr []byte) uint64 { return 0 } -func (self *StateDB) SetNonce(addr []byte, nonce uint64) { +func (self *StateDB) GetCode(addr []byte) []byte { stateObject := self.GetStateObject(addr) if stateObject != nil { - stateObject.Nonce = nonce + return stateObject.Code } + + return nil } -func (self *StateDB) GetCode(addr []byte) []byte { - stateObject := self.GetStateObject(addr) +func (self *StateDB) GetState(a, b []byte) []byte { + stateObject := self.GetStateObject(a) if stateObject != nil { - return stateObject.Code + return stateObject.GetState(b).Bytes() } return nil } -func (self *StateDB) SetCode(addr, code []byte) { +func (self *StateDB) SetNonce(addr []byte, nonce uint64) { stateObject := self.GetStateObject(addr) if stateObject != nil { - stateObject.SetCode(code) + stateObject.Nonce = nonce + stateObject.dirty = true } } -// TODO vars -func (self *StateDB) GetState(a, b []byte) []byte { - stateObject := self.GetStateObject(a) +func (self *StateDB) SetCode(addr, code []byte) { + stateObject := self.GetStateObject(addr) if stateObject != nil { - return stateObject.GetState(b).Bytes() + stateObject.SetCode(code) + stateObject.dirty = true } - - return nil } func (self *StateDB) SetState(addr, key []byte, value interface{}) { stateObject := self.GetStateObject(addr) if stateObject != nil { stateObject.SetState(key, ethutil.NewValue(value)) + stateObject.dirty = true } } @@ -122,6 +124,7 @@ func (self *StateDB) Delete(addr []byte) bool { stateObject := self.GetStateObject(addr) if stateObject != nil { stateObject.MarkForDeletion() + stateObject.dirty = true return true } @@ -282,16 +285,18 @@ func (self *StateDB) Refunds() map[string]*big.Int { } func (self *StateDB) Update(gasUsed *big.Int) { - self.refund = make(map[string]*big.Int) for _, stateObject := range self.stateObjects { - if stateObject.remove { - self.DeleteStateObject(stateObject) - } else { - stateObject.Sync() - - self.UpdateStateObject(stateObject) + if stateObject.dirty { + if stateObject.remove { + self.DeleteStateObject(stateObject) + } else { + stateObject.Sync() + + self.UpdateStateObject(stateObject) + } + stateObject.dirty = false } } } -- cgit v1.2.3 From 01ce066d4307e6e8cab815eab9295a0b259130b9 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 20 Feb 2015 11:37:33 +0100 Subject: state: improve TestDump --- state/state_test.go | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'state') diff --git a/state/state_test.go b/state/state_test.go index 7c54cedc0..ee1cf9286 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -1,6 +1,8 @@ package state import ( + "math/big" + checker "gopkg.in/check.v1" "github.com/ethereum/go-ethereum/ethdb" @@ -16,11 +18,42 @@ var _ = checker.Suite(&StateSuite{}) // var ZeroHash256 = make([]byte, 32) func (s *StateSuite) TestDump(c *checker.C) { - key := []byte{0x01} - value := []byte("foo") - s.state.trie.Update(key, value) - dump := s.state.Dump() - c.Assert(dump, checker.NotNil) + // generate a few entries + obj1 := s.state.GetOrNewStateObject([]byte{0x01}) + obj1.AddBalance(big.NewInt(22)) + obj2 := s.state.GetOrNewStateObject([]byte{0x01, 0x02}) + obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3}) + obj3 := s.state.GetOrNewStateObject([]byte{0x02}) + obj3.SetBalance(big.NewInt(44)) + + // write some of them to the trie + s.state.UpdateStateObject(obj1) + s.state.UpdateStateObject(obj2) + + // check that dump contains the state objects that are in trie + got := string(s.state.Dump()) + want := `{ + "root": "4e3a59299745ba6752247c8b91d0f716dac9ec235861c91f5ac1894a361d87ba", + "accounts": { + "0000000000000000000000000000000000000001": { + "balance": "22", + "nonce": 0, + "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", + "storage": {} + }, + "0000000000000000000000000000000000000102": { + "balance": "0", + "nonce": 0, + "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3", + "storage": {} + } + } +}` + if got != want { + c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want) + } } func (s *StateSuite) SetUpTest(c *checker.C) { -- cgit v1.2.3 From d8ac267f4128117c3fb9736a40f3dbc327582e32 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 20 Feb 2015 14:01:30 +0100 Subject: dirty tracking for state objects fixed --- state/state_object.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'state') diff --git a/state/state_object.go b/state/state_object.go index d50c9fd7a..226c25299 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -65,7 +65,7 @@ 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{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int)} + object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true} object.State = New(nil, db) //New(trie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -118,6 +118,7 @@ func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { } func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) { self.SetState(key.Bytes(), value) + self.dirty = true } func (self *StateObject) Storage() map[string]*ethutil.Value { @@ -142,6 +143,7 @@ func (self *StateObject) GetState(k []byte) *ethutil.Value { func (self *StateObject) SetState(k []byte, value *ethutil.Value) { key := ethutil.LeftPadBytes(k, 32) self.storage[string(key)] = value.Copy() + self.dirty = true } func (self *StateObject) Sync() { @@ -166,6 +168,7 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { func (c *StateObject) AddBalance(amount *big.Int) { c.SetBalance(new(big.Int).Add(c.balance, amount)) + c.dirty = true statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount) } @@ -180,6 +183,7 @@ func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) } func (c *StateObject) SetBalance(amount *big.Int) { c.balance = amount + c.dirty = true } func (self *StateObject) Balance() *big.Int { return self.balance } @@ -198,6 +202,8 @@ func (c *StateObject) ConvertGas(gas, price *big.Int) error { c.SubAmount(total) + c.dirty = true + return nil } @@ -219,6 +225,8 @@ func (self *StateObject) BuyGas(gas, price *big.Int) error { self.AddAmount(rGas) + self.dirty = true + return nil } -- cgit v1.2.3 From ea9a549bbdc8377bca73f1417f2dc4a18396a382 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 20 Feb 2015 14:19:34 +0100 Subject: Removed exported fields from state object and added proper set/getters --- state/dump.go | 4 +-- state/state_object.go | 67 +++++++++++++++++++++++++++++++++------------------ state/statedb.go | 12 +++------ 3 files changed, 50 insertions(+), 33 deletions(-) (limited to 'state') diff --git a/state/dump.go b/state/dump.go index ac646480c..81895f1a3 100644 --- a/state/dump.go +++ b/state/dump.go @@ -30,7 +30,7 @@ func (self *StateDB) Dump() []byte { for it.Next() { 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 := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)} account.Storage = make(map[string]string) storageIt := stateObject.State.trie.Iterator() @@ -50,7 +50,7 @@ func (self *StateDB) Dump() []byte { // Debug stuff func (self *StateObject) CreateOutputForDiff() { - fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce) + fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.nonce) it := self.State.trie.Iterator() for it.Next() { fmt.Printf("%x %x\n", it.Key, it.Value) diff --git a/state/state_object.go b/state/state_object.go index 226c25299..477b912a1 100644 --- a/state/state_object.go +++ b/state/state_object.go @@ -36,11 +36,11 @@ type StateObject struct { // Shared attributes balance *big.Int codeHash []byte - Nonce uint64 + nonce uint64 // Contract related attributes State *StateDB - Code Code - InitCode Code + code Code + initCode Code storage Storage @@ -89,20 +89,21 @@ func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateOb object := &StateObject{address: address, db: db} //object.RlpDecode(data) - object.Nonce = extobject.Nonce + object.nonce = extobject.Nonce object.balance = extobject.Balance object.codeHash = extobject.CodeHash object.State = New(extobject.Root, db) object.storage = make(map[string]*ethutil.Value) object.gasPool = new(big.Int) - object.Code, _ = db.Get(extobject.CodeHash) + object.code, _ = db.Get(extobject.CodeHash) return object } func (self *StateObject) MarkForDeletion() { self.remove = true - statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance) + self.dirty = true + statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.nonce, self.balance) } func (c *StateObject) getAddr(addr []byte) *ethutil.Value { @@ -159,25 +160,24 @@ func (self *StateObject) Sync() { } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { - if int64(len(c.Code)-1) < pc.Int64() { + if int64(len(c.code)-1) < pc.Int64() { return ethutil.NewValue(0) } - return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]}) + return ethutil.NewValueFromBytes([]byte{c.code[pc.Int64()]}) } func (c *StateObject) AddBalance(amount *big.Int) { c.SetBalance(new(big.Int).Add(c.balance, amount)) - c.dirty = true - statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount) + statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount) } func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) } func (c *StateObject) SubBalance(amount *big.Int) { c.SetBalance(new(big.Int).Sub(c.balance, amount)) - statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.balance, amount) + statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount) } func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) } @@ -186,8 +186,6 @@ func (c *StateObject) SetBalance(amount *big.Int) { c.dirty = true } -func (self *StateObject) Balance() *big.Int { return self.balance } - // // Gas setters and getters // @@ -243,12 +241,12 @@ func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address(), self.db) stateObject.balance.Set(self.balance) stateObject.codeHash = ethutil.CopyBytes(self.codeHash) - stateObject.Nonce = self.Nonce + stateObject.nonce = self.nonce if self.State != nil { stateObject.State = self.State.Copy() } - stateObject.Code = ethutil.CopyBytes(self.Code) - stateObject.InitCode = ethutil.CopyBytes(self.InitCode) + stateObject.code = ethutil.CopyBytes(self.code) + stateObject.initCode = ethutil.CopyBytes(self.initCode) stateObject.storage = self.storage.Copy() stateObject.gasPool.Set(self.gasPool) stateObject.remove = self.remove @@ -265,8 +263,12 @@ func (self *StateObject) Set(stateObject *StateObject) { // Attribute accessors // +func (self *StateObject) Balance() *big.Int { + return self.balance +} + func (c *StateObject) N() *big.Int { - return big.NewInt(int64(c.Nonce)) + return big.NewInt(int64(c.nonce)) } // Returns the address of the contract/account @@ -276,7 +278,7 @@ func (c *StateObject) Address() []byte { // Returns the initialization Code func (c *StateObject) Init() Code { - return c.InitCode + return c.initCode } func (self *StateObject) Trie() *trie.Trie { @@ -287,8 +289,27 @@ func (self *StateObject) Root() []byte { return self.Trie().Root() } +func (self *StateObject) Code() []byte { + return self.code +} + func (self *StateObject) SetCode(code []byte) { - self.Code = code + self.code = code + self.dirty = true +} + +func (self *StateObject) SetInitCode(code []byte) { + self.initCode = code + self.dirty = true +} + +func (self *StateObject) SetNonce(nonce uint64) { + self.nonce = nonce + self.dirty = true +} + +func (self *StateObject) Nonce() uint64 { + return self.nonce } // @@ -297,16 +318,16 @@ func (self *StateObject) SetCode(code []byte) { // State object encoding methods func (c *StateObject) RlpEncode() []byte { - return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.Root(), c.CodeHash()}) + return ethutil.Encode([]interface{}{c.nonce, c.balance, c.Root(), c.CodeHash()}) } func (c *StateObject) CodeHash() ethutil.Bytes { - return crypto.Sha3(c.Code) + return crypto.Sha3(c.code) } func (c *StateObject) RlpDecode(data []byte) { decoder := ethutil.NewValueFromBytes(data) - c.Nonce = decoder.Get(0).Uint() + c.nonce = decoder.Get(0).Uint() c.balance = decoder.Get(1).BigInt() 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) @@ -314,7 +335,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.codeHash = decoder.Get(3).Bytes() - c.Code, _ = c.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/statedb.go b/state/statedb.go index 8c8a21db9..7e2b24b94 100644 --- a/state/statedb.go +++ b/state/statedb.go @@ -72,7 +72,7 @@ func (self *StateDB) AddBalance(addr []byte, amount *big.Int) { func (self *StateDB) GetNonce(addr []byte) uint64 { stateObject := self.GetStateObject(addr) if stateObject != nil { - return stateObject.Nonce + return stateObject.nonce } return 0 @@ -81,7 +81,7 @@ func (self *StateDB) GetNonce(addr []byte) uint64 { func (self *StateDB) GetCode(addr []byte) []byte { stateObject := self.GetStateObject(addr) if stateObject != nil { - return stateObject.Code + return stateObject.code } return nil @@ -99,8 +99,7 @@ func (self *StateDB) GetState(a, b []byte) []byte { func (self *StateDB) SetNonce(addr []byte, nonce uint64) { stateObject := self.GetStateObject(addr) if stateObject != nil { - stateObject.Nonce = nonce - stateObject.dirty = true + stateObject.SetNonce(nonce) } } @@ -108,7 +107,6 @@ func (self *StateDB) SetCode(addr, code []byte) { stateObject := self.GetStateObject(addr) if stateObject != nil { stateObject.SetCode(code) - stateObject.dirty = true } } @@ -116,7 +114,6 @@ func (self *StateDB) SetState(addr, key []byte, value interface{}) { stateObject := self.GetStateObject(addr) if stateObject != nil { stateObject.SetState(key, ethutil.NewValue(value)) - stateObject.dirty = true } } @@ -124,7 +121,6 @@ func (self *StateDB) Delete(addr []byte) bool { stateObject := self.GetStateObject(addr) if stateObject != nil { stateObject.MarkForDeletion() - stateObject.dirty = true return true } @@ -141,7 +137,7 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) { addr := stateObject.Address() if len(stateObject.CodeHash()) > 0 { - self.db.Put(stateObject.CodeHash(), stateObject.Code) + self.db.Put(stateObject.CodeHash(), stateObject.code) } self.trie.Update(addr, stateObject.RlpEncode()) -- cgit v1.2.3