Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gouging settings API to accept base unit #1505

Merged
merged 20 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4b3ab23
Merge dev -> master (#1355)
ChrisSchinnerl Jun 28, 2024
f357d2e
Use unit SC/TB/month for maxStoragePrice when pinning it (#1358)
ChrisSchinnerl Jul 3, 2024
037e9dd
Merge branch 'master' into chris/v110
ChrisSchinnerl Aug 23, 2024
d615bdd
e2e: cancel syncer context
ChrisSchinnerl Aug 23, 2024
028a6fb
Fix node setup (#1477)
ChrisSchinnerl Aug 26, 2024
1ed897e
Merge dev -> master (#1474)
ChrisSchinnerl Aug 26, 2024
3b7130a
sql: only raise transaction log level to warn after 1s
ChrisSchinnerl Aug 26, 2024
85e9d3f
Cherry-Pick Txn log level change into 'master' (#1481)
ChrisSchinnerl Aug 28, 2024
8a8e783
gouging: change gouging settings to base unit
ChrisSchinnerl Sep 3, 2024
0e49a7c
e2e: fix TestGouging
ChrisSchinnerl Sep 3, 2024
dc3580b
stores: add migrations
ChrisSchinnerl Sep 3, 2024
df0301d
Merge branch 'dev' into chris/gouging-base-units
ChrisSchinnerl Sep 3, 2024
842a66a
Merge branch 'dev' into chris/gouging-base-units
ChrisSchinnerl Sep 5, 2024
e80d17f
Merge branch 'dev' into chris/gouging-base-units
ChrisSchinnerl Sep 6, 2024
51d35ee
gouging: address review comments
ChrisSchinnerl Sep 9, 2024
c8980ae
Merge branch 'api-breakers' into chris/gouging-base-units
ChrisSchinnerl Sep 9, 2024
c439b8b
bus: handle updatePinnedSettings error
ChrisSchinnerl Sep 9, 2024
a2b7da3
Merge branch 'chris/gouging-base-units' of github.com:SiaFoundation/r…
ChrisSchinnerl Sep 9, 2024
d1fe477
bus: increase sleep in updatPinnedSettings
ChrisSchinnerl Sep 9, 2024
ac8a663
stores: fix SQLite migratiton
ChrisSchinnerl Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var (
DefaultGougingSettings = GougingSettings{
MaxRPCPrice: types.Siacoins(1).Div64(1000), // 1mS per RPC
MaxContractPrice: types.Siacoins(15), // 15 SC per contract
MaxDownloadPrice: types.Siacoins(3000), // 3000 SC per 1 TB
MaxUploadPrice: types.Siacoins(3000), // 3000 SC per 1 TB
MaxDownloadPrice: types.Siacoins(3000).Div64(1e12), // 3000 SC per 1 TB
MaxUploadPrice: types.Siacoins(3000).Div64(1e12), // 3000 SC per 1 TB
MaxStoragePrice: types.Siacoins(3000).Div64(1e12).Div64(144 * 30), // 3000 SC per TB per month
HostBlockHeightLeeway: 6, // 6 blocks
MinPriceTableValidity: 5 * time.Minute, // 5 minutes
Expand Down
7 changes: 2 additions & 5 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func New(ctx context.Context, masterKey [32]byte, am AlertManager, wm WebhooksMa
masterKey: masterKey,

accounts: store,
explorer: ibus.NewExplorer(explorerURL),
s: s,
cm: cm,
w: w,
Expand All @@ -366,15 +367,11 @@ func New(ctx context.Context, masterKey [32]byte, am AlertManager, wm WebhooksMa
// create contract locker
b.contractLocker = ibus.NewContractLocker()

// create explorer
e := ibus.NewExplorer(explorerURL)
b.explorer = e

// create sectors cache
b.sectors = ibus.NewSectorsCache()

// create pin manager
b.pinMgr = ibus.NewPinManager(b.alerts, wm, e, store, defaultPinUpdateInterval, defaultPinRateWindow, l)
b.pinMgr = ibus.NewPinManager(b.alerts, wm, b.explorer, store, defaultPinUpdateInterval, defaultPinRateWindow, l)

// create chain subscriber
b.cs = ibus.NewChainSubscriber(wm, cm, store, w, announcementMaxAge, l)
Expand Down
8 changes: 4 additions & 4 deletions internal/bus/pinmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ func (pm *pinManager) updateGougingSettings(ctx context.Context, pins api.Gougin

// update max download price
if pins.MaxDownload.IsPinned() {
update, err := convertCurrencyToSC(decimal.NewFromFloat(pins.MaxDownload.Value), rate)
maxDownloadCurr, err := convertCurrencyToSC(decimal.NewFromFloat(pins.MaxDownload.Value), rate)
if err != nil {
pm.logger.Warn("failed to convert max download price to currency")
} else if !gs.MaxDownloadPrice.Equals(update) {
} else if update := maxDownloadCurr.Div64(1e12); !gs.MaxDownloadPrice.Equals(update) {
gs.MaxDownloadPrice = update
pm.logger.Infow("updating max download price", "old", gs.MaxDownloadPrice, "new", update, "rate", rate)
updated = true
Expand All @@ -288,10 +288,10 @@ func (pm *pinManager) updateGougingSettings(ctx context.Context, pins api.Gougin

// update max upload price
if pins.MaxUpload.IsPinned() {
update, err := convertCurrencyToSC(decimal.NewFromFloat(pins.MaxUpload.Value), rate)
maxUploadCurr, err := convertCurrencyToSC(decimal.NewFromFloat(pins.MaxUpload.Value), rate)
if err != nil {
pm.logger.Warnw("failed to convert max upload price to currency", zap.Error(err))
} else if !gs.MaxUploadPrice.Equals(update) {
} else if update := maxUploadCurr.Div64(1e12); !gs.MaxUploadPrice.Equals(update) {
pm.logger.Infow("updating max upload price", "old", gs.MaxUploadPrice, "new", update, "rate", rate)
gs.MaxUploadPrice = update
updated = true
Expand Down
7 changes: 5 additions & 2 deletions internal/bus/pinmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ func (ms *mockPinStore) gougingSettings() api.GougingSettings {

func (ms *mockPinStore) updatPinnedSettings(pps api.PricePinSettings) {
b, _ := json.Marshal(pps)
ms.UpdateSetting(context.Background(), api.SettingPricePinning, string(b))
time.Sleep(2 * testUpdateInterval)
err := ms.UpdateSetting(context.Background(), api.SettingPricePinning, string(b))
if err != nil {
panic(err)
}
time.Sleep(10 * testUpdateInterval)
}

func (ms *mockPinStore) Setting(ctx context.Context, key string) (string, error) {
Expand Down
33 changes: 13 additions & 20 deletions internal/gouging/gouging.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
)

const (
bytesPerTB = 1e12

// maxBaseRPCPriceVsBandwidth is the max ratio for sane pricing between the
// MinBaseRPCPrice and the MinDownloadBandwidthPrice. This ensures that 1
// million base RPC charges are at most 1% of the cost to download 4TB. This
Expand Down Expand Up @@ -190,7 +188,11 @@ func checkPriceGougingPT(gs api.GougingSettings, cs api.ConsensusState, txnFee t
}

// check LatestRevisionCost - expect sane value
maxRevisionCost, overflow := gs.MaxRPCPrice.AddWithOverflow(gs.MaxDownloadPrice.Div64(bytesPerTB).Mul64(2048))
twoKiBMax, overflow := gs.MaxDownloadPrice.Mul64WithOverflow(2048)
if overflow {
twoKiBMax = types.MaxCurrency
}
maxRevisionCost, overflow := gs.MaxRPCPrice.AddWithOverflow(twoKiBMax)
peterjan marked this conversation as resolved.
Show resolved Hide resolved
if overflow {
maxRevisionCost = types.MaxCurrency
}
Expand Down Expand Up @@ -292,12 +294,9 @@ func checkPruneGougingRHPv2(gs api.GougingSettings, hs *rhpv2.HostSettings) erro
if overflow {
return fmt.Errorf("%w: overflow detected when computing sector download price", ErrHostSettingsGouging)
}
dpptb, overflow := sectorDownloadPrice.Mul64WithOverflow(uint64(bytesPerTB) / rhpv2.SectorSize) // sectors per TB
if overflow {
return fmt.Errorf("%w: overflow detected when computing download price per TiB", ErrHostSettingsGouging)
}
if !gs.MaxDownloadPrice.IsZero() && dpptb.Cmp(gs.MaxDownloadPrice) > 0 {
return fmt.Errorf("%w: cost per TiB exceeds max dl price: %v > %v", ErrHostSettingsGouging, dpptb, gs.MaxDownloadPrice)
dppb := sectorDownloadPrice.Div64(rhpv2.SectorSize)
if !gs.MaxDownloadPrice.IsZero() && dppb.Cmp(gs.MaxDownloadPrice) > 0 {
return fmt.Errorf("%w: cost per byte exceeds max dl price: %v > %v", ErrHostSettingsGouging, dppb, gs.MaxDownloadPrice)
}
return nil
}
Expand All @@ -310,12 +309,9 @@ func checkDownloadGougingRHPv3(gs api.GougingSettings, pt *rhpv3.HostPriceTable)
if overflow {
return fmt.Errorf("%w: overflow detected when computing sector download price", ErrPriceTableGouging)
}
dpptb, overflow := sectorDownloadPrice.Mul64WithOverflow(uint64(bytesPerTB) / rhpv2.SectorSize) // sectors per TiB
if overflow {
return fmt.Errorf("%w: overflow detected when computing download price per TiB", ErrPriceTableGouging)
}
if !gs.MaxDownloadPrice.IsZero() && dpptb.Cmp(gs.MaxDownloadPrice) > 0 {
return fmt.Errorf("%w: cost per TiB exceeds max dl price: %v > %v", ErrPriceTableGouging, dpptb, gs.MaxDownloadPrice)
dppb := sectorDownloadPrice.Div64(rhpv2.SectorSize)
if !gs.MaxDownloadPrice.IsZero() && dppb.Cmp(gs.MaxDownloadPrice) > 0 {
return fmt.Errorf("%w: cost per byte exceeds max dl price: %v > %v", ErrPriceTableGouging, dppb, gs.MaxDownloadPrice)
}
return nil
}
Expand All @@ -328,12 +324,9 @@ func checkUploadGougingRHPv3(gs api.GougingSettings, pt *rhpv3.HostPriceTable) e
if overflow {
return fmt.Errorf("%w: overflow detected when computing sector price", ErrPriceTableGouging)
}
uploadPrice, overflow := sectorUploadPricePerMonth.Mul64WithOverflow(uint64(bytesPerTB) / rhpv2.SectorSize) // sectors per TiB
if overflow {
return fmt.Errorf("%w: overflow detected when computing upload price per TiB", ErrPriceTableGouging)
}
uploadPrice := sectorUploadPricePerMonth.Div64(rhpv2.SectorSize)
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
if !gs.MaxUploadPrice.IsZero() && uploadPrice.Cmp(gs.MaxUploadPrice) > 0 {
return fmt.Errorf("%w: cost per TiB exceeds max ul price: %v > %v", ErrPriceTableGouging, uploadPrice, gs.MaxUploadPrice)
return fmt.Errorf("%w: cost per byte exceeds max ul price: %v > %v", ErrPriceTableGouging, uploadPrice, gs.MaxUploadPrice)
}
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 @@ -217,6 +217,12 @@ var (
return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00017_unix_ms", log)
},
},
{
ID: "00018_gouging_units",
Migrate: func(tx Tx) error {
return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00018_gouging_units", log)
},
},
}
}
MetricsMigrations = func(ctx context.Context, migrationsFs embed.FS, log *zap.SugaredLogger) []Migration {
Expand Down
18 changes: 18 additions & 0 deletions stores/sql/mysql/migrations/main/migration_00018_gouging_units.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
UPDATE settings
SET value = (
-- Update settings to new values
SELECT JSON_REPLACE(value, '$.maxDownloadPrice', newMaxDownloadPrice, '$.maxUploadPrice', newMaxUploadPrice)
FROM (
-- Convert TB to bytes by trimming the last 12 digits
SELECT
SUBSTR(maxDownloadPrice, 1, LENGTH(maxDownloadPrice)-12) AS newMaxDownloadPrice,
SUBSTR(maxUploadPrice, 1, LENGTH(maxUploadPrice)-12) AS newMaxUploadPrice
FROM (
-- SELECT previous settings
SELECT
JSON_UNQUOTE(JSON_EXTRACT(value, '$.maxDownloadPrice')) AS maxDownloadPrice,
JSON_UNQUOTE(JSON_EXTRACT(value, '$.maxUploadPrice')) AS maxUploadPrice
) AS _
) AS _
)
WHERE settings.key = "gouging";
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
UPDATE settings
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
SET value = (
-- Update settings to new values
SELECT JSON_REPLACE(value, '$.maxDownloadPrice', newMaxDownloadPrice, '$.maxUploadPrice', newMaxUploadPrice)
FROM (
-- Convert TB to bytes by trimming the last 12 digits
SELECT
SUBSTR(maxDownloadPrice, 1, LENGTH(maxDownloadPrice)-12) AS newMaxDownloadPrice,
SUBSTR(maxUploadPrice, 1, LENGTH(maxUploadPrice)-12) AS newMaxUploadPrice
FROM (
-- SELECT previous settings
SELECT
JSON_EXTRACT(value, '$.maxDownloadPrice') AS maxDownloadPrice,
JSON_EXTRACT(value, '$.maxUploadPrice') AS maxUploadPrice
) AS _
) AS _
)
WHERE settings.key = "gouging";
Loading