forked from bitcoinfees/feesim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
197 lines (174 loc) · 4.6 KB
/
service.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
package main
import (
"fmt"
"net"
"net/http"
"github.com/gorilla/rpc"
jsonrpc "github.com/gorilla/rpc/json"
"github.com/rcrowley/go-metrics"
col "github.com/bitcoinfees/feesim/collect"
"github.com/bitcoinfees/feesim/sim"
)
type Service struct {
FeeSim *FeeSim
DLog *DebugLog
Cfg config
}
func (s *Service) ListenAndServe() error {
var methods = map[string]string{
"stop": "Service.Stop",
"status": "Service.Status",
"estimatefee": "Service.EstimateFee",
"predictscores": "Service.PredictScores",
"txrate": "Service.TxRate",
"caprate": "Service.CapRate",
"mempoolsize": "Service.MempoolSize",
"pause": "Service.Pause",
"unpause": "Service.Unpause",
"setdebug": "Service.SetDebug",
"config": "Service.Config",
"metrics": "Service.Metrics",
"blocksource": "Service.BlockSource",
"txsource": "Service.TxSource",
"mempoolstate": "Service.MempoolState",
}
srv := rpc.NewServer()
srv.RegisterCodec(jsonrpc.NewCodec(), "application/json")
srv.RegisterService(s, "")
srv.RegisterCustomNames(methods)
http.Handle("/", srv)
addr := net.JoinHostPort(s.Cfg.AppRPC.Host, s.Cfg.AppRPC.Port)
s.DLog.Logger.Println("RPC server listening on", addr)
return http.ListenAndServe(addr, nil)
}
func (s *Service) Stop(r *http.Request, args *struct{}, reply *struct{}) error {
go s.FeeSim.Stop()
return nil
}
func (s *Service) Status(r *http.Request, args *struct{}, reply *map[string]string) error {
*reply = s.FeeSim.Status()
return nil
}
// NOTE: There's no fail-safe max value, take care.
func (s *Service) EstimateFee(r *http.Request, args *int, reply *interface{}) error {
result, err := s.FeeSim.Result()
if err != nil {
return err
}
if *args < 0 {
return fmt.Errorf("argument must be >= 0")
}
if *args > len(result) {
return fmt.Errorf("MaxBlockConfirms=%d exceeded", len(result))
}
// Convert from satoshis to BTC, to conform to Bitcoin Core's estimatefee API
resultBTC := make([]float64, len(result))
for i, satoshis := range result {
if satoshis == -1 {
resultBTC[i] = -1
} else {
resultBTC[i] = float64(satoshis) / coin
}
}
if *args == 0 {
*reply = resultBTC
} else {
*reply = resultBTC[*args-1]
}
return nil
}
func (s *Service) PredictScores(r *http.Request, args *struct{}, reply *map[string][]float64) error {
attained, exceeded, err := s.FeeSim.PredictScores()
if err != nil {
return err
}
scores := make(map[string][]float64)
scores["attained"] = attained
scores["exceeded"] = exceeded
*reply = scores
return nil
}
func (s *Service) TxRate(r *http.Request, args *int, reply *sim.MonotonicFn) error {
n := *args
if n <= 0 {
n = 20
}
txsource, err := s.FeeSim.TxSource()
if err != nil {
return err
}
*reply = txsource.RateFn().Approx(n)
return nil
}
func (s *Service) CapRate(r *http.Request, args *int, reply *sim.MonotonicFn) error {
n := *args
if n <= 0 {
n = 20
}
blocksource, err := s.FeeSim.BlockSource()
if err != nil {
return err
}
*reply = blocksource.RateFn().Approx(n)
return nil
}
func (s *Service) MempoolSize(r *http.Request, args *int, reply *sim.MonotonicFn) error {
n := *args
if n <= 0 {
n = 20
}
state := s.FeeSim.State()
if state == nil {
return fmt.Errorf("mempool not available")
}
*reply = state.SizeFn().Approx(n)
return nil
}
func (s *Service) Pause(r *http.Request, args *struct{}, reply *struct{}) error {
s.FeeSim.Pause(true)
return nil
}
func (s *Service) Unpause(r *http.Request, args *struct{}, reply *struct{}) error {
s.FeeSim.Pause(false)
return nil
}
func (s *Service) SetDebug(r *http.Request, args *bool, reply *bool) error {
s.DLog.SetDebug(*args)
*reply = *args
return nil
}
func (s *Service) Config(r *http.Request, args *struct{}, reply *interface{}) error {
c := s.Cfg
// Hide the password just in case
c.BitcoinRPC.Password = "********"
*reply = c
return nil
}
func (s *Service) Metrics(r *http.Request, args *struct{}, reply *metrics.Registry) error {
*reply = metrics.DefaultRegistry
return nil
}
func (s *Service) BlockSource(r *http.Request, args *struct{}, reply *sim.BlockSource) error {
blocksource, err := s.FeeSim.BlockSource()
if err != nil {
return err
}
*reply = blocksource
return nil
}
func (s *Service) TxSource(r *http.Request, args *struct{}, reply *sim.TxSource) error {
txsource, err := s.FeeSim.TxSource()
if err != nil {
return err
}
*reply = txsource
return nil
}
func (s *Service) MempoolState(r *http.Request, args *struct{}, reply **col.MempoolState) error {
state := s.FeeSim.State()
if state == nil {
return fmt.Errorf("mempool not available")
}
*reply = state
return nil
}