From 53fc2819b99de0a5a4603d7ed29cce0ffb9ebb76 Mon Sep 17 00:00:00 2001 From: chao Date: Mon, 9 Sep 2019 20:09:36 -0700 Subject: [PATCH] close connection after pushnewblock error --- api/service/syncing/downloader/client.go | 4 ++-- node/node_syncing.go | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/api/service/syncing/downloader/client.go b/api/service/syncing/downloader/client.go index 9fe6dff4a..cdbaba633 100644 --- a/api/service/syncing/downloader/client.go +++ b/api/service/syncing/downloader/client.go @@ -89,7 +89,7 @@ func (client *Client) Register(hash []byte, ip, port string) *pb.DownloaderRespo } // PushNewBlock will send the lastest verified block to registered nodes -func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, timeout bool) *pb.DownloaderResponse { +func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, timeout bool) (*pb.DownloaderResponse, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -107,7 +107,7 @@ func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, time if err != nil { utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Msg("[SYNC] unable to send new block to unsync node") } - return response + return response, err } // GetBlockChainHeight gets the blockheight from peer diff --git a/node/node_syncing.go b/node/node_syncing.go index e9a8d59c7..4a452e1bd 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -296,7 +296,12 @@ func (node *Node) SendNewBlockToUnsync() { delete(node.peerRegistrationRecord, peerID) continue } - response := config.client.PushNewBlock(node.GetSyncID(), blockHash, false) + response, err := config.client.PushNewBlock(node.GetSyncID(), blockHash, false) + // close the connection if cannot push new block to unsync node + if err != nil { + node.peerRegistrationRecord[peerID].client.Close() + delete(node.peerRegistrationRecord, peerID) + } if response != nil && response.Type == downloader_pb.DownloaderResponse_INSYNC { node.peerRegistrationRecord[peerID].client.Close() delete(node.peerRegistrationRecord, peerID)