4
4
package jsonrpc
5
5
6
6
import (
7
+ "encoding/json"
7
8
"errors"
8
9
"fmt"
9
10
"math"
@@ -12,8 +13,10 @@ import (
12
13
13
14
"github.com/ethereum/go-ethereum"
14
15
"github.com/ethereum/go-ethereum/common"
16
+ "github.com/ethereum/go-ethereum/common/hexutil"
15
17
"github.com/ethereum/go-ethereum/core/types"
16
18
"github.com/ethereum/go-ethereum/eth/tracers"
19
+ "github.com/ethereum/go-ethereum/rlp"
17
20
"github.com/ethereum/go-ethereum/rpc"
18
21
"github.com/labstack/gommon/log"
19
22
"github.com/samber/lo"
@@ -662,43 +665,27 @@ func (e *EVMChain) iscRequestsInBlock(evmBlockNumber uint64) (*blocklog.BlockInf
662
665
return blocklog .GetRequestsInBlock (blocklogStatePartition , iscBlockIndex )
663
666
}
664
667
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 ) {
666
669
tracerType := "callTracer"
667
670
if config .Tracer != nil {
668
671
tracerType = * config .Tracer
669
672
}
670
673
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
-
687
674
tracer , err := newTracer (tracerType , & tracers.Context {
688
675
BlockHash : blockHash ,
689
676
BlockNumber : new (big.Int ).SetUint64 (blockNumber ),
690
- TxIndex : txi ,
677
+ TxIndex : int ( txIndex ) ,
691
678
TxHash : txHash ,
692
- }, config .TracerConfig , blockTxs )
679
+ }, config .TracerConfig )
693
680
if err != nil {
694
681
return nil , err
695
682
}
696
683
697
684
err = e .backend .EVMTrace (
698
- iscBlock .PreviousAliasOutput ,
699
- iscBlock .Timestamp ,
700
- iscRequestsInBlock ,
701
- txIndex ,
685
+ blockInfo .PreviousAliasOutput ,
686
+ blockInfo .Timestamp ,
687
+ requestsInBlock ,
688
+ & txIndex ,
702
689
& blockNumber ,
703
690
tracer ,
704
691
)
@@ -709,6 +696,53 @@ func (e *EVMChain) Trace(config *tracers.TraceConfig, txIndex *uint64, txHash co
709
696
return tracer .GetResult ()
710
697
}
711
698
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
+
712
746
func (e * EVMChain ) TraceTransaction (txHash common.Hash , config * tracers.TraceConfig ) (any , error ) {
713
747
e .log .Debugf ("TraceTransaction(txHash=%v, config=?)" , txHash )
714
748
@@ -717,45 +751,79 @@ func (e *EVMChain) TraceTransaction(txHash common.Hash, config *tracers.TraceCon
717
751
return nil , err
718
752
}
719
753
if blockNumber == 0 {
720
- return nil , errors .New ("tx not found" )
754
+ return nil , errors .New ("transaction not found" )
721
755
}
722
756
723
- return e .Trace (config , & txIndex , txHash , blockNumber , blockHash )
757
+ return e .traceTransaction (config , txIndex , txHash , blockNumber , blockHash )
724
758
}
725
759
726
760
func (e * EVMChain ) TraceBlockByHash (blockHash common.Hash , config * tracers.TraceConfig ) (any , error ) {
727
761
e .log .Debugf ("TraceBlockByHash(blockHash=%v, config=?)" , blockHash )
728
762
729
763
block := e .BlockByHash (blockHash )
730
764
if block == nil {
731
- return nil , errors . New ("block not found" )
765
+ return nil , fmt . Errorf ("block not found: %s" , blockHash . String () )
732
766
}
733
767
734
- return e .Trace (config , nil , common. Hash {} , block .Number ().Uint64 (), blockHash )
768
+ return e .traceBlock (config , block .Number ().Uint64 (), blockHash )
735
769
}
736
770
737
771
func (e * EVMChain ) TraceBlockByNumber (blockNumber uint64 , config * tracers.TraceConfig ) (any , error ) {
738
772
e .log .Debugf ("TraceBlockByNumber(blockNumber=%v, config=?)" , blockNumber )
739
773
740
774
block , err := e .BlockByNumber (big .NewInt (int64 (blockNumber )))
741
775
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 ())
743
796
}
744
797
745
- return e . Trace ( config , nil , common. Hash {}, blockNumber , block . Hash () )
798
+ return rlp . EncodeToBytes ( block )
746
799
}
747
800
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 )
752
820
if err != nil {
753
821
return nil , nil , err
754
822
}
755
823
756
824
db := blockchainDB (chainState )
757
825
758
- return db .GetReceiptsByBlockNumber (bn . Uint64 ()), db .GetTransactionsByBlockNumber (bn . Uint64 ()), nil
826
+ return db .GetReceiptsByBlockNumber (block . NumberU64 ()), db .GetTransactionsByBlockNumber (block . NumberU64 ()), nil
759
827
}
760
828
761
829
var maxUint32 = big .NewInt (math .MaxUint32 )
0 commit comments