Skip to content

Commit

Permalink
servers/bitwindow: return error from tick
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 2, 2024
1 parent 834495b commit f03b646
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
7 changes: 0 additions & 7 deletions servers/bitwindow/bdk/bdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ func (w *Wallet) execWallet(ctx context.Context, args ...string) ([]byte, error)
return nil, err
}

compacted := bytes.NewBuffer(nil)
// Sensitive stuff! Avoid logging the response
if err := json.Compact(compacted, res); err != nil {
// Revert back to non-compacted
compacted = bytes.NewBuffer(res)
}

return res, nil
}

Expand Down
25 changes: 13 additions & 12 deletions servers/bitwindow/engines/bitcoind_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Parser struct {
//
// Should be started in a goroutine.
func (p *Parser) Run(ctx context.Context) error {
// Check every other second. We wouldn't wanna miss a nice rate, now would we
alertTicker := time.NewTicker(2 * time.Second)
defer alertTicker.Stop()

Expand All @@ -59,28 +58,28 @@ func (p *Parser) Run(ctx context.Context) error {
continue
}

// nolint:ineffassign
processing = true
p.handleTick(ctx)
if err := p.handleTick(ctx); err != nil {
zerolog.Ctx(ctx).Error().
Err(err).
Msgf("bitcoind_engine/parser: could not handle tick")
return err
}
processing = false
}
}
}

func (p *Parser) handleTick(ctx context.Context) {
func (p *Parser) handleTick(ctx context.Context) error {
if err := p.detectChainDeletion(ctx); err != nil {
zerolog.Ctx(ctx).Error().
Err(err).
Msgf("bitcoind_engine/parser: could not detect chain deletion")
return
return fmt.Errorf("detect chain deletion: %w", err)
}

// Get latest processed height
lastProcessedHeight, lastProcessedHash, err := blocks.LatestProcessedHeight(ctx, p.db)
if err != nil {
zerolog.Ctx(ctx).Error().
Err(err).
Msgf("bitcoind_engine/parser: could not get latest processed height")
return
return fmt.Errorf("get latest processed height: %w", err)
}

// Get current blockchain height
Expand All @@ -89,7 +88,7 @@ func (p *Parser) handleTick(ctx context.Context) {
zerolog.Ctx(ctx).Error().
Err(err).
Msgf("bitcoind_engine/parser: could not get current height")
return
return nil
}

if lastProcessedHeight == currentHeight && lastProcessedHash != currentHash {
Expand All @@ -107,6 +106,8 @@ func (p *Parser) handleTick(ctx context.Context) {
continue
}
}

return nil
}

func (p *Parser) processBlock(ctx context.Context, height int32) error {
Expand Down
3 changes: 1 addition & 2 deletions servers/bitwindow/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ require (
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
github.com/mattn/go-sqlite3 v1.14.24
)

require github.com/mattn/go-sqlite3 v1.14.24
6 changes: 4 additions & 2 deletions servers/bitwindow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func realMain(ctx context.Context) error {
db, err := database.New(ctx)
if err != nil {
log.Error().Err(err).Msg("init database")
return err
return fmt.Errorf("init database: %w", err)
}
defer db.Close()

Expand Down Expand Up @@ -141,11 +141,13 @@ func realMain(ctx context.Context) error {
}

bitcoinEngine := bitcoind_engine.New(proxy, db)
go bitcoinEngine.Run(ctx)

log.Info().Msgf("server: listening on %s", conf.APIHost)

errs := make(chan error)
go func() {
errs <- bitcoinEngine.Run(ctx)
}()
go func() {
errs <- srv.Serve(ctx, conf.APIHost)
}()
Expand Down

0 comments on commit f03b646

Please sign in to comment.