2026-03-09 16:21:42 +00:00
|
|
|
// 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"
|
|
|
|
|
|
2026-03-22 01:49:26 +00:00
|
|
|
coreerr "dappco.re/go/core/log"
|
2026-03-16 21:16:34 +00:00
|
|
|
|
2026-03-22 01:49:26 +00:00
|
|
|
"dappco.re/go/core/blockchain/chain"
|
|
|
|
|
"dappco.re/go/core/blockchain/config"
|
|
|
|
|
"dappco.re/go/core/blockchain/p2p"
|
|
|
|
|
levin "dappco.re/go/core/p2p/node/levin"
|
2026-03-09 16:21:42 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
func runChainSyncLoop(ctx context.Context, blockchain *chain.Chain, chainConfig *config.ChainConfig, hardForks []config.HardFork, seed string) {
|
2026-03-09 16:21:42 +00:00
|
|
|
opts := chain.SyncOptions{
|
|
|
|
|
VerifySignatures: false,
|
2026-04-04 04:18:37 +00:00
|
|
|
Forks: hardForks,
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
if err := runChainSyncOnce(ctx, blockchain, chainConfig, opts, seed); err != nil {
|
2026-03-09 16:21:42 +00:00
|
|
|
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):
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig *config.ChainConfig, opts chain.SyncOptions, seed string) error {
|
2026-03-09 16:21:42 +00:00
|
|
|
conn, err := net.DialTimeout("tcp", seed, 10*time.Second)
|
|
|
|
|
if err != nil {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", fmt.Sprintf("dial %s", seed), err)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
levinConn := levin.NewConnection(conn)
|
2026-03-09 16:21:42 +00:00
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
var peerIDBytes [8]byte
|
|
|
|
|
rand.Read(peerIDBytes[:])
|
|
|
|
|
peerID := binary.LittleEndian.Uint64(peerIDBytes[:])
|
2026-03-09 16:21:42 +00:00
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
localHeight, _ := blockchain.Height()
|
2026-03-09 16:21:42 +00:00
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
handshakeReq := p2p.HandshakeRequest{
|
2026-03-09 16:21:42 +00:00
|
|
|
NodeData: p2p.NodeData{
|
2026-04-04 04:18:37 +00:00
|
|
|
NetworkID: chainConfig.NetworkID,
|
2026-03-09 16:21:42 +00:00
|
|
|
PeerID: peerID,
|
|
|
|
|
LocalTime: time.Now().Unix(),
|
|
|
|
|
MyPort: 0,
|
|
|
|
|
},
|
|
|
|
|
PayloadData: p2p.CoreSyncData{
|
|
|
|
|
CurrentHeight: localHeight,
|
|
|
|
|
ClientVersion: config.ClientVersion,
|
|
|
|
|
NonPruningMode: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-04-04 04:18:37 +00:00
|
|
|
payload, err := p2p.EncodeHandshakeRequest(&handshakeReq)
|
2026-03-09 16:21:42 +00:00
|
|
|
if err != nil {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", "encode handshake", err)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
2026-04-04 04:18:37 +00:00
|
|
|
if err := levinConn.WritePacket(p2p.CommandHandshake, payload, true); err != nil {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", "write handshake", err)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
hdr, data, err := levinConn.ReadPacket()
|
2026-03-09 16:21:42 +00:00
|
|
|
if err != nil {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", "read handshake", err)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
if hdr.Command != uint32(p2p.CommandHandshake) {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", fmt.Sprintf("unexpected command %d", hdr.Command), nil)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
var handshakeResp p2p.HandshakeResponse
|
|
|
|
|
if err := handshakeResp.Decode(data); err != nil {
|
2026-04-04 04:10:23 +00:00
|
|
|
return coreerr.E("runChainSyncOnce", "decode handshake", err)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 18:30:32 +00:00
|
|
|
if !p2p.MeetsMinimumBuildVersion(handshakeResp.PayloadData.ClientVersion, chainConfig.IsTestnet) {
|
|
|
|
|
minBuild := p2p.MinimumRequiredBuildVersion(chainConfig.IsTestnet)
|
|
|
|
|
return coreerr.E(
|
|
|
|
|
"runChainSyncOnce",
|
|
|
|
|
fmt.Sprintf("peer build %q below minimum %d", handshakeResp.PayloadData.ClientVersion, minBuild),
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:21:42 +00:00
|
|
|
localSync := p2p.CoreSyncData{
|
|
|
|
|
CurrentHeight: localHeight,
|
|
|
|
|
ClientVersion: config.ClientVersion,
|
|
|
|
|
NonPruningMode: true,
|
|
|
|
|
}
|
2026-04-04 04:18:37 +00:00
|
|
|
p2pConn := chain.NewLevinP2PConn(levinConn, handshakeResp.PayloadData.CurrentHeight, localSync)
|
2026-03-09 16:21:42 +00:00
|
|
|
|
2026-04-04 04:18:37 +00:00
|
|
|
return blockchain.P2PSync(ctx, p2pConn, opts)
|
2026-03-09 16:21:42 +00:00
|
|
|
}
|