diff --git a/sync_loop.go b/sync_loop.go index 72f0b2e..1e1131d 100644 --- a/sync_loop.go +++ b/sync_loop.go @@ -60,13 +60,18 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig } defer conn.Close() - levinConnection := levin.NewConnection(conn) + p2pConn := levin.NewConnection(conn) var peerIDBytes [8]byte - rand.Read(peerIDBytes[:]) + if _, err := rand.Read(peerIDBytes[:]); err != nil { + return coreerr.E("runChainSyncOnce", "generate peer id", err) + } peerID := binary.LittleEndian.Uint64(peerIDBytes[:]) - localHeight, _ := blockchain.Height() + localHeight, err := blockchain.Height() + if err != nil { + return coreerr.E("runChainSyncOnce", "get local height", err) + } handshakeRequest := p2p.HandshakeRequest{ NodeData: p2p.NodeData{ @@ -85,11 +90,11 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig if err != nil { return coreerr.E("runChainSyncOnce", "encode handshake", err) } - if err := levinConnection.WritePacket(p2p.CommandHandshake, payload, true); err != nil { + if err := p2pConn.WritePacket(p2p.CommandHandshake, payload, true); err != nil { return coreerr.E("runChainSyncOnce", "write handshake", err) } - packetHeader, packetData, err := levinConnection.ReadPacket() + packetHeader, packetData, err := p2pConn.ReadPacket() if err != nil { return coreerr.E("runChainSyncOnce", "read handshake", err) } @@ -111,7 +116,7 @@ func runChainSyncOnce(ctx context.Context, blockchain *chain.Chain, chainConfig ClientVersion: config.ClientVersion, NonPruningMode: true, } - p2pConnection := chain.NewLevinP2PConn(levinConnection, handshakeResponse.PayloadData.CurrentHeight, localSyncData) + p2pConnection := chain.NewLevinP2PConn(p2pConn, handshakeResponse.PayloadData.CurrentHeight, localSyncData) return blockchain.P2PSync(ctx, p2pConnection, opts) }