lotus/chain/syncstate.go

85 lines
1.4 KiB
Go
Raw Permalink Normal View History

package chain
import (
"sync"
2019-12-04 03:56:29 +00:00
"time"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-02-08 02:18:32 +00:00
"github.com/filecoin-project/lotus/api"
2020-07-10 14:43:14 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
)
type SyncerStateSnapshot struct {
2019-12-04 03:56:29 +00:00
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
}
type SyncerState struct {
lk sync.Mutex
data SyncerStateSnapshot
}
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.data.Stage = v
2019-12-04 03:56:29 +00:00
if v == api.StageSyncComplete {
ss.data.End = build.Clock.Now()
2019-12-04 03:56:29 +00:00
}
}
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.data.Target = target
ss.data.Base = base
ss.data.Stage = api.StageHeaders
ss.data.Height = 0
ss.data.Message = ""
ss.data.Start = build.Clock.Now()
ss.data.End = time.Time{}
}
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
}
ss.lk.Lock()
defer ss.lk.Unlock()
ss.data.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.data.Message = err.Error()
ss.data.Stage = api.StageSyncErrored
ss.data.End = build.Clock.Now()
2019-12-04 03:56:29 +00:00
}
func (ss *SyncerState) Snapshot() SyncerStateSnapshot {
ss.lk.Lock()
defer ss.lk.Unlock()
return ss.data
}