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

(BEDS-561) adapt return struct #952

Merged
merged 6 commits into from
Oct 17, 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
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ require (
github.com/prysmaticlabs/go-ssz v0.0.0-20210121151755-f6208871c388
github.com/rocket-pool/rocketpool-go v1.8.3-0.20240618173422-783b8668f5b4
github.com/rocket-pool/smartnode v1.13.6
github.com/shopspring/decimal v1.3.1
github.com/shopspring/decimal v1.4.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,8 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9Nz
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
43 changes: 43 additions & 0 deletions backend/pkg/api/data_access/header.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package dataaccess

import (
"context"
"database/sql"
"fmt"

t "github.com/gobitfly/beaconchain/pkg/api/types"
"github.com/gobitfly/beaconchain/pkg/commons/cache"
"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/price"
"github.com/gobitfly/beaconchain/pkg/commons/utils"
)

func (d *DataAccessService) GetLatestSlot() (uint64, error) {
Expand All @@ -26,6 +32,43 @@ func (d *DataAccessService) GetBlockHeightAt(slot uint64) (uint64, error) {
return d.dummy.GetBlockHeightAt(slot)
}

// returns the block number of the latest existing block at or before the given slot
func (d *DataAccessService) GetLatestBlockHeightForSlot(ctx context.Context, slot uint64) (uint64, error) {
query := `SELECT MAX(exec_block_number) FROM blocks WHERE slot <= $1`
res := uint64(0)
err := d.alloyReader.GetContext(ctx, &res, query, slot)
if err != nil {
if err == sql.ErrNoRows {
log.Warnf("no EL block found at or before slot %d", slot)
return 0, nil
}
return 0, fmt.Errorf("failed to get latest existing block height at or before slot %d: %w", slot, err)
}
return res, nil
}

func (d *DataAccessService) GetLatestBlockHeightsForEpoch(ctx context.Context, epoch uint64) ([]uint64, error) {
// use 2 epochs as safety margin
query := `
WITH recent_blocks AS (
SELECT slot, exec_block_number
FROM blocks
WHERE slot < $1
ORDER BY slot DESC
LIMIT $2 * 2
)
SELECT MAX(exec_block_number) OVER (ORDER BY slot) AS block
FROM recent_blocks
ORDER BY slot DESC
LIMIT $2`
res := []uint64{}
err := d.alloyReader.SelectContext(ctx, &res, query, (epoch+1)*utils.Config.Chain.ClConfig.SlotsPerEpoch, utils.Config.Chain.ClConfig.SlotsPerEpoch)
if err != nil {
return nil, fmt.Errorf("failed to get latest existing block heights for slots in epoch %d: %w", epoch, err)
}
return res, nil
}

func (d *DataAccessService) GetLatestExchangeRates() ([]t.EthConversionRate, error) {
result := []t.EthConversionRate{}

Expand Down
Loading
Loading