-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
128 lines (116 loc) · 3.73 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package service
import (
"context"
"time"
"github.com/google/wire"
"github.com/nervina-labs/cota-syncer/internal/biz"
"github.com/nervina-labs/cota-syncer/internal/data"
"github.com/nervina-labs/cota-syncer/internal/logger"
ckbTypes "github.com/nervosnetwork/ckb-sdk-go/types"
)
var ProviderSet = wire.NewSet(NewBlockSyncService, NewCheckInfoService, NewMetadataSyncService, NewInvalidDataService, NewWithdrawExtraInfoService)
type BlockSyncService struct {
checkInfoUsecase *biz.CheckInfoUsecase
logger *logger.Logger
client *data.CkbNodeClient
status chan struct{}
systemScripts data.SystemScripts
blockSyncer data.BlockSyncer
}
func (s *BlockSyncService) Start(ctx context.Context, mode string) error {
s.logger.Info(ctx, "Successfully started the sync service~")
go func() {
for {
select {
case <-ctx.Done():
s.status <- struct{}{}
s.logger.Infof(ctx, "receive cancel signal %v", ctx.Err())
return
default:
s.sync(ctx)
if mode == "normal" {
time.Sleep(1 * time.Second)
}
}
}
}()
return nil
}
func (s *BlockSyncService) sync(ctx context.Context) {
checkInfo := biz.CheckInfo{CheckType: biz.SyncBlock}
err := s.checkInfoUsecase.LastCheckInfo(ctx, &checkInfo)
if err != nil {
s.logger.Errorf(ctx, "get %s check info error: %v", checkInfo.CheckType.String(), err)
}
tipBlockNumber, err := s.client.Rpc.GetTipBlockNumber(ctx)
if err != nil {
s.logger.Errorf(ctx, "get tip block number rpc error: %v", err)
}
s.logger.Infof(ctx, "check tip block number: %v, tip block number: %v", checkInfo.BlockNumber, tipBlockNumber)
if checkInfo.BlockNumber > tipBlockNumber {
return
}
targetBlockNumber := checkInfo.BlockNumber + 1
if targetBlockNumber > tipBlockNumber {
return
}
targetBlock, err := s.client.Rpc.GetBlockByNumber(ctx, targetBlockNumber)
if err != nil {
s.logger.Errorf(ctx, "get block %d rpc error: %v", targetBlockNumber, err)
return
}
// rollback
if isForked(checkInfo, targetBlock) {
s.logger.Info(ctx, "forked")
err = s.rollback(ctx, checkInfo.BlockNumber)
if err != nil {
s.logger.Errorf(ctx, "rollback %s error: %v", checkInfo.CheckType.String(), err)
}
return
}
// save key pairs
checkInfo.BlockNumber = targetBlockNumber
checkInfo.BlockHash = targetBlock.Header.Hash.String()[2:]
err = s.syncBlock(ctx, targetBlock, checkInfo)
if err != nil {
s.logger.Errorf(ctx, "save %s kv pairs error: %v", checkInfo.CheckType.String(), err)
}
}
func isForked(checkInfo biz.CheckInfo, targetBlock *ckbTypes.Block) bool {
if checkInfo.BlockHash == "" {
return false
}
return checkInfo.BlockHash != targetBlock.Header.ParentHash.String()[2:]
}
func (s *BlockSyncService) syncBlock(ctx context.Context, block *ckbTypes.Block, checkInfo biz.CheckInfo) error {
return s.blockSyncer.Sync(ctx, block, checkInfo, s.systemScripts)
}
func (s *BlockSyncService) rollback(ctx context.Context, blockNumber uint64) error {
return s.blockSyncer.Rollback(ctx, blockNumber)
}
func (s *BlockSyncService) Stop(ctx context.Context) error {
s.client.Rpc.Close()
for {
select {
case <-s.status:
s.logger.Info(ctx, "Successfully closed the cota entries sync service~")
return nil
default:
time.Sleep(1 * time.Second)
}
}
}
func NewBlockSyncService(checkInfoUsecase *biz.CheckInfoUsecase, logger *logger.Logger, client *data.CkbNodeClient, systemScripts data.SystemScripts, blockSyncer data.BlockSyncer) *BlockSyncService {
return &BlockSyncService{
checkInfoUsecase: checkInfoUsecase,
logger: logger,
client: client,
status: make(chan struct{}, 1),
systemScripts: systemScripts,
blockSyncer: blockSyncer,
}
}
type Service interface {
Start(context.Context, string) error
Stop(context.Context) error
}