Skip to content

Commit

Permalink
refactor: refactor block info fetching and enhance error handling (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackhorseya authored Oct 5, 2024
2 parents 879dd11 + 532b07f commit d26acf6
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions app/domain/block/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package block
import (
"context"
"errors"
"fmt"
"io"
"strconv"
"time"
Expand Down Expand Up @@ -149,7 +150,7 @@ func (i *impl) FoundNewBlock(stream grpc.BidiStreamingServer[model.Block, model.
continue
}

err = i.FetchBlockInfo(ctx, newBlock)
err = i.fetchBlockInfo(ctx, newBlock)
if errors.Is(err, context.Canceled) {
ctx.Info("found new block canceled")
return nil
Expand All @@ -173,34 +174,6 @@ func (i *impl) FoundNewBlock(stream grpc.BidiStreamingServer[model.Block, model.
}
}

// FetchBlockInfo is used to fetch block info
func (i *impl) FetchBlockInfo(c context.Context, block *model.Block) (err error) {
ctx, span := contextx.StartSpan(c, "block.biz.FetchBlockInfo")
defer span.End()

// 初始化 TON API 客戶端
api := ton.NewAPIClient(i.tonClient).WithRetry()

// 查找區塊
blockID, err := api.LookupBlock(ctx, block.Workchain, block.Shard, block.SeqNo)
if err != nil && !errors.Is(err, context.Canceled) {
ctx.Error("failed to lookup block", zap.Error(err), zap.Any("block", block))
return err
}

// 獲取區塊資訊
blockData, err := api.GetBlockData(ctx, blockID)
if err != nil && !errors.Is(err, context.Canceled) {
ctx.Error("failed to get block data", zap.Error(err))
return err
}

// fill block info
block.Timestamp = timestamppb.New(time.Unix(int64(blockData.BlockInfo.GenUtime), 0))

return nil
}

func (i *impl) GetBlock(c context.Context, req *biz.GetBlockRequest) (*model.Block, error) {
ctx, span := contextx.StartSpan(c, "block.biz.GetBlock")
defer span.End()
Expand Down Expand Up @@ -242,6 +215,54 @@ func (i *impl) ListBlocks(req *biz.ListBlocksRequest, stream grpc.ServerStreamin
}

func (i *impl) FoundNewBlockNonStream(c context.Context, req *biz.FoundNewBlockRequest) (*model.Block, error) {
// TODO: 2024/10/5|sean|implement me
panic("implement me")
ctx, span := contextx.StartSpan(c, "block.biz.FoundNewBlockNonStream")
defer span.End()

block, err := model.NewBlock(req.Workchain, req.Shard, req.SeqNo)
if err != nil {
ctx.Error("failed to create block", zap.Error(err), zap.Any("request", &req))
return nil, err
}

err = i.fetchBlockInfo(ctx, block)
if err != nil {
ctx.Error("failed to fetch block info", zap.Error(err), zap.Any("block", &block))
return nil, fmt.Errorf("failed to fetch block info: %w", err)
}

err = i.blocks.Create(ctx, block)
if err != nil {
ctx.Error("failed to create block", zap.Error(err), zap.Any("block", &block))
return nil, fmt.Errorf("failed to create block: %w", err)
}

return block, nil
}

// fetchBlockInfo is used to fetch block info
func (i *impl) fetchBlockInfo(c context.Context, block *model.Block) (err error) {
ctx, span := contextx.StartSpan(c, "block.biz.fetchBlockInfo")
defer span.End()

// 初始化 TON API 客戶端
api := ton.NewAPIClient(i.tonClient).WithRetry()

// 查找區塊
blockID, err := api.LookupBlock(ctx, block.Workchain, block.Shard, block.SeqNo)
if err != nil && !errors.Is(err, context.Canceled) {
ctx.Error("failed to lookup block", zap.Error(err), zap.Any("block", block))
return err
}

// 獲取區塊資訊
blockData, err := api.GetBlockData(ctx, blockID)
if err != nil && !errors.Is(err, context.Canceled) {
ctx.Error("failed to get block data", zap.Error(err))
return err
}

// fill block info
block.Timestamp = timestamppb.New(time.Unix(int64(blockData.BlockInfo.GenUtime), 0))

return nil
}

0 comments on commit d26acf6

Please sign in to comment.