Skip to content

Commit

Permalink
voluntaryExit
Browse files Browse the repository at this point in the history
  • Loading branch information
domiwei committed Apr 17, 2024
1 parent 618d267 commit 92a78d5
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 70 deletions.
3 changes: 3 additions & 0 deletions cl/beacon/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type ApiHandler struct {
syncCommitteeMessagesService services.SyncCommitteeMessagesService
syncContributionAndProofsService services.SyncContributionService
aggregateAndProofsService services.AggregateAndProofService
voluntaryExitService services.VoluntaryExitService
}

func NewApiHandler(
Expand Down Expand Up @@ -110,6 +111,7 @@ func NewApiHandler(
syncCommitteeMessagesService services.SyncCommitteeMessagesService,
syncContributionAndProofs services.SyncContributionService,
aggregateAndProofs services.AggregateAndProofService,
voluntaryExitService services.VoluntaryExitService,
) *ApiHandler {
blobBundles, err := lru.New[common.Bytes48, BlobBundle]("blobs", maxBlobBundleCacheSize)
if err != nil {
Expand Down Expand Up @@ -146,6 +148,7 @@ func NewApiHandler(
syncCommitteeMessagesService: syncCommitteeMessagesService,
syncContributionAndProofsService: syncContributionAndProofs,
aggregateAndProofsService: aggregateAndProofs,
voluntaryExitService: voluntaryExitService,
}
}

Expand Down
3 changes: 2 additions & 1 deletion cl/beacon/handler/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ func (a *ApiHandler) PostEthV1BeaconPoolVoluntaryExits(w http.ResponseWriter, r
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := a.forkchoiceStore.OnVoluntaryExit(&req, false); err != nil {
if err := a.voluntaryExitService.ProcessMessage(r.Context(), nil, &req); err != nil && !errors.Is(err, services.ErrIgnore) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// Broadcast to gossip
if a.sentinel != nil {
encodedSSZ, err := req.EncodeSSZ(nil)
Expand Down
8 changes: 6 additions & 2 deletions cl/beacon/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
syncCommitteeMessagesService := mock_services.NewMockSyncCommitteeMessagesService(ctrl)
syncContributionService := mock_services.NewMockSyncContributionService(ctrl)
aggregateAndProofsService := mock_services.NewMockAggregateAndProofService(ctrl)
voluntaryExitService := mock_services.NewMockVoluntaryExitService(ctrl)
// ctx context.Context, subnetID *uint64, msg *cltypes.SyncCommitteeMessage) error
syncCommitteeMessagesService.EXPECT().ProcessMessage(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, subnetID *uint64, msg *cltypes.SyncCommitteeMessage) error {
return h.syncMessagePool.AddSyncCommitteeMessage(postState, *subnetID, msg)
Expand All @@ -96,11 +97,14 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
syncContributionService.EXPECT().ProcessMessage(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, subnetID *uint64, msg *cltypes.SignedContributionAndProof) error {
return h.syncMessagePool.AddSyncContribution(postState, msg.Message.Contribution)
}).AnyTimes()

aggregateAndProofsService.EXPECT().ProcessMessage(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, subnetID *uint64, msg *cltypes.SignedAggregateAndProof) error {
opPool.AttestationsPool.Insert(msg.Message.Aggregate.Signature(), msg.Message.Aggregate)
return nil
}).AnyTimes()
voluntaryExitService.EXPECT().ProcessMessage(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, subnetID *uint64, msg *cltypes.SignedVoluntaryExit) error {
opPool.VoluntaryExistsPool.Insert(msg.VoluntaryExit.ValidatorIndex, msg)
return nil
}).AnyTimes()

vp = validator_params.NewValidatorParams()
h = NewApiHandler(
Expand All @@ -124,7 +128,7 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
Events: true,
Validator: true,
Lighthouse: true,
}, nil, blobStorage, nil, vp, nil, nil, fcu.SyncContributionPool, nil, nil, syncCommitteeMessagesService, syncContributionService, aggregateAndProofsService) // TODO: add tests
}, nil, blobStorage, nil, vp, nil, nil, fcu.SyncContributionPool, nil, nil, syncCommitteeMessagesService, syncContributionService, aggregateAndProofsService, voluntaryExitService) // TODO: add tests
h.Init()
return
}
1 change: 1 addition & 0 deletions cl/beacon/handler/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (t *validatorTestSuite) SetupTest() {
nil,
nil,
nil,
nil,
)
t.gomockCtrl = gomockCtrl
}
Expand Down
8 changes: 0 additions & 8 deletions cl/phase1/forkchoice/fork_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ func TestForkChoiceBasic(t *testing.T) {
for sd.HeadState() == nil {
time.Sleep(time.Millisecond)
}
// Try processing a voluntary exit
err = store.OnVoluntaryExit(&cltypes.SignedVoluntaryExit{
VoluntaryExit: &cltypes.VoluntaryExit{
Epoch: 0,
ValidatorIndex: 0,
},
}, true)
require.NoError(t, err)
// Try processing a bls execution change exit
err = store.OnBlsToExecutionChange(&cltypes.SignedBLSToExecutionChange{
Message: &cltypes.BLSToExecutionChange{
Expand Down
1 change: 0 additions & 1 deletion cl/phase1/forkchoice/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type ForkChoiceStorageReader interface {
type ForkChoiceStorageWriter interface {
OnAttestation(attestation *solid.Attestation, fromBlock, insert bool) error
OnAttesterSlashing(attesterSlashing *cltypes.AttesterSlashing, test bool) error
OnVoluntaryExit(signedVoluntaryExit *cltypes.SignedVoluntaryExit, test bool) error
OnProposerSlashing(proposerSlashing *cltypes.ProposerSlashing, test bool) error
OnBlsToExecutionChange(signedChange *cltypes.SignedBLSToExecutionChange, test bool) error
OnBlock(ctx context.Context, block *cltypes.SignedBeaconBlock, newPayload bool, fullValidation bool, checkDataAvaibility bool) error
Expand Down
56 changes: 0 additions & 56 deletions cl/phase1/forkchoice/on_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package forkchoice

import (
"bytes"
"errors"
"fmt"

"github.com/Giulio2002/bls"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/fork"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
Expand All @@ -17,60 +15,6 @@ import (
// NOTE: This file implements non-official handlers for other types of iterations. what it does is,using the forkchoices
// and verify external operations and eventually push them in the operations pool.

// OnVoluntaryExit is a non-official handler for voluntary exit operations. it pushes the voluntary exit in the pool.
func (f *ForkChoiceStore) OnVoluntaryExit(signedVoluntaryExit *cltypes.SignedVoluntaryExit, test bool) error {
voluntaryExit := signedVoluntaryExit.VoluntaryExit
if f.operationsPool.VoluntaryExistsPool.Has(voluntaryExit.ValidatorIndex) {
f.emitters.Publish("voluntary_exit", voluntaryExit)
return nil
}

s := f.syncedDataManager.HeadState()
if s == nil {
return nil
}

val, err := s.ValidatorForValidatorIndex(int(voluntaryExit.ValidatorIndex))
if err != nil {
return err
}

if val.ExitEpoch() != f.beaconCfg.FarFutureEpoch {
return nil
}

pk := val.PublicKey()

domainType := f.beaconCfg.DomainVoluntaryExit
var domain []byte

if s.Version() < clparams.DenebVersion {
domain, err = s.GetDomain(domainType, voluntaryExit.Epoch)
} else if s.Version() >= clparams.DenebVersion {
domain, err = fork.ComputeDomain(domainType[:], utils.Uint32ToBytes4(uint32(s.BeaconConfig().CapellaForkVersion)), s.GenesisValidatorsRoot())
}
if err != nil {
return err
}

signingRoot, err := fork.ComputeSigningRoot(voluntaryExit, domain)
if err != nil {
return err
}
if !test {
valid, err := bls.Verify(signedVoluntaryExit.Signature[:], signingRoot[:], pk[:])
if err != nil {
return err
}
if !valid {
return errors.New("ProcessVoluntaryExit: BLS verification failed")
}
}
f.emitters.Publish("voluntary_exit", voluntaryExit)
f.operationsPool.VoluntaryExistsPool.Insert(voluntaryExit.ValidatorIndex, signedVoluntaryExit)
return nil
}

// OnProposerSlashing is a non-official handler for proposer slashing operations. it pushes the proposer slashing in the pool.
func (f *ForkChoiceStore) OnProposerSlashing(proposerSlashing *cltypes.ProposerSlashing, test bool) (err error) {
if f.operationsPool.ProposerSlashingsPool.Has(pool.ComputeKeyForProposerSlashing(proposerSlashing)) {
Expand Down
10 changes: 9 additions & 1 deletion cl/phase1/network/gossip_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type GossipManager struct {
syncContributionService services.SyncContributionService
aggregateAndProofService services.AggregateAndProofService
attestationService services.AttestationService
voluntaryExitService services.VoluntaryExitService
}

func NewGossipReceiver(
Expand All @@ -58,6 +59,7 @@ func NewGossipReceiver(
syncContributionService services.SyncContributionService,
aggregateAndProofService services.AggregateAndProofService,
attestationService services.AttestationService,
voluntaryExitService services.VoluntaryExitService,
) *GossipManager {
return &GossipManager{
sentinel: s,
Expand All @@ -72,6 +74,7 @@ func NewGossipReceiver(
syncContributionService: syncContributionService,
aggregateAndProofService: aggregateAndProofService,
attestationService: attestationService,
voluntaryExitService: voluntaryExitService,
}
}

Expand Down Expand Up @@ -147,7 +150,12 @@ func (g *GossipManager) routeAndProcess(ctx context.Context, data *sentinel.Goss
}
return g.syncContributionService.ProcessMessage(ctx, data.SubnetId, obj)
case gossip.TopicNameVoluntaryExit:
return operationsContract[*cltypes.SignedVoluntaryExit](ctx, g, data, int(version), "voluntary exit", g.forkChoice.OnVoluntaryExit)
obj := &cltypes.SignedVoluntaryExit{}
if err := obj.DecodeSSZ(data.Data, int(version)); err != nil {
return err
}
return g.voluntaryExitService.ProcessMessage(ctx, data.SubnetId, obj)

case gossip.TopicNameProposerSlashing:
return operationsContract[*cltypes.ProposerSlashing](ctx, g, data, int(version), "proposer slashing", g.forkChoice.OnProposerSlashing)
case gossip.TopicNameAttesterSlashing:
Expand Down
3 changes: 3 additions & 0 deletions cl/phase1/network/services/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ type AggregateAndProofService Service[*cltypes.SignedAggregateAndProof]

//go:generate mockgen -destination=./mock_services/attestation_service_mock.go -package=mock_services . AttestationService
type AttestationService Service[*solid.Attestation]

//go:generate mockgen -destination=./mock_services/voluntary_exit_service_mock.go -package=mock_services . VoluntaryExitService
type VoluntaryExitService Service[*cltypes.SignedVoluntaryExit]

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

Loading

0 comments on commit 92a78d5

Please sign in to comment.