@@ -78,10 +78,27 @@ func (b *BlockData) GetEpochBlockData(epoch uint64) (*EpochBlockData, error) {
7878
7979 block := beaconBlock .Data
8080
81- b .extractWithdrawals (block , data .Withdrawals )
81+ b .ExtractWithdrawals (block , data .Withdrawals )
8282
8383 // Extract transaction fees
84- proposerTip , err := b .GetProposerTip (block )
84+ blockNumber := b .GetBlockNumber (block )
85+
86+ retryOpts := []retry.Option {
87+ retry .Attempts (5 ),
88+ retry .Delay (5 * time .Second ),
89+ }
90+
91+ header , err := b .getBlockHeader (blockNumber , retryOpts )
92+ if err != nil {
93+ return nil , errors .Wrap (err , "error getting block header and receipts" )
94+ }
95+ rawTxs := b .GetBlockTransactions (block )
96+ receipts , err := b .getBlockReceipts (rawTxs , retryOpts )
97+ if err != nil {
98+ return nil , errors .Wrap (err , "error getting block receipts" )
99+ }
100+
101+ proposerTip , err := b .GetProposerTip (block , header , receipts )
85102 if err != nil {
86103 return nil , errors .Wrap (err , "error getting proposer tip" )
87104 }
@@ -95,7 +112,7 @@ func (b *BlockData) GetEpochBlockData(epoch uint64) (*EpochBlockData, error) {
95112 return data , nil
96113}
97114
98- func (b * BlockData ) extractWithdrawals (beaconBlock * spec.VersionedSignedBeaconBlock , withdrawals map [uint64 ]* big.Int ) {
115+ func (b * BlockData ) ExtractWithdrawals (beaconBlock * spec.VersionedSignedBeaconBlock , withdrawals map [uint64 ]* big.Int ) {
99116 blockWithdrawals := b .GetBlockWithdrawals (beaconBlock )
100117 for _ , withdrawal := range blockWithdrawals {
101118 idx := uint64 (withdrawal .ValidatorIndex )
@@ -106,74 +123,53 @@ func (b *BlockData) extractWithdrawals(beaconBlock *spec.VersionedSignedBeaconBl
106123 }
107124}
108125
109- func (b * BlockData ) GetProposerTip (beaconBlock * spec.VersionedSignedBeaconBlock ) (* big.Int , error ) {
110- blockNumber := b .GetBlockNumber (beaconBlock )
126+ func (b * BlockData ) GetProposerTip (
127+ beaconBlock * spec.VersionedSignedBeaconBlock ,
128+ header * types.Header ,
129+ receipts []* types.Receipt ,
130+ ) (* big.Int , error ) {
111131 rawTxs := b .GetBlockTransactions (beaconBlock )
112- retryOpts := []retry.Option {
113- retry .Attempts (5 ),
114- retry .Delay (5 * time .Second ),
115- }
116- header , err := b .getBlockHeader (blockNumber , retryOpts )
117- if err != nil {
118- return nil , errors .Wrap (err , "error getting block header and receipts" )
119- }
132+
120133 baseFeePerGasBytes := b .GetBaseFeePerGas (beaconBlock )
121134 baseFeePerGas := new (big.Int ).SetBytes (baseFeePerGasBytes [:])
122135
123136 tips := big .NewInt (0 )
124137
125- var g errgroup.Group
126- var mu sync.Mutex
127- // Limit concurrent requests
128- g .SetLimit (10 )
138+ for i , rawTx := range rawTxs {
139+ var tx types.Transaction
140+ err := tx .UnmarshalBinary (rawTx )
141+ if err != nil {
142+ return nil , errors .Wrap (err , "error unmarshalling transaction" )
143+ }
144+ txReceipt := receipts [i ]
129145
130- for _ , rawTx := range rawTxs {
131- g .Go (func () error {
132- var tx types.Transaction
133- err = tx .UnmarshalBinary (rawTx )
134- if err != nil {
135- return errors .Wrap (err , "error unmarshalling transaction" )
136- }
137- txReceipt , err := b .getTransactionReceipt (& tx , retryOpts )
138- if err != nil {
139- return errors .Wrap (err , "error getting block receipt" )
140- }
141- if err != nil {
142- return errors .Wrap (err , "error unmarshalling transaction" )
143- }
144- if tx .Hash () != txReceipt .TxHash {
145- return errors .New ("transaction hash mismatch" )
146- }
146+ if tx .Hash () != txReceipt .TxHash {
147+ return nil , errors .New ("transaction hash mismatch" )
148+ }
147149
148- tipFee := new (big.Int )
149- gasPrice := tx .GasPrice ()
150- gasUsed := big .NewInt (int64 (txReceipt .GasUsed ))
151-
152- switch tx .Type () {
153- case 0 , 1 :
154- tipFee .Mul (gasPrice , gasUsed )
155- case 2 , 3 , 4 :
156- tip := new (big.Int ).Add (tx .GasTipCap (), header .BaseFee )
157- gasFeeCap := tx .GasFeeCap ()
158- var usedGasPrice * big.Int
159- if gasFeeCap .Cmp (tip ) < 0 {
160- usedGasPrice = gasFeeCap
161- } else {
162- usedGasPrice = tip
163- }
164- tipFee = new (big.Int ).Mul (usedGasPrice , gasUsed )
165- default :
166- return errors .Errorf ("unknown transaction type: %d, hash: %s" , tx .Type (), tx .Hash ().String ())
150+ tipFee := new (big.Int )
151+ gasPrice := tx .GasPrice ()
152+ gasUsed := big .NewInt (int64 (txReceipt .GasUsed ))
153+
154+ switch tx .Type () {
155+ case 0 , 1 :
156+ tipFee .Mul (gasPrice , gasUsed )
157+ case 2 , 3 , 4 :
158+ tip := new (big.Int ).Add (tx .GasTipCap (), header .BaseFee )
159+ gasFeeCap := tx .GasFeeCap ()
160+ var usedGasPrice * big.Int
161+ if gasFeeCap .Cmp (tip ) < 0 {
162+ usedGasPrice = gasFeeCap
163+ } else {
164+ usedGasPrice = tip
167165 }
168- mu .Lock ()
169- tips .Add (tips , tipFee )
170- mu .Unlock ()
171- return nil
172- })
173- }
174- if err := g .Wait (); err != nil {
175- return nil , errors .Wrap (err , "error getting proposer tip" )
166+ tipFee = new (big.Int ).Mul (usedGasPrice , gasUsed )
167+ default :
168+ return nil , errors .Errorf ("unknown transaction type: %d, hash: %s" , tx .Type (), tx .Hash ().String ())
169+ }
170+ tips .Add (tips , tipFee )
176171 }
172+
177173 burnt := new (big.Int ).Mul (big .NewInt (int64 (b .GetGasUsed (beaconBlock ))), baseFeePerGas )
178174 proposerReward := new (big.Int ).Sub (tips , burnt )
179175 return proposerReward , nil
@@ -203,6 +199,38 @@ func (b *BlockData) getBlockHeader(
203199 return header , nil
204200}
205201
202+ func (b * BlockData ) getBlockReceipts (rawTxs []bellatrix.Transaction , retryOpts []retry.Option ) ([]* types.Receipt , error ) {
203+ receipts := make ([]* types.Receipt , len (rawTxs ))
204+ var err error
205+
206+ var g errgroup.Group
207+ var mu sync.Mutex
208+ // Limit concurrent requests
209+ g .SetLimit (10 )
210+
211+ for i , rawTx := range rawTxs {
212+ g .Go (func () error {
213+ var tx types.Transaction
214+ err = tx .UnmarshalBinary (rawTx )
215+ if err != nil {
216+ return errors .Wrap (err , "error unmarshalling transaction" )
217+ }
218+ receipt , err := b .getTransactionReceipt (& tx , retryOpts )
219+ if err != nil {
220+ return errors .Wrap (err , "error getting transaction receipt" )
221+ }
222+ mu .Lock ()
223+ receipts [i ] = receipt
224+ mu .Unlock ()
225+ return nil
226+ })
227+ }
228+ if err := g .Wait (); err != nil {
229+ return nil , errors .Wrap (err , "error getting block receipts" )
230+ }
231+ return receipts , nil
232+ }
233+
206234func (b * BlockData ) getTransactionReceipt (tx * types.Transaction , retryOpts []retry.Option ) (* types.Receipt , error ) {
207235 var receipt * types.Receipt
208236 var err error
0 commit comments