lotus/chain/syncstate.go

77 lines
1.2 KiB
Go
Raw Normal View History

package chain
import (
"fmt"
"sync"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
)
2019-09-30 22:29:40 +00:00
func SyncStageString(v api.SyncStateStage) string {
switch v {
2019-09-30 22:29:40 +00:00
case api.StageHeaders:
return "header sync"
2019-09-30 22:29:40 +00:00
case api.StagePersistHeaders:
return "persisting headers"
2019-09-30 22:29:40 +00:00
case api.StageMessages:
return "message sync"
2019-09-30 22:29:40 +00:00
case api.StageSyncComplete:
return "complete"
default:
return fmt.Sprintf("<unknown: %d>", v)
}
}
type SyncerState struct {
lk sync.Mutex
Target *types.TipSet
Base *types.TipSet
2019-09-30 22:29:40 +00:00
Stage api.SyncStateStage
Height uint64
}
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
}
ss.lk.Lock()
defer ss.lk.Unlock()
ss.Stage = v
}
func (ss *SyncerState) Init(base, target *types.TipSet) {
2019-11-15 21:35:29 +00:00
if ss == nil {
return
}
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
ss.Height = 0
}
func (ss *SyncerState) SetHeight(h uint64) {
2019-11-15 21:35:29 +00:00
if ss == nil {
return
}
ss.lk.Lock()
defer ss.lk.Unlock()
ss.Height = h
}
func (ss *SyncerState) Snapshot() SyncerState {
ss.lk.Lock()
defer ss.lk.Unlock()
return SyncerState{
Base: ss.Base,
Target: ss.Target,
Stage: ss.Stage,
Height: ss.Height,
}
}