fiz: correções da pool

This commit is contained in:
Júnior
2025-06-17 18:26:14 -03:00
parent 682027d517
commit 9259f36e9c
31 changed files with 373 additions and 269 deletions

View File

@ -1,30 +1,33 @@
package api
import (
"encoding/json"
"log"
"net/http"
"dejo_node/internal/transactions"
"net/http"
)
func (h *Handler) SendTransaction(w http.ResponseWriter, r *http.Request) {
log.Println("📥 Nova transação recebida")
func (h *Handler) SubmitTransaction(w http.ResponseWriter, r *http.Request) {
var tx transactions.Transaction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&tx); err != nil {
http.Error(w, "formato inválido", http.StatusBadRequest)
err := ReadJSON(r, &tx)
if err != nil {
WriteError(w, http.StatusBadRequest, "Transação inválida")
return
}
if tx.From == "" || tx.To == "" || tx.Value <= 0 {
http.Error(w, "transação inválida", http.StatusBadRequest)
err = h.Mempool.Add(&tx)
if err != nil {
WriteError(w, http.StatusInternalServerError, "Erro ao adicionar transação")
return
}
h.Mempool.Add(&tx)
log.Printf("✅ Transação adicionada ao mempool: %+v\n", tx)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
}
func (h *Handler) GetTransaction(w http.ResponseWriter, r *http.Request) {
hash := r.URL.Query().Get("hash")
tx := h.Mempool.GetByHash(hash)
if tx == nil {
WriteError(w, http.StatusNotFound, "Transação não encontrada")
return
}
WriteJSON(w, http.StatusOK, tx)
}