generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmerkleroots_api.go
107 lines (90 loc) · 2.91 KB
/
merkleroots_api.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
package merkleroots
import (
"context"
"errors"
"fmt"
"net/url"
goclienterr "github.com/bitcoin-sv/spv-wallet-go-client/errors"
"github.com/bitcoin-sv/spv-wallet-go-client/internal/api/v1/queryparams"
"github.com/bitcoin-sv/spv-wallet-go-client/queries"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/go-resty/resty/v2"
)
const (
route = "api/v1/merkleroots"
api = "User Merkle roots API"
)
// MerkleRootsRepository is an interface responsible for storing synchronized MerkleRoots and retrieving the last evaluation key from the database.
type MerkleRootsRepository interface {
// GetLastMerkleRoot should return the Merkle root with the highest height from your memory, or undefined if empty.
GetLastMerkleRoot() string
// SaveMerkleRoots should store newly synced merkle roots into your storage;
// NOTE: items are sorted in ascending order by block height.
SaveMerkleRoots(syncedMerkleRoots []models.MerkleRoot) error
}
type API struct {
url *url.URL
httpClient *resty.Client
}
func (a *API) MerkleRoots(ctx context.Context, merkleRootOpts ...queries.MerkleRootsQueryOption) (*queries.MerkleRootPage, error) {
var query queries.MerkleRootsQuery
for _, o := range merkleRootOpts {
o(&query)
}
params := queryparams.NewURLValues()
params.AddPair("batchSize", query.BatchSize)
params.AddPair("lastEvaluatedKey", query.LastEvaluatedKey)
var result queries.MerkleRootPage
_, err := a.httpClient.R().
SetContext(ctx).
SetResult(&result).
SetQueryParams(params.ParseToMap()).
Get(a.url.String())
if err != nil {
return nil, fmt.Errorf("HTTP response failure: %w", err)
}
return &result, nil
}
func NewAPI(url *url.URL, httpClient *resty.Client) *API {
return &API{
url: url.JoinPath(route),
httpClient: httpClient,
}
}
func (a *API) SyncMerkleRoots(ctx context.Context, repo MerkleRootsRepository) error {
lastEvaluatedKey := repo.GetLastMerkleRoot()
previousLastEvaluatedKey := lastEvaluatedKey
for {
select {
case <-ctx.Done():
return goclienterr.ErrSyncMerkleRootsTimeout
default:
// Query the MerkleRoots API
result, err := a.MerkleRoots(ctx, queries.MerkleRootsQueryWithLastEvaluatedKey(lastEvaluatedKey))
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return goclienterr.ErrSyncMerkleRootsTimeout
}
return fmt.Errorf("failed to fetch merkle roots from API: %w", err)
}
// Handle empty results
if len(result.Content) == 0 {
return nil
}
// Update the last evaluated key
lastEvaluatedKey = result.Page.LastEvaluatedKey
if lastEvaluatedKey != "" && previousLastEvaluatedKey == lastEvaluatedKey {
return goclienterr.ErrStaleLastEvaluatedKey
}
// Save fetched Merkle roots
err = repo.SaveMerkleRoots(result.Content)
if err != nil {
return fmt.Errorf("failed to save merkle roots: %w", err)
}
if lastEvaluatedKey == "" {
return nil
}
previousLastEvaluatedKey = lastEvaluatedKey
}
}
}