From 32d125131f602d63f66ee7eb09439074f0b94a91 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 24 Jul 2014 12:04:15 +0200 Subject: Refactored to new state and vm --- ethstate/state.go | 52 ++++++++++++++++++++++++------------------------ ethstate/state_object.go | 44 +++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 50 deletions(-) (limited to 'ethstate') diff --git a/ethstate/state.go b/ethstate/state.go index a4b9b1e9c..51b585d4d 100644 --- a/ethstate/state.go +++ b/ethstate/state.go @@ -17,7 +17,7 @@ var statelogger = ethlog.NewLogger("STATE") // * Accounts type State struct { // The trie for this structure - trie *ethtrie.Trie + Trie *ethtrie.Trie stateObjects map[string]*StateObject @@ -26,7 +26,7 @@ type State struct { // Create a new state from a given trie func NewState(trie *ethtrie.Trie) *State { - return &State{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()} + return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()} } // Retrieve the balance from the given address or 0 if object not found @@ -58,14 +58,14 @@ func (self *State) UpdateStateObject(stateObject *StateObject) { ethutil.Config.Db.Put(ethcrypto.Sha3Bin(stateObject.Code), stateObject.Code) - self.trie.Update(string(addr), string(stateObject.RlpEncode())) + self.Trie.Update(string(addr), string(stateObject.RlpEncode())) self.manifest.AddObjectChange(stateObject) } // Delete the given state object and delete it from the state trie func (self *State) DeleteStateObject(stateObject *StateObject) { - self.trie.Delete(string(stateObject.Address())) + self.Trie.Delete(string(stateObject.Address())) delete(self.stateObjects, string(stateObject.Address())) } @@ -79,7 +79,7 @@ func (self *State) GetStateObject(addr []byte) *StateObject { return stateObject } - data := self.trie.Get(string(addr)) + data := self.Trie.Get(string(addr)) if len(data) == 0 { return nil } @@ -122,12 +122,12 @@ func (self *State) GetAccount(addr []byte) *StateObject { // func (s *State) Cmp(other *State) bool { - return s.trie.Cmp(other.trie) + return s.Trie.Cmp(other.Trie) } func (self *State) Copy() *State { - if self.trie != nil { - state := NewState(self.trie.Copy()) + if self.Trie != nil { + state := NewState(self.Trie.Copy()) for k, stateObject := range self.stateObjects { state.stateObjects[k] = stateObject.Copy() } @@ -143,21 +143,21 @@ func (self *State) Set(state *State) { panic("Tried setting 'state' to nil through 'Set'") } - self.trie = state.trie + self.Trie = state.Trie self.stateObjects = state.stateObjects } func (s *State) Root() interface{} { - return s.trie.Root + return s.Trie.Root } // Resets the trie and all siblings func (s *State) Reset() { - s.trie.Undo() + s.Trie.Undo() // Reset all nested states for _, stateObject := range s.stateObjects { - if stateObject.state == nil { + if stateObject.State == nil { continue } @@ -174,14 +174,14 @@ func (s *State) Sync() { for _, stateObject := range s.stateObjects { //s.UpdateStateObject(stateObject) - if stateObject.state == nil { + if stateObject.State == nil { continue } - stateObject.state.Sync() + stateObject.State.Sync() } - s.trie.Sync() + s.Trie.Sync() s.Empty() } @@ -202,11 +202,11 @@ func (self *State) Update() { } // FIXME trie delete is broken - valid, t2 := ethtrie.ParanoiaCheck(self.trie) + valid, t2 := ethtrie.ParanoiaCheck(self.Trie) if !valid { - statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.trie.Root, t2.Root) + statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.Trie.Root, t2.Root) - self.trie = t2 + self.Trie = t2 } } @@ -230,8 +230,8 @@ type Manifest struct { objectAddresses map[string]bool storageAddresses map[string]map[string]bool - objectChanges map[string]*StateObject - storageChanges map[string]map[string]*big.Int + ObjectChanges map[string]*StateObject + StorageChanges map[string]map[string]*big.Int } func NewManifest() *Manifest { @@ -242,18 +242,18 @@ func NewManifest() *Manifest { } func (m *Manifest) Reset() { - m.objectChanges = make(map[string]*StateObject) - m.storageChanges = make(map[string]map[string]*big.Int) + m.ObjectChanges = make(map[string]*StateObject) + m.StorageChanges = make(map[string]map[string]*big.Int) } func (m *Manifest) AddObjectChange(stateObject *StateObject) { - m.objectChanges[string(stateObject.Address())] = stateObject + m.ObjectChanges[string(stateObject.Address())] = stateObject } func (m *Manifest) AddStorageChange(stateObject *StateObject, storageAddr []byte, storage *big.Int) { - if m.storageChanges[string(stateObject.Address())] == nil { - m.storageChanges[string(stateObject.Address())] = make(map[string]*big.Int) + if m.StorageChanges[string(stateObject.Address())] == nil { + m.StorageChanges[string(stateObject.Address())] = make(map[string]*big.Int) } - m.storageChanges[string(stateObject.Address())][string(storageAddr)] = storage + m.StorageChanges[string(stateObject.Address())][string(storageAddr)] = storage } diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 6b00c5369..ab14b8604 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -34,9 +34,9 @@ type StateObject struct { CodeHash []byte Nonce uint64 // Contract related attributes - state *State + State *State Code Code - initCode Code + InitCode Code storage Storage @@ -53,7 +53,7 @@ type StateObject struct { func (self *StateObject) Reset() { self.storage = make(Storage) - self.state.Reset() + self.State.Reset() } /* @@ -79,7 +79,7 @@ func NewStateObject(addr []byte) *StateObject { address := ethutil.Address(addr) object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} - object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) + object.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -89,7 +89,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.Amount = Amount - contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) + contract.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) return contract } @@ -107,11 +107,11 @@ func (self *StateObject) MarkForDeletion() { } func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { - return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr)))) + return ethutil.NewValueFromBytes([]byte(c.State.Trie.Get(string(addr)))) } func (c *StateObject) SetAddr(addr []byte, value interface{}) { - c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) + c.State.Trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) } func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { @@ -152,7 +152,7 @@ func (self *StateObject) EachStorage(cb ethtrie.EachCallback) { cb(key, encoded) } - it := self.state.trie.NewIterator() + 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 { @@ -166,18 +166,18 @@ func (self *StateObject) Sync() { if value.Len() == 0 { // value.BigInt().Cmp(ethutil.Big0) == 0 { //data := self.getStorage([]byte(key)) //fmt.Printf("deleting %x %x 0x%x\n", self.Address(), []byte(key), data) - self.state.trie.Delete(string(key)) + self.State.Trie.Delete(string(key)) continue } self.SetAddr([]byte(key), value) } - valid, t2 := ethtrie.ParanoiaCheck(self.state.trie) + valid, t2 := ethtrie.ParanoiaCheck(self.State.Trie) if !valid { - statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.state.trie.Root, t2.Root) + statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Trie.Root, t2.Root) - self.state.trie = t2 + self.State.Trie = t2 } } @@ -255,11 +255,11 @@ func (self *StateObject) Copy() *StateObject { stateObject.Amount.Set(self.Amount) stateObject.CodeHash = ethutil.CopyBytes(self.CodeHash) stateObject.Nonce = self.Nonce - if self.state != nil { - stateObject.state = self.state.Copy() + if self.State != nil { + stateObject.State = self.State.Copy() } stateObject.Code = ethutil.CopyBytes(self.Code) - stateObject.initCode = ethutil.CopyBytes(self.initCode) + stateObject.InitCode = ethutil.CopyBytes(self.InitCode) stateObject.storage = self.storage.Copy() stateObject.gasPool.Set(self.gasPool) @@ -274,10 +274,6 @@ func (self *StateObject) Set(stateObject *StateObject) { // Attribute accessors // -func (c *StateObject) State() *State { - return c.state -} - func (c *StateObject) N() *big.Int { return big.NewInt(int64(c.Nonce)) } @@ -289,12 +285,12 @@ func (c *StateObject) Address() []byte { // Returns the initialization Code func (c *StateObject) Init() Code { - return c.initCode + return c.InitCode } // Debug stuff func (self *StateObject) CreateOutputForDiff() { - fmt.Printf("%x %x %x %x\n", self.Address(), self.state.Root(), self.Amount.Bytes(), self.Nonce) + fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Amount.Bytes(), self.Nonce) self.EachStorage(func(addr string, value *ethutil.Value) { fmt.Printf("%x %x\n", addr, value.Bytes()) }) @@ -307,8 +303,8 @@ func (self *StateObject) CreateOutputForDiff() { // State object encoding methods func (c *StateObject) RlpEncode() []byte { var root interface{} - if c.state != nil { - root = c.state.trie.Root + if c.State != nil { + root = c.State.Trie.Root } else { root = "" } @@ -321,7 +317,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.Amount = decoder.Get(1).BigInt() - c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) -- cgit