2018-08-09 01:06:34 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/types"
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO: Ported from the SDK and may have a different context/value for
|
|
|
|
// Ethermint.
|
|
|
|
verifySigCost = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
// internalAnteHandler reflects a function signature an internal ante handler
|
|
|
|
// must implementing. Internal ante handlers will be dependant upon the
|
|
|
|
// transaction type.
|
|
|
|
type internalAnteHandler func(
|
2018-10-24 14:49:37 +00:00
|
|
|
sdkCtx sdk.Context, tx sdk.Tx, accMapper auth.AccountKeeper,
|
2018-08-09 01:06:34 +00:00
|
|
|
) (newCtx sdk.Context, res sdk.Result, abort bool)
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
// AnteHandler is responsible for attempting to route an Ethereum or SDK
|
|
|
|
// transaction to an internal ante handler for performing transaction-level
|
|
|
|
// processing (e.g. fee payment, signature verification) before being passed
|
|
|
|
// onto it's respective handler.
|
2018-10-24 14:49:37 +00:00
|
|
|
func AnteHandler(ak auth.AccountKeeper, _ auth.FeeCollectionKeeper) sdk.AnteHandler {
|
|
|
|
return func(sdkCtx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
|
2018-08-09 01:06:34 +00:00
|
|
|
var (
|
|
|
|
handler internalAnteHandler
|
2018-08-16 21:04:19 +00:00
|
|
|
gasLimit int64
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
switch tx := tx.(type) {
|
|
|
|
case types.Transaction:
|
2018-08-30 21:36:14 +00:00
|
|
|
gasLimit = int64(tx.Data().GasLimit)
|
2018-08-09 01:06:34 +00:00
|
|
|
handler = handleEthTx
|
2018-08-24 15:29:10 +00:00
|
|
|
case auth.StdTx:
|
2018-08-09 01:06:34 +00:00
|
|
|
gasLimit = tx.Fee.Gas
|
|
|
|
handler = handleEmbeddedTx
|
|
|
|
default:
|
|
|
|
return sdkCtx, sdk.ErrInternal(fmt.Sprintf("invalid transaction: %T", tx)).Result(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
newCtx = sdkCtx.WithGasMeter(sdk.NewGasMeter(gasLimit))
|
|
|
|
|
|
|
|
// AnteHandlers must have their own defer/recover in order for the
|
|
|
|
// BaseApp to know how much gas was used! This is because the GasMeter
|
|
|
|
// is created in the AnteHandler, but if it panics the context won't be
|
|
|
|
// set properly in runTx's recover.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch rType := r.(type) {
|
|
|
|
case sdk.ErrorOutOfGas:
|
|
|
|
log := fmt.Sprintf("out of gas in location: %v", rType.Descriptor)
|
|
|
|
res = sdk.ErrOutOfGas(log).Result()
|
|
|
|
res.GasWanted = gasLimit
|
|
|
|
res.GasUsed = newCtx.GasMeter().GasConsumed()
|
|
|
|
abort = true
|
|
|
|
default:
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
return handler(newCtx, tx, ak)
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEthTx implements an ante handler for an Ethereum transaction. It
|
|
|
|
// validates the signature and if valid returns an OK result.
|
2018-10-24 14:49:37 +00:00
|
|
|
func handleEthTx(sdkCtx sdk.Context, tx sdk.Tx, ak auth.AccountKeeper) (sdk.Context, sdk.Result, bool) {
|
2018-08-09 01:06:34 +00:00
|
|
|
ethTx, ok := tx.(types.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return sdkCtx, sdk.ErrInternal(fmt.Sprintf("invalid transaction: %T", tx)).Result(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// the SDK chainID is a string representation of integer
|
|
|
|
chainID, ok := new(big.Int).SetString(sdkCtx.ChainID(), 10)
|
|
|
|
if !ok {
|
|
|
|
return sdkCtx, sdk.ErrInternal(fmt.Sprintf("invalid chainID: %s", sdkCtx.ChainID())).Result(), true
|
|
|
|
}
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
sdkCtx.GasMeter().ConsumeGas(verifySigCost, "ante: verify Ethereum signature")
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
addr, err := ethTx.VerifySig(chainID)
|
2018-08-09 01:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return sdkCtx, sdk.ErrUnauthorized("signature verification failed").Result(), true
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
acc := ak.GetAccount(sdkCtx, addr.Bytes())
|
2018-08-30 21:36:14 +00:00
|
|
|
|
|
|
|
// validate the account nonce (referred to as sequence in the AccountMapper)
|
2018-08-16 21:04:19 +00:00
|
|
|
seq := acc.GetSequence()
|
2018-08-30 21:36:14 +00:00
|
|
|
if ethTx.Data().AccountNonce != uint64(seq) {
|
|
|
|
return sdkCtx, sdk.ErrInvalidSequence(fmt.Sprintf("invalid account nonce; expected: %d", seq)).Result(), true
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
2018-08-30 21:36:14 +00:00
|
|
|
|
2018-10-03 16:43:00 +00:00
|
|
|
// TODO: The EVM will handle incrementing the nonce (sequence number)
|
|
|
|
//
|
|
|
|
// TODO: Investigate gas consumption models as the EVM instruction set has its
|
|
|
|
// own and we should probably not charge for additional gas where we don't have
|
|
|
|
// to.
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
ak.SetAccount(sdkCtx, acc)
|
2018-08-30 21:36:14 +00:00
|
|
|
return sdkCtx, sdk.Result{GasWanted: int64(ethTx.Data().GasLimit)}, false
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleEmbeddedTx implements an ante handler for an SDK transaction. It
|
|
|
|
// validates the signature and if valid returns an OK result.
|
2018-10-24 14:49:37 +00:00
|
|
|
func handleEmbeddedTx(sdkCtx sdk.Context, tx sdk.Tx, ak auth.AccountKeeper) (sdk.Context, sdk.Result, bool) {
|
2018-08-24 15:29:10 +00:00
|
|
|
stdTx, ok := tx.(auth.StdTx)
|
2018-08-09 01:06:34 +00:00
|
|
|
if !ok {
|
|
|
|
return sdkCtx, sdk.ErrInternal(fmt.Sprintf("invalid transaction: %T", tx)).Result(), true
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
if err := validateStdTxBasic(stdTx); err != nil {
|
2018-08-09 01:06:34 +00:00
|
|
|
return sdkCtx, err.Result(), true
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
signerAddrs := stdTx.GetSigners()
|
2018-08-09 01:06:34 +00:00
|
|
|
signerAccs := make([]auth.Account, len(signerAddrs))
|
|
|
|
|
|
|
|
// validate signatures
|
2018-08-24 15:29:10 +00:00
|
|
|
for i, sig := range stdTx.Signatures {
|
2018-08-09 01:06:34 +00:00
|
|
|
signer := ethcmn.BytesToAddress(signerAddrs[i].Bytes())
|
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
acc, err := validateSignature(sdkCtx, stdTx, signer, sig, ak)
|
2018-08-16 22:37:10 +00:00
|
|
|
if err != nil {
|
2018-08-14 01:02:21 +00:00
|
|
|
return sdkCtx, err.Result(), true
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fees!
|
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
ak.SetAccount(sdkCtx, acc)
|
2018-08-30 21:36:14 +00:00
|
|
|
signerAccs[i] = acc
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newCtx := auth.WithSigners(sdkCtx, signerAccs)
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
return newCtx, sdk.Result{GasWanted: stdTx.Fee.Gas}, false
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
// validateStdTxBasic validates an auth.StdTx based on parameters that do not
|
2018-08-09 01:06:34 +00:00
|
|
|
// depend on the context.
|
2018-08-24 15:29:10 +00:00
|
|
|
func validateStdTxBasic(stdTx auth.StdTx) (err sdk.Error) {
|
|
|
|
sigs := stdTx.Signatures
|
2018-08-09 01:06:34 +00:00
|
|
|
if len(sigs) == 0 {
|
|
|
|
return sdk.ErrUnauthorized("transaction missing signatures")
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
signerAddrs := stdTx.GetSigners()
|
2018-08-09 01:06:34 +00:00
|
|
|
if len(sigs) != len(signerAddrs) {
|
|
|
|
return sdk.ErrUnauthorized("invalid number of transaction signers")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSignature(
|
2018-08-24 15:29:10 +00:00
|
|
|
sdkCtx sdk.Context, stdTx auth.StdTx, signer ethcmn.Address,
|
2018-10-24 14:49:37 +00:00
|
|
|
sig auth.StdSignature, ak auth.AccountKeeper,
|
2018-08-09 01:06:34 +00:00
|
|
|
) (acc auth.Account, sdkErr sdk.Error) {
|
|
|
|
|
|
|
|
chainID := sdkCtx.ChainID()
|
|
|
|
|
2018-10-24 14:49:37 +00:00
|
|
|
acc = ak.GetAccount(sdkCtx, signer.Bytes())
|
2018-08-09 01:06:34 +00:00
|
|
|
if acc == nil {
|
|
|
|
return nil, sdk.ErrUnknownAddress(fmt.Sprintf("no account with address %s found", signer))
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
accNum := acc.GetAccountNumber()
|
|
|
|
if accNum != sig.AccountNumber {
|
|
|
|
return nil, sdk.ErrInvalidSequence(
|
|
|
|
fmt.Sprintf("invalid account number; got %d, expected %d", sig.AccountNumber, accNum))
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
accSeq := acc.GetSequence()
|
|
|
|
if accSeq != sig.Sequence {
|
|
|
|
return nil, sdk.ErrInvalidSequence(
|
|
|
|
fmt.Sprintf("invalid account sequence; got %d, expected %d", sig.Sequence, accSeq))
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
err := acc.SetSequence(accSeq + 1)
|
2018-08-09 01:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(err.Error())
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
signBytes := types.GetStdTxSignBytes(chainID, accNum, accSeq, stdTx.Fee, stdTx.GetMsgs(), stdTx.Memo)
|
|
|
|
|
2018-08-09 01:06:34 +00:00
|
|
|
// consume gas for signature verification
|
2018-08-24 15:29:10 +00:00
|
|
|
sdkCtx.GasMeter().ConsumeGas(verifySigCost, "ante signature verification")
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
if err := types.ValidateSigner(signBytes, sig.Signature, signer); err != nil {
|
2018-08-09 01:06:34 +00:00
|
|
|
return nil, sdk.ErrUnauthorized(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|