@@ -57,6 +57,7 @@ type Block struct {
57
57
58
58
type Peer struct {
59
59
address string
60
+ lastBlock int
60
61
network wire.BitcoinNet
61
62
mu sync.RWMutex
62
63
readConn net.Conn
@@ -96,18 +97,20 @@ type Peer struct {
96
97
}
97
98
98
99
// NewPeer returns a new bitcoin peer for the provided address and configuration.
99
- func NewPeer (logger * slog.Logger , address string , peerHandler PeerHandlerI , network wire.BitcoinNet , options ... PeerOptions ) (* Peer , error ) {
100
+ func NewPeer (logger * slog.Logger , address string , lastBlock int , peerHandler PeerHandlerI , network wire.BitcoinNet , options ... PeerOptions ) (* Peer , error ) {
100
101
writeChan := make (chan wire.Message , 10000 )
101
102
102
103
peerLogger := logger .With (
103
104
slog .Group ("peer" ,
104
105
slog .String ("network" , network .String ()),
105
106
slog .String ("address" , address ),
107
+ slog .Int ("address" , lastBlock ),
106
108
),
107
109
)
108
110
109
111
p := & Peer {
110
112
network : network ,
113
+ lastBlock : lastBlock ,
111
114
address : address ,
112
115
writeChan : writeChan ,
113
116
pingPongAlive : make (chan struct {}, 1 ),
@@ -447,6 +450,11 @@ func (p *Peer) startReadHandler(ctx context.Context) {
447
450
continue
448
451
}
449
452
453
+ versionMsg := msg .(* wire.MsgVersion )
454
+ if err = p .peerHandler .HandleVersion (versionMsg , p ); err != nil {
455
+ commandLogger .Error ("HandlerVersion returned an error." , slog .String (errKey , err .Error ()))
456
+ }
457
+
450
458
verackMsg := wire .NewMsgVerAck ()
451
459
if err = wire .WriteMessage (readConn , verackMsg , wire .ProtocolVersion , p .network ); err != nil {
452
460
commandLogger .Error ("failed to write message" , slog .String (errKey , err .Error ()))
@@ -508,6 +516,26 @@ func (p *Peer) startReadHandler(ctx context.Context) {
508
516
commandLogger .Error ("Unable to process tx" , slog .String (hashKey , txMsg .TxHash ().String ()), slog .String (errKey , err .Error ()))
509
517
}
510
518
519
+ case wire .CmdHeaders :
520
+ msgHeaders , ok := msg .(* wire.MsgHeaders )
521
+ if ! ok {
522
+ continue
523
+ }
524
+
525
+ if err = p .peerHandler .HandleHeaders (msgHeaders , p ); err != nil {
526
+ commandLogger .Error ("Unable to process headers" , slog .String (errKey , err .Error ()))
527
+ }
528
+
529
+ case wire .CmdAddr :
530
+ msgAddress , ok := msg .(* wire.MsgAddr )
531
+ if ! ok {
532
+ continue
533
+ }
534
+
535
+ if err = p .peerHandler .HandleAddresses (msgAddress , p ); err != nil {
536
+ commandLogger .Error ("Unable to process headers" , slog .String (errKey , err .Error ()))
537
+ }
538
+
511
539
case wire .CmdBlock :
512
540
msgBlock , ok := msg .(* wire.MsgBlock )
513
541
if ok {
@@ -763,7 +791,6 @@ func (p *Peer) startWriteChannelHandler(ctx context.Context, instance int) {
763
791
}
764
792
765
793
func (p * Peer ) versionMessage (address string ) * wire.MsgVersion {
766
- lastBlock := int32 (0 )
767
794
768
795
tcpAddrMe := & net.TCPAddr {IP : nil , Port : 0 }
769
796
me := wire .NewNetAddress (tcpAddrMe , wire .SFNodeNetwork )
@@ -786,7 +813,7 @@ func (p *Peer) versionMessage(address string) *wire.MsgVersion {
786
813
p .logger .Error ("RandomUint64: failed to generate nonce" , slog .String (errKey , err .Error ()))
787
814
}
788
815
789
- msg := wire .NewMsgVersion (me , you , nonce , lastBlock )
816
+ msg := wire .NewMsgVersion (me , you , nonce , int32 ( p . lastBlock ) )
790
817
791
818
if p .userAgentName != nil && p .userAgentVersion != nil {
792
819
err = msg .AddUserAgent (* p .userAgentName , * p .userAgentVersion )
0 commit comments