Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caplin: Fixed up validator's attestation rate during epoch slots #12377

Open
wants to merge 105 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
961b44a
save
Giulio2002 Oct 19, 2024
e445eb6
save
Giulio2002 Oct 19, 2024
684953e
save
Giulio2002 Oct 19, 2024
328bf17
save
Giulio2002 Oct 19, 2024
4c39f66
save
Giulio2002 Oct 19, 2024
d4b593f
save
Giulio2002 Oct 19, 2024
07bb119
save
Giulio2002 Oct 19, 2024
c92cd47
save
Giulio2002 Oct 19, 2024
3193906
save
Giulio2002 Oct 19, 2024
64dccf1
save
Giulio2002 Oct 19, 2024
a829c5a
save
Giulio2002 Oct 19, 2024
c5bb858
save
Giulio2002 Oct 19, 2024
ca828d5
save
Giulio2002 Oct 19, 2024
97a3d76
save
Giulio2002 Oct 19, 2024
a62a186
save
Giulio2002 Oct 19, 2024
c4a0771
save
Giulio2002 Oct 19, 2024
c67a69a
save
Giulio2002 Oct 19, 2024
22b8957
save
Giulio2002 Oct 19, 2024
c1108e4
save
Giulio2002 Oct 19, 2024
4bd4774
save
Giulio2002 Oct 19, 2024
2af1354
save
Giulio2002 Oct 19, 2024
65b7c12
save
Giulio2002 Oct 19, 2024
599f157
save
Giulio2002 Oct 19, 2024
aa018a3
save
Giulio2002 Oct 19, 2024
a3ab725
save
Giulio2002 Oct 19, 2024
6d1612c
save
Giulio2002 Oct 19, 2024
516a109
save
Giulio2002 Oct 19, 2024
c8e09cd
save
Giulio2002 Oct 19, 2024
df27bf1
save
Giulio2002 Oct 19, 2024
98ad74f
save
Giulio2002 Oct 19, 2024
c7be3d0
save
Giulio2002 Oct 19, 2024
0c90b7a
save
Giulio2002 Oct 19, 2024
53593a3
save
Giulio2002 Oct 19, 2024
585de84
save
Giulio2002 Oct 20, 2024
35f94f8
save
Giulio2002 Oct 20, 2024
b628b06
save
Giulio2002 Oct 20, 2024
6a1e214
save
Giulio2002 Oct 20, 2024
8032a85
save
Giulio2002 Oct 20, 2024
90799b3
save
Giulio2002 Oct 20, 2024
c232fd8
save
Giulio2002 Oct 20, 2024
60c4d66
save
Giulio2002 Oct 20, 2024
8113f8f
save
Giulio2002 Oct 20, 2024
90745fe
save
Giulio2002 Oct 20, 2024
7981611
save
Giulio2002 Oct 20, 2024
9d5d814
save
Giulio2002 Oct 20, 2024
17defe5
save
Giulio2002 Oct 20, 2024
6acaf66
save
Giulio2002 Oct 20, 2024
8661499
save
Giulio2002 Oct 20, 2024
9e32a56
save
Giulio2002 Oct 20, 2024
608cd8e
save
Giulio2002 Oct 20, 2024
278b9ab
save
Giulio2002 Oct 20, 2024
1151314
save
Giulio2002 Oct 20, 2024
b3d347a
save
Giulio2002 Oct 20, 2024
0d20182
save
Giulio2002 Oct 20, 2024
92c7339
save
Giulio2002 Oct 20, 2024
b86b458
save
Giulio2002 Oct 20, 2024
36f6ad4
save
Giulio2002 Oct 20, 2024
2c168d2
save
Giulio2002 Oct 20, 2024
7ac4dec
save
Giulio2002 Oct 20, 2024
a547547
save
Giulio2002 Oct 20, 2024
49b134e
save
Giulio2002 Oct 20, 2024
1d1c4c9
save
Giulio2002 Oct 21, 2024
0b4655d
save
Giulio2002 Oct 21, 2024
65e5549
save
Giulio2002 Oct 21, 2024
b4f0f77
save
Giulio2002 Oct 21, 2024
70509b2
save
Giulio2002 Oct 21, 2024
52c8621
save
Giulio2002 Oct 21, 2024
64e23a7
save
Giulio2002 Oct 21, 2024
00b9152
save
Giulio2002 Oct 21, 2024
517a598
save
Giulio2002 Oct 21, 2024
a49f597
save
Giulio2002 Oct 21, 2024
389c283
save
Giulio2002 Oct 21, 2024
572a9d1
save
Giulio2002 Oct 21, 2024
056809d
save
Giulio2002 Oct 21, 2024
8df254d
save
Giulio2002 Oct 21, 2024
b86dd17
save
Giulio2002 Oct 21, 2024
4fb5456
save
Giulio2002 Oct 21, 2024
45757a6
save
Giulio2002 Oct 21, 2024
7689fb0
save
Giulio2002 Oct 21, 2024
5fc08cf
save
Giulio2002 Oct 21, 2024
7391485
save
Giulio2002 Oct 21, 2024
e2fc397
save
Giulio2002 Oct 21, 2024
874c4c3
Merge remote-tracking branch 'origin/main' into fix_holesky
Giulio2002 Oct 21, 2024
343db4a
save
Giulio2002 Oct 21, 2024
7fee400
fix tests
Giulio2002 Oct 21, 2024
3c17975
save
Giulio2002 Oct 22, 2024
b282d25
save
Giulio2002 Oct 22, 2024
18d0534
save
Giulio2002 Oct 22, 2024
c873f5f
save
Giulio2002 Oct 22, 2024
80056a9
save
Giulio2002 Oct 22, 2024
1592838
save
Giulio2002 Oct 22, 2024
b89f67c
Update cl/beacon/handler/handler.go
domiwei Oct 23, 2024
5aeb41d
Update cl/beacon/handler/handler.go
domiwei Oct 23, 2024
65acf65
save
Giulio2002 Oct 23, 2024
4137bb4
Merge branch 'fix_holesky' of https://github.com/erigontech/erigon in…
Giulio2002 Oct 23, 2024
1811556
save
Giulio2002 Oct 23, 2024
c7568fb
save
Giulio2002 Oct 23, 2024
0d0e81b
Update cl/phase1/stages/forkchoice.go
Giulio2002 Oct 23, 2024
897e610
save
Giulio2002 Oct 23, 2024
1edbc64
Merge branch 'fix_holesky' of https://github.com/erigontech/erigon in…
Giulio2002 Oct 23, 2024
2b5da68
save
Giulio2002 Oct 23, 2024
2cbf143
save
Giulio2002 Oct 24, 2024
d539495
save
Giulio2002 Oct 24, 2024
fd010ac
save
Giulio2002 Oct 25, 2024
615da81
save
Giulio2002 Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cl/beacon/handler/attestation_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
if err != nil {
return nil, err
}
_, headSlot, err := a.forkchoiceStore.GetHead()
_, headSlot, statusCode, err := a.getHead()
if err != nil {
return nil, err
return nil, beaconhttp.NewEndpointError(statusCode, err)
}
headEpoch := headSlot / a.beaconChainCfg.SlotsPerEpoch
if epoch > headEpoch {
Expand Down
74 changes: 59 additions & 15 deletions cl/beacon/handler/block_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/erigontech/erigon/cl/abstract"
"github.com/erigontech/erigon/cl/beacon/beaconhttp"
"github.com/erigontech/erigon/cl/beacon/builder"
"github.com/erigontech/erigon/cl/beacon/synced_data"
"github.com/erigontech/erigon/cl/clparams"
"github.com/erigontech/erigon/cl/cltypes"
"github.com/erigontech/erigon/cl/cltypes/solid"
Expand Down Expand Up @@ -75,6 +76,42 @@ var (

var defaultGraffitiString = "Caplin"

const missedTimeout = 500 * time.Millisecond

func (a *ApiHandler) waitUntilHeadStateAtEpochIsReadyOrCountAsMissed(ctx context.Context, syncedData synced_data.SyncedData, epoch uint64) error {
timer := time.NewTimer(missedTimeout)
checkIfSlotIsThere := func() (bool, error) {
tx, err := a.indiciesDB.BeginRo(ctx)
if err != nil {
return false, err
}
defer tx.Rollback()
blockRoot, err := beacon_indicies.ReadCanonicalBlockRoot(tx, epoch*a.beaconChainCfg.SlotsPerEpoch)
if err != nil {
return false, err
}
return blockRoot != (libcommon.Hash{}), nil
}

defer timer.Stop()
for {
select {
case <-timer.C:
return nil
case <-ctx.Done():
return fmt.Errorf("waiting for head state to reach slot %d: %w", epoch, ctx.Err())
default:
}
ready, err := checkIfSlotIsThere()
if err != nil {
return err
}
if ready {
return nil
}
time.Sleep(30 * time.Millisecond)
}
}
func (a *ApiHandler) GetEthV1ValidatorAttestationData(
w http.ResponseWriter,
r *http.Request,
Expand All @@ -83,6 +120,24 @@ func (a *ApiHandler) GetEthV1ValidatorAttestationData(
if err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, err)
}
// wait until the head state is at the target slot or later
err = a.waitUntilHeadStateAtEpochIsReadyOrCountAsMissed(r.Context(), a.syncedData, *slot/a.beaconChainCfg.SlotsPerEpoch)
if err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusServiceUnavailable, err)
}
tx, err := a.indiciesDB.BeginRo(r.Context())
if err != nil {
return nil, err
}
defer tx.Rollback()
headState := a.syncedData.HeadState()
if headState == nil {
return nil, beaconhttp.NewEndpointError(
http.StatusServiceUnavailable,
errors.New("beacon node is still syncing"),
)
}

