-
Notifications
You must be signed in to change notification settings - Fork 462
/
status.go
169 lines (144 loc) · 4.05 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package chain
import (
"fmt"
"sync"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/venus/pkg/block"
logging "github.com/ipfs/go-log/v2"
)
var logChainStatus = logging.Logger("chain/status")
// Reporter defines an interface to updating and reporting the status of the blockchain.
type Reporter interface {
UpdateStatus(...StatusUpdates)
Status() Status
}
// StatusReporter implements the Reporter interface.
type StatusReporter struct {
statusMu sync.Mutex
status *Status
}
// UpdateStatus updates the status heald by StatusReporter.
func (sr *StatusReporter) UpdateStatus(update ...StatusUpdates) {
sr.statusMu.Lock()
defer sr.statusMu.Unlock()
for _, u := range update {
u(sr.status)
}
logChainStatus.Debugf("syncing status: %s", sr.status.String())
}
// Status returns a copy of the current status.
func (sr *StatusReporter) Status() Status {
return *sr.status
}
// NewStatusReporter initializes a new StatusReporter.
func NewStatusReporter() *StatusReporter {
return &StatusReporter{
status: newDefaultChainStatus(),
}
}
// Status defines a structure used to represent the state of a chain store and syncer.
type Status struct {
// The heaviest TipSet that has been fully validated.
ValidatedHead block.TipSetKey
// The height of ValidatedHead.
ValidatedHeadHeight abi.ChainEpoch
// They head of the chain currently being fetched/validated, or undef if none.
SyncingHead block.TipSetKey
// The height of SyncingHead.
SyncingHeight uint64
// Whether SyncingTip is trusted as a head far away from the validated head.
SyncingTrusted bool
// Unix time at which syncing of chain at SyncingHead began, zero if valdation hasn't started.
SyncingStarted int64
// Whether SyncingHead has been validated.
SyncingComplete bool
// Whether SyncingHead has been fetched.
SyncingFetchComplete bool
// The key of the tipset currently being fetched
FetchingHead block.TipSetKey
// The height of FetchingHead
FetchingHeight uint64
}
// NewDefaultChainStatus returns a ChainStaus with the default empty values.
func newDefaultChainStatus() *Status {
return &Status{
ValidatedHead: block.TipSetKey{},
ValidatedHeadHeight: 0,
SyncingHead: block.TipSetKey{},
SyncingHeight: 0,
SyncingTrusted: false,
SyncingStarted: 0,
SyncingComplete: true,
SyncingFetchComplete: true,
FetchingHead: block.TipSetKey{},
FetchingHeight: 0,
}
}
// String returns the Status as a string
func (s Status) String() string {
return fmt.Sprintf("validatedHead=%s, validatedHeight=%d, syncingStarted=%d, syncingHead=%s, syncingHeight=%d, syncingTrusted=%t, syncingComplete=%t syncingFetchComplete=%t fetchingHead=%s, fetchingHeight=%d",
s.ValidatedHead, s.ValidatedHeadHeight, s.SyncingStarted,
s.SyncingHead, s.SyncingHeight, s.SyncingTrusted, s.SyncingComplete, s.SyncingFetchComplete,
s.FetchingHead, s.FetchingHeight)
}
// StatusUpdates defines a type for ipdating syncer status.
type StatusUpdates func(*Status)
//
// Validation Updates
//
func validateHead(u block.TipSetKey) StatusUpdates {
return func(s *Status) {
s.ValidatedHead = u
}
}
func validateHeight(u abi.ChainEpoch) StatusUpdates {
return func(s *Status) {
s.ValidatedHeadHeight = u
}
}
//
// Syncing Updates
//
func syncHead(u block.TipSetKey) StatusUpdates {
return func(s *Status) {
s.SyncingHead = u
}
}
func syncHeight(u uint64) StatusUpdates {
return func(s *Status) {
s.SyncingHeight = u
}
}
func syncTrusted(u bool) StatusUpdates {
return func(s *Status) {
s.SyncingTrusted = u
}
}
func syncingStarted(u int64) StatusUpdates {
return func(s *Status) {
s.SyncingStarted = u
}
}
func syncComplete(u bool) StatusUpdates {
return func(s *Status) {
s.SyncingComplete = u
}
}
func syncFetchComplete(u bool) StatusUpdates {
return func(s *Status) {
s.SyncingFetchComplete = u
}
}
//
// Fetching Updates
//
func fetchHead(u block.TipSetKey) StatusUpdates {
return func(s *Status) {
s.FetchingHead = u
}
}
func fetchHeight(u uint64) StatusUpdates {
return func(s *Status) {
s.FetchingHeight = u
}
}