-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitcoin.go
356 lines (316 loc) · 10 KB
/
bitcoin.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
package main
import (
"context"
"errors"
"strings"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/rpcclient"
"github.com/shopspring/decimal"
)
type bitcoinClientAlias struct {
*rpcclient.Client
}
func (conf *configure) bitcoinClient() *rpcclient.Client {
connCfg := &rpcclient.ConnConfig{
Host: strings.Join([]string{conf.BitcoinHost, conf.BitcoinPort}, ":"),
User: conf.BitcoinUser,
Pass: conf.BitcoinPass,
HTTPPostMode: conf.BitcoinhttpMode,
DisableTLS: conf.BitcoinDisableTLS,
}
client, err := rpcclient.New(connCfg, nil)
if err != nil {
sugar.Fatal("bitcoind client err: ", err.Error())
}
return client
}
func (btcClient *bitcoinClientAlias) ReSetSync(hightest int32, elasticClient *elasticClientAlias) {
names, err := elasticClient.IndexNames()
ctx := context.Background()
if err != nil {
sugar.Fatal("ctx error: ", err.Error())
}
for _, name := range names {
elasticClient.DeleteIndex(name).Do(ctx)
}
elasticClient.createIndices()
btcClient.dumpToES(int32(1), hightest, int(ROLLBACKHEIGHT), elasticClient)
}
func (btcClient *bitcoinClientAlias) getBlock(height int32) (*btcjson.GetBlockVerboseResult, error) {
blockHash, err := btcClient.GetBlockHash(int64(height))
if err != nil {
return nil, err
}
block, err := btcClient.GetBlockVerboseTxM(blockHash)
if err != nil {
return nil, err
}
return block, nil
}
// Balance type struct
type Balance struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
}
// BalanceJournal 余额变更流水
type BalanceJournal struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
Operate string `json:"operate"`
Txid string `json:"txid"`
}
// AddressWithAmount 地址-余额类型
type AddressWithAmount struct {
Address string `json:"address"`
Amount decimal.Decimal `json:"amount"`
}
// AddressWithAmountAndTxid 地址-余额类型
type AddressWithAmountAndTxid struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
Txid string `json:"txid"`
}
// BalanceWithID 类型
type BalanceWithID struct {
ID string `json:"id"`
Balance Balance `json:"balance"`
}
// VoutWithID type struct
type VoutWithID struct {
ID string
Vout *VoutStream
}
// VoutStream type struct
type VoutStream struct {
TxIDBelongTo string `json:"txidbelongto"`
Value float64 `json:"value"`
Voutindex uint32 `json:"voutindex"`
Coinbase bool `json:"coinbase"`
Addresses []string `json:"addresses"`
Used interface{} `json:"used"`
}
// AddressWithValueInTx 交易中地输入输出的地址和余额
type AddressWithValueInTx struct {
Address string `json:"address"`
Value float64 `json:"value"`
}
// IndexUTXO vout 索引
type IndexUTXO struct {
Txid string
Index uint32
}
// TxStream type struct
type esTx struct {
Txid string `json:"txid"`
Fee float64 `json:"fee"`
BlockHash string `json:"blockhash"`
Time int64 `json:"time"`
Vins []AddressWithValueInTx `json:"vins"`
Vouts []AddressWithValueInTx `json:"vouts"`
}
type voutUsed struct {
Txid string `json:"txid"` // 所在交易的 id
VinIndex uint32 `json:"vinindex"` // 作为 vin 被使用时,vin 的 vout 字段
}
// BTCBlockWithTxDetail elasticsearch 中 block Type 数据
func blockWithTxDetail(block *btcjson.GetBlockVerboseResult) interface{} {
txs := blockTx(block.Tx)
blockWithTx := map[string]interface{}{
"hash": block.Hash,
"strippedsize": block.StrippedSize,
"size": block.Size,
"weight": block.Weight,
"height": block.Height,
"versionHex": block.VersionHex,
"merkleroot": block.MerkleRoot,
"time": block.Time,
"nonce": block.Nonce,
"bits": block.Bits,
"difficulty": block.Difficulty,
"previoushash": block.PreviousHash,
"nexthash": block.NextHash,
"tx": txs,
}
return blockWithTx
}
func blockTx(txs []btcjson.TxRawResult) []map[string]interface{} {
var rawTxs []map[string]interface{}
for _, tx := range txs {
// https://tradeblock.com/blog/bitcoin-0-8-5-released-provides-critical-bug-fixes/
txVersion := tx.Version
if tx.Version < 0 {
txVersion = 1
}
vouts := txVouts(tx)
vins := txVins(tx)
rawTxs = append(rawTxs, map[string]interface{}{
"txid": tx.Txid,
"hash": tx.Hash,
"version": txVersion,
"size": tx.Size,
"vsize": tx.Vsize,
"locktime": tx.LockTime,
"vout": vouts,
"vin": vins,
})
}
return rawTxs
}
func txVouts(tx btcjson.TxRawResult) []map[string]interface{} {
var vouts []map[string]interface{}
for _, vout := range tx.Vout {
vouts = append(vouts, map[string]interface{}{
"value": vout.Value,
"n": vout.N,
"scriptPubKey": map[string]interface{}{
"asm": vout.ScriptPubKey.Asm,
"reqSigs": vout.ScriptPubKey.ReqSigs,
"type": vout.ScriptPubKey.Type,
"addresses": vout.ScriptPubKey.Addresses,
},
})
}
return vouts
}
func txVins(tx btcjson.TxRawResult) []map[string]interface{} {
var vins []map[string]interface{}
for _, vin := range tx.Vin {
if len(tx.Vin) == 1 && len(vin.Coinbase) != 0 && len(vin.Txid) == 0 {
vins = append(vins, map[string]interface{}{
"coinbase": vin.Coinbase,
"sequence": vin.Sequence,
})
break
}
vins = append(vins, map[string]interface{}{
"txid": vin.Txid,
"vout": vin.Vout,
"scriptSig": map[string]interface{}{
"asm": vin.ScriptSig.Asm,
},
"sequence": vin.Sequence,
})
}
return vins
}
// get addresses in bitcoin vout
func voutAddressFun(vout btcjson.Vout) (*[]string, error) {
var addresses []string
if len(vout.ScriptPubKey.Addresses) > 0 {
addresses = vout.ScriptPubKey.Addresses
return &addresses, nil
}
if len(addresses) == 0 {
return nil, errors.New("Unable to decode output address")
}
return nil, errors.New("address not fount in vout")
}
// VoutStream elasticsearch 中 voutstream Type 数据
func newVoutFun(vout btcjson.Vout, vins []btcjson.Vin, TxID string) (*VoutStream, error) {
coinbase := false
if len(vins[0].Coinbase) != 0 && len(vins[0].Txid) == 0 {
coinbase = true
}
addresses, err := voutAddressFun(vout)
if err != nil {
return nil, err
}
v := &VoutStream{
TxIDBelongTo: TxID,
Value: vout.Value,
Voutindex: vout.N,
Coinbase: coinbase,
Addresses: *addresses,
Used: nil,
}
return v, nil
}
func newBalanceJournalFun(address, ope, txid string, amount float64) BalanceJournal {
balancejournal := BalanceJournal{
Address: address,
Operate: ope,
Amount: amount,
Txid: txid,
}
return balancejournal
}
// elasticsearch 中 txstream Type 数据
func esTxFun(tx btcjson.TxRawResult, blockHash string, simpleVins, simpleVouts []AddressWithValueInTx, vinAmount, voutAmount decimal.Decimal) *esTx {
// caculate tx fee
fee := vinAmount.Sub(voutAmount)
if len(tx.Vin) == 1 && len(tx.Vin[0].Coinbase) != 0 && len(tx.Vin[0].Txid) == 0 || vinAmount.Equal(voutAmount) {
fee = decimal.NewFromFloat(0)
}
// bulk insert tx docutment
esFee, _ := fee.Float64()
result := &esTx{
Txid: tx.Txid,
Fee: esFee,
BlockHash: blockHash,
Time: tx.Time, // TODO: time field is nil, need to fix
Vins: simpleVins,
Vouts: simpleVouts,
}
return result
}
// return value:
// *[]*AddressWithValueInTx for elasticsearch tx Type vouts field
// *[]interface{} all addresses related to the vout
// *[]*Balance all addresses related to the vout with value amount
func parseTxVout(vout btcjson.Vout, txid string) ([]AddressWithValueInTx, []interface{}, []Balance, []AddressWithAmountAndTxid) {
var (
txVoutsField []AddressWithValueInTx
voutAddresses []interface{} // All addresses related with vout in a block
voutAddressWithAmounts []Balance
voutAddressWithAmountAndTxidSlice []AddressWithAmountAndTxid
)
// vouts field in tx type
for _, address := range vout.ScriptPubKey.Addresses {
txVoutsField = append(txVoutsField, AddressWithValueInTx{
Address: address,
Value: vout.Value,
})
// vout addresses slice
voutAddresses = append(voutAddresses, address)
// vout addresses with amount
voutAddressWithAmounts = append(voutAddressWithAmounts, Balance{address, vout.Value})
voutAddressWithAmountAndTxidSlice = append(voutAddressWithAmountAndTxidSlice, AddressWithAmountAndTxid{
Address: address, Amount: vout.Value, Txid: txid})
}
return txVoutsField, voutAddresses, voutAddressWithAmounts, voutAddressWithAmountAndTxidSlice
}
// return value
// []*AddressWithValueInTx for elasticsearch tx Type vins field
// []interface{} all addresses related to the vin
// []*Balance all addresses related to the vout with value amount
func parseESVout(voutWithID VoutWithID, txid string) ([]AddressWithValueInTx, []interface{}, []Balance, []AddressWithAmountAndTxid) {
var (
txTypeVinsField []AddressWithValueInTx
vinAddresses []interface{}
vinAddressWithAmountSlice []Balance
vinAddressWithAmountAndTxidSlice []AddressWithAmountAndTxid
)
for _, address := range voutWithID.Vout.Addresses {
vinAddresses = append(vinAddresses, address)
vinAddressWithAmountSlice = append(vinAddressWithAmountSlice, Balance{address, voutWithID.Vout.Value})
txTypeVinsField = append(txTypeVinsField, AddressWithValueInTx{address, voutWithID.Vout.Value})
vinAddressWithAmountAndTxidSlice = append(vinAddressWithAmountAndTxidSlice, AddressWithAmountAndTxid{
Address: address, Amount: voutWithID.Vout.Value, Txid: txid})
}
return txTypeVinsField, vinAddresses, vinAddressWithAmountSlice, vinAddressWithAmountAndTxidSlice
}
func indexedVinsFun(vins []btcjson.Vin) []IndexUTXO {
var IndexUTXOs []IndexUTXO
for _, vin := range vins {
item := IndexUTXO{vin.Txid, vin.Vout}
IndexUTXOs = append(IndexUTXOs, item)
}
return IndexUTXOs
}
func indexedVoutsFun(vouts []btcjson.Vout, txid string) []IndexUTXO {
var IndexUTXOs []IndexUTXO
for _, vout := range vouts {
IndexUTXOs = append(IndexUTXOs, IndexUTXO{txid, vout.N})
}
return IndexUTXOs
}