diff --git a/internal/storage/postgres/search.go b/internal/storage/postgres/search.go index 5f6855b..87f404a 100644 --- a/internal/storage/postgres/search.go +++ b/internal/storage/postgres/search.go @@ -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" @@ -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)). diff --git a/internal/storage/postgres/search_test.go b/internal/storage/postgres/search_test.go index 56f8f72..b4f62e3 100644 --- a/internal/storage/postgres/search_test.go +++ b/internal/storage/postgres/search_test.go @@ -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()