-
Notifications
You must be signed in to change notification settings - Fork 626
[FIX] Checking assigned task before task type is decided #1748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,19 +37,94 @@ type ProverTask interface { | |
| Assign(ctx *gin.Context, getTaskParameter *coordinatorType.GetTaskParameter) (*coordinatorType.GetTaskSchema, error) | ||
| } | ||
|
|
||
| // ProverTaskManager manage task which has been assigned | ||
| type ProverTaskManager struct { | ||
| proverTaskOrm *orm.ProverTask | ||
| proverBlockListOrm *orm.ProverBlockList | ||
| } | ||
|
|
||
| const proverTaskCtxKey = "prover_task_context_key" | ||
|
|
||
| // NewProverTaskManager new a prover task manager | ||
| func NewProverTaskManager(db *gorm.DB) *ProverTaskManager { | ||
| return &ProverTaskManager{ | ||
| proverTaskOrm: orm.NewProverTask(db), | ||
| proverBlockListOrm: orm.NewProverBlockList(db), | ||
| } | ||
| } | ||
|
|
||
| // checkParameter check the prover task parameter illegal | ||
| func (b *ProverTaskManager) CheckParameter(ctx *gin.Context) (*orm.ProverTask, error) { | ||
| var ptc proverTaskContext | ||
| ptc.HardForkNames = make(map[string]struct{}) | ||
|
|
||
| publicKey, publicKeyExist := ctx.Get(coordinatorType.PublicKey) | ||
| if !publicKeyExist { | ||
| return nil, errors.New("get public key from context failed") | ||
| } | ||
| ptc.PublicKey = publicKey.(string) | ||
|
|
||
| proverName, proverNameExist := ctx.Get(coordinatorType.ProverName) | ||
| if !proverNameExist { | ||
| return nil, errors.New("get prover name from context failed") | ||
| } | ||
| ptc.ProverName = proverName.(string) | ||
|
|
||
| proverVersion, proverVersionExist := ctx.Get(coordinatorType.ProverVersion) | ||
| if !proverVersionExist { | ||
| return nil, errors.New("get prover version from context failed") | ||
| } | ||
| ptc.ProverVersion = proverVersion.(string) | ||
|
|
||
| ProverProviderType, ProverProviderTypeExist := ctx.Get(coordinatorType.ProverProviderTypeKey) | ||
| if !ProverProviderTypeExist { | ||
| // for backward compatibility, set ProverProviderType as internal | ||
| ProverProviderType = float64(coordinatorType.ProverProviderTypeInternal) | ||
| } | ||
| ptc.ProverProviderType = uint8(ProverProviderType.(float64)) | ||
|
|
||
| hardForkNamesStr, hardForkNameExist := ctx.Get(coordinatorType.HardForkName) | ||
| if !hardForkNameExist { | ||
| return nil, errors.New("get hard fork name from context failed") | ||
| } | ||
| hardForkNames := strings.Split(hardForkNamesStr.(string), ",") | ||
| for _, hardForkName := range hardForkNames { | ||
| ptc.HardForkNames[hardForkName] = struct{}{} | ||
| } | ||
|
|
||
| isBlocked, err := b.proverBlockListOrm.IsPublicKeyBlocked(ctx.Copy(), publicKey.(string)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to check whether the public key %s is blocked before assigning a chunk task, err: %w, proverName: %s, proverVersion: %s", publicKey, err, proverName, proverVersion) | ||
| } | ||
| if isBlocked { | ||
| return nil, fmt.Errorf("public key %s is blocked from fetching tasks. ProverName: %s, ProverVersion: %s", publicKey, proverName, proverVersion) | ||
| } | ||
|
|
||
| assigned, err := b.proverTaskOrm.IsProverAssigned(ctx.Copy(), publicKey.(string)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to check if prover %s is assigned a task, err: %w", publicKey.(string), err) | ||
| } | ||
|
|
||
| ptc.hasAssignedTask = assigned | ||
|
|
||
| ctx.Set(proverTaskCtxKey, &ptc) | ||
|
|
||
| return assigned, nil | ||
| } | ||
|
Comment on lines
+57
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address type assertion safety concerns. The method performs multiple type assertions without checking the underlying type (lines 65, 71, 77, 84, 90), which could panic if the context values have unexpected types. While these values are typically set by middleware, consider either:
Additionally, line 84 has a compound operation that's particularly risky: ptc.ProverProviderType = uint8(ProverProviderType.(float64))If Apply this pattern for safer type assertions: - ptc.PublicKey = publicKey.(string)
+ publicKeyStr, ok := publicKey.(string)
+ if !ok {
+ return nil, fmt.Errorf("public key has invalid type: expected string")
+ }
+ ptc.PublicKey = publicKeyStrConsider applying similar checks for other type assertions, especially line 84 which converts float64 to uint8. 🤖 Prompt for AI Agents |
||
|
|
||
| // BaseProverTask a base prover task which contain series functions | ||
| type BaseProverTask struct { | ||
| cfg *config.Config | ||
| chainCfg *params.ChainConfig | ||
| db *gorm.DB | ||
| expectedVk map[string][]byte | ||
|
|
||
| batchOrm *orm.Batch | ||
| chunkOrm *orm.Chunk | ||
| bundleOrm *orm.Bundle | ||
| blockOrm *orm.L2Block | ||
| proverTaskOrm *orm.ProverTask | ||
| proverBlockListOrm *orm.ProverBlockList | ||
| batchOrm *orm.Batch | ||
| chunkOrm *orm.Chunk | ||
| bundleOrm *orm.Bundle | ||
| blockOrm *orm.L2Block | ||
|
|
||
| proverTaskOrm *orm.ProverTask | ||
| } | ||
|
|
||
| type proverTaskContext struct { | ||
|
|
@@ -132,59 +207,13 @@ func (b *BaseProverTask) hardForkSanityCheck(ctx *gin.Context, taskCtx *proverTa | |
| } | ||
|
|
||
| // checkParameter check the prover task parameter illegal | ||
| func (b *BaseProverTask) checkParameter(ctx *gin.Context) (*proverTaskContext, error) { | ||
| var ptc proverTaskContext | ||
| ptc.HardForkNames = make(map[string]struct{}) | ||
|
|
||
| publicKey, publicKeyExist := ctx.Get(coordinatorType.PublicKey) | ||
| if !publicKeyExist { | ||
| return nil, errors.New("get public key from context failed") | ||
| } | ||
| ptc.PublicKey = publicKey.(string) | ||
|
|
||
| proverName, proverNameExist := ctx.Get(coordinatorType.ProverName) | ||
| if !proverNameExist { | ||
| return nil, errors.New("get prover name from context failed") | ||
| } | ||
| ptc.ProverName = proverName.(string) | ||
|
|
||
| proverVersion, proverVersionExist := ctx.Get(coordinatorType.ProverVersion) | ||
| if !proverVersionExist { | ||
| return nil, errors.New("get prover version from context failed") | ||
| } | ||
| ptc.ProverVersion = proverVersion.(string) | ||
|
|
||
| ProverProviderType, ProverProviderTypeExist := ctx.Get(coordinatorType.ProverProviderTypeKey) | ||
| if !ProverProviderTypeExist { | ||
| // for backward compatibility, set ProverProviderType as internal | ||
| ProverProviderType = float64(coordinatorType.ProverProviderTypeInternal) | ||
| } | ||
| ptc.ProverProviderType = uint8(ProverProviderType.(float64)) | ||
|
|
||
| hardForkNamesStr, hardForkNameExist := ctx.Get(coordinatorType.HardForkName) | ||
| if !hardForkNameExist { | ||
| return nil, errors.New("get hard fork name from context failed") | ||
| } | ||
| hardForkNames := strings.Split(hardForkNamesStr.(string), ",") | ||
| for _, hardForkName := range hardForkNames { | ||
| ptc.HardForkNames[hardForkName] = struct{}{} | ||
| func (b *BaseProverTask) checkParameter(ctx *gin.Context) *proverTaskContext { | ||
| pctx, exist := ctx.Get(proverTaskCtxKey) | ||
| if !exist { | ||
| return nil | ||
| } | ||
|
|
||
| isBlocked, err := b.proverBlockListOrm.IsPublicKeyBlocked(ctx.Copy(), publicKey.(string)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to check whether the public key %s is blocked before assigning a chunk task, err: %w, proverName: %s, proverVersion: %s", publicKey, err, proverName, proverVersion) | ||
| } | ||
| if isBlocked { | ||
| return nil, fmt.Errorf("public key %s is blocked from fetching tasks. ProverName: %s, ProverVersion: %s", publicKey, proverName, proverVersion) | ||
| } | ||
|
|
||
| assigned, err := b.proverTaskOrm.IsProverAssigned(ctx.Copy(), publicKey.(string)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to check if prover %s is assigned a task, err: %w", publicKey.(string), err) | ||
| } | ||
|
|
||
| ptc.hasAssignedTask = assigned | ||
| return &ptc, nil | ||
| return pctx.(*proverTaskContext) | ||
| } | ||
|
|
||
| func (b *BaseProverTask) applyUniversal(schema *coordinatorType.GetTaskSchema) (*coordinatorType.GetTaskSchema, []byte, error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add logging and improve error message for parameter validation failures.
The current implementation returns a generic error without logging or context when parameter validation fails. This significantly reduces observability and makes debugging difficult in production.
Consider adding a log statement and a more descriptive error message:
func (bp *BatchProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinatorType.GetTaskParameter) (*coordinatorType.GetTaskSchema, error) { taskCtx := bp.checkParameter(ctx) if taskCtx == nil { - return nil, fmt.Errorf("check prover task parameter missed") + log.Warn("parameter validation failed for batch task assignment", "height", getTaskParameter.ProverHeight) + return nil, fmt.Errorf("failed to validate prover task parameters") }Note: If
checkParameterstores error details in the context, consider retrieving and logging them here for better diagnostics.📝 Committable suggestion
🤖 Prompt for AI Agents