Skip to content

Commit

Permalink
Separate endpoint for beacon state (#1191)
Browse files Browse the repository at this point in the history
* separate endpoint for beacon state

* fix interface

* default values for config path
  • Loading branch information
claravanstaden committed May 9, 2024
1 parent 4e6700e commit a007086
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 29 deletions.
45 changes: 27 additions & 18 deletions relayer/cmd/generate_beacon_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func generateBeaconDataCmd() *cobra.Command {
RunE: generateBeaconTestFixture,
}

cmd.Flags().String("url", "http://127.0.0.1:9596", "Beacon URL")
cmd.Flags().String("config", "/tmp/snowbridge/beacon-relay.json", "Path to the beacon relay config")
cmd.Flags().Bool("wait_until_next_period", true, "Waiting until next period")
cmd.Flags().Uint32("nonce", 1, "Nonce of the inbound message")
cmd.Flags().String("test_case", "register_token", "Inbound test case")
Expand All @@ -55,8 +55,8 @@ func generateBeaconCheckpointCmd() *cobra.Command {
RunE: generateBeaconCheckpoint,
}

cmd.Flags().String("url", "http://127.0.0.1:9596", "Beacon URL")
cmd.Flags().Bool("export_json", false, "Export Json")
cmd.Flags().String("config", "/tmp/snowbridge/beacon-relay.json", "Path to the beacon relay config")
cmd.Flags().Bool("export-json", false, "Export Json")

return cmd
}
Expand All @@ -68,7 +68,7 @@ func generateExecutionUpdateCmd() *cobra.Command {
RunE: generateExecutionUpdate,
}

