Sitelet https://github.com/DAD-Chain/dad-go/commit/288251bdc6b621bd8fbf0196925aa2656b14853d
Skip to content

Commit 288251b

Browse files
ZhangBiFenZhangBiFen
authored andcommitted
ONT-546 refactor error format in p2p module (#178)
1 parent 0f0ed46 commit 288251b

35 files changed

Lines changed: 661 additions & 410 deletions

‎errors/errcode.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const (
4747
ErrNoAccount ErrCode = 45014
4848
ErrRetryExhausted ErrCode = 45015
4949
ErrTxPoolFull ErrCode = 45016
50+
ErrNetPackFail ErrCode = 45017
51+
ErrNetUnPackFail ErrCode = 45018
52+
ErrNetVerifyFail ErrCode = 45019
5053
)
5154

5255
func (err ErrCode) Error() string {
@@ -85,6 +88,12 @@ func (err ErrCode) Error() string {
8588
return "retry exhausted"
8689
case ErrTxPoolFull:
8790
return "tx pool full"
91+
case ErrNetPackFail:
92+
return "net msg pack fail"
93+
case ErrNetUnPackFail:
94+
return "net msg unpack fail"
95+
case ErrNetVerifyFail:
96+
return "net msg verify fail"
8897
}
8998

9099
return fmt.Sprintf("Unknown error? Error code = %d", err)

‎main.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ func initP2PNode(ctx *cli.Context, acc *account.Account, txpoolSvr *proc.TXPoolS
254254
if config.DefConfig.Genesis.ConsensusType == config.CONSENSUS_TYPE_SOLO {
255255
return nil, nil, nil
256256
}
257-
p2p, err := p2pserver.NewServer(acc)
258-
if err != nil {
259-
return nil, nil, fmt.Errorf("P2P node NewServer error:%s", err)
260-
}
257+
p2p := p2pserver.NewServer(acc)
258+
261259
p2pActor := p2pactor.NewP2PActor(p2p)
262260
p2pPID, err := p2pActor.Start()
263261
if err != nil {

‎p2pserver/actor/req/consensus.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func SetConsensusPid(conPid *actor.PID) {
3131
ConsensusPid = conPid
3232
}
3333

34-
func NotifyPeerState(peer keypair.PublicKey, connected bool) error {
34+
func NotifyPeerState(peer keypair.PublicKey, connected bool) {
3535
if config.DefConfig.Genesis.ConsensusType != config.CONSENSUS_TYPE_VBFT {
36-
return nil
36+
return
3737
}
3838

3939
if ConsensusPid != nil {
@@ -42,5 +42,4 @@ func NotifyPeerState(peer keypair.PublicKey, connected bool) error {
4242
Connected: connected,
4343
})
4444
}
45-
return nil
4645
}

‎p2pserver/actor/req/txnpool.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func SetTxnPoolPid(txnPid *actor.PID) {
4141
//add txn to txnpool
4242
func AddTransaction(transaction *types.Transaction) {
4343
if txnPoolPid == nil {
44-
log.Error("p2p tx pool pid is nil")
44+
log.Error("net_server AddTransaction(): txnpool pid is nil")
4545
return
4646
}
4747
txReq := &tc.TxReq{
@@ -54,13 +54,13 @@ func AddTransaction(transaction *types.Transaction) {
5454
//get txn according to hash
5555
func GetTransaction(hash common.Uint256) (*types.Transaction, error) {
5656
if txnPoolPid == nil {
57-
log.Error("p2p tx pool pid is nil")
58-
return nil, errors.NewErr("p2p tx pool pid is nil")
57+
log.Error("net_server tx pool pid is nil")
58+
return nil, errors.NewErr("net_server tx pool pid is nil")
5959
}
6060
future := txnPoolPid.RequestFuture(&tc.GetTxnReq{Hash: hash}, txnPoolReqTimeout)
6161
result, err := future.Result()
6262
if err != nil {
63-
log.Error(errors.NewErr("net_server GetTransaction ERROR: "), err)
63+
log.Errorf("net_server GetTransaction error: %v\n", err)
6464
return nil, err
6565
}
6666
return result.(tc.GetTxnRsp).Txn, nil

‎p2pserver/actor/server/actor.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,16 @@ func (this *P2PActor) Receive(ctx actor.Context) {
9898
default:
9999
err := this.server.Xmit(ctx.Message())
100100
if nil != err {
101-
log.Error("Error Xmit message ", err.Error(), reflect.TypeOf(ctx.Message()))
101+
log.Error("error xmit message ", err.Error(), reflect.TypeOf(ctx.Message()))
102102
}
103103
}
104104
}
105105

106106
//stop handler
107107
func (this *P2PActor) handleStopServerReq(ctx actor.Context, req *StopServerReq) {
108-
err := this.server.Stop()
108+
this.server.Stop()
109109
if ctx.Sender() != nil {
110-
resp := &StopServerRsp{
111-
Error: err,
112-
}
110+
resp := &StopServerRsp{}
113111
ctx.Sender().Request(resp, ctx.Self())
114112
}
115113
}

‎p2pserver/actor/server/common.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type StopServerReq struct {
2929

3030
//response of stop request
3131
type StopServerRsp struct {
32-
Error error
3332
}
3433

3534
//version request

‎p2pserver/block_sync.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (this *BlockSyncMgr) checkTimeout() {
187187
}
188188
flightInfo.ResetStartTime()
189189
flightInfo.MarkFailedNode()
190-
log.Infof("BlockSyncMgr checkTimeout sync headers:%d timeout after:%d s Times:%d", height, SYNC_HEADER_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes())
190+
log.Infof("checkTimeout sync headers:%d timeout after:%d s Times:%d", height, SYNC_HEADER_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes())
191191
reqNode := this.getNodeWithMinFailedTimes(flightInfo, curBlockHeight)
192192
if reqNode == nil {
193193
break
@@ -197,7 +197,7 @@ func (this *BlockSyncMgr) checkTimeout() {
197197
headerHash := this.ledger.GetCurrentHeaderHash()
198198
buf, err := msgpack.NewHeadersReq(headerHash)
199199
if err != nil {
200-
log.Error("failed build a new headersReq")
200+
log.Error("checkTimeout failed build a new headersReq")
201201
} else {
202202
this.server.Send(reqNode, buf, false)
203203
}
@@ -209,7 +209,7 @@ func (this *BlockSyncMgr) checkTimeout() {
209209
}
210210
flightInfo.ResetStartTime()
211211
flightInfo.MarkFailedNode()
212-
log.Infof("BlockSyncMgr checkTimeout sync height:%d block:%x timeout after:%d s times:%d", flightInfo.Height, blockHash, SYNC_BLOCK_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes())
212+
log.Infof("checkTimeout sync height:%d block:0x%x timeout after:%d s times:%d", flightInfo.Height, blockHash, SYNC_BLOCK_REQUEST_TIMEOUT, flightInfo.GetTotalFailedTimes())
213213
reqNode := this.getNodeWithMinFailedTimes(flightInfo, curBlockHeight)
214214
if reqNode == nil {
215215
break
@@ -218,12 +218,12 @@ func (this *BlockSyncMgr) checkTimeout() {
218218

219219
reqBuf, err := msgpack.NewBlkDataReq(blockHash)
220220
if err != nil {
221-
log.Error("syncBlock error:", err)
221+
log.Error("checkTimeout NewBlkDataReq error:", err)
222222
}
223223
err = this.server.Send(reqNode, reqBuf, false)
224224

225225
if err != nil {
226-
log.Errorf("BlockSyncMgr checkTimeout Height:%d Hash:%x ReqBlkData error:%s", flightInfo.Height, blockHash, err)
226+
log.Errorf("checkTimeout reqNode ID:0x%x Send error:%s", reqNode.GetID(), err)
227227
continue
228228
}
229229
}
@@ -263,12 +263,12 @@ func (this *BlockSyncMgr) syncHeader() {
263263
headerHash := this.ledger.GetCurrentHeaderHash()
264264
buf, err := msgpack.NewHeadersReq(headerHash)
265265
if err != nil {
266-
log.Error("failed build a new headersReq")
266+
log.Error("syncHeader failed build a new headersReq")
267267
} else {
268268
this.server.Send(reqNode, buf, false)
269269
}
270270

271-
log.Infof("SendMsgSyncHeaders Request Height:%d", NextHeaderId)
271+
log.Infof("syncHeader request Height:%d", NextHeaderId)
272272
}
273273

274274
func (this *BlockSyncMgr) syncBlock() {
@@ -324,7 +324,7 @@ func (this *BlockSyncMgr) syncBlock() {
324324
}
325325
err = this.server.Send(reqNode, reqBuf, false)
326326
if err != nil {
327-
log.Errorf("BlockSyncMgr syncBlock Height:%d ReqBlkData error:%s", nextBlockHeight, err)
327+
log.Errorf("syncBlock Height:%d ReqBlkData error:%s", nextBlockHeight, err)
328328
return
329329
}
330330
counter++
@@ -350,7 +350,7 @@ func (this *BlockSyncMgr) OnHeaderReceive(headers []*types.Header) {
350350
err := this.ledger.AddHeaders(headers)
351351
this.delFlightHeader(height)
352352
if err != nil {
353-
log.Errorf("BlockSyncMgr AddHeaders error:%s", err)
353+
log.Errorf("OnHeaderReceive AddHeaders error:%s", err)
354354
return
355355
}
356356
this.syncHeader()
@@ -379,7 +379,7 @@ func (this *BlockSyncMgr) OnBlockReceive(block *types.Block) {
379379

380380
//OnAddNode to node list when a new node added
381381
func (this *BlockSyncMgr) OnAddNode(nodeId uint64) {
382-
log.Infof("BlockSyncMgr OnAddNode:%d", nodeId)
382+
log.Infof("OnAddNode:%d", nodeId)
383383
this.lock.Lock()
384384
defer this.lock.Unlock()
385385
this.nodeList = append(this.nodeList, nodeId)
@@ -400,7 +400,7 @@ func (this *BlockSyncMgr) OnDelNode(nodeId uint64) {
400400
return
401401
}
402402
this.nodeList = append(this.nodeList[:index], this.nodeList[index+1:]...)
403-
log.Infof("BlockSyncMgr OnDelNode:%d", nodeId)
403+
log.Infof("OnDelNode:%d", nodeId)
404404
}
405405

406406
func (this *BlockSyncMgr) tryGetSyncHeaderLock() bool {
@@ -496,7 +496,7 @@ func (this *BlockSyncMgr) saveBlock() {
496496
err := this.ledger.AddBlock(nextBlock)
497497
this.delBlockCache(nextBlockHeight)
498498
if err != nil {
499-
log.Warnf("BlockSyncMgr saveBlock Height:%d AddBlock error:%s", nextBlockHeight, err)
499+
log.Warnf("saveBlock Height:%d AddBlock error:%s", nextBlockHeight, err)
500500
reqNode := this.getNextNode(nextBlockHeight)
501501
if reqNode == nil {
502502
return
@@ -509,7 +509,7 @@ func (this *BlockSyncMgr) saveBlock() {
509509
}
510510
err = this.server.Send(reqNode, reqBuf, false)
511511
if err != nil {
512-
log.Errorf("BlockSyncMgr saveBlock Height:%d ReqBlkData error:%s", nextBlockHeight, err)
512+
log.Errorf("saveBlock Height:%d ReqBlkData error:%s", nextBlockHeight, err)
513513
}
514514
return
515515
}

‎p2pserver/common/p2p_common.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ const (
2828
SERVICE_NODE = 2 //peer only sync with consensus peer
2929
)
3030

31-
//config const
32-
const (
33-
VERIFY_NODE_NAME = "verify" //verify mode name in config.json
34-
SERVICE_NODE_NAME = "service" //service mode name in config.json
35-
)
36-
3731
//msg cmd const
3832
const (
3933
MSG_CMD_LEN = 12 //msg type length in byte
@@ -58,8 +52,8 @@ const (
5852
CONN_MONITOR = 6 //time to retry connect in sec
5953
CONN_MAX_BACK = 4000 //max backoff time in micro sec
6054
MAX_RETRY_COUNT = 3 //max reconnect time of remote peer
61-
CHAN_CAPABILITY = 10000 //chhannel capability of recv link
62-
SYNC_BLK_WAIT = 2 //time interval for blk sync check
55+
CHAN_CAPABILITY = 10000 //channel capability of recv link
56+
SYNC_BLK_WAIT = 2 //timespan for blk sync check
6357
)
6458

6559
// The peer state

‎p2pserver/link/link.go‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ func (this *Link) SetConn(conn net.Conn) {
104104
this.conn = conn
105105
}
106106

107-
//record latest getting message time
107+
//record latest message time
108108
func (this *Link) UpdateRXTime(t time.Time) {
109109
this.time = t
110110
}
111111

112-
//UpdateRXTime return the latest message time
112+
//GetRXTime return the latest message time
113113
func (this *Link) GetRXTime() time.Time {
114114
return this.time
115115
}
@@ -139,8 +139,7 @@ func unpackNodeBuf(this *Link, buf []byte) {
139139
if types.ValidMsgHdr(rxBuf.p) == false {
140140
rxBuf.p = nil
141141
rxBuf.len = 0
142-
log.Warn("Get error message header, TODO: relocate the msg header")
143-
// TODO Relocate the message header
142+
log.Warn("Get error message header")
144143
return
145144
}
146145

@@ -167,7 +166,7 @@ func unpackNodeBuf(this *Link, buf []byte) {
167166
}
168167
}
169168

170-
//pushdata send packed data to channel
169+
//pushdata send package data to channel
171170
func (this *Link) pushdata(buf []byte) {
172171
p2pMsg := &common.MsgPayload{
173172
Id: this.id,
@@ -190,10 +189,9 @@ func (this *Link) Rx() {
190189
this.UpdateRXTime(t)
191190
unpackNodeBuf(this, buf[0:len])
192191
case io.EOF:
193-
//log.Error("Rx io.EOF: ", err, ", node id is ", node.GetID())
194192
goto DISCONNECT
195193
default:
196-
log.Error("Read connection error ", err)
194+
log.Error("read connection error ", err)
197195
goto DISCONNECT
198196
}
199197
}
@@ -238,7 +236,7 @@ func (this *Link) Tx(buf []byte) error {
238236
}
239237
_, err := this.conn.Write(buf)
240238
if err != nil {
241-
log.Error("Error sending messge to peer node ", err.Error())
239+
log.Error("error sending messge to peer node ", err.Error())
242240
this.disconnectNotify()
243241
return err
244242
}

0 commit comments

Comments
 (0)