Skip to content

Commit af70677

Browse files
james-prysmrkapka
andauthored
Deneb: Produce Block V3 - adding consensus block value (#12948)
* adding in block rewards to represent consensus payload * Update beacon-chain/rpc/eth/validator/handlers_block.go Co-authored-by: Radosław Kapka <[email protected]> * radek's comments * more review changes * adding more tests for forks * gaz * updating names * gaz * fixing imports * fixing variable name * gaz * fixing test * renaming variables to match data --------- Co-authored-by: Radosław Kapka <[email protected]>
1 parent 8eb82dd commit af70677

File tree

17 files changed

+547
-158
lines changed

17 files changed

+547
-158
lines changed

api/headers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const (
44
VersionHeader = "Eth-Consensus-Version"
55
ExecutionPayloadBlindedHeader = "Eth-Execution-Payload-Blinded"
66
ExecutionPayloadValueHeader = "Eth-Execution-Payload-Value"
7+
ConsensusBlockValueHeader = "Eth-Consensus-Block-Value"
78
JsonMediaType = "application/json"
89
OctetStreamMediaType = "application/octet-stream"
910
)

beacon-chain/rpc/eth/rewards/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"handlers.go",
77
"server.go",
8+
"service.go",
89
"structs.go",
910
],
1011
importpath = "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/rewards",
@@ -21,11 +22,13 @@ go_library(
2122
"//beacon-chain/state/stategen:go_default_library",
2223
"//config/fieldparams:go_default_library",
2324
"//config/params:go_default_library",
25+
"//consensus-types/interfaces:go_default_library",
2426
"//consensus-types/primitives:go_default_library",
2527
"//network/http:go_default_library",
2628
"//runtime/version:go_default_library",
2729
"//time/slots:go_default_library",
2830
"@com_github_wealdtech_go_bytesutil//:go_default_library",
31+
"@io_opencensus_go//trace:go_default_library",
2932
],
3033
)
3134

@@ -54,6 +57,7 @@ go_test(
5457
"//testing/assert:go_default_library",
5558
"//testing/require:go_default_library",
5659
"//testing/util:go_default_library",
60+
"@com_github_pkg_errors//:go_default_library",
5761
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
5862
],
5963
)

beacon-chain/rpc/eth/rewards/handlers.go

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
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.
2625
func (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.
167108
func (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

Comments
 (0)