aboutsummaryrefslogtreecommitdiff
path: root/core/state_transition.go
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2020-09-15 23:55:34 -0400
committerDeterminant <[email protected]>2020-09-15 23:55:34 -0400
commit78745551c077bf54151202138c2629f288769561 (patch)
tree2b628e99fd110617089778fa91235ecd2888f4ef /core/state_transition.go
parent7d1388c743b4ec8f4a86bea95bfada785dee83f7 (diff)
WIP: geth-tavum
Diffstat (limited to 'core/state_transition.go')
-rw-r--r--core/state_transition.go135
1 files changed, 86 insertions, 49 deletions
diff --git a/core/state_transition.go b/core/state_transition.go
index de7488e..a2dbdcf 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -17,18 +17,12 @@
package core
import (
- "errors"
"math"
"math/big"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/log"
-)
-
-var (
- errInsufficientBalanceForGas = errors.New("insufficient balance to pay for gas")
+ "github.com/ethereum/go-ethereum/common"
)
/*
@@ -63,7 +57,6 @@ type StateTransition struct {
// Message represents a message sent to a contract.
type Message interface {
From() common.Address
- //FromFrontier() (common.Address, error)
To() *common.Address
GasPrice() *big.Int
@@ -75,11 +68,46 @@ type Message interface {
Data() []byte
}
+// ExecutionResult includes all output after executing given evm
+// message no matter the execution itself is successful or not.
+type ExecutionResult struct {
+ UsedGas uint64 // Total used gas but include the refunded gas
+ Err error // Any error encountered during the execution(listed in core/vm/errors.go)
+ ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
+}
+
+// Unwrap returns the internal evm error which allows us for further
+// analysis outside.
+func (result *ExecutionResult) Unwrap() error {
+ return result.Err
+}
+
+// Failed returns the indicator whether the execution is successful or not
+func (result *ExecutionResult) Failed() bool { return result.Err != nil }
+
+// Return is a helper function to help caller distinguish between revert reason
+// and function return. Return returns the data after execution if no error occurs.
+func (result *ExecutionResult) Return() []byte {
+ if result.Err != nil {
+ return nil
+ }
+ return common.CopyBytes(result.ReturnData)
+}
+
+// Revert returns the concrete revert reason if the execution is aborted by `REVERT`
+// opcode. Note the reason can be nil if no data supplied with revert opcode.
+func (result *ExecutionResult) Revert() []byte {
+ if result.Err != vm.ErrExecutionReverted {
+ return nil
+ }
+ return common.CopyBytes(result.ReturnData)
+}
+
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
-func IntrinsicGas(data []byte, contractCreation, isEIP155 bool, isEIP2028 bool) (uint64, error) {
+func IntrinsicGas(data []byte, contractCreation, isHomestead bool, isEIP2028 bool) (uint64, error) {
// Set the starting gas for the raw transaction
var gas uint64
- if contractCreation && isEIP155 {
+ if contractCreation && isHomestead {
gas = params.TxGasContractCreation
} else {
gas = params.TxGas
@@ -99,13 +127,13 @@ func IntrinsicGas(data []byte, contractCreation, isEIP155 bool, isEIP2028 bool)
nonZeroGas = params.TxDataNonZeroGasEIP2028
}
if (math.MaxUint64-gas)/nonZeroGas < nz {
- return 0, vm.ErrOutOfGas
+ return 0, ErrGasUintOverflow
}
gas += nz * nonZeroGas
z := uint64(len(data)) - nz
if (math.MaxUint64-gas)/params.TxDataZeroGas < z {
- return 0, vm.ErrOutOfGas
+ return 0, ErrGasUintOverflow
}
gas += z * params.TxDataZeroGas
}
@@ -132,7 +160,7 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
// the gas used (which includes gas refunds) and an error if it failed. An error always
// indicates a core error meaning that the message would always fail for that particular
// state and would never be accepted within a block.
-func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, bool, error) {
+func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) (*ExecutionResult, error) {
return NewStateTransition(evm, msg, gp).TransitionDb()
}
@@ -144,19 +172,10 @@ func (st *StateTransition) to() common.Address {
return *st.msg.To()
}
-func (st *StateTransition) useGas(amount uint64) error {
- if st.gas < amount {
- return vm.ErrOutOfGas
- }
- st.gas -= amount
-
- return nil
-}
-
func (st *StateTransition) buyGas() error {
mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice)
if st.state.GetBalance(st.msg.From()).Cmp(mgval) < 0 {
- return errInsufficientBalanceForGas
+ return ErrInsufficientFunds
}
if err := st.gp.SubGas(st.msg.Gas()); err != nil {
return err
@@ -182,11 +201,32 @@ func (st *StateTransition) preCheck() error {
}
// TransitionDb will transition the state by applying the current message and
-// returning the result including the used gas. It returns an error if failed.
-// An error indicates a consensus issue.
-func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bool, err error) {
- if err = st.preCheck(); err != nil {
- return
+// returning the evm execution result with following fields.
+//
+// - used gas:
+// total gas used (including gas being refunded)
+// - returndata:
+// the returned data from evm
+// - concrete execution error:
+// various **EVM** error which aborts the execution,
+// e.g. ErrOutOfGas, ErrExecutionReverted
+//
+// However if any consensus issue encountered, return the error directly with
+// nil evm execution result.
+func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
+ // First check this message satisfies all consensus rules before
+ // applying the message. The rules include these clauses
+ //
+ // 1. the nonce of the message caller is correct
+ // 2. caller has enough balance to cover transaction fee(gaslimit * gasprice)
+ // 3. the amount of gas required is available in the block
+ // 4. the purchased gas is enough to cover intrinsic usage
+ // 5. there is no overflow when calculating intrinsic gas
+ // 6. caller has enough balance to cover asset transfer for **topmost** call
+
+ // Check clauses 1-3, buy gas if everything is correct
+ if err := st.preCheck(); err != nil {
+ return nil, err
}
msg := st.msg
sender := vm.AccountRef(msg.From())
@@ -194,42 +234,39 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
istanbul := st.evm.ChainConfig().IsIstanbul(st.evm.BlockNumber)
contractCreation := msg.To() == nil
- // Pay intrinsic gas
+ // Check clauses 4-5, subtract intrinsic gas if everything is correct
gas, err := IntrinsicGas(st.data, contractCreation, homestead, istanbul)
if err != nil {
- return nil, 0, false, err
+ return nil, err
}
- if err = st.useGas(gas); err != nil {
- return nil, 0, false, err
+ if st.gas < gas {
+ return nil, ErrIntrinsicGas
}
+ st.gas -= gas
+ // Check clause 6
+ if msg.Value().Sign() > 0 && !st.evm.CanTransfer(st.state, msg.From(), msg.Value()) {
+ return nil, ErrInsufficientFundsForTransfer
+ }
var (
- evm = st.evm
- // vm errors do not effect consensus and are therefor
- // not assigned to err, except for insufficient balance
- // error.
- vmerr error
+ ret []byte
+ vmerr error // vm errors do not effect consensus and are therefore not assigned to err
)
if contractCreation {
- ret, _, st.gas, vmerr = evm.Create(sender, st.data, st.gas, st.value)
+ ret, _, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
- ret, st.gas, vmerr = evm.Call(sender, st.to(), st.data, st.gas, st.value)
- }
- if vmerr != nil {
- log.Debug("VM returned with error", "err", vmerr)
- // The only possible consensus-error would be if there wasn't
- // sufficient balance to make the transfer happen. The first
- // balance transfer may never fail.
- if vmerr == vm.ErrInsufficientBalance {
- return nil, 0, false, vmerr
- }
+ ret, st.gas, vmerr = st.evm.Call(sender, st.to(), st.data, st.gas, st.value)
}
st.refundGas()
st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))
- return ret, st.gasUsed(), vmerr != nil, err
+ return &ExecutionResult{
+ UsedGas: st.gasUsed(),
+ Err: vmerr,
+ ReturnData: ret,
+ }, nil
}
func (st *StateTransition) refundGas() {