Skip to content

Commit

Permalink
feat: use finalized next sync committee branch (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Jan 5, 2024
1 parent 025a934 commit b12fc2b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
23 changes: 15 additions & 8 deletions chains/evm/listener/events/handlers/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/rs/zerolog/log"
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message"
"github.com/sygmaprotocol/spectre-node/chains/evm/prover"
"github.com/sygmaprotocol/sygma-core/relayer/message"
consensus "github.com/umbracle/go-eth-consensus"
)

type SyncCommitteeFetcher interface {
Expand Down Expand Up @@ -42,18 +44,23 @@ func NewRotateHandler(msgChan chan []*message.Message, syncCommitteeFetcher Sync
// HandleEvents checks if there is a new sync committee and sends a rotate message
// if there is
func (h *RotateHandler) HandleEvents(checkpoint *apiv1.Finality) error {
sArgs, err := h.prover.StepArgs()
args, err := h.prover.RotateArgs(uint64(checkpoint.Finalized.Epoch))
if err != nil {
return err
}
rArgs, err := h.prover.RotateArgs(uint64(checkpoint.Justified.Epoch))
if err != nil {
return err
sArgs := &prover.StepArgs{
Pubkeys: args.Pubkeys,
Update: &consensus.LightClientFinalityUpdateCapella{
AttestedHeader: args.Update.AttestedHeader,
FinalizedHeader: args.Update.FinalizedHeader,
FinalityBranch: args.Update.FinalityBranch,
SyncAggregate: args.Update.SyncAggregate,
SignatureSlot: args.Update.SignatureSlot,
},
Domain: args.Domain,
Spec: args.Spec,
}

if sArgs.Update.FinalizedHeader.Header.Slot != rArgs.Update.AttestedHeader.Header.Slot {
return nil
}
syncCommittee, err := h.syncCommitteeFetcher.SyncCommittee(context.Background(), &api.SyncCommitteeOpts{
State: "finalized",
})
Expand All @@ -66,7 +73,7 @@ func (h *RotateHandler) HandleEvents(checkpoint *apiv1.Finality) error {

log.Info().Uint8("domainID", h.domainID).Msgf("Rotating committee")

rotateProof, err := h.prover.RotateProof(rArgs)
rotateProof, err := h.prover.RotateProof(args)
if err != nil {
return err
}
Expand Down
35 changes: 31 additions & 4 deletions chains/evm/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ type StepArgs struct {
}

type RotateArgs struct {
Spec Spec
Update *consensus.LightClientUpdateCapella
Spec Spec
Update *consensus.LightClientUpdateCapella
Pubkeys [512][48]byte
Domain phase0.Domain
}

type ProverResponse struct {
Expand Down Expand Up @@ -134,6 +136,7 @@ func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], er

// RotateProof generates the proof for the sync committee rotation for the period
func (p *Prover) RotateProof(args *RotateArgs) (*EvmProof[message.RotateInput], error) {
args.Update.AttestedHeader = args.Update.FinalizedHeader
syncCommiteeRoot, err := p.pubkeysRoot(args.Update.NextSyncCommittee.PubKeys)
if err != nil {
return nil, err
Expand Down Expand Up @@ -216,9 +219,33 @@ func (p *Prover) RotateArgs(epoch uint64) (*RotateArgs, error) {
return nil, fmt.Errorf("missing light client updates")
}
update := updates[0]

finalizedNextSyncCommitteeBranch := make([][32]byte, len(update.NextSyncCommitteeBranch))
blockRoot, err := p.beaconClient.BeaconBlockRoot(context.Background(), &api.BeaconBlockRootOpts{
Block: fmt.Sprint(update.FinalizedHeader.Header.Slot),
})
if err != nil {
return nil, err
}
bootstrap, err := p.lightClient.Bootstrap(blockRoot.Data.String())
if err != nil {
return nil, err
}

copy(finalizedNextSyncCommitteeBranch, bootstrap.CurrentSyncCommitteeBranch)
finalizedNextSyncCommitteeBranch[0] = update.NextSyncCommitteeBranch[0]
update.NextSyncCommitteeBranch = finalizedNextSyncCommitteeBranch

domain, err := p.beaconClient.Domain(context.Background(), SYNC_COMMITTEE_DOMAIN, phase0.Epoch(update.FinalizedHeader.Header.Slot/32))
if err != nil {
return nil, err
}

return &RotateArgs{
Update: update,
Spec: p.spec,
Update: update,
Spec: p.spec,
Pubkeys: bootstrap.CurrentSyncCommittee.PubKeys,
Domain: domain,
}, nil
}

Expand Down

0 comments on commit b12fc2b

Please sign in to comment.