Skip to content

Commit fb90e7b

Browse files
authored
fix return type to use blocker (#742)
* fix return type to use blocker * add more blocker type
1 parent d576806 commit fb90e7b

File tree

2 files changed

+56
-55
lines changed

2 files changed

+56
-55
lines changed

common/blocker/blocker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type (
1616
)
1717

1818
var (
19+
ServiceErr TypeBlocker = "ServiceErr"
20+
InternalErr TypeBlocker = "InternalErr"
1921
DBErr TypeBlocker = "DBErr"
2022
DBRowNotFound TypeBlocker = "DBRowNotFound"
2123
BlockErr TypeBlocker = "BlockErr"

p2p/service/p2pServerService.go

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"context"
55

6+
"github.com/zoobc/zoobc-core/common/blocker"
67
"github.com/zoobc/zoobc-core/common/chaintype"
78
"github.com/zoobc/zoobc-core/common/constant"
89
"github.com/zoobc/zoobc-core/common/model"
@@ -11,9 +12,7 @@ import (
1112
"github.com/zoobc/zoobc-core/observer"
1213
"github.com/zoobc/zoobc-core/p2p/strategy"
1314
p2pUtil "github.com/zoobc/zoobc-core/p2p/util"
14-
"google.golang.org/grpc/codes"
1515
"google.golang.org/grpc/metadata"
16-
"google.golang.org/grpc/status"
1716
)
1817

1918
type (
@@ -110,7 +109,7 @@ func (ps *P2PServerService) GetPeerInfo(ctx context.Context, req *model.GetPeerI
110109
HostInfo: ps.PeerExplorer.GetHostInfo(),
111110
}, nil
112111
}
113-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
112+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
114113
}
115114

116115
// GetMorePeers contains info other peers
@@ -123,7 +122,7 @@ func (ps *P2PServerService) GetMorePeers(ctx context.Context, req *model.Empty)
123122
}
124123
return nodes, nil
125124
}
126-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
125+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
127126
}
128127

129128
// SendPeers receives set of peers info from other node and put them into the unresolved peers
@@ -135,11 +134,11 @@ func (ps *P2PServerService) SendPeers(
135134
// TODO: only accept nodes that are already registered in the node registration
136135
err := ps.PeerExplorer.AddToUnresolvedPeers(peers, true)
137136
if err != nil {
138-
return nil, status.Error(codes.Internal, err.Error())
137+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
139138
}
140139
return &model.Empty{}, nil
141140
}
142-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
141+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
143142
}
144143

