Skip to content

Commit

Permalink
Fixed reverting error
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Jul 3, 2014
1 parent 5d67139 commit 8baa0f8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
8 changes: 5 additions & 3 deletions ethchain/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ func (self *State) Copy() *State {
}

func (self *State) Set(state *State) {
//s.trie = snapshot.trie
//s.stateObjects = snapshot.stateObjects
self = state
if state == nil {
panic("Tried setting 'state' to nil through 'Set'")
}

*self = *state
}

func (s *State) Put(key, object []byte) {
Expand Down
5 changes: 2 additions & 3 deletions ethchain/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,12 @@ func (self *StateObject) Copy() *StateObject {
}
stateObject.script = ethutil.CopyBytes(self.script)
stateObject.initScript = ethutil.CopyBytes(self.initScript)
//stateObject.gasPool.Set(self.gasPool)

return self
return stateObject
}

func (self *StateObject) Set(stateObject *StateObject) {
self = stateObject
*self = *stateObject
}

//
Expand Down
41 changes: 16 additions & 25 deletions ethchain/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,34 @@ func (self *StateTransition) TransitionState() (err error) {
return
}

//snapshot := self.state.Copy()

// Process the init code and create 'valid' contract
if IsContractAddr(self.receiver) {
// Evaluate the initialization script
// and use the return value as the
// script section for the state object.
self.data = nil

code, err, deepErr := self.Eval(receiver.Init(), receiver)
if err != nil || deepErr {
statelogger.Debugln("~> init")
code, err := self.Eval(receiver.Init(), receiver)
if err != nil {
//self.state.Set(snapshot)
self.state.ResetStateObject(receiver)

return fmt.Errorf("Error during init script run %v (deepErr = %v)", err, deepErr)
return fmt.Errorf("Error during init execution %v", err)
}

receiver.script = code
} else {
if len(receiver.Script()) > 0 {
var deepErr bool
_, err, deepErr = self.Eval(receiver.Script(), receiver)
statelogger.Debugln("~> code")
_, err = self.Eval(receiver.Script(), receiver)
if err != nil {
//self.state.Set(snapshot)
self.state.ResetStateObject(receiver)

return fmt.Errorf("Error during code execution %v (deepErr = %v)", err, deepErr)
return fmt.Errorf("Error during code execution %v", err)
}
}
}
Expand All @@ -235,7 +240,7 @@ func (self *StateTransition) transferValue(sender, receiver *StateObject) error
return nil
}

func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []byte, err error, deepErr bool) {
func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []byte, err error) {
var (
block = self.block
initiator = self.Sender()
Expand All @@ -255,14 +260,13 @@ func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []by
})
vm.Verbose = true

ret, err, deepErr = Call(vm, closure, self.data)
ret, err = Call(vm, closure, self.data)

return
}

func Call(vm *Vm, closure *Closure, data []byte) (ret []byte, err error, deepErr bool) {
func Call(vm *Vm, closure *Closure, data []byte) (ret []byte, err error) {
ret, _, err = closure.Call(vm, data)
deepErr = vm.err != nil

if ethutil.Config.Paranoia {
var (
Expand All @@ -274,21 +278,8 @@ func Call(vm *Vm, closure *Closure, data []byte) (ret []byte, err error, deepErr
if !valid {
// TODO FIXME ASAP
context.state.trie = t2
/*
statelogger.Debugf("(o): %x\n", trie.Root)
trie.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
statelogger.Debugf("%x : %x\n", key, v.Str())
})
statelogger.Debugf("(c): %x\n", trie2.Root)
trie2.NewIterator().Each(func(key string, v *ethutil.Value) {
v.Decode()
statelogger.Debugf("%x : %x\n", key, v.Str())
})
*/

//return nil, fmt.Errorf("PARANOIA: Different state object roots during copy"), false

statelogger.Debugln("Warn: PARANOIA: Different state object roots during copy")
}
}

Expand Down
13 changes: 6 additions & 7 deletions ethchain/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (self *Vm) Printf(format string, v ...interface{}) *Vm {

func (self *Vm) Endl() *Vm {
if self.Verbose {
vmlogger.Infoln(self.logStr)
vmlogger.Debugln(self.logStr)
self.logStr = ""
}

Expand Down Expand Up @@ -652,7 +652,7 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
c := NewClosure(closure, contract, contract.initScript, vm.state, gas, closure.Price)
// Call the closure and set the return value as
// main script.
contract.script, err, _ = Call(vm, c, nil)
contract.script, err = Call(vm, c, nil)
} else {
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", value, closure.object.Amount)
}
Expand Down Expand Up @@ -690,25 +690,24 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) {

stack.Push(ethutil.BigFalse)
} else {
snapshot := vm.state.Copy()
//snapshot := vm.state.Copy()

stateObject := vm.state.GetOrNewStateObject(addr.Bytes())

closure.object.SubAmount(value)
// Add the value to the state object
stateObject.AddAmount(value)

// Create a new callable closure
closure := NewClosure(closure, stateObject, stateObject.script, vm.state, gas, closure.Price)
// Executer the closure and get the return value (if any)
ret, err, _ := Call(vm, closure, args)
ret, err := Call(vm, closure, args)
if err != nil {
stack.Push(ethutil.BigFalse)

vmlogger.Debugf("Closure execution failed. %v\n", err)

vm.err = err
vm.state.Set(snapshot)
//vm.state.Set(snapshot)
vm.state.ResetStateObject(stateObject)
} else {
stack.Push(ethutil.BigTrue)

Expand Down

0 comments on commit 8baa0f8

Please sign in to comment.