Skip to content

Commit

Permalink
feat(BUX-712): renaming to privated methods, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Apr 3, 2024
1 parent 6259531 commit 2c376ec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
1 change: 1 addition & 0 deletions internal/transports/p2p/peer/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
minAcceptableProtocolVersion = wire.MultipleAddressVersion
pingInterval = 2 * time.Minute
writeMsgChannelBufferSize = 10
ourServices = wire.SFspv
)

var zeroHash chainhash.Hash
36 changes: 16 additions & 20 deletions internal/transports/p2p/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type Peer struct {
protocolVersion uint32
nonce uint64
lastBlock int32
startingHeight int32
timeOffset int64
userAgent string
verAckReceived bool
Expand Down Expand Up @@ -100,6 +99,15 @@ func (p *Peer) Connect() error {
}
p.conn = conn
p.log.Info().Msgf("connected to peer: %s", p)

err = p.negotiateProtocol()
if err != nil {
p.log.Error().Msgf("error negotiating protocol with peer %s, reason: %v", p, err)
return err
}

go p.pingHandler()

return nil
}

Expand All @@ -118,26 +126,19 @@ func (p *Peer) Disconnect() error {
return nil
}

func (p *Peer) Start() error {
err := p.negotiateProtocol()
if err != nil {
p.log.Error().Msgf("error negotiating protocol with peer %s, reason: %v", p, err)
return err
}

func (p *Peer) StartHeadersSync() error {
go p.writeMsgHandler()
go p.readMsgHandler()
go p.pingHandler()

err = p.startSync()
err := p.requestHeaders()
if err != nil {
p.log.Error().Msgf("error starting sync from peer %s, reason: %v", p, err)
p.log.Error().Msgf("error requesting headers from peer %s, reason: %v", p, err)
return err
}
return nil
}

func (p *Peer) startSync() error {
func (p *Peer) requestHeaders() error {
p.log.Info().Msgf("requesting headers from peer %s", p)

var err error
Expand Down Expand Up @@ -244,11 +245,7 @@ func (p *Peer) readMsgHandler() {
}

func (p *Peer) writeMessage(msg wire.Message) error {
err := wire.WriteMessage(p.conn, msg, p.protocolVersion, p.chainParams.Net)
if err != nil {
return err
}
return nil
return wire.WriteMessage(p.conn, msg, p.protocolVersion, p.chainParams.Net)
}

func (p *Peer) writeRejectMessage(msg wire.Message, reason string) {
Expand Down Expand Up @@ -300,7 +297,7 @@ func (p *Peer) writeOurVersionMsg() error {
p.nonce = nonce

ourNA := &wire.NetAddress{
Services: wire.SFspv,
Services: ourServices,
Timestamp: time.Now(),
}
theirNA := wire.NewNetAddress(p.addr, 0)
Expand All @@ -314,7 +311,7 @@ func (p *Peer) writeOurVersionMsg() error {
return err
}

msg.Services = p.services
msg.Services = ourServices

err = p.writeMessage(msg)
if err != nil {
Expand Down Expand Up @@ -377,7 +374,6 @@ func (p *Peer) handleVersionMessage(msg *wire.MsgVersion) error {
p.protocolVersion = min(p.protocolVersion, uint32(msg.ProtocolVersion))
p.services = msg.Services
p.lastBlock = msg.LastBlock
p.startingHeight = msg.LastBlock
p.timeOffset = msg.Timestamp.Unix() - time.Now().Unix()
p.userAgent = msg.UserAgent

Expand Down
2 changes: 1 addition & 1 deletion internal/transports/p2p/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/rs/zerolog"
)

func SeedFromDNS(seeds []chaincfg.DNSSeed, log *zerolog.Logger) []net.IP {
func seedFromDNS(seeds []chaincfg.DNSSeed, log *zerolog.Logger) []net.IP {
allSeedPeers := make([]net.IP, 0)

for _, seed := range seeds {
Expand Down
2 changes: 1 addition & 1 deletion internal/transports/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewServer(
}

func (s *server) Start() error {
seeds := SeedFromDNS(s.chainParams.DNSSeeds, s.log)
seeds := seedFromDNS(s.chainParams.DNSSeeds, s.log)
if len(seeds) == 0 {
return errors.New("no seeds found")
}
Expand Down

0 comments on commit 2c376ec

Please sign in to comment.