Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
check statedb errors
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed Apr 25, 2023
1 parent eabc299 commit de6c523
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func (ga *GenesisAlloc) deriveHash() (common.Hash, error) {
statedb.AddBalance(addr, account.Balance)
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)
if err := statedb.Error(); err != nil {
return common.Hash{}, err
}
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
Expand All @@ -147,6 +150,9 @@ func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhas
statedb.AddBalance(addr, account.Balance)
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)
if err := statedb.Error(); err != nil {
return err
}
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
Expand Down
8 changes: 7 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (st *StateTransition) buyGas() error {

st.initialGas = st.msg.GasLimit
st.state.SubBalance(st.msg.From, mgval)
return nil
return st.state.Error()
}

func (st *StateTransition) preCheck() error {
Expand Down Expand Up @@ -372,6 +372,9 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From, st.state.GetNonce(sender.Address())+1)
if err := st.state.Error(); err != nil {
return nil, err
}
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, msg.Value)
}

Expand All @@ -395,6 +398,9 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
fee := new(big.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTip)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
if err := st.state.Error(); err != nil {
return nil, err
}
}

return &ExecutionResult{
Expand Down
9 changes: 9 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
// but is the correct thing to do and matters on other networks, in tests, and potential
// future scenarios
evm.StateDB.AddBalance(addr, big0)
if err := evm.StateDB.Error(); err != nil {
return nil, gas, err
}

// Invoke tracer hooks that signal entering/exiting a call frame
if evm.Config.Debug {
Expand Down Expand Up @@ -430,6 +433,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
return nil, common.Address{}, gas, ErrNonceUintOverflow
}
evm.StateDB.SetNonce(caller.Address(), nonce+1)
if err := evm.StateDB.Error(); err != nil {
return nil, common.Address{}, gas, err
}
// We add this to the access list _before_ taking a snapshot. Even if the creation fails,
// the access-list change should not be rolled back
if evm.chainRules.IsBerlin {
Expand All @@ -445,6 +451,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
evm.StateDB.CreateAccount(address)
if evm.chainRules.IsEIP158 {
evm.StateDB.SetNonce(address, 1)
if err := evm.StateDB.Error(); err != nil {
return nil, common.Address{}, gas, err
}
}
evm.Context.Transfer(evm.StateDB, caller.Address(), address, value)

Expand Down
3 changes: 3 additions & 0 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
beneficiary := scope.Stack.pop()
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
if err := interpreter.evm.StateDB.Error(); err != nil {
return nil, err
}
interpreter.evm.StateDB.Suicide(scope.Contract.Address())
if interpreter.evm.Config.Debug {
interpreter.evm.Config.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
Expand Down
6 changes: 6 additions & 0 deletions ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,9 @@ func (diff *StateOverride) Apply(state state.StateDBI) error {
// Override account nonce.
if account.Nonce != nil {
state.SetNonce(addr, uint64(*account.Nonce))
if err := state.Error(); err != nil {
return err
}
}
// Override account(contract) code.
if account.Code != nil {
Expand All @@ -894,6 +897,9 @@ func (diff *StateOverride) Apply(state state.StateDBI) error {
// Override account balance.
if account.Balance != nil {
state.SetBalance(addr, (*big.Int)(*account.Balance))
if err := state.Error(); err != nil {
return err
}
}
if account.State != nil && account.StateDiff != nil {
return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
Expand Down

0 comments on commit de6c523

Please sign in to comment.