diff --git a/cmd/init.go b/cmd/init.go index 8900d20..5aeaa79 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -114,7 +114,12 @@ var initCmd = &cli.Command{ return err } - if err := storageMinerInit(cctx, r, &fullnode); err != nil { + networkParams, err := fullNodeAPI.StateGetNetworkParams(ctx) + if err != nil { + return err + } + + if err := storageMinerInit(cctx, r, &fullnode, networkParams.BlockDelaySecs); err != nil { log.Errorf("Failed to initialize sophon-miner: %+v", err) path, err := homedir.Expand(repoPath) if err != nil { @@ -133,7 +138,7 @@ var initCmd = &cli.Command{ }, } -func storageMinerInit(cctx *cli.Context, r repo.Repo, fn *config.APIInfo) error { +func storageMinerInit(cctx *cli.Context, r repo.Repo, fn *config.APIInfo, blockDelay uint64) error { lr, err := r.Lock() if err != nil { return err @@ -180,6 +185,17 @@ func storageMinerInit(cctx *cli.Context, r repo.Repo, fn *config.APIInfo) error cfg.API.ListenAddress = ma.String() cfg.SlashFilter.Type = sfType cfg.SlashFilter.MySQL.Conn = cctx.String("mysql-conn") + + // set the default value of PropagationDelaySecs based on experience. + switch blockDelay { + case 30: + cfg.PropagationDelaySecs = 12 + case 4: + cfg.PropagationDelaySecs = 1 + default: + cfg.PropagationDelaySecs = blockDelay / 2 + } + }); err != nil { return fmt.Errorf("modify config failed: %w", err) } diff --git a/miner/multiminer.go b/miner/multiminer.go index ce6a0f8..d8e4317 100644 --- a/miner/multiminer.go +++ b/miner/multiminer.go @@ -595,16 +595,29 @@ func (m *Miner) getLatestBase(ctx context.Context) (*MiningBase, time.Duration, var onDone func(bool, abi.ChainEpoch, error) var injectNulls abi.ChainEpoch + // overlapCount is used to avoid getting stuck in an endless loop + overlapCount := 0 + for { prebase, err := m.GetBestMiningCandidate(ctx) if err != nil { return nil, time.Second * 5, fmt.Errorf("get best mining candidate: %w", err) } - if base != nil && base.TipSet.Height() == prebase.TipSet.Height() && base.NullRounds == prebase.NullRounds { - base = prebase - break + if base != nil { + if base.TipSet.Height() == prebase.TipSet.Height() && base.NullRounds == prebase.NullRounds { + base = prebase + break + } else { + if overlapCount >= 5 { + log.Warnf("wait to long (about %d epochs) to get mining base, please check your config of PropagationDelaySecs and network", base.TipSet.Height()+base.NullRounds-prebase.TipSet.Height()+prebase.NullRounds) + overlapCount = 0 + } else { + overlapCount++ + } + } } + if base != nil { onDone(false, 0, nil) }