Skip to content

Commit be5ec98

Browse files
committed
Update task scheduling structure
1 parent 0737d5d commit be5ec98

File tree

8 files changed

+127
-96
lines changed

8 files changed

+127
-96
lines changed

configs/system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// Name is the name of the program
2323
Name = "miner"
2424
// version
25-
Version = "v0.8.2 2502171422-1739773355316"
25+
Version = "v0.8.2 2502181401-1739858489666"
2626
// Description is the description of the program
2727
Description = "Storage miner implementation in CESS networks"
2828
// NameSpace is the cached namespace

node/calctag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func (n *Node) CalcTag(ch chan<- bool) {
4343
n.Pnc(utils.RecoverError(err))
4444
}
4545
}()
46+
47+
if n.GetState() != chain.MINER_STATE_POSITIVE {
48+
return
49+
}
50+
4651
n.getAllFileDirs()
4752
}
4853

node/chainsync.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ func (n *Node) SyncTeeInfo(ch chan<- bool) {
6868
n.Pnc(utils.RecoverError(err))
6969
}
7070
}()
71+
72+
st := n.GetState()
73+
if st != chain.MINER_STATE_POSITIVE && st != chain.MINER_STATE_FROZEN {
74+
return
75+
}
76+
7177
var dialOptions []grpc.DialOption
7278
var chainPublickey = make([]byte, chain.WorkerPublicKeyLen)
7379

@@ -158,13 +164,19 @@ func (n *Node) SyncTeeInfo(ch chan<- bool) {
158164
}
159165
}
160166

