1
1
import { HashObject } from "@chainsafe/as-sha256" ;
2
2
import { expect } from "chai" ;
3
3
import { LeafNode , Node } from "../../src" ;
4
- import { packedNodeRootsToBytes , packedRootsBytesToLeafNodes } from "../../src/packedNode" ;
4
+ import { packedNodeRootsToBytes , packedRootsBytesToLeafNodes , packedUintNum64sToLeafNodes } from "../../src/packedNode" ;
5
5
6
6
describe ( "subtree / packedNode single node" , ( ) => {
7
7
const testCases : {
8
8
id : string ;
9
9
size : number ;
10
10
nodes : Node [ ] ;
11
11
outStr : string ;
12
+ testPackedNumbers ?: boolean ;
12
13
} [ ] = [
13
14
{
14
15
id : "One byte" ,
@@ -48,11 +49,37 @@ describe("subtree / packedNode single node", () => {
48
49
nodes : [ LeafNode . fromHashObject ( { h0 : 0x0708090a , h1 : 0x01020304 , h2 : 0 , h3 : 0 , h4 : 0 , h5 : 0 , h6 : 0 , h7 : 0 } ) ] ,
49
50
outStr : "0x0a09080704030201" ,
50
51
} ,
52
+ {
53
+ id : "2 h values fits uint64 number" ,
54
+ size : 8 ,
55
+ nodes : [ LeafNode . fromHashObject ( { h0 : 0x0708090a , h1 : 0x0102030 , h2 : 0 , h3 : 0 , h4 : 0 , h5 : 0 , h6 : 0 , h7 : 0 } ) ] ,
56
+ outStr : "0x0a09080730201000" ,
57
+ testPackedNumbers : true ,
58
+ } ,
51
59
{
52
60
id : "32 bytes zero" ,
53
61
size : 32 ,
54
62
nodes : [ LeafNode . fromHashObject ( { h0 : 0 , h1 : 0 , h2 : 0 , h3 : 0 , h4 : 0 , h5 : 0 , h6 : 0 , h7 : 0 } ) ] ,
55
63
outStr : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
64
+ testPackedNumbers : true ,
65
+ } ,
66
+ {
67
+ id : "32 bytes max" ,
68
+ size : 32 ,
69
+ nodes : [
70
+ LeafNode . fromHashObject ( {
71
+ h0 : 0xffffffff ,
72
+ h1 : 0xffffffff ,
73
+ h2 : 0xffffffff ,
74
+ h3 : 0xffffffff ,
75
+ h4 : 0xffffffff ,
76
+ h5 : 0xffffffff ,
77
+ h6 : 0xffffffff ,
78
+ h7 : 0xffffffff ,
79
+ } ) ,
80
+ ] ,
81
+ outStr : "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ,
82
+ testPackedNumbers : true ,
56
83
} ,
57
84
{
58
85
id : "32 bytes same" ,
@@ -77,9 +104,82 @@ describe("subtree / packedNode single node", () => {
77
104
] ,
78
105
outStr : "0x18735c375d8e7b2922ef64f165d10b9ace103f326627f0a21d0fe8a0a573f83f" ,
79
106
} ,
107
+ // same to random tests but trim h1, h3, h5, h7 to make h{2*i} + h{2*i + 1} fits uint64
108
+ {
109
+ id : "8 bytes random fits unit64 number" ,
110
+ size : 32 ,
111
+ nodes : [
112
+ LeafNode . fromHashObject ( {
113
+ h0 : 928805656 ,
114
+ h1 : 6959632 ,
115
+ h2 : 0 ,
116
+ h3 : 0 ,
117
+ h4 : 0 ,
118
+ h5 : 0 ,
119
+ h6 : 0 ,
120
+ h7 : 0 ,
121
+ } ) ,
122
+ ] ,
123
+ outStr : "0x18735c3710326a00000000000000000000000000000000000000000000000000" ,
124
+ testPackedNumbers : true ,
125
+ } ,
126
+ {
127
+ id : "16 bytes random fits unit64 number" ,
128
+ size : 32 ,
129
+ nodes : [
130
+ LeafNode . fromHashObject ( {
131
+ h0 : 928805656 ,
132
+ h1 : 6959632 ,
133
+ h2 : 4049923874 ,
134
+ h3 : 258446 ,
135
+ h4 : 0 ,
136
+ h5 : 0 ,
137
+ h6 : 0 ,
138
+ h7 : 0 ,
139
+ } ) ,
140
+ ] ,
141
+ outStr : "0x18735c3710326a0022ef64f18ef1030000000000000000000000000000000000" ,
142
+ testPackedNumbers : true ,
143
+ } ,
144
+ {
145
+ id : "24 bytes random fits unit64 number" ,
146
+ size : 32 ,
147
+ nodes : [
148
+ LeafNode . fromHashObject ( {
149
+ h0 : 928805656 ,
150
+ h1 : 6959632 ,
151
+ h2 : 4049923874 ,
152
+ h3 : 258446 ,
153
+ h4 : 842993870 ,
154
+ h5 : 273364 ,
155
+ h6 : 0 ,
156
+ h7 : 0 ,
157
+ } ) ,
158
+ ] ,
159
+ outStr : "0x18735c3710326a0022ef64f18ef10300ce103f32d42b04000000000000000000" ,
160
+ testPackedNumbers : true ,
161
+ } ,
162
+ {
163
+ id : "32 bytes random fits unit64 number" ,
164
+ size : 32 ,
165
+ nodes : [
166
+ LeafNode . fromHashObject ( {
167
+ h0 : 928805656 ,
168
+ h1 : 6959632 ,
169
+ h2 : 4049923874 ,
170
+ h3 : 258446 ,
171
+ h4 : 842993870 ,
172
+ h5 : 273364 ,
173
+ h6 : 2699562781 ,
174
+ h7 : 107324 ,
175
+ } ) ,
176
+ ] ,
177
+ outStr : "0x18735c3710326a0022ef64f18ef10300ce103f32d42b04001d0fe8a03ca30100" ,
178
+ testPackedNumbers : true ,
179
+ } ,
80
180
] ;
81
181
82
- for ( const { id, size, nodes, outStr} of testCases ) {
182
+ for ( const { id, size, nodes, outStr, testPackedNumbers } of testCases ) {
83
183
it ( `${ id } - packedNodeRootsToBytes` , ( ) => {
84
184
const uint8Array = new Uint8Array ( size ) ;
85
185
const dataView = new DataView ( uint8Array . buffer , uint8Array . byteOffset , uint8Array . byteLength ) ;
@@ -93,6 +193,28 @@ describe("subtree / packedNode single node", () => {
93
193
const nodesRes = packedRootsBytesToLeafNodes ( dataView , 0 , size ) ;
94
194
expect ( onlyHashObject ( nodesRes [ 0 ] . rootHashObject ) ) . to . deep . equal ( onlyHashObject ( nodes [ 0 ] . rootHashObject ) ) ;
95
195
} ) ;
196
+
197
+ // 1 UintNum64 = 8 bytes
198
+ if ( testPackedNumbers ) {
199
+ const NUMBER_2_POW_32 = 2 ** 32 ;
200
+ it ( `${ id } - packedUintNum64sToLeafNodes, value size=${ Math . floor ( size / 8 ) } ` , ( ) => {
201
+ const values : number [ ] = [ ] ;
202
+ const uint8Array = new Uint8Array ( Buffer . from ( outStr . replace ( "0x" , "" ) , "hex" ) ) ;
203
+ const dataView = new DataView ( uint8Array . buffer , uint8Array . byteOffset , uint8Array . byteLength ) ;
204
+ for ( let i = 0 ; i < size ; i += 8 ) {
205
+ const a = dataView . getUint32 ( i , true ) ;
206
+ const b = dataView . getUint32 ( i + 4 , true ) ;
207
+ if ( a === 0xffffffff && b === 0xffffffff ) {
208
+ values . push ( Infinity ) ;
209
+ } else {
210
+ values . push ( b * NUMBER_2_POW_32 + a ) ;
211
+ }
212
+ }
213
+
214
+ const nodesRes = packedUintNum64sToLeafNodes ( values ) ;
215
+ expect ( onlyHashObject ( nodesRes [ 0 ] . rootHashObject ) ) . to . deep . equal ( onlyHashObject ( nodes [ 0 ] . rootHashObject ) ) ;
216
+ } ) ;
217
+ }
96
218
}
97
219
} ) ;
98
220
0 commit comments