@@ -6,11 +6,9 @@ import (
6
6
"time"
7
7
8
8
logging "github.com/ipfs/go-log/v2"
9
- "github.com/jpillora/backoff"
10
9
"golang.org/x/xerrors"
11
10
12
11
"github.com/filecoin-project/go-address"
13
- "github.com/filecoin-project/go-f3/gpbft"
14
12
"github.com/filecoin-project/go-f3/manifest"
15
13
16
14
"github.com/filecoin-project/curio/deps"
@@ -44,7 +42,6 @@ var log = logging.Logger("cf3")
44
42
type F3ParticipationAPI interface {
45
43
F3GetOrRenewParticipationTicket (ctx context.Context , minerID address.Address , previous api.F3ParticipationTicket , instances uint64 ) (api.F3ParticipationTicket , error ) //perm:sign
46
44
F3Participate (ctx context.Context , ticket api.F3ParticipationTicket ) (api.F3ParticipationLease , error )
47
- F3GetProgress (ctx context.Context ) (gpbft.Instant , error )
48
45
F3GetManifest (ctx context.Context ) (* manifest.Manifest , error )
49
46
}
50
47
@@ -94,31 +91,26 @@ func (f *F3Task) Do(taskID harmonytask.TaskID, stillOwned func() bool) (done boo
94
91
return false , xerrors .Errorf ("failed to get participation ticket: %w" , err )
95
92
}
96
93
97
- lease , participating , err := f .tryF3Participate (ctx , stillOwned , ticket )
98
- if err != nil {
99
- return false , xerrors .Errorf ("failed to participate in F3: %w" , err )
100
- }
101
- if ! participating {
102
- return false , xerrors .Errorf ("failed to participate in F3: not participating" )
103
- }
104
-
94
+ // Store the ticket in the database
105
95
_ , err = f .db .Exec (ctx , "UPDATE f3_tasks SET previous_ticket = $1 WHERE task_id = $2" , ticket , taskID )
106
96
if err != nil {
107
97
return false , xerrors .Errorf ("failed to update previous ticket: %w" , err )
108
98
}
109
99
110
- err = f .awaitLeaseExpiry (ctx , stillOwned , lease )
100
+ // Start participation loop
101
+ err = f .participateLoop (ctx , stillOwned , ticket )
111
102
if err != nil {
112
- return false , xerrors .Errorf ("failed to await lease expiry : %w" , err )
103
+ return false , xerrors .Errorf ("failed during participation loop : %w" , err )
113
104
}
105
+ // When participateLoop returns, we go back to get a new ticket
114
106
}
115
107
116
108
return false , xerrors .Errorf ("f3 task is background task" )
117
109
}
118
110
119
111
func (f * F3Task ) tryGetF3ParticipationTicket (ctx context.Context , stillOwned func () bool , participant address.Address , previousTicket []byte ) (api.F3ParticipationTicket , error ) {
120
112
for stillOwned () {
121
- switch ticket , err := f .api .F3GetOrRenewParticipationTicket (ctx , participant , previousTicket , ParticipationLeaseTerm ); {
113
+ switch ticket , err := f .api .F3GetOrRenewParticipationTicket (ctx , participant , previousTicket , f . leaseTerm ); {
122
114
case ctx .Err () != nil :
123
115
return api.F3ParticipationTicket {}, ctx .Err ()
124
116
case errors .Is (err , api .ErrF3Disabled ):
@@ -136,99 +128,56 @@ func (f *F3Task) tryGetF3ParticipationTicket(ctx context.Context, stillOwned fun
136
128
return api.F3ParticipationTicket {}, ctx .Err ()
137
129
}
138
130
139
- func (f * F3Task ) tryF3Participate (ctx context.Context , stillOwned func () bool , ticket api.F3ParticipationTicket ) (api.F3ParticipationLease , bool , error ) {
131
+ func (f * F3Task ) participateLoop (ctx context.Context , stillOwned func () bool , ticket api.F3ParticipationTicket ) error {
132
+ renewLeaseWithin := f .leaseTerm / 2
133
+ var (
134
+ haveLease bool
135
+ )
140
136
for stillOwned () {
141
- switch lease , err := f .api .F3Participate (ctx , ticket ); {
137
+ lease , err := f .api .F3Participate (ctx , ticket )
138
+ switch {
142
139
case ctx .Err () != nil :
143
- return api. F3ParticipationLease {}, false , ctx .Err ()
140
+ return ctx .Err ()
144
141
case errors .Is (err , api .ErrF3Disabled ):
145
142
log .Errorw ("Cannot participate in F3 as it is disabled." , "err" , err )
146
- return api. F3ParticipationLease {}, false , xerrors .Errorf ("attempting F3 participation with ticket: %w" , err )
143
+ return xerrors .Errorf ("attempting F3 participation with ticket: %w" , err )
147
144
case errors .Is (err , api .ErrF3ParticipationTicketExpired ):
148
145
log .Warnw ("F3 participation ticket expired while attempting to participate. Acquiring a new ticket." , "err" , err )
149
- return api. F3ParticipationLease {}, false , nil
146
+ return nil // Return to get a new ticket
150
147
case errors .Is (err , api .ErrF3ParticipationTicketStartBeforeExisting ):
151
148
log .Warnw ("F3 participation ticket starts before the existing lease. Acquiring a new ticket." , "err" , err )
152
- return api. F3ParticipationLease {}, false , nil
149
+ return nil // Return to get a new ticket
153
150
case errors .Is (err , api .ErrF3ParticipationTicketInvalid ):
154
151
log .Errorw ("F3 participation ticket is not valid. Acquiring a new ticket after backoff." , "err" , err )
155
152
time .Sleep (1 * time .Second )
156
- return api. F3ParticipationLease {}, false , nil
153
+ return nil // Return to get a new ticket
157
154
case errors .Is (err , api .ErrF3ParticipationIssuerMismatch ):
158
- log .Warnw ("Node is not the issuer of F3 participation ticket. Miner maybe load-balancing or node has changed. Retrying F3 participation after backoff ." , "err" , err )
155
+ log .Warnw ("Node is not the issuer of F3 participation ticket. Miner may be load-balancing or node has changed. Retrying F3 participation." , "err" , err )
159
156
time .Sleep (1 * time .Second )
160
157
continue
161
158
case errors .Is (err , api .ErrF3NotReady ):
162
- log .Warnw ("F3 is not ready. Retrying F3 participation after backoff ." , "err" , err )
159
+ log .Warnw ("F3 is not ready. Retrying F3 participation." , "err" , err )
163
160
time .Sleep (30 * time .Second )
164
161
continue
165
162
case err != nil :
166
163
log .Errorw ("Unexpected error while attempting F3 participation. Retrying after backoff" , "err" , err )
167
- return api.F3ParticipationLease {}, false , xerrors .Errorf ("attempting F3 participation with ticket: %w" , err )
164
+ return xerrors .Errorf ("attempting F3 participation with ticket: %w" , err )
165
+ case lease .ValidityTerm <= renewLeaseWithin :
166
+ return nil // Return to get a new ticket
168
167
default :
169
- log .Infow ("Successfully acquired F3 participation lease." ,
170
- "issuer" , lease .Issuer ,
171
- "not-before" , lease .FromInstance ,
172
- "not-after" , lease .ToInstance (),
173
- )
174
- return lease , true , nil
175
- }
176
- }
177
- return api.F3ParticipationLease {}, false , ctx .Err ()
178
- }
179
-
180
- func (f * F3Task ) awaitLeaseExpiry (ctx context.Context , stillOwned func () bool , lease api.F3ParticipationLease ) error {
181
- backoff := & backoff.Backoff {
182
- Min : 1 * time .Second ,
183
- Max : 1 * time .Minute ,
184
- Factor : 1.5 ,
185
- }
186
-
187
- renewLeaseWithin := f .leaseTerm / 2
188
- for stillOwned () {
189
- manifest , err := f .api .F3GetManifest (ctx )
190
- switch {
191
- case errors .Is (err , api .ErrF3Disabled ):
192
- log .Errorw ("Cannot await F3 participation lease expiry as F3 is disabled." , "err" , err )
193
- return xerrors .Errorf ("awaiting F3 participation lease expiry: %w" , err )
194
- case err != nil :
195
- if backoff .Attempt () > ParticipationCheckProgressMaxAttempts {
196
- log .Errorw ("Too many failures while attempting to check F3 progress. Restarting participation." , "attempts" , backoff .Attempt (), "err" , err )
197
- return nil
198
- }
199
- log .Errorw ("Failed to check F3 progress while awaiting lease expiry. Retrying after backoff." , "attempts" , backoff .Attempt (), "backoff" , backoff .Duration (), "err" , err )
200
- time .Sleep (backoff .Duration ())
201
- continue
202
- case manifest == nil || manifest .NetworkName != lease .Network :
203
- // If we got an unexpected manifest, or no manifest, go back to the
204
- // beginning and try to get another ticket. Switching from having a manifest
205
- // to having no manifest can theoretically happen if the lotus node reboots
206
- // and has no static manifest.
207
- return nil
208
- }
209
- switch progress , err := f .api .F3GetProgress (ctx ); {
210
- case errors .Is (err , api .ErrF3Disabled ):
211
- log .Errorw ("Cannot await F3 participation lease expiry as F3 is disabled." , "err" , err )
212
- return xerrors .Errorf ("awaiting F3 participation lease expiry: %w" , err )
213
- case err != nil :
214
- if backoff .Attempt () > ParticipationCheckProgressMaxAttempts {
215
- log .Errorw ("Too many failures while attempting to check F3 progress. Restarting participation." , "attempts" , backoff .Attempt (), "err" , err )
216
- return nil
168
+ // Successfully participated
169
+ if ! haveLease {
170
+ log .Infow ("Successfully acquired F3 participation lease." ,
171
+ "issuer" , lease .Issuer ,
172
+ "not-before" , lease .FromInstance ,
173
+ "not-after" , lease .ToInstance (),
174
+ )
175
+ haveLease = true
217
176
}
218
- log .Errorw ("Failed to check F3 progress while awaiting lease expiry. Retrying after backoff." , "attempts" , backoff .Attempt (), "backoff" , backoff .Duration (), "err" , err )
219
- time .Sleep (backoff .Duration ())
220
- case progress .ID + renewLeaseWithin >= lease .ToInstance ():
221
- log .Infof ("F3 progressed (%d) to within %d instances of lease expiry (%d). Renewing participation." , progress .ID , renewLeaseWithin , lease .ToInstance ())
222
- return nil
223
- default :
224
- remainingInstanceLease := lease .ToInstance () - progress .ID
225
- waitTime := time .Duration (remainingInstanceLease - renewLeaseWithin ) * manifest .CatchUpAlignment
226
- if waitTime == 0 {
227
- waitTime = 100 * time .Millisecond
228
- }
229
- log .Debugf ("F3 participation lease is valid for further %d instances. Re-checking after %s." , remainingInstanceLease , waitTime )
230
- time .Sleep (waitTime )
231
177
}
178
+
179
+ log .Debugf ("F3 participation lease is valid for further %d instances." , lease .ValidityTerm )
180
+ time .Sleep (time .Second * 5 )
232
181
}
233
182
return ctx .Err ()
234
183
}
0 commit comments