Skip to content

fix return type to use blocker #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
)

var (
ServiceErr TypeBlocker = "ServiceErr"
InternalErr TypeBlocker = "InternalErr"
DBErr TypeBlocker = "DBErr"
DBRowNotFound TypeBlocker = "DBRowNotFound"
BlockErr TypeBlocker = "BlockErr"
Expand Down
109 changes: 54 additions & 55 deletions p2p/service/p2pServerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"

"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/model"
Expand All @@ -11,9 +12,7 @@ import (
"github.com/zoobc/zoobc-core/observer"
"github.com/zoobc/zoobc-core/p2p/strategy"
p2pUtil "github.com/zoobc/zoobc-core/p2p/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

type (
Expand Down Expand Up @@ -110,7 +109,7 @@ func (ps *P2PServerService) GetPeerInfo(ctx context.Context, req *model.GetPeerI
HostInfo: ps.PeerExplorer.GetHostInfo(),
}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

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

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

// GetCumulativeDifficulty responds to the request of the cumulative difficulty status of a node
Expand All @@ -150,21 +149,21 @@ func (ps *P2PServerService) GetCumulativeDifficulty(
if ps.PeerExplorer.ValidateRequest(ctx) {
blockService := ps.BlockServices[chainType.GetTypeInt()]
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
lastBlock, err := blockService.GetLastBlock()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
}
return &model.GetCumulativeDifficultyResponse{
CumulativeDifficulty: lastBlock.CumulativeDifficulty,
Height: lastBlock.Height,
}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

func (ps P2PServerService) GetCommonMilestoneBlockIDs(
Expand All @@ -182,17 +181,17 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
blockService = ps.BlockServices[chainType.GetTypeInt()]
)
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
if lastBlockID == 0 && lastMilestoneBlockID == 0 {
return nil, status.Error(codes.InvalidArgument, "either LastBlockID or LastMilestoneBlockID has to be supplied")
return nil, blocker.NewBlocker(blocker.ValidationErr, "either LastBlockID or LastMilestoneBlockID has to be supplied")
}
myLastBlock, err := blockService.GetLastBlock()
if err != nil || myLastBlock == nil {
return nil, status.Error(codes.Internal, "failedGetLastBlock")
return nil, blocker.NewBlocker(blocker.InternalErr, "failedGetLastBlock")
}
myLastBlockID := myLastBlock.ID
myBlockchainHeight := myLastBlock.Height
Expand All @@ -213,7 +212,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
lastMilestoneBlock, err := blockService.GetBlockByID(lastMilestoneBlockID, false)
// this error is handled because when lastMilestoneBlockID is provided, it was expected to be the one returned from this node
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
}
height = lastMilestoneBlock.GetHeight()
jump = util.MinUint32(constant.SafeBlockGap, util.MaxUint32(myBlockchainHeight, 1))
Expand All @@ -227,7 +226,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(
for ; limit > 0; limit-- {
block, err := blockService.GetBlockByHeight(height)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
}
blockIds = append(blockIds, block.ID)
switch {
Expand All @@ -242,7 +241,7 @@ func (ps P2PServerService) GetCommonMilestoneBlockIDs(

return &model.GetCommonMilestoneBlockIdsResponse{BlockIds: blockIds}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

func (ps *P2PServerService) GetNextBlockIDs(
Expand All @@ -257,8 +256,8 @@ func (ps *P2PServerService) GetNextBlockIDs(
blockService = ps.BlockServices[chainType.GetTypeInt()]
)
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
Expand All @@ -269,19 +268,19 @@ func (ps *P2PServerService) GetNextBlockIDs(

foundBlock, err := blockService.GetBlockByID(reqBlockID, false)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
}
blocks, err := blockService.GetBlocksFromHeight(foundBlock.Height, limit, false)
if err != nil {
return nil, status.Error(codes.Internal, "failedGetBlocks")
return nil, blocker.NewBlocker(blocker.InternalErr, "failedGetBlocks")
}
for _, block := range blocks {
blockIds = append(blockIds, block.ID)
}

return blockIds, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

func (ps *P2PServerService) GetNextBlocks(
Expand All @@ -297,20 +296,20 @@ func (ps *P2PServerService) GetNextBlocks(
blockService = ps.BlockServices[chainType.GetTypeInt()]
)
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
blockService.ChainWriteLock(constant.BlockchainSendingBlocks)
defer blockService.ChainWriteUnlock(constant.BlockchainSendingBlocks)
block, err := blockService.GetBlockByID(blockID, false)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
return nil, blocker.NewBlocker(blocker.ValidationErr, err.Error())
}
blocks, err := blockService.GetBlocksFromHeight(block.Height, uint32(len(blockIDList)), true)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
return nil, blocker.NewBlocker(blocker.ValidationErr, err.Error())
}

for idx, block := range blocks {
Expand All @@ -319,13 +318,13 @@ func (ps *P2PServerService) GetNextBlocks(
}
err = blockService.PopulateBlockData(block)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, blocker.NewBlocker(blocker.InternalErr, err.Error())
}
blocksMessage = append(blocksMessage, block)
}
return &model.BlocksData{NextBlocks: blocksMessage}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

// SendBlock receive block from other node
Expand All @@ -338,8 +337,8 @@ func (ps *P2PServerService) SendBlock(
if ps.PeerExplorer.ValidateRequest(ctx) {
var md, _ = metadata.FromIncomingContext(ctx)
if len(md) == 0 {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"InvalidContext",
)
}
Expand All @@ -348,19 +347,19 @@ func (ps *P2PServerService) SendBlock(
peer, err = p2pUtil.ParsePeer(fullAddress)
)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalidPeer")
return nil, blocker.NewBlocker(blocker.ValidationErr, "invalidPeer")
}
blockService := ps.BlockServices[chainType.GetTypeInt()]
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
lastBlock, err := blockService.GetLastBlock()
if err != nil {
return nil, status.Error(
codes.Internal,
return nil, blocker.NewBlocker(
blocker.InternalErr,
"failGetLastBlock",
)
}
Expand All @@ -378,7 +377,7 @@ func (ps *P2PServerService) SendBlock(
BatchReceipt: batchReceipt,
}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

// SendTransaction receive transaction from other node and calling TransactionReceived Event
Expand All @@ -391,22 +390,22 @@ func (ps *P2PServerService) SendTransaction(
if ps.PeerExplorer.ValidateRequest(ctx) {
var blockService = ps.BlockServices[chainType.GetTypeInt()]
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
lastBlock, err := blockService.GetLastBlock()
if err != nil {
return nil, status.Error(
codes.Internal,
return nil, blocker.NewBlocker(
blocker.InternalErr,
"failGetLastBlock",
)
}
var mempoolService = ps.MempoolServices[chainType.GetTypeInt()]
if mempoolService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"mempoolServiceNotFoundByThisChainType",
)
}
Expand All @@ -423,7 +422,7 @@ func (ps *P2PServerService) SendTransaction(
BatchReceipt: batchReceipt,
}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

// SendBlockTransactions receive a list of transaction from other node and calling TransactionReceived Event
Expand All @@ -436,22 +435,22 @@ func (ps *P2PServerService) SendBlockTransactions(
if ps.PeerExplorer.ValidateRequest(ctx) {
var blockService = ps.BlockServices[chainType.GetTypeInt()]
if blockService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"blockServiceNotFoundByThisChainType",
)
}
lastBlock, err := blockService.GetLastBlock()
if err != nil {
return nil, status.Error(
codes.Internal,
return nil, blocker.NewBlocker(
blocker.InternalErr,
"failGetLastBlock",
)
}
var mempoolService = ps.MempoolServices[chainType.GetTypeInt()]
if mempoolService == nil {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"mempoolServiceNotFoundByThisChainType",
)
}
Expand All @@ -468,7 +467,7 @@ func (ps *P2PServerService) SendBlockTransactions(
BatchReceipts: batchReceipts,
}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

func (ps *P2PServerService) RequestBlockTransactions(
Expand All @@ -480,8 +479,8 @@ func (ps *P2PServerService) RequestBlockTransactions(
if ps.PeerExplorer.ValidateRequest(ctx) {
var md, _ = metadata.FromIncomingContext(ctx)
if len(md) == 0 {
return nil, status.Error(
codes.InvalidArgument,
return nil, blocker.NewBlocker(
blocker.ValidationErr,
"invalidContext",
)
}
Expand All @@ -490,12 +489,12 @@ func (ps *P2PServerService) RequestBlockTransactions(
peer, err = p2pUtil.ParsePeer(fullAddress)
)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalidPeer")
return nil, blocker.NewBlocker(blocker.ValidationErr, "invalidPeer")
}
ps.Observer.Notify(observer.BlockTransactionsRequested, transactionsIDs, chainType, blockID, peer)
return &model.Empty{}, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}

func (ps *P2PServerService) RequestDownloadFile(
Expand All @@ -521,5 +520,5 @@ func (ps *P2PServerService) RequestDownloadFile(
}
return res, nil
}
return nil, status.Error(codes.Unauthenticated, "Rejected request")
return nil, blocker.NewBlocker(blocker.P2PNetworkConnectionErr, "Rejected request")
}