145144
// GetCumulativeDifficulty responds to the request of the cumulative difficulty status of a node
@@ -150,21 +149,21 @@ func (ps *P2PServerService) GetCumulativeDifficulty(
150149
if ps.PeerExplorer.ValidateRequest(ctx) {
151150
blockService := ps.BlockServices[chainType.GetTypeInt()]
152151
if blockService == nil {
153-
return nil, status.Error(
154-
codes.InvalidArgument,
152+
return nil, blocker.NewBlocker(
153+
blocker.ValidationErr,
155154
"blockServiceNotFoundByThisChainType",
156155
)
157156
}
158157
lastBlock, err := blockService.GetLastBlock()
159158
if err != nil {
160-
return nil, status.Error(codes.Internal, err.Error())
159+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
161160
}
162161
return &model.GetCumulativeDifficultyResponse{
163162
CumulativeDifficulty: lastBlock.CumulativeDifficulty,
164163
Height: lastBlock.Height,
165164
}, nil
166165
}
167-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
166+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
168167
}
169168

170169
func (ps P2PServerService) GetCommonMilestoneBlockIDs(
@@ -182,17 +181,17 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
182181
blockService = ps.BlockServices[chainType.GetTypeInt()]
183182
)
184183
if blockService == nil {
185-
return nil, status.Error(
186-
codes.InvalidArgument,
184+
return nil, blocker.NewBlocker(
185+
blocker.ValidationErr,
187186
"blockServiceNotFoundByThisChainType",
188187
)
189188
}
190189
if lastBlockID == 0 && lastMilestoneBlockID == 0 {
191-
return nil, status.Error(codes.InvalidArgument, "either LastBlockID or LastMilestoneBlockID has to be supplied")
190+
return nil, blocker.NewBlocker(blocker.ValidationErr, "either LastBlockID or LastMilestoneBlockID has to be supplied")
192191
}
193192
myLastBlock, err := blockService.GetLastBlock()
194193
if err != nil || myLastBlock == nil {
195-
return nil, status.Error(codes.Internal, "failedGetLastBlock")
194+
return nil, blocker.NewBlocker(blocker.InternalErr, "failedGetLastBlock")
196195
}
197196
myLastBlockID := myLastBlock.ID
198197
myBlockchainHeight := myLastBlock.Height
@@ -213,7 +212,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
213212
lastMilestoneBlock, err := blockService.GetBlockByID(lastMilestoneBlockID, false)
214213
// this error is handled because when lastMilestoneBlockID is provided, it was expected to be the one returned from this node
215214
if err != nil {
216-
return nil, status.Error(codes.Internal, err.Error())
215+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
217216
}
218217
height = lastMilestoneBlock.GetHeight()
219218
jump = util.MinUint32(constant.SafeBlockGap, util.MaxUint32(myBlockchainHeight, 1))
@@ -227,7 +226,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
227226
for ; limit > 0; limit-- {
228227
block, err := blockService.GetBlockByHeight(height)
229228
if err != nil {
230-
return nil, status.Error(codes.Internal, err.Error())
229+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
231230
}
232231
blockIds = append(blockIds, block.ID)
233232
switch {
@@ -242,7 +241,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
242241

243242
return &model.GetCommonMilestoneBlockIdsResponse{BlockIds: blockIds}, nil
244243
}
245-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
244+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
246245
}
247246

248247
func (ps *P2PServerService) GetNextBlockIDs(
@@ -257,8 +256,8 @@ func (ps *P2PServerService) GetNextBlockIDs(
257256
blockService = ps.BlockServices[chainType.GetTypeInt()]
258257
)
259258
if blockService == nil {
260-
return nil, status.Error(
261-
codes.InvalidArgument,
259+
return nil, blocker.NewBlocker(
260+
blocker.ValidationErr,
262261
"blockServiceNotFoundByThisChainType",
263262
)
264263
}
@@ -269,19 +268,19 @@ func (ps *P2PServerService) GetNextBlockIDs(
269268

270269
foundBlock, err := blockService.GetBlockByID(reqBlockID, false)
271270
if err != nil {
272-
return nil, status.Error(codes.Internal, err.Error())
271+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
273272
}
274273
blocks, err := blockService.GetBlocksFromHeight(foundBlock.Height, limit, false)
275274
if err != nil {
276-
return nil, status.Error(codes.Internal, "failedGetBlocks")
275+
return nil, blocker.NewBlocker(blocker.InternalErr, "failedGetBlocks")
277276
}
278277
for _, block := range blocks {
279278
blockIds = append(blockIds, block.ID)
280279
}
281280

282281
return blockIds, nil
283282
}
284-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
283+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
285284
}
286285

287286
func (ps *P2PServerService) GetNextBlocks(
@@ -297,20 +296,20 @@ func (ps *P2PServerService) GetNextBlocks(
297296
blockService = ps.BlockServices[chainType.GetTypeInt()]
298297
)
299298
if blockService == nil {
300-
return nil, status.Error(
301-
codes.InvalidArgument,
299+
return nil, blocker.NewBlocker(
300+
blocker.ValidationErr,
302301
"blockServiceNotFoundByThisChainType",
303302
)
304303
}
305304
blockService.ChainWriteLock(constant.BlockchainSendingBlocks)
306305
defer blockService.ChainWriteUnlock(constant.BlockchainSendingBlocks)
307306
block, err := blockService.GetBlockByID(blockID, false)
308307
if err != nil {
309-
return nil, status.Error(codes.InvalidArgument, err.Error())
308+
return nil, blocker.NewBlocker(blocker.ValidationErr, err.Error())
310309
}
311310
blocks, err := blockService.GetBlocksFromHeight(block.Height, uint32(len(blockIDList)), true)
312311
if err != nil {
313-
return nil, status.Error(codes.InvalidArgument, err.Error())
312+
return nil, blocker.NewBlocker(blocker.ValidationErr, err.Error())
314313
}
315314

316315
for idx, block := range blocks {
@@ -319,13 +318,13 @@ func (ps *P2PServerService) GetNextBlocks(
319318
}
320319
err = blockService.PopulateBlockData(block)
321320
if err != nil {
322-
return nil, status.Error(codes.Internal, err.Error())
321+
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
323322
}
324323
blocksMessage = append(blocksMessage, block)
325324
}
326325
return &model.BlocksData{NextBlocks: blocksMessage}, nil
327326
}
328-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
327+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
329328
}
330329

331330
// SendBlock receive block from other node
@@ -338,8 +337,8 @@ func (ps *P2PServerService) SendBlock(
338337
if ps.PeerExplorer.ValidateRequest(ctx) {
339338
var md, _ = metadata.FromIncomingContext(ctx)
340339
if len(md) == 0 {
341-
return nil, status.Error(
342-
codes.InvalidArgument,
340+
return nil, blocker.NewBlocker(
341+
blocker.ValidationErr,
343342
"InvalidContext",
344343
)
345344
}
@@ -348,19 +347,19 @@ func (ps *P2PServerService) SendBlock(
348347
peer, err = p2pUtil.ParsePeer(fullAddress)
349348
)
350349
if err != nil {
351-
return nil, status.Error(codes.InvalidArgument, "invalidPeer")
350+
return nil, blocker.NewBlocker(blocker.ValidationErr, "invalidPeer")
352351
}
353352
blockService := ps.BlockServices[chainType.GetTypeInt()]
354353
if blockService == nil {
355-
return nil, status.Error(
356-
codes.InvalidArgument,
354+
return nil, blocker.NewBlocker(
355+
blocker.ValidationErr,
357356
"blockServiceNotFoundByThisChainType",
358357
)
359358
}
360359
lastBlock, err := blockService.GetLastBlock()
361360
if err != nil {
362-
return nil, status.Error(
363-
codes.Internal,
361+
return nil, blocker.NewBlocker(
362+
blocker.InternalErr,
364363
"failGetLastBlock",
365364
)
366365
}
@@ -378,7 +377,7 @@ func (ps *P2PServerService) SendBlock(
378377
BatchReceipt: batchReceipt,
379378
}, nil
380379
}
381-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
380+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
382381
}
383382

384383
// SendTransaction receive transaction from other node and calling TransactionReceived Event
@@ -391,22 +390,22 @@ func (ps *P2PServerService) SendTransaction(
391390
if ps.PeerExplorer.ValidateRequest(ctx) {
392391
var blockService = ps.BlockServices[chainType.GetTypeInt()]
393392
if blockService == nil {
394-
return nil, status.Error(
395-
codes.InvalidArgument,
393+
return nil, blocker.NewBlocker(
394+
blocker.ValidationErr,
396395
"blockServiceNotFoundByThisChainType",
397396
)
398397
}
399398
lastBlock, err := blockService.GetLastBlock()
400399
if err != nil {
401-
return nil, status.Error(
402-
codes.Internal,
400+
return nil, blocker.NewBlocker(
401+
blocker.InternalErr,
403402
"failGetLastBlock",
404403
)
405404
}
406405
var mempoolService = ps.MempoolServices[chainType.GetTypeInt()]
407406
if mempoolService == nil {
408-
return nil, status.Error(
409-
codes.InvalidArgument,
407+
return nil, blocker.NewBlocker(
408+
blocker.ValidationErr,
410409
"mempoolServiceNotFoundByThisChainType",
411410
)
412411
}
@@ -423,7 +422,7 @@ func (ps *P2PServerService) SendTransaction(
423422
BatchReceipt: batchReceipt,
424423
}, nil
425424
}
426-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
425+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
427426
}
428427

429428
// SendBlockTransactions receive a list of transaction from other node and calling TransactionReceived Event
@@ -436,22 +435,22 @@ func (ps *P2PServerService) SendBlockTransactions(
436435
if ps.PeerExplorer.ValidateRequest(ctx) {
437436
var blockService = ps.BlockServices[chainType.GetTypeInt()]
438437
if blockService == nil {
439-
return nil, status.Error(
440-
codes.InvalidArgument,
438+
return nil, blocker.NewBlocker(
439+
blocker.ValidationErr,
441440
"blockServiceNotFoundByThisChainType",
442441
)
443442
}
444443
lastBlock, err := blockService.GetLastBlock()
445444
if err != nil {
446-
return nil, status.Error(
447-
codes.Internal,
445+
return nil, blocker.NewBlocker(
446+
blocker.InternalErr,
448447
"failGetLastBlock",
449448
)
450449
}
451450
var mempoolService = ps.MempoolServices[chainType.GetTypeInt()]
452451
if mempoolService == nil {
453-
return nil, status.Error(
454-
codes.InvalidArgument,
452+
return nil, blocker.NewBlocker(
453+
blocker.ValidationErr,
455454
"mempoolServiceNotFoundByThisChainType",
456455
)
457456
}
@@ -468,7 +467,7 @@ func (ps *P2PServerService) SendBlockTransactions(
468467
BatchReceipts: batchReceipts,
469468
}, nil
470469
}
471-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
470+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
472471
}
473472

474473
func (ps *P2PServerService) RequestBlockTransactions(
@@ -480,8 +479,8 @@ func (ps *P2PServerService) RequestBlockTransactions(
480479
if ps.PeerExplorer.ValidateRequest(ctx) {
481480
var md, _ = metadata.FromIncomingContext(ctx)
482481
if len(md) == 0 {
483-
return nil, status.Error(
484-
codes.InvalidArgument,
482+
return nil, blocker.NewBlocker(
483+
blocker.ValidationErr,
485484
"invalidContext",
486485
)
487486
}
@@ -490,12 +489,12 @@ func (ps *P2PServerService) RequestBlockTransactions(
490489
peer, err = p2pUtil.ParsePeer(fullAddress)
491490
)
492491
if err != nil {
493-
return nil, status.Error(codes.InvalidArgument, "invalidPeer")
492+
return nil, blocker.NewBlocker(blocker.ValidationErr, "invalidPeer")
494493
}
495494
ps.Observer.Notify(observer.BlockTransactionsRequested, transactionsIDs, chainType, blockID, peer)
496495
return &model.Empty{}, nil
497496
}
498-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
497+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
499498
}
500499

501500
func (ps *P2PServerService) RequestDownloadFile(
@@ -521,5 +520,5 @@ func (ps *P2PServerService) RequestDownloadFile(
521520
}
522521
return res, nil
523522
}
524-
return nil, status.Error(codes.Unauthenticated, "Rejected request")
523+
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
525524
}

0 commit comments

Comments
 (0)