161-
func (n *Node) syncMinerStatus() {
167+
func (n *Node) syncMinerStatus(ch chan<- bool) {
162168
defer func() {
169+
ch <- true
163170
if err := recover(); err != nil {
164171
n.Pnc(utils.RecoverError(err))
165172
}
166173
}()
167174

175+
st := n.GetState()
176+
if st == chain.MINER_STATE_EXIT || st == chain.MINER_STATE_OFFLINE {
177+
return
178+
}
179+
168180
minerInfo, err := n.QueryMinerItems(n.GetSignatureAccPulickey(), -1)
169181
if err != nil {
170182
n.Log("err", err.Error())

node/idlegen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func (n *Node) GenIdle(ch chan<- bool) {
3232
return
3333
}
3434

35+
if n.GetState() != chain.MINER_STATE_POSITIVE {
36+
return
37+
}
38+
3539
decSpace, validSpace, usedSpace, lockSpace := n.GetMinerSpaceInfo()
3640
if (validSpace + usedSpace + lockSpace) >= decSpace {
3741
n.Space("info", "The declared space has been authenticated")

node/init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ func (n *Node) InitChainClient() {
121121
n.InitChainclient(cli)
122122
n.InitWorkspace(filepath.Join(n.ReadWorkspace(), n.GetSignatureAcc(), configs.Name))
123123

124-
err = n.InitExtrinsicsNameForMiner()
124+
err = checkRpcSynchronization(cli)
125125
if err != nil {
126-
out.Err("Please verify the RPC version and ensure it has been synchronized to the latest state.")
126+
out.Err("Failed to sync block: network error")
127127
os.Exit(1)
128128
}
129129

130-
err = checkRpcSynchronization(cli)
130+
err = n.InitExtrinsicsNameForMiner()
131131
if err != nil {
132-
out.Err("Failed to sync block: network error")
132+
out.Err("Please verify the RPC version and ensure it has been synchronized to the latest state.")
133133
os.Exit(1)
134134
}
135135

@@ -400,7 +400,7 @@ func (n *Node) queryPodr2KeyFromTee() ([]byte, error) {
400400
if err == nil {
401401
continue
402402
}
403-
out.Tip(fmt.Sprintf("Requesting podr2 public key from tee: %s", pubkeyHex))
403+
//out.Tip(fmt.Sprintf("Requesting podr2 public key from tee: %s", pubkeyHex))
404404
for tryCount := uint8(0); tryCount <= 3; tryCount++ {
405405
endpoint, err := n.QueryEndpoints(teelist[i].Pubkey, -1)
406406
if err != nil {

node/node.go

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -102,136 +102,138 @@ func (n *Node) Start() {
102102
signal.Notify(exitCh, os.Interrupt, os.Kill, syscall.SIGTERM)
103103
go exitHandle(exitCh)
104104

105+
// check pois
106+
go n.CheckPois(int(n.ReadUseCpu()))
107+
108+
// sync self info
109+
syncMinerStatusCh := make(chan bool, 1)
110+
go n.syncMinerStatus(syncMinerStatusCh)
111+
112+
// sync tee info
113+
syncTeeCh := make(chan bool, 1)
114+
go n.SyncTeeInfo(syncTeeCh)
115+
105116
reportFileCh := make(chan bool, 1)
106117
reportFileCh <- true
118+
107119
genIdleCh := make(chan bool, 1)
108120
genIdleCh <- true
109-
attestationIdleCh := make(chan bool, 1)
110-
attestationIdleCh <- true
121+
122+
certIdleCh := make(chan bool, 1)
123+
certIdleCh <- true
124+
111125
calcTagCh := make(chan bool, 1)
112126
calcTagCh <- true
113127

114-
tick_57s := time.NewTicker(chain.BlockInterval * 57)
115-
defer tick_57s.Stop()
128+
idleChallCh := make(chan bool, 1)
129+
idleChallCh <- true
116130

117-
n.syncMinerStatus()
131+
serviceChallCh := make(chan bool, 1)
132+
serviceChallCh <- true
118133

119-
go n.CheckPois(int(n.ReadUseCpu()))
120-
go n.TaskPeriod_15s()
121-
go n.TaskPeriod_10m()
122-
go n.TaskPeriod_1h()
134+
replaceIdleCh := make(chan bool, 1)
135+
replaceIdleCh <- true
123136

124-
out.Ok("Service started successfully")
137+
restoreCh := make(chan bool, 1)
138+
restoreCh <- true
139+
140+
tNow := time.Now().Unix()
141+
tOld_12s := tNow
142+
tOld_20s := tNow
143+
tOld_30s := tNow
144+
tOld_40s := tNow
145+
tOld_50s := tNow
146+
tOld_1m := tNow
147+
tOld_3m := tNow
148+
tOld_10m := tNow
149+
tOld_1h := tNow
125150

151+
out.Ok("Service started successfully")
126152
for {
127-
select {
128-
case <-tick_57s.C:
129-
if n.GetState() == chain.MINER_STATE_EXIT ||
130-
n.GetState() == chain.MINER_STATE_OFFLINE {
131-
break
153+
tNow = time.Now().Unix()
154+
155+
// 10s challenge
156+
if tNow-tOld_12s >= 12 {
157+
if len(idleChallCh) > 0 || len(serviceChallCh) > 0 {
158+
go n.ChallengeMgt(idleChallCh, serviceChallCh)
159+
tOld_12s = tNow
160+
}
161+
}
162+
163+
// 20s sync self info
164+
if tNow-tOld_20s >= 20 {
165+
if len(syncMinerStatusCh) > 0 {
166+
<-syncMinerStatusCh
167+
go n.syncMinerStatus(syncMinerStatusCh)
168+
tOld_20s = tNow
132169
}
170+
}
133171

172+
// 30s report file
173+
if tNow-tOld_30s >= 30 {
134174
if len(reportFileCh) > 0 {
135175
<-reportFileCh
136176
go n.ReportFiles(reportFileCh)
177+
tOld_30s = tNow
137178
}
179+
}
138180

139-
if len(attestationIdleCh) > 0 {
140-
<-attestationIdleCh
141-
go n.CertIdle(attestationIdleCh)
181+
// 40s gen idle
182+
if tNow-tOld_40s >= 40 {
183+
if len(genIdleCh) > 0 {
184+
if !n.GetIdleChallenging() && !n.GetServiceChallenging() {
185+
<-genIdleCh
186+
go n.GenIdle(genIdleCh)
187+
tOld_40s = tNow
188+
}
142189
}
190+
}
143191

192+
// 50s cert idle
193+
if tNow-tOld_50s >= 50 {
194+
if len(certIdleCh) > 0 {
195+
<-certIdleCh
196+
go n.CertIdle(certIdleCh)
197+
tOld_50s = tNow
198+
}
199+
}
200+
201+
// 1m calc tag
202+
if tNow-tOld_1m >= 60 {
144203
if len(calcTagCh) > 0 {
145204
<-calcTagCh
146205
go n.CalcTag(calcTagCh)
206+
tOld_1m = tNow
147207
}
148-
149-
if len(genIdleCh) > 0 && !n.GetIdleChallenging() && !n.GetServiceChallenging() {
150-
<-genIdleCh
151-
go n.GenIdle(genIdleCh)
152-
}
153-
default:
154-
time.Sleep(time.Second)
155208
}
156-
}
157-
}
158209

159-
func (n *Node) TaskPeriod_15s() {
160-
n.Log("info", "start TaskPeriod_15s")
161-
tick_15s := time.NewTicker(time.Second * 15)
162-
defer tick_15s.Stop()
163-
idleChallCh := make(chan bool, 1)
164-
idleChallCh <- true
165-
serviceChallCh := make(chan bool, 1)
166-
serviceChallCh <- true
167-
for {
168-
select {
169-
case <-tick_15s.C:
170-
if n.GetState() == chain.MINER_STATE_EXIT ||
171-
n.GetState() == chain.MINER_STATE_OFFLINE {
172-
break
173-
}
174-
if len(idleChallCh) > 0 || len(serviceChallCh) > 0 {
175-
go n.ChallengeMgt(idleChallCh, serviceChallCh)
176-
time.Sleep(time.Second)
210+
// 3m replace idle
211+
if tNow-tOld_3m >= 180 {
212+
if len(replaceIdleCh) > 0 {
213+
<-replaceIdleCh
214+
go n.ReplaceIdle(replaceIdleCh)
215+
tOld_3m = tNow
177216
}
178-
default:
179-
time.Sleep(time.Second)
180217
}
181-
}
182-
}
183218

184-
func (n *Node) TaskPeriod_10m() {
185-
n.Log("info", "start TaskPeriod_10m")
186-
tick_10m := time.NewTicker(time.Minute * 10)
187-
defer tick_10m.Stop()
188-
syncTeeCh := make(chan bool, 1)
189-
replaceIdleCh := make(chan bool, 1)
190-
replaceIdleCh <- true
191-
192-
go n.SyncTeeInfo(syncTeeCh)
193-
for {
194-
select {
195-
case <-tick_10m.C:
196-
n.syncMinerStatus()
197-
if n.GetState() == chain.MINER_STATE_EXIT ||
198-
n.GetState() == chain.MINER_STATE_OFFLINE {
199-
break
200-
}
219+
// 10m sync tee info
220+
if tNow-tOld_10m >= 600 {
201221
if len(syncTeeCh) > 0 {
202222
<-syncTeeCh
203223
go n.SyncTeeInfo(syncTeeCh)
224+
tOld_10m = tNow
204225
}
205-
if len(replaceIdleCh) > 0 {
206-
<-replaceIdleCh
207-
go n.ReplaceIdle(replaceIdleCh)
208-
}
209-
default:
210-
time.Sleep(time.Second)
211226
}
212-
}
213-
}
214227

215-
func (n *Node) TaskPeriod_1h() {
216-
n.Log("info", "start TaskPeriod_1h")
217-
tick_1h := time.NewTicker(time.Hour)
218-
defer tick_1h.Stop()
219-
restoreCh := make(chan bool, 1)
220-
restoreCh <- true
221-
for {
222-
select {
223-
case <-tick_1h.C:
224-
if n.GetState() == chain.MINER_STATE_EXIT ||
225-
n.GetState() == chain.MINER_STATE_OFFLINE {
226-
break
227-
}
228+
// 1h restore file
229+
if tNow-tOld_1h >= 3600 {
228230
if len(restoreCh) > 0 {
229231
<-restoreCh
230232
go n.RestoreFiles(restoreCh)
233+
tOld_1h = tNow
231234
}
232-
default:
233-
time.Sleep(time.Second)
234235
}
236+
time.Sleep(time.Millisecond * 100)
235237
}
236238
}
237239

node/report.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (n *Node) ReportFiles(ch chan<- bool) {
2727
}
2828
}()
2929

30+
if n.GetState() != chain.MINER_STATE_POSITIVE {
31+
return
32+
}
33+
3034
roothashs, err := utils.Dirs(n.GetReportDir())
3135
if err != nil {
3236
n.Report("err", fmt.Sprintf("[Dirs(TmpDir)] %v", err))

node/restore.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (n *Node) RestoreFiles(ch chan bool) {
4141
}
4242
}()
4343

44+
if n.GetState() != chain.MINER_STATE_POSITIVE {
45+
return
46+
}
47+
4448
err := n.RestoreLocalFiles()
4549
if err != nil {
4650
n.Restore("err", err.Error())

0 commit comments

Comments
 (0)