2020-08-07 14:33:55 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
2021-12-11 21:03:00 +00:00
|
|
|
"context"
|
2020-08-07 17:10:09 +00:00
|
|
|
"encoding/json"
|
2020-08-12 18:24:35 +00:00
|
|
|
"fmt"
|
2020-08-07 16:50:10 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
|
2020-08-07 14:33:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-07 17:10:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2020-08-07 14:33:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ReplaceByFeeRatioDefault = 1.25
|
|
|
|
MemPoolSizeLimitHiDefault = 30000
|
|
|
|
MemPoolSizeLimitLoDefault = 20000
|
2020-08-07 16:50:10 +00:00
|
|
|
PruneCooldownDefault = time.Minute
|
2020-08-09 00:22:37 +00:00
|
|
|
GasLimitOverestimation = 1.25
|
2020-08-07 17:10:09 +00:00
|
|
|
|
|
|
|
ConfigKey = datastore.NewKey("/mpool/config")
|
2020-08-07 14:33:55 +00:00
|
|
|
)
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func loadConfig(ctx context.Context, ds dtypes.MetadataDS) (*types.MpoolConfig, error) {
|
|
|
|
haveCfg, err := ds.Has(ctx, ConfigKey)
|
2020-08-07 17:10:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:20:22 +00:00
|
|
|
if !haveCfg {
|
2020-08-07 17:10:09 +00:00
|
|
|
return DefaultConfig(), nil
|
|
|
|
}
|
2020-08-07 17:20:22 +00:00
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
cfgBytes, err := ds.Get(ctx, ConfigKey)
|
2020-08-07 17:20:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg := new(types.MpoolConfig)
|
|
|
|
err = json.Unmarshal(cfgBytes, cfg)
|
|
|
|
return cfg, err
|
2020-08-07 17:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func saveConfig(ctx context.Context, cfg *types.MpoolConfig, ds dtypes.MetadataDS) error {
|
2020-08-07 17:10:09 +00:00
|
|
|
cfgBytes, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 21:03:00 +00:00
|
|
|
return ds.Put(ctx, ConfigKey, cfgBytes)
|
2020-08-07 17:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 14:33:55 +00:00
|
|
|
func (mp *MessagePool) GetConfig() *types.MpoolConfig {
|
2021-01-14 02:55:49 +00:00
|
|
|
return mp.getConfig().Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mp *MessagePool) getConfig() *types.MpoolConfig {
|
|
|
|
mp.cfgLk.RLock()
|
|
|
|
defer mp.cfgLk.RUnlock()
|
|
|
|
return mp.cfg
|
2020-08-07 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 18:24:35 +00:00
|
|
|
func validateConfg(cfg *types.MpoolConfig) error {
|
|
|
|
if cfg.ReplaceByFeeRatio < ReplaceByFeeRatioDefault {
|
|
|
|
return fmt.Errorf("'ReplaceByFeeRatio' is less than required %f < %f",
|
|
|
|
cfg.ReplaceByFeeRatio, ReplaceByFeeRatioDefault)
|
|
|
|
}
|
|
|
|
if cfg.GasLimitOverestimation < 1 {
|
|
|
|
return fmt.Errorf("'GasLimitOverestimation' cannot be less than 1")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (mp *MessagePool) SetConfig(ctx context.Context, cfg *types.MpoolConfig) error {
|
2020-08-12 18:24:35 +00:00
|
|
|
if err := validateConfg(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-07 14:33:55 +00:00
|
|
|
cfg = cfg.Clone()
|
2020-08-12 18:24:35 +00:00
|
|
|
|
2020-08-07 14:33:55 +00:00
|
|
|
mp.cfgLk.Lock()
|
|
|
|
mp.cfg = cfg
|
2021-12-11 21:03:00 +00:00
|
|
|
err := saveConfig(ctx, cfg, mp.ds)
|
2020-08-07 17:10:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error persisting mpool config: %s", err)
|
|
|
|
}
|
2020-08-07 14:33:55 +00:00
|
|
|
mp.cfgLk.Unlock()
|
2020-08-12 18:24:35 +00:00
|
|
|
|
|
|
|
return nil
|
2020-08-07 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultConfig() *types.MpoolConfig {
|
|
|
|
return &types.MpoolConfig{
|
2020-08-08 15:09:47 +00:00
|
|
|
SizeLimitHigh: MemPoolSizeLimitHiDefault,
|
|
|
|
SizeLimitLow: MemPoolSizeLimitLoDefault,
|
|
|
|
ReplaceByFeeRatio: ReplaceByFeeRatioDefault,
|
|
|
|
PruneCooldown: PruneCooldownDefault,
|
|
|
|
GasLimitOverestimation: GasLimitOverestimation,
|
2020-08-07 14:33:55 +00:00
|
|
|
}
|
|
|
|
}
|