Skip to content

Commit 319fd8f

Browse files
Remove gorm from hostdb related tests (#1420)
Apart from the cleanup it - fixes a bug where a blocked host changing its net address via an announcement won't be unblocked - fixes a bug in `UpdateHost` where the MySQL buffer was busy because we didn't consume all `rows` before executing the next query
2 parents 360ffff + a728a0a commit 319fd8f

File tree

9 files changed

+290
-876
lines changed

9 files changed

+290
-876
lines changed

stores/hostdb.go

Lines changed: 0 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -2,259 +2,19 @@ package stores
22

33
import (
44
"context"
5-
dsql "database/sql"
65
"errors"
76
"fmt"
8-
"net"
9-
"strings"
107
"time"
118

129
"go.sia.tech/core/types"
13-
"go.sia.tech/coreutils/chain"
1410
"go.sia.tech/renterd/api"
1511
sql "go.sia.tech/renterd/stores/sql"
16-
"gorm.io/gorm"
17-
"gorm.io/gorm/clause"
1812
)
1913

2014
var (
2115
ErrNegativeMaxDowntime = errors.New("max downtime can not be negative")
2216
)
2317

24-
type (
25-
// dbHost defines a api.Interaction as persisted in the DB. Deleting a
26-
// host from the db will cascade the deletion and also delete the
27-
// corresponding announcements and interactions with that host.
28-
//
29-
// NOTE: updating the host entity requires an update to the field map passed
30-
// to 'Update' when recording host interactions
31-
dbHost struct {
32-
Model
33-
34-
PublicKey publicKey `gorm:"unique;index;NOT NULL;size:32"`
35-
Settings hostSettings
36-
PriceTable hostPriceTable
37-
PriceTableExpiry dsql.NullTime
38-
39-
TotalScans uint64
40-
LastScan int64 `gorm:"index"` // unix nano
41-
LastScanSuccess bool
42-
SecondToLastScanSuccess bool
43-
Scanned bool `gorm:"index"`
44-
Uptime time.Duration
45-
Downtime time.Duration
46-
47-
// RecentDowntime and RecentScanFailures are used to determine whether a
48-
// host is eligible for pruning.
49-
RecentDowntime time.Duration `gorm:"index"`
50-
RecentScanFailures uint64 `gorm:"index"`
51-
52-
SuccessfulInteractions float64
53-
FailedInteractions float64
54-
55-
LostSectors uint64
56-
57-
LastAnnouncement time.Time
58-
NetAddress string `gorm:"index"`
59-
Subnets string
60-
61-
Allowlist []dbAllowlistEntry `gorm:"many2many:host_allowlist_entry_hosts;constraint:OnDelete:CASCADE"`
62-
Blocklist []dbBlocklistEntry `gorm:"many2many:host_blocklist_entry_hosts;constraint:OnDelete:CASCADE"`
63-
Checks []dbHostCheck `gorm:"foreignKey:DBHostID;constraint:OnDelete:CASCADE"`
64-
}
65-
66-
// dbHostCheck contains information about a host that is collected and used
67-
// by the autopilot.
68-
dbHostCheck struct {
69-
Model
70-
71-
DBAutopilotID uint
72-
73-
DBHostID uint
74-
DBHost dbHost
75-
76-
// usability
77-
UsabilityBlocked bool
78-
UsabilityOffline bool
79-
UsabilityLowScore bool
80-
UsabilityRedundantIP bool
81-
UsabilityGouging bool
82-
UsabilityNotAcceptingContracts bool
83-
UsabilityNotAnnounced bool
84-
UsabilityNotCompletingScan bool
85-
86-
// score
87-
ScoreAge float64
88-
ScoreCollateral float64
89-
ScoreInteractions float64
90-
ScoreStorageRemaining float64
91-
ScoreUptime float64
92-
ScoreVersion float64
93-
ScorePrices float64
94-
95-
// gouging
96-
GougingContractErr string
97-
GougingDownloadErr string
98-
GougingGougingErr string
99-
GougingPruneErr string
100-
GougingUploadErr string
101-
}
102-
103-
// dbAllowlistEntry defines a table that stores the host blocklist.
104-
dbAllowlistEntry struct {
105-
Model
106-
Entry publicKey `gorm:"unique;index;NOT NULL;size:32"`
107-
Hosts []dbHost `gorm:"many2many:host_allowlist_entry_hosts;constraint:OnDelete:CASCADE"`
108-
}
109-
110-
// dbBlocklistEntry defines a table that stores the host blocklist.
111-
dbBlocklistEntry struct {
112-
Model
113-
Entry string `gorm:"unique;index;NOT NULL"`
114-
Hosts []dbHost `gorm:"many2many:host_blocklist_entry_hosts;constraint:OnDelete:CASCADE"`
115-
}
116-
117-
// dbAnnouncement is a table used for storing all announcements. It
118-
// doesn't have any relations to dbHost which means it won't
119-
// automatically prune when a host is deleted.
120-
dbAnnouncement struct {
121-
Model
122-
HostKey publicKey `gorm:"NOT NULL"`
123-
124-
BlockHeight uint64
125-
BlockID string
126-
NetAddress string
127-
}
128-
129-
// announcement describes an announcement for a single host.
130-
announcement struct {
131-
chain.HostAnnouncement
132-
blockHeight uint64
133-
blockID types.BlockID
134-
hk types.PublicKey
135-
timestamp time.Time
136-
}
137-
)
138-
139-
// TableName implements the gorm.Tabler interface.
140-
func (dbAnnouncement) TableName() string { return "host_announcements" }
141-
142-
// TableName implements the gorm.Tabler interface.
143-
func (dbHost) TableName() string { return "hosts" }
144-
145-
// TableName implements the gorm.Tabler interface.
146-
func (dbHostCheck) TableName() string { return "host_checks" }
147-
148-
// TableName implements the gorm.Tabler interface.
149-
func (dbAllowlistEntry) TableName() string { return "host_allowlist_entries" }
150-
151-
// TableName implements the gorm.Tabler interface.
152-
func (dbBlocklistEntry) TableName() string { return "host_blocklist_entries" }
153-
154-
func (h *dbHost) BeforeCreate(tx *gorm.DB) (err error) {
155-
tx.Statement.AddClause(clause.OnConflict{
156-
Columns: []clause.Column{{Name: "public_key"}},
157-
DoUpdates: clause.AssignmentColumns([]string{"last_announcement", "net_address"}),
158-
})
159-
return nil
160-
}
161-
162-
func (e *dbAllowlistEntry) AfterCreate(tx *gorm.DB) error {
163-
// NOTE: the ID is zero here if we ignore a conflict on create
164-
if e.ID == 0 {
165-
return nil
166-
}
167-
168-
params := map[string]interface{}{
169-
"entry_id": e.ID,
170-
"exact_entry": publicKey(e.Entry),
171-
}
172-
173-
// insert entries into the allowlist
174-
if isSQLite(tx) {
175-
return tx.Exec(`INSERT OR IGNORE INTO host_allowlist_entry_hosts (db_allowlist_entry_id, db_host_id)
176-
SELECT @entry_id, id FROM (
177-
SELECT id
178-
FROM hosts
179-
WHERE public_key = @exact_entry
180-
)`, params).Error
181-
}
182-
183-
return tx.Exec(`INSERT IGNORE INTO host_allowlist_entry_hosts (db_allowlist_entry_id, db_host_id)
184-
SELECT @entry_id, id FROM (
185-
SELECT id
186-
FROM hosts
187-
WHERE public_key=@exact_entry
188-
) AS _`, params).Error
189-
}
190-
191-
func (e *dbAllowlistEntry) BeforeCreate(tx *gorm.DB) (err error) {
192-
tx.Statement.AddClause(clause.OnConflict{
193-
Columns: []clause.Column{{Name: "entry"}},
194-
DoNothing: true,
195-
})
196-
return nil
197-
}
198-
199-
func (e *dbBlocklistEntry) AfterCreate(tx *gorm.DB) error {
200-
// NOTE: the ID is zero here if we ignore a conflict on create
201-
if e.ID == 0 {
202-
return nil
203-
}
204-
205-
params := map[string]interface{}{
206-
"entry_id": e.ID,
207-
"exact_entry": e.Entry,
208-
"like_entry": fmt.Sprintf("%%.%s", e.Entry),
209-
}
210-
211-
// insert entries into the blocklist
212-
if isSQLite(tx) {
213-
return tx.Exec(`
214-
INSERT OR IGNORE INTO host_blocklist_entry_hosts (db_blocklist_entry_id, db_host_id)
215-
SELECT @entry_id, id FROM (
216-
SELECT id
217-
FROM hosts
218-
WHERE net_address == @exact_entry OR
219-
rtrim(rtrim(net_address, replace(net_address, ':', '')),':') == @exact_entry OR
220-
rtrim(rtrim(net_address, replace(net_address, ':', '')),':') LIKE @like_entry
221-
)`, params).Error
222-
}
223-
224-
return tx.Exec(`
225-
INSERT IGNORE INTO host_blocklist_entry_hosts (db_blocklist_entry_id, db_host_id)
226-
SELECT @entry_id, id FROM (
227-
SELECT id
228-
FROM hosts
229-
WHERE net_address=@exact_entry OR
230-
SUBSTRING_INDEX(net_address,':',1)=@exact_entry OR
231-
SUBSTRING_INDEX(net_address,':',1) LIKE @like_entry
232-
) AS _`, params).Error
233-
}
234-
235-
func (e *dbBlocklistEntry) BeforeCreate(tx *gorm.DB) (err error) {
236-
tx.Statement.AddClause(clause.OnConflict{
237-
Columns: []clause.Column{{Name: "entry"}},
238-
DoNothing: true,
239-
})
240-
return nil
241-
}
242-
243-
func (e *dbBlocklistEntry) blocks(h dbHost) bool {
244-
values := []string{h.NetAddress}
245-
host, _, err := net.SplitHostPort(h.NetAddress)
246-
if err == nil {
247-
values = append(values, host)
248-
}
249-
250-
for _, value := range values {
251-
if value == e.Entry || strings.HasSuffix(value, "."+e.Entry) {
252-
return true
253-
}
254-
}
255-
return false
256-
}
257-
25818
// Host returns information about a host.
25919
func (s *SQLStore) Host(ctx context.Context, hostKey types.PublicKey) (api.Host, error) {
26020
hosts, err := s.SearchHosts(ctx, "", api.HostFilterModeAll, api.UsabilityFilterModeAll, "", []types.PublicKey{hostKey}, 0, 1)
@@ -362,45 +122,3 @@ func (s *SQLStore) RecordPriceTables(ctx context.Context, priceTableUpdate []api
362122
return tx.RecordPriceTables(ctx, priceTableUpdate)
363123
})
364124
}
365-
366-
func insertAnnouncements(tx *gorm.DB, as []announcement) error {
367-
var hosts []dbHost
368-
var announcements []dbAnnouncement
369-
for _, a := range as {
370-
hosts = append(hosts, dbHost{
371-
PublicKey: publicKey(a.hk),
372-
LastAnnouncement: a.timestamp.UTC(),
373-
NetAddress: a.NetAddress,
374-
})
375-
announcements = append(announcements, dbAnnouncement{
376-
HostKey: publicKey(a.hk),
377-
BlockHeight: a.blockHeight,
378-
BlockID: a.blockID.String(),
379-
NetAddress: a.NetAddress,
380-
})
381-
}
382-
if err := tx.Create(&announcements).Error; err != nil {
383-
return err
384-
}
385-
return tx.Create(&hosts).Error
386-
}
387-
388-
func getBlocklists(tx *gorm.DB) ([]dbAllowlistEntry, []dbBlocklistEntry, error) {
389-
var allowlist []dbAllowlistEntry
390-
if err := tx.
391-
Model(&dbAllowlistEntry{}).
392-
Find(&allowlist).
393-
Error; err != nil {
394-
return nil, nil, err
395-
}
396-
397-
var blocklist []dbBlocklistEntry
398-
if err := tx.
399-
Model(&dbBlocklistEntry{}).
400-
Find(&blocklist).
401-
Error; err != nil {
402-
return nil, nil, err
403-
}
404-
405-
return allowlist, blocklist, nil
406-
}

0 commit comments

Comments
 (0)