Skip to content

Commit

Permalink
Manual sync committee update (#1164)
Browse files Browse the repository at this point in the history
* adds sync committee to update method

* progress

* manual sync committee update

* fix tests

* cleanup

* remove unnecessary check

* simplify checkpoint populate

* fix method

---------

Co-authored-by: claravanstaden <Cats 4 life!>
  • Loading branch information
claravanstaden authored Apr 18, 2024
1 parent af101b6 commit 7f41605
Show file tree
Hide file tree
Showing 36 changed files with 1,122 additions and 565 deletions.
47 changes: 27 additions & 20 deletions relayer/cmd/generate_beacon_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
beaconjson "github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/json"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"
executionConf "github.com/snowfork/snowbridge/relayer/relays/execution"

Expand Down Expand Up @@ -128,12 +129,13 @@ func generateBeaconCheckpoint(cmd *cobra.Command, _ []string) error {
return err
}

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)
p := protocol.New(conf.Source.Beacon.Spec)
store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
defer store.Close()

client := api.NewBeaconClient(endpoint, conf.Source.Beacon.Spec.SlotsInEpoch)
s := syncer.New(client, conf.Source.Beacon.Spec, &store)
client := api.NewBeaconClient(endpoint)
s := syncer.New(client, &store, p)

checkPointScale, err := s.GetCheckpoint()
if err != nil {
Expand Down Expand Up @@ -182,13 +184,15 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
return err
}

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)
p := protocol.New(conf.Source.Beacon.Spec)

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
defer store.Close()

log.WithFields(log.Fields{"endpoint": endpoint}).Info("connecting to beacon API")
client := api.NewBeaconClient(endpoint, conf.Source.Beacon.Spec.SlotsInEpoch)
s := syncer.New(client, conf.Source.Beacon.Spec, &store)
client := api.NewBeaconClient(endpoint)
s := syncer.New(client, &store, p)

viper.SetConfigFile("/tmp/snowbridge/execution-relay-asset-hub.json")

Expand Down Expand Up @@ -226,11 +230,11 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
return err
}
initialSyncHeaderSlot := initialSync.Header.Slot
initialSyncPeriod := s.ComputeSyncPeriodAtSlot(initialSyncHeaderSlot)
initialEpoch := s.ComputeEpochAtSlot(initialSyncHeaderSlot)
initialSyncPeriod := p.ComputeSyncPeriodAtSlot(initialSyncHeaderSlot)
initialEpoch := p.ComputeEpochAtSlot(initialSyncHeaderSlot)

