-
Notifications
You must be signed in to change notification settings - Fork 3
/
command.go
420 lines (355 loc) · 9.35 KB
/
command.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package command
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"strconv"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/catalogfi/indexer/model"
)
type Storage interface {
GetBlockHash(height int32) (string, error)
GetLatestBlockHash() (string, error)
GetLatestBlockHeight() (int32, error)
GetTransaction(hash string) (Transaction, error)
GetBlockFromHash(hash string) (*btcutil.Block, error)
GetHeaderFromHeight(height int32) (BlockHeader, error)
GetHeaderFromHash(hash string) (BlockHeader, error)
ListUnspent(startBlock, endBlock int, addresses []string, includeUnsafe bool, queryOptions ListUnspentQueryOptions) ([]model.OutPoint, error)
}
type Command interface {
Name() string
Query(str Storage, params []interface{}) (interface{}, error)
}
// getbestblockhash
type getBestBlockHash struct {
}
func GetBestBlockHash() Command {
return &getBestBlockHash{}
}
func (g *getBestBlockHash) Name() string {
return "getbestblockhash"
}
func (g *getBestBlockHash) Query(str Storage, params []interface{}) (interface{}, error) {
return str.GetLatestBlockHash()
}
// getblockhash
type getBlockHash struct {
}
func GetBlockHash() Command {
return &getBlockHash{}
}
func (g *getBlockHash) Name() string {
return "getblockhash"
}
func (g *getBlockHash) Query(str Storage, params []interface{}) (interface{}, error) {
height, ok := params[0].(float64)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required a number", params[0])
}
return str.GetBlockHash(int32(height))
}
// getblockcount
type getBlockCount struct {
}
func GetBlockCount() Command {
return &getBlockCount{}
}
func (g *getBlockCount) Name() string {
return "getblockcount"
}
func (g *getBlockCount) Query(str Storage, params []interface{}) (interface{}, error) {
return str.GetLatestBlockHeight()
}
// getblockheader
type getBlockHeader struct {
}
func GetBlockHeader() Command {
return &getBlockHeader{}
}
func (g *getBlockHeader) Name() string {
return "getblockheader"
}
type BlockHeader struct {
Header *wire.BlockHeader
Height int32
NumTxs int64
}
func (g *getBlockHeader) Query(str Storage, params []interface{}) (interface{}, error) {
if len(params) < 1 && len(params) > 2 {
return nil, fmt.Errorf("invalid number of parameters: %d, required 1 or 2", len(params))
}
blockHash, ok := params[0].(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required string", params[0])
}
header, err := str.GetHeaderFromHash(blockHash)
if err != nil {
return nil, err
}
verbose := true
if len(params) == 2 {
verbose, ok = params[1].(bool)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required boolean", params[1])
}
}
if !verbose {
buf := new(bytes.Buffer)
if err := header.Header.Serialize(buf); err != nil {
return nil, err
}
return hex.EncodeToString(buf.Bytes()), nil
}
tip, err := str.GetLatestBlockHeight()
if err != nil {
return nil, err
}
medianHeader, err := str.GetHeaderFromHeight(getMedianBlockHeight(header.Height))
if err != nil {
return nil, err
}
nextBlockHash, _ := str.GetBlockHash(header.Height + 1)
return EncodeBlockHeader(header.Header, header.NumTxs, header.Height, uint32(tip-header.Height)+1, medianHeader.Header.Timestamp.Unix(), nextBlockHash)
}
func getMedianBlockHeight(height int32) int32 {
if height == 0 || height == 1 {
return height
}
if height > 11 {
return height - 6
}
if height%2 == 0 {
return height / 2
}
return (height + 1) / 2
}
// getblock
type getBlock struct {
}
func GetBlock() Command {
return &getBlock{}
}
func (g *getBlock) Name() string {
return "getblock"
}
func (g *getBlock) Query(str Storage, params []interface{}) (interface{}, error) {
if len(params) < 1 && len(params) > 2 {
return nil, fmt.Errorf("invalid number of parameters: %d, required 1 or 2", len(params))
}
blockHash, ok := params[0].(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required string", params[0])
}
block, err := str.GetBlockFromHash(blockHash)
if err != nil {
return nil, err
}
verbose := 1
if len(params) == 2 {
verboseF, ok := params[1].(float64)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required boolean", params[1])
}
verbose = int(verboseF)
}
if verbose == 0 {
bb, err := block.Bytes()
if err != nil {
return nil, err
}
return hex.EncodeToString(bb), nil
}
tip, err := str.GetLatestBlockHeight()
if err != nil {
return nil, err
}
medianHeader, err := str.GetHeaderFromHeight(getMedianBlockHeight(block.Height()))
if err != nil {
return nil, err
}
nextBlockHash, _ := str.GetBlockHash(block.Height() + 1)
return EncodeBlock(block, uint32(tip-block.Height())+1, medianHeader.Header.Timestamp.Unix(), nextBlockHash, verbose)
}
// getrawtransaction
type getRawTransaction struct {
}
func GetRawTransaction() Command {
return &getRawTransaction{}
}
func (g *getRawTransaction) Name() string {
return "getrawtransaction"
}
type Transaction struct {
Tx *wire.MsgTx
BlockHash string
BlockTime int64
Height int32
}
func (g *getRawTransaction) Query(str Storage, params []interface{}) (interface{}, error) {
txHash := ""
verbose := false
switch len(params) {
case 3:
fallthrough
case 2:
param2, ok := params[1].(bool)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required boolean", params[1])
}
verbose = param2
fallthrough
case 1:
param1, ok := params[0].(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required string", params[0])
}
txHash = param1
default:
return nil, fmt.Errorf("invalid number of parameters: %d, required 1, 2 or 3", len(params))
}
tx, err := str.GetTransaction(txHash)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := tx.Tx.Serialize(buf); err != nil {
return nil, err
}
if !verbose {
return hex.EncodeToString(buf.Bytes()), nil
}
tip, err := str.GetLatestBlockHeight()
if err != nil {
return nil, err
}
return EncodeTransaction(tx.Tx, tx.BlockHash, uint32(tip-tx.Height)+1, tx.BlockTime), nil
}
// listunspent
type listUnspent struct {
}
func ListUnspent() Command {
return &listUnspent{}
}
func (g *listUnspent) Name() string {
return "listunspent"
}
func parseAmount(amt interface{}) (int64, bool) {
switch amt := amt.(type) {
case float64:
return int64(amt * 1e8), true
case string:
amtF, err := strconv.ParseFloat(amt, 64)
if err != nil {
return 0, false
}
return int64(amtF * 1e8), true
default:
return 0, false
}
}
func (g *listUnspent) Query(str Storage, params []interface{}) (interface{}, error) {
// Defaults
minconf := 1
maxconf := 9999999
addresses := []string{}
includeUnsafe := true
queryOptions := ListUnspentQueryOptions{
MinimumAmount: 0,
MaximumAmount: math.MaxInt64,
MaximumCount: math.MaxUint32,
MinimumSumAmount: math.MaxInt64,
}
switch len(params) {
case 5:
qoj, ok := params[4].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required object", params[4])
}
maxAmt, ok := parseAmount(qoj["maximumAmount"])
if ok {
queryOptions.MaximumAmount = maxAmt
}
minAmt, ok := parseAmount(qoj["minimumAmount"])
if ok {
queryOptions.MinimumAmount = minAmt
}
maxCount, ok := qoj["maximumCount"].(float64)
if ok {
queryOptions.MaximumCount = uint32(maxCount)
}
minSumAmt, ok := parseAmount(qoj["minimumSumAmount"])
if ok {
queryOptions.MinimumSumAmount = minSumAmt
}
fallthrough
case 4:
// unconfirmed replacement transactions are considered unsafe
// and are not eligible for spending by fundrawtransaction and sendtoaddress
param3, ok := params[3].(bool)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required boolean", params[3])
}
includeUnsafe = param3
fallthrough
case 3:
// The bitcoin addresses to filter
param2, ok := params[2].([]interface{}) // only this is used right now
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required array", params[2])
}
addresses = make([]string, len(param2))
for i, addr := range param2 {
addresses[i], ok = addr.(string)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required string", addr)
}
}
// The maximum confirmations to filter
param1, ok := params[1].(float64)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required number", params[1])
}
maxconf = int(param1)
// The minimum confirmations to filter
param0, ok := params[0].(float64)
if !ok {
return nil, fmt.Errorf("invalid parameter type: %T, required number", params[0])
}
minconf = int(param0)
default:
return nil, fmt.Errorf("invalid number of parameters needed 3-5, got %d", len(params))
}
tip, err := str.GetLatestBlockHeight()
if err != nil {
return nil, err
}
startBlock := int(tip) + 1 - maxconf
if startBlock < 0 {
startBlock = 0
}
endBlock := int(tip) + 1 - minconf
if endBlock < 0 {
endBlock = 0
}
outpoints, err := str.ListUnspent(startBlock, endBlock, addresses, includeUnsafe, queryOptions)
if err != nil {
return nil, err
}
unspents := []Unspent{}
sumAmount := int64(0)
for _, op := range outpoints {
tx, err := str.GetTransaction(op.FundingTxHash)
if err != nil {
return nil, err
}
unspents = append(unspents, EncodeUnspent(op, uint32(tip-tx.Height)+1))
sumAmount += op.Value
if sumAmount >= queryOptions.MinimumSumAmount {
break
}
}
return unspents, nil
}