forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made caplin endpoints flaggable and added documentation to the README. (
erigontech#9458) * Fixed small hiccup in attestations deltas * Added flags to caplin to determine which endpoint to expose. * Fused ValidatorAPI <-> APIHandler
- Loading branch information
1 parent
e66842d
commit dd1c521
Showing
25 changed files
with
508 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package handler | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/gfx-labs/sse" | ||
"github.com/ledgerwatch/log/v3" | ||
) | ||
|
||
var validTopics = map[string]struct{}{ | ||
"head": {}, | ||
"block": {}, | ||
"attestation": {}, | ||
"voluntary_exit": {}, | ||
"bls_to_execution_change": {}, | ||
"finalized_checkpoint": {}, | ||
"chain_reorg": {}, | ||
"contribution_and_proof": {}, | ||
"light_client_finality_update": {}, | ||
"light_client_optimistic_update": {}, | ||
"payload_attributes": {}, | ||
"*": {}, | ||
} | ||
|
||
func (a *ApiHandler) EventSourceGetV1Events(w http.ResponseWriter, r *http.Request) { | ||
sink, err := sse.DefaultUpgrader.Upgrade(w, r) | ||
if err != nil { | ||
http.Error(w, "failed to upgrade", http.StatusInternalServerError) | ||
} | ||
topics := r.URL.Query()["topics"] | ||
for _, v := range topics { | ||
if _, ok := validTopics[v]; !ok { | ||
http.Error(w, fmt.Sprintf("invalid Topic: %s", v), http.StatusBadRequest) | ||
} | ||
} | ||
var mu sync.Mutex | ||
closer, err := a.emitters.Subscribe(topics, func(topic string, item any) { | ||
buf := &bytes.Buffer{} | ||
err := json.NewEncoder(buf).Encode(item) | ||
if err != nil { | ||
// return early | ||
return | ||
} | ||
mu.Lock() | ||
err = sink.Encode(&sse.Event{ | ||
Event: []byte(topic), | ||
Data: buf, | ||
}) | ||
mu.Unlock() | ||
if err != nil { | ||
log.Error("failed to encode data", "topic", topic, "err", err) | ||
} | ||
// OK to ignore this error. maybe should log it later? | ||
}) | ||
if err != nil { | ||
http.Error(w, "failed to subscribe", http.StatusInternalServerError) | ||
return | ||
} | ||
defer closer() | ||
<-r.Context().Done() | ||
|
||
} |
Oops, something went wrong.