Skip to content

Commit 291c84d

Browse files
committed
Fix tracer and Trace RPC calls
1 parent 3ea7745 commit 291c84d

File tree

5 files changed

+263
-128
lines changed

5 files changed

+263
-128
lines changed

packages/evm/jsonrpc/evmchain.go

Lines changed: 102 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package jsonrpc
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"math"
@@ -12,8 +13,10 @@ import (
1213

1314
"github.com/ethereum/go-ethereum"
1415
"github.com/ethereum/go-ethereum/common"
16+
"github.com/ethereum/go-ethereum/common/hexutil"
1517
"github.com/ethereum/go-ethereum/core/types"
1618
"github.com/ethereum/go-ethereum/eth/tracers"
19+
"github.com/ethereum/go-ethereum/rlp"
1720
"github.com/ethereum/go-ethereum/rpc"
1821
"github.com/labstack/gommon/log"
1922
"github.com/samber/lo"
@@ -662,43 +665,27 @@ func (e *EVMChain) iscRequestsInBlock(evmBlockNumber uint64) (*blocklog.BlockInf
662665
return blocklog.GetRequestsInBlock(blocklogStatePartition, iscBlockIndex)
663666
}
664667

665-
func (e *EVMChain) Trace(config *tracers.TraceConfig, txIndex *uint64, txHash common.Hash, blockNumber uint64, blockHash common.Hash) (any, error) {
668+
func (e *EVMChain) trace(config *tracers.TraceConfig, blockInfo *blocklog.BlockInfo, requestsInBlock []isc.Request, txIndex uint64, txHash common.Hash, blockNumber uint64, blockHash common.Hash) (json.RawMessage, error) {
666669
tracerType := "callTracer"
667670
if config.Tracer != nil {
668671
tracerType = *config.Tracer
669672
}
670673

671-
iscBlock, iscRequestsInBlock, err := e.iscRequestsInBlock(blockNumber)
672-
if err != nil {
673-
return nil, err
674-
}
675-
676-
var blockTxs types.Transactions
677-
var txi int
678-
if txIndex != nil {
679-
txi = int(*txIndex)
680-
} else {
681-
blockTxs, err = e.txsByBlockNumber(new(big.Int).SetUint64(blockNumber))
682-
if err != nil {
683-
return nil, err
684-
}
685-
}
686-
687674
tracer, err := newTracer(tracerType, &tracers.Context{
688675
BlockHash: blockHash,
689676
BlockNumber: new(big.Int).SetUint64(blockNumber),
690-
TxIndex: txi,
677+
TxIndex: int(txIndex),
691678
TxHash: txHash,
692-
}, config.TracerConfig, blockTxs)
679+
}, config.TracerConfig)
693680
if err != nil {
694681
return nil, err
695682
}
696683

697684
err = e.backend.EVMTrace(
698-
iscBlock.PreviousAliasOutput,
699-
iscBlock.Timestamp,
700-
iscRequestsInBlock,
701-
txIndex,
685+
blockInfo.PreviousAliasOutput,
686+
blockInfo.Timestamp,
687+
requestsInBlock,
688+
&txIndex,
702689
&blockNumber,
703690
tracer,
704691
)
@@ -709,6 +696,53 @@ func (e *EVMChain) Trace(config *tracers.TraceConfig, txIndex *uint64, txHash co
709696
return tracer.GetResult()
710697
}
711698

699+
func (e *EVMChain) traceTransaction(config *tracers.TraceConfig, txIndex uint64, txHash common.Hash, blockNumber uint64, blockHash common.Hash) (any, error) {
700+
iscBlock, iscRequestsInBlock, err := e.iscRequestsInBlock(blockNumber)
701+
if err != nil {
702+
return nil, err
703+
}
704+
705+
result, err := e.trace(config, iscBlock, iscRequestsInBlock, txIndex, txHash, blockNumber, blockHash)
706+
if err != nil {
707+
return nil, err
708+
}
709+
710+
return result, nil
711+
}
712+
713+
func (e *EVMChain) traceBlock(config *tracers.TraceConfig, blockNumber uint64, blockHash common.Hash) (any, error) {
714+
iscBlock, iscRequestsInBlock, err := e.iscRequestsInBlock(blockNumber)
715+
if err != nil {
716+
return nil, err
717+
}
718+
719+
blockTxs, err := e.txsByBlockNumber(new(big.Int).SetUint64(blockNumber))
720+
if err != nil {
721+
return nil, err
722+
}
723+
724+
results := make([]TxTraceResult, 0)
725+
726+
for i, tx := range blockTxs {
727+
result, err := e.trace(config, iscBlock, iscRequestsInBlock, uint64(i), tx.Hash(), blockNumber, blockHash)
728+
729+
if err == nil {
730+
results = append(results, TxTraceResult{
731+
TxHash: tx.Hash(),
732+
Result: result,
733+
})
734+
}
735+
736+
if err != nil && !errors.Is(err, ErrIncorrectTopLevelCalls) {
737+
return nil, err
738+
}
739+
740+
// Continue the loop for next TXs
741+
}
742+
743+
return results, nil
744+
}
745+
712746
func (e *EVMChain) TraceTransaction(txHash common.Hash, config *tracers.TraceConfig) (any, error) {
713747
e.log.Debugf("TraceTransaction(txHash=%v, config=?)", txHash)
714748

@@ -717,45 +751,79 @@ func (e *EVMChain) TraceTransaction(txHash common.Hash, config *tracers.TraceCon
717751
return nil, err
718752
}
719753
if blockNumber == 0 {
720-
return nil, errors.New("tx not found")
754+
return nil, errors.New("transaction not found")
721755
}
722756

723-
return e.Trace(config, &txIndex, txHash, blockNumber, blockHash)
757+
return e.traceTransaction(config, txIndex, txHash, blockNumber, blockHash)
724758
}
725759

726760
func (e *EVMChain) TraceBlockByHash(blockHash common.Hash, config *tracers.TraceConfig) (any, error) {
727761
e.log.Debugf("TraceBlockByHash(blockHash=%v, config=?)", blockHash)
728762

729763
block := e.BlockByHash(blockHash)
730764
if block == nil {
731-
return nil, errors.New("block not found")
765+
return nil, fmt.Errorf("block not found: %s", blockHash.String())
732766
}
733767

734-
return e.Trace(config, nil, common.Hash{}, block.Number().Uint64(), blockHash)
768+
return e.traceBlock(config, block.Number().Uint64(), blockHash)
735769
}
736770

737771
func (e *EVMChain) TraceBlockByNumber(blockNumber uint64, config *tracers.TraceConfig) (any, error) {
738772
e.log.Debugf("TraceBlockByNumber(blockNumber=%v, config=?)", blockNumber)
739773

740774
block, err := e.BlockByNumber(big.NewInt(int64(blockNumber)))
741775
if err != nil {
742-
return nil, fmt.Errorf("block not found: %w", err)
776+
return nil, fmt.Errorf("block not found: %d", blockNumber)
777+
}
778+
779+
return e.traceBlock(config, blockNumber, block.Hash())
780+
}
781+
782+
func (e *EVMChain) GetRawBlock(blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
783+
var block *types.Block
784+
var err error
785+
786+
if h, ok := blockNrOrHash.Hash(); ok {
787+
block = e.BlockByHash(h)
788+
} else if n, ok := blockNrOrHash.Number(); ok {
789+
block, err = e.BlockByNumber(big.NewInt(n.Int64()))
790+
791+
if err != nil {
792+
return nil, err
793+
}
794+
} else {
795+
return nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
743796
}
744797

745-
return e.Trace(config, nil, common.Hash{}, blockNumber, block.Hash())
798+
return rlp.EncodeToBytes(block)
746799
}
747800

748-
func (e *EVMChain) GetBlockReceipts(blockNumber rpc.BlockNumber) ([]*types.Receipt, []*types.Transaction, error) {
749-
e.log.Debugf("GetBlockReceipts(blockNumber=%v)", blockNumber)
750-
bn := parseBlockNumber(blockNumber)
751-
chainState, err := e.iscStateFromEVMBlockNumber(bn)
801+
func (e *EVMChain) GetBlockReceipts(blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, []*types.Transaction, error) {
802+
e.log.Debugf("GetBlockReceipts(blockNumber=%v)", blockNrOrHash.String())
803+
804+
var block *types.Block
805+
var err error
806+
807+
if h, ok := blockNrOrHash.Hash(); ok {
808+
block = e.BlockByHash(h)
809+
} else if n, ok := blockNrOrHash.Number(); ok {
810+
block, err = e.BlockByNumber(parseBlockNumber(n))
811+
812+
if err != nil {
813+
return nil, nil, err
814+
}
815+
} else {
816+
return nil, nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
817+
}
818+
819+
chainState, err := e.iscStateFromEVMBlockNumberOrHash(&blockNrOrHash)
752820
if err != nil {
753821
return nil, nil, err
754822
}
755823

756824
db := blockchainDB(chainState)
757825

758-
return db.GetReceiptsByBlockNumber(bn.Uint64()), db.GetTransactionsByBlockNumber(bn.Uint64()), nil
826+
return db.GetReceiptsByBlockNumber(block.NumberU64()), db.GetTransactionsByBlockNumber(block.NumberU64()), nil
759827
}
760828

761829
var maxUint32 = big.NewInt(math.MaxUint32)

0 commit comments

Comments
 (0)