Skip to content

Commit

Permalink
Add logger for the fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Jan 2, 2024
1 parent 653d23a commit 1055433
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
27 changes: 23 additions & 4 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,36 @@ import (

"github.com/gnolang/gno/tm2/pkg/bft/types"
storageErrors "github.com/gnolang/tx-indexer/storage/errors"
"go.uber.org/zap"
)

type Fetcher struct {
storage Storage
client Client

logger *zap.Logger
queryInterval time.Duration // block query interval
}

// NewFetcher creates a new transaction result fetcher instance
// that gets data from a remote chain
// TODO add logger
func NewFetcher(
storage Storage,
client Client,
opts ...Option,
) *Fetcher {
return &Fetcher{
f := &Fetcher{
storage: storage,
client: client,
queryInterval: 1 * time.Second,
logger: zap.NewNop(),
}

for _, opt := range opts {
opt(f)
}

return f
}

// FetchTransactions runs the transaction fetcher [BLOCKING]
Expand Down Expand Up @@ -77,7 +86,8 @@ func (f *Fetcher) FetchTransactions(ctx context.Context) error {
for {
select {
case <-ctx.Done():
// TODO log
f.logger.Info("Stopping fetch service...")

return nil
case <-ticker.C:
if currentHeight, err = catchupWithChain(currentHeight); err != nil {
Expand All @@ -89,7 +99,6 @@ func (f *Fetcher) FetchTransactions(ctx context.Context) error {

// fetchAndSaveBlockData commits the block data to storage
func (f *Fetcher) fetchAndSaveBlockData(blockNum int64) error {
// TODO log
// Get block info from the chain
block, err := f.client.GetBlock(blockNum)
if err != nil {
Expand All @@ -100,8 +109,12 @@ func (f *Fetcher) fetchAndSaveBlockData(blockNum int64) error {
return fmt.Errorf("unable to save block, %w", saveErr)
}

f.logger.Info("Saved block data", zap.Int64("number", block.Block.Height))

// Skip empty blocks
if block.Block.NumTxs == 0 {
f.logger.Debug("Block is empty", zap.Int64("number", block.Block.Height))

return nil
}

Expand All @@ -123,6 +136,12 @@ func (f *Fetcher) fetchAndSaveBlockData(blockNum int64) error {
if err := f.storage.SaveTx(result); err != nil {
return fmt.Errorf("unable to save tx, %w", err)
}

f.logger.Info(
"Saved block tx",
zap.Int64("number", block.Block.Height),
zap.String("hash", string(tx.Hash())),
)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion fetch/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
storageErrors "github.com/gnolang/tx-indexer/storage/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestNodeFetcher_FetchTransactions_Invalid(t *testing.T) {
Expand All @@ -34,7 +35,7 @@ func TestNodeFetcher_FetchTransactions_Invalid(t *testing.T) {
)

// Create the fetcher
f := NewFetcher(mockStorage, &mockClient{})
f := NewFetcher(mockStorage, &mockClient{}, WithLogger(zap.NewNop()))

assert.ErrorIs(
t,
Expand Down
13 changes: 13 additions & 0 deletions fetch/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fetch

import "go.uber.org/zap"

type Option func(f *Fetcher)

// WithLogger sets the logger to be used
// with the fetcher
func WithLogger(logger *zap.Logger) Option {
return func(f *Fetcher) {
f.logger = logger
}
}

0 comments on commit 1055433

Please sign in to comment.