Skip to content

Commit c74c07e

Browse files
committed
Fixed error for invalid transaction
1 parent 6518da7 commit c74c07e

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

‎core/error.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ func IsNonceErr(err error) bool {
8787
return ok
8888
}
8989

90+
type InvalidTxErr struct {
91+
Message string
92+
}
93+
94+
func (err *InvalidTxErr) Error() string {
95+
return err.Message
96+
}
97+
98+
func InvalidTxError(err error) *InvalidTxErr {
99+
return &InvalidTxErr{fmt.Sprintf("%v", err)}
100+
}
101+
102+
func IsInvalidTxErr(err error) bool {
103+
_, ok := err.(*InvalidTxErr)
104+
105+
return ok
106+
}
107+
90108
type OutOfGasErr struct {
91109
Message string
92110
}

‎core/state_transition.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package core
33
import (
44
"fmt"
55
"math/big"
6-
76
"github.com/ethereum/go-ethereum/crypto"
87
"github.com/ethereum/go-ethereum/ethutil"
98
"github.com/ethereum/go-ethereum/state"
@@ -146,7 +145,7 @@ func (self *StateTransition) preCheck() (err error) {
146145

147146
// Pre-pay gas / Buy gas of the coinbase account
148147
if err = self.BuyGas(); err != nil {
149-
return err
148+
return InvalidTxError(err)
150149
}
151150

152151
return nil
@@ -167,15 +166,15 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) {
167166

168167
defer self.RefundGas()
169168

170-
// Increment the nonce for the next transaction
171-
self.state.SetNonce(sender.Address(), sender.Nonce()+1)
172-
//sender.Nonce += 1
173-
174169
// Transaction gas
175170
if err = self.UseGas(vm.GasTx); err != nil {
176-
return
171+
return nil, InvalidTxError(err)
177172
}
178173

174+
// Increment the nonce for the next transaction
175+
self.state.SetNonce(sender.Address(), sender.Nonce()+1)
176+
//sender.Nonce += 1
177+
179178
// Pay data gas
180179
var dgas int64
181180
for _, byt := range self.data {

‎tests/helper/vm.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
167167
// Set pre compiled contracts
168168
vm.Precompiled = vm.PrecompiledContracts()
169169

170+
snapshot := statedb.Copy()
170171
coinbase := statedb.GetOrNewStateObject(caddr)
171172
coinbase.SetGasPool(ethutil.Big(env["currentGasLimit"]))
172173

@@ -175,6 +176,9 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
175176
st := core.NewStateTransition(vmenv, message, coinbase)
176177
vmenv.origin = keyPair.Address()
177178
ret, err := st.TransitionState()
179+
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) {
180+
statedb.Set(snapshot)
181+
}
178182
statedb.Update(vmenv.Gas)
179183

180184
return ret, vmenv.logs, vmenv.Gas, err

‎tests/vm/gh_test.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ func RunVmTest(p string, t *testing.T) {
7979
helper.CreateFileTests(t, p, &tests)
8080

8181
for name, test := range tests {
82-
helper.Logger.SetLogLevel(4)
83-
if name != "callcodeToNameRegistratorAddresTooBigRight" {
84-
continue
85-
}
8682
db, _ := ethdb.NewMemDatabase()
8783
statedb := state.New(nil, db)
8884
for addr, account := range test.Pre {
@@ -161,7 +157,7 @@ func RunVmTest(p string, t *testing.T) {
161157

162158
if !isVmTest {
163159
if !bytes.Equal(ethutil.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
164-
//t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
160+
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
165161
}
166162
}
167163

0 commit comments

Comments
 (0)