errGetBlockByHash = errors.New("Get block by hash error")
errBroadcastStatus = errors.New("Broadcast new status block error")
errReqBlock = errors.New("Request block error")
+ errPeerNotRegister = errors.New("peer is not registered")
)
//TODO: add retry mechanism
orphanNum := uint64(0)
reqNum := uint64(0)
isOrphan := false
- peer := bk.peers.Peer(peerID)
- for num <= maxPeerHeight && num > 0 {
+ bkPeer, ok := bk.peers.Peer(peerID)
+ if !ok {
+ log.Info("peer is not registered")
+ return errPeerNotRegister
+ }
+ swPeer := bkPeer.getPeer()
+ for 0 < num && num <= maxPeerHeight {
if isOrphan {
reqNum = orphanNum
} else {
block, err := bk.BlockRequest(peerID, reqNum)
if errors.Root(err) == errPeerDropped || errors.Root(err) == errGetBlockTimeout || errors.Root(err) == errReqBlock {
log.WithField("Peer abnormality. PeerID: ", peerID).Info(err)
- if peer == nil {
+ if bkPeer == nil {
log.Info("peer is not registered")
break
}
- log.Info("Peer communication abnormality")
- if ban := peer.addBanScore(0, 50, "block request error"); ban {
- bk.sw.AddBannedPeer(peer.getPeer())
- }
- bk.sw.StopPeerGracefully(peer.getPeer())
+ log.Info("Block keeper request block error. Stop peer.")
+ bk.sw.StopPeerGracefully(swPeer)
break
}
isOrphan, err = bk.chain.ProcessBlock(block)
if err != nil {
- if ban := peer.addBanScore(50, 0, "block process error"); ban {
- bk.sw.AddBannedPeer(peer.getPeer())
- bk.sw.StopPeerGracefully(peer.getPeer())
+ if bkPeer == nil {
+ log.Info("peer is deleted")
+ break
+ }
+ if ban := bkPeer.addBanScore(20, 0, "block process error"); ban {
+ bk.sw.AddBannedPeer(swPeer)
+ bk.sw.StopPeerGracefully(swPeer)
}
log.WithField("hash:", block.Hash()).Errorf("blockKeeper fail process block %v ", err)
break
return errBroadcastStatus
}
for _, peer := range peers {
- if ban := peer.addBanScore(0, 50, "Broadcast block error"); ban {
- peer := bk.peers.Peer(peer.id).getPeer()
- bk.sw.AddBannedPeer(peer)
- bk.sw.StopPeerGracefully(peer)
+ if peer == nil {
+ return errPeerNotRegister
}
+ swPeer := peer.getPeer()
+ log.Info("Block keeper broadcast block error. Stop peer.")
+ bk.sw.StopPeerGracefully(swPeer)
}
}
return nil
func (bk *blockKeeper) txsProcessWorker() {
for txsResponse := range bk.txsProcessCh {
tx := txsResponse.tx
- peer:=bk.peers.Peer(txsResponse.peerID)
log.Info("Receive new tx from remote peer. TxID:", tx.ID.String())
bk.peers.MarkTransaction(txsResponse.peerID, &tx.ID)
if isOrphan, err := bk.chain.ValidateTx(tx); err != nil && isOrphan == false {
- if ban := peer.addBanScore(50, 0, "tx error"); ban {
- bk.sw.AddBannedPeer(peer.getPeer())
- bk.sw.StopPeerGracefully(peer.getPeer())
+ if bkPeer, ok := bk.peers.Peer(txsResponse.peerID); ok {
+ swPeer := bkPeer.getPeer()
+ if ban := bkPeer.addBanScore(10, 0, "tx error"); ban {
+ bk.sw.AddBannedPeer(swPeer)
+ bk.sw.StopPeerGracefully(swPeer)
+ }
}
}
}