Skip to content

Commit

Permalink
Merge branch 'dev' into chris/refactor-contract-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl authored Dec 12, 2024
2 parents 0d9235a + 9b05f24 commit 1f4bf3c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/gouging/gouging.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func checkPriceGougingHS(gs api.GougingSettings, hs *rhpv2.HostSettings) error {

// check EA expiry
if hs.EphemeralAccountExpiry < time.Duration(gs.MinAccountExpiry) {
return fmt.Errorf("'EphemeralAccountExpiry' is less than the allowed minimum value, %v < %v", hs.EphemeralAccountExpiry, gs.MinAccountExpiry)
return fmt.Errorf("'EphemeralAccountExpiry' is less than the allowed minimum value, %v < %v", hs.EphemeralAccountExpiry, time.Duration(gs.MinAccountExpiry))
}

return nil
Expand Down Expand Up @@ -280,7 +280,7 @@ func checkPriceGougingPT(gs api.GougingSettings, cs api.ConsensusState, pt *rhpv

// check Validity
if pt.Validity < time.Duration(gs.MinPriceTableValidity) {
return fmt.Errorf("'Validity' is less than the allowed minimum value, %v < %v", pt.Validity, gs.MinPriceTableValidity)
return fmt.Errorf("'Validity' is less than the allowed minimum value, %v < %v", pt.Validity, time.Duration(gs.MinPriceTableValidity))
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/sql/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ var (
return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00033_remove_contract_sets", log)
},
},
{
ID: "00034_v2",
Migrate: func(tx Tx) error {
return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00034_v2", log)
},
},
}
}
MetricsMigrations = func(ctx context.Context, migrationsFs embed.FS, log *zap.SugaredLogger) []Migration {
Expand Down
4 changes: 1 addition & 3 deletions stores/sql/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,10 +912,8 @@ func (tx MainDatabaseTx) SaveAccounts(ctx context.Context, accounts []api.Accoun
res, err := stmt.Exec(ctx, time.Now(), (ssql.PublicKey)(acc.ID), acc.CleanShutdown, (ssql.PublicKey)(acc.HostKey), (*ssql.BigInt)(acc.Balance), (*ssql.BigInt)(acc.Drift), acc.RequiresSync, acc.Owner)
if err != nil {
return fmt.Errorf("failed to insert account %v: %w", acc.ID, err)
} else if n, err := res.RowsAffected(); err != nil {
} else if _, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
} else if n != 1 && n != 2 { // 1 for insert, 2 for update
return fmt.Errorf("expected 1 row affected, got %v", n)
}
}
return nil
Expand Down
67 changes: 67 additions & 0 deletions stores/sql/mysql/migrations/main/migration_00034_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- add v2 settings to host
ALTER TABLE hosts
ADD COLUMN v2_settings JSON;

UPDATE hosts
SET
hosts.v2_settings = '{}';

-- drop resolved addresses
ALTER TABLE hosts
DROP COLUMN resolved_addresses;

-- add column to host_checks
ALTER TABLE host_checks
ADD COLUMN `usability_low_max_duration` boolean NOT NULL DEFAULT false;

CREATE INDEX `idx_host_checks_usability_low_max_duration` ON `host_checks` (`usability_low_max_duration`);

-- drop host announcements
DROP TABLE host_announcements;

-- add new table host_addresses
CREATE TABLE `host_addresses` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime (3) DEFAULT NULL,
`db_host_id` bigint unsigned NOT NULL,
`net_address` longtext NOT NULL,
`protocol` tinyint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `ìdx_host_addresses_db_host_id` (`db_host_id`),
CONSTRAINT `fk_host_addresses_db_host` FOREIGN KEY (`db_host_id`) REFERENCES `hosts` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;

