Skip to content

Commit 500b696

Browse files
committed
Add ExpectedJSONSize
1 parent 9e4b1e8 commit 500b696

File tree

4 files changed

+1350
-0
lines changed

4 files changed

+1350
-0
lines changed

types/ibc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,14 @@ type IBCReceiveResponse struct {
325325
// above)
326326
Events []Event `json:"events"`
327327
}
328+
329+
var (
330+
_ ExpectedJSONSize = IBCChannelOpenMsg{}
331+
_ ExpectedJSONSize = IBCChannelConnectMsg{}
332+
_ ExpectedJSONSize = IBCChannelCloseMsg{}
333+
_ ExpectedJSONSize = IBCPacketReceiveMsg{}
334+
_ ExpectedJSONSize = IBCPacketAckMsg{}
335+
_ ExpectedJSONSize = IBCPacketTimeoutMsg{}
336+
_ ExpectedJSONSize = IBCSourceCallbackMsg{}
337+
_ ExpectedJSONSize = IBCDestinationCallbackMsg{}
338+
)

types/json_size.go

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
package types
2+
3+
type ExpectedJSONSize interface {
4+
ExpectedJSONSize() int
5+
}
6+
7+
func ExpectedJSONSizeString(s string) int {
8+
// 2x quote + length of string + escaping overhead
9+
var out int = quotes + len(s)
10+
for _, r := range s {
11+
if r == '"' || r == '\\' {
12+
out += 1
13+
} else if r <= 0x1F {
14+
// control codes \u0000 - \u001f
15+
out += 5
16+
} else if r == '<' || r == '>' || r == '&' {
17+
// Go escapes HTML which is a bit pointless but legal
18+
// \u003c, \u003e, \u0026
19+
out += 5
20+
}
21+
}
22+
return out
23+
}
24+
25+
func ExpectedJSONSizeBinary(b []byte) int {
26+
if b == nil {
27+
return null
28+
}
29+
// padded base64 encoding (https://stackoverflow.com/questions/13378815/base64-length-calculation) plus quotes
30+
b64Len := ((4 * len(b) / 3) + 3) & ^3
31+
return b64Len + quotes
32+
}
33+
34+
func ExpectedJSONSizeInt(i int) int {
35+
out := 0
36+
// minus sign or zero
37+
if i <= 0 {
38+
i = -i
39+
out += 1
40+
}
41+
for i > 0 {
42+
i /= 10
43+
out += 1
44+
}
45+
return out
46+
}
47+
48+
func ExpectedJSONSizeUint64(i uint64) int {
49+
if i == 0 {
50+
return 1
51+
}
52+
53+
out := 0
54+
for i > 0 {
55+
i /= 10
56+
out += 1
57+
}
58+
return out
59+
}
60+
61+
func ExpectedJSONSizeBool(b bool) int {
62+
if b {
63+
return 4 // true
64+
} else {
65+
return 5 // false
66+
}
67+
}
68+
69+
// The size in bytes in JSON serialization
70+
const (
71+
brackets int = 2 // a pair of brackets {} or []
72+
quotes int = 2 // a pair of quotes ""
73+
comma int = 1 // ,
74+
colon int = 1 // :
75+
null int = 4 // null
76+
)
77+
78+
func (t IBCEndpoint) ExpectedJSONSize() int {
79+
// PortID string `json:"port_id"`
80+
// ChannelID string `json:"channel_id"`
81+
return brackets +
82+
9 + colon + ExpectedJSONSizeString(t.PortID) + comma +
83+
12 + colon + ExpectedJSONSizeString(t.ChannelID)
84+
}
85+
86+
func (t IBCChannel) ExpectedJSONSize() int {
87+
// Endpoint IBCEndpoint `json:"endpoint"`
88+
// CounterpartyEndpoint IBCEndpoint `json:"counterparty_endpoint"`
89+
// Order IBCOrder `json:"order"`
90+
// Version string `json:"version"`
91+
// ConnectionID string `json:"connection_id"`
92+
return brackets +
93+
10 + colon + t.Endpoint.ExpectedJSONSize() + comma +
94+
23 + colon + t.CounterpartyEndpoint.ExpectedJSONSize() + comma +
95+
7 + colon + ExpectedJSONSizeString(t.Order) + comma +
96+
9 + colon + ExpectedJSONSizeString(t.Version) + comma +
97+
15 + colon + ExpectedJSONSizeString(t.ConnectionID)
98+
}
99+
100+
func (t IBCOpenInit) ExpectedJSONSize() int {
101+
// Channel IBCChannel `json:"channel"`
102+
return brackets + 9 + colon + t.Channel.ExpectedJSONSize()
103+
}
104+
105+
func (t IBCOpenTry) ExpectedJSONSize() int {
106+
// Channel IBCChannel `json:"channel"`
107+
// CounterpartyVersion string `json:"counterparty_version"`
108+
return brackets +
109+
9 + colon + t.Channel.ExpectedJSONSize() + comma +
110+
22 + colon + ExpectedJSONSizeString(t.CounterpartyVersion)
111+
}
112+
113+
func (t IBCChannelOpenMsg) ExpectedJSONSize() int {
114+
// OpenInit *IBCOpenInit `json:"open_init,omitempty"`
115+
// OpenTry *IBCOpenTry `json:"open_try,omitempty"`
116+
out := brackets
117+
if t.OpenInit != nil {
118+
out += 11 + colon + t.OpenInit.ExpectedJSONSize()
119+
}
120+
if t.OpenInit != nil && t.OpenTry != nil {
121+
// this case should not happen for an enum but we don't have an error return type, so just handle it
122+
out += comma
123+
}
124+
if t.OpenTry != nil {
125+
out += 10 + colon + t.OpenTry.ExpectedJSONSize()
126+
}
127+
return out
128+
}
129+
130+
func (t IBCOpenAck) ExpectedJSONSize() int {
131+
// Channel IBCChannel `json:"channel"`
132+
// CounterpartyVersion string `json:"counterparty_version"`
133+
return brackets +
134+
9 + colon + t.Channel.ExpectedJSONSize() + comma +
135+
22 + colon + ExpectedJSONSizeString(t.CounterpartyVersion)
136+
}
137+
138+
func (t IBCOpenConfirm) ExpectedJSONSize() int {
139+
// Channel IBCChannel `json:"channel"`
140+
return brackets + 9 + colon + t.Channel.ExpectedJSONSize()
141+
}
142+
143+
func (t IBCChannelConnectMsg) ExpectedJSONSize() int {
144+
// OpenAck *IBCOpenAck `json:"open_ack,omitempty"`
145+
// OpenConfirm *IBCOpenConfirm `json:"open_confirm,omitempty"`
146+
out := brackets
147+
if t.OpenAck != nil {
148+
out += 10 + colon + t.OpenAck.ExpectedJSONSize()
149+
}
150+
if t.OpenAck != nil && t.OpenConfirm != nil {
151+
// this case should not happen for an enum but we don't have an error return type, so just handle it
152+
out += comma
153+
}
154+
if t.OpenConfirm != nil {
155+
out += 14 + colon + t.OpenConfirm.ExpectedJSONSize()
156+
}
157+
return out
158+
}
159+
160+
func (t IBCCloseInit) ExpectedJSONSize() int {
161+
// Channel IBCChannel `json:"channel"`
162+
return brackets + 9 + colon + t.Channel.ExpectedJSONSize()
163+
}
164+
165+
func (t IBCCloseConfirm) ExpectedJSONSize() int {
166+
// Channel IBCChannel `json:"channel"`
167+
return brackets + 9 + colon + t.Channel.ExpectedJSONSize()
168+
}
169+
170+
func (t IBCChannelCloseMsg) ExpectedJSONSize() int {
171+
// CloseInit *IBCCloseInit `json:"close_init,omitempty"`
172+
// CloseConfirm *IBCCloseConfirm `json:"close_confirm,omitempty"`
173+
out := brackets
174+
if t.CloseInit != nil {
175+
out += 12 + colon + t.CloseInit.ExpectedJSONSize()
176+
}
177+
if t.CloseInit != nil && t.CloseConfirm != nil {
178+
// this case should not happen for an enum but we don't have an error return type, so just handle it
179+
out += comma
180+
}
181+
if t.CloseConfirm != nil {
182+
out += 15 + colon + t.CloseConfirm.ExpectedJSONSize()
183+
}
184+
return out
185+
}
186+
187+
func (t IBCTimeoutBlock) ExpectedJSONSize() int {
188+
// Revision uint64 `json:"revision"`
189+
// Height uint64 `json:"height"`
190+
return brackets +
191+
10 + colon + ExpectedJSONSizeUint64(t.Revision) + comma +
192+
8 + colon + ExpectedJSONSizeUint64(t.Height)
193+
}
194+
195+
func (t IBCTimeout) ExpectedJSONSize() int {
196+
// Block *IBCTimeoutBlock `json:"block"`
197+
// Timestamp uint64 `json:"timestamp,string,omitempty"`
198+
out := brackets
199+
200+
if t.Block != nil {
201+
out += 7 + colon + t.Block.ExpectedJSONSize()
202+
} else {
203+
out += 7 + colon + null // Block never omitted
204+
}
205+
206+
if t.Timestamp != 0 {
207+
out += comma + 11 + colon + ExpectedJSONSizeUint64(t.Timestamp) + quotes
208+
}
209+
210+
return out
211+
}
212+
213+
func (t IBCPacket) ExpectedJSONSize() int {
214+
// Data []byte `json:"data"`
215+
// Src IBCEndpoint `json:"src"`
216+
// Dest IBCEndpoint `json:"dest"`
217+
// Sequence uint64 `json:"sequence"`
218+
// Timeout IBCTimeout `json:"timeout"`
219+
return brackets +
220+
6 + colon + ExpectedJSONSizeBinary(t.Data) + comma +
221+
5 + colon + t.Src.ExpectedJSONSize() + comma +
222+
6 + colon + t.Dest.ExpectedJSONSize() + comma +
223+
10 + colon + ExpectedJSONSizeUint64(t.Sequence) + comma +
224+
9 + colon + t.Timeout.ExpectedJSONSize()
225+
}
226+
227+
func (t IBCAcknowledgement) ExpectedJSONSize() int {
228+
// Data []byte `json:"data"`
229+
return brackets +
230+
6 + colon + ExpectedJSONSizeBinary(t.Data)
231+
}
232+
233+
func (t IBCPacketReceiveMsg) ExpectedJSONSize() int {
234+
// Packet IBCPacket `json:"packet"`
235+
// Relayer string `json:"relayer"`
236+
return brackets +
237+
8 + colon + t.Packet.ExpectedJSONSize() + comma +
238+
9 + colon + ExpectedJSONSizeString(t.Relayer)
239+
}
240+
241+
func (t IBCPacketAckMsg) ExpectedJSONSize() int {
242+
// Acknowledgement IBCAcknowledgement `json:"acknowledgement"`
243+
// OriginalPacket IBCPacket `json:"original_packet"`
244+
// Relayer string `json:"relayer"`
245+
return brackets +
246+
17 + colon + t.Acknowledgement.ExpectedJSONSize() + comma +
247+
17 + colon + t.OriginalPacket.ExpectedJSONSize() + comma +
248+
9 + colon + ExpectedJSONSizeString(t.Relayer)
249+
}
250+
251+
func (t IBCPacketTimeoutMsg) ExpectedJSONSize() int {
252+
// Packet IBCPacket `json:"packet"`
253+
// Relayer string `json:"relayer"`
254+
return brackets +
255+
8 + colon + t.Packet.ExpectedJSONSize() + comma +
256+
9 + colon + ExpectedJSONSizeString(t.Relayer)
257+
}
258+
259+
func (t IBCAckCallbackMsg) ExpectedJSONSize() int {
260+
// Acknowledgement IBCAcknowledgement `json:"acknowledgement"`
261+
// OriginalPacket IBCPacket `json:"original_packet"`
262+
// Relayer string `json:"relayer"`
263+
return brackets +
264+
17 + colon + t.Acknowledgement.ExpectedJSONSize() + comma +
265+
17 + colon + t.OriginalPacket.ExpectedJSONSize() + comma +
266+
9 + colon + ExpectedJSONSizeString(t.Relayer)
267+
}
268+
269+
func (t IBCTimeoutCallbackMsg) ExpectedJSONSize() int {
270+
// Packet IBCPacket `json:"packet"`
271+
// Relayer string `json:"relayer"`
272+
return brackets +
273+
8 + colon + t.Packet.ExpectedJSONSize() + comma +
274+
9 + colon + ExpectedJSONSizeString(t.Relayer)
275+
}
276+
277+
func (t IBCSourceCallbackMsg) ExpectedJSONSize() int {
278+
// Acknowledgement *IBCAckCallbackMsg `json:"acknowledgement,omitempty"`
279+
// Timeout *IBCTimeoutCallbackMsg `json:"timeout,omitempty"`
280+
out := brackets
281+
282+
if t.Acknowledgement != nil {
283+
out += 17 + colon + t.Acknowledgement.ExpectedJSONSize()
284+
}
285+
286+
if t.Acknowledgement != nil && t.Timeout != nil {
287+
out += comma
288+
}
289+
290+
if t.Timeout != nil {
291+
out += 9 + colon + t.Timeout.ExpectedJSONSize()
292+
}
293+
294+
return out
295+
}
296+
297+
func (t IBCDestinationCallbackMsg) ExpectedJSONSize() int {
298+
// Ack IBCAcknowledgement `json:"ack"`
299+
// Packet IBCPacket `json:"packet"`
300+
return brackets +
301+
5 + colon + t.Ack.ExpectedJSONSize() + comma +
302+
8 + colon + t.Packet.ExpectedJSONSize()
303+
}

0 commit comments

Comments
 (0)