cmd.Flags().String("url", "http://127.0.0.1:9596", "Beacon URL")
cmd.Flags().String("config", "/tmp/snowbridge/beacon-relay.json", "Path to the beacon relay config")
cmd.Flags().Uint32("slot", 1, "slot number")
return cmd
}
Expand Down Expand Up @@ -97,12 +97,12 @@ const (
// Only print the hex encoded call as output of this command
func generateBeaconCheckpoint(cmd *cobra.Command, _ []string) error {
err := func() error {
endpoint, err := cmd.Flags().GetString("url")
config, err := cmd.Flags().GetString("config")
if err != nil {
return err
}

viper.SetConfigFile("web/packages/test/config/beacon-relay.json")
viper.SetConfigFile(config)

if err := viper.ReadInConfig(); err != nil {
return err
Expand All @@ -119,7 +119,7 @@ func generateBeaconCheckpoint(cmd *cobra.Command, _ []string) error {
store.Connect()
defer store.Close()

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

checkPointScale, err := s.GetCheckpoint()
Expand Down Expand Up @@ -153,12 +153,12 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
err := func() error {
ctx := context.Background()

endpoint, err := cmd.Flags().GetString("url")
config, err := cmd.Flags().GetString("config")
if err != nil {
return err
}

viper.SetConfigFile("web/packages/test/config/beacon-relay.json")
viper.SetConfigFile(config)
if err = viper.ReadInConfig(); err != nil {
return err
}
Expand All @@ -172,11 +172,14 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
p := protocol.New(conf.Source.Beacon.Spec)

store := store.New(conf.Source.Beacon.DataStore.Location, conf.Source.Beacon.DataStore.MaxEntries, *p)
store.Connect()
err = store.Connect()
if err != nil {
return err
}
defer store.Close()

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

viper.SetConfigFile("/tmp/snowbridge/execution-relay-asset-hub.json")
Expand Down Expand Up @@ -503,19 +506,25 @@ func writeBenchmarkDataFile(path string, fileContents string) error {

func generateExecutionUpdate(cmd *cobra.Command, _ []string) error {
err := func() error {
endpoint, _ := cmd.Flags().GetString("url")
beaconSlot, _ := cmd.Flags().GetUint32("slot")
config, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
beaconSlot, err := cmd.Flags().GetUint32("slot")
if err != nil {
return err
}

viper.SetConfigFile("web/packages/test/config/beacon-relay.json")
viper.SetConfigFile(config)
if err := viper.ReadInConfig(); err != nil {
return err
}
var conf beaconConf.Config
err := viper.Unmarshal(&conf)
err = viper.Unmarshal(&conf)
if err != nil {
return err
}
log.WithFields(log.Fields{"endpoint": endpoint}).Info("connecting to beacon API")
log.WithFields(log.Fields{"endpoint": conf.Source.Beacon.Endpoint}).Info("connecting to beacon API")

p := protocol.New(conf.Source.Beacon.Spec)

Expand All @@ -524,7 +533,7 @@ func generateExecutionUpdate(cmd *cobra.Command, _ []string) error {
defer store.Close()

// generate executionUpdate
client := api.NewBeaconClient(endpoint)
client := api.NewBeaconClient(conf.Source.Beacon.Endpoint, conf.Source.Beacon.StateEndpoint)
s := syncer.New(client, &store, p)
blockRoot, err := s.Client.GetBeaconBlockRoot(uint64(beaconSlot))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion relayer/cmd/import_execution_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func importExecutionHeaderFn(cmd *cobra.Command, _ []string) error {
store.Connect()
defer store.Close()

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

beaconHeaderHash := common.HexToHash(finalizedHeader)
Expand Down
2 changes: 1 addition & 1 deletion relayer/cmd/store_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func storeBeaconStateInDB(cmd *cobra.Command, _ []string) error {

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)
beaconClient := api.NewBeaconClient(conf.Source.Beacon.Endpoint, conf.Source.Beacon.StateEndpoint)
syncer := syncer.New(beaconClient, &store, p)

err = store.Connect()
Expand Down
7 changes: 4 additions & 3 deletions relayer/relays/beacon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type DataStore struct {
}

type BeaconConfig struct {
Endpoint string `mapstructure:"endpoint"`
Spec SpecSettings `mapstructure:"spec"`
DataStore DataStore `mapstructure:"datastore"`
Endpoint string `mapstructure:"endpoint"`
StateEndpoint string `mapstructure:"stateEndpoint"`
Spec SpecSettings `mapstructure:"spec"`
DataStore DataStore `mapstructure:"datastore"`
}

type SinkConfig struct {
Expand Down
10 changes: 6 additions & 4 deletions relayer/relays/beacon/header/syncer/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ type BeaconAPI interface {
}

type BeaconClient struct {
httpClient http.Client
endpoint string
httpClient http.Client
endpoint string
stateEndpoint string
}

func NewBeaconClient(endpoint string) *BeaconClient {
func NewBeaconClient(endpoint, stateEndpoint string) *BeaconClient {
return &BeaconClient{
http.Client{},
endpoint,
stateEndpoint,
}
}

Expand Down Expand Up @@ -401,7 +403,7 @@ func (b *BeaconClient) GetLatestFinalizedUpdate() (LatestFinalisedUpdateResponse

func (b *BeaconClient) GetBeaconState(stateIdOrSlot string) ([]byte, error) {
var data []byte
req, err := http.NewRequest("GET", fmt.Sprintf("%s/eth/v2/debug/beacon/states/%s", b.endpoint, stateIdOrSlot), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/eth/v2/debug/beacon/states/%s", b.stateEndpoint, stateIdOrSlot), nil)
if err != nil {
return data, err
}
Expand Down
2 changes: 1 addition & 1 deletion relayer/relays/beacon/header/syncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
const TestUrl = "https://lodestar-sepolia.chainsafe.io"

func newTestRunner() *Syncer {
return New(api.NewBeaconClient(TestUrl), &mock.Store{}, protocol.New(config.SpecSettings{
return New(api.NewBeaconClient(TestUrl, TestUrl), &mock.Store{}, protocol.New(config.SpecSettings{
SlotsInEpoch: 32,
EpochsPerSyncCommitteePeriod: 256,
DenebForkEpoch: 0,
Expand Down
2 changes: 1 addition & 1 deletion relayer/relays/beacon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *Relay) Start(ctx context.Context, eg *errgroup.Group) error {
}
defer s.Close()

beaconAPI := api.NewBeaconClient(r.config.Source.Beacon.Endpoint)
beaconAPI := api.NewBeaconClient(r.config.Source.Beacon.Endpoint, r.config.Source.Beacon.StateEndpoint)
headers := header.New(
writer,
beaconAPI,
Expand Down
1 change: 1 addition & 0 deletions web/packages/test/config/beacon-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"source": {
"beacon": {
"endpoint": "http://127.0.0.1:9596",
"stateEndpoint": "http://127.0.0.1:9596",
"spec": {
"syncCommitteeSize": 512,
"slotsInEpoch": 32,
Expand Down

0 comments on commit a007086

Please sign in to comment.