Files
dejo-node/cmd/main.go
2025-06-17 18:26:14 -03:00

91 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"context"
"dejo_node/internal/api"
"dejo_node/internal/consensus"
"dejo_node/internal/staking"
"dejo_node/internal/storage"
"dejo_node/internal/transactions"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
)
func main() {
ctx := context.Background()
nodeID := os.Getenv("NODE_ID")
port := os.Getenv("DEJO_PORT")
blockStore := storage.NewBlockStore("./data/blocks")
stakingStore := staking.NewStakingStore()
// Carregar staking existente se disponível
if err := stakingStore.LoadFromDisk("./data/staking.db"); err != nil {
fmt.Println(" Criando novo staking store")
}
// Registrar todos os validadores com stake mínimo
minStake := uint64(1000)
validators := strings.Split(os.Getenv("DEJO_VALIDATORS"), ",")
for _, validator := range validators {
if err := stakingStore.Stake(validator, minStake, 1000); err != nil {
panic(err)
}
}
mp := transactions.NewMempool()
handler := api.NewHandler()
handler.SetStores(blockStore, stakingStore, mp)
server := api.NewServer(handler)
go func() {
addr := ":" + port
fmt.Printf("🌐 Iniciando servidor HTTP em %s\n", addr)
// Adicionar pequeno delay para garantir que tudo está inicializado
time.Sleep(100 * time.Millisecond)
// Verificar se o servidor está respondendo
go func() {
time.Sleep(2 * time.Second) // Aumentar tempo de espera
client := http.Client{Timeout: 2 * time.Second}
resp, err := client.Get("http://localhost" + addr + "/ping")
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
log.Printf("⚠️ Aviso: Servidor HTTP pode não estar respondendo - %v", err)
return // Não encerrar o programa, apenas registrar o aviso
}
resp.Body.Close()
}()
if err := http.ListenAndServe(addr, server); err != nil {
log.Fatalf("❌ Falha ao iniciar servidor HTTP: %v", err)
}
}()
round := consensus.NewRoundState(1)
transport := consensus.NewHTTPTransport()
consensus.StartConsensusLoop(
ctx,
nodeID,
round,
transport.Broadcast,
700,
blockStore,
func() *transactions.Block {
return &transactions.Block{
Txns: mp.All(),
}
},
stakingStore,
1000, // minStake
&consensus.LivenessMonitor{},
)
}