Remove global loggers from components.
- Web API Cache - Recovery Middleware - Database upgrades - Address Generator
This commit is contained in:
parent
63ce069a88
commit
01b0df2d7a
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2021 The Decred developers
|
||||
// Copyright (c) 2021-2022 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@ -8,10 +8,11 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/decred/slog"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
func removeOldFeeTxUpgrade(db *bolt.DB) error {
|
||||
func removeOldFeeTxUpgrade(db *bolt.DB, log slog.Logger) error {
|
||||
log.Infof("Upgrading database to version %d", removeOldFeeTxVersion)
|
||||
|
||||
// Run the upgrade in a single database transaction so it can be safely
|
||||
|
||||
@ -8,10 +8,11 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/decred/slog"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
func ticketBucketUpgrade(db *bolt.DB) error {
|
||||
func ticketBucketUpgrade(db *bolt.DB, log slog.Logger) error {
|
||||
log.Infof("Upgrading database to version %d", ticketBucketVersion)
|
||||
|
||||
// Run the upgrade in a single database transaction so it can be safely
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2021 The Decred developers
|
||||
// Copyright (c) 2021-2022 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@ -7,10 +7,11 @@ package database
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/decred/slog"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
func altSignAddrUpgrade(db *bolt.DB) error {
|
||||
func altSignAddrUpgrade(db *bolt.DB, log slog.Logger) error {
|
||||
log.Infof("Upgrading database to version %d", altSignAddrVersion)
|
||||
|
||||
// Run the upgrade in a single database transaction so it can be safely
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2021 The Decred developers
|
||||
// Copyright (c) 2021-2022 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@ -7,6 +7,7 @@ package database
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/decred/slog"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
@ -37,7 +38,7 @@ const (
|
||||
|
||||
// upgrades maps between old database versions and the upgrade function to
|
||||
// upgrade the database to the next version.
|
||||
var upgrades = []func(tx *bolt.DB) error{
|
||||
var upgrades = []func(tx *bolt.DB, log slog.Logger) error{
|
||||
initialVersion: removeOldFeeTxUpgrade,
|
||||
removeOldFeeTxVersion: ticketBucketUpgrade,
|
||||
ticketBucketVersion: altSignAddrUpgrade,
|
||||
@ -77,7 +78,7 @@ func (vdb *VspDatabase) Upgrade(currentVersion uint32) error {
|
||||
|
||||
// Execute all necessary upgrades in order.
|
||||
for _, upgrade := range upgrades[currentVersion:] {
|
||||
err := upgrade(vdb.db)
|
||||
err := upgrade(vdb.db, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2020 The Decred developers
|
||||
// Copyright (c) 2020-2022 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@ -10,15 +10,17 @@ import (
|
||||
"github.com/decred/dcrd/chaincfg/v3"
|
||||
"github.com/decred/dcrd/hdkeychain/v3"
|
||||
"github.com/decred/dcrd/txscript/v4/stdaddr"
|
||||
"github.com/decred/slog"
|
||||
)
|
||||
|
||||
type addressGenerator struct {
|
||||
external *hdkeychain.ExtendedKey
|
||||
netParams *chaincfg.Params
|
||||
lastUsedIndex uint32
|
||||
log slog.Logger
|
||||
}
|
||||
|
||||
func newAddressGenerator(xPub string, netParams *chaincfg.Params, lastUsedIdx uint32) (*addressGenerator, error) {
|
||||
func newAddressGenerator(xPub string, netParams *chaincfg.Params, lastUsedIdx uint32, log slog.Logger) (*addressGenerator, error) {
|
||||
xPubKey, err := hdkeychain.NewKeyFromString(xPub, netParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -38,6 +40,7 @@ func newAddressGenerator(xPub string, netParams *chaincfg.Params, lastUsedIdx ui
|
||||
external: external,
|
||||
netParams: netParams,
|
||||
lastUsedIndex: lastUsedIdx,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -58,7 +61,7 @@ func (m *addressGenerator) NextAddress() (string, uint32, error) {
|
||||
if err != nil {
|
||||
if errors.Is(err, hdkeychain.ErrInvalidChild) {
|
||||
invalidChildren++
|
||||
log.Warnf("Generating address for index %d failed: %v", m.lastUsedIndex, err)
|
||||
m.log.Warnf("Generating address for index %d failed: %v", m.lastUsedIndex, err)
|
||||
// If this happens 3 times, something is seriously wrong, so
|
||||
// return an error.
|
||||
if invalidChildren > 2 {
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/decred/slog"
|
||||
"github.com/decred/vspd/database"
|
||||
"github.com/decred/vspd/rpc"
|
||||
"github.com/dustin/go-humanize"
|
||||
@ -21,6 +22,7 @@ type cache struct {
|
||||
data cacheData
|
||||
// mtx must be held to read/write cache data.
|
||||
mtx sync.RWMutex
|
||||
log slog.Logger
|
||||
}
|
||||
|
||||
type cacheData struct {
|
||||
@ -45,11 +47,12 @@ func (c *cache) getData() cacheData {
|
||||
}
|
||||
|
||||
// newCache creates a new cache and initializes it with static values.
|
||||
func newCache(signPubKey string) *cache {
|
||||
func newCache(signPubKey string, log slog.Logger) *cache {
|
||||
return &cache{
|
||||
data: cacheData{
|
||||
PubKey: signPubKey,
|
||||
},
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,9 +89,9 @@ func (c *cache) update(db *database.VspDatabase, dcrd rpc.DcrdConnect,
|
||||
|
||||
clients, failedConnections := wallets.Clients()
|
||||
if len(clients) == 0 {
|
||||
log.Error("Could not connect to any wallets")
|
||||
c.log.Error("Could not connect to any wallets")
|
||||
} else if len(failedConnections) > 0 {
|
||||
log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d",
|
||||
c.log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d",
|
||||
len(failedConnections), len(clients))
|
||||
}
|
||||
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
// Copyright (c) 2020-2022 The Decred developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package webapi
|
||||
|
||||
import (
|
||||
@ -10,6 +14,7 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/decred/slog"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -17,7 +22,7 @@ import (
|
||||
// request handlers. It logs the panic, a stack trace, and the full request
|
||||
// details. It also ensure the client receives a 500 response rather than no
|
||||
// response at all.
|
||||
func Recovery() gin.HandlerFunc {
|
||||
func Recovery(log slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
||||
@ -89,7 +89,7 @@ func Start(shutdownCtx context.Context, requestShutdown func(), shutdownWg *sync
|
||||
}
|
||||
|
||||
// Populate cached VSP stats before starting webserver.
|
||||
s.cache = newCache(base64.StdEncoding.EncodeToString(s.signPubKey))
|
||||
s.cache = newCache(base64.StdEncoding.EncodeToString(s.signPubKey), log)
|
||||
err = s.cache.update(vdb, dcrd, wallets)
|
||||
if err != nil {
|
||||
log.Errorf("Could not initialize VSP stats cache: %v", err)
|
||||
@ -105,7 +105,7 @@ func Start(shutdownCtx context.Context, requestShutdown func(), shutdownWg *sync
|
||||
if err != nil {
|
||||
return fmt.Errorf("db.GetFeeXPub error: %w", err)
|
||||
}
|
||||
s.addrGen, err = newAddressGenerator(feeXPub, config.NetParams, idx)
|
||||
s.addrGen, err = newAddressGenerator(feeXPub, config.NetParams, idx, log)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize fee address generator: %w", err)
|
||||
}
|
||||
@ -213,7 +213,7 @@ func (s *Server) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
|
||||
// Recovery middleware handles any go panics generated while processing web
|
||||
// requests. Ensures a 500 response is sent to the client rather than
|
||||
// sending no response at all.
|
||||
router.Use(Recovery())
|
||||
router.Use(Recovery(log))
|
||||
|
||||
if s.cfg.Debug {
|
||||
// Logger middleware outputs very detailed logging of webserver requests
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user