-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpool.go
More file actions
294 lines (266 loc) · 9.03 KB
/
Copy pathpool.go
File metadata and controls
294 lines (266 loc) · 9.03 KB
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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"os"
"strings"
"text/template"
"time"
"github.com/OpenPeeDeeP/xdg"
"github.com/dgraph-io/badger"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/vipnode/vipnode/ethnode"
"github.com/vipnode/vipnode/internal/pretty"
ws "github.com/vipnode/vipnode/jsonrpc2/ws/gorilla"
"github.com/vipnode/vipnode/pool"
"github.com/vipnode/vipnode/pool/balance"
"github.com/vipnode/vipnode/pool/payment"
"github.com/vipnode/vipnode/pool/status"
"github.com/vipnode/vipnode/pool/store"
badgerStore "github.com/vipnode/vipnode/pool/store/badger"
memoryStore "github.com/vipnode/vipnode/pool/store/memory"
"golang.org/x/crypto/acme/autocert"
)
const healthTimeout = time.Second * 5
// findDataDir returns a valid data dir, will create it if it doesn't
// exist.
func findDataDir(overridePath string) (string, error) {
path := overridePath
if path == "" {
path = xdg.New("vipnode", "pool").DataHome()
}
err := os.MkdirAll(path, 0700)
return path, err
}
func runPool(options Options) error {
var storeDriver store.Store
switch options.Pool.Store {
case "memory":
storeDriver = memoryStore.New()
defer storeDriver.Close()
case "persist":
fallthrough
case "badger":
dir, err := findDataDir(options.Pool.DataDir)
if err != nil {
return err
}
badgerOpts := badger.DefaultOptions
badgerOpts.Dir = dir
badgerOpts.ValueDir = dir
storeDriver, err = badgerStore.Open(badgerOpts)
if err != nil {
return err
}
defer storeDriver.Close()
logger.Infof("Persistent store using badger backend: %s", dir)
default:
return errors.New("storage driver not implemented")
}
balanceStore := store.BalanceStore(storeDriver)
var settleHandler payment.SettleHandler
var depositGetter func(ctx context.Context) (*big.Int, error)
if options.Pool.Contract.Addr != "" {
// Payment contract implements NodeBalanceStore used by the balance
// manager, but with contract awareness.
contractPath, err := url.Parse(options.Pool.Contract.Addr)
if err != nil {
return err
}
contractAddr := common.HexToAddress(contractPath.Hostname())
network := contractPath.Scheme
ethclient, err := ethclient.Dial(options.Pool.Contract.RPC)
if err != nil {
return err
}
// Confirm we're on the right network.
// Note: The contract network/node can be independent of the --restrict-network setting
gotNetwork, err := ethclient.NetworkID(context.Background())
if err != nil {
return err
}
if networkID := ethnode.NetworkID(int(gotNetwork.Int64())); !networkID.Is(network) {
return ErrExplain{
errors.New("ethereum network mismatch for payment contract"),
fmt.Sprintf("Contract is on %q while the Contact RPC is a %q node. Please provide a Contract RPC on the same network as the contract.", network, networkID),
}
}
var transactOpts *bind.TransactOpts
if options.Pool.Contract.KeyStore != "" {
transactOpts, err = unlockTransactor(options.Pool.Contract.KeyStore)
if err != nil {
return ErrExplain{
err,
"Failed to unlock the keystore for the contract operator wallet. Make sure the path is correct and the decryption password is set in the `KEYSTORE_PASSPHRASE` environment variable.",
}
}
}
if transactOpts == nil {
logger.Warningf("Contract payment starting in read-only mode because --contract-keystore was not set. Withdraw and settlement attempts will fail.")
}
contract, err := payment.ContractPayment(storeDriver, contractAddr, ethclient, transactOpts)
if err != nil {
if err, ok := err.(payment.AddressMismatchError); ok {
return ErrExplain{
err,
"Contract keystore must match the wallet of the contract operator. Make sure you're providing the correct keystore.",
}
}
return err
}
balanceStore = contract
settleHandler = contract.OpSettle
depositGetter = func(ctx context.Context) (*big.Int, error) {
r, err := ethclient.PendingBalanceAt(ctx, contractAddr)
if err != nil {
// Try again in case the connection dropped
logger.Warningf("PoolStatus: ethclient.PendingBalanceAt failed, retrying: %s", err)
r, err = ethclient.PendingBalanceAt(ctx, contractAddr)
}
if err != nil {
logger.Errorf("PoolStatus: ethclient.PendingBalanceAt failed twice: %s", err)
}
return r, err
}
}
// Setup balance manager
creditPerInterval, err := pretty.ParseEther(options.Pool.Contract.Price)
if err != nil {
return fmt.Errorf("failed to parse contract price: %s", err)
}
balanceManager := balance.PayPerInterval(
balanceStore,
time.Minute*1, // Interval
creditPerInterval,
)
if options.Pool.Contract.MinBalance != "off" {
minBalance, err := pretty.ParseEther(options.Pool.Contract.MinBalance)
if err != nil {
return fmt.Errorf("failed to parse contract minimum balance: %s", err)
}
balanceManager.MinBalance = minBalance
}
// Setup welcome message template
var welcomeTmpl *template.Template
if welcomeMsg := options.Pool.Contract.Welcome; welcomeMsg != "" {
welcomeTmpl, err = template.New("vipnode_welcome").Parse(welcomeMsg)
if err != nil {
return err
}
}
var networkID ethnode.NetworkID
if options.Pool.RestrictNetwork != "" {
networkID = ethnode.ParseNetwork(options.Pool.RestrictNetwork)
if networkID == ethnode.UnknownNetwork {
return ErrExplain{errors.New("unknown network"), `Failed to parse network ID provided to --restrict-network`}
}
}
p := pool.New(storeDriver, balanceManager)
p.MaxRequestHosts = options.Pool.MaxRequestHosts
p.Version = fmt.Sprintf("vipnode/pool/%s", Version)
if welcomeTmpl != nil {
p.ClientMessager = func(nodeID string) string {
var buf bytes.Buffer
err := welcomeTmpl.Execute(&buf, struct {
NodeID string
}{
NodeID: nodeID,
})
if err != nil {
// TODO: Should this be recoverable? What conditions would cause this?
logger.Errorf("ClientMessager failed: %s", err)
}
return buf.String()
}
}
p.RestrictNetwork = networkID
p.BlockNumberProvider = func(network ethnode.NetworkID) (uint64, error) {
// TODO: Does it make sense also fetching this from an external service? Eg: Infura's eth_blockNumber?
if network != p.RestrictNetwork {
return 0, fmt.Errorf("block number provider does not support network: %s", network)
}
stats, err := p.Store.Stats()
if err != nil {
return 0, err
}
return stats.LatestBlockNumber, nil
}
handler := &server{
ws: &ws.Upgrader{},
header: http.Header{},
onDisconnect: p.CloseRemote,
}
if options.Pool.AllowOrigin != "" {
handler.header.Set("Access-Control-Allow-Origin", options.Pool.AllowOrigin)
}
if err := handler.Register("vipnode_", p, "connect", "disconnect", "ping", "update", "peer", "client", "host"); err != nil {
return err
}
// Pool payment management API (optional)
payment := &payment.PaymentService{
NonceStore: storeDriver,
AccountStore: storeDriver,
BalanceStore: balanceStore, // Proxy smart contract store if available
WithdrawFee: func(amount *big.Int) *big.Int {
// TODO: Adjust fee dynamically based on gas price?
fee := big.NewInt(2500000000000000) // 0.0025 ETH
return amount.Sub(amount, fee)
},
WithdrawMin: big.NewInt(5000000000000000), // 0.005 ETH
Settle: settleHandler,
}
if err := handler.Register("pool_", payment); err != nil {
return err
}
// Pool status dashboard API
dashboard := &status.PoolStatus{
Store: storeDriver,
GetTotalDeposit: depositGetter,
TimeStarted: time.Now(),
Version: Version,
CacheDuration: time.Minute * 1,
}
if err := handler.Register("pool_", dashboard); err != nil {
return err
}
// PoolStatus-based healthcheck for our HTTP handler
handler.healthCheck = func(w io.Writer) error {
ctx, cancel := context.WithTimeout(context.Background(), healthTimeout)
status, err := dashboard.Status(ctx)
defer cancel()
if err != nil {
return err
}
return json.NewEncoder(w).Encode(status)
}
if options.Pool.TLSHost != "" {
if !strings.HasSuffix(":443", options.Pool.Bind) {
logger.Warningf("Ignoring --bind value (%q) because it's not 443 and --tlshost is set.", options.Pool.Bind)
}
logger.Infof("Starting pool (version %s), acquiring ACME certificate and listening on: https://%s", Version, options.Pool.TLSHost)
err := http.Serve(autocert.NewListener(options.Pool.TLSHost), handler)
if strings.HasSuffix(err.Error(), "bind: permission denied") {
err = ErrExplain{err, "Hosting a pool with autocert requires CAP_NET_BIND_SERVICE capability permission to bind on low-numbered ports. See: https://superuser.com/questions/710253/allow-non-root-process-to-bind-to-port-80-and-443/892391"}
}
return err
}
logger.Infof("Starting pool (version %s), listening on: %s", Version, options.Pool.Bind)
return http.ListenAndServe(options.Pool.Bind, handler)
}
func unlockTransactor(keystorePath string) (*bind.TransactOpts, error) {
pw := os.Getenv("KEYSTORE_PASSPHRASE")
r, err := os.Open(keystorePath)
if err != nil {
return nil, err
}
return bind.NewTransactor(r, pw)
}