2019-10-03 16:46:02 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-04-16 15:47:39 +00:00
|
|
|
"errors"
|
2019-10-03 16:46:02 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2019-10-04 19:32:56 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2019-10-03 16:46:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-16 15:47:39 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-10-03 16:46:02 +00:00
|
|
|
emint "github.com/cosmos/ethermint/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateTransition defines data to transitionDB in evm
|
|
|
|
type StateTransition struct {
|
2020-03-16 22:53:24 +00:00
|
|
|
Payload []byte
|
|
|
|
Recipient *common.Address
|
2019-10-03 16:46:02 +00:00
|
|
|
AccountNonce uint64
|
|
|
|
GasLimit uint64
|
2020-03-16 22:53:24 +00:00
|
|
|
Price *big.Int
|
2019-10-03 16:46:02 +00:00
|
|
|
Amount *big.Int
|
|
|
|
ChainID *big.Int
|
2020-03-16 22:53:24 +00:00
|
|
|
Csdb *CommitStateDB
|
2019-10-04 19:32:56 +00:00
|
|
|
THash *common.Hash
|
2020-03-16 22:53:24 +00:00
|
|
|
Sender common.Address
|
2019-10-22 16:40:34 +00:00
|
|
|
Simulate bool
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// ReturnData represents what's returned from a transition
|
|
|
|
type ReturnData struct {
|
|
|
|
Logs []*ethtypes.Log
|
|
|
|
Bloom *big.Int
|
|
|
|
Result *sdk.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move to keeper
|
2019-10-03 16:46:02 +00:00
|
|
|
// TransitionCSDB performs an evm state transition from a transaction
|
2020-04-01 18:49:21 +00:00
|
|
|
// TODO: update godoc, it doesn't explain what it does in depth.
|
|
|
|
func (st StateTransition) TransitionCSDB(ctx sdk.Context) (*ReturnData, error) {
|
2019-11-15 17:02:13 +00:00
|
|
|
contractCreation := st.Recipient == nil
|
2019-10-29 15:52:26 +00:00
|
|
|
|
2019-10-30 18:39:46 +00:00
|
|
|
cost, err := core.IntrinsicGas(st.Payload, contractCreation, true)
|
2019-10-29 15:52:26 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "invalid intrinsic gas for transaction")
|
2019-10-18 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
// This gas limit the the transaction gas limit with intrinsic gas subtracted
|
2019-10-29 15:52:26 +00:00
|
|
|
gasLimit := st.GasLimit - ctx.GasMeter().GasConsumed()
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
csdb := st.Csdb.WithContext(ctx)
|
2019-10-29 15:52:26 +00:00
|
|
|
if st.Simulate {
|
|
|
|
// gasLimit is set here because stdTxs incur gaskv charges in the ante handler, but for eth_call
|
|
|
|
// the cost needs to be the same as an Ethereum transaction sent through the web3 API
|
2019-11-04 16:59:16 +00:00
|
|
|
consumedGas := ctx.GasMeter().GasConsumed()
|
2019-10-29 15:52:26 +00:00
|
|
|
gasLimit = st.GasLimit - cost
|
2019-11-04 16:59:16 +00:00
|
|
|
if consumedGas < cost {
|
|
|
|
// If Cosmos standard tx ante handler cost is less than EVM intrinsic cost
|
|
|
|
// gas must be consumed to match to accurately simulate an Ethereum transaction
|
|
|
|
ctx.GasMeter().ConsumeGas(cost-consumedGas, "Intrinsic gas match")
|
|
|
|
}
|
2019-10-30 15:16:00 +00:00
|
|
|
|
2019-11-04 16:59:16 +00:00
|
|
|
csdb = st.Csdb.Copy()
|
2019-10-29 15:52:26 +00:00
|
|
|
}
|
2019-10-18 23:14:38 +00:00
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
// This gas meter is set up to consume gas from gaskv during evm execution and be ignored
|
|
|
|
currentGasMeter := ctx.GasMeter()
|
|
|
|
evmGasMeter := sdk.NewInfiniteGasMeter()
|
|
|
|
csdb.WithContext(ctx.WithGasMeter(evmGasMeter))
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Clear cache of accounts to handle changes outside of the EVM
|
|
|
|
csdb.UpdateAccounts()
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
gasPrice := ctx.MinGasPrices().AmountOf(emint.DenomDefault)
|
|
|
|
if gasPrice.IsNil() {
|
|
|
|
return nil, errors.New("gas price cannot be nil")
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:46:02 +00:00
|
|
|
// Create context for evm
|
|
|
|
context := vm.Context{
|
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
|
|
|
Origin: st.Sender,
|
2020-04-01 18:49:21 +00:00
|
|
|
Coinbase: common.Address{}, // TODO: explain why this is empty
|
2019-10-03 16:46:02 +00:00
|
|
|
BlockNumber: big.NewInt(ctx.BlockHeight()),
|
2020-01-08 20:03:39 +00:00
|
|
|
Time: big.NewInt(ctx.BlockHeader().Time.Unix()),
|
2020-04-01 18:49:21 +00:00
|
|
|
Difficulty: big.NewInt(0), // unused. Only required in PoW context
|
2019-10-18 23:14:38 +00:00
|
|
|
GasLimit: gasLimit,
|
2020-04-22 19:26:01 +00:00
|
|
|
GasPrice: gasPrice.BigInt(),
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
evm := vm.NewEVM(context, csdb, GenerateChainConfig(st.ChainID), vm.Config{})
|
2019-10-03 16:46:02 +00:00
|
|
|
|
|
|
|
var (
|
2019-10-22 16:40:34 +00:00
|
|
|
ret []byte
|
2019-10-03 16:46:02 +00:00
|
|
|
leftOverGas uint64
|
|
|
|
addr common.Address
|
|
|
|
senderRef = vm.AccountRef(st.Sender)
|
|
|
|
)
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Get nonce of account outside of the EVM
|
|
|
|
currentNonce := st.Csdb.GetNonce(st.Sender)
|
|
|
|
// Set nonce of sender account before evm state transition for usage in generating Create address
|
|
|
|
st.Csdb.SetNonce(st.Sender, st.AccountNonce)
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
switch contractCreation {
|
|
|
|
case true:
|
|
|
|
ret, addr, leftOverGas, err = evm.Create(senderRef, st.Payload, gasLimit, st.Amount)
|
|
|
|
default:
|
2019-11-15 17:02:13 +00:00
|
|
|
// Increment the nonce for the next transaction (just for evm state transition)
|
2019-11-04 16:59:16 +00:00
|
|
|
csdb.SetNonce(st.Sender, csdb.GetNonce(st.Sender)+1)
|
2020-04-01 18:49:21 +00:00
|
|
|
ret, leftOverGas, err = evm.Call(senderRef, *st.Recipient, st.Payload, gasLimit, st.Amount)
|
|
|
|
}
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
gasConsumed := gasLimit - leftOverGas
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Resets nonce to value pre state transition
|
|
|
|
st.Csdb.SetNonce(st.Sender, currentNonce)
|
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
// Generate bloom filter to be saved in tx receipt data
|
|
|
|
bloomInt := big.NewInt(0)
|
|
|
|
var bloomFilter ethtypes.Bloom
|
2020-04-01 18:49:21 +00:00
|
|
|
var logs []*ethtypes.Log
|
2020-04-13 19:18:50 +00:00
|
|
|
|
2019-11-04 16:59:16 +00:00
|
|
|
if st.THash != nil && !st.Simulate {
|
2020-04-01 18:49:21 +00:00
|
|
|
logs, err = csdb.GetLogs(*st.THash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-13 19:18:50 +00:00
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
bloomInt = ethtypes.LogsBloom(logs)
|
|
|
|
bloomFilter = ethtypes.BytesToBloom(bloomInt.Bytes())
|
|
|
|
}
|
|
|
|
|
2019-10-22 16:40:34 +00:00
|
|
|
// Encode all necessary data into slice of bytes to return in sdk result
|
2020-04-01 18:49:21 +00:00
|
|
|
res := &ResultData{
|
|
|
|
Address: addr,
|
|
|
|
Bloom: bloomFilter,
|
|
|
|
Logs: logs,
|
|
|
|
Ret: ret,
|
2020-04-02 00:43:59 +00:00
|
|
|
TxHash: *st.THash,
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultData, err := EncodeResultData(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2019-10-22 16:04:51 +00:00
|
|
|
// handle errors
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == vm.ErrOutOfGas || err == vm.ErrCodeStoreOutOfGas {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "evm execution went out of gas")
|
2019-10-30 18:39:46 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2019-10-30 18:39:46 +00:00
|
|
|
// Consume gas before returning
|
2020-04-01 18:49:21 +00:00
|
|
|
ctx.GasMeter().ConsumeGas(gasConsumed, "EVM execution consumption")
|
|
|
|
return nil, err
|
2019-10-22 16:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Refund unused gas here, if intended in future
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2019-11-04 16:59:16 +00:00
|
|
|
if !st.Simulate {
|
|
|
|
// Finalise state if not a simulated transaction
|
2020-04-01 18:49:21 +00:00
|
|
|
// TODO: change to depend on config
|
|
|
|
if err := st.Csdb.Finalise(true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-04 16:59:16 +00:00
|
|
|
}
|
2019-10-22 16:04:51 +00:00
|
|
|
|
|
|
|
// Consume gas from evm execution
|
|
|
|
// Out of gas check does not need to be done here since it is done within the EVM execution
|
2020-04-01 18:49:21 +00:00
|
|
|
ctx.WithGasMeter(currentGasMeter).GasMeter().ConsumeGas(gasConsumed, "EVM execution consumption")
|
2019-10-22 16:04:51 +00:00
|
|
|
|
2020-04-13 19:18:50 +00:00
|
|
|
err = st.Csdb.SetLogs(*st.THash, logs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
returnData := &ReturnData{
|
|
|
|
Logs: logs,
|
|
|
|
Bloom: bloomInt,
|
|
|
|
Result: &sdk.Result{Data: resultData},
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return returnData, nil
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|