Skip to content

Commit

Permalink
Use main logger in handler and fetcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Jun 29, 2022
1 parent f40e112 commit 55c83ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
14 changes: 6 additions & 8 deletions processor/blockprocessor/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func InitializeLedgerSimple(ctx context.Context, logger *log.Logger, round uint6
return fmt.Errorf("InitializeLedgerSimple() err: indexer data directory missing")
}
// create algod client
bot, err = getFetcher(opts)
bot, err = getFetcher(logger, opts)
if err != nil {
return fmt.Errorf("InitializeLedgerSimple() err: %w", err)
}
Expand All @@ -52,7 +52,7 @@ func InitializeLedgerSimple(ctx context.Context, logger *log.Logger, round uint6
return nil
}
bot.SetNextRound(proc.NextRoundToProcess())
handler := blockHandler(round, proc, cf, 1*time.Second)
handler := blockHandler(logger, round, proc, cf, 1*time.Second)
bot.SetBlockHandler(handler)

logger.Info("Starting ledger migration.")
Expand Down Expand Up @@ -156,10 +156,10 @@ func InitializeLedgerFastCatchup(ctx context.Context, logger *log.Logger, catchp

// blockHandler creates a handler complying to the fetcher block handler interface. In case of a failure it keeps
// attempting to add the block until the fetcher shuts down.
func blockHandler(dbRound uint64, proc processor.Processor, cancel context.CancelFunc, retryDelay time.Duration) func(context.Context, *rpcs.EncodedBlockCert) error {
func blockHandler(logger *log.Logger, dbRound uint64, proc processor.Processor, cancel context.CancelFunc, retryDelay time.Duration) func(context.Context, *rpcs.EncodedBlockCert) error {
return func(ctx context.Context, block *rpcs.EncodedBlockCert) error {
for {
err := handleBlock(block, proc)
err := handleBlock(logger, block, proc)
if err == nil {
if uint64(block.Block.Round()) == dbRound {
// migration completes
Expand All @@ -180,8 +180,7 @@ func blockHandler(dbRound uint64, proc processor.Processor, cancel context.Cance
}
}

func handleBlock(block *rpcs.EncodedBlockCert, proc processor.Processor) error {
logger := log.New()
func handleBlock(logger *log.Logger, block *rpcs.EncodedBlockCert, proc processor.Processor) error {
err := proc.Process(block)
if err != nil {
logger.WithError(err).Errorf(
Expand All @@ -205,8 +204,7 @@ func getGenesis(client *algod.Client) (bookkeeping.Genesis, error) {

return res, nil
}
func getFetcher(opts *idb.IndexerDbOptions) (fetcher.Fetcher, error) {
logger := log.New()
func getFetcher(logger *log.Logger, opts *idb.IndexerDbOptions) (fetcher.Fetcher, error) {
var err error
var bot fetcher.Fetcher
if opts.AlgodDataDir != "" {
Expand Down
7 changes: 3 additions & 4 deletions processor/blockprocessor/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/jarcoal/httpmock"
"github.com/sirupsen/logrus"
test2 "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -65,17 +64,17 @@ func TestRunMigration(t *testing.T) {
}

// migrate 3 rounds
err = InitializeLedgerSimple(context.Background(), logrus.New(), 3, &opts)
assert.NoError(t, err)
log, _ := test2.NewNullLogger()
err = InitializeLedgerSimple(context.Background(), log, 3, &opts)
assert.NoError(t, err)
l, err := util.MakeLedger(log, false, &genesis, opts.IndexerDatadir)
assert.NoError(t, err)
// check 3 rounds written to ledger
assert.Equal(t, uint64(3), uint64(l.Latest()))
l.Close()

// migration continues from last round
err = InitializeLedgerSimple(context.Background(), logrus.New(), 6, &opts)
err = InitializeLedgerSimple(context.Background(), log, 6, &opts)
assert.NoError(t, err)

l, err = util.MakeLedger(log, false, &genesis, opts.IndexerDatadir)
Expand Down

0 comments on commit 55c83ae

Please sign in to comment.