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

Feature: bulk request of block data #136

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cosmossdk.io/math v1.1.2
github.com/celestiaorg/celestia-app v1.0.0
github.com/cosmos/cosmos-sdk v0.46.14
github.com/cosmos/ibc-go/v6 v6.2.0
github.com/dipdup-io/workerpool v0.0.4
github.com/dipdup-net/go-lib v0.3.5
github.com/dipdup-net/indexer-sdk v0.0.3
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
github.com/cosmos/gogoproto v1.4.11 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.6 // indirect
github.com/cosmos/ibc-go/v6 v6.2.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.1 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
Expand Down
3 changes: 0 additions & 3 deletions internal/storage/mock/address.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/balance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/block.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/block_stats.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/constant.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/denom_metadata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/generic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/message.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/namespace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/stats.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/tx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/mock/validator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/types/event_type_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/types/module_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/types/msg_address_type_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/types/msg_type_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/storage/types/status_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions pkg/indexer/receiver/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package receiver

import (
"context"
"sort"
"time"

ic "github.com/dipdup-io/celestia-indexer/pkg/indexer/config"
nodeTypes "github.com/dipdup-io/celestia-indexer/pkg/node/types"
"github.com/dipdup-io/celestia-indexer/pkg/types"
"github.com/dipdup-net/indexer-sdk/pkg/modules/stopper"
"github.com/pkg/errors"
"go.uber.org/mock/gomock"
"sort"
"time"
)

func (s *ModuleTestSuite) TestModule_SyncGracefullyStops() {
Expand Down Expand Up @@ -82,14 +83,13 @@ func (s *ModuleTestSuite) TestModule_SyncReadsBlocks() {

for i := types.Level(1); i <= blockCount; i++ {
s.api.EXPECT().
Block(gomock.Any(), i).
Return(getResultBlock(i), nil).
MaxTimes(1)

s.api.EXPECT().
BlockResults(gomock.Any(), i).
Return(getResultBlockResults(i), nil).
MaxTimes(1)
BlockData(gomock.Any(), i).
Return(types.BlockData{
ResultBlock: getResultBlock(i),
ResultBlockResults: getResultBlockResults(i),
}, nil).
MaxTimes(1).
MinTimes(1)
}
})

Expand Down
16 changes: 1 addition & 15 deletions pkg/indexer/receiver/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *Module) worker(ctx context.Context, level types.Level) {
default:
}

block, err := r.blockData(ctx, level)
block, err := r.api.BlockData(ctx, level)
if err != nil {
if errors.Is(err, context.Canceled) {
return
Expand All @@ -47,17 +47,3 @@ func (r *Module) worker(ctx context.Context, level types.Level) {
Msg("received block")
r.blocks <- result
}

func (r *Module) blockData(ctx context.Context, level types.Level) (types.BlockData, error) {
block, err := r.api.Block(ctx, level)
if err != nil {
return types.BlockData{}, err
}

blockResults, err := r.api.BlockResults(ctx, level)
if err != nil {
return types.BlockData{}, err
}

return types.BlockData{ResultBlock: block, ResultBlockResults: blockResults}, nil
}
1 change: 1 addition & 0 deletions pkg/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Api interface {
Block(ctx context.Context, level pkgTypes.Level) (pkgTypes.ResultBlock, error)
BlockResults(ctx context.Context, level pkgTypes.Level) (pkgTypes.ResultBlockResults, error)
Genesis(ctx context.Context) (types.Genesis, error)
BlockData(ctx context.Context, level pkgTypes.Level) (pkgTypes.BlockData, error)
}

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
Expand Down
42 changes: 39 additions & 3 deletions pkg/node/mock/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 45 additions & 1 deletion pkg/node/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package rpc

import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"time"

"github.com/dipdup-io/celestia-indexer/pkg/node/types"
"github.com/goccy/go-json"

"github.com/pkg/errors"
Expand Down Expand Up @@ -50,7 +52,6 @@ func NewAPI(cfg config.DataSource) API {
}
}

// get -
func (api *API) get(ctx context.Context, path string, args map[string]string, output any) error {
u, err := url.Parse(api.cfg.URL)
if err != nil {
Expand Down Expand Up @@ -99,6 +100,49 @@ func (api *API) get(ctx context.Context, path string, args map[string]string, ou
return err
}

func (api *API) post(ctx context.Context, requests []types.Request, output any) error {
u, err := url.Parse(api.cfg.URL)
if err != nil {
return err
}

body := new(bytes.Buffer)
if err := json.NewEncoder(body).Encode(requests); err != nil {
return errors.Wrap(err, "invalid bulk post request")
}

if api.rateLimit != nil {
if err := api.rateLimit.Wait(ctx); err != nil {
return err
}
}

start := time.Now()

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), body)
if err != nil {
return err
}

response, err := api.client.Do(req)
if err != nil {
return err
}
defer closeWithLogError(response.Body, api.log)

api.log.Trace().
Int64("ms", time.Since(start).Milliseconds()).
Str("url", u.String()).
Msg("post request")

if response.StatusCode != http.StatusOK {
return errors.Errorf("invalid status: %d", response.StatusCode)
}

err = json.NewDecoder(response.Body).DecodeWithOption(output)
return err
}

func closeWithLogError(stream io.ReadCloser, log zerolog.Logger) {
if _, err := io.Copy(io.Discard, stream); err != nil {
log.Err(err).Msg("api copy GET body response to discard")
Expand Down
Loading
Loading