diff --git a/stores/chain.go b/stores/chain.go index 57c8c0868..2920c8b37 100644 --- a/stores/chain.go +++ b/stores/chain.go @@ -38,12 +38,12 @@ func (s *SQLStore) BeginChainUpdateTx() (chain.ChainUpdateTx, error) { func (u *chainUpdateTx) ApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []wallet.Event) error { // remove spent outputs for _, e := range spent { - // TODO: check if rows affected is > 0? - if err := u.tx. + if res := u.tx. Where("output_id", hash256(e.ID)). - Delete(&dbWalletOutput{}). - Error; err != nil { - return err + Delete(&dbWalletOutput{}); res.Error != nil { + return res.Error + } else if res.RowsAffected != 1 { + return fmt.Errorf("spent output with id %v not found ", e.ID) } } @@ -108,7 +108,7 @@ func (u *chainUpdateTx) ContractState(fcid types.FileContractID) (api.ContractSt err := u.tx. Select("state"). Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Scan(&state). Error @@ -116,7 +116,7 @@ func (u *chainUpdateTx) ContractState(fcid types.FileContractID) (api.ContractSt err = u.tx. Select("state"). Model(&dbArchivedContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Scan(&state). Error } @@ -212,7 +212,7 @@ func (u *chainUpdateTx) UpdateContract(fcid types.FileContractID, revisionHeight var c dbContract if err := u.tx. Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Take(&c).Error; err == nil { c.RevisionHeight = revisionHeight if isUpdatedRevision(c.RevisionNumber) { @@ -225,7 +225,7 @@ func (u *chainUpdateTx) UpdateContract(fcid types.FileContractID, revisionHeight var ac dbArchivedContract if err := u.tx. Model(&dbArchivedContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Take(&ac).Error; err == nil { ac.RevisionHeight = revisionHeight if isUpdatedRevision(ac.RevisionNumber) { @@ -251,14 +251,14 @@ func (u *chainUpdateTx) UpdateContractState(fcid types.FileContractID, state api if err := u.tx. Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Update("state", cs). Error; err != nil { return err } return u.tx. Model(&dbArchivedContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Update("state", cs). Error } @@ -268,14 +268,14 @@ func (u *chainUpdateTx) UpdateContractState(fcid types.FileContractID, state api func (u *chainUpdateTx) UpdateContractProofHeight(fcid types.FileContractID, proofHeight uint64) error { if err := u.tx. Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Update("proof_height", proofHeight). Error; err != nil { return err } return u.tx. Model(&dbArchivedContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Update("proof_height", proofHeight). Error } @@ -285,7 +285,8 @@ func (u *chainUpdateTx) UpdateContractProofHeight(fcid types.FileContractID, pro func (u *chainUpdateTx) UpdateFailedContracts(blockHeight uint64) error { return u.tx. Model(&dbContract{}). - Where("state = ? AND ? > window_end", contractStateActive, blockHeight). + Where("window_end <= ?", blockHeight). + Where("state", contractStateActive). Update("state", contractStateFailed). Error } diff --git a/stores/hostdb.go b/stores/hostdb.go index caa93ae70..ae3b47e47 100644 --- a/stores/hostdb.go +++ b/stores/hostdb.go @@ -1184,10 +1184,10 @@ func updateProofHeight(db *gorm.DB, fcid types.FileContractID, blockHeight uint6 func updateActiveAndArchivedContract(tx *gorm.DB, fcid types.FileContractID, updates map[string]interface{}) error { err1 := tx.Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Updates(updates).Error err2 := tx.Model(&dbArchivedContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Updates(updates).Error if err1 != nil || err2 != nil { return fmt.Errorf("%s; %s", err1, err2) diff --git a/stores/metadata.go b/stores/metadata.go index a0f31cfe8..f168f48e5 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -1389,7 +1389,7 @@ func (s *SQLStore) RecordContractSpending(ctx context.Context, records []api.Con err := s.retryTransaction(ctx, func(tx *gorm.DB) error { var contract dbContract err := tx.Model(&dbContract{}). - Where("fcid = ?", fileContractID(fcid)). + Where("fcid", fileContractID(fcid)). Joins("Host"). Take(&contract).Error if errors.Is(err, gorm.ErrRecordNotFound) { diff --git a/stores/metadata_test.go b/stores/metadata_test.go index 82dc70a30..b06dd1c78 100644 --- a/stores/metadata_test.go +++ b/stores/metadata_test.go @@ -4152,13 +4152,7 @@ func TestSlabCleanup(t *testing.T) { HealthValidUntil: 100, } if err := ss.db.Create(&bufferedSlab).Error; err != nil { - if strings.Contains(err.Error(), "database table is locked") { - time.Sleep(time.Second) // wait for slabs to be pruned in the background - err = ss.db.Create(&bufferedSlab).Error - } - if err != nil { - t.Fatal(err) - } + t.Fatal(err) } obj3 := dbObject{ ObjectID: "3",