@@ -3,6 +3,7 @@ package service
3
3
import (
4
4
"context"
5
5
6
+ "github.com/zoobc/zoobc-core/common/blocker"
6
7
"github.com/zoobc/zoobc-core/common/chaintype"
7
8
"github.com/zoobc/zoobc-core/common/constant"
8
9
"github.com/zoobc/zoobc-core/common/model"
@@ -11,9 +12,7 @@ import (
11
12
"github.com/zoobc/zoobc-core/observer"
12
13
"github.com/zoobc/zoobc-core/p2p/strategy"
13
14
p2pUtil "github.com/zoobc/zoobc-core/p2p/util"
14
- "google.golang.org/grpc/codes"
15
15
"google.golang.org/grpc/metadata"
16
- "google.golang.org/grpc/status"
17
16
)
18
17
19
18
type (
@@ -110,7 +109,7 @@ func (ps *P2PServerService) GetPeerInfo(ctx context.Context, req *model.GetPeerI
110
109
HostInfo : ps .PeerExplorer .GetHostInfo (),
111
110
}, nil
112
111
}
113
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
112
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
114
113
}
115
114
116
115
// GetMorePeers contains info other peers
@@ -123,7 +122,7 @@ func (ps *P2PServerService) GetMorePeers(ctx context.Context, req *model.Empty)
123
122
}
124
123
return nodes , nil
125
124
}
126
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
125
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
127
126
}
128
127
129
128
// SendPeers receives set of peers info from other node and put them into the unresolved peers
@@ -135,11 +134,11 @@ func (ps *P2PServerService) SendPeers(
135
134
// TODO: only accept nodes that are already registered in the node registration
136
135
err := ps .PeerExplorer .AddToUnresolvedPeers (peers , true )
137
136
if err != nil {
138
- return nil , status . Error ( codes . Internal , err .Error ())
137
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
139
138
}
140
139
return & model.Empty {}, nil
141
140
}
142
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
141
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
143
142
}
144
143
145
144
// GetCumulativeDifficulty responds to the request of the cumulative difficulty status of a node
@@ -150,21 +149,21 @@ func (ps *P2PServerService) GetCumulativeDifficulty(
150
149
if ps .PeerExplorer .ValidateRequest (ctx ) {
151
150
blockService := ps .BlockServices [chainType .GetTypeInt ()]
152
151
if blockService == nil {
153
- return nil , status . Error (
154
- codes . InvalidArgument ,
152
+ return nil , blocker . NewBlocker (
153
+ blocker . ValidationErr ,
155
154
"blockServiceNotFoundByThisChainType" ,
156
155
)
157
156
}
158
157
lastBlock , err := blockService .GetLastBlock ()
159
158
if err != nil {
160
- return nil , status . Error ( codes . Internal , err .Error ())
159
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
161
160
}
162
161
return & model.GetCumulativeDifficultyResponse {
163
162
CumulativeDifficulty : lastBlock .CumulativeDifficulty ,
164
163
Height : lastBlock .Height ,
165
164
}, nil
166
165
}
167
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
166
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
168
167
}
169
168
170
169
func (ps P2PServerService ) GetCommonMilestoneBlockIDs (
@@ -182,17 +181,17 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
182
181
blockService = ps .BlockServices [chainType .GetTypeInt ()]
183
182
)
184
183
if blockService == nil {
185
- return nil , status . Error (
186
- codes . InvalidArgument ,
184
+ return nil , blocker . NewBlocker (
185
+ blocker . ValidationErr ,
187
186
"blockServiceNotFoundByThisChainType" ,
188
187
)
189
188
}
190
189
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" )
192
191
}
193
192
myLastBlock , err := blockService .GetLastBlock ()
194
193
if err != nil || myLastBlock == nil {
195
- return nil , status . Error ( codes . Internal , "failedGetLastBlock" )
194
+ return nil , blocker . NewBlocker ( blocker . InternalErr , "failedGetLastBlock" )
196
195
}
197
196
myLastBlockID := myLastBlock .ID
198
197
myBlockchainHeight := myLastBlock .Height
@@ -213,7 +212,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
213
212
lastMilestoneBlock , err := blockService .GetBlockByID (lastMilestoneBlockID , false )
214
213
// this error is handled because when lastMilestoneBlockID is provided, it was expected to be the one returned from this node
215
214
if err != nil {
216
- return nil , status . Error ( codes . Internal , err .Error ())
215
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
217
216
}
218
217
height = lastMilestoneBlock .GetHeight ()
219
218
jump = util .MinUint32 (constant .SafeBlockGap , util .MaxUint32 (myBlockchainHeight , 1 ))
@@ -227,7 +226,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
227
226
for ; limit > 0 ; limit -- {
228
227
block , err := blockService .GetBlockByHeight (height )
229
228
if err != nil {
230
- return nil , status . Error ( codes . Internal , err .Error ())
229
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
231
230
}
232
231
blockIds = append (blockIds , block .ID )
233
232
switch {
@@ -242,7 +241,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
242
241
243
242
return & model.GetCommonMilestoneBlockIdsResponse {BlockIds : blockIds }, nil
244
243
}
245
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
244
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
246
245
}
247
246
248
247
func (ps * P2PServerService ) GetNextBlockIDs (
@@ -257,8 +256,8 @@ func (ps *P2PServerService) GetNextBlockIDs(
257
256
blockService = ps .BlockServices [chainType .GetTypeInt ()]
258
257
)
259
258
if blockService == nil {
260
- return nil , status . Error (
261
- codes . InvalidArgument ,
259
+ return nil , blocker . NewBlocker (
260
+ blocker . ValidationErr ,
262
261
"blockServiceNotFoundByThisChainType" ,
263
262
)
264
263
}
@@ -269,19 +268,19 @@ func (ps *P2PServerService) GetNextBlockIDs(
269
268
270
269
foundBlock , err := blockService .GetBlockByID (reqBlockID , false )
271
270
if err != nil {
272
- return nil , status . Error ( codes . Internal , err .Error ())
271
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
273
272
}
274
273
blocks , err := blockService .GetBlocksFromHeight (foundBlock .Height , limit , false )
275
274
if err != nil {
276
- return nil , status . Error ( codes . Internal , "failedGetBlocks" )
275
+ return nil , blocker . NewBlocker ( blocker . InternalErr , "failedGetBlocks" )
277
276
}
278
277
for _ , block := range blocks {
279
278
blockIds = append (blockIds , block .ID )
280
279
}
281
280
282
281
return blockIds , nil
283
282
}
284
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
283
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
285
284
}
286
285
287
286
func (ps * P2PServerService ) GetNextBlocks (
@@ -297,20 +296,20 @@ func (ps *P2PServerService) GetNextBlocks(
297
296
blockService = ps .BlockServices [chainType .GetTypeInt ()]
298
297
)
299
298
if blockService == nil {
300
- return nil , status . Error (
301
- codes . InvalidArgument ,
299
+ return nil , blocker . NewBlocker (
300
+ blocker . ValidationErr ,
302
301
"blockServiceNotFoundByThisChainType" ,
303
302
)
304
303
}
305
304
blockService .ChainWriteLock (constant .BlockchainSendingBlocks )
306
305
defer blockService .ChainWriteUnlock (constant .BlockchainSendingBlocks )
307
306
block , err := blockService .GetBlockByID (blockID , false )
308
307
if err != nil {
309
- return nil , status . Error ( codes . InvalidArgument , err .Error ())
308
+ return nil , blocker . NewBlocker ( blocker . ValidationErr , err .Error ())
310
309
}
311
310
blocks , err := blockService .GetBlocksFromHeight (block .Height , uint32 (len (blockIDList )), true )
312
311
if err != nil {
313
- return nil , status . Error ( codes . InvalidArgument , err .Error ())
312
+ return nil , blocker . NewBlocker ( blocker . ValidationErr , err .Error ())
314
313
}
315
314
316
315
for idx , block := range blocks {
@@ -319,13 +318,13 @@ func (ps *P2PServerService) GetNextBlocks(
319
318
}
320
319
err = blockService .PopulateBlockData (block )
321
320
if err != nil {
322
- return nil , status . Error ( codes . Internal , err .Error ())
321
+ return nil , blocker . NewBlocker ( blocker . InternalErr , err .Error ())
323
322
}
324
323
blocksMessage = append (blocksMessage , block )
325
324
}
326
325
return & model.BlocksData {NextBlocks : blocksMessage }, nil
327
326
}
328
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
327
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
329
328
}
330
329
331
330
// SendBlock receive block from other node
@@ -338,8 +337,8 @@ func (ps *P2PServerService) SendBlock(
338
337
if ps .PeerExplorer .ValidateRequest (ctx ) {
339
338
var md , _ = metadata .FromIncomingContext (ctx )
340
339
if len (md ) == 0 {
341
- return nil , status . Error (
342
- codes . InvalidArgument ,
340
+ return nil , blocker . NewBlocker (
341
+ blocker . ValidationErr ,
343
342
"InvalidContext" ,
344
343
)
345
344
}
@@ -348,19 +347,19 @@ func (ps *P2PServerService) SendBlock(
348
347
peer , err = p2pUtil .ParsePeer (fullAddress )
349
348
)
350
349
if err != nil {
351
- return nil , status . Error ( codes . InvalidArgument , "invalidPeer" )
350
+ return nil , blocker . NewBlocker ( blocker . ValidationErr , "invalidPeer" )
352
351
}
353
352
blockService := ps .BlockServices [chainType .GetTypeInt ()]
354
353
if blockService == nil {
355
- return nil , status . Error (
356
- codes . InvalidArgument ,
354
+ return nil , blocker . NewBlocker (
355
+ blocker . ValidationErr ,
357
356
"blockServiceNotFoundByThisChainType" ,
358
357
)
359
358
}
360
359
lastBlock , err := blockService .GetLastBlock ()
361
360
if err != nil {
362
- return nil , status . Error (
363
- codes . Internal ,
361
+ return nil , blocker . NewBlocker (
362
+ blocker . InternalErr ,
364
363
"failGetLastBlock" ,
365
364
)
366
365
}
@@ -378,7 +377,7 @@ func (ps *P2PServerService) SendBlock(
378
377
BatchReceipt : batchReceipt ,
379
378
}, nil
380
379
}
381
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
380
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
382
381
}
383
382
384
383
// SendTransaction receive transaction from other node and calling TransactionReceived Event
@@ -391,22 +390,22 @@ func (ps *P2PServerService) SendTransaction(
391
390
if ps .PeerExplorer .ValidateRequest (ctx ) {
392
391
var blockService = ps .BlockServices [chainType .GetTypeInt ()]
393
392
if blockService == nil {
394
- return nil , status . Error (
395
- codes . InvalidArgument ,
393
+ return nil , blocker . NewBlocker (
394
+ blocker . ValidationErr ,
396
395
"blockServiceNotFoundByThisChainType" ,
397
396
)
398
397
}
399
398
lastBlock , err := blockService .GetLastBlock ()
400
399
if err != nil {
401
- return nil , status . Error (
402
- codes . Internal ,
400
+ return nil , blocker . NewBlocker (
401
+ blocker . InternalErr ,
403
402
"failGetLastBlock" ,
404
403
)
405
404
}
406
405
var mempoolService = ps .MempoolServices [chainType .GetTypeInt ()]
407
406
if mempoolService == nil {
408
- return nil , status . Error (
409
- codes . InvalidArgument ,
407
+ return nil , blocker . NewBlocker (
408
+ blocker . ValidationErr ,
410
409
"mempoolServiceNotFoundByThisChainType" ,
411
410
)
412
411
}
@@ -423,7 +422,7 @@ func (ps *P2PServerService) SendTransaction(
423
422
BatchReceipt : batchReceipt ,
424
423
}, nil
425
424
}
426
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
425
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
427
426
}
428
427
429
428
// SendBlockTransactions receive a list of transaction from other node and calling TransactionReceived Event
@@ -436,22 +435,22 @@ func (ps *P2PServerService) SendBlockTransactions(
436
435
if ps .PeerExplorer .ValidateRequest (ctx ) {
437
436
var blockService = ps .BlockServices [chainType .GetTypeInt ()]
438
437
if blockService == nil {
439
- return nil , status . Error (
440
- codes . InvalidArgument ,
438
+ return nil , blocker . NewBlocker (
439
+ blocker . ValidationErr ,
441
440
"blockServiceNotFoundByThisChainType" ,
442
441
)
443
442
}
444
443
lastBlock , err := blockService .GetLastBlock ()
445
444
if err != nil {
446
- return nil , status . Error (
447
- codes . Internal ,
445
+ return nil , blocker . NewBlocker (
446
+ blocker . InternalErr ,
448
447
"failGetLastBlock" ,
449
448
)
450
449
}
451
450
var mempoolService = ps .MempoolServices [chainType .GetTypeInt ()]
452
451
if mempoolService == nil {
453
- return nil , status . Error (
454
- codes . InvalidArgument ,
452
+ return nil , blocker . NewBlocker (
453
+ blocker . ValidationErr ,
455
454
"mempoolServiceNotFoundByThisChainType" ,
456
455
)
457
456
}
@@ -468,7 +467,7 @@ func (ps *P2PServerService) SendBlockTransactions(
468
467
BatchReceipts : batchReceipts ,
469
468
}, nil
470
469
}
471
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
470
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
472
471
}
473
472
474
473
func (ps * P2PServerService ) RequestBlockTransactions (
@@ -480,8 +479,8 @@ func (ps *P2PServerService) RequestBlockTransactions(
480
479
if ps .PeerExplorer .ValidateRequest (ctx ) {
481
480
var md , _ = metadata .FromIncomingContext (ctx )
482
481
if len (md ) == 0 {
483
- return nil , status . Error (
484
- codes . InvalidArgument ,
482
+ return nil , blocker . NewBlocker (
483
+ blocker . ValidationErr ,
485
484
"invalidContext" ,
486
485
)
487
486
}
@@ -490,12 +489,12 @@ func (ps *P2PServerService) RequestBlockTransactions(
490
489
peer , err = p2pUtil .ParsePeer (fullAddress )
491
490
)
492
491
if err != nil {
493
- return nil , status . Error ( codes . InvalidArgument , "invalidPeer" )
492
+ return nil , blocker . NewBlocker ( blocker . ValidationErr , "invalidPeer" )
494
493
}
495
494
ps .Observer .Notify (observer .BlockTransactionsRequested , transactionsIDs , chainType , blockID , peer )
496
495
return & model.Empty {}, nil
497
496
}
498
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
497
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
499
498
}
500
499
501
500
func (ps * P2PServerService ) RequestDownloadFile (
@@ -521,5 +520,5 @@ func (ps *P2PServerService) RequestDownloadFile(
521
520
}
522
521
return res , nil
523
522
}
524
- return nil , status . Error ( codes . Unauthenticated , "Rejected request" )
523
+ return nil , blocker . NewBlocker ( blocker . P2PNetworkConnectionErr , "Rejected request" )
525
524
}
0 commit comments