diff --git a/internal/bus/chainsubscriber.go b/internal/bus/chainsubscriber.go index 9445e2f19..23da73cfa 100644 --- a/internal/bus/chainsubscriber.go +++ b/internal/bus/chainsubscriber.go @@ -200,7 +200,7 @@ func (s *chainSubscriber) applyChainUpdate(tx sql.ChainUpdateTx, cau chain.Apply cau.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool) { if err != nil { return - } else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil { + } else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil { err = lookupErr return } else if !known { @@ -217,7 +217,7 @@ func (s *chainSubscriber) applyChainUpdate(tx sql.ChainUpdateTx, cau chain.Apply cau.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) { if err != nil { return - } else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil { + } else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil { err = lookupErr return } else if !known { @@ -268,7 +268,7 @@ func (s *chainSubscriber) revertChainUpdate(tx sql.ChainUpdateTx, cru chain.Reve cru.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, _ bool) { if err != nil { return - } else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil { + } else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil { err = lookupErr return } else if !known { @@ -285,7 +285,7 @@ func (s *chainSubscriber) revertChainUpdate(tx sql.ChainUpdateTx, cru chain.Reve cru.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) { if err != nil { return - } else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil { + } else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil { err = lookupErr return } else if !known { diff --git a/stores/sql/chain.go b/stores/sql/chain.go index 990c2c4cf..c872adfd0 100644 --- a/stores/sql/chain.go +++ b/stores/sql/chain.go @@ -205,10 +205,9 @@ func FileContractElement(ctx context.Context, tx sql.Tx, fcid types.FileContract } func IsKnownContract(ctx context.Context, tx sql.Tx, fcid types.FileContractID) (known bool, _ error) { - err := tx.QueryRow(ctx, "SELECT 1 FROM contracts WHERE fcid = ?", FileContractID(fcid)).Scan(&known) - if errors.Is(err, dsql.ErrNoRows) { - return false, nil - } else if err != nil { + err := tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM contracts WHERE fcid = ?)", FileContractID(fcid)). + Scan(&known) + if err != nil { return false, err } return known, nil @@ -221,7 +220,7 @@ func RecordContractRenewal(ctx context.Context, tx sql.Tx, oldFCID, newFCID type } _, err = tx.Exec(ctx, "UPDATE contracts SET contracts.renewed_from = ? WHERE contracts.fcid = ?", FileContractID(oldFCID), FileContractID(newFCID)) if err != nil { - return fmt.Errorf("failed to update renewed_to of new contract: %w", err) + return fmt.Errorf("failed to update renewed_from of new contract: %w", err) } return nil } diff --git a/stores/sql/mysql/chain.go b/stores/sql/mysql/chain.go index 1d2683075..8d854fb27 100644 --- a/stores/sql/mysql/chain.go +++ b/stores/sql/mysql/chain.go @@ -212,10 +212,6 @@ func (c chainUpdateTx) FileContractElement(fcid types.FileContractID) (types.V2F } func (c chainUpdateTx) IsKnownContract(fcid types.FileContractID) (bool, error) { - if c.known == nil { - c.known = make(map[types.FileContractID]bool) - } - if relevant, ok := c.known[fcid]; ok { return relevant, nil } diff --git a/stores/sql/mysql/main.go b/stores/sql/mysql/main.go index 8d856fe8c..7dba272ec 100644 --- a/stores/sql/mysql/main.go +++ b/stores/sql/mysql/main.go @@ -654,9 +654,10 @@ func (tx *MainDatabaseTx) Peers(ctx context.Context) ([]syncer.PeerInfo, error) func (tx *MainDatabaseTx) ProcessChainUpdate(ctx context.Context, fn func(ssql.ChainUpdateTx) error) error { return fn(&chainUpdateTx{ - ctx: ctx, - tx: tx, - l: tx.log.Named("ProcessChainUpdate"), + ctx: ctx, + known: make(map[types.FileContractID]bool), + tx: tx, + l: tx.log.Named("ProcessChainUpdate"), }) } diff --git a/stores/sql/sqlite/chain.go b/stores/sql/sqlite/chain.go index f3abf5353..cf642e8fe 100644 --- a/stores/sql/sqlite/chain.go +++ b/stores/sql/sqlite/chain.go @@ -213,10 +213,6 @@ func (c chainUpdateTx) FileContractElement(fcid types.FileContractID) (types.V2F } func (c chainUpdateTx) IsKnownContract(fcid types.FileContractID) (bool, error) { - if c.known == nil { - c.known = make(map[types.FileContractID]bool) - } - if relevant, ok := c.known[fcid]; ok { return relevant, nil } diff --git a/stores/sql/sqlite/main.go b/stores/sql/sqlite/main.go index 51fcd5592..0e488c6ed 100644 --- a/stores/sql/sqlite/main.go +++ b/stores/sql/sqlite/main.go @@ -650,9 +650,10 @@ func (tx *MainDatabaseTx) Peers(ctx context.Context) ([]syncer.PeerInfo, error) func (tx *MainDatabaseTx) ProcessChainUpdate(ctx context.Context, fn func(ssql.ChainUpdateTx) error) (err error) { return fn(&chainUpdateTx{ - ctx: ctx, - tx: tx, - l: tx.log.Named("ProcessChainUpdate"), + ctx: ctx, + known: make(map[types.FileContractID]bool), + tx: tx, + l: tx.log.Named("ProcessChainUpdate"), }) }