forked from blockwatch-cc/tzstats-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq.go
185 lines (168 loc) · 3.38 KB
/
zmq.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: [email protected]
package tzstats
import (
"context"
"encoding/json"
"fmt"
"blockwatch.cc/tzgo/tezos"
)
type ZmqMessage struct {
topic string
body []byte
fields []string
}
func NewZmqMessage(topic, body []byte) *ZmqMessage {
return &ZmqMessage{string(topic), body, nil}
}
func (m *ZmqMessage) GetField(name string) (string, bool) {
return getTableColumn(m.body, zmqFields(m.topic), name)
}
func (m *ZmqMessage) DecodeOpHash() (tezos.OpHash, error) {
return tezos.ParseOpHash(string(m.body))
}
func (m *ZmqMessage) DecodeBlockHash() (tezos.BlockHash, error) {
return tezos.ParseBlockHash(string(m.body))
}
func (m *ZmqMessage) DecodeOp() (*Op, error) {
o := new(Op).WithColumns(ZmqRawOpColumns...)
if err := json.Unmarshal(m.body, o); err != nil {
return nil, err
}
return o, nil
}
func (m *ZmqMessage) DecodeOpWithScript(ctx context.Context, c *Client) (*Op, error) {
o := new(Op).WithColumns(ZmqRawOpColumns...)
// we may need contract scripts
if is, ok := m.GetField("is_contract"); ok && is == "1" {
recv, ok := m.GetField("receiver")
if ok && recv != "" && recv != "null" {
addr, err := tezos.ParseAddress(recv)
if err != nil {
return nil, fmt.Errorf("decode: invalid receiver address %s: %v, %#v", recv, err, string(m.body))
}
// load contract type info (required for decoding storage/param data)
script, err := c.loadCachedContractScript(ctx, addr)
if err != nil {
return nil, err
}
o = o.WithScript(script)
}
}
if err := json.Unmarshal(m.body, o); err != nil {
return nil, err
}
return o, nil
}
func (m *ZmqMessage) DecodeBlock() (*Block, error) {
b := new(Block).WithColumns(ZmqRawBlockColumns...)
if err := json.Unmarshal(m.body, b); err != nil {
return nil, err
}
return b, nil
}
func (m *ZmqMessage) DecodeStatus() (*Status, error) {
s := new(Status).WithColumns(ZmqStatusColumns...)
if err := json.Unmarshal(m.body, s); err != nil {
return nil, err
}
return s, nil
}
func zmqFields(topic string) []string {
switch topic {
case "raw_block", "raw_block/rollback":
return ZmqRawBlockColumns
case "raw_op", "raw_op/rollback":
return ZmqRawOpColumns
case "status":
return ZmqStatusColumns
default:
return nil
}
}
var ZmqRawBlockColumns = []string{
"row_id",
"hash",
"predecessor",
"height",
"cycle",
"time",
"solvetime",
"version",
"round",
"nonce",
"voting_period_kind",
"baker",
"proposer",
"n_ops_applied",
"n_ops_failed",
"n_contract_calls",
"n_events",
"volume",
"fee",
"reward",
"deposit",
"activated_supply",
"burned_supply",
"minted_supply",
"n_accounts",
"n_new_accounts",
"n_new_contracts",
"n_cleared_accounts",
"n_funded_accounts",
"gas_limit",
"gas_used",
"storage_paid",
"lb_esc_vote",
"lb_esc_ema",
"protocol",
}
var ZmqRawOpColumns = []string{
"row_id",
"type",
"hash",
"block",
"height",
"cycle",
"time",
"op_n",
"op_p",
"op_c",
"op_i",
"status",
"is_success",
"is_contract",
"is_internal",
"is_event",
"counter",
"gas_limit",
"gas_used",
"storage_limit",
"storage_paid",
"volume",
"fee",
"reward",
"deposit",
"burned",
"sender_id",
"sender",
"receiver_id",
"receiver",
"creator_id",
"creator",
"baker_id",
"baker",
"data",
"parameters",
"storage",
"big_map_diff",
"errors",
"entrypoint",
}
var ZmqStatusColumns = []string{
"status",
"blocks",
"finalized",
"indexed",
"progress",
}