/
http.go
163 lines (140 loc) 路 4.32 KB
/
http.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
package client
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"net/http"
"time"
"github.com/drand/drand/beacon"
"github.com/drand/drand/key"
"github.com/drand/drand/log"
drand "github.com/drand/drand/protobuf/drand"
json "github.com/nikkolasg/hexjson"
)
// HTTPGetter is an interface for the exercised methods of an `http.Client`,
// or equivalent alternative.
type HTTPGetter interface {
Do(req *http.Request) (*http.Response, error)
Get(url string) (resp *http.Response, err error)
}
// NewHTTPClient creates a new client pointing to an HTTP endpoint
func NewHTTPClient(url string, groupHash []byte, client HTTPGetter) (Client, error) {
if client == nil {
client = &http.Client{}
}
c := &httpClient{
root: url,
client: client,
l: log.DefaultLogger,
}
group, err := c.FetchGroupInfo(groupHash)
if err != nil {
return nil, err
}
c.group = group
return c, nil
}
// NewHTTPClientWithGroup constructs an http client when the group parameters are already known.
func NewHTTPClientWithGroup(url string, group *key.Group, client HTTPGetter) (Client, error) {
if client == nil {
client = &http.Client{}
}
c := &httpClient{
root: url,
group: group,
client: client,
l: log.DefaultLogger,
}
return c, nil
}
// httpClient implements Client through http requests to a Drand relay.
type httpClient struct {
root string
client HTTPGetter
group *key.Group
l log.Logger
}
// FetchGroupInfo attempts to initialize an httpClient when
// it does not know the full group paramters for a drand group.
func (h *httpClient) FetchGroupInfo(groupHash []byte) (*key.Group, error) {
if h.group != nil {
return h.group, nil
}
// fetch the `Group` to validate connectivity.
groupResp, err := h.client.Get(fmt.Sprintf("%s/group", h.root))
if err != nil {
return nil, err
}
protoGrp := drand.GroupPacket{}
if err := json.NewDecoder(groupResp.Body).Decode(&protoGrp); err != nil {
return nil, err
}
grp, err := key.GroupFromProto(&protoGrp)
if err != nil {
return nil, err
}
if grp.PublicKey == nil {
return nil, fmt.Errorf("Group does not have a valid key for validation")
}
if groupHash == nil {
h.l.Warn("http_client", "instantiated without trustroot", "groupHash", hex.EncodeToString(grp.Hash()))
}
if groupHash != nil && !bytes.Equal(grp.Hash(), groupHash) {
return nil, fmt.Errorf("%s does not advertise the expected drand group (%x vs %x)", h.root, grp.Hash(), groupHash)
}
return grp, nil
}
// Implement textMarshaller
func (h *httpClient) MarshalText() ([]byte, error) {
return json.Marshal(h)
}
// RandomData holds the full random response from the server, including data needed
// for validation.
type RandomData struct {
Rnd uint64 `json:"round,omitempty"`
Random []byte `json:"randomness,omitempty"`
Signature []byte `json:"signature,omitempty"`
PreviousSignature []byte `json:"previous_signature,omitempty"`
}
// Round provides access to the round associatted with this random data.
func (r *RandomData) Round() uint64 {
return r.Rnd
}
// Randomness exports the randomness
func (r *RandomData) Randomness() []byte {
return r.Random
}
// Get returns a the randomness at `round` or an error.
func (h *httpClient) Get(ctx context.Context, round uint64) (Result, error) {
randResponse, err := h.client.Get(fmt.Sprintf("%s/public/%d", h.root, round))
if err != nil {
return nil, err
}
randResp := RandomData{}
if err := json.NewDecoder(randResponse.Body).Decode(&randResp); err != nil {
return nil, err
}
if len(randResp.Signature) == 0 || len(randResp.PreviousSignature) == 0 {
return nil, fmt.Errorf("insufficent response")
}
b := beacon.Beacon{
PreviousSig: randResp.PreviousSignature,
Round: randResp.Rnd,
Signature: randResp.Signature,
}
if err := beacon.VerifyBeacon(h.group.PublicKey.Key(), &b); err != nil {
h.l.Warn("http_client", "failed to verify value", "err", err)
return nil, err
}
return &randResp, nil
}
// Watch returns new randomness as it becomes available.
func (h *httpClient) Watch(ctx context.Context) <-chan Result {
return pollingWatcher(ctx, h, h.group, h.l)
}
// RoundAt will return the most recent round of randomness that will be available
// at time for the current client.
func (h *httpClient) RoundAt(time time.Time) uint64 {
return beacon.CurrentRound(time.Unix(), h.group.Period, h.group.GenesisTime)
}