-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (92 loc) · 2.63 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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
cstypes "github.com/tendermint/tendermint/consensus/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/rpc/client/http"
tmtypes "github.com/tendermint/tendermint/types"
)
const genesis = "/Users/zakimanian/cosmoshub-test-stargate/config/genesis.json"
const nodeAddress = "http://34.66.219.254:26657"
type roundVotes struct {
Round int32 `json:"round"`
Prevotes []string `json:"prevotes"`
PrevotesBitArray string `json:"prevotes_bit_array"`
Precommits []string `json:"precommits"`
PrecommitsBitArray string `json:"precommits_bit_array"`
}
func main() {
jsonBlob, err := ioutil.ReadFile(genesis)
if err != nil {
log.Fatal(err)
}
_, err = tmtypes.GenesisDocFromJSON(jsonBlob)
if err != nil {
log.Fatal(err)
}
c, err := http.New(nodeAddress, "/websocket")
if err != nil {
log.Fatalf("can't connect to node: %s", err)
}
stateJSON, err := c.ConsensusState(context.Background())
if err != nil {
log.Fatalf("can't get consensus state from node: %s", err)
}
var state cstypes.RoundStateSimple
var votes []roundVotes
err = tmjson.Unmarshal(stateJSON.RoundState, &state)
if err != nil {
log.Fatalf("Unmarshalling round state error: %s", err)
}
err = tmjson.Unmarshal(state.Votes, &votes)
if err != nil {
log.Fatalf("Unmarshalling vote state error: %s", err)
}
for _, round := range votes {
var prevotes []Vote
var precommits []Vote
for _, prevoteStr := range round.Prevotes {
if prevoteStr != "nil-Vote" {
prevotes = append(prevotes, unpackVote(prevoteStr))
}
}
for _, precommitStr := range round.Precommits {
if precommitStr != "nil-Vote" {
precommits = append(precommits, unpackVote(precommitStr))
}
}
fmt.Println(prevotes)
}
}
type Vote struct {
position int64
fingerprint string
block string
time time.Time
}
func unpackVote(vote string) Vote {
var v Vote
preFixedRemoved := strings.ReplaceAll(vote, "Vote{", "")
suffixFixedRemoved := strings.ReplaceAll(preFixedRemoved, "}", "")
components := strings.Split(suffixFixedRemoved, " ")
fingerprintComponents := strings.Split(components[0], ":")
position, err := strconv.ParseInt(fingerprintComponents[0], 10, 64)
if err != nil {
log.Fatalf("Cannot parse position from %s", vote)
}
v.position = int64(position)
v.fingerprint = fingerprintComponents[1]
v.block = components[2]
voteTime, err := time.Parse(time.RFC3339, components[5])
if err != nil {
log.Fatalf("Cannot parse position from %s", vote)
}
v.time = voteTime
return v
}