Migrate from stdlib flag to cli.Main() + WithCommands() pattern: - AddChainCommands() registration with persistent --data-dir/--seed/--testnet flags - Explorer subcommand (TUI block explorer, replaces old default mode) - Sync subcommand with headless foreground and --daemon/--stop modes - sync_service.go extracts syncLoop/syncOnce from old main.go - cmd/core-chain/main.go standalone binary entry point - Add go-process dependency for daemon lifecycle Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
// Copyright (c) 2017-2026 Lethean (https://lt.hn)
|
|
//
|
|
// Licensed under the European Union Public Licence (EUPL) version 1.2.
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package blockchain
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"time"
|
|
|
|
"forge.lthn.ai/core/go-blockchain/chain"
|
|
"forge.lthn.ai/core/go-blockchain/config"
|
|
"forge.lthn.ai/core/go-blockchain/p2p"
|
|
levin "forge.lthn.ai/core/go-p2p/node/levin"
|
|
)
|
|
|
|
func syncLoop(ctx context.Context, c *chain.Chain, cfg *config.ChainConfig, forks []config.HardFork, seed string) {
|
|
opts := chain.SyncOptions{
|
|
VerifySignatures: false,
|
|
Forks: forks,
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
}
|
|
|
|
if err := syncOnce(ctx, c, cfg, opts, seed); err != nil {
|
|
log.Printf("sync: %v (retrying in 10s)", err)
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(10 * time.Second):
|
|
}
|
|
continue
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(30 * time.Second):
|
|
}
|
|
}
|
|
}
|
|
|
|
func syncOnce(ctx context.Context, c *chain.Chain, cfg *config.ChainConfig, opts chain.SyncOptions, seed string) error {
|
|
conn, err := net.DialTimeout("tcp", seed, 10*time.Second)
|
|
if err != nil {
|
|
return fmt.Errorf("dial %s: %w", seed, err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
lc := levin.NewConnection(conn)
|
|
|
|
var peerIDBuf [8]byte
|
|
rand.Read(peerIDBuf[:])
|
|
peerID := binary.LittleEndian.Uint64(peerIDBuf[:])
|
|
|
|
localHeight, _ := c.Height()
|
|
|
|
req := p2p.HandshakeRequest{
|
|
NodeData: p2p.NodeData{
|
|
NetworkID: cfg.NetworkID,
|
|
PeerID: peerID,
|
|
LocalTime: time.Now().Unix(),
|
|
MyPort: 0,
|
|
},
|
|
PayloadData: p2p.CoreSyncData{
|
|
CurrentHeight: localHeight,
|
|
ClientVersion: config.ClientVersion,
|
|
NonPruningMode: true,
|
|
},
|
|
}
|
|
payload, err := p2p.EncodeHandshakeRequest(&req)
|
|
if err != nil {
|
|
return fmt.Errorf("encode handshake: %w", err)
|
|
}
|
|
if err := lc.WritePacket(p2p.CommandHandshake, payload, true); err != nil {
|
|
return fmt.Errorf("write handshake: %w", err)
|
|
}
|
|
|
|
hdr, data, err := lc.ReadPacket()
|
|
if err != nil {
|
|
return fmt.Errorf("read handshake: %w", err)
|
|
}
|
|
if hdr.Command != uint32(p2p.CommandHandshake) {
|
|
return fmt.Errorf("unexpected command %d", hdr.Command)
|
|
}
|
|
|
|
var resp p2p.HandshakeResponse
|
|
if err := resp.Decode(data); err != nil {
|
|
return fmt.Errorf("decode handshake: %w", err)
|
|
}
|
|
|
|
localSync := p2p.CoreSyncData{
|
|
CurrentHeight: localHeight,
|
|
ClientVersion: config.ClientVersion,
|
|
NonPruningMode: true,
|
|
}
|
|
p2pConn := chain.NewLevinP2PConn(lc, resp.PayloadData.CurrentHeight, localSync)
|
|
|
|
return c.P2PSync(ctx, p2pConn, opts)
|
|
}
|