diff options
author | Ethan Buchman <ethan@coinculture.info> | 2015-02-18 08:25:18 +0800 |
---|---|---|
committer | Ethan Buchman <ethan@coinculture.info> | 2015-02-18 08:25:18 +0800 |
commit | 2ba65f4fbaea49c1e0d99959b0331e09b153f931 (patch) | |
tree | add8cabb05cd7fbf0ba4b4bbaf9460dacfc2082d /state | |
parent | 2da367a2bee84d74d1bb0ea1b42d4c22fae486dd (diff) | |
parent | 60318c96d03bcaaf731802b1080a3d87cb482124 (diff) | |
download | go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.gz go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.bz2 go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.lz go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.xz go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.zst go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.zip |
Merge branch 'develop' of https://github.com/ethereum/go-ethereum into develop
Diffstat (limited to 'state')
-rw-r--r-- | state/manifest.go | 61 | ||||
-rw-r--r-- | state/state_object.go | 54 | ||||
-rw-r--r-- | state/statedb.go | 70 |
3 files changed, 46 insertions, 139 deletions
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 913c57a31..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 } @@ -121,26 +143,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 +152,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 { @@ -300,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())) diff --git a/state/statedb.go b/state/statedb.go index 3176ab755..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 @@ -31,8 +29,8 @@ type StateDB struct { // Create a new state from a given trie func New(root []byte, db ethutil.Database) *StateDB { - trie := trie.New(root, db) - return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} + 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() { @@ -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,34 +211,27 @@ 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) { - 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 } @@ -290,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 := 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 { - return self.manifest } // Debug stuff |