Skip to content

Commit

Permalink
feat: update ScanBlock function with new API calls
Browse files Browse the repository at this point in the history
- Add new imports for `ton` and `zap` packages
- Refactor `ScanBlock` function to use new API calls and error handling
- Add unit tests for the `ScanBlock` function

Signed-off-by: Sean Zheng <[email protected]>
  • Loading branch information
blackhorseya committed Jul 28, 2024
1 parent ecfaa9c commit c5e2b4d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
40 changes: 37 additions & 3 deletions app/domain/block/biz/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (

"github.com/blackhorseya/ryze/app/infra/tonx"
"github.com/blackhorseya/ryze/entity/domain/block/model"
"github.com/blackhorseya/ryze/pkg/contextx"
"github.com/xssnick/tonutils-go/ton"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -34,7 +37,38 @@ func (i *impl) GetBlocks(request *model.GetBlocksRequest, server model.BlockServ
panic("implement me")
}

func (i *impl) ScanBlock(request *model.ScanBlockRequest, server model.BlockService_ScanBlockServer) error {
// TODO: 2024/7/28|sean|implement me
panic("implement me")
func (i *impl) ScanBlock(request *model.ScanBlockRequest, stream model.BlockService_ScanBlockServer) error {
api := ton.NewAPIClient(i.client, ton.ProofCheckPolicyFast).WithRetry()
api.SetTrustedBlockFromConfig(i.client.Config)

ctx := contextx.WithContext(stream.Context())
master, err := api.GetMasterchainInfo(ctx)
if err != nil {
ctx.Error("failed to get masterchain info", zap.Error(err))
return err
}
ctx.Info("master proofs chain successfully verified, all data is now safe and trusted!")

stickyContext := api.Client().StickyContext(ctx)
shardLastSeqno := map[string]uint32{}
firstShards, err := api.GetBlockShardsInfo(stickyContext, master)
if err != nil {
ctx.Error("failed to get block shards info", zap.Error(err))
return err
}

for _, shard := range firstShards {
shardLastSeqno[tonx.GetShardID(shard)] = shard.SeqNo
}

for {
ctx.Info("scanning master block", zap.Uint32("seq_no", master.SeqNo))

next := master.SeqNo + 1
master, err = api.WaitForBlock(next).LookupBlock(ctx, master.Workchain, master.Shard, next)
if err != nil {
ctx.Error("failed to lookup block", zap.Uint32("seq_no", next), zap.Error(err))
return err
}
}
}
44 changes: 44 additions & 0 deletions app/domain/block/biz/block_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package biz

import (
"testing"
"time"

"github.com/blackhorseya/ryze/app/infra/tonx"
"github.com/blackhorseya/ryze/entity/domain/block/model"
"github.com/blackhorseya/ryze/pkg/contextx"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

type suiteTester struct {
suite.Suite

ctrl *gomock.Controller
client *tonx.Client
biz model.BlockServiceServer
}

func (s *suiteTester) SetupTest() {
s.ctrl = gomock.NewController(s.T())
s.client, _ = tonx.NewClient(tonx.Options{Network: "testnet"})
s.biz = NewBlockService(s.client)
}

func (s *suiteTester) TearDownTest() {
s.ctrl.Finish()
}

func TestAll(t *testing.T) {
suite.Run(t, new(suiteTester))
}

func (s *suiteTester) Test_impl_ScanBlock() {
stream := model.NewMockBlockService_ScanBlockServer(s.ctrl)

timeout, cancelFunc := contextx.WithTimeout(contextx.Background(), 2*time.Second)
defer cancelFunc()

stream.EXPECT().Context().Return(timeout).Times(1)
_ = s.biz.ScanBlock(&model.ScanBlockRequest{}, stream)
}
12 changes: 12 additions & 0 deletions app/infra/tonx/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tonx

import (
"fmt"

"github.com/xssnick/tonutils-go/ton"
)

// GetShardID is used to get the shard id
func GetShardID(shard *ton.BlockIDExt) string {
return fmt.Sprintf("%d|%d", shard.Workchain, shard.Shard)
}

0 comments on commit c5e2b4d

Please sign in to comment.