-- update gouging setting durations from ns to ms
UPDATE settings
SET
value = (
-- Update settings to new values
SELECT
JSON_REPLACE (
value,
'$.minAccountExpiry',
newMinAccountExpiry,
'$.minPriceTableValidity',
newMinPriceTableValidity
)
FROM
(
-- Convert ns to ms by trimming the last 3 digits
SELECT
SUBSTR (minAccountExpiry, 1, LENGTH (minAccountExpiry) -3) AS newMinAccountExpiry,
SUBSTR (
minPriceTableValidity,
1,
LENGTH (minPriceTableValidity) -3
) AS newMinPriceTableValidity
FROM
(
-- SELECT previous settings
SELECT
JSON_UNQUOTE (JSON_EXTRACT (value, '$.minAccountExpiry')) AS minAccountExpiry,
JSON_UNQUOTE (JSON_EXTRACT (value, '$.minPriceTableValidity')) AS minPriceTableValidity
) AS _
) AS _
)
WHERE
settings.key = "gouging";
4 changes: 1 addition & 3 deletions stores/sql/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,8 @@ func (tx *MainDatabaseTx) SaveAccounts(ctx context.Context, accounts []api.Accou
res, err := stmt.Exec(ctx, time.Now(), (ssql.PublicKey)(acc.ID), acc.CleanShutdown, (ssql.PublicKey)(acc.HostKey), (*ssql.BigInt)(acc.Balance), (*ssql.BigInt)(acc.Drift), acc.RequiresSync, acc.Owner)
if err != nil {
return fmt.Errorf("failed to insert account %v: %w", acc.ID, err)
} else if n, err := res.RowsAffected(); err != nil {
} else if _, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
} else if n != 1 {
return fmt.Errorf("expected 1 row affected, got %v", n)
}
}
return nil
Expand Down
67 changes: 67 additions & 0 deletions stores/sql/sqlite/migrations/main/migration_00034_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- add v2 settings to host
ALTER TABLE hosts
ADD COLUMN v2_settings text;

UPDATE hosts
SET
hosts.v2_settings = '{}';

-- drop resolved addresses
ALTER TABLE hosts
DROP COLUMN resolved_addresses;

-- add column to host_checks
ALTER TABLE host_checks
ADD COLUMN usability_low_max_duration INTEGER NOT NULL DEFAULT 0;

CREATE INDEX `idx_host_checks_usability_low_max_duration` ON `host_checks` (`usability_low_max_duration`);

-- drop host announcements
DROP TABLE host_announcements;

-- add new table host_addresses
CREATE TABLE `host_addresses` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`created_at` datetime NOT NULL,
`db_host_id` integer NOT NULL,
`net_address` text NOT NULL,
`protocol` integer NOT NULL,
CONSTRAINT `fk_host_addresses_db_host` FOREIGN KEY (`db_host_id`) REFERENCES `hosts` (`id`) ON DELETE CASCADE
);

CREATE INDEX `idx_host_addresses_db_host_id` ON `host_addresses` (`db_host_id`);

-- update gouging setting durations from ns to ms
UPDATE settings
SET
value = (
-- Update settings to new values
SELECT
JSON_REPLACE (
value,
'$.minAccountExpiry',
newMinAccountExpiry,
'$.minPriceTableValidity',
newMinPriceTableValidity
)
FROM
(
-- Convert ns to ms by trimming the last 3 digits
SELECT
SUBSTR (minAccountExpiry, 1, LENGTH (minAccountExpiry) -3) AS newMinAccountExpiry,
SUBSTR (
minPriceTableValidity,
1,
LENGTH (minPriceTableValidity) -3
) AS newMinPriceTableValidity
FROM
(
-- SELECT previous settings
SELECT
JSON_EXTRACT (value, '$.minAccountExpiry') AS minAccountExpiry,
JSON_EXTRACT (value, '$.minPriceTableValidity') AS minPriceTableValidity
) AS _
) AS _
)
WHERE
settings.key = "gouging";

0 comments on commit 1f4bf3c

Please sign in to comment.