-
Notifications
You must be signed in to change notification settings - Fork 211
/
poet.go
203 lines (172 loc) · 6.09 KB
/
poet.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package activation
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/hashicorp/go-retryablehttp"
rpcapi "github.com/spacemeshos/poet/release/proto/go/rpc/api/v1"
"github.com/spacemeshos/poet/shared"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)
var (
ErrNotFound = errors.New("not found")
ErrUnavailable = errors.New("unavailable")
)
// HTTPPoetClient implements PoetProvingServiceClient interface.
type HTTPPoetClient struct {
baseURL *url.URL
poetServiceID types.PoetServiceID
client *retryablehttp.Client
}
func defaultPoetClientFunc(address string, cfg PoetConfig) (PoetProvingServiceClient, error) {
return NewHTTPPoetClient(address, cfg)
}
func checkRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
if retry, err := retryablehttp.DefaultRetryPolicy(ctx, resp, err); retry || err != nil {
return retry, err
}
if resp.StatusCode == http.StatusNotFound {
return true, nil
}
return false, nil
}
type PoetClientOpts func(*HTTPPoetClient)
func withCustomHttpClient(client *http.Client) PoetClientOpts {
return func(c *HTTPPoetClient) {
c.client.HTTPClient = client
}
}
// NewHTTPPoetClient returns new instance of HTTPPoetClient connecting to the specified url.
func NewHTTPPoetClient(baseUrl string, cfg PoetConfig, opts ...PoetClientOpts) (*HTTPPoetClient, error) {
client := &retryablehttp.Client{
RetryMax: 10,
RetryWaitMin: cfg.CycleGap / 100,
RetryWaitMax: cfg.CycleGap / 10,
Backoff: retryablehttp.LinearJitterBackoff,
CheckRetry: checkRetry,
}
baseURL, err := url.Parse(baseUrl)
if err != nil {
return nil, fmt.Errorf("parsing address: %w", err)
}
if baseURL.Scheme == "" {
baseURL.Scheme = "http"
}
poetClient := &HTTPPoetClient{
baseURL: baseURL,
client: client,
}
for _, opt := range opts {
opt(poetClient)
}
return poetClient, nil
}
// Start is an administrative endpoint of the proving service that tells it to start. This is mostly done in tests,
// since it requires administrative permissions to the proving service.
func (c *HTTPPoetClient) Start(ctx context.Context, gatewayAddresses []string) error {
reqBody := rpcapi.StartRequest{GatewayAddresses: gatewayAddresses}
if err := c.req(ctx, http.MethodPost, "/v1/start", &reqBody, nil); err != nil {
return fmt.Errorf("starting poet: %w", err)
}
return nil
}
// Submit registers a challenge in the proving service current open round.
func (c *HTTPPoetClient) Submit(ctx context.Context, challenge []byte, signature []byte) (*types.PoetRound, error) {
request := rpcapi.SubmitRequest{
Challenge: challenge,
Signature: signature,
}
resBody := rpcapi.SubmitResponse{}
if err := c.req(ctx, http.MethodPost, "/v1/submit", &request, &resBody); err != nil {
return nil, fmt.Errorf("submitting challenge: %w", err)
}
roundEnd := time.Time{}
if resBody.RoundEnd != nil {
roundEnd = time.Now().Add(resBody.RoundEnd.AsDuration())
}
if len(resBody.Hash) != types.Hash32Length {
return nil, fmt.Errorf("invalid hash len (%d instead of %d)", len(resBody.Hash), types.Hash32Length)
}
hash := types.Hash32{}
hash.SetBytes(resBody.Hash)
return &types.PoetRound{ID: resBody.RoundId, ChallengeHash: hash, End: types.RoundEnd(roundEnd)}, nil
}
// PoetServiceID returns the public key of the PoET proving service.
func (c *HTTPPoetClient) PoetServiceID(ctx context.Context) (types.PoetServiceID, error) {
if c.poetServiceID != nil {
return c.poetServiceID, nil
}
resBody := rpcapi.GetInfoResponse{}
if err := c.req(ctx, http.MethodGet, "/v1/info", nil, &resBody); err != nil {
return nil, fmt.Errorf("getting poet ID: %w", err)
}
c.poetServiceID = types.PoetServiceID(resBody.ServicePubkey)
return c.poetServiceID, nil
}
// Proof implements PoetProvingServiceClient.
func (c *HTTPPoetClient) Proof(ctx context.Context, roundID string) (*types.PoetProofMessage, error) {
resBody := rpcapi.GetProofResponse{}
if err := c.req(ctx, http.MethodGet, fmt.Sprintf("/v1/proofs/%s", roundID), nil, &resBody); err != nil {
return nil, fmt.Errorf("getting proof: %w", err)
}
proof := types.PoetProofMessage{
PoetProof: types.PoetProof{
MerkleProof: shared.MerkleProof{
Root: resBody.Proof.GetProof().GetRoot(),
ProvenLeaves: resBody.Proof.GetProof().GetProvenLeaves(),
ProofNodes: resBody.Proof.GetProof().GetProofNodes(),
},
Members: resBody.Proof.GetMembers(),
LeafCount: resBody.Proof.GetLeaves(),
},
PoetServiceID: resBody.Pubkey,
RoundID: roundID,
}
if c.poetServiceID == nil {
c.poetServiceID = proof.PoetServiceID
}
return &proof, nil
}
func (c *HTTPPoetClient) req(ctx context.Context, method string, path string, reqBody proto.Message, resBody proto.Message) error {
jsonReqBody, err := protojson.Marshal(reqBody)
if err != nil {
return fmt.Errorf("marshaling request body: %w", err)
}
req, err := retryablehttp.NewRequestWithContext(ctx, method, c.baseURL.JoinPath(path).String(), jsonReqBody)
if err != nil {
return fmt.Errorf("creating HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
res, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("doing request: %w", err)
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("reading response body (%w)", err)
}
log.GetLogger().WithContext(ctx).With().Debug("response from poet", log.String("status", res.Status), log.String("body", string(data)))
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
return fmt.Errorf("%w: response status code: %s, body: %s", ErrNotFound, res.Status, string(data))
case http.StatusServiceUnavailable:
return fmt.Errorf("%w: response status code: %s, body: %s", ErrUnavailable, res.Status, string(data))
default:
return fmt.Errorf("unrecognized error: status code: %s, body: %s", res.Status, string(data))
}
if resBody != nil {
if err := protojson.Unmarshal(data, resBody); err != nil {
return fmt.Errorf("decoding response body to proto: %w", err)
}
}
return nil
}