refactor(blockchain): clarify handshake sync naming
Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
f1738527bc
commit
ccdcfbaacf
3 changed files with 25 additions and 13 deletions
|
|
@ -177,6 +177,10 @@ func (r *HandshakeResponse) Decode(data []byte) error {
|
|||
|
||||
// ValidateHandshakeResponse verifies that a remote peer's handshake response
|
||||
// matches the expected network and satisfies the minimum build version gate.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// err := ValidateHandshakeResponse(&resp, config.NetworkIDMainnet, false)
|
||||
func ValidateHandshakeResponse(resp *HandshakeResponse, expectedNetworkID [16]byte, isTestnet bool) error {
|
||||
if resp.NodeData.NetworkID != expectedNetworkID {
|
||||
return fmt.Errorf("p2p: peer network id %x does not match expected %x",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ const (
|
|||
|
||||
// MinimumRequiredBuildVersion returns the minimum accepted peer version gate
|
||||
// for the given network.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// MinimumRequiredBuildVersion(false) // 601 on mainnet
|
||||
func MinimumRequiredBuildVersion(isTestnet bool) uint64 {
|
||||
if isTestnet {
|
||||
return MinimumRequiredBuildVersionTestnet
|
||||
|
|
@ -69,6 +73,10 @@ func PeerBuildVersion(clientVersion string) (uint64, bool) {
|
|||
|
||||
// MeetsMinimumBuildVersion reports whether the peer's version is acceptable
|
||||
// for the given network.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// MeetsMinimumBuildVersion("6.0.1.2[go-blockchain]", false) // true
|
||||
func MeetsMinimumBuildVersion(clientVersion string, isTestnet bool) bool {
|
||||
buildVersion, ok := PeerBuildVersion(clientVersion)
|
||||
if !ok {
|
||||
|
|
|
|||
26
sync_loop.go
26
sync_loop.go
|
|
@ -60,7 +60,7 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig
|
|||
}
|
||||
defer conn.Close()
|
||||
|
||||
levinConn := levin.NewConnection(conn)
|
||||
levinConnection := levin.NewConnection(conn)
|
||||
|
||||
var peerIDBytes [8]byte
|
||||
rand.Read(peerIDBytes[:])
|
||||
|
|
@ -68,7 +68,7 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig
|
|||
|
||||
localHeight, _ := blockchain.Height()
|
||||
|
||||
handshakeReq := p2p.HandshakeRequest{
|
||||
handshakeRequest := p2p.HandshakeRequest{
|
||||
NodeData: p2p.NodeData{
|
||||
NetworkID: chainConfig.NetworkID,
|
||||
PeerID: peerID,
|
||||
|
|
@ -81,37 +81,37 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig
|
|||
NonPruningMode: true,
|
||||
},
|
||||
}
|
||||
payload, err := p2p.EncodeHandshakeRequest(&handshakeReq)
|
||||
payload, err := p2p.EncodeHandshakeRequest(&handshakeRequest)
|
||||
if err != nil {
|
||||
return coreerr.E("runChainSyncOnce", "encode handshake", err)
|
||||
}
|
||||
if err := levinConn.WritePacket(p2p.CommandHandshake, payload, true); err != nil {
|
||||
if err := levinConnection.WritePacket(p2p.CommandHandshake, payload, true); err != nil {
|
||||
return coreerr.E("runChainSyncOnce", "write handshake", err)
|
||||
}
|
||||
|
||||
hdr, data, err := levinConn.ReadPacket()
|
||||
packetHeader, packetData, err := levinConnection.ReadPacket()
|
||||
if err != nil {
|
||||
return coreerr.E("runChainSyncOnce", "read handshake", err)
|
||||
}
|
||||
if hdr.Command != uint32(p2p.CommandHandshake) {
|
||||
return coreerr.E("runChainSyncOnce", fmt.Sprintf("unexpected command %d", hdr.Command), nil)
|
||||
if packetHeader.Command != uint32(p2p.CommandHandshake) {
|
||||
return coreerr.E("runChainSyncOnce", fmt.Sprintf("unexpected command %d", packetHeader.Command), nil)
|
||||
}
|
||||
|
||||
var handshakeResp p2p.HandshakeResponse
|
||||
if err := handshakeResp.Decode(data); err != nil {
|
||||
var handshakeResponse p2p.HandshakeResponse
|
||||
if err := handshakeResponse.Decode(packetData); err != nil {
|
||||
return coreerr.E("runChainSyncOnce", "decode handshake", err)
|
||||
}
|
||||
|
||||
if err := p2p.ValidateHandshakeResponse(&handshakeResp, chainConfig.NetworkID, chainConfig.IsTestnet); err != nil {
|
||||
if err := p2p.ValidateHandshakeResponse(&handshakeResponse, chainConfig.NetworkID, chainConfig.IsTestnet); err != nil {
|
||||
return coreerr.E("runChainSyncOnce", "validate handshake", err)
|
||||
}
|
||||
|
||||
localSync := p2p.CoreSyncData{
|
||||
localSyncData := p2p.CoreSyncData{
|
||||
CurrentHeight: localHeight,
|
||||
ClientVersion: config.ClientVersion,
|
||||
NonPruningMode: true,
|
||||
}
|
||||
p2pConn := chain.NewLevinP2PConn(levinConn, handshakeResp.PayloadData.CurrentHeight, localSync)
|
||||
p2pConnection := chain.NewLevinP2PConn(levinConnection, handshakeResponse.PayloadData.CurrentHeight, localSyncData)
|
||||
|
||||
return blockchain.P2PSync(ctx, p2pConn, opts)
|
||||
return blockchain.P2PSync(ctx, p2pConnection, opts)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue