mirror of https://github.com/status-im/op-geth.git
Reset state when a transition fails
This commit is contained in:
parent
509389be97
commit
731f55a05d
|
@ -78,11 +78,16 @@ func (s *State) EachStorage(cb ethutil.EachCallback) {
|
||||||
it.Each(cb)
|
it.Each(cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *State) ResetStateObject(stateObject *StateObject) {
|
||||||
|
stateObject.state.Reset()
|
||||||
|
|
||||||
|
delete(self.stateObjects, string(stateObject.Address()))
|
||||||
|
}
|
||||||
|
|
||||||
func (self *State) UpdateStateObject(stateObject *StateObject) {
|
func (self *State) UpdateStateObject(stateObject *StateObject) {
|
||||||
addr := stateObject.Address()
|
addr := stateObject.Address()
|
||||||
|
|
||||||
if self.stateObjects[string(addr)] == nil {
|
if self.stateObjects[string(addr)] == nil {
|
||||||
panic("?")
|
|
||||||
self.stateObjects[string(addr)] = stateObject
|
self.stateObjects[string(addr)] = stateObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,17 +23,36 @@ import (
|
||||||
* 6) Derive new state root
|
* 6) Derive new state root
|
||||||
*/
|
*/
|
||||||
type StateTransition struct {
|
type StateTransition struct {
|
||||||
coinbase []byte
|
coinbase, receiver []byte
|
||||||
tx *Transaction
|
tx *Transaction
|
||||||
gas *big.Int
|
gas, gasPrice *big.Int
|
||||||
state *State
|
value *big.Int
|
||||||
block *Block
|
data []byte
|
||||||
|
state *State
|
||||||
|
block *Block
|
||||||
|
|
||||||
cb, rec, sen *StateObject
|
cb, rec, sen *StateObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Transition(coinbase, sender, receiver, data []byte, gas, gasPrice, value *big.Int, state *State, block *Block) (ret []byte, err error) {
|
||||||
|
stateTransition := &StateTransition{
|
||||||
|
coinbase: coinbase,
|
||||||
|
receiver: receiver,
|
||||||
|
cb: state.GetOrNewStateObject(coinbase),
|
||||||
|
rec: state.GetOrNewStateObject(receiver),
|
||||||
|
sen: state.GetOrNewStateObject(sender),
|
||||||
|
gas: gas,
|
||||||
|
gasPrice: gasPrice,
|
||||||
|
value: value,
|
||||||
|
state: state,
|
||||||
|
block: block,
|
||||||
|
}
|
||||||
|
|
||||||
|
return stateTransition.Transition()
|
||||||
|
}
|
||||||
|
|
||||||
func NewStateTransition(coinbase *StateObject, tx *Transaction, state *State, block *Block) *StateTransition {
|
func NewStateTransition(coinbase *StateObject, tx *Transaction, state *State, block *Block) *StateTransition {
|
||||||
return &StateTransition{coinbase.Address(), tx, new(big.Int), state, block, coinbase, nil, nil}
|
return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) Coinbase() *StateObject {
|
func (self *StateTransition) Coinbase() *StateObject {
|
||||||
|
@ -53,7 +72,7 @@ func (self *StateTransition) Sender() *StateObject {
|
||||||
return self.sen
|
return self.sen
|
||||||
}
|
}
|
||||||
func (self *StateTransition) Receiver() *StateObject {
|
func (self *StateTransition) Receiver() *StateObject {
|
||||||
if self.tx.CreatesContract() {
|
if self.tx != nil && self.tx.CreatesContract() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,22 +132,10 @@ func (self *StateTransition) RefundGas() {
|
||||||
sender.AddAmount(remaining)
|
sender.AddAmount(remaining)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) TransitionState() (err error) {
|
func (self *StateTransition) preCheck() (err error) {
|
||||||
ethutil.Config.Log.Printf(ethutil.LogLevelInfo, "(~) %x\n", self.tx.Hash())
|
|
||||||
|
|
||||||
/*
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
ethutil.Config.Log.Infoln(r)
|
|
||||||
err = fmt.Errorf("state transition err %v", r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
*/
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tx = self.tx
|
tx = self.tx
|
||||||
sender = self.Sender()
|
sender = self.Sender()
|
||||||
receiver *StateObject
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Make sure this transaction's nonce is correct
|
// Make sure this transaction's nonce is correct
|
||||||
|
@ -141,10 +148,40 @@ func (self *StateTransition) TransitionState() (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *StateTransition) TransitionState() (err error) {
|
||||||
|
ethutil.Config.Log.Printf(ethutil.LogLevelInfo, "(~) %x\n", self.tx.Hash())
|
||||||
|
|
||||||
|
/*
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
ethutil.Config.Log.Infoln(r)
|
||||||
|
err = fmt.Errorf("state transition err %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
*/
|
||||||
|
|
||||||
// XXX Transactions after this point are considered valid.
|
// XXX Transactions after this point are considered valid.
|
||||||
|
if err = self.preCheck(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
defer self.RefundGas()
|
defer self.RefundGas()
|
||||||
|
|
||||||
|
_, err = self.Transition()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *StateTransition) Transition() (ret []byte, err error) {
|
||||||
|
var (
|
||||||
|
tx = self.tx
|
||||||
|
sender = self.Sender()
|
||||||
|
receiver *StateObject
|
||||||
|
)
|
||||||
|
|
||||||
// Increment the nonce for the next transaction
|
// Increment the nonce for the next transaction
|
||||||
sender.Nonce += 1
|
sender.Nonce += 1
|
||||||
|
|
||||||
|
@ -152,14 +189,14 @@ func (self *StateTransition) TransitionState() (err error) {
|
||||||
|
|
||||||
// Transaction gas
|
// Transaction gas
|
||||||
if err = self.UseGas(GasTx); err != nil {
|
if err = self.UseGas(GasTx); err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pay data gas
|
// Pay data gas
|
||||||
dataPrice := big.NewInt(int64(len(tx.Data)))
|
dataPrice := big.NewInt(int64(len(self.data)))
|
||||||
dataPrice.Mul(dataPrice, GasData)
|
dataPrice.Mul(dataPrice, GasData)
|
||||||
if err = self.UseGas(dataPrice); err != nil {
|
if err = self.UseGas(dataPrice); err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the receiver is nil it's a contract (\0*32).
|
// If the receiver is nil it's a contract (\0*32).
|
||||||
|
@ -167,25 +204,28 @@ func (self *StateTransition) TransitionState() (err error) {
|
||||||
// Create a new state object for the contract
|
// Create a new state object for the contract
|
||||||
receiver = self.MakeStateObject(self.state, tx)
|
receiver = self.MakeStateObject(self.state, tx)
|
||||||
if receiver == nil {
|
if receiver == nil {
|
||||||
return fmt.Errorf("ERR. Unable to create contract with transaction %v", tx)
|
return nil, fmt.Errorf("Unable to create contract")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transfer value from sender to receiver
|
// Transfer value from sender to receiver
|
||||||
if err = self.transferValue(sender, receiver); err != nil {
|
if err = self.transferValue(sender, receiver); err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the init code and create 'valid' contract
|
// Process the init code and create 'valid' contract
|
||||||
if tx.CreatesContract() {
|
if IsContractAddr(self.receiver) {
|
||||||
// Evaluate the initialization script
|
// Evaluate the initialization script
|
||||||
// and use the return value as the
|
// and use the return value as the
|
||||||
// script section for the state object.
|
// script section for the state object.
|
||||||
|
self.data = nil
|
||||||
ethutil.Config.Log.Println(ethutil.LogLevelSystem, receiver.Init())
|
ethutil.Config.Log.Println(ethutil.LogLevelSystem, receiver.Init())
|
||||||
|
|
||||||
code, err := self.Eval(receiver.Init(), receiver)
|
code, err := self.Eval(receiver.Init(), receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error during init script run %v", err)
|
self.state.ResetStateObject(receiver)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Error during init script run %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
receiver.script = code
|
receiver.script = code
|
||||||
|
@ -193,53 +233,55 @@ func (self *StateTransition) TransitionState() (err error) {
|
||||||
if len(receiver.Script()) > 0 {
|
if len(receiver.Script()) > 0 {
|
||||||
ethutil.Config.Log.Println(ethutil.LogLevelSystem, receiver.Script())
|
ethutil.Config.Log.Println(ethutil.LogLevelSystem, receiver.Script())
|
||||||
|
|
||||||
_, err := self.Eval(receiver.Script(), receiver)
|
ret, err = self.Eval(receiver.Script(), receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error during code execution %v", err)
|
self.state.ResetStateObject(receiver)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Error during code execution %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) transferValue(sender, receiver *StateObject) error {
|
func (self *StateTransition) transferValue(sender, receiver *StateObject) error {
|
||||||
if sender.Amount.Cmp(self.tx.Value) < 0 {
|
if sender.Amount.Cmp(self.value) < 0 {
|
||||||
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.tx.Value, sender.Amount)
|
return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.tx.Value.Cmp(ethutil.Big0) > 0 {
|
//if self.value.Cmp(ethutil.Big0) > 0 {
|
||||||
// Subtract the amount from the senders account
|
// Subtract the amount from the senders account
|
||||||
sender.SubAmount(self.tx.Value)
|
sender.SubAmount(self.value)
|
||||||
// Add the amount to receivers account which should conclude this transaction
|
// Add the amount to receivers account which should conclude this transaction
|
||||||
receiver.AddAmount(self.tx.Value)
|
receiver.AddAmount(self.value)
|
||||||
|
|
||||||
ethutil.Config.Log.Debugf("%x => %x (%v) %x\n", sender.Address()[:4], receiver.Address()[:4], self.tx.Value, self.tx.Hash())
|
//ethutil.Config.Log.Debugf("%x => %x (%v)\n", sender.Address()[:4], receiver.Address()[:4], self.value)
|
||||||
}
|
//}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []byte, err error) {
|
func (self *StateTransition) Eval(script []byte, context *StateObject) (ret []byte, err error) {
|
||||||
var (
|
var (
|
||||||
tx = self.tx
|
|
||||||
block = self.block
|
block = self.block
|
||||||
initiator = self.Sender()
|
initiator = self.Sender()
|
||||||
state = self.state
|
state = self.state
|
||||||
)
|
)
|
||||||
|
|
||||||
closure := NewClosure(initiator, context, script, state, self.gas, tx.GasPrice)
|
closure := NewClosure(initiator, context, script, state, self.gas, self.gasPrice)
|
||||||
vm := NewVm(state, nil, RuntimeVars{
|
vm := NewVm(state, nil, RuntimeVars{
|
||||||
Origin: initiator.Address(),
|
Origin: initiator.Address(),
|
||||||
BlockNumber: block.BlockInfo().Number,
|
Block: block,
|
||||||
|
BlockNumber: block.Number,
|
||||||
PrevHash: block.PrevHash,
|
PrevHash: block.PrevHash,
|
||||||
Coinbase: block.Coinbase,
|
Coinbase: block.Coinbase,
|
||||||
Time: block.Time,
|
Time: block.Time,
|
||||||
Diff: block.Difficulty,
|
Diff: block.Difficulty,
|
||||||
Value: tx.Value,
|
Value: self.value,
|
||||||
})
|
})
|
||||||
vm.Verbose = true
|
vm.Verbose = true
|
||||||
ret, _, err = closure.Call(vm, tx.Data, nil)
|
ret, _, err = closure.Call(vm, self.data, nil)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue