Skip to content

Commit

Permalink
Feature: search block by height
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Nov 5, 2024
1 parent 928ba27 commit 1bad83c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/storage/postgres/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/base64"
"encoding/hex"
"strconv"

"github.com/celenium-io/astria-indexer/internal/astria"
"github.com/celenium-io/astria-indexer/internal/storage"
Expand Down Expand Up @@ -39,6 +40,15 @@ func (s *Search) Search(ctx context.Context, query string) (results []storage.Se

searchQuery = searchQuery.UnionAll(bridgeQuery)

if height, err := strconv.ParseInt(query, 10, 64); err == nil {
heightQuery := s.db.DB().NewSelect().
Model((*storage.Block)(nil)).
ColumnExpr("id, encode(hash, 'hex') as value, 'block' as type").
Where("height = ?", height)

searchQuery = searchQuery.UnionAll(heightQuery)
}

if hash, err := hex.DecodeString(query); err == nil {
blockQuery := s.db.DB().NewSelect().
Model((*storage.Block)(nil)).
Expand Down
13 changes: 13 additions & 0 deletions internal/storage/postgres/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ func (s *StorageTestSuite) TestSearchBlock() {
s.Require().EqualValues("block", result.Type)
}

func (s *StorageTestSuite) TestSearchBlockByHeight() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

results, err := s.storage.Search.Search(ctx, "7965")
s.Require().NoError(err)
s.Require().Len(results, 1)

result := results[0]
s.Require().EqualValues("b15d072afc508558b3e962060c701a695af5d6a041d4a25c63240bbff5064b3b", result.Value)
s.Require().EqualValues("block", result.Type)
}

func (s *StorageTestSuite) TestSearchTx() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()
Expand Down

0 comments on commit 1bad83c

Please sign in to comment.