Skip to content

Commit

Permalink
stores: fix applyRevisionUpdate to avoid NDF
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Apr 22, 2024
1 parent 80a7e20 commit 04870fc
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions stores/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,11 +1119,46 @@ func insertAnnouncements(tx *gorm.DB, as []announcement) error {
}

func applyRevisionUpdate(db *gorm.DB, fcid types.FileContractID, rev revisionUpdate) error {
return updateActiveAndArchivedContract(db, fcid, map[string]interface{}{
"revision_height": rev.height,
"revision_number": fmt.Sprint(rev.number),
"size": rev.size,
})
// isUpdatedRevision indicates whether the given revision number is greater
// than the one currently set on the contract
isUpdatedRevision := func(currRevStr string) bool {
var currRev uint64
_, _ = fmt.Sscan(currRevStr, &currRev)
return rev.number > currRev
}

// update either active or archived contract
var update interface{}
var c dbContract
if err := db.
Model(&dbContract{}).
Where("fcid", fileContractID(fcid)).
Take(&c).Error; err == nil {
c.RevisionHeight = rev.height
if isUpdatedRevision(c.RevisionNumber) {
c.RevisionNumber = fmt.Sprint(rev.number)
c.Size = rev.size
}
update = c
} else if err == gorm.ErrRecordNotFound {
// try archived contracts
var ac dbArchivedContract
if err := db.
Model(&dbArchivedContract{}).
Where("fcid", fileContractID(fcid)).
Take(&ac).Error; err == nil {
ac.RevisionHeight = rev.height
if isUpdatedRevision(ac.RevisionNumber) {
ac.RevisionNumber = fmt.Sprint(rev.number)
ac.Size = rev.size
}
update = ac
}
}
if update == nil {
return nil
}
return db.Save(update).Error
}

func updateContractState(db *gorm.DB, fcid types.FileContractID, cs contractState) error {
Expand Down

0 comments on commit 04870fc

Please sign in to comment.