/
post_client.go
179 lines (158 loc) · 4.23 KB
/
post_client.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
package grpcserver
import (
"bytes"
"context"
"errors"
"fmt"
"time"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/common/types"
)
// postClient represents a connection to a PoST service.
//
// It uses the grpc interface of the node to send commands to the post service.
// Additionally if instructed it will start the post service and connect it to
// the node.
type postClient struct {
con chan<- postCommand
closed chan struct{}
}
func newPostClient(con chan<- postCommand) *postClient {
return &postClient{
con: con,
closed: make(chan struct{}),
}
}
func (pc *postClient) Info(ctx context.Context) (*types.PostInfo, error) {
req := &pb.NodeRequest{
Kind: &pb.NodeRequest_Metadata{
Metadata: &pb.MetadataRequest{},
},
}
resp, err := pc.send(ctx, req)
if err != nil {
return nil, err
}
metadataResp := resp.GetMetadata()
if metadataResp == nil {
return nil, fmt.Errorf("unexpected response of type: %T", resp.GetKind())
}
meta := metadataResp.GetMeta()
if meta == nil {
return nil, errors.New("post metadata is nil")
}
var nonce *types.VRFPostIndex
if meta.Nonce != nil {
nonce = new(types.VRFPostIndex)
*nonce = types.VRFPostIndex(meta.GetNonce())
}
return &types.PostInfo{
NodeID: types.BytesToNodeID(meta.GetNodeId()),
CommitmentATX: types.BytesToATXID(meta.GetCommitmentAtxId()),
Nonce: nonce,
NumUnits: meta.GetNumUnits(),
LabelsPerUnit: meta.GetLabelsPerUnit(),
}, nil
}
func (pc *postClient) Proof(ctx context.Context, challenge []byte) (*types.Post, *types.PostInfo, error) {
req := &pb.NodeRequest{
Kind: &pb.NodeRequest_GenProof{
GenProof: &pb.GenProofRequest{
Challenge: challenge,
},
},
}
var proofResp *pb.GenProofResponse
for {
resp, err := pc.send(ctx, req)
if err != nil {
return nil, nil, err
}
proofResp = resp.GetGenProof()
if proofResp == nil {
return nil, nil, fmt.Errorf("unexpected response of type: %T", resp.GetKind())
}
switch proofResp.GetStatus() {
case pb.GenProofStatus_GEN_PROOF_STATUS_ERROR:
return nil, nil, fmt.Errorf("error generating proof: %s", proofResp)
case pb.GenProofStatus_GEN_PROOF_STATUS_UNSPECIFIED:
return nil, nil, fmt.Errorf("unspecified error generating proof: %s", proofResp)
case pb.GenProofStatus_GEN_PROOF_STATUS_OK:
default:
return nil, nil, fmt.Errorf("unknown status: %s", proofResp)
}
if proofResp.GetProof() != nil {
break
}
select {
case <-ctx.Done():
return nil, nil, ctx.Err()
case <-time.After(2 * time.Second):
// TODO(mafa): make polling interval configurable
continue
}
}
proof := proofResp.GetProof()
metadata := proofResp.GetMetadata()
if metadata == nil {
return nil, nil, errors.New("proof metadata is nil")
}
if !bytes.Equal(metadata.GetChallenge(), challenge) {
return nil, nil, fmt.Errorf("unexpected challenge: %x", metadata.GetChallenge())
}
proofMeta := metadata.GetMeta()
if proofMeta == nil {
return nil, nil, errors.New("post metadata is nil")
}
post := &types.Post{
Nonce: proof.GetNonce(),
Indices: proof.GetIndices(),
Pow: proof.GetPow(),
}
var nonce *types.VRFPostIndex
if proofMeta.Nonce != nil {
nonce = new(types.VRFPostIndex)
*nonce = types.VRFPostIndex(proofMeta.GetNonce())
}
postInfo := &types.PostInfo{
NodeID: types.BytesToNodeID(proofMeta.NodeId),
CommitmentATX: types.BytesToATXID(proofMeta.CommitmentAtxId),
Nonce: nonce,
NumUnits: proofMeta.GetNumUnits(),
LabelsPerUnit: proofMeta.GetLabelsPerUnit(),
}
return post, postInfo, nil
}
func (pc *postClient) send(ctx context.Context, req *pb.NodeRequest) (*pb.ServiceResponse, error) {
resp := make(chan *pb.ServiceResponse, 1)
cmd := postCommand{
req: req,
resp: resp,
}
// send command
select {
case <-pc.closed:
return nil, activation.ErrPostClientClosed
case <-ctx.Done():
return nil, ctx.Err()
case pc.con <- cmd:
}
// receive response
select {
case <-pc.closed:
return nil, activation.ErrPostClientClosed
case <-ctx.Done():
return nil, ctx.Err()
case resp := <-resp:
return resp, nil
}
}
func (pc *postClient) Close() error {
select {
case <-pc.closed:
default:
close(pc.closed)
}
return nil
}