88 "strings"
99
1010 "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/altair"
11- coreblocks "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/blocks"
1211 "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/epoch/precompute"
13- "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/validators"
1412 "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared"
1513 "github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
1614 fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
@@ -20,10 +18,13 @@ import (
2018 "github.com/prysmaticlabs/prysm/v4/runtime/version"
2119 "github.com/prysmaticlabs/prysm/v4/time/slots"
2220 "github.com/wealdtech/go-bytesutil"
21+ "go.opencensus.io/trace"
2322)
2423
2524// BlockRewards is an HTTP handler for Beacon API getBlockRewards.
2625func (s * Server ) BlockRewards (w http.ResponseWriter , r * http.Request ) {
26+ ctx , span := trace .StartSpan (r .Context (), "beacon.BlockRewards" )
27+ defer span .End ()
2728 segments := strings .Split (r .URL .Path , "/" )
2829 blockId := segments [len (segments )- 1 ]
2930
@@ -36,63 +37,6 @@ func (s *Server) BlockRewards(w http.ResponseWriter, r *http.Request) {
3637 return
3738 }
3839
39- // We want to run several block processing functions that update the proposer's balance.
40- // This will allow us to calculate proposer rewards for each operation (atts, slashings etc).
41- // To do this, we replay the state up to the block's slot, but before processing the block.
42- st , err := s .ReplayerBuilder .ReplayerForSlot (blk .Block ().Slot ()- 1 ).ReplayToSlot (r .Context (), blk .Block ().Slot ())
43- if err != nil {
44- http2 .HandleError (w , "Could not get state: " + err .Error (), http .StatusInternalServerError )
45- return
46- }
47-
48- proposerIndex := blk .Block ().ProposerIndex ()
49- initBalance , err := st .BalanceAtIndex (proposerIndex )
50- if err != nil {
51- http2 .HandleError (w , "Could not get proposer's balance: " + err .Error (), http .StatusInternalServerError )
52- return
53- }
54- st , err = altair .ProcessAttestationsNoVerifySignature (r .Context (), st , blk )
55- if err != nil {
56- http2 .HandleError (w , "Could not get attestation rewards" + err .Error (), http .StatusInternalServerError )
57- return
58- }
59- attBalance , err := st .BalanceAtIndex (proposerIndex )
60- if err != nil {
61- http2 .HandleError (w , "Could not get proposer's balance: " + err .Error (), http .StatusInternalServerError )
62- return
63- }
64- st , err = coreblocks .ProcessAttesterSlashings (r .Context (), st , blk .Block ().Body ().AttesterSlashings (), validators .SlashValidator )
65- if err != nil {
66- http2 .HandleError (w , "Could not get attester slashing rewards: " + err .Error (), http .StatusInternalServerError )
67- return
68- }
69- attSlashingsBalance , err := st .BalanceAtIndex (proposerIndex )
70- if err != nil {
71- http2 .HandleError (w , "Could not get proposer's balance: " + err .Error (), http .StatusInternalServerError )
72- return
73- }
74- st , err = coreblocks .ProcessProposerSlashings (r .Context (), st , blk .Block ().Body ().ProposerSlashings (), validators .SlashValidator )
75- if err != nil {
76- http2 .HandleError (w , "Could not get proposer slashing rewards" + err .Error (), http .StatusInternalServerError )
77- return
78- }
79- proposerSlashingsBalance , err := st .BalanceAtIndex (proposerIndex )
80- if err != nil {
81- http2 .HandleError (w , "Could not get proposer's balance: " + err .Error (), http .StatusInternalServerError )
82- return
83- }
84- sa , err := blk .Block ().Body ().SyncAggregate ()
85- if err != nil {
86- http2 .HandleError (w , "Could not get sync aggregate: " + err .Error (), http .StatusInternalServerError )
87- return
88- }
89- var syncCommitteeReward uint64
90- _ , syncCommitteeReward , err = altair .ProcessSyncAggregate (r .Context (), st , sa )
91- if err != nil {
92- http2 .HandleError (w , "Could not get sync aggregate rewards: " + err .Error (), http .StatusInternalServerError )
93- return
94- }
95-
9640 optimistic , err := s .OptimisticModeFetcher .IsOptimistic (r .Context ())
9741 if err != nil {
9842 http2 .HandleError (w , "Could not get optimistic mode info: " + err .Error (), http .StatusInternalServerError )
@@ -103,18 +47,15 @@ func (s *Server) BlockRewards(w http.ResponseWriter, r *http.Request) {
10347 http2 .HandleError (w , "Could not get block root: " + err .Error (), http .StatusInternalServerError )
10448 return
10549 }
106-
50+ blockRewards , httpError := s .BlockRewardFetcher .GetBlockRewardsData (ctx , blk )
51+ if httpError != nil {
52+ http2 .WriteError (w , httpError )
53+ return
54+ }
10755 response := & BlockRewardsResponse {
108- Data : BlockRewards {
109- ProposerIndex : strconv .FormatUint (uint64 (proposerIndex ), 10 ),
110- Total : strconv .FormatUint (proposerSlashingsBalance - initBalance + syncCommitteeReward , 10 ),
111- Attestations : strconv .FormatUint (attBalance - initBalance , 10 ),
112- SyncAggregate : strconv .FormatUint (syncCommitteeReward , 10 ),
113- ProposerSlashings : strconv .FormatUint (proposerSlashingsBalance - attSlashingsBalance , 10 ),
114- AttesterSlashings : strconv .FormatUint (attSlashingsBalance - attBalance , 10 ),
115- },
56+ Data : blockRewards ,
11657 ExecutionOptimistic : optimistic ,
117- Finalized : s .FinalizationFetcher .IsFinalized (r . Context () , blkRoot ),
58+ Finalized : s .FinalizationFetcher .IsFinalized (ctx , blkRoot ),
11859 }
11960 http2 .WriteJson (w , response )
12061}
@@ -165,6 +106,8 @@ func (s *Server) AttestationRewards(w http.ResponseWriter, r *http.Request) {
165106// SyncCommitteeRewards retrieves rewards info for sync committee members specified by array of public keys or validator index.
166107// If no array is provided, return reward info for every committee member.
167108func (s * Server ) SyncCommitteeRewards (w http.ResponseWriter , r * http.Request ) {
109+ ctx , span := trace .StartSpan (r .Context (), "beacon.SyncCommitteeRewards" )
110+ defer span .End ()
168111 segments := strings .Split (r .URL .Path , "/" )
169112 blockId := segments [len (segments )- 1 ]
170113
@@ -176,9 +119,10 @@ func (s *Server) SyncCommitteeRewards(w http.ResponseWriter, r *http.Request) {
176119 http2 .HandleError (w , "Sync committee rewards are not supported for Phase 0" , http .StatusBadRequest )
177120 return
178121 }
179- st , err := s .ReplayerBuilder .ReplayerForSlot (blk .Block ().Slot ()- 1 ).ReplayToSlot (r .Context (), blk .Block ().Slot ())
180- if err != nil {
181- http2 .HandleError (w , "Could not get state: " + err .Error (), http .StatusInternalServerError )
122+
123+ st , httpErr := s .BlockRewardFetcher .GetStateForRewards (ctx , blk )
124+ if httpErr != nil {
125+ http2 .WriteError (w , httpErr )
182126 return
183127 }
184128 sa , err := blk .Block ().Body ().SyncAggregate ()
0 commit comments