-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetblock.go
169 lines (138 loc) · 3.76 KB
/
getblock.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
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/btcsuite/btcd/wire"
)
var esploras = []string{
"https://mempool.space/api",
"https://blockstream.info/api",
"https://mempool.emzy.de/api",
}
var blockFetchFunctions = []func(hash string) ([]byte, error){
blockFromBlockchainInfo,
blockFromBlockchair,
blockFromEsplora,
}
var current int
func getBlock(height int) (*wire.MsgBlock, error) {
hash, err := getHash(height)
if err != nil {
return nil, fmt.Errorf("no hash for block %d: %w", height, err)
}
errs := make([]error, 0, 3)
for i := 0; i < len(blockFetchFunctions); i++ {
current++
fnidx := (i + current) % len(blockFetchFunctions)
fetchBlock := blockFetchFunctions[fnidx]
rawBlock, err := fetchBlock(hash)
if err != nil {
errs = append(errs, fmt.Errorf("failed to fetch from %d: %w", fnidx, err))
continue
}
block := &wire.MsgBlock{}
if err := block.Deserialize(bytes.NewReader(rawBlock)); err != nil {
errs = append(errs, fmt.Errorf("failed to deserialize block from %d: %w", fnidx, err))
continue
}
return block, nil
}
return nil, errors.Join(errs...)
}
func getHash(height int) (hash string, err error) {
errs := make([]error, 0, len(esploras))
for _, endpoint := range esploras {
w, err := http.Get(fmt.Sprintf(endpoint+"/block-height/%d", height))
if err != nil {
errs = append(errs, fmt.Errorf("failed to get from %s", endpoint))
continue
}
defer w.Body.Close()
if w.StatusCode >= 404 {
errs = append(errs, fmt.Errorf("404 at %s", endpoint))
continue
}
data, err := io.ReadAll(w.Body)
if err != nil {
errs = append(errs, err)
continue
}
hash = strings.TrimSpace(string(data))
if len(hash) > 64 {
err = fmt.Errorf("got something that isn't a block hash from %s: %s", endpoint, hash[:64])
errs = append(errs, err)
continue
}
return hash, nil
}
return "", errors.Join(errs...)
}
func blockFromBlockchainInfo(hash string) ([]byte, error) {
w, err := http.Get(fmt.Sprintf("https://blockchain.info/rawblock/%s?format=hex", hash))
if err != nil {
return nil, fmt.Errorf("failed to get raw block %s from blockchain.info: %s", hash, err.Error())
}
defer w.Body.Close()
block, _ := io.ReadAll(w.Body)
if len(block) < 100 {
return nil, fmt.Errorf("block not available? %s", string(block))
}
blockbytes, err := hex.DecodeString(string(block))
if err != nil {
return nil, fmt.Errorf("block from blockchain.info is invalid hex: %w", err)
}
return blockbytes, nil
}
func blockFromBlockchair(hash string) ([]byte, error) {
url := "https://api.blockchair.com/bitcoin/raw/block/"
w, err := http.Get(url + hash)
if err != nil {
return nil, fmt.Errorf(
"failed to get raw block %s from blockchair.com: %s", hash, err.Error())
}
defer w.Body.Close()
var data struct {
Data map[string]struct {
RawBlock string `json:"raw_block"`
} `json:"data"`
}
err = json.NewDecoder(w.Body).Decode(&data)
if err != nil {
return nil, err
}
if bdata, ok := data.Data[hash]; ok {
blockbytes, err := hex.DecodeString(bdata.RawBlock)
if err != nil {
return nil, fmt.Errorf("block from blockchair is invalid hex: %w", err)
}
return blockbytes, nil
} else {
// block not available here yet
return nil, nil
}
}
func blockFromEsplora(hash string) ([]byte, error) {
errs := make([]error, 0, len(esploras))
var block []byte
for _, endpoint := range esploras {
w, err := http.Get(fmt.Sprintf(endpoint+"/block/%s/raw", hash))
if err != nil {
errs = append(errs, fmt.Errorf("failed to get from %s: %w", endpoint, err))
continue
}
defer w.Body.Close()
block, _ = io.ReadAll(w.Body)
if len(block) < 200 {
// block not available yet
return nil, nil
}
break
}
return block, errors.Join(errs...)
}