Skip to content

disallow manual trigger of completed tasks #110

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

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/taskengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ func (n *Engine) TriggerTask(user *model.User, payload *avsproto.UserTriggerTask
return nil, err
}

if !task.Runable() {
return nil, grpcstatus.Errorf(codes.FailedPrecondition, TaskIsNotRunable)
}

if !task.OwnedBy(user.Address) {
// only the owner of a task can trigger it
return nil, grpcstatus.Errorf(codes.NotFound, TaskNotFoundError)
Expand Down
44 changes: 43 additions & 1 deletion core/taskengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ func TestTriggerSync(t *testing.T) {
t.Errorf("invalid triggered block. expect 101 got %d", execution.TriggerMetadata.BlockNumber)
}
}

func TestTriggerAsync(t *testing.T) {
db := testutil.TestMustDB()
defer storage.Destroy(db.(*storage.BadgerStorage))
Expand Down Expand Up @@ -413,3 +412,46 @@ func TestTriggerAsync(t *testing.T) {
t.Errorf("invalid execution status, expected completed but got %s", avsproto.TaskStatus_name[int32(executionStatus.Status)])
}
}

func TestTriggerCompletedTaskReturnError(t *testing.T) {
db := testutil.TestMustDB()
defer storage.Destroy(db.(*storage.BadgerStorage))

config := testutil.GetAggregatorConfig()
n := New(db, config, nil, testutil.GetLogger())

// Now create a test task
tr1 := testutil.RestTask()
tr1.Name = "t1"
tr1.MaxExecution = 1
// salt 0
tr1.SmartWalletAddress = "0x7c3a76086588230c7B3f4839A4c1F5BBafcd57C6"
result, _ := n.CreateTask(testutil.TestUser1(), tr1)

resultTrigger, err := n.TriggerTask(testutil.TestUser1(), &avsproto.UserTriggerTaskReq{
TaskId: result.Id,
TriggerMetadata: &avsproto.TriggerMetadata{
BlockNumber: 101,
},
IsBlocking: true,
})

if err != nil || resultTrigger == nil {
t.Errorf("expected trigger succesfully but got error: %s", err)
}

fmt.Println(resultTrigger)
// Now the task has reach its max run, and canot run anymore
resultTrigger, err = n.TriggerTask(testutil.TestUser1(), &avsproto.UserTriggerTaskReq{
TaskId: result.Id,
TriggerMetadata: &avsproto.TriggerMetadata{
BlockNumber: 101,
},
IsBlocking: true,
})

if err == nil || resultTrigger != nil {
t.Errorf("expect trigger error but succeed")
}

}
1 change: 1 addition & 0 deletions core/taskengine/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (

TaskStorageCorruptedError = "task data storage is corrupted"
TaskIDMissing = "Missing task id in request"
TaskIsNotRunable = "The workflow is not in a runable status, it has reached the limit execution or the expiration time"

InvalidCursor = "cursor is not valid"
InvalidPaginationParam = "item per page is not valid"
Expand Down
12 changes: 12 additions & 0 deletions model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func (t *Task) OwnedBy(address common.Address) bool {
return strings.EqualFold(t.Owner, address.Hex())
}

// A task is runable when both of these condition are matched
// 1. Its max execution has not reached
// 2. Its expiration time has not reached
func (t *Task) Runable() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure about the naming in go, but to be consistent with the name TaskIsNotRunable, I’d call this function isRunnable(). The is will clearly indicate that this is boolean returning function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me change.

name convention in go:

  • anything with lower case initial is private: mean it can only be call from inside the package
  • antyhing upper case is public: mean it can be call from outside(the one that import it)

so will change to IsRunable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisli30 update to IsRunable because

model/tasks.go is in package model

It was being call in package taskengine so it need to be IsRunable

This isn't just a convention in term of syntax. It's the language spec https://go.dev/ref/spec#Exported_identifiers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, understood and sounds good!

// When MaxExecution is 0, it is unlimited run
reachedMaxRun := t.MaxExecution > 0 && t.TotalExecution >= t.MaxExecution

reachedExpiredTime := t.ExpiredAt > 0 && time.Unix(t.ExpiredAt, 0).Before(time.Now())

return !reachedMaxRun && !reachedExpiredTime
}

// Given a task key generated from Key(), extract the ID part
func TaskKeyToId(key []byte) []byte {
// <43-byte>:<43-byte>:
Expand Down
Loading