-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
229 lines (196 loc) · 4.89 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
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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
const (
ACTIVATION_PERIOD = 750
ENFORCEMENT_PERIOD = 950
TARGET_WINDOW = 1000
)
var (
BlockIndex map[int]int
version, block int
verbose bool
RPCHost string
RPCPort int
RPCUsername string
RPCPassword string
)
func BuildURL() string {
return fmt.Sprintf("http://%s:%s@%s:%d", RPCUsername, RPCPassword, RPCHost, RPCPort)
}
func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) (err error) {
res, err := http.Get(url)
if err != nil {
return err
}
if res.StatusCode != 200 {
log.Printf("HTTP status code: %d\n", res.StatusCode)
return errors.New("Status code was not 200.")
}
contents, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
if jsonDecode {
err := JSONDecode(contents, &result)
if err != nil {
return err
}
} else {
result = &contents
}
return nil
}
func JSONDecode(data []byte, to interface{}) error {
err := json.Unmarshal(data, &to)
if err != nil {
return err
}
return nil
}
func SendRPCRequest(method, req interface{}) (map[string]interface{}, error) {
var params []interface{}
if req != nil {
params = append(params, req)
} else {
params = nil
}
data, err := json.Marshal(map[string]interface{}{
"method": method,
"id": 1,
"params": params,
})
if err != nil {
return nil, err
}
resp, err := http.Post(BuildURL(), "application/json", strings.NewReader(string(data)))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
if result["error"] != nil {
errorMsg := result["error"].(map[string]interface{})
return nil, fmt.Errorf("Error code: %v, message: %v\n", errorMsg["code"], errorMsg["message"])
}
return result, nil
}
func GetBlockVersion(block int) (int, error) {
result, err := SendRPCRequest("getblockhash", block)
if err != nil {
return 0, err
}
blockHash := result["result"].(string)
result, err = SendRPCRequest("getblock", blockHash)
if err != nil {
return 0, err
}
result = result["result"].(map[string]interface{})
version := result["version"].(float64)
return int(version), nil
}
func CheckBlocks(minVersion, height, threshold int) (bool, int, int) {
nFound := 0
blockVer := 0
var ok bool
var err error
lastBlock := 0
for i := 0; i < TARGET_WINDOW && nFound < threshold && i >= 0; i++ {
blockVer, ok = BlockIndex[height]
if !ok {
blockVer, err = GetBlockVersion(height)
if err != nil {
log.Fatal("Failed to obtain block version.")
}
BlockIndex[height] = blockVer
}
if blockVer >= minVersion {
nFound++
}
if blockVer == minVersion-1 {
if height > lastBlock {
lastBlock = height
}
}
height--
}
if nFound >= threshold {
return true, nFound, lastBlock
}
return false, nFound, lastBlock
}
func GetVersionBIPString(version int) string {
versionStr := ""
switch version {
case 2:
versionStr = "BIP34"
case 3:
versionStr = "BIP66"
case 4:
versionStr = "BIP65"
default:
versionStr = "NA"
}
return versionStr
}
func main() {
BlockIndex = make(map[int]int)
flag.StringVar(&RPCHost, "rpchost", "127.0.0.1", "The RPC host to connect to.")
flag.IntVar(&RPCPort, "rpcport", 9333, "The RPC port to connect to.")
flag.StringVar(&RPCUsername, "rpcuser", "user", "The RPC username.")
flag.StringVar(&RPCPassword, "rpcpass", "pass", "The RPC password.")
flag.IntVar(&version, "version", 3, "The block version to check.")
flag.IntVar(&block, "block", 810000, "Block height to start checking from.")
flag.BoolVar(&verbose, "verbose", false, "Toggle verbose reporting.")
flag.Parse()
versionStr := GetVersionBIPString(version)
log.Printf("RPC URL: %s", BuildURL())
log.Printf("Checking for block version %d (%s) activation height with start height %d.\n", version, versionStr, block)
bActivated := false
height := block
percentage := float64(0)
quit := false
for {
if !bActivated {
success, found, _ := CheckBlocks(version, height, ACTIVATION_PERIOD)
percentage = float64(found) / TARGET_WINDOW * 100 / 1
if success {
log.Printf("Block %d reached version %d (%s) activation.\n", height+1, version, versionStr)
bActivated = true
}
} else {
success, found, last := CheckBlocks(version, height, ENFORCEMENT_PERIOD)
percentage = float64(found) / TARGET_WINDOW * 100 / 1
if success {
log.Printf("Block %d reached version %d (%s) enforcement.\n", height+1, version, versionStr)
log.Printf("Last version %d block: %d.\n", version-1, last)
quit = true
}
}
if verbose {
blockVer := BlockIndex[height]
log.Printf("Block height: %d Version: %d Percentage: %.2f%%\n", height, blockVer, percentage)
}
if quit {
break
}
height++
}
}