@@ -112,21 +112,101 @@ func GetBlockRangeInPages(ctx context.Context, from, to, pageSize uint64, c *eth
112112 return allBlocks , nil
113113}
114114
115+ var getReceiptsByBlockIsSupported * bool
116+
115117func GetReceipts (ctx context.Context , rawBlocks []* json.RawMessage , c * ethrpc.Client , batchSize uint64 ) ([]* json.RawMessage , error ) {
118+ if getReceiptsByBlockIsSupported == nil {
119+ err := c .CallContext (ctx , nil , "eth_getBlockReceipts" , "0x0" )
120+ supported := err == nil
121+ getReceiptsByBlockIsSupported = & supported
122+ }
123+
124+ if getReceiptsByBlockIsSupported != nil && * getReceiptsByBlockIsSupported {
125+ return getReceiptsByBlock (ctx , rawBlocks , c , batchSize )
126+ }
127+
128+ return getReceiptsByTx (ctx , rawBlocks , c , batchSize )
129+ }
130+
131+ func getReceiptsByBlock (ctx context.Context , rawBlocks []* json.RawMessage , c * ethrpc.Client , batchSize uint64 ) ([]* json.RawMessage , error ) {
132+ var startBlock * string
133+ batchElements := make ([]ethrpc.BatchElem , 0 , len (rawBlocks ))
134+ for _ , rawBlock := range rawBlocks {
135+ var block simpleRPCBlock
136+ err := json .Unmarshal (* rawBlock , & block )
137+ if err != nil {
138+ return nil , err
139+ }
140+ batchElements = append (batchElements , ethrpc.BatchElem {
141+ Method : "eth_getBlockReceipts" ,
142+ Args : []interface {}{block .Number },
143+ Result : new ([]* json.RawMessage ),
144+ })
145+ if startBlock == nil {
146+ startBlock = & block .Number
147+ }
148+ }
149+ if len (batchElements ) == 0 {
150+ log .Debug ().Int ("Length of BatchElem" , len (batchElements )).Msg ("BatchElem is empty" )
151+ return nil , nil
152+ }
153+
154+ var start uint64 = 0
155+ for {
156+ last := false
157+ end := start + batchSize
158+ if int (end ) >= len (batchElements ) {
159+ last = true
160+ end = uint64 (len (batchElements ))
161+ }
162+
163+ log .Trace ().Str ("startblock" , * startBlock ).Uint64 ("start" , start ).Uint64 ("end" , end ).Msg ("Fetching receipt range" )
164+ err := c .BatchCallContext (ctx , batchElements [start :end ])
165+ if err != nil {
166+ log .Error ().Err (err ).Uint64 ("start" , start ).Uint64 ("end" , end ).Msg ("RPC issue fetching receipts, have you checked the batch size limit of the RPC endpoint and adjusted the --batch-size flag?" )
167+ break
168+ }
169+ start = end
170+ if last {
171+ break
172+ }
173+ }
174+
175+ receipts := make ([]* json.RawMessage , 0 )
176+ for _ , b := range batchElements {
177+ if b .Error != nil {
178+ log .Error ().Err (b .Error ).
179+ Interface ("blockNumber" , b .Args [0 ]).
180+ Msg ("Block response err" )
181+ return nil , b .Error
182+ }
183+ if b .Result == nil || reflect .ValueOf (b .Result ).IsNil () {
184+ continue
185+ }
186+ rs := * (b .Result .(* []* json.RawMessage ))
187+ receipts = append (receipts , rs ... )
188+ }
189+ if len (receipts ) == 0 {
190+ log .Info ().Msg ("No receipts have been fetched" )
191+ return nil , nil
192+ }
193+ log .Info ().Int ("blocks" , len (rawBlocks )).Int ("receipts" , len (receipts )).Msg ("Fetched tx receipts" )
194+ return receipts , nil
195+ }
196+
197+ func getReceiptsByTx (ctx context.Context , rawBlocks []* json.RawMessage , c * ethrpc.Client , batchSize uint64 ) ([]* json.RawMessage , error ) {
116198 txHashes := make ([]string , 0 )
117199 txHashMap := make (map [string ]string , 0 )
118200 for _ , rb := range rawBlocks {
119201 var block simpleRPCBlock
120202 err := json .Unmarshal (* rb , & block )
121203 if err != nil {
122204 return nil , err
123-
124205 }
125206 for _ , tx := range block .Transactions {
126207 txHashes = append (txHashes , tx .Hash )
127208 txHashMap [tx .Hash ] = block .Number
128209 }
129-
130210 }
131211 if len (txHashes ) == 0 {
132212 return nil , nil
@@ -199,7 +279,7 @@ func GetReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Cl
199279 receipts = append (receipts , b .Result .(* json.RawMessage ))
200280 }
201281 if len (receipts ) == 0 {
202- log .Error ().Msg ("No receipts have been fetched" )
282+ log .Info ().Msg ("No receipts have been fetched" )
203283 return nil , nil
204284 }
205285 log .Info ().Int ("hashes" , len (txHashes )).Int ("receipts" , len (receipts )).Msg ("Fetched tx receipts" )
0 commit comments