forked from dyzz/gobtclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (171 loc) · 4.16 KB
/
main.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
//go:generate protoc -I $GOPATH/src --go_out=$GOPATH/src $GOPATH/src/github.com/libreoscar/btcwatch/message/schema.proto
package main
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcrpcclient"
"github.com/btcsuite/btcutil"
"github.com/golang/protobuf/proto"
"github.com/libreoscar/btcwatch/addr"
"github.com/libreoscar/btcwatch/message"
"github.com/davecgh/go-spew/spew"
"github.com/libreoscar/utils/log"
zmq "github.com/pebbe/zmq4"
"io"
"net/http"
"os"
"sync"
"time"
)
var client *btcrpcclient.Client
var sender *zmq.Socket
var logger = log.New(log.DEBUG)
var isTestnet = false
func loadConf() *btcrpcclient.ConnConfig {
file, err := os.Open("conf.json")
if err != nil {
logger.Crit("failed to open \"conf.json\"")
os.Exit(-1)
}
decoder := json.NewDecoder(file)
rpcConf := &btcrpcclient.ConnConfig{}
err = decoder.Decode(rpcConf)
if err != nil {
logger.Crit(fmt.Sprintf("decode error:%s", err.Error()))
os.Exit(-1)
}
logger.Info(fmt.Sprintf("is testnet:%v", isTestnet))
rpcConf.HTTPPostMode = true
rpcConf.DisableTLS = true
return rpcConf
}
func getInfo(client *btcrpcclient.Client) {
// getinfo demo
info, err := client.GetInfo()
if err != nil {
logger.Crit(err.Error())
}
logger.Info(fmt.Sprintf("Bitcoind Info: %v", spew.Sdump(info)))
}
func decodePkScript(script []byte) (message []byte) {
if len(script) < 3 || len(script) > 82 || script[0] != 0x6a {
return nil
} else {
size := int(script[1])
if size != len(script)-2 {
return nil
}
return script[2:]
}
}
func checkBlock(client *btcrpcclient.Client, blockNum int64) {
blockHash, err := client.GetBlockHash(blockNum)
if err != nil {
logger.Crit(err.Error())
return
}
block, err := client.GetBlock(blockHash)
if err != nil {
logger.Crit(err.Error())
return
}
txs := block.Transactions()
var processedBlock = &message.ProcessedBlock{
int32(blockNum),
make([]*message.ProcessedTx, 0),
}
logger.Info("Processing txs...")
start := time.Now()
var wg sync.WaitGroup
for txIndex, tx := range txs {
wg.Add(1)
go func(txIndex int, tx *btcutil.Tx) {
defer wg.Done()
vouts := tx.MsgTx().TxOut
result := make([]*message.TxResult, len(vouts))
hasReturn := false
for i, vout := range vouts {
btcAddr := addr.NewAddrFromPkScript(vout.PkScript, isTestnet)
if btcAddr != nil {
result[i] = &message.TxResult{
&message.TxResult_Transfer{
&message.ValueTransfer{
btcAddr.String(),
uint64(vout.Value),
},
},
}
} else {
msg := decodePkScript(vout.PkScript)
if msg != nil {
result[i] = &message.TxResult{
&message.TxResult_Msg{
&message.OpReturnMsg{string(msg)},
},
}
hasReturn = true
}
}
}
if hasReturn {
processedBlock.Txs = append(processedBlock.Txs,
&message.ProcessedTx{
tx.Sha().String(),
result,
})
}
}(txIndex, tx)
}
wg.Wait()
spew.Dump(processedBlock)
data, err := proto.Marshal(processedBlock)
if err != nil {
logger.Crit(err.Error())
} else {
logger.Info("Publish to ZMQ...")
spew.Dump(data)
sender.SendBytes(data, 0)
}
elapsed := time.Since(start)
logger.Info(fmt.Sprintf("Process done in %s", elapsed))
logger.Info(fmt.Sprintf("Block %d has %d OP_Return Txs", blockNum, len(processedBlock.Txs)))
}
func blockNotify(w http.ResponseWriter, r *http.Request) {
logger.Info("Received new block!")
blockNum, err := client.GetBlockCount()
if err != nil {
io.WriteString(w, "bitcoind rpc failed\n")
}
checkBlock(client, blockNum)
}
func main() {
connCfg := loadConf()
var err error
client, err = btcrpcclient.New(connCfg, nil)
if err != nil {
logger.Crit(err.Error())
return
}
defer client.Shutdown()
var wg sync.WaitGroup
wg.Add(1)
http.HandleFunc("/block", blockNotify)
logger.Info("Starting server...")
// Start http server for bitcoind
go func() {
defer wg.Done()
err = http.ListenAndServe("127.0.0.1:8000", nil)
if err != nil {
logger.Crit(err.Error())
}
}()
// Start ZMQ server for braft
sender, err = zmq.NewSocket(zmq.PUB)
defer sender.Close()
if err != nil {
logger.Crit(err.Error())
}
sender.Bind("tcp://*:8001")
logger.Info("ZMQ server started...")
wg.Wait()
}