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

add block encoding param to block index api #1752

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 57 additions & 15 deletions api/indexer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
const Endpoint = "/indexer"

var (
ErrTxNotFound = errors.New("tx not found")
ErrTxNotFound = errors.New("tx not found")
ErrInvalidEncodingParameter = errors.New("invalid encoding parameter")
ErrUnsupportedEncoding = errors.New("unsupported encoding")

_ api.HandlerFactory[api.VM] = (*apiFactory)(nil)
)
Expand All @@ -46,59 +48,82 @@ func (f *apiFactory) New(vm api.VM) (api.Handler, error) {
}

type GetBlockRequest struct {
BlockID ids.ID `json:"blockID"`
BlockID ids.ID `json:"blockID"`
Encoding Encoding `json:"encoding"`
}

type GetBlockByHeightRequest struct {
Height uint64 `json:"height"`
Height uint64 `json:"height"`
Encoding Encoding `json:"encoding"`
}

type GetLatestBlockRequest struct {
Encoding Encoding `json:"encoding"`
}

type GetBlockResponse struct {
Block *chain.ExecutedBlock `json:"block"`
BlockBytes codec.Bytes `json:"blockBytes"`
Block any `json:"block"`
}

func (g *GetBlockResponse) setResponse(block *chain.ExecutedBlock) error {
g.Block = block
blockBytes, err := block.Marshal()
if err != nil {
return err
func (g *GetBlockResponse) setResponse(block *chain.ExecutedBlock, encoding Encoding) error {
switch encoding {
case JSON:
g.Block = block
case Hex:
blockBytes, err := block.Marshal()
if err != nil {
return err
}
g.Block = codec.Bytes(blockBytes)
default:
return ErrUnsupportedEncoding
}
g.BlockBytes = blockBytes
return nil
}

func (s *Server) GetBlock(req *http.Request, args *GetBlockRequest, reply *GetBlockResponse) error {
_, span := s.tracer.Start(req.Context(), "Indexer.GetBlock")
defer span.End()

if err := args.Encoding.Validate(); err != nil {
return err
}

block, err := s.indexer.GetBlock(args.BlockID)
if err != nil {
return err
}
return reply.setResponse(block)
return reply.setResponse(block, args.Encoding)
}

func (s *Server) GetBlockByHeight(req *http.Request, args *GetBlockByHeightRequest, reply *GetBlockResponse) error {
_, span := s.tracer.Start(req.Context(), "Indexer.GetBlockByHeight")
defer span.End()

if err := args.Encoding.Validate(); err != nil {
return err
}

block, err := s.indexer.GetBlockByHeight(args.Height)
if err != nil {
return err
}
return reply.setResponse(block)
return reply.setResponse(block, args.Encoding)
}

func (s *Server) GetLatestBlock(req *http.Request, _ *struct{}, reply *GetBlockResponse) error {
func (s *Server) GetLatestBlock(req *http.Request, args *GetLatestBlockRequest, reply *GetBlockResponse) error {
_, span := s.tracer.Start(req.Context(), "Indexer.GetLatestBlock")
defer span.End()

if err := args.Encoding.Validate(); err != nil {
return err
}

block, err := s.indexer.GetLatestBlock()
if err != nil {
return err
}
return reply.setResponse(block)
return reply.setResponse(block, args.Encoding)
}

type GetTxRequest struct {
Expand Down Expand Up @@ -143,3 +168,20 @@ func (s *Server) GetTx(req *http.Request, args *GetTxRequest, reply *GetTxRespon
reply.ErrorStr = errorStr
return nil
}

type Encoding string

var (
JSON Encoding = "json"
Hex Encoding = "hex"
)

func (e *Encoding) Validate() error {
if *e == "" {
*e = JSON
}
if *e != JSON && *e != Hex {
return ErrInvalidEncodingParameter
}
return nil
}
84 changes: 84 additions & 0 deletions api/indexer/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package indexer

import (
"encoding/json"
"fmt"
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk/chain/chaintest"
"github.com/ava-labs/hypersdk/codec"
)

func TestEncodingValidate(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

tests := []struct {
input Encoding
exp Encoding
expErr error
}{
{
input: JSON,
exp: JSON,
},
{
input: Hex,
exp: Hex,
},
{
input: Encoding(""),
exp: JSON,
},
{
input: Encoding("another"),
expErr: ErrInvalidEncodingParameter,
},
}

for _, test := range tests {
err := test.input.Validate()
if test.expErr != nil {
require.EqualError(t, ErrInvalidEncodingParameter, err.Error())
} else {
require.Equal(t, test.exp, test.input)
}
}
}

func TestBlockResponse(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we test via httptest rather than calling individual functions this way?

ex of setting up httptest server: https://github.com/ava-labs/hypersdk/blob/main/tests/integration/integration.go#L236

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two ways we could go testing here imo:

  • test the server/client w/ mocked indexer using httptest and test the indexer separately
  • test the indexer + server/client solely through the server/client

Could we add basic tests here that use httptest and the server/client with a mocked of the indexer (will need to change indexer to interface as it's used in server/client)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also avoid this pattern of using t.Run within a single unit test. I think this makes sense for table tests (and this could be changed to a table test imo), but less so in this pattern with mostly independent test logic:

t.Run("JSON", func(t *testing.T) {
	// independent test logic
})
t.Run("Hex", func(t *testing.T) {
	// independent test logic
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea by the way! The client was broken because of my changes !

blocks := chaintest.GenerateEmptyExecutedBlocks(require.New(t), ids.GenerateTestID(), 0, 0, 0, 1)
t.Run("JSON", func(t *testing.T) {
require := require.New(t)
r := GetBlockResponse{}
err := r.setResponse(blocks[0], JSON)
require.NoError(err)
marshaledBlock, err := json.Marshal(blocks[0])
require.NoError(err)
res, err := json.Marshal(r)
require.NoError(err)
require.Equal([]byte(fmt.Sprintf(`{"block":%s}`, string(marshaledBlock))), res)
})
t.Run("Hex", func(t *testing.T) {
require := require.New(t)
r := GetBlockResponse{}
err := r.setResponse(blocks[0], Hex)
require.NoError(err)
blockBytes, err := blocks[0].Marshal()
require.NoError(err)
res, err := json.Marshal(r)
require.NoError(err)
marshaledBlock, err := codec.Bytes(blockBytes).MarshalText()
require.NoError(err)
require.Equal([]byte(fmt.Sprintf(`{"block":"%s"}`, string(marshaledBlock))), res)
})
t.Run("Unknown", func(t *testing.T) {
require := require.New(t)
r := GetBlockResponse{}
err := r.setResponse(blocks[0], Encoding("unknown"))
require.EqualError(err, ErrUnsupportedEncoding.Error())
})
}
Loading