Skip to content

Commit

Permalink
Fix the log event trigger to match the schema. (#15035)
Browse files Browse the repository at this point in the history
* Fix the log event trigger to match the schema.

* Decode the bytes instead and add an 0x prefix

* Fix wrapping the log.Data as values.Map and ensure working tests

* Fixed lint errors

* Fixed more lint errors

---------

Co-authored-by: Sri Kidambi <[email protected]>
  • Loading branch information
nolag and kidambisrinivas authored Oct 30, 2024
1 parent 4d14533 commit cf726db
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"type": "integer",
"minimum": 0
}
}
},
"required": ["Height", "Hash", "Timestamp"]
},
"config": {
"type": "object",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions core/capabilities/triggers/logevent/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,35 @@ func (l *logEventTrigger) listen() {

// Create log event trigger capability response
func createTriggerResponse(log types.Sequence, version string) capabilities.TriggerResponse {
wrappedPayload, err := values.WrapMap(log)
dataAsValuesMap, err := values.WrapMap(log.Data)
if err != nil {
return capabilities.TriggerResponse{
Err: fmt.Errorf("error wrapping trigger event: %s", err),
Err: fmt.Errorf("error decoding log data as values.Map: %w", err),
}
}
dataAsMap := map[string]any{}
err = dataAsValuesMap.UnwrapTo(&dataAsMap)
if err != nil {
return capabilities.TriggerResponse{
Err: fmt.Errorf("error decoding log data as map[string]any: %w", err),
}
}

wrappedPayload, err := values.WrapMap(&logeventcap.Output{
Cursor: log.Cursor,
Data: dataAsMap,
Head: logeventcap.Head{
Hash: fmt.Sprintf("0x%x", log.Hash),
Height: log.Height,
Timestamp: log.Timestamp,
},
})
if err != nil {
return capabilities.TriggerResponse{
Err: fmt.Errorf("error wrapping trigger event: %w", err),
}
}

return capabilities.TriggerResponse{
Event: capabilities.TriggerEvent{
TriggerType: version,
Expand Down
89 changes: 89 additions & 0 deletions core/scripts/gateway/web_api_trigger/test_server/test_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"fmt"
"io"
"net/http"
"time"
)

func handler(w http.ResponseWriter, r *http.Request) {
// Log request method and URL
fmt.Printf("Received %s request for %s\n", r.Method, r.URL.Path)

// Handle GET requests
if r.Method == http.MethodGet {
fmt.Println("GET request received")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("GET request received"))
if err != nil {
http.Error(w, "could not write request body", http.StatusInternalServerError)
return
}
}

// Handle POST requests
if r.Method == http.MethodPost {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read request body", http.StatusInternalServerError)
return
}
fmt.Printf("POST request body: %s\n", string(body))

w.WriteHeader(http.StatusOK)
}
}

func lockHandler(w http.ResponseWriter, r *http.Request) {
// Log request method and URL
fmt.Printf("Received %s request for %s\n", r.Method, r.URL.Path)

// Handle POST requests
if r.Method == http.MethodPost {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read request body", http.StatusInternalServerError)
return
}
fmt.Printf("Assets locked. E2E ID: %s\n", string(body))

w.WriteHeader(http.StatusOK)
}
}

func unlockHandler(w http.ResponseWriter, r *http.Request) {
// Log request method and URL
fmt.Printf("Received %s request for %s\n", r.Method, r.URL.Path)

// Handle POST requests
if r.Method == http.MethodPost {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read request body", http.StatusInternalServerError)
return
}
fmt.Printf("Assets unlocked. Settlement E2E ID: %s\n", string(body))

w.WriteHeader(http.StatusOK)
}
}

func main() {
// Register the handler for all incoming requests
http.HandleFunc("/", handler)
http.HandleFunc("/lock", lockHandler)
http.HandleFunc("/unlock", unlockHandler)

// Listen on port 1000
port := ":1000"
fmt.Printf("Server listening on port %s\n", port)
server := &http.Server{
Addr: port,
ReadHeaderTimeout: 30 * time.Second,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}

0 comments on commit cf726db

Please sign in to comment.