// generate SyncCommitteeUpdate for filling the missing NextSyncCommittee in initial checkpoint
syncCommitteeUpdateScale, err := s.GetSyncCommitteePeriodUpdate(initialSyncPeriod)
syncCommitteeUpdateScale, err := s.GetSyncCommitteePeriodUpdate(initialSyncPeriod, 0)
if err != nil {
return fmt.Errorf("get sync committee update: %w", err)
}
Expand Down Expand Up @@ -333,11 +337,11 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
if finalizedUpdate.AttestedHeader.Slot <= initialSyncHeaderSlot {
return fmt.Errorf("AttestedHeader slot should be greater than initialSyncHeaderSlot")
}
finalizedEpoch := s.ComputeEpochAtSlot(finalizedUpdate.AttestedHeader.Slot)
finalizedEpoch := p.ComputeEpochAtSlot(finalizedUpdate.AttestedHeader.Slot)
if finalizedEpoch <= initialEpoch {
return fmt.Errorf("epoch in FinalizedUpdate should be greater than initialEpoch")
}
finalizedPeriod := s.ComputeSyncPeriodAtSlot(finalizedUpdate.FinalizedHeader.Slot)
finalizedPeriod := p.ComputeSyncPeriodAtSlot(finalizedUpdate.FinalizedHeader.Slot)
if initialSyncPeriod != finalizedPeriod {
return fmt.Errorf("initialSyncPeriod should be consistent with finalizedUpdatePeriod")
}
Expand Down Expand Up @@ -389,7 +393,7 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("get next finalized header update: %w", err)
}
nextFinalizedUpdate := nextFinalizedUpdateScale.Payload.ToJSON()
nextFinalizedUpdatePeriod := s.ComputeSyncPeriodAtSlot(nextFinalizedUpdate.FinalizedHeader.Slot)
nextFinalizedUpdatePeriod := p.ComputeSyncPeriodAtSlot(nextFinalizedUpdate.FinalizedHeader.Slot)
if initialSyncPeriod+1 == nextFinalizedUpdatePeriod {
err := writeJSONToFile(nextFinalizedUpdate, fmt.Sprintf("%s/%s", pathToBeaconTestFixtureFiles, "next-finalized-header-update.json"))
if err != nil {
Expand All @@ -398,7 +402,7 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
log.Info("created next finalized header update file")

// generate nextSyncCommitteeUpdate
nextSyncCommitteeUpdateScale, err := s.GetSyncCommitteePeriodUpdate(initialSyncPeriod + 1)
nextSyncCommitteeUpdateScale, err := s.GetSyncCommitteePeriodUpdate(initialSyncPeriod+1, 0)
if err != nil {
return fmt.Errorf("get sync committee update: %w", err)
}
Expand Down Expand Up @@ -480,16 +484,17 @@ func generateExecutionUpdate(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
specSettings := conf.Source.Beacon.Spec
log.WithFields(log.Fields{"endpoint": endpoint}).Info("connecting to beacon API")

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)
p := protocol.New(conf.Source.Beacon.Spec)

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
defer store.Close()

// generate executionUpdate
client := api.NewBeaconClient(endpoint, specSettings.SlotsInEpoch)
s := syncer.New(client, specSettings, &store)
client := api.NewBeaconClient(endpoint)
s := syncer.New(client, &store, p)
blockRoot, err := s.Client.GetBeaconBlockRoot(uint64(beaconSlot))
if err != nil {
return fmt.Errorf("fetch block: %w", err)
Expand Down Expand Up @@ -667,13 +672,15 @@ func generateInboundFixture(cmd *cobra.Command, _ []string) error {
return err
}

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)
p := protocol.New(conf.Source.Beacon.Spec)

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
defer store.Close()

log.WithFields(log.Fields{"endpoint": endpoint}).Info("connecting to beacon API")
client := api.NewBeaconClient(endpoint, conf.Source.Beacon.Spec.SlotsInEpoch)
s := syncer.New(client, conf.Source.Beacon.Spec, &store)
client := api.NewBeaconClient(endpoint)
s := syncer.New(client, &store, p)

viper.SetConfigFile("/tmp/snowbridge/execution-relay-asset-hub.json")

Expand Down
10 changes: 5 additions & 5 deletions relayer/cmd/import_execution_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -89,8 +90,6 @@ func importExecutionHeaderFn(cmd *cobra.Command, _ []string) error {
return err
}

specSettings := conf.Source.Beacon.Spec

keypair, err := getKeyPair(privateKeyFile)
if err != nil {
return fmt.Errorf("get keypair from file: %w", err)
Expand All @@ -110,12 +109,13 @@ func importExecutionHeaderFn(cmd *cobra.Command, _ []string) error {

log.WithField("hash", beaconHeader).Info("will be syncing execution header for beacon hash")

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)
p := protocol.New(conf.Source.Beacon.Spec)
store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
defer store.Close()

client := api.NewBeaconClient(lodestarEndpoint, specSettings.SlotsInEpoch)
syncer := syncer.New(client, specSettings, &store)
client := api.NewBeaconClient(lodestarEndpoint)
syncer := syncer.New(client, &store, p)

beaconHeaderHash := common.HexToHash(finalizedHeader)

Expand Down
31 changes: 8 additions & 23 deletions relayer/cmd/store_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package cmd

import (
"fmt"
log "github.com/sirupsen/logrus"
"strconv"

"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"

_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

_ "github.com/mattn/go-sqlite3"
)

func storeBeaconState() *cobra.Command {
Expand Down Expand Up @@ -49,11 +49,10 @@ func storeBeaconStateInDB(cmd *cobra.Command, _ []string) error {
return err
}

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries)

specSettings := conf.Source.Beacon.Spec
beaconClient := api.NewBeaconClient(conf.Source.Beacon.Endpoint, specSettings.SlotsInEpoch)
syncer := syncer.New(beaconClient, specSettings, &store)
p := protocol.New(conf.Source.Beacon.Spec)
store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
beaconClient := api.NewBeaconClient(conf.Source.Beacon.Endpoint)
syncer := syncer.New(beaconClient, &store, p)

err = store.Connect()
if err != nil {
Expand All @@ -69,8 +68,6 @@ func storeBeaconStateInDB(cmd *cobra.Command, _ []string) error {

attestedHeaderSlot := uint64(update.Payload.AttestedHeader.Slot)
finalizedHeaderSlot := uint64(update.Payload.FinalizedHeader.Slot)
attestedSyncPeriod := syncer.ComputeSyncPeriodAtSlot(attestedHeaderSlot)
finalizedSyncPeriod := syncer.ComputeSyncPeriodAtSlot(finalizedHeaderSlot)

attestedBeaconData, err := syncer.Client.GetBeaconState(strconv.FormatUint(attestedHeaderSlot, 10))
if err != nil {
Expand All @@ -81,19 +78,7 @@ func storeBeaconStateInDB(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("download finalized beacon state at slot %d: %w", finalizedHeaderSlot, err)
}

err = store.WriteStateFile(attestedHeaderSlot, attestedBeaconData)
if err != nil {
return err
}
err = store.WriteStateFile(finalizedHeaderSlot, finalizedBeaconData)
if err != nil {
return err
}

err = store.StoreUpdate(attestedHeaderSlot, finalizedHeaderSlot, attestedSyncPeriod, finalizedSyncPeriod)
if err != nil {
return fmt.Errorf("store beacon update: %w", err)
}
err = store.WriteEntry(attestedHeaderSlot, finalizedHeaderSlot, attestedBeaconData, finalizedBeaconData)

deletedSlots, err := store.PruneOldStates()
log.WithField("deletedSlots", deletedSlots).Info("deleted old beacon states")
Expand Down
1 change: 1 addition & 0 deletions relayer/relays/beacon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Config struct {
}

type SpecSettings struct {
SyncCommitteeSize uint64 `mapstructure:"syncCommitteeSize"`
SlotsInEpoch uint64 `mapstructure:"slotsInEpoch"`
EpochsPerSyncCommitteePeriod uint64 `mapstructure:"epochsPerSyncCommitteePeriod"`
DenebForkEpoch uint64 `mapstructure:"denebForkedEpoch"`
Expand Down
Loading

0 comments on commit 7f41605

Please sign in to comment.