2019-09-30 21:06:47 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2019-12-04 03:56:29 +00:00
|
|
|
"time"
|
2019-09-30 21:06:47 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-02-08 02:18:32 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-07-10 14:43:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-30 21:06:47 +00:00
|
|
|
)
|
|
|
|
|
2019-09-30 22:29:40 +00:00
|
|
|
func SyncStageString(v api.SyncStateStage) string {
|
2019-09-30 21:06:47 +00:00
|
|
|
switch v {
|
2019-09-30 22:29:40 +00:00
|
|
|
case api.StageHeaders:
|
2019-09-30 21:06:47 +00:00
|
|
|
return "header sync"
|
2019-09-30 22:29:40 +00:00
|
|
|
case api.StagePersistHeaders:
|
2019-09-30 21:06:47 +00:00
|
|
|
return "persisting headers"
|
2019-09-30 22:29:40 +00:00
|
|
|
case api.StageMessages:
|
2019-09-30 21:06:47 +00:00
|
|
|
return "message sync"
|
2019-09-30 22:29:40 +00:00
|
|
|
case api.StageSyncComplete:
|
2019-09-30 21:06:47 +00:00
|
|
|
return "complete"
|
2019-12-04 03:56:29 +00:00
|
|
|
case api.StageSyncErrored:
|
|
|
|
return "error"
|
2019-09-30 21:06:47 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("<unknown: %d>", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SyncerState struct {
|
2019-12-04 03:56:29 +00:00
|
|
|
lk sync.Mutex
|
|
|
|
Target *types.TipSet
|
|
|
|
Base *types.TipSet
|
|
|
|
Stage api.SyncStateStage
|
2020-02-08 02:18:32 +00:00
|
|
|
Height abi.ChainEpoch
|
2019-12-04 03:56:29 +00:00
|
|
|
Message string
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 22:29:40 +00:00
|
|
|
func (ss *SyncerState) SetStage(v api.SyncStateStage) {
|
2019-11-15 21:35:29 +00:00
|
|
|
if ss == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:06:47 +00:00
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
ss.Stage = v
|
2019-12-04 03:56:29 +00:00
|
|
|
if v == api.StageSyncComplete {
|
2020-07-10 14:43:14 +00:00
|
|
|
ss.End = build.Clock.Now()
|
2019-12-04 03:56:29 +00:00
|
|
|
}
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *SyncerState) Init(base, target *types.TipSet) {
|
2019-11-15 21:35:29 +00:00
|
|
|
if ss == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:06:47 +00:00
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
ss.Target = target
|
|
|
|
ss.Base = base
|
2019-09-30 22:29:40 +00:00
|
|
|
ss.Stage = api.StageHeaders
|
2019-09-30 21:06:47 +00:00
|
|
|
ss.Height = 0
|
2019-12-04 03:56:29 +00:00
|
|
|
ss.Message = ""
|
2020-07-10 14:43:14 +00:00
|
|
|
ss.Start = build.Clock.Now()
|
2019-12-04 03:56:29 +00:00
|
|
|
ss.End = time.Time{}
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (ss *SyncerState) SetHeight(h abi.ChainEpoch) {
|
2019-11-15 21:35:29 +00:00
|
|
|
if ss == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:06:47 +00:00
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
ss.Height = h
|
|
|
|
}
|
|
|
|
|
2019-12-04 03:56:29 +00:00
|
|
|
func (ss *SyncerState) Error(err error) {
|
|
|
|
if ss == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
ss.Message = err.Error()
|
|
|
|
ss.Stage = api.StageSyncErrored
|
2020-07-10 14:43:14 +00:00
|
|
|
ss.End = build.Clock.Now()
|
2019-12-04 03:56:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 21:06:47 +00:00
|
|
|
func (ss *SyncerState) Snapshot() SyncerState {
|
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
return SyncerState{
|
2019-12-04 03:56:29 +00:00
|
|
|
Base: ss.Base,
|
|
|
|
Target: ss.Target,
|
|
|
|
Stage: ss.Stage,
|
|
|
|
Height: ss.Height,
|
|
|
|
Message: ss.Message,
|
|
|
|
Start: ss.Start,
|
|
|
|
End: ss.End,
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
}
|