Skip to content

Commit

Permalink
feat: Added primary indexes and GraphQL interface. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro authored Mar 18, 2024
1 parent 101fcfa commit ac3eda5
Show file tree
Hide file tree
Showing 42 changed files with 6,075 additions and 656 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fixalign:
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
fieldalignment -fix $(filter-out $@,$(MAKECMDGOALS)) # the full package name (not path!)

.PHONY: generate
generate:
go generate ./...

.PHONY: test
test:
go clean -testcache
Expand Down
10 changes: 6 additions & 4 deletions client/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ type Batch struct {
}

// AddBlockRequest adds a new block request (block fetch) to the batch
func (b *Batch) AddBlockRequest(blockNum int64) error {
if _, err := b.batch.Block(&blockNum); err != nil {
func (b *Batch) AddBlockRequest(blockNum uint64) error {
bn := int64(blockNum)
if _, err := b.batch.Block(&bn); err != nil {
return fmt.Errorf("unable to add block request, %w", err)
}

return nil
}

// AddBlockResultsRequest adds a new block results request (block results fetch) to the batch
func (b *Batch) AddBlockResultsRequest(blockNum int64) error {
if _, err := b.batch.BlockResults(&blockNum); err != nil {
func (b *Batch) AddBlockResultsRequest(blockNum uint64) error {
bn := int64(blockNum)
if _, err := b.batch.BlockResults(&bn); err != nil {
return fmt.Errorf("unable to add block results request, %w", err)
}

Expand Down
17 changes: 11 additions & 6 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

rpcClient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
core_types "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"

clientTypes "github.com/gnolang/tx-indexer/client/types"
)

Expand All @@ -27,26 +28,30 @@ func (c *Client) CreateBatch() clientTypes.Batch {
}
}

func (c *Client) GetLatestBlockNumber() (int64, error) {
func (c *Client) GetLatestBlockNumber() (uint64, error) {
status, err := c.client.Status()
if err != nil {
return 0, fmt.Errorf("unable to get chain status, %w", err)
}

return status.SyncInfo.LatestBlockHeight, nil
return uint64(status.SyncInfo.LatestBlockHeight), nil
}

func (c *Client) GetBlock(blockNum int64) (*core_types.ResultBlock, error) {
block, err := c.client.Block(&blockNum)
func (c *Client) GetBlock(blockNum uint64) (*core_types.ResultBlock, error) {
bn := int64(blockNum)

block, err := c.client.Block(&bn)
if err != nil {
return nil, fmt.Errorf("unable to get block, %w", err)
}

return block, nil
}

func (c *Client) GetBlockResults(blockNum int64) (*core_types.ResultBlockResults, error) {
results, err := c.client.BlockResults(&blockNum)
func (c *Client) GetBlockResults(blockNum uint64) (*core_types.ResultBlockResults, error) {
bn := int64(blockNum)

results, err := c.client.BlockResults(&bn)
if err != nil {
return nil, fmt.Errorf("unable to get block results, %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package types
// Batch defines the interface for the client batch
type Batch interface {
// AddBlockRequest adds a new block request (block fetch) to the batch
AddBlockRequest(int64) error
AddBlockRequest(uint64) error

// AddBlockResultsRequest adds a new block results request (block results fetch) to the batch
AddBlockResultsRequest(int64) error
AddBlockResultsRequest(uint64) error

// Execute sends the batch off for processing by the node
Execute() ([]any, error)
Expand Down
16 changes: 10 additions & 6 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"flag"
"fmt"

"github.com/go-chi/chi/v5"
"github.com/peterbourgon/ff/v3/ffcli"
"go.uber.org/zap"

"github.com/gnolang/tx-indexer/client"
"github.com/gnolang/tx-indexer/events"
"github.com/gnolang/tx-indexer/fetch"
"github.com/gnolang/tx-indexer/serve"
"github.com/gnolang/tx-indexer/serve/graph"
"github.com/gnolang/tx-indexer/storage"
)

Expand Down Expand Up @@ -141,20 +143,26 @@ func (c *startCfg) exec(ctx context.Context) error {

// Create the JSON-RPC service
j := setupJSONRPC(
c.listenAddress,
db,
em,
logger,
)

mux := chi.NewMux()
mux = j.SetupRoutes(mux)
mux = graph.Setup(db, em, mux)

// Create the HTTP server
hs := serve.NewHTTPServer(mux, c.listenAddress, logger.Named("http-server"))

// Create a new waiter
w := newWaiter(ctx)

// Add the fetcher service
w.add(f.FetchChainData)

// Add the JSON-RPC service
w.add(j.Serve)
w.add(hs.Serve)

// Wait for the services to stop
return errors.Join(
Expand All @@ -165,7 +173,6 @@ func (c *startCfg) exec(ctx context.Context) error {

// setupJSONRPC sets up the JSONRPC instance
func setupJSONRPC(
listenAddress string,
db *storage.Pebble,
em *events.Manager,
logger *zap.Logger,
Expand All @@ -175,9 +182,6 @@ func setupJSONRPC(
serve.WithLogger(
logger.Named("json-rpc"),
),
serve.WithListenAddress(
listenAddress,
),
)

// Transaction handlers
Expand Down
8 changes: 4 additions & 4 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (f *Fetcher) FetchChainData(ctx context.Context) error {
for _, gap := range gaps {
f.logger.Info(
"Fetching range",
zap.Int64("from", gap.from),
zap.Int64("to", gap.to),
zap.Uint64("from", gap.from),
zap.Uint64("to", gap.to),
)

// Spawn worker
Expand Down Expand Up @@ -223,8 +223,8 @@ func (f *Fetcher) FetchChainData(ctx context.Context) error {

f.logger.Info(
"Added to batch block and tx data for range",
zap.Int64("from", item.chunkRange.from),
zap.Int64("to", item.chunkRange.to),
zap.Uint64("from", item.chunkRange.from),
zap.Uint64("to", item.chunkRange.to),
)

// Save the latest height data
Expand Down
Loading

0 comments on commit ac3eda5

Please sign in to comment.