forked from TrueCloudLab/neoneo-go
Merge pull request #1326 from nspcc-dev/fix-block-synchronization
Fix block synchronization
This commit is contained in:
commit
58af143f25
4 changed files with 27 additions and 1 deletions
|
@ -286,6 +286,11 @@ func (p *localPeer) SendPing(m *Message) error {
|
|||
_ = p.EnqueueMessage(m)
|
||||
return nil
|
||||
}
|
||||
func (p *localPeer) HandlePing(ping *payload.Ping) error {
|
||||
p.lastBlockIndex = ping.LastBlockIndex
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *localPeer) HandlePong(pong *payload.Ping) error {
|
||||
p.lastBlockIndex = pong.LastBlockIndex
|
||||
p.pingSent--
|
||||
|
|
|
@ -67,6 +67,9 @@ type Peer interface {
|
|||
HandleVersion(*payload.Version) error
|
||||
HandleVersionAck() error
|
||||
|
||||
// HandlePing checks ping contents against Peer's state and updates it.
|
||||
HandlePing(ping *payload.Ping) error
|
||||
|
||||
// HandlePong checks pong contents against Peer's state and updates it.
|
||||
HandlePong(pong *payload.Ping) error
|
||||
}
|
||||
|
|
|
@ -453,6 +453,16 @@ func (s *Server) handleBlockCmd(p Peer, block *block.Block) error {
|
|||
|
||||
// handlePing processes ping request.
|
||||
func (s *Server) handlePing(p Peer, ping *payload.Ping) error {
|
||||
err := p.HandlePing(ping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.chain.BlockHeight() < ping.LastBlockIndex {
|
||||
err = s.requestBlocks(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return p.EnqueueP2PMessage(NewMessage(CMDPong, payload.NewPing(s.chain.BlockHeight(), s.id)))
|
||||
}
|
||||
|
||||
|
@ -694,7 +704,7 @@ func (s *Server) handleGetAddrCmd(p Peer) error {
|
|||
// to sync up in blocks. A maximum of maxBlockBatch will
|
||||
// send at once.
|
||||
func (s *Server) requestBlocks(p Peer) error {
|
||||
payload := payload.NewGetBlockByIndex(s.chain.BlockHeight(), -1)
|
||||
payload := payload.NewGetBlockByIndex(s.chain.BlockHeight()+1, -1)
|
||||
return p.EnqueueP2PMessage(NewMessage(CMDGetBlockByIndex, payload))
|
||||
}
|
||||
|
||||
|
|
|
@ -431,6 +431,14 @@ func (p *TCPPeer) SendPing(msg *Message) error {
|
|||
return p.EnqueueMessage(msg)
|
||||
}
|
||||
|
||||
// HandlePing handles a ping message received from the peer.
|
||||
func (p *TCPPeer) HandlePing(ping *payload.Ping) error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.lastBlockIndex = ping.LastBlockIndex
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandlePong handles a pong message received from the peer and does appropriate
|
||||
// accounting of outstanding pings and timeouts.
|
||||
func (p *TCPPeer) HandlePong(pong *payload.Ping) error {
|
||||
|
|
Loading…
Reference in a new issue