Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't use spec for known networks #7

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions fetcher/beaconchain/beaconchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var (

getValidatorsPath = "eth/v1/beacon/states/%s/validators"
urlEndpoint *url.URL
slotsPerEpoch = uint64(32)
slotsPerEpoch uint64
leonz789 marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
Expand Down Expand Up @@ -162,24 +162,40 @@ func Init(confPath string) error {
types.Fetchers[types.BeaconChain] = Fetch
types.UpdateNativeAssetID(cfg.NSTID)

// parse nstID by splitting it
nstID := strings.Split(cfg.NSTID, "_")
if len(nstID) != 2 {
return feedertypes.ErrInitFail.Wrap("invalid nstID format")
}
// the second element is the lzID of the chain
lzID, err := strconv.ParseUint(nstID[1], 16, 64)
if err != nil {
return feedertypes.ErrInitFail.Wrap(err.Error())
}
if slotsPerEpochKnown, ok := types.ChainToSlotsPerEpoch[lzID]; ok {
slotsPerEpoch = slotsPerEpochKnown
return nil
}

// else, we need the slotsPerEpoch from beaconchain endpoint
// if it is not found or parsing errors, we can panic.
u := urlEndpoint.JoinPath(urlQuerySlotsPerEpoch)
res, err := http.Get(u.String())
if err != nil {
return feedertypes.ErrInitFail.Wrap(err.Error())
panic(feedertypes.ErrInitFail.Wrap(err.Error()))
}
result, err := io.ReadAll(res.Body)
if err != nil {
return feedertypes.ErrInitFail.Wrap(err.Error())
panic(feedertypes.ErrInitFail.Wrap(err.Error()))
}
var re ResultConfig
if err = json.Unmarshal(result, &re); err != nil {
return feedertypes.ErrInitFail.Wrap(err.Error())
panic(feedertypes.ErrInitFail.Wrap(err.Error()))
}

if slotsPerEpoch, err = strconv.ParseUint(re.Data.SlotsPerEpoch, 10, 64); err != nil {
log.Printf("Failed to get slotsPerEpoch from beaconchain endpoint with error:%s, we use fallback value of 32ETH instead of panic", err)
slotsPerEpoch = 32
panic(feedertypes.ErrInitFail.Wrap(err.Error()))
}

MaxMustermann2 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand Down Expand Up @@ -578,8 +594,8 @@ func GetFinalizedEpoch() (epoch uint64, stateRoot string, err error) {
return
}
slot, _ := strconv.ParseUint(re.Data.Header.Message.Slot, 10, 64)
epoch = slot / uint64(slotsPerEpoch)
if slot%uint64(slotsPerEpoch) > 0 {
epoch = slot / slotsPerEpoch
if slot%slotsPerEpoch > 0 {
leonz789 marked this conversation as resolved.
Show resolved Hide resolved
u = urlEndpoint.JoinPath(urlQueryHeader, strconv.FormatUint(epoch*slotsPerEpoch, 10))
res, err = http.Get(u.String())
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions fetcher/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ const (
BeaconChain = "beaconchain"

NativeTokenETH = "nsteth"

DefaultSlotsPerEpoch = uint64(32)
)

var (
ChainToSlotsPerEpoch = map[uint64]uint64{
101: DefaultSlotsPerEpoch,
40161: DefaultSlotsPerEpoch,
40217: DefaultSlotsPerEpoch,
}
NativeTokenETHAssetID = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_0x65"
NativeRestakings = map[string][]string{
"eth": {BeaconChain, NativeTokenETH},
Expand Down
Loading