From e7caa50932036a01325f547be410f4543557e699 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 11 Mar 2024 06:42:51 -0400 Subject: [PATCH] fix: don't store MaxRPCSCPrice as a float, and use big.Rat to compute it --- config/sia.go | 18 ++++++++++++------ renter/price_tracker.go | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/config/sia.go b/config/sia.go index dd72afb..739fb86 100644 --- a/config/sia.go +++ b/config/sia.go @@ -1,6 +1,9 @@ package config -import "errors" +import ( + "errors" + "math/big" +) var _ Validator = (*SiaConfig)(nil) var _ Defaults = (*SiaConfig)(nil) @@ -13,7 +16,7 @@ type SiaConfig struct { MaxDownloadPrice float64 `mapstructure:"max_download_price"` MaxStoragePrice float64 `mapstructure:"max_storage_price"` MaxContractSCPrice float64 `mapstructure:"max_contract_sc_price"` - MaxRPCSCPrice float64 `mapstructure:"max_rpc_sc_price"` + MaxRPCSCPrice string `mapstructure:"max_rpc_sc_price"` } func (s SiaConfig) Defaults() map[string]interface{} { @@ -44,12 +47,15 @@ func (s SiaConfig) Validate() error { return errors.New("core.storage.sia.max_storage_price must be greater than 0") } - if s.MaxRPCSCPrice <= 0 { - return errors.New("core.storage.sia.max_rpc_sc_price must be greater than 0") + err := errors.New("failed to parse core.storage.sia.max_rpc_sc_price ") + + rat, ok := new(big.Rat).SetString(s.MaxRPCSCPrice) + if !ok { + return err } - if s.MaxRPCSCPrice <= 0 { - return errors.New("core.storage.sia.max_contract_sc_price must be greater than 0") + if rat.Cmp(new(big.Rat).SetUint64(0)) <= 0 { + return err } return nil diff --git a/renter/price_tracker.go b/renter/price_tracker.go index 15e4846..ef6f9e4 100644 --- a/renter/price_tracker.go +++ b/renter/price_tracker.go @@ -139,12 +139,17 @@ SELECT AVG(rate) as average_rate FROM ( return err } - maxRPCPrice := p.config.Config().Core.Storage.Sia.MaxRPCSCPrice - maxRPCPrice = maxRPCPrice / 1_000_000 + maxRPCPrice, ok := new(big.Rat).SetString(p.config.Config().Core.Storage.Sia.MaxRPCSCPrice) - p.logger.Debug("Setting max RPC price", zap.Float64("maxRPCPrice", maxRPCPrice)) + if !ok { + return errors.New("failed to parse max rpc price") + } - gouge.MaxRPCPrice, err = siacoinsFromFloat(maxRPCPrice) + maxRPCPrice = new(big.Rat).Quo(maxRPCPrice, new(big.Rat).SetUint64(1_000_000)) + + p.logger.Debug("Setting max RPC price", zap.String("maxRPCPrice", maxRPCPrice.FloatString(2))) + + gouge.MaxRPCPrice, err = siacoinsFromRat(maxRPCPrice) if err != nil { return err } @@ -260,8 +265,7 @@ func NewPriceTracker(params PriceTrackerParams) *PriceTracker { } } -func siacoinsFromFloat(f float64) (types.Currency, error) { - r := new(big.Rat).SetFloat64(f) +func siacoinsFromRat(r *big.Rat) (types.Currency, error) { r.Mul(r, new(big.Rat).SetInt(types.HastingsPerSiacoin.Big())) i := new(big.Int).Div(r.Num(), r.Denom()) if i.Sign() < 0 { @@ -271,3 +275,8 @@ func siacoinsFromFloat(f float64) (types.Currency, error) { } return types.NewCurrency(i.Uint64(), new(big.Int).Rsh(i, 64).Uint64()), nil } + +func siacoinsFromFloat(f float64) (types.Currency, error) { + r := new(big.Rat).SetFloat64(f) + return siacoinsFromRat(r) +}