Skip to content

Commit

Permalink
migrate trigger from expr -> goja
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n committed Dec 27, 2024
1 parent ad81242 commit 76c2bcd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 48 deletions.
35 changes: 11 additions & 24 deletions core/taskengine/trigger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/AvaProtocol/ap-avs/core/taskengine/macros"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
"github.com/dop251/goja"
"github.com/ginkgoch/godash/v2"

"github.com/ethereum/go-ethereum"
Expand All @@ -26,7 +25,7 @@ type EventMark struct {
}

type Check struct {
Program *vm.Program
Program string
TaskMetadata *avsproto.SyncMessagesResp_TaskMetadata
}

Expand Down Expand Up @@ -71,24 +70,7 @@ func NewEventTrigger(o *RpcOption, triggerCh chan TriggerMetadata[EventMark]) *E

// TODO: track remainExecution and expriedAt before merge
func (t *EventTrigger) AddCheck(check *avsproto.SyncMessagesResp_TaskMetadata) error {
// Dummy value to get type
envs := macros.GetEnvs(map[string]interface{}{
"trigger1": map[string]interface{}{
"data": map[string]interface{}{
"address": "dummy",
"topics": godash.Map([]common.Hash{}, func(topic common.Hash) string {
return "0x"
}),
"data": "0x",
"tx_hash": "dummy",
},
},
})
program, err := expr.Compile(check.GetTrigger().GetEvent().GetExpression(), expr.Env(envs), expr.AsBool())
if err != nil {
return err
}

program := check.GetTrigger().GetEvent().GetExpression()
t.checks.Store(check.TaskId, &Check{
Program: program,
TaskMetadata: check,
Expand Down Expand Up @@ -167,7 +149,8 @@ func (evt *EventTrigger) Run(ctx context.Context) error {
return err
}

func (evt *EventTrigger) Evaluate(event *types.Log, program *vm.Program) (bool, error) {
func (evt *EventTrigger) Evaluate(event *types.Log, program string) (bool, error) {
jsvm := goja.New()
envs := macros.GetEnvs(map[string]interface{}{
"trigger1": map[string]interface{}{
"data": map[string]interface{}{
Expand All @@ -180,12 +163,16 @@ func (evt *EventTrigger) Evaluate(event *types.Log, program *vm.Program) (bool,
},
},
})
for k, v := range envs {
jsvm.Set(k, v)
}

result, err := expr.Run(program, envs)
//result, err := expr.Run(program, envs)
result, err := jsvm.RunString(program)

if err != nil {
return false, err
}

return result.(bool), err
return result.Export().(bool), err
}
33 changes: 9 additions & 24 deletions core/taskengine/trigger/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package trigger
import (
"testing"

"github.com/AvaProtocol/ap-avs/core/taskengine/macros"
"github.com/AvaProtocol/ap-avs/core/testutil"
"github.com/expr-lang/expr"
)

func TestChainlinkLatestAnswer(t *testing.T) {
Expand All @@ -20,35 +18,22 @@ func TestChainlinkLatestAnswer(t *testing.T) {
WsRpcURL: testutil.GetTestRPCURL(),
}, make(chan TriggerMetadata[EventMark], 1000))

envs := macros.GetEnvs(map[string]interface{}{
"trigger1": map[string]interface{}{
"data": map[string]interface{}{
"topics": []string{
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0xabcdef",
"0xc114fb059434563dc65ac8d57e7976e3eac534f4",
},
},
},
})

program, err := expr.Compile(`
trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && trigger1.data.topics[2] == "0xc114fb059434563dc65ac8d57e7976e3eac534f4"
`, expr.Env(envs), expr.AsBool())

if err != nil {
panic(err)
}
program := `trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && trigger1.data.topics[2] == "0xc114fb059434563dc65ac8d57e7976e3eac534f4"`

result, err := eventTrigger.Evaluate(event, program)
if !result {
t.Errorf("expect expression to be match, but got false: error: %v", err)
}

program, err = expr.Compile(`
(trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && trigger1.data.topics[2] == "abc")
`)
program = `trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && trigger1.data.topics[2] == "abc"`

result, err = eventTrigger.Evaluate(event, program)
if result {
t.Errorf("expect expression to be not match, but got match: error: %v", err)
}

event, err = testutil.GetEventForTx("0x8f7c1f698f03d6d32c996b679ea1ebad45bbcdd9aa95d250dda74763cc0f508d", 81)
program = `trigger1.data.address == "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789" && trigger1.data.topics[0] == "0xbb47ee3e183a558b1a2ff0874b079f3fc5478b7454eacf2bfc5af2ff5878f972"`
result, err = eventTrigger.Evaluate(event, program)
if result {
t.Errorf("expect expression to be not match, but got match: error: %v", err)
Expand Down

0 comments on commit 76c2bcd

Please sign in to comment.