Skip to content

Commit

Permalink
add blockdao server and grpc blockdao (#4263)
Browse files Browse the repository at this point in the history
Co-authored-by: envestcc <[email protected]>
  • Loading branch information
CoderZhi and envestcc authored Oct 25, 2024
1 parent 4d34f88 commit d32bf7d
Show file tree
Hide file tree
Showing 11 changed files with 2,078 additions and 14 deletions.
163 changes: 163 additions & 0 deletions api/blockdaoservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package api

import (
"context"
"encoding/hex"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/iotexproject/iotex-core/v2/blockchain/blockdao"
"github.com/iotexproject/iotex-core/v2/blockchain/blockdao/blockdaopb"
)

type blockDAOService struct {
dao blockdao.BlockDAO
}

func newBlockDAOService(dao blockdao.BlockDAO) *blockDAOService {
return &blockDAOService{
dao: dao,
}
}

func (service *blockDAOService) Height(context.Context, *emptypb.Empty) (*blockdaopb.BlockHeightResponse, error) {
height, err := service.dao.Height()
if err != nil {
return nil, err
}
return &blockdaopb.BlockHeightResponse{
Height: height,
}, nil
}

func (service *blockDAOService) GetBlockHash(_ context.Context, request *blockdaopb.BlockHeightRequest) (*blockdaopb.BlockHashResponse, error) {
h, err := service.dao.GetBlockHash(request.Height)
if err != nil {
return nil, err
}

return &blockdaopb.BlockHashResponse{
Hash: hex.EncodeToString(h[:]),
}, nil
}

func (service *blockDAOService) GetBlockHeight(_ context.Context, request *blockdaopb.BlockHashRequest) (*blockdaopb.BlockHeightResponse, error) {
h, err := hash.HexStringToHash256(request.Hash)
if err != nil {
return nil, err
}
height, err := service.dao.GetBlockHeight(h)
if err != nil {
return nil, err
}

return &blockdaopb.BlockHeightResponse{
Height: height,
}, nil

}

func (service *blockDAOService) GetBlock(_ context.Context, request *blockdaopb.BlockHashRequest) (*blockdaopb.GetBlockResponse, error) {
h, err := hash.HexStringToHash256(request.Hash)
if err != nil {
return nil, err
}
blk, err := service.dao.GetBlock(h)
if err != nil {
return nil, err
}

return &blockdaopb.GetBlockResponse{
Block: blk.ConvertToBlockPb(),
}, nil
}

func (service *blockDAOService) GetBlockByHeight(_ context.Context, request *blockdaopb.BlockHeightRequest) (*blockdaopb.GetBlockResponse, error) {
blk, err := service.dao.GetBlockByHeight(request.Height)
if err != nil {
return nil, err
}
return &blockdaopb.GetBlockResponse{
Block: blk.ConvertToBlockPb(),
}, nil
}

func (service *blockDAOService) GetReceipts(_ context.Context, request *blockdaopb.BlockHeightRequest) (*iotextypes.Receipts, error) {
receipts, err := service.dao.GetReceipts(request.Height)
if err != nil {
return nil, err
}
arr := make([]*iotextypes.Receipt, 0, len(receipts))
for _, receipt := range receipts {
arr = append(arr, receipt.ConvertToReceiptPb())
}

return &iotextypes.Receipts{
Receipts: arr,
}, nil
}

func (service *blockDAOService) ContainsTransactionLog(context.Context, *emptypb.Empty) (*blockdaopb.ContainsTransactionLogResponse, error) {
return &blockdaopb.ContainsTransactionLogResponse{
Yes: service.dao.ContainsTransactionLog(),
}, nil
}

func (service *blockDAOService) TransactionLogs(_ context.Context, request *blockdaopb.BlockHeightRequest) (*blockdaopb.TransactionLogsResponse, error) {
logs, err := service.dao.TransactionLogs(request.Height)
if err != nil {
return nil, err
}
return &blockdaopb.TransactionLogsResponse{
TransactionLogs: logs,
}, nil
}

func (service *blockDAOService) Header(_ context.Context, request *blockdaopb.BlockHashRequest) (*blockdaopb.HeaderResponse, error) {
h, err := hash.HexStringToHash256(request.Hash)
if err != nil {
return nil, err
}
header, err := service.dao.Header(h)
if err != nil {
return nil, err
}

return &blockdaopb.HeaderResponse{
Header: header.Proto(),
}, nil

}

func (service *blockDAOService) HeaderByHeight(_ context.Context, request *blockdaopb.BlockHeightRequest) (*blockdaopb.HeaderResponse, error) {
header, err := service.dao.HeaderByHeight(request.Height)
if err != nil {
return nil, err
}

return &blockdaopb.HeaderResponse{
Header: header.Proto(),
}, nil
}

func (service *blockDAOService) FooterByHeight(_ context.Context, request *blockdaopb.BlockHeightRequest) (*blockdaopb.FooterResponse, error) {
footer, err := service.dao.FooterByHeight(request.Height)
if err != nil {
return nil, err
}
footerpb, err := footer.ConvertToBlockFooterPb()
if err != nil {
return nil, err
}

return &blockdaopb.FooterResponse{
Footer: footerpb,
}, nil
}
6 changes: 5 additions & 1 deletion api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/iotexproject/iotex-core/v2/api/logfilter"
apitypes "github.com/iotexproject/iotex-core/v2/api/types"
"github.com/iotexproject/iotex-core/v2/blockchain/block"
"github.com/iotexproject/iotex-core/v2/blockchain/blockdao/blockdaopb"
"github.com/iotexproject/iotex-core/v2/pkg/log"
"github.com/iotexproject/iotex-core/v2/pkg/recovery"
"github.com/iotexproject/iotex-core/v2/pkg/tracer"
Expand Down Expand Up @@ -81,7 +82,7 @@ func RecoveryInterceptor() grpc_recovery.Option {
}

// NewGRPCServer creates a new grpc server
func NewGRPCServer(core CoreService, grpcPort int) *GRPCServer {
func NewGRPCServer(core CoreService, bds *blockDAOService, grpcPort int) *GRPCServer {
if grpcPort == 0 {
return nil
}
Expand All @@ -104,6 +105,9 @@ func NewGRPCServer(core CoreService, grpcPort int) *GRPCServer {
//serviceName: grpc.health.v1.Health
grpc_health_v1.RegisterHealthServer(gSvr, health.NewServer())
iotexapi.RegisterAPIServiceServer(gSvr, newGRPCHandler(core))
if bds != nil {
blockdaopb.RegisterBlockDAOServiceServer(gSvr, bds)
}
grpc_prometheus.EnableHandlingTimeHistogram()
grpc_prometheus.Register(gSvr)
reflection.Register(gSvr)
Expand Down
2 changes: 1 addition & 1 deletion api/serverV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewServerV2(

return &ServerV2{
core: coreAPI,
grpcServer: NewGRPCServer(coreAPI, cfg.GRPCPort),
grpcServer: NewGRPCServer(coreAPI, newBlockDAOService(dao), cfg.GRPCPort),
httpSvr: NewHTTPServer("", cfg.HTTPPort, wrappedWeb3Handler),
websocketSvr: NewHTTPServer("", cfg.WebSocketPort, wrappedWebsocketHandler),
tracer: tp,
Expand Down
2 changes: 1 addition & 1 deletion api/serverV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestServerV2(t *testing.T) {
web3Handler := NewWeb3Handler(core, "", _defaultBatchRequestLimit)
svr := &ServerV2{
core: core,
grpcServer: NewGRPCServer(core, testutil.RandomPort()),
grpcServer: NewGRPCServer(core, nil, testutil.RandomPort()),
httpSvr: NewHTTPServer("", testutil.RandomPort(), newHTTPHandler(web3Handler)),
websocketSvr: NewHTTPServer("", testutil.RandomPort(), NewWebsocketHandler(core, web3Handler, nil)),
}
Expand Down
8 changes: 5 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,12 @@ func (bc *blockchain) commitBlock(blk *block.Block) error {
putTimer := bc.timerFactory.NewTimer("putBlock")
err = bc.dao.PutBlock(ctx, blk)
putTimer.End()
switch {
case errors.Cause(err) == filedao.ErrAlreadyExist:
switch errors.Cause(err) {
case filedao.ErrAlreadyExist, blockdao.ErrAlreadyExist:
return nil
case err != nil:
case nil:
// do nothing
default:
return err
}
blkHash := blk.HashBlock()
Expand Down
Loading

0 comments on commit d32bf7d

Please sign in to comment.