diff options
Diffstat (limited to 'ethchain/state_object.go')
-rw-r--r-- | ethchain/state_object.go | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/ethchain/state_object.go b/ethchain/state_object.go index ebc050863..5791c6ed1 100644 --- a/ethchain/state_object.go +++ b/ethchain/state_object.go @@ -15,6 +15,18 @@ func (self Code) String() string { return strings.Join(Disassemble(self), " ") } +type Storage map[string]*ethutil.Value + +func (self Storage) Copy() Storage { + cpy := make(Storage) + for key, value := range self { + // XXX Do we need a 'value' copy or is this sufficient? + cpy[key] = value + } + + return cpy +} + type StateObject struct { // Address of the object address []byte @@ -27,7 +39,7 @@ type StateObject struct { script Code initScript Code - storage map[string]*ethutil.Value + storage Storage // Total gas pool is the total amount of gas currently // left if this object is the coinbase. Gas is directly @@ -41,7 +53,8 @@ type StateObject struct { } func (self *StateObject) Reset() { - self.storage = make(map[string]*ethutil.Value) + self.storage = make(Storage) + self.state.Reset() } // Converts an transaction in to a state object @@ -66,7 +79,7 @@ func NewStateObject(addr []byte) *StateObject { object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) - object.storage = make(map[string]*ethutil.Value) + object.storage = make(Storage) return object } @@ -95,7 +108,7 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject { func (self *StateObject) MarkForDeletion() { self.remove = true - statelogger.Infof("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Amount) + statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Amount) } func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { @@ -113,23 +126,24 @@ func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) { self.setStorage(key.Bytes(), value) } -func (self *StateObject) getStorage(key []byte) *ethutil.Value { - k := ethutil.LeftPadBytes(key, 32) +func (self *StateObject) getStorage(k []byte) *ethutil.Value { + key := ethutil.LeftPadBytes(k, 32) - value := self.storage[string(k)] + value := self.storage[string(key)] if value == nil { - value = self.GetAddr(k) + value = self.GetAddr(key) - self.storage[string(k)] = value + if !value.IsNil() { + self.storage[string(key)] = value + } } return value } -func (self *StateObject) setStorage(key []byte, value *ethutil.Value) { - k := ethutil.LeftPadBytes(key, 32) - - self.storage[string(k)] = value +func (self *StateObject) setStorage(k []byte, value *ethutil.Value) { + key := ethutil.LeftPadBytes(k, 32) + self.storage[string(key)] = value.Copy() } func (self *StateObject) Sync() { @@ -140,9 +154,16 @@ func (self *StateObject) Sync() { } self.SetAddr([]byte(key), value) + } + valid, t2 := ethtrie.ParanoiaCheck(self.state.trie) + if !valid { + self.state.trie = t2 + + statelogger.Infoln("Warn: PARANOIA: Different state storage root during copy") } } + func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { if int64(len(c.script)-1) < pc.Int64() { return ethutil.NewValue(0) @@ -154,13 +175,13 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { func (c *StateObject) AddAmount(amount *big.Int) { c.SetAmount(new(big.Int).Add(c.Amount, amount)) - statelogger.Infof("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.DebugDetailf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount) } func (c *StateObject) SubAmount(amount *big.Int) { c.SetAmount(new(big.Int).Sub(c.Amount, amount)) - statelogger.Infof("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.DebugDetailf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) } func (c *StateObject) SetAmount(amount *big.Int) { @@ -222,6 +243,7 @@ func (self *StateObject) Copy() *StateObject { } stateObject.script = ethutil.CopyBytes(self.script) stateObject.initScript = ethutil.CopyBytes(self.initScript) + stateObject.storage = self.storage.Copy() return stateObject } |