Skip to content

Commit f6091dc

Browse files
committed
feat: allow 'latest' search for slots, blocks & epochs
1 parent 0fb1389 commit f6091dc

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

backend/pkg/api/data_access/header.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"fmt"
77

8+
"github.com/doug-martin/goqu/v9"
89
t "github.com/gobitfly/beaconchain/pkg/api/types"
910
"github.com/gobitfly/beaconchain/pkg/commons/cache"
1011
"github.com/gobitfly/beaconchain/pkg/commons/log"
@@ -23,8 +24,19 @@ func (d *DataAccessService) GetLatestFinalizedEpoch(ctx context.Context) (uint64
2324
}
2425

2526
func (d *DataAccessService) GetLatestBlock(ctx context.Context) (uint64, error) {
26-
// @DATA-ACCESS implement
27-
return d.dummy.GetLatestBlock(ctx)
27+
ds := goqu.Dialect("postgres").
28+
From(goqu.T("blocks")).
29+
Select(goqu.MAX(goqu.C("exec_block_number")))
30+
31+
res, err := runQuery[uint64](ctx, d.readerDb, ds)
32+
if err != nil {
33+
if err == sql.ErrNoRows {
34+
log.Warnf("no EL block found%d")
35+
return 0, nil
36+
}
37+
return 0, fmt.Errorf("failed to get latest existing block height: %w", err)
38+
}
39+
return res, nil
2840
}
2941

3042
func (d *DataAccessService) GetBlockHeightAt(ctx context.Context, slot uint64) (uint64, error) {

backend/pkg/api/handlers/search.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ func init() {
127127
handlerFunc: handleSearchTransaction,
128128
},
129129
blockKey: {
130-
regex: types.ReInteger,
130+
regex: types.ReIntegerOrLatest,
131131
responseType: string(blockKey),
132132
handlerFunc: handleSearchBlock,
133133
},
134134
slotKey: {
135-
regex: types.ReInteger,
135+
regex: types.ReIntegerOrLatest,
136136
responseType: "slot",
137137
handlerFunc: handleSearchSlot,
138138
},
@@ -147,7 +147,7 @@ func init() {
147147
handlerFunc: handleSearchSlotByStateRoot,
148148
},
149149
epochKey: {
150-
regex: types.ReInteger,
150+
regex: types.ReIntegerOrLatest,
151151
responseType: string(epochKey),
152152
handlerFunc: handleSearchEpoch,
153153
},
@@ -376,6 +376,13 @@ func handleSearchTransaction(ctx context.Context, h *HandlerService, input strin
376376
}
377377

378378
func handleSearchBlock(ctx context.Context, h *HandlerService, input string, chainId uint64) (*types.SearchResult, error) {
379+
if input == "latest" {
380+
result, err := h.daService.GetLatestBlock(ctx)
381+
if err != nil {
382+
return nil, err
383+
}
384+
return asSearchResult(blockKey, chainId, &types.SearchBlock{BlockNumber: result}, nil)
385+
}
379386
blockNumber, err := strconv.ParseUint(input, 10, 64)
380387
if err != nil {
381388
return nil, err
@@ -385,6 +392,13 @@ func handleSearchBlock(ctx context.Context, h *HandlerService, input string, cha
385392
}
386393

387394
func handleSearchSlot(ctx context.Context, h *HandlerService, input string, chainId uint64) (*types.SearchResult, error) {
395+
if input == "latest" {
396+
result, err := h.daService.GetLatestSlot(ctx)
397+
if err != nil {
398+
return nil, err
399+
}
400+
return asSearchResult(slotKey, chainId, &types.SearchSlot{Slot: result}, nil)
401+
}
388402
slot, err := strconv.ParseUint(input, 10, 64)
389403
if err != nil {
390404
return nil, err
@@ -412,6 +426,14 @@ func handleSearchSlotByStateRoot(ctx context.Context, h *HandlerService, input s
412426
}
413427

414428
func handleSearchEpoch(ctx context.Context, h *HandlerService, input string, chainId uint64) (*types.SearchResult, error) {
429+
if input == "latest" {
430+
result, err := h.daService.GetLatestSlot(ctx)
431+
if err != nil {
432+
return nil, err
433+
}
434+
epoch := result / h.cfg.ClConfig.SlotsPerEpoch
435+
return asSearchResult(epochKey, chainId, &types.SearchEpoch{Epoch: epoch}, nil)
436+
}
415437
epoch, err := strconv.ParseUint(input, 10, 64)
416438
if err != nil {
417439
return nil, err

backend/pkg/api/types/regexes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "regexp"
55
var (
66
ReName = regexp.MustCompile(`^[a-zA-Z0-9_\-.\ ]*$`)
77
ReInteger = regexp.MustCompile(`^[0-9]+$`)
8+
ReIntegerOrLatest = regexp.MustCompile(`^(latest|[0-9]+)$`)
89
ReValidatorDashboardPublicId = regexp.MustCompile(`^v-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
910
ReValidatorPublicKeyWithPrefix = regexp.MustCompile(`^0x[0-9a-fA-F]{96}$`)
1011
ReValidatorPublicKey = regexp.MustCompile(`^(0x)?[0-9a-fA-F]{96}$`)

0 commit comments

Comments
 (0)