-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
427 lines (395 loc) · 11.8 KB
/
tree_test.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package tree
import (
"encoding/json"
"fmt"
"github.com/cyphrme/coze"
)
func Example_IntToBytesLE() {
// Byte one boundary
fmt.Printf("0:%b\n", IntToBytesLE(0))
fmt.Printf("1:%b\n", IntToBytesLE(1))
fmt.Printf("2:%b\n", IntToBytesLE(2))
// Byte two boundary
fmt.Printf("255:%b\n", IntToBytesLE(255))
fmt.Printf("256:%b\n", IntToBytesLE(256))
fmt.Printf("257:%b\n", IntToBytesLE(257))
// Byte three boundary
fmt.Printf("65535:%b\n", IntToBytesLE(65535))
fmt.Printf("65536:%b\n", IntToBytesLE(65536))
fmt.Printf("65537:%b\n", IntToBytesLE(65537))
// 8 byte boundary
fmt.Printf("18446744073709551615:%b\n", IntToBytesLE(18446744073709551615))
// Output:
// 0:[0]
// 1:[1]
// 2:[10]
// 255:[11111111]
// 256:[0 1]
// 257:[1 1]
// 65535:[11111111 11111111]
// 65536:[0 0 1]
// 65537:[1 0 1]
// 18446744073709551615:[11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111]
}
func Example_intToBytesBE() {
// Byte one boundary
fmt.Printf("0:%b\n", IntToBytesBE(0))
fmt.Printf("1:%b\n", IntToBytesBE(1))
fmt.Printf("2:%b\n", IntToBytesBE(2))
// Byte two boundary
fmt.Printf("255:%b\n", IntToBytesBE(255))
fmt.Printf("256:%b\n", IntToBytesBE(256))
fmt.Printf("257:%b\n", IntToBytesBE(257))
// Byte three boundary
fmt.Printf("65535:%b\n", IntToBytesBE(65535))
fmt.Printf("65536:%b\n", IntToBytesBE(65536))
fmt.Printf("65537:%b\n", IntToBytesBE(65537))
// 8 byte boundary
fmt.Printf("18446744073709551615:%b\n", IntToBytesBE(18446744073709551615))
// Output:
// 0:[0]
// 1:[1]
// 2:[10]
// 255:[11111111]
// 256:[1 0]
// 257:[1 1]
// 65535:[11111111 11111111]
// 65536:[1 0 0]
// 65537:[1 0 1]
// 18446744073709551615:[11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111]
}
func ExampleTree_Populate() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{3},
}
t.Populate()
fmt.Println(t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 3
// ],
// "branches": [
// "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
// "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg",
// "nO49t9F-tf96Os0Eab4bqhnVywHbqJkaGlNgAACoRRI"
// ]
// }
}
// ExampleTree_Skip tests skipping in the tree. With a skip of 1, the branch
// value should match ExampleTree_Populate starting at the second value.
func ExampleTree_Skip() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{3},
Skip: 1,
}
t.Populate()
fmt.Println(t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 3
// ],
// "skip": 1,
// "branches": [
// "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg",
// "nO49t9F-tf96Os0Eab4bqhnVywHbqJkaGlNgAACoRRI"
// ]
// }
}
// ExampleTree_Populate_zero tests a zero sized tree.
func ExampleTree_Populate_zero() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{0},
PathCalc: true,
}
t.Populate()
fmt.Println(t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 0
// ],
// "path_calc": true
// }
}
// Tests the nonce 255/256, one byte to two bytes, boundary
func ExampleTree_Populate_nonceBoundary() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{257},
Skip: 255,
}
t.Populate()
fmt.Println(t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 257
// ],
// "skip": 255,
// "branches": [
// "1ILX9r1f7c3hK3Gwv_i5rmV-T7E4Sq0vjJsJE1J7SwM",
// "DLbAu75AbsA7GV2cxe_KRJiJvdOu3Jkbxt-Xbow5-LU"
// ]
// }
}
// ExampleTree_PathCalc_len tests calculating paths.
func ExampleTree_PathCalc_len() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{1, 2, 2},
PathCalc: true,
}
t.Populate()
fmt.Printf("Paths: %d, PathsID %d, Leaves: %d, LeavesID: %d\n", len(t.Paths), len(t.PathsID), len(t.Leaves), len(t.LeavesID))
t = Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{2},
PathCalc: true,
}
t.Populate()
fmt.Printf("Paths: %d, PathsID %d, Leaves: %d, LeavesID: %d\n", len(t.Paths), len(t.PathsID), len(t.Leaves), len(t.LeavesID))
//fmt.Println(t)
// Output:
// Paths: 7, PathsID 7, Leaves: 4, LeavesID: 4
// Paths: 2, PathsID 2, Leaves: 2, LeavesID: 2
}
func Example_big() {
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{2, 2},
}
err := t.Populate()
if err != nil {
fmt.Println(err)
}
// fmt.Printf("%+v\n", t)
fmt.Printf("%s\n", t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 2,
// 2
// ],
// "branches": [
// "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
// "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg"
// ],
// "children": [
// {
// "alg": "SHA-256",
// "seed": "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
// "id": "FBrxbr4wZPHuGyM2Nl7gzoZTfZ-oLQP4pwKBpDxIIb0",
// "branch_level_sizes": [
// 2
// ],
// "branches": [
// "0RJw1SSIXkrW3SID6eoANHq7Y1gB6ZNKo0nuC8pL_qM",
// "yZJ4i_WzjbPHrbczNt2AW-zk1mDJE77ps98jN9fYaFA"
// ]
// },
// {
// "alg": "SHA-256",
// "seed": "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg",
// "id": "DG6CXAv4yPV4Ebs1igPW1uZspBO5e5QW9BmY7dJ_lv8",
// "branch_level_sizes": [
// 2
// ],
// "branches": [
// "IVhtm0XLC2K_pp0P6PYESAfQ8qJJuerxVR9DHu5mozA",
// "JokEZgl_qDf7Nkf4z5AQdfeUOBSW0DqbWdRdmE4afl0"
// ]
// }
// ]
// }
}
func Example_max() {
max := 3
t := Tree{
Alg: coze.SHA256,
Seed: coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
BLS: []int{2, 2},
MaxTotalLeaves: &max,
}
err := t.Populate()
if err != nil {
fmt.Println(err)
}
// fmt.Printf("%+v\n", t)
fmt.Printf("%s\n", t)
// Output:
// {
// "alg": "SHA-256",
// "seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
// "branch_level_sizes": [
// 2,
// 2
// ],
// "branches": [
// "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
// "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg"
// ],
// "children": [
// {
// "alg": "SHA-256",
// "seed": "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
// "id": "FBrxbr4wZPHuGyM2Nl7gzoZTfZ-oLQP4pwKBpDxIIb0",
// "branch_level_sizes": [
// 2
// ],
// "branches": [
// "0RJw1SSIXkrW3SID6eoANHq7Y1gB6ZNKo0nuC8pL_qM",
// "yZJ4i_WzjbPHrbczNt2AW-zk1mDJE77ps98jN9fYaFA"
// ]
// },
// {
// "alg": "SHA-256",
// "seed": "sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg",
// "id": "DG6CXAv4yPV4Ebs1igPW1uZspBO5e5QW9BmY7dJ_lv8",
// "branch_level_sizes": [
// 2
// ],
// "branches": [
// "IVhtm0XLC2K_pp0P6PYESAfQ8qJJuerxVR9DHu5mozA",
// ""
// ]
// }
// ]
// }
}
// ExampleB64Map_jsonMarshal does a round trip Marshal and Unmarshal. Only one
// key is used for testing since since Go maps are unordered and this test uses
// a static string.
func ExampleB64Map_jsonMarshal() {
lp := B64Map{
SB64(coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno")): []coze.B64{
coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
coze.MustDecode("zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0")},
}
b, err := coze.MarshalPretty(lp)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
// Output:
// {
// "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno": [
// "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0"
// ]
// }
}
// ExampleB64Map_jsonUnmarshal tests B64Map JSON unmarshalling. Only one key is
// unmarshalled since Go maps are unordered and the test uses a static string.
func ExampleB64Map_jsonUnmarshal() {
sB64Map := `{
"RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno": [
"RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
"zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0"
]
}`
lp := new(B64Map)
err := json.Unmarshal([]byte(sB64Map), lp)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", lp)
// Output:
// &map[UnBNTTRfbFU2akNqM2FzWkV0SUZ5WXFQakMyTDZtbHVjbDdWR012QXVubw:[RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0]]
}
// ExampleTree_jsonUnmarshal tests tree JSON unmarshalling.
func ExampleTree_jsonUnmarshal() {
// Generate the following using:
// ts, _ := NewTreePopulated(coze.SHA256, coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"), []int{1, 2})
tb := []byte(`{
"alg": "SHA-256",
"seed": "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
"id": "JArIzKVsB7CBjQ9zqB-Y1RbDdET2Bi-oyWADoVIUJ94",
"branch_level_sizes": [
1,
2
],
"branches": [
"JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4"
],
"children": [
{
"alg": "SHA-256",
"seed": "JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4",
"id": "FBrxbr4wZPHuGyM2Nl7gzoZTfZ-oLQP4pwKBpDxIIb0",
"branch_level_sizes": [
2
],
"branches": [
"0RJw1SSIXkrW3SID6eoANHq7Y1gB6ZNKo0nuC8pL_qM",
"yZJ4i_WzjbPHrbczNt2AW-zk1mDJE77ps98jN9fYaFA"
]
}
]
}`)
t := new(Tree)
err := json.Unmarshal(tb, t)
if err != nil {
panic(err)
}
ts := fmt.Sprintf("%s", t)
fmt.Println(string(tb) == ts)
// Output:
//true
}
func ExampleNewTreePopulated_branches() {
t, _ := NewTreePopulated(coze.SHA256, coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"), []int{3})
fmt.Println(t.Branches)
// Output:
// [JmXqHaRJWmRD8qLK6yPyAfH-jiJ9EDJXQsUnAwO5ot4 sGYg2-Wl8uE4OSaYWk2v7pBj7pYNHrl0K-pxWG6_YDg nO49t9F-tf96Os0Eab4bqhnVywHbqJkaGlNgAACoRRI]
}
// ExampleTree_printSB64 ensures SB64 and B64Map/B64MapP prints out correctly.
func ExampleTree_printSB64() {
t2 := Tree{
Paths: B64MapP{
SB64(coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno")): &[]coze.B64{
coze.MustDecode("RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno"),
coze.MustDecode("zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0")},
},
}
fmt.Printf("%s\n", t2)
// Output:
// {
// "alg": "",
// "seed": "",
// "id": "",
// "paths": {
// "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno": [
// "RpMM4_lU6jCj3asZEtIFyYqPjC2L6mlucl7VGMvAuno",
// "zVzgRU3WFpnrlVJAnI4ZU1Od4Agl5Zd4jIP79oubOW0"
// ]
// }
// }
}