From 1055433fe6107dfd4aa420bb7f4a74185d848e35 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Tue, 2 Jan 2024 14:37:05 +0100 Subject: [PATCH] Add logger for the fetcher --- fetch/fetch.go | 27 +++++++++++++++++++++++---- fetch/fetch_test.go | 3 ++- fetch/options.go | 13 +++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 fetch/options.go diff --git a/fetch/fetch.go b/fetch/fetch.go index ab525b9b..3f418f51 100644 --- a/fetch/fetch.go +++ b/fetch/fetch.go @@ -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] @@ -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 { @@ -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 { @@ -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 } @@ -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 diff --git a/fetch/fetch_test.go b/fetch/fetch_test.go index ed3f5bdc..ed944e35 100644 --- a/fetch/fetch_test.go +++ b/fetch/fetch_test.go @@ -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) { @@ -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, diff --git a/fetch/options.go b/fetch/options.go new file mode 100644 index 00000000..1675cb81 --- /dev/null +++ b/fetch/options.go @@ -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 + } +}