Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable unsigned transactions in tracing #41

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,35 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to load evm config: %s", err.Error())
}
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))

txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))

for i, tx := range req.Predecessors {
ethTx := tx.AsTransaction()
msg, err := ethTx.AsMessage(signer, cfg.BaseFee)
var msg ethtypes.Message
// if tx is not unsigned, from field should be derived from signer, which can be done using AsMessage function
if !isUnsigned(ethTx) {
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
msg, err = ethTx.AsMessage(signer, cfg.BaseFee)
if err != nil {
continue
}
// tx.From = m.From().String()
skosito marked this conversation as resolved.
Show resolved Hide resolved
} else {
msg = ethtypes.NewMessage(
common.HexToAddress(tx.From),
ethTx.To(),
ethTx.Nonce(),
ethTx.Value(),
ethTx.Gas(),
new(big.Int).Set(ethTx.GasPrice()),
new(big.Int).Set(ethTx.GasFeeCap()),
new(big.Int).Set(ethTx.GasTipCap()),
ethTx.Data(),
ethTx.AccessList(),
false, // isFake
skosito marked this conversation as resolved.
Show resolved Hide resolved
)
}
if err != nil {
continue
}
Expand All @@ -454,7 +477,10 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
_ = json.Unmarshal([]byte(req.TraceConfig.TracerJsonConfig), &tracerConfig)
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}

result, _, err := k.traceTx(ctx, cfg, txConfig, signer, tx, req.TraceConfig, false, tracerConfig)
result, _, err := k.traceTx(
ctx, cfg, txConfig, common.HexToAddress(req.Msg.From), tx,
req.TraceConfig, false, tracerConfig,
)
if err != nil {
// error will be returned with detail status from traceTx
return nil, err
Expand All @@ -470,6 +496,12 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
}, nil
}

func isUnsigned(ethTx *ethtypes.Transaction) bool {
r, v, s := ethTx.RawSignatureValues()

return (r == nil && v == nil && s == nil) || (r.Cmp(big.NewInt(0)) == 0 && v.Cmp(big.NewInt(0)) == 0 && s.Cmp(big.NewInt(0)) == 0)
skosito marked this conversation as resolved.
Show resolved Hide resolved
}

// TraceBlock configures a new tracer according to the provided configuration, and
// executes the given message in the provided environment for all the transactions in the queried block.
// The return value will be tracer dependent.
Expand Down Expand Up @@ -502,7 +534,6 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
if err != nil {
return nil, status.Error(codes.Internal, "failed to load evm config")
}
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
txsLength := len(req.Txs)
results := make([]*types.TxTraceResult, 0, txsLength)

Expand All @@ -512,7 +543,10 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
ethTx := tx.AsTransaction()
txConfig.TxHash = ethTx.Hash()
txConfig.TxIndex = uint(i)
traceResult, logIndex, err := k.traceTx(ctx, cfg, txConfig, signer, ethTx, req.TraceConfig, true, nil)
traceResult, logIndex, err := k.traceTx(
ctx, cfg, txConfig, common.HexToAddress(tx.From),
ethTx, req.TraceConfig, true, nil,
)
if err != nil {
result.Error = err.Error()
} else {
Expand All @@ -537,7 +571,7 @@ func (k *Keeper) traceTx(
ctx sdk.Context,
cfg *statedb.EVMConfig,
txConfig statedb.TxConfig,
signer ethtypes.Signer,
from common.Address,
tx *ethtypes.Transaction,
traceConfig *types.TraceConfig,
commitMessage bool,
Expand All @@ -550,7 +584,29 @@ func (k *Keeper) traceTx(
err error
timeout = defaultTraceTimeout
)
msg, err := tx.AsMessage(signer, cfg.BaseFee)
// if tx is not unsigned, from field should be derived from signer, which can be done using AsMessage function
if !isUnsigned(tx) {
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
m, err := tx.AsMessage(signer, cfg.BaseFee)
if err != nil {
return nil, 0, status.Error(codes.Internal, err.Error())
}
from = common.HexToAddress(m.From().String())
}
msg := ethtypes.NewMessage(
from,
tx.To(),
tx.Nonce(),
tx.Value(),
tx.Gas(),
new(big.Int).Set(tx.GasPrice()),
new(big.Int).Set(tx.GasFeeCap()),
new(big.Int).Set(tx.GasTipCap()),
tx.Data(),
tx.AccessList(),
false, // isFake
)
skosito marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return nil, 0, status.Error(codes.Internal, err.Error())
}
Expand Down
3 changes: 3 additions & 0 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package keeper

import (
"fmt"
"math/big"

tmtypes "github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -157,6 +158,8 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, msgEth *types.MsgEthereumTx)
return nil, errorsmod.Wrap(err, "failed to load evm config")
}
ethTx := msgEth.AsTransaction()
fmt.Println("regular ethermint tx", ethTx.Hash())
skosito marked this conversation as resolved.
Show resolved Hide resolved

txConfig := k.TxConfig(ctx, ethTx.Hash())

// get the signer according to the chain rules from the config and block height
Expand Down
Loading