Skip to content

Commit

Permalink
stores: add sanity check
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Apr 22, 2024
1 parent 04870fc commit ff4c32a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
29 changes: 15 additions & 14 deletions stores/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -108,15 +108,15 @@ 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

if err == gorm.ErrRecordNotFound {
err = u.tx.
Select("state").
Model(&dbArchivedContract{}).
Where("fcid = ?", fileContractID(fcid)).
Where("fcid", fileContractID(fcid)).
Scan(&state).
Error
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions stores/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion stores/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 1 addition & 7 deletions stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ff4c32a

Please sign in to comment.