This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
299 lines (277 loc) · 8.44 KB
/
server.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package muse
import (
"context"
"crypto/ed25519"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
"time"
"go.sia.tech/siad/modules"
"lukechampine.com/frand"
"lukechampine.com/shard"
"lukechampine.com/us/hostdb"
"lukechampine.com/us/renter"
"lukechampine.com/us/renter/proto"
)
func writeJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json")
// encode nil slices as [] instead of null
if val := reflect.ValueOf(v); val.Kind() == reflect.Slice && val.Len() == 0 {
w.Write([]byte("[]\n"))
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
enc.Encode(v)
}
type server struct {
hostSets map[string][]hostdb.HostPublicKey
dir string
wallet proto.Wallet
tpool proto.TransactionPool
shard *shard.Client
mu sync.Mutex
utxoMu sync.Mutex // separate mutex for utxos, preventing reuse
}
func (s *server) handleForm(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
var rf RequestForm
if err := json.NewDecoder(req.Body).Decode(&rf); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
start := time.Now()
log.Println("resolving a host key:", rf.HostKey)
hostAddr, err := s.shard.ResolveHostKey(rf.HostKey)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rf.Settings.NetAddress = hostAddr
host := hostdb.ScannedHost{
HostSettings: rf.Settings,
PublicKey: rf.HostKey,
}
key := ed25519.NewKeyFromSeed(frand.Bytes(32))
log.Println("trying to lock utxoMu:", rf.HostKey, time.Since(start))
s.utxoMu.Lock()
log.Println("forming a contract:", rf.HostKey, time.Since(start))
rev, txnSet, err := proto.FormContract(s.wallet, s.tpool, key, host, rf.Funds, rf.StartHeight, rf.EndHeight)
if err != nil {
log.Println("release utxoMu:", rf.HostKey, time.Since(start))
s.utxoMu.Unlock()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// submit txnSet to tpool
//
// NOTE: if the tpool rejects the transaction, we log the error, but
// still treat the contract as valid. The host has signed the
// transaction, so presumably they already submitted it to their own
// tpool without error, and intend to honor the contract. Our tpool
// *shouldn't* reject the transaction, but it might if we desync from
// the network somehow.
log.Println("submitting transaction set:", rf.HostKey, time.Since(start))
submitErr := s.tpool.AcceptTransactionSet(txnSet)
log.Println("release utxoMu:", rf.HostKey, time.Since(start))
s.utxoMu.Unlock()
if submitErr != nil && submitErr != modules.ErrDuplicateTransactionSet {
log.Println("WARN: contract transaction was not accepted", submitErr)
}
c := Contract{
Contract: renter.Contract{
HostKey: rev.HostKey(),
ID: rev.ID(),
RenterKey: key,
},
HostAddress: host.NetAddress,
EndHeight: rf.EndHeight,
}
writeJSON(w, c)
log.Println("forming a contract finishes:", rf.HostKey, time.Since(start))
}
func (s *server) handleRenew(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
var rf RequestRenew
if err := json.NewDecoder(req.Body).Decode(&rf); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
start := time.Now()
log.Println("resolving a host key:", rf.HostKey)
hostAddr, err := s.shard.ResolveHostKey(rf.HostKey)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rf.Settings.NetAddress = hostAddr
host := hostdb.ScannedHost{
PublicKey: rf.HostKey,
HostSettings: rf.Settings,
}
log.Println("trying to lock utxoMu:", rf.HostKey, time.Since(start))
s.utxoMu.Lock()
log.Println("renewing a contract:", rf.HostKey, time.Since(start))
rev, txnSet, err := proto.RenewContract(s.wallet, s.tpool, rf.ID, rf.RenterKey, host, rf.Funds, rf.StartHeight, rf.EndHeight)
if err != nil {
log.Println("release utxoMu:", rf.HostKey, time.Since(start))
s.utxoMu.Unlock()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// submit txnSet to tpool (see handleForm)
log.Println("submitting transaction set:", rf.HostKey, time.Since(start))
submitErr := s.tpool.AcceptTransactionSet(txnSet)
s.utxoMu.Unlock()
if submitErr != nil && submitErr != modules.ErrDuplicateTransactionSet {
log.Println("WARN: contract transaction was not accepted", submitErr)
}
c := Contract{
Contract: renter.Contract{
HostKey: rev.HostKey(),
ID: rev.ID(),
RenterKey: rf.RenterKey,
},
HostAddress: rf.Settings.NetAddress,
EndHeight: rf.EndHeight,
}
writeJSON(w, c)
log.Println("renewing a contract finishes:", rf.HostKey, time.Since(start))
}
func (s *server) handleHostSets(w http.ResponseWriter, req *http.Request) {
setName := strings.TrimPrefix(req.URL.Path, "/hostsets/")
if strings.Contains(setName, "/") {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
switch req.Method {
case http.MethodGet:
if setName == "" {
s.mu.Lock()
setNames := make([]string, 0, len(s.hostSets))
for name := range s.hostSets {
setNames = append(setNames, name)
}
s.mu.Unlock()
sort.Strings(setNames)
writeJSON(w, setNames)
} else {
if setName == "" {
http.Error(w, "No host set name provided", http.StatusBadRequest)
return
}
s.mu.Lock()
set, ok := s.hostSets[setName]
hostKeys := append([]hostdb.HostPublicKey(nil), set...)
s.mu.Unlock()
if !ok {
http.Error(w, "No record of that host set", http.StatusBadRequest)
return
}
writeJSON(w, hostKeys)
}
case http.MethodPut:
if setName == "" {
http.Error(w, "No host set name provided", http.StatusBadRequest)
return
}
var hostKeys []hostdb.HostPublicKey
if err := json.NewDecoder(req.Body).Decode(&hostKeys); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sort.Slice(hostKeys, func(i, j int) bool {
return hostKeys[i] < hostKeys[j]
})
s.mu.Lock()
s.hostSets[setName] = hostKeys
if len(hostKeys) == 0 {
delete(s.hostSets, setName)
}
hostSetsJSON, _ := json.MarshalIndent(s.hostSets, "", " ")
s.mu.Unlock()
err := ioutil.WriteFile(filepath.Join(s.dir, "hostSets.json"), hostSetsJSON, 0660)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
func (s *server) handleScan(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
var rs RequestScan
if err := json.NewDecoder(req.Body).Decode(&rs); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
hostAddr, err := s.shard.ResolveHostKey(rs.HostKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
host, err := hostdb.Scan(ctx, hostAddr, rs.HostKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, host.HostSettings)
}
// NewServer returns an HTTP handler that serves the muse API.
func NewServer(dir string, wallet proto.Wallet, tpool proto.TransactionPool, shardAddr string) (http.Handler, error) {
srv := &server{
wallet: wallet,
tpool: tpool,
shard: shard.NewClient(shardAddr),
dir: dir,
}
// load host sets
hostSetsJSON, err := ioutil.ReadFile(filepath.Join(dir, "hostSets.json"))
if os.IsNotExist(err) {
srv.hostSets = make(map[string][]hostdb.HostPublicKey)
} else {
if err != nil {
return nil, err
}
if err := json.Unmarshal(hostSetsJSON, &srv.hostSets); err != nil {
return nil, err
}
}
mux := http.NewServeMux()
mux.HandleFunc("/form", srv.handleForm)
mux.HandleFunc("/renew", srv.handleRenew)
mux.HandleFunc("/hostsets/", srv.handleHostSets)
mux.HandleFunc("/scan", srv.handleScan)
// shard proxy
shardURL, err := url.Parse(shardAddr)
if err != nil {
return nil, err
}
mux.Handle("/shard/", &httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Scheme = shardURL.Scheme
req.URL.Host = shardURL.Host
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/shard")
}})
return mux, nil
}