-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbest_quote_test.go
281 lines (227 loc) · 12.5 KB
/
best_quote_test.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
package minercraft
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonicpow/go-minercraft/v2/apis/mapi"
)
// mockHTTPValidBestQuote for mocking requests
type mockHTTPValidBestQuote struct{}
// Do is a mock http request
func (m *mockHTTPValidBestQuote) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T21:26:17.410Z\",\"expiryTime\":\"2020-10-09T21:36:17.410Z\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"0000000000000000035c5f8c0294802a01e500fa7b95337963bb3640da3bd565\",\"currentHighestBlockHeight\":656169,\"minerReputation\":null,\"fees\":[{\"id\":1,\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":400,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"id\":2,\"feeType\":\"data\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":225,\"bytes\":1000}}]}",
"signature": "3045022100eed49f6bf75d8f975f581271e3df658fbe8ec67e6301ea8fc25a72d18c92e30e022056af253f0d24db6a8fde4e2c1ee95e7a5ecf2c7cdc93246f8328c9e0ca582fc4",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:08:26.236Z\",\"expiryTime\":\"2020-10-09T22:18:26.236Z\",\"minerId\":\"0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087\",\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":400,\"bytes\":1000},\"relayFee\":{\"satoshis\":100,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":430,\"bytes\":1000},\"relayFee\":{\"satoshis\":110,\"bytes\":1000}}]}",
"signature": "3044022011f90db2661726eb2659c3447ccaa9fd3368194f87d5d86a23e673c45d5d714502200c51eb600e3370b49d759aa4d441000286937b0803037a1d6de4c5a5c559d74c",
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:09:04.433Z\",\"expiryTime\":\"2020-10-09T22:19:04.433Z\",\"minerId\":null,\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":420,\"bytes\":1000},\"relayFee\":{\"satoshis\":150,\"bytes\":1000}}]}",
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:09:04.433Z\",\"expiryTime\":\"2020-10-09T22:19:04.433Z\",\"minerId\":null,\"currentHighestBlockHash\":\"0000000000000000101c34c7cabadbff321f125fac9ba3c2b1294c4d81085f4a\",\"currentHighestBlockHeight\":713780,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}}]}",
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
// Default is valid
return resp, nil
}
// mockHTTPBadRate for mocking requests
type mockHTTPBadRate struct{}
// Do is a mock http request
func (m *mockHTTPBadRate) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T21:26:17.410Z\",\"expiryTime\":\"2020-10-09T21:36:17.410Z\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"0000000000000000035c5f8c0294802a01e500fa7b95337963bb3640da3bd565\",\"currentHighestBlockHeight\":656169,\"minerReputation\":null,\"fees\":[{\"id\":1,\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}},{\"id\":2,\"feeType\":\"data\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}}]}",
"signature": "3045022100eed49f6bf75d8f975f581271e3df658fbe8ec67e6301ea8fc25a72d18c92e30e022056af253f0d24db6a8fde4e2c1ee95e7a5ecf2c7cdc93246f8328c9e0ca582fc4",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:08:26.236Z\",\"expiryTime\":\"2020-10-09T22:18:26.236Z\",\"minerId\":\"0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087\",\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}}]}",
"signature": "3044022011f90db2661726eb2659c3447ccaa9fd3368194f87d5d86a23e673c45d5d714502200c51eb600e3370b49d759aa4d441000286937b0803037a1d6de4c5a5c559d74c",
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}
if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
// Default is valid
return resp, nil
}
// mockHTTPBestQuoteAllFailed for mocking requests
type mockHTTPBestQuoteAllFailed struct{}
// Do is a mock http request
func (m *mockHTTPBestQuoteAllFailed) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}
// Default is valid
return resp, nil
}
// TestClient_BestQuote tests the method BestQuote()
func TestClient_BestQuote(t *testing.T) {
t.Run("get a valid best quote", func(t *testing.T) {
// Create a client
client := newTestClient(&mockHTTPValidBestQuote{})
// Create a req
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
require.NoError(t, err)
require.NotNil(t, response)
// Check returned values
assert.Equal(t, testEncoding, response.Encoding)
assert.Equal(t, testMimeType, response.MimeType)
// Check that we got fees
assert.Len(t, response.Quote.Fees, 2)
})
t.Run("http error", func(t *testing.T) {
client := newTestClient(&mockHTTPError{})
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
require.Error(t, err)
require.Nil(t, response)
})
t.Run("bad request", func(t *testing.T) {
client := newTestClient(&mockHTTPBadRequest{})
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
require.Error(t, err)
require.Nil(t, response)
})
t.Run("invalid JSON", func(t *testing.T) {
client := newTestClient(&mockHTTPInvalidJSON{})
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
require.Error(t, err)
require.Nil(t, response)
})
t.Run("invalid category", func(t *testing.T) {
client := newTestClient(&mockHTTPValidBestQuote{})
response, err := client.BestQuote(context.Background(), "invalid", mapi.FeeTypeData)
require.Error(t, err)
require.Nil(t, response)
// Create a req
response, err = client.BestQuote(context.Background(), mapi.FeeCategoryMining, "invalid")
require.Error(t, err)
require.Nil(t, response)
})
t.Run("better rate", func(t *testing.T) {
client := newTestClient(&mockHTTPBetterRate{})
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryRelay, mapi.FeeTypeData)
require.NoError(t, err)
require.NotNil(t, response)
// Check that we got fees
assert.Len(t, response.Quote.Fees, 2)
var fee uint64
fee, err = response.Quote.CalculateFee(mapi.FeeCategoryRelay, mapi.FeeTypeData, 1000)
require.NoError(t, err)
assert.Equal(t, uint64(250), fee)
fee, err = response.Quote.CalculateFee(mapi.FeeCategoryMining, mapi.FeeTypeData, 1000)
require.NoError(t, err)
assert.Equal(t, uint64(500), fee)
})
t.Run("bad rate", func(t *testing.T) {
client := newTestClient(&mockHTTPBadRate{})
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryRelay, mapi.FeeTypeData)
require.Error(t, err)
require.Nil(t, response)
})
// TODO: Verify this test case for Arc API
// t.Run("best quote - two failed", func(t *testing.T) {
// // Create a client
// client := newTestClient(&mockHTTPBestQuoteTwoFailed{})
// // Create a req
// response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
// require.NoError(t, err)
// require.NotNil(t, response)
// // Check returned values
// assert.Equal(t, testEncoding, response.Encoding)
// assert.Equal(t, testMimeType, response.MimeType)
// // Check that we got fees
// assert.Equal(t, 2, len(response.Quote.Fees))
// assert.Equal(t, MinerMempool, response.Miner.Name)
// })
t.Run("best quote - all failed", func(t *testing.T) {
// Create a client
client := newTestClient(&mockHTTPBestQuoteAllFailed{})
// Create a req
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
require.Error(t, err)
assert.Nil(t, response)
})
}
// ExampleClient_BestQuote example using BestQuote()
func ExampleClient_BestQuote() {
// Create a client (using a test client vs NewClient())
client := newTestClient(&mockHTTPValidBestQuote{})
// Create a req
_, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Note: cannot show response since the miner might be different each time
fmt.Printf("got best quote!")
// Output:got best quote!
}
// BenchmarkClient_BestQuote benchmarks the method BestQuote()
func BenchmarkClient_BestQuote(b *testing.B) {
client := newTestClient(&mockHTTPValidBestQuote{})
for i := 0; i < b.N; i++ {
_, _ = client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
}
}