From ee4a440534e1a4ec754dd66c7f1a8ff196f12e3f Mon Sep 17 00:00:00 2001 From: jholdstock Date: Fri, 15 Sep 2023 15:07:09 +0100 Subject: [PATCH] multi: Move vspd to internal package. --- cmd/vspd/main.go | 13 +++++++++++-- {cmd => internal}/vspd/spentticket.go | 4 ++-- {cmd => internal}/vspd/vspd.go | 28 +++++++++++---------------- 3 files changed, 24 insertions(+), 21 deletions(-) rename {cmd => internal}/vspd/spentticket.go (98%) rename {cmd => internal}/vspd/vspd.go (96%) diff --git a/cmd/vspd/main.go b/cmd/vspd/main.go index 50fe3be..3229d06 100644 --- a/cmd/vspd/main.go +++ b/cmd/vspd/main.go @@ -15,10 +15,19 @@ import ( "github.com/decred/vspd/database" "github.com/decred/vspd/internal/config" "github.com/decred/vspd/internal/version" + "github.com/decred/vspd/internal/vspd" "github.com/decred/vspd/internal/webapi" "github.com/decred/vspd/rpc" ) +const ( + // maxVoteChangeRecords defines how many vote change records will be stored + // for each ticket. The limit is in place to mitigate DoS attacks on server + // storage space. When storing a new record breaches this limit, the oldest + // record in the database is deleted. + maxVoteChangeRecords = 10 +) + func main() { os.Exit(run()) } @@ -109,10 +118,10 @@ func run() int { }() // Start vspd. - vspd := newVspd(cfg.network, log, db, dcrd, wallets, blockNotifChan) + vspd := vspd.New(cfg.network, log, db, dcrd, wallets, blockNotifChan) shutdownWg.Add(1) go func() { - vspd.run(ctx) + vspd.Run(ctx) shutdownWg.Done() }() diff --git a/cmd/vspd/spentticket.go b/internal/vspd/spentticket.go similarity index 98% rename from cmd/vspd/spentticket.go rename to internal/vspd/spentticket.go index c130a54..de79e8c 100644 --- a/cmd/vspd/spentticket.go +++ b/internal/vspd/spentticket.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file.package main -package main +package vspd import ( "context" @@ -64,7 +64,7 @@ func (s *spentTicket) missed() bool { // against the block filters of the mainchain blocks between the provided start // block and the current best block. Returns any found spent tickets and the // height of the most recent scanned block. -func (v *vspd) findSpentTickets(ctx context.Context, toCheck database.TicketList, +func (v *Vspd) findSpentTickets(ctx context.Context, toCheck database.TicketList, startHeight int64) ([]spentTicket, int64, error) { dcrdClient, _, err := v.dcrd.Client() if err != nil { diff --git a/cmd/vspd/vspd.go b/internal/vspd/vspd.go similarity index 96% rename from cmd/vspd/vspd.go rename to internal/vspd/vspd.go index a7dedd6..f833ec8 100644 --- a/cmd/vspd/vspd.go +++ b/internal/vspd/vspd.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package main +package vspd import ( "context" @@ -24,12 +24,6 @@ const ( // ticket purchase or a fee transaction to be final. requiredConfs = 6 - // maxVoteChangeRecords defines how many vote change records will be stored - // for each ticket. The limit is in place to mitigate DoS attacks on server - // storage space. When storing a new record breaches this limit, the oldest - // record in the database is deleted. - maxVoteChangeRecords = 10 - // consistencyInterval is the time period between wallet consistency checks. consistencyInterval = 30 * time.Minute @@ -37,7 +31,7 @@ const ( dcrdInterval = time.Second * 15 ) -type vspd struct { +type Vspd struct { network *config.Network log slog.Logger db *database.VspDatabase @@ -51,10 +45,10 @@ type vspd struct { lastScannedBlock int64 } -func newVspd(network *config.Network, log slog.Logger, db *database.VspDatabase, - dcrd rpc.DcrdConnect, wallets rpc.WalletConnect, blockNotifChan chan *wire.BlockHeader) *vspd { +func New(network *config.Network, log slog.Logger, db *database.VspDatabase, + dcrd rpc.DcrdConnect, wallets rpc.WalletConnect, blockNotifChan chan *wire.BlockHeader) *Vspd { - v := &vspd{ + v := &Vspd{ network: network, log: log, db: db, @@ -67,7 +61,7 @@ func newVspd(network *config.Network, log slog.Logger, db *database.VspDatabase, return v } -func (v *vspd) run(ctx context.Context) { +func (v *Vspd) Run(ctx context.Context) { // Run database integrity checks to ensure all data in database is present // and up-to-date. err := v.checkDatabaseIntegrity(ctx) @@ -137,7 +131,7 @@ func (v *vspd) run(ctx context.Context) { // checkDatabaseIntegrity starts the process of ensuring that all data expected // to be in the database is present and up to date. -func (v *vspd) checkDatabaseIntegrity(ctx context.Context) error { +func (v *Vspd) checkDatabaseIntegrity(ctx context.Context) error { err := v.checkPurchaseHeights() if err != nil { return fmt.Errorf("checkPurchaseHeights error: %w", err) @@ -154,7 +148,7 @@ func (v *vspd) checkDatabaseIntegrity(ctx context.Context) error { // checkPurchaseHeights ensures a purchase height is recorded for all confirmed // tickets in the database. This is necessary because of an old bug which, in // some circumstances, would prevent purchase height from being stored. -func (v *vspd) checkPurchaseHeights() error { +func (v *Vspd) checkPurchaseHeights() error { missing, err := v.db.GetMissingPurchaseHeight() if err != nil { // Cannot proceed if this fails, return. @@ -198,7 +192,7 @@ func (v *vspd) checkPurchaseHeights() error { // checkRevoked ensures that any tickets in the database with outcome set to // revoked are updated to either expired or missed. -func (v *vspd) checkRevoked(ctx context.Context) error { +func (v *Vspd) checkRevoked(ctx context.Context) error { revoked, err := v.db.GetRevokedTickets() if err != nil { return fmt.Errorf("db.GetRevoked error: %w", err) @@ -254,7 +248,7 @@ func (v *vspd) checkRevoked(ctx context.Context) error { // blockConnected is called once when vspd starts up, and once each time a // blockconnected notification is received from dcrd. -func (v *vspd) blockConnected(ctx context.Context) { +func (v *Vspd) blockConnected(ctx context.Context) { const funcName = "blockConnected" dcrdClient, _, err := v.dcrd.Client() @@ -533,7 +527,7 @@ func (v *vspd) blockConnected(ctx context.Context) { // checkWalletConsistency will retrieve all votable tickets from the database // and ensure they are all added to voting wallets with the correct vote // choices. -func (v *vspd) checkWalletConsistency(ctx context.Context) { +func (v *Vspd) checkWalletConsistency(ctx context.Context) { const funcName = "checkWalletConsistency" v.log.Info("Checking voting wallet consistency")