Skip to content

Commit

Permalink
Caplin: refactored proposers reading (#12776)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Nov 20, 2024
1 parent a2462c6 commit 5000597
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 11 additions & 12 deletions cl/beacon/handler/duties_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ import (
"sync"

"github.com/erigontech/erigon/cl/beacon/beaconhttp"
"github.com/erigontech/erigon/cl/persistence/base_encoding"
state_accessors "github.com/erigontech/erigon/cl/persistence/state"
"github.com/erigontech/erigon/cl/phase1/core/state"
shuffling2 "github.com/erigontech/erigon/cl/phase1/core/state/shuffling"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"
)

type proposerDuties struct {
Expand All @@ -56,26 +54,27 @@ func (a *ApiHandler) getDutiesProposer(w http.ResponseWriter, r *http.Request) (
return nil, err
}
defer tx.Rollback()
key := base_encoding.Encode64ToBytes4(epoch)
indiciesBytes, err := tx.GetOne(kv.Proposers, key)
view := a.caplinStateSnapshots.View()
defer view.Close()

indicies, err := state_accessors.ReadProposersInEpoch(state_accessors.GetValFnTxAndSnapshot(tx, view), epoch)
if err != nil {
return nil, err
}
if len(indiciesBytes) != int(a.beaconChainCfg.SlotsPerEpoch*4) {
return nil, beaconhttp.NewEndpointError(http.StatusInternalServerError, errors.New("proposer duties is corrupted"))
if len(indicies) == 0 {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no proposers for this epoch. either this range was prune or not backfilled"))
}
duties := make([]proposerDuties, a.beaconChainCfg.SlotsPerEpoch)
for i := uint64(0); i < a.beaconChainCfg.SlotsPerEpoch; i++ {
validatorIndex := binary.BigEndian.Uint32(indiciesBytes[i*4 : i*4+4])
duties := make([]proposerDuties, len(indicies))
for i, validatorIndex := range indicies {
var pk libcommon.Bytes48
pk, err := state_accessors.ReadPublicKeyByIndex(tx, uint64(validatorIndex))
pk, err := state_accessors.ReadPublicKeyByIndex(tx, validatorIndex)
if err != nil {
return nil, err
}
duties[i] = proposerDuties{
Pubkey: pk,
ValidatorIndex: uint64(validatorIndex),
Slot: epoch*a.beaconChainCfg.SlotsPerEpoch + i,
ValidatorIndex: validatorIndex,
Slot: epoch*a.beaconChainCfg.SlotsPerEpoch + uint64(i),
}
}
return newBeaconResponse(duties).
Expand Down
18 changes: 18 additions & 0 deletions cl/persistence/state/state_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,21 @@ func ReadActiveIndicies(getFn GetValFn, slot uint64) ([]uint64, error) {
buf := bytes.NewBuffer(v)
return base_encoding.ReadRabbits(nil, buf)
}

func ReadProposersInEpoch(getFn GetValFn, epoch uint64) ([]uint64, error) {
key := base_encoding.Encode64ToBytes4(epoch)

indiciesBytes, err := getFn(kv.Proposers, key)
if err != nil {
return nil, err
}
if len(indiciesBytes) == 0 {
return nil, nil
}
var ret []uint64
for i := 0; i < len(indiciesBytes); i += 4 {
validatorIndex := binary.BigEndian.Uint32(indiciesBytes[i : i+4])
ret = append(ret, uint64(validatorIndex))
}
return ret, nil
}

0 comments on commit 5000597

Please sign in to comment.