commit inicial do projeto
This commit is contained in:
68
internal/sync/height.go
Normal file
68
internal/sync/height.go
Normal file
@ -0,0 +1,68 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"dejo_node/internal/transactions"
|
||||
|
||||
network "github.com/libp2p/go-libp2p/core/network"
|
||||
)
|
||||
|
||||
type SyncMessage struct {
|
||||
Type string `json:"type"`
|
||||
Height int64 `json:"height,omitempty"`
|
||||
Block *transactions.Block `json:"block,omitempty"`
|
||||
}
|
||||
|
||||
func (s *SyncManager) handleStream(stream network.Stream) {
|
||||
fmt.Println("📥 [SYNC] Conexão de:", stream.Conn().RemotePeer())
|
||||
defer stream.Close()
|
||||
|
||||
decoder := json.NewDecoder(stream)
|
||||
encoder := json.NewEncoder(stream)
|
||||
|
||||
for {
|
||||
var msg SyncMessage
|
||||
if err := decoder.Decode(&msg); err != nil {
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
fmt.Println("Erro ao decodificar mensagem de sync:", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch msg.Type {
|
||||
case "RequestHeight":
|
||||
latest, err := s.BlockStore.GetLatestBlock()
|
||||
if err != nil {
|
||||
fmt.Println("Erro ao obter bloco mais recente:", err)
|
||||
return
|
||||
}
|
||||
height := int64(latest.Index)
|
||||
resp := SyncMessage{
|
||||
Type: "ResponseHeight",
|
||||
Height: height,
|
||||
}
|
||||
_ = encoder.Encode(resp)
|
||||
fmt.Println("↩️ Respondendo altura:", height)
|
||||
|
||||
case "RequestBlock":
|
||||
blk, err := s.BlockStore.GetBlockByIndex(int(msg.Height))
|
||||
if err != nil {
|
||||
fmt.Println("❌ Erro ao buscar bloco:", err)
|
||||
return
|
||||
}
|
||||
resp := SyncMessage{
|
||||
Type: "ResponseBlock",
|
||||
Block: blk,
|
||||
}
|
||||
_ = encoder.Encode(resp)
|
||||
fmt.Printf("📤 Enviando bloco altura %d\n", msg.Height)
|
||||
|
||||
default:
|
||||
fmt.Println("Mensagem de sync desconhecida:", msg.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
33
internal/sync/sync.go
Normal file
33
internal/sync/sync.go
Normal file
@ -0,0 +1,33 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"dejo_node/internal/storage"
|
||||
|
||||
host "github.com/libp2p/go-libp2p/core/host"
|
||||
)
|
||||
|
||||
const SyncProtocolID = "/dejo/sync/1.0.0"
|
||||
|
||||
// SyncManager coordena o processo de sincronização de blocos com outros peers.
|
||||
type SyncManager struct {
|
||||
Ctx context.Context
|
||||
Host host.Host
|
||||
BlockStore *storage.BlockStore
|
||||
}
|
||||
|
||||
// NewSyncManager cria um novo gerenciador de sincronização.
|
||||
func NewSyncManager(ctx context.Context, h host.Host, bs *storage.BlockStore) *SyncManager {
|
||||
manager := &SyncManager{
|
||||
Ctx: ctx,
|
||||
Host: h,
|
||||
BlockStore: bs,
|
||||
}
|
||||
|
||||
h.SetStreamHandler(SyncProtocolID, manager.handleStream)
|
||||
fmt.Println("🔄 Handler de sync registrado para protocolo:", SyncProtocolID)
|
||||
|
||||
return manager
|
||||
}
|
||||
Reference in New Issue
Block a user