Skip to content

Commit 06b7a38

Browse files
authored
Merge pull request #3526 from dessaya/trace-fix
some fixes in jsonrpc debug_traceTransaction
2 parents 7782990 + 19b7158 commit 06b7a38

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

packages/evm/jsonrpc/evmchain.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
hivedb "github.com/iotaledger/hive.go/kvstore/database"
2525
"github.com/iotaledger/hive.go/logger"
2626
"github.com/iotaledger/hive.go/runtime/event"
27+
2728
"github.com/iotaledger/wasp/packages/evm/evmtypes"
2829
"github.com/iotaledger/wasp/packages/evm/evmutil"
2930
"github.com/iotaledger/wasp/packages/evm/jsonrpc/jsonrpcindex"
@@ -538,6 +539,14 @@ func (e *EVMChain) BlockTransactionCountByNumber(blockNumber *big.Int) (uint64,
538539
//nolint:gocyclo
539540
func (e *EVMChain) Logs(query *ethereum.FilterQuery, params *LogsLimits) ([]*types.Log, error) {
540541
e.log.Debugf("Logs(q=%v)", query)
542+
if query == nil {
543+
query = &ethereum.FilterQuery{}
544+
}
545+
546+
if params == nil {
547+
params = &LogsLimits{}
548+
}
549+
541550
logs := make([]*types.Log, 0)
542551

543552
// single block query
@@ -722,7 +731,11 @@ func (e *EVMChain) traceTransaction(
722731
if len(txResults) <= int(txIndex) {
723732
return nil, errors.New("tx trace not found in tracer result")
724733
}
725-
return txResults[int(txIndex)].Result, nil
734+
txTrace := txResults[int(txIndex)]
735+
if txTrace.Error != "" {
736+
return nil, errors.New(txTrace.Error)
737+
}
738+
return txTrace.Result, nil
726739
}
727740

728741
func (e *EVMChain) debugTraceBlock(config *tracers.TraceConfig, block *types.Block) (any, error) {

packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,49 @@ func TestRPCTraceTx(t *testing.T) {
610610
})
611611
}
612612

613+
func TestRPCTraceFailedTx(t *testing.T) {
614+
env := newSoloTestEnv(t)
615+
creator, creatorAddress := env.soloChain.NewEthereumAccountWithL2Funds()
616+
creatorL2Balance := env.Balance(creatorAddress)
617+
contractABI, err := abi.JSON(strings.NewReader(evmtest.ISCTestContractABI))
618+
require.NoError(t, err)
619+
_, _, contractAddress := env.DeployEVMContract(creator, contractABI, evmtest.ISCTestContractBytecode)
620+
621+
tx := types.MustSignNewTx(creator, types.NewEIP155Signer(big.NewInt(int64(env.ChainID))),
622+
&types.LegacyTx{
623+
Nonce: env.NonceAt(creatorAddress),
624+
To: &contractAddress,
625+
Value: creatorL2Balance,
626+
Gas: 100000000000000,
627+
GasPrice: big.NewInt(10000000000),
628+
Data: lo.Must(contractABI.Pack("sendTo", common.Address{0x1}, big.NewInt(1))),
629+
})
630+
631+
_, err = env.SendTransactionAndWait(tx)
632+
require.ErrorContains(t, err, "insufficient funds for gas * price + value")
633+
634+
bi := env.soloChain.GetLatestBlockInfo()
635+
require.EqualValues(t, 0, bi.NumSuccessfulRequests)
636+
637+
t.Run("callTracer", func(t *testing.T) {
638+
_, err := env.traceTransactionWithCallTracer(tx.Hash())
639+
require.ErrorContains(t, err, "expected exactly one top-level call")
640+
})
641+
642+
t.Run("prestate", func(t *testing.T) {
643+
accountMap, err := env.traceTransactionWithPrestate(tx.Hash())
644+
// t.Logf("%s", lo.Must(json.MarshalIndent(accountMap, "", " ")))
645+
require.NoError(t, err)
646+
require.NotEmpty(t, accountMap)
647+
648+
diff, err := env.traceTransactionWithPrestateDiff(tx.Hash())
649+
// t.Logf("%s", lo.Must(json.MarshalIndent(diff, "", " ")))
650+
require.NoError(t, err)
651+
require.NotEmpty(t, diff.Pre)
652+
require.Empty(t, diff.Post)
653+
})
654+
}
655+
613656
// Transfer calls produce "fake" Transactions to simulate EVM behavior.
614657
// They are not real in the sense of being persisted to the blockchain, therefore requires additional checks.
615658
func TestRPCTraceEVMDeposit(t *testing.T) {

packages/evm/jsonrpc/tracer_call.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ func (t *callTracer) OnEnter(depth int, typ byte, from common.Address, to common
179179
}
180180

181181
toCopy := to
182+
if value == nil {
183+
value = new(big.Int)
184+
}
182185
call := CallFrame{
183186
Type: NewOpCodeJSON(vm.OpCode(typ)),
184187
From: from,
@@ -274,13 +277,15 @@ func (t *callTracer) OnLog(log *types.Log) {
274277
// error arising from the encoding or forceful termination (via `Stop`).
275278
func (t *callTracer) GetResult() (json.RawMessage, error) {
276279
r := lo.Map(t.txTraces, func(tr *callTxTracer, _ int) TxTraceResult {
277-
if len(tr.frames) != 1 {
278-
panic("expected exactly 1 top-level call")
279-
}
280-
return TxTraceResult{
280+
ret := TxTraceResult{
281281
TxHash: tr.txHash,
282-
Result: lo.Must(json.Marshal(tr.frames[0])),
283282
}
283+
if len(tr.frames) != 1 {
284+
ret.Error = "expected exactly one top-level call; tx may be invalid"
285+
} else {
286+
ret.Result = lo.Must(json.Marshal(tr.frames[0]))
287+
}
288+
return ret
284289
})
285290
return json.Marshal(r)
286291
}

0 commit comments

Comments
 (0)