fix: don't store MaxRPCSCPrice as a float, and use big.Rat to compute it

This commit is contained in:
Derrick Hammer 2024-03-11 06:42:51 -04:00
parent 7f9887bdcc
commit e7caa50932
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 27 additions and 12 deletions

View File

@ -1,6 +1,9 @@
package config package config
import "errors" import (
"errors"
"math/big"
)
var _ Validator = (*SiaConfig)(nil) var _ Validator = (*SiaConfig)(nil)
var _ Defaults = (*SiaConfig)(nil) var _ Defaults = (*SiaConfig)(nil)
@ -13,7 +16,7 @@ type SiaConfig struct {
MaxDownloadPrice float64 `mapstructure:"max_download_price"` MaxDownloadPrice float64 `mapstructure:"max_download_price"`
MaxStoragePrice float64 `mapstructure:"max_storage_price"` MaxStoragePrice float64 `mapstructure:"max_storage_price"`
MaxContractSCPrice float64 `mapstructure:"max_contract_sc_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{} { 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") return errors.New("core.storage.sia.max_storage_price must be greater than 0")
} }
if s.MaxRPCSCPrice <= 0 { err := errors.New("failed to parse core.storage.sia.max_rpc_sc_price ")
return errors.New("core.storage.sia.max_rpc_sc_price must be greater than 0")
rat, ok := new(big.Rat).SetString(s.MaxRPCSCPrice)
if !ok {
return err
} }
if s.MaxRPCSCPrice <= 0 { if rat.Cmp(new(big.Rat).SetUint64(0)) <= 0 {
return errors.New("core.storage.sia.max_contract_sc_price must be greater than 0") return err
} }
return nil return nil

View File

@ -139,12 +139,17 @@ SELECT AVG(rate) as average_rate FROM (
return err return err
} }
maxRPCPrice := p.config.Config().Core.Storage.Sia.MaxRPCSCPrice maxRPCPrice, ok := new(big.Rat).SetString(p.config.Config().Core.Storage.Sia.MaxRPCSCPrice)
maxRPCPrice = maxRPCPrice / 1_000_000
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 { if err != nil {
return err return err
} }
@ -260,8 +265,7 @@ func NewPriceTracker(params PriceTrackerParams) *PriceTracker {
} }
} }
func siacoinsFromFloat(f float64) (types.Currency, error) { func siacoinsFromRat(r *big.Rat) (types.Currency, error) {
r := new(big.Rat).SetFloat64(f)
r.Mul(r, new(big.Rat).SetInt(types.HastingsPerSiacoin.Big())) r.Mul(r, new(big.Rat).SetInt(types.HastingsPerSiacoin.Big()))
i := new(big.Int).Div(r.Num(), r.Denom()) i := new(big.Int).Div(r.Num(), r.Denom())
if i.Sign() < 0 { 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 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)
}