forked from btcsuite/blocksafari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocksafari.go
executable file
·362 lines (315 loc) · 8.03 KB
/
blocksafari.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
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/PointCoin/btcjson"
"github.com/PointCoin/btcrpcclient"
"github.com/PointCoin/btcwire"
"github.com/davecgh/go-spew/spew"
)
const (
numMainPageBlocks = 30 // number of blocks to display on main page
)
var (
client *btcrpcclient.Client
)
func handleBlock(w http.ResponseWriter, r *http.Request) {
blockhash := r.URL.Path[len("/block"):]
if len(blockhash) < 2 || len(blockhash[1:]) != 64 {
printErrorPage(w, "Invalid block hash")
return
}
sha, err := btcwire.NewShaHashFromStr(blockhash[1:])
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
block, err := client.GetBlockVerbose(sha, true)
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
title := fmt.Sprintf("Block %v", block.Height)
printHTMLHeader(w, title)
printBlock(w, block, block.RawTx)
printHTMLFooter(w)
}
func handleBlockNum(w http.ResponseWriter, r *http.Request) {
blocknum := r.URL.Path[len("/b"):]
if len(blocknum) < 2 {
return
}
blocknum = blocknum[1:]
b, err := strconv.Atoi(blocknum)
if err != nil {
fmt.Fprintf(w, "invalid block number: %v", blocknum)
return
}
hash, err := client.GetBlockHash(int64(b))
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
uri := fmt.Sprintf("http://%v/block/%v", r.Host, hash.String())
w.Header().Set("Location", uri)
w.WriteHeader(307)
}
func handleCSS(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/css"):]
if len(file) < 2 {
return
}
http.ServeFile(w, r, "css/"+file[1:])
}
func handleJS(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/js"):]
if len(file) < 2 {
return
}
http.ServeFile(w, r, "js/"+file[1:])
}
func handleScores(w http.ResponseWriter, r *http.Request) {
sha, err := client.GetBestBlockHash()
numBlocks, _ := client.GetBlockCount()
if err != nil {
printErrorPage(w, "Unable to get best blockhash")
return
}
blocks := make([]*btcjson.BlockResult, numBlocks)
blocks[0], err = client.GetBlockVerbose(sha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
for j := 1; int64(j) < numBlocks && blocks[j-1].PreviousHash != ""; j++ {
prevsha, _ := btcwire.NewShaHashFromStr(blocks[j-1].PreviousHash)
blocks[j], err = client.GetBlockVerbose(prevsha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
}
printHTMLHeader(w, "Welcome")
printScores(w, blocks)
printHTMLFooter(w)
}
func handlePager(w http.ResponseWriter, r *http.Request) {
h := r.URL.Path[len("/height"):]
height, err := strconv.Atoi(h[1:])
if err != nil {
printErrorPage(w, "Unable to parse block height")
return
}
sha, err := client.GetBlockHash(int64(height))
if err != nil {
printErrorPage(w, "No block found at that height")
}
blocks := make([]*btcjson.BlockResult, numMainPageBlocks)
blocks[0], err = client.GetBlockVerbose(sha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
for i := 1; int64(i) < numMainPageBlocks && blocks[i-1].PreviousHash != ""; i++ {
prevsha, _ := btcwire.NewShaHashFromStr(blocks[i-1].PreviousHash)
blocks[i], err = client.GetBlockVerbose(prevsha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
}
printHTMLHeader(w, "Welcome")
printBlockPager(w, blocks)
printHTMLFooter(w)
}
func handleMain(w http.ResponseWriter, r *http.Request) {
sha, err := client.GetBestBlockHash()
if err != nil {
printErrorPage(w, "Unable to get best blockhash")
return
}
blocks := make([]*btcjson.BlockResult, numMainPageBlocks)
blocks[0], err = client.GetBlockVerbose(sha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
for j := 1; int64(j) < numMainPageBlocks && blocks[j-1].PreviousHash != ""; j++ {
prevsha, _ := btcwire.NewShaHashFromStr(blocks[j-1].PreviousHash)
blocks[j], err = client.GetBlockVerbose(prevsha, true)
if err != nil {
printErrorPage(w, "Error retrieving block")
return
}
}
printHTMLHeader(w, "Welcome")
printMainBlock(w, blocks)
printHTMLFooter(w)
}
func handleRawBlock(w http.ResponseWriter, r *http.Request) {
block := r.URL.Path[len("/rawblock"):]
if len(block) < 2 {
printErrorPage(w, "Invalid block hash")
return
}
sha, err := btcwire.NewShaHashFromStr(block[1:])
if err != nil {
printErrorPage(w, "Invalid block hash")
return
}
output, err := client.GetBlock(sha)
if err != nil {
printErrorPage(w, "Block not found")
return
}
printContentType(w, "text/plain")
fmt.Fprintf(w, "%v", spew.Sdump(output))
}
func handleRawTx(w http.ResponseWriter, r *http.Request) {
tx := r.URL.Path[len("/rawtx"):]
if len(tx) < 2 {
printErrorPage(w, "Invalid transaction sha")
return
}
sha, err := btcwire.NewShaHashFromStr(tx[1:])
if err != nil {
printErrorPage(w, "Invalid transaction sha")
return
}
output, err := client.GetRawTransactionVerbose(sha)
if err != nil {
printErrorPage(w, "Transaction not found")
return
}
printContentType(w, "text/plain")
fmt.Fprintf(w, "%v", spew.Sdump(output))
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
q := r.URL.Path[len("/search"):]
if len(q) < 2 {
return
}
q = q[1:]
if len(q) == 64 {
uri := fmt.Sprintf("http://%v/block/%v", r.Host, q)
w.Header().Set("Location", uri)
w.WriteHeader(307)
} else if _, err := strconv.ParseInt(q, 0, 64); err == nil {
uri := fmt.Sprintf("http://%v/b/%v", r.Host, q)
w.Header().Set("Location", uri)
w.WriteHeader(307)
} else {
str := "Unknown search value: " + q
printErrorPage(w, str)
}
}
func handleTx(w http.ResponseWriter, r *http.Request) {
tx := r.URL.Path[len("/tx"):]
if len(tx) < 2 {
printErrorPage(w, "Invalid TX hash")
return
}
sha, err := btcwire.NewShaHashFromStr(tx[1:])
if err != nil {
printErrorPage(w, "Invalid TX hash")
return
}
t, err := client.GetRawTransactionVerbose(sha)
if err != nil {
printErrorPage(w, "Unable to retrieve tx")
return
}
title := fmt.Sprintf("Tx %v\n", t.Txid)
printHTMLHeader(w, title)
printTx(w, t)
printHTMLFooter(w)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:]
r.Header.Set("Connection", "close")
page := strings.Split(path, "/")[0]
switch page {
case "b":
handleBlockNum(w, r)
case "block":
handleBlock(w, r)
case "css":
handleCSS(w, r)
case "js":
handleJS(w, r)
case "rawblock":
handleRawBlock(w, r)
case "rawtx":
handleRawTx(w, r)
case "search":
handleSearch(w, r)
case "tx":
handleTx(w, r)
case "scores":
handleScores(w, r)
case "height":
handlePager(w, r)
case "":
handleMain(w, r)
default:
/* XXX - serve 404's */
fmt.Fprintf(w, "404 - Not found")
}
}
func main() {
var err error
cfg, _, err = loadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "loadConfig failed: %v\n", err)
os.Exit(-1)
}
listeners := make([]net.Listener, 0, len(cfg.Listeners))
for _, addr := range cfg.Listeners {
listener, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintf(os.Stderr, "Listen failed: %v\n", err)
os.Exit(-1)
}
listeners = append(listeners, listener)
}
cert, err := ioutil.ReadFile(cfg.RPCCert)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load certificate: %v\n", err)
os.Exit(-1)
}
rpccfg := btcrpcclient.ConnConfig{
Host: cfg.RPCServer,
Endpoint: "ws",
User: cfg.RPCUser,
Pass: cfg.RPCPassword,
Certificates: cert,
}
client, err = btcrpcclient.New(&rpccfg, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "New RPC Client failed: %v\n", err)
os.Exit(-1)
}
http.HandleFunc("/", handleRequest)
httpServeMux := http.NewServeMux()
httpServer := &http.Server{Handler: httpServeMux}
httpServeMux.HandleFunc("/", handleRequest)
var wg sync.WaitGroup
for _, listener := range listeners {
wg.Add(1)
go func(listener net.Listener) {
fmt.Fprintf(os.Stderr, "HTTP server listening on %s\n", listener.Addr())
httpServer.Serve(listener)
wg.Done()
}(listener)
}
wg.Wait()
}