-
Notifications
You must be signed in to change notification settings - Fork 101
/
store_beacon_state.go
87 lines (70 loc) · 2.38 KB
/
store_beacon_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cmd
import (
"fmt"
"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"
)
func storeBeaconState() *cobra.Command {
cmd := &cobra.Command{
Use: "store-beacon-state",
Short: "Import the provided execution header.",
Args: cobra.ExactArgs(0),
RunE: storeBeaconStateInDB,
}
cmd.Flags().String("config", "", "path to the beacon config file to use")
err := cmd.MarkFlagRequired("config")
if err != nil {
return nil
}
return cmd
}
func storeBeaconStateInDB(cmd *cobra.Command, _ []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
return err
}
var conf config.Config
err = viper.Unmarshal(&conf)
if err != nil {
return err
}
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 {
return err
}
defer store.Close()
update, err := syncer.GetFinalizedUpdate()
if err != nil {
return err
}
attestedHeaderSlot := uint64(update.Payload.AttestedHeader.Slot)
finalizedHeaderSlot := uint64(update.Payload.FinalizedHeader.Slot)
attestedBeaconData, err := syncer.Client.GetBeaconState(strconv.FormatUint(attestedHeaderSlot, 10))
if err != nil {
return fmt.Errorf("download attested beacon state at slot %d: %w", attestedHeaderSlot, err)
}
finalizedBeaconData, err := syncer.Client.GetBeaconState(strconv.FormatUint(finalizedHeaderSlot, 10))
if err != nil {
return fmt.Errorf("download finalized beacon state at slot %d: %w", finalizedHeaderSlot, err)
}
err = store.WriteEntry(attestedHeaderSlot, finalizedHeaderSlot, attestedBeaconData, finalizedBeaconData)
deletedSlots, err := store.PruneOldStates()
log.WithField("deletedSlots", deletedSlots).Info("deleted old beacon states")
return nil
}