committeeIndex, err := beaconhttp.Uint64FromQueryParams(r, "committee_index")
if err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, err)
Expand All @@ -105,16 +160,10 @@ func (a *ApiHandler) GetEthV1ValidatorAttestationData(
committeeIndex = &zero
}

headState := a.syncedData.HeadState()
if headState == nil {
return nil, beaconhttp.NewEndpointError(
http.StatusServiceUnavailable,
errors.New("beacon node is still syncing"),
)
}

attestationData, err := a.attestationProducer.ProduceAndCacheAttestationData(
tx,
headState,
a.syncedData.HeadRoot(),
*slot,
*committeeIndex,
)
Expand Down Expand Up @@ -188,12 +237,7 @@ func (a *ApiHandler) GetEthV3ValidatorBlock(
)
}

baseBlockRoot, err := s.BlockRoot()
if err != nil {
log.Warn("Failed to get block root", "err", err)
return nil, err
}

baseBlockRoot := a.syncedData.HeadRoot()
sourceBlock, err := a.blockReader.ReadBlockByRoot(ctx, tx, baseBlockRoot)
if err != nil {
log.Warn("Failed to get source block", "err", err, "root", baseBlockRoot)
Expand Down Expand Up @@ -1161,7 +1205,7 @@ func (a *ApiHandler) findBestAttestationsForBlockProduction(
for _, att := range atts {
expectedReward, err := computeAttestationReward(s, att)
if err != nil {
log.Warn("[Block Production] Could not compute expected attestation reward", "reason", err)
log.Debug("[Block Production] Could not compute expected attestation reward", "reason", err)
continue
}
if expectedReward == 0 {
Expand Down
11 changes: 4 additions & 7 deletions cl/beacon/handler/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ type headerResponse struct {
Header *cltypes.SignedBeaconBlockHeader `json:"header"`
}

type getHeadersRequest struct {
Slot *uint64 `json:"slot,omitempty,string"`
ParentRoot *libcommon.Hash `json:"root,omitempty"`
}

func (a *ApiHandler) rootFromBlockId(ctx context.Context, tx kv.Tx, blockId *beaconhttp.SegmentID) (root libcommon.Hash, err error) {
switch {
case blockId.Head():
root, _, err = a.forkchoiceStore.GetHead()
var statusCode int
root, _, statusCode, err = a.getHead()
if err != nil {
return libcommon.Hash{}, err
return libcommon.Hash{}, beaconhttp.NewEndpointError(statusCode, err)
}
case blockId.Finalized():
root = a.forkchoiceStore.FinalizedCheckpoint().Root
Expand Down Expand Up @@ -201,6 +197,7 @@ func (a *ApiHandler) GetEthV1BeaconBlockRoot(w http.ResponseWriter, r *http.Requ
return nil, err
}
isOptimistic := a.forkchoiceStore.IsRootOptimistic(root)

// check if the root exist
slot, err := beacon_indicies.ReadBlockSlotByBlockRoot(tx, root)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cl/beacon/handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func (a *ApiHandler) GetEth1V1BuilderStatesExpectedWithdrawals(w http.ResponseWr
if a.beaconChainCfg.GetCurrentStateVersion(*slot/a.beaconChainCfg.SlotsPerEpoch) < clparams.CapellaVersion {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, errors.New("the specified state is not a capella state"))
}
headRoot, _, err := a.forkchoiceStore.GetHead()
headRoot, _, statusCode, err := a.getHead()
if err != nil {
return nil, err
return nil, beaconhttp.NewEndpointError(statusCode, err)
}

if a.syncedData.Syncing() {
return nil, beaconhttp.NewEndpointError(http.StatusServiceUnavailable, errors.New("beacon node is syncing"))
}
Expand Down
8 changes: 4 additions & 4 deletions cl/beacon/handler/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func (a *ApiHandler) GetEthV2DebugBeaconHeads(w http.ResponseWriter, r *http.Req
if a.syncedData.Syncing() {
return nil, beaconhttp.NewEndpointError(http.StatusServiceUnavailable, errors.New("beacon node is syncing"))
}
hash, slotNumber, err := a.forkchoiceStore.GetHead()
root, slot, statusCode, err := a.getHead()
if err != nil {
return nil, err
return nil, beaconhttp.NewEndpointError(statusCode, err)
}
return newBeaconResponse(
[]interface{}{
map[string]interface{}{
"slot": strconv.FormatUint(slotNumber, 10),
"root": hash,
"slot": strconv.FormatUint(slot, 10),
"root": root,
"execution_optimistic": false,
},
}), nil
Expand Down
18 changes: 18 additions & 0 deletions cl/beacon/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package handler

import (
"errors"
"net/http"
"sync"

Expand Down Expand Up @@ -107,6 +108,7 @@ type ApiHandler struct {
proposerSlashingService services.ProposerSlashingService
builderClient builder.BuilderClient
validatorsMonitor monitor.ValidatorMonitor
enableMemoizedHeadState bool
}

func NewApiHandler(
Expand Down Expand Up @@ -141,6 +143,7 @@ func NewApiHandler(
proposerSlashingService services.ProposerSlashingService,
builderClient builder.BuilderClient,
validatorMonitor monitor.ValidatorMonitor,
enableMemoizedHeadState bool,
) *ApiHandler {
blobBundles, err := lru.New[common.Bytes48, BlobBundle]("blobs", maxBlobBundleCacheSize)
if err != nil {
Expand Down Expand Up @@ -183,6 +186,7 @@ func NewApiHandler(
proposerSlashingService: proposerSlashingService,
builderClient: builderClient,
validatorsMonitor: validatorMonitor,
enableMemoizedHeadState: enableMemoizedHeadState,
}
}

Expand Down Expand Up @@ -352,3 +356,17 @@ func (a *ApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
a.mux.ServeHTTP(w, r)
}

func (a *ApiHandler) getHead() (common.Hash, uint64, int, error) {
if a.enableMemoizedHeadState {
if a.syncedData.Syncing() {
return common.Hash{}, 0, http.StatusServiceUnavailable, errors.New("beacon node is syncing")
}
return a.syncedData.HeadRoot(), a.syncedData.HeadSlot(), 0, nil
}
blockRoot, blockSlot, err := a.forkchoiceStore.GetHead(nil)
if err != nil {
return common.Hash{}, 0, http.StatusInternalServerError, err
}
return blockRoot, blockSlot, 0, nil
}
5 changes: 1 addition & 4 deletions cl/beacon/handler/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func (a *ApiHandler) blockRootFromStateId(ctx context.Context, tx kv.Tx, stateId

switch {
case stateId.Head():
root, _, err = a.forkchoiceStore.GetHead()
if err != nil {
return libcommon.Hash{}, http.StatusInternalServerError, err
}
root, _, httpStatusErr, err = a.getHead()
return
case stateId.Finalized():
root = a.forkchoiceStore.FinalizedCheckpoint().Root
Expand Down
1 change: 1 addition & 0 deletions cl/beacon/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
proposerSlashingService,
nil,
mockValidatorMonitor,
false,
) // TODO: add tests
h.Init()
return
Expand Down
1 change: 1 addition & 0 deletions cl/beacon/handler/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (t *validatorTestSuite) SetupTest() {
nil,
nil,
nil,
false,
)
t.gomockCtrl = gomockCtrl
}
Expand Down
2 changes: 2 additions & 0 deletions cl/beacon/synced_data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package synced_data

import (
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon/cl/abstract"
"github.com/erigontech/erigon/cl/phase1/core/state"
)
Expand All @@ -29,4 +30,5 @@ type SyncedData interface {
HeadStateMutator() abstract.BeaconStateMutator
Syncing() bool
HeadSlot() uint64
HeadRoot() common.Hash
}
48 changes: 43 additions & 5 deletions cl/beacon/synced_data/mock_services/synced_data_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading