Skip to content

Commit

Permalink
Support msgs without signature in tracing grpc query
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed May 21, 2024
1 parent 35f3f79 commit 9b5396d
Showing 1 changed file with 63 additions and 7 deletions.
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()
} 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
)
}
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)
}

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)
}

// 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
)

if err != nil {
return nil, 0, status.Error(codes.Internal, err.Error())
}
Expand Down

0 comments on commit 9b5396d

Please sign in to comment.