diff --git a/examples/webhooks/main.go b/examples/webhooks/main.go index a466301..5d5986b 100644 --- a/examples/webhooks/main.go +++ b/examples/webhooks/main.go @@ -1,9 +1,12 @@ package main import ( + "crypto/sha256" + "encoding/hex" "encoding/json" "fmt" "net/http" + "sync" ) type AccountTxPayload struct { @@ -12,20 +15,53 @@ type AccountTxPayload struct { TxHash string `json:"tx_hash"` } +type MempoolEvent struct { + EventType string `json:"event_type"` + Boc string `json:"boc"` +} + +var ( + mu sync.Mutex + hashes = make(map[string]int) +) + func webhook(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() - var payload AccountTxPayload + fmt.Printf("req: %v\n", req.Method) + for name, header := range req.Header { + fmt.Printf("header: %v -> %#v\n", name, header) + + } + var payload MempoolEvent if err := json.NewDecoder(req.Body).Decode(&payload); err != nil { w.WriteHeader(http.StatusBadRequest) return } - fmt.Printf("%v\n", payload) + hash := sha256.New() + hash.Write([]byte(payload.Boc)) + payloadHash := hash.Sum(nil) + payloadHex := hex.EncodeToString(payloadHash) + w.WriteHeader(http.StatusOK) + + mu.Lock() + defer mu.Unlock() + + hashes[payloadHex] += 1 + + value := hashes[payloadHex] + if value > 2 { + fmt.Printf("payload hex %v -> %v\n", payloadHex, value) + } + + if len(hashes) > 100_000 { + hashes = make(map[string]int) + } } func main() { - http.HandleFunc("/", webhook) - http.ListenAndServe(":8092", nil) + http.HandleFunc("/webhook", webhook) + http.ListenAndServe(":8030", nil) }