-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
82 lines (72 loc) · 2.24 KB
/
sync.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
package sdk
import (
"context"
"encoding/hex"
"connectrpc.com/connect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/sync"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/sync/syncconnect"
)
type SyncServiceClient syncconnect.SyncServiceClient
func NewSyncServiceClient(u *UtxorpcClient) SyncServiceClient {
return u.NewSyncServiceClient()
}
func (u *UtxorpcClient) NewSyncServiceClient() SyncServiceClient {
return syncconnect.NewSyncServiceClient(
u.httpClient,
u.baseUrl,
connect.WithGRPC(),
)
}
func syncIntersect(blockHashStr string, blockIndex int64) []*sync.BlockRef {
var intersect []*sync.BlockRef
// Construct the BlockRef based on the provided parameters
blockRef := &sync.BlockRef{}
if blockHashStr != "" {
hash, err := hex.DecodeString(blockHashStr)
if err != nil {
return nil
}
blockRef.Hash = hash
}
// We assume blockIndex can be 0 or any positive number
if blockIndex > -1 {
blockRef.Index = uint64(blockIndex)
}
// Only add blockRef to intersect if at least one of blockHashStr or blockIndex is provided
if blockHashStr != "" || blockIndex > -1 {
intersect = []*sync.BlockRef{blockRef}
}
return intersect
}
func (u *UtxorpcClient) FetchBlock(
blockHashStr string,
blockIndex int64,
) (*connect.Response[sync.FetchBlockResponse], error) {
ctx := context.Background()
req := &sync.FetchBlockRequest{Ref: syncIntersect(blockHashStr, blockIndex)}
return u.FetchBlockWithContext(ctx, req)
}
func (u *UtxorpcClient) FetchBlockWithContext(
ctx context.Context,
blockReq *sync.FetchBlockRequest,
) (*connect.Response[sync.FetchBlockResponse], error) {
req := connect.NewRequest(blockReq)
u.AddHeadersToRequest(req)
return u.Sync.FetchBlock(ctx, req)
}
func (u *UtxorpcClient) FollowTip(
blockHashStr string,
blockIndex int64,
) (*connect.ServerStreamForClient[sync.FollowTipResponse], error) {
ctx := context.Background()
req := &sync.FollowTipRequest{Intersect: syncIntersect(blockHashStr, blockIndex)}
return u.FollowTipWithContext(ctx, req)
}
func (u *UtxorpcClient) FollowTipWithContext(
ctx context.Context,
blockReq *sync.FollowTipRequest,
) (*connect.ServerStreamForClient[sync.FollowTipResponse], error) {
req := connect.NewRequest(blockReq)
u.AddHeadersToRequest(req)
return u.Sync.FollowTip(ctx, req)
}