Skip to content

Commit

Permalink
feat: update ScanBlock function with new API calls (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackhorseya authored Jul 28, 2024
2 parents 189cc56 + c5e2b4d commit 5d92125
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
6 changes: 6 additions & 0 deletions adapter/block/scan/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/blackhorseya/ryze/app/domain/block/biz"
"github.com/blackhorseya/ryze/app/infra/configx"
"github.com/blackhorseya/ryze/app/infra/otelx"
"github.com/blackhorseya/ryze/app/infra/tonx"
"github.com/blackhorseya/ryze/app/infra/transports/httpx"
"github.com/blackhorseya/ryze/pkg/adapterx"
"github.com/blackhorseya/ryze/pkg/contextx"
Expand All @@ -37,6 +38,10 @@ func initServer(app *configx.Application) (*httpx.Server, error) {
return httpx.NewServer(app.HTTP)
}

func initTonx() (*tonx.Client, error) {
return tonx.NewClient(tonx.Options{Network: "mainnet"})
}

func New(v *viper.Viper) (adapterx.Restful, error) {
panic(wire.Build(
wire.Struct(new(wirex.Injector), "*"),
Expand All @@ -47,5 +52,6 @@ func New(v *viper.Viper) (adapterx.Restful, error) {
initServer,

biz.NewBlockService,
initTonx,
))
}
11 changes: 10 additions & 1 deletion adapter/block/scan/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 43 additions & 5 deletions app/domain/block/biz/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package biz
import (
"context"

"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"
)

type impl struct {
client *tonx.Client
}

// NewBlockService is used to create a new model.BlockServiceServer
func NewBlockService() model.BlockServiceServer {
return &impl{}
func NewBlockService(client *tonx.Client) model.BlockServiceServer {
return &impl{
client: client,
}
}

func (i *impl) GetBlock(ctx context.Context, request *model.GetBlockRequest) (*model.Block, error) {
Expand All @@ -30,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 5d92125

Please sign in to comment.