Skip to content

Fixed runTask returns undefined for eventTrigger problem #331

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 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
293 changes: 242 additions & 51 deletions core/taskengine/engine.go

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions core/taskengine/engine_trigger_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,26 @@ func TestBuildTriggerDataMapFromProtobufEventTriggerComprehensive(t *testing.T)
description: "Should map all TransferLog fields including the critical log_index field",
verifyFunc: func(t *testing.T, result map[string]interface{}) {
expected := map[string]interface{}{
"token_name": "Test Token",
"token_symbol": "TEST",
"token_decimals": uint32(18),
"transaction_hash": "0x1234567890abcdef",
"address": "0xabcdef1234567890",
"block_number": uint64(12345678),
"block_timestamp": uint64(1672531200),
"from_address": "0x1111111111111111",
"to_address": "0x2222222222222222",
"value": "1000000000000000000",
"value_formatted": "1.0",
"transaction_index": uint32(5),
"log_index": uint32(3),
"type": "TRIGGER_TYPE_EVENT",
"tokenName": "Test Token",
"tokenSymbol": "TEST",
"tokenDecimals": uint32(18),
"transactionHash": "0x1234567890abcdef",
"address": "0xabcdef1234567890",
"blockNumber": uint64(12345678),
"blockTimestamp": uint64(1672531200),
"fromAddress": "0x1111111111111111",
"toAddress": "0x2222222222222222",
"value": "1000000000000000000",
"valueFormatted": "1.0",
"transactionIndex": uint32(5),
"logIndex": uint32(3),
"type": "TRIGGER_TYPE_EVENT",
}

require.Equal(t, expected, result, "All TransferLog fields should be properly mapped")

require.Contains(t, result, "log_index", "log_index field should be present")
require.Equal(t, uint32(3), result["log_index"], "log_index should have correct value")
require.Contains(t, result, "logIndex", "logIndex field should be present")
require.Equal(t, uint32(3), result["logIndex"], "logIndex should have correct value")
},
},
{
Expand All @@ -281,29 +281,29 @@ func TestBuildTriggerDataMapFromProtobufEventTriggerComprehensive(t *testing.T)
description: "Should map all EvmLog fields including log_index",
verifyFunc: func(t *testing.T, result map[string]interface{}) {
expected := map[string]interface{}{
"block_number": uint64(12345678),
"log_index": uint32(3),
"tx_hash": "0x1234567890abcdef",
"address": "0xabcdef1234567890",
"topics": []string{"0xtopic1", "0xtopic2", "0xtopic3"},
"data": "0xdeadbeef",
"block_hash": "0xblockhash123456",
"transaction_index": uint32(5),
"removed": false,
"type": "TRIGGER_TYPE_EVENT",
"blockNumber": uint64(12345678),
"logIndex": uint32(3),
"transactionHash": "0x1234567890abcdef",
"address": "0xabcdef1234567890",
"topics": []string{"0xtopic1", "0xtopic2", "0xtopic3"},
"data": "0xdeadbeef",
"blockHash": "0xblockhash123456",
"transactionIndex": uint32(5),
"removed": false,
"type": "TRIGGER_TYPE_EVENT",
}

require.Equal(t, expected, result, "All EvmLog fields should be properly mapped")

require.Contains(t, result, "log_index", "log_index field should be present")
require.Equal(t, uint32(3), result["log_index"], "log_index should have correct value")
require.Contains(t, result, "logIndex", "logIndex field should be present")
require.Equal(t, uint32(3), result["logIndex"], "logIndex should have correct value")
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := buildTriggerDataMapFromProtobuf(avsproto.TriggerType_TRIGGER_TYPE_EVENT, test.input)
result := buildTriggerDataMapFromProtobuf(avsproto.TriggerType_TRIGGER_TYPE_EVENT, test.input, nil)

require.NotNil(t, result, "buildTriggerDataMapFromProtobuf should never return nil")

Expand All @@ -325,7 +325,7 @@ func TestBuildTriggerDataMapFromProtobufFieldCompleteness(t *testing.T) {
}

for _, triggerType := range triggerTypes {
result := buildTriggerDataMapFromProtobuf(triggerType, nil)
result := buildTriggerDataMapFromProtobuf(triggerType, nil, nil)
require.Contains(t, result, "type", "All trigger types should include type field")
require.Equal(t, triggerType.String(), result["type"], "Type field should match trigger type")
}
Expand Down
4 changes: 2 additions & 2 deletions core/taskengine/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestExecutorRunTaskSucess(t *testing.T) {
Id: "a1",
Type: "if",
// The test data is of this transaction https://sepolia.etherscan.io/tx/0x53beb2163994510e0984b436ebc828dc57e480ee671cfbe7ed52776c2a4830c8 which is 3.45 token
Expression: "Number(triggertest.data.value_formatted) >= 3",
Expression: "Number(triggertest.data.valueFormatted) >= 3",
},
},
},
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestExecutorRunTaskReturnAllExecutionData(t *testing.T) {
{
Id: "condition1",
Type: "if",
Expression: "Number(triggertest.data.value_formatted) >= 3",
Expression: "Number(triggertest.data.valueFormatted) >= 3",
},
},
},
Expand Down
41 changes: 35 additions & 6 deletions core/taskengine/run_node_immediately.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,38 @@ func (n *Engine) runProcessingNodeWithInputs(nodeType string, nodeConfig map[str
vm.WithLogger(n.logger).WithDb(n.db)

// Add input variables to VM for template processing and node access
// Apply dual-access mapping to enable both camelCase and snake_case field access
processedInputVariables := make(map[string]interface{})
for key, value := range inputVariables {
vm.AddVar(key, value)
// Apply dual-access mapping if the value is a map with a "data" field
if valueMap, ok := value.(map[string]interface{}); ok {
if dataField, hasData := valueMap["data"]; hasData {
if dataMap, isDataMap := dataField.(map[string]interface{}); isDataMap {
// Apply dual-access mapping to the data field
dualAccessData := CreateDualAccessMap(dataMap)
// Create a new map with the dual-access data
processedValue := make(map[string]interface{})
for k, v := range valueMap {
if k == "data" {
processedValue[k] = dualAccessData
} else {
processedValue[k] = v
}
}
vm.AddVar(key, processedValue)
processedInputVariables[key] = processedValue
} else {
vm.AddVar(key, value)
processedInputVariables[key] = value
}
} else {
vm.AddVar(key, value)
processedInputVariables[key] = value
}
} else {
vm.AddVar(key, value)
processedInputVariables[key] = value
}
}

// Create node from type and config
Expand All @@ -730,8 +760,8 @@ func (n *Engine) runProcessingNodeWithInputs(nodeType string, nodeConfig map[str
return nil, fmt.Errorf("failed to create node: %w", err)
}

// Execute the node
executionStep, err := vm.RunNodeWithInputs(node, inputVariables)
// Execute the node with processed input variables
executionStep, err := vm.RunNodeWithInputs(node, processedInputVariables)
if err != nil {
return nil, fmt.Errorf("node execution failed: %w", err)
}
Expand Down Expand Up @@ -1071,8 +1101,7 @@ func (n *Engine) RunNodeImmediatelyRPC(user *model.User, req *avsproto.RunNodeWi

// Log successful execution
if n.logger != nil {
n.logger.Info("RunNodeImmediatelyRPC: Executed successfully", "nodeTypeStr", nodeTypeStr, "originalNodeType", req.NodeType, "configKeys", getStringMapKeys(nodeConfig), "inputKeys", getStringMapKeys(inputVariables))

n.logger.Info("RunNodeImmediatelyRPC: Executed successfully", "nodeTypeStr", nodeTypeStr, "originalNodeType", req.NodeType)
}

// Convert result to the appropriate protobuf output type
Expand Down Expand Up @@ -1572,7 +1601,7 @@ func (n *Engine) RunTriggerRPC(user *model.User, req *avsproto.RunTriggerReq) (*

// Log successful execution
if n.logger != nil {
n.logger.Info("RunTriggerRPC: Executed successfully", "triggerTypeStr", triggerTypeStr, "originalTriggerType", req.TriggerType, "configKeys", getStringMapKeys(triggerConfig))
n.logger.Info("RunTriggerRPC: Executed successfully", "triggerTypeStr", triggerTypeStr, "originalTriggerType", req.TriggerType)
}

// Convert result to the appropriate protobuf output type
Expand